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