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