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