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