Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / em28xx / em28xx-video.c
blobac2bd935927e713e19e0ee67ef861f0154e33a86
1 /*
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
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>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
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.
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.
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.
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>
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>
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>"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
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)
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
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)
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
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 };
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");
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
90 /* supported video standards */
91 static struct em28xx_fmt format[] = {
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,
125 /* supported controls */
126 /* Common to all boards */
127 static struct v4l2_queryctrl ac97_qctrl[] = {
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,
149 /* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
154 * Announces that a buffer were filled and request the next
156 static inline void buffer_filled(struct em28xx *dev,
157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
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);
166 dev->isoc_ctl.vid_buf = NULL;
168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
172 static inline void vbi_buffer_filled(struct em28xx *dev,
173 struct em28xx_dmaqueue *dma_q,
174 struct em28xx_buffer *buf)
176 /* Advice that buffer was filled */
177 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179 buf->vb.state = VIDEOBUF_DONE;
180 buf->vb.field_count++;
181 do_gettimeofday(&buf->vb.ts);
183 dev->isoc_ctl.vbi_buf = NULL;
185 list_del(&buf->vb.queue);
186 wake_up(&buf->vb.done);
190 * Identify the buffer header type and properly handles
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)
198 void *fieldstart, *startwrite, *startread;
199 int linesdone, currlinedone, offset, lencopy, remain;
200 int bytesperline = dev->width << 1;
202 if (dma_q->pos + len > buf->vb.size)
203 len = buf->vb.size - dma_q->pos;
205 if (p[0] != 0x88 && p[0] != 0x22) {
206 em28xx_isocdbg("frame is not complete\n");
207 len += 4;
208 } else
209 p += 4;
211 startread = p;
212 remain = len;
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;
224 linesdone = dma_q->pos / bytesperline;
225 currlinedone = dma_q->pos % bytesperline;
227 if (dev->progressive)
228 offset = linesdone * bytesperline + currlinedone;
229 else
230 offset = linesdone * bytesperline * 2 + currlinedone;
232 startwrite = fieldstart + offset;
233 lencopy = bytesperline - currlinedone;
234 lencopy = lencopy > remain ? remain : lencopy;
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;
243 if (lencopy <= 0)
244 return;
245 memcpy(startwrite, startread, lencopy);
247 remain -= lencopy;
249 while (remain > 0) {
250 startwrite += lencopy + bytesperline;
251 startread += lencopy;
252 if (bytesperline > remain)
253 lencopy = remain;
254 else
255 lencopy = bytesperline;
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;
266 if (lencopy <= 0)
267 break;
269 memcpy(startwrite, startread, lencopy);
271 remain -= lencopy;
274 dma_q->pos += len;
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)
283 void *startwrite, *startread;
284 int offset;
285 int bytesperline = dev->vbi_width;
287 if (dev == NULL) {
288 em28xx_isocdbg("dev is null\n");
289 return;
292 if (dma_q == NULL) {
293 em28xx_isocdbg("dma_q is null\n");
294 return;
296 if (buf == NULL) {
297 return;
299 if (p == NULL) {
300 em28xx_isocdbg("p is null\n");
301 return;
303 if (outp == NULL) {
304 em28xx_isocdbg("outp is null\n");
305 return;
308 if (dma_q->pos + len > buf->vb.size)
309 len = buf->vb.size - dma_q->pos;
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;
319 startread = p;
321 startwrite = outp + dma_q->pos;
322 offset = dma_q->pos;
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;
330 memcpy(startwrite, startread, len);
331 dma_q->pos += len;
334 static inline void print_err_status(struct em28xx *dev,
335 int packet, int status)
337 char *errmsg = "Unknown";
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;
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);
374 * video-buf generic routine to get the next available buffer
376 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
377 struct em28xx_buffer **buf)
379 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
380 char *outp;
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;
389 /* Get the next buffer */
390 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
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);
396 dev->isoc_ctl.vid_buf = *buf;
398 return;
402 * video-buf generic routine to get the next available VBI buffer
404 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
405 struct em28xx_buffer **buf)
407 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
408 char *outp;
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;
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);
423 dev->isoc_ctl.vbi_buf = *buf;
425 return;
429 * Controls the isoc copy of each urb packet
431 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
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;
439 if (!dev)
440 return 0;
442 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
443 return 0;
445 if (urb->status < 0) {
446 print_err_status(dev, -1, urb->status);
447 if (urb->status == -ENOENT)
448 return 0;
451 buf = dev->isoc_ctl.vid_buf;
452 if (buf != NULL)
453 outp = videobuf_to_vmalloc(&buf->vb);
455 for (i = 0; i < urb->number_of_packets; i++) {
456 int status = urb->iso_frame_desc[i].status;
458 if (status < 0) {
459 print_err_status(dev, i, status);
460 if (urb->iso_frame_desc[i].status != -EPROTO)
461 continue;
464 len = urb->iso_frame_desc[i].actual_length - 4;
466 if (urb->iso_frame_desc[i].actual_length <= 0) {
467 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
468 continue;
470 if (urb->iso_frame_desc[i].actual_length >
471 dev->max_pkt_size) {
472 em28xx_isocdbg("packet bigger than packet size");
473 continue;
476 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
478 /* FIXME: incomplete buffer checks where removed to make
479 logic simpler. Impacts of those changes should be evaluated
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;
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");
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);
500 if (buf != NULL) {
501 if (p[2] & 1)
502 buf->top_field = 0;
503 else
504 buf->top_field = 1;
507 dma_q->pos = 0;
509 if (buf != NULL)
510 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
512 return rc;
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)
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;
528 if (!dev)
529 return 0;
531 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
532 return 0;
534 if (urb->status < 0) {
535 print_err_status(dev, -1, urb->status);
536 if (urb->status == -ENOENT)
537 return 0;
540 buf = dev->isoc_ctl.vid_buf;
541 if (buf != NULL)
542 outp = videobuf_to_vmalloc(&buf->vb);
544 vbi_buf = dev->isoc_ctl.vbi_buf;
545 if (vbi_buf != NULL)
546 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
548 for (i = 0; i < urb->number_of_packets; i++) {
549 int status = urb->iso_frame_desc[i].status;
551 if (status < 0) {
552 print_err_status(dev, i, status);
553 if (urb->iso_frame_desc[i].status != -EPROTO)
554 continue;
557 len = urb->iso_frame_desc[i].actual_length - 4;
559 if (urb->iso_frame_desc[i].actual_length <= 0) {
560 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
561 continue;
563 if (urb->iso_frame_desc[i].actual_length >
564 dev->max_pkt_size) {
565 em28xx_isocdbg("packet bigger than packet size");
566 continue;
569 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
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];
581 vbi_size = dev->vbi_width * dev->vbi_height;
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);
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;
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;
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");
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);
645 if (buf != NULL) {
646 if (dev->cur_field & 1)
647 buf->top_field = 0;
648 else
649 buf->top_field = 1;
652 dma_q->pos = 0;
654 if (buf != NULL && dev->capture_type == 2)
655 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
657 return rc;
661 /* ------------------------------------------------------------------
662 Videobuf operations
663 ------------------------------------------------------------------*/
665 static int
666 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
668 struct em28xx_fh *fh = vq->priv_data;
669 struct em28xx *dev = fh->dev;
670 struct v4l2_frequency f;
672 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
673 >> 3;
675 if (0 == *count)
676 *count = EM28XX_DEF_BUF;
678 if (*count < EM28XX_MIN_BUF)
679 *count = EM28XX_MIN_BUF;
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;
686 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
688 return 0;
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)
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();
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.)
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.
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);
714 videobuf_vmalloc_free(&buf->vb);
715 buf->vb.state = VIDEOBUF_NEEDS_INIT;
718 static int
719 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
720 enum v4l2_field field)
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;
727 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
728 + 7) >> 3;
730 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
731 return -EINVAL;
733 buf->vb.width = dev->width;
734 buf->vb.height = dev->height;
735 buf->vb.field = field;
737 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
738 rc = videobuf_iolock(vq, &buf->vb, NULL);
739 if (rc < 0)
740 goto fail;
743 if (!dev->isoc_ctl.num_bufs)
744 urb_init = 1;
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;
761 buf->vb.state = VIDEOBUF_PREPARED;
762 return 0;
764 fail:
765 free_buffer(vq, buf);
766 return rc;
769 static void
770 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
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;
779 buf->vb.state = VIDEOBUF_QUEUED;
780 list_add_tail(&buf->vb.queue, &vidq->active);
784 static void buffer_release(struct videobuf_queue *vq,
785 struct videobuf_buffer *vb)
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;
793 em28xx_isocdbg("em28xx: called buffer_release\n");
795 free_buffer(vq, buf);
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,
805 /********************* v4l2 interface **************************************/
807 static void video_mux(struct em28xx *dev, int index)
809 dev->ctl_input = index;
810 dev->ctl_ainput = INPUT(index)->amux;
811 dev->ctl_aoutput = INPUT(index)->aout;
813 if (!dev->ctl_aoutput)
814 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
816 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
817 INPUT(index)->vmux, 0, 0);
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);
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);
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);
834 em28xx_audio_analog_set(dev);
837 /* Usage lock check functions */
838 static int res_get(struct em28xx_fh *fh, unsigned int bit)
840 struct em28xx *dev = fh->dev;
842 if (fh->resources & bit)
843 /* have it already allocated */
844 return 1;
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;
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;
861 static int res_check(struct em28xx_fh *fh, unsigned int bit)
863 return fh->resources & bit;
866 static int res_locked(struct em28xx *dev, unsigned int bit)
868 return dev->resources & bit;
871 static void res_free(struct em28xx_fh *fh, unsigned int bits)
873 struct em28xx *dev = fh->dev;
875 BUG_ON((fh->resources & bits) != bits);
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);
884 static int get_ressource(struct em28xx_fh *fh)
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;
898 * ac97_queryctrl()
899 * return the ac97 supported controls
901 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
903 int i;
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;
912 /* Control is not ac97 related */
913 return 1;
917 * ac97_get_ctrl()
918 * return the current values for ac97 mute and volume
920 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
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;
936 * ac97_set_ctrl()
937 * set values for ac97 mute and volume
939 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
941 int i;
943 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
944 if (ctrl->id == ac97_qctrl[i].id)
945 goto handle;
947 /* Announce that hasn't handle it */
948 return 1;
950 handle:
951 if (ctrl->value < ac97_qctrl[i].minimum ||
952 ctrl->value > ac97_qctrl[i].maximum)
953 return -ERANGE;
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;
964 return em28xx_audio_analog_set(dev);
967 static int check_dev(struct em28xx *dev)
969 if (dev->state & DEV_DISCONNECTED) {
970 em28xx_errdev("v4l2 ioctl: device not present\n");
971 return -ENODEV;
974 if (dev->state & DEV_MISCONFIGURED) {
975 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
976 "close and open it again\n");
977 return -EIO;
979 return 0;
982 static void get_scale(struct em28xx *dev,
983 unsigned int width, unsigned int height,
984 unsigned int *hscale, unsigned int *vscale)
986 unsigned int maxw = norm_maxw(dev);
987 unsigned int maxh = norm_maxh(dev);
989 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
990 if (*hscale >= 0x4000)
991 *hscale = 0x3fff;
993 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
994 if (*vscale >= 0x4000)
995 *vscale = 0x3fff;
998 /* ------------------------------------------------------------------
999 IOCTL vidioc handling
1000 ------------------------------------------------------------------*/
1002 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1003 struct v4l2_format *f)
1005 struct em28xx_fh *fh = priv;
1006 struct em28xx *dev = fh->dev;
1008 mutex_lock(&dev->lock);
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;
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;
1024 mutex_unlock(&dev->lock);
1025 return 0;
1028 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1030 unsigned int i;
1032 for (i = 0; i < ARRAY_SIZE(format); i++)
1033 if (format[i].fourcc == fourcc)
1034 return &format[i];
1036 return NULL;
1039 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1040 struct v4l2_format *f)
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;
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;
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);
1069 get_scale(dev, width, height, &hscale, &vscale);
1071 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1072 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
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;
1086 return 0;
1089 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1090 unsigned width, unsigned height)
1092 struct em28xx_fmt *fmt;
1094 fmt = format_by_fourcc(fourcc);
1095 if (!fmt)
1096 return -EINVAL;
1098 dev->format = fmt;
1099 dev->width = width;
1100 dev->height = height;
1102 /* set new image size */
1103 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1105 em28xx_set_alternate(dev);
1106 em28xx_resolution_set(dev);
1108 return 0;
1111 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1112 struct v4l2_format *f)
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1116 int rc;
1118 rc = check_dev(dev);
1119 if (rc < 0)
1120 return rc;
1122 mutex_lock(&dev->lock);
1124 vidioc_try_fmt_vid_cap(file, priv, f);
1126 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1127 em28xx_errdev("%s queue busy\n", __func__);
1128 rc = -EBUSY;
1129 goto out;
1132 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1133 f->fmt.pix.width, f->fmt.pix.height);
1135 out:
1136 mutex_unlock(&dev->lock);
1137 return rc;
1140 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1142 struct em28xx_fh *fh = priv;
1143 struct em28xx *dev = fh->dev;
1144 int rc;
1146 rc = check_dev(dev);
1147 if (rc < 0)
1148 return rc;
1150 *norm = dev->norm;
1152 return 0;
1155 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 struct v4l2_format f;
1160 int rc;
1162 rc = check_dev(dev);
1163 if (rc < 0)
1164 return rc;
1166 mutex_lock(&dev->lock);
1167 dev->norm = *norm;
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);
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);
1179 em28xx_resolution_set(dev);
1180 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1182 mutex_unlock(&dev->lock);
1183 return 0;
1186 static int vidioc_g_parm(struct file *file, void *priv,
1187 struct v4l2_streamparm *p)
1189 struct em28xx_fh *fh = priv;
1190 struct em28xx *dev = fh->dev;
1191 int rc = 0;
1193 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1194 return -EINVAL;
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);
1203 return rc;
1206 static int vidioc_s_parm(struct file *file, void *priv,
1207 struct v4l2_streamparm *p)
1209 struct em28xx_fh *fh = priv;
1210 struct em28xx *dev = fh->dev;
1212 if (!dev->board.is_webcam)
1213 return -EINVAL;
1215 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1216 return -EINVAL;
1218 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
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",
1233 static int vidioc_enum_input(struct file *file, void *priv,
1234 struct v4l2_input *i)
1236 struct em28xx_fh *fh = priv;
1237 struct em28xx *dev = fh->dev;
1238 unsigned int n;
1240 n = i->index;
1241 if (n >= MAX_EM28XX_INPUT)
1242 return -EINVAL;
1243 if (0 == INPUT(n)->type)
1244 return -EINVAL;
1246 i->index = n;
1247 i->type = V4L2_INPUT_TYPE_CAMERA;
1249 strcpy(i->name, iname[INPUT(n)->type]);
1251 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1252 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1253 i->type = V4L2_INPUT_TYPE_TUNER;
1255 i->std = dev->vdev->tvnorms;
1257 return 0;
1260 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1262 struct em28xx_fh *fh = priv;
1263 struct em28xx *dev = fh->dev;
1265 *i = dev->ctl_input;
1267 return 0;
1270 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1272 struct em28xx_fh *fh = priv;
1273 struct em28xx *dev = fh->dev;
1274 int rc;
1276 rc = check_dev(dev);
1277 if (rc < 0)
1278 return rc;
1280 if (i >= MAX_EM28XX_INPUT)
1281 return -EINVAL;
1282 if (0 == INPUT(i)->type)
1283 return -EINVAL;
1285 dev->ctl_input = i;
1287 mutex_lock(&dev->lock);
1288 video_mux(dev, dev->ctl_input);
1289 mutex_unlock(&dev->lock);
1290 return 0;
1293 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
1298 if (!dev->audio_mode.has_audio)
1299 return -EINVAL;
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;
1330 a->index = dev->ctl_ainput;
1331 a->capability = V4L2_AUDCAP_STEREO;
1333 return 0;
1336 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1338 struct em28xx_fh *fh = priv;
1339 struct em28xx *dev = fh->dev;
1342 if (!dev->audio_mode.has_audio)
1343 return -EINVAL;
1345 if (a->index >= MAX_EM28XX_INPUT)
1346 return -EINVAL;
1347 if (0 == INPUT(a->index)->type)
1348 return -EINVAL;
1350 mutex_lock(&dev->lock);
1352 dev->ctl_ainput = INPUT(a->index)->amux;
1353 dev->ctl_aoutput = INPUT(a->index)->aout;
1355 if (!dev->ctl_aoutput)
1356 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1358 mutex_unlock(&dev->lock);
1359 return 0;
1362 static int vidioc_queryctrl(struct file *file, void *priv,
1363 struct v4l2_queryctrl *qc)
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1367 int id = qc->id;
1368 int rc;
1370 rc = check_dev(dev);
1371 if (rc < 0)
1372 return rc;
1374 memset(qc, 0, sizeof(*qc));
1376 qc->id = id;
1378 /* enumberate AC97 controls */
1379 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1380 rc = ac97_queryctrl(qc);
1381 if (!rc)
1382 return 0;
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);
1390 if (qc->type)
1391 return 0;
1392 else
1393 return -EINVAL;
1396 static int vidioc_g_ctrl(struct file *file, void *priv,
1397 struct v4l2_control *ctrl)
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1401 int rc;
1403 rc = check_dev(dev);
1404 if (rc < 0)
1405 return rc;
1406 rc = 0;
1408 mutex_lock(&dev->lock);
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;
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;
1422 mutex_unlock(&dev->lock);
1423 return rc;
1426 static int vidioc_s_ctrl(struct file *file, void *priv,
1427 struct v4l2_control *ctrl)
1429 struct em28xx_fh *fh = priv;
1430 struct em28xx *dev = fh->dev;
1431 int rc;
1433 rc = check_dev(dev);
1434 if (rc < 0)
1435 return rc;
1437 mutex_lock(&dev->lock);
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;
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);
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.
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);
1466 mutex_unlock(&dev->lock);
1467 return rc;
1470 static int vidioc_g_tuner(struct file *file, void *priv,
1471 struct v4l2_tuner *t)
1473 struct em28xx_fh *fh = priv;
1474 struct em28xx *dev = fh->dev;
1475 int rc;
1477 rc = check_dev(dev);
1478 if (rc < 0)
1479 return rc;
1481 if (0 != t->index)
1482 return -EINVAL;
1484 strcpy(t->name, "Tuner");
1486 mutex_lock(&dev->lock);
1487 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1488 mutex_unlock(&dev->lock);
1490 return 0;
1493 static int vidioc_s_tuner(struct file *file, void *priv,
1494 struct v4l2_tuner *t)
1496 struct em28xx_fh *fh = priv;
1497 struct em28xx *dev = fh->dev;
1498 int rc;
1500 rc = check_dev(dev);
1501 if (rc < 0)
1502 return rc;
1504 if (0 != t->index)
1505 return -EINVAL;
1507 mutex_lock(&dev->lock);
1508 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1509 mutex_unlock(&dev->lock);
1511 return 0;
1514 static int vidioc_g_frequency(struct file *file, void *priv,
1515 struct v4l2_frequency *f)
1517 struct em28xx_fh *fh = priv;
1518 struct em28xx *dev = fh->dev;
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);
1525 return 0;
1528 static int vidioc_s_frequency(struct file *file, void *priv,
1529 struct v4l2_frequency *f)
1531 struct em28xx_fh *fh = priv;
1532 struct em28xx *dev = fh->dev;
1533 int rc;
1535 rc = check_dev(dev);
1536 if (rc < 0)
1537 return rc;
1539 if (0 != f->tuner)
1540 return -EINVAL;
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;
1547 mutex_lock(&dev->lock);
1549 dev->ctl_freq = f->frequency;
1550 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1552 mutex_unlock(&dev->lock);
1554 return 0;
1557 #ifdef CONFIG_VIDEO_ADV_DEBUG
1558 static int em28xx_reg_len(int reg)
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;
1570 static int vidioc_g_chip_ident(struct file *file, void *priv,
1571 struct v4l2_dbg_chip_ident *chip)
1573 struct em28xx_fh *fh = priv;
1574 struct em28xx *dev = fh->dev;
1576 chip->ident = V4L2_IDENT_NONE;
1577 chip->revision = 0;
1579 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1581 return 0;
1585 static int vidioc_g_register(struct file *file, void *priv,
1586 struct v4l2_dbg_register *reg)
1588 struct em28xx_fh *fh = priv;
1589 struct em28xx *dev = fh->dev;
1590 int ret;
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;
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;
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);
1622 if (ret < 0)
1623 return ret;
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;
1635 reg->val = le16_to_cpu(val);
1638 return 0;
1641 static int vidioc_s_register(struct file *file, void *priv,
1642 struct v4l2_dbg_register *reg)
1644 struct em28xx_fh *fh = priv;
1645 struct em28xx *dev = fh->dev;
1646 __le16 buf;
1647 int rc;
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);
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;
1668 /* Match host */
1669 buf = cpu_to_le16(reg->val);
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);
1676 return rc;
1678 #endif
1681 static int vidioc_cropcap(struct file *file, void *priv,
1682 struct v4l2_cropcap *cc)
1684 struct em28xx_fh *fh = priv;
1685 struct em28xx *dev = fh->dev;
1687 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1688 return -EINVAL;
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;
1698 return 0;
1701 static int vidioc_streamon(struct file *file, void *priv,
1702 enum v4l2_buf_type type)
1704 struct em28xx_fh *fh = priv;
1705 struct em28xx *dev = fh->dev;
1706 int rc = -EINVAL;
1708 rc = check_dev(dev);
1709 if (rc < 0)
1710 return rc;
1712 if (unlikely(type != fh->type))
1713 return -EINVAL;
1715 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1716 fh, type, fh->resources, dev->resources);
1718 if (unlikely(!res_get(fh, get_ressource(fh))))
1719 return -EBUSY;
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);
1726 return rc;
1729 static int vidioc_streamoff(struct file *file, void *priv,
1730 enum v4l2_buf_type type)
1732 struct em28xx_fh *fh = priv;
1733 struct em28xx *dev = fh->dev;
1734 int rc;
1736 rc = check_dev(dev);
1737 if (rc < 0)
1738 return rc;
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;
1746 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1747 fh, type, fh->resources, dev->resources);
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);
1757 return 0;
1760 static int vidioc_querycap(struct file *file, void *priv,
1761 struct v4l2_capability *cap)
1763 struct em28xx_fh *fh = priv;
1764 struct em28xx *dev = fh->dev;
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));
1770 cap->version = EM28XX_VERSION_CODE;
1772 cap->capabilities =
1773 V4L2_CAP_SLICED_VBI_CAPTURE |
1774 V4L2_CAP_VIDEO_CAPTURE |
1775 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1777 if (dev->vbi_dev)
1778 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1780 if (dev->audio_mode.has_audio)
1781 cap->capabilities |= V4L2_CAP_AUDIO;
1783 if (dev->tuner_type != TUNER_ABSENT)
1784 cap->capabilities |= V4L2_CAP_TUNER;
1786 return 0;
1789 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1790 struct v4l2_fmtdesc *f)
1792 if (unlikely(f->index >= ARRAY_SIZE(format)))
1793 return -EINVAL;
1795 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1796 f->pixelformat = format[f->index].fourcc;
1798 return 0;
1801 /* Sliced VBI ioctls */
1802 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1803 struct v4l2_format *f)
1805 struct em28xx_fh *fh = priv;
1806 struct em28xx *dev = fh->dev;
1807 int rc;
1809 rc = check_dev(dev);
1810 if (rc < 0)
1811 return rc;
1813 mutex_lock(&dev->lock);
1815 f->fmt.sliced.service_set = 0;
1816 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1818 if (f->fmt.sliced.service_set == 0)
1819 rc = -EINVAL;
1821 mutex_unlock(&dev->lock);
1823 return rc;
1826 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1827 struct v4l2_format *f)
1829 struct em28xx_fh *fh = priv;
1830 struct em28xx *dev = fh->dev;
1831 int rc;
1833 rc = check_dev(dev);
1834 if (rc < 0)
1835 return rc;
1837 mutex_lock(&dev->lock);
1838 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1839 mutex_unlock(&dev->lock);
1841 if (f->fmt.sliced.service_set == 0)
1842 return -EINVAL;
1844 return 0;
1847 /* RAW VBI ioctls */
1849 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1850 struct v4l2_format *format)
1852 struct em28xx_fh *fh = priv;
1853 struct em28xx *dev = fh->dev;
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;
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;
1874 return 0;
1877 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1878 struct v4l2_format *format)
1880 struct em28xx_fh *fh = priv;
1881 struct em28xx *dev = fh->dev;
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;
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;
1902 return 0;
1905 static int vidioc_reqbufs(struct file *file, void *priv,
1906 struct v4l2_requestbuffers *rb)
1908 struct em28xx_fh *fh = priv;
1909 struct em28xx *dev = fh->dev;
1910 int rc;
1912 rc = check_dev(dev);
1913 if (rc < 0)
1914 return rc;
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);
1922 static int vidioc_querybuf(struct file *file, void *priv,
1923 struct v4l2_buffer *b)
1925 struct em28xx_fh *fh = priv;
1926 struct em28xx *dev = fh->dev;
1927 int rc;
1929 rc = check_dev(dev);
1930 if (rc < 0)
1931 return rc;
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;
1944 return result;
1948 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1950 struct em28xx_fh *fh = priv;
1951 struct em28xx *dev = fh->dev;
1952 int rc;
1954 rc = check_dev(dev);
1955 if (rc < 0)
1956 return rc;
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);
1964 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1966 struct em28xx_fh *fh = priv;
1967 struct em28xx *dev = fh->dev;
1968 int rc;
1970 rc = check_dev(dev);
1971 if (rc < 0)
1972 return rc;
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);
1982 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1983 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1985 struct em28xx_fh *fh = priv;
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);
1992 #endif
1995 /* ----------------------------------------------------------- */
1996 /* RADIO ESPECIFIC IOCTLS */
1997 /* ----------------------------------------------------------- */
1999 static int radio_querycap(struct file *file, void *priv,
2000 struct v4l2_capability *cap)
2002 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
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));
2008 cap->version = EM28XX_VERSION_CODE;
2009 cap->capabilities = V4L2_CAP_TUNER;
2010 return 0;
2013 static int radio_g_tuner(struct file *file, void *priv,
2014 struct v4l2_tuner *t)
2016 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2018 if (unlikely(t->index > 0))
2019 return -EINVAL;
2021 strcpy(t->name, "Radio");
2022 t->type = V4L2_TUNER_RADIO;
2024 mutex_lock(&dev->lock);
2025 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
2026 mutex_unlock(&dev->lock);
2028 return 0;
2031 static int radio_enum_input(struct file *file, void *priv,
2032 struct v4l2_input *i)
2034 if (i->index != 0)
2035 return -EINVAL;
2036 strcpy(i->name, "Radio");
2037 i->type = V4L2_INPUT_TYPE_TUNER;
2039 return 0;
2042 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2044 if (unlikely(a->index))
2045 return -EINVAL;
2047 strcpy(a->name, "Radio");
2048 return 0;
2051 static int radio_s_tuner(struct file *file, void *priv,
2052 struct v4l2_tuner *t)
2054 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2056 if (0 != t->index)
2057 return -EINVAL;
2059 mutex_lock(&dev->lock);
2060 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2061 mutex_unlock(&dev->lock);
2063 return 0;
2066 static int radio_s_audio(struct file *file, void *fh,
2067 struct v4l2_audio *a)
2069 return 0;
2072 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2074 return 0;
2077 static int radio_queryctrl(struct file *file, void *priv,
2078 struct v4l2_queryctrl *qc)
2080 int i;
2082 if (qc->id < V4L2_CID_BASE ||
2083 qc->id >= V4L2_CID_LASTP1)
2084 return -EINVAL;
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;
2093 return -EINVAL;
2097 * em28xx_v4l2_open()
2098 * inits the device and starts isoc transfer
2100 static int em28xx_v4l2_open(struct file *filp)
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;
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;
2121 mutex_lock(&dev->lock);
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);
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;
2134 fh->dev = dev;
2135 fh->radio = radio;
2136 fh->type = fh_type;
2137 filp->private_data = fh;
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);
2144 /* Needed, since GPIO might have disabled power of
2145 some i2c device
2147 em28xx_wake_i2c(dev);
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);
2155 dev->users++;
2157 if (dev->progressive)
2158 field = V4L2_FIELD_NONE;
2159 else
2160 field = V4L2_FIELD_INTERLACED;
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);
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);
2173 mutex_unlock(&dev->lock);
2175 return errCode;
2179 * em28xx_realease_resources()
2180 * unregisters the v4l2,i2c and usb devices
2181 * called when the device gets disconected or at module unload
2183 void em28xx_release_analog_resources(struct em28xx *dev)
2186 /*FIXME: I2C IR should be disconnected */
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;
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;
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;
2216 * em28xx_v4l2_close()
2217 * stops streaming and deallocates all resources allocated by the v4l2
2218 * calls and ioctls
2220 static int em28xx_v4l2_close(struct file *filp)
2222 struct em28xx_fh *fh = filp->private_data;
2223 struct em28xx *dev = fh->dev;
2224 int errCode;
2226 em28xx_videodbg("users=%d\n", dev->users);
2228 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2229 videobuf_stop(&fh->vb_vidq);
2230 res_free(fh, EM28XX_RESOURCE_VIDEO);
2233 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2234 videobuf_stop(&fh->vb_vbiq);
2235 res_free(fh, EM28XX_RESOURCE_VBI);
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;
2247 /* Save some power by putting tuner to sleep */
2248 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2250 /* do this before setting alternate! */
2251 em28xx_uninit_isoc(dev);
2252 em28xx_set_mode(dev, EM28XX_SUSPEND);
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);
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;
2273 * em28xx_v4l2_read()
2274 * will allocate buffers when called for the first time
2276 static ssize_t
2277 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2278 loff_t *pos)
2280 struct em28xx_fh *fh = filp->private_data;
2281 struct em28xx *dev = fh->dev;
2282 int rc;
2284 rc = check_dev(dev);
2285 if (rc < 0)
2286 return rc;
2288 /* FIXME: read() is not prepared to allow changing the video
2289 resolution while streaming. Seems a bug at em28xx_set_fmt
2292 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2293 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2294 return -EBUSY;
2296 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2297 filp->f_flags & O_NONBLOCK);
2301 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2302 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2303 return -EBUSY;
2305 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2306 filp->f_flags & O_NONBLOCK);
2309 return 0;
2313 * em28xx_v4l2_poll()
2314 * will allocate buffers when called for the first time
2316 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2318 struct em28xx_fh *fh = filp->private_data;
2319 struct em28xx *dev = fh->dev;
2320 int rc;
2322 rc = check_dev(dev);
2323 if (rc < 0)
2324 return rc;
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;
2340 * em28xx_v4l2_mmap()
2342 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2344 struct em28xx_fh *fh = filp->private_data;
2345 struct em28xx *dev = fh->dev;
2346 int rc;
2348 rc = check_dev(dev);
2349 if (rc < 0)
2350 return rc;
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);
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);
2362 return rc;
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,
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,
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,
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
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,
2426 .tvnorms = V4L2_STD_ALL,
2427 .current_norm = V4L2_STD_PAL,
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,
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
2456 static struct video_device em28xx_radio_template = {
2457 .name = "em28xx-radio",
2458 .fops = &radio_fops,
2459 .ioctl_ops = &radio_ioctl_ops,
2462 /******************************** usb interface ******************************/
2466 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2467 const struct video_device *template,
2468 const char *type_name)
2470 struct video_device *vfd;
2472 vfd = video_device_alloc();
2473 if (NULL == vfd)
2474 return NULL;
2476 *vfd = *template;
2477 vfd->v4l2_dev = &dev->v4l2_dev;
2478 vfd->release = video_device_release;
2479 vfd->debug = video_debug;
2481 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2482 dev->name, type_name);
2484 video_set_drvdata(vfd, dev);
2485 return vfd;
2488 int em28xx_register_analog_devices(struct em28xx *dev)
2490 u8 val;
2491 int ret;
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);
2498 /* set default norm */
2499 dev->norm = em28xx_video_template.current_norm;
2500 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2501 dev->ctl_input = 0;
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));
2508 video_mux(dev, dev->ctl_input);
2510 /* Audio defaults */
2511 dev->mute = 1;
2512 dev->volume = 0x1f;
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));
2519 em28xx_set_outfmt(dev);
2520 em28xx_colorlevels_set_default(dev);
2521 em28xx_compression_disable(dev);
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;
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;
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");
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;
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;
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;
2566 em28xx_info("Registered radio device as %s\n",
2567 video_device_node_name(dev->radio_dev));
2570 em28xx_info("V4L2 video device registered as %s\n",
2571 video_device_node_name(dev->vdev));
2573 if (dev->vbi_dev)
2574 em28xx_info("V4L2 VBI device registered as %s\n",
2575 video_device_node_name(dev->vbi_dev));
2577 return 0;