2 cx231xx-video.c - driver for Conexant Cx23100/101/102 USB video capture devices
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 Based on cx23885 driver
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/bitmap.h>
29 #include <linux/usb.h>
30 #include <linux/i2c.h>
31 #include <linux/version.h>
33 #include <linux/mutex.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <media/msp3400.h>
39 #include <media/tuner.h>
41 #include "dvb_frontend.h"
44 #include "cx231xx-vbi.h"
46 #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
47 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
49 #define cx231xx_videodbg(fmt, arg...) do {\
51 printk(KERN_INFO "%s %s :"fmt, \
52 dev->name, __func__ , ##arg); } while (0)
54 static unsigned int isoc_debug;
55 module_param(isoc_debug, int, 0644);
56 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
58 #define cx231xx_isocdbg(fmt, arg...) \
61 printk(KERN_INFO "%s %s :"fmt, \
62 dev->name, __func__ , ##arg); \
66 MODULE_AUTHOR(DRIVER_AUTHOR);
67 MODULE_DESCRIPTION(DRIVER_DESC);
68 MODULE_LICENSE("GPL");
70 static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
71 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
72 static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
73 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75 module_param_array(card, int, NULL, 0444);
76 module_param_array(video_nr, int, NULL, 0444);
77 module_param_array(vbi_nr, int, NULL, 0444);
78 module_param_array(radio_nr, int, NULL, 0444);
80 MODULE_PARM_DESC(card, "card type");
81 MODULE_PARM_DESC(video_nr, "video device numbers");
82 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
83 MODULE_PARM_DESC(radio_nr, "radio device numbers");
85 static unsigned int video_debug;
86 module_param(video_debug, int, 0644);
87 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
89 /* supported video standards */
90 static struct cx231xx_fmt format[] = {
92 .name = "16bpp YUY2, 4:2:2, packed",
93 .fourcc = V4L2_PIX_FMT_YUYV,
99 /* supported controls */
100 /* Common to all boards */
102 /* ------------------------------------------------------------------- */
104 static const struct v4l2_queryctrl no_ctl = {
106 .flags = V4L2_CTRL_FLAG_DISABLED,
109 static struct cx231xx_ctrl cx231xx_ctls[] = {
113 .id = V4L2_CID_BRIGHTNESS,
114 .name = "Brightness",
118 .default_value = 0x7f,
119 .type = V4L2_CTRL_TYPE_INTEGER,
127 .id = V4L2_CID_CONTRAST,
132 .default_value = 0x3f,
133 .type = V4L2_CTRL_TYPE_INTEGER,
146 .default_value = 0x7f,
147 .type = V4L2_CTRL_TYPE_INTEGER,
154 /* strictly, this only describes only U saturation.
155 * V saturation is handled specially through code.
158 .id = V4L2_CID_SATURATION,
159 .name = "Saturation",
163 .default_value = 0x7f,
164 .type = V4L2_CTRL_TYPE_INTEGER,
173 .id = V4L2_CID_AUDIO_MUTE,
178 .type = V4L2_CTRL_TYPE_BOOLEAN,
181 .mask = (0x1f << 24),
185 .id = V4L2_CID_AUDIO_VOLUME,
190 .default_value = 0x3f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
193 .reg = PATH1_VOL_CTL,
198 static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
200 static const u32 cx231xx_user_ctrls[] = {
206 V4L2_CID_AUDIO_VOLUME,
208 V4L2_CID_AUDIO_BALANCE,
214 static const u32 *ctrl_classes[] = {
219 /* ------------------------------------------------------------------
220 Video buffer and parser functions
221 ------------------------------------------------------------------*/
224 * Announces that a buffer were filled and request the next
226 static inline void buffer_filled(struct cx231xx *dev,
227 struct cx231xx_dmaqueue *dma_q,
228 struct cx231xx_buffer *buf)
230 /* Advice that buffer was filled */
231 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
232 buf->vb.state = VIDEOBUF_DONE;
233 buf->vb.field_count++;
234 do_gettimeofday(&buf->vb.ts);
236 dev->video_mode.isoc_ctl.buf = NULL;
238 list_del(&buf->vb.queue);
239 wake_up(&buf->vb.done);
242 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
244 char *errmsg = "Unknown";
248 errmsg = "unlinked synchronuously";
251 errmsg = "unlinked asynchronuously";
254 errmsg = "Buffer error (overrun)";
257 errmsg = "Stalled (device not responding)";
260 errmsg = "Babble (bad cable?)";
263 errmsg = "Bit-stuff error (bad cable?)";
266 errmsg = "CRC/Timeout (could be anything)";
269 errmsg = "Device does not respond";
273 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
275 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
276 packet, status, errmsg);
281 * video-buf generic routine to get the next available buffer
283 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
284 struct cx231xx_buffer **buf)
286 struct cx231xx_video_mode *vmode =
287 container_of(dma_q, struct cx231xx_video_mode, vidq);
288 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
292 if (list_empty(&dma_q->active)) {
293 cx231xx_isocdbg("No active queue to serve\n");
294 dev->video_mode.isoc_ctl.buf = NULL;
299 /* Get the next buffer */
300 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
302 /* Cleans up buffer - Usefull for testing for frame/URB loss */
303 outp = videobuf_to_vmalloc(&(*buf)->vb);
304 memset(outp, 0, (*buf)->vb.size);
306 dev->video_mode.isoc_ctl.buf = *buf;
312 * Controls the isoc copy of each urb packet
314 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
316 struct cx231xx_buffer *buf;
317 struct cx231xx_dmaqueue *dma_q = urb->context;
318 unsigned char *outp = NULL;
320 unsigned char *p_buffer;
321 u32 bytes_parsed = 0, buffer_size = 0;
327 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
330 if (urb->status < 0) {
331 print_err_status(dev, -1, urb->status);
332 if (urb->status == -ENOENT)
336 buf = dev->video_mode.isoc_ctl.buf;
338 outp = videobuf_to_vmalloc(&buf->vb);
340 for (i = 0; i < urb->number_of_packets; i++) {
341 int status = urb->iso_frame_desc[i].status;
344 print_err_status(dev, i, status);
345 if (urb->iso_frame_desc[i].status != -EPROTO)
349 if (urb->iso_frame_desc[i].actual_length <= 0) {
350 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
353 if (urb->iso_frame_desc[i].actual_length >
354 dev->video_mode.max_pkt_size) {
355 cx231xx_isocdbg("packet bigger than packet size");
359 /* get buffer pointer and length */
360 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
361 buffer_size = urb->iso_frame_desc[i].actual_length;
364 if (dma_q->is_partial_line) {
365 /* Handle the case where we were working on a partial line */
366 sav_eav = dma_q->last_sav;
368 /* Check for a SAV/EAV overlapping the buffer boundary */
370 cx231xx_find_boundary_SAV_EAV(p_buffer,
376 /* Get the first line if we have some portion of an SAV/EAV from the last buffer
379 bytes_parsed += cx231xx_get_video_line(dev, dma_q, sav_eav, /* SAV/EAV */
380 p_buffer + bytes_parsed, /* p_buffer */
381 buffer_size - bytes_parsed); /* buffer size */
384 /* Now parse data that is completely in this buffer */
385 /* dma_q->is_partial_line = 0; */
387 while (bytes_parsed < buffer_size) {
390 sav_eav = cx231xx_find_next_SAV_EAV(p_buffer + bytes_parsed, /* p_buffer */
391 buffer_size - bytes_parsed, /* buffer size */
392 &bytes_used); /* Receives bytes used to get SAV/EAV */
394 bytes_parsed += bytes_used;
397 if (sav_eav && (bytes_parsed < buffer_size)) {
398 bytes_parsed += cx231xx_get_video_line(dev, dma_q, sav_eav, /* SAV/EAV */
399 p_buffer + bytes_parsed, /* p_buffer */
400 buffer_size - bytes_parsed); /* buffer size */
404 /* Save the last four bytes of the buffer so we can check the buffer boundary
405 condition next time */
406 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
413 u8 cx231xx_find_boundary_SAV_EAV(u8 * p_buffer, u8 * partial_buf,
417 u8 boundary_bytes[8];
422 /* Create an array of the last 4 bytes of the last buffer and the first
423 4 bytes of the current buffer. */
425 memcpy(boundary_bytes, partial_buf, 4);
426 memcpy(boundary_bytes + 4, p_buffer, 4);
428 /* Check for the SAV/EAV in the boundary buffer */
430 cx231xx_find_next_SAV_EAV((u8 *) & boundary_bytes, 8, &bytes_used);
433 /* found a boundary SAV/EAV. Updates the bytes used to reflect
434 only those used in the new buffer */
435 *p_bytes_used = bytes_used - 4;
441 u8 cx231xx_find_next_SAV_EAV(u8 * p_buffer, u32 buffer_size, u32 * p_bytes_used)
446 /* Don't search if the buffer size is less than 4. It causes a page fault since
447 buffer_size - 4 evaluates to a large number in that case. */
448 if (buffer_size < 4) {
449 *p_bytes_used = buffer_size;
453 for (i = 0; i < (buffer_size - 3); i++) {
455 if ((p_buffer[i] == 0xFF) &&
456 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
458 *p_bytes_used = i + 4;
459 sav_eav = p_buffer[i + 3];
464 *p_bytes_used = buffer_size;
468 u32 cx231xx_get_video_line(struct cx231xx * dev,
469 struct cx231xx_dmaqueue * dma_q, u8 sav_eav,
470 u8 * p_buffer, u32 buffer_size)
472 u32 bytes_copied = 0;
473 int current_field = -1;
476 case SAV_ACTIVE_VIDEO_FIELD1:
477 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
478 there will be no active data contained between the SAV and EAV */
479 if ((buffer_size > 3) &&
480 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00)
481 && (p_buffer[2] == 0x00)
482 && ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1)
483 || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2)
484 || (p_buffer[3] == EAV_VBLANK_FIELD1)
485 || (p_buffer[3] == EAV_VBLANK_FIELD2)
493 case SAV_ACTIVE_VIDEO_FIELD2:
494 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
495 there will be no active data contained between the SAV and EAV */
496 if ((buffer_size > 3) &&
497 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00)
498 && (p_buffer[2] == 0x00)
499 && ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1)
500 || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2)
501 || (p_buffer[3] == EAV_VBLANK_FIELD1)
502 || (p_buffer[3] == EAV_VBLANK_FIELD2)
511 dma_q->last_sav = sav_eav;
514 cx231xx_copy_video_line(dev, dma_q, p_buffer, buffer_size,
520 u32 cx231xx_copy_video_line(struct cx231xx * dev,
521 struct cx231xx_dmaqueue * dma_q, u8 * p_line,
522 u32 length, int field_number)
525 struct cx231xx_buffer *buf;
526 u32 _line_size = dev->width * 2;
528 if (dma_q->current_field != field_number) {
529 cx231xx_reset_video_buffer(dev, dma_q);
532 /* get the buffer pointer */
533 buf = dev->video_mode.isoc_ctl.buf;
535 /* Remember the field number for next time */
536 dma_q->current_field = field_number;
538 bytes_to_copy = dma_q->bytes_left_in_line;
539 if (bytes_to_copy > length)
540 bytes_to_copy = length;
542 if (dma_q->lines_completed >= dma_q->lines_per_field) {
543 dma_q->bytes_left_in_line -= bytes_to_copy;
544 dma_q->is_partial_line =
545 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
549 dma_q->is_partial_line = 1;
551 /* If we don't have a buffer, just return the number of bytes we would
552 have copied if we had a buffer. */
554 dma_q->bytes_left_in_line -= bytes_to_copy;
555 dma_q->is_partial_line =
556 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
557 return bytes_to_copy;
560 /* copy the data to video buffer */
561 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
563 dma_q->pos += bytes_to_copy;
564 dma_q->bytes_left_in_line -= bytes_to_copy;
566 if (dma_q->bytes_left_in_line == 0) {
568 dma_q->bytes_left_in_line = _line_size;
569 dma_q->lines_completed++;
570 dma_q->is_partial_line = 0;
572 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
574 buffer_filled(dev, dma_q, buf);
578 dma_q->lines_completed = 0;
582 return bytes_to_copy;
585 void cx231xx_reset_video_buffer(struct cx231xx *dev,
586 struct cx231xx_dmaqueue *dma_q)
588 struct cx231xx_buffer *buf;
590 /* handle the switch from field 1 to field 2 */
591 if (dma_q->current_field == 1) {
592 if (dma_q->lines_completed >= dma_q->lines_per_field) {
593 dma_q->field1_done = 1;
595 dma_q->field1_done = 0;
599 buf = dev->video_mode.isoc_ctl.buf;
603 /* first try to get the buffer */
604 get_next_buf(dma_q, &buf);
607 outp = videobuf_to_vmalloc(&buf->vb);
610 dma_q->field1_done = 0;
611 dma_q->current_field = -1;
614 /* reset the counters */
615 dma_q->bytes_left_in_line = dev->width << 1;
616 dma_q->lines_completed = 0;
619 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
620 u8 * p_buffer, u32 bytes_to_copy)
622 u8 *p_out_buffer = NULL;
623 u32 current_line_bytes_copied = 0;
624 struct cx231xx_buffer *buf;
625 u32 _line_size = dev->width << 1;
629 buf = dev->video_mode.isoc_ctl.buf;
634 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
636 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
638 /* Offset field 2 one line from the top of the buffer */
639 offset = (dma_q->current_field == 1) ? 0 : _line_size;
641 /* Offset for field 2 */
642 startwrite = p_out_buffer + offset;
644 /* lines already completed in the current field */
645 startwrite += (dma_q->lines_completed * _line_size * 2);
647 /* bytes already completed in the current line */
648 startwrite += current_line_bytes_copied;
651 dma_q->bytes_left_in_line >
652 bytes_to_copy ? bytes_to_copy : dma_q->bytes_left_in_line;
654 if ((u8 *) (startwrite + lencopy) >
655 (u8 *) (p_out_buffer + buf->vb.size)) {
659 /* The below copies the UYVY data straight into video buffer */
660 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
665 void cx231xx_swab(u16 * from, u16 * to, u16 len)
672 for (i = 0; i < len / 2; i++) {
673 to[i] = (from[i] << 8) | (from[i] >> 8);
677 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
679 u8 buffer_complete = 0;
681 /* Dual field stream */
683 ((dma_q->current_field == 2) &&
684 (dma_q->lines_completed >= dma_q->lines_per_field) &&
687 return buffer_complete;
690 /* ------------------------------------------------------------------
692 ------------------------------------------------------------------*/
695 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
697 struct cx231xx_fh *fh = vq->priv_data;
698 struct cx231xx *dev = fh->dev;
699 struct v4l2_frequency f;
702 (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
704 *count = CX231XX_DEF_BUF;
706 if (*count < CX231XX_MIN_BUF)
707 *count = CX231XX_MIN_BUF;
709 /* Ask tuner to go to analog mode */
710 memset(&f, 0, sizeof(f));
711 f.frequency = dev->ctl_freq;
712 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
714 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
719 /* This is called *without* dev->slock held; please keep it that way */
720 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
722 struct cx231xx_fh *fh = vq->priv_data;
723 struct cx231xx *dev = fh->dev;
724 unsigned long flags = 0;
728 /* We used to wait for the buffer to finish here, but this didn't work
729 because, as we were keeping the state as VIDEOBUF_QUEUED,
730 videobuf_queue_cancel marked it as finished for us.
731 (Also, it could wedge forever if the hardware was misconfigured.)
733 This should be safe; by the time we get here, the buffer isn't
734 queued anymore. If we ever start marking the buffers as
735 VIDEOBUF_ACTIVE, it won't be, though.
737 spin_lock_irqsave(&dev->video_mode.slock, flags);
738 if (dev->video_mode.isoc_ctl.buf == buf)
739 dev->video_mode.isoc_ctl.buf = NULL;
740 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
742 videobuf_vmalloc_free(&buf->vb);
743 buf->vb.state = VIDEOBUF_NEEDS_INIT;
747 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
748 enum v4l2_field field)
750 struct cx231xx_fh *fh = vq->priv_data;
751 struct cx231xx_buffer *buf =
752 container_of(vb, struct cx231xx_buffer, vb);
753 struct cx231xx *dev = fh->dev;
754 int rc = 0, urb_init = 0;
756 /* The only currently supported format is 16 bits/pixel */
758 (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
760 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
763 buf->vb.width = dev->width;
764 buf->vb.height = dev->height;
765 buf->vb.field = field;
767 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
768 rc = videobuf_iolock(vq, &buf->vb, NULL);
773 if (!dev->video_mode.isoc_ctl.num_bufs)
777 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
779 dev->video_mode.max_pkt_size,
785 buf->vb.state = VIDEOBUF_PREPARED;
789 free_buffer(vq, buf);
793 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
795 struct cx231xx_buffer *buf =
796 container_of(vb, struct cx231xx_buffer, vb);
797 struct cx231xx_fh *fh = vq->priv_data;
798 struct cx231xx *dev = fh->dev;
799 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
801 buf->vb.state = VIDEOBUF_QUEUED;
802 list_add_tail(&buf->vb.queue, &vidq->active);
806 static void buffer_release(struct videobuf_queue *vq,
807 struct videobuf_buffer *vb)
809 struct cx231xx_buffer *buf =
810 container_of(vb, struct cx231xx_buffer, vb);
811 struct cx231xx_fh *fh = vq->priv_data;
812 struct cx231xx *dev = (struct cx231xx *)fh->dev;
814 cx231xx_isocdbg("cx231xx: called buffer_release\n");
816 free_buffer(vq, buf);
819 static struct videobuf_queue_ops cx231xx_video_qops = {
820 .buf_setup = buffer_setup,
821 .buf_prepare = buffer_prepare,
822 .buf_queue = buffer_queue,
823 .buf_release = buffer_release,
826 /********************* v4l2 interface **************************************/
828 void video_mux(struct cx231xx *dev, int index)
831 struct v4l2_routing route;
833 route.input = INPUT(index)->vmux;
835 dev->video_input = index;
836 dev->ctl_ainput = INPUT(index)->amux;
838 cx231xx_set_video_input_mux(dev, index);
840 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_INT_S_VIDEO_ROUTING,
843 cx231xx_set_audio_input(dev, dev->ctl_ainput);
845 cx231xx_info("video_mux : %d\n", index);
847 /* do mode control overrides if required */
848 cx231xx_do_mode_ctrl_overrides(dev);
851 /* Usage lock check functions */
852 static int res_get(struct cx231xx_fh *fh)
854 struct cx231xx *dev = fh->dev;
857 /* This instance already has stream_on */
861 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
865 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
866 if (dev->vbi_stream_on)
868 dev->vbi_stream_on = 1;
877 static int res_check(struct cx231xx_fh *fh)
879 return (fh->stream_on);
882 static void res_free(struct cx231xx_fh *fh)
884 struct cx231xx *dev = fh->dev;
888 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
891 dev->vbi_stream_on = 0;
894 static int check_dev(struct cx231xx *dev)
896 if (dev->state & DEV_DISCONNECTED) {
897 cx231xx_errdev("v4l2 ioctl: device not present\n");
901 if (dev->state & DEV_MISCONFIGURED) {
902 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
903 "close and open it again\n");
909 void get_scale(struct cx231xx *dev,
910 unsigned int width, unsigned int height,
911 unsigned int *hscale, unsigned int *vscale)
913 unsigned int maxw = norm_maxw(dev);
914 unsigned int maxh = norm_maxh(dev);
916 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
917 if (*hscale >= 0x4000)
920 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
921 if (*vscale >= 0x4000)
924 dev->hscale = *hscale;
925 dev->vscale = *vscale;
929 /* ------------------------------------------------------------------
930 IOCTL vidioc handling
931 ------------------------------------------------------------------*/
933 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
934 struct v4l2_format *f)
936 struct cx231xx_fh *fh = priv;
937 struct cx231xx *dev = fh->dev;
939 mutex_lock(&dev->lock);
941 f->fmt.pix.width = dev->width;
942 f->fmt.pix.height = dev->height;
943 f->fmt.pix.pixelformat = dev->format->fourcc;;
944 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
945 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
946 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
948 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
950 mutex_unlock(&dev->lock);
954 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
958 for (i = 0; i < ARRAY_SIZE(format); i++)
959 if (format[i].fourcc == fourcc)
965 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
966 struct v4l2_format *f)
968 struct cx231xx_fh *fh = priv;
969 struct cx231xx *dev = fh->dev;
970 int width = f->fmt.pix.width;
971 int height = f->fmt.pix.height;
972 unsigned int maxw = norm_maxw(dev);
973 unsigned int maxh = norm_maxh(dev);
974 unsigned int hscale, vscale;
975 struct cx231xx_fmt *fmt;
977 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
979 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
980 f->fmt.pix.pixelformat);
984 /* width must even because of the YUYV format
985 height must be even because of interlacing */
989 if (unlikely(height < 32))
991 if (unlikely(height > maxh))
993 if (unlikely(width < 48))
995 if (unlikely(width > maxw))
998 get_scale(dev, width, height, &hscale, &vscale);
1000 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1001 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1003 f->fmt.pix.width = width;
1004 f->fmt.pix.height = height;
1005 f->fmt.pix.pixelformat = fmt->fourcc;
1006 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1007 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1008 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1009 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1014 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1015 struct v4l2_format *f)
1017 struct cx231xx_fh *fh = priv;
1018 struct cx231xx *dev = fh->dev;
1020 struct cx231xx_fmt *fmt;
1022 rc = check_dev(dev);
1026 mutex_lock(&dev->lock);
1028 vidioc_try_fmt_vid_cap(file, priv, f);
1030 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1036 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1037 cx231xx_errdev("%s queue busy\n", __func__);
1042 if (dev->stream_on && !fh->stream_on) {
1043 cx231xx_errdev("%s device in use by another fh\n", __func__);
1048 /* set new image size */
1049 dev->width = f->fmt.pix.width;
1050 dev->height = f->fmt.pix.height;
1052 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1054 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_FMT, f);
1056 /* Set the correct alternate setting for this resolution */
1057 cx231xx_resolution_set(dev);
1060 mutex_unlock(&dev->lock);
1064 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
1066 struct cx231xx_fh *fh = priv;
1067 struct cx231xx *dev = fh->dev;
1073 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1075 struct cx231xx_fh *fh = priv;
1076 struct cx231xx *dev = fh->dev;
1077 struct v4l2_format f;
1080 rc = check_dev(dev);
1084 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1086 mutex_lock(&dev->lock);
1089 /* Adjusts width/height, if needed */
1090 f.fmt.pix.width = dev->width;
1091 f.fmt.pix.height = dev->height;
1092 vidioc_try_fmt_vid_cap(file, priv, &f);
1094 /* set new image size */
1095 dev->width = f.fmt.pix.width;
1096 dev->height = f.fmt.pix.height;
1097 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1099 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_STD, &dev->norm);
1101 mutex_unlock(&dev->lock);
1103 cx231xx_resolution_set(dev);
1105 /* do mode control overrides */
1106 cx231xx_do_mode_ctrl_overrides(dev);
1111 static const char *iname[] = {
1112 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1113 [CX231XX_VMUX_SVIDEO] = "S-Video",
1114 [CX231XX_VMUX_TELEVISION] = "Television",
1115 [CX231XX_VMUX_CABLE] = "Cable TV",
1116 [CX231XX_VMUX_DVB] = "DVB",
1117 [CX231XX_VMUX_DEBUG] = "for debug only",
1120 static int vidioc_enum_input(struct file *file, void *priv,
1121 struct v4l2_input *i)
1123 struct cx231xx_fh *fh = priv;
1124 struct cx231xx *dev = fh->dev;
1128 if (n >= MAX_CX231XX_INPUT)
1130 if (0 == INPUT(n)->type)
1134 i->type = V4L2_INPUT_TYPE_CAMERA;
1136 strcpy(i->name, iname[INPUT(n)->type]);
1138 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1139 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1140 i->type = V4L2_INPUT_TYPE_TUNER;
1142 i->std = dev->vdev->tvnorms;
1147 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1149 struct cx231xx_fh *fh = priv;
1150 struct cx231xx *dev = fh->dev;
1152 *i = dev->video_input;
1157 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1159 struct cx231xx_fh *fh = priv;
1160 struct cx231xx *dev = fh->dev;
1163 rc = check_dev(dev);
1167 if (i >= MAX_CX231XX_INPUT)
1169 if (0 == INPUT(i)->type)
1172 mutex_lock(&dev->lock);
1176 mutex_unlock(&dev->lock);
1180 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1182 struct cx231xx_fh *fh = priv;
1183 struct cx231xx *dev = fh->dev;
1186 case CX231XX_AMUX_VIDEO:
1187 strcpy(a->name, "Television");
1189 case CX231XX_AMUX_LINE_IN:
1190 strcpy(a->name, "Line In");
1196 a->index = dev->ctl_ainput;
1197 a->capability = V4L2_AUDCAP_STEREO;
1202 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1204 struct cx231xx_fh *fh = priv;
1205 struct cx231xx *dev = fh->dev;
1208 /* Doesn't allow manual routing */
1209 if (a->index != dev->ctl_ainput)
1212 dev->ctl_ainput = INPUT(a->index)->amux;
1213 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1218 static int vidioc_queryctrl(struct file *file, void *priv,
1219 struct v4l2_queryctrl *qc)
1221 struct cx231xx_fh *fh = priv;
1222 struct cx231xx *dev = fh->dev;
1227 rc = check_dev(dev);
1231 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1232 if (unlikely(qc->id == 0))
1235 memset(qc, 0, sizeof(*qc));
1239 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1242 for (i = 0; i < CX231XX_CTLS; i++)
1243 if (cx231xx_ctls[i].v.id == qc->id)
1246 if (i == CX231XX_CTLS) {
1250 *qc = cx231xx_ctls[i].v;
1252 mutex_lock(&dev->lock);
1253 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_QUERYCTRL, qc);
1254 mutex_unlock(&dev->lock);
1262 static int vidioc_g_ctrl(struct file *file, void *priv,
1263 struct v4l2_control *ctrl)
1265 struct cx231xx_fh *fh = priv;
1266 struct cx231xx *dev = fh->dev;
1269 rc = check_dev(dev);
1273 mutex_lock(&dev->lock);
1275 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_CTRL, ctrl);
1277 mutex_unlock(&dev->lock);
1281 static int vidioc_s_ctrl(struct file *file, void *priv,
1282 struct v4l2_control *ctrl)
1284 struct cx231xx_fh *fh = priv;
1285 struct cx231xx *dev = fh->dev;
1288 rc = check_dev(dev);
1292 mutex_lock(&dev->lock);
1294 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_CTRL, ctrl);
1296 mutex_unlock(&dev->lock);
1300 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1302 struct cx231xx_fh *fh = priv;
1303 struct cx231xx *dev = fh->dev;
1306 rc = check_dev(dev);
1313 strcpy(t->name, "Tuner");
1315 t->type = V4L2_TUNER_ANALOG_TV;
1316 t->capability = V4L2_TUNER_CAP_NORM;
1317 t->rangehigh = 0xffffffffUL;
1318 t->signal = 0xffff; /* LOCKED */
1323 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1325 struct cx231xx_fh *fh = priv;
1326 struct cx231xx *dev = fh->dev;
1329 rc = check_dev(dev);
1336 mutex_lock(&dev->lock);
1338 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1340 mutex_unlock(&dev->lock);
1345 static int vidioc_g_frequency(struct file *file, void *priv,
1346 struct v4l2_frequency *f)
1348 struct cx231xx_fh *fh = priv;
1349 struct cx231xx *dev = fh->dev;
1351 mutex_lock(&dev->lock);
1352 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1353 f->frequency = dev->ctl_freq;
1355 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1357 mutex_unlock(&dev->lock);
1362 static int vidioc_s_frequency(struct file *file, void *priv,
1363 struct v4l2_frequency *f)
1365 struct cx231xx_fh *fh = priv;
1366 struct cx231xx *dev = fh->dev;
1369 rc = check_dev(dev);
1376 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1378 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1381 /* set pre channel change settings in DIF first */
1382 rc = cx231xx_tuner_pre_channel_change(dev);
1384 mutex_lock(&dev->lock);
1386 dev->ctl_freq = f->frequency;
1388 if (dev->tuner_type == TUNER_XC5000) {
1389 if (dev->cx231xx_set_analog_freq != NULL) {
1390 dev->cx231xx_set_analog_freq(dev, f->frequency);
1393 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY,
1397 mutex_unlock(&dev->lock);
1399 /* set post channel change settings in DIF first */
1400 rc = cx231xx_tuner_post_channel_change(dev);
1402 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1407 #ifdef CONFIG_VIDEO_ADV_DEBUG
1410 -R, --list-registers=type=<host/i2cdrv/i2caddr>,chip=<chip>[,min=<addr>,max=<addr>]
1411 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1412 -r, --set-register=type=<host/i2cdrv/i2caddr>,chip=<chip>,reg=<addr>,val=<val>
1413 set the register [VIDIOC_DBG_S_REGISTER]
1415 if type == host, then <chip> is the hosts chip ID (default 0)
1416 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1417 if type == i2caddr, then <chip> is the 7-bit I2C address
1420 static int vidioc_g_register(struct file *file, void *priv,
1421 struct v4l2_dbg_register *reg)
1423 struct cx231xx_fh *fh = priv;
1424 struct cx231xx *dev = fh->dev;
1426 u8 value[4] = { 0, 0, 0, 0 };
1429 switch (reg->match.type) {
1430 case V4L2_CHIP_MATCH_HOST:
1431 switch (reg->match.addr) {
1432 case 0: /* Cx231xx - internal registers */
1434 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1435 (u16) reg->reg, value, 4);
1437 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1440 case 1: /* Colibri - read byte */
1442 cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1443 (u16) reg->reg, 2, &data, 1);
1444 reg->val = le32_to_cpu(data & 0xff);
1446 case 14: /* Colibri - read dword */
1448 cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1449 (u16) reg->reg, 2, &data, 4);
1450 reg->val = le32_to_cpu(data);
1452 case 2: /* Hammerhead - read byte */
1454 cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1455 (u16) reg->reg, 2, &data, 1);
1456 reg->val = le32_to_cpu(data & 0xff);
1458 case 24: /* Hammerhead - read dword */
1460 cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1461 (u16) reg->reg, 2, &data, 4);
1462 reg->val = le32_to_cpu(data);
1464 case 3: /* flatiron - read byte */
1466 cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1467 (u16) reg->reg, 1, &data, 1);
1468 reg->val = le32_to_cpu(data & 0xff);
1470 case 34: /* flatiron - read dword */
1472 cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1473 (u16) reg->reg, 1, &data, 4);
1474 reg->val = le32_to_cpu(data);
1477 return ret < 0 ? ret : 0;
1479 case V4L2_CHIP_MATCH_I2C_DRIVER:
1480 cx231xx_i2c_call_clients(&dev->i2c_bus[0],
1481 VIDIOC_DBG_G_REGISTER, reg);
1483 case V4L2_CHIP_MATCH_I2C_ADDR:
1484 /* Not supported yet */
1487 if (!v4l2_chip_match_host(®->match))
1491 mutex_lock(&dev->lock);
1493 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1495 mutex_unlock(&dev->lock);
1500 static int vidioc_s_register(struct file *file, void *priv,
1501 struct v4l2_dbg_register *reg)
1503 struct cx231xx_fh *fh = priv;
1504 struct cx231xx *dev = fh->dev;
1508 u8 data[4] = { 0, 0, 0, 0 };
1510 buf = cpu_to_le64(reg->val);
1512 switch (reg->match.type) {
1513 case V4L2_CHIP_MATCH_HOST:
1515 value = (u32) buf & 0xffffffff;
1517 switch (reg->match.addr) {
1518 case 0: /* cx231xx internal registers */
1519 data[0] = (u8) value;
1520 data[1] = (u8) (value >> 8);
1521 data[2] = (u8) (value >> 16);
1522 data[3] = (u8) (value >> 24);
1524 cx231xx_write_ctrl_reg(dev,
1526 (u16) reg->reg, data,
1529 case 1: /* Colibri - read byte */
1531 cx231xx_write_i2c_data(dev,
1532 Colibri_DEVICE_ADDRESS,
1536 case 14: /* Colibri - read dword */
1538 cx231xx_write_i2c_data(dev,
1539 Colibri_DEVICE_ADDRESS,
1543 case 2: /* Hammerhead - read byte */
1545 cx231xx_write_i2c_data(dev,
1546 HAMMERHEAD_I2C_ADDRESS,
1550 case 24: /* Hammerhead - read dword */
1552 cx231xx_write_i2c_data(dev,
1553 HAMMERHEAD_I2C_ADDRESS,
1557 case 3: /* flatiron - read byte */
1559 cx231xx_write_i2c_data(dev,
1560 Flatrion_DEVICE_ADDRESS,
1564 case 34: /* flatiron - read dword */
1566 cx231xx_write_i2c_data(dev,
1567 Flatrion_DEVICE_ADDRESS,
1573 return ret < 0 ? ret : 0;
1579 mutex_lock(&dev->lock);
1581 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_S_REGISTER, reg);
1583 mutex_unlock(&dev->lock);
1589 static int vidioc_cropcap(struct file *file, void *priv,
1590 struct v4l2_cropcap *cc)
1592 struct cx231xx_fh *fh = priv;
1593 struct cx231xx *dev = fh->dev;
1595 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1598 cc->bounds.left = 0;
1600 cc->bounds.width = dev->width;
1601 cc->bounds.height = dev->height;
1602 cc->defrect = cc->bounds;
1603 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1604 cc->pixelaspect.denominator = 59;
1609 static int vidioc_streamon(struct file *file, void *priv,
1610 enum v4l2_buf_type type)
1612 struct cx231xx_fh *fh = priv;
1613 struct cx231xx *dev = fh->dev;
1616 rc = check_dev(dev);
1620 mutex_lock(&dev->lock);
1623 if (likely(rc >= 0))
1624 rc = videobuf_streamon(&fh->vb_vidq);
1626 mutex_unlock(&dev->lock);
1631 static int vidioc_streamoff(struct file *file, void *priv,
1632 enum v4l2_buf_type type)
1634 struct cx231xx_fh *fh = priv;
1635 struct cx231xx *dev = fh->dev;
1638 rc = check_dev(dev);
1642 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1643 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1645 if (type != fh->type)
1648 mutex_lock(&dev->lock);
1650 videobuf_streamoff(&fh->vb_vidq);
1653 mutex_unlock(&dev->lock);
1658 static int vidioc_querycap(struct file *file, void *priv,
1659 struct v4l2_capability *cap)
1661 struct cx231xx_fh *fh = priv;
1662 struct cx231xx *dev = fh->dev;
1664 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1665 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1666 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
1667 sizeof(cap->bus_info));
1669 cap->version = CX231XX_VERSION_CODE;
1671 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1673 V4L2_CAP_SLICED_VBI_CAPTURE |
1675 V4L2_CAP_VIDEO_CAPTURE |
1676 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1678 if (dev->tuner_type != TUNER_ABSENT)
1679 cap->capabilities |= V4L2_CAP_TUNER;
1684 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1685 struct v4l2_fmtdesc *f)
1687 if (unlikely(f->index >= ARRAY_SIZE(format)))
1690 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1691 f->pixelformat = format[f->index].fourcc;
1696 /* Sliced VBI ioctls */
1697 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1698 struct v4l2_format *f)
1700 struct cx231xx_fh *fh = priv;
1701 struct cx231xx *dev = fh->dev;
1704 rc = check_dev(dev);
1708 mutex_lock(&dev->lock);
1710 f->fmt.sliced.service_set = 0;
1712 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1714 if (f->fmt.sliced.service_set == 0)
1717 mutex_unlock(&dev->lock);
1721 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1722 struct v4l2_format *f)
1724 struct cx231xx_fh *fh = priv;
1725 struct cx231xx *dev = fh->dev;
1728 rc = check_dev(dev);
1732 mutex_lock(&dev->lock);
1733 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1734 mutex_unlock(&dev->lock);
1736 if (f->fmt.sliced.service_set == 0)
1742 /* RAW VBI ioctls */
1744 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1745 struct v4l2_format *f)
1747 struct cx231xx_fh *fh = priv;
1748 struct cx231xx *dev = fh->dev;
1750 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1751 35468950 : 28636363;
1752 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1753 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1754 f->fmt.vbi.offset = 64 * 4;
1755 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1756 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1757 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1758 PAL_VBI_LINES : NTSC_VBI_LINES;
1759 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1760 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1761 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1767 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1768 struct v4l2_format *f)
1770 struct cx231xx_fh *fh = priv;
1771 struct cx231xx *dev = fh->dev;
1773 if (dev->vbi_stream_on && !fh->stream_on) {
1774 cx231xx_errdev("%s device in use by another fh\n", __func__);
1778 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1779 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1780 35468950 : 28636363;
1781 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1782 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1783 f->fmt.vbi.offset = 244;
1784 f->fmt.vbi.flags = 0;
1785 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1786 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1787 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1788 PAL_VBI_LINES : NTSC_VBI_LINES;
1789 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1790 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1791 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1797 static int vidioc_reqbufs(struct file *file, void *priv,
1798 struct v4l2_requestbuffers *rb)
1800 struct cx231xx_fh *fh = priv;
1801 struct cx231xx *dev = fh->dev;
1804 rc = check_dev(dev);
1808 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1811 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1813 struct cx231xx_fh *fh = priv;
1814 struct cx231xx *dev = fh->dev;
1817 rc = check_dev(dev);
1821 return (videobuf_querybuf(&fh->vb_vidq, b));
1824 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1826 struct cx231xx_fh *fh = priv;
1827 struct cx231xx *dev = fh->dev;
1830 rc = check_dev(dev);
1834 return (videobuf_qbuf(&fh->vb_vidq, b));
1837 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1839 struct cx231xx_fh *fh = priv;
1840 struct cx231xx *dev = fh->dev;
1843 rc = check_dev(dev);
1847 return (videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK));
1850 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1851 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1853 struct cx231xx_fh *fh = priv;
1855 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1859 /* ----------------------------------------------------------- */
1860 /* RADIO ESPECIFIC IOCTLS */
1861 /* ----------------------------------------------------------- */
1863 static int radio_querycap(struct file *file, void *priv,
1864 struct v4l2_capability *cap)
1866 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1868 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1869 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1870 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1872 cap->version = CX231XX_VERSION_CODE;
1873 cap->capabilities = V4L2_CAP_TUNER;
1877 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1879 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1881 if (unlikely(t->index > 0))
1884 strcpy(t->name, "Radio");
1885 t->type = V4L2_TUNER_RADIO;
1887 mutex_lock(&dev->lock);
1888 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
1889 mutex_unlock(&dev->lock);
1894 static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1898 strcpy(i->name, "Radio");
1899 i->type = V4L2_INPUT_TYPE_TUNER;
1904 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1906 if (unlikely(a->index))
1909 strcpy(a->name, "Radio");
1913 static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1915 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1920 mutex_lock(&dev->lock);
1921 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1922 mutex_unlock(&dev->lock);
1927 static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1932 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1937 static int radio_queryctrl(struct file *file, void *priv,
1938 struct v4l2_queryctrl *c)
1942 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
1944 if (c->id == V4L2_CID_AUDIO_MUTE) {
1945 for (i = 0; i < CX231XX_CTLS; i++)
1946 if (cx231xx_ctls[i].v.id == c->id)
1948 *c = cx231xx_ctls[i].v;
1955 * cx231xx_v4l2_open()
1956 * inits the device and starts isoc transfer
1958 static int cx231xx_v4l2_open(struct file *filp)
1960 int minor = video_devdata(filp)->minor;
1961 int errCode = 0, radio = 0;
1962 struct cx231xx *dev = NULL;
1963 struct cx231xx_fh *fh;
1964 enum v4l2_buf_type fh_type = 0;
1966 dev = cx231xx_get_device(minor, &fh_type, &radio);
1970 mutex_lock(&dev->lock);
1972 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
1973 minor, v4l2_type_names[fh_type], dev->users);
1976 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1979 ("Device locked on digital mode. Can't open analog\n");
1980 mutex_unlock(&dev->lock);
1985 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1987 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1988 mutex_unlock(&dev->lock);
1994 filp->private_data = fh;
1996 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1997 dev->width = norm_maxw(dev);
1998 dev->height = norm_maxh(dev);
2002 /* Power up in Analog TV mode */
2003 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
2006 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2008 cx231xx_resolution_set(dev);
2010 /* set video alternate setting */
2011 cx231xx_set_video_alternate(dev);
2013 /* Needed, since GPIO might have disabled power of
2015 cx231xx_config_i2c(dev);
2017 /* device needs to be initialized before isoc transfer */
2018 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
2019 video_mux(dev, dev->video_input);
2023 cx231xx_videodbg("video_open: setting radio device\n");
2025 /* cx231xx_start_radio(dev); */
2027 cx231xx_i2c_call_clients(&dev->i2c_bus[1], AUDC_SET_RADIO,
2033 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2034 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops, NULL, &dev->video_mode.slock, fh->type, V4L2_FIELD_INTERLACED, /* V4L2_FIELD_SEQ_TB, */
2035 sizeof(struct cx231xx_buffer), fh);
2038 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2040 /* Set the required alternate setting VBI interface works in Bulk mode only */
2041 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2043 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops, NULL, &dev->vbi_mode.slock, fh->type, V4L2_FIELD_SEQ_TB, /* V4L2_FIELD_INTERLACED, */
2044 sizeof(struct cx231xx_buffer), fh);
2047 mutex_unlock(&dev->lock);
2053 * cx231xx_realease_resources()
2054 * unregisters the v4l2,i2c and usb devices
2055 * called when the device gets disconected or at module unload
2057 void cx231xx_release_analog_resources(struct cx231xx *dev)
2060 /*FIXME: I2C IR should be disconnected */
2062 if (dev->radio_dev) {
2063 if (-1 != dev->radio_dev->minor)
2064 video_unregister_device(dev->radio_dev);
2066 video_device_release(dev->radio_dev);
2067 dev->radio_dev = NULL;
2070 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
2072 if (-1 != dev->vbi_dev->minor)
2073 video_unregister_device(dev->vbi_dev);
2075 video_device_release(dev->vbi_dev);
2076 dev->vbi_dev = NULL;
2079 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
2081 if (-1 != dev->vdev->minor)
2082 video_unregister_device(dev->vdev);
2084 video_device_release(dev->vdev);
2090 * cx231xx_v4l2_close()
2091 * stops streaming and deallocates all resources allocated by the v4l2
2094 static int cx231xx_v4l2_close(struct file *filp)
2096 struct cx231xx_fh *fh = filp->private_data;
2097 struct cx231xx *dev = fh->dev;
2099 cx231xx_videodbg("users=%d\n", dev->users);
2101 mutex_lock(&dev->lock);
2106 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2107 videobuf_stop(&fh->vb_vidq);
2108 videobuf_mmap_free(&fh->vb_vidq);
2110 /* the device is already disconnect,
2111 free the remaining resources */
2112 if (dev->state & DEV_DISCONNECTED) {
2113 cx231xx_release_resources(dev);
2114 mutex_unlock(&dev->lock);
2119 /* do this before setting alternate! */
2120 cx231xx_uninit_vbi_isoc(dev);
2122 /* set alternate 0 */
2123 if (!dev->vbi_or_sliced_cc_mode) {
2124 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2126 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2131 wake_up_interruptible_nr(&dev->open, 1);
2132 mutex_unlock(&dev->lock);
2136 if (dev->users == 1) {
2137 videobuf_stop(&fh->vb_vidq);
2138 videobuf_mmap_free(&fh->vb_vidq);
2140 /* the device is already disconnect,
2141 free the remaining resources */
2142 if (dev->state & DEV_DISCONNECTED) {
2143 cx231xx_release_resources(dev);
2144 mutex_unlock(&dev->lock);
2149 /* Save some power by putting tuner to sleep */
2150 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_STANDBY,
2153 /* do this before setting alternate! */
2154 cx231xx_uninit_isoc(dev);
2155 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2157 /* set alternate 0 */
2158 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2162 wake_up_interruptible_nr(&dev->open, 1);
2163 mutex_unlock(&dev->lock);
2168 * cx231xx_v4l2_read()
2169 * will allocate buffers when called for the first time
2172 cx231xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
2175 struct cx231xx_fh *fh = filp->private_data;
2176 struct cx231xx *dev = fh->dev;
2179 rc = check_dev(dev);
2183 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2184 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2185 mutex_lock(&dev->lock);
2187 mutex_unlock(&dev->lock);
2189 if (unlikely(rc < 0))
2192 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2193 filp->f_flags & O_NONBLOCK);
2199 * cx231xx_v4l2_poll()
2200 * will allocate buffers when called for the first time
2202 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2204 struct cx231xx_fh *fh = filp->private_data;
2205 struct cx231xx *dev = fh->dev;
2208 rc = check_dev(dev);
2212 mutex_lock(&dev->lock);
2214 mutex_unlock(&dev->lock);
2216 if (unlikely(rc < 0))
2219 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2220 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2221 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2227 * cx231xx_v4l2_mmap()
2229 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2231 struct cx231xx_fh *fh = filp->private_data;
2232 struct cx231xx *dev = fh->dev;
2235 rc = check_dev(dev);
2239 mutex_lock(&dev->lock);
2241 mutex_unlock(&dev->lock);
2243 if (unlikely(rc < 0))
2246 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2248 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2249 (unsigned long)vma->vm_start,
2250 (unsigned long)vma->vm_end -
2251 (unsigned long)vma->vm_start, rc);
2256 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2257 .owner = THIS_MODULE,
2258 .open = cx231xx_v4l2_open,
2259 .release = cx231xx_v4l2_close,
2260 .read = cx231xx_v4l2_read,
2261 .poll = cx231xx_v4l2_poll,
2262 .mmap = cx231xx_v4l2_mmap,
2263 .ioctl = video_ioctl2,
2266 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2267 .vidioc_querycap = vidioc_querycap,
2268 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2269 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2270 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2271 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2272 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2273 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2274 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2275 .vidioc_g_audio = vidioc_g_audio,
2276 .vidioc_s_audio = vidioc_s_audio,
2277 .vidioc_cropcap = vidioc_cropcap,
2278 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2279 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2280 .vidioc_reqbufs = vidioc_reqbufs,
2281 .vidioc_querybuf = vidioc_querybuf,
2282 .vidioc_qbuf = vidioc_qbuf,
2283 .vidioc_dqbuf = vidioc_dqbuf,
2284 .vidioc_s_std = vidioc_s_std,
2285 .vidioc_g_std = vidioc_g_std,
2286 .vidioc_enum_input = vidioc_enum_input,
2287 .vidioc_g_input = vidioc_g_input,
2288 .vidioc_s_input = vidioc_s_input,
2289 .vidioc_queryctrl = vidioc_queryctrl,
2290 .vidioc_g_ctrl = vidioc_g_ctrl,
2291 .vidioc_s_ctrl = vidioc_s_ctrl,
2292 .vidioc_streamon = vidioc_streamon,
2293 .vidioc_streamoff = vidioc_streamoff,
2294 .vidioc_g_tuner = vidioc_g_tuner,
2295 .vidioc_s_tuner = vidioc_s_tuner,
2296 .vidioc_g_frequency = vidioc_g_frequency,
2297 .vidioc_s_frequency = vidioc_s_frequency,
2298 #ifdef CONFIG_VIDEO_ADV_DEBUG
2299 .vidioc_g_register = vidioc_g_register,
2300 .vidioc_s_register = vidioc_s_register,
2302 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2303 .vidiocgmbuf = vidiocgmbuf,
2307 static struct video_device cx231xx_vbi_template;
2309 static const struct video_device cx231xx_video_template = {
2310 .fops = &cx231xx_v4l_fops,
2311 .release = video_device_release,
2312 .ioctl_ops = &video_ioctl_ops,
2314 .tvnorms = V4L2_STD_ALL,
2315 .current_norm = V4L2_STD_PAL,
2318 static const struct v4l2_file_operations radio_fops = {
2319 .owner = THIS_MODULE,
2320 .open = cx231xx_v4l2_open,
2321 .release = cx231xx_v4l2_close,
2322 .ioctl = video_ioctl2,
2325 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2326 .vidioc_querycap = radio_querycap,
2327 .vidioc_g_tuner = radio_g_tuner,
2328 .vidioc_enum_input = radio_enum_input,
2329 .vidioc_g_audio = radio_g_audio,
2330 .vidioc_s_tuner = radio_s_tuner,
2331 .vidioc_s_audio = radio_s_audio,
2332 .vidioc_s_input = radio_s_input,
2333 .vidioc_queryctrl = radio_queryctrl,
2334 .vidioc_g_ctrl = vidioc_g_ctrl,
2335 .vidioc_s_ctrl = vidioc_s_ctrl,
2336 .vidioc_g_frequency = vidioc_g_frequency,
2337 .vidioc_s_frequency = vidioc_s_frequency,
2338 #ifdef CONFIG_VIDEO_ADV_DEBUG
2339 .vidioc_g_register = vidioc_g_register,
2340 .vidioc_s_register = vidioc_s_register,
2344 static struct video_device cx231xx_radio_template = {
2345 .name = "cx231xx-radio",
2346 .fops = &radio_fops,
2347 .ioctl_ops = &radio_ioctl_ops,
2351 /******************************** usb interface ******************************/
2353 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev, const struct video_device
2354 *template, const char *type_name)
2356 struct video_device *vfd;
2358 vfd = video_device_alloc();
2363 vfd->parent = &dev->udev->dev;
2364 vfd->release = video_device_release;
2365 vfd->debug = video_debug;
2367 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2372 int cx231xx_register_analog_devices(struct cx231xx *dev)
2376 cx231xx_info("%s()\n", __func__);
2378 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2380 (CX231XX_VERSION_CODE >> 16) & 0xff,
2381 (CX231XX_VERSION_CODE >> 8) & 0xff,
2382 CX231XX_VERSION_CODE & 0xff);
2384 /* set default norm */
2385 /*dev->norm = cx231xx_video_template.current_norm; */
2386 dev->width = norm_maxw(dev);
2387 dev->height = norm_maxh(dev);
2388 dev->interlaced = 0;
2392 /* Analog specific initialization */
2393 dev->format = &format[0];
2394 /* video_mux(dev, dev->video_input); */
2396 /* Audio defaults */
2400 /* enable vbi capturing */
2401 /* write code here... */
2403 /* allocate and fill video video_device struct */
2404 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2406 cx231xx_errdev("cannot allocate video_device.\n");
2410 /* register v4l2 video video_device */
2411 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2412 video_nr[dev->devno]);
2414 cx231xx_errdev("unable to register video device (error=%i).\n",
2419 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2420 dev->name, dev->vdev->num);
2422 /* Initialize VBI template */
2423 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2424 sizeof(cx231xx_vbi_template));
2425 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2427 /* Allocate and fill vbi video_device struct */
2428 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2430 /* register v4l2 vbi video_device */
2431 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2432 vbi_nr[dev->devno]);
2434 cx231xx_errdev("unable to register vbi device\n");
2438 cx231xx_info("%s/0: registered device vbi%d\n",
2439 dev->name, dev->vbi_dev->num);
2441 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2443 cx231xx_vdev_init(dev, &cx231xx_radio_template, "radio");
2444 if (!dev->radio_dev) {
2445 cx231xx_errdev("cannot allocate video_device.\n");
2448 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2449 radio_nr[dev->devno]);
2451 cx231xx_errdev("can't register radio device\n");
2454 cx231xx_info("Registered radio device as /dev/radio%d\n",
2455 dev->radio_dev->num);
2458 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2459 dev->vdev->num, dev->vbi_dev->num);