2 cx231xx-video.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
7 Based on cx23885 driver
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 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>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.h>
43 #include "dvb_frontend.h"
46 #include "cx231xx-vbi.h"
48 #define CX231XX_VERSION "0.0.2"
50 #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
51 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
53 #define cx231xx_videodbg(fmt, arg...) do {\
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
58 static unsigned int isoc_debug;
59 module_param(isoc_debug, int, 0644);
60 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
62 #define cx231xx_isocdbg(fmt, arg...) \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
73 MODULE_VERSION(CX231XX_VERSION);
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]");
94 /* supported video standards */
95 static struct cx231xx_fmt format[] = {
97 .name = "16bpp YUY2, 4:2:2, packed",
98 .fourcc = V4L2_PIX_FMT_YUYV,
105 /* ------------------------------------------------------------------
106 Video buffer and parser functions
107 ------------------------------------------------------------------*/
110 * Announces that a buffer were filled and request the next
112 static inline void buffer_filled(struct cx231xx *dev,
113 struct cx231xx_dmaqueue *dma_q,
114 struct cx231xx_buffer *buf)
116 /* Advice that buffer was filled */
117 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
118 buf->vb.state = VIDEOBUF_DONE;
119 buf->vb.field_count++;
120 v4l2_get_timestamp(&buf->vb.ts);
123 dev->video_mode.isoc_ctl.buf = NULL;
125 dev->video_mode.bulk_ctl.buf = NULL;
127 list_del(&buf->vb.queue);
128 wake_up(&buf->vb.done);
131 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
133 char *errmsg = "Unknown";
137 errmsg = "unlinked synchronuously";
140 errmsg = "unlinked asynchronuously";
143 errmsg = "Buffer error (overrun)";
146 errmsg = "Stalled (device not responding)";
149 errmsg = "Babble (bad cable?)";
152 errmsg = "Bit-stuff error (bad cable?)";
155 errmsg = "CRC/Timeout (could be anything)";
158 errmsg = "Device does not respond";
162 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
164 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
165 packet, status, errmsg);
170 * video-buf generic routine to get the next available buffer
172 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
173 struct cx231xx_buffer **buf)
175 struct cx231xx_video_mode *vmode =
176 container_of(dma_q, struct cx231xx_video_mode, vidq);
177 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
181 if (list_empty(&dma_q->active)) {
182 cx231xx_isocdbg("No active queue to serve\n");
184 dev->video_mode.isoc_ctl.buf = NULL;
186 dev->video_mode.bulk_ctl.buf = NULL;
191 /* Get the next buffer */
192 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
194 /* Cleans up buffer - Useful for testing for frame/URB loss */
195 outp = videobuf_to_vmalloc(&(*buf)->vb);
196 memset(outp, 0, (*buf)->vb.size);
199 dev->video_mode.isoc_ctl.buf = *buf;
201 dev->video_mode.bulk_ctl.buf = *buf;
207 * Controls the isoc copy of each urb packet
209 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
211 struct cx231xx_dmaqueue *dma_q = urb->context;
213 unsigned char *p_buffer;
214 u32 bytes_parsed = 0, buffer_size = 0;
220 if (dev->state & DEV_DISCONNECTED)
223 if (urb->status < 0) {
224 print_err_status(dev, -1, urb->status);
225 if (urb->status == -ENOENT)
229 for (i = 0; i < urb->number_of_packets; i++) {
230 int status = urb->iso_frame_desc[i].status;
233 print_err_status(dev, i, status);
234 if (urb->iso_frame_desc[i].status != -EPROTO)
238 if (urb->iso_frame_desc[i].actual_length <= 0) {
239 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
242 if (urb->iso_frame_desc[i].actual_length >
243 dev->video_mode.max_pkt_size) {
244 cx231xx_isocdbg("packet bigger than packet size");
248 /* get buffer pointer and length */
249 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
250 buffer_size = urb->iso_frame_desc[i].actual_length;
253 if (dma_q->is_partial_line) {
254 /* Handle the case of a partial line */
255 sav_eav = dma_q->last_sav;
257 /* Check for a SAV/EAV overlapping
258 the buffer boundary */
260 cx231xx_find_boundary_SAV_EAV(p_buffer,
266 /* Get the first line if we have some portion of an SAV/EAV from
267 the last buffer or a partial line */
269 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
270 sav_eav, /* SAV/EAV */
271 p_buffer + bytes_parsed, /* p_buffer */
272 buffer_size - bytes_parsed);/* buf size */
275 /* Now parse data that is completely in this buffer */
276 /* dma_q->is_partial_line = 0; */
278 while (bytes_parsed < buffer_size) {
281 sav_eav = cx231xx_find_next_SAV_EAV(
282 p_buffer + bytes_parsed, /* p_buffer */
283 buffer_size - bytes_parsed, /* buf size */
284 &bytes_used);/* bytes used to get SAV/EAV */
286 bytes_parsed += bytes_used;
289 if (sav_eav && (bytes_parsed < buffer_size)) {
290 bytes_parsed += cx231xx_get_video_line(dev,
291 dma_q, sav_eav, /* SAV/EAV */
292 p_buffer + bytes_parsed,/* p_buffer */
293 buffer_size - bytes_parsed);/*buf size*/
297 /* Save the last four bytes of the buffer so we can check the
298 buffer boundary condition next time */
299 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
306 static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
308 struct cx231xx_dmaqueue *dma_q = urb->context;
310 unsigned char *p_buffer;
311 u32 bytes_parsed = 0, buffer_size = 0;
317 if (dev->state & DEV_DISCONNECTED)
320 if (urb->status < 0) {
321 print_err_status(dev, -1, urb->status);
322 if (urb->status == -ENOENT)
328 /* get buffer pointer and length */
329 p_buffer = urb->transfer_buffer;
330 buffer_size = urb->actual_length;
333 if (dma_q->is_partial_line) {
334 /* Handle the case of a partial line */
335 sav_eav = dma_q->last_sav;
337 /* Check for a SAV/EAV overlapping
338 the buffer boundary */
340 cx231xx_find_boundary_SAV_EAV(p_buffer,
346 /* Get the first line if we have some portion of an SAV/EAV from
347 the last buffer or a partial line */
349 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
350 sav_eav, /* SAV/EAV */
351 p_buffer + bytes_parsed, /* p_buffer */
352 buffer_size - bytes_parsed);/* buf size */
355 /* Now parse data that is completely in this buffer */
356 /* dma_q->is_partial_line = 0; */
358 while (bytes_parsed < buffer_size) {
361 sav_eav = cx231xx_find_next_SAV_EAV(
362 p_buffer + bytes_parsed, /* p_buffer */
363 buffer_size - bytes_parsed, /* buf size */
364 &bytes_used);/* bytes used to get SAV/EAV */
366 bytes_parsed += bytes_used;
369 if (sav_eav && (bytes_parsed < buffer_size)) {
370 bytes_parsed += cx231xx_get_video_line(dev,
371 dma_q, sav_eav, /* SAV/EAV */
372 p_buffer + bytes_parsed,/* p_buffer */
373 buffer_size - bytes_parsed);/*buf size*/
377 /* Save the last four bytes of the buffer so we can check the
378 buffer boundary condition next time */
379 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
387 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
391 u8 boundary_bytes[8];
396 /* Create an array of the last 4 bytes of the last buffer and the first
397 4 bytes of the current buffer. */
399 memcpy(boundary_bytes, partial_buf, 4);
400 memcpy(boundary_bytes + 4, p_buffer, 4);
402 /* Check for the SAV/EAV in the boundary buffer */
403 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
407 /* found a boundary SAV/EAV. Updates the bytes used to reflect
408 only those used in the new buffer */
409 *p_bytes_used = bytes_used - 4;
415 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
421 * Don't search if the buffer size is less than 4. It causes a page
422 * fault since buffer_size - 4 evaluates to a large number in that
425 if (buffer_size < 4) {
426 *p_bytes_used = buffer_size;
430 for (i = 0; i < (buffer_size - 3); i++) {
432 if ((p_buffer[i] == 0xFF) &&
433 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
435 *p_bytes_used = i + 4;
436 sav_eav = p_buffer[i + 3];
441 *p_bytes_used = buffer_size;
445 u32 cx231xx_get_video_line(struct cx231xx *dev,
446 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
447 u8 *p_buffer, u32 buffer_size)
449 u32 bytes_copied = 0;
450 int current_field = -1;
453 case SAV_ACTIVE_VIDEO_FIELD1:
454 /* looking for skipped line which occurred in PAL 720x480 mode.
455 In this case, there will be no active data contained
456 between the SAV and EAV */
457 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
458 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
459 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
460 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
461 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
462 (p_buffer[3] == EAV_VBLANK_FIELD2)))
467 case SAV_ACTIVE_VIDEO_FIELD2:
468 /* looking for skipped line which occurred in PAL 720x480 mode.
469 In this case, there will be no active data contained between
471 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
472 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
473 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
474 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
475 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
476 (p_buffer[3] == EAV_VBLANK_FIELD2)))
482 dma_q->last_sav = sav_eav;
484 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
485 buffer_size, current_field);
490 u32 cx231xx_copy_video_line(struct cx231xx *dev,
491 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
492 u32 length, int field_number)
495 struct cx231xx_buffer *buf;
496 u32 _line_size = dev->width * 2;
498 if (dma_q->current_field != field_number)
499 cx231xx_reset_video_buffer(dev, dma_q);
501 /* get the buffer pointer */
503 buf = dev->video_mode.isoc_ctl.buf;
505 buf = dev->video_mode.bulk_ctl.buf;
507 /* Remember the field number for next time */
508 dma_q->current_field = field_number;
510 bytes_to_copy = dma_q->bytes_left_in_line;
511 if (bytes_to_copy > length)
512 bytes_to_copy = length;
514 if (dma_q->lines_completed >= dma_q->lines_per_field) {
515 dma_q->bytes_left_in_line -= bytes_to_copy;
516 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
521 dma_q->is_partial_line = 1;
523 /* If we don't have a buffer, just return the number of bytes we would
524 have copied if we had a buffer. */
526 dma_q->bytes_left_in_line -= bytes_to_copy;
527 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
529 return bytes_to_copy;
532 /* copy the data to video buffer */
533 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
535 dma_q->pos += bytes_to_copy;
536 dma_q->bytes_left_in_line -= bytes_to_copy;
538 if (dma_q->bytes_left_in_line == 0) {
539 dma_q->bytes_left_in_line = _line_size;
540 dma_q->lines_completed++;
541 dma_q->is_partial_line = 0;
543 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
544 buffer_filled(dev, dma_q, buf);
548 dma_q->lines_completed = 0;
552 return bytes_to_copy;
555 void cx231xx_reset_video_buffer(struct cx231xx *dev,
556 struct cx231xx_dmaqueue *dma_q)
558 struct cx231xx_buffer *buf;
560 /* handle the switch from field 1 to field 2 */
561 if (dma_q->current_field == 1) {
562 if (dma_q->lines_completed >= dma_q->lines_per_field)
563 dma_q->field1_done = 1;
565 dma_q->field1_done = 0;
569 buf = dev->video_mode.isoc_ctl.buf;
571 buf = dev->video_mode.bulk_ctl.buf;
574 /* first try to get the buffer */
575 get_next_buf(dma_q, &buf);
578 dma_q->field1_done = 0;
579 dma_q->current_field = -1;
582 /* reset the counters */
583 dma_q->bytes_left_in_line = dev->width << 1;
584 dma_q->lines_completed = 0;
587 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
588 u8 *p_buffer, u32 bytes_to_copy)
590 u8 *p_out_buffer = NULL;
591 u32 current_line_bytes_copied = 0;
592 struct cx231xx_buffer *buf;
593 u32 _line_size = dev->width << 1;
598 buf = dev->video_mode.isoc_ctl.buf;
600 buf = dev->video_mode.bulk_ctl.buf;
605 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
607 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
609 /* Offset field 2 one line from the top of the buffer */
610 offset = (dma_q->current_field == 1) ? 0 : _line_size;
612 /* Offset for field 2 */
613 startwrite = p_out_buffer + offset;
615 /* lines already completed in the current field */
616 startwrite += (dma_q->lines_completed * _line_size * 2);
618 /* bytes already completed in the current line */
619 startwrite += current_line_bytes_copied;
621 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
622 bytes_to_copy : dma_q->bytes_left_in_line;
624 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
627 /* The below copies the UYVY data straight into video buffer */
628 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
633 void cx231xx_swab(u16 *from, u16 *to, u16 len)
640 for (i = 0; i < len / 2; i++)
641 to[i] = (from[i] << 8) | (from[i] >> 8);
644 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
646 u8 buffer_complete = 0;
648 /* Dual field stream */
649 buffer_complete = ((dma_q->current_field == 2) &&
650 (dma_q->lines_completed >= dma_q->lines_per_field) &&
653 return buffer_complete;
656 /* ------------------------------------------------------------------
658 ------------------------------------------------------------------*/
661 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
663 struct cx231xx_fh *fh = vq->priv_data;
664 struct cx231xx *dev = fh->dev;
666 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
668 *count = CX231XX_DEF_BUF;
670 if (*count < CX231XX_MIN_BUF)
671 *count = CX231XX_MIN_BUF;
676 /* This is called *without* dev->slock held; please keep it that way */
677 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
679 struct cx231xx_fh *fh = vq->priv_data;
680 struct cx231xx *dev = fh->dev;
681 unsigned long flags = 0;
686 /* We used to wait for the buffer to finish here, but this didn't work
687 because, as we were keeping the state as VIDEOBUF_QUEUED,
688 videobuf_queue_cancel marked it as finished for us.
689 (Also, it could wedge forever if the hardware was misconfigured.)
691 This should be safe; by the time we get here, the buffer isn't
692 queued anymore. If we ever start marking the buffers as
693 VIDEOBUF_ACTIVE, it won't be, though.
695 spin_lock_irqsave(&dev->video_mode.slock, flags);
697 if (dev->video_mode.isoc_ctl.buf == buf)
698 dev->video_mode.isoc_ctl.buf = NULL;
700 if (dev->video_mode.bulk_ctl.buf == buf)
701 dev->video_mode.bulk_ctl.buf = NULL;
703 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
705 videobuf_vmalloc_free(&buf->vb);
706 buf->vb.state = VIDEOBUF_NEEDS_INIT;
710 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
711 enum v4l2_field field)
713 struct cx231xx_fh *fh = vq->priv_data;
714 struct cx231xx_buffer *buf =
715 container_of(vb, struct cx231xx_buffer, vb);
716 struct cx231xx *dev = fh->dev;
717 int rc = 0, urb_init = 0;
719 /* The only currently supported format is 16 bits/pixel */
720 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
722 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
725 buf->vb.width = dev->width;
726 buf->vb.height = dev->height;
727 buf->vb.field = field;
729 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
730 rc = videobuf_iolock(vq, &buf->vb, NULL);
736 if (!dev->video_mode.isoc_ctl.num_bufs)
739 if (!dev->video_mode.bulk_ctl.num_bufs)
742 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
743 urb_init, dev->video_mode.max_pkt_size);*/
747 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
749 dev->video_mode.max_pkt_size,
752 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
754 dev->video_mode.max_pkt_size,
760 buf->vb.state = VIDEOBUF_PREPARED;
764 free_buffer(vq, buf);
768 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
770 struct cx231xx_buffer *buf =
771 container_of(vb, struct cx231xx_buffer, vb);
772 struct cx231xx_fh *fh = vq->priv_data;
773 struct cx231xx *dev = fh->dev;
774 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
776 buf->vb.state = VIDEOBUF_QUEUED;
777 list_add_tail(&buf->vb.queue, &vidq->active);
781 static void buffer_release(struct videobuf_queue *vq,
782 struct videobuf_buffer *vb)
784 struct cx231xx_buffer *buf =
785 container_of(vb, struct cx231xx_buffer, vb);
786 struct cx231xx_fh *fh = vq->priv_data;
787 struct cx231xx *dev = (struct cx231xx *)fh->dev;
789 cx231xx_isocdbg("cx231xx: called buffer_release\n");
791 free_buffer(vq, buf);
794 static struct videobuf_queue_ops cx231xx_video_qops = {
795 .buf_setup = buffer_setup,
796 .buf_prepare = buffer_prepare,
797 .buf_queue = buffer_queue,
798 .buf_release = buffer_release,
801 /********************* v4l2 interface **************************************/
803 void video_mux(struct cx231xx *dev, int index)
805 dev->video_input = index;
806 dev->ctl_ainput = INPUT(index)->amux;
808 cx231xx_set_video_input_mux(dev, index);
810 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
812 cx231xx_set_audio_input(dev, dev->ctl_ainput);
814 cx231xx_info("video_mux : %d\n", index);
816 /* do mode control overrides if required */
817 cx231xx_do_mode_ctrl_overrides(dev);
820 /* Usage lock check functions */
821 static int res_get(struct cx231xx_fh *fh)
823 struct cx231xx *dev = fh->dev;
826 /* This instance already has stream_on */
830 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
834 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
835 if (dev->vbi_stream_on)
837 dev->vbi_stream_on = 1;
846 static int res_check(struct cx231xx_fh *fh)
848 return fh->stream_on;
851 static void res_free(struct cx231xx_fh *fh)
853 struct cx231xx *dev = fh->dev;
857 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
859 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
860 dev->vbi_stream_on = 0;
863 static int check_dev(struct cx231xx *dev)
865 if (dev->state & DEV_DISCONNECTED) {
866 cx231xx_errdev("v4l2 ioctl: device not present\n");
872 /* ------------------------------------------------------------------
873 IOCTL vidioc handling
874 ------------------------------------------------------------------*/
876 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
877 struct v4l2_format *f)
879 struct cx231xx_fh *fh = priv;
880 struct cx231xx *dev = fh->dev;
882 f->fmt.pix.width = dev->width;
883 f->fmt.pix.height = dev->height;
884 f->fmt.pix.pixelformat = dev->format->fourcc;
885 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
886 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
887 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
889 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
895 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
899 for (i = 0; i < ARRAY_SIZE(format); i++)
900 if (format[i].fourcc == fourcc)
906 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
907 struct v4l2_format *f)
909 struct cx231xx_fh *fh = priv;
910 struct cx231xx *dev = fh->dev;
911 unsigned int width = f->fmt.pix.width;
912 unsigned int height = f->fmt.pix.height;
913 unsigned int maxw = norm_maxw(dev);
914 unsigned int maxh = norm_maxh(dev);
915 struct cx231xx_fmt *fmt;
917 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
919 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
920 f->fmt.pix.pixelformat);
924 /* width must even because of the YUYV format
925 height must be even because of interlacing */
926 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
928 f->fmt.pix.width = width;
929 f->fmt.pix.height = height;
930 f->fmt.pix.pixelformat = fmt->fourcc;
931 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
932 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
933 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
934 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
940 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
941 struct v4l2_format *f)
943 struct cx231xx_fh *fh = priv;
944 struct cx231xx *dev = fh->dev;
946 struct cx231xx_fmt *fmt;
947 struct v4l2_mbus_framefmt mbus_fmt;
953 vidioc_try_fmt_vid_cap(file, priv, f);
955 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
959 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
960 cx231xx_errdev("%s queue busy\n", __func__);
964 if (dev->stream_on && !fh->stream_on) {
965 cx231xx_errdev("%s device in use by another fh\n", __func__);
969 /* set new image size */
970 dev->width = f->fmt.pix.width;
971 dev->height = f->fmt.pix.height;
974 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
975 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
976 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
981 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
983 struct cx231xx_fh *fh = priv;
984 struct cx231xx *dev = fh->dev;
990 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
992 struct cx231xx_fh *fh = priv;
993 struct cx231xx *dev = fh->dev;
994 struct v4l2_mbus_framefmt mbus_fmt;
1001 if (dev->norm == norm)
1004 if (videobuf_queue_is_busy(&fh->vb_vidq))
1009 /* Adjusts width/height, if needed */
1011 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
1013 call_all(dev, core, s_std, dev->norm);
1015 /* We need to reset basic properties in the decoder related to
1016 resolution (since a standard change effects things like the number
1017 of lines in VACT, etc) */
1018 memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1019 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1020 mbus_fmt.width = dev->width;
1021 mbus_fmt.height = dev->height;
1022 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1024 /* do mode control overrides */
1025 cx231xx_do_mode_ctrl_overrides(dev);
1030 static const char *iname[] = {
1031 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1032 [CX231XX_VMUX_SVIDEO] = "S-Video",
1033 [CX231XX_VMUX_TELEVISION] = "Television",
1034 [CX231XX_VMUX_CABLE] = "Cable TV",
1035 [CX231XX_VMUX_DVB] = "DVB",
1036 [CX231XX_VMUX_DEBUG] = "for debug only",
1039 int cx231xx_enum_input(struct file *file, void *priv,
1040 struct v4l2_input *i)
1042 struct cx231xx_fh *fh = priv;
1043 struct cx231xx *dev = fh->dev;
1045 unsigned int ret, n;
1048 if (n >= MAX_CX231XX_INPUT)
1050 if (0 == INPUT(n)->type)
1054 i->type = V4L2_INPUT_TYPE_CAMERA;
1056 strcpy(i->name, iname[INPUT(n)->type]);
1058 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1059 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1060 i->type = V4L2_INPUT_TYPE_TUNER;
1062 i->std = dev->vdev->tvnorms;
1064 /* If they are asking about the active input, read signal status */
1065 if (n == dev->video_input) {
1066 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1067 GEN_STAT, 2, &gen_stat, 4);
1069 if ((gen_stat & FLD_VPRES) == 0x00)
1070 i->status |= V4L2_IN_ST_NO_SIGNAL;
1071 if ((gen_stat & FLD_HLOCK) == 0x00)
1072 i->status |= V4L2_IN_ST_NO_H_LOCK;
1079 int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
1081 struct cx231xx_fh *fh = priv;
1082 struct cx231xx *dev = fh->dev;
1084 *i = dev->video_input;
1089 int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
1091 struct cx231xx_fh *fh = priv;
1092 struct cx231xx *dev = fh->dev;
1096 rc = check_dev(dev);
1100 if (i >= MAX_CX231XX_INPUT)
1102 if (0 == INPUT(i)->type)
1107 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1108 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1109 /* There's a tuner, so reset the standard and put it on the
1110 last known frequency (since it was probably powered down
1112 call_all(dev, core, s_std, dev->norm);
1118 int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1120 struct cx231xx_fh *fh = priv;
1121 struct cx231xx *dev = fh->dev;
1124 rc = check_dev(dev);
1131 strcpy(t->name, "Tuner");
1133 t->type = V4L2_TUNER_ANALOG_TV;
1134 t->capability = V4L2_TUNER_CAP_NORM;
1135 t->rangehigh = 0xffffffffUL;
1136 t->signal = 0xffff; /* LOCKED */
1137 call_all(dev, tuner, g_tuner, t);
1142 int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1144 struct cx231xx_fh *fh = priv;
1145 struct cx231xx *dev = fh->dev;
1148 rc = check_dev(dev);
1155 call_all(dev, tuner, s_tuner, t);
1160 int cx231xx_g_frequency(struct file *file, void *priv,
1161 struct v4l2_frequency *f)
1163 struct cx231xx_fh *fh = priv;
1164 struct cx231xx *dev = fh->dev;
1169 f->frequency = dev->ctl_freq;
1174 int cx231xx_s_frequency(struct file *file, void *priv,
1175 const struct v4l2_frequency *f)
1177 struct cx231xx_fh *fh = priv;
1178 struct cx231xx *dev = fh->dev;
1179 struct v4l2_frequency new_freq = *f;
1181 u32 if_frequency = 5400000;
1183 cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1184 f->frequency, f->type);
1185 /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
1187 rc = check_dev(dev);
1194 /* set pre channel change settings in DIF first */
1195 rc = cx231xx_tuner_pre_channel_change(dev);
1197 call_all(dev, tuner, s_frequency, f);
1198 call_all(dev, tuner, g_frequency, &new_freq);
1199 dev->ctl_freq = new_freq.frequency;
1201 /* set post channel change settings in DIF first */
1202 rc = cx231xx_tuner_post_channel_change(dev);
1204 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1205 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1206 if_frequency = 5400000; /*5.4MHz */
1207 else if (dev->norm & V4L2_STD_B)
1208 if_frequency = 6000000; /*6.0MHz */
1209 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1210 if_frequency = 6900000; /*6.9MHz */
1211 else if (dev->norm & V4L2_STD_GH)
1212 if_frequency = 7100000; /*7.1MHz */
1213 else if (dev->norm & V4L2_STD_PAL_I)
1214 if_frequency = 7250000; /*7.25MHz */
1215 else if (dev->norm & V4L2_STD_SECAM_L)
1216 if_frequency = 6900000; /*6.9MHz */
1217 else if (dev->norm & V4L2_STD_SECAM_LC)
1218 if_frequency = 1250000; /*1.25MHz */
1220 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1221 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1223 update_HH_register_after_set_DIF(dev);
1226 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1231 int cx231xx_g_chip_ident(struct file *file, void *fh,
1232 struct v4l2_dbg_chip_ident *chip)
1234 chip->ident = V4L2_IDENT_NONE;
1236 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1237 if (v4l2_chip_match_host(&chip->match))
1238 chip->ident = V4L2_IDENT_CX23100;
1244 #ifdef CONFIG_VIDEO_ADV_DEBUG
1247 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1248 chip=<chip>[,min=<addr>,max=<addr>]
1249 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1250 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1251 chip=<chip>,reg=<addr>,val=<val>
1252 set the register [VIDIOC_DBG_S_REGISTER]
1254 if type == host, then <chip> is the hosts chip ID (default 0)
1255 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1256 if type == i2caddr, then <chip> is the 7-bit I2C address
1259 int cx231xx_g_register(struct file *file, void *priv,
1260 struct v4l2_dbg_register *reg)
1262 struct cx231xx_fh *fh = priv;
1263 struct cx231xx *dev = fh->dev;
1265 u8 value[4] = { 0, 0, 0, 0 };
1268 switch (reg->match.type) {
1269 case V4L2_CHIP_MATCH_HOST:
1270 switch (reg->match.addr) {
1271 case 0: /* Cx231xx - internal registers */
1272 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1273 (u16)reg->reg, value, 4);
1274 reg->val = value[0] | value[1] << 8 |
1275 value[2] << 16 | value[3] << 24;
1277 case 1: /* AFE - read byte */
1278 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1279 (u16)reg->reg, 2, &data, 1);
1280 reg->val = le32_to_cpu(data & 0xff);
1282 case 14: /* AFE - read dword */
1283 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1284 (u16)reg->reg, 2, &data, 4);
1285 reg->val = le32_to_cpu(data);
1287 case 2: /* Video Block - read byte */
1288 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1289 (u16)reg->reg, 2, &data, 1);
1290 reg->val = le32_to_cpu(data & 0xff);
1292 case 24: /* Video Block - read dword */
1293 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1294 (u16)reg->reg, 2, &data, 4);
1295 reg->val = le32_to_cpu(data);
1297 case 3: /* I2S block - read byte */
1298 ret = cx231xx_read_i2c_data(dev,
1299 I2S_BLK_DEVICE_ADDRESS,
1302 reg->val = le32_to_cpu(data & 0xff);
1304 case 34: /* I2S Block - read dword */
1306 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1307 (u16)reg->reg, 1, &data, 4);
1308 reg->val = le32_to_cpu(data);
1311 return ret < 0 ? ret : 0;
1313 case V4L2_CHIP_MATCH_I2C_DRIVER:
1314 call_all(dev, core, g_register, reg);
1316 case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
1317 switch (reg->match.addr) {
1318 case 0: /* Cx231xx - internal registers */
1319 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1320 (u16)reg->reg, value, 4);
1321 reg->val = value[0] | value[1] << 8 |
1322 value[2] << 16 | value[3] << 24;
1325 case 0x600:/* AFE - read byte */
1326 ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
1329 reg->val = le32_to_cpu(data & 0xff);
1332 case 0x880:/* Video Block - read byte */
1333 if (reg->reg < 0x0b) {
1334 ret = cx231xx_read_i2c_master(dev,
1335 VID_BLK_I2C_ADDRESS,
1338 reg->val = le32_to_cpu(data & 0xff);
1340 ret = cx231xx_read_i2c_master(dev,
1341 VID_BLK_I2C_ADDRESS,
1344 reg->val = le32_to_cpu(data);
1348 ret = cx231xx_read_i2c_master(dev,
1349 I2S_BLK_DEVICE_ADDRESS,
1352 reg->val = le32_to_cpu(data & 0xff);
1356 cx231xx_read_i2c_master(dev, 0x40,
1359 reg->val = le32_to_cpu(data & 0xff);
1363 cx231xx_read_i2c_master(dev, 0xc0,
1366 reg->val = le32_to_cpu(data);
1370 cx231xx_read_i2c_master(dev, 0x02,
1373 reg->val = le32_to_cpu(data & 0xff);
1376 ret = cx231xx_read_i2c_master(dev,
1380 reg->val = le32_to_cpu(data);
1383 ret = cx231xx_read_i2c_master(dev,
1387 reg->val = le32_to_cpu(data);
1391 cx231xx_info("no match device address!!\n");
1394 return ret < 0 ? ret : 0;
1397 if (!v4l2_chip_match_host(®->match))
1401 call_all(dev, core, g_register, reg);
1406 int cx231xx_s_register(struct file *file, void *priv,
1407 const struct v4l2_dbg_register *reg)
1409 struct cx231xx_fh *fh = priv;
1410 struct cx231xx *dev = fh->dev;
1414 u8 data[4] = { 0, 0, 0, 0 };
1416 buf = cpu_to_le64(reg->val);
1418 switch (reg->match.type) {
1419 case V4L2_CHIP_MATCH_HOST:
1421 value = (u32) buf & 0xffffffff;
1423 switch (reg->match.addr) {
1424 case 0: /* cx231xx internal registers */
1425 data[0] = (u8) value;
1426 data[1] = (u8) (value >> 8);
1427 data[2] = (u8) (value >> 16);
1428 data[3] = (u8) (value >> 24);
1429 ret = cx231xx_write_ctrl_reg(dev,
1431 (u16)reg->reg, data,
1434 case 1: /* AFE - read byte */
1435 ret = cx231xx_write_i2c_data(dev,
1440 case 14: /* AFE - read dword */
1441 ret = cx231xx_write_i2c_data(dev,
1446 case 2: /* Video Block - read byte */
1448 cx231xx_write_i2c_data(dev,
1449 VID_BLK_I2C_ADDRESS,
1453 case 24: /* Video Block - read dword */
1455 cx231xx_write_i2c_data(dev,
1456 VID_BLK_I2C_ADDRESS,
1460 case 3: /* I2S block - read byte */
1462 cx231xx_write_i2c_data(dev,
1463 I2S_BLK_DEVICE_ADDRESS,
1467 case 34: /* I2S block - read dword */
1469 cx231xx_write_i2c_data(dev,
1470 I2S_BLK_DEVICE_ADDRESS,
1476 return ret < 0 ? ret : 0;
1477 case V4L2_CHIP_MATCH_I2C_ADDR:
1479 value = (u32) buf & 0xffffffff;
1481 switch (reg->match.addr) {
1482 case 0:/*cx231xx internal registers*/
1483 data[0] = (u8) value;
1484 data[1] = (u8) (value >> 8);
1485 data[2] = (u8) (value >> 16);
1486 data[3] = (u8) (value >> 24);
1487 ret = cx231xx_write_ctrl_reg(dev,
1489 (u16)reg->reg, data,
1492 case 0x600:/* AFE - read byte */
1493 ret = cx231xx_write_i2c_master(dev,
1499 case 0x880:/* Video Block - read byte */
1500 if (reg->reg < 0x0b)
1501 cx231xx_write_i2c_master(dev,
1502 VID_BLK_I2C_ADDRESS,
1506 cx231xx_write_i2c_master(dev,
1507 VID_BLK_I2C_ADDRESS,
1513 cx231xx_write_i2c_master(dev,
1514 I2S_BLK_DEVICE_ADDRESS,
1520 cx231xx_write_i2c_master(dev,
1527 cx231xx_write_i2c_master(dev,
1535 cx231xx_write_i2c_master(dev,
1542 cx231xx_write_i2c_master(dev,
1550 cx231xx_write_i2c_master(dev,
1556 cx231xx_info("no match device address, "
1557 "the value is %x\n", reg->match.addr);
1567 call_all(dev, core, s_register, reg);
1573 static int vidioc_cropcap(struct file *file, void *priv,
1574 struct v4l2_cropcap *cc)
1576 struct cx231xx_fh *fh = priv;
1577 struct cx231xx *dev = fh->dev;
1579 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1582 cc->bounds.left = 0;
1584 cc->bounds.width = dev->width;
1585 cc->bounds.height = dev->height;
1586 cc->defrect = cc->bounds;
1587 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1588 cc->pixelaspect.denominator = 59;
1593 static int vidioc_streamon(struct file *file, void *priv,
1594 enum v4l2_buf_type type)
1596 struct cx231xx_fh *fh = priv;
1597 struct cx231xx *dev = fh->dev;
1600 rc = check_dev(dev);
1606 if (likely(rc >= 0))
1607 rc = videobuf_streamon(&fh->vb_vidq);
1609 call_all(dev, video, s_stream, 1);
1614 static int vidioc_streamoff(struct file *file, void *priv,
1615 enum v4l2_buf_type type)
1617 struct cx231xx_fh *fh = priv;
1618 struct cx231xx *dev = fh->dev;
1621 rc = check_dev(dev);
1625 if (type != fh->type)
1628 cx25840_call(dev, video, s_stream, 0);
1630 videobuf_streamoff(&fh->vb_vidq);
1636 int cx231xx_querycap(struct file *file, void *priv,
1637 struct v4l2_capability *cap)
1639 struct video_device *vdev = video_devdata(file);
1640 struct cx231xx_fh *fh = priv;
1641 struct cx231xx *dev = fh->dev;
1643 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1644 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1645 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1647 if (vdev->vfl_type == VFL_TYPE_RADIO)
1648 cap->device_caps = V4L2_CAP_RADIO;
1650 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1651 if (vdev->vfl_type == VFL_TYPE_VBI)
1652 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1654 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1656 if (dev->tuner_type != TUNER_ABSENT)
1657 cap->device_caps |= V4L2_CAP_TUNER;
1658 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
1659 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1660 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1662 cap->capabilities |= V4L2_CAP_RADIO;
1667 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1668 struct v4l2_fmtdesc *f)
1670 if (unlikely(f->index >= ARRAY_SIZE(format)))
1673 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1674 f->pixelformat = format[f->index].fourcc;
1679 /* RAW VBI ioctls */
1681 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1682 struct v4l2_format *f)
1684 struct cx231xx_fh *fh = priv;
1685 struct cx231xx *dev = fh->dev;
1687 f->fmt.vbi.sampling_rate = 6750000 * 4;
1688 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1689 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1690 f->fmt.vbi.offset = 0;
1691 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1692 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1693 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1694 PAL_VBI_LINES : NTSC_VBI_LINES;
1695 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1696 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1697 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1698 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1704 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1705 struct v4l2_format *f)
1707 struct cx231xx_fh *fh = priv;
1708 struct cx231xx *dev = fh->dev;
1710 f->fmt.vbi.sampling_rate = 6750000 * 4;
1711 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1712 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1713 f->fmt.vbi.offset = 0;
1714 f->fmt.vbi.flags = 0;
1715 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1716 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1717 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1718 PAL_VBI_LINES : NTSC_VBI_LINES;
1719 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1720 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1721 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1722 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1728 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1729 struct v4l2_format *f)
1731 struct cx231xx_fh *fh = priv;
1732 struct cx231xx *dev = fh->dev;
1734 if (dev->vbi_stream_on && !fh->stream_on) {
1735 cx231xx_errdev("%s device in use by another fh\n", __func__);
1738 return vidioc_try_fmt_vbi_cap(file, priv, f);
1741 static int vidioc_reqbufs(struct file *file, void *priv,
1742 struct v4l2_requestbuffers *rb)
1744 struct cx231xx_fh *fh = priv;
1745 struct cx231xx *dev = fh->dev;
1748 rc = check_dev(dev);
1752 return videobuf_reqbufs(&fh->vb_vidq, rb);
1755 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1757 struct cx231xx_fh *fh = priv;
1758 struct cx231xx *dev = fh->dev;
1761 rc = check_dev(dev);
1765 return videobuf_querybuf(&fh->vb_vidq, b);
1768 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1770 struct cx231xx_fh *fh = priv;
1771 struct cx231xx *dev = fh->dev;
1774 rc = check_dev(dev);
1778 return videobuf_qbuf(&fh->vb_vidq, b);
1781 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1783 struct cx231xx_fh *fh = priv;
1784 struct cx231xx *dev = fh->dev;
1787 rc = check_dev(dev);
1791 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1794 /* ----------------------------------------------------------- */
1795 /* RADIO ESPECIFIC IOCTLS */
1796 /* ----------------------------------------------------------- */
1798 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1800 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1805 strcpy(t->name, "Radio");
1807 call_all(dev, tuner, g_tuner, t);
1811 static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1813 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1818 call_all(dev, tuner, s_tuner, t);
1824 * cx231xx_v4l2_open()
1825 * inits the device and starts isoc transfer
1827 static int cx231xx_v4l2_open(struct file *filp)
1829 int errCode = 0, radio = 0;
1830 struct video_device *vdev = video_devdata(filp);
1831 struct cx231xx *dev = video_drvdata(filp);
1832 struct cx231xx_fh *fh;
1833 enum v4l2_buf_type fh_type = 0;
1835 switch (vdev->vfl_type) {
1836 case VFL_TYPE_GRABBER:
1837 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1840 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1842 case VFL_TYPE_RADIO:
1847 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1848 video_device_node_name(vdev), v4l2_type_names[fh_type],
1852 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1855 ("Device locked on digital mode. Can't open analog\n");
1860 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1862 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1865 if (mutex_lock_interruptible(&dev->lock)) {
1867 return -ERESTARTSYS;
1871 filp->private_data = fh;
1872 v4l2_fh_init(&fh->fh, vdev);
1874 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1875 /* Power up in Analog TV mode */
1876 if (dev->board.external_av)
1877 cx231xx_set_power_mode(dev,
1878 POLARIS_AVMODE_ENXTERNAL_AV);
1880 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1883 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1886 /* set video alternate setting */
1887 cx231xx_set_video_alternate(dev);
1889 /* Needed, since GPIO might have disabled power of
1891 cx231xx_config_i2c(dev);
1893 /* device needs to be initialized before isoc transfer */
1894 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1898 cx231xx_videodbg("video_open: setting radio device\n");
1900 /* cx231xx_start_radio(dev); */
1902 call_all(dev, tuner, s_radio);
1907 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1908 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1909 NULL, &dev->video_mode.slock,
1910 fh->type, V4L2_FIELD_INTERLACED,
1911 sizeof(struct cx231xx_buffer),
1913 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1914 /* Set the required alternate setting VBI interface works in
1916 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1918 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1919 NULL, &dev->vbi_mode.slock,
1920 fh->type, V4L2_FIELD_SEQ_TB,
1921 sizeof(struct cx231xx_buffer),
1924 mutex_unlock(&dev->lock);
1925 v4l2_fh_add(&fh->fh);
1931 * cx231xx_realease_resources()
1932 * unregisters the v4l2,i2c and usb devices
1933 * called when the device gets disconected or at module unload
1935 void cx231xx_release_analog_resources(struct cx231xx *dev)
1938 /*FIXME: I2C IR should be disconnected */
1940 if (dev->radio_dev) {
1941 if (video_is_registered(dev->radio_dev))
1942 video_unregister_device(dev->radio_dev);
1944 video_device_release(dev->radio_dev);
1945 dev->radio_dev = NULL;
1948 cx231xx_info("V4L2 device %s deregistered\n",
1949 video_device_node_name(dev->vbi_dev));
1950 if (video_is_registered(dev->vbi_dev))
1951 video_unregister_device(dev->vbi_dev);
1953 video_device_release(dev->vbi_dev);
1954 dev->vbi_dev = NULL;
1957 cx231xx_info("V4L2 device %s deregistered\n",
1958 video_device_node_name(dev->vdev));
1960 if (dev->board.has_417)
1961 cx231xx_417_unregister(dev);
1963 if (video_is_registered(dev->vdev))
1964 video_unregister_device(dev->vdev);
1966 video_device_release(dev->vdev);
1969 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1970 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1975 * stops streaming and deallocates all resources allocated by the v4l2
1978 static int cx231xx_close(struct file *filp)
1980 struct cx231xx_fh *fh = filp->private_data;
1981 struct cx231xx *dev = fh->dev;
1983 cx231xx_videodbg("users=%d\n", dev->users);
1985 cx231xx_videodbg("users=%d\n", dev->users);
1990 * To workaround error number=-71 on EP0 for VideoGrabber,
1991 * need exclude following.
1992 * FIXME: It is probably safe to remove most of these, as we're
1993 * now avoiding the alternate setting for INDEX_VANC
1995 if (!dev->board.no_alt_vanc)
1996 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1997 videobuf_stop(&fh->vb_vidq);
1998 videobuf_mmap_free(&fh->vb_vidq);
2000 /* the device is already disconnect,
2001 free the remaining resources */
2002 if (dev->state & DEV_DISCONNECTED) {
2003 if (atomic_read(&dev->devlist_count) > 0) {
2004 cx231xx_release_resources(dev);
2011 /* do this before setting alternate! */
2012 cx231xx_uninit_vbi_isoc(dev);
2014 /* set alternate 0 */
2015 if (!dev->vbi_or_sliced_cc_mode)
2016 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2018 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2020 v4l2_fh_del(&fh->fh);
2021 v4l2_fh_exit(&fh->fh);
2024 wake_up_interruptible_nr(&dev->open, 1);
2028 v4l2_fh_del(&fh->fh);
2031 videobuf_stop(&fh->vb_vidq);
2032 videobuf_mmap_free(&fh->vb_vidq);
2034 /* the device is already disconnect,
2035 free the remaining resources */
2036 if (dev->state & DEV_DISCONNECTED) {
2037 cx231xx_release_resources(dev);
2042 /* Save some power by putting tuner to sleep */
2043 call_all(dev, core, s_power, 0);
2045 /* do this before setting alternate! */
2047 cx231xx_uninit_isoc(dev);
2049 cx231xx_uninit_bulk(dev);
2050 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2052 /* set alternate 0 */
2053 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2055 v4l2_fh_exit(&fh->fh);
2057 wake_up_interruptible_nr(&dev->open, 1);
2061 static int cx231xx_v4l2_close(struct file *filp)
2063 struct cx231xx_fh *fh = filp->private_data;
2064 struct cx231xx *dev = fh->dev;
2067 mutex_lock(&dev->lock);
2068 rc = cx231xx_close(filp);
2069 mutex_unlock(&dev->lock);
2074 * cx231xx_v4l2_read()
2075 * will allocate buffers when called for the first time
2078 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2081 struct cx231xx_fh *fh = filp->private_data;
2082 struct cx231xx *dev = fh->dev;
2085 rc = check_dev(dev);
2089 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2090 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2093 if (unlikely(rc < 0))
2096 if (mutex_lock_interruptible(&dev->lock))
2097 return -ERESTARTSYS;
2098 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2099 filp->f_flags & O_NONBLOCK);
2100 mutex_unlock(&dev->lock);
2107 * cx231xx_v4l2_poll()
2108 * will allocate buffers when called for the first time
2110 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
2112 unsigned long req_events = poll_requested_events(wait);
2113 struct cx231xx_fh *fh = filp->private_data;
2114 struct cx231xx *dev = fh->dev;
2118 rc = check_dev(dev);
2124 if (unlikely(rc < 0))
2127 if (v4l2_event_pending(&fh->fh))
2130 poll_wait(filp, &fh->fh.wait, wait);
2132 if (!(req_events & (POLLIN | POLLRDNORM)))
2135 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2136 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
2137 mutex_lock(&dev->lock);
2138 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2139 mutex_unlock(&dev->lock);
2142 return res | POLLERR;
2146 * cx231xx_v4l2_mmap()
2148 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2150 struct cx231xx_fh *fh = filp->private_data;
2151 struct cx231xx *dev = fh->dev;
2154 rc = check_dev(dev);
2160 if (unlikely(rc < 0))
2163 if (mutex_lock_interruptible(&dev->lock))
2164 return -ERESTARTSYS;
2165 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2166 mutex_unlock(&dev->lock);
2168 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2169 (unsigned long)vma->vm_start,
2170 (unsigned long)vma->vm_end -
2171 (unsigned long)vma->vm_start, rc);
2176 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2177 .owner = THIS_MODULE,
2178 .open = cx231xx_v4l2_open,
2179 .release = cx231xx_v4l2_close,
2180 .read = cx231xx_v4l2_read,
2181 .poll = cx231xx_v4l2_poll,
2182 .mmap = cx231xx_v4l2_mmap,
2183 .unlocked_ioctl = video_ioctl2,
2186 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2187 .vidioc_querycap = cx231xx_querycap,
2188 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2189 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2190 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2191 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2192 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2193 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2194 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2195 .vidioc_cropcap = vidioc_cropcap,
2196 .vidioc_reqbufs = vidioc_reqbufs,
2197 .vidioc_querybuf = vidioc_querybuf,
2198 .vidioc_qbuf = vidioc_qbuf,
2199 .vidioc_dqbuf = vidioc_dqbuf,
2200 .vidioc_s_std = vidioc_s_std,
2201 .vidioc_g_std = vidioc_g_std,
2202 .vidioc_enum_input = cx231xx_enum_input,
2203 .vidioc_g_input = cx231xx_g_input,
2204 .vidioc_s_input = cx231xx_s_input,
2205 .vidioc_streamon = vidioc_streamon,
2206 .vidioc_streamoff = vidioc_streamoff,
2207 .vidioc_g_tuner = cx231xx_g_tuner,
2208 .vidioc_s_tuner = cx231xx_s_tuner,
2209 .vidioc_g_frequency = cx231xx_g_frequency,
2210 .vidioc_s_frequency = cx231xx_s_frequency,
2211 .vidioc_g_chip_ident = cx231xx_g_chip_ident,
2212 #ifdef CONFIG_VIDEO_ADV_DEBUG
2213 .vidioc_g_register = cx231xx_g_register,
2214 .vidioc_s_register = cx231xx_s_register,
2216 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2217 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2220 static struct video_device cx231xx_vbi_template;
2222 static const struct video_device cx231xx_video_template = {
2223 .fops = &cx231xx_v4l_fops,
2224 .release = video_device_release,
2225 .ioctl_ops = &video_ioctl_ops,
2226 .tvnorms = V4L2_STD_ALL,
2229 static const struct v4l2_file_operations radio_fops = {
2230 .owner = THIS_MODULE,
2231 .open = cx231xx_v4l2_open,
2232 .release = cx231xx_v4l2_close,
2233 .poll = v4l2_ctrl_poll,
2234 .unlocked_ioctl = video_ioctl2,
2237 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2238 .vidioc_querycap = cx231xx_querycap,
2239 .vidioc_g_tuner = radio_g_tuner,
2240 .vidioc_s_tuner = radio_s_tuner,
2241 .vidioc_g_frequency = cx231xx_g_frequency,
2242 .vidioc_s_frequency = cx231xx_s_frequency,
2243 .vidioc_g_chip_ident = cx231xx_g_chip_ident,
2244 #ifdef CONFIG_VIDEO_ADV_DEBUG
2245 .vidioc_g_register = cx231xx_g_register,
2246 .vidioc_s_register = cx231xx_s_register,
2248 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2249 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2252 static struct video_device cx231xx_radio_template = {
2253 .name = "cx231xx-radio",
2254 .fops = &radio_fops,
2255 .ioctl_ops = &radio_ioctl_ops,
2258 /******************************** usb interface ******************************/
2260 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2261 const struct video_device
2262 *template, const char *type_name)
2264 struct video_device *vfd;
2266 vfd = video_device_alloc();
2271 vfd->v4l2_dev = &dev->v4l2_dev;
2272 vfd->release = video_device_release;
2273 vfd->debug = video_debug;
2274 vfd->lock = &dev->lock;
2275 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2277 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2279 video_set_drvdata(vfd, dev);
2280 if (dev->tuner_type == TUNER_ABSENT) {
2281 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2282 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2283 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2284 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2289 int cx231xx_register_analog_devices(struct cx231xx *dev)
2293 cx231xx_info("%s: v4l2 driver version %s\n",
2294 dev->name, CX231XX_VERSION);
2296 /* set default norm */
2297 dev->norm = V4L2_STD_PAL;
2298 dev->width = norm_maxw(dev);
2299 dev->height = norm_maxh(dev);
2300 dev->interlaced = 0;
2302 /* Analog specific initialization */
2303 dev->format = &format[0];
2305 /* Set the initial input */
2306 video_mux(dev, dev->video_input);
2308 call_all(dev, core, s_std, dev->norm);
2310 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2311 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2313 if (dev->sd_cx25840) {
2314 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2315 dev->sd_cx25840->ctrl_handler, NULL);
2316 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2317 dev->sd_cx25840->ctrl_handler,
2318 v4l2_ctrl_radio_filter);
2321 if (dev->ctrl_handler.error)
2322 return dev->ctrl_handler.error;
2323 if (dev->radio_ctrl_handler.error)
2324 return dev->radio_ctrl_handler.error;
2326 /* enable vbi capturing */
2327 /* write code here... */
2329 /* allocate and fill video video_device struct */
2330 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2332 cx231xx_errdev("cannot allocate video_device.\n");
2336 dev->vdev->ctrl_handler = &dev->ctrl_handler;
2337 /* register v4l2 video video_device */
2338 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2339 video_nr[dev->devno]);
2341 cx231xx_errdev("unable to register video device (error=%i).\n",
2346 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2347 dev->name, video_device_node_name(dev->vdev));
2349 /* Initialize VBI template */
2350 cx231xx_vbi_template = cx231xx_video_template;
2351 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2353 /* Allocate and fill vbi video_device struct */
2354 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2356 if (!dev->vbi_dev) {
2357 cx231xx_errdev("cannot allocate video_device.\n");
2360 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
2361 /* register v4l2 vbi video_device */
2362 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2363 vbi_nr[dev->devno]);
2365 cx231xx_errdev("unable to register vbi device\n");
2369 cx231xx_info("%s/0: registered device %s\n",
2370 dev->name, video_device_node_name(dev->vbi_dev));
2372 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2373 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2375 if (!dev->radio_dev) {
2376 cx231xx_errdev("cannot allocate video_device.\n");
2379 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
2380 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2381 radio_nr[dev->devno]);
2383 cx231xx_errdev("can't register radio device\n");
2386 cx231xx_info("Registered radio device as %s\n",
2387 video_device_node_name(dev->radio_dev));
2390 cx231xx_info("V4L2 device registered as %s and %s\n",
2391 video_device_node_name(dev->vdev),
2392 video_device_node_name(dev->vbi_dev));