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