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