]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (3421): Several fixes to prepare for VBI
[net-next-2.6.git] / drivers / media / video / em28xx / em28xx-video.c
CommitLineData
a6c2ba28 1/*
f7abcd38 2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
a6c2ba28 3
f7abcd38
MCC
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
4ac97914 6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
f7abcd38 7 Sascha Sommer <saschasommer@freenet.de>
a6c2ba28 8
439090d7
MCC
9 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
a6c2ba28
AM
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
e5589bef 31#include <linux/bitmap.h>
a6c2ba28 32#include <linux/usb.h>
a6c2ba28 33#include <linux/i2c.h>
b296fc60 34#include <linux/version.h>
a6c2ba28 35#include <linux/video_decoder.h>
1e4baed3 36#include <linux/mutex.h>
a6c2ba28 37
f7abcd38 38#include "em28xx.h"
d5e52653 39#include <media/tuner.h>
c0477ad9 40#include <media/v4l2-common.h>
a6c2ba28 41
f7abcd38
MCC
42#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
44 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
45 "Sascha Sommer <saschasommer@freenet.de>"
a6c2ba28 46
f7abcd38
MCC
47#define DRIVER_NAME "em28xx"
48#define DRIVER_DESC "Empia em28xx based USB video device driver"
3acf2809 49#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
a6c2ba28 50
3acf2809 51#define em28xx_videodbg(fmt, arg...) do {\
4ac97914
MCC
52 if (video_debug) \
53 printk(KERN_INFO "%s %s :"fmt, \
f85c657f 54 dev->name, __FUNCTION__ , ##arg); } while (0)
a6c2ba28
AM
55
56MODULE_AUTHOR(DRIVER_AUTHOR);
57MODULE_DESCRIPTION(DRIVER_DESC);
58MODULE_LICENSE("GPL");
59
3acf2809 60static LIST_HEAD(em28xx_devlist);
9c75541f 61
9d4d9c05 62static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
e5589bef
MCC
63static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
596d92d5 65module_param_array(card, int, NULL, 0444);
e5589bef
MCC
66module_param_array(video_nr, int, NULL, 0444);
67module_param_array(vbi_nr, int, NULL, 0444);
596d92d5 68MODULE_PARM_DESC(card,"card type");
e5589bef
MCC
69MODULE_PARM_DESC(video_nr,"video device numbers");
70MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
596d92d5 71
a6c2ba28
AM
72static int tuner = -1;
73module_param(tuner, int, 0444);
74MODULE_PARM_DESC(tuner, "tuner type");
75
76static unsigned int video_debug = 0;
77module_param(video_debug,int,0644);
78MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
79
e5589bef
MCC
80/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
81static unsigned long em28xx_devused;
82
a6c2ba28 83/* supported tv norms */
3acf2809 84static struct em28xx_tvnorm tvnorms[] = {
a6c2ba28
AM
85 {
86 .name = "PAL",
87 .id = V4L2_STD_PAL,
88 .mode = VIDEO_MODE_PAL,
89 }, {
90 .name = "NTSC",
91 .id = V4L2_STD_NTSC,
92 .mode = VIDEO_MODE_NTSC,
93 }, {
94 .name = "SECAM",
95 .id = V4L2_STD_SECAM,
96 .mode = VIDEO_MODE_SECAM,
97 }, {
98 .name = "PAL-M",
99 .id = V4L2_STD_PAL_M,
100 .mode = VIDEO_MODE_PAL,
101 }
102};
103
a38a7d4f
MR
104static const unsigned char saa7114_i2c_init[] = {
105 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
106 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
107 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
108 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
109 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
110 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
111 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
112 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
113 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
114 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
115 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
116 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
117 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
118 0xbe,0x00,0xbf,0x00
119};
120
a6c2ba28
AM
121#define TVNORMS ARRAY_SIZE(tvnorms)
122
123/* supported controls */
c0477ad9 124/* Common to all boards */
3acf2809 125static struct v4l2_queryctrl em28xx_qctrl[] = {
c0477ad9
MCC
126 {
127 .id = V4L2_CID_AUDIO_VOLUME,
128 .type = V4L2_CTRL_TYPE_INTEGER,
129 .name = "Volume",
130 .minimum = 0x0,
131 .maximum = 0x1f,
132 .step = 0x1,
133 .default_value = 0x1f,
134 .flags = 0,
135 },{
136 .id = V4L2_CID_AUDIO_MUTE,
137 .type = V4L2_CTRL_TYPE_BOOLEAN,
138 .name = "Mute",
139 .minimum = 0,
140 .maximum = 1,
141 .step = 1,
142 .default_value = 1,
143 .flags = 0,
144 }
145};
146
147/* FIXME: These are specific to saa711x - should be moved to its code */
148static struct v4l2_queryctrl saa711x_qctrl[] = {
a6c2ba28
AM
149 {
150 .id = V4L2_CID_BRIGHTNESS,
151 .type = V4L2_CTRL_TYPE_INTEGER,
152 .name = "Brightness",
153 .minimum = -128,
154 .maximum = 127,
155 .step = 1,
156 .default_value = 0,
157 .flags = 0,
158 },{
159 .id = V4L2_CID_CONTRAST,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "Contrast",
162 .minimum = 0x0,
163 .maximum = 0x1f,
164 .step = 0x1,
165 .default_value = 0x10,
166 .flags = 0,
167 },{
168 .id = V4L2_CID_SATURATION,
169 .type = V4L2_CTRL_TYPE_INTEGER,
170 .name = "Saturation",
171 .minimum = 0x0,
172 .maximum = 0x1f,
173 .step = 0x1,
174 .default_value = 0x10,
175 .flags = 0,
a6c2ba28
AM
176 },{
177 .id = V4L2_CID_RED_BALANCE,
178 .type = V4L2_CTRL_TYPE_INTEGER,
179 .name = "Red chroma balance",
180 .minimum = -128,
181 .maximum = 127,
182 .step = 1,
183 .default_value = 0,
184 .flags = 0,
185 },{
186 .id = V4L2_CID_BLUE_BALANCE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "Blue chroma balance",
189 .minimum = -128,
190 .maximum = 127,
191 .step = 1,
192 .default_value = 0,
193 .flags = 0,
194 },{
195 .id = V4L2_CID_GAMMA,
196 .type = V4L2_CTRL_TYPE_INTEGER,
197 .name = "Gamma",
198 .minimum = 0x0,
199 .maximum = 0x3f,
200 .step = 0x1,
201 .default_value = 0x20,
202 .flags = 0,
c0477ad9 203 }
a6c2ba28
AM
204};
205
3acf2809 206static struct usb_driver em28xx_usb_driver;
a6c2ba28 207
1e4baed3 208static DEFINE_MUTEX(em28xx_sysfs_lock);
3acf2809 209static DECLARE_RWSEM(em28xx_disconnect);
a6c2ba28
AM
210
211/********************* v4l2 interface ******************************************/
212
a6c2ba28 213/*
3acf2809 214 * em28xx_config()
a6c2ba28
AM
215 * inits registers with sane defaults
216 */
3acf2809 217static int em28xx_config(struct em28xx *dev)
a6c2ba28
AM
218{
219
220 /* Sets I2C speed to 100 KHz */
3acf2809 221 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
a6c2ba28
AM
222
223 /* enable vbi capturing */
e5589bef
MCC
224
225 em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1);
226 em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1);
227 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
228
3acf2809 229 em28xx_audio_usb_mute(dev, 1);
a6c2ba28
AM
230 dev->mute = 1; /* maybe not the right place... */
231 dev->volume = 0x1f;
3acf2809
MCC
232 em28xx_audio_analog_set(dev);
233 em28xx_audio_analog_setup(dev);
234 em28xx_outfmt_set_yuv422(dev);
235 em28xx_colorlevels_set_default(dev);
236 em28xx_compression_disable(dev);
a6c2ba28
AM
237
238 return 0;
239}
240
241/*
3acf2809 242 * em28xx_config_i2c()
a6c2ba28
AM
243 * configure i2c attached devices
244 */
943a4902 245static void em28xx_config_i2c(struct em28xx *dev)
a6c2ba28
AM
246{
247 struct v4l2_frequency f;
3acf2809 248 struct video_decoder_init em28xx_vdi = {.data = NULL };
a6c2ba28
AM
249
250
251 /* configure decoder */
a38a7d4f 252 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
3acf2809
MCC
253 em28xx_vdi.data=saa7114_i2c_init;
254 em28xx_vdi.len=sizeof(saa7114_i2c_init);
a38a7d4f
MR
255 }
256
257
3acf2809
MCC
258 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
259 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
260/* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
261/* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
262/* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
263/* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
a6c2ba28
AM
264
265 /* configure tuner */
266 f.tuner = 0;
267 f.type = V4L2_TUNER_ANALOG_TV;
268 f.frequency = 9076; /* FIXME:remove magic number */
269 dev->ctl_freq = f.frequency;
3acf2809 270 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
a6c2ba28
AM
271
272 /* configure tda9887 */
273
a6c2ba28 274
3acf2809 275/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
a6c2ba28
AM
276}
277
278/*
3acf2809 279 * em28xx_empty_framequeues()
a6c2ba28
AM
280 * prepare queues for incoming and outgoing frames
281 */
3acf2809 282static void em28xx_empty_framequeues(struct em28xx *dev)
a6c2ba28
AM
283{
284 u32 i;
285
286 INIT_LIST_HEAD(&dev->inqueue);
287 INIT_LIST_HEAD(&dev->outqueue);
288
3acf2809 289 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
a6c2ba28
AM
290 dev->frame[i].state = F_UNUSED;
291 dev->frame[i].buf.bytesused = 0;
292 }
293}
294
eac94356
MCC
295static void video_mux(struct em28xx *dev, int index)
296{
297 int input, ainput;
298
299 input = INPUT(index)->vmux;
300 dev->ctl_input = index;
301 dev->ctl_ainput = INPUT(index)->amux;
302
303 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
304
305
306 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
307
308 if (dev->has_msp34xx) {
9bb13a6d
MCC
309 if (dev->i2s_speed)
310 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
eac94356
MCC
311 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
312 ainput = EM28XX_AUDIO_SRC_TUNER;
313 em28xx_audio_source(dev, ainput);
314 } else {
315 switch (dev->ctl_ainput) {
316 case 0:
317 ainput = EM28XX_AUDIO_SRC_TUNER;
318 break;
319 default:
320 ainput = EM28XX_AUDIO_SRC_LINE;
321 }
322 em28xx_audio_source(dev, ainput);
323 }
324}
325
a6c2ba28 326/*
3acf2809 327 * em28xx_v4l2_open()
a6c2ba28
AM
328 * inits the device and starts isoc transfer
329 */
3acf2809 330static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
a6c2ba28 331{
a6c2ba28 332 int minor = iminor(inode);
a6c2ba28 333 int errCode = 0;
3acf2809 334 struct em28xx *h,*dev = NULL;
9c75541f
MR
335 struct list_head *list;
336
3acf2809
MCC
337 list_for_each(list,&em28xx_devlist) {
338 h = list_entry(list, struct em28xx, devlist);
9c75541f
MR
339 if (h->vdev->minor == minor) {
340 dev = h;
e5589bef
MCC
341 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
342 }
343 if (h->vbi_dev->minor == minor) {
344 dev = h;
345 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
9c75541f
MR
346 }
347 }
e5589bef
MCC
348 if (NULL == dev)
349 return -ENODEV;
9c75541f
MR
350
351 filp->private_data=dev;
352
e5589bef
MCC
353 em28xx_videodbg("open minor=%d type=%s users=%d\n",
354 minor,v4l2_type_names[dev->type],dev->users);
a6c2ba28 355
3acf2809 356 if (!down_read_trylock(&em28xx_disconnect))
a6c2ba28
AM
357 return -ERESTARTSYS;
358
359 if (dev->users) {
3acf2809
MCC
360 em28xx_warn("this driver can be opened only once\n");
361 up_read(&em28xx_disconnect);
a6c2ba28
AM
362 return -EBUSY;
363 }
364
a6c2ba28
AM
365 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
366 spin_lock_init(&dev->queue_lock);
367 init_waitqueue_head(&dev->wait_frame);
368 init_waitqueue_head(&dev->wait_stream);
369
370 down(&dev->lock);
371
e5589bef
MCC
372 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
373 em28xx_set_alternate(dev);
a6c2ba28 374
e5589bef
MCC
375 dev->width = norm_maxw(dev);
376 dev->height = norm_maxh(dev);
377 dev->frame_size = dev->width * dev->height * 2;
378 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
379 dev->bytesperline = dev->width * 2;
380 dev->hscale = 0;
381 dev->vscale = 0;
a6c2ba28 382
e5589bef
MCC
383 em28xx_capture_start(dev, 1);
384 em28xx_resolution_set(dev);
a6c2ba28 385
e5589bef
MCC
386 /* start the transfer */
387 errCode = em28xx_init_isoc(dev);
388 if (errCode)
389 goto err;
390
391 video_mux(dev, 0);
392 }
a6c2ba28
AM
393
394 dev->users++;
395 filp->private_data = dev;
396 dev->io = IO_NONE;
397 dev->stream = STREAM_OFF;
398 dev->num_frames = 0;
399
400 /* prepare queues */
3acf2809 401 em28xx_empty_framequeues(dev);
a6c2ba28
AM
402
403 dev->state |= DEV_INITIALIZED;
404
405 err:
406 up(&dev->lock);
3acf2809 407 up_read(&em28xx_disconnect);
a6c2ba28
AM
408 return errCode;
409}
410
411/*
3acf2809 412 * em28xx_realease_resources()
a6c2ba28
AM
413 * unregisters the v4l2,i2c and usb devices
414 * called when the device gets disconected or at module unload
415*/
3acf2809 416static void em28xx_release_resources(struct em28xx *dev)
a6c2ba28 417{
1e4baed3 418 mutex_lock(&em28xx_sysfs_lock);
a6c2ba28 419
e5589bef
MCC
420 /*FIXME: I2C IR should be disconnected */
421
422 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
423 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
424 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
9c75541f 425 list_del(&dev->devlist);
a6c2ba28 426 video_unregister_device(dev->vdev);
e5589bef 427 video_unregister_device(dev->vbi_dev);
3acf2809 428 em28xx_i2c_unregister(dev);
a6c2ba28 429 usb_put_dev(dev->udev);
1e4baed3 430 mutex_unlock(&em28xx_sysfs_lock);
e5589bef
MCC
431
432
433 /* Mark device as unused */
434 em28xx_devused&=~(1<<dev->devno);
a6c2ba28
AM
435}
436
437/*
3acf2809 438 * em28xx_v4l2_close()
a6c2ba28
AM
439 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
440 */
3acf2809 441static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
a6c2ba28 442{
a6c2ba28 443 int errCode;
3acf2809 444 struct em28xx *dev=filp->private_data;
a6c2ba28 445
eac94356 446 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28
AM
447
448 down(&dev->lock);
449
3acf2809 450 em28xx_uninit_isoc(dev);
a6c2ba28 451
3acf2809 452 em28xx_release_buffers(dev);
a6c2ba28
AM
453
454 /* the device is already disconnect, free the remaining resources */
455 if (dev->state & DEV_DISCONNECTED) {
3acf2809 456 em28xx_release_resources(dev);
a6c2ba28
AM
457 up(&dev->lock);
458 kfree(dev);
459 return 0;
460 }
461
462 /* set alternate 0 */
463 dev->alt = 0;
eac94356 464 em28xx_videodbg("setting alternate 0\n");
a6c2ba28
AM
465 errCode = usb_set_interface(dev->udev, 0, 0);
466 if (errCode < 0) {
3acf2809 467 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
a6c2ba28
AM
468 errCode);
469 }
470
471 dev->users--;
472 wake_up_interruptible_nr(&dev->open, 1);
473 up(&dev->lock);
474 return 0;
475}
476
477/*
3acf2809 478 * em28xx_v4l2_read()
a6c2ba28
AM
479 * will allocate buffers when called for the first time
480 */
481static ssize_t
3acf2809 482em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
a6c2ba28
AM
483 loff_t * f_pos)
484{
3acf2809 485 struct em28xx_frame_t *f, *i;
a6c2ba28
AM
486 unsigned long lock_flags;
487 int ret = 0;
3acf2809 488 struct em28xx *dev = filp->private_data;
a6c2ba28 489
e5589bef
MCC
490 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
491 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
492 }
493 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
494 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
495 em28xx_videodbg("not supported yet! ...\n");
496 if (copy_to_user(buf, "", 1)) {
497 up(&dev->fileop_lock);
498 return -EFAULT;
499 }
500 return (1);
501 }
502 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
503 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
504 em28xx_videodbg("not supported yet! ...\n");
505 if (copy_to_user(buf, "", 1)) {
506 up(&dev->fileop_lock);
507 return -EFAULT;
508 }
509 return (1);
510 }
511
a6c2ba28
AM
512 if (down_interruptible(&dev->fileop_lock))
513 return -ERESTARTSYS;
514
515 if (dev->state & DEV_DISCONNECTED) {
eac94356 516 em28xx_videodbg("device not present\n");
a6c2ba28
AM
517 up(&dev->fileop_lock);
518 return -ENODEV;
519 }
520
521 if (dev->state & DEV_MISCONFIGURED) {
eac94356 522 em28xx_videodbg("device misconfigured; close and open it again\n");
a6c2ba28
AM
523 up(&dev->fileop_lock);
524 return -EIO;
525 }
526
527 if (dev->io == IO_MMAP) {
3acf2809 528 em28xx_videodbg ("IO method is set to mmap; close and open"
eac94356 529 " the device again to choose the read method\n");
a6c2ba28
AM
530 up(&dev->fileop_lock);
531 return -EINVAL;
532 }
533
534 if (dev->io == IO_NONE) {
3acf2809
MCC
535 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
536 em28xx_errdev("read failed, not enough memory\n");
a6c2ba28
AM
537 up(&dev->fileop_lock);
538 return -ENOMEM;
539 }
540 dev->io = IO_READ;
541 dev->stream = STREAM_ON;
3acf2809 542 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
543 }
544
545 if (!count) {
546 up(&dev->fileop_lock);
547 return 0;
548 }
549
550 if (list_empty(&dev->outqueue)) {
551 if (filp->f_flags & O_NONBLOCK) {
552 up(&dev->fileop_lock);
553 return -EAGAIN;
554 }
555 ret = wait_event_interruptible
556 (dev->wait_frame,
557 (!list_empty(&dev->outqueue)) ||
558 (dev->state & DEV_DISCONNECTED));
559 if (ret) {
560 up(&dev->fileop_lock);
561 return ret;
562 }
563 if (dev->state & DEV_DISCONNECTED) {
564 up(&dev->fileop_lock);
565 return -ENODEV;
566 }
567 }
568
3acf2809 569 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
a6c2ba28
AM
570
571 spin_lock_irqsave(&dev->queue_lock, lock_flags);
572 list_for_each_entry(i, &dev->outqueue, frame)
573 i->state = F_UNUSED;
574 INIT_LIST_HEAD(&dev->outqueue);
575 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
576
3acf2809 577 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
578
579 if (count > f->buf.length)
580 count = f->buf.length;
581
582 if (copy_to_user(buf, f->bufmem, count)) {
583 up(&dev->fileop_lock);
584 return -EFAULT;
585 }
586 *f_pos += count;
587
588 up(&dev->fileop_lock);
589
590 return count;
591}
592
593/*
3acf2809 594 * em28xx_v4l2_poll()
a6c2ba28
AM
595 * will allocate buffers when called for the first time
596 */
3acf2809 597static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
a6c2ba28 598{
a6c2ba28 599 unsigned int mask = 0;
3acf2809 600 struct em28xx *dev = filp->private_data;
a6c2ba28
AM
601
602 if (down_interruptible(&dev->fileop_lock))
603 return POLLERR;
604
605 if (dev->state & DEV_DISCONNECTED) {
eac94356 606 em28xx_videodbg("device not present\n");
a6c2ba28 607 } else if (dev->state & DEV_MISCONFIGURED) {
eac94356 608 em28xx_videodbg("device is misconfigured; close and open it again\n");
a6c2ba28
AM
609 } else {
610 if (dev->io == IO_NONE) {
3acf2809
MCC
611 if (!em28xx_request_buffers
612 (dev, EM28XX_NUM_READ_FRAMES)) {
613 em28xx_warn
a6c2ba28
AM
614 ("poll() failed, not enough memory\n");
615 } else {
616 dev->io = IO_READ;
617 dev->stream = STREAM_ON;
618 }
619 }
620
621 if (dev->io == IO_READ) {
3acf2809 622 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
623 poll_wait(filp, &dev->wait_frame, wait);
624
625 if (!list_empty(&dev->outqueue))
626 mask |= POLLIN | POLLRDNORM;
627
628 up(&dev->fileop_lock);
629
630 return mask;
631 }
632 }
633
634 up(&dev->fileop_lock);
635 return POLLERR;
636}
637
638/*
3acf2809 639 * em28xx_vm_open()
a6c2ba28 640 */
3acf2809 641static void em28xx_vm_open(struct vm_area_struct *vma)
a6c2ba28 642{
3acf2809 643 struct em28xx_frame_t *f = vma->vm_private_data;
a6c2ba28
AM
644 f->vma_use_count++;
645}
646
647/*
3acf2809 648 * em28xx_vm_close()
a6c2ba28 649 */
3acf2809 650static void em28xx_vm_close(struct vm_area_struct *vma)
a6c2ba28
AM
651{
652 /* NOTE: buffers are not freed here */
3acf2809 653 struct em28xx_frame_t *f = vma->vm_private_data;
a6c2ba28
AM
654 f->vma_use_count--;
655}
656
3acf2809
MCC
657static struct vm_operations_struct em28xx_vm_ops = {
658 .open = em28xx_vm_open,
659 .close = em28xx_vm_close,
a6c2ba28
AM
660};
661
662/*
3acf2809 663 * em28xx_v4l2_mmap()
a6c2ba28 664 */
3acf2809 665static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
a6c2ba28 666{
a6c2ba28 667 unsigned long size = vma->vm_end - vma->vm_start,
3639c861
SS
668 start = vma->vm_start;
669 void *pos;
a6c2ba28 670 u32 i;
9c75541f 671
3acf2809 672 struct em28xx *dev = filp->private_data;
9c75541f 673
a6c2ba28
AM
674 if (down_interruptible(&dev->fileop_lock))
675 return -ERESTARTSYS;
676
677 if (dev->state & DEV_DISCONNECTED) {
eac94356 678 em28xx_videodbg("mmap: device not present\n");
a6c2ba28
AM
679 up(&dev->fileop_lock);
680 return -ENODEV;
681 }
682
683 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 684 em28xx_videodbg ("mmap: Device is misconfigured; close and "
eac94356 685 "open it again\n");
a6c2ba28
AM
686 up(&dev->fileop_lock);
687 return -EIO;
688 }
689
690 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
691 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
692 up(&dev->fileop_lock);
693 return -EINVAL;
694 }
695
696 for (i = 0; i < dev->num_frames; i++) {
697 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
698 break;
699 }
700 if (i == dev->num_frames) {
eac94356 701 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
a6c2ba28
AM
702 up(&dev->fileop_lock);
703 return -EINVAL;
704 }
705
706 /* VM_IO is eventually going to replace PageReserved altogether */
707 vma->vm_flags |= VM_IO;
708 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
709
3639c861 710 pos = dev->frame[i].bufmem;
a6c2ba28 711 while (size > 0) { /* size is page-aligned */
3639c861
SS
712 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
713 em28xx_videodbg("mmap: vm_insert_page failed\n");
a6c2ba28
AM
714 up(&dev->fileop_lock);
715 return -EAGAIN;
716 }
717 start += PAGE_SIZE;
718 pos += PAGE_SIZE;
719 size -= PAGE_SIZE;
720 }
721
3acf2809 722 vma->vm_ops = &em28xx_vm_ops;
a6c2ba28
AM
723 vma->vm_private_data = &dev->frame[i];
724
3acf2809 725 em28xx_vm_open(vma);
a6c2ba28
AM
726 up(&dev->fileop_lock);
727 return 0;
728}
729
730/*
3acf2809 731 * em28xx_get_ctrl()
a6c2ba28
AM
732 * return the current saturation, brightness or contrast, mute state
733 */
3acf2809 734static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
a6c2ba28 735{
a6c2ba28
AM
736 switch (ctrl->id) {
737 case V4L2_CID_AUDIO_MUTE:
738 ctrl->value = dev->mute;
739 return 0;
740 case V4L2_CID_AUDIO_VOLUME:
741 ctrl->value = dev->volume;
742 return 0;
c0477ad9
MCC
743 default:
744 return -EINVAL;
745 }
746}
747
748/*FIXME: should be moved to saa711x */
749static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
750{
751 s32 tmp;
752 switch (ctrl->id) {
a6c2ba28 753 case V4L2_CID_BRIGHTNESS:
3acf2809 754 if ((tmp = em28xx_brightness_get(dev)) < 0)
a6c2ba28
AM
755 return -EIO;
756 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
757 return 0;
758 case V4L2_CID_CONTRAST:
3acf2809 759 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
a6c2ba28
AM
760 return -EIO;
761 return 0;
762 case V4L2_CID_SATURATION:
3acf2809 763 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
a6c2ba28
AM
764 return -EIO;
765 return 0;
766 case V4L2_CID_RED_BALANCE:
3acf2809 767 if ((tmp = em28xx_v_balance_get(dev)) < 0)
a6c2ba28
AM
768 return -EIO;
769 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
770 return 0;
771 case V4L2_CID_BLUE_BALANCE:
3acf2809 772 if ((tmp = em28xx_u_balance_get(dev)) < 0)
a6c2ba28
AM
773 return -EIO;
774 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
775 return 0;
776 case V4L2_CID_GAMMA:
3acf2809 777 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
a6c2ba28
AM
778 return -EIO;
779 return 0;
780 default:
781 return -EINVAL;
782 }
783}
784
785/*
3acf2809 786 * em28xx_set_ctrl()
a6c2ba28
AM
787 * mute or set new saturation, brightness or contrast
788 */
3acf2809 789static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
a6c2ba28
AM
790{
791 switch (ctrl->id) {
792 case V4L2_CID_AUDIO_MUTE:
793 if (ctrl->value != dev->mute) {
794 dev->mute = ctrl->value;
3acf2809
MCC
795 em28xx_audio_usb_mute(dev, ctrl->value);
796 return em28xx_audio_analog_set(dev);
a6c2ba28
AM
797 }
798 return 0;
799 case V4L2_CID_AUDIO_VOLUME:
800 dev->volume = ctrl->value;
3acf2809 801 return em28xx_audio_analog_set(dev);
c0477ad9
MCC
802 default:
803 return -EINVAL;
804 }
805}
806
807/*FIXME: should be moved to saa711x */
808static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
809{
810 switch (ctrl->id) {
a6c2ba28 811 case V4L2_CID_BRIGHTNESS:
3acf2809 812 return em28xx_brightness_set(dev, ctrl->value);
a6c2ba28 813 case V4L2_CID_CONTRAST:
3acf2809 814 return em28xx_contrast_set(dev, ctrl->value);
a6c2ba28 815 case V4L2_CID_SATURATION:
3acf2809 816 return em28xx_saturation_set(dev, ctrl->value);
a6c2ba28 817 case V4L2_CID_RED_BALANCE:
3acf2809 818 return em28xx_v_balance_set(dev, ctrl->value);
a6c2ba28 819 case V4L2_CID_BLUE_BALANCE:
3acf2809 820 return em28xx_u_balance_set(dev, ctrl->value);
a6c2ba28 821 case V4L2_CID_GAMMA:
3acf2809 822 return em28xx_gamma_set(dev, ctrl->value);
a6c2ba28
AM
823 default:
824 return -EINVAL;
825 }
826}
827
828/*
3acf2809 829 * em28xx_stream_interrupt()
a6c2ba28
AM
830 * stops streaming
831 */
3acf2809 832static int em28xx_stream_interrupt(struct em28xx *dev)
a6c2ba28
AM
833{
834 int ret = 0;
835
836 /* stop reading from the device */
837
838 dev->stream = STREAM_INTERRUPT;
839 ret = wait_event_timeout(dev->wait_stream,
840 (dev->stream == STREAM_OFF) ||
841 (dev->state & DEV_DISCONNECTED),
3acf2809 842 EM28XX_URB_TIMEOUT);
a6c2ba28
AM
843 if (dev->state & DEV_DISCONNECTED)
844 return -ENODEV;
845 else if (ret) {
846 dev->state |= DEV_MISCONFIGURED;
3acf2809 847 em28xx_videodbg("device is misconfigured; close and "
e5589bef
MCC
848 "open /dev/video%d again\n",
849 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
a6c2ba28
AM
850 return ret;
851 }
852
853 return 0;
854}
855
3acf2809 856static int em28xx_set_norm(struct em28xx *dev, int width, int height)
a6c2ba28
AM
857{
858 unsigned int hscale, vscale;
859 unsigned int maxh, maxw;
860
861 maxw = norm_maxw(dev);
862 maxh = norm_maxh(dev);
863
864 /* width must even because of the YUYV format */
865 /* height must be even because of interlacing */
866 height &= 0xfffe;
867 width &= 0xfffe;
868
869 if (height < 32)
870 height = 32;
871 if (height > maxh)
872 height = maxh;
873 if (width < 48)
874 width = 48;
875 if (width > maxw)
876 width = maxw;
877
878 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
879 hscale = 0x3fff;
880 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
881
882 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
883 vscale = 0x3fff;
884 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
885
886 /* set new image size */
887 dev->width = width;
888 dev->height = height;
889 dev->frame_size = dev->width * dev->height * 2;
890 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
891 dev->bytesperline = dev->width * 2;
892 dev->hscale = hscale;
893 dev->vscale = vscale;
894
3acf2809 895 em28xx_resolution_set(dev);
a6c2ba28
AM
896
897 return 0;
898}
899
e5589bef
MCC
900
901static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
902{
903 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
904 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
905 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
906 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
907 "V4L2_BUF_TYPE_VBI_CAPTURE" :
908 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
909 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
910 "not supported");
911
912 if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
913 format->fmt.pix.width = dev->width;
914 format->fmt.pix.height = dev->height;
915 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
916 format->fmt.pix.bytesperline = dev->bytesperline;
917 format->fmt.pix.sizeimage = dev->frame_size;
918 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
919 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
920
921 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
922 dev->height);
923 return 0;
924 }
925
926 return -EINVAL;
927}
928
929
a6c2ba28 930/*
3acf2809 931 * em28xx_v4l2_do_ioctl()
a6c2ba28 932 * This function is _not_ called directly, but from
3acf2809 933 * em28xx_v4l2_ioctl. Userspace
a6c2ba28
AM
934 * copying is done already, arg is a kernel pointer.
935 */
3acf2809
MCC
936static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
937 struct em28xx *dev, unsigned int cmd, void *arg,
a6c2ba28
AM
938 v4l2_kioctl driver_ioctl)
939{
940 int ret;
941
942 switch (cmd) {
943 /* ---------- tv norms ---------- */
944 case VIDIOC_ENUMSTD:
945 {
946 struct v4l2_standard *e = arg;
947 unsigned int i;
948
949 i = e->index;
950 if (i >= TVNORMS)
951 return -EINVAL;
952 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
953 tvnorms[e->index].name);
954 e->index = i;
955 if (ret < 0)
956 return ret;
957 return 0;
958 }
959 case VIDIOC_G_STD:
960 {
961 v4l2_std_id *id = arg;
962
963 *id = dev->tvnorm->id;
964 return 0;
965 }
966 case VIDIOC_S_STD:
967 {
968 v4l2_std_id *id = arg;
969 unsigned int i;
970
971 for (i = 0; i < TVNORMS; i++)
972 if (*id == tvnorms[i].id)
973 break;
974 if (i == TVNORMS)
975 for (i = 0; i < TVNORMS; i++)
976 if (*id & tvnorms[i].id)
977 break;
978 if (i == TVNORMS)
979 return -EINVAL;
980
981 down(&dev->lock);
982 dev->tvnorm = &tvnorms[i];
983
3acf2809 984 em28xx_set_norm(dev, dev->width, dev->height);
a6c2ba28
AM
985
986/*
987 dev->width=norm_maxw(dev);
988 dev->height=norm_maxh(dev);
989 dev->frame_size=dev->width*dev->height*2;
990 dev->field_size=dev->frame_size>>1;
991 dev->bytesperline=dev->width*2;
992 dev->hscale=0;
993 dev->vscale=0;
994
3acf2809 995 em28xx_resolution_set(dev);
a6c2ba28
AM
996*/
997/*
3acf2809
MCC
998 em28xx_uninit_isoc(dev);
999 em28xx_set_alternate(dev);
1000 em28xx_capture_start(dev, 1);
1001 em28xx_resolution_set(dev);
1002 em28xx_init_isoc(dev);
a6c2ba28 1003*/
3acf2809 1004 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
a6c2ba28 1005 &tvnorms[i].mode);
3acf2809 1006 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
a6c2ba28
AM
1007 &dev->tvnorm->id);
1008
1009 up(&dev->lock);
1010
1011 return 0;
1012 }
1013
1014 /* ------ input switching ---------- */
1015 case VIDIOC_ENUMINPUT:
1016 {
1017 struct v4l2_input *i = arg;
1018 unsigned int n;
1019 static const char *iname[] = {
3acf2809
MCC
1020 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1021 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1022 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1023 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1024 [EM28XX_VMUX_SVIDEO] = "S-Video",
1025 [EM28XX_VMUX_TELEVISION] = "Television",
1026 [EM28XX_VMUX_CABLE] = "Cable TV",
1027 [EM28XX_VMUX_DVB] = "DVB",
1028 [EM28XX_VMUX_DEBUG] = "for debug only",
a6c2ba28
AM
1029 };
1030
1031 n = i->index;
3acf2809 1032 if (n >= MAX_EM28XX_INPUT)
a6c2ba28
AM
1033 return -EINVAL;
1034 if (0 == INPUT(n)->type)
1035 return -EINVAL;
1036 memset(i, 0, sizeof(*i));
1037 i->index = n;
1038 i->type = V4L2_INPUT_TYPE_CAMERA;
1039 strcpy(i->name, iname[INPUT(n)->type]);
3acf2809
MCC
1040 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1041 (EM28XX_VMUX_CABLE == INPUT(n)->type))
a6c2ba28
AM
1042 i->type = V4L2_INPUT_TYPE_TUNER;
1043 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1044 i->std |= tvnorms[n].id;
1045 return 0;
1046 }
1047
1048 case VIDIOC_G_INPUT:
1049 {
1050 int *i = arg;
1051 *i = dev->ctl_input;
1052
1053 return 0;
1054 }
1055
1056 case VIDIOC_S_INPUT:
1057 {
1058 int *index = arg;
1059
3acf2809 1060 if (*index >= MAX_EM28XX_INPUT)
a6c2ba28
AM
1061 return -EINVAL;
1062 if (0 == INPUT(*index)->type)
1063 return -EINVAL;
1064
1065 down(&dev->lock);
1066 video_mux(dev, *index);
1067 up(&dev->lock);
1068
1069 return 0;
1070 }
1071
1072 case VIDIOC_G_AUDIO:
1073 {
1074 struct v4l2_audio *a = arg;
1075 unsigned int index = a->index;
1076
1077 if (a->index > 1)
1078 return -EINVAL;
1079 memset(a, 0, sizeof(*a));
1080 index = dev->ctl_ainput;
1081
1082 if (index == 0) {
1083 strcpy(a->name, "Television");
1084 } else {
1085 strcpy(a->name, "Line In");
1086 }
1087 a->capability = V4L2_AUDCAP_STEREO;
1088 a->index = index;
1089 return 0;
1090 }
1091
1092 case VIDIOC_S_AUDIO:
1093 {
1094 struct v4l2_audio *a = arg;
1095 if (a->index != dev->ctl_ainput)
1096 return -EINVAL;
1097
1098 return 0;
1099 }
1100
1101 /* --- controls ---------------------------------------------- */
1102 case VIDIOC_QUERYCTRL:
1103 {
1104 struct v4l2_queryctrl *qc = arg;
c0477ad9
MCC
1105 int i, id=qc->id;
1106
1107 memset(qc,0,sizeof(*qc));
1108 qc->id=id;
1109
1110 if (!dev->has_msp34xx) {
1111 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1112 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1113 memcpy(qc, &(em28xx_qctrl[i]),
1114 sizeof(*qc));
1115 return 0;
1116 }
1117 }
1118 }
1119 if (dev->decoder == EM28XX_TVP5150) {
1120 em28xx_i2c_call_clients(dev,cmd,qc);
1121 if (qc->type)
1122 return 0;
1123 else
1124 return -EINVAL;
1125 }
1126 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1127 if (qc->id && qc->id == saa711x_qctrl[i].id) {
1128 memcpy(qc, &(saa711x_qctrl[i]),
a6c2ba28
AM
1129 sizeof(*qc));
1130 return 0;
1131 }
c0477ad9 1132 }
a6c2ba28
AM
1133
1134 return -EINVAL;
1135 }
1136
1137 case VIDIOC_G_CTRL:
1138 {
1139 struct v4l2_control *ctrl = arg;
c0477ad9 1140 int retval=-EINVAL;
a6c2ba28 1141
c0477ad9
MCC
1142 if (!dev->has_msp34xx)
1143 retval=em28xx_get_ctrl(dev, ctrl);
1144 if (retval==-EINVAL) {
1145 if (dev->decoder == EM28XX_TVP5150) {
1146 em28xx_i2c_call_clients(dev,cmd,arg);
1147 return 0;
1148 }
a6c2ba28 1149
c0477ad9
MCC
1150 return saa711x_get_ctrl(dev, ctrl);
1151 } else return retval;
a6c2ba28
AM
1152 }
1153
a6c2ba28
AM
1154 case VIDIOC_S_CTRL:
1155 {
1156 struct v4l2_control *ctrl = arg;
c0477ad9
MCC
1157 u8 i;
1158
1159 if (!dev->has_msp34xx){
1160 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1161 if (ctrl->id == em28xx_qctrl[i].id) {
1162 if (ctrl->value <
1163 em28xx_qctrl[i].minimum
1164 || ctrl->value >
1165 em28xx_qctrl[i].maximum)
1166 return -ERANGE;
1167 return em28xx_set_ctrl(dev, ctrl);
1168 }
1169 }
1170 }
a6c2ba28 1171
c0477ad9
MCC
1172 if (dev->decoder == EM28XX_TVP5150) {
1173 em28xx_i2c_call_clients(dev,cmd,arg);
1174 return 0;
7865c44d 1175 } else if (!dev->has_msp34xx) {
c0477ad9
MCC
1176 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1177 if (ctrl->id == em28xx_qctrl[i].id) {
1178 if (ctrl->value <
1179 em28xx_qctrl[i].minimum
1180 || ctrl->value >
1181 em28xx_qctrl[i].maximum)
1182 return -ERANGE;
1183 return em28xx_set_ctrl(dev, ctrl);
1184 }
1185 }
1186 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1187 if (ctrl->id == saa711x_qctrl[i].id) {
1188 if (ctrl->value <
1189 saa711x_qctrl[i].minimum
1190 || ctrl->value >
1191 saa711x_qctrl[i].maximum)
1192 return -ERANGE;
1193 return saa711x_set_ctrl(dev, ctrl);
1194 }
a6c2ba28 1195 }
c0477ad9
MCC
1196 }
1197
a6c2ba28
AM
1198 return -EINVAL;
1199 }
1200
1201 /* --- tuner ioctls ------------------------------------------ */
1202 case VIDIOC_G_TUNER:
1203 {
1204 struct v4l2_tuner *t = arg;
1205 int status = 0;
1206
1207 if (0 != t->index)
1208 return -EINVAL;
1209
1210 memset(t, 0, sizeof(*t));
1211 strcpy(t->name, "Tuner");
1212 t->type = V4L2_TUNER_ANALOG_TV;
1213 t->capability = V4L2_TUNER_CAP_NORM;
1214 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1215/* t->signal = 0xffff;*/
3acf2809 1216/* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
a6c2ba28
AM
1217 /* No way to get signal strength? */
1218 down(&dev->lock);
3acf2809 1219 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
a6c2ba28
AM
1220 &status);
1221 up(&dev->lock);
1222 t->signal =
1223 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1224
eac94356 1225 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
a6c2ba28
AM
1226 t->afc);
1227 return 0;
1228 }
1229 case VIDIOC_S_TUNER:
1230 {
1231 struct v4l2_tuner *t = arg;
1232 int status = 0;
1233
1234 if (0 != t->index)
1235 return -EINVAL;
1236 memset(t, 0, sizeof(*t));
1237 strcpy(t->name, "Tuner");
1238 t->type = V4L2_TUNER_ANALOG_TV;
1239 t->capability = V4L2_TUNER_CAP_NORM;
1240 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1241/* t->signal = 0xffff; */
1242 /* No way to get signal strength? */
1243 down(&dev->lock);
3acf2809 1244 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
a6c2ba28
AM
1245 &status);
1246 up(&dev->lock);
1247 t->signal =
1248 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1249
3acf2809 1250 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
a6c2ba28
AM
1251 t->signal, t->afc);
1252 return 0;
1253 }
1254 case VIDIOC_G_FREQUENCY:
1255 {
1256 struct v4l2_frequency *f = arg;
1257
1258 memset(f, 0, sizeof(*f));
1259 f->type = V4L2_TUNER_ANALOG_TV;
1260 f->frequency = dev->ctl_freq;
1261
1262 return 0;
1263 }
1264 case VIDIOC_S_FREQUENCY:
1265 {
1266 struct v4l2_frequency *f = arg;
1267
1268 if (0 != f->tuner)
1269 return -EINVAL;
1270
1271 if (V4L2_TUNER_ANALOG_TV != f->type)
1272 return -EINVAL;
1273
1274 down(&dev->lock);
1275 dev->ctl_freq = f->frequency;
3acf2809 1276 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
a6c2ba28
AM
1277 up(&dev->lock);
1278 return 0;
1279 }
1280
1281 case VIDIOC_CROPCAP:
1282 {
1283 struct v4l2_cropcap *cc = arg;
1284
1285 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
596d92d5 1286 return -EINVAL;
a6c2ba28
AM
1287 cc->bounds.left = 0;
1288 cc->bounds.top = 0;
1289 cc->bounds.width = dev->width;
1290 cc->bounds.height = dev->height;
1291 cc->defrect = cc->bounds;
1292 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1293 cc->pixelaspect.denominator = 59;
1294 return 0;
1295 }
1296 case VIDIOC_STREAMON:
1297 {
1298 int *type = arg;
1299
1300 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1301 || dev->io != IO_MMAP)
1302 return -EINVAL;
1303
1304 if (list_empty(&dev->inqueue))
1305 return -EINVAL;
1306
1307 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1308
eac94356 1309 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
a6c2ba28
AM
1310
1311 return 0;
1312 }
1313 case VIDIOC_STREAMOFF:
1314 {
1315 int *type = arg;
1316 int ret;
1317
1318 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1319 || dev->io != IO_MMAP)
1320 return -EINVAL;
1321
1322 if (dev->stream == STREAM_ON) {
eac94356 1323 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
3acf2809 1324 if ((ret = em28xx_stream_interrupt(dev)))
a6c2ba28
AM
1325 return ret;
1326 }
3acf2809 1327 em28xx_empty_framequeues(dev);
a6c2ba28
AM
1328
1329 return 0;
1330 }
1331 default:
1332 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1333 driver_ioctl);
1334 }
1335 return 0;
1336}
1337
1338/*
3acf2809 1339 * em28xx_v4l2_do_ioctl()
a6c2ba28 1340 * This function is _not_ called directly, but from
3acf2809 1341 * em28xx_v4l2_ioctl. Userspace
a6c2ba28
AM
1342 * copying is done already, arg is a kernel pointer.
1343 */
3acf2809 1344static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
a6c2ba28
AM
1345 unsigned int cmd, void *arg)
1346{
3acf2809 1347 struct em28xx *dev = filp->private_data;
a6c2ba28
AM
1348
1349 if (!dev)
1350 return -ENODEV;
1351
1352 if (video_debug > 1)
5e453dc7 1353 v4l_print_ioctl(dev->name,cmd);
a6c2ba28
AM
1354
1355 switch (cmd) {
1356
1357 /* --- capabilities ------------------------------------------ */
1358 case VIDIOC_QUERYCAP:
1359 {
1360 struct v4l2_capability *cap = arg;
1361
1362 memset(cap, 0, sizeof(*cap));
3acf2809
MCC
1363 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1364 strlcpy(cap->card, em28xx_boards[dev->model].name,
a6c2ba28
AM
1365 sizeof(cap->card));
1366 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1367 sizeof(cap->bus_info));
3acf2809 1368 cap->version = EM28XX_VERSION_CODE;
a6c2ba28 1369 cap->capabilities =
e5589bef 1370 V4L2_CAP_SLICED_VBI_CAPTURE |
a6c2ba28
AM
1371 V4L2_CAP_VIDEO_CAPTURE |
1372 V4L2_CAP_AUDIO |
1373 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1374 if (dev->has_tuner)
1375 cap->capabilities |= V4L2_CAP_TUNER;
1376 return 0;
1377 }
1378
1379 /* --- capture ioctls ---------------------------------------- */
1380 case VIDIOC_ENUM_FMT:
1381 {
1382 struct v4l2_fmtdesc *fmtd = arg;
1383
1384 if (fmtd->index != 0)
1385 return -EINVAL;
1386 memset(fmtd, 0, sizeof(*fmtd));
1387 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1388 strcpy(fmtd->description, "Packed YUY2");
1389 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1390 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1391 return 0;
1392 }
1393
1394 case VIDIOC_G_FMT:
e5589bef 1395 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
a6c2ba28
AM
1396
1397 case VIDIOC_TRY_FMT:
1398 case VIDIOC_S_FMT:
1399 {
1400 struct v4l2_format *format = arg;
1401 u32 i;
1402 int ret = 0;
1403 int width = format->fmt.pix.width;
1404 int height = format->fmt.pix.height;
1405 unsigned int hscale, vscale;
1406 unsigned int maxh, maxw;
1407
1408 maxw = norm_maxw(dev);
1409 maxh = norm_maxh(dev);
1410
1411/* int both_fields; */
1412
eac94356 1413 em28xx_videodbg("%s: type=%s\n",
a6c2ba28
AM
1414 cmd ==
1415 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1416 "VIDIOC_S_FMT",
1417 format->type ==
1418 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1419 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1420 V4L2_BUF_TYPE_VBI_CAPTURE ?
1421 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1422 "not supported");
1423
1424 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1425 return -EINVAL;
1426
eac94356 1427 em28xx_videodbg("%s: requested %dx%d\n",
a6c2ba28
AM
1428 cmd ==
1429 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1430 "VIDIOC_S_FMT", format->fmt.pix.width,
1431 format->fmt.pix.height);
1432
1433 /* FIXME: Move some code away from here */
1434 /* width must even because of the YUYV format */
1435 /* height must be even because of interlacing */
1436 height &= 0xfffe;
1437 width &= 0xfffe;
1438
1439 if (height < 32)
1440 height = 32;
1441 if (height > maxh)
1442 height = maxh;
1443 if (width < 48)
1444 width = 48;
1445 if (width > maxw)
1446 width = maxw;
1447
74458e6c 1448 if(dev->is_em2800){
52c02fcd 1449 /* the em2800 can only scale down to 50% */
74458e6c
SS
1450 if(height % (maxh / 2))
1451 height=maxh;
1452 if(width % (maxw / 2))
1453 width=maxw;
52c02fcd
SS
1454 /* according to empiatech support */
1455 /* the MaxPacketSize is to small to support */
1456 /* framesizes larger than 640x480 @ 30 fps */
1457 /* or 640x576 @ 25 fps. As this would cut */
1458 /* of a part of the image we prefer */
1459 /* 360x576 or 360x480 for now */
74458e6c
SS
1460 if(width == maxw && height == maxh)
1461 width /= 2;
1462 }
1463
a6c2ba28
AM
1464 if ((hscale =
1465 (((unsigned long)maxw) << 12) / width - 4096L) >=
1466 0x4000)
1467 hscale = 0x3fff;
1468 width =
1469 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1470
1471 if ((vscale =
1472 (((unsigned long)maxh) << 12) / height - 4096L) >=
1473 0x4000)
1474 vscale = 0x3fff;
1475 height =
1476 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1477
1478 format->fmt.pix.width = width;
1479 format->fmt.pix.height = height;
1480 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1481 format->fmt.pix.bytesperline = width * 2;
1482 format->fmt.pix.sizeimage = width * 2 * height;
1483 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1484 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1485
eac94356 1486 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
a6c2ba28
AM
1487 cmd ==
1488 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1489 "VIDIOC_S_FMT", format->fmt.pix.width,
1490 format->fmt.pix.height, hscale, vscale);
1491
1492 if (cmd == VIDIOC_TRY_FMT)
1493 return 0;
1494
1495 for (i = 0; i < dev->num_frames; i++)
1496 if (dev->frame[i].vma_use_count) {
3acf2809 1497 em28xx_videodbg("VIDIOC_S_FMT failed. "
eac94356 1498 "Unmap the buffers first.\n");
a6c2ba28
AM
1499 return -EINVAL;
1500 }
1501
1502 /* stop io in case it is already in progress */
1503 if (dev->stream == STREAM_ON) {
eac94356 1504 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
3acf2809 1505 if ((ret = em28xx_stream_interrupt(dev)))
a6c2ba28
AM
1506 return ret;
1507 }
1508
3acf2809 1509 em28xx_release_buffers(dev);
a6c2ba28
AM
1510 dev->io = IO_NONE;
1511
1512 /* set new image size */
1513 dev->width = width;
1514 dev->height = height;
1515 dev->frame_size = dev->width * dev->height * 2;
1516 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1517 dev->bytesperline = dev->width * 2;
1518 dev->hscale = hscale;
1519 dev->vscale = vscale;
1520/* dev->both_fileds = both_fileds; */
3acf2809
MCC
1521 em28xx_uninit_isoc(dev);
1522 em28xx_set_alternate(dev);
1523 em28xx_capture_start(dev, 1);
1524 em28xx_resolution_set(dev);
1525 em28xx_init_isoc(dev);
a6c2ba28
AM
1526
1527 return 0;
1528 }
1529
1530 /* --- streaming capture ------------------------------------- */
1531 case VIDIOC_REQBUFS:
1532 {
1533 struct v4l2_requestbuffers *rb = arg;
1534 u32 i;
1535 int ret;
1536
1537 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1538 rb->memory != V4L2_MEMORY_MMAP)
1539 return -EINVAL;
1540
1541 if (dev->io == IO_READ) {
3acf2809 1542 em28xx_videodbg ("method is set to read;"
a6c2ba28 1543 " close and open the device again to"
eac94356 1544 " choose the mmap I/O method\n");
a6c2ba28
AM
1545 return -EINVAL;
1546 }
1547
1548 for (i = 0; i < dev->num_frames; i++)
1549 if (dev->frame[i].vma_use_count) {
eac94356 1550 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
a6c2ba28
AM
1551 return -EINVAL;
1552 }
1553
1554 if (dev->stream == STREAM_ON) {
eac94356 1555 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
3acf2809 1556 if ((ret = em28xx_stream_interrupt(dev)))
a6c2ba28
AM
1557 return ret;
1558 }
1559
3acf2809 1560 em28xx_empty_framequeues(dev);
a6c2ba28 1561
3acf2809 1562 em28xx_release_buffers(dev);
a6c2ba28
AM
1563 if (rb->count)
1564 rb->count =
3acf2809 1565 em28xx_request_buffers(dev, rb->count);
a6c2ba28
AM
1566
1567 dev->frame_current = NULL;
1568
eac94356 1569 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
a6c2ba28
AM
1570 rb->count);
1571 dev->io = rb->count ? IO_MMAP : IO_NONE;
1572 return 0;
1573 }
1574
1575 case VIDIOC_QUERYBUF:
1576 {
1577 struct v4l2_buffer *b = arg;
1578
1579 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1580 b->index >= dev->num_frames || dev->io != IO_MMAP)
1581 return -EINVAL;
1582
1583 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1584
1585 if (dev->frame[b->index].vma_use_count) {
1586 b->flags |= V4L2_BUF_FLAG_MAPPED;
1587 }
1588 if (dev->frame[b->index].state == F_DONE)
1589 b->flags |= V4L2_BUF_FLAG_DONE;
1590 else if (dev->frame[b->index].state != F_UNUSED)
1591 b->flags |= V4L2_BUF_FLAG_QUEUED;
1592 return 0;
1593 }
1594 case VIDIOC_QBUF:
1595 {
1596 struct v4l2_buffer *b = arg;
1597 unsigned long lock_flags;
1598
1599 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1600 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1601 return -EINVAL;
1602 }
1603
1604 if (dev->frame[b->index].state != F_UNUSED) {
1605 return -EAGAIN;
1606 }
1607 dev->frame[b->index].state = F_QUEUED;
1608
1609 /* add frame to fifo */
1610 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1611 list_add_tail(&dev->frame[b->index].frame,
1612 &dev->inqueue);
1613 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1614
1615 return 0;
1616 }
1617 case VIDIOC_DQBUF:
1618 {
1619 struct v4l2_buffer *b = arg;
3acf2809 1620 struct em28xx_frame_t *f;
a6c2ba28
AM
1621 unsigned long lock_flags;
1622 int ret = 0;
1623
1624 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1625 || dev->io != IO_MMAP)
1626 return -EINVAL;
1627
1628 if (list_empty(&dev->outqueue)) {
1629 if (dev->stream == STREAM_OFF)
1630 return -EINVAL;
1631 if (filp->f_flags & O_NONBLOCK)
1632 return -EAGAIN;
1633 ret = wait_event_interruptible
1634 (dev->wait_frame,
1635 (!list_empty(&dev->outqueue)) ||
1636 (dev->state & DEV_DISCONNECTED));
1637 if (ret)
1638 return ret;
1639 if (dev->state & DEV_DISCONNECTED)
1640 return -ENODEV;
1641 }
1642
1643 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1644 f = list_entry(dev->outqueue.next,
3acf2809 1645 struct em28xx_frame_t, frame);
a6c2ba28
AM
1646 list_del(dev->outqueue.next);
1647 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1648
1649 f->state = F_UNUSED;
1650 memcpy(b, &f->buf, sizeof(*b));
1651
1652 if (f->vma_use_count)
1653 b->flags |= V4L2_BUF_FLAG_MAPPED;
1654
1655 return 0;
1656 }
1657 default:
3acf2809
MCC
1658 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1659 em28xx_video_do_ioctl);
a6c2ba28
AM
1660 }
1661 return 0;
1662}
1663
1664/*
3acf2809
MCC
1665 * em28xx_v4l2_ioctl()
1666 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
a6c2ba28 1667 */
3acf2809 1668static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
a6c2ba28
AM
1669 unsigned int cmd, unsigned long arg)
1670{
a6c2ba28 1671 int ret = 0;
3acf2809 1672 struct em28xx *dev = filp->private_data;
a6c2ba28
AM
1673
1674 if (down_interruptible(&dev->fileop_lock))
1675 return -ERESTARTSYS;
1676
1677 if (dev->state & DEV_DISCONNECTED) {
3acf2809 1678 em28xx_errdev("v4l2 ioctl: device not present\n");
a6c2ba28
AM
1679 up(&dev->fileop_lock);
1680 return -ENODEV;
1681 }
1682
1683 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 1684 em28xx_errdev
a6c2ba28
AM
1685 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1686 up(&dev->fileop_lock);
1687 return -EIO;
1688 }
1689
3acf2809 1690 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
a6c2ba28
AM
1691
1692 up(&dev->fileop_lock);
1693
1694 return ret;
1695}
1696
3acf2809 1697static struct file_operations em28xx_v4l_fops = {
a6c2ba28 1698 .owner = THIS_MODULE,
3acf2809
MCC
1699 .open = em28xx_v4l2_open,
1700 .release = em28xx_v4l2_close,
1701 .ioctl = em28xx_v4l2_ioctl,
1702 .read = em28xx_v4l2_read,
1703 .poll = em28xx_v4l2_poll,
1704 .mmap = em28xx_v4l2_mmap,
a6c2ba28 1705 .llseek = no_llseek,
17cbe2e5
MCC
1706 .compat_ioctl = v4l_compat_ioctl32,
1707
a6c2ba28
AM
1708};
1709
1710/******************************** usb interface *****************************************/
1711
1712/*
3acf2809 1713 * em28xx_init_dev()
a6c2ba28
AM
1714 * allocates and inits the device structs, registers i2c bus and v4l device
1715 */
3acf2809 1716static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
a6c2ba28
AM
1717 int minor, int model)
1718{
3acf2809 1719 struct em28xx *dev = *devhandle;
a6c2ba28
AM
1720 int retval = -ENOMEM;
1721 int errCode, i;
1722 unsigned int maxh, maxw;
a6c2ba28 1723
a6c2ba28
AM
1724 dev->udev = udev;
1725 dev->model = model;
1726 init_MUTEX(&dev->lock);
1727 init_waitqueue_head(&dev->open);
1728
3acf2809
MCC
1729 dev->em28xx_write_regs = em28xx_write_regs;
1730 dev->em28xx_read_reg = em28xx_read_reg;
1731 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1732 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1733 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1734 dev->is_em2800 = em28xx_boards[model].is_em2800;
1735 dev->has_tuner = em28xx_boards[model].has_tuner;
1736 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1737 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1738 dev->decoder = em28xx_boards[model].decoder;
a6c2ba28
AM
1739
1740 if (tuner >= 0)
1741 dev->tuner_type = tuner;
1742 else
3acf2809 1743 dev->tuner_type = em28xx_boards[model].tuner_type;
a6c2ba28 1744
3acf2809 1745 dev->video_inputs = em28xx_boards[model].vchannels;
a6c2ba28
AM
1746
1747 for (i = 0; i < TVNORMS; i++)
3acf2809 1748 if (em28xx_boards[model].norm == tvnorms[i].mode)
a6c2ba28
AM
1749 break;
1750 if (i == TVNORMS)
1751 i = 0;
1752
1753 dev->tvnorm = &tvnorms[i]; /* set default norm */
1754
3acf2809 1755 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
a6c2ba28
AM
1756
1757 maxw = norm_maxw(dev);
1758 maxh = norm_maxh(dev);
1759
1760 /* set default image size */
1761 dev->width = maxw;
1762 dev->height = maxh;
3acf2809 1763 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
a6c2ba28
AM
1764 dev->field_size = dev->width * dev->height;
1765 dev->frame_size =
1766 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1767 dev->bytesperline = dev->width * 2;
1768 dev->hscale = 0;
1769 dev->vscale = 0;
1770 dev->ctl_input = 2;
1771
1772 /* setup video picture settings for saa7113h */
1773 memset(&dev->vpic, 0, sizeof(dev->vpic));
1774 dev->vpic.colour = 128 << 8;
1775 dev->vpic.hue = 128 << 8;
1776 dev->vpic.brightness = 128 << 8;
1777 dev->vpic.contrast = 192 << 8;
1778 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1779 dev->vpic.depth = 16;
1780 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1781
a6c2ba28
AM
1782#ifdef CONFIG_MODULES
1783 /* request some modules */
3acf2809 1784 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
91cad0f2 1785 request_module("saa711x");
3acf2809 1786 if (dev->decoder == EM28XX_TVP5150)
a6c2ba28
AM
1787 request_module("tvp5150");
1788 if (dev->has_tuner)
1789 request_module("tuner");
1790 if (dev->tda9887_conf)
1791 request_module("tda9887");
1792#endif
3acf2809 1793 errCode = em28xx_config(dev);
a6c2ba28 1794 if (errCode) {
3acf2809 1795 em28xx_errdev("error configuring device\n");
a6c2ba28 1796 kfree(dev);
e5589bef 1797 em28xx_devused&=~(1<<dev->devno);
a6c2ba28
AM
1798 return -ENOMEM;
1799 }
1800
1801 down(&dev->lock);
1802 /* register i2c bus */
3acf2809 1803 em28xx_i2c_register(dev);
a6c2ba28
AM
1804
1805 /* Do board specific init and eeprom reading */
3acf2809 1806 em28xx_card_setup(dev);
a6c2ba28
AM
1807
1808 /* configure the device */
3acf2809 1809 em28xx_config_i2c(dev);
a6c2ba28
AM
1810
1811 up(&dev->lock);
1812
3acf2809 1813 errCode = em28xx_config(dev);
a6c2ba28
AM
1814
1815#ifdef CONFIG_MODULES
1816 if (dev->has_msp34xx)
1817 request_module("msp3400");
1818#endif
1819 /* allocate and fill v4l2 device struct */
1820 dev->vdev = video_device_alloc();
1821 if (NULL == dev->vdev) {
3acf2809 1822 em28xx_errdev("cannot allocate video_device.\n");
a6c2ba28 1823 kfree(dev);
e5589bef
MCC
1824 em28xx_devused&=~(1<<dev->devno);
1825 return -ENOMEM;
1826 }
1827
1828 dev->vbi_dev = video_device_alloc();
1829 if (NULL == dev->vbi_dev) {
1830 em28xx_errdev("cannot allocate video_device.\n");
1831 kfree(dev->vdev);
1832 kfree(dev);
1833 em28xx_devused&=~(1<<dev->devno);
a6c2ba28
AM
1834 return -ENOMEM;
1835 }
1836
e5589bef
MCC
1837 /* Fills VBI device info */
1838 dev->vbi_dev->type = VFL_TYPE_VBI;
1839 dev->vbi_dev->hardware = 0;
1840 dev->vbi_dev->fops = &em28xx_v4l_fops;
1841 dev->vbi_dev->minor = -1;
1842 dev->vbi_dev->dev = &dev->udev->dev;
1843 dev->vbi_dev->release = video_device_release;
1844 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1845 "em28xx",dev->devno,"vbi");
1846
1847 /* Fills CAPTURE device info */
a6c2ba28
AM
1848 dev->vdev->type = VID_TYPE_CAPTURE;
1849 if (dev->has_tuner)
1850 dev->vdev->type |= VID_TYPE_TUNER;
1851 dev->vdev->hardware = 0;
3acf2809 1852 dev->vdev->fops = &em28xx_v4l_fops;
a6c2ba28
AM
1853 dev->vdev->minor = -1;
1854 dev->vdev->dev = &dev->udev->dev;
1855 dev->vdev->release = video_device_release;
e5589bef
MCC
1856 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1857 "em28xx",dev->devno,"video");
1858
3acf2809 1859 list_add_tail(&dev->devlist,&em28xx_devlist);
a6c2ba28
AM
1860
1861 /* register v4l2 device */
1862 down(&dev->lock);
e5589bef
MCC
1863 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1864 video_nr[dev->devno]))) {
3acf2809 1865 em28xx_errdev("unable to register video device (error=%i).\n",
a6c2ba28
AM
1866 retval);
1867 up(&dev->lock);
9c75541f 1868 list_del(&dev->devlist);
a6c2ba28
AM
1869 video_device_release(dev->vdev);
1870 kfree(dev);
e5589bef 1871 em28xx_devused&=~(1<<dev->devno);
a6c2ba28
AM
1872 return -ENODEV;
1873 }
e5589bef
MCC
1874
1875 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1876 vbi_nr[dev->devno]) < 0) {
1877 printk("unable to register vbi device\n");
1878 up(&dev->lock);
1879 list_del(&dev->devlist);
1880 video_device_release(dev->vbi_dev);
1881 video_device_release(dev->vdev);
1882 kfree(dev);
1883 em28xx_devused&=~(1<<dev->devno);
1884 return -ENODEV;
1885 } else {
1886 printk("registered VBI\n");
1887 }
1888
a6c2ba28
AM
1889 if (dev->has_msp34xx) {
1890 /* Send a reset to other chips via gpio */
3acf2809 1891 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
a6c2ba28 1892 udelay(2500);
3acf2809 1893 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
a6c2ba28
AM
1894 udelay(2500);
1895
1896 }
1897 video_mux(dev, 0);
1898
1899 up(&dev->lock);
1900
e5589bef
MCC
1901 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1902 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1903 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
a6c2ba28 1904
a6c2ba28
AM
1905 return 0;
1906}
1907
1908/*
3acf2809 1909 * em28xx_usb_probe()
a6c2ba28
AM
1910 * checks for supported devices
1911 */
3acf2809 1912static int em28xx_usb_probe(struct usb_interface *interface,
a6c2ba28
AM
1913 const struct usb_device_id *id)
1914{
1915 const struct usb_endpoint_descriptor *endpoint;
1916 struct usb_device *udev;
9d4d9c05 1917 struct usb_interface *uif;
3acf2809 1918 struct em28xx *dev = NULL;
a6c2ba28 1919 int retval = -ENODEV;
d5e52653 1920 int model,i,nr,ifnum;
a6c2ba28
AM
1921
1922 udev = usb_get_dev(interface_to_usbdev(interface));
d5e52653
MCC
1923 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1924
e5589bef
MCC
1925 /* Check to see next free device and mark as used */
1926 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1927 em28xx_devused|=1<<nr;
a6c2ba28 1928
596d92d5 1929 /* Don't register audio interfaces */
91cad0f2 1930 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
3acf2809 1931 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1932 udev->descriptor.idVendor,udev->descriptor.idProduct,
1933 ifnum,
1934 interface->altsetting[0].desc.bInterfaceClass);
e5589bef
MCC
1935
1936 em28xx_devused&=~(1<<nr);
596d92d5 1937 return -ENODEV;
91cad0f2
MCC
1938 }
1939
3acf2809 1940 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1941 udev->descriptor.idVendor,udev->descriptor.idProduct,
1942 ifnum,
1943 interface->altsetting[0].desc.bInterfaceClass);
596d92d5 1944
d5e52653
MCC
1945 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1946
a6c2ba28
AM
1947 /* check if the the device has the iso in endpoint at the correct place */
1948 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1949 USB_ENDPOINT_XFER_ISOC) {
3acf2809 1950 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
e5589bef 1951 em28xx_devused&=~(1<<nr);
a6c2ba28
AM
1952 return -ENODEV;
1953 }
1954 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
3acf2809 1955 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
e5589bef 1956 em28xx_devused&=~(1<<nr);
a6c2ba28
AM
1957 return -ENODEV;
1958 }
1959
596d92d5 1960 model=id->driver_info;
596d92d5 1961
e5589bef 1962 if (nr > EM28XX_MAXBOARDS) {
9d4d9c05 1963 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
e5589bef 1964 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1965 return -ENOMEM;
1966 }
1967
1968 /* allocate memory for our device state and initialize it */
7408187d 1969 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596d92d5 1970 if (dev == NULL) {
3acf2809 1971 em28xx_err(DRIVER_NAME ": out of memory!\n");
e5589bef 1972 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1973 return -ENOMEM;
1974 }
596d92d5 1975
e5589bef
MCC
1976 snprintf(dev->name, 29, "em28xx #%d", nr);
1977 dev->devno=nr;
1978
9d4d9c05
MCC
1979 /* compute alternate max packet sizes */
1980 uif = udev->actconfig->interface[0];
1981
1982 dev->num_alt=uif->num_altsetting;
e5589bef 1983 em28xx_info("Alternate settings: %i\n",dev->num_alt);
9d4d9c05
MCC
1984// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1985 dev->alt_max_pkt_size = kmalloc(32*
1986 dev->num_alt,GFP_KERNEL);
1987 if (dev->alt_max_pkt_size == NULL) {
e5589bef
MCC
1988 em28xx_errdev("out of memory!\n");
1989 em28xx_devused&=~(1<<nr);
9d4d9c05
MCC
1990 return -ENOMEM;
1991 }
1992
1993 for (i = 0; i < dev->num_alt ; i++) {
1994 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1995 wMaxPacketSize);
1996 dev->alt_max_pkt_size[i] =
1997 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
e5589bef 1998 em28xx_info("Alternate setting %i, max size= %i\n",i,
9d4d9c05
MCC
1999 dev->alt_max_pkt_size[i]);
2000 }
2001
3acf2809 2002 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
596d92d5
MCC
2003 model=card[nr];
2004
2005 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
e5589bef 2006 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
596d92d5
MCC
2007 "%s: be autodetected. Please pass card=<n> insmod option to\n"
2008 "%s: workaround that. Redirect complaints to the vendor of\n"
e5589bef
MCC
2009 "%s: the TV card. Generic type will be used."
2010 "%s: Best regards,\n"
596d92d5
MCC
2011 "%s: -- tux\n",
2012 dev->name,dev->name,dev->name,dev->name,dev->name);
e5589bef 2013 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
596d92d5 2014 dev->name);
3acf2809 2015 for (i = 0; i < em28xx_bcount; i++) {
e5589bef
MCC
2016 em28xx_errdev(" card=%d -> %s\n", i,
2017 em28xx_boards[i].name);
596d92d5
MCC
2018 }
2019 }
2020
a6c2ba28 2021 /* allocate device struct */
3acf2809 2022 retval = em28xx_init_dev(&dev, udev, nr, model);
a6c2ba28
AM
2023 if (retval)
2024 return retval;
2025
3acf2809 2026 em28xx_info("Found %s\n", em28xx_boards[model].name);
a6c2ba28
AM
2027
2028 /* save our data pointer in this interface device */
2029 usb_set_intfdata(interface, dev);
2030 return 0;
2031}
2032
2033/*
3acf2809 2034 * em28xx_usb_disconnect()
a6c2ba28
AM
2035 * called when the device gets diconencted
2036 * video device will be unregistered on v4l2_close in case it is still open
2037 */
3acf2809 2038static void em28xx_usb_disconnect(struct usb_interface *interface)
a6c2ba28 2039{
3acf2809 2040 struct em28xx *dev = usb_get_intfdata(interface);
a6c2ba28
AM
2041 usb_set_intfdata(interface, NULL);
2042
2043 if (!dev)
2044 return;
2045
3acf2809 2046 down_write(&em28xx_disconnect);
a6c2ba28
AM
2047
2048 down(&dev->lock);
2049
3acf2809 2050 em28xx_info("disconnecting %s\n", dev->vdev->name);
a6c2ba28
AM
2051
2052 wake_up_interruptible_all(&dev->open);
2053
2054 if (dev->users) {
3acf2809 2055 em28xx_warn
a6c2ba28 2056 ("device /dev/video%d is open! Deregistration and memory "
e5589bef
MCC
2057 "deallocation are deferred on close.\n",
2058 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2059
a6c2ba28 2060 dev->state |= DEV_MISCONFIGURED;
3acf2809 2061 em28xx_uninit_isoc(dev);
a6c2ba28
AM
2062 dev->state |= DEV_DISCONNECTED;
2063 wake_up_interruptible(&dev->wait_frame);
2064 wake_up_interruptible(&dev->wait_stream);
2065 } else {
2066 dev->state |= DEV_DISCONNECTED;
3acf2809 2067 em28xx_release_resources(dev);
a6c2ba28
AM
2068 }
2069
2070 up(&dev->lock);
2071
9d4d9c05
MCC
2072 if (!dev->users) {
2073 kfree(dev->alt_max_pkt_size);
a6c2ba28 2074 kfree(dev);
9d4d9c05 2075 }
a6c2ba28 2076
3acf2809 2077 up_write(&em28xx_disconnect);
a6c2ba28
AM
2078}
2079
3acf2809 2080static struct usb_driver em28xx_usb_driver = {
3acf2809
MCC
2081 .name = "em28xx",
2082 .probe = em28xx_usb_probe,
2083 .disconnect = em28xx_usb_disconnect,
2084 .id_table = em28xx_id_table,
a6c2ba28
AM
2085};
2086
3acf2809 2087static int __init em28xx_module_init(void)
a6c2ba28
AM
2088{
2089 int result;
2090
2091 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
3acf2809
MCC
2092 (EM28XX_VERSION_CODE >> 16) & 0xff,
2093 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
a6c2ba28
AM
2094#ifdef SNAPSHOT
2095 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2096 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2097#endif
2098
2099 /* register this driver with the USB subsystem */
3acf2809 2100 result = usb_register(&em28xx_usb_driver);
a6c2ba28 2101 if (result)
3acf2809 2102 em28xx_err(DRIVER_NAME
a6c2ba28
AM
2103 " usb_register failed. Error number %d.\n", result);
2104
2105 return result;
2106}
2107
3acf2809 2108static void __exit em28xx_module_exit(void)
a6c2ba28
AM
2109{
2110 /* deregister this driver with the USB subsystem */
3acf2809 2111 usb_deregister(&em28xx_usb_driver);
a6c2ba28
AM
2112}
2113
3acf2809
MCC
2114module_init(em28xx_module_init);
2115module_exit(em28xx_module_exit);