V4L/DVB (3423): CodingStyle fixes.
[safe/jmp/linux-2.6] / drivers / media / video / em28xx / em28xx-video.c
1 /*
2    em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9         Some parts based on SN9C10x PC Camera Controllers GPL driver made
10                 by Luca Risolia <luca.risolia@studio.unibo.it>
11
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>
31 #include <linux/bitmap.h>
32 #include <linux/usb.h>
33 #include <linux/i2c.h>
34 #include <linux/version.h>
35 #include <linux/video_decoder.h>
36 #include <linux/mutex.h>
37
38 #include "em28xx.h"
39 #include <media/tuner.h>
40 #include <media/v4l2-common.h>
41
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>"
46
47 #define DRIVER_NAME         "em28xx"
48 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
49 #define EM28XX_VERSION_CODE  KERNEL_VERSION(0, 0, 1)
50
51 #define em28xx_videodbg(fmt, arg...) do {\
52         if (video_debug) \
53                 printk(KERN_INFO "%s %s :"fmt, \
54                          dev->name, __FUNCTION__ , ##arg); } while (0)
55
56 MODULE_AUTHOR(DRIVER_AUTHOR);
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_LICENSE("GPL");
59
60 static LIST_HEAD(em28xx_devlist);
61
62 static unsigned int card[]     = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
63 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65 module_param_array(card,  int, NULL, 0444);
66 module_param_array(video_nr, int, NULL, 0444);
67 module_param_array(vbi_nr, int, NULL, 0444);
68 MODULE_PARM_DESC(card,"card type");
69 MODULE_PARM_DESC(video_nr,"video device numbers");
70 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
71
72 static int tuner = -1;
73 module_param(tuner, int, 0444);
74 MODULE_PARM_DESC(tuner, "tuner type");
75
76 static unsigned int video_debug = 0;
77 module_param(video_debug,int,0644);
78 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
79
80 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
81 static unsigned long em28xx_devused;
82
83 /* supported tv norms */
84 static struct em28xx_tvnorm tvnorms[] = {
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
104 static 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
121 #define TVNORMS ARRAY_SIZE(tvnorms)
122
123 /* supported controls */
124 /* Common to all boards */
125 static struct v4l2_queryctrl em28xx_qctrl[] = {
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 */
148 static struct v4l2_queryctrl saa711x_qctrl[] = {
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,
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,
203         }
204 };
205
206 static struct usb_driver em28xx_usb_driver;
207
208 static DEFINE_MUTEX(em28xx_sysfs_lock);
209 static DECLARE_RWSEM(em28xx_disconnect);
210
211 /*********************  v4l2 interface  ******************************************/
212
213 /*
214  * em28xx_config()
215  * inits registers with sane defaults
216  */
217 static int em28xx_config(struct em28xx *dev)
218 {
219
220         /* Sets I2C speed to 100 KHz */
221         em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
222
223         /* enable vbi capturing */
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
229         em28xx_audio_usb_mute(dev, 1);
230         dev->mute = 1;          /* maybe not the right place... */
231         dev->volume = 0x1f;
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);
237
238         return 0;
239 }
240
241 /*
242  * em28xx_config_i2c()
243  * configure i2c attached devices
244  */
245 static void em28xx_config_i2c(struct em28xx *dev)
246 {
247         struct v4l2_frequency f;
248         struct video_decoder_init em28xx_vdi = {.data = NULL };
249
250
251         /* configure decoder */
252         if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
253                 em28xx_vdi.data=saa7114_i2c_init;
254                 em28xx_vdi.len=sizeof(saa7114_i2c_init);
255         }
256
257
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); */
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;
270         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
271
272         /* configure tda9887 */
273
274
275 /*      em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
276 }
277
278 /*
279  * em28xx_empty_framequeues()
280  * prepare queues for incoming and outgoing frames
281  */
282 static void em28xx_empty_framequeues(struct em28xx *dev)
283 {
284         u32 i;
285
286         INIT_LIST_HEAD(&dev->inqueue);
287         INIT_LIST_HEAD(&dev->outqueue);
288
289         for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
290                 dev->frame[i].state = F_UNUSED;
291                 dev->frame[i].buf.bytesused = 0;
292         }
293 }
294
295 static 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) {
309                 if (dev->i2s_speed)
310                         em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
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
326 /*
327  * em28xx_v4l2_open()
328  * inits the device and starts isoc transfer
329  */
330 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
331 {
332         int minor = iminor(inode);
333         int errCode = 0;
334         struct em28xx *h,*dev = NULL;
335         struct list_head *list;
336
337         list_for_each(list,&em28xx_devlist) {
338                 h = list_entry(list, struct em28xx, devlist);
339                 if (h->vdev->minor == minor) {
340                         dev  = h;
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;
346                 }
347         }
348         if (NULL == dev)
349                 return -ENODEV;
350
351         filp->private_data=dev;
352
353         em28xx_videodbg("open minor=%d type=%s users=%d\n",
354                                 minor,v4l2_type_names[dev->type],dev->users);
355
356         if (!down_read_trylock(&em28xx_disconnect))
357                 return -ERESTARTSYS;
358
359         if (dev->users) {
360                 em28xx_warn("this driver can be opened only once\n");
361                 up_read(&em28xx_disconnect);
362                 return -EBUSY;
363         }
364
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
372         if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
373                 em28xx_set_alternate(dev);
374
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;
382
383                 em28xx_capture_start(dev, 1);
384                 em28xx_resolution_set(dev);
385
386                 /* start the transfer */
387                 errCode = em28xx_init_isoc(dev);
388                 if (errCode)
389                         goto err;
390
391                 video_mux(dev, 0);
392         }
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 */
401         em28xx_empty_framequeues(dev);
402
403         dev->state |= DEV_INITIALIZED;
404
405       err:
406         up(&dev->lock);
407         up_read(&em28xx_disconnect);
408         return errCode;
409 }
410
411 /*
412  * em28xx_realease_resources()
413  * unregisters the v4l2,i2c and usb devices
414  * called when the device gets disconected or at module unload
415 */
416 static void em28xx_release_resources(struct em28xx *dev)
417 {
418         mutex_lock(&em28xx_sysfs_lock);
419
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);
425         list_del(&dev->devlist);
426         video_unregister_device(dev->vdev);
427         video_unregister_device(dev->vbi_dev);
428         em28xx_i2c_unregister(dev);
429         usb_put_dev(dev->udev);
430         mutex_unlock(&em28xx_sysfs_lock);
431
432
433         /* Mark device as unused */
434         em28xx_devused&=~(1<<dev->devno);
435 }
436
437 /*
438  * em28xx_v4l2_close()
439  * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
440  */
441 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
442 {
443         int errCode;
444         struct em28xx *dev=filp->private_data;
445
446         em28xx_videodbg("users=%d\n", dev->users);
447
448         down(&dev->lock);
449
450         em28xx_uninit_isoc(dev);
451
452         em28xx_release_buffers(dev);
453
454         /* the device is already disconnect, free the remaining resources */
455         if (dev->state & DEV_DISCONNECTED) {
456                 em28xx_release_resources(dev);
457                 up(&dev->lock);
458                 kfree(dev);
459                 return 0;
460         }
461
462         /* set alternate 0 */
463         dev->alt = 0;
464         em28xx_videodbg("setting alternate 0\n");
465         errCode = usb_set_interface(dev->udev, 0, 0);
466         if (errCode < 0) {
467                 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
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 /*
478  * em28xx_v4l2_read()
479  * will allocate buffers when called for the first time
480  */
481 static ssize_t
482 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
483                  loff_t * f_pos)
484 {
485         struct em28xx_frame_t *f, *i;
486         unsigned long lock_flags;
487         int ret = 0;
488         struct em28xx *dev = filp->private_data;
489
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
512         if (down_interruptible(&dev->fileop_lock))
513                 return -ERESTARTSYS;
514
515         if (dev->state & DEV_DISCONNECTED) {
516                 em28xx_videodbg("device not present\n");
517                 up(&dev->fileop_lock);
518                 return -ENODEV;
519         }
520
521         if (dev->state & DEV_MISCONFIGURED) {
522                 em28xx_videodbg("device misconfigured; close and open it again\n");
523                 up(&dev->fileop_lock);
524                 return -EIO;
525         }
526
527         if (dev->io == IO_MMAP) {
528                 em28xx_videodbg ("IO method is set to mmap; close and open"
529                                 " the device again to choose the read method\n");
530                 up(&dev->fileop_lock);
531                 return -EINVAL;
532         }
533
534         if (dev->io == IO_NONE) {
535                 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
536                         em28xx_errdev("read failed, not enough memory\n");
537                         up(&dev->fileop_lock);
538                         return -ENOMEM;
539                 }
540                 dev->io = IO_READ;
541                 dev->stream = STREAM_ON;
542                 em28xx_queue_unusedframes(dev);
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
569         f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
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
577         em28xx_queue_unusedframes(dev);
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 /*
594  * em28xx_v4l2_poll()
595  * will allocate buffers when called for the first time
596  */
597 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
598 {
599         unsigned int mask = 0;
600         struct em28xx *dev = filp->private_data;
601
602         if (down_interruptible(&dev->fileop_lock))
603                 return POLLERR;
604
605         if (dev->state & DEV_DISCONNECTED) {
606                 em28xx_videodbg("device not present\n");
607         } else if (dev->state & DEV_MISCONFIGURED) {
608                 em28xx_videodbg("device is misconfigured; close and open it again\n");
609         } else {
610                 if (dev->io == IO_NONE) {
611                         if (!em28xx_request_buffers
612                             (dev, EM28XX_NUM_READ_FRAMES)) {
613                                 em28xx_warn
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) {
622                         em28xx_queue_unusedframes(dev);
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 /*
639  * em28xx_vm_open()
640  */
641 static void em28xx_vm_open(struct vm_area_struct *vma)
642 {
643         struct em28xx_frame_t *f = vma->vm_private_data;
644         f->vma_use_count++;
645 }
646
647 /*
648  * em28xx_vm_close()
649  */
650 static void em28xx_vm_close(struct vm_area_struct *vma)
651 {
652         /* NOTE: buffers are not freed here */
653         struct em28xx_frame_t *f = vma->vm_private_data;
654         f->vma_use_count--;
655 }
656
657 static struct vm_operations_struct em28xx_vm_ops = {
658         .open = em28xx_vm_open,
659         .close = em28xx_vm_close,
660 };
661
662 /*
663  * em28xx_v4l2_mmap()
664  */
665 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
666 {
667         unsigned long size = vma->vm_end - vma->vm_start,
668             start = vma->vm_start;
669         void *pos;
670         u32 i;
671
672         struct em28xx *dev = filp->private_data;
673
674         if (down_interruptible(&dev->fileop_lock))
675                 return -ERESTARTSYS;
676
677         if (dev->state & DEV_DISCONNECTED) {
678                 em28xx_videodbg("mmap: device not present\n");
679                 up(&dev->fileop_lock);
680                 return -ENODEV;
681         }
682
683         if (dev->state & DEV_MISCONFIGURED) {
684                 em28xx_videodbg ("mmap: Device is misconfigured; close and "
685                                                 "open it again\n");
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) {
701                 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
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
710         pos = dev->frame[i].bufmem;
711         while (size > 0) {      /* size is page-aligned */
712                 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
713                         em28xx_videodbg("mmap: vm_insert_page failed\n");
714                         up(&dev->fileop_lock);
715                         return -EAGAIN;
716                 }
717                 start += PAGE_SIZE;
718                 pos += PAGE_SIZE;
719                 size -= PAGE_SIZE;
720         }
721
722         vma->vm_ops = &em28xx_vm_ops;
723         vma->vm_private_data = &dev->frame[i];
724
725         em28xx_vm_open(vma);
726         up(&dev->fileop_lock);
727         return 0;
728 }
729
730 /*
731  * em28xx_get_ctrl()
732  * return the current saturation, brightness or contrast, mute state
733  */
734 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
735 {
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;
743         default:
744                 return -EINVAL;
745         }
746 }
747
748 /*FIXME: should be moved to saa711x */
749 static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
750 {
751         s32 tmp;
752         switch (ctrl->id) {
753         case V4L2_CID_BRIGHTNESS:
754                 if ((tmp = em28xx_brightness_get(dev)) < 0)
755                         return -EIO;
756                 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
757                 return 0;
758         case V4L2_CID_CONTRAST:
759                 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
760                         return -EIO;
761                 return 0;
762         case V4L2_CID_SATURATION:
763                 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
764                         return -EIO;
765                 return 0;
766         case V4L2_CID_RED_BALANCE:
767                 if ((tmp = em28xx_v_balance_get(dev)) < 0)
768                         return -EIO;
769                 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
770                 return 0;
771         case V4L2_CID_BLUE_BALANCE:
772                 if ((tmp = em28xx_u_balance_get(dev)) < 0)
773                         return -EIO;
774                 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
775                 return 0;
776         case V4L2_CID_GAMMA:
777                 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
778                         return -EIO;
779                 return 0;
780         default:
781                 return -EINVAL;
782         }
783 }
784
785 /*
786  * em28xx_set_ctrl()
787  * mute or set new saturation, brightness or contrast
788  */
789 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
790 {
791         switch (ctrl->id) {
792         case V4L2_CID_AUDIO_MUTE:
793                 if (ctrl->value != dev->mute) {
794                         dev->mute = ctrl->value;
795                         em28xx_audio_usb_mute(dev, ctrl->value);
796                         return em28xx_audio_analog_set(dev);
797                 }
798                 return 0;
799         case V4L2_CID_AUDIO_VOLUME:
800                 dev->volume = ctrl->value;
801                 return em28xx_audio_analog_set(dev);
802         default:
803                 return -EINVAL;
804         }
805 }
806
807 /*FIXME: should be moved to saa711x */
808 static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
809 {
810         switch (ctrl->id) {
811         case V4L2_CID_BRIGHTNESS:
812                 return em28xx_brightness_set(dev, ctrl->value);
813         case V4L2_CID_CONTRAST:
814                 return em28xx_contrast_set(dev, ctrl->value);
815         case V4L2_CID_SATURATION:
816                 return em28xx_saturation_set(dev, ctrl->value);
817         case V4L2_CID_RED_BALANCE:
818                 return em28xx_v_balance_set(dev, ctrl->value);
819         case V4L2_CID_BLUE_BALANCE:
820                 return em28xx_u_balance_set(dev, ctrl->value);
821         case V4L2_CID_GAMMA:
822                 return em28xx_gamma_set(dev, ctrl->value);
823         default:
824                 return -EINVAL;
825         }
826 }
827
828 /*
829  * em28xx_stream_interrupt()
830  * stops streaming
831  */
832 static int em28xx_stream_interrupt(struct em28xx *dev)
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),
842                                  EM28XX_URB_TIMEOUT);
843         if (dev->state & DEV_DISCONNECTED)
844                 return -ENODEV;
845         else if (ret) {
846                 dev->state |= DEV_MISCONFIGURED;
847                 em28xx_videodbg("device is misconfigured; close and "
848                         "open /dev/video%d again\n",
849                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
850                 return ret;
851         }
852
853         return 0;
854 }
855
856 static int em28xx_set_norm(struct em28xx *dev, int width, int height)
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
895         em28xx_resolution_set(dev);
896
897         return 0;
898 }
899
900 static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
901 {
902         em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
903                 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
904                 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
905                 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
906                 "V4L2_BUF_TYPE_VBI_CAPTURE" :
907                 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
908                 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
909                 "not supported");
910
911         switch (format->type) {
912         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
913         {
914                 format->fmt.pix.width = dev->width;
915                 format->fmt.pix.height = dev->height;
916                 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
917                 format->fmt.pix.bytesperline = dev->bytesperline;
918                 format->fmt.pix.sizeimage = dev->frame_size;
919                 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
920                 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;       /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
921
922                 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
923                         dev->height);
924                 break;
925         }
926
927         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
928         {
929                 format->fmt.sliced.service_set=0;
930
931                 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
932
933                 if (format->fmt.sliced.service_set==0)
934                         return -EINVAL;
935
936                 break;
937         }
938
939         default:
940                 return -EINVAL;
941         }
942         return (0);
943 }
944
945 static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
946 {
947         u32 i;
948         int ret = 0;
949         int width = format->fmt.pix.width;
950         int height = format->fmt.pix.height;
951         unsigned int hscale, vscale;
952         unsigned int maxh, maxw;
953
954         maxw = norm_maxw(dev);
955         maxh = norm_maxh(dev);
956
957         em28xx_videodbg("%s: type=%s\n",
958                         cmd == VIDIOC_TRY_FMT ?
959                         "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
960                         format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
961                         "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
962                         format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
963                         "V4L2_BUF_TYPE_VBI_CAPTURE " :
964                         "not supported");
965
966         if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
967                 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
968
969                 if (format->fmt.sliced.service_set==0)
970                         return -EINVAL;
971
972                 return 0;
973         }
974
975
976         if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
977                 return -EINVAL;
978
979         em28xx_videodbg("%s: requested %dx%d\n",
980                 cmd == VIDIOC_TRY_FMT ?
981                 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
982                 format->fmt.pix.width, format->fmt.pix.height);
983
984         /* FIXME: Move some code away from here */
985         /* width must even because of the YUYV format */
986         /* height must be even because of interlacing */
987         height &= 0xfffe;
988         width &= 0xfffe;
989
990         if (height < 32)
991                 height = 32;
992         if (height > maxh)
993                 height = maxh;
994         if (width < 48)
995                 width = 48;
996         if (width > maxw)
997                 width = maxw;
998
999         if(dev->is_em2800){
1000                 /* the em2800 can only scale down to 50% */
1001                 if(height % (maxh / 2))
1002                         height=maxh;
1003                 if(width % (maxw / 2))
1004                         width=maxw;
1005                 /* according to empiatech support */
1006                 /* the MaxPacketSize is to small to support */
1007                 /* framesizes larger than 640x480 @ 30 fps */
1008                 /* or 640x576 @ 25 fps. As this would cut */
1009                 /* of a part of the image we prefer */
1010                 /* 360x576 or 360x480 for now */
1011                 if(width == maxw && height == maxh)
1012                         width /= 2;
1013         }
1014
1015         if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
1016                 hscale = 0x3fff;
1017
1018         width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1019
1020         if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
1021                 vscale = 0x3fff;
1022
1023         height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1024
1025         format->fmt.pix.width = width;
1026         format->fmt.pix.height = height;
1027         format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1028         format->fmt.pix.bytesperline = width * 2;
1029         format->fmt.pix.sizeimage = width * 2 * height;
1030         format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1031         format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1032
1033         em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
1034                 cmd == VIDIOC_TRY_FMT ?
1035                 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
1036                 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
1037
1038         if (cmd == VIDIOC_TRY_FMT)
1039                 return 0;
1040
1041         for (i = 0; i < dev->num_frames; i++)
1042                 if (dev->frame[i].vma_use_count) {
1043                         em28xx_videodbg("VIDIOC_S_FMT failed. "
1044                                 "Unmap the buffers first.\n");
1045                         return -EINVAL;
1046                 }
1047
1048         /* stop io in case it is already in progress */
1049         if (dev->stream == STREAM_ON) {
1050                 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
1051                 if ((ret = em28xx_stream_interrupt(dev)))
1052                         return ret;
1053         }
1054
1055         em28xx_release_buffers(dev);
1056         dev->io = IO_NONE;
1057
1058         /* set new image size */
1059         dev->width = width;
1060         dev->height = height;
1061         dev->frame_size = dev->width * dev->height * 2;
1062         dev->field_size = dev->frame_size >> 1;
1063         dev->bytesperline = dev->width * 2;
1064         dev->hscale = hscale;
1065         dev->vscale = vscale;
1066         em28xx_uninit_isoc(dev);
1067         em28xx_set_alternate(dev);
1068         em28xx_capture_start(dev, 1);
1069         em28xx_resolution_set(dev);
1070         em28xx_init_isoc(dev);
1071
1072         return 0;
1073 }
1074
1075 /*
1076  * em28xx_v4l2_do_ioctl()
1077  * This function is _not_ called directly, but from
1078  * em28xx_v4l2_ioctl. Userspace
1079  * copying is done already, arg is a kernel pointer.
1080  */
1081 static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
1082                            struct em28xx *dev, unsigned int cmd, void *arg,
1083                            v4l2_kioctl driver_ioctl)
1084 {
1085         int ret;
1086
1087         switch (cmd) {
1088                 /* ---------- tv norms ---------- */
1089         case VIDIOC_ENUMSTD:
1090         {
1091                 struct v4l2_standard *e = arg;
1092                 unsigned int i;
1093
1094                 i = e->index;
1095                 if (i >= TVNORMS)
1096                         return -EINVAL;
1097                 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
1098                                                 tvnorms[e->index].name);
1099                 e->index = i;
1100                 if (ret < 0)
1101                         return ret;
1102                 return 0;
1103         }
1104         case VIDIOC_G_STD:
1105         {
1106                 v4l2_std_id *id = arg;
1107
1108                 *id = dev->tvnorm->id;
1109                 return 0;
1110         }
1111         case VIDIOC_S_STD:
1112         {
1113                 v4l2_std_id *id = arg;
1114                 unsigned int i;
1115
1116                 for (i = 0; i < TVNORMS; i++)
1117                         if (*id == tvnorms[i].id)
1118                                 break;
1119                 if (i == TVNORMS)
1120                         for (i = 0; i < TVNORMS; i++)
1121                                 if (*id & tvnorms[i].id)
1122                                         break;
1123                 if (i == TVNORMS)
1124                         return -EINVAL;
1125
1126                 down(&dev->lock);
1127                 dev->tvnorm = &tvnorms[i];
1128
1129                 em28xx_set_norm(dev, dev->width, dev->height);
1130
1131                 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
1132                                         &tvnorms[i].mode);
1133                 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
1134                                         &dev->tvnorm->id);
1135
1136                 up(&dev->lock);
1137
1138                 return 0;
1139         }
1140
1141         /* ------ input switching ---------- */
1142         case VIDIOC_ENUMINPUT:
1143         {
1144                 struct v4l2_input *i = arg;
1145                 unsigned int n;
1146                 static const char *iname[] = {
1147                         [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1148                         [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1149                         [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1150                         [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1151                         [EM28XX_VMUX_SVIDEO] = "S-Video",
1152                         [EM28XX_VMUX_TELEVISION] = "Television",
1153                         [EM28XX_VMUX_CABLE] = "Cable TV",
1154                         [EM28XX_VMUX_DVB] = "DVB",
1155                         [EM28XX_VMUX_DEBUG] = "for debug only",
1156                 };
1157
1158                 n = i->index;
1159                 if (n >= MAX_EM28XX_INPUT)
1160                         return -EINVAL;
1161                 if (0 == INPUT(n)->type)
1162                         return -EINVAL;
1163                 memset(i, 0, sizeof(*i));
1164                 i->index = n;
1165                 i->type = V4L2_INPUT_TYPE_CAMERA;
1166                 strcpy(i->name, iname[INPUT(n)->type]);
1167                 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1168                         (EM28XX_VMUX_CABLE == INPUT(n)->type))
1169                         i->type = V4L2_INPUT_TYPE_TUNER;
1170                 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1171                         i->std |= tvnorms[n].id;
1172                 return 0;
1173         }
1174         case VIDIOC_G_INPUT:
1175         {
1176                 int *i = arg;
1177                 *i = dev->ctl_input;
1178
1179                 return 0;
1180         }
1181         case VIDIOC_S_INPUT:
1182         {
1183                 int *index = arg;
1184
1185                 if (*index >= MAX_EM28XX_INPUT)
1186                         return -EINVAL;
1187                 if (0 == INPUT(*index)->type)
1188                         return -EINVAL;
1189
1190                 down(&dev->lock);
1191                 video_mux(dev, *index);
1192                 up(&dev->lock);
1193
1194                 return 0;
1195         }
1196         case VIDIOC_G_AUDIO:
1197         {
1198                 struct v4l2_audio *a = arg;
1199                 unsigned int index = a->index;
1200
1201                 if (a->index > 1)
1202                         return -EINVAL;
1203                 memset(a, 0, sizeof(*a));
1204                 index = dev->ctl_ainput;
1205
1206                 if (index == 0) {
1207                         strcpy(a->name, "Television");
1208                 } else {
1209                         strcpy(a->name, "Line In");
1210                 }
1211                 a->capability = V4L2_AUDCAP_STEREO;
1212                 a->index = index;
1213                 return 0;
1214         }
1215         case VIDIOC_S_AUDIO:
1216         {
1217                 struct v4l2_audio *a = arg;
1218
1219                 if (a->index != dev->ctl_ainput)
1220                         return -EINVAL;
1221
1222                 return 0;
1223         }
1224
1225         /* --- controls ---------------------------------------------- */
1226         case VIDIOC_QUERYCTRL:
1227         {
1228                 struct v4l2_queryctrl *qc = arg;
1229                 int i, id=qc->id;
1230
1231                 memset(qc,0,sizeof(*qc));
1232                 qc->id=id;
1233
1234                 if (!dev->has_msp34xx) {
1235                         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1236                                 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1237                                         memcpy(qc, &(em28xx_qctrl[i]),
1238                                         sizeof(*qc));
1239                                         return 0;
1240                                 }
1241                         }
1242                 }
1243                 if (dev->decoder == EM28XX_TVP5150) {
1244                         em28xx_i2c_call_clients(dev,cmd,qc);
1245                         if (qc->type)
1246                                 return 0;
1247                         else
1248                                 return -EINVAL;
1249                 }
1250                 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1251                         if (qc->id && qc->id == saa711x_qctrl[i].id) {
1252                                 memcpy(qc, &(saa711x_qctrl[i]),
1253                                         sizeof(*qc));
1254                                 return 0;
1255                         }
1256                 }
1257
1258                 return -EINVAL;
1259         }
1260         case VIDIOC_G_CTRL:
1261         {
1262                 struct v4l2_control *ctrl = arg;
1263                 int retval=-EINVAL;
1264
1265                 if (!dev->has_msp34xx)
1266                         retval=em28xx_get_ctrl(dev, ctrl);
1267                 if (retval==-EINVAL) {
1268                         if (dev->decoder == EM28XX_TVP5150) {
1269                                 em28xx_i2c_call_clients(dev,cmd,arg);
1270                                 return 0;
1271                         }
1272
1273                         return saa711x_get_ctrl(dev, ctrl);
1274                 } else return retval;
1275         }
1276         case VIDIOC_S_CTRL:
1277         {
1278                 struct v4l2_control *ctrl = arg;
1279                 u8 i;
1280
1281                 if (!dev->has_msp34xx){
1282                         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1283                                 if (ctrl->id == em28xx_qctrl[i].id) {
1284                                         if (ctrl->value <
1285                                         em28xx_qctrl[i].minimum
1286                                         || ctrl->value >
1287                                         em28xx_qctrl[i].maximum)
1288                                                 return -ERANGE;
1289                                         return em28xx_set_ctrl(dev, ctrl);
1290                                 }
1291                         }
1292                 }
1293
1294                 if (dev->decoder == EM28XX_TVP5150) {
1295                         em28xx_i2c_call_clients(dev,cmd,arg);
1296                         return 0;
1297                 } else if (!dev->has_msp34xx) {
1298                         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1299                                 if (ctrl->id == em28xx_qctrl[i].id) {
1300                                         if (ctrl->value <
1301                                         em28xx_qctrl[i].minimum
1302                                         || ctrl->value >
1303                                         em28xx_qctrl[i].maximum)
1304                                                 return -ERANGE;
1305                                         return em28xx_set_ctrl(dev, ctrl);
1306                                 }
1307                         }
1308                         for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1309                                 if (ctrl->id == saa711x_qctrl[i].id) {
1310                                         if (ctrl->value <
1311                                         saa711x_qctrl[i].minimum
1312                                         || ctrl->value >
1313                                         saa711x_qctrl[i].maximum)
1314                                                 return -ERANGE;
1315                                         return saa711x_set_ctrl(dev, ctrl);
1316                                 }
1317                         }
1318                 }
1319
1320                 return -EINVAL;
1321         }
1322         /* --- tuner ioctls ------------------------------------------ */
1323         case VIDIOC_G_TUNER:
1324         {
1325                 struct v4l2_tuner *t = arg;
1326                 int status = 0;
1327
1328                 if (0 != t->index)
1329                         return -EINVAL;
1330
1331                 memset(t, 0, sizeof(*t));
1332                 strcpy(t->name, "Tuner");
1333                 t->type = V4L2_TUNER_ANALOG_TV;
1334                 t->capability = V4L2_TUNER_CAP_NORM;
1335                 t->rangehigh = 0xffffffffUL;    /* FIXME: set correct range */
1336 /*              t->signal = 0xffff;*/
1337 /*              em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1338                 /* No way to get signal strength? */
1339                 down(&dev->lock);
1340                 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1341                                         &status);
1342                 up(&dev->lock);
1343                 t->signal =
1344                         (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1345
1346                 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1347                                 t->afc);
1348                 return 0;
1349         }
1350         case VIDIOC_S_TUNER:
1351         {
1352                 struct v4l2_tuner *t = arg;
1353                 int status = 0;
1354
1355                 if (0 != t->index)
1356                         return -EINVAL;
1357                 memset(t, 0, sizeof(*t));
1358                 strcpy(t->name, "Tuner");
1359                 t->type = V4L2_TUNER_ANALOG_TV;
1360                 t->capability = V4L2_TUNER_CAP_NORM;
1361                 t->rangehigh = 0xffffffffUL;    /* FIXME: set correct range */
1362 /*              t->signal = 0xffff; */
1363                 /* No way to get signal strength? */
1364                 down(&dev->lock);
1365                 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
1366                                         &status);
1367                 up(&dev->lock);
1368                 t->signal =
1369                         (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1370
1371                 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1372                                 t->signal, t->afc);
1373                 return 0;
1374         }
1375         case VIDIOC_G_FREQUENCY:
1376         {
1377                 struct v4l2_frequency *f = arg;
1378
1379                 memset(f, 0, sizeof(*f));
1380                 f->type = V4L2_TUNER_ANALOG_TV;
1381                 f->frequency = dev->ctl_freq;
1382
1383                 return 0;
1384         }
1385         case VIDIOC_S_FREQUENCY:
1386         {
1387                 struct v4l2_frequency *f = arg;
1388
1389                 if (0 != f->tuner)
1390                         return -EINVAL;
1391
1392                 if (V4L2_TUNER_ANALOG_TV != f->type)
1393                         return -EINVAL;
1394
1395                 down(&dev->lock);
1396                 dev->ctl_freq = f->frequency;
1397                 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1398                 up(&dev->lock);
1399                 return 0;
1400         }
1401         case VIDIOC_CROPCAP:
1402         {
1403                 struct v4l2_cropcap *cc = arg;
1404
1405                 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1406                         return -EINVAL;
1407                 cc->bounds.left = 0;
1408                 cc->bounds.top = 0;
1409                 cc->bounds.width = dev->width;
1410                 cc->bounds.height = dev->height;
1411                 cc->defrect = cc->bounds;
1412                 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1413                 cc->pixelaspect.denominator = 59;
1414                 return 0;
1415         }
1416         case VIDIOC_STREAMON:
1417         {
1418                 int *type = arg;
1419
1420                 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1421                         || dev->io != IO_MMAP)
1422                         return -EINVAL;
1423
1424                 if (list_empty(&dev->inqueue))
1425                         return -EINVAL;
1426
1427                 dev->stream = STREAM_ON;        /* FIXME: Start video capture here? */
1428
1429                 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1430
1431                 return 0;
1432         }
1433         case VIDIOC_STREAMOFF:
1434         {
1435                 int *type = arg;
1436                 int ret;
1437
1438                 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1439                         || dev->io != IO_MMAP)
1440                         return -EINVAL;
1441
1442                 if (dev->stream == STREAM_ON) {
1443                         em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1444                         if ((ret = em28xx_stream_interrupt(dev)))
1445                                 return ret;
1446                 }
1447                 em28xx_empty_framequeues(dev);
1448
1449                 return 0;
1450         }
1451         default:
1452                 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1453                                                   driver_ioctl);
1454         }
1455         return 0;
1456 }
1457
1458 /*
1459  * em28xx_v4l2_do_ioctl()
1460  * This function is _not_ called directly, but from
1461  * em28xx_v4l2_ioctl. Userspace
1462  * copying is done already, arg is a kernel pointer.
1463  */
1464 static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
1465                                  unsigned int cmd, void *arg)
1466 {
1467         struct em28xx *dev = filp->private_data;
1468
1469         if (!dev)
1470                 return -ENODEV;
1471
1472         if (video_debug > 1)
1473                 v4l_print_ioctl(dev->name,cmd);
1474
1475         switch (cmd) {
1476
1477                 /* --- capabilities ------------------------------------------ */
1478         case VIDIOC_QUERYCAP:
1479                 {
1480                 struct v4l2_capability *cap = arg;
1481
1482                 memset(cap, 0, sizeof(*cap));
1483                 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1484                 strlcpy(cap->card, em28xx_boards[dev->model].name,
1485                         sizeof(cap->card));
1486                 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1487                         sizeof(cap->bus_info));
1488                 cap->version = EM28XX_VERSION_CODE;
1489                 cap->capabilities =
1490                                 V4L2_CAP_SLICED_VBI_CAPTURE |
1491                                 V4L2_CAP_VIDEO_CAPTURE |
1492                                 V4L2_CAP_AUDIO |
1493                                 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1494                 if (dev->has_tuner)
1495                         cap->capabilities |= V4L2_CAP_TUNER;
1496                 return 0;
1497         }
1498         /* --- capture ioctls ---------------------------------------- */
1499         case VIDIOC_ENUM_FMT:
1500         {
1501                 struct v4l2_fmtdesc *fmtd = arg;
1502
1503                 if (fmtd->index != 0)
1504                         return -EINVAL;
1505                 memset(fmtd, 0, sizeof(*fmtd));
1506                 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1507                 strcpy(fmtd->description, "Packed YUY2");
1508                 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1509                 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1510                 return 0;
1511         }
1512         case VIDIOC_G_FMT:
1513                 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1514
1515         case VIDIOC_TRY_FMT:
1516         case VIDIOC_S_FMT:
1517                 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
1518
1519         case VIDIOC_REQBUFS:
1520         {
1521                 struct v4l2_requestbuffers *rb = arg;
1522                 u32 i;
1523                 int ret;
1524
1525                 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1526                         rb->memory != V4L2_MEMORY_MMAP)
1527                         return -EINVAL;
1528
1529                 if (dev->io == IO_READ) {
1530                         em28xx_videodbg ("method is set to read;"
1531                                 " close and open the device again to"
1532                                 " choose the mmap I/O method\n");
1533                         return -EINVAL;
1534                 }
1535
1536                 for (i = 0; i < dev->num_frames; i++)
1537                         if (dev->frame[i].vma_use_count) {
1538                                 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1539                                 return -EINVAL;
1540                         }
1541
1542                 if (dev->stream == STREAM_ON) {
1543                         em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1544                         if ((ret = em28xx_stream_interrupt(dev)))
1545                                 return ret;
1546                 }
1547
1548                 em28xx_empty_framequeues(dev);
1549
1550                 em28xx_release_buffers(dev);
1551                 if (rb->count)
1552                         rb->count =
1553                                 em28xx_request_buffers(dev, rb->count);
1554
1555                 dev->frame_current = NULL;
1556
1557                 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1558                                                 rb->count);
1559                 dev->io = rb->count ? IO_MMAP : IO_NONE;
1560                 return 0;
1561         }
1562         case VIDIOC_QUERYBUF:
1563         {
1564                 struct v4l2_buffer *b = arg;
1565
1566                 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1567                         b->index >= dev->num_frames || dev->io != IO_MMAP)
1568                         return -EINVAL;
1569
1570                 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1571
1572                 if (dev->frame[b->index].vma_use_count) {
1573                         b->flags |= V4L2_BUF_FLAG_MAPPED;
1574                 }
1575                 if (dev->frame[b->index].state == F_DONE)
1576                         b->flags |= V4L2_BUF_FLAG_DONE;
1577                 else if (dev->frame[b->index].state != F_UNUSED)
1578                         b->flags |= V4L2_BUF_FLAG_QUEUED;
1579                 return 0;
1580         }
1581         case VIDIOC_QBUF:
1582         {
1583                 struct v4l2_buffer *b = arg;
1584                 unsigned long lock_flags;
1585
1586                 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1587                         b->index >= dev->num_frames || dev->io != IO_MMAP) {
1588                         return -EINVAL;
1589                 }
1590
1591                 if (dev->frame[b->index].state != F_UNUSED) {
1592                         return -EAGAIN;
1593                 }
1594                 dev->frame[b->index].state = F_QUEUED;
1595
1596                 /* add frame to fifo */
1597                 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1598                 list_add_tail(&dev->frame[b->index].frame,
1599                                 &dev->inqueue);
1600                 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1601
1602                 return 0;
1603         }
1604         case VIDIOC_DQBUF:
1605         {
1606                 struct v4l2_buffer *b = arg;
1607                 struct em28xx_frame_t *f;
1608                 unsigned long lock_flags;
1609                 int ret = 0;
1610
1611                 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1612                         || dev->io != IO_MMAP)
1613                         return -EINVAL;
1614
1615                 if (list_empty(&dev->outqueue)) {
1616                         if (dev->stream == STREAM_OFF)
1617                                 return -EINVAL;
1618                         if (filp->f_flags & O_NONBLOCK)
1619                                 return -EAGAIN;
1620                         ret = wait_event_interruptible
1621                                 (dev->wait_frame,
1622                                 (!list_empty(&dev->outqueue)) ||
1623                                 (dev->state & DEV_DISCONNECTED));
1624                         if (ret)
1625                                 return ret;
1626                         if (dev->state & DEV_DISCONNECTED)
1627                                 return -ENODEV;
1628                 }
1629
1630                 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1631                 f = list_entry(dev->outqueue.next,
1632                                 struct em28xx_frame_t, frame);
1633                 list_del(dev->outqueue.next);
1634                 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1635
1636                 f->state = F_UNUSED;
1637                 memcpy(b, &f->buf, sizeof(*b));
1638
1639                 if (f->vma_use_count)
1640                         b->flags |= V4L2_BUF_FLAG_MAPPED;
1641
1642                 return 0;
1643         }
1644         default:
1645                 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1646                                        em28xx_video_do_ioctl);
1647         }
1648         return 0;
1649 }
1650
1651 /*
1652  * em28xx_v4l2_ioctl()
1653  * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1654  */
1655 static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
1656                              unsigned int cmd, unsigned long arg)
1657 {
1658         int ret = 0;
1659         struct em28xx *dev = filp->private_data;
1660
1661         if (down_interruptible(&dev->fileop_lock))
1662                 return -ERESTARTSYS;
1663
1664         if (dev->state & DEV_DISCONNECTED) {
1665                 em28xx_errdev("v4l2 ioctl: device not present\n");
1666                 up(&dev->fileop_lock);
1667                 return -ENODEV;
1668         }
1669
1670         if (dev->state & DEV_MISCONFIGURED) {
1671                 em28xx_errdev
1672                     ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1673                 up(&dev->fileop_lock);
1674                 return -EIO;
1675         }
1676
1677         ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
1678
1679         up(&dev->fileop_lock);
1680
1681         return ret;
1682 }
1683
1684 static struct file_operations em28xx_v4l_fops = {
1685         .owner = THIS_MODULE,
1686         .open = em28xx_v4l2_open,
1687         .release = em28xx_v4l2_close,
1688         .ioctl = em28xx_v4l2_ioctl,
1689         .read = em28xx_v4l2_read,
1690         .poll = em28xx_v4l2_poll,
1691         .mmap = em28xx_v4l2_mmap,
1692         .llseek = no_llseek,
1693         .compat_ioctl   = v4l_compat_ioctl32,
1694
1695 };
1696
1697 /******************************** usb interface *****************************************/
1698
1699 /*
1700  * em28xx_init_dev()
1701  * allocates and inits the device structs, registers i2c bus and v4l device
1702  */
1703 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1704                            int minor, int model)
1705 {
1706         struct em28xx *dev = *devhandle;
1707         int retval = -ENOMEM;
1708         int errCode, i;
1709         unsigned int maxh, maxw;
1710
1711         dev->udev = udev;
1712         dev->model = model;
1713         init_MUTEX(&dev->lock);
1714         init_waitqueue_head(&dev->open);
1715
1716         dev->em28xx_write_regs = em28xx_write_regs;
1717         dev->em28xx_read_reg = em28xx_read_reg;
1718         dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1719         dev->em28xx_write_regs_req = em28xx_write_regs_req;
1720         dev->em28xx_read_reg_req = em28xx_read_reg_req;
1721         dev->is_em2800 = em28xx_boards[model].is_em2800;
1722         dev->has_tuner = em28xx_boards[model].has_tuner;
1723         dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1724         dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1725         dev->decoder = em28xx_boards[model].decoder;
1726
1727         if (tuner >= 0)
1728                 dev->tuner_type = tuner;
1729         else
1730                 dev->tuner_type = em28xx_boards[model].tuner_type;
1731
1732         dev->video_inputs = em28xx_boards[model].vchannels;
1733
1734         for (i = 0; i < TVNORMS; i++)
1735                 if (em28xx_boards[model].norm == tvnorms[i].mode)
1736                         break;
1737         if (i == TVNORMS)
1738                 i = 0;
1739
1740         dev->tvnorm = &tvnorms[i];      /* set default norm */
1741
1742         em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1743
1744         maxw = norm_maxw(dev);
1745         maxh = norm_maxh(dev);
1746
1747         /* set default image size */
1748         dev->width = maxw;
1749         dev->height = maxh;
1750         dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1751         dev->field_size = dev->width * dev->height;
1752         dev->frame_size =
1753             dev->interlaced ? dev->field_size << 1 : dev->field_size;
1754         dev->bytesperline = dev->width * 2;
1755         dev->hscale = 0;
1756         dev->vscale = 0;
1757         dev->ctl_input = 2;
1758
1759         /* setup video picture settings for saa7113h */
1760         memset(&dev->vpic, 0, sizeof(dev->vpic));
1761         dev->vpic.colour = 128 << 8;
1762         dev->vpic.hue = 128 << 8;
1763         dev->vpic.brightness = 128 << 8;
1764         dev->vpic.contrast = 192 << 8;
1765         dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1766         dev->vpic.depth = 16;
1767         dev->vpic.palette = VIDEO_PALETTE_YUV422;
1768
1769 #ifdef CONFIG_MODULES
1770         /* request some modules */
1771         if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
1772                 request_module("saa711x");
1773         if (dev->decoder == EM28XX_TVP5150)
1774                 request_module("tvp5150");
1775         if (dev->has_tuner)
1776                 request_module("tuner");
1777         if (dev->tda9887_conf)
1778                 request_module("tda9887");
1779 #endif
1780         errCode = em28xx_config(dev);
1781         if (errCode) {
1782                 em28xx_errdev("error configuring device\n");
1783                 kfree(dev);
1784                 em28xx_devused&=~(1<<dev->devno);
1785                 return -ENOMEM;
1786         }
1787
1788         down(&dev->lock);
1789         /* register i2c bus */
1790         em28xx_i2c_register(dev);
1791
1792         /* Do board specific init and eeprom reading */
1793         em28xx_card_setup(dev);
1794
1795         /* configure the device */
1796         em28xx_config_i2c(dev);
1797
1798         up(&dev->lock);
1799
1800         errCode = em28xx_config(dev);
1801
1802 #ifdef CONFIG_MODULES
1803         if (dev->has_msp34xx)
1804                 request_module("msp3400");
1805 #endif
1806         /* allocate and fill v4l2 device struct */
1807         dev->vdev = video_device_alloc();
1808         if (NULL == dev->vdev) {
1809                 em28xx_errdev("cannot allocate video_device.\n");
1810                 kfree(dev);
1811                 em28xx_devused&=~(1<<dev->devno);
1812                 return -ENOMEM;
1813         }
1814
1815         dev->vbi_dev = video_device_alloc();
1816         if (NULL == dev->vbi_dev) {
1817                 em28xx_errdev("cannot allocate video_device.\n");
1818                 kfree(dev->vdev);
1819                 kfree(dev);
1820                 em28xx_devused&=~(1<<dev->devno);
1821                 return -ENOMEM;
1822         }
1823
1824         /* Fills VBI device info */
1825         dev->vbi_dev->type = VFL_TYPE_VBI;
1826         dev->vbi_dev->hardware = 0;
1827         dev->vbi_dev->fops = &em28xx_v4l_fops;
1828         dev->vbi_dev->minor = -1;
1829         dev->vbi_dev->dev = &dev->udev->dev;
1830         dev->vbi_dev->release = video_device_release;
1831         snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1832                                                          "em28xx",dev->devno,"vbi");
1833
1834         /* Fills CAPTURE device info */
1835         dev->vdev->type = VID_TYPE_CAPTURE;
1836         if (dev->has_tuner)
1837                 dev->vdev->type |= VID_TYPE_TUNER;
1838         dev->vdev->hardware = 0;
1839         dev->vdev->fops = &em28xx_v4l_fops;
1840         dev->vdev->minor = -1;
1841         dev->vdev->dev = &dev->udev->dev;
1842         dev->vdev->release = video_device_release;
1843         snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1844                                                          "em28xx",dev->devno,"video");
1845
1846         list_add_tail(&dev->devlist,&em28xx_devlist);
1847
1848         /* register v4l2 device */
1849         down(&dev->lock);
1850         if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1851                                          video_nr[dev->devno]))) {
1852                 em28xx_errdev("unable to register video device (error=%i).\n",
1853                               retval);
1854                 up(&dev->lock);
1855                 list_del(&dev->devlist);
1856                 video_device_release(dev->vdev);
1857                 kfree(dev);
1858                 em28xx_devused&=~(1<<dev->devno);
1859                 return -ENODEV;
1860         }
1861
1862         if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1863                                         vbi_nr[dev->devno]) < 0) {
1864                 printk("unable to register vbi device\n");
1865                 up(&dev->lock);
1866                 list_del(&dev->devlist);
1867                 video_device_release(dev->vbi_dev);
1868                 video_device_release(dev->vdev);
1869                 kfree(dev);
1870                 em28xx_devused&=~(1<<dev->devno);
1871                 return -ENODEV;
1872         } else {
1873                 printk("registered VBI\n");
1874         }
1875
1876         if (dev->has_msp34xx) {
1877                 /* Send a reset to other chips via gpio */
1878                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1879                 udelay(2500);
1880                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1881                 udelay(2500);
1882
1883         }
1884         video_mux(dev, 0);
1885
1886         up(&dev->lock);
1887
1888         em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1889                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1890                                 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1891
1892         return 0;
1893 }
1894
1895 /*
1896  * em28xx_usb_probe()
1897  * checks for supported devices
1898  */
1899 static int em28xx_usb_probe(struct usb_interface *interface,
1900                             const struct usb_device_id *id)
1901 {
1902         const struct usb_endpoint_descriptor *endpoint;
1903         struct usb_device *udev;
1904         struct usb_interface *uif;
1905         struct em28xx *dev = NULL;
1906         int retval = -ENODEV;
1907         int model,i,nr,ifnum;
1908
1909         udev = usb_get_dev(interface_to_usbdev(interface));
1910         ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1911
1912         /* Check to see next free device and mark as used */
1913         nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1914         em28xx_devused|=1<<nr;
1915
1916         /* Don't register audio interfaces */
1917         if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1918                 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1919                                 udev->descriptor.idVendor,udev->descriptor.idProduct,
1920                                 ifnum,
1921                                 interface->altsetting[0].desc.bInterfaceClass);
1922
1923                 em28xx_devused&=~(1<<nr);
1924                 return -ENODEV;
1925         }
1926
1927         em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1928                         udev->descriptor.idVendor,udev->descriptor.idProduct,
1929                         ifnum,
1930                         interface->altsetting[0].desc.bInterfaceClass);
1931
1932         endpoint = &interface->cur_altsetting->endpoint[1].desc;
1933
1934         /* check if the the device has the iso in endpoint at the correct place */
1935         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1936             USB_ENDPOINT_XFER_ISOC) {
1937                 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1938                 em28xx_devused&=~(1<<nr);
1939                 return -ENODEV;
1940         }
1941         if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1942                 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1943                 em28xx_devused&=~(1<<nr);
1944                 return -ENODEV;
1945         }
1946
1947         model=id->driver_info;
1948
1949         if (nr > EM28XX_MAXBOARDS) {
1950                 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1951                 em28xx_devused&=~(1<<nr);
1952                 return -ENOMEM;
1953         }
1954
1955         /* allocate memory for our device state and initialize it */
1956         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1957         if (dev == NULL) {
1958                 em28xx_err(DRIVER_NAME ": out of memory!\n");
1959                 em28xx_devused&=~(1<<nr);
1960                 return -ENOMEM;
1961         }
1962
1963         snprintf(dev->name, 29, "em28xx #%d", nr);
1964         dev->devno=nr;
1965
1966         /* compute alternate max packet sizes */
1967         uif = udev->actconfig->interface[0];
1968
1969         dev->num_alt=uif->num_altsetting;
1970         em28xx_info("Alternate settings: %i\n",dev->num_alt);
1971 //      dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1972         dev->alt_max_pkt_size = kmalloc(32*
1973                                                 dev->num_alt,GFP_KERNEL);
1974         if (dev->alt_max_pkt_size == NULL) {
1975                 em28xx_errdev("out of memory!\n");
1976                 em28xx_devused&=~(1<<nr);
1977                 return -ENOMEM;
1978         }
1979
1980         for (i = 0; i < dev->num_alt ; i++) {
1981                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1982                                                         wMaxPacketSize);
1983                 dev->alt_max_pkt_size[i] =
1984                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1985                 em28xx_info("Alternate setting %i, max size= %i\n",i,
1986                                                         dev->alt_max_pkt_size[i]);
1987         }
1988
1989         if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1990                 model=card[nr];
1991
1992         if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1993                 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
1994                         "%s: be autodetected.  Please pass card=<n> insmod option to\n"
1995                         "%s: workaround that.  Redirect complaints to the vendor of\n"
1996                         "%s: the TV card. Generic type will be used."
1997                         "%s: Best regards,\n"
1998                         "%s:         -- tux\n",
1999                         dev->name,dev->name,dev->name,dev->name,dev->name);
2000                 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
2001                         dev->name);
2002                 for (i = 0; i < em28xx_bcount; i++) {
2003                         em28xx_errdev("    card=%d -> %s\n", i,
2004                                                         em28xx_boards[i].name);
2005                 }
2006         }
2007
2008         /* allocate device struct */
2009         retval = em28xx_init_dev(&dev, udev, nr, model);
2010         if (retval)
2011                 return retval;
2012
2013         em28xx_info("Found %s\n", em28xx_boards[model].name);
2014
2015         /* save our data pointer in this interface device */
2016         usb_set_intfdata(interface, dev);
2017         return 0;
2018 }
2019
2020 /*
2021  * em28xx_usb_disconnect()
2022  * called when the device gets diconencted
2023  * video device will be unregistered on v4l2_close in case it is still open
2024  */
2025 static void em28xx_usb_disconnect(struct usb_interface *interface)
2026 {
2027         struct em28xx *dev = usb_get_intfdata(interface);
2028         usb_set_intfdata(interface, NULL);
2029
2030         if (!dev)
2031                 return;
2032
2033         down_write(&em28xx_disconnect);
2034
2035         down(&dev->lock);
2036
2037         em28xx_info("disconnecting %s\n", dev->vdev->name);
2038
2039         wake_up_interruptible_all(&dev->open);
2040
2041         if (dev->users) {
2042                 em28xx_warn
2043                     ("device /dev/video%d is open! Deregistration and memory "
2044                      "deallocation are deferred on close.\n",
2045                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2046
2047                 dev->state |= DEV_MISCONFIGURED;
2048                 em28xx_uninit_isoc(dev);
2049                 dev->state |= DEV_DISCONNECTED;
2050                 wake_up_interruptible(&dev->wait_frame);
2051                 wake_up_interruptible(&dev->wait_stream);
2052         } else {
2053                 dev->state |= DEV_DISCONNECTED;
2054                 em28xx_release_resources(dev);
2055         }
2056
2057         up(&dev->lock);
2058
2059         if (!dev->users) {
2060                 kfree(dev->alt_max_pkt_size);
2061                 kfree(dev);
2062         }
2063
2064         up_write(&em28xx_disconnect);
2065 }
2066
2067 static struct usb_driver em28xx_usb_driver = {
2068         .name = "em28xx",
2069         .probe = em28xx_usb_probe,
2070         .disconnect = em28xx_usb_disconnect,
2071         .id_table = em28xx_id_table,
2072 };
2073
2074 static int __init em28xx_module_init(void)
2075 {
2076         int result;
2077
2078         printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2079                (EM28XX_VERSION_CODE >> 16) & 0xff,
2080                (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2081 #ifdef SNAPSHOT
2082         printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2083                SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2084 #endif
2085
2086         /* register this driver with the USB subsystem */
2087         result = usb_register(&em28xx_usb_driver);
2088         if (result)
2089                 em28xx_err(DRIVER_NAME
2090                            " usb_register failed. Error number %d.\n", result);
2091
2092         return result;
2093 }
2094
2095 static void __exit em28xx_module_exit(void)
2096 {
2097         /* deregister this driver with the USB subsystem */
2098         usb_deregister(&em28xx_usb_driver);
2099 }
2100
2101 module_init(em28xx_module_init);
2102 module_exit(em28xx_module_exit);