]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (11380): v4l2-subdev: change s_routing prototype
[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
3                     video capture devices
4
5    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6                       Markus Rechberger <mrechberger@gmail.com>
7                       Mauro Carvalho Chehab <mchehab@infradead.org>
8                       Sascha Sommer <saschasommer@freenet.de>
9
10         Some parts based on SN9C10x PC Camera Controllers GPL driver made
11                 by Luca Risolia <luca.risolia@studio.unibo.it>
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
38
39 #include "em28xx.h"
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
45
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47                       "Markus Rechberger <mrechberger@gmail.com>, " \
48                       "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49                       "Sascha Sommer <saschasommer@freenet.de>"
50
51 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE  KERNEL_VERSION(0, 1, 2)
53
54 #define em28xx_videodbg(fmt, arg...) do {\
55         if (video_debug) \
56                 printk(KERN_INFO "%s %s :"fmt, \
57                          dev->name, __func__ , ##arg); } while (0)
58
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
62
63 #define em28xx_isocdbg(fmt, arg...) \
64 do {\
65         if (isoc_debug) { \
66                 printk(KERN_INFO "%s %s :"fmt, \
67                          dev->name, __func__ , ##arg); \
68         } \
69   } while (0)
70
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
74
75 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[]   = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
79 module_param_array(video_nr, int, NULL, 0444);
80 module_param_array(vbi_nr, int, NULL, 0444);
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr,   "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
85
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
89
90 /* supported video standards */
91 static struct em28xx_fmt format[] = {
92         {
93                 .name     = "16bpp YUY2, 4:2:2, packed",
94                 .fourcc   = V4L2_PIX_FMT_YUYV,
95                 .depth    = 16,
96                 .reg      = EM28XX_OUTFMT_YUV422_Y0UY1V,
97         },
98 };
99
100 /* supported controls */
101 /* Common to all boards */
102 static struct v4l2_queryctrl em28xx_qctrl[] = {
103         {
104                 .id = V4L2_CID_AUDIO_VOLUME,
105                 .type = V4L2_CTRL_TYPE_INTEGER,
106                 .name = "Volume",
107                 .minimum = 0x0,
108                 .maximum = 0x1f,
109                 .step = 0x1,
110                 .default_value = 0x1f,
111                 .flags = 0,
112         }, {
113                 .id = V4L2_CID_AUDIO_MUTE,
114                 .type = V4L2_CTRL_TYPE_BOOLEAN,
115                 .name = "Mute",
116                 .minimum = 0,
117                 .maximum = 1,
118                 .step = 1,
119                 .default_value = 1,
120                 .flags = 0,
121         }
122 };
123
124 /* ------------------------------------------------------------------
125         DMA and thread functions
126    ------------------------------------------------------------------*/
127
128 /*
129  * Announces that a buffer were filled and request the next
130  */
131 static inline void buffer_filled(struct em28xx *dev,
132                                   struct em28xx_dmaqueue *dma_q,
133                                   struct em28xx_buffer *buf)
134 {
135         /* Advice that buffer was filled */
136         em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137         buf->vb.state = VIDEOBUF_DONE;
138         buf->vb.field_count++;
139         do_gettimeofday(&buf->vb.ts);
140
141         dev->isoc_ctl.buf = NULL;
142
143         list_del(&buf->vb.queue);
144         wake_up(&buf->vb.done);
145 }
146
147 /*
148  * Identify the buffer header type and properly handles
149  */
150 static void em28xx_copy_video(struct em28xx *dev,
151                               struct em28xx_dmaqueue  *dma_q,
152                               struct em28xx_buffer *buf,
153                               unsigned char *p,
154                               unsigned char *outp, unsigned long len)
155 {
156         void *fieldstart, *startwrite, *startread;
157         int  linesdone, currlinedone, offset, lencopy, remain;
158         int bytesperline = dev->width << 1;
159
160         if (dma_q->pos + len > buf->vb.size)
161                 len = buf->vb.size - dma_q->pos;
162
163         if (p[0] != 0x88 && p[0] != 0x22) {
164                 em28xx_isocdbg("frame is not complete\n");
165                 len += 4;
166         } else
167                 p += 4;
168
169         startread = p;
170         remain = len;
171
172         /* Interlaces frame */
173         if (buf->top_field)
174                 fieldstart = outp;
175         else
176                 fieldstart = outp + bytesperline;
177
178         linesdone = dma_q->pos / bytesperline;
179         currlinedone = dma_q->pos % bytesperline;
180         offset = linesdone * bytesperline * 2 + currlinedone;
181         startwrite = fieldstart + offset;
182         lencopy = bytesperline - currlinedone;
183         lencopy = lencopy > remain ? remain : lencopy;
184
185         if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
186                 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
187                                ((char *)startwrite + lencopy) -
188                                ((char *)outp + buf->vb.size));
189                 remain = (char *)outp + buf->vb.size - (char *)startwrite;
190                 lencopy = remain;
191         }
192         if (lencopy <= 0)
193                 return;
194         memcpy(startwrite, startread, lencopy);
195
196         remain -= lencopy;
197
198         while (remain > 0) {
199                 startwrite += lencopy + bytesperline;
200                 startread += lencopy;
201                 if (bytesperline > remain)
202                         lencopy = remain;
203                 else
204                         lencopy = bytesperline;
205
206                 if ((char *)startwrite + lencopy > (char *)outp +
207                     buf->vb.size) {
208                         em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
209                                        ((char *)startwrite + lencopy) -
210                                        ((char *)outp + buf->vb.size));
211                         lencopy = remain = (char *)outp + buf->vb.size -
212                                            (char *)startwrite;
213                 }
214                 if (lencopy <= 0)
215                         break;
216
217                 memcpy(startwrite, startread, lencopy);
218
219                 remain -= lencopy;
220         }
221
222         dma_q->pos += len;
223 }
224
225 static inline void print_err_status(struct em28xx *dev,
226                                      int packet, int status)
227 {
228         char *errmsg = "Unknown";
229
230         switch (status) {
231         case -ENOENT:
232                 errmsg = "unlinked synchronuously";
233                 break;
234         case -ECONNRESET:
235                 errmsg = "unlinked asynchronuously";
236                 break;
237         case -ENOSR:
238                 errmsg = "Buffer error (overrun)";
239                 break;
240         case -EPIPE:
241                 errmsg = "Stalled (device not responding)";
242                 break;
243         case -EOVERFLOW:
244                 errmsg = "Babble (bad cable?)";
245                 break;
246         case -EPROTO:
247                 errmsg = "Bit-stuff error (bad cable?)";
248                 break;
249         case -EILSEQ:
250                 errmsg = "CRC/Timeout (could be anything)";
251                 break;
252         case -ETIME:
253                 errmsg = "Device does not respond";
254                 break;
255         }
256         if (packet < 0) {
257                 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
258         } else {
259                 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
260                                packet, status, errmsg);
261         }
262 }
263
264 /*
265  * video-buf generic routine to get the next available buffer
266  */
267 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
268                                           struct em28xx_buffer **buf)
269 {
270         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
271         char *outp;
272
273         if (list_empty(&dma_q->active)) {
274                 em28xx_isocdbg("No active queue to serve\n");
275                 dev->isoc_ctl.buf = NULL;
276                 *buf = NULL;
277                 return;
278         }
279
280         /* Get the next buffer */
281         *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
282
283         /* Cleans up buffer - Usefull for testing for frame/URB loss */
284         outp = videobuf_to_vmalloc(&(*buf)->vb);
285         memset(outp, 0, (*buf)->vb.size);
286
287         dev->isoc_ctl.buf = *buf;
288
289         return;
290 }
291
292 /*
293  * Controls the isoc copy of each urb packet
294  */
295 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
296 {
297         struct em28xx_buffer    *buf;
298         struct em28xx_dmaqueue  *dma_q = urb->context;
299         unsigned char *outp = NULL;
300         int i, len = 0, rc = 1;
301         unsigned char *p;
302
303         if (!dev)
304                 return 0;
305
306         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
307                 return 0;
308
309         if (urb->status < 0) {
310                 print_err_status(dev, -1, urb->status);
311                 if (urb->status == -ENOENT)
312                         return 0;
313         }
314
315         buf = dev->isoc_ctl.buf;
316         if (buf != NULL)
317                 outp = videobuf_to_vmalloc(&buf->vb);
318
319         for (i = 0; i < urb->number_of_packets; i++) {
320                 int status = urb->iso_frame_desc[i].status;
321
322                 if (status < 0) {
323                         print_err_status(dev, i, status);
324                         if (urb->iso_frame_desc[i].status != -EPROTO)
325                                 continue;
326                 }
327
328                 len = urb->iso_frame_desc[i].actual_length - 4;
329
330                 if (urb->iso_frame_desc[i].actual_length <= 0) {
331                         /* em28xx_isocdbg("packet %d is empty",i); - spammy */
332                         continue;
333                 }
334                 if (urb->iso_frame_desc[i].actual_length >
335                                                 dev->max_pkt_size) {
336                         em28xx_isocdbg("packet bigger than packet size");
337                         continue;
338                 }
339
340                 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
341
342                 /* FIXME: incomplete buffer checks where removed to make
343                    logic simpler. Impacts of those changes should be evaluated
344                  */
345                 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
346                         em28xx_isocdbg("VBI HEADER!!!\n");
347                         /* FIXME: Should add vbi copy */
348                         continue;
349                 }
350                 if (p[0] == 0x22 && p[1] == 0x5a) {
351                         em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
352                                        len, (p[2] & 1) ? "odd" : "even");
353
354                         if (!(p[2] & 1)) {
355                                 if (buf != NULL)
356                                         buffer_filled(dev, dma_q, buf);
357                                 get_next_buf(dma_q, &buf);
358                                 if (buf == NULL)
359                                         outp = NULL;
360                                 else
361                                         outp = videobuf_to_vmalloc(&buf->vb);
362                         }
363
364                         if (buf != NULL) {
365                                 if (p[2] & 1)
366                                         buf->top_field = 0;
367                                 else
368                                         buf->top_field = 1;
369                         }
370
371                         dma_q->pos = 0;
372                 }
373                 if (buf != NULL)
374                         em28xx_copy_video(dev, dma_q, buf, p, outp, len);
375         }
376         return rc;
377 }
378
379 /* ------------------------------------------------------------------
380         Videobuf operations
381    ------------------------------------------------------------------*/
382
383 static int
384 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
385 {
386         struct em28xx_fh *fh = vq->priv_data;
387         struct em28xx        *dev = fh->dev;
388         struct v4l2_frequency f;
389
390         *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
391
392         if (0 == *count)
393                 *count = EM28XX_DEF_BUF;
394
395         if (*count < EM28XX_MIN_BUF)
396                 *count = EM28XX_MIN_BUF;
397
398         /* Ask tuner to go to analog or radio mode */
399         memset(&f, 0, sizeof(f));
400         f.frequency = dev->ctl_freq;
401         f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
402
403         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
404
405         return 0;
406 }
407
408 /* This is called *without* dev->slock held; please keep it that way */
409 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
410 {
411         struct em28xx_fh     *fh  = vq->priv_data;
412         struct em28xx        *dev = fh->dev;
413         unsigned long flags = 0;
414         if (in_interrupt())
415                 BUG();
416
417         /* We used to wait for the buffer to finish here, but this didn't work
418            because, as we were keeping the state as VIDEOBUF_QUEUED,
419            videobuf_queue_cancel marked it as finished for us.
420            (Also, it could wedge forever if the hardware was misconfigured.)
421
422            This should be safe; by the time we get here, the buffer isn't
423            queued anymore. If we ever start marking the buffers as
424            VIDEOBUF_ACTIVE, it won't be, though.
425         */
426         spin_lock_irqsave(&dev->slock, flags);
427         if (dev->isoc_ctl.buf == buf)
428                 dev->isoc_ctl.buf = NULL;
429         spin_unlock_irqrestore(&dev->slock, flags);
430
431         videobuf_vmalloc_free(&buf->vb);
432         buf->vb.state = VIDEOBUF_NEEDS_INIT;
433 }
434
435 static int
436 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
437                                                 enum v4l2_field field)
438 {
439         struct em28xx_fh     *fh  = vq->priv_data;
440         struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
441         struct em28xx        *dev = fh->dev;
442         int                  rc = 0, urb_init = 0;
443
444         buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
445
446         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
447                 return -EINVAL;
448
449         buf->vb.width  = dev->width;
450         buf->vb.height = dev->height;
451         buf->vb.field  = field;
452
453         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
454                 rc = videobuf_iolock(vq, &buf->vb, NULL);
455                 if (rc < 0)
456                         goto fail;
457         }
458
459         if (!dev->isoc_ctl.num_bufs)
460                 urb_init = 1;
461
462         if (urb_init) {
463                 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
464                                       EM28XX_NUM_BUFS, dev->max_pkt_size,
465                                       em28xx_isoc_copy);
466                 if (rc < 0)
467                         goto fail;
468         }
469
470         buf->vb.state = VIDEOBUF_PREPARED;
471         return 0;
472
473 fail:
474         free_buffer(vq, buf);
475         return rc;
476 }
477
478 static void
479 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
480 {
481         struct em28xx_buffer    *buf     = container_of(vb,
482                                                         struct em28xx_buffer,
483                                                         vb);
484         struct em28xx_fh        *fh      = vq->priv_data;
485         struct em28xx           *dev     = fh->dev;
486         struct em28xx_dmaqueue  *vidq    = &dev->vidq;
487
488         buf->vb.state = VIDEOBUF_QUEUED;
489         list_add_tail(&buf->vb.queue, &vidq->active);
490
491 }
492
493 static void buffer_release(struct videobuf_queue *vq,
494                                 struct videobuf_buffer *vb)
495 {
496         struct em28xx_buffer   *buf  = container_of(vb,
497                                                     struct em28xx_buffer,
498                                                     vb);
499         struct em28xx_fh       *fh   = vq->priv_data;
500         struct em28xx          *dev  = (struct em28xx *)fh->dev;
501
502         em28xx_isocdbg("em28xx: called buffer_release\n");
503
504         free_buffer(vq, buf);
505 }
506
507 static struct videobuf_queue_ops em28xx_video_qops = {
508         .buf_setup      = buffer_setup,
509         .buf_prepare    = buffer_prepare,
510         .buf_queue      = buffer_queue,
511         .buf_release    = buffer_release,
512 };
513
514 /*********************  v4l2 interface  **************************************/
515
516 static void video_mux(struct em28xx *dev, int index)
517 {
518         dev->ctl_input = index;
519         dev->ctl_ainput = INPUT(index)->amux;
520         dev->ctl_aoutput = INPUT(index)->aout;
521
522         if (!dev->ctl_aoutput)
523                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
524
525         v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
526                         INPUT(index)->vmux, 0, 0);
527
528         if (dev->board.has_msp34xx) {
529                 if (dev->i2s_speed) {
530                         v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
531                                 s_i2s_clock_freq, dev->i2s_speed);
532                 }
533                 /* Note: this is msp3400 specific */
534                 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
535                          dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
536         }
537
538         if (dev->board.adecoder != EM28XX_NOADECODER) {
539                 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
540                         dev->ctl_ainput, dev->ctl_aoutput, 0);
541         }
542
543         em28xx_audio_analog_set(dev);
544 }
545
546 /* Usage lock check functions */
547 static int res_get(struct em28xx_fh *fh)
548 {
549         struct em28xx    *dev = fh->dev;
550         int              rc   = 0;
551
552         /* This instance already has stream_on */
553         if (fh->stream_on)
554                 return rc;
555
556         if (dev->stream_on)
557                 return -EBUSY;
558
559         dev->stream_on = 1;
560         fh->stream_on  = 1;
561         return rc;
562 }
563
564 static int res_check(struct em28xx_fh *fh)
565 {
566         return fh->stream_on;
567 }
568
569 static void res_free(struct em28xx_fh *fh)
570 {
571         struct em28xx    *dev = fh->dev;
572
573         fh->stream_on = 0;
574         dev->stream_on = 0;
575 }
576
577 /*
578  * em28xx_get_ctrl()
579  * return the current saturation, brightness or contrast, mute state
580  */
581 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
582 {
583         switch (ctrl->id) {
584         case V4L2_CID_AUDIO_MUTE:
585                 ctrl->value = dev->mute;
586                 return 0;
587         case V4L2_CID_AUDIO_VOLUME:
588                 ctrl->value = dev->volume;
589                 return 0;
590         default:
591                 return -EINVAL;
592         }
593 }
594
595 /*
596  * em28xx_set_ctrl()
597  * mute or set new saturation, brightness or contrast
598  */
599 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
600 {
601         switch (ctrl->id) {
602         case V4L2_CID_AUDIO_MUTE:
603                 if (ctrl->value != dev->mute) {
604                         dev->mute = ctrl->value;
605                         return em28xx_audio_analog_set(dev);
606                 }
607                 return 0;
608         case V4L2_CID_AUDIO_VOLUME:
609                 dev->volume = ctrl->value;
610                 return em28xx_audio_analog_set(dev);
611         default:
612                 return -EINVAL;
613         }
614 }
615
616 static int check_dev(struct em28xx *dev)
617 {
618         if (dev->state & DEV_DISCONNECTED) {
619                 em28xx_errdev("v4l2 ioctl: device not present\n");
620                 return -ENODEV;
621         }
622
623         if (dev->state & DEV_MISCONFIGURED) {
624                 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
625                               "close and open it again\n");
626                 return -EIO;
627         }
628         return 0;
629 }
630
631 static void get_scale(struct em28xx *dev,
632                         unsigned int width, unsigned int height,
633                         unsigned int *hscale, unsigned int *vscale)
634 {
635         unsigned int          maxw   = norm_maxw(dev);
636         unsigned int          maxh   = norm_maxh(dev);
637
638         *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
639         if (*hscale >= 0x4000)
640                 *hscale = 0x3fff;
641
642         *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
643         if (*vscale >= 0x4000)
644                 *vscale = 0x3fff;
645 }
646
647 /* ------------------------------------------------------------------
648         IOCTL vidioc handling
649    ------------------------------------------------------------------*/
650
651 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
652                                         struct v4l2_format *f)
653 {
654         struct em28xx_fh      *fh  = priv;
655         struct em28xx         *dev = fh->dev;
656
657         mutex_lock(&dev->lock);
658
659         f->fmt.pix.width = dev->width;
660         f->fmt.pix.height = dev->height;
661         f->fmt.pix.pixelformat = dev->format->fourcc;
662         f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
663         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline  * dev->height;
664         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
665
666         /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
667         f->fmt.pix.field = dev->interlaced ?
668                            V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
669
670         mutex_unlock(&dev->lock);
671         return 0;
672 }
673
674 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
675 {
676         unsigned int i;
677
678         for (i = 0; i < ARRAY_SIZE(format); i++)
679                 if (format[i].fourcc == fourcc)
680                         return &format[i];
681
682         return NULL;
683 }
684
685 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
686                         struct v4l2_format *f)
687 {
688         struct em28xx_fh      *fh    = priv;
689         struct em28xx         *dev   = fh->dev;
690         int                   width  = f->fmt.pix.width;
691         int                   height = f->fmt.pix.height;
692         unsigned int          maxw   = norm_maxw(dev);
693         unsigned int          maxh   = norm_maxh(dev);
694         unsigned int          hscale, vscale;
695         struct em28xx_fmt     *fmt;
696
697         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
698         if (!fmt) {
699                 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
700                                 f->fmt.pix.pixelformat);
701                 return -EINVAL;
702         }
703
704         /* width must even because of the YUYV format
705            height must be even because of interlacing */
706         height &= 0xfffe;
707         width  &= 0xfffe;
708
709         if (unlikely(height < 32))
710                 height = 32;
711         if (unlikely(height > maxh))
712                 height = maxh;
713         if (unlikely(width < 48))
714                 width = 48;
715         if (unlikely(width > maxw))
716                 width = maxw;
717
718         if (dev->board.is_em2800) {
719                 /* the em2800 can only scale down to 50% */
720                 if (height % (maxh / 2))
721                         height = maxh;
722                 if (width % (maxw / 2))
723                         width = maxw;
724                 /* according to empiatech support */
725                 /* the MaxPacketSize is to small to support */
726                 /* framesizes larger than 640x480 @ 30 fps */
727                 /* or 640x576 @ 25 fps. As this would cut */
728                 /* of a part of the image we prefer */
729                 /* 360x576 or 360x480 for now */
730                 if (width == maxw && height == maxh)
731                         width /= 2;
732         }
733
734         get_scale(dev, width, height, &hscale, &vscale);
735
736         width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
737         height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
738
739         f->fmt.pix.width = width;
740         f->fmt.pix.height = height;
741         f->fmt.pix.pixelformat = fmt->fourcc;
742         f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
743         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
744         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
745         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
746
747         return 0;
748 }
749
750 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
751                         struct v4l2_format *f)
752 {
753         struct em28xx_fh      *fh  = priv;
754         struct em28xx         *dev = fh->dev;
755         int                   rc;
756         struct em28xx_fmt     *fmt;
757
758         rc = check_dev(dev);
759         if (rc < 0)
760                 return rc;
761
762         mutex_lock(&dev->lock);
763
764         vidioc_try_fmt_vid_cap(file, priv, f);
765
766         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
767         if (!fmt) {
768                 rc = -EINVAL;
769                 goto out;
770         }
771
772         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
773                 em28xx_errdev("%s queue busy\n", __func__);
774                 rc = -EBUSY;
775                 goto out;
776         }
777
778         if (dev->stream_on && !fh->stream_on) {
779                 em28xx_errdev("%s device in use by another fh\n", __func__);
780                 rc = -EBUSY;
781                 goto out;
782         }
783
784         /* set new image size */
785         dev->width = f->fmt.pix.width;
786         dev->height = f->fmt.pix.height;
787         dev->format = fmt;
788         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
789
790         em28xx_set_alternate(dev);
791         em28xx_resolution_set(dev);
792
793         rc = 0;
794
795 out:
796         mutex_unlock(&dev->lock);
797         return rc;
798 }
799
800 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
801 {
802         struct em28xx_fh   *fh  = priv;
803         struct em28xx      *dev = fh->dev;
804         struct v4l2_format f;
805         int                rc;
806
807         rc = check_dev(dev);
808         if (rc < 0)
809                 return rc;
810
811         mutex_lock(&dev->lock);
812         dev->norm = *norm;
813
814         /* Adjusts width/height, if needed */
815         f.fmt.pix.width = dev->width;
816         f.fmt.pix.height = dev->height;
817         vidioc_try_fmt_vid_cap(file, priv, &f);
818
819         /* set new image size */
820         dev->width = f.fmt.pix.width;
821         dev->height = f.fmt.pix.height;
822         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
823
824         em28xx_resolution_set(dev);
825         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
826
827         mutex_unlock(&dev->lock);
828         return 0;
829 }
830
831 static const char *iname[] = {
832         [EM28XX_VMUX_COMPOSITE1] = "Composite1",
833         [EM28XX_VMUX_COMPOSITE2] = "Composite2",
834         [EM28XX_VMUX_COMPOSITE3] = "Composite3",
835         [EM28XX_VMUX_COMPOSITE4] = "Composite4",
836         [EM28XX_VMUX_SVIDEO]     = "S-Video",
837         [EM28XX_VMUX_TELEVISION] = "Television",
838         [EM28XX_VMUX_CABLE]      = "Cable TV",
839         [EM28XX_VMUX_DVB]        = "DVB",
840         [EM28XX_VMUX_DEBUG]      = "for debug only",
841 };
842
843 static int vidioc_enum_input(struct file *file, void *priv,
844                                 struct v4l2_input *i)
845 {
846         struct em28xx_fh   *fh  = priv;
847         struct em28xx      *dev = fh->dev;
848         unsigned int       n;
849
850         n = i->index;
851         if (n >= MAX_EM28XX_INPUT)
852                 return -EINVAL;
853         if (0 == INPUT(n)->type)
854                 return -EINVAL;
855
856         i->index = n;
857         i->type = V4L2_INPUT_TYPE_CAMERA;
858
859         strcpy(i->name, iname[INPUT(n)->type]);
860
861         if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
862                 (EM28XX_VMUX_CABLE == INPUT(n)->type))
863                 i->type = V4L2_INPUT_TYPE_TUNER;
864
865         i->std = dev->vdev->tvnorms;
866
867         return 0;
868 }
869
870 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
871 {
872         struct em28xx_fh   *fh  = priv;
873         struct em28xx      *dev = fh->dev;
874
875         *i = dev->ctl_input;
876
877         return 0;
878 }
879
880 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
881 {
882         struct em28xx_fh   *fh  = priv;
883         struct em28xx      *dev = fh->dev;
884         int                rc;
885
886         rc = check_dev(dev);
887         if (rc < 0)
888                 return rc;
889
890         if (i >= MAX_EM28XX_INPUT)
891                 return -EINVAL;
892         if (0 == INPUT(i)->type)
893                 return -EINVAL;
894
895         dev->ctl_input = i;
896
897         mutex_lock(&dev->lock);
898         video_mux(dev, dev->ctl_input);
899         mutex_unlock(&dev->lock);
900         return 0;
901 }
902
903 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
904 {
905         struct em28xx_fh   *fh    = priv;
906         struct em28xx      *dev   = fh->dev;
907
908         switch (a->index) {
909         case EM28XX_AMUX_VIDEO:
910                 strcpy(a->name, "Television");
911                 break;
912         case EM28XX_AMUX_LINE_IN:
913                 strcpy(a->name, "Line In");
914                 break;
915         case EM28XX_AMUX_VIDEO2:
916                 strcpy(a->name, "Television alt");
917                 break;
918         case EM28XX_AMUX_PHONE:
919                 strcpy(a->name, "Phone");
920                 break;
921         case EM28XX_AMUX_MIC:
922                 strcpy(a->name, "Mic");
923                 break;
924         case EM28XX_AMUX_CD:
925                 strcpy(a->name, "CD");
926                 break;
927         case EM28XX_AMUX_AUX:
928                 strcpy(a->name, "Aux");
929                 break;
930         case EM28XX_AMUX_PCM_OUT:
931                 strcpy(a->name, "PCM");
932                 break;
933         default:
934                 return -EINVAL;
935         }
936
937         a->index = dev->ctl_ainput;
938         a->capability = V4L2_AUDCAP_STEREO;
939
940         return 0;
941 }
942
943 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
944 {
945         struct em28xx_fh   *fh  = priv;
946         struct em28xx      *dev = fh->dev;
947
948
949         if (a->index >= MAX_EM28XX_INPUT)
950                 return -EINVAL;
951         if (0 == INPUT(a->index)->type)
952                 return -EINVAL;
953
954         mutex_lock(&dev->lock);
955
956         dev->ctl_ainput = INPUT(a->index)->amux;
957         dev->ctl_aoutput = INPUT(a->index)->aout;
958
959         if (!dev->ctl_aoutput)
960                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
961
962         mutex_unlock(&dev->lock);
963         return 0;
964 }
965
966 static int vidioc_queryctrl(struct file *file, void *priv,
967                                 struct v4l2_queryctrl *qc)
968 {
969         struct em28xx_fh      *fh  = priv;
970         struct em28xx         *dev = fh->dev;
971         int                   id  = qc->id;
972         int                   i;
973         int                   rc;
974
975         rc = check_dev(dev);
976         if (rc < 0)
977                 return rc;
978
979         memset(qc, 0, sizeof(*qc));
980
981         qc->id = id;
982
983         if (!dev->board.has_msp34xx) {
984                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
985                         if (qc->id && qc->id == em28xx_qctrl[i].id) {
986                                 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
987                                 return 0;
988                         }
989                 }
990         }
991
992         mutex_lock(&dev->lock);
993         v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
994         mutex_unlock(&dev->lock);
995
996         if (qc->type)
997                 return 0;
998         else
999                 return -EINVAL;
1000 }
1001
1002 static int vidioc_g_ctrl(struct file *file, void *priv,
1003                                 struct v4l2_control *ctrl)
1004 {
1005         struct em28xx_fh      *fh  = priv;
1006         struct em28xx         *dev = fh->dev;
1007         int                   rc;
1008
1009         rc = check_dev(dev);
1010         if (rc < 0)
1011                 return rc;
1012         rc = 0;
1013
1014         mutex_lock(&dev->lock);
1015
1016         if (dev->board.has_msp34xx)
1017                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1018         else {
1019                 rc = em28xx_get_ctrl(dev, ctrl);
1020                 if (rc < 0) {
1021                         v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1022                         rc = 0;
1023                 }
1024         }
1025
1026         mutex_unlock(&dev->lock);
1027         return rc;
1028 }
1029
1030 static int vidioc_s_ctrl(struct file *file, void *priv,
1031                                 struct v4l2_control *ctrl)
1032 {
1033         struct em28xx_fh      *fh  = priv;
1034         struct em28xx         *dev = fh->dev;
1035         u8                    i;
1036         int                   rc;
1037
1038         rc = check_dev(dev);
1039         if (rc < 0)
1040                 return rc;
1041
1042         mutex_lock(&dev->lock);
1043
1044         if (dev->board.has_msp34xx)
1045                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1046         else {
1047                 rc = 1;
1048                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1049                         if (ctrl->id == em28xx_qctrl[i].id) {
1050                                 if (ctrl->value < em28xx_qctrl[i].minimum ||
1051                                     ctrl->value > em28xx_qctrl[i].maximum) {
1052                                         rc = -ERANGE;
1053                                         break;
1054                                 }
1055
1056                                 rc = em28xx_set_ctrl(dev, ctrl);
1057                                 break;
1058                         }
1059                 }
1060         }
1061
1062         /* Control not found - try to send it to the attached devices */
1063         if (rc == 1) {
1064                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1065                 rc = 0;
1066         }
1067
1068         mutex_unlock(&dev->lock);
1069         return rc;
1070 }
1071
1072 static int vidioc_g_tuner(struct file *file, void *priv,
1073                                 struct v4l2_tuner *t)
1074 {
1075         struct em28xx_fh      *fh  = priv;
1076         struct em28xx         *dev = fh->dev;
1077         int                   rc;
1078
1079         rc = check_dev(dev);
1080         if (rc < 0)
1081                 return rc;
1082
1083         if (0 != t->index)
1084                 return -EINVAL;
1085
1086         strcpy(t->name, "Tuner");
1087
1088         mutex_lock(&dev->lock);
1089         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1090         mutex_unlock(&dev->lock);
1091
1092         return 0;
1093 }
1094
1095 static int vidioc_s_tuner(struct file *file, void *priv,
1096                                 struct v4l2_tuner *t)
1097 {
1098         struct em28xx_fh      *fh  = priv;
1099         struct em28xx         *dev = fh->dev;
1100         int                   rc;
1101
1102         rc = check_dev(dev);
1103         if (rc < 0)
1104                 return rc;
1105
1106         if (0 != t->index)
1107                 return -EINVAL;
1108
1109         mutex_lock(&dev->lock);
1110         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1111         mutex_unlock(&dev->lock);
1112
1113         return 0;
1114 }
1115
1116 static int vidioc_g_frequency(struct file *file, void *priv,
1117                                 struct v4l2_frequency *f)
1118 {
1119         struct em28xx_fh      *fh  = priv;
1120         struct em28xx         *dev = fh->dev;
1121
1122         mutex_lock(&dev->lock);
1123         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1124         f->frequency = dev->ctl_freq;
1125         mutex_unlock(&dev->lock);
1126
1127         return 0;
1128 }
1129
1130 static int vidioc_s_frequency(struct file *file, void *priv,
1131                                 struct v4l2_frequency *f)
1132 {
1133         struct em28xx_fh      *fh  = priv;
1134         struct em28xx         *dev = fh->dev;
1135         int                   rc;
1136
1137         rc = check_dev(dev);
1138         if (rc < 0)
1139                 return rc;
1140
1141         if (0 != f->tuner)
1142                 return -EINVAL;
1143
1144         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1145                 return -EINVAL;
1146         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1147                 return -EINVAL;
1148
1149         mutex_lock(&dev->lock);
1150
1151         dev->ctl_freq = f->frequency;
1152         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1153
1154         mutex_unlock(&dev->lock);
1155
1156         return 0;
1157 }
1158
1159 #ifdef CONFIG_VIDEO_ADV_DEBUG
1160 static int em28xx_reg_len(int reg)
1161 {
1162         switch (reg) {
1163         case EM28XX_R40_AC97LSB:
1164         case EM28XX_R30_HSCALELOW:
1165         case EM28XX_R32_VSCALELOW:
1166                 return 2;
1167         default:
1168                 return 1;
1169         }
1170 }
1171
1172 static int vidioc_g_chip_ident(struct file *file, void *priv,
1173                struct v4l2_dbg_chip_ident *chip)
1174 {
1175         struct em28xx_fh      *fh  = priv;
1176         struct em28xx         *dev = fh->dev;
1177
1178         chip->ident = V4L2_IDENT_NONE;
1179         chip->revision = 0;
1180
1181         v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1182
1183         return 0;
1184 }
1185
1186
1187 static int vidioc_g_register(struct file *file, void *priv,
1188                              struct v4l2_dbg_register *reg)
1189 {
1190         struct em28xx_fh      *fh  = priv;
1191         struct em28xx         *dev = fh->dev;
1192         int ret;
1193
1194         switch (reg->match.type) {
1195         case V4L2_CHIP_MATCH_AC97:
1196                 mutex_lock(&dev->lock);
1197                 ret = em28xx_read_ac97(dev, reg->reg);
1198                 mutex_unlock(&dev->lock);
1199                 if (ret < 0)
1200                         return ret;
1201
1202                 reg->val = ret;
1203                 reg->size = 1;
1204                 return 0;
1205         case V4L2_CHIP_MATCH_I2C_DRIVER:
1206                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1207                 return 0;
1208         case V4L2_CHIP_MATCH_I2C_ADDR:
1209                 /* Not supported yet */
1210                 return -EINVAL;
1211         default:
1212                 if (!v4l2_chip_match_host(&reg->match))
1213                         return -EINVAL;
1214         }
1215
1216         /* Match host */
1217         reg->size = em28xx_reg_len(reg->reg);
1218         if (reg->size == 1) {
1219                 mutex_lock(&dev->lock);
1220                 ret = em28xx_read_reg(dev, reg->reg);
1221                 mutex_unlock(&dev->lock);
1222
1223                 if (ret < 0)
1224                         return ret;
1225
1226                 reg->val = ret;
1227         } else {
1228                 __le16 val = 0;
1229                 mutex_lock(&dev->lock);
1230                 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1231                                                    reg->reg, (char *)&val, 2);
1232                 mutex_unlock(&dev->lock);
1233                 if (ret < 0)
1234                         return ret;
1235
1236                 reg->val = le16_to_cpu(val);
1237         }
1238
1239         return 0;
1240 }
1241
1242 static int vidioc_s_register(struct file *file, void *priv,
1243                              struct v4l2_dbg_register *reg)
1244 {
1245         struct em28xx_fh      *fh  = priv;
1246         struct em28xx         *dev = fh->dev;
1247         __le16 buf;
1248         int    rc;
1249
1250         switch (reg->match.type) {
1251         case V4L2_CHIP_MATCH_AC97:
1252                 mutex_lock(&dev->lock);
1253                 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1254                 mutex_unlock(&dev->lock);
1255
1256                 return rc;
1257         case V4L2_CHIP_MATCH_I2C_DRIVER:
1258                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1259                 return 0;
1260         case V4L2_CHIP_MATCH_I2C_ADDR:
1261                 /* Not supported yet */
1262                 return -EINVAL;
1263         default:
1264                 if (!v4l2_chip_match_host(&reg->match))
1265                         return -EINVAL;
1266         }
1267
1268         /* Match host */
1269         buf = cpu_to_le16(reg->val);
1270
1271         mutex_lock(&dev->lock);
1272         rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1273                                em28xx_reg_len(reg->reg));
1274         mutex_unlock(&dev->lock);
1275
1276         return rc;
1277 }
1278 #endif
1279
1280
1281 static int vidioc_cropcap(struct file *file, void *priv,
1282                                         struct v4l2_cropcap *cc)
1283 {
1284         struct em28xx_fh      *fh  = priv;
1285         struct em28xx         *dev = fh->dev;
1286
1287         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1288                 return -EINVAL;
1289
1290         cc->bounds.left = 0;
1291         cc->bounds.top = 0;
1292         cc->bounds.width = dev->width;
1293         cc->bounds.height = dev->height;
1294         cc->defrect = cc->bounds;
1295         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1296         cc->pixelaspect.denominator = 59;
1297
1298         return 0;
1299 }
1300
1301 static int vidioc_streamon(struct file *file, void *priv,
1302                                         enum v4l2_buf_type type)
1303 {
1304         struct em28xx_fh      *fh  = priv;
1305         struct em28xx         *dev = fh->dev;
1306         int                   rc;
1307
1308         rc = check_dev(dev);
1309         if (rc < 0)
1310                 return rc;
1311
1312
1313         mutex_lock(&dev->lock);
1314         rc = res_get(fh);
1315
1316         if (likely(rc >= 0))
1317                 rc = videobuf_streamon(&fh->vb_vidq);
1318
1319         mutex_unlock(&dev->lock);
1320
1321         return rc;
1322 }
1323
1324 static int vidioc_streamoff(struct file *file, void *priv,
1325                                         enum v4l2_buf_type type)
1326 {
1327         struct em28xx_fh      *fh  = priv;
1328         struct em28xx         *dev = fh->dev;
1329         int                   rc;
1330
1331         rc = check_dev(dev);
1332         if (rc < 0)
1333                 return rc;
1334
1335         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1336                 return -EINVAL;
1337         if (type != fh->type)
1338                 return -EINVAL;
1339
1340         mutex_lock(&dev->lock);
1341
1342         videobuf_streamoff(&fh->vb_vidq);
1343         res_free(fh);
1344
1345         mutex_unlock(&dev->lock);
1346
1347         return 0;
1348 }
1349
1350 static int vidioc_querycap(struct file *file, void  *priv,
1351                                         struct v4l2_capability *cap)
1352 {
1353         struct em28xx_fh      *fh  = priv;
1354         struct em28xx         *dev = fh->dev;
1355
1356         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1357         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1358         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1359
1360         cap->version = EM28XX_VERSION_CODE;
1361
1362         cap->capabilities =
1363                         V4L2_CAP_SLICED_VBI_CAPTURE |
1364                         V4L2_CAP_VIDEO_CAPTURE |
1365                         V4L2_CAP_AUDIO |
1366                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1367
1368         if (dev->tuner_type != TUNER_ABSENT)
1369                 cap->capabilities |= V4L2_CAP_TUNER;
1370
1371         return 0;
1372 }
1373
1374 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1375                                         struct v4l2_fmtdesc *f)
1376 {
1377         if (unlikely(f->index >= ARRAY_SIZE(format)))
1378                 return -EINVAL;
1379
1380         strlcpy(f->description, format[f->index].name, sizeof(f->description));
1381         f->pixelformat = format[f->index].fourcc;
1382
1383         return 0;
1384 }
1385
1386 /* Sliced VBI ioctls */
1387 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1388                                         struct v4l2_format *f)
1389 {
1390         struct em28xx_fh      *fh  = priv;
1391         struct em28xx         *dev = fh->dev;
1392         int                   rc;
1393
1394         rc = check_dev(dev);
1395         if (rc < 0)
1396                 return rc;
1397
1398         mutex_lock(&dev->lock);
1399
1400         f->fmt.sliced.service_set = 0;
1401         v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1402
1403         if (f->fmt.sliced.service_set == 0)
1404                 rc = -EINVAL;
1405
1406         mutex_unlock(&dev->lock);
1407
1408         return rc;
1409 }
1410
1411 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1412                         struct v4l2_format *f)
1413 {
1414         struct em28xx_fh      *fh  = priv;
1415         struct em28xx         *dev = fh->dev;
1416         int                   rc;
1417
1418         rc = check_dev(dev);
1419         if (rc < 0)
1420                 return rc;
1421
1422         mutex_lock(&dev->lock);
1423         v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1424         mutex_unlock(&dev->lock);
1425
1426         if (f->fmt.sliced.service_set == 0)
1427                 return -EINVAL;
1428
1429         return 0;
1430 }
1431
1432
1433 static int vidioc_reqbufs(struct file *file, void *priv,
1434                           struct v4l2_requestbuffers *rb)
1435 {
1436         struct em28xx_fh      *fh  = priv;
1437         struct em28xx         *dev = fh->dev;
1438         int                   rc;
1439
1440         rc = check_dev(dev);
1441         if (rc < 0)
1442                 return rc;
1443
1444         return videobuf_reqbufs(&fh->vb_vidq, rb);
1445 }
1446
1447 static int vidioc_querybuf(struct file *file, void *priv,
1448                            struct v4l2_buffer *b)
1449 {
1450         struct em28xx_fh      *fh  = priv;
1451         struct em28xx         *dev = fh->dev;
1452         int                   rc;
1453
1454         rc = check_dev(dev);
1455         if (rc < 0)
1456                 return rc;
1457
1458         return videobuf_querybuf(&fh->vb_vidq, b);
1459 }
1460
1461 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1462 {
1463         struct em28xx_fh      *fh  = priv;
1464         struct em28xx         *dev = fh->dev;
1465         int                   rc;
1466
1467         rc = check_dev(dev);
1468         if (rc < 0)
1469                 return rc;
1470
1471         return videobuf_qbuf(&fh->vb_vidq, b);
1472 }
1473
1474 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1475 {
1476         struct em28xx_fh      *fh  = priv;
1477         struct em28xx         *dev = fh->dev;
1478         int                   rc;
1479
1480         rc = check_dev(dev);
1481         if (rc < 0)
1482                 return rc;
1483
1484         return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1485 }
1486
1487 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1488 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1489 {
1490         struct em28xx_fh  *fh = priv;
1491
1492         return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1493 }
1494 #endif
1495
1496
1497 /* ----------------------------------------------------------- */
1498 /* RADIO ESPECIFIC IOCTLS                                      */
1499 /* ----------------------------------------------------------- */
1500
1501 static int radio_querycap(struct file *file, void  *priv,
1502                           struct v4l2_capability *cap)
1503 {
1504         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1505
1506         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1507         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1508         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1509
1510         cap->version = EM28XX_VERSION_CODE;
1511         cap->capabilities = V4L2_CAP_TUNER;
1512         return 0;
1513 }
1514
1515 static int radio_g_tuner(struct file *file, void *priv,
1516                          struct v4l2_tuner *t)
1517 {
1518         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1519
1520         if (unlikely(t->index > 0))
1521                 return -EINVAL;
1522
1523         strcpy(t->name, "Radio");
1524         t->type = V4L2_TUNER_RADIO;
1525
1526         mutex_lock(&dev->lock);
1527         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1528         mutex_unlock(&dev->lock);
1529
1530         return 0;
1531 }
1532
1533 static int radio_enum_input(struct file *file, void *priv,
1534                             struct v4l2_input *i)
1535 {
1536         if (i->index != 0)
1537                 return -EINVAL;
1538         strcpy(i->name, "Radio");
1539         i->type = V4L2_INPUT_TYPE_TUNER;
1540
1541         return 0;
1542 }
1543
1544 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1545 {
1546         if (unlikely(a->index))
1547                 return -EINVAL;
1548
1549         strcpy(a->name, "Radio");
1550         return 0;
1551 }
1552
1553 static int radio_s_tuner(struct file *file, void *priv,
1554                          struct v4l2_tuner *t)
1555 {
1556         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1557
1558         if (0 != t->index)
1559                 return -EINVAL;
1560
1561         mutex_lock(&dev->lock);
1562         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1563         mutex_unlock(&dev->lock);
1564
1565         return 0;
1566 }
1567
1568 static int radio_s_audio(struct file *file, void *fh,
1569                          struct v4l2_audio *a)
1570 {
1571         return 0;
1572 }
1573
1574 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1575 {
1576         return 0;
1577 }
1578
1579 static int radio_queryctrl(struct file *file, void *priv,
1580                            struct v4l2_queryctrl *qc)
1581 {
1582         int i;
1583
1584         if (qc->id <  V4L2_CID_BASE ||
1585                 qc->id >= V4L2_CID_LASTP1)
1586                 return -EINVAL;
1587
1588         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1589                 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1590                         memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1591                         return 0;
1592                 }
1593         }
1594
1595         return -EINVAL;
1596 }
1597
1598 /*
1599  * em28xx_v4l2_open()
1600  * inits the device and starts isoc transfer
1601  */
1602 static int em28xx_v4l2_open(struct file *filp)
1603 {
1604         int minor = video_devdata(filp)->minor;
1605         int errCode = 0, radio;
1606         struct em28xx *dev;
1607         enum v4l2_buf_type fh_type;
1608         struct em28xx_fh *fh;
1609
1610         dev = em28xx_get_device(minor, &fh_type, &radio);
1611
1612         if (NULL == dev)
1613                 return -ENODEV;
1614
1615         mutex_lock(&dev->lock);
1616
1617         em28xx_videodbg("open minor=%d type=%s users=%d\n",
1618                                 minor, v4l2_type_names[fh_type], dev->users);
1619
1620
1621         fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1622         if (!fh) {
1623                 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1624                 mutex_unlock(&dev->lock);
1625                 return -ENOMEM;
1626         }
1627         fh->dev = dev;
1628         fh->radio = radio;
1629         fh->type = fh_type;
1630         filp->private_data = fh;
1631
1632         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1633                 dev->width = norm_maxw(dev);
1634                 dev->height = norm_maxh(dev);
1635                 dev->hscale = 0;
1636                 dev->vscale = 0;
1637
1638                 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1639                 em28xx_set_alternate(dev);
1640                 em28xx_resolution_set(dev);
1641
1642                 /* Needed, since GPIO might have disabled power of
1643                    some i2c device
1644                  */
1645                 em28xx_wake_i2c(dev);
1646
1647         }
1648         if (fh->radio) {
1649                 em28xx_videodbg("video_open: setting radio device\n");
1650                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1651         }
1652
1653         dev->users++;
1654
1655         videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1656                         NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1657                         sizeof(struct em28xx_buffer), fh);
1658
1659         mutex_unlock(&dev->lock);
1660
1661         return errCode;
1662 }
1663
1664 /*
1665  * em28xx_realease_resources()
1666  * unregisters the v4l2,i2c and usb devices
1667  * called when the device gets disconected or at module unload
1668 */
1669 void em28xx_release_analog_resources(struct em28xx *dev)
1670 {
1671
1672         /*FIXME: I2C IR should be disconnected */
1673
1674         if (dev->radio_dev) {
1675                 if (-1 != dev->radio_dev->minor)
1676                         video_unregister_device(dev->radio_dev);
1677                 else
1678                         video_device_release(dev->radio_dev);
1679                 dev->radio_dev = NULL;
1680         }
1681         if (dev->vbi_dev) {
1682                 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1683                             dev->vbi_dev->num);
1684                 if (-1 != dev->vbi_dev->minor)
1685                         video_unregister_device(dev->vbi_dev);
1686                 else
1687                         video_device_release(dev->vbi_dev);
1688                 dev->vbi_dev = NULL;
1689         }
1690         if (dev->vdev) {
1691                 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1692                             dev->vdev->num);
1693                 if (-1 != dev->vdev->minor)
1694                         video_unregister_device(dev->vdev);
1695                 else
1696                         video_device_release(dev->vdev);
1697                 dev->vdev = NULL;
1698         }
1699 }
1700
1701 /*
1702  * em28xx_v4l2_close()
1703  * stops streaming and deallocates all resources allocated by the v4l2
1704  * calls and ioctls
1705  */
1706 static int em28xx_v4l2_close(struct file *filp)
1707 {
1708         struct em28xx_fh *fh  = filp->private_data;
1709         struct em28xx    *dev = fh->dev;
1710         int              errCode;
1711
1712         em28xx_videodbg("users=%d\n", dev->users);
1713
1714
1715         mutex_lock(&dev->lock);
1716         if (res_check(fh))
1717                 res_free(fh);
1718
1719         if (dev->users == 1) {
1720                 videobuf_stop(&fh->vb_vidq);
1721                 videobuf_mmap_free(&fh->vb_vidq);
1722
1723                 /* the device is already disconnect,
1724                    free the remaining resources */
1725                 if (dev->state & DEV_DISCONNECTED) {
1726                         em28xx_release_resources(dev);
1727                         mutex_unlock(&dev->lock);
1728                         kfree(dev);
1729                         return 0;
1730                 }
1731
1732                 /* Save some power by putting tuner to sleep */
1733                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
1734
1735                 /* do this before setting alternate! */
1736                 em28xx_uninit_isoc(dev);
1737                 em28xx_set_mode(dev, EM28XX_SUSPEND);
1738
1739                 /* set alternate 0 */
1740                 dev->alt = 0;
1741                 em28xx_videodbg("setting alternate 0\n");
1742                 errCode = usb_set_interface(dev->udev, 0, 0);
1743                 if (errCode < 0) {
1744                         em28xx_errdev("cannot change alternate number to "
1745                                         "0 (error=%i)\n", errCode);
1746                 }
1747         }
1748         kfree(fh);
1749         dev->users--;
1750         wake_up_interruptible_nr(&dev->open, 1);
1751         mutex_unlock(&dev->lock);
1752         return 0;
1753 }
1754
1755 /*
1756  * em28xx_v4l2_read()
1757  * will allocate buffers when called for the first time
1758  */
1759 static ssize_t
1760 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1761                  loff_t *pos)
1762 {
1763         struct em28xx_fh *fh = filp->private_data;
1764         struct em28xx *dev = fh->dev;
1765         int rc;
1766
1767         rc = check_dev(dev);
1768         if (rc < 0)
1769                 return rc;
1770
1771         /* FIXME: read() is not prepared to allow changing the video
1772            resolution while streaming. Seems a bug at em28xx_set_fmt
1773          */
1774
1775         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1776                 mutex_lock(&dev->lock);
1777                 rc = res_get(fh);
1778                 mutex_unlock(&dev->lock);
1779
1780                 if (unlikely(rc < 0))
1781                         return rc;
1782
1783                 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1784                                         filp->f_flags & O_NONBLOCK);
1785         }
1786         return 0;
1787 }
1788
1789 /*
1790  * em28xx_v4l2_poll()
1791  * will allocate buffers when called for the first time
1792  */
1793 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1794 {
1795         struct em28xx_fh *fh = filp->private_data;
1796         struct em28xx *dev = fh->dev;
1797         int rc;
1798
1799         rc = check_dev(dev);
1800         if (rc < 0)
1801                 return rc;
1802
1803         mutex_lock(&dev->lock);
1804         rc = res_get(fh);
1805         mutex_unlock(&dev->lock);
1806
1807         if (unlikely(rc < 0))
1808                 return POLLERR;
1809
1810         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1811                 return POLLERR;
1812
1813         return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1814 }
1815
1816 /*
1817  * em28xx_v4l2_mmap()
1818  */
1819 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1820 {
1821         struct em28xx_fh *fh    = filp->private_data;
1822         struct em28xx    *dev   = fh->dev;
1823         int              rc;
1824
1825         rc = check_dev(dev);
1826         if (rc < 0)
1827                 return rc;
1828
1829         mutex_lock(&dev->lock);
1830         rc = res_get(fh);
1831         mutex_unlock(&dev->lock);
1832
1833         if (unlikely(rc < 0))
1834                 return rc;
1835
1836         rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1837
1838         em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1839                 (unsigned long)vma->vm_start,
1840                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1841                 rc);
1842
1843         return rc;
1844 }
1845
1846 static const struct v4l2_file_operations em28xx_v4l_fops = {
1847         .owner         = THIS_MODULE,
1848         .open          = em28xx_v4l2_open,
1849         .release       = em28xx_v4l2_close,
1850         .read          = em28xx_v4l2_read,
1851         .poll          = em28xx_v4l2_poll,
1852         .mmap          = em28xx_v4l2_mmap,
1853         .ioctl         = video_ioctl2,
1854 };
1855
1856 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1857         .vidioc_querycap            = vidioc_querycap,
1858         .vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1859         .vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1860         .vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1861         .vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1862         .vidioc_g_audio             = vidioc_g_audio,
1863         .vidioc_s_audio             = vidioc_s_audio,
1864         .vidioc_cropcap             = vidioc_cropcap,
1865
1866         .vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
1867         .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1868         .vidioc_s_fmt_sliced_vbi_cap   = vidioc_try_set_sliced_vbi_cap,
1869
1870         .vidioc_reqbufs             = vidioc_reqbufs,
1871         .vidioc_querybuf            = vidioc_querybuf,
1872         .vidioc_qbuf                = vidioc_qbuf,
1873         .vidioc_dqbuf               = vidioc_dqbuf,
1874         .vidioc_s_std               = vidioc_s_std,
1875         .vidioc_enum_input          = vidioc_enum_input,
1876         .vidioc_g_input             = vidioc_g_input,
1877         .vidioc_s_input             = vidioc_s_input,
1878         .vidioc_queryctrl           = vidioc_queryctrl,
1879         .vidioc_g_ctrl              = vidioc_g_ctrl,
1880         .vidioc_s_ctrl              = vidioc_s_ctrl,
1881         .vidioc_streamon            = vidioc_streamon,
1882         .vidioc_streamoff           = vidioc_streamoff,
1883         .vidioc_g_tuner             = vidioc_g_tuner,
1884         .vidioc_s_tuner             = vidioc_s_tuner,
1885         .vidioc_g_frequency         = vidioc_g_frequency,
1886         .vidioc_s_frequency         = vidioc_s_frequency,
1887 #ifdef CONFIG_VIDEO_ADV_DEBUG
1888         .vidioc_g_register          = vidioc_g_register,
1889         .vidioc_s_register          = vidioc_s_register,
1890         .vidioc_g_chip_ident        = vidioc_g_chip_ident,
1891 #endif
1892 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1893         .vidiocgmbuf                = vidiocgmbuf,
1894 #endif
1895 };
1896
1897 static const struct video_device em28xx_video_template = {
1898         .fops                       = &em28xx_v4l_fops,
1899         .release                    = video_device_release,
1900         .ioctl_ops                  = &video_ioctl_ops,
1901
1902         .minor                      = -1,
1903
1904         .tvnorms                    = V4L2_STD_ALL,
1905         .current_norm               = V4L2_STD_PAL,
1906 };
1907
1908 static const struct v4l2_file_operations radio_fops = {
1909         .owner         = THIS_MODULE,
1910         .open          = em28xx_v4l2_open,
1911         .release       = em28xx_v4l2_close,
1912         .ioctl         = video_ioctl2,
1913 };
1914
1915 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1916         .vidioc_querycap      = radio_querycap,
1917         .vidioc_g_tuner       = radio_g_tuner,
1918         .vidioc_enum_input    = radio_enum_input,
1919         .vidioc_g_audio       = radio_g_audio,
1920         .vidioc_s_tuner       = radio_s_tuner,
1921         .vidioc_s_audio       = radio_s_audio,
1922         .vidioc_s_input       = radio_s_input,
1923         .vidioc_queryctrl     = radio_queryctrl,
1924         .vidioc_g_ctrl        = vidioc_g_ctrl,
1925         .vidioc_s_ctrl        = vidioc_s_ctrl,
1926         .vidioc_g_frequency   = vidioc_g_frequency,
1927         .vidioc_s_frequency   = vidioc_s_frequency,
1928 #ifdef CONFIG_VIDEO_ADV_DEBUG
1929         .vidioc_g_register    = vidioc_g_register,
1930         .vidioc_s_register    = vidioc_s_register,
1931 #endif
1932 };
1933
1934 static struct video_device em28xx_radio_template = {
1935         .name                 = "em28xx-radio",
1936         .fops                 = &radio_fops,
1937         .ioctl_ops            = &radio_ioctl_ops,
1938         .minor                = -1,
1939 };
1940
1941 /******************************** usb interface ******************************/
1942
1943
1944
1945 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1946                                         const struct video_device *template,
1947                                         const char *type_name)
1948 {
1949         struct video_device *vfd;
1950
1951         vfd = video_device_alloc();
1952         if (NULL == vfd)
1953                 return NULL;
1954
1955         *vfd            = *template;
1956         vfd->minor      = -1;
1957         vfd->v4l2_dev   = &dev->v4l2_dev;
1958         vfd->release    = video_device_release;
1959         vfd->debug      = video_debug;
1960
1961         snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1962                  dev->name, type_name);
1963
1964         return vfd;
1965 }
1966
1967 int em28xx_register_analog_devices(struct em28xx *dev)
1968 {
1969       u8 val;
1970         int ret;
1971
1972         printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1973                 dev->name,
1974                 (EM28XX_VERSION_CODE >> 16) & 0xff,
1975                 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1976
1977         /* set default norm */
1978         dev->norm = em28xx_video_template.current_norm;
1979         dev->width = norm_maxw(dev);
1980         dev->height = norm_maxh(dev);
1981         dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1982         dev->hscale = 0;
1983         dev->vscale = 0;
1984         dev->ctl_input = 0;
1985
1986         /* Analog specific initialization */
1987         dev->format = &format[0];
1988         video_mux(dev, dev->ctl_input);
1989
1990         /* Audio defaults */
1991         dev->mute = 1;
1992         dev->volume = 0x1f;
1993
1994         /* enable vbi capturing */
1995
1996 /*      em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
1997         val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1998         em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1999                          (EM28XX_XCLK_AUDIO_UNMUTE | val));
2000         em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2001
2002         em28xx_set_outfmt(dev);
2003         em28xx_colorlevels_set_default(dev);
2004         em28xx_compression_disable(dev);
2005
2006         /* allocate and fill video video_device struct */
2007         dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2008         if (!dev->vdev) {
2009                 em28xx_errdev("cannot allocate video_device.\n");
2010                 return -ENODEV;
2011         }
2012
2013         /* register v4l2 video video_device */
2014         ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2015                                        video_nr[dev->devno]);
2016         if (ret) {
2017                 em28xx_errdev("unable to register video device (error=%i).\n",
2018                               ret);
2019                 return ret;
2020         }
2021
2022         /* Allocate and fill vbi video_device struct */
2023         dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2024
2025         /* register v4l2 vbi video_device */
2026         ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2027                                         vbi_nr[dev->devno]);
2028         if (ret < 0) {
2029                 em28xx_errdev("unable to register vbi device\n");
2030                 return ret;
2031         }
2032
2033         if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2034                 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2035                                                   "radio");
2036                 if (!dev->radio_dev) {
2037                         em28xx_errdev("cannot allocate video_device.\n");
2038                         return -ENODEV;
2039                 }
2040                 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2041                                             radio_nr[dev->devno]);
2042                 if (ret < 0) {
2043                         em28xx_errdev("can't register radio device\n");
2044                         return ret;
2045                 }
2046                 em28xx_info("Registered radio device as /dev/radio%d\n",
2047                             dev->radio_dev->num);
2048         }
2049
2050         em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2051                                 dev->vdev->num, dev->vbi_dev->num);
2052
2053         return 0;
2054 }