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