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