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