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