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