[media] em28xx: rename function em28xx_isoc_copy_vbi and extend for USB bulk transfers
[cascardo/linux.git] / drivers / media / usb / 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    Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
10
11         Some parts based on SN9C10x PC Camera Controllers GPL driver made
12                 by Luca Risolia <luca.risolia@studio.unibo.it>
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/bitmap.h>
34 #include <linux/usb.h>
35 #include <linux/i2c.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
39
40 #include "em28xx.h"
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43 #include <media/v4l2-chip-ident.h>
44 #include <media/msp3400.h>
45 #include <media/tuner.h>
46
47 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48                       "Markus Rechberger <mrechberger@gmail.com>, " \
49                       "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
50                       "Sascha Sommer <saschasommer@freenet.de>"
51
52 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
53
54 #define EM28XX_VERSION "0.1.3"
55
56 #define em28xx_videodbg(fmt, arg...) do {\
57         if (video_debug) \
58                 printk(KERN_INFO "%s %s :"fmt, \
59                          dev->name, __func__ , ##arg); } while (0)
60
61 static unsigned int isoc_debug;
62 module_param(isoc_debug, int, 0644);
63 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
64
65 #define em28xx_isocdbg(fmt, arg...) \
66 do {\
67         if (isoc_debug) { \
68                 printk(KERN_INFO "%s %s :"fmt, \
69                          dev->name, __func__ , ##arg); \
70         } \
71   } while (0)
72
73 MODULE_AUTHOR(DRIVER_AUTHOR);
74 MODULE_DESCRIPTION(DRIVER_DESC);
75 MODULE_LICENSE("GPL");
76 MODULE_VERSION(EM28XX_VERSION);
77
78 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int vbi_nr[]   = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
82 module_param_array(video_nr, int, NULL, 0444);
83 module_param_array(vbi_nr, int, NULL, 0444);
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(video_nr, "video device numbers");
86 MODULE_PARM_DESC(vbi_nr,   "vbi device numbers");
87 MODULE_PARM_DESC(radio_nr, "radio device numbers");
88
89 static unsigned int video_debug;
90 module_param(video_debug, int, 0644);
91 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
92
93 /* supported video standards */
94 static struct em28xx_fmt format[] = {
95         {
96                 .name     = "16 bpp YUY2, 4:2:2, packed",
97                 .fourcc   = V4L2_PIX_FMT_YUYV,
98                 .depth    = 16,
99                 .reg      = EM28XX_OUTFMT_YUV422_Y0UY1V,
100         }, {
101                 .name     = "16 bpp RGB 565, LE",
102                 .fourcc   = V4L2_PIX_FMT_RGB565,
103                 .depth    = 16,
104                 .reg      = EM28XX_OUTFMT_RGB_16_656,
105         }, {
106                 .name     = "8 bpp Bayer BGBG..GRGR",
107                 .fourcc   = V4L2_PIX_FMT_SBGGR8,
108                 .depth    = 8,
109                 .reg      = EM28XX_OUTFMT_RGB_8_BGBG,
110         }, {
111                 .name     = "8 bpp Bayer GRGR..BGBG",
112                 .fourcc   = V4L2_PIX_FMT_SGRBG8,
113                 .depth    = 8,
114                 .reg      = EM28XX_OUTFMT_RGB_8_GRGR,
115         }, {
116                 .name     = "8 bpp Bayer GBGB..RGRG",
117                 .fourcc   = V4L2_PIX_FMT_SGBRG8,
118                 .depth    = 8,
119                 .reg      = EM28XX_OUTFMT_RGB_8_GBGB,
120         }, {
121                 .name     = "12 bpp YUV411",
122                 .fourcc   = V4L2_PIX_FMT_YUV411P,
123                 .depth    = 12,
124                 .reg      = EM28XX_OUTFMT_YUV411,
125         },
126 };
127
128 /* supported controls */
129 /* Common to all boards */
130 static struct v4l2_queryctrl ac97_qctrl[] = {
131         {
132                 .id = V4L2_CID_AUDIO_VOLUME,
133                 .type = V4L2_CTRL_TYPE_INTEGER,
134                 .name = "Volume",
135                 .minimum = 0x0,
136                 .maximum = 0x1f,
137                 .step = 0x1,
138                 .default_value = 0x1f,
139                 .flags = V4L2_CTRL_FLAG_SLIDER,
140         }, {
141                 .id = V4L2_CID_AUDIO_MUTE,
142                 .type = V4L2_CTRL_TYPE_BOOLEAN,
143                 .name = "Mute",
144                 .minimum = 0,
145                 .maximum = 1,
146                 .step = 1,
147                 .default_value = 1,
148                 .flags = 0,
149         }
150 };
151
152 /* ------------------------------------------------------------------
153         DMA and thread functions
154    ------------------------------------------------------------------*/
155
156 /*
157  * Announces that a buffer were filled and request the next
158  */
159 static inline void buffer_filled(struct em28xx *dev,
160                                   struct em28xx_dmaqueue *dma_q,
161                                   struct em28xx_buffer *buf)
162 {
163         /* Advice that buffer was filled */
164         em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165         buf->vb.state = VIDEOBUF_DONE;
166         buf->vb.field_count++;
167         v4l2_get_timestamp(&buf->vb.ts);
168
169         dev->usb_ctl.vid_buf = NULL;
170
171         list_del(&buf->vb.queue);
172         wake_up(&buf->vb.done);
173 }
174
175 static inline void vbi_buffer_filled(struct em28xx *dev,
176                                      struct em28xx_dmaqueue *dma_q,
177                                      struct em28xx_buffer *buf)
178 {
179         /* Advice that buffer was filled */
180         em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182         buf->vb.state = VIDEOBUF_DONE;
183         buf->vb.field_count++;
184         v4l2_get_timestamp(&buf->vb.ts);
185
186         dev->usb_ctl.vbi_buf = NULL;
187
188         list_del(&buf->vb.queue);
189         wake_up(&buf->vb.done);
190 }
191
192 /*
193  * Identify the buffer header type and properly handles
194  */
195 static void em28xx_copy_video(struct em28xx *dev,
196                               struct em28xx_dmaqueue  *dma_q,
197                               struct em28xx_buffer *buf,
198                               unsigned char *p,
199                               unsigned char *outp, unsigned long len)
200 {
201         void *fieldstart, *startwrite, *startread;
202         int  linesdone, currlinedone, offset, lencopy, remain;
203         int bytesperline = dev->width << 1;
204
205         if (dma_q->pos + len > buf->vb.size)
206                 len = buf->vb.size - dma_q->pos;
207
208         startread = p;
209         remain = len;
210
211         if (dev->progressive || buf->top_field)
212                 fieldstart = outp;
213         else /* interlaced mode, even nr. of lines */
214                 fieldstart = outp + bytesperline;
215
216         linesdone = dma_q->pos / bytesperline;
217         currlinedone = dma_q->pos % bytesperline;
218
219         if (dev->progressive)
220                 offset = linesdone * bytesperline + currlinedone;
221         else
222                 offset = linesdone * bytesperline * 2 + currlinedone;
223
224         startwrite = fieldstart + offset;
225         lencopy = bytesperline - currlinedone;
226         lencopy = lencopy > remain ? remain : lencopy;
227
228         if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
229                 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
230                                ((char *)startwrite + lencopy) -
231                                ((char *)outp + buf->vb.size));
232                 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233                 lencopy = remain;
234         }
235         if (lencopy <= 0)
236                 return;
237         memcpy(startwrite, startread, lencopy);
238
239         remain -= lencopy;
240
241         while (remain > 0) {
242                 if (dev->progressive)
243                         startwrite += lencopy;
244                 else
245                         startwrite += lencopy + bytesperline;
246                 startread += lencopy;
247                 if (bytesperline > remain)
248                         lencopy = remain;
249                 else
250                         lencopy = bytesperline;
251
252                 if ((char *)startwrite + lencopy > (char *)outp +
253                     buf->vb.size) {
254                         em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255                                        "(2)\n",
256                                        ((char *)startwrite + lencopy) -
257                                        ((char *)outp + buf->vb.size));
258                         lencopy = remain = (char *)outp + buf->vb.size -
259                                            (char *)startwrite;
260                 }
261                 if (lencopy <= 0)
262                         break;
263
264                 memcpy(startwrite, startread, lencopy);
265
266                 remain -= lencopy;
267         }
268
269         dma_q->pos += len;
270 }
271
272 static void em28xx_copy_vbi(struct em28xx *dev,
273                               struct em28xx_dmaqueue  *dma_q,
274                               struct em28xx_buffer *buf,
275                               unsigned char *p,
276                               unsigned char *outp, unsigned long len)
277 {
278         void *startwrite, *startread;
279         int  offset;
280         int bytesperline;
281
282         if (dev == NULL) {
283                 em28xx_isocdbg("dev is null\n");
284                 return;
285         }
286         bytesperline = dev->vbi_width;
287
288         if (dma_q == NULL) {
289                 em28xx_isocdbg("dma_q is null\n");
290                 return;
291         }
292         if (buf == NULL) {
293                 return;
294         }
295         if (p == NULL) {
296                 em28xx_isocdbg("p is null\n");
297                 return;
298         }
299         if (outp == NULL) {
300                 em28xx_isocdbg("outp is null\n");
301                 return;
302         }
303
304         if (dma_q->pos + len > buf->vb.size)
305                 len = buf->vb.size - dma_q->pos;
306
307         startread = p;
308
309         startwrite = outp + dma_q->pos;
310         offset = dma_q->pos;
311
312         /* Make sure the bottom field populates the second half of the frame */
313         if (buf->top_field == 0) {
314                 startwrite += bytesperline * dev->vbi_height;
315                 offset += bytesperline * dev->vbi_height;
316         }
317
318         memcpy(startwrite, startread, len);
319         dma_q->pos += len;
320 }
321
322 static inline void print_err_status(struct em28xx *dev,
323                                      int packet, int status)
324 {
325         char *errmsg = "Unknown";
326
327         switch (status) {
328         case -ENOENT:
329                 errmsg = "unlinked synchronuously";
330                 break;
331         case -ECONNRESET:
332                 errmsg = "unlinked asynchronuously";
333                 break;
334         case -ENOSR:
335                 errmsg = "Buffer error (overrun)";
336                 break;
337         case -EPIPE:
338                 errmsg = "Stalled (device not responding)";
339                 break;
340         case -EOVERFLOW:
341                 errmsg = "Babble (bad cable?)";
342                 break;
343         case -EPROTO:
344                 errmsg = "Bit-stuff error (bad cable?)";
345                 break;
346         case -EILSEQ:
347                 errmsg = "CRC/Timeout (could be anything)";
348                 break;
349         case -ETIME:
350                 errmsg = "Device does not respond";
351                 break;
352         }
353         if (packet < 0) {
354                 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355         } else {
356                 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357                                packet, status, errmsg);
358         }
359 }
360
361 /*
362  * video-buf generic routine to get the next available buffer
363  */
364 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
365                                           struct em28xx_buffer **buf)
366 {
367         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
368         char *outp;
369
370         if (list_empty(&dma_q->active)) {
371                 em28xx_isocdbg("No active queue to serve\n");
372                 dev->usb_ctl.vid_buf = NULL;
373                 *buf = NULL;
374                 return;
375         }
376
377         /* Get the next buffer */
378         *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
379
380         /* Cleans up buffer - Useful for testing for frame/URB loss */
381         outp = videobuf_to_vmalloc(&(*buf)->vb);
382         memset(outp, 0, (*buf)->vb.size);
383
384         dev->usb_ctl.vid_buf = *buf;
385
386         return;
387 }
388
389 /*
390  * video-buf generic routine to get the next available VBI buffer
391  */
392 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393                                     struct em28xx_buffer **buf)
394 {
395         struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396         char *outp;
397
398         if (list_empty(&dma_q->active)) {
399                 em28xx_isocdbg("No active queue to serve\n");
400                 dev->usb_ctl.vbi_buf = NULL;
401                 *buf = NULL;
402                 return;
403         }
404
405         /* Get the next buffer */
406         *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
407         /* Cleans up buffer - Useful for testing for frame/URB loss */
408         outp = videobuf_to_vmalloc(&(*buf)->vb);
409         memset(outp, 0x00, (*buf)->vb.size);
410
411         dev->usb_ctl.vbi_buf = *buf;
412
413         return;
414 }
415
416 /* Processes and copies the URB data content to a frame buffer queue */
417 static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
418 {
419         struct em28xx_buffer    *buf;
420         struct em28xx_dmaqueue  *dma_q = &dev->vidq;
421         int xfer_bulk, num_packets, i, rc = 1;
422         unsigned int actual_length, len = 0;
423         unsigned char *p, *outp = NULL;
424
425         if (!dev)
426                 return 0;
427
428         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
429                 return 0;
430
431         if (urb->status < 0)
432                 print_err_status(dev, -1, urb->status);
433
434         xfer_bulk = usb_pipebulk(urb->pipe);
435
436         buf = dev->usb_ctl.vid_buf;
437         if (buf != NULL)
438                 outp = videobuf_to_vmalloc(&buf->vb);
439
440         if (xfer_bulk) /* bulk */
441                 num_packets = 1;
442         else /* isoc */
443                 num_packets = urb->number_of_packets;
444
445         for (i = 0; i < num_packets; i++) {
446                 if (xfer_bulk) { /* bulk */
447                         actual_length = urb->actual_length;
448
449                         p = urb->transfer_buffer;
450                 } else { /* isoc */
451                         if (urb->iso_frame_desc[i].status < 0) {
452                                 print_err_status(dev, i,
453                                                  urb->iso_frame_desc[i].status);
454                                 if (urb->iso_frame_desc[i].status != -EPROTO)
455                                         continue;
456                         }
457
458                         actual_length = urb->iso_frame_desc[i].actual_length;
459                         if (actual_length > dev->max_pkt_size) {
460                                 em28xx_isocdbg("packet bigger than packet size");
461                                 continue;
462                         }
463
464                         p = urb->transfer_buffer +
465                             urb->iso_frame_desc[i].offset;
466                 }
467
468                 if (actual_length <= 0) {
469                         /* NOTE: happens very often with isoc transfers */
470                         /* em28xx_usbdbg("packet %d is empty",i); - spammy */
471                         continue;
472                 }
473
474                 /* FIXME: incomplete buffer checks where removed to make
475                    logic simpler. Impacts of those changes should be evaluated
476                  */
477                 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478                         em28xx_isocdbg("VBI HEADER!!!\n");
479                         /* FIXME: Should add vbi copy */
480                         continue;
481                 }
482                 if (p[0] == 0x22 && p[1] == 0x5a) {
483                         em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
484                                        len, (p[2] & 1) ? "odd" : "even");
485
486                         if (dev->progressive || !(p[2] & 1)) {
487                                 if (buf != NULL)
488                                         buffer_filled(dev, dma_q, buf);
489                                 get_next_buf(dma_q, &buf);
490                                 if (buf == NULL)
491                                         outp = NULL;
492                                 else
493                                         outp = videobuf_to_vmalloc(&buf->vb);
494                         }
495
496                         if (buf != NULL) {
497                                 if (p[2] & 1)
498                                         buf->top_field = 0;
499                                 else
500                                         buf->top_field = 1;
501                         }
502
503                         dma_q->pos = 0;
504                 }
505                 if (buf != NULL) {
506                         if (p[0] != 0x88 && p[0] != 0x22) {
507                                 /* NOTE: no intermediate data packet header
508                                  * 88 88 88 88 when using bulk transfers */
509                                 em28xx_isocdbg("frame is not complete\n");
510                                 len = actual_length;
511                         } else {
512                                 len = actual_length - 4;
513                                 p += 4;
514                         }
515                         em28xx_copy_video(dev, dma_q, buf, p, outp, len);
516                 }
517         }
518         return rc;
519 }
520
521 /* Version of the urb data handler that takes into account a mixture of
522    video and VBI data */
523 static inline int em28xx_urb_data_copy_vbi(struct em28xx *dev, struct urb *urb)
524 {
525         struct em28xx_buffer    *buf, *vbi_buf;
526         struct em28xx_dmaqueue  *dma_q = &dev->vidq;
527         struct em28xx_dmaqueue  *vbi_dma_q = &dev->vbiq;
528         int xfer_bulk, vbi_size, num_packets, i, rc = 1;
529         unsigned int actual_length, len = 0;
530         unsigned char *p, *outp = NULL, *vbioutp = NULL;
531
532         if (!dev)
533                 return 0;
534
535         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
536                 return 0;
537
538         if (urb->status < 0)
539                 print_err_status(dev, -1, urb->status);
540
541         xfer_bulk = usb_pipebulk(urb->pipe);
542
543         buf = dev->usb_ctl.vid_buf;
544         if (buf != NULL)
545                 outp = videobuf_to_vmalloc(&buf->vb);
546
547         vbi_buf = dev->usb_ctl.vbi_buf;
548         if (vbi_buf != NULL)
549                 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
550
551         if (xfer_bulk) /* bulk */
552                 num_packets = 1;
553         else /* isoc */
554                 num_packets = urb->number_of_packets;
555
556         for (i = 0; i < num_packets; i++) {
557                 if (xfer_bulk) { /* bulk */
558                         actual_length = urb->actual_length;
559
560                         p = urb->transfer_buffer;
561                 } else { /* isoc */
562                         if (urb->iso_frame_desc[i].status < 0) {
563                                 print_err_status(dev, i,
564                                                  urb->iso_frame_desc[i].status);
565                                 if (urb->iso_frame_desc[i].status != -EPROTO)
566                                         continue;
567                         }
568
569                         actual_length = urb->iso_frame_desc[i].actual_length;
570                         if (actual_length > dev->max_pkt_size) {
571                                 em28xx_isocdbg("packet bigger than packet size");
572                                 continue;
573                         }
574
575                         p = urb->transfer_buffer +
576                             urb->iso_frame_desc[i].offset;
577                 }
578
579                 if (actual_length <= 0) {
580                         /* NOTE: happens very often with isoc transfers */
581                         /* em28xx_usbdbg("packet %d is empty",i); - spammy */
582                         continue;
583                 }
584
585                 /* capture type 0 = vbi start
586                    capture type 1 = video start
587                    capture type 2 = video in progress */
588                 if (p[0] == 0x33 && p[1] == 0x95) {
589                         dev->capture_type = 0;
590                         dev->vbi_read = 0;
591                         em28xx_isocdbg("VBI START HEADER!!!\n");
592                         dev->cur_field = p[2];
593                         p += 4;
594                         len = actual_length - 4;
595                 } else if (p[0] == 0x88 && p[1] == 0x88 &&
596                            p[2] == 0x88 && p[3] == 0x88) {
597                         /* continuation */
598                         p += 4;
599                         len = actual_length - 4;
600                 } else if (p[0] == 0x22 && p[1] == 0x5a) {
601                         /* start video */
602                         p += 4;
603                         len = actual_length - 4;
604                 } else {
605                         /* NOTE: With bulk transfers, intermediate data packets
606                          * have no continuation header */
607                         len = actual_length;
608                 }
609
610                 vbi_size = dev->vbi_width * dev->vbi_height;
611
612                 if (dev->capture_type == 0) {
613                         if (dev->vbi_read >= vbi_size) {
614                                 /* We've already read all the VBI data, so
615                                    treat the rest as video */
616                                 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
617                         } else if ((dev->vbi_read + len) < vbi_size) {
618                                 /* This entire frame is VBI data */
619                                 if (dev->vbi_read == 0 &&
620                                     (!(dev->cur_field & 1))) {
621                                         /* Brand new frame */
622                                         if (vbi_buf != NULL)
623                                                 vbi_buffer_filled(dev,
624                                                                   vbi_dma_q,
625                                                                   vbi_buf);
626                                         vbi_get_next_buf(vbi_dma_q, &vbi_buf);
627                                         if (vbi_buf == NULL)
628                                                 vbioutp = NULL;
629                                         else
630                                                 vbioutp = videobuf_to_vmalloc(
631                                                         &vbi_buf->vb);
632                                 }
633
634                                 if (dev->vbi_read == 0) {
635                                         vbi_dma_q->pos = 0;
636                                         if (vbi_buf != NULL) {
637                                                 if (dev->cur_field & 1)
638                                                         vbi_buf->top_field = 0;
639                                                 else
640                                                         vbi_buf->top_field = 1;
641                                         }
642                                 }
643
644                                 dev->vbi_read += len;
645                                 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646                                                 vbioutp, len);
647                         } else {
648                                 /* Some of this frame is VBI data and some is
649                                    video data */
650                                 int vbi_data_len = vbi_size - dev->vbi_read;
651                                 dev->vbi_read += vbi_data_len;
652                                 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
653                                                 vbioutp, vbi_data_len);
654                                 dev->capture_type = 1;
655                                 p += vbi_data_len;
656                                 len -= vbi_data_len;
657                         }
658                 }
659
660                 if (dev->capture_type == 1) {
661                         dev->capture_type = 2;
662                         if (dev->progressive || !(dev->cur_field & 1)) {
663                                 if (buf != NULL)
664                                         buffer_filled(dev, dma_q, buf);
665                                 get_next_buf(dma_q, &buf);
666                                 if (buf == NULL)
667                                         outp = NULL;
668                                 else
669                                         outp = videobuf_to_vmalloc(&buf->vb);
670                         }
671                         if (buf != NULL) {
672                                 if (dev->cur_field & 1)
673                                         buf->top_field = 0;
674                                 else
675                                         buf->top_field = 1;
676                         }
677
678                         dma_q->pos = 0;
679                 }
680
681                 if (buf != NULL && dev->capture_type == 2) {
682                         if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
683                             p[2] == 0x88 && p[3] == 0x88) {
684                                 p += 4;
685                                 len -= 4;
686                         }
687                         if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
688                                 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
689                                                p[2], len, (p[2] & 1) ?
690                                                "odd" : "even");
691                                 p += 4;
692                                 len -= 4;
693                         }
694
695                         if (len > 0)
696                                 em28xx_copy_video(dev, dma_q, buf, p, outp,
697                                                   len);
698                 }
699         }
700         return rc;
701 }
702
703
704 /* ------------------------------------------------------------------
705         Videobuf operations
706    ------------------------------------------------------------------*/
707
708 static int
709 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
710 {
711         struct em28xx_fh *fh = vq->priv_data;
712         struct em28xx        *dev = fh->dev;
713         struct v4l2_frequency f;
714
715         *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
716                 >> 3;
717
718         if (0 == *count)
719                 *count = EM28XX_DEF_BUF;
720
721         if (*count < EM28XX_MIN_BUF)
722                 *count = EM28XX_MIN_BUF;
723
724         /* Ask tuner to go to analog or radio mode */
725         memset(&f, 0, sizeof(f));
726         f.frequency = dev->ctl_freq;
727         f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
728
729         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
730
731         return 0;
732 }
733
734 /* This is called *without* dev->slock held; please keep it that way */
735 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
736 {
737         struct em28xx_fh     *fh  = vq->priv_data;
738         struct em28xx        *dev = fh->dev;
739         unsigned long flags = 0;
740         if (in_interrupt())
741                 BUG();
742
743         /* We used to wait for the buffer to finish here, but this didn't work
744            because, as we were keeping the state as VIDEOBUF_QUEUED,
745            videobuf_queue_cancel marked it as finished for us.
746            (Also, it could wedge forever if the hardware was misconfigured.)
747
748            This should be safe; by the time we get here, the buffer isn't
749            queued anymore. If we ever start marking the buffers as
750            VIDEOBUF_ACTIVE, it won't be, though.
751         */
752         spin_lock_irqsave(&dev->slock, flags);
753         if (dev->usb_ctl.vid_buf == buf)
754                 dev->usb_ctl.vid_buf = NULL;
755         spin_unlock_irqrestore(&dev->slock, flags);
756
757         videobuf_vmalloc_free(&buf->vb);
758         buf->vb.state = VIDEOBUF_NEEDS_INIT;
759 }
760
761 static int
762 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
763                                                 enum v4l2_field field)
764 {
765         struct em28xx_fh     *fh  = vq->priv_data;
766         struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
767         struct em28xx        *dev = fh->dev;
768         int                  rc = 0, urb_init = 0;
769
770         buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
771                         + 7) >> 3;
772
773         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
774                 return -EINVAL;
775
776         buf->vb.width  = dev->width;
777         buf->vb.height = dev->height;
778         buf->vb.field  = field;
779
780         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
781                 rc = videobuf_iolock(vq, &buf->vb, NULL);
782                 if (rc < 0)
783                         goto fail;
784         }
785
786         if (!dev->usb_ctl.analog_bufs.num_bufs)
787                 urb_init = 1;
788
789         if (urb_init) {
790                 if (em28xx_vbi_supported(dev) == 1)
791                         rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
792                                                   EM28XX_NUM_BUFS,
793                                                   dev->max_pkt_size,
794                                                   EM28XX_NUM_ISOC_PACKETS,
795                                                   em28xx_urb_data_copy_vbi);
796                 else
797                         rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
798                                                   EM28XX_NUM_BUFS,
799                                                   dev->max_pkt_size,
800                                                   EM28XX_NUM_ISOC_PACKETS,
801                                                   em28xx_urb_data_copy);
802                 if (rc < 0)
803                         goto fail;
804         }
805
806         buf->vb.state = VIDEOBUF_PREPARED;
807         return 0;
808
809 fail:
810         free_buffer(vq, buf);
811         return rc;
812 }
813
814 static void
815 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
816 {
817         struct em28xx_buffer    *buf     = container_of(vb,
818                                                         struct em28xx_buffer,
819                                                         vb);
820         struct em28xx_fh        *fh      = vq->priv_data;
821         struct em28xx           *dev     = fh->dev;
822         struct em28xx_dmaqueue  *vidq    = &dev->vidq;
823
824         buf->vb.state = VIDEOBUF_QUEUED;
825         list_add_tail(&buf->vb.queue, &vidq->active);
826
827 }
828
829 static void buffer_release(struct videobuf_queue *vq,
830                                 struct videobuf_buffer *vb)
831 {
832         struct em28xx_buffer   *buf  = container_of(vb,
833                                                     struct em28xx_buffer,
834                                                     vb);
835         struct em28xx_fh       *fh   = vq->priv_data;
836         struct em28xx          *dev  = (struct em28xx *)fh->dev;
837
838         em28xx_isocdbg("em28xx: called buffer_release\n");
839
840         free_buffer(vq, buf);
841 }
842
843 static struct videobuf_queue_ops em28xx_video_qops = {
844         .buf_setup      = buffer_setup,
845         .buf_prepare    = buffer_prepare,
846         .buf_queue      = buffer_queue,
847         .buf_release    = buffer_release,
848 };
849
850 /*********************  v4l2 interface  **************************************/
851
852 static void video_mux(struct em28xx *dev, int index)
853 {
854         dev->ctl_input = index;
855         dev->ctl_ainput = INPUT(index)->amux;
856         dev->ctl_aoutput = INPUT(index)->aout;
857
858         if (!dev->ctl_aoutput)
859                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
860
861         v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
862                         INPUT(index)->vmux, 0, 0);
863
864         if (dev->board.has_msp34xx) {
865                 if (dev->i2s_speed) {
866                         v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
867                                 s_i2s_clock_freq, dev->i2s_speed);
868                 }
869                 /* Note: this is msp3400 specific */
870                 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
871                          dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
872         }
873
874         if (dev->board.adecoder != EM28XX_NOADECODER) {
875                 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
876                         dev->ctl_ainput, dev->ctl_aoutput, 0);
877         }
878
879         em28xx_audio_analog_set(dev);
880 }
881
882 /* Usage lock check functions */
883 static int res_get(struct em28xx_fh *fh, unsigned int bit)
884 {
885         struct em28xx    *dev = fh->dev;
886
887         if (fh->resources & bit)
888                 /* have it already allocated */
889                 return 1;
890
891         /* is it free? */
892         if (dev->resources & bit) {
893                 /* no, someone else uses it */
894                 return 0;
895         }
896         /* it's free, grab it */
897         fh->resources  |= bit;
898         dev->resources |= bit;
899         em28xx_videodbg("res: get %d\n", bit);
900         return 1;
901 }
902
903 static int res_check(struct em28xx_fh *fh, unsigned int bit)
904 {
905         return fh->resources & bit;
906 }
907
908 static int res_locked(struct em28xx *dev, unsigned int bit)
909 {
910         return dev->resources & bit;
911 }
912
913 static void res_free(struct em28xx_fh *fh, unsigned int bits)
914 {
915         struct em28xx    *dev = fh->dev;
916
917         BUG_ON((fh->resources & bits) != bits);
918
919         fh->resources  &= ~bits;
920         dev->resources &= ~bits;
921         em28xx_videodbg("res: put %d\n", bits);
922 }
923
924 static int get_ressource(struct em28xx_fh *fh)
925 {
926         switch (fh->type) {
927         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
928                 return EM28XX_RESOURCE_VIDEO;
929         case V4L2_BUF_TYPE_VBI_CAPTURE:
930                 return EM28XX_RESOURCE_VBI;
931         default:
932                 BUG();
933                 return 0;
934         }
935 }
936
937 /*
938  * ac97_queryctrl()
939  * return the ac97 supported controls
940  */
941 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
942 {
943         int i;
944
945         for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
946                 if (qc->id && qc->id == ac97_qctrl[i].id) {
947                         memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
948                         return 0;
949                 }
950         }
951
952         /* Control is not ac97 related */
953         return 1;
954 }
955
956 /*
957  * ac97_get_ctrl()
958  * return the current values for ac97 mute and volume
959  */
960 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
961 {
962         switch (ctrl->id) {
963         case V4L2_CID_AUDIO_MUTE:
964                 ctrl->value = dev->mute;
965                 return 0;
966         case V4L2_CID_AUDIO_VOLUME:
967                 ctrl->value = dev->volume;
968                 return 0;
969         default:
970                 /* Control is not ac97 related */
971                 return 1;
972         }
973 }
974
975 /*
976  * ac97_set_ctrl()
977  * set values for ac97 mute and volume
978  */
979 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
980 {
981         int i;
982
983         for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
984                 if (ctrl->id == ac97_qctrl[i].id)
985                         goto handle;
986
987         /* Announce that hasn't handle it */
988         return 1;
989
990 handle:
991         if (ctrl->value < ac97_qctrl[i].minimum ||
992             ctrl->value > ac97_qctrl[i].maximum)
993                 return -ERANGE;
994
995         switch (ctrl->id) {
996         case V4L2_CID_AUDIO_MUTE:
997                 dev->mute = ctrl->value;
998                 break;
999         case V4L2_CID_AUDIO_VOLUME:
1000                 dev->volume = ctrl->value;
1001                 break;
1002         }
1003
1004         return em28xx_audio_analog_set(dev);
1005 }
1006
1007 static int check_dev(struct em28xx *dev)
1008 {
1009         if (dev->state & DEV_DISCONNECTED) {
1010                 em28xx_errdev("v4l2 ioctl: device not present\n");
1011                 return -ENODEV;
1012         }
1013
1014         if (dev->state & DEV_MISCONFIGURED) {
1015                 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1016                               "close and open it again\n");
1017                 return -EIO;
1018         }
1019         return 0;
1020 }
1021
1022 static void get_scale(struct em28xx *dev,
1023                         unsigned int width, unsigned int height,
1024                         unsigned int *hscale, unsigned int *vscale)
1025 {
1026         unsigned int          maxw = norm_maxw(dev);
1027         unsigned int          maxh = norm_maxh(dev);
1028
1029         *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1030         if (*hscale >= 0x4000)
1031                 *hscale = 0x3fff;
1032
1033         *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1034         if (*vscale >= 0x4000)
1035                 *vscale = 0x3fff;
1036 }
1037
1038 /* ------------------------------------------------------------------
1039         IOCTL vidioc handling
1040    ------------------------------------------------------------------*/
1041
1042 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1043                                         struct v4l2_format *f)
1044 {
1045         struct em28xx_fh      *fh  = priv;
1046         struct em28xx         *dev = fh->dev;
1047
1048         f->fmt.pix.width = dev->width;
1049         f->fmt.pix.height = dev->height;
1050         f->fmt.pix.pixelformat = dev->format->fourcc;
1051         f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1052         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline  * dev->height;
1053         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1054
1055         /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1056         if (dev->progressive)
1057                 f->fmt.pix.field = V4L2_FIELD_NONE;
1058         else
1059                 f->fmt.pix.field = dev->interlaced ?
1060                            V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1061         return 0;
1062 }
1063
1064 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1065 {
1066         unsigned int i;
1067
1068         for (i = 0; i < ARRAY_SIZE(format); i++)
1069                 if (format[i].fourcc == fourcc)
1070                         return &format[i];
1071
1072         return NULL;
1073 }
1074
1075 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1076                         struct v4l2_format *f)
1077 {
1078         struct em28xx_fh      *fh    = priv;
1079         struct em28xx         *dev   = fh->dev;
1080         unsigned int          width  = f->fmt.pix.width;
1081         unsigned int          height = f->fmt.pix.height;
1082         unsigned int          maxw   = norm_maxw(dev);
1083         unsigned int          maxh   = norm_maxh(dev);
1084         unsigned int          hscale, vscale;
1085         struct em28xx_fmt     *fmt;
1086
1087         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1088         if (!fmt) {
1089                 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1090                                 f->fmt.pix.pixelformat);
1091                 return -EINVAL;
1092         }
1093
1094         if (dev->board.is_em2800) {
1095                 /* the em2800 can only scale down to 50% */
1096                 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1097                 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1098                 /* MaxPacketSize for em2800 is too small to capture at full resolution
1099                  * use half of maxw as the scaler can only scale to 50% */
1100                 if (width == maxw && height == maxh)
1101                         width /= 2;
1102         } else {
1103                 /* width must even because of the YUYV format
1104                    height must be even because of interlacing */
1105                 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1106                                       1, 0);
1107         }
1108
1109         get_scale(dev, width, height, &hscale, &vscale);
1110
1111         width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1112         height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1113
1114         f->fmt.pix.width = width;
1115         f->fmt.pix.height = height;
1116         f->fmt.pix.pixelformat = fmt->fourcc;
1117         f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1118         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1119         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1120         if (dev->progressive)
1121                 f->fmt.pix.field = V4L2_FIELD_NONE;
1122         else
1123                 f->fmt.pix.field = dev->interlaced ?
1124                            V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1125
1126         return 0;
1127 }
1128
1129 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1130                                    unsigned width, unsigned height)
1131 {
1132         struct em28xx_fmt     *fmt;
1133
1134         fmt = format_by_fourcc(fourcc);
1135         if (!fmt)
1136                 return -EINVAL;
1137
1138         dev->format = fmt;
1139         dev->width  = width;
1140         dev->height = height;
1141
1142         /* set new image size */
1143         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1144
1145         em28xx_set_alternate(dev);
1146         em28xx_resolution_set(dev);
1147
1148         return 0;
1149 }
1150
1151 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1152                         struct v4l2_format *f)
1153 {
1154         struct em28xx_fh      *fh  = priv;
1155         struct em28xx         *dev = fh->dev;
1156         int                   rc;
1157
1158         rc = check_dev(dev);
1159         if (rc < 0)
1160                 return rc;
1161
1162         vidioc_try_fmt_vid_cap(file, priv, f);
1163
1164         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1165                 em28xx_errdev("%s queue busy\n", __func__);
1166                 return -EBUSY;
1167         }
1168
1169         return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1170                                 f->fmt.pix.width, f->fmt.pix.height);
1171 }
1172
1173 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1174 {
1175         struct em28xx_fh   *fh  = priv;
1176         struct em28xx      *dev = fh->dev;
1177         int                rc;
1178
1179         rc = check_dev(dev);
1180         if (rc < 0)
1181                 return rc;
1182
1183         *norm = dev->norm;
1184
1185         return 0;
1186 }
1187
1188 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1189 {
1190         struct em28xx_fh   *fh  = priv;
1191         struct em28xx      *dev = fh->dev;
1192         int                rc;
1193
1194         rc = check_dev(dev);
1195         if (rc < 0)
1196                 return rc;
1197
1198         v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1199
1200         return 0;
1201 }
1202
1203 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1204 {
1205         struct em28xx_fh   *fh  = priv;
1206         struct em28xx      *dev = fh->dev;
1207         struct v4l2_format f;
1208         int                rc;
1209
1210         rc = check_dev(dev);
1211         if (rc < 0)
1212                 return rc;
1213
1214         dev->norm = *norm;
1215
1216         /* Adjusts width/height, if needed */
1217         f.fmt.pix.width = dev->width;
1218         f.fmt.pix.height = dev->height;
1219         vidioc_try_fmt_vid_cap(file, priv, &f);
1220
1221         /* set new image size */
1222         dev->width = f.fmt.pix.width;
1223         dev->height = f.fmt.pix.height;
1224         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1225
1226         em28xx_resolution_set(dev);
1227         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1228
1229         return 0;
1230 }
1231
1232 static int vidioc_g_parm(struct file *file, void *priv,
1233                          struct v4l2_streamparm *p)
1234 {
1235         struct em28xx_fh   *fh  = priv;
1236         struct em28xx      *dev = fh->dev;
1237         int rc = 0;
1238
1239         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1240                 return -EINVAL;
1241
1242         if (dev->board.is_webcam)
1243                 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1244                                                 video, g_parm, p);
1245         else
1246                 v4l2_video_std_frame_period(dev->norm,
1247                                                  &p->parm.capture.timeperframe);
1248
1249         return rc;
1250 }
1251
1252 static int vidioc_s_parm(struct file *file, void *priv,
1253                          struct v4l2_streamparm *p)
1254 {
1255         struct em28xx_fh   *fh  = priv;
1256         struct em28xx      *dev = fh->dev;
1257
1258         if (!dev->board.is_webcam)
1259                 return -EINVAL;
1260
1261         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1262                 return -EINVAL;
1263
1264         return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1265 }
1266
1267 static const char *iname[] = {
1268         [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1269         [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1270         [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1271         [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1272         [EM28XX_VMUX_SVIDEO]     = "S-Video",
1273         [EM28XX_VMUX_TELEVISION] = "Television",
1274         [EM28XX_VMUX_CABLE]      = "Cable TV",
1275         [EM28XX_VMUX_DVB]        = "DVB",
1276         [EM28XX_VMUX_DEBUG]      = "for debug only",
1277 };
1278
1279 static int vidioc_enum_input(struct file *file, void *priv,
1280                                 struct v4l2_input *i)
1281 {
1282         struct em28xx_fh   *fh  = priv;
1283         struct em28xx      *dev = fh->dev;
1284         unsigned int       n;
1285
1286         n = i->index;
1287         if (n >= MAX_EM28XX_INPUT)
1288                 return -EINVAL;
1289         if (0 == INPUT(n)->type)
1290                 return -EINVAL;
1291
1292         i->index = n;
1293         i->type = V4L2_INPUT_TYPE_CAMERA;
1294
1295         strcpy(i->name, iname[INPUT(n)->type]);
1296
1297         if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1298                 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1299                 i->type = V4L2_INPUT_TYPE_TUNER;
1300
1301         i->std = dev->vdev->tvnorms;
1302
1303         return 0;
1304 }
1305
1306 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1307 {
1308         struct em28xx_fh   *fh  = priv;
1309         struct em28xx      *dev = fh->dev;
1310
1311         *i = dev->ctl_input;
1312
1313         return 0;
1314 }
1315
1316 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1317 {
1318         struct em28xx_fh   *fh  = priv;
1319         struct em28xx      *dev = fh->dev;
1320         int                rc;
1321
1322         rc = check_dev(dev);
1323         if (rc < 0)
1324                 return rc;
1325
1326         if (i >= MAX_EM28XX_INPUT)
1327                 return -EINVAL;
1328         if (0 == INPUT(i)->type)
1329                 return -EINVAL;
1330
1331         video_mux(dev, i);
1332         return 0;
1333 }
1334
1335 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1336 {
1337         struct em28xx_fh   *fh    = priv;
1338         struct em28xx      *dev   = fh->dev;
1339
1340         if (!dev->audio_mode.has_audio)
1341                 return -EINVAL;
1342
1343         switch (a->index) {
1344         case EM28XX_AMUX_VIDEO:
1345                 strcpy(a->name, "Television");
1346                 break;
1347         case EM28XX_AMUX_LINE_IN:
1348                 strcpy(a->name, "Line In");
1349                 break;
1350         case EM28XX_AMUX_VIDEO2:
1351                 strcpy(a->name, "Television alt");
1352                 break;
1353         case EM28XX_AMUX_PHONE:
1354                 strcpy(a->name, "Phone");
1355                 break;
1356         case EM28XX_AMUX_MIC:
1357                 strcpy(a->name, "Mic");
1358                 break;
1359         case EM28XX_AMUX_CD:
1360                 strcpy(a->name, "CD");
1361                 break;
1362         case EM28XX_AMUX_AUX:
1363                 strcpy(a->name, "Aux");
1364                 break;
1365         case EM28XX_AMUX_PCM_OUT:
1366                 strcpy(a->name, "PCM");
1367                 break;
1368         default:
1369                 return -EINVAL;
1370         }
1371
1372         a->index = dev->ctl_ainput;
1373         a->capability = V4L2_AUDCAP_STEREO;
1374
1375         return 0;
1376 }
1377
1378 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1379 {
1380         struct em28xx_fh   *fh  = priv;
1381         struct em28xx      *dev = fh->dev;
1382
1383
1384         if (!dev->audio_mode.has_audio)
1385                 return -EINVAL;
1386
1387         if (a->index >= MAX_EM28XX_INPUT)
1388                 return -EINVAL;
1389         if (0 == INPUT(a->index)->type)
1390                 return -EINVAL;
1391
1392         dev->ctl_ainput = INPUT(a->index)->amux;
1393         dev->ctl_aoutput = INPUT(a->index)->aout;
1394
1395         if (!dev->ctl_aoutput)
1396                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1397
1398         return 0;
1399 }
1400
1401 static int vidioc_queryctrl(struct file *file, void *priv,
1402                                 struct v4l2_queryctrl *qc)
1403 {
1404         struct em28xx_fh      *fh  = priv;
1405         struct em28xx         *dev = fh->dev;
1406         int                   id  = qc->id;
1407         int                   rc;
1408
1409         rc = check_dev(dev);
1410         if (rc < 0)
1411                 return rc;
1412
1413         memset(qc, 0, sizeof(*qc));
1414
1415         qc->id = id;
1416
1417         /* enumerate AC97 controls */
1418         if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1419                 rc = ac97_queryctrl(qc);
1420                 if (!rc)
1421                         return 0;
1422         }
1423
1424         /* enumerate V4L2 device controls */
1425         v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1426
1427         if (qc->type)
1428                 return 0;
1429         else
1430                 return -EINVAL;
1431 }
1432
1433 /*
1434  * FIXME: This is an indirect way to check if a control exists at a
1435  * subdev. Instead of that hack, maybe the better would be to change all
1436  * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1437  */
1438 static int check_subdev_ctrl(struct em28xx *dev, int id)
1439 {
1440         struct v4l2_queryctrl qc;
1441
1442         memset(&qc, 0, sizeof(qc));
1443         qc.id = id;
1444
1445         /* enumerate V4L2 device controls */
1446         v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1447
1448         if (qc.type)
1449                 return 0;
1450         else
1451                 return -EINVAL;
1452 }
1453
1454 static int vidioc_g_ctrl(struct file *file, void *priv,
1455                                 struct v4l2_control *ctrl)
1456 {
1457         struct em28xx_fh      *fh  = priv;
1458         struct em28xx         *dev = fh->dev;
1459         int                   rc;
1460
1461         rc = check_dev(dev);
1462         if (rc < 0)
1463                 return rc;
1464         rc = 0;
1465
1466         /* Set an AC97 control */
1467         if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1468                 rc = ac97_get_ctrl(dev, ctrl);
1469         else
1470                 rc = 1;
1471
1472         /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1473         if (rc == 1) {
1474                 if (check_subdev_ctrl(dev, ctrl->id))
1475                         return -EINVAL;
1476
1477                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1478                 rc = 0;
1479         }
1480
1481         return rc;
1482 }
1483
1484 static int vidioc_s_ctrl(struct file *file, void *priv,
1485                                 struct v4l2_control *ctrl)
1486 {
1487         struct em28xx_fh      *fh  = priv;
1488         struct em28xx         *dev = fh->dev;
1489         int                   rc;
1490
1491         rc = check_dev(dev);
1492         if (rc < 0)
1493                 return rc;
1494
1495         /* Set an AC97 control */
1496         if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1497                 rc = ac97_set_ctrl(dev, ctrl);
1498         else
1499                 rc = 1;
1500
1501         /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1502         if (rc == 1) {
1503                 rc = check_subdev_ctrl(dev, ctrl->id);
1504                 if (!rc)
1505                         v4l2_device_call_all(&dev->v4l2_dev, 0,
1506                                              core, s_ctrl, ctrl);
1507                 /*
1508                  * In the case of non-AC97 volume controls, we still need
1509                  * to do some setups at em28xx, in order to mute/unmute
1510                  * and to adjust audio volume. However, the value ranges
1511                  * should be checked by the corresponding V4L subdriver.
1512                  */
1513                 switch (ctrl->id) {
1514                 case V4L2_CID_AUDIO_MUTE:
1515                         dev->mute = ctrl->value;
1516                         rc = em28xx_audio_analog_set(dev);
1517                         break;
1518                 case V4L2_CID_AUDIO_VOLUME:
1519                         dev->volume = ctrl->value;
1520                         rc = em28xx_audio_analog_set(dev);
1521                 }
1522         }
1523         return (rc < 0) ? rc : 0;
1524 }
1525
1526 static int vidioc_g_tuner(struct file *file, void *priv,
1527                                 struct v4l2_tuner *t)
1528 {
1529         struct em28xx_fh      *fh  = priv;
1530         struct em28xx         *dev = fh->dev;
1531         int                   rc;
1532
1533         rc = check_dev(dev);
1534         if (rc < 0)
1535                 return rc;
1536
1537         if (0 != t->index)
1538                 return -EINVAL;
1539
1540         strcpy(t->name, "Tuner");
1541         t->type = V4L2_TUNER_ANALOG_TV;
1542
1543         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1544         return 0;
1545 }
1546
1547 static int vidioc_s_tuner(struct file *file, void *priv,
1548                                 struct v4l2_tuner *t)
1549 {
1550         struct em28xx_fh      *fh  = priv;
1551         struct em28xx         *dev = fh->dev;
1552         int                   rc;
1553
1554         rc = check_dev(dev);
1555         if (rc < 0)
1556                 return rc;
1557
1558         if (0 != t->index)
1559                 return -EINVAL;
1560
1561         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1562         return 0;
1563 }
1564
1565 static int vidioc_g_frequency(struct file *file, void *priv,
1566                                 struct v4l2_frequency *f)
1567 {
1568         struct em28xx_fh      *fh  = priv;
1569         struct em28xx         *dev = fh->dev;
1570
1571         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1572         f->frequency = dev->ctl_freq;
1573         return 0;
1574 }
1575
1576 static int vidioc_s_frequency(struct file *file, void *priv,
1577                                 struct v4l2_frequency *f)
1578 {
1579         struct em28xx_fh      *fh  = priv;
1580         struct em28xx         *dev = fh->dev;
1581         int                   rc;
1582
1583         rc = check_dev(dev);
1584         if (rc < 0)
1585                 return rc;
1586
1587         if (0 != f->tuner)
1588                 return -EINVAL;
1589
1590         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1591                 return -EINVAL;
1592         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1593                 return -EINVAL;
1594
1595         dev->ctl_freq = f->frequency;
1596         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1597
1598         return 0;
1599 }
1600
1601 #ifdef CONFIG_VIDEO_ADV_DEBUG
1602 static int em28xx_reg_len(int reg)
1603 {
1604         switch (reg) {
1605         case EM28XX_R40_AC97LSB:
1606         case EM28XX_R30_HSCALELOW:
1607         case EM28XX_R32_VSCALELOW:
1608                 return 2;
1609         default:
1610                 return 1;
1611         }
1612 }
1613
1614 static int vidioc_g_chip_ident(struct file *file, void *priv,
1615                struct v4l2_dbg_chip_ident *chip)
1616 {
1617         struct em28xx_fh      *fh  = priv;
1618         struct em28xx         *dev = fh->dev;
1619
1620         chip->ident = V4L2_IDENT_NONE;
1621         chip->revision = 0;
1622
1623         v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1624
1625         return 0;
1626 }
1627
1628
1629 static int vidioc_g_register(struct file *file, void *priv,
1630                              struct v4l2_dbg_register *reg)
1631 {
1632         struct em28xx_fh      *fh  = priv;
1633         struct em28xx         *dev = fh->dev;
1634         int ret;
1635
1636         switch (reg->match.type) {
1637         case V4L2_CHIP_MATCH_AC97:
1638                 ret = em28xx_read_ac97(dev, reg->reg);
1639                 if (ret < 0)
1640                         return ret;
1641
1642                 reg->val = ret;
1643                 reg->size = 1;
1644                 return 0;
1645         case V4L2_CHIP_MATCH_I2C_DRIVER:
1646                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1647                 return 0;
1648         case V4L2_CHIP_MATCH_I2C_ADDR:
1649                 /* TODO: is this correct? */
1650                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1651                 return 0;
1652         default:
1653                 if (!v4l2_chip_match_host(&reg->match))
1654                         return -EINVAL;
1655         }
1656
1657         /* Match host */
1658         reg->size = em28xx_reg_len(reg->reg);
1659         if (reg->size == 1) {
1660                 ret = em28xx_read_reg(dev, reg->reg);
1661
1662                 if (ret < 0)
1663                         return ret;
1664
1665                 reg->val = ret;
1666         } else {
1667                 __le16 val = 0;
1668                 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1669                                                    reg->reg, (char *)&val, 2);
1670                 if (ret < 0)
1671                         return ret;
1672
1673                 reg->val = le16_to_cpu(val);
1674         }
1675
1676         return 0;
1677 }
1678
1679 static int vidioc_s_register(struct file *file, void *priv,
1680                              struct v4l2_dbg_register *reg)
1681 {
1682         struct em28xx_fh      *fh  = priv;
1683         struct em28xx         *dev = fh->dev;
1684         __le16 buf;
1685
1686         switch (reg->match.type) {
1687         case V4L2_CHIP_MATCH_AC97:
1688                 return em28xx_write_ac97(dev, reg->reg, reg->val);
1689         case V4L2_CHIP_MATCH_I2C_DRIVER:
1690                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1691                 return 0;
1692         case V4L2_CHIP_MATCH_I2C_ADDR:
1693                 /* TODO: is this correct? */
1694                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1695                 return 0;
1696         default:
1697                 if (!v4l2_chip_match_host(&reg->match))
1698                         return -EINVAL;
1699         }
1700
1701         /* Match host */
1702         buf = cpu_to_le16(reg->val);
1703
1704         return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1705                                em28xx_reg_len(reg->reg));
1706 }
1707 #endif
1708
1709
1710 static int vidioc_cropcap(struct file *file, void *priv,
1711                                         struct v4l2_cropcap *cc)
1712 {
1713         struct em28xx_fh      *fh  = priv;
1714         struct em28xx         *dev = fh->dev;
1715
1716         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1717                 return -EINVAL;
1718
1719         cc->bounds.left = 0;
1720         cc->bounds.top = 0;
1721         cc->bounds.width = dev->width;
1722         cc->bounds.height = dev->height;
1723         cc->defrect = cc->bounds;
1724         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1725         cc->pixelaspect.denominator = 59;
1726
1727         return 0;
1728 }
1729
1730 static int vidioc_streamon(struct file *file, void *priv,
1731                                         enum v4l2_buf_type type)
1732 {
1733         struct em28xx_fh      *fh  = priv;
1734         struct em28xx         *dev = fh->dev;
1735         int                   rc = -EINVAL;
1736
1737         rc = check_dev(dev);
1738         if (rc < 0)
1739                 return rc;
1740
1741         if (unlikely(type != fh->type))
1742                 return -EINVAL;
1743
1744         em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1745                         fh, type, fh->resources, dev->resources);
1746
1747         if (unlikely(!res_get(fh, get_ressource(fh))))
1748                 return -EBUSY;
1749
1750         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1751                 rc = videobuf_streamon(&fh->vb_vidq);
1752         else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1753                 rc = videobuf_streamon(&fh->vb_vbiq);
1754
1755         return rc;
1756 }
1757
1758 static int vidioc_streamoff(struct file *file, void *priv,
1759                                         enum v4l2_buf_type type)
1760 {
1761         struct em28xx_fh      *fh  = priv;
1762         struct em28xx         *dev = fh->dev;
1763         int                   rc;
1764
1765         rc = check_dev(dev);
1766         if (rc < 0)
1767                 return rc;
1768
1769         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1770             fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1771                 return -EINVAL;
1772         if (type != fh->type)
1773                 return -EINVAL;
1774
1775         em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1776                         fh, type, fh->resources, dev->resources);
1777
1778         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1779                 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1780                         videobuf_streamoff(&fh->vb_vidq);
1781                         res_free(fh, EM28XX_RESOURCE_VIDEO);
1782                 }
1783         } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1784                 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1785                         videobuf_streamoff(&fh->vb_vbiq);
1786                         res_free(fh, EM28XX_RESOURCE_VBI);
1787                 }
1788         }
1789
1790         return 0;
1791 }
1792
1793 static int vidioc_querycap(struct file *file, void  *priv,
1794                                         struct v4l2_capability *cap)
1795 {
1796         struct em28xx_fh      *fh  = priv;
1797         struct em28xx         *dev = fh->dev;
1798
1799         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1800         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1801         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1802
1803         cap->capabilities =
1804                         V4L2_CAP_SLICED_VBI_CAPTURE |
1805                         V4L2_CAP_VIDEO_CAPTURE |
1806                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1807
1808         if (dev->vbi_dev)
1809                 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1810
1811         if (dev->audio_mode.has_audio)
1812                 cap->capabilities |= V4L2_CAP_AUDIO;
1813
1814         if (dev->tuner_type != TUNER_ABSENT)
1815                 cap->capabilities |= V4L2_CAP_TUNER;
1816
1817         return 0;
1818 }
1819
1820 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1821                                         struct v4l2_fmtdesc *f)
1822 {
1823         if (unlikely(f->index >= ARRAY_SIZE(format)))
1824                 return -EINVAL;
1825
1826         strlcpy(f->description, format[f->index].name, sizeof(f->description));
1827         f->pixelformat = format[f->index].fourcc;
1828
1829         return 0;
1830 }
1831
1832 static int vidioc_enum_framesizes(struct file *file, void *priv,
1833                                   struct v4l2_frmsizeenum *fsize)
1834 {
1835         struct em28xx_fh      *fh  = priv;
1836         struct em28xx         *dev = fh->dev;
1837         struct em28xx_fmt     *fmt;
1838         unsigned int          maxw = norm_maxw(dev);
1839         unsigned int          maxh = norm_maxh(dev);
1840
1841         fmt = format_by_fourcc(fsize->pixel_format);
1842         if (!fmt) {
1843                 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1844                                 fsize->pixel_format);
1845                 return -EINVAL;
1846         }
1847
1848         if (dev->board.is_em2800) {
1849                 if (fsize->index > 1)
1850                         return -EINVAL;
1851                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1852                 fsize->discrete.width = maxw / (1 + fsize->index);
1853                 fsize->discrete.height = maxh / (1 + fsize->index);
1854                 return 0;
1855         }
1856
1857         if (fsize->index != 0)
1858                 return -EINVAL;
1859
1860         /* Report a continuous range */
1861         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1862         fsize->stepwise.min_width = 48;
1863         fsize->stepwise.min_height = 32;
1864         fsize->stepwise.max_width = maxw;
1865         fsize->stepwise.max_height = maxh;
1866         fsize->stepwise.step_width = 1;
1867         fsize->stepwise.step_height = 1;
1868         return 0;
1869 }
1870
1871 /* Sliced VBI ioctls */
1872 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1873                                         struct v4l2_format *f)
1874 {
1875         struct em28xx_fh      *fh  = priv;
1876         struct em28xx         *dev = fh->dev;
1877         int                   rc;
1878
1879         rc = check_dev(dev);
1880         if (rc < 0)
1881                 return rc;
1882
1883         f->fmt.sliced.service_set = 0;
1884         v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1885
1886         if (f->fmt.sliced.service_set == 0)
1887                 rc = -EINVAL;
1888
1889         return rc;
1890 }
1891
1892 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1893                         struct v4l2_format *f)
1894 {
1895         struct em28xx_fh      *fh  = priv;
1896         struct em28xx         *dev = fh->dev;
1897         int                   rc;
1898
1899         rc = check_dev(dev);
1900         if (rc < 0)
1901                 return rc;
1902
1903         v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1904
1905         if (f->fmt.sliced.service_set == 0)
1906                 return -EINVAL;
1907
1908         return 0;
1909 }
1910
1911 /* RAW VBI ioctls */
1912
1913 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1914                                 struct v4l2_format *format)
1915 {
1916         struct em28xx_fh      *fh  = priv;
1917         struct em28xx         *dev = fh->dev;
1918
1919         format->fmt.vbi.samples_per_line = dev->vbi_width;
1920         format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1921         format->fmt.vbi.offset = 0;
1922         format->fmt.vbi.flags = 0;
1923         format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1924         format->fmt.vbi.count[0] = dev->vbi_height;
1925         format->fmt.vbi.count[1] = dev->vbi_height;
1926
1927         /* Varies by video standard (NTSC, PAL, etc.) */
1928         if (dev->norm & V4L2_STD_525_60) {
1929                 /* NTSC */
1930                 format->fmt.vbi.start[0] = 10;
1931                 format->fmt.vbi.start[1] = 273;
1932         } else if (dev->norm & V4L2_STD_625_50) {
1933                 /* PAL */
1934                 format->fmt.vbi.start[0] = 6;
1935                 format->fmt.vbi.start[1] = 318;
1936         }
1937
1938         return 0;
1939 }
1940
1941 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1942                                 struct v4l2_format *format)
1943 {
1944         struct em28xx_fh      *fh  = priv;
1945         struct em28xx         *dev = fh->dev;
1946
1947         format->fmt.vbi.samples_per_line = dev->vbi_width;
1948         format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1949         format->fmt.vbi.offset = 0;
1950         format->fmt.vbi.flags = 0;
1951         format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1952         format->fmt.vbi.count[0] = dev->vbi_height;
1953         format->fmt.vbi.count[1] = dev->vbi_height;
1954
1955         /* Varies by video standard (NTSC, PAL, etc.) */
1956         if (dev->norm & V4L2_STD_525_60) {
1957                 /* NTSC */
1958                 format->fmt.vbi.start[0] = 10;
1959                 format->fmt.vbi.start[1] = 273;
1960         } else if (dev->norm & V4L2_STD_625_50) {
1961                 /* PAL */
1962                 format->fmt.vbi.start[0] = 6;
1963                 format->fmt.vbi.start[1] = 318;
1964         }
1965
1966         return 0;
1967 }
1968
1969 static int vidioc_reqbufs(struct file *file, void *priv,
1970                           struct v4l2_requestbuffers *rb)
1971 {
1972         struct em28xx_fh      *fh  = priv;
1973         struct em28xx         *dev = fh->dev;
1974         int                   rc;
1975
1976         rc = check_dev(dev);
1977         if (rc < 0)
1978                 return rc;
1979
1980         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1981                 return videobuf_reqbufs(&fh->vb_vidq, rb);
1982         else
1983                 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1984 }
1985
1986 static int vidioc_querybuf(struct file *file, void *priv,
1987                            struct v4l2_buffer *b)
1988 {
1989         struct em28xx_fh      *fh  = priv;
1990         struct em28xx         *dev = fh->dev;
1991         int                   rc;
1992
1993         rc = check_dev(dev);
1994         if (rc < 0)
1995                 return rc;
1996
1997         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1998                 return videobuf_querybuf(&fh->vb_vidq, b);
1999         else {
2000                 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
2001                    the videobuf framework, but we probably shouldn't be
2002                    returning a buffer larger than that which was asked for.
2003                    At a minimum, it causes a crash in zvbi since it does
2004                    a memcpy based on the source buffer length */
2005                 int result = videobuf_querybuf(&fh->vb_vbiq, b);
2006                 b->length = dev->vbi_width * dev->vbi_height * 2;
2007
2008                 return result;
2009         }
2010 }
2011
2012 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2013 {
2014         struct em28xx_fh      *fh  = priv;
2015         struct em28xx         *dev = fh->dev;
2016         int                   rc;
2017
2018         rc = check_dev(dev);
2019         if (rc < 0)
2020                 return rc;
2021
2022         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2023                 return videobuf_qbuf(&fh->vb_vidq, b);
2024         else
2025                 return videobuf_qbuf(&fh->vb_vbiq, b);
2026 }
2027
2028 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2029 {
2030         struct em28xx_fh      *fh  = priv;
2031         struct em28xx         *dev = fh->dev;
2032         int                   rc;
2033
2034         rc = check_dev(dev);
2035         if (rc < 0)
2036                 return rc;
2037
2038         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2039                 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2040                                       O_NONBLOCK);
2041         else
2042                 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2043                                       O_NONBLOCK);
2044 }
2045
2046 /* ----------------------------------------------------------- */
2047 /* RADIO ESPECIFIC IOCTLS                                      */
2048 /* ----------------------------------------------------------- */
2049
2050 static int radio_querycap(struct file *file, void  *priv,
2051                           struct v4l2_capability *cap)
2052 {
2053         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2054
2055         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2056         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2057         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2058
2059         cap->capabilities = V4L2_CAP_TUNER;
2060         return 0;
2061 }
2062
2063 static int radio_g_tuner(struct file *file, void *priv,
2064                          struct v4l2_tuner *t)
2065 {
2066         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2067
2068         if (unlikely(t->index > 0))
2069                 return -EINVAL;
2070
2071         strcpy(t->name, "Radio");
2072         t->type = V4L2_TUNER_RADIO;
2073
2074         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
2075
2076         return 0;
2077 }
2078
2079 static int radio_enum_input(struct file *file, void *priv,
2080                             struct v4l2_input *i)
2081 {
2082         if (i->index != 0)
2083                 return -EINVAL;
2084         strcpy(i->name, "Radio");
2085         i->type = V4L2_INPUT_TYPE_TUNER;
2086
2087         return 0;
2088 }
2089
2090 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2091 {
2092         if (unlikely(a->index))
2093                 return -EINVAL;
2094
2095         strcpy(a->name, "Radio");
2096         return 0;
2097 }
2098
2099 static int radio_s_tuner(struct file *file, void *priv,
2100                          struct v4l2_tuner *t)
2101 {
2102         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2103
2104         if (0 != t->index)
2105                 return -EINVAL;
2106
2107         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2108
2109         return 0;
2110 }
2111
2112 static int radio_s_audio(struct file *file, void *fh,
2113                          const struct v4l2_audio *a)
2114 {
2115         return 0;
2116 }
2117
2118 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2119 {
2120         return 0;
2121 }
2122
2123 static int radio_queryctrl(struct file *file, void *priv,
2124                            struct v4l2_queryctrl *qc)
2125 {
2126         int i;
2127
2128         if (qc->id <  V4L2_CID_BASE ||
2129                 qc->id >= V4L2_CID_LASTP1)
2130                 return -EINVAL;
2131
2132         for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2133                 if (qc->id && qc->id == ac97_qctrl[i].id) {
2134                         memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2135                         return 0;
2136                 }
2137         }
2138
2139         return -EINVAL;
2140 }
2141
2142 /*
2143  * em28xx_v4l2_open()
2144  * inits the device and starts isoc transfer
2145  */
2146 static int em28xx_v4l2_open(struct file *filp)
2147 {
2148         int errCode = 0, radio = 0;
2149         struct video_device *vdev = video_devdata(filp);
2150         struct em28xx *dev = video_drvdata(filp);
2151         enum v4l2_buf_type fh_type = 0;
2152         struct em28xx_fh *fh;
2153         enum v4l2_field field;
2154
2155         switch (vdev->vfl_type) {
2156         case VFL_TYPE_GRABBER:
2157                 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2158                 break;
2159         case VFL_TYPE_VBI:
2160                 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2161                 break;
2162         case VFL_TYPE_RADIO:
2163                 radio = 1;
2164                 break;
2165         }
2166
2167         em28xx_videodbg("open dev=%s type=%s users=%d\n",
2168                         video_device_node_name(vdev), v4l2_type_names[fh_type],
2169                         dev->users);
2170
2171
2172         if (mutex_lock_interruptible(&dev->lock))
2173                 return -ERESTARTSYS;
2174         fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
2175         if (!fh) {
2176                 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2177                 mutex_unlock(&dev->lock);
2178                 return -ENOMEM;
2179         }
2180         fh->dev = dev;
2181         fh->radio = radio;
2182         fh->type = fh_type;
2183         filp->private_data = fh;
2184
2185         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2186                 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2187                 em28xx_set_alternate(dev);
2188                 em28xx_resolution_set(dev);
2189
2190                 /* Needed, since GPIO might have disabled power of
2191                    some i2c device
2192                  */
2193                 em28xx_wake_i2c(dev);
2194
2195         }
2196         if (fh->radio) {
2197                 em28xx_videodbg("video_open: setting radio device\n");
2198                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2199         }
2200
2201         dev->users++;
2202
2203         if (dev->progressive)
2204                 field = V4L2_FIELD_NONE;
2205         else
2206                 field = V4L2_FIELD_INTERLACED;
2207
2208         videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2209                                     NULL, &dev->slock,
2210                                     V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2211                                     sizeof(struct em28xx_buffer), fh, &dev->lock);
2212
2213         videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2214                                     NULL, &dev->slock,
2215                                     V4L2_BUF_TYPE_VBI_CAPTURE,
2216                                     V4L2_FIELD_SEQ_TB,
2217                                     sizeof(struct em28xx_buffer), fh, &dev->lock);
2218         mutex_unlock(&dev->lock);
2219
2220         return errCode;
2221 }
2222
2223 /*
2224  * em28xx_realease_resources()
2225  * unregisters the v4l2,i2c and usb devices
2226  * called when the device gets disconected or at module unload
2227 */
2228 void em28xx_release_analog_resources(struct em28xx *dev)
2229 {
2230
2231         /*FIXME: I2C IR should be disconnected */
2232
2233         if (dev->radio_dev) {
2234                 if (video_is_registered(dev->radio_dev))
2235                         video_unregister_device(dev->radio_dev);
2236                 else
2237                         video_device_release(dev->radio_dev);
2238                 dev->radio_dev = NULL;
2239         }
2240         if (dev->vbi_dev) {
2241                 em28xx_info("V4L2 device %s deregistered\n",
2242                             video_device_node_name(dev->vbi_dev));
2243                 if (video_is_registered(dev->vbi_dev))
2244                         video_unregister_device(dev->vbi_dev);
2245                 else
2246                         video_device_release(dev->vbi_dev);
2247                 dev->vbi_dev = NULL;
2248         }
2249         if (dev->vdev) {
2250                 em28xx_info("V4L2 device %s deregistered\n",
2251                             video_device_node_name(dev->vdev));
2252                 if (video_is_registered(dev->vdev))
2253                         video_unregister_device(dev->vdev);
2254                 else
2255                         video_device_release(dev->vdev);
2256                 dev->vdev = NULL;
2257         }
2258 }
2259
2260 /*
2261  * em28xx_v4l2_close()
2262  * stops streaming and deallocates all resources allocated by the v4l2
2263  * calls and ioctls
2264  */
2265 static int em28xx_v4l2_close(struct file *filp)
2266 {
2267         struct em28xx_fh *fh  = filp->private_data;
2268         struct em28xx    *dev = fh->dev;
2269         int              errCode;
2270
2271         em28xx_videodbg("users=%d\n", dev->users);
2272
2273         mutex_lock(&dev->lock);
2274         if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2275                 videobuf_stop(&fh->vb_vidq);
2276                 res_free(fh, EM28XX_RESOURCE_VIDEO);
2277         }
2278
2279         if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2280                 videobuf_stop(&fh->vb_vbiq);
2281                 res_free(fh, EM28XX_RESOURCE_VBI);
2282         }
2283
2284         if (dev->users == 1) {
2285                 /* the device is already disconnect,
2286                    free the remaining resources */
2287                 if (dev->state & DEV_DISCONNECTED) {
2288                         em28xx_release_resources(dev);
2289                         kfree(dev->alt_max_pkt_size);
2290                         mutex_unlock(&dev->lock);
2291                         kfree(dev);
2292                         kfree(fh);
2293                         return 0;
2294                 }
2295
2296                 /* Save some power by putting tuner to sleep */
2297                 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2298
2299                 /* do this before setting alternate! */
2300                 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
2301                 em28xx_set_mode(dev, EM28XX_SUSPEND);
2302
2303                 /* set alternate 0 */
2304                 dev->alt = 0;
2305                 em28xx_videodbg("setting alternate 0\n");
2306                 errCode = usb_set_interface(dev->udev, 0, 0);
2307                 if (errCode < 0) {
2308                         em28xx_errdev("cannot change alternate number to "
2309                                         "0 (error=%i)\n", errCode);
2310                 }
2311         }
2312
2313         videobuf_mmap_free(&fh->vb_vidq);
2314         videobuf_mmap_free(&fh->vb_vbiq);
2315         kfree(fh);
2316         dev->users--;
2317         mutex_unlock(&dev->lock);
2318         return 0;
2319 }
2320
2321 /*
2322  * em28xx_v4l2_read()
2323  * will allocate buffers when called for the first time
2324  */
2325 static ssize_t
2326 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2327                  loff_t *pos)
2328 {
2329         struct em28xx_fh *fh = filp->private_data;
2330         struct em28xx *dev = fh->dev;
2331         int rc;
2332
2333         rc = check_dev(dev);
2334         if (rc < 0)
2335                 return rc;
2336
2337         if (mutex_lock_interruptible(&dev->lock))
2338                 return -ERESTARTSYS;
2339         /* FIXME: read() is not prepared to allow changing the video
2340            resolution while streaming. Seems a bug at em28xx_set_fmt
2341          */
2342
2343         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2344                 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2345                         rc = -EBUSY;
2346                 else
2347                         rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2348                                         filp->f_flags & O_NONBLOCK);
2349         } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2350                 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2351                         rc = -EBUSY;
2352                 else
2353                         rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2354                                         filp->f_flags & O_NONBLOCK);
2355         }
2356         mutex_unlock(&dev->lock);
2357
2358         return rc;
2359 }
2360
2361 /*
2362  * em28xx_poll()
2363  * will allocate buffers when called for the first time
2364  */
2365 static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
2366 {
2367         struct em28xx_fh *fh = filp->private_data;
2368         struct em28xx *dev = fh->dev;
2369         int rc;
2370
2371         rc = check_dev(dev);
2372         if (rc < 0)
2373                 return rc;
2374
2375         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2376                 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2377                         return POLLERR;
2378                 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2379         } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2380                 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2381                         return POLLERR;
2382                 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2383         } else {
2384                 return POLLERR;
2385         }
2386 }
2387
2388 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2389 {
2390         struct em28xx_fh *fh = filp->private_data;
2391         struct em28xx *dev = fh->dev;
2392         unsigned int res;
2393
2394         mutex_lock(&dev->lock);
2395         res = em28xx_poll(filp, wait);
2396         mutex_unlock(&dev->lock);
2397         return res;
2398 }
2399
2400 /*
2401  * em28xx_v4l2_mmap()
2402  */
2403 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2404 {
2405         struct em28xx_fh *fh    = filp->private_data;
2406         struct em28xx    *dev   = fh->dev;
2407         int              rc;
2408
2409         rc = check_dev(dev);
2410         if (rc < 0)
2411                 return rc;
2412
2413         if (mutex_lock_interruptible(&dev->lock))
2414                 return -ERESTARTSYS;
2415         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2416                 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2417         else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2418                 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2419         mutex_unlock(&dev->lock);
2420
2421         em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2422                 (unsigned long)vma->vm_start,
2423                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2424                 rc);
2425
2426         return rc;
2427 }
2428
2429 static const struct v4l2_file_operations em28xx_v4l_fops = {
2430         .owner         = THIS_MODULE,
2431         .open          = em28xx_v4l2_open,
2432         .release       = em28xx_v4l2_close,
2433         .read          = em28xx_v4l2_read,
2434         .poll          = em28xx_v4l2_poll,
2435         .mmap          = em28xx_v4l2_mmap,
2436         .unlocked_ioctl = video_ioctl2,
2437 };
2438
2439 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2440         .vidioc_querycap            = vidioc_querycap,
2441         .vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
2442         .vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
2443         .vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
2444         .vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
2445         .vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
2446         .vidioc_s_fmt_vbi_cap       = vidioc_s_fmt_vbi_cap,
2447         .vidioc_enum_framesizes     = vidioc_enum_framesizes,
2448         .vidioc_g_audio             = vidioc_g_audio,
2449         .vidioc_s_audio             = vidioc_s_audio,
2450         .vidioc_cropcap             = vidioc_cropcap,
2451         .vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
2452         .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2453         .vidioc_s_fmt_sliced_vbi_cap   = vidioc_try_set_sliced_vbi_cap,
2454
2455         .vidioc_reqbufs             = vidioc_reqbufs,
2456         .vidioc_querybuf            = vidioc_querybuf,
2457         .vidioc_qbuf                = vidioc_qbuf,
2458         .vidioc_dqbuf               = vidioc_dqbuf,
2459         .vidioc_g_std               = vidioc_g_std,
2460         .vidioc_querystd            = vidioc_querystd,
2461         .vidioc_s_std               = vidioc_s_std,
2462         .vidioc_g_parm              = vidioc_g_parm,
2463         .vidioc_s_parm              = vidioc_s_parm,
2464         .vidioc_enum_input          = vidioc_enum_input,
2465         .vidioc_g_input             = vidioc_g_input,
2466         .vidioc_s_input             = vidioc_s_input,
2467         .vidioc_queryctrl           = vidioc_queryctrl,
2468         .vidioc_g_ctrl              = vidioc_g_ctrl,
2469         .vidioc_s_ctrl              = vidioc_s_ctrl,
2470         .vidioc_streamon            = vidioc_streamon,
2471         .vidioc_streamoff           = vidioc_streamoff,
2472         .vidioc_g_tuner             = vidioc_g_tuner,
2473         .vidioc_s_tuner             = vidioc_s_tuner,
2474         .vidioc_g_frequency         = vidioc_g_frequency,
2475         .vidioc_s_frequency         = vidioc_s_frequency,
2476 #ifdef CONFIG_VIDEO_ADV_DEBUG
2477         .vidioc_g_register          = vidioc_g_register,
2478         .vidioc_s_register          = vidioc_s_register,
2479         .vidioc_g_chip_ident        = vidioc_g_chip_ident,
2480 #endif
2481 };
2482
2483 static const struct video_device em28xx_video_template = {
2484         .fops                       = &em28xx_v4l_fops,
2485         .release                    = video_device_release,
2486         .ioctl_ops                  = &video_ioctl_ops,
2487
2488         .tvnorms                    = V4L2_STD_ALL,
2489         .current_norm               = V4L2_STD_PAL,
2490 };
2491
2492 static const struct v4l2_file_operations radio_fops = {
2493         .owner         = THIS_MODULE,
2494         .open          = em28xx_v4l2_open,
2495         .release       = em28xx_v4l2_close,
2496         .unlocked_ioctl = video_ioctl2,
2497 };
2498
2499 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2500         .vidioc_querycap      = radio_querycap,
2501         .vidioc_g_tuner       = radio_g_tuner,
2502         .vidioc_enum_input    = radio_enum_input,
2503         .vidioc_g_audio       = radio_g_audio,
2504         .vidioc_s_tuner       = radio_s_tuner,
2505         .vidioc_s_audio       = radio_s_audio,
2506         .vidioc_s_input       = radio_s_input,
2507         .vidioc_queryctrl     = radio_queryctrl,
2508         .vidioc_g_ctrl        = vidioc_g_ctrl,
2509         .vidioc_s_ctrl        = vidioc_s_ctrl,
2510         .vidioc_g_frequency   = vidioc_g_frequency,
2511         .vidioc_s_frequency   = vidioc_s_frequency,
2512 #ifdef CONFIG_VIDEO_ADV_DEBUG
2513         .vidioc_g_register    = vidioc_g_register,
2514         .vidioc_s_register    = vidioc_s_register,
2515 #endif
2516 };
2517
2518 static struct video_device em28xx_radio_template = {
2519         .name                 = "em28xx-radio",
2520         .fops                 = &radio_fops,
2521         .ioctl_ops            = &radio_ioctl_ops,
2522 };
2523
2524 /******************************** usb interface ******************************/
2525
2526
2527
2528 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2529                                         const struct video_device *template,
2530                                         const char *type_name)
2531 {
2532         struct video_device *vfd;
2533
2534         vfd = video_device_alloc();
2535         if (NULL == vfd)
2536                 return NULL;
2537
2538         *vfd            = *template;
2539         vfd->v4l2_dev   = &dev->v4l2_dev;
2540         vfd->release    = video_device_release;
2541         vfd->debug      = video_debug;
2542         vfd->lock       = &dev->lock;
2543
2544         snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2545                  dev->name, type_name);
2546
2547         video_set_drvdata(vfd, dev);
2548         return vfd;
2549 }
2550
2551 int em28xx_register_analog_devices(struct em28xx *dev)
2552 {
2553       u8 val;
2554         int ret;
2555         unsigned int maxw;
2556
2557         printk(KERN_INFO "%s: v4l2 driver version %s\n",
2558                 dev->name, EM28XX_VERSION);
2559
2560         /* set default norm */
2561         dev->norm = em28xx_video_template.current_norm;
2562         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
2563         dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2564
2565         /* Analog specific initialization */
2566         dev->format = &format[0];
2567
2568         maxw = norm_maxw(dev);
2569         /* MaxPacketSize for em2800 is too small to capture at full resolution
2570          * use half of maxw as the scaler can only scale to 50% */
2571         if (dev->board.is_em2800)
2572             maxw /= 2;
2573
2574         em28xx_set_video_format(dev, format[0].fourcc,
2575                                 maxw, norm_maxh(dev));
2576
2577         video_mux(dev, 0);
2578
2579         /* Audio defaults */
2580         dev->mute = 1;
2581         dev->volume = 0x1f;
2582
2583 /*      em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2584         val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2585         em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2586                          (EM28XX_XCLK_AUDIO_UNMUTE | val));
2587
2588         em28xx_set_outfmt(dev);
2589         em28xx_colorlevels_set_default(dev);
2590         em28xx_compression_disable(dev);
2591
2592         /* allocate and fill video video_device struct */
2593         dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2594         if (!dev->vdev) {
2595                 em28xx_errdev("cannot allocate video_device.\n");
2596                 return -ENODEV;
2597         }
2598
2599         /* register v4l2 video video_device */
2600         ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2601                                        video_nr[dev->devno]);
2602         if (ret) {
2603                 em28xx_errdev("unable to register video device (error=%i).\n",
2604                               ret);
2605                 return ret;
2606         }
2607
2608         /* Allocate and fill vbi video_device struct */
2609         if (em28xx_vbi_supported(dev) == 1) {
2610                 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2611                                                 "vbi");
2612
2613                 /* register v4l2 vbi video_device */
2614                 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2615                                             vbi_nr[dev->devno]);
2616                 if (ret < 0) {
2617                         em28xx_errdev("unable to register vbi device\n");
2618                         return ret;
2619                 }
2620         }
2621
2622         if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2623                 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2624                                                   "radio");
2625                 if (!dev->radio_dev) {
2626                         em28xx_errdev("cannot allocate video_device.\n");
2627                         return -ENODEV;
2628                 }
2629                 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2630                                             radio_nr[dev->devno]);
2631                 if (ret < 0) {
2632                         em28xx_errdev("can't register radio device\n");
2633                         return ret;
2634                 }
2635                 em28xx_info("Registered radio device as %s\n",
2636                             video_device_node_name(dev->radio_dev));
2637         }
2638
2639         em28xx_info("V4L2 video device registered as %s\n",
2640                     video_device_node_name(dev->vdev));
2641
2642         if (dev->vbi_dev)
2643                 em28xx_info("V4L2 VBI device registered as %s\n",
2644                             video_device_node_name(dev->vbi_dev));
2645
2646         return 0;
2647 }