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