aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video
diff options
context:
space:
mode:
authorakpm@osdl.org <akpm@osdl.org>2005-11-09 00:37:07 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2005-11-09 10:56:15 -0500
commita6c2ba283565dbc9f055dcb2ecba1971460bb535 (patch)
treefd262f8af5bffc99753ff861be85574616113467 /drivers/media/video
parent4b017415fc9ab63f7c0e49aced5e403c18d55659 (diff)
[PATCH] v4l: 716: support for em28xx board family
- Added support for em28xx board family Signed-off-by: Ludovico Cavedon <cavedon@sssup.it> Signed-off-by: Markus Rechberger <mrechberger@gmail.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@brturbo.com.br> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/media/video')
-rw-r--r--drivers/media/video/em28xx/em28xx-cards.c168
-rw-r--r--drivers/media/video/em28xx/em28xx-core.c807
-rw-r--r--drivers/media/video/em28xx/em28xx-i2c.c443
-rw-r--r--drivers/media/video/em28xx/em28xx-video.c1815
-rw-r--r--drivers/media/video/em28xx/em28xx.h479
-rw-r--r--drivers/media/video/tvp5150.c337
6 files changed, 3938 insertions, 111 deletions
diff --git a/drivers/media/video/em28xx/em28xx-cards.c b/drivers/media/video/em28xx/em28xx-cards.c
new file mode 100644
index 000000000000..b6d89decfbd8
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx-cards.c
@@ -0,0 +1,168 @@
1/*
2 em2820-cards.c - driver for Empia EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/pci.h>
28#include <linux/delay.h>
29#include <linux/i2c.h>
30#include <linux/usb.h>
31#include <media/tuner.h>
32#include "audiochip.h"
33#include "tveeprom.h"
34#include "msp3400.h"
35
36#include "em2820.h"
37
38enum em2820_board_entry {
39 EM2820_BOARD_TERRATEC_CINERGY_250,
40 EM2820_BOARD_PINNACLE_USB_2,
41 EM2820_BOARD_HAUPPAUGE_WINTV_USB_2,
42 EM2820_BOARD_MSI_VOX_USB_2
43};
44
45struct em2820_board em2820_boards[] = {
46 [EM2820_BOARD_TERRATEC_CINERGY_250] = {
47 .name = "Terratec Cinergy 250 USB",
48 .vchannels = 3,
49 .norm = VIDEO_MODE_PAL,
50 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
51 .tda9887_conf = TDA9887_PRESENT,
52 .has_tuner = 1,
53 .decoder = EM2820_SAA7113,
54 .input = {{
55 .type = EM2820_VMUX_TELEVISION,
56 .vmux = 2,
57 .amux = 0,
58 },{
59 .type = EM2820_VMUX_COMPOSITE1,
60 .vmux = 0,
61 .amux = 1,
62 },{
63 .type = EM2820_VMUX_SVIDEO,
64 .vmux = 9,
65 .amux = 1,
66 }},
67 },
68 [EM2820_BOARD_PINNACLE_USB_2] = {
69 .name = "Pinnacle PCTV USB 2",
70 .vchannels = 3,
71 .norm = VIDEO_MODE_PAL,
72 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
73 .tda9887_conf = TDA9887_PRESENT,
74 .has_tuner = 1,
75 .decoder = EM2820_SAA7113,
76 .input = {{
77 .type = EM2820_VMUX_TELEVISION,
78 .vmux = 2,
79 .amux = 0,
80 },{
81 .type = EM2820_VMUX_COMPOSITE1,
82 .vmux = 0,
83 .amux = 1,
84 },{
85 .type = EM2820_VMUX_SVIDEO,
86 .vmux = 9,
87 .amux = 1,
88 }},
89 },
90 [EM2820_BOARD_HAUPPAUGE_WINTV_USB_2] = {
91 .name = "Hauppauge WinTV USB 2",
92 .vchannels = 3,
93 .norm = VIDEO_MODE_NTSC,
94 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
95 .tda9887_conf = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE,
96 .has_tuner = 1,
97 .decoder = EM2820_TVP5150,
98 .has_msp34xx = 1,
99 /*FIXME: S-Video not tested */
100 .input = {{
101 .type = EM2820_VMUX_TELEVISION,
102 .vmux = 0,
103 .amux = 0,
104 },{
105 .type = EM2820_VMUX_SVIDEO,
106 .vmux = 2,
107 .amux = 1,
108 }},
109 },
110 [EM2820_BOARD_MSI_VOX_USB_2] = {
111 .name = "MSI VOX USB 2.0",
112 .vchannels = 3,
113 .norm = VIDEO_MODE_PAL,
114 .tuner_type = TUNER_PHILIPS_PAL,
115 .tda9887_conf = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE,
116 .has_tuner = 1,
117 .decoder = EM2820_SAA7114,
118 .input = {{
119 .type = EM2820_VMUX_TELEVISION,
120 .vmux = 2,
121 .amux = 0,
122 },{
123 .type = EM2820_VMUX_COMPOSITE1,
124 .vmux = 0,
125 .amux = 1,
126 },{
127 .type = EM2820_VMUX_SVIDEO,
128 .vmux = 9,
129 .amux = 1,
130 }},
131 },
132 { } /* Terminating entry */
133};
134
135/* table of devices that work with this driver */
136struct usb_device_id em2820_id_table [] = {
137 /* Terratec Cinerhy 200 USB: em2800 nor supported, at the moment */
138 /* { USB_DEVICE(0xeb1a, 0x2800), .driver_info = EM2800_BOARD_TERRATEC_CINERGY_200 }, */
139 { USB_DEVICE(0x0ccd, 0x0036), .driver_info = EM2820_BOARD_TERRATEC_CINERGY_250 },
140 { USB_DEVICE(0x2304, 0x0208), .driver_info = EM2820_BOARD_PINNACLE_USB_2 },
141 { USB_DEVICE(0x2040, 0x4200), .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
142 { USB_DEVICE(0xeb1a, 0x2820), .driver_info = EM2820_BOARD_MSI_VOX_USB_2 },
143 { },
144};
145
146void em2820_card_setup(struct em2820 *dev)
147{
148 /* request some modules */
149 if (dev->model == EM2820_BOARD_HAUPPAUGE_WINTV_USB_2) {
150 struct tveeprom tv;
151#ifdef CONFIG_MODULES
152 request_module("tveeprom");
153#endif
154 /* Call first TVeeprom */
155
156 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, dev->eedata);
157
158 dev->tuner_type= tv.tuner_type;
159 if (tv.audio_processor == AUDIO_CHIP_MSP34XX) {
160 dev->has_msp34xx=1;
161 } else dev->has_msp34xx=0;
162 }
163}
164
165EXPORT_SYMBOL(em2820_boards);
166EXPORT_SYMBOL(em2820_id_table);
167
168MODULE_DEVICE_TABLE (usb, em2820_id_table);
diff --git a/drivers/media/video/em28xx/em28xx-core.c b/drivers/media/video/em28xx/em28xx-core.c
new file mode 100644
index 000000000000..a75a91da70e9
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx-core.c
@@ -0,0 +1,807 @@
1/*
2 em2820-core.c - driver for Empia EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/videodev.h>
30#include <linux/usb.h>
31#include <linux/vmalloc.h>
32
33#include "em2820.h"
34
35/* #define ENABLE_DEBUG_ISOC_FRAMES */
36
37unsigned int core_debug = 0;
38module_param(core_debug,int,0644);
39MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
40
41#define em2820_coredbg(fmt, arg...) do {\
42 if (core_debug) \
43 printk(KERN_INFO "%s %s :"fmt, \
44 dev->name, __FUNCTION__ , ##arg); } while (0)
45
46unsigned int reg_debug = 0;
47module_param(reg_debug,int,0644);
48MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
49
50#define em2820_regdbg(fmt, arg...) do {\
51 if (reg_debug) \
52 printk(KERN_INFO "%s %s :"fmt, \
53 dev->name, __FUNCTION__ , ##arg); } while (0)
54
55unsigned int isoc_debug = 0;
56module_param(isoc_debug,int,0644);
57MODULE_PARM_DESC(core_debug,"enable debug messages [isoc transfers]");
58
59#define em2820_isocdbg(fmt, arg...) do {\
60 if (isoc_debug) \
61 printk(KERN_INFO "%s %s :"fmt, \
62 dev->name, __FUNCTION__ , ##arg); } while (0)
63
64static int alt = EM2820_PINOUT;
65module_param(alt, int, 0644);
66MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
67
68/* ------------------------------------------------------------------ */
69/* debug help functions */
70
71static const char *v4l1_ioctls[] = {
72 "0", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
73 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
74 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
75 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
76 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
77#define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
78
79static const char *v4l2_ioctls[] = {
80 "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
81 "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
82 "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
83 "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
84 "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
85 "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
86 "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
87 "44", "45", "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
88 "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
89 "S_MODULATOR"
90};
91#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
92
93void em2820_print_ioctl(char *name, unsigned int cmd)
94{
95 char *dir;
96
97 switch (_IOC_DIR(cmd)) {
98 case _IOC_NONE: dir = "--"; break;
99 case _IOC_READ: dir = "r-"; break;
100 case _IOC_WRITE: dir = "-w"; break;
101 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
102 default: dir = "??"; break;
103 }
104 switch (_IOC_TYPE(cmd)) {
105 case 'v':
106 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
107 name, cmd, dir, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
108 v4l1_ioctls[_IOC_NR(cmd)] : "???");
109 break;
110 case 'V':
111 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
112 name, cmd, dir, (_IOC_NR(cmd) < V4L2_IOCTLS) ?
113 v4l2_ioctls[_IOC_NR(cmd)] : "???");
114 break;
115 default:
116 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
117 name, cmd, dir, _IOC_NR(cmd));
118 }
119}
120
121static void *rvmalloc(size_t size)
122{
123 void *mem;
124 unsigned long adr;
125
126 size = PAGE_ALIGN(size);
127
128 mem = vmalloc_32((unsigned long)size);
129 if (!mem)
130 return NULL;
131
132 memset(mem, 0, size);
133
134 adr = (unsigned long)mem;
135 while (size > 0) {
136 SetPageReserved(vmalloc_to_page((void *)adr));
137 adr += PAGE_SIZE;
138 size -= PAGE_SIZE;
139 }
140
141 return mem;
142}
143
144static void rvfree(void *mem, size_t size)
145{
146 unsigned long adr;
147
148 if (!mem)
149 return;
150
151 size = PAGE_ALIGN(size);
152
153 adr = (unsigned long)mem;
154 while (size > 0) {
155 ClearPageReserved(vmalloc_to_page((void *)adr));
156 adr += PAGE_SIZE;
157 size -= PAGE_SIZE;
158 }
159
160 vfree(mem);
161}
162
163/*
164 * em2820_request_buffers()
165 * allocate a number of buffers
166 */
167u32 em2820_request_buffers(struct em2820 *dev, u32 count)
168{
169 const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
170 void *buff = NULL;
171 u32 i;
172 em2820_coredbg("requested %i buffers with size %i", count, imagesize);
173 if (count > EM2820_NUM_FRAMES)
174 count = EM2820_NUM_FRAMES;
175
176 dev->num_frames = count;
177 while (dev->num_frames > 0) {
178 if ((buff = rvmalloc(dev->num_frames * imagesize)))
179 break;
180 dev->num_frames--;
181 }
182
183 for (i = 0; i < dev->num_frames; i++) {
184 dev->frame[i].bufmem = buff + i * imagesize;
185 dev->frame[i].buf.index = i;
186 dev->frame[i].buf.m.offset = i * imagesize;
187 dev->frame[i].buf.length = dev->frame_size;
188 dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
189 dev->frame[i].buf.sequence = 0;
190 dev->frame[i].buf.field = V4L2_FIELD_NONE;
191 dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
192 dev->frame[i].buf.flags = 0;
193 }
194 return dev->num_frames;
195}
196
197/*
198 * em2820_queue_unusedframes()
199 * add all frames that are not currently in use to the inbuffer queue
200 */
201void em2820_queue_unusedframes(struct em2820 *dev)
202{
203 unsigned long lock_flags;
204 u32 i;
205
206 for (i = 0; i < dev->num_frames; i++)
207 if (dev->frame[i].state == F_UNUSED) {
208 dev->frame[i].state = F_QUEUED;
209 spin_lock_irqsave(&dev->queue_lock, lock_flags);
210 list_add_tail(&dev->frame[i].frame, &dev->inqueue);
211 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
212 }
213}
214
215/*
216 * em2820_release_buffers()
217 * free frame buffers
218 */
219void em2820_release_buffers(struct em2820 *dev)
220{
221 if (dev->num_frames) {
222 rvfree(dev->frame[0].bufmem,
223 dev->num_frames * PAGE_ALIGN(dev->frame[0].buf.length));
224 dev->num_frames = 0;
225 }
226}
227
228/*
229 * em2820_read_reg_req()
230 * reads data from the usb device specifying bRequest
231 */
232int em2820_read_reg_req_len(struct em2820 *dev, u8 req, u16 reg,
233 char *buf, int len)
234{
235 int ret, byte;
236
237 em2820_regdbg("req=%02x, reg=%02x ", req, reg);
238
239 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
240 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
241 0x0000, reg, buf, len, HZ);
242
243 if (reg_debug){
244 printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
245 for (byte = 0; byte < len; byte++) {
246 printk(" %02x", buf[byte]);
247 }
248 printk("\n");
249 }
250
251 return ret;
252}
253
254/*
255 * em2820_read_reg_req()
256 * reads data from the usb device specifying bRequest
257 */
258int em2820_read_reg_req(struct em2820 *dev, u8 req, u16 reg)
259{
260 u8 val;
261 int ret;
262
263 em2820_regdbg("req=%02x, reg=%02x:", req, reg);
264
265 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
266 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
267 0x0000, reg, &val, 1, HZ);
268
269 if (reg_debug)
270 printk(ret < 0 ? " failed!\n" : "%02x\n", val);
271
272 if (ret < 0)
273 return ret;
274
275 return val;
276}
277
278int em2820_read_reg(struct em2820 *dev, u16 reg)
279{
280 return em2820_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
281}
282
283/*
284 * em2820_write_regs_req()
285 * sends data to the usb device, specifying bRequest
286 */
287int em2820_write_regs_req(struct em2820 *dev, u8 req, u16 reg, char *buf,
288 int len)
289{
290 int ret;
291
292 /*usb_control_msg seems to expect a kmalloced buffer */
293 unsigned char *bufs = kmalloc(len, GFP_KERNEL);
294
295 em2820_regdbg("req=%02x reg=%02x:", req, reg);
296
297 if (reg_debug) {
298 int i;
299 for (i = 0; i < len; ++i)
300 printk (" %02x", (unsigned char)buf[i]);
301 printk ("\n");
302 }
303
304 if (!bufs)
305 return -ENOMEM;
306 memcpy(bufs, buf, len);
307 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
308 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
309 0x0000, reg, bufs, len, HZ);
310 mdelay(5); /* FIXME: magic number */
311 kfree(bufs);
312 return ret;
313}
314
315int em2820_write_regs(struct em2820 *dev, u16 reg, char *buf, int len)
316{
317 return em2820_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
318}
319
320/*
321 * em2820_write_reg_bits()
322 * sets only some bits (specified by bitmask) of a register, by first reading
323 * the actual value
324 */
325int em2820_write_reg_bits(struct em2820 *dev, u16 reg, u8 val,
326 u8 bitmask)
327{
328 int oldval;
329 u8 newval;
330 if ((oldval = em2820_read_reg(dev, reg)) < 0)
331 return oldval;
332 newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
333 return em2820_write_regs(dev, reg, &newval, 1);
334}
335
336/*
337 * em2820_write_ac97()
338 * write a 16 bit value to the specified AC97 address (LSB first!)
339 */
340int em2820_write_ac97(struct em2820 *dev, u8 reg, u8 * val)
341{
342 int ret;
343 u8 addr = reg & 0x7f;
344 if ((ret = em2820_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
345 return ret;
346 if ((ret = em2820_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
347 return ret;
348 if ((ret = em2820_read_reg(dev, AC97BUSY_REG)) < 0)
349 return ret;
350 else if (((u8) ret) & 0x01) {
351 em2820_warn ("AC97 command still being exectuted: not handled properly!\n");
352 }
353 return 0;
354}
355
356int em2820_audio_analog_set(struct em2820 *dev)
357{
358 char s[2] = { 0x00, 0x00 };
359 s[0] |= 0x1f - dev->volume;
360 s[1] |= 0x1f - dev->volume;
361 if (dev->mute)
362 s[1] |= 0x80;
363 return em2820_write_ac97(dev, MASTER_AC97, s);
364}
365
366
367int em2820_colorlevels_set_default(struct em2820 *dev)
368{
369 em2820_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
370 em2820_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
371 em2820_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
372 em2820_write_regs(dev, UOFFSET_REG, "\x00", 1);
373 em2820_write_regs(dev, VOFFSET_REG, "\x00", 1);
374 em2820_write_regs(dev, SHARPNESS_REG, "\x00", 1);
375
376 em2820_write_regs(dev, GAMMA_REG, "\x20", 1);
377 em2820_write_regs(dev, RGAIN_REG, "\x20", 1);
378 em2820_write_regs(dev, GGAIN_REG, "\x20", 1);
379 em2820_write_regs(dev, BGAIN_REG, "\x20", 1);
380 em2820_write_regs(dev, ROFFSET_REG, "\x00", 1);
381 em2820_write_regs(dev, GOFFSET_REG, "\x00", 1);
382 return em2820_write_regs(dev, BOFFSET_REG, "\x00", 1);
383}
384
385int em2820_capture_start(struct em2820 *dev, int start)
386{
387 int ret;
388 /* FIXME: which is the best order? */
389 /* video registers are sampled by VREF */
390 if ((ret = em2820_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
391 0x10)) < 0)
392 return ret;
393 /* enable video capture */
394 return em2820_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
395}
396
397int em2820_outfmt_set_yuv422(struct em2820 *dev)
398{
399 em2820_write_regs(dev, OUTFMT_REG, "\x34", 1);
400 em2820_write_regs(dev, VINMODE_REG, "\x10", 1);
401 return em2820_write_regs(dev, VINCTRL_REG, "\x11", 1);
402}
403
404int em2820_accumulator_set(struct em2820 *dev, u8 xmin, u8 xmax, u8 ymin,
405 u8 ymax)
406{
407 em2820_coredbg("em2820 Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
408
409 em2820_write_regs(dev, XMIN_REG, &xmin, 1);
410 em2820_write_regs(dev, XMAX_REG, &xmax, 1);
411 em2820_write_regs(dev, YMIN_REG, &ymin, 1);
412 return em2820_write_regs(dev, YMAX_REG, &ymax, 1);
413}
414
415int em2820_capture_area_set(struct em2820 *dev, u8 hstart, u8 vstart,
416 u16 width, u16 height)
417{
418 u8 cwidth = width;
419 u8 cheight = height;
420 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
421
422 em2820_coredbg("em2820 Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
423 (height | (overflow & 1) << 8));
424
425 em2820_write_regs(dev, HSTART_REG, &hstart, 1);
426 em2820_write_regs(dev, VSTART_REG, &vstart, 1);
427 em2820_write_regs(dev, CWIDTH_REG, &cwidth, 1);
428 em2820_write_regs(dev, CHEIGHT_REG, &cheight, 1);
429 return em2820_write_regs(dev, OFLOW_REG, &overflow, 1);
430}
431
432int em2820_scaler_set(struct em2820 *dev, u16 h, u16 v)
433{
434 u8 buf[2];
435 buf[0] = h;
436 buf[1] = h >> 8;
437 em2820_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
438 buf[0] = v;
439 buf[1] = v >> 8;
440 em2820_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
441 /* when H and V mixershould be used? */
442 /* return em2820_write_reg_bits(dev, COMPR_REG, (h ? 0x20 : 0x00) | (v ? 0x10 : 0x00), 0x30); */
443 /* it seems that both H and V scalers must be active to work correctly */
444 return em2820_write_reg_bits(dev, COMPR_REG, h
445 || v ? 0x30 : 0x00, 0x30);
446}
447
448/* FIXME: this only function read values from dev */
449int em2820_resolution_set(struct em2820 *dev)
450{
451 int width, height;
452 width = norm_maxw(dev);
453 height = norm_maxh(dev) >> 1;
454
455 em2820_outfmt_set_yuv422(dev);
456 em2820_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
457 em2820_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
458 return em2820_scaler_set(dev, dev->hscale, dev->vscale);
459}
460
461
462/******************* isoc transfer handling ****************************/
463
464#ifdef ENABLE_DEBUG_ISOC_FRAMES
465static void em2820_isoc_dump(struct urb *urb, struct pt_regs *regs)
466{
467 int len = 0;
468 int ntrans = 0;
469 int i;
470
471 printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
472 urb->start_frame, urb->number_of_packets,
473 urb->error_count);
474 for (i = 0; i < urb->number_of_packets; i++) {
475 unsigned char *buf =
476 urb->transfer_buffer +
477 urb->iso_frame_desc[i].offset;
478 int alen = urb->iso_frame_desc[i].actual_length;
479 if (alen > 0) {
480 if (buf[0] == 0x88) {
481 ntrans++;
482 len += alen;
483 } else if (buf[0] == 0x22) {
484 printk(KERN_DEBUG
485 "= l=%d nt=%d bpp=%d\n",
486 len - 4 * ntrans, ntrans,
487 ntrans == 0 ? 0 : len / ntrans);
488 ntrans = 1;
489 len = alen;
490 } else
491 printk(KERN_DEBUG "!\n");
492 }
493 printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
494 urb->iso_frame_desc[i].status,
495 urb->iso_frame_desc[i].actual_length,
496 (unsigned int)
497 *((unsigned char *)(urb->transfer_buffer +
498 urb->iso_frame_desc[i].
499 offset)));
500 }
501}
502#endif
503
504static inline int em2820_isoc_video(struct em2820 *dev,struct em2820_frame_t **f,
505 unsigned long *lock_flags, unsigned char buf)
506{
507 if (!(buf & 0x01)) {
508 if ((*f)->state == F_GRABBING) {
509 /*previous frame is incomplete */
510 if ((*f)->fieldbytesused < dev->field_size) {
511 (*f)->state = F_ERROR;
512 em2820_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
513 dev->field_size-(*f)->fieldbytesused);
514 } else {
515 (*f)->state = F_DONE;
516 (*f)->buf.bytesused = dev->frame_size;
517 }
518 }
519 if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
520 /* move current frame to outqueue and get next free buffer from inqueue */
521 spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
522 list_move_tail(&(*f)->frame, &dev->outqueue);
523 if (!list_empty(&dev->inqueue))
524 (*f) = list_entry(dev-> inqueue.next,
525 struct em2820_frame_t,frame);
526 else
527 (*f) = NULL;
528 spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
529 }
530 if (!(*f)) {
531 em2820_isocdbg ("new frame but no buffer is free");
532 return -1;
533 }
534 do_gettimeofday(&(*f)->buf.timestamp);
535 (*f)->buf.sequence = ++dev->frame_count;
536 (*f)->buf.field = V4L2_FIELD_INTERLACED;
537 (*f)->state = F_GRABBING;
538 (*f)->buf.bytesused = 0;
539 (*f)->top_field = 1;
540 (*f)->fieldbytesused = 0;
541 } else {
542 /* acquiring bottom field */
543 if ((*f)->state == F_GRABBING) {
544 if (!(*f)->top_field) {
545 (*f)->state = F_ERROR;
546 em2820_isocdbg ("unexpected begin of bottom field; discarding it");
547 } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
548 (*f)->state = F_ERROR;
549 em2820_isocdbg ("dropping incomplete top field (%i missing bytes)",
550 dev->field_size-(*f)->fieldbytesused);
551 } else {
552 (*f)->top_field = 0;
553 (*f)->fieldbytesused = 0;
554 }
555 }
556 }
557 return (0);
558}
559
560static inline void em2820_isoc_video_copy(struct em2820 *dev,
561 struct em2820_frame_t **f, unsigned char *buf, int len)
562{
563 void *fieldstart, *startwrite, *startread;
564 int linesdone, currlinedone, offset, lencopy,remain;
565
566 if ((*f)->fieldbytesused + len > dev->field_size)
567 len =dev->field_size - (*f)->fieldbytesused;
568 remain = len;
569 startread = buf + 4;
570 if ((*f)->top_field)
571 fieldstart = (*f)->bufmem;
572 else
573 fieldstart = (*f)->bufmem + dev->bytesperline;
574
575 linesdone = (*f)->fieldbytesused / dev->bytesperline;
576 currlinedone = (*f)->fieldbytesused % dev->bytesperline;
577 offset = linesdone * dev->bytesperline * 2 + currlinedone;
578 startwrite = fieldstart + offset;
579 lencopy = dev->bytesperline - currlinedone;
580 lencopy = lencopy > remain ? remain : lencopy;
581
582 memcpy(startwrite, startread, lencopy);
583 remain -= lencopy;
584
585 while (remain > 0) {
586 startwrite += lencopy + dev->bytesperline;
587 startread += lencopy;
588 if (dev->bytesperline > remain)
589 lencopy = remain;
590 else
591 lencopy = dev->bytesperline;
592
593 memcpy(startwrite, startread, lencopy);
594 remain -= lencopy;
595 }
596
597 (*f)->fieldbytesused += len;
598}
599
600/*
601 * em2820_isoIrq()
602 * handles the incoming isoc urbs and fills the frames from our inqueue
603 */
604void em2820_isocIrq(struct urb *urb, struct pt_regs *regs)
605{
606 struct em2820 *dev = urb->context;
607 int i, status;
608 struct em2820_frame_t **f;
609 unsigned long lock_flags;
610
611 if (!dev)
612 return;
613#ifdef ENABLE_DEBUG_ISOC_FRAMES
614 if (isoc_debug>1)
615 em2820_isoc_dump(urb, regs);
616#endif
617
618 if (urb->status == -ENOENT)
619 return;
620
621 f = &dev->frame_current;
622
623 if (dev->stream == STREAM_INTERRUPT) {
624 dev->stream = STREAM_OFF;
625 if ((*f))
626 (*f)->state = F_QUEUED;
627 em2820_isocdbg("stream interrupted");
628 wake_up_interruptible(&dev->wait_stream);
629 }
630
631 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
632 return;
633
634 if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
635 if (!(*f))
636 (*f) = list_entry(dev->inqueue.next,
637 struct em2820_frame_t, frame);
638
639 for (i = 0; i < urb->number_of_packets; i++) {
640 unsigned char *buf = urb->transfer_buffer +
641 urb->iso_frame_desc[i].offset;
642 int len = urb->iso_frame_desc[i].actual_length - 4;
643
644 if (urb->iso_frame_desc[i].status) {
645 em2820_isocdbg("data error: [%d] len=%d, status=%d", i,
646 urb->iso_frame_desc[i].actual_length,
647 urb->iso_frame_desc[i].status);
648 continue;
649 }
650 if (urb->iso_frame_desc[i].actual_length <= 0) {
651 em2820_isocdbg("packet %d is empty",i);
652 continue;
653 }
654 if (urb->iso_frame_desc[i].actual_length >
655 dev->max_pkt_size) {
656 em2820_isocdbg("packet bigger than packet size");
657 continue;
658 }
659 /*new frame */
660 if (buf[0] == 0x22 && buf[1] == 0x5a) {
661 em2820_isocdbg("Video frame, length=%i!",len);
662
663 if (em2820_isoc_video(dev,f,&lock_flags,buf[2]))
664 break;
665 } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
666 em2820_isocdbg("VBI HEADER!!!");
667 }
668
669 /* actual copying */
670 if ((*f)->state == F_GRABBING) {
671 em2820_isoc_video_copy(dev,f,buf, len);
672 }
673 }
674 }
675
676 for (i = 0; i < urb->number_of_packets; i++) {
677 urb->iso_frame_desc[i].status = 0;
678 urb->iso_frame_desc[i].actual_length = 0;
679 }
680
681 urb->status = 0;
682 if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
683 em2820_errdev("resubmit of urb failed (error=%i)\n", status);
684 dev->state |= DEV_MISCONFIGURED;
685 }
686 wake_up_interruptible(&dev->wait_frame);
687 return;
688}
689
690/*
691 * em2820_uninit_isoc()
692 * deallocates the buffers and urbs allocated during em2820_init_iosc()
693 */
694void em2820_uninit_isoc(struct em2820 *dev)
695{
696 int i;
697
698 for (i = 0; i < EM2820_NUM_BUFS; i++) {
699 if (dev->urb[i]) {
700 usb_kill_urb(dev->urb[i]);
701 usb_free_urb(dev->urb[i]);
702 }
703 dev->urb[i] = NULL;
704 if (dev->transfer_buffer[i])
705 kfree(dev->transfer_buffer[i]);
706 dev->transfer_buffer[i] = NULL;
707 }
708 em2820_capture_start(dev, 0);
709}
710
711/*
712 * em2820_init_isoc()
713 * allocates transfer buffers and submits the urbs for isoc transfer
714 */
715int em2820_init_isoc(struct em2820 *dev)
716{
717 /* change interface to 3 which allowes the biggest packet sizes */
718 int i, errCode;
719 const int sb_size = EM2820_NUM_PACKETS * dev->max_pkt_size;
720
721 /* reset streaming vars */
722 dev->frame_current = NULL;
723 dev->frame_count = 0;
724
725 /* allocate urbs */
726 for (i = 0; i < EM2820_NUM_BUFS; i++) {
727 struct urb *urb;
728 int j, k;
729 /* allocate transfer buffer */
730 dev->transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
731 if (!dev->transfer_buffer[i]) {
732 em2820_errdev
733 ("unable to allocate %i bytes for transfer buffer %i\n",
734 sb_size, i);
735 em2820_uninit_isoc(dev);
736 return -ENOMEM;
737 }
738 memset(dev->transfer_buffer[i], 0, sb_size);
739 urb = usb_alloc_urb(EM2820_NUM_PACKETS, GFP_KERNEL);
740 if (urb) {
741 urb->dev = dev->udev;
742 urb->context = dev;
743 urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
744 urb->transfer_flags = URB_ISO_ASAP;
745 urb->interval = 1;
746 urb->transfer_buffer = dev->transfer_buffer[i];
747 urb->complete = em2820_isocIrq;
748 urb->number_of_packets = EM2820_NUM_PACKETS;
749 urb->transfer_buffer_length = sb_size;
750 for (j = k = 0; j < EM2820_NUM_PACKETS;
751 j++, k += dev->max_pkt_size) {
752 urb->iso_frame_desc[j].offset = k;
753 urb->iso_frame_desc[j].length =
754 dev->max_pkt_size;
755 }
756 dev->urb[i] = urb;
757 } else {
758 em2820_errdev("cannot alloc urb %i\n", i);
759 em2820_uninit_isoc(dev);
760 return -ENOMEM;
761 }
762 }
763
764 /* submit urbs */
765 for (i = 0; i < EM2820_NUM_BUFS; i++) {
766 errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
767 if (errCode) {
768 em2820_errdev("submit of urb %i failed (error=%i)\n", i,
769 errCode);
770 em2820_uninit_isoc(dev);
771 return errCode;
772 }
773 }
774
775 return 0;
776}
777
778int em2820_set_alternate(struct em2820 *dev)
779{
780 int errCode, prev_alt = dev->alt;
781 dev->alt = alt;
782 if (dev->alt == 0) {
783 int i;
784 unsigned int min_pkt_size = dev->field_size / 137; /* FIXME: empiric magic number */
785 em2820_coredbg("minimum isoc packet size: %u", min_pkt_size);
786 dev->alt = 7;
787 for (i = 1; i < EM2820_MAX_ALT; i += 2) /* FIXME: skip even alternate: why do they not work? */
788 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
789 dev->alt = i;
790 break;
791 }
792 }
793
794 if (dev->alt != prev_alt) {
795 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
796 em2820_coredbg("setting alternate %d with wMaxPacketSize=%u", dev->alt,
797 dev->max_pkt_size);
798 errCode = usb_set_interface(dev->udev, 0, dev->alt);
799 if (errCode < 0) {
800 em2820_errdev
801 ("cannot change alternate number to %d (error=%i)\n",
802 dev->alt, errCode);
803 return errCode;
804 }
805 }
806 return 0;
807}
diff --git a/drivers/media/video/em28xx/em28xx-i2c.c b/drivers/media/video/em28xx/em28xx-i2c.c
new file mode 100644
index 000000000000..95f80a7615bd
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx-i2c.c
@@ -0,0 +1,443 @@
1/*
2 em2820-i2c.c - driver for Empia EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/usb.h>
28#include <linux/i2c.h>
29#include <linux/videodev.h>
30#include <media/tuner.h>
31#include <linux/video_decoder.h>
32
33/* To be moved to compat.h */
34#if !defined(I2C_HW_B_EM2820)
35#define I2C_HW_B_EM2820 I2C_HW_B_BT848
36#endif
37
38#include "em2820.h"
39
40/* ----------------------------------------------------------- */
41
42static unsigned int i2c_scan = 0;
43module_param(i2c_scan, int, 0444);
44MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
45
46static unsigned int i2c_debug = 0;
47module_param(i2c_debug, int, 0644);
48MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
49
50#define dprintk(fmt, args...) if (i2c_debug) do {\
51 printk(KERN_DEBUG "%s: %s: " fmt "\n",\
52 dev->name, __FUNCTION__ , ##args); } while (0)
53#define dprintk1(fmt, args...) if (i2c_debug) do{ \
54 printk(KERN_DEBUG "%s: %s: " fmt, \
55 dev->name, __FUNCTION__ , ##args); } while (0)
56#define dprintk2(fmt, args...) if (i2c_debug) do {\
57 printk(fmt , ##args); } while (0)
58
59/*
60 * i2c_send_bytes()
61 * untested for more than 4 bytes
62 */
63static int i2c_send_bytes(void *data, unsigned char addr, char *buf, short len,
64 int stop)
65{
66 int wrcount = 0;
67 struct em2820 *dev = (struct em2820 *)data;
68
69 wrcount = dev->em2820_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
70
71 return wrcount;
72}
73
74/*
75 * i2c_recv_byte()
76 * read a byte from the i2c device
77 */
78static int i2c_recv_bytes(struct em2820 *dev, unsigned char addr, char *buf,
79 int len)
80{
81 int ret;
82 ret = dev->em2820_read_reg_req_len(dev, 2, addr, buf, len);
83 if (ret < 0) {
84 em2820_warn("reading i2c device failed (error=%i)\n", ret);
85 return ret;
86 }
87 if (dev->em2820_read_reg(dev, 0x5) != 0)
88 return -ENODEV;
89 return ret;
90}
91
92/*
93 * i2c_check_for_device()
94 * check if there is a i2c_device at the supplied address
95 */
96static int i2c_check_for_device(struct em2820 *dev, unsigned char addr)
97{
98 char msg;
99 int ret;
100 msg = addr;
101
102 ret = dev->em2820_read_reg_req(dev, 2, addr);
103 if (ret < 0) {
104 em2820_warn("reading from i2c device failed (error=%i)\n", ret);
105 return ret;
106 }
107 if (dev->em2820_read_reg(dev, 0x5) != 0)
108 return -ENODEV;
109 return 0;
110}
111
112/*
113 * em2820_i2c_xfer()
114 * the main i2c transfer function
115 */
116static int em2820_i2c_xfer(struct i2c_adapter *i2c_adap,
117 struct i2c_msg msgs[], int num)
118{
119 struct em2820 *dev = i2c_adap->algo_data;
120 int addr, rc, i, byte;
121
122 if (num <= 0)
123 return 0;
124 for (i = 0; i < num; i++) {
125 addr = msgs[i].addr << 1;
126 dprintk1("%s %s addr=%x len=%d:",
127 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
128 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
129 if (!msgs[i].len) { /* no len: check only for device presence */
130 rc = i2c_check_for_device(dev, addr);
131 if (rc < 0) {
132 dprintk2(" no device\n");
133 return rc;
134 }
135
136 }
137 if (msgs[i].flags & I2C_M_RD) {
138 /* read bytes */
139
140 rc = i2c_recv_bytes(dev, addr, msgs[i].buf,
141 msgs[i].len);
142 if (i2c_debug) {
143 for (byte = 0; byte < msgs[i].len; byte++) {
144 printk(" %02x", msgs[i].buf[byte]);
145 }
146 }
147 } else {
148 /* write bytes */
149 if (i2c_debug) {
150 for (byte = 0; byte < msgs[i].len; byte++)
151 printk(" %02x", msgs[i].buf[byte]);
152 }
153 rc = i2c_send_bytes(dev, addr, msgs[i].buf, msgs[i].len,
154 i == num - 1);
155 if (rc < 0)
156 goto err;
157 }
158 if (i2c_debug)
159 printk("\n");
160 }
161
162 return num;
163 err:
164 dprintk2(" ERROR: %i\n", rc);
165 return rc;
166}
167
168static int em2820_i2c_eeprom(struct em2820 *dev, unsigned char *eedata, int len)
169{
170 unsigned char buf, *p = eedata;
171 struct em2820_eeprom *em_eeprom = (void *)eedata;
172 int i, err, size = len, block;
173
174 dev->i2c_client.addr = 0xa0 >> 1;
175 buf = 0;
176 if (1 != (err = i2c_master_send(&dev->i2c_client, &buf, 1))) {
177 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
178 dev->name, err);
179 return -1;
180 }
181 while (size > 0) {
182 if (size > 16)
183 block = 16;
184 else
185 block = size;
186
187 if (block !=
188 (err = i2c_master_recv(&dev->i2c_client, p, block))) {
189 printk(KERN_WARNING
190 "%s: i2c eeprom read error (err=%d)\n",
191 dev->name, err);
192 return -1;
193 }
194 size -= block;
195 p += block;
196 }
197 for (i = 0; i < len; i++) {
198 if (0 == (i % 16))
199 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
200 printk(" %02x", eedata[i]);
201 if (15 == (i % 16))
202 printk("\n");
203 }
204
205 printk(KERN_INFO "EEPROM ID= 0x%08x\n", em_eeprom->id);
206 printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
207 em_eeprom->product_ID);
208
209 switch (em_eeprom->chip_conf >> 4 & 0x3) {
210 case 0:
211 printk(KERN_INFO "No audio on board.\n");
212 break;
213 case 1:
214 printk(KERN_INFO "AC97 audio (5 sample rates)\n");
215 break;
216 case 2:
217 printk(KERN_INFO "I2S audio, sample rate=32k\n");
218 break;
219 case 3:
220 printk(KERN_INFO "I2S audio, 3 sample rates\n");
221 break;
222 }
223
224 if (em_eeprom->chip_conf & 1 << 3)
225 printk(KERN_INFO "USB Remote wakeup capable\n");
226
227 if (em_eeprom->chip_conf & 1 << 2)
228 printk(KERN_INFO "USB Self power capable\n");
229
230 switch (em_eeprom->chip_conf & 0x3) {
231 case 0:
232 printk(KERN_INFO "500mA max power\n");
233 break;
234 case 1:
235 printk(KERN_INFO "400mA max power\n");
236 break;
237 case 2:
238 printk(KERN_INFO "300mA max power\n");
239 break;
240 case 3:
241 printk(KERN_INFO "200mA max power\n");
242 break;
243 }
244
245 return 0;
246}
247
248/* ----------------------------------------------------------- */
249
250/*
251 * algo_control()
252 */
253static int algo_control(struct i2c_adapter *adapter,
254 unsigned int cmd, unsigned long arg)
255{
256 return 0;
257}
258
259/*
260 * functionality()
261 */
262static u32 functionality(struct i2c_adapter *adap)
263{
264 return I2C_FUNC_SMBUS_EMUL;
265}
266
267#ifndef I2C_PEC
268static void inc_use(struct i2c_adapter *adap)
269{
270 MOD_INC_USE_COUNT;
271}
272
273static void dec_use(struct i2c_adapter *adap)
274{
275 MOD_DEC_USE_COUNT;
276}
277#endif
278
279static int em2820_set_tuner(int check_eeprom, struct i2c_client *client)
280{
281 struct em2820 *dev = client->adapter->algo_data;
282
283 struct tuner_setup tun_setup;
284
285 /* tuner */
286 if (dev->has_tuner) {
287 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
288 tun_setup.type = dev->tuner_type;
289 tun_setup.addr = dev->tuner_addr;
290
291 em2820_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
292 }
293 return (0);
294}
295
296/*
297 * attach_inform()
298 * gets called when a device attaches to the i2c bus
299 * does some basic configuration
300 */
301static int attach_inform(struct i2c_client *client)
302{
303 struct em2820 *dev = client->adapter->algo_data;
304
305 dprintk("address %x", client->addr << 1);
306 switch (client->addr << 1) {
307 case 0x68:
308 em2820_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
309 break;
310 case 0x4a:
311 dprintk1("attach_inform: saa7113 detected.\n");
312 break;
313 case 0xa0:
314 dprintk1("attach_inform: eeprom detected.\n");
315 break;
316 case 0x80:
317 case 0x88:
318 dprintk1("attach_inform: msp34xx detected.\n");
319 break;
320 case 0xb8:
321 case 0xba:
322 dprintk1("attach_inform: tvp5150 detected.\n");
323 break;
324 default:
325 dev->tuner_addr = client->addr;
326 em2820_set_tuner(-1, client);
327 }
328
329 return 0;
330}
331
332static struct i2c_algorithm em2820_algo = {
333 .name = "em2820",
334 .id = I2C_HW_B_EM2820,
335 .master_xfer = em2820_i2c_xfer,
336 .algo_control = algo_control,
337 .functionality = functionality,
338};
339
340static struct i2c_adapter em2820_adap_template = {
341#ifdef I2C_PEC
342 .owner = THIS_MODULE,
343#else
344 .inc_use = inc_use,
345 .dec_use = dec_use,
346#endif
347#ifdef I2C_CLASS_TV_ANALOG
348 .class = I2C_CLASS_TV_ANALOG,
349#endif
350 .name = "em2820",
351 .id = I2C_HW_B_EM2820,
352 .algo = &em2820_algo,
353 .client_register = attach_inform,
354};
355
356static struct i2c_client em2820_client_template = {
357 .name = "em2820 internal",
358 .flags = I2C_CLIENT_ALLOW_USE,
359};
360
361/* ----------------------------------------------------------- */
362
363/*
364 * i2c_devs
365 * incomplete list of known devices
366 */
367static char *i2c_devs[128] = {
368 [0x4a >> 1] = "saa7113h",
369 [0x60 >> 1] = "remote IR sensor",
370 [0x86 >> 1] = "tda9887",
371 [0x80 >> 1] = "msp34xx",
372 [0x88 >> 1] = "msp34xx",
373 [0xa0 >> 1] = "eeprom",
374 [0xb8 >> 1] = "tvp5150a",
375 [0xba >> 1] = "tvp5150a",
376 [0xc0 >> 1] = "tuner (analog)",
377 [0xc2 >> 1] = "tuner (analog)",
378 [0xc4 >> 1] = "tuner (analog)",
379 [0xc6 >> 1] = "tuner (analog)",
380};
381
382/*
383 * do_i2c_scan()
384 * check i2c address range for devices
385 */
386static void do_i2c_scan(char *name, struct i2c_client *c)
387{
388 unsigned char buf;
389 int i, rc;
390
391 for (i = 0; i < 128; i++) {
392 c->addr = i;
393 rc = i2c_master_recv(c, &buf, 0);
394 if (rc < 0)
395 continue;
396 printk(KERN_INFO "%s: found device @ 0x%x [%s]", name,
397 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
398 }
399}
400
401/*
402 * em2820_i2c_call_clients()
403 * send commands to all attached i2c devices
404 */
405void em2820_i2c_call_clients(struct em2820 *dev, unsigned int cmd, void *arg)
406{
407 BUG_ON(NULL == dev->i2c_adap.algo_data);
408 i2c_clients_command(&dev->i2c_adap, cmd, arg);
409}
410
411/*
412 * em2820_i2c_register()
413 * register i2c bus
414 */
415int em2820_i2c_register(struct em2820 *dev)
416{
417 BUG_ON(!dev->em2820_write_regs || !dev->em2820_read_reg);
418 BUG_ON(!dev->em2820_write_regs_req || !dev->em2820_read_reg_req);
419 dev->i2c_adap = em2820_adap_template;
420 dev->i2c_adap.dev.parent = &dev->udev->dev;
421 strcpy(dev->i2c_adap.name, dev->name);
422 dev->i2c_adap.algo_data = dev;
423 i2c_add_adapter(&dev->i2c_adap);
424
425 dev->i2c_client = em2820_client_template;
426 dev->i2c_client.adapter = &dev->i2c_adap;
427
428 em2820_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
429
430 if (i2c_scan)
431 do_i2c_scan(dev->name, &dev->i2c_client);
432 return 0;
433}
434
435/*
436 * em2820_i2c_unregister()
437 * unregister i2c_bus
438 */
439int em2820_i2c_unregister(struct em2820 *dev)
440{
441 i2c_del_adapter(&dev->i2c_adap);
442 return 0;
443}
diff --git a/drivers/media/video/em28xx/em28xx-video.c b/drivers/media/video/em28xx/em28xx-video.c
new file mode 100644
index 000000000000..a000131a8f5d
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx-video.c
@@ -0,0 +1,1815 @@
1/*
2 em2820-video.c - driver for Empia EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/usb.h>
30#include <linux/videodev.h>
31#include <linux/i2c.h>
32#include <media/tuner.h>
33#include <linux/video_decoder.h>
34
35#include "em2820.h"
36
37#define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \
38 "Ludovico Cavedon <cavedon@sssup.it>, " \
39 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>"
40
41#define DRIVER_NAME "em2820"
42#define DRIVER_DESC "Empia em2820 based USB video device driver"
43#define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1)
44
45#define em2820_videodbg(fmt, arg...) do {\
46 if (video_debug) \
47 printk(KERN_INFO "%s %s :"fmt, \
48 dev->name, __FUNCTION__ , ##arg); } while (0)
49
50MODULE_AUTHOR(DRIVER_AUTHOR);
51MODULE_DESCRIPTION(DRIVER_DESC);
52MODULE_LICENSE("GPL");
53
54static int tuner = -1;
55module_param(tuner, int, 0444);
56MODULE_PARM_DESC(tuner, "tuner type");
57
58static unsigned int video_debug = 0;
59module_param(video_debug,int,0644);
60MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
61
62/* supported tv norms */
63static struct em2820_tvnorm tvnorms[] = {
64 {
65 .name = "PAL",
66 .id = V4L2_STD_PAL,
67 .mode = VIDEO_MODE_PAL,
68 }, {
69 .name = "NTSC",
70 .id = V4L2_STD_NTSC,
71 .mode = VIDEO_MODE_NTSC,
72 }, {
73 .name = "SECAM",
74 .id = V4L2_STD_SECAM,
75 .mode = VIDEO_MODE_SECAM,
76 }, {
77 .name = "PAL-M",
78 .id = V4L2_STD_PAL_M,
79 .mode = VIDEO_MODE_PAL,
80 }
81};
82
83#define TVNORMS ARRAY_SIZE(tvnorms)
84
85/* supported controls */
86static struct v4l2_queryctrl em2820_qctrl[] = {
87 {
88 .id = V4L2_CID_BRIGHTNESS,
89 .type = V4L2_CTRL_TYPE_INTEGER,
90 .name = "Brightness",
91 .minimum = -128,
92 .maximum = 127,
93 .step = 1,
94 .default_value = 0,
95 .flags = 0,
96 },{
97 .id = V4L2_CID_CONTRAST,
98 .type = V4L2_CTRL_TYPE_INTEGER,
99 .name = "Contrast",
100 .minimum = 0x0,
101 .maximum = 0x1f,
102 .step = 0x1,
103 .default_value = 0x10,
104 .flags = 0,
105 },{
106 .id = V4L2_CID_SATURATION,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "Saturation",
109 .minimum = 0x0,
110 .maximum = 0x1f,
111 .step = 0x1,
112 .default_value = 0x10,
113 .flags = 0,
114 },{
115 .id = V4L2_CID_AUDIO_VOLUME,
116 .type = V4L2_CTRL_TYPE_INTEGER,
117 .name = "Volume",
118 .minimum = 0x0,
119 .maximum = 0x1f,
120 .step = 0x1,
121 .default_value = 0x1f,
122 .flags = 0,
123 },{
124 .id = V4L2_CID_AUDIO_MUTE,
125 .type = V4L2_CTRL_TYPE_BOOLEAN,
126 .name = "Mute",
127 .minimum = 0,
128 .maximum = 1,
129 .step = 1,
130 .default_value = 1,
131 .flags = 0,
132 },{
133 .id = V4L2_CID_RED_BALANCE,
134 .type = V4L2_CTRL_TYPE_INTEGER,
135 .name = "Red chroma balance",
136 .minimum = -128,
137 .maximum = 127,
138 .step = 1,
139 .default_value = 0,
140 .flags = 0,
141 },{
142 .id = V4L2_CID_BLUE_BALANCE,
143 .type = V4L2_CTRL_TYPE_INTEGER,
144 .name = "Blue chroma balance",
145 .minimum = -128,
146 .maximum = 127,
147 .step = 1,
148 .default_value = 0,
149 .flags = 0,
150 },{
151 .id = V4L2_CID_GAMMA,
152 .type = V4L2_CTRL_TYPE_INTEGER,
153 .name = "Gamma",
154 .minimum = 0x0,
155 .maximum = 0x3f,
156 .step = 0x1,
157 .default_value = 0x20,
158 .flags = 0,
159 }
160};
161
162static struct usb_driver em2820_usb_driver;
163
164static DECLARE_MUTEX(em2820_sysfs_lock);
165static DECLARE_RWSEM(em2820_disconnect);
166
167/********************* v4l2 interface ******************************************/
168
169static inline unsigned long kvirt_to_pa(unsigned long adr)
170{
171 unsigned long kva, ret;
172
173 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
174 kva |= adr & (PAGE_SIZE - 1);
175 ret = __pa(kva);
176 return ret;
177}
178
179/*
180 * em2820_config()
181 * inits registers with sane defaults
182 */
183static int em2820_config(struct em2820 *dev)
184{
185
186 /* Sets I2C speed to 100 KHz */
187 em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
188
189 /* enable vbi capturing */
190 em2820_audio_usb_mute(dev, 1);
191 dev->mute = 1; /* maybe not the right place... */
192 dev->volume = 0x1f;
193 em2820_audio_analog_set(dev);
194 em2820_audio_analog_setup(dev);
195 em2820_outfmt_set_yuv422(dev);
196 em2820_colorlevels_set_default(dev);
197 em2820_compression_disable(dev);
198
199 return 0;
200}
201
202/*
203 * em2820_config_i2c()
204 * configure i2c attached devices
205 */
206void em2820_config_i2c(struct em2820 *dev)
207{
208 struct v4l2_frequency f;
209 struct video_decoder_init em2820_vdi = {.data = NULL };
210
211
212 /* configure decoder */
213 em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
214 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
215/* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
216/* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
217/* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
218/* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
219
220 /* configure tuner */
221 f.tuner = 0;
222 f.type = V4L2_TUNER_ANALOG_TV;
223 f.frequency = 9076; /* FIXME:remove magic number */
224 dev->ctl_freq = f.frequency;
225 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
226
227 /* configure tda9887 */
228
229 em2820_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
230
231/* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
232}
233
234/*
235 * em2820_empty_framequeues()
236 * prepare queues for incoming and outgoing frames
237 */
238static void em2820_empty_framequeues(struct em2820 *dev)
239{
240 u32 i;
241
242 INIT_LIST_HEAD(&dev->inqueue);
243 INIT_LIST_HEAD(&dev->outqueue);
244
245 for (i = 0; i < EM2820_NUM_FRAMES; i++) {
246 dev->frame[i].state = F_UNUSED;
247 dev->frame[i].buf.bytesused = 0;
248 }
249}
250
251/*
252 * em2820_v4l2_open()
253 * inits the device and starts isoc transfer
254 */
255static int em2820_v4l2_open(struct inode *inode, struct file *filp)
256{
257 struct video_device *vdev = video_devdata(filp);
258 int minor = iminor(inode);
259 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
260 int errCode = 0;
261
262 em2820_videodbg("users=%d", dev->users);
263
264 if (!down_read_trylock(&em2820_disconnect))
265 return -ERESTARTSYS;
266
267 if (dev->users) {
268 em2820_warn("this driver can be opened only once\n");
269 up_read(&em2820_disconnect);
270 return -EBUSY;
271 }
272
273/* if(dev->vbi_dev->minor == minor){
274 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
275 }*/
276 if (dev->vdev->minor == minor) {
277 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
278 }
279
280 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
281 spin_lock_init(&dev->queue_lock);
282 init_waitqueue_head(&dev->wait_frame);
283 init_waitqueue_head(&dev->wait_stream);
284
285 down(&dev->lock);
286
287 em2820_set_alternate(dev);
288
289 dev->width = norm_maxw(dev);
290 dev->height = norm_maxh(dev);
291 dev->frame_size = dev->width * dev->height * 2;
292 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
293 dev->bytesperline = dev->width * 2;
294 dev->hscale = 0;
295 dev->vscale = 0;
296
297 em2820_capture_start(dev, 1);
298 em2820_resolution_set(dev);
299
300 /* start the transfer */
301 errCode = em2820_init_isoc(dev);
302 if (errCode)
303 goto err;
304
305 dev->users++;
306 filp->private_data = dev;
307 dev->io = IO_NONE;
308 dev->stream = STREAM_OFF;
309 dev->num_frames = 0;
310
311 /* prepare queues */
312 em2820_empty_framequeues(dev);
313
314 dev->state |= DEV_INITIALIZED;
315
316 err:
317 up(&dev->lock);
318 up_read(&em2820_disconnect);
319 return errCode;
320}
321
322/*
323 * em2820_realease_resources()
324 * unregisters the v4l2,i2c and usb devices
325 * called when the device gets disconected or at module unload
326*/
327static void em2820_release_resources(struct em2820 *dev)
328{
329 down(&em2820_sysfs_lock);
330
331 em2820_info("V4L2 device /dev/video%d deregistered\n",
332 dev->vdev->minor);
333 video_set_drvdata(dev->vdev, NULL);
334 video_unregister_device(dev->vdev);
335/* video_unregister_device(dev->vbi_dev); */
336 em2820_i2c_unregister(dev);
337 usb_put_dev(dev->udev);
338 up(&em2820_sysfs_lock);
339}
340
341/*
342 * em2820_v4l2_close()
343 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
344 */
345static int em2820_v4l2_close(struct inode *inode, struct file *file)
346{
347 struct video_device *vdev = video_devdata(file);
348 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
349 int errCode;
350
351 em2820_videodbg("users=%d", dev->users);
352
353 down(&dev->lock);
354
355 em2820_uninit_isoc(dev);
356
357 em2820_release_buffers(dev);
358
359 /* the device is already disconnect, free the remaining resources */
360 if (dev->state & DEV_DISCONNECTED) {
361 em2820_release_resources(dev);
362 up(&dev->lock);
363 kfree(dev);
364 return 0;
365 }
366
367 /* set alternate 0 */
368 dev->alt = 0;
369 em2820_videodbg("setting alternate 0");
370 errCode = usb_set_interface(dev->udev, 0, 0);
371 if (errCode < 0) {
372 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
373 errCode);
374 }
375
376 dev->users--;
377 wake_up_interruptible_nr(&dev->open, 1);
378 up(&dev->lock);
379 return 0;
380}
381
382/*
383 * em2820_v4l2_read()
384 * will allocate buffers when called for the first time
385 */
386static ssize_t
387em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
388 loff_t * f_pos)
389{
390 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
391 struct em2820_frame_t *f, *i;
392 unsigned long lock_flags;
393 int ret = 0;
394
395 if (down_interruptible(&dev->fileop_lock))
396 return -ERESTARTSYS;
397
398 if (dev->state & DEV_DISCONNECTED) {
399 em2820_videodbg("device not present");
400 up(&dev->fileop_lock);
401 return -ENODEV;
402 }
403
404 if (dev->state & DEV_MISCONFIGURED) {
405 em2820_videodbg("device misconfigured; close and open it again");
406 up(&dev->fileop_lock);
407 return -EIO;
408 }
409
410 if (dev->io == IO_MMAP) {
411 em2820_videodbg ("IO method is set to mmap; close and open"
412 " the device again to choose the read method");
413 up(&dev->fileop_lock);
414 return -EINVAL;
415 }
416
417 if (dev->io == IO_NONE) {
418 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
419 em2820_errdev("read failed, not enough memory\n");
420 up(&dev->fileop_lock);
421 return -ENOMEM;
422 }
423 dev->io = IO_READ;
424 dev->stream = STREAM_ON;
425 em2820_queue_unusedframes(dev);
426 }
427
428 if (!count) {
429 up(&dev->fileop_lock);
430 return 0;
431 }
432
433 if (list_empty(&dev->outqueue)) {
434 if (filp->f_flags & O_NONBLOCK) {
435 up(&dev->fileop_lock);
436 return -EAGAIN;
437 }
438 ret = wait_event_interruptible
439 (dev->wait_frame,
440 (!list_empty(&dev->outqueue)) ||
441 (dev->state & DEV_DISCONNECTED));
442 if (ret) {
443 up(&dev->fileop_lock);
444 return ret;
445 }
446 if (dev->state & DEV_DISCONNECTED) {
447 up(&dev->fileop_lock);
448 return -ENODEV;
449 }
450 }
451
452 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
453
454 spin_lock_irqsave(&dev->queue_lock, lock_flags);
455 list_for_each_entry(i, &dev->outqueue, frame)
456 i->state = F_UNUSED;
457 INIT_LIST_HEAD(&dev->outqueue);
458 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
459
460 em2820_queue_unusedframes(dev);
461
462 if (count > f->buf.length)
463 count = f->buf.length;
464
465 if (copy_to_user(buf, f->bufmem, count)) {
466 up(&dev->fileop_lock);
467 return -EFAULT;
468 }
469 *f_pos += count;
470
471 up(&dev->fileop_lock);
472
473 return count;
474}
475
476/*
477 * em2820_v4l2_poll()
478 * will allocate buffers when called for the first time
479 */
480static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
481{
482 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
483 unsigned int mask = 0;
484
485 if (down_interruptible(&dev->fileop_lock))
486 return POLLERR;
487
488 if (dev->state & DEV_DISCONNECTED) {
489 em2820_videodbg("device not present");
490 } else if (dev->state & DEV_MISCONFIGURED) {
491 em2820_videodbg("device is misconfigured; close and open it again");
492 } else {
493 if (dev->io == IO_NONE) {
494 if (!em2820_request_buffers
495 (dev, EM2820_NUM_READ_FRAMES)) {
496 em2820_warn
497 ("poll() failed, not enough memory\n");
498 } else {
499 dev->io = IO_READ;
500 dev->stream = STREAM_ON;
501 }
502 }
503
504 if (dev->io == IO_READ) {
505 em2820_queue_unusedframes(dev);
506 poll_wait(filp, &dev->wait_frame, wait);
507
508 if (!list_empty(&dev->outqueue))
509 mask |= POLLIN | POLLRDNORM;
510
511 up(&dev->fileop_lock);
512
513 return mask;
514 }
515 }
516
517 up(&dev->fileop_lock);
518 return POLLERR;
519}
520
521/*
522 * em2820_vm_open()
523 */
524static void em2820_vm_open(struct vm_area_struct *vma)
525{
526 struct em2820_frame_t *f = vma->vm_private_data;
527 f->vma_use_count++;
528}
529
530/*
531 * em2820_vm_close()
532 */
533static void em2820_vm_close(struct vm_area_struct *vma)
534{
535 /* NOTE: buffers are not freed here */
536 struct em2820_frame_t *f = vma->vm_private_data;
537 f->vma_use_count--;
538}
539
540static struct vm_operations_struct em2820_vm_ops = {
541 .open = em2820_vm_open,
542 .close = em2820_vm_close,
543};
544
545/*
546 * em2820_v4l2_mmap()
547 */
548static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
549{
550 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
551 unsigned long size = vma->vm_end - vma->vm_start,
552 start = vma->vm_start, pos, page;
553 u32 i;
554 if (down_interruptible(&dev->fileop_lock))
555 return -ERESTARTSYS;
556
557 if (dev->state & DEV_DISCONNECTED) {
558 em2820_videodbg("mmap: device not present");
559 up(&dev->fileop_lock);
560 return -ENODEV;
561 }
562
563 if (dev->state & DEV_MISCONFIGURED) {
564 em2820_videodbg ("mmap: Device is misconfigured; close and "
565 "open it again");
566 up(&dev->fileop_lock);
567 return -EIO;
568 }
569
570 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
571 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
572 up(&dev->fileop_lock);
573 return -EINVAL;
574 }
575
576 for (i = 0; i < dev->num_frames; i++) {
577 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
578 break;
579 }
580 if (i == dev->num_frames) {
581 em2820_videodbg("mmap: user supplied mapping address is out of range");
582 up(&dev->fileop_lock);
583 return -EINVAL;
584 }
585
586 /* VM_IO is eventually going to replace PageReserved altogether */
587 vma->vm_flags |= VM_IO;
588 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
589
590 pos = (unsigned long)dev->frame[i].bufmem;
591 while (size > 0) { /* size is page-aligned */
592 page = vmalloc_to_pfn((void *)pos);
593 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
594 vma->vm_page_prot)) {
595 em2820_videodbg("mmap: rename page map failed");
596 up(&dev->fileop_lock);
597 return -EAGAIN;
598 }
599 start += PAGE_SIZE;
600 pos += PAGE_SIZE;
601 size -= PAGE_SIZE;
602 }
603
604 vma->vm_ops = &em2820_vm_ops;
605 vma->vm_private_data = &dev->frame[i];
606
607 em2820_vm_open(vma);
608 up(&dev->fileop_lock);
609 return 0;
610}
611
612/*
613 * em2820_get_ctrl()
614 * return the current saturation, brightness or contrast, mute state
615 */
616static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
617{
618 s32 tmp;
619 switch (ctrl->id) {
620 case V4L2_CID_AUDIO_MUTE:
621 ctrl->value = dev->mute;
622 return 0;
623 case V4L2_CID_AUDIO_VOLUME:
624 ctrl->value = dev->volume;
625 return 0;
626 case V4L2_CID_BRIGHTNESS:
627 if ((tmp = em2820_brightness_get(dev)) < 0)
628 return -EIO;
629 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
630 return 0;
631 case V4L2_CID_CONTRAST:
632 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
633 return -EIO;
634 return 0;
635 case V4L2_CID_SATURATION:
636 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
637 return -EIO;
638 return 0;
639 case V4L2_CID_RED_BALANCE:
640 if ((tmp = em2820_v_balance_get(dev)) < 0)
641 return -EIO;
642 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
643 return 0;
644 case V4L2_CID_BLUE_BALANCE:
645 if ((tmp = em2820_u_balance_get(dev)) < 0)
646 return -EIO;
647 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
648 return 0;
649 case V4L2_CID_GAMMA:
650 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
651 return -EIO;
652 return 0;
653 default:
654 return -EINVAL;
655 }
656}
657
658/*
659 * em2820_set_ctrl()
660 * mute or set new saturation, brightness or contrast
661 */
662static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
663{
664 switch (ctrl->id) {
665 case V4L2_CID_AUDIO_MUTE:
666 if (ctrl->value != dev->mute) {
667 dev->mute = ctrl->value;
668 em2820_audio_usb_mute(dev, ctrl->value);
669 return em2820_audio_analog_set(dev);
670 }
671 return 0;
672 case V4L2_CID_AUDIO_VOLUME:
673 dev->volume = ctrl->value;
674 return em2820_audio_analog_set(dev);
675 case V4L2_CID_BRIGHTNESS:
676 return em2820_brightness_set(dev, ctrl->value);
677 case V4L2_CID_CONTRAST:
678 return em2820_contrast_set(dev, ctrl->value);
679 case V4L2_CID_SATURATION:
680 return em2820_saturation_set(dev, ctrl->value);
681 case V4L2_CID_RED_BALANCE:
682 return em2820_v_balance_set(dev, ctrl->value);
683 case V4L2_CID_BLUE_BALANCE:
684 return em2820_u_balance_set(dev, ctrl->value);
685 case V4L2_CID_GAMMA:
686 return em2820_gamma_set(dev, ctrl->value);
687 default:
688 return -EINVAL;
689 }
690}
691
692/*
693 * em2820_stream_interrupt()
694 * stops streaming
695 */
696static int em2820_stream_interrupt(struct em2820 *dev)
697{
698 int ret = 0;
699
700 /* stop reading from the device */
701
702 dev->stream = STREAM_INTERRUPT;
703 ret = wait_event_timeout(dev->wait_stream,
704 (dev->stream == STREAM_OFF) ||
705 (dev->state & DEV_DISCONNECTED),
706 EM2820_URB_TIMEOUT);
707 if (dev->state & DEV_DISCONNECTED)
708 return -ENODEV;
709 else if (ret) {
710 dev->state |= DEV_MISCONFIGURED;
711 em2820_videodbg("device is misconfigured; close and "
712 "open /dev/video%d again", dev->vdev->minor);
713 return ret;
714 }
715
716 return 0;
717}
718
719static int em2820_set_norm(struct em2820 *dev, int width, int height)
720{
721 unsigned int hscale, vscale;
722 unsigned int maxh, maxw;
723
724 maxw = norm_maxw(dev);
725 maxh = norm_maxh(dev);
726
727 /* width must even because of the YUYV format */
728 /* height must be even because of interlacing */
729 height &= 0xfffe;
730 width &= 0xfffe;
731
732 if (height < 32)
733 height = 32;
734 if (height > maxh)
735 height = maxh;
736 if (width < 48)
737 width = 48;
738 if (width > maxw)
739 width = maxw;
740
741 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
742 hscale = 0x3fff;
743 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
744
745 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
746 vscale = 0x3fff;
747 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
748
749 /* set new image size */
750 dev->width = width;
751 dev->height = height;
752 dev->frame_size = dev->width * dev->height * 2;
753 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
754 dev->bytesperline = dev->width * 2;
755 dev->hscale = hscale;
756 dev->vscale = vscale;
757
758 em2820_resolution_set(dev);
759
760 return 0;
761}
762
763static void video_mux(struct em2820 *dev, int index)
764{
765 int input, ainput;
766
767 input = INPUT(index)->vmux;
768 dev->ctl_input = index;
769
770 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
771
772 dev->ctl_ainput = INPUT(index)->amux;
773
774 switch (dev->ctl_ainput) {
775 case 0:
776 ainput = EM2820_AUDIO_SRC_TUNER;
777 break;
778 default:
779 ainput = EM2820_AUDIO_SRC_LINE;
780 }
781
782 em2820_audio_source(dev, ainput);
783}
784
785/*
786 * em2820_v4l2_do_ioctl()
787 * This function is _not_ called directly, but from
788 * em2820_v4l2_ioctl. Userspace
789 * copying is done already, arg is a kernel pointer.
790 */
791static int em2820_do_ioctl(struct inode *inode, struct file *filp,
792 struct em2820 *dev, unsigned int cmd, void *arg,
793 v4l2_kioctl driver_ioctl)
794{
795 int ret;
796
797 switch (cmd) {
798 /* ---------- tv norms ---------- */
799 case VIDIOC_ENUMSTD:
800 {
801 struct v4l2_standard *e = arg;
802 unsigned int i;
803
804 i = e->index;
805 if (i >= TVNORMS)
806 return -EINVAL;
807 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
808 tvnorms[e->index].name);
809 e->index = i;
810 if (ret < 0)
811 return ret;
812 return 0;
813 }
814 case VIDIOC_G_STD:
815 {
816 v4l2_std_id *id = arg;
817
818 *id = dev->tvnorm->id;
819 return 0;
820 }
821 case VIDIOC_S_STD:
822 {
823 v4l2_std_id *id = arg;
824 unsigned int i;
825
826 for (i = 0; i < TVNORMS; i++)
827 if (*id == tvnorms[i].id)
828 break;
829 if (i == TVNORMS)
830 for (i = 0; i < TVNORMS; i++)
831 if (*id & tvnorms[i].id)
832 break;
833 if (i == TVNORMS)
834 return -EINVAL;
835
836 down(&dev->lock);
837 dev->tvnorm = &tvnorms[i];
838
839 em2820_set_norm(dev, dev->width, dev->height);
840
841/*
842 dev->width=norm_maxw(dev);
843 dev->height=norm_maxh(dev);
844 dev->frame_size=dev->width*dev->height*2;
845 dev->field_size=dev->frame_size>>1;
846 dev->bytesperline=dev->width*2;
847 dev->hscale=0;
848 dev->vscale=0;
849
850 em2820_resolution_set(dev);
851*/
852/*
853 em2820_uninit_isoc(dev);
854 em2820_set_alternate(dev);
855 em2820_capture_start(dev, 1);
856 em2820_resolution_set(dev);
857 em2820_init_isoc(dev);
858*/
859 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
860 &tvnorms[i].mode);
861 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
862 &dev->tvnorm->id);
863
864 up(&dev->lock);
865
866 return 0;
867 }
868
869 /* ------ input switching ---------- */
870 case VIDIOC_ENUMINPUT:
871 {
872 struct v4l2_input *i = arg;
873 unsigned int n;
874 static const char *iname[] = {
875 [EM2820_VMUX_COMPOSITE1] = "Composite1",
876 [EM2820_VMUX_COMPOSITE2] = "Composite2",
877 [EM2820_VMUX_COMPOSITE3] = "Composite3",
878 [EM2820_VMUX_COMPOSITE4] = "Composite4",
879 [EM2820_VMUX_SVIDEO] = "S-Video",
880 [EM2820_VMUX_TELEVISION] = "Television",
881 [EM2820_VMUX_CABLE] = "Cable TV",
882 [EM2820_VMUX_DVB] = "DVB",
883 [EM2820_VMUX_DEBUG] = "for debug only",
884 };
885
886 n = i->index;
887 if (n >= MAX_EM2820_INPUT)
888 return -EINVAL;
889 if (0 == INPUT(n)->type)
890 return -EINVAL;
891 memset(i, 0, sizeof(*i));
892 i->index = n;
893 i->type = V4L2_INPUT_TYPE_CAMERA;
894 strcpy(i->name, iname[INPUT(n)->type]);
895 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
896 (EM2820_VMUX_CABLE == INPUT(n)->type))
897 i->type = V4L2_INPUT_TYPE_TUNER;
898 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
899 i->std |= tvnorms[n].id;
900 return 0;
901 }
902
903 case VIDIOC_G_INPUT:
904 {
905 int *i = arg;
906 *i = dev->ctl_input;
907
908 return 0;
909 }
910
911 case VIDIOC_S_INPUT:
912 {
913 int *index = arg;
914
915 if (*index >= MAX_EM2820_INPUT)
916 return -EINVAL;
917 if (0 == INPUT(*index)->type)
918 return -EINVAL;
919
920 down(&dev->lock);
921 video_mux(dev, *index);
922 up(&dev->lock);
923
924 return 0;
925 }
926
927 case VIDIOC_G_AUDIO:
928 {
929 struct v4l2_audio *a = arg;
930 unsigned int index = a->index;
931
932 if (a->index > 1)
933 return -EINVAL;
934 memset(a, 0, sizeof(*a));
935 index = dev->ctl_ainput;
936
937 if (index == 0) {
938 strcpy(a->name, "Television");
939 } else {
940 strcpy(a->name, "Line In");
941 }
942 a->capability = V4L2_AUDCAP_STEREO;
943 a->index = index;
944 return 0;
945 }
946
947 case VIDIOC_S_AUDIO:
948 {
949 struct v4l2_audio *a = arg;
950 if (a->index != dev->ctl_ainput)
951 return -EINVAL;
952
953 return 0;
954 }
955
956 /* --- controls ---------------------------------------------- */
957 case VIDIOC_QUERYCTRL:
958 {
959 struct v4l2_queryctrl *qc = arg;
960 u8 i, n;
961 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
962 for (i = 0; i < n; i++)
963 if (qc->id && qc->id == em2820_qctrl[i].id) {
964 memcpy(qc, &(em2820_qctrl[i]),
965 sizeof(*qc));
966 return 0;
967 }
968
969 return -EINVAL;
970 }
971
972 case VIDIOC_G_CTRL:
973 {
974 struct v4l2_control *ctrl = arg;
975
976
977 return em2820_get_ctrl(dev, ctrl);
978 }
979
980 case VIDIOC_S_CTRL_OLD: /* ??? */
981 case VIDIOC_S_CTRL:
982 {
983 struct v4l2_control *ctrl = arg;
984 u8 i, n;
985
986
987 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
988 for (i = 0; i < n; i++)
989 if (ctrl->id == em2820_qctrl[i].id) {
990 if (ctrl->value <
991 em2820_qctrl[i].minimum
992 || ctrl->value >
993 em2820_qctrl[i].maximum)
994 return -ERANGE;
995
996 return em2820_set_ctrl(dev, ctrl);
997 }
998 return -EINVAL;
999 }
1000
1001 /* --- tuner ioctls ------------------------------------------ */
1002 case VIDIOC_G_TUNER:
1003 {
1004 struct v4l2_tuner *t = arg;
1005 int status = 0;
1006
1007 if (0 != t->index)
1008 return -EINVAL;
1009
1010 memset(t, 0, sizeof(*t));
1011 strcpy(t->name, "Tuner");
1012 t->type = V4L2_TUNER_ANALOG_TV;
1013 t->capability = V4L2_TUNER_CAP_NORM;
1014 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1015/* t->signal = 0xffff;*/
1016/* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1017 /* No way to get signal strength? */
1018 down(&dev->lock);
1019 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1020 &status);
1021 up(&dev->lock);
1022 t->signal =
1023 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1024
1025 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1026 t->afc);
1027 return 0;
1028 }
1029 case VIDIOC_S_TUNER:
1030 {
1031 struct v4l2_tuner *t = arg;
1032 int status = 0;
1033
1034 if (0 != t->index)
1035 return -EINVAL;
1036 memset(t, 0, sizeof(*t));
1037 strcpy(t->name, "Tuner");
1038 t->type = V4L2_TUNER_ANALOG_TV;
1039 t->capability = V4L2_TUNER_CAP_NORM;
1040 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1041/* t->signal = 0xffff; */
1042 /* No way to get signal strength? */
1043 down(&dev->lock);
1044 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1045 &status);
1046 up(&dev->lock);
1047 t->signal =
1048 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1049
1050 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1051 t->signal, t->afc);
1052 return 0;
1053 }
1054 case VIDIOC_G_FREQUENCY:
1055 {
1056 struct v4l2_frequency *f = arg;
1057
1058 memset(f, 0, sizeof(*f));
1059 f->type = V4L2_TUNER_ANALOG_TV;
1060 f->frequency = dev->ctl_freq;
1061
1062 return 0;
1063 }
1064 case VIDIOC_S_FREQUENCY:
1065 {
1066 struct v4l2_frequency *f = arg;
1067
1068 if (0 != f->tuner)
1069 return -EINVAL;
1070
1071 if (V4L2_TUNER_ANALOG_TV != f->type)
1072 return -EINVAL;
1073
1074 down(&dev->lock);
1075 dev->ctl_freq = f->frequency;
1076 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1077 up(&dev->lock);
1078 return 0;
1079 }
1080
1081 case VIDIOC_CROPCAP:
1082 {
1083 struct v4l2_cropcap *cc = arg;
1084
1085 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1086 return EINVAL;
1087 cc->bounds.left = 0;
1088 cc->bounds.top = 0;
1089 cc->bounds.width = dev->width;
1090 cc->bounds.height = dev->height;
1091 cc->defrect = cc->bounds;
1092 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1093 cc->pixelaspect.denominator = 59;
1094 return 0;
1095 }
1096 case VIDIOC_STREAMON:
1097 {
1098 int *type = arg;
1099
1100 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1101 || dev->io != IO_MMAP)
1102 return -EINVAL;
1103
1104 if (list_empty(&dev->inqueue))
1105 return -EINVAL;
1106
1107 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1108
1109 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1110
1111 return 0;
1112 }
1113 case VIDIOC_STREAMOFF:
1114 {
1115 int *type = arg;
1116 int ret;
1117
1118 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1119 || dev->io != IO_MMAP)
1120 return -EINVAL;
1121
1122 if (dev->stream == STREAM_ON) {
1123 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1124 if ((ret = em2820_stream_interrupt(dev)))
1125 return ret;
1126 }
1127 em2820_empty_framequeues(dev);
1128
1129 return 0;
1130 }
1131 default:
1132 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1133 driver_ioctl);
1134 }
1135 return 0;
1136}
1137
1138/*
1139 * em2820_v4l2_do_ioctl()
1140 * This function is _not_ called directly, but from
1141 * em2820_v4l2_ioctl. Userspace
1142 * copying is done already, arg is a kernel pointer.
1143 */
1144static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1145 unsigned int cmd, void *arg)
1146{
1147 struct em2820 *dev = filp->private_data;
1148
1149 if (!dev)
1150 return -ENODEV;
1151
1152 if (video_debug > 1)
1153 em2820_print_ioctl(dev->name,cmd);
1154
1155 switch (cmd) {
1156
1157 /* --- capabilities ------------------------------------------ */
1158 case VIDIOC_QUERYCAP:
1159 {
1160 struct v4l2_capability *cap = arg;
1161
1162 memset(cap, 0, sizeof(*cap));
1163 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1164 strlcpy(cap->card, em2820_boards[dev->model].name,
1165 sizeof(cap->card));
1166 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1167 sizeof(cap->bus_info));
1168 cap->version = EM2820_VERSION_CODE;
1169 cap->capabilities =
1170 V4L2_CAP_VIDEO_CAPTURE |
1171 V4L2_CAP_AUDIO |
1172 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1173 if (dev->has_tuner)
1174 cap->capabilities |= V4L2_CAP_TUNER;
1175 return 0;
1176 }
1177
1178 /* --- capture ioctls ---------------------------------------- */
1179 case VIDIOC_ENUM_FMT:
1180 {
1181 struct v4l2_fmtdesc *fmtd = arg;
1182
1183 if (fmtd->index != 0)
1184 return -EINVAL;
1185 memset(fmtd, 0, sizeof(*fmtd));
1186 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1187 strcpy(fmtd->description, "Packed YUY2");
1188 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1189 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1190 return 0;
1191 }
1192
1193 case VIDIOC_G_FMT:
1194 {
1195 struct v4l2_format *format = arg;
1196
1197 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1198 format->type ==
1199 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1200 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1201 V4L2_BUF_TYPE_VBI_CAPTURE ?
1202 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1203 "not supported");
1204
1205 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 return -EINVAL;
1207
1208 format->fmt.pix.width = dev->width;
1209 format->fmt.pix.height = dev->height;
1210 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1211 format->fmt.pix.bytesperline = dev->bytesperline;
1212 format->fmt.pix.sizeimage = dev->frame_size;
1213 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1214 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1215
1216 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1217 dev->height);
1218 return 0;
1219 }
1220
1221 case VIDIOC_TRY_FMT:
1222 case VIDIOC_S_FMT:
1223 {
1224 struct v4l2_format *format = arg;
1225 u32 i;
1226 int ret = 0;
1227 int width = format->fmt.pix.width;
1228 int height = format->fmt.pix.height;
1229 unsigned int hscale, vscale;
1230 unsigned int maxh, maxw;
1231
1232 maxw = norm_maxw(dev);
1233 maxh = norm_maxh(dev);
1234
1235/* int both_fields; */
1236
1237 em2820_videodbg("%s: type=%s",
1238 cmd ==
1239 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1240 "VIDIOC_S_FMT",
1241 format->type ==
1242 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1243 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1244 V4L2_BUF_TYPE_VBI_CAPTURE ?
1245 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1246 "not supported");
1247
1248 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1249 return -EINVAL;
1250
1251 em2820_videodbg("%s: requested %dx%d",
1252 cmd ==
1253 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1254 "VIDIOC_S_FMT", format->fmt.pix.width,
1255 format->fmt.pix.height);
1256
1257 /* FIXME: Move some code away from here */
1258 /* width must even because of the YUYV format */
1259 /* height must be even because of interlacing */
1260 height &= 0xfffe;
1261 width &= 0xfffe;
1262
1263 if (height < 32)
1264 height = 32;
1265 if (height > maxh)
1266 height = maxh;
1267 if (width < 48)
1268 width = 48;
1269 if (width > maxw)
1270 width = maxw;
1271
1272 if ((hscale =
1273 (((unsigned long)maxw) << 12) / width - 4096L) >=
1274 0x4000)
1275 hscale = 0x3fff;
1276 width =
1277 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1278
1279 if ((vscale =
1280 (((unsigned long)maxh) << 12) / height - 4096L) >=
1281 0x4000)
1282 vscale = 0x3fff;
1283 height =
1284 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1285
1286 format->fmt.pix.width = width;
1287 format->fmt.pix.height = height;
1288 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1289 format->fmt.pix.bytesperline = width * 2;
1290 format->fmt.pix.sizeimage = width * 2 * height;
1291 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1292 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1293
1294 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1295 cmd ==
1296 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1297 "VIDIOC_S_FMT", format->fmt.pix.width,
1298 format->fmt.pix.height, hscale, vscale);
1299
1300 if (cmd == VIDIOC_TRY_FMT)
1301 return 0;
1302
1303 for (i = 0; i < dev->num_frames; i++)
1304 if (dev->frame[i].vma_use_count) {
1305 em2820_videodbg("VIDIOC_S_FMT failed. "
1306 "Unmap the buffers first.");
1307 return -EINVAL;
1308 }
1309
1310 /* stop io in case it is already in progress */
1311 if (dev->stream == STREAM_ON) {
1312 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1313 if ((ret = em2820_stream_interrupt(dev)))
1314 return ret;
1315 }
1316
1317 em2820_release_buffers(dev);
1318 dev->io = IO_NONE;
1319
1320 /* set new image size */
1321 dev->width = width;
1322 dev->height = height;
1323 dev->frame_size = dev->width * dev->height * 2;
1324 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1325 dev->bytesperline = dev->width * 2;
1326 dev->hscale = hscale;
1327 dev->vscale = vscale;
1328/* dev->both_fileds = both_fileds; */
1329 em2820_uninit_isoc(dev);
1330 em2820_set_alternate(dev);
1331 em2820_capture_start(dev, 1);
1332 em2820_resolution_set(dev);
1333 em2820_init_isoc(dev);
1334
1335 return 0;
1336 }
1337
1338 /* --- streaming capture ------------------------------------- */
1339 case VIDIOC_REQBUFS:
1340 {
1341 struct v4l2_requestbuffers *rb = arg;
1342 u32 i;
1343 int ret;
1344
1345 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1346 rb->memory != V4L2_MEMORY_MMAP)
1347 return -EINVAL;
1348
1349 if (dev->io == IO_READ) {
1350 em2820_videodbg ("method is set to read;"
1351 " close and open the device again to"
1352 " choose the mmap I/O method");
1353 return -EINVAL;
1354 }
1355
1356 for (i = 0; i < dev->num_frames; i++)
1357 if (dev->frame[i].vma_use_count) {
1358 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1359 return -EINVAL;
1360 }
1361
1362 if (dev->stream == STREAM_ON) {
1363 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1364 if ((ret = em2820_stream_interrupt(dev)))
1365 return ret;
1366 }
1367
1368 em2820_empty_framequeues(dev);
1369
1370 em2820_release_buffers(dev);
1371 if (rb->count)
1372 rb->count =
1373 em2820_request_buffers(dev, rb->count);
1374
1375 dev->frame_current = NULL;
1376
1377 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1378 rb->count);
1379 dev->io = rb->count ? IO_MMAP : IO_NONE;
1380 return 0;
1381 }
1382
1383 case VIDIOC_QUERYBUF:
1384 {
1385 struct v4l2_buffer *b = arg;
1386
1387 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1388 b->index >= dev->num_frames || dev->io != IO_MMAP)
1389 return -EINVAL;
1390
1391 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1392
1393 if (dev->frame[b->index].vma_use_count) {
1394 b->flags |= V4L2_BUF_FLAG_MAPPED;
1395 }
1396 if (dev->frame[b->index].state == F_DONE)
1397 b->flags |= V4L2_BUF_FLAG_DONE;
1398 else if (dev->frame[b->index].state != F_UNUSED)
1399 b->flags |= V4L2_BUF_FLAG_QUEUED;
1400 return 0;
1401 }
1402 case VIDIOC_QBUF:
1403 {
1404 struct v4l2_buffer *b = arg;
1405 unsigned long lock_flags;
1406
1407 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1408 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1409 return -EINVAL;
1410 }
1411
1412 if (dev->frame[b->index].state != F_UNUSED) {
1413 return -EAGAIN;
1414 }
1415 dev->frame[b->index].state = F_QUEUED;
1416
1417 /* add frame to fifo */
1418 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1419 list_add_tail(&dev->frame[b->index].frame,
1420 &dev->inqueue);
1421 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1422
1423 return 0;
1424 }
1425 case VIDIOC_DQBUF:
1426 {
1427 struct v4l2_buffer *b = arg;
1428 struct em2820_frame_t *f;
1429 unsigned long lock_flags;
1430 int ret = 0;
1431
1432 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1433 || dev->io != IO_MMAP)
1434 return -EINVAL;
1435
1436 if (list_empty(&dev->outqueue)) {
1437 if (dev->stream == STREAM_OFF)
1438 return -EINVAL;
1439 if (filp->f_flags & O_NONBLOCK)
1440 return -EAGAIN;
1441 ret = wait_event_interruptible
1442 (dev->wait_frame,
1443 (!list_empty(&dev->outqueue)) ||
1444 (dev->state & DEV_DISCONNECTED));
1445 if (ret)
1446 return ret;
1447 if (dev->state & DEV_DISCONNECTED)
1448 return -ENODEV;
1449 }
1450
1451 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1452 f = list_entry(dev->outqueue.next,
1453 struct em2820_frame_t, frame);
1454 list_del(dev->outqueue.next);
1455 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1456
1457 f->state = F_UNUSED;
1458 memcpy(b, &f->buf, sizeof(*b));
1459
1460 if (f->vma_use_count)
1461 b->flags |= V4L2_BUF_FLAG_MAPPED;
1462
1463 return 0;
1464 }
1465 default:
1466 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1467 em2820_video_do_ioctl);
1468 }
1469 return 0;
1470}
1471
1472/*
1473 * em2820_v4l2_ioctl()
1474 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1475 */
1476static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1477 unsigned int cmd, unsigned long arg)
1478{
1479 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
1480 int ret = 0;
1481
1482 if (down_interruptible(&dev->fileop_lock))
1483 return -ERESTARTSYS;
1484
1485 if (dev->state & DEV_DISCONNECTED) {
1486 em2820_errdev("v4l2 ioctl: device not present\n");
1487 up(&dev->fileop_lock);
1488 return -ENODEV;
1489 }
1490
1491 if (dev->state & DEV_MISCONFIGURED) {
1492 em2820_errdev
1493 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1494 up(&dev->fileop_lock);
1495 return -EIO;
1496 }
1497
1498 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1499
1500 up(&dev->fileop_lock);
1501
1502 return ret;
1503}
1504
1505static struct file_operations em2820_v4l_fops = {
1506 .owner = THIS_MODULE,
1507 .open = em2820_v4l2_open,
1508 .release = em2820_v4l2_close,
1509 .ioctl = em2820_v4l2_ioctl,
1510 .read = em2820_v4l2_read,
1511 .poll = em2820_v4l2_poll,
1512 .mmap = em2820_v4l2_mmap,
1513 .llseek = no_llseek,
1514};
1515
1516/******************************** usb interface *****************************************/
1517
1518/*
1519 * em2820_init_dev()
1520 * allocates and inits the device structs, registers i2c bus and v4l device
1521 */
1522static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1523 int minor, int model)
1524{
1525 struct em2820 *dev;
1526 int retval = -ENOMEM;
1527 int errCode, i;
1528 unsigned int maxh, maxw;
1529 struct usb_interface *uif;
1530
1531 /* allocate memory for our device state and initialize it */
1532 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1533 if (dev == NULL) {
1534 em2820_err(DRIVER_NAME ": out of memory!\n");
1535 return -ENOMEM;
1536 }
1537 memset(dev, 0x00, sizeof(*dev));
1538
1539 snprintf(dev->name, 29, "em2820 #%d", minor);
1540 dev->udev = udev;
1541 dev->model = model;
1542 init_MUTEX(&dev->lock);
1543 init_waitqueue_head(&dev->open);
1544
1545 dev->em2820_write_regs = em2820_write_regs;
1546 dev->em2820_read_reg = em2820_read_reg;
1547 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1548 dev->em2820_write_regs_req = em2820_write_regs_req;
1549 dev->em2820_read_reg_req = em2820_read_reg_req;
1550 dev->has_tuner = em2820_boards[model].has_tuner;
1551 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1552 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1553 dev->decoder = em2820_boards[model].decoder;
1554
1555 if (tuner >= 0)
1556 dev->tuner_type = tuner;
1557 else
1558 dev->tuner_type = em2820_boards[model].tuner_type;
1559
1560 dev->video_inputs = em2820_boards[model].vchannels;
1561
1562 for (i = 0; i < TVNORMS; i++)
1563 if (em2820_boards[model].norm == tvnorms[i].mode)
1564 break;
1565 if (i == TVNORMS)
1566 i = 0;
1567
1568 dev->tvnorm = &tvnorms[i]; /* set default norm */
1569
1570 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1571
1572 maxw = norm_maxw(dev);
1573 maxh = norm_maxh(dev);
1574
1575 /* set default image size */
1576 dev->width = maxw;
1577 dev->height = maxh;
1578 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1579 dev->field_size = dev->width * dev->height;
1580 dev->frame_size =
1581 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1582 dev->bytesperline = dev->width * 2;
1583 dev->hscale = 0;
1584 dev->vscale = 0;
1585 dev->ctl_input = 2;
1586
1587 /* setup video picture settings for saa7113h */
1588 memset(&dev->vpic, 0, sizeof(dev->vpic));
1589 dev->vpic.colour = 128 << 8;
1590 dev->vpic.hue = 128 << 8;
1591 dev->vpic.brightness = 128 << 8;
1592 dev->vpic.contrast = 192 << 8;
1593 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1594 dev->vpic.depth = 16;
1595 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1596
1597 /* compute alternate max packet sizes */
1598 uif = dev->udev->actconfig->interface[0];
1599 dev->alt_max_pkt_size[0] = 0;
1600 for (i = 1; i <= EM2820_MAX_ALT; i++) {
1601 u16 tmp =
1602 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1603 wMaxPacketSize);
1604 dev->alt_max_pkt_size[i] =
1605 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1606 }
1607
1608#ifdef CONFIG_MODULES
1609 /* request some modules */
1610 if (dev->decoder == EM2820_SAA7113)
1611 request_module("saa7113");
1612 if (dev->decoder == EM2820_SAA7114)
1613 request_module("saa7114");
1614 if (dev->decoder == EM2820_TVP5150)
1615 request_module("tvp5150");
1616 if (dev->has_tuner)
1617 request_module("tuner");
1618 if (dev->tda9887_conf)
1619 request_module("tda9887");
1620#endif
1621 errCode = em2820_config(dev);
1622 if (errCode) {
1623 em2820_errdev("error configuring device\n");
1624 kfree(dev);
1625 return -ENOMEM;
1626 }
1627
1628 down(&dev->lock);
1629 /* register i2c bus */
1630 em2820_i2c_register(dev);
1631
1632 /* Do board specific init and eeprom reading */
1633 em2820_card_setup(dev);
1634
1635 /* configure the device */
1636 em2820_config_i2c(dev);
1637
1638 up(&dev->lock);
1639
1640 errCode = em2820_config(dev);
1641
1642#ifdef CONFIG_MODULES
1643 if (dev->has_msp34xx)
1644 request_module("msp3400");
1645#endif
1646 /* allocate and fill v4l2 device struct */
1647 dev->vdev = video_device_alloc();
1648 if (NULL == dev->vdev) {
1649 em2820_errdev("cannot allocate video_device.\n");
1650 kfree(dev);
1651 return -ENOMEM;
1652 }
1653
1654 dev->vdev->owner = THIS_MODULE;
1655 dev->vdev->type = VID_TYPE_CAPTURE;
1656 if (dev->has_tuner)
1657 dev->vdev->type |= VID_TYPE_TUNER;
1658 dev->vdev->hardware = 0;
1659 dev->vdev->fops = &em2820_v4l_fops;
1660 dev->vdev->minor = -1;
1661 dev->vdev->dev = &dev->udev->dev;
1662 dev->vdev->release = video_device_release;
1663 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1664 "em2820 video");
1665 video_set_drvdata(dev->vdev, dev);
1666
1667 /* register v4l2 device */
1668 down(&dev->lock);
1669 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1670 em2820_errdev("unable to register video device (error=%i).\n",
1671 retval);
1672 up(&dev->lock);
1673 video_set_drvdata(dev->vdev, NULL);
1674 video_device_release(dev->vdev);
1675 kfree(dev);
1676 return -ENODEV;
1677 }
1678 if (dev->has_msp34xx) {
1679 /* Send a reset to other chips via gpio */
1680 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1681 udelay(2500);
1682 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1683 udelay(2500);
1684
1685 }
1686 video_mux(dev, 0);
1687
1688 up(&dev->lock);
1689
1690 em2820_info("V4L2 device registered as /dev/video%d\n",
1691 dev->vdev->minor);
1692
1693 *devhandle = dev;
1694 return 0;
1695}
1696
1697/*
1698 * em2820_usb_probe()
1699 * checks for supported devices
1700 */
1701static int em2820_usb_probe(struct usb_interface *interface,
1702 const struct usb_device_id *id)
1703{
1704 const struct usb_endpoint_descriptor *endpoint;
1705 struct usb_device *udev;
1706 struct em2820 *dev = NULL;
1707 int retval = -ENODEV;
1708
1709 udev = usb_get_dev(interface_to_usbdev(interface));
1710 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1711
1712 /* check if the the device has the iso in endpoint at the correct place */
1713 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1714 USB_ENDPOINT_XFER_ISOC) {
1715/* em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); */
1716 return -ENODEV;
1717 }
1718 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1719/* em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); */
1720 return -ENODEV;
1721 }
1722
1723 /* allocate device struct */
1724 retval = em2820_init_dev(&dev, udev, interface->minor, id->driver_info);
1725 if (retval)
1726 return retval;
1727
1728 em2820_info("Found %s\n", em2820_boards[id->driver_info].name);
1729
1730 /* save our data pointer in this interface device */
1731 usb_set_intfdata(interface, dev);
1732 return 0;
1733}
1734
1735/*
1736 * em2820_usb_disconnect()
1737 * called when the device gets diconencted
1738 * video device will be unregistered on v4l2_close in case it is still open
1739 */
1740static void em2820_usb_disconnect(struct usb_interface *interface)
1741{
1742 struct em2820 *dev = usb_get_intfdata(interface);
1743 usb_set_intfdata(interface, NULL);
1744
1745 if (!dev)
1746 return;
1747
1748 down_write(&em2820_disconnect);
1749
1750 down(&dev->lock);
1751
1752 em2820_info("disconnecting %s\n", dev->vdev->name);
1753
1754 wake_up_interruptible_all(&dev->open);
1755
1756 if (dev->users) {
1757 em2820_warn
1758 ("device /dev/video%d is open! Deregistration and memory "
1759 "deallocation are deferred on close.\n", dev->vdev->minor);
1760 dev->state |= DEV_MISCONFIGURED;
1761 em2820_uninit_isoc(dev);
1762 dev->state |= DEV_DISCONNECTED;
1763 wake_up_interruptible(&dev->wait_frame);
1764 wake_up_interruptible(&dev->wait_stream);
1765 } else {
1766 dev->state |= DEV_DISCONNECTED;
1767 em2820_release_resources(dev);
1768 }
1769
1770 up(&dev->lock);
1771
1772 if (!dev->users)
1773 kfree(dev);
1774
1775 up_write(&em2820_disconnect);
1776
1777}
1778
1779static struct usb_driver em2820_usb_driver = {
1780 .owner = THIS_MODULE,
1781 .name = "em2820",
1782 .probe = em2820_usb_probe,
1783 .disconnect = em2820_usb_disconnect,
1784 .id_table = em2820_id_table,
1785};
1786
1787static int __init em2820_module_init(void)
1788{
1789 int result;
1790
1791 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1792 (EM2820_VERSION_CODE >> 16) & 0xff,
1793 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1794#ifdef SNAPSHOT
1795 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1796 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1797#endif
1798
1799 /* register this driver with the USB subsystem */
1800 result = usb_register(&em2820_usb_driver);
1801 if (result)
1802 em2820_err(DRIVER_NAME
1803 " usb_register failed. Error number %d.\n", result);
1804
1805 return result;
1806}
1807
1808static void __exit em2820_module_exit(void)
1809{
1810 /* deregister this driver with the USB subsystem */
1811 usb_deregister(&em2820_usb_driver);
1812}
1813
1814module_init(em2820_module_init);
1815module_exit(em2820_module_exit);
diff --git a/drivers/media/video/em28xx/em28xx.h b/drivers/media/video/em28xx/em28xx.h
new file mode 100644
index 000000000000..7779121a3dea
--- /dev/null
+++ b/drivers/media/video/em28xx/em28xx.h
@@ -0,0 +1,479 @@
1/*
2 em2820-cards.c - driver for Empia EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#ifndef _EM2820_H
26#define _EM2820_H
27
28#include <linux/videodev.h>
29#include <linux/i2c.h>
30
31/* maximum number of frames that can be queued */
32#define EM2820_NUM_FRAMES 5
33/* number of frames that get used for v4l2_read() */
34#define EM2820_NUM_READ_FRAMES 2
35
36/* number of buffers for isoc transfers */
37#define EM2820_NUM_BUFS 5
38
39/* number of packets for each buffer */
40// windows requests only 40 packets .. so we better do the same
41// this is what I found out for all alternate numbers there!
42
43#define EM2820_NUM_PACKETS 40
44
45/* packet size for each packet */
46/* no longer needed: read from endpoint descriptor */
47//#define EM2820_MAX_PACKET_SIZE 3072 //7
48//#define EM2820_MAX_PACKET_SIZE 2892 //6
49//#define EM2820_MAX_PACKET_SIZE 2580 //5
50//#define EM2820_MAX_PACKET_SIZE 1448 //2
51
52/* default alternate; 0 means choose the best */
53#define EM2820_PINOUT 0
54#define EM2820_MAX_ALT 7
55
56#define EM2820_INTERLACED_DEFAULT 1
57
58/*
59#define (use usbview if you want to get the other alternate number infos)
60#define
61#define alternate number 2
62#define Endpoint Address: 82
63 Direction: in
64 Attribute: 1
65 Type: Isoc
66 Max Packet Size: 1448
67 Interval: 125us
68
69 alternate number 7
70
71 Endpoint Address: 82
72 Direction: in
73 Attribute: 1
74 Type: Isoc
75 Max Packet Size: 3072
76 Interval: 125us
77*/
78
79/* time to wait when stopping the isoc transfer */
80#define EM2820_URB_TIMEOUT msecs_to_jiffies(EM2820_NUM_BUFS * EM2820_NUM_PACKETS)
81
82/* the various frame states */
83enum em2820_frame_state {
84 F_UNUSED = 0,
85 F_QUEUED,
86 F_GRABBING,
87 F_DONE,
88 F_ERROR,
89};
90
91/* stream states */
92enum em2820_stream_state {
93 STREAM_OFF,
94 STREAM_INTERRUPT,
95 STREAM_ON,
96};
97
98/* frames */
99struct em2820_frame_t {
100 void *bufmem;
101 struct v4l2_buffer buf;
102 enum em2820_frame_state state;
103 struct list_head frame;
104 unsigned long vma_use_count;
105 int top_field;
106 int fieldbytesused;
107};
108
109/* io methods */
110enum em2820_io_method {
111 IO_NONE,
112 IO_READ,
113 IO_MMAP,
114};
115
116/* inputs */
117
118#define MAX_EM2820_INPUT 4
119enum enum2820_itype {
120 EM2820_VMUX_COMPOSITE1 = 1,
121 EM2820_VMUX_COMPOSITE2,
122 EM2820_VMUX_COMPOSITE3,
123 EM2820_VMUX_COMPOSITE4,
124 EM2820_VMUX_SVIDEO,
125 EM2820_VMUX_TELEVISION,
126 EM2820_VMUX_CABLE,
127 EM2820_VMUX_DVB,
128 EM2820_VMUX_DEBUG,
129 EM2820_RADIO,
130};
131
132struct em2820_input {
133 enum enum2820_itype type;
134 unsigned int vmux;
135 unsigned int amux;
136};
137
138#define INPUT(nr) (&em2820_boards[dev->model].input[nr])
139
140enum em2820_decoder {
141 EM2820_TVP5150,
142 EM2820_SAA7113,
143 EM2820_SAA7114
144};
145
146struct em2820_board {
147 char *name;
148
149 int vchannels;
150 int norm;
151 int tuner_type;
152
153 /* i2c flags */
154 unsigned int tda9887_conf;
155
156 unsigned int has_tuner:1;
157 unsigned int has_msp34xx:1;
158
159 enum em2820_decoder decoder;
160
161 struct em2820_input input[MAX_EM2820_INPUT];
162};
163
164struct em2820_eeprom {
165 u32 id; /* 0x9567eb1a */
166 u16 vendor_ID;
167 u16 product_ID;
168
169 u16 chip_conf;
170
171 u16 board_conf;
172
173 u16 string1, string2, string3;
174
175 u8 string_idx_table;
176};
177
178/* device states */
179enum em2820_dev_state {
180 DEV_INITIALIZED = 0x01,
181 DEV_DISCONNECTED = 0x02,
182 DEV_MISCONFIGURED = 0x04,
183};
184
185/* tvnorms */
186struct em2820_tvnorm {
187 char *name;
188 v4l2_std_id id;
189 /* mode for saa7113h */
190 int mode;
191};
192
193/* main device struct */
194struct em2820 {
195 /* generic device properties */
196 char name[30]; /* name (including minor) of the device */
197 int model; /* index in the device_data struct */
198 int video_inputs; /* number of video inputs */
199 unsigned int has_tuner:1;
200 unsigned int has_msp34xx:1;
201 unsigned int has_tda9887:1;
202
203 enum em2820_decoder decoder;
204
205 int tuner_type; /* type of the tuner */
206 int tuner_addr; /* tuner address */
207 int tda9887_conf;
208 /* i2c i/o */
209 struct i2c_adapter i2c_adap;
210 struct i2c_client i2c_client;
211 /* video for linux */
212 int users; /* user count for exclusive use */
213 struct video_device *vdev; /* video for linux device struct */
214 struct video_picture vpic; /* picture settings only used to init saa7113h */
215 struct em2820_tvnorm *tvnorm; /* selected tv norm */
216 int ctl_freq; /* selected frequency */
217 unsigned int ctl_input; /* selected input */
218 unsigned int ctl_ainput; /* slected audio input */
219 int mute;
220 int volume;
221 /* frame properties */
222 struct em2820_frame_t frame[EM2820_NUM_FRAMES]; /* list of frames */
223 int num_frames; /* number of frames currently in use */
224 unsigned int frame_count; /* total number of transfered frames */
225 struct em2820_frame_t *frame_current; /* the frame that is being filled */
226 int width; /* current frame width */
227 int height; /* current frame height */
228 int frame_size; /* current frame size */
229 int field_size; /* current field size */
230 int bytesperline;
231 int hscale; /* horizontal scale factor (see datasheet) */
232 int vscale; /* vertical scale factor (see datasheet) */
233 int interlaced; /* 1=interlace fileds, 0=just top fileds */
234 int type;
235
236 /* states */
237 enum em2820_dev_state state;
238 enum em2820_stream_state stream;
239 enum em2820_io_method io;
240 /* locks */
241 struct semaphore lock, fileop_lock;
242 spinlock_t queue_lock;
243 struct list_head inqueue, outqueue;
244 wait_queue_head_t open, wait_frame, wait_stream;
245 struct video_device *vbi_dev;
246
247 unsigned char eedata[256];
248
249 /* usb transfer */
250 struct usb_device *udev; /* the usb device */
251 int alt; /* alternate */
252 int max_pkt_size; /* max packet size of isoc transaction */
253 unsigned int alt_max_pkt_size[EM2820_MAX_ALT + 1]; /* array of wMaxPacketSize */
254 struct urb *urb[EM2820_NUM_BUFS]; /* urb for isoc transfers */
255 char *transfer_buffer[EM2820_NUM_BUFS]; /* transfer buffers for isoc transfer */
256 /* helper funcs that call usb_control_msg */
257 int (*em2820_write_regs) (struct em2820 * dev, u16 reg, char *buf,
258 int len);
259 int (*em2820_read_reg) (struct em2820 * dev, u16 reg);
260 int (*em2820_read_reg_req_len) (struct em2820 * dev, u8 req, u16 reg,
261 char *buf, int len);
262 int (*em2820_write_regs_req) (struct em2820 * dev, u8 req, u16 reg,
263 char *buf, int len);
264 int (*em2820_read_reg_req) (struct em2820 * dev, u8 req, u16 reg);
265};
266
267/* Provided by em2820-i2c.c */
268
269void em2820_i2c_call_clients(struct em2820 *dev, unsigned int cmd, void *arg);
270int em2820_i2c_register(struct em2820 *dev);
271int em2820_i2c_unregister(struct em2820 *dev);
272
273/* Provided by em2820-core.c */
274
275void em2820_print_ioctl(char *name, unsigned int cmd);
276
277u32 em2820_request_buffers(struct em2820 *dev, u32 count);
278void em2820_queue_unusedframes(struct em2820 *dev);
279void em2820_release_buffers(struct em2820 *dev);
280
281int em2820_read_reg_req_len(struct em2820 *dev, u8 req, u16 reg,
282 char *buf, int len);
283int em2820_read_reg_req(struct em2820 *dev, u8 req, u16 reg);
284int em2820_read_reg(struct em2820 *dev, u16 reg);
285int em2820_write_regs_req(struct em2820 *dev, u8 req, u16 reg, char *buf,
286 int len);
287int em2820_write_regs(struct em2820 *dev, u16 reg, char *buf, int len);
288int em2820_write_reg_bits(struct em2820 *dev, u16 reg, u8 val,
289 u8 bitmask);
290int em2820_write_ac97(struct em2820 *dev, u8 reg, u8 * val);
291int em2820_audio_analog_set(struct em2820 *dev);
292int em2820_colorlevels_set_default(struct em2820 *dev);
293int em2820_capture_start(struct em2820 *dev, int start);
294int em2820_outfmt_set_yuv422(struct em2820 *dev);
295int em2820_accumulator_set(struct em2820 *dev, u8 xmin, u8 xmax, u8 ymin,
296 u8 ymax);
297int em2820_capture_area_set(struct em2820 *dev, u8 hstart, u8 vstart,
298 u16 width, u16 height);
299int em2820_scaler_set(struct em2820 *dev, u16 h, u16 v);
300int em2820_resolution_set(struct em2820 *dev);
301void em2820_isocIrq(struct urb *urb, struct pt_regs *regs);
302int em2820_init_isoc(struct em2820 *dev);
303void em2820_uninit_isoc(struct em2820 *dev);
304int em2820_set_alternate(struct em2820 *dev);
305
306/* Provided by em2820-cards.c */
307extern void em2820_card_setup(struct em2820 *dev);
308extern struct em2820_board em2820_boards[];
309extern struct usb_device_id em2820_id_table[];
310
311/* em2820 registers */
312#define USBSUSP_REG 0x0c /* */
313
314#define AUDIOSRC_REG 0x0e
315#define XCLK_REG 0x0f
316
317#define VINMODE_REG 0x10
318#define VINCTRL_REG 0x11
319#define VINENABLE_REG 0x12 /* */
320
321#define GAMMA_REG 0x14
322#define RGAIN_REG 0x15
323#define GGAIN_REG 0x16
324#define BGAIN_REG 0x17
325#define ROFFSET_REG 0x18
326#define GOFFSET_REG 0x19
327#define BOFFSET_REG 0x1a
328
329#define OFLOW_REG 0x1b
330#define HSTART_REG 0x1c
331#define VSTART_REG 0x1d
332#define CWIDTH_REG 0x1e
333#define CHEIGHT_REG 0x1f
334
335#define YGAIN_REG 0x20
336#define YOFFSET_REG 0x21
337#define UVGAIN_REG 0x22
338#define UOFFSET_REG 0x23
339#define VOFFSET_REG 0x24
340#define SHARPNESS_REG 0x25
341
342#define COMPR_REG 0x26
343#define OUTFMT_REG 0x27
344
345#define XMIN_REG 0x28
346#define XMAX_REG 0x29
347#define YMIN_REG 0x2a
348#define YMAX_REG 0x2b
349
350#define HSCALELOW_REG 0x30
351#define HSCALEHIGH_REG 0x31
352#define VSCALELOW_REG 0x32
353#define VSCALEHIGH_REG 0x33
354
355#define AC97LSB_REG 0x40
356#define AC97MSB_REG 0x41
357#define AC97ADDR_REG 0x42
358#define AC97BUSY_REG 0x43
359
360/* em202 registers */
361#define MASTER_AC97 0x02
362#define VIDEO_AC97 0x14
363
364/* register settings */
365#define EM2820_AUDIO_SRC_TUNER 0xc0
366#define EM2820_AUDIO_SRC_LINE 0x80
367
368/* printk macros */
369
370#define em2820_err(fmt, arg...) do {\
371 printk(KERN_ERR fmt , ##arg); } while (0)
372
373#define em2820_errdev(fmt, arg...) do {\
374 printk(KERN_ERR "%s: "fmt,\
375 dev->name , ##arg); } while (0)
376
377#define em2820_info(fmt, arg...) do {\
378 printk(KERN_INFO "%s: "fmt,\
379 dev->name , ##arg); } while (0)
380#define em2820_warn(fmt, arg...) do {\
381 printk(KERN_WARNING "%s: "fmt,\
382 dev->name , ##arg); } while (0)
383
384inline static int em2820_audio_source(struct em2820 *dev, int input)
385{
386 return em2820_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
387}
388
389inline static int em2820_audio_usb_mute(struct em2820 *dev, int mute)
390{
391 return em2820_write_reg_bits(dev, XCLK_REG, mute ? 0x00 : 0x80, 0x80);
392}
393
394inline static int em2820_audio_analog_setup(struct em2820 *dev)
395{
396 /* unmute video mixer with default volume level */
397 return em2820_write_ac97(dev, VIDEO_AC97, "\x08\x08");
398}
399
400inline static int em2820_compression_disable(struct em2820 *dev)
401{
402 /* side effect of disabling scaler and mixer */
403 return em2820_write_regs(dev, COMPR_REG, "\x00", 1);
404}
405
406inline static int em2820_contrast_get(struct em2820 *dev)
407{
408 return em2820_read_reg(dev, YGAIN_REG) & 0x1f;
409}
410
411inline static int em2820_brightness_get(struct em2820 *dev)
412{
413 return em2820_read_reg(dev, YOFFSET_REG);
414}
415
416inline static int em2820_saturation_get(struct em2820 *dev)
417{
418 return em2820_read_reg(dev, UVGAIN_REG) & 0x1f;
419}
420
421inline static int em2820_u_balance_get(struct em2820 *dev)
422{
423 return em2820_read_reg(dev, UOFFSET_REG);
424}
425
426inline static int em2820_v_balance_get(struct em2820 *dev)
427{
428 return em2820_read_reg(dev, VOFFSET_REG);
429}
430
431inline static int em2820_gamma_get(struct em2820 *dev)
432{
433 return em2820_read_reg(dev, GAMMA_REG) & 0x3f;
434}
435
436inline static int em2820_contrast_set(struct em2820 *dev, s32 val)
437{
438 u8 tmp = (u8) val;
439 return em2820_write_regs(dev, YGAIN_REG, &tmp, 1);
440}
441
442inline static int em2820_brightness_set(struct em2820 *dev, s32 val)
443{
444 u8 tmp = (u8) val;
445 return em2820_write_regs(dev, YOFFSET_REG, &tmp, 1);
446}
447
448inline static int em2820_saturation_set(struct em2820 *dev, s32 val)
449{
450 u8 tmp = (u8) val;
451 return em2820_write_regs(dev, UVGAIN_REG, &tmp, 1);
452}
453
454inline static int em2820_u_balance_set(struct em2820 *dev, s32 val)
455{
456 u8 tmp = (u8) val;
457 return em2820_write_regs(dev, UOFFSET_REG, &tmp, 1);
458}
459
460inline static int em2820_v_balance_set(struct em2820 *dev, s32 val)
461{
462 u8 tmp = (u8) val;
463 return em2820_write_regs(dev, VOFFSET_REG, &tmp, 1);
464}
465
466inline static int em2820_gamma_set(struct em2820 *dev, s32 val)
467{
468 u8 tmp = (u8) val;
469 return em2820_write_regs(dev, GAMMA_REG, &tmp, 1);
470}
471
472/*FIXME: maxw should be dependent of alt mode */
473#define norm_maxw(dev) 720
474inline static unsigned int norm_maxh(struct em2820 *dev)
475{
476 return (dev->tvnorm->id & V4L2_STD_625_50) ? 576 : 480;
477}
478
479#endif
diff --git a/drivers/media/video/tvp5150.c b/drivers/media/video/tvp5150.c
index 6e3ba23104d2..03f360b45fdf 100644
--- a/drivers/media/video/tvp5150.c
+++ b/drivers/media/video/tvp5150.c
@@ -35,6 +35,47 @@ MODULE_PARM_DESC(debug, "Debug level (0-1)");
35 printk(format , ##args); \ 35 printk(format , ##args); \
36 } while (0) 36 } while (0)
37 37
38/* supported controls */
39static struct v4l2_queryctrl tvp5150_qctrl[] = {
40 {
41 .id = V4L2_CID_BRIGHTNESS,
42 .type = V4L2_CTRL_TYPE_INTEGER,
43 .name = "Brightness",
44 .minimum = 0,
45 .maximum = 255,
46 .step = 1,
47 .default_value = 0,
48 .flags = 0,
49 }, {
50 .id = V4L2_CID_CONTRAST,
51 .type = V4L2_CTRL_TYPE_INTEGER,
52 .name = "Contrast",
53 .minimum = 0,
54 .maximum = 255,
55 .step = 0x1,
56 .default_value = 0x10,
57 .flags = 0,
58 }, {
59 .id = V4L2_CID_SATURATION,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Saturation",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 0x1,
65 .default_value = 0x10,
66 .flags = 0,
67 }, {
68 .id = V4L2_CID_HUE,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Hue",
71 .minimum = -128,
72 .maximum = 127,
73 .step = 0x1,
74 .default_value = 0x10,
75 .flags = 0,
76 }
77};
78
38struct tvp5150 { 79struct tvp5150 {
39 struct i2c_client *client; 80 struct i2c_client *client;
40 81
@@ -73,7 +114,7 @@ static inline void tvp5150_write(struct i2c_client *c, unsigned char addr,
73 114
74 buffer[0] = addr; 115 buffer[0] = addr;
75 buffer[1] = value; 116 buffer[1] = value;
76 dprintk(1,"tvp5150: writing 0x%02x 0x%02x\n",buffer[0],buffer[1]); 117 dprintk(1, "tvp5150: writing 0x%02x 0x%02x\n", buffer[0], buffer[1]);
77 if (2 != (rc = i2c_master_send(c, buffer, 2))) 118 if (2 != (rc = i2c_master_send(c, buffer, 2)))
78 dprintk(0, "i2c i/o error: rc == %d (should be 2)\n", rc); 119 dprintk(0, "i2c i/o error: rc == %d (should be 2)\n", rc);
79} 120}
@@ -398,28 +439,11 @@ static inline void tvp5150_selmux(struct i2c_client *c,
398 enum tvp5150_input input) 439 enum tvp5150_input input)
399{ 440{
400 struct tvp5150 *decoder = i2c_get_clientdata(c); 441 struct tvp5150 *decoder = i2c_get_clientdata(c);
401 int tvp_input;
402
403 /* FIXME: It is dependent of basic driver */
404 switch (input)
405 {
406 case 2:
407 tvp_input=TVP5150_ANALOG_CH0;
408 break;
409 case 0:
410 tvp_input=TVP5150_ANALOG_CH1;
411 break;
412 case 1:
413 tvp_input=TVP5150_SVIDEO;
414 break;
415 default:
416 tvp_input=TVP5150_BLACK_SCREEN;
417 }
418 442
419 if (!decoder->enable) 443 if (!decoder->enable)
420 tvp_input|=TVP5150_BLACK_SCREEN; 444 input |= TVP5150_BLACK_SCREEN;
421 445
422 tvp5150_write(c, TVP5150_VD_IN_SRC_SEL_1, tvp_input); 446 tvp5150_write(c, TVP5150_VD_IN_SRC_SEL_1, input);
423}; 447};
424 448
425static inline void tvp5150_reset(struct i2c_client *c) 449static inline void tvp5150_reset(struct i2c_client *c)
@@ -432,7 +456,7 @@ static inline void tvp5150_reset(struct i2c_client *c)
432 tvp5150_write(c, TVP5150_ANAL_CHL_CTL, 0x15); 456 tvp5150_write(c, TVP5150_ANAL_CHL_CTL, 0x15);
433 457
434 /* Normal Operation */ 458 /* Normal Operation */
435// tvp5150_write(c, TVP5150_OP_MODE_CTL, 0x00); 459// tvp5150_write(c, TVP5150_OP_MODE_CTL, 0x00);
436 460
437 /* Activate YCrCb output 0x9 or 0xd ? */ 461 /* Activate YCrCb output 0x9 or 0xd ? */
438 tvp5150_write(c, TVP5150_MISC_CTL, 0x6f); 462 tvp5150_write(c, TVP5150_MISC_CTL, 0x6f);
@@ -458,6 +482,50 @@ static inline void tvp5150_reset(struct i2c_client *c)
458 tvp5150_write(c, TVP5150_HUE_CTL, (decoder->hue - 32768) >> 8); 482 tvp5150_write(c, TVP5150_HUE_CTL, (decoder->hue - 32768) >> 8);
459}; 483};
460 484
485static int tvp5150_get_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
486{
487/* struct tvp5150 *decoder = i2c_get_clientdata(c); */
488
489 switch (ctrl->id) {
490 case V4L2_CID_BRIGHTNESS:
491 ctrl->value = tvp5150_read(c, TVP5150_BRIGHT_CTL);
492 return 0;
493 case V4L2_CID_CONTRAST:
494 ctrl->value = tvp5150_read(c, TVP5150_CONTRAST_CTL);
495 return 0;
496 case V4L2_CID_SATURATION:
497 ctrl->value = tvp5150_read(c, TVP5150_SATURATION_CTL);
498 return 0;
499 case V4L2_CID_HUE:
500 ctrl->value = tvp5150_read(c, TVP5150_HUE_CTL);
501 return 0;
502 default:
503 return -EINVAL;
504 }
505}
506
507static int tvp5150_set_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
508{
509/* struct tvp5150 *decoder = i2c_get_clientdata(c); */
510
511 switch (ctrl->id) {
512 case V4L2_CID_BRIGHTNESS:
513 tvp5150_write(c, TVP5150_BRIGHT_CTL, ctrl->value);
514 return 0;
515 case V4L2_CID_CONTRAST:
516 tvp5150_write(c, TVP5150_CONTRAST_CTL, ctrl->value);
517 return 0;
518 case V4L2_CID_SATURATION:
519 tvp5150_write(c, TVP5150_SATURATION_CTL, ctrl->value);
520 return 0;
521 case V4L2_CID_HUE:
522 tvp5150_write(c, TVP5150_HUE_CTL, ctrl->value);
523 return 0;
524 default:
525 return -EINVAL;
526 }
527}
528
461/**************************************************************************** 529/****************************************************************************
462 I2C Command 530 I2C Command
463 ****************************************************************************/ 531 ****************************************************************************/
@@ -478,21 +546,21 @@ static int tvp5150_command(struct i2c_client *client,
478 break; 546 break;
479 547
480 case DECODER_GET_CAPABILITIES: 548 case DECODER_GET_CAPABILITIES:
481 { 549 {
482 struct video_decoder_capability *cap = arg; 550 struct video_decoder_capability *cap = arg;
483 551
484 cap->flags = VIDEO_DECODER_PAL | 552 cap->flags = VIDEO_DECODER_PAL |
485 VIDEO_DECODER_NTSC | 553 VIDEO_DECODER_NTSC |
486 VIDEO_DECODER_SECAM | 554 VIDEO_DECODER_SECAM |
487 VIDEO_DECODER_AUTO | VIDEO_DECODER_CCIR; 555 VIDEO_DECODER_AUTO | VIDEO_DECODER_CCIR;
488 cap->inputs = 3; 556 cap->inputs = 3;
489 cap->outputs = 1; 557 cap->outputs = 1;
490 break; 558 break;
491 } 559 }
492 case DECODER_GET_STATUS: 560 case DECODER_GET_STATUS:
493 { 561 {
494 break; 562 break;
495 } 563 }
496 564
497 case DECODER_SET_GPIO: 565 case DECODER_SET_GPIO:
498 break; 566 break;
@@ -501,87 +569,138 @@ static int tvp5150_command(struct i2c_client *client,
501 break; 569 break;
502 570
503 case DECODER_SET_NORM: 571 case DECODER_SET_NORM:
504 { 572 {
505 int *iarg = arg; 573 int *iarg = arg;
506 574
507 switch (*iarg) { 575 switch (*iarg) {
508 576
509 case VIDEO_MODE_NTSC: 577 case VIDEO_MODE_NTSC:
510 break; 578 break;
511 579
512 case VIDEO_MODE_PAL: 580 case VIDEO_MODE_PAL:
513 break; 581 break;
514 582
515 case VIDEO_MODE_SECAM: 583 case VIDEO_MODE_SECAM:
516 break; 584 break;
517 585
518 case VIDEO_MODE_AUTO: 586 case VIDEO_MODE_AUTO:
519 break; 587 break;
520 588
521 default: 589 default:
522 return -EINVAL; 590 return -EINVAL;
523 591
592 }
593 decoder->norm = *iarg;
594 break;
524 } 595 }
525 decoder->norm = *iarg;
526 break;
527 }
528 case DECODER_SET_INPUT: 596 case DECODER_SET_INPUT:
529 { 597 {
530 int *iarg = arg; 598 int *iarg = arg;
531 if (*iarg < 0 || *iarg > 3) { 599 if (*iarg < 0 || *iarg > 3) {
532 return -EINVAL; 600 return -EINVAL;
533 } 601 }
534 602
535 decoder->input=*iarg; 603 decoder->input = *iarg;
536 tvp5150_selmux(client, decoder->input); 604 tvp5150_selmux(client, decoder->input);
537 605
538 break; 606 break;
539 } 607 }
540 case DECODER_SET_OUTPUT: 608 case DECODER_SET_OUTPUT:
541 { 609 {
542 int *iarg = arg; 610 int *iarg = arg;
543 611
544 /* not much choice of outputs */ 612 /* not much choice of outputs */
545 if (*iarg != 0) { 613 if (*iarg != 0) {
546 return -EINVAL; 614 return -EINVAL;
615 }
616 break;
547 } 617 }
548 break;
549 }
550 case DECODER_ENABLE_OUTPUT: 618 case DECODER_ENABLE_OUTPUT:
551 { 619 {
552 int *iarg = arg; 620 int *iarg = arg;
553 621
554 decoder->enable = (*iarg != 0); 622 decoder->enable = (*iarg != 0);
555 623
556 tvp5150_selmux(client, decoder->input); 624 tvp5150_selmux(client, decoder->input);
557 625
558 break; 626 break;
559 }
560 case DECODER_SET_PICTURE:
561 {
562 struct video_picture *pic = arg;
563 if (decoder->bright != pic->brightness) {
564 /* We want 0 to 255 we get 0-65535 */
565 decoder->bright = pic->brightness;
566 tvp5150_write(client, TVP5150_BRIGHT_CTL, decoder->bright >> 8);
567 } 627 }
568 if (decoder->contrast != pic->contrast) { 628 case VIDIOC_QUERYCTRL:
569 /* We want 0 to 255 we get 0-65535 */ 629 {
570 decoder->contrast = pic->contrast; 630 struct v4l2_queryctrl *qc = arg;
571 tvp5150_write(client, TVP5150_CONTRAST_CTL, decoder->contrast >> 8); 631 u8 i, n;
632
633 dprintk(1, KERN_DEBUG "VIDIOC_QUERYCTRL");
634
635 n = sizeof(tvp5150_qctrl) / sizeof(tvp5150_qctrl[0]);
636 for (i = 0; i < n; i++)
637 if (qc->id && qc->id == tvp5150_qctrl[i].id) {
638 memcpy(qc, &(tvp5150_qctrl[i]),
639 sizeof(*qc));
640 return 0;
641 }
642
643 return -EINVAL;
572 } 644 }
573 if (decoder->sat != pic->colour) { 645 case VIDIOC_G_CTRL:
574 /* We want 0 to 255 we get 0-65535 */ 646 {
575 decoder->sat = pic->colour; 647 struct v4l2_control *ctrl = arg;
576 tvp5150_write(client, TVP5150_SATURATION_CTL, decoder->contrast >> 8); 648 dprintk(1, KERN_DEBUG "VIDIOC_G_CTRL");
649
650 return tvp5150_get_ctrl(client, ctrl);
577 } 651 }
578 if (decoder->hue != pic->hue) { 652 case VIDIOC_S_CTRL_OLD: /* ??? */
579 /* We want -128 to 127 we get 0-65535 */ 653 case VIDIOC_S_CTRL:
580 decoder->hue = pic->hue; 654 {
581 tvp5150_write(client, TVP5150_HUE_CTL, (decoder->hue - 32768) >> 8); 655 struct v4l2_control *ctrl = arg;
656 u8 i, n;
657 dprintk(1, KERN_DEBUG "VIDIOC_S_CTRL");
658 n = sizeof(tvp5150_qctrl) / sizeof(tvp5150_qctrl[0]);
659 for (i = 0; i < n; i++)
660 if (ctrl->id == tvp5150_qctrl[i].id) {
661 if (ctrl->value <
662 tvp5150_qctrl[i].minimum
663 || ctrl->value >
664 tvp5150_qctrl[i].maximum)
665 return -ERANGE;
666 dprintk(1,
667 KERN_DEBUG
668 "VIDIOC_S_CTRL: id=%d, value=%d",
669 ctrl->id, ctrl->value);
670 return tvp5150_set_ctrl(client, ctrl);
671 }
672 return -EINVAL;
673 }
674
675 case DECODER_SET_PICTURE:
676 {
677 struct video_picture *pic = arg;
678 if (decoder->bright != pic->brightness) {
679 /* We want 0 to 255 we get 0-65535 */
680 decoder->bright = pic->brightness;
681 tvp5150_write(client, TVP5150_BRIGHT_CTL,
682 decoder->bright >> 8);
683 }
684 if (decoder->contrast != pic->contrast) {
685 /* We want 0 to 255 we get 0-65535 */
686 decoder->contrast = pic->contrast;
687 tvp5150_write(client, TVP5150_CONTRAST_CTL,
688 decoder->contrast >> 8);
689 }
690 if (decoder->sat != pic->colour) {
691 /* We want 0 to 255 we get 0-65535 */
692 decoder->sat = pic->colour;
693 tvp5150_write(client, TVP5150_SATURATION_CTL,
694 decoder->contrast >> 8);
695 }
696 if (decoder->hue != pic->hue) {
697 /* We want -128 to 127 we get 0-65535 */
698 decoder->hue = pic->hue;
699 tvp5150_write(client, TVP5150_HUE_CTL,
700 (decoder->hue - 32768) >> 8);
701 }
702 break;
582 } 703 }
583 break;
584 }
585 default: 704 default:
586 return -EINVAL; 705 return -EINVAL;
587 } 706 }
@@ -594,15 +713,14 @@ static int tvp5150_command(struct i2c_client *client,
594 ****************************************************************************/ 713 ****************************************************************************/
595static struct i2c_driver driver; 714static struct i2c_driver driver;
596 715
597static struct i2c_client client_template = 716static struct i2c_client client_template = {
598{ 717 .name = "(unset)",
599 .name = "(unset)", 718 .flags = I2C_CLIENT_ALLOW_USE,
600 .flags = I2C_CLIENT_ALLOW_USE, 719 .driver = &driver,
601 .driver = &driver,
602}; 720};
603 721
604static int tvp5150_detect_client (struct i2c_adapter *adapter, 722static int tvp5150_detect_client(struct i2c_adapter *adapter,
605 int address, int kind) 723 int address, int kind)
606{ 724{
607 struct i2c_client *client; 725 struct i2c_client *client;
608 struct tvp5150 *core; 726 struct tvp5150 *core;
@@ -625,7 +743,7 @@ static int tvp5150_detect_client (struct i2c_adapter *adapter,
625 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); 743 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
626 if (client == 0) 744 if (client == 0)
627 return -ENOMEM; 745 return -ENOMEM;
628 memcpy(client,&client_template,sizeof(struct i2c_client)); 746 memcpy(client, &client_template, sizeof(struct i2c_client));
629 747
630 core = kmalloc(sizeof(struct tvp5150), GFP_KERNEL); 748 core = kmalloc(sizeof(struct tvp5150), GFP_KERNEL);
631 if (core == 0) { 749 if (core == 0) {
@@ -651,13 +769,13 @@ static int tvp5150_detect_client (struct i2c_adapter *adapter,
651 return rv; 769 return rv;
652 } 770 }
653 771
654 dump_reg (client); 772 if (debug > 1)
773 dump_reg(client);
655 774
656 return 0; 775 return 0;
657} 776}
658 777
659static int 778static int tvp5150_attach_adapter(struct i2c_adapter *adapter)
660tvp5150_attach_adapter (struct i2c_adapter *adapter)
661{ 779{
662 dprintk(1, 780 dprintk(1,
663 KERN_INFO 781 KERN_INFO
@@ -666,8 +784,7 @@ tvp5150_attach_adapter (struct i2c_adapter *adapter)
666 return i2c_probe(adapter, &addr_data, &tvp5150_detect_client); 784 return i2c_probe(adapter, &addr_data, &tvp5150_detect_client);
667} 785}
668 786
669static int 787static int tvp5150_detach_client(struct i2c_client *client)
670tvp5150_detach_client (struct i2c_client *client)
671{ 788{
672 struct tvp5150 *decoder = i2c_get_clientdata(client); 789 struct tvp5150 *decoder = i2c_get_clientdata(client);
673 int err; 790 int err;
@@ -699,14 +816,12 @@ static struct i2c_driver driver = {
699 .command = tvp5150_command, 816 .command = tvp5150_command,
700}; 817};
701 818
702static int __init 819static int __init tvp5150_init(void)
703tvp5150_init (void)
704{ 820{
705 return i2c_add_driver(&driver); 821 return i2c_add_driver(&driver);
706} 822}
707 823
708static void __exit 824static void __exit tvp5150_exit(void)
709tvp5150_exit (void)
710{ 825{
711 i2c_del_driver(&driver); 826 i2c_del_driver(&driver);
712} 827}