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