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.
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/bitmap.h>
30 #include <linux/usb.h>
31 #include <linux/i2c.h>
32 #include <linux/version.h>
34 #include <linux/mutex.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/msp3400.h>
40 #include <media/tuner.h>
42 #include "dvb_frontend.h"
45 #include "cx231xx-vbi.h"
48 #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
49 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
52 #define cx231xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0)
57 static unsigned int isoc_debug;
58 module_param(isoc_debug, int, 0644);
59 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61 #define cx231xx_isocdbg(fmt, arg...) \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); \
69 MODULE_AUTHOR(DRIVER_AUTHOR);
70 MODULE_DESCRIPTION(DRIVER_DESC);
71 MODULE_LICENSE("GPL");
75 static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
80 module_param_array(card, int, NULL, 0444);
81 module_param_array(video_nr, int, NULL, 0444);
82 module_param_array(vbi_nr, int, NULL, 0444);
83 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(card, "card type");
86 MODULE_PARM_DESC(video_nr, "video device numbers");
87 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88 MODULE_PARM_DESC(radio_nr, "radio device numbers");
90 static unsigned int video_debug;
91 module_param(video_debug, int, 0644);
92 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
96 /* supported video standards */
97 static struct cx231xx_fmt format[] = {
99 .name = "16bpp YUY2, 4:2:2, packed",
100 .fourcc = V4L2_PIX_FMT_YUYV,
107 /* supported controls */
108 /* Common to all boards */
110 /* ------------------------------------------------------------------- */
112 static const struct v4l2_queryctrl no_ctl = {
114 .flags = V4L2_CTRL_FLAG_DISABLED,
117 static struct cx231xx_ctrl cx231xx_ctls[] = {
121 .id = V4L2_CID_BRIGHTNESS,
122 .name = "Brightness",
126 .default_value = 0x7f,
127 .type = V4L2_CTRL_TYPE_INTEGER,
135 .id = V4L2_CID_CONTRAST,
140 .default_value = 0x3f,
141 .type = V4L2_CTRL_TYPE_INTEGER,
154 .default_value = 0x7f,
155 .type = V4L2_CTRL_TYPE_INTEGER,
162 /* strictly, this only describes only U saturation.
163 * V saturation is handled specially through code.
166 .id = V4L2_CID_SATURATION,
167 .name = "Saturation",
171 .default_value = 0x7f,
172 .type = V4L2_CTRL_TYPE_INTEGER,
181 .id = V4L2_CID_AUDIO_MUTE,
186 .type = V4L2_CTRL_TYPE_BOOLEAN,
189 .mask = (0x1f << 24),
193 .id = V4L2_CID_AUDIO_VOLUME,
198 .default_value = 0x3f,
199 .type = V4L2_CTRL_TYPE_INTEGER,
201 .reg = PATH1_VOL_CTL,
206 static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
208 static const u32 cx231xx_user_ctrls[] = {
214 V4L2_CID_AUDIO_VOLUME,
216 V4L2_CID_AUDIO_BALANCE,
222 static const u32 *ctrl_classes[] = {
228 /* ------------------------------------------------------------------
229 Video buffer and parser functions
230 ------------------------------------------------------------------*/
233 * Announces that a buffer were filled and request the next
235 static inline void buffer_filled(struct cx231xx *dev,
236 struct cx231xx_dmaqueue *dma_q,
237 struct cx231xx_buffer *buf)
239 /* Advice that buffer was filled */
240 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
241 buf->vb.state = VIDEOBUF_DONE;
242 buf->vb.field_count++;
243 do_gettimeofday(&buf->vb.ts);
245 dev->video_mode.isoc_ctl.buf = NULL;
247 list_del(&buf->vb.queue);
248 wake_up(&buf->vb.done);
252 static inline void print_err_status(struct cx231xx *dev,
253 int packet, int status)
255 char *errmsg = "Unknown";
259 errmsg = "unlinked synchronuously";
262 errmsg = "unlinked asynchronuously";
265 errmsg = "Buffer error (overrun)";
268 errmsg = "Stalled (device not responding)";
271 errmsg = "Babble (bad cable?)";
274 errmsg = "Bit-stuff error (bad cable?)";
277 errmsg = "CRC/Timeout (could be anything)";
280 errmsg = "Device does not respond";
284 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
286 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
287 packet, status, errmsg);
292 * video-buf generic routine to get the next available buffer
294 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
295 struct cx231xx_buffer **buf)
297 struct cx231xx_video_mode *vmode = container_of(dma_q, struct cx231xx_video_mode, vidq);
298 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
303 if (list_empty(&dma_q->active)) {
304 cx231xx_isocdbg("No active queue to serve\n");
305 dev->video_mode.isoc_ctl.buf = NULL;
310 /* Get the next buffer */
311 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
313 /* Cleans up buffer - Usefull for testing for frame/URB loss */
314 outp = videobuf_to_vmalloc(&(*buf)->vb);
315 memset(outp, 0, (*buf)->vb.size);
317 dev->video_mode.isoc_ctl.buf = *buf;
323 * Controls the isoc copy of each urb packet
325 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
327 struct cx231xx_buffer *buf;
328 struct cx231xx_dmaqueue *dma_q = urb->context;
329 unsigned char *outp = NULL;
331 unsigned char *p_buffer;
332 u32 bytes_parsed = 0, buffer_size = 0;
338 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
341 if (urb->status < 0) {
342 print_err_status(dev, -1, urb->status);
343 if (urb->status == -ENOENT)
347 buf = dev->video_mode.isoc_ctl.buf;
349 outp = videobuf_to_vmalloc(&buf->vb);
351 for (i = 0; i < urb->number_of_packets; i++) {
352 int status = urb->iso_frame_desc[i].status;
355 print_err_status(dev, i, status);
356 if (urb->iso_frame_desc[i].status != -EPROTO)
360 if (urb->iso_frame_desc[i].actual_length <= 0) {
361 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
364 if (urb->iso_frame_desc[i].actual_length >
365 dev->video_mode.max_pkt_size) {
366 cx231xx_isocdbg("packet bigger than packet size");
370 /* get buffer pointer and length */
371 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
372 buffer_size = urb->iso_frame_desc[i].actual_length;
375 if(dma_q->is_partial_line)
377 /* Handle the case where we were working on a partial line */
378 sav_eav = dma_q->last_sav;
380 /* Check for a SAV/EAV overlapping the buffer boundary */
381 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer, dma_q->partial_buf, &bytes_parsed);
385 /* Get the first line if we have some portion of an SAV/EAV from the last buffer
388 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
389 sav_eav, /* SAV/EAV */
390 p_buffer + bytes_parsed, /* p_buffer */
391 buffer_size - bytes_parsed); /* buffer size */
394 /* Now parse data that is completely in this buffer */
395 /* dma_q->is_partial_line = 0; */
397 while(bytes_parsed < buffer_size)
401 sav_eav = cx231xx_find_next_SAV_EAV(
402 p_buffer + bytes_parsed, /* p_buffer */
403 buffer_size - bytes_parsed, /* buffer size */
404 &bytes_used); /* Receives bytes used to get SAV/EAV */
406 bytes_parsed += bytes_used;
409 if(sav_eav && (bytes_parsed < buffer_size))
411 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
412 sav_eav, /* SAV/EAV */
413 p_buffer + bytes_parsed, /* p_buffer */
414 buffer_size - bytes_parsed); /* buffer size */
418 /* Save the last four bytes of the buffer so we can check the buffer boundary
419 condition next time */
420 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
427 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf, u32 *p_bytes_used)
430 u8 boundary_bytes[8];
435 /* Create an array of the last 4 bytes of the last buffer and the first
436 4 bytes of the current buffer. */
438 memcpy(boundary_bytes, partial_buf, 4);
439 memcpy(boundary_bytes + 4, p_buffer, 4);
441 /* Check for the SAV/EAV in the boundary buffer */
442 sav_eav = cx231xx_find_next_SAV_EAV((u8*)&boundary_bytes, 8, &bytes_used);
445 /* found a boundary SAV/EAV. Updates the bytes used to reflect
446 only those used in the new buffer */
447 *p_bytes_used = bytes_used - 4;
453 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
458 /* Don't search if the buffer size is less than 4. It causes a page fault since
459 buffer_size - 4 evaluates to a large number in that case. */
460 if(buffer_size < 4) {
461 *p_bytes_used = buffer_size;
465 for(i = 0;i < (buffer_size - 3); i++) {
467 if((p_buffer[i] == 0xFF) &&
468 (p_buffer[i+1] == 0x00) &&
469 (p_buffer[i+2] == 0x00)) {
472 sav_eav = p_buffer[i+3];
477 *p_bytes_used = buffer_size;
484 u32 cx231xx_get_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
485 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
487 u32 bytes_copied = 0;
488 int current_field = -1;
492 case SAV_ACTIVE_VIDEO_FIELD1:
493 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
494 there will be no active data contained between the SAV and EAV */
495 if ( (buffer_size > 3) &&
496 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
497 ( (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
498 (p_buffer[3] == EAV_VBLANK_FIELD1) || (p_buffer[3] == EAV_VBLANK_FIELD2)
507 case SAV_ACTIVE_VIDEO_FIELD2:
508 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
509 there will be no active data contained between the SAV and EAV */
510 if ( (buffer_size > 3) &&
511 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
512 ( (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
513 (p_buffer[3] == EAV_VBLANK_FIELD1) || (p_buffer[3] == EAV_VBLANK_FIELD2)
523 dma_q->last_sav = sav_eav;
525 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer, buffer_size, current_field);
530 u32 cx231xx_copy_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
531 u8 *p_line, u32 length, int field_number)
534 struct cx231xx_buffer *buf;
535 u32 _line_size = dev->width * 2;
537 if( dma_q->current_field != field_number ) {
538 cx231xx_reset_video_buffer(dev, dma_q);
541 /* get the buffer pointer */
542 buf = dev->video_mode.isoc_ctl.buf;
544 /* Remember the field number for next time */
545 dma_q->current_field = field_number;
547 bytes_to_copy = dma_q->bytes_left_in_line;
548 if(bytes_to_copy > length)
549 bytes_to_copy = length;
552 if(dma_q->lines_completed >= dma_q->lines_per_field) {
553 dma_q->bytes_left_in_line -= bytes_to_copy;
554 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
558 dma_q->is_partial_line = 1;
560 /* If we don't have a buffer, just return the number of bytes we would
561 have copied if we had a buffer. */
564 dma_q->bytes_left_in_line -= bytes_to_copy;
565 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
566 return bytes_to_copy;
569 /* copy the data to video buffer */
570 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
572 dma_q->pos += bytes_to_copy;
573 dma_q->bytes_left_in_line -= bytes_to_copy;
575 if(dma_q->bytes_left_in_line == 0) {
577 dma_q->bytes_left_in_line = _line_size;
578 dma_q->lines_completed++;
579 dma_q->is_partial_line = 0;
581 if(cx231xx_is_buffer_done(dev, dma_q) && buf) {
583 buffer_filled(dev, dma_q, buf);
587 dma_q->lines_completed = 0;
591 return bytes_to_copy;
594 void cx231xx_reset_video_buffer(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
596 struct cx231xx_buffer *buf;
598 /* handle the switch from field 1 to field 2 */
599 if(dma_q->current_field == 1) {
600 if(dma_q->lines_completed >= dma_q->lines_per_field ) {
601 dma_q->field1_done = 1;
603 dma_q->field1_done = 0;
607 buf = dev->video_mode.isoc_ctl.buf;
611 /* first try to get the buffer */
612 get_next_buf(dma_q, &buf);
615 outp = videobuf_to_vmalloc(&buf->vb);
618 dma_q->field1_done = 0;
619 dma_q->current_field = -1;
622 /* reset the counters */
623 dma_q->bytes_left_in_line = dev->width << 1;
624 dma_q->lines_completed = 0;
627 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
628 u8 *p_buffer, u32 bytes_to_copy)
630 u8 *p_out_buffer = NULL;
631 u32 current_line_bytes_copied = 0;
632 struct cx231xx_buffer *buf;
633 u32 _line_size = dev->width << 1;
637 buf = dev->video_mode.isoc_ctl.buf;
642 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
644 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
646 /* Offset field 2 one line from the top of the buffer */
647 offset = (dma_q->current_field == 1)? 0: _line_size;
649 /* Offset for field 2 */
650 startwrite = p_out_buffer + offset;
652 /* lines already completed in the current field */
653 startwrite += (dma_q->lines_completed * _line_size * 2);
655 /* bytes already completed in the current line */
656 startwrite += current_line_bytes_copied;
658 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ? bytes_to_copy : dma_q->bytes_left_in_line;
660 if( (u8*)(startwrite +lencopy) > (u8*)(p_out_buffer+ buf->vb.size) ) {
664 /* The below copies the UYVY data straight into video buffer */
665 cx231xx_swab( (u16*)p_buffer, (u16*)startwrite, (u16)lencopy);
670 void cx231xx_swab(u16 *from, u16 *to, u16 len)
677 for(i = 0; i < len/2; i++) {
678 to[i] = (from[i] << 8) | (from[i] >> 8);
682 u8 cx231xx_is_buffer_done(struct cx231xx *dev,struct cx231xx_dmaqueue *dma_q)
684 u8 buffer_complete = 0;
686 /* Dual field stream */
688 ((dma_q->current_field == 2) &&
689 (dma_q->lines_completed >= dma_q->lines_per_field) &&
692 return buffer_complete;
696 /* ------------------------------------------------------------------
698 ------------------------------------------------------------------*/
701 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
703 struct cx231xx_fh *fh = vq->priv_data;
704 struct cx231xx *dev = fh->dev;
705 struct v4l2_frequency f;
707 *size = ( fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
709 *count = CX231XX_DEF_BUF;
711 if (*count < CX231XX_MIN_BUF)
712 *count = CX231XX_MIN_BUF;
714 /* Ask tuner to go to analog mode */
715 memset(&f, 0, sizeof(f));
716 f.frequency = dev->ctl_freq;
717 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
719 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
724 /* This is called *without* dev->slock held; please keep it that way */
725 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
727 struct cx231xx_fh *fh = vq->priv_data;
728 struct cx231xx *dev = fh->dev;
729 unsigned long flags = 0;
733 /* We used to wait for the buffer to finish here, but this didn't work
734 because, as we were keeping the state as VIDEOBUF_QUEUED,
735 videobuf_queue_cancel marked it as finished for us.
736 (Also, it could wedge forever if the hardware was misconfigured.)
738 This should be safe; by the time we get here, the buffer isn't
739 queued anymore. If we ever start marking the buffers as
740 VIDEOBUF_ACTIVE, it won't be, though.
742 spin_lock_irqsave(&dev->video_mode.slock, flags);
743 if (dev->video_mode.isoc_ctl.buf == buf)
744 dev->video_mode.isoc_ctl.buf = NULL;
745 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
747 videobuf_vmalloc_free(&buf->vb);
748 buf->vb.state = VIDEOBUF_NEEDS_INIT;
752 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
753 enum v4l2_field field)
755 struct cx231xx_fh *fh = vq->priv_data;
756 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
757 struct cx231xx *dev = fh->dev;
758 int rc = 0, urb_init = 0;
760 /* The only currently supported format is 16 bits/pixel */
761 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
763 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
766 buf->vb.width = dev->width;
767 buf->vb.height = dev->height;
768 buf->vb.field = field;
770 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
771 rc = videobuf_iolock(vq, &buf->vb, NULL);
776 if (!dev->video_mode.isoc_ctl.num_bufs)
780 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
781 CX231XX_NUM_BUFS, dev->video_mode.max_pkt_size,
787 buf->vb.state = VIDEOBUF_PREPARED;
791 free_buffer(vq, buf);
796 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
798 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
799 struct cx231xx_fh *fh = vq->priv_data;
800 struct cx231xx *dev = fh->dev;
801 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
803 buf->vb.state = VIDEOBUF_QUEUED;
804 list_add_tail(&buf->vb.queue, &vidq->active);
808 static void buffer_release(struct videobuf_queue *vq,
809 struct videobuf_buffer *vb)
811 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
812 struct cx231xx_fh *fh = vq->priv_data;
813 struct cx231xx *dev = (struct cx231xx *)fh->dev;
815 cx231xx_isocdbg("cx231xx: called buffer_release\n");
817 free_buffer(vq, buf);
820 static struct videobuf_queue_ops cx231xx_video_qops = {
821 .buf_setup = buffer_setup,
822 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue,
824 .buf_release = buffer_release,
827 /********************* v4l2 interface **************************************/
830 void video_mux(struct cx231xx *dev, int index)
833 struct v4l2_routing route;
835 route.input = INPUT(index)->vmux;
837 dev->video_input = index;
838 dev->ctl_ainput = INPUT(index)->amux;
840 cx231xx_set_video_input_mux(dev,index);
842 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_INT_S_VIDEO_ROUTING, &route);
844 cx231xx_set_audio_input(dev, dev->ctl_ainput );
846 cx231xx_info("video_mux : %d\n", index);
848 /* do mode control overrides if required */
849 cx231xx_do_mode_ctrl_overrides(dev);
852 /* Usage lock check functions */
853 static int res_get(struct cx231xx_fh *fh)
855 struct cx231xx *dev = fh->dev;
858 /* This instance already has stream_on */
862 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
866 } else if(fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
867 if (dev->vbi_stream_on)
869 dev->vbi_stream_on = 1;
878 static int res_check(struct cx231xx_fh *fh)
880 return (fh->stream_on);
883 static void res_free(struct cx231xx_fh *fh)
885 struct cx231xx *dev = fh->dev;
889 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 if(fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
892 dev->vbi_stream_on = 0;
895 static int check_dev(struct cx231xx *dev)
897 if (dev->state & DEV_DISCONNECTED) {
898 cx231xx_errdev("v4l2 ioctl: device not present\n");
902 if (dev->state & DEV_MISCONFIGURED) {
903 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
904 "close and open it again\n");
910 void get_scale(struct cx231xx *dev,
911 unsigned int width, unsigned int height,
912 unsigned int *hscale, unsigned int *vscale)
914 unsigned int maxw = norm_maxw(dev);
915 unsigned int maxh = norm_maxh(dev);
917 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
918 if (*hscale >= 0x4000)
921 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
922 if (*vscale >= 0x4000)
925 dev->hscale = *hscale;
926 dev->vscale = *vscale;
930 /* ------------------------------------------------------------------
931 IOCTL vidioc handling
932 ------------------------------------------------------------------*/
934 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
935 struct v4l2_format *f)
937 struct cx231xx_fh *fh = priv;
938 struct cx231xx *dev = fh->dev;
940 mutex_lock(&dev->lock);
942 f->fmt.pix.width = dev->width;
943 f->fmt.pix.height = dev->height;
944 f->fmt.pix.pixelformat = dev->format->fourcc;;
945 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
946 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
947 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
949 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
951 mutex_unlock(&dev->lock);
955 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
959 for (i = 0; i < ARRAY_SIZE(format); i++)
960 if (format[i].fourcc == fourcc)
966 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
967 struct v4l2_format *f)
969 struct cx231xx_fh *fh = priv;
970 struct cx231xx *dev = fh->dev;
971 int width = f->fmt.pix.width;
972 int height = f->fmt.pix.height;
973 unsigned int maxw = norm_maxw(dev);
974 unsigned int maxh = norm_maxh(dev);
975 unsigned int hscale, vscale;
976 struct cx231xx_fmt *fmt;
978 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
980 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
981 f->fmt.pix.pixelformat);
985 /* width must even because of the YUYV format
986 height must be even because of interlacing */
990 if (unlikely(height < 32))
992 if (unlikely(height > maxh))
994 if (unlikely(width < 48))
996 if (unlikely(width > maxw))
999 get_scale(dev, width, height, &hscale, &vscale);
1001 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1002 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1004 f->fmt.pix.width = width;
1005 f->fmt.pix.height = height;
1006 f->fmt.pix.pixelformat = fmt->fourcc;
1007 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1008 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1009 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1010 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1015 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1016 struct v4l2_format *f)
1018 struct cx231xx_fh *fh = priv;
1019 struct cx231xx *dev = fh->dev;
1021 struct cx231xx_fmt *fmt;
1023 rc = check_dev(dev);
1028 mutex_lock(&dev->lock);
1030 vidioc_try_fmt_vid_cap(file, priv, f);
1032 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1038 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1039 cx231xx_errdev("%s queue busy\n", __func__);
1044 if (dev->stream_on && !fh->stream_on) {
1045 cx231xx_errdev("%s device in use by another fh\n", __func__);
1050 /* set new image size */
1051 dev->width = f->fmt.pix.width;
1052 dev->height = f->fmt.pix.height;
1054 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1056 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_FMT, f);
1058 /* Set the correct alternate setting for this resolution */
1059 cx231xx_resolution_set(dev);
1062 mutex_unlock(&dev->lock);
1066 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1068 struct cx231xx_fh *fh = priv;
1069 struct cx231xx *dev = fh->dev;
1075 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1077 struct cx231xx_fh *fh = priv;
1078 struct cx231xx *dev = fh->dev;
1079 struct v4l2_format f;
1082 rc = check_dev(dev);
1086 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1088 mutex_lock(&dev->lock);
1092 /* Adjusts width/height, if needed */
1093 f.fmt.pix.width = dev->width;
1094 f.fmt.pix.height = dev->height;
1095 vidioc_try_fmt_vid_cap(file, priv, &f);
1097 /* set new image size */
1098 dev->width = f.fmt.pix.width;
1099 dev->height = f.fmt.pix.height;
1100 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1102 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_STD, &dev->norm);
1104 mutex_unlock(&dev->lock);
1106 cx231xx_resolution_set(dev);
1108 /* do mode control overrides */
1109 cx231xx_do_mode_ctrl_overrides(dev);
1114 static const char *iname[] = {
1115 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1116 [CX231XX_VMUX_SVIDEO] = "S-Video",
1117 [CX231XX_VMUX_TELEVISION] = "Television",
1118 [CX231XX_VMUX_CABLE] = "Cable TV",
1119 [CX231XX_VMUX_DVB] = "DVB",
1120 [CX231XX_VMUX_DEBUG] = "for debug only",
1123 static int vidioc_enum_input(struct file *file, void *priv,
1124 struct v4l2_input *i)
1126 struct cx231xx_fh *fh = priv;
1127 struct cx231xx *dev = fh->dev;
1131 if (n >= MAX_CX231XX_INPUT)
1133 if (0 == INPUT(n)->type)
1137 i->type = V4L2_INPUT_TYPE_CAMERA;
1139 strcpy(i->name, iname[INPUT(n)->type]);
1141 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1142 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1143 i->type = V4L2_INPUT_TYPE_TUNER;
1145 i->std = dev->vdev->tvnorms;
1150 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1152 struct cx231xx_fh *fh = priv;
1153 struct cx231xx *dev = fh->dev;
1155 *i = dev->video_input;
1160 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1162 struct cx231xx_fh *fh = priv;
1163 struct cx231xx *dev = fh->dev;
1166 rc = check_dev(dev);
1170 if (i >= MAX_CX231XX_INPUT)
1172 if (0 == INPUT(i)->type)
1175 mutex_lock(&dev->lock);
1179 mutex_unlock(&dev->lock);
1183 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1185 struct cx231xx_fh *fh = priv;
1186 struct cx231xx *dev = fh->dev;
1189 case CX231XX_AMUX_VIDEO:
1190 strcpy(a->name, "Television");
1192 case CX231XX_AMUX_LINE_IN:
1193 strcpy(a->name, "Line In");
1199 a->index = dev->ctl_ainput;
1200 a->capability = V4L2_AUDCAP_STEREO;
1205 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1207 struct cx231xx_fh *fh = priv;
1208 struct cx231xx *dev = fh->dev;
1212 /* Doesn't allow manual routing */
1213 if (a->index != dev->ctl_ainput)
1216 dev->ctl_ainput = INPUT(a->index)->amux;
1217 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1222 static int vidioc_queryctrl(struct file *file, void *priv,
1223 struct v4l2_queryctrl *qc)
1225 struct cx231xx_fh *fh = priv;
1226 struct cx231xx *dev = fh->dev;
1231 rc = check_dev(dev);
1235 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1236 if (unlikely(qc->id == 0))
1239 memset(qc, 0, sizeof(*qc));
1243 if (qc->id < V4L2_CID_BASE ||
1244 qc->id >= V4L2_CID_LASTP1)
1247 for (i = 0; i < CX231XX_CTLS; i++)
1248 if (cx231xx_ctls[i].v.id == qc->id)
1251 if (i == CX231XX_CTLS) {
1255 *qc = cx231xx_ctls[i].v;
1257 mutex_lock(&dev->lock);
1258 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_QUERYCTRL, qc);
1259 mutex_unlock(&dev->lock);
1267 static int vidioc_g_ctrl(struct file *file, void *priv,
1268 struct v4l2_control *ctrl)
1270 struct cx231xx_fh *fh = priv;
1271 struct cx231xx *dev = fh->dev;
1274 rc = check_dev(dev);
1278 mutex_lock(&dev->lock);
1280 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_CTRL, ctrl);
1282 mutex_unlock(&dev->lock);
1286 static int vidioc_s_ctrl(struct file *file, void *priv,
1287 struct v4l2_control *ctrl)
1289 struct cx231xx_fh *fh = priv;
1290 struct cx231xx *dev = fh->dev;
1293 rc = check_dev(dev);
1297 mutex_lock(&dev->lock);
1299 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_CTRL, ctrl);
1301 mutex_unlock(&dev->lock);
1305 static int vidioc_g_tuner(struct file *file, void *priv,
1306 struct v4l2_tuner *t)
1308 struct cx231xx_fh *fh = priv;
1309 struct cx231xx *dev = fh->dev;
1312 rc = check_dev(dev);
1319 strcpy(t->name, "Tuner");
1321 t->type = V4L2_TUNER_ANALOG_TV;
1322 t->capability = V4L2_TUNER_CAP_NORM;
1323 t->rangehigh = 0xffffffffUL;
1324 t->signal = 0xffff ; /* LOCKED */
1329 static int vidioc_s_tuner(struct file *file, void *priv,
1330 struct v4l2_tuner *t)
1332 struct cx231xx_fh *fh = priv;
1333 struct cx231xx *dev = fh->dev;
1336 rc = check_dev(dev);
1343 mutex_lock(&dev->lock);
1345 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1347 mutex_unlock(&dev->lock);
1352 static int vidioc_g_frequency(struct file *file, void *priv,
1353 struct v4l2_frequency *f)
1355 struct cx231xx_fh *fh = priv;
1356 struct cx231xx *dev = fh->dev;
1358 mutex_lock(&dev->lock);
1359 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1360 f->frequency = dev->ctl_freq;
1362 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1364 mutex_unlock(&dev->lock);
1369 static int vidioc_s_frequency(struct file *file, void *priv,
1370 struct v4l2_frequency *f)
1372 struct cx231xx_fh *fh = priv;
1373 struct cx231xx *dev = fh->dev;
1376 rc = check_dev(dev);
1383 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1385 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1388 /* set pre channel change settings in DIF first */
1389 rc = cx231xx_tuner_pre_channel_change(dev);
1391 mutex_lock(&dev->lock);
1393 dev->ctl_freq = f->frequency;
1395 if(dev->tuner_type == TUNER_XC5000) {
1396 if( dev->cx231xx_set_analog_freq != NULL ) {
1397 dev->cx231xx_set_analog_freq(dev, f->frequency );
1400 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1403 mutex_unlock(&dev->lock);
1405 /* set post channel change settings in DIF first */
1406 rc = cx231xx_tuner_post_channel_change(dev);
1408 cx231xx_info("Set New FREQUENCY to %d\n",f->frequency);
1413 #ifdef CONFIG_VIDEO_ADV_DEBUG
1417 -R, --list-registers=type=<host/i2cdrv/i2caddr>,chip=<chip>[,min=<addr>,max=<addr>]
1418 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1419 -r, --set-register=type=<host/i2cdrv/i2caddr>,chip=<chip>,reg=<addr>,val=<val>
1420 set the register [VIDIOC_DBG_S_REGISTER]
1422 if type == host, then <chip> is the hosts chip ID (default 0)
1423 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1424 if type == i2caddr, then <chip> is the 7-bit I2C address
1428 static int vidioc_g_register(struct file *file, void *priv,
1429 struct v4l2_dbg_register *reg)
1431 struct cx231xx_fh *fh = priv;
1432 struct cx231xx *dev = fh->dev;
1434 u8 value[4] ={0,0,0,0};
1437 switch (reg->match.type) {
1438 case V4L2_CHIP_MATCH_HOST:
1439 switch(reg->match.addr) {
1440 case 0: /* Cx231xx - internal registers */
1441 ret = cx231xx_read_ctrl_reg(dev,VRT_GET_REGISTER, (u16) reg->reg, value, 4);
1442 reg->val = value[0] | value[1] << 8 | value[2] << 16 | value[3] << 24;
1444 case 1: /* Colibri - read byte */
1445 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, &data, 1);
1446 reg->val = le32_to_cpu(data & 0xff);
1448 case 14: /* Colibri - read dword */
1449 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, &data, 4);
1450 reg->val = le32_to_cpu(data);
1452 case 2: /* Hammerhead - read byte */
1453 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, &data, 1);
1454 reg->val = le32_to_cpu(data & 0xff);
1456 case 24: /* Hammerhead - read dword */
1457 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, &data, 4);
1458 reg->val = le32_to_cpu(data);
1460 case 3: /* flatiron - read byte */
1461 ret = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, &data, 1);
1462 reg->val = le32_to_cpu(data & 0xff);
1464 case 34: /* flatiron - read dword */
1465 ret = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, &data, 4);
1466 reg->val = le32_to_cpu(data);
1469 return ret < 0?ret:0;
1471 case V4L2_CHIP_MATCH_I2C_DRIVER:
1472 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1474 case V4L2_CHIP_MATCH_I2C_ADDR:
1475 /* Not supported yet */
1478 if (!v4l2_chip_match_host(®->match))
1483 mutex_lock(&dev->lock);
1485 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1487 mutex_unlock(&dev->lock);
1492 static int vidioc_s_register(struct file *file, void *priv,
1493 struct v4l2_dbg_register *reg)
1495 struct cx231xx_fh *fh = priv;
1496 struct cx231xx *dev = fh->dev;
1500 u8 data[4] ={0,0,0,0};
1502 buf = cpu_to_le64(reg->val);
1504 switch (reg->match.type) {
1505 case V4L2_CHIP_MATCH_HOST:
1507 value = (u32) buf & 0xffffffff;
1509 switch(reg->match.addr) {
1510 case 0: /* cx231xx internal registers */
1512 data[1]=(u8)(value>>8);
1513 data[2]=(u8)(value>>16);
1514 data[3]=(u8)(value>>24);
1515 ret = cx231xx_write_ctrl_reg(dev,VRT_SET_REGISTER, (u16) reg->reg, data, 4);
1517 case 1: /* Colibri - read byte */
1518 ret = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, value, 1);
1520 case 14: /* Colibri - read dword */
1521 ret = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, value, 4);
1523 case 2: /* Hammerhead - read byte */
1524 ret = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, value, 1);
1526 case 24: /* Hammerhead - read dword */
1527 ret = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, value, 4);
1529 case 3: /* flatiron - read byte */
1530 ret = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, value, 1);
1532 case 34: /* flatiron - read dword */
1533 ret = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, value, 4);
1537 return ret < 0?ret:0;
1543 mutex_lock(&dev->lock);
1545 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_S_REGISTER, reg);
1547 mutex_unlock(&dev->lock);
1554 static int vidioc_cropcap(struct file *file, void *priv,
1555 struct v4l2_cropcap *cc)
1557 struct cx231xx_fh *fh = priv;
1558 struct cx231xx *dev = fh->dev;
1560 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1563 cc->bounds.left = 0;
1565 cc->bounds.width = dev->width;
1566 cc->bounds.height = dev->height;
1567 cc->defrect = cc->bounds;
1568 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1569 cc->pixelaspect.denominator = 59;
1574 static int vidioc_streamon(struct file *file, void *priv,
1575 enum v4l2_buf_type type)
1577 struct cx231xx_fh *fh = priv;
1578 struct cx231xx *dev = fh->dev;
1581 rc = check_dev(dev);
1585 mutex_lock(&dev->lock);
1588 if (likely(rc >= 0))
1589 rc = videobuf_streamon(&fh->vb_vidq);
1591 mutex_unlock(&dev->lock);
1596 static int vidioc_streamoff(struct file *file, void *priv,
1597 enum v4l2_buf_type type)
1599 struct cx231xx_fh *fh = priv;
1600 struct cx231xx *dev = fh->dev;
1603 rc = check_dev(dev);
1607 if ( (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1608 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) )
1610 if (type != fh->type)
1613 mutex_lock(&dev->lock);
1615 videobuf_streamoff(&fh->vb_vidq);
1618 mutex_unlock(&dev->lock);
1623 static int vidioc_querycap(struct file *file, void *priv,
1624 struct v4l2_capability *cap)
1626 struct cx231xx_fh *fh = priv;
1627 struct cx231xx *dev = fh->dev;
1629 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1630 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1631 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1633 cap->version = CX231XX_VERSION_CODE;
1636 V4L2_CAP_VBI_CAPTURE |
1638 V4L2_CAP_SLICED_VBI_CAPTURE |
1640 V4L2_CAP_VIDEO_CAPTURE |
1642 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1644 if (dev->tuner_type != TUNER_ABSENT)
1645 cap->capabilities |= V4L2_CAP_TUNER;
1650 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1651 struct v4l2_fmtdesc *f)
1653 if (unlikely(f->index >= ARRAY_SIZE(format)))
1656 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1657 f->pixelformat = format[f->index].fourcc;
1662 /* Sliced VBI ioctls */
1663 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1664 struct v4l2_format *f)
1666 struct cx231xx_fh *fh = priv;
1667 struct cx231xx *dev = fh->dev;
1670 rc = check_dev(dev);
1674 mutex_lock(&dev->lock);
1676 f->fmt.sliced.service_set = 0;
1678 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1680 if (f->fmt.sliced.service_set == 0)
1683 mutex_unlock(&dev->lock);
1687 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1688 struct v4l2_format *f)
1690 struct cx231xx_fh *fh = priv;
1691 struct cx231xx *dev = fh->dev;
1694 rc = check_dev(dev);
1698 mutex_lock(&dev->lock);
1699 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1700 mutex_unlock(&dev->lock);
1702 if (f->fmt.sliced.service_set == 0)
1709 /* RAW VBI ioctls */
1711 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1712 struct v4l2_format *f)
1714 struct cx231xx_fh *fh = priv;
1715 struct cx231xx *dev = fh->dev;
1717 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1719 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1720 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1721 f->fmt.vbi.offset = 64 * 4;
1722 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1723 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1724 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1725 PAL_VBI_LINES : NTSC_VBI_LINES;
1726 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1727 PAL_VBI_START_LINE+312 : NTSC_VBI_START_LINE + 263;
1728 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1734 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1735 struct v4l2_format *f)
1737 struct cx231xx_fh *fh = priv;
1738 struct cx231xx *dev = fh->dev;
1740 if (dev->vbi_stream_on && !fh->stream_on) {
1741 cx231xx_errdev("%s device in use by another fh\n", __func__);
1745 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1746 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1748 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1749 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1750 f->fmt.vbi.offset = 244;
1751 f->fmt.vbi.flags = 0;
1752 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1753 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1754 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1755 PAL_VBI_LINES : NTSC_VBI_LINES;
1756 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1757 PAL_VBI_START_LINE+312 : NTSC_VBI_START_LINE + 263;
1758 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1764 static int vidioc_reqbufs(struct file *file, void *priv,
1765 struct v4l2_requestbuffers *rb)
1767 struct cx231xx_fh *fh = priv;
1768 struct cx231xx *dev = fh->dev;
1771 rc = check_dev(dev);
1775 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1778 static int vidioc_querybuf(struct file *file, void *priv,
1779 struct v4l2_buffer *b)
1781 struct cx231xx_fh *fh = priv;
1782 struct cx231xx *dev = fh->dev;
1785 rc = check_dev(dev);
1789 return (videobuf_querybuf(&fh->vb_vidq, b));
1792 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1794 struct cx231xx_fh *fh = priv;
1795 struct cx231xx *dev = fh->dev;
1798 rc = check_dev(dev);
1802 return (videobuf_qbuf(&fh->vb_vidq, b));
1805 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1807 struct cx231xx_fh *fh = priv;
1808 struct cx231xx *dev = fh->dev;
1811 rc = check_dev(dev);
1815 return (videobuf_dqbuf(&fh->vb_vidq, b,
1816 file->f_flags & O_NONBLOCK));
1819 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1820 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1822 struct cx231xx_fh *fh = priv;
1824 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1829 /* ----------------------------------------------------------- */
1830 /* RADIO ESPECIFIC IOCTLS */
1831 /* ----------------------------------------------------------- */
1833 static int radio_querycap(struct file *file, void *priv,
1834 struct v4l2_capability *cap)
1836 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1838 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1839 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1840 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1842 cap->version = CX231XX_VERSION_CODE;
1843 cap->capabilities = V4L2_CAP_TUNER;
1847 static int radio_g_tuner(struct file *file, void *priv,
1848 struct v4l2_tuner *t)
1850 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1852 if (unlikely(t->index > 0))
1855 strcpy(t->name, "Radio");
1856 t->type = V4L2_TUNER_RADIO;
1858 mutex_lock(&dev->lock);
1859 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
1860 mutex_unlock(&dev->lock);
1865 static int radio_enum_input(struct file *file, void *priv,
1866 struct v4l2_input *i)
1870 strcpy(i->name, "Radio");
1871 i->type = V4L2_INPUT_TYPE_TUNER;
1876 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1878 if (unlikely(a->index))
1881 strcpy(a->name, "Radio");
1885 static int radio_s_tuner(struct file *file, void *priv,
1886 struct v4l2_tuner *t)
1888 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1893 mutex_lock(&dev->lock);
1894 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1895 mutex_unlock(&dev->lock);
1900 static int radio_s_audio(struct file *file, void *fh,
1901 struct v4l2_audio *a)
1906 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1911 static int radio_queryctrl(struct file *file, void *priv,
1912 struct v4l2_queryctrl *c)
1916 if (c->id < V4L2_CID_BASE ||
1917 c->id >= V4L2_CID_LASTP1)
1919 if (c->id == V4L2_CID_AUDIO_MUTE) {
1920 for (i = 0; i < CX231XX_CTLS; i++)
1921 if (cx231xx_ctls[i].v.id == c->id)
1923 *c = cx231xx_ctls[i].v;
1930 * cx231xx_v4l2_open()
1931 * inits the device and starts isoc transfer
1933 static int cx231xx_v4l2_open(struct file *filp)
1935 int minor = video_devdata(filp)->minor;
1936 int errCode = 0, radio = 0;
1937 struct cx231xx *dev = NULL;
1938 struct cx231xx_fh *fh;
1939 enum v4l2_buf_type fh_type = 0;
1941 dev = cx231xx_get_device(minor, &fh_type, &radio);
1945 mutex_lock(&dev->lock);
1947 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
1948 minor, v4l2_type_names[fh_type], dev->users);
1951 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1953 cx231xx_errdev("Device locked on digital mode. Can't open analog\n");
1954 mutex_unlock(&dev->lock);
1959 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1961 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1962 mutex_unlock(&dev->lock);
1968 filp->private_data = fh;
1970 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1971 dev->width = norm_maxw(dev);
1972 dev->height = norm_maxh(dev);
1977 /* Power up in Analog TV mode */
1978 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1981 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1983 cx231xx_resolution_set(dev);
1985 /* set video alternate setting */
1986 cx231xx_set_video_alternate(dev);
1988 /* Needed, since GPIO might have disabled power of
1990 cx231xx_config_i2c(dev);
1992 /* device needs to be initialized before isoc transfer */
1993 dev->video_input = dev->video_input > 2 ? 2: dev->video_input;
1994 video_mux(dev, dev->video_input );
1998 cx231xx_videodbg("video_open: setting radio device\n");
2000 /* cx231xx_start_radio(dev); */
2002 cx231xx_i2c_call_clients(&dev->i2c_bus[1], AUDC_SET_RADIO, NULL);
2007 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2008 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2009 NULL, &dev->video_mode.slock, fh->type, V4L2_FIELD_INTERLACED, /* V4L2_FIELD_SEQ_TB, */
2010 sizeof(struct cx231xx_buffer), fh);
2013 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2015 /* Set the required alternate setting VBI interface works in Bulk mode only */
2016 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2018 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2019 NULL, &dev->vbi_mode.slock, fh->type, V4L2_FIELD_SEQ_TB, /* V4L2_FIELD_INTERLACED, */
2020 sizeof(struct cx231xx_buffer), fh);
2023 mutex_unlock(&dev->lock);
2029 * cx231xx_realease_resources()
2030 * unregisters the v4l2,i2c and usb devices
2031 * called when the device gets disconected or at module unload
2033 void cx231xx_release_analog_resources(struct cx231xx *dev)
2036 /*FIXME: I2C IR should be disconnected */
2038 if (dev->radio_dev) {
2039 if (-1 != dev->radio_dev->minor)
2040 video_unregister_device(dev->radio_dev);
2042 video_device_release(dev->radio_dev);
2043 dev->radio_dev = NULL;
2046 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
2048 if (-1 != dev->vbi_dev->minor)
2049 video_unregister_device(dev->vbi_dev);
2051 video_device_release(dev->vbi_dev);
2052 dev->vbi_dev = NULL;
2055 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
2057 if (-1 != dev->vdev->minor)
2058 video_unregister_device(dev->vdev);
2060 video_device_release(dev->vdev);
2066 * cx231xx_v4l2_close()
2067 * stops streaming and deallocates all resources allocated by the v4l2
2070 static int cx231xx_v4l2_close(struct file *filp)
2072 struct cx231xx_fh *fh = filp->private_data;
2073 struct cx231xx *dev = fh->dev;
2075 cx231xx_videodbg("users=%d\n", dev->users);
2077 mutex_lock(&dev->lock);
2082 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2083 videobuf_stop(&fh->vb_vidq);
2084 videobuf_mmap_free(&fh->vb_vidq);
2086 /* the device is already disconnect,
2087 free the remaining resources */
2088 if (dev->state & DEV_DISCONNECTED) {
2089 cx231xx_release_resources(dev);
2090 mutex_unlock(&dev->lock);
2095 /* do this before setting alternate! */
2096 cx231xx_uninit_vbi_isoc(dev);
2098 /* set alternate 0 */
2099 if( !dev->vbi_or_sliced_cc_mode) {
2100 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2102 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2107 wake_up_interruptible_nr(&dev->open, 1);
2108 mutex_unlock(&dev->lock);
2112 if (dev->users == 1) {
2113 videobuf_stop(&fh->vb_vidq);
2114 videobuf_mmap_free(&fh->vb_vidq);
2116 /* the device is already disconnect,
2117 free the remaining resources */
2118 if (dev->state & DEV_DISCONNECTED) {
2119 cx231xx_release_resources(dev);
2120 mutex_unlock(&dev->lock);
2125 /* Save some power by putting tuner to sleep */
2126 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_STANDBY, NULL);
2128 /* do this before setting alternate! */
2129 cx231xx_uninit_isoc(dev);
2130 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2132 /* set alternate 0 */
2133 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2137 wake_up_interruptible_nr(&dev->open, 1);
2138 mutex_unlock(&dev->lock);
2143 * cx231xx_v4l2_read()
2144 * will allocate buffers when called for the first time
2147 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2150 struct cx231xx_fh *fh = filp->private_data;
2151 struct cx231xx *dev = fh->dev;
2154 rc = check_dev(dev);
2158 if ( (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2159 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) ) {
2160 mutex_lock(&dev->lock);
2162 mutex_unlock(&dev->lock);
2164 if (unlikely(rc < 0))
2167 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2168 filp->f_flags & O_NONBLOCK);
2174 * cx231xx_v4l2_poll()
2175 * will allocate buffers when called for the first time
2177 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2179 struct cx231xx_fh *fh = filp->private_data;
2180 struct cx231xx *dev = fh->dev;
2183 rc = check_dev(dev);
2187 mutex_lock(&dev->lock);
2189 mutex_unlock(&dev->lock);
2191 if (unlikely(rc < 0))
2194 if ( (V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2195 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) )
2196 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2202 * cx231xx_v4l2_mmap()
2204 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2206 struct cx231xx_fh *fh = filp->private_data;
2207 struct cx231xx *dev = fh->dev;
2210 rc = check_dev(dev);
2214 mutex_lock(&dev->lock);
2216 mutex_unlock(&dev->lock);
2218 if (unlikely(rc < 0))
2221 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2223 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2224 (unsigned long)vma->vm_start,
2225 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2231 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2232 .owner = THIS_MODULE,
2233 .open = cx231xx_v4l2_open,
2234 .release = cx231xx_v4l2_close,
2235 .read = cx231xx_v4l2_read,
2236 .poll = cx231xx_v4l2_poll,
2237 .mmap = cx231xx_v4l2_mmap,
2238 .ioctl = video_ioctl2,
2241 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2242 .vidioc_querycap = vidioc_querycap,
2243 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2244 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2245 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2246 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2247 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2248 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2249 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2250 .vidioc_g_audio = vidioc_g_audio,
2251 .vidioc_s_audio = vidioc_s_audio,
2252 .vidioc_cropcap = vidioc_cropcap,
2253 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2254 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2255 .vidioc_reqbufs = vidioc_reqbufs,
2256 .vidioc_querybuf = vidioc_querybuf,
2257 .vidioc_qbuf = vidioc_qbuf,
2258 .vidioc_dqbuf = vidioc_dqbuf,
2259 .vidioc_s_std = vidioc_s_std,
2260 .vidioc_g_std = vidioc_g_std,
2261 .vidioc_enum_input = vidioc_enum_input,
2262 .vidioc_g_input = vidioc_g_input,
2263 .vidioc_s_input = vidioc_s_input,
2264 .vidioc_queryctrl = vidioc_queryctrl,
2265 .vidioc_g_ctrl = vidioc_g_ctrl,
2266 .vidioc_s_ctrl = vidioc_s_ctrl,
2267 .vidioc_streamon = vidioc_streamon,
2268 .vidioc_streamoff = vidioc_streamoff,
2269 .vidioc_g_tuner = vidioc_g_tuner,
2270 .vidioc_s_tuner = vidioc_s_tuner,
2271 .vidioc_g_frequency = vidioc_g_frequency,
2272 .vidioc_s_frequency = vidioc_s_frequency,
2273 #ifdef CONFIG_VIDEO_ADV_DEBUG
2274 .vidioc_g_register = vidioc_g_register,
2275 .vidioc_s_register = vidioc_s_register,
2277 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2278 .vidiocgmbuf = vidiocgmbuf,
2282 static struct video_device cx231xx_vbi_template;
2284 static const struct video_device cx231xx_video_template = {
2285 .fops = &cx231xx_v4l_fops,
2286 .release = video_device_release,
2287 .ioctl_ops = &video_ioctl_ops,
2289 .tvnorms = V4L2_STD_ALL,
2290 .current_norm = V4L2_STD_PAL,
2293 static const struct v4l2_file_operations radio_fops = {
2294 .owner = THIS_MODULE,
2295 .open = cx231xx_v4l2_open,
2296 .release = cx231xx_v4l2_close,
2297 .ioctl = video_ioctl2,
2300 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2301 .vidioc_querycap = radio_querycap,
2302 .vidioc_g_tuner = radio_g_tuner,
2303 .vidioc_enum_input = radio_enum_input,
2304 .vidioc_g_audio = radio_g_audio,
2305 .vidioc_s_tuner = radio_s_tuner,
2306 .vidioc_s_audio = radio_s_audio,
2307 .vidioc_s_input = radio_s_input,
2308 .vidioc_queryctrl = radio_queryctrl,
2309 .vidioc_g_ctrl = vidioc_g_ctrl,
2310 .vidioc_s_ctrl = vidioc_s_ctrl,
2311 .vidioc_g_frequency = vidioc_g_frequency,
2312 .vidioc_s_frequency = vidioc_s_frequency,
2313 #ifdef CONFIG_VIDEO_ADV_DEBUG
2314 .vidioc_g_register = vidioc_g_register,
2315 .vidioc_s_register = vidioc_s_register,
2319 static struct video_device cx231xx_radio_template = {
2320 .name = "cx231xx-radio",
2321 .fops = &radio_fops,
2322 .ioctl_ops = &radio_ioctl_ops,
2326 /******************************** usb interface ******************************/
2329 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2330 const struct video_device *template,
2331 const char *type_name)
2333 struct video_device *vfd;
2335 vfd = video_device_alloc();
2340 vfd->parent = &dev->udev->dev;
2341 vfd->release = video_device_release;
2342 vfd->debug = video_debug;
2344 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2345 dev->name, type_name);
2350 int cx231xx_register_analog_devices(struct cx231xx *dev)
2354 cx231xx_info("%s()\n", __func__);
2356 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2358 (CX231XX_VERSION_CODE >> 16) & 0xff,
2359 (CX231XX_VERSION_CODE >> 8) & 0xff, CX231XX_VERSION_CODE & 0xff);
2361 /* set default norm */
2362 /*dev->norm = cx231xx_video_template.current_norm;*/
2363 dev->width = norm_maxw(dev);
2364 dev->height = norm_maxh(dev);
2365 dev->interlaced = 0;
2369 /* Analog specific initialization */
2370 dev->format = &format[0];
2371 /* video_mux(dev, dev->video_input); */
2373 /* Audio defaults */
2377 /* enable vbi capturing */
2378 /* write code here... */
2380 /* allocate and fill video video_device struct */
2381 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2383 cx231xx_errdev("cannot allocate video_device.\n");
2387 /* register v4l2 video video_device */
2388 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2389 video_nr[dev->devno]);
2391 cx231xx_errdev("unable to register video device (error=%i).\n", ret);
2395 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2396 dev->name, dev->vdev->num);
2398 /* Initialize VBI template */
2399 memcpy( &cx231xx_vbi_template, &cx231xx_video_template,
2400 sizeof(cx231xx_vbi_template) );
2401 strcpy(cx231xx_vbi_template.name,"cx231xx-vbi");
2404 /* Allocate and fill vbi video_device struct */
2405 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2407 /* register v4l2 vbi video_device */
2408 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2409 vbi_nr[dev->devno]);
2411 cx231xx_errdev("unable to register vbi device\n");
2415 cx231xx_info("%s/0: registered device vbi%d\n",
2416 dev->name, dev->vbi_dev->num);
2418 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2419 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template, "radio");
2420 if (!dev->radio_dev) {
2421 cx231xx_errdev("cannot allocate video_device.\n");
2424 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2425 radio_nr[dev->devno]);
2427 cx231xx_errdev("can't register radio device\n");
2430 cx231xx_info("Registered radio device as /dev/radio%d\n",
2431 dev->radio_dev->num);
2434 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2435 dev->vdev->num, dev->vbi_dev->num);