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>
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"
47 #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
48 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
50 #define cx231xx_videodbg(fmt, arg...) do {\
52 printk(KERN_INFO "%s %s :"fmt, \
53 dev->name, __func__ , ##arg); } while (0)
55 static unsigned int isoc_debug;
56 module_param(isoc_debug, int, 0644);
57 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
59 #define cx231xx_isocdbg(fmt, arg...) \
62 printk(KERN_INFO "%s %s :"fmt, \
63 dev->name, __func__ , ##arg); \
67 MODULE_AUTHOR(DRIVER_AUTHOR);
68 MODULE_DESCRIPTION(DRIVER_DESC);
69 MODULE_LICENSE("GPL");
71 static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
72 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
73 static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
74 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76 module_param_array(card, int, NULL, 0444);
77 module_param_array(video_nr, int, NULL, 0444);
78 module_param_array(vbi_nr, int, NULL, 0444);
79 module_param_array(radio_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(card, "card type");
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
90 /* supported video standards */
91 static struct cx231xx_fmt format[] = {
93 .name = "16bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
100 /* supported controls */
101 /* Common to all boards */
103 /* ------------------------------------------------------------------- */
105 static const struct v4l2_queryctrl no_ctl = {
107 .flags = V4L2_CTRL_FLAG_DISABLED,
110 static struct cx231xx_ctrl cx231xx_ctls[] = {
114 .id = V4L2_CID_BRIGHTNESS,
115 .name = "Brightness",
119 .default_value = 0x7f,
120 .type = V4L2_CTRL_TYPE_INTEGER,
128 .id = V4L2_CID_CONTRAST,
133 .default_value = 0x3f,
134 .type = V4L2_CTRL_TYPE_INTEGER,
147 .default_value = 0x7f,
148 .type = V4L2_CTRL_TYPE_INTEGER,
155 /* strictly, this only describes only U saturation.
156 * V saturation is handled specially through code.
159 .id = V4L2_CID_SATURATION,
160 .name = "Saturation",
164 .default_value = 0x7f,
165 .type = V4L2_CTRL_TYPE_INTEGER,
174 .id = V4L2_CID_AUDIO_MUTE,
179 .type = V4L2_CTRL_TYPE_BOOLEAN,
182 .mask = (0x1f << 24),
186 .id = V4L2_CID_AUDIO_VOLUME,
191 .default_value = 0x3f,
192 .type = V4L2_CTRL_TYPE_INTEGER,
194 .reg = PATH1_VOL_CTL,
199 static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
201 static const u32 cx231xx_user_ctrls[] = {
207 V4L2_CID_AUDIO_VOLUME,
209 V4L2_CID_AUDIO_BALANCE,
215 static const u32 *ctrl_classes[] = {
220 /* ------------------------------------------------------------------
221 Video buffer and parser functions
222 ------------------------------------------------------------------*/
225 * Announces that a buffer were filled and request the next
227 static inline void buffer_filled(struct cx231xx *dev,
228 struct cx231xx_dmaqueue *dma_q,
229 struct cx231xx_buffer *buf)
231 /* Advice that buffer was filled */
232 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
233 buf->vb.state = VIDEOBUF_DONE;
234 buf->vb.field_count++;
235 do_gettimeofday(&buf->vb.ts);
237 dev->video_mode.isoc_ctl.buf = NULL;
239 list_del(&buf->vb.queue);
240 wake_up(&buf->vb.done);
243 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
245 char *errmsg = "Unknown";
249 errmsg = "unlinked synchronuously";
252 errmsg = "unlinked asynchronuously";
255 errmsg = "Buffer error (overrun)";
258 errmsg = "Stalled (device not responding)";
261 errmsg = "Babble (bad cable?)";
264 errmsg = "Bit-stuff error (bad cable?)";
267 errmsg = "CRC/Timeout (could be anything)";
270 errmsg = "Device does not respond";
274 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
276 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
277 packet, status, errmsg);
282 * video-buf generic routine to get the next available buffer
284 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
285 struct cx231xx_buffer **buf)
287 struct cx231xx_video_mode *vmode =
288 container_of(dma_q, struct cx231xx_video_mode, vidq);
289 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
293 if (list_empty(&dma_q->active)) {
294 cx231xx_isocdbg("No active queue to serve\n");
295 dev->video_mode.isoc_ctl.buf = NULL;
300 /* Get the next buffer */
301 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
303 /* Cleans up buffer - Usefull for testing for frame/URB loss */
304 outp = videobuf_to_vmalloc(&(*buf)->vb);
305 memset(outp, 0, (*buf)->vb.size);
307 dev->video_mode.isoc_ctl.buf = *buf;
313 * Controls the isoc copy of each urb packet
315 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
317 struct cx231xx_buffer *buf;
318 struct cx231xx_dmaqueue *dma_q = urb->context;
319 unsigned char *outp = NULL;
321 unsigned char *p_buffer;
322 u32 bytes_parsed = 0, buffer_size = 0;
328 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
331 if (urb->status < 0) {
332 print_err_status(dev, -1, urb->status);
333 if (urb->status == -ENOENT)
337 buf = dev->video_mode.isoc_ctl.buf;
339 outp = videobuf_to_vmalloc(&buf->vb);
341 for (i = 0; i < urb->number_of_packets; i++) {
342 int status = urb->iso_frame_desc[i].status;
345 print_err_status(dev, i, status);
346 if (urb->iso_frame_desc[i].status != -EPROTO)
350 if (urb->iso_frame_desc[i].actual_length <= 0) {
351 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
354 if (urb->iso_frame_desc[i].actual_length >
355 dev->video_mode.max_pkt_size) {
356 cx231xx_isocdbg("packet bigger than packet size");
360 /* get buffer pointer and length */
361 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
362 buffer_size = urb->iso_frame_desc[i].actual_length;
365 if (dma_q->is_partial_line) {
366 /* Handle the case where we were working on a partial line */
367 sav_eav = dma_q->last_sav;
369 /* Check for a SAV/EAV overlapping the buffer boundary */
371 cx231xx_find_boundary_SAV_EAV(p_buffer,
377 /* Get the first line if we have some portion of an SAV/EAV from
378 the last buffer or a partial line */
380 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
381 sav_eav, /* SAV/EAV */
382 p_buffer + bytes_parsed, /* p_buffer */
383 buffer_size - bytes_parsed); /* buffer size */
386 /* Now parse data that is completely in this buffer */
387 /* dma_q->is_partial_line = 0; */
389 while (bytes_parsed < buffer_size) {
392 sav_eav = cx231xx_find_next_SAV_EAV(p_buffer + bytes_parsed, /* p_buffer */
393 buffer_size - bytes_parsed, /* buffer size */
394 &bytes_used); /* Receives bytes used to get SAV/EAV */
396 bytes_parsed += bytes_used;
399 if (sav_eav && (bytes_parsed < buffer_size)) {
400 bytes_parsed += cx231xx_get_video_line(dev,
401 dma_q, sav_eav, /* SAV/EAV */
402 p_buffer + bytes_parsed, /* p_buffer */
403 buffer_size - bytes_parsed); /* buffer size */
407 /* Save the last four bytes of the buffer so we can check the
408 buffer boundary condition next time */
409 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
416 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
420 u8 boundary_bytes[8];
425 /* Create an array of the last 4 bytes of the last buffer and the first
426 4 bytes of the current buffer. */
428 memcpy(boundary_bytes, partial_buf, 4);
429 memcpy(boundary_bytes + 4, p_buffer, 4);
431 /* Check for the SAV/EAV in the boundary buffer */
432 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
436 /* found a boundary SAV/EAV. Updates the bytes used to reflect
437 only those used in the new buffer */
438 *p_bytes_used = bytes_used - 4;
444 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
450 * Don't search if the buffer size is less than 4. It causes a page
451 * fault since buffer_size - 4 evaluates to a large number in that
454 if (buffer_size < 4) {
455 *p_bytes_used = buffer_size;
459 for (i = 0; i < (buffer_size - 3); i++) {
461 if ((p_buffer[i] == 0xFF) &&
462 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
464 *p_bytes_used = i + 4;
465 sav_eav = p_buffer[i + 3];
470 *p_bytes_used = buffer_size;
474 u32 cx231xx_get_video_line(struct cx231xx *dev,
475 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
476 u8 *p_buffer, u32 buffer_size)
478 u32 bytes_copied = 0;
479 int current_field = -1;
482 case SAV_ACTIVE_VIDEO_FIELD1:
483 /* looking for skipped line which occurred in PAL 720x480 mode.
484 In this case, there will be no active data contained
485 between the SAV and EAV */
486 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
487 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
488 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
489 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
490 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
491 (p_buffer[3] == EAV_VBLANK_FIELD2)))
496 case SAV_ACTIVE_VIDEO_FIELD2:
497 /* looking for skipped line which occurred in PAL 720x480 mode.
498 In this case, there will be no active data contained between
500 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
501 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
502 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
503 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
504 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
505 (p_buffer[3] == EAV_VBLANK_FIELD2)))
511 dma_q->last_sav = sav_eav;
513 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
514 buffer_size, current_field);
519 u32 cx231xx_copy_video_line(struct cx231xx *dev,
520 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
521 u32 length, int field_number)
524 struct cx231xx_buffer *buf;
525 u32 _line_size = dev->width * 2;
527 if (dma_q->current_field != field_number)
528 cx231xx_reset_video_buffer(dev, dma_q);
530 /* get the buffer pointer */
531 buf = dev->video_mode.isoc_ctl.buf;
533 /* Remember the field number for next time */
534 dma_q->current_field = field_number;
536 bytes_to_copy = dma_q->bytes_left_in_line;
537 if (bytes_to_copy > length)
538 bytes_to_copy = length;
540 if (dma_q->lines_completed >= dma_q->lines_per_field) {
541 dma_q->bytes_left_in_line -= bytes_to_copy;
542 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
547 dma_q->is_partial_line = 1;
549 /* If we don't have a buffer, just return the number of bytes we would
550 have copied if we had a buffer. */
552 dma_q->bytes_left_in_line -= bytes_to_copy;
553 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
555 return bytes_to_copy;
558 /* copy the data to video buffer */
559 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
561 dma_q->pos += bytes_to_copy;
562 dma_q->bytes_left_in_line -= bytes_to_copy;
564 if (dma_q->bytes_left_in_line == 0) {
565 dma_q->bytes_left_in_line = _line_size;
566 dma_q->lines_completed++;
567 dma_q->is_partial_line = 0;
569 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
570 buffer_filled(dev, dma_q, buf);
574 dma_q->lines_completed = 0;
578 return bytes_to_copy;
581 void cx231xx_reset_video_buffer(struct cx231xx *dev,
582 struct cx231xx_dmaqueue *dma_q)
584 struct cx231xx_buffer *buf;
586 /* handle the switch from field 1 to field 2 */
587 if (dma_q->current_field == 1) {
588 if (dma_q->lines_completed >= dma_q->lines_per_field)
589 dma_q->field1_done = 1;
591 dma_q->field1_done = 0;
594 buf = dev->video_mode.isoc_ctl.buf;
598 /* first try to get the buffer */
599 get_next_buf(dma_q, &buf);
602 outp = videobuf_to_vmalloc(&buf->vb);
605 dma_q->field1_done = 0;
606 dma_q->current_field = -1;
609 /* reset the counters */
610 dma_q->bytes_left_in_line = dev->width << 1;
611 dma_q->lines_completed = 0;
614 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
615 u8 *p_buffer, u32 bytes_to_copy)
617 u8 *p_out_buffer = NULL;
618 u32 current_line_bytes_copied = 0;
619 struct cx231xx_buffer *buf;
620 u32 _line_size = dev->width << 1;
624 buf = dev->video_mode.isoc_ctl.buf;
629 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
631 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
633 /* Offset field 2 one line from the top of the buffer */
634 offset = (dma_q->current_field == 1) ? 0 : _line_size;
636 /* Offset for field 2 */
637 startwrite = p_out_buffer + offset;
639 /* lines already completed in the current field */
640 startwrite += (dma_q->lines_completed * _line_size * 2);
642 /* bytes already completed in the current line */
643 startwrite += current_line_bytes_copied;
645 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
646 bytes_to_copy : dma_q->bytes_left_in_line;
648 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
651 /* The below copies the UYVY data straight into video buffer */
652 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
657 void cx231xx_swab(u16 *from, u16 *to, u16 len)
664 for (i = 0; i < len / 2; i++)
665 to[i] = (from[i] << 8) | (from[i] >> 8);
668 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
670 u8 buffer_complete = 0;
672 /* Dual field stream */
673 buffer_complete = ((dma_q->current_field == 2) &&
674 (dma_q->lines_completed >= dma_q->lines_per_field) &&
677 return buffer_complete;
680 /* ------------------------------------------------------------------
682 ------------------------------------------------------------------*/
685 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
687 struct cx231xx_fh *fh = vq->priv_data;
688 struct cx231xx *dev = fh->dev;
689 struct v4l2_frequency f;
691 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
693 *count = CX231XX_DEF_BUF;
695 if (*count < CX231XX_MIN_BUF)
696 *count = CX231XX_MIN_BUF;
698 /* Ask tuner to go to analog mode */
699 memset(&f, 0, sizeof(f));
700 f.frequency = dev->ctl_freq;
701 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
703 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
708 /* This is called *without* dev->slock held; please keep it that way */
709 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
711 struct cx231xx_fh *fh = vq->priv_data;
712 struct cx231xx *dev = fh->dev;
713 unsigned long flags = 0;
718 /* We used to wait for the buffer to finish here, but this didn't work
719 because, as we were keeping the state as VIDEOBUF_QUEUED,
720 videobuf_queue_cancel marked it as finished for us.
721 (Also, it could wedge forever if the hardware was misconfigured.)
723 This should be safe; by the time we get here, the buffer isn't
724 queued anymore. If we ever start marking the buffers as
725 VIDEOBUF_ACTIVE, it won't be, though.
727 spin_lock_irqsave(&dev->video_mode.slock, flags);
728 if (dev->video_mode.isoc_ctl.buf == buf)
729 dev->video_mode.isoc_ctl.buf = NULL;
730 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
732 videobuf_vmalloc_free(&buf->vb);
733 buf->vb.state = VIDEOBUF_NEEDS_INIT;
737 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
738 enum v4l2_field field)
740 struct cx231xx_fh *fh = vq->priv_data;
741 struct cx231xx_buffer *buf =
742 container_of(vb, struct cx231xx_buffer, vb);
743 struct cx231xx *dev = fh->dev;
744 int rc = 0, urb_init = 0;
746 /* The only currently supported format is 16 bits/pixel */
747 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
749 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
752 buf->vb.width = dev->width;
753 buf->vb.height = dev->height;
754 buf->vb.field = field;
756 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
757 rc = videobuf_iolock(vq, &buf->vb, NULL);
762 if (!dev->video_mode.isoc_ctl.num_bufs)
766 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
768 dev->video_mode.max_pkt_size,
774 buf->vb.state = VIDEOBUF_PREPARED;
778 free_buffer(vq, buf);
782 static void buffer_queue(struct videobuf_queue *vq, 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 = fh->dev;
788 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
790 buf->vb.state = VIDEOBUF_QUEUED;
791 list_add_tail(&buf->vb.queue, &vidq->active);
795 static void buffer_release(struct videobuf_queue *vq,
796 struct videobuf_buffer *vb)
798 struct cx231xx_buffer *buf =
799 container_of(vb, struct cx231xx_buffer, vb);
800 struct cx231xx_fh *fh = vq->priv_data;
801 struct cx231xx *dev = (struct cx231xx *)fh->dev;
803 cx231xx_isocdbg("cx231xx: called buffer_release\n");
805 free_buffer(vq, buf);
808 static struct videobuf_queue_ops cx231xx_video_qops = {
809 .buf_setup = buffer_setup,
810 .buf_prepare = buffer_prepare,
811 .buf_queue = buffer_queue,
812 .buf_release = buffer_release,
815 /********************* v4l2 interface **************************************/
817 void video_mux(struct cx231xx *dev, int index)
820 struct v4l2_routing route;
822 route.input = INPUT(index)->vmux;
824 dev->video_input = index;
825 dev->ctl_ainput = INPUT(index)->amux;
827 cx231xx_set_video_input_mux(dev, index);
829 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_INT_S_VIDEO_ROUTING,
832 cx231xx_set_audio_input(dev, dev->ctl_ainput);
834 cx231xx_info("video_mux : %d\n", index);
836 /* do mode control overrides if required */
837 cx231xx_do_mode_ctrl_overrides(dev);
840 /* Usage lock check functions */
841 static int res_get(struct cx231xx_fh *fh)
843 struct cx231xx *dev = fh->dev;
846 /* This instance already has stream_on */
850 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
854 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
855 if (dev->vbi_stream_on)
857 dev->vbi_stream_on = 1;
866 static int res_check(struct cx231xx_fh *fh)
868 return fh->stream_on;
871 static void res_free(struct cx231xx_fh *fh)
873 struct cx231xx *dev = fh->dev;
877 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
879 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
880 dev->vbi_stream_on = 0;
883 static int check_dev(struct cx231xx *dev)
885 if (dev->state & DEV_DISCONNECTED) {
886 cx231xx_errdev("v4l2 ioctl: device not present\n");
890 if (dev->state & DEV_MISCONFIGURED) {
891 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
892 "close and open it again\n");
898 void get_scale(struct cx231xx *dev,
899 unsigned int width, unsigned int height,
900 unsigned int *hscale, unsigned int *vscale)
902 unsigned int maxw = norm_maxw(dev);
903 unsigned int maxh = norm_maxh(dev);
905 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
906 if (*hscale >= 0x4000)
909 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
910 if (*vscale >= 0x4000)
913 dev->hscale = *hscale;
914 dev->vscale = *vscale;
918 /* ------------------------------------------------------------------
919 IOCTL vidioc handling
920 ------------------------------------------------------------------*/
922 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
923 struct v4l2_format *f)
925 struct cx231xx_fh *fh = priv;
926 struct cx231xx *dev = fh->dev;
928 mutex_lock(&dev->lock);
930 f->fmt.pix.width = dev->width;
931 f->fmt.pix.height = dev->height;
932 f->fmt.pix.pixelformat = dev->format->fourcc;;
933 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
934 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
935 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
937 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
939 mutex_unlock(&dev->lock);
944 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
948 for (i = 0; i < ARRAY_SIZE(format); i++)
949 if (format[i].fourcc == fourcc)
955 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
956 struct v4l2_format *f)
958 struct cx231xx_fh *fh = priv;
959 struct cx231xx *dev = fh->dev;
960 int width = f->fmt.pix.width;
961 int height = f->fmt.pix.height;
962 unsigned int maxw = norm_maxw(dev);
963 unsigned int maxh = norm_maxh(dev);
964 unsigned int hscale, vscale;
965 struct cx231xx_fmt *fmt;
967 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
969 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
970 f->fmt.pix.pixelformat);
974 /* width must even because of the YUYV format
975 height must be even because of interlacing */
979 if (unlikely(height < 32))
981 if (unlikely(height > maxh))
983 if (unlikely(width < 48))
985 if (unlikely(width > maxw))
988 get_scale(dev, width, height, &hscale, &vscale);
990 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
991 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
993 f->fmt.pix.width = width;
994 f->fmt.pix.height = height;
995 f->fmt.pix.pixelformat = fmt->fourcc;
996 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
997 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
998 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
999 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1004 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1005 struct v4l2_format *f)
1007 struct cx231xx_fh *fh = priv;
1008 struct cx231xx *dev = fh->dev;
1010 struct cx231xx_fmt *fmt;
1012 rc = check_dev(dev);
1016 mutex_lock(&dev->lock);
1018 vidioc_try_fmt_vid_cap(file, priv, f);
1020 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1026 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1027 cx231xx_errdev("%s queue busy\n", __func__);
1032 if (dev->stream_on && !fh->stream_on) {
1033 cx231xx_errdev("%s device in use by another fh\n", __func__);
1038 /* set new image size */
1039 dev->width = f->fmt.pix.width;
1040 dev->height = f->fmt.pix.height;
1042 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1044 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_FMT, f);
1046 /* Set the correct alternate setting for this resolution */
1047 cx231xx_resolution_set(dev);
1050 mutex_unlock(&dev->lock);
1054 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
1056 struct cx231xx_fh *fh = priv;
1057 struct cx231xx *dev = fh->dev;
1063 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1065 struct cx231xx_fh *fh = priv;
1066 struct cx231xx *dev = fh->dev;
1067 struct v4l2_format f;
1070 rc = check_dev(dev);
1074 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1076 mutex_lock(&dev->lock);
1079 /* Adjusts width/height, if needed */
1080 f.fmt.pix.width = dev->width;
1081 f.fmt.pix.height = dev->height;
1082 vidioc_try_fmt_vid_cap(file, priv, &f);
1084 /* set new image size */
1085 dev->width = f.fmt.pix.width;
1086 dev->height = f.fmt.pix.height;
1087 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1089 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_STD, &dev->norm);
1091 mutex_unlock(&dev->lock);
1093 cx231xx_resolution_set(dev);
1095 /* do mode control overrides */
1096 cx231xx_do_mode_ctrl_overrides(dev);
1101 static const char *iname[] = {
1102 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1103 [CX231XX_VMUX_SVIDEO] = "S-Video",
1104 [CX231XX_VMUX_TELEVISION] = "Television",
1105 [CX231XX_VMUX_CABLE] = "Cable TV",
1106 [CX231XX_VMUX_DVB] = "DVB",
1107 [CX231XX_VMUX_DEBUG] = "for debug only",
1110 static int vidioc_enum_input(struct file *file, void *priv,
1111 struct v4l2_input *i)
1113 struct cx231xx_fh *fh = priv;
1114 struct cx231xx *dev = fh->dev;
1118 if (n >= MAX_CX231XX_INPUT)
1120 if (0 == INPUT(n)->type)
1124 i->type = V4L2_INPUT_TYPE_CAMERA;
1126 strcpy(i->name, iname[INPUT(n)->type]);
1128 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1129 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1130 i->type = V4L2_INPUT_TYPE_TUNER;
1132 i->std = dev->vdev->tvnorms;
1137 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1139 struct cx231xx_fh *fh = priv;
1140 struct cx231xx *dev = fh->dev;
1142 *i = dev->video_input;
1147 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1149 struct cx231xx_fh *fh = priv;
1150 struct cx231xx *dev = fh->dev;
1153 rc = check_dev(dev);
1157 if (i >= MAX_CX231XX_INPUT)
1159 if (0 == INPUT(i)->type)
1162 mutex_lock(&dev->lock);
1166 mutex_unlock(&dev->lock);
1170 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1172 struct cx231xx_fh *fh = priv;
1173 struct cx231xx *dev = fh->dev;
1176 case CX231XX_AMUX_VIDEO:
1177 strcpy(a->name, "Television");
1179 case CX231XX_AMUX_LINE_IN:
1180 strcpy(a->name, "Line In");
1186 a->index = dev->ctl_ainput;
1187 a->capability = V4L2_AUDCAP_STEREO;
1192 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1194 struct cx231xx_fh *fh = priv;
1195 struct cx231xx *dev = fh->dev;
1198 /* Doesn't allow manual routing */
1199 if (a->index != dev->ctl_ainput)
1202 dev->ctl_ainput = INPUT(a->index)->amux;
1203 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1208 static int vidioc_queryctrl(struct file *file, void *priv,
1209 struct v4l2_queryctrl *qc)
1211 struct cx231xx_fh *fh = priv;
1212 struct cx231xx *dev = fh->dev;
1217 rc = check_dev(dev);
1221 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1222 if (unlikely(qc->id == 0))
1225 memset(qc, 0, sizeof(*qc));
1229 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1232 for (i = 0; i < CX231XX_CTLS; i++)
1233 if (cx231xx_ctls[i].v.id == qc->id)
1236 if (i == CX231XX_CTLS) {
1240 *qc = cx231xx_ctls[i].v;
1242 mutex_lock(&dev->lock);
1243 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_QUERYCTRL, qc);
1244 mutex_unlock(&dev->lock);
1252 static int vidioc_g_ctrl(struct file *file, void *priv,
1253 struct v4l2_control *ctrl)
1255 struct cx231xx_fh *fh = priv;
1256 struct cx231xx *dev = fh->dev;
1259 rc = check_dev(dev);
1263 mutex_lock(&dev->lock);
1265 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_CTRL, ctrl);
1267 mutex_unlock(&dev->lock);
1271 static int vidioc_s_ctrl(struct file *file, void *priv,
1272 struct v4l2_control *ctrl)
1274 struct cx231xx_fh *fh = priv;
1275 struct cx231xx *dev = fh->dev;
1278 rc = check_dev(dev);
1282 mutex_lock(&dev->lock);
1284 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_CTRL, ctrl);
1286 mutex_unlock(&dev->lock);
1290 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1292 struct cx231xx_fh *fh = priv;
1293 struct cx231xx *dev = fh->dev;
1296 rc = check_dev(dev);
1303 strcpy(t->name, "Tuner");
1305 t->type = V4L2_TUNER_ANALOG_TV;
1306 t->capability = V4L2_TUNER_CAP_NORM;
1307 t->rangehigh = 0xffffffffUL;
1308 t->signal = 0xffff; /* LOCKED */
1313 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1315 struct cx231xx_fh *fh = priv;
1316 struct cx231xx *dev = fh->dev;
1319 rc = check_dev(dev);
1326 mutex_lock(&dev->lock);
1328 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1330 mutex_unlock(&dev->lock);
1335 static int vidioc_g_frequency(struct file *file, void *priv,
1336 struct v4l2_frequency *f)
1338 struct cx231xx_fh *fh = priv;
1339 struct cx231xx *dev = fh->dev;
1341 mutex_lock(&dev->lock);
1342 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1343 f->frequency = dev->ctl_freq;
1345 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1347 mutex_unlock(&dev->lock);
1352 static int vidioc_s_frequency(struct file *file, void *priv,
1353 struct v4l2_frequency *f)
1355 struct cx231xx_fh *fh = priv;
1356 struct cx231xx *dev = fh->dev;
1359 rc = check_dev(dev);
1366 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1368 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1371 /* set pre channel change settings in DIF first */
1372 rc = cx231xx_tuner_pre_channel_change(dev);
1374 mutex_lock(&dev->lock);
1376 dev->ctl_freq = f->frequency;
1378 if (dev->tuner_type == TUNER_XC5000) {
1379 if (dev->cx231xx_set_analog_freq != NULL)
1380 dev->cx231xx_set_analog_freq(dev, f->frequency);
1382 cx231xx_i2c_call_clients(&dev->i2c_bus[1],
1383 VIDIOC_S_FREQUENCY, f);
1386 mutex_unlock(&dev->lock);
1388 /* set post channel change settings in DIF first */
1389 rc = cx231xx_tuner_post_channel_change(dev);
1391 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1396 #ifdef CONFIG_VIDEO_ADV_DEBUG
1399 -R, --list-registers=type=<host/i2cdrv/i2caddr>,chip=<chip>[,min=<addr>,max=<addr>]
1400 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1401 -r, --set-register=type=<host/i2cdrv/i2caddr>,chip=<chip>,reg=<addr>,val=<val>
1402 set the register [VIDIOC_DBG_S_REGISTER]
1404 if type == host, then <chip> is the hosts chip ID (default 0)
1405 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1406 if type == i2caddr, then <chip> is the 7-bit I2C address
1409 static int vidioc_g_register(struct file *file, void *priv,
1410 struct v4l2_dbg_register *reg)
1412 struct cx231xx_fh *fh = priv;
1413 struct cx231xx *dev = fh->dev;
1415 u8 value[4] = { 0, 0, 0, 0 };
1418 switch (reg->match.type) {
1419 case V4L2_CHIP_MATCH_HOST:
1420 switch (reg->match.addr) {
1421 case 0: /* Cx231xx - internal registers */
1422 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1423 (u16)reg->reg, value, 4);
1424 reg->val = value[0] | value[1] << 8 |
1425 value[2] << 16 | value[3] << 24;
1427 case 1: /* Colibri - read byte */
1428 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1429 (u16)reg->reg, 2, &data, 1);
1430 reg->val = le32_to_cpu(data & 0xff);
1432 case 14: /* Colibri - read dword */
1433 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1434 (u16)reg->reg, 2, &data, 4);
1435 reg->val = le32_to_cpu(data);
1437 case 2: /* Hammerhead - read byte */
1438 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1439 (u16)reg->reg, 2, &data, 1);
1440 reg->val = le32_to_cpu(data & 0xff);
1442 case 24: /* Hammerhead - read dword */
1443 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1444 (u16)reg->reg, 2, &data, 4);
1445 reg->val = le32_to_cpu(data);
1447 case 3: /* flatiron - read byte */
1448 ret = cx231xx_read_i2c_data(dev,
1449 Flatrion_DEVICE_ADDRESS,
1452 reg->val = le32_to_cpu(data & 0xff);
1454 case 34: /* flatiron - read dword */
1456 cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1457 (u16)reg->reg, 1, &data, 4);
1458 reg->val = le32_to_cpu(data);
1461 return ret < 0 ? ret : 0;
1463 case V4L2_CHIP_MATCH_I2C_DRIVER:
1464 cx231xx_i2c_call_clients(&dev->i2c_bus[0],
1465 VIDIOC_DBG_G_REGISTER, reg);
1467 case V4L2_CHIP_MATCH_I2C_ADDR:
1468 /* Not supported yet */
1471 if (!v4l2_chip_match_host(®->match))
1475 mutex_lock(&dev->lock);
1476 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1477 mutex_unlock(&dev->lock);
1482 static int vidioc_s_register(struct file *file, void *priv,
1483 struct v4l2_dbg_register *reg)
1485 struct cx231xx_fh *fh = priv;
1486 struct cx231xx *dev = fh->dev;
1490 u8 data[4] = { 0, 0, 0, 0 };
1492 buf = cpu_to_le64(reg->val);
1494 switch (reg->match.type) {
1495 case V4L2_CHIP_MATCH_HOST:
1497 value = (u32) buf & 0xffffffff;
1499 switch (reg->match.addr) {
1500 case 0: /* cx231xx internal registers */
1501 data[0] = (u8) value;
1502 data[1] = (u8) (value >> 8);
1503 data[2] = (u8) (value >> 16);
1504 data[3] = (u8) (value >> 24);
1505 ret = cx231xx_write_ctrl_reg(dev,
1507 (u16)reg->reg, data,
1510 case 1: /* Colibri - read byte */
1511 ret = cx231xx_write_i2c_data(dev,
1512 Colibri_DEVICE_ADDRESS,
1516 case 14: /* Colibri - read dword */
1517 ret = cx231xx_write_i2c_data(dev,
1518 Colibri_DEVICE_ADDRESS,
1522 case 2: /* Hammerhead - read byte */
1524 cx231xx_write_i2c_data(dev,
1525 HAMMERHEAD_I2C_ADDRESS,
1529 case 24: /* Hammerhead - read dword */
1531 cx231xx_write_i2c_data(dev,
1532 HAMMERHEAD_I2C_ADDRESS,
1536 case 3: /* flatiron - read byte */
1538 cx231xx_write_i2c_data(dev,
1539 Flatrion_DEVICE_ADDRESS,
1543 case 34: /* flatiron - read dword */
1545 cx231xx_write_i2c_data(dev,
1546 Flatrion_DEVICE_ADDRESS,
1552 return ret < 0 ? ret : 0;
1558 mutex_lock(&dev->lock);
1560 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_S_REGISTER, reg);
1562 mutex_unlock(&dev->lock);
1568 static int vidioc_cropcap(struct file *file, void *priv,
1569 struct v4l2_cropcap *cc)
1571 struct cx231xx_fh *fh = priv;
1572 struct cx231xx *dev = fh->dev;
1574 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1577 cc->bounds.left = 0;
1579 cc->bounds.width = dev->width;
1580 cc->bounds.height = dev->height;
1581 cc->defrect = cc->bounds;
1582 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1583 cc->pixelaspect.denominator = 59;
1588 static int vidioc_streamon(struct file *file, void *priv,
1589 enum v4l2_buf_type type)
1591 struct cx231xx_fh *fh = priv;
1592 struct cx231xx *dev = fh->dev;
1595 rc = check_dev(dev);
1599 mutex_lock(&dev->lock);
1602 if (likely(rc >= 0))
1603 rc = videobuf_streamon(&fh->vb_vidq);
1605 mutex_unlock(&dev->lock);
1610 static int vidioc_streamoff(struct file *file, void *priv,
1611 enum v4l2_buf_type type)
1613 struct cx231xx_fh *fh = priv;
1614 struct cx231xx *dev = fh->dev;
1617 rc = check_dev(dev);
1621 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1622 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1624 if (type != fh->type)
1627 mutex_lock(&dev->lock);
1629 videobuf_streamoff(&fh->vb_vidq);
1632 mutex_unlock(&dev->lock);
1637 static int vidioc_querycap(struct file *file, void *priv,
1638 struct v4l2_capability *cap)
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 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
1646 sizeof(cap->bus_info));
1648 cap->version = CX231XX_VERSION_CODE;
1650 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1652 V4L2_CAP_SLICED_VBI_CAPTURE |
1654 V4L2_CAP_VIDEO_CAPTURE |
1655 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1657 if (dev->tuner_type != TUNER_ABSENT)
1658 cap->capabilities |= V4L2_CAP_TUNER;
1663 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1664 struct v4l2_fmtdesc *f)
1666 if (unlikely(f->index >= ARRAY_SIZE(format)))
1669 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1670 f->pixelformat = format[f->index].fourcc;
1675 /* Sliced VBI ioctls */
1676 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1677 struct v4l2_format *f)
1679 struct cx231xx_fh *fh = priv;
1680 struct cx231xx *dev = fh->dev;
1683 rc = check_dev(dev);
1687 mutex_lock(&dev->lock);
1689 f->fmt.sliced.service_set = 0;
1691 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1693 if (f->fmt.sliced.service_set == 0)
1696 mutex_unlock(&dev->lock);
1700 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1701 struct v4l2_format *f)
1703 struct cx231xx_fh *fh = priv;
1704 struct cx231xx *dev = fh->dev;
1707 rc = check_dev(dev);
1711 mutex_lock(&dev->lock);
1712 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1713 mutex_unlock(&dev->lock);
1715 if (f->fmt.sliced.service_set == 0)
1721 /* RAW VBI ioctls */
1723 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1724 struct v4l2_format *f)
1726 struct cx231xx_fh *fh = priv;
1727 struct cx231xx *dev = fh->dev;
1729 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1730 35468950 : 28636363;
1731 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1732 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1733 f->fmt.vbi.offset = 64 * 4;
1734 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1735 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1736 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1737 PAL_VBI_LINES : NTSC_VBI_LINES;
1738 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1739 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1740 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1746 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1747 struct v4l2_format *f)
1749 struct cx231xx_fh *fh = priv;
1750 struct cx231xx *dev = fh->dev;
1752 if (dev->vbi_stream_on && !fh->stream_on) {
1753 cx231xx_errdev("%s device in use by another fh\n", __func__);
1757 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1758 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1759 35468950 : 28636363;
1760 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1761 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1762 f->fmt.vbi.offset = 244;
1763 f->fmt.vbi.flags = 0;
1764 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1765 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1766 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1767 PAL_VBI_LINES : NTSC_VBI_LINES;
1768 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1769 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1770 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1776 static int vidioc_reqbufs(struct file *file, void *priv,
1777 struct v4l2_requestbuffers *rb)
1779 struct cx231xx_fh *fh = priv;
1780 struct cx231xx *dev = fh->dev;
1783 rc = check_dev(dev);
1787 return videobuf_reqbufs(&fh->vb_vidq, rb);
1790 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1792 struct cx231xx_fh *fh = priv;
1793 struct cx231xx *dev = fh->dev;
1796 rc = check_dev(dev);
1800 return videobuf_querybuf(&fh->vb_vidq, b);
1803 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1805 struct cx231xx_fh *fh = priv;
1806 struct cx231xx *dev = fh->dev;
1809 rc = check_dev(dev);
1813 return videobuf_qbuf(&fh->vb_vidq, b);
1816 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1818 struct cx231xx_fh *fh = priv;
1819 struct cx231xx *dev = fh->dev;
1822 rc = check_dev(dev);
1826 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1829 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1830 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1832 struct cx231xx_fh *fh = priv;
1834 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1838 /* ----------------------------------------------------------- */
1839 /* RADIO ESPECIFIC IOCTLS */
1840 /* ----------------------------------------------------------- */
1842 static int radio_querycap(struct file *file, void *priv,
1843 struct v4l2_capability *cap)
1845 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1847 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1848 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1849 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1851 cap->version = CX231XX_VERSION_CODE;
1852 cap->capabilities = V4L2_CAP_TUNER;
1856 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1858 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1860 if (unlikely(t->index > 0))
1863 strcpy(t->name, "Radio");
1864 t->type = V4L2_TUNER_RADIO;
1866 mutex_lock(&dev->lock);
1867 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
1868 mutex_unlock(&dev->lock);
1873 static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1877 strcpy(i->name, "Radio");
1878 i->type = V4L2_INPUT_TYPE_TUNER;
1883 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1885 if (unlikely(a->index))
1888 strcpy(a->name, "Radio");
1892 static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1894 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1899 mutex_lock(&dev->lock);
1900 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1901 mutex_unlock(&dev->lock);
1906 static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1911 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1916 static int radio_queryctrl(struct file *file, void *priv,
1917 struct v4l2_queryctrl *c)
1921 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
1923 if (c->id == V4L2_CID_AUDIO_MUTE) {
1924 for (i = 0; i < CX231XX_CTLS; i++)
1925 if (cx231xx_ctls[i].v.id == c->id)
1927 *c = cx231xx_ctls[i].v;
1934 * cx231xx_v4l2_open()
1935 * inits the device and starts isoc transfer
1937 static int cx231xx_v4l2_open(struct file *filp)
1939 int minor = video_devdata(filp)->minor;
1940 int errCode = 0, radio = 0;
1941 struct cx231xx *dev = NULL;
1942 struct cx231xx_fh *fh;
1943 enum v4l2_buf_type fh_type = 0;
1945 dev = cx231xx_get_device(minor, &fh_type, &radio);
1949 mutex_lock(&dev->lock);
1951 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
1952 minor, v4l2_type_names[fh_type], dev->users);
1955 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1958 ("Device locked on digital mode. Can't open analog\n");
1959 mutex_unlock(&dev->lock);
1964 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1966 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1967 mutex_unlock(&dev->lock);
1973 filp->private_data = fh;
1975 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1976 dev->width = norm_maxw(dev);
1977 dev->height = norm_maxh(dev);
1981 /* Power up in Analog TV mode */
1982 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1985 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1987 cx231xx_resolution_set(dev);
1989 /* set video alternate setting */
1990 cx231xx_set_video_alternate(dev);
1992 /* Needed, since GPIO might have disabled power of
1994 cx231xx_config_i2c(dev);
1996 /* device needs to be initialized before isoc transfer */
1997 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1998 video_mux(dev, dev->video_input);
2002 cx231xx_videodbg("video_open: setting radio device\n");
2004 /* cx231xx_start_radio(dev); */
2006 cx231xx_i2c_call_clients(&dev->i2c_bus[1], AUDC_SET_RADIO,
2012 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2013 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2014 NULL, &dev->video_mode.slock,
2015 fh->type, V4L2_FIELD_INTERLACED,
2016 sizeof(struct cx231xx_buffer), fh);
2017 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2018 /* Set the required alternate setting VBI interface works in
2020 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2022 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2023 NULL, &dev->vbi_mode.slock,
2024 fh->type, V4L2_FIELD_SEQ_TB,
2025 sizeof(struct cx231xx_buffer), fh);
2028 mutex_unlock(&dev->lock);
2034 * cx231xx_realease_resources()
2035 * unregisters the v4l2,i2c and usb devices
2036 * called when the device gets disconected or at module unload
2038 void cx231xx_release_analog_resources(struct cx231xx *dev)
2041 /*FIXME: I2C IR should be disconnected */
2043 if (dev->radio_dev) {
2044 if (-1 != dev->radio_dev->minor)
2045 video_unregister_device(dev->radio_dev);
2047 video_device_release(dev->radio_dev);
2048 dev->radio_dev = NULL;
2051 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
2053 if (-1 != dev->vbi_dev->minor)
2054 video_unregister_device(dev->vbi_dev);
2056 video_device_release(dev->vbi_dev);
2057 dev->vbi_dev = NULL;
2060 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
2062 if (-1 != dev->vdev->minor)
2063 video_unregister_device(dev->vdev);
2065 video_device_release(dev->vdev);
2071 * cx231xx_v4l2_close()
2072 * stops streaming and deallocates all resources allocated by the v4l2
2075 static int cx231xx_v4l2_close(struct file *filp)
2077 struct cx231xx_fh *fh = filp->private_data;
2078 struct cx231xx *dev = fh->dev;
2080 cx231xx_videodbg("users=%d\n", dev->users);
2082 mutex_lock(&dev->lock);
2087 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2088 videobuf_stop(&fh->vb_vidq);
2089 videobuf_mmap_free(&fh->vb_vidq);
2091 /* the device is already disconnect,
2092 free the remaining resources */
2093 if (dev->state & DEV_DISCONNECTED) {
2094 cx231xx_release_resources(dev);
2095 mutex_unlock(&dev->lock);
2100 /* do this before setting alternate! */
2101 cx231xx_uninit_vbi_isoc(dev);
2103 /* set alternate 0 */
2104 if (!dev->vbi_or_sliced_cc_mode)
2105 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2107 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2111 wake_up_interruptible_nr(&dev->open, 1);
2112 mutex_unlock(&dev->lock);
2116 if (dev->users == 1) {
2117 videobuf_stop(&fh->vb_vidq);
2118 videobuf_mmap_free(&fh->vb_vidq);
2120 /* the device is already disconnect,
2121 free the remaining resources */
2122 if (dev->state & DEV_DISCONNECTED) {
2123 cx231xx_release_resources(dev);
2124 mutex_unlock(&dev->lock);
2129 /* Save some power by putting tuner to sleep */
2130 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_STANDBY,
2133 /* do this before setting alternate! */
2134 cx231xx_uninit_isoc(dev);
2135 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2137 /* set alternate 0 */
2138 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2142 wake_up_interruptible_nr(&dev->open, 1);
2143 mutex_unlock(&dev->lock);
2148 * cx231xx_v4l2_read()
2149 * will allocate buffers when called for the first time
2152 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2155 struct cx231xx_fh *fh = filp->private_data;
2156 struct cx231xx *dev = fh->dev;
2159 rc = check_dev(dev);
2163 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2164 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2165 mutex_lock(&dev->lock);
2167 mutex_unlock(&dev->lock);
2169 if (unlikely(rc < 0))
2172 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2173 filp->f_flags & O_NONBLOCK);
2179 * cx231xx_v4l2_poll()
2180 * will allocate buffers when called for the first time
2182 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2184 struct cx231xx_fh *fh = filp->private_data;
2185 struct cx231xx *dev = fh->dev;
2188 rc = check_dev(dev);
2192 mutex_lock(&dev->lock);
2194 mutex_unlock(&dev->lock);
2196 if (unlikely(rc < 0))
2199 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2200 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2201 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2207 * cx231xx_v4l2_mmap()
2209 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2211 struct cx231xx_fh *fh = filp->private_data;
2212 struct cx231xx *dev = fh->dev;
2215 rc = check_dev(dev);
2219 mutex_lock(&dev->lock);
2221 mutex_unlock(&dev->lock);
2223 if (unlikely(rc < 0))
2226 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2228 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2229 (unsigned long)vma->vm_start,
2230 (unsigned long)vma->vm_end -
2231 (unsigned long)vma->vm_start, rc);
2236 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2237 .owner = THIS_MODULE,
2238 .open = cx231xx_v4l2_open,
2239 .release = cx231xx_v4l2_close,
2240 .read = cx231xx_v4l2_read,
2241 .poll = cx231xx_v4l2_poll,
2242 .mmap = cx231xx_v4l2_mmap,
2243 .ioctl = video_ioctl2,
2246 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2247 .vidioc_querycap = vidioc_querycap,
2248 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2249 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2250 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2251 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2252 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2253 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2254 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2255 .vidioc_g_audio = vidioc_g_audio,
2256 .vidioc_s_audio = vidioc_s_audio,
2257 .vidioc_cropcap = vidioc_cropcap,
2258 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2259 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2260 .vidioc_reqbufs = vidioc_reqbufs,
2261 .vidioc_querybuf = vidioc_querybuf,
2262 .vidioc_qbuf = vidioc_qbuf,
2263 .vidioc_dqbuf = vidioc_dqbuf,
2264 .vidioc_s_std = vidioc_s_std,
2265 .vidioc_g_std = vidioc_g_std,
2266 .vidioc_enum_input = vidioc_enum_input,
2267 .vidioc_g_input = vidioc_g_input,
2268 .vidioc_s_input = vidioc_s_input,
2269 .vidioc_queryctrl = vidioc_queryctrl,
2270 .vidioc_g_ctrl = vidioc_g_ctrl,
2271 .vidioc_s_ctrl = vidioc_s_ctrl,
2272 .vidioc_streamon = vidioc_streamon,
2273 .vidioc_streamoff = vidioc_streamoff,
2274 .vidioc_g_tuner = vidioc_g_tuner,
2275 .vidioc_s_tuner = vidioc_s_tuner,
2276 .vidioc_g_frequency = vidioc_g_frequency,
2277 .vidioc_s_frequency = vidioc_s_frequency,
2278 #ifdef CONFIG_VIDEO_ADV_DEBUG
2279 .vidioc_g_register = vidioc_g_register,
2280 .vidioc_s_register = vidioc_s_register,
2282 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2283 .vidiocgmbuf = vidiocgmbuf,
2287 static struct video_device cx231xx_vbi_template;
2289 static const struct video_device cx231xx_video_template = {
2290 .fops = &cx231xx_v4l_fops,
2291 .release = video_device_release,
2292 .ioctl_ops = &video_ioctl_ops,
2294 .tvnorms = V4L2_STD_ALL,
2295 .current_norm = V4L2_STD_PAL,
2298 static const struct v4l2_file_operations radio_fops = {
2299 .owner = THIS_MODULE,
2300 .open = cx231xx_v4l2_open,
2301 .release = cx231xx_v4l2_close,
2302 .ioctl = video_ioctl2,
2305 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2306 .vidioc_querycap = radio_querycap,
2307 .vidioc_g_tuner = radio_g_tuner,
2308 .vidioc_enum_input = radio_enum_input,
2309 .vidioc_g_audio = radio_g_audio,
2310 .vidioc_s_tuner = radio_s_tuner,
2311 .vidioc_s_audio = radio_s_audio,
2312 .vidioc_s_input = radio_s_input,
2313 .vidioc_queryctrl = radio_queryctrl,
2314 .vidioc_g_ctrl = vidioc_g_ctrl,
2315 .vidioc_s_ctrl = vidioc_s_ctrl,
2316 .vidioc_g_frequency = vidioc_g_frequency,
2317 .vidioc_s_frequency = vidioc_s_frequency,
2318 #ifdef CONFIG_VIDEO_ADV_DEBUG
2319 .vidioc_g_register = vidioc_g_register,
2320 .vidioc_s_register = vidioc_s_register,
2324 static struct video_device cx231xx_radio_template = {
2325 .name = "cx231xx-radio",
2326 .fops = &radio_fops,
2327 .ioctl_ops = &radio_ioctl_ops,
2331 /******************************** usb interface ******************************/
2333 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev, const struct video_device
2334 *template, const char *type_name)
2336 struct video_device *vfd;
2338 vfd = video_device_alloc();
2344 vfd->parent = &dev->udev->dev;
2345 vfd->release = video_device_release;
2346 vfd->debug = video_debug;
2348 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2353 int cx231xx_register_analog_devices(struct cx231xx *dev)
2357 cx231xx_info("%s()\n", __func__);
2359 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2361 (CX231XX_VERSION_CODE >> 16) & 0xff,
2362 (CX231XX_VERSION_CODE >> 8) & 0xff,
2363 CX231XX_VERSION_CODE & 0xff);
2365 /* set default norm */
2366 /*dev->norm = cx231xx_video_template.current_norm; */
2367 dev->width = norm_maxw(dev);
2368 dev->height = norm_maxh(dev);
2369 dev->interlaced = 0;
2373 /* Analog specific initialization */
2374 dev->format = &format[0];
2375 /* video_mux(dev, dev->video_input); */
2377 /* Audio defaults */
2381 /* enable vbi capturing */
2382 /* write code here... */
2384 /* allocate and fill video video_device struct */
2385 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2387 cx231xx_errdev("cannot allocate video_device.\n");
2391 /* register v4l2 video video_device */
2392 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2393 video_nr[dev->devno]);
2395 cx231xx_errdev("unable to register video device (error=%i).\n",
2400 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2401 dev->name, dev->vdev->num);
2403 /* Initialize VBI template */
2404 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2405 sizeof(cx231xx_vbi_template));
2406 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2408 /* Allocate and fill vbi video_device struct */
2409 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2411 /* register v4l2 vbi video_device */
2412 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2413 vbi_nr[dev->devno]);
2415 cx231xx_errdev("unable to register vbi device\n");
2419 cx231xx_info("%s/0: registered device vbi%d\n",
2420 dev->name, dev->vbi_dev->num);
2422 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2423 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2425 if (!dev->radio_dev) {
2426 cx231xx_errdev("cannot allocate video_device.\n");
2429 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2430 radio_nr[dev->devno]);
2432 cx231xx_errdev("can't register radio device\n");
2435 cx231xx_info("Registered radio device as /dev/radio%d\n",
2436 dev->radio_dev->num);
2439 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2440 dev->vdev->num, dev->vbi_dev->num);