GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / cx231xx / cx231xx-video.c
blob6d4f66d9fddfb208d3cec191865debf4679730b0
1 /*
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>
6 Based on em28xx driver
7 Based on cx23885 driver
8 Based on cx88 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>
33 #include <linux/mm.h>
34 #include <linux/mutex.h>
35 #include <linux/slab.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.h>
43 #include "dvb_frontend.h"
45 #include "cx231xx.h"
46 #include "cx231xx-vbi.h"
48 #define CX231XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
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 {\
54 if (video_debug) \
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...) \
63 do {\
64 if (isoc_debug) { \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
67 } \
68 } while (0)
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
74 static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
79 module_param_array(card, int, NULL, 0444);
80 module_param_array(video_nr, int, NULL, 0444);
81 module_param_array(vbi_nr, int, NULL, 0444);
82 module_param_array(radio_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(card, "card type");
85 MODULE_PARM_DESC(video_nr, "video device numbers");
86 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87 MODULE_PARM_DESC(radio_nr, "radio device numbers");
89 static unsigned int video_debug;
90 module_param(video_debug, int, 0644);
91 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
93 /* supported video standards */
94 static struct cx231xx_fmt format[] = {
96 .name = "16bpp YUY2, 4:2:2, packed",
97 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
99 .reg = 0,
103 /* supported controls */
104 /* Common to all boards */
106 /* ------------------------------------------------------------------- */
108 static const struct v4l2_queryctrl no_ctl = {
109 .name = "42",
110 .flags = V4L2_CTRL_FLAG_DISABLED,
113 static struct cx231xx_ctrl cx231xx_ctls[] = {
114 /* --- video --- */
116 .v = {
117 .id = V4L2_CID_BRIGHTNESS,
118 .name = "Brightness",
119 .minimum = 0x00,
120 .maximum = 0xff,
121 .step = 1,
122 .default_value = 0x7f,
123 .type = V4L2_CTRL_TYPE_INTEGER,
125 .off = 128,
126 .reg = LUMA_CTRL,
127 .mask = 0x00ff,
128 .shift = 0,
129 }, {
130 .v = {
131 .id = V4L2_CID_CONTRAST,
132 .name = "Contrast",
133 .minimum = 0,
134 .maximum = 0xff,
135 .step = 1,
136 .default_value = 0x3f,
137 .type = V4L2_CTRL_TYPE_INTEGER,
139 .off = 0,
140 .reg = LUMA_CTRL,
141 .mask = 0xff00,
142 .shift = 8,
143 }, {
144 .v = {
145 .id = V4L2_CID_HUE,
146 .name = "Hue",
147 .minimum = 0,
148 .maximum = 0xff,
149 .step = 1,
150 .default_value = 0x7f,
151 .type = V4L2_CTRL_TYPE_INTEGER,
153 .off = 128,
154 .reg = CHROMA_CTRL,
155 .mask = 0xff0000,
156 .shift = 16,
157 }, {
158 /* strictly, this only describes only U saturation.
159 * V saturation is handled specially through code.
161 .v = {
162 .id = V4L2_CID_SATURATION,
163 .name = "Saturation",
164 .minimum = 0,
165 .maximum = 0xff,
166 .step = 1,
167 .default_value = 0x7f,
168 .type = V4L2_CTRL_TYPE_INTEGER,
170 .off = 0,
171 .reg = CHROMA_CTRL,
172 .mask = 0x00ff,
173 .shift = 0,
174 }, {
175 /* --- audio --- */
176 .v = {
177 .id = V4L2_CID_AUDIO_MUTE,
178 .name = "Mute",
179 .minimum = 0,
180 .maximum = 1,
181 .default_value = 1,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
184 .reg = PATH1_CTL1,
185 .mask = (0x1f << 24),
186 .shift = 24,
187 }, {
188 .v = {
189 .id = V4L2_CID_AUDIO_VOLUME,
190 .name = "Volume",
191 .minimum = 0,
192 .maximum = 0x3f,
193 .step = 1,
194 .default_value = 0x3f,
195 .type = V4L2_CTRL_TYPE_INTEGER,
197 .reg = PATH1_VOL_CTL,
198 .mask = 0xff,
199 .shift = 0,
202 static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
204 static const u32 cx231xx_user_ctrls[] = {
205 V4L2_CID_USER_CLASS,
206 V4L2_CID_BRIGHTNESS,
207 V4L2_CID_CONTRAST,
208 V4L2_CID_SATURATION,
209 V4L2_CID_HUE,
210 V4L2_CID_AUDIO_VOLUME,
211 V4L2_CID_AUDIO_MUTE,
215 static const u32 *ctrl_classes[] = {
216 cx231xx_user_ctrls,
217 NULL
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";
247 switch (status) {
248 case -ENOENT:
249 errmsg = "unlinked synchronuously";
250 break;
251 case -ECONNRESET:
252 errmsg = "unlinked asynchronuously";
253 break;
254 case -ENOSR:
255 errmsg = "Buffer error (overrun)";
256 break;
257 case -EPIPE:
258 errmsg = "Stalled (device not responding)";
259 break;
260 case -EOVERFLOW:
261 errmsg = "Babble (bad cable?)";
262 break;
263 case -EPROTO:
264 errmsg = "Bit-stuff error (bad cable?)";
265 break;
266 case -EILSEQ:
267 errmsg = "CRC/Timeout (could be anything)";
268 break;
269 case -ETIME:
270 errmsg = "Device does not respond";
271 break;
273 if (packet < 0) {
274 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
275 } else {
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);
291 char *outp;
293 if (list_empty(&dma_q->active)) {
294 cx231xx_isocdbg("No active queue to serve\n");
295 dev->video_mode.isoc_ctl.buf = NULL;
296 *buf = NULL;
297 return;
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;
309 return;
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;
320 int i, rc = 1;
321 unsigned char *p_buffer;
322 u32 bytes_parsed = 0, buffer_size = 0;
323 u8 sav_eav = 0;
325 if (!dev)
326 return 0;
328 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
329 return 0;
331 if (urb->status < 0) {
332 print_err_status(dev, -1, urb->status);
333 if (urb->status == -ENOENT)
334 return 0;
337 buf = dev->video_mode.isoc_ctl.buf;
338 if (buf != NULL)
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;
344 if (status < 0) {
345 print_err_status(dev, i, status);
346 if (urb->iso_frame_desc[i].status != -EPROTO)
347 continue;
350 if (urb->iso_frame_desc[i].actual_length <= 0) {
351 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
352 continue;
354 if (urb->iso_frame_desc[i].actual_length >
355 dev->video_mode.max_pkt_size) {
356 cx231xx_isocdbg("packet bigger than packet size");
357 continue;
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;
363 bytes_parsed = 0;
365 if (dma_q->is_partial_line) {
366 /* Handle the case of a partial line */
367 sav_eav = dma_q->last_sav;
368 } else {
369 /* Check for a SAV/EAV overlapping
370 the buffer boundary */
371 sav_eav =
372 cx231xx_find_boundary_SAV_EAV(p_buffer,
373 dma_q->partial_buf,
374 &bytes_parsed);
377 sav_eav &= 0xF0;
378 /* Get the first line if we have some portion of an SAV/EAV from
379 the last buffer or a partial line */
380 if (sav_eav) {
381 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
382 sav_eav, /* SAV/EAV */
383 p_buffer + bytes_parsed, /* p_buffer */
384 buffer_size - bytes_parsed);/* buf size */
387 /* Now parse data that is completely in this buffer */
388 /* dma_q->is_partial_line = 0; */
390 while (bytes_parsed < buffer_size) {
391 u32 bytes_used = 0;
393 sav_eav = cx231xx_find_next_SAV_EAV(
394 p_buffer + bytes_parsed, /* p_buffer */
395 buffer_size - bytes_parsed, /* buf size */
396 &bytes_used);/* bytes used to get SAV/EAV */
398 bytes_parsed += bytes_used;
400 sav_eav &= 0xF0;
401 if (sav_eav && (bytes_parsed < buffer_size)) {
402 bytes_parsed += cx231xx_get_video_line(dev,
403 dma_q, sav_eav, /* SAV/EAV */
404 p_buffer + bytes_parsed,/* p_buffer */
405 buffer_size - bytes_parsed);/*buf size*/
409 /* Save the last four bytes of the buffer so we can check the
410 buffer boundary condition next time */
411 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
412 bytes_parsed = 0;
415 return rc;
418 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
419 u32 *p_bytes_used)
421 u32 bytes_used;
422 u8 boundary_bytes[8];
423 u8 sav_eav = 0;
425 *p_bytes_used = 0;
427 /* Create an array of the last 4 bytes of the last buffer and the first
428 4 bytes of the current buffer. */
430 memcpy(boundary_bytes, partial_buf, 4);
431 memcpy(boundary_bytes + 4, p_buffer, 4);
433 /* Check for the SAV/EAV in the boundary buffer */
434 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
435 &bytes_used);
437 if (sav_eav) {
438 /* found a boundary SAV/EAV. Updates the bytes used to reflect
439 only those used in the new buffer */
440 *p_bytes_used = bytes_used - 4;
443 return sav_eav;
446 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
448 u32 i;
449 u8 sav_eav = 0;
452 * Don't search if the buffer size is less than 4. It causes a page
453 * fault since buffer_size - 4 evaluates to a large number in that
454 * case.
456 if (buffer_size < 4) {
457 *p_bytes_used = buffer_size;
458 return 0;
461 for (i = 0; i < (buffer_size - 3); i++) {
463 if ((p_buffer[i] == 0xFF) &&
464 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
466 *p_bytes_used = i + 4;
467 sav_eav = p_buffer[i + 3];
468 return sav_eav;
472 *p_bytes_used = buffer_size;
473 return 0;
476 u32 cx231xx_get_video_line(struct cx231xx *dev,
477 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
478 u8 *p_buffer, u32 buffer_size)
480 u32 bytes_copied = 0;
481 int current_field = -1;
483 switch (sav_eav) {
484 case SAV_ACTIVE_VIDEO_FIELD1:
485 /* looking for skipped line which occurred in PAL 720x480 mode.
486 In this case, there will be no active data contained
487 between the SAV and EAV */
488 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
489 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
490 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
491 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
492 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
493 (p_buffer[3] == EAV_VBLANK_FIELD2)))
494 return bytes_copied;
495 current_field = 1;
496 break;
498 case SAV_ACTIVE_VIDEO_FIELD2:
499 /* looking for skipped line which occurred in PAL 720x480 mode.
500 In this case, there will be no active data contained between
501 the SAV and EAV */
502 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
503 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
504 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
505 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
506 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
507 (p_buffer[3] == EAV_VBLANK_FIELD2)))
508 return bytes_copied;
509 current_field = 2;
510 break;
513 dma_q->last_sav = sav_eav;
515 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
516 buffer_size, current_field);
518 return bytes_copied;
521 u32 cx231xx_copy_video_line(struct cx231xx *dev,
522 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
523 u32 length, int field_number)
525 u32 bytes_to_copy;
526 struct cx231xx_buffer *buf;
527 u32 _line_size = dev->width * 2;
529 if (dma_q->current_field != field_number)
530 cx231xx_reset_video_buffer(dev, dma_q);
532 /* get the buffer pointer */
533 buf = dev->video_mode.isoc_ctl.buf;
535 /* Remember the field number for next time */
536 dma_q->current_field = field_number;
538 bytes_to_copy = dma_q->bytes_left_in_line;
539 if (bytes_to_copy > length)
540 bytes_to_copy = length;
542 if (dma_q->lines_completed >= dma_q->lines_per_field) {
543 dma_q->bytes_left_in_line -= bytes_to_copy;
544 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
545 0 : 1;
546 return 0;
549 dma_q->is_partial_line = 1;
551 /* If we don't have a buffer, just return the number of bytes we would
552 have copied if we had a buffer. */
553 if (!buf) {
554 dma_q->bytes_left_in_line -= bytes_to_copy;
555 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
556 ? 0 : 1;
557 return bytes_to_copy;
560 /* copy the data to video buffer */
561 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
563 dma_q->pos += bytes_to_copy;
564 dma_q->bytes_left_in_line -= bytes_to_copy;
566 if (dma_q->bytes_left_in_line == 0) {
567 dma_q->bytes_left_in_line = _line_size;
568 dma_q->lines_completed++;
569 dma_q->is_partial_line = 0;
571 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
572 buffer_filled(dev, dma_q, buf);
574 dma_q->pos = 0;
575 buf = NULL;
576 dma_q->lines_completed = 0;
580 return bytes_to_copy;
583 void cx231xx_reset_video_buffer(struct cx231xx *dev,
584 struct cx231xx_dmaqueue *dma_q)
586 struct cx231xx_buffer *buf;
588 /* handle the switch from field 1 to field 2 */
589 if (dma_q->current_field == 1) {
590 if (dma_q->lines_completed >= dma_q->lines_per_field)
591 dma_q->field1_done = 1;
592 else
593 dma_q->field1_done = 0;
596 buf = dev->video_mode.isoc_ctl.buf;
598 if (buf == NULL) {
599 u8 *outp = NULL;
600 /* first try to get the buffer */
601 get_next_buf(dma_q, &buf);
603 if (buf)
604 outp = videobuf_to_vmalloc(&buf->vb);
606 dma_q->pos = 0;
607 dma_q->field1_done = 0;
608 dma_q->current_field = -1;
611 /* reset the counters */
612 dma_q->bytes_left_in_line = dev->width << 1;
613 dma_q->lines_completed = 0;
616 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
617 u8 *p_buffer, u32 bytes_to_copy)
619 u8 *p_out_buffer = NULL;
620 u32 current_line_bytes_copied = 0;
621 struct cx231xx_buffer *buf;
622 u32 _line_size = dev->width << 1;
623 void *startwrite;
624 int offset, lencopy;
626 buf = dev->video_mode.isoc_ctl.buf;
628 if (buf == NULL)
629 return -1;
631 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
633 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
635 /* Offset field 2 one line from the top of the buffer */
636 offset = (dma_q->current_field == 1) ? 0 : _line_size;
638 /* Offset for field 2 */
639 startwrite = p_out_buffer + offset;
641 /* lines already completed in the current field */
642 startwrite += (dma_q->lines_completed * _line_size * 2);
644 /* bytes already completed in the current line */
645 startwrite += current_line_bytes_copied;
647 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
648 bytes_to_copy : dma_q->bytes_left_in_line;
650 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
651 return 0;
653 /* The below copies the UYVY data straight into video buffer */
654 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
656 return 0;
659 void cx231xx_swab(u16 *from, u16 *to, u16 len)
661 u16 i;
663 if (len <= 0)
664 return;
666 for (i = 0; i < len / 2; i++)
667 to[i] = (from[i] << 8) | (from[i] >> 8);
670 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
672 u8 buffer_complete = 0;
674 /* Dual field stream */
675 buffer_complete = ((dma_q->current_field == 2) &&
676 (dma_q->lines_completed >= dma_q->lines_per_field) &&
677 dma_q->field1_done);
679 return buffer_complete;
682 /* ------------------------------------------------------------------
683 Videobuf operations
684 ------------------------------------------------------------------*/
686 static int
687 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
689 struct cx231xx_fh *fh = vq->priv_data;
690 struct cx231xx *dev = fh->dev;
691 struct v4l2_frequency f;
693 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
694 if (0 == *count)
695 *count = CX231XX_DEF_BUF;
697 if (*count < CX231XX_MIN_BUF)
698 *count = CX231XX_MIN_BUF;
700 /* Ask tuner to go to analog mode */
701 memset(&f, 0, sizeof(f));
702 f.frequency = dev->ctl_freq;
703 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
705 call_all(dev, tuner, s_frequency, &f);
707 return 0;
710 /* This is called *without* dev->slock held; please keep it that way */
711 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
713 struct cx231xx_fh *fh = vq->priv_data;
714 struct cx231xx *dev = fh->dev;
715 unsigned long flags = 0;
717 if (in_interrupt())
718 BUG();
720 /* We used to wait for the buffer to finish here, but this didn't work
721 because, as we were keeping the state as VIDEOBUF_QUEUED,
722 videobuf_queue_cancel marked it as finished for us.
723 (Also, it could wedge forever if the hardware was misconfigured.)
725 This should be safe; by the time we get here, the buffer isn't
726 queued anymore. If we ever start marking the buffers as
727 VIDEOBUF_ACTIVE, it won't be, though.
729 spin_lock_irqsave(&dev->video_mode.slock, flags);
730 if (dev->video_mode.isoc_ctl.buf == buf)
731 dev->video_mode.isoc_ctl.buf = NULL;
732 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
734 videobuf_vmalloc_free(&buf->vb);
735 buf->vb.state = VIDEOBUF_NEEDS_INIT;
738 static int
739 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
740 enum v4l2_field field)
742 struct cx231xx_fh *fh = vq->priv_data;
743 struct cx231xx_buffer *buf =
744 container_of(vb, struct cx231xx_buffer, vb);
745 struct cx231xx *dev = fh->dev;
746 int rc = 0, urb_init = 0;
748 /* The only currently supported format is 16 bits/pixel */
749 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
750 + 7) >> 3;
751 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
752 return -EINVAL;
754 buf->vb.width = dev->width;
755 buf->vb.height = dev->height;
756 buf->vb.field = field;
758 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
759 rc = videobuf_iolock(vq, &buf->vb, NULL);
760 if (rc < 0)
761 goto fail;
764 if (!dev->video_mode.isoc_ctl.num_bufs)
765 urb_init = 1;
767 if (urb_init) {
768 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
769 CX231XX_NUM_BUFS,
770 dev->video_mode.max_pkt_size,
771 cx231xx_isoc_copy);
772 if (rc < 0)
773 goto fail;
776 buf->vb.state = VIDEOBUF_PREPARED;
777 return 0;
779 fail:
780 free_buffer(vq, buf);
781 return rc;
784 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
786 struct cx231xx_buffer *buf =
787 container_of(vb, struct cx231xx_buffer, vb);
788 struct cx231xx_fh *fh = vq->priv_data;
789 struct cx231xx *dev = fh->dev;
790 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
792 buf->vb.state = VIDEOBUF_QUEUED;
793 list_add_tail(&buf->vb.queue, &vidq->active);
797 static void buffer_release(struct videobuf_queue *vq,
798 struct videobuf_buffer *vb)
800 struct cx231xx_buffer *buf =
801 container_of(vb, struct cx231xx_buffer, vb);
802 struct cx231xx_fh *fh = vq->priv_data;
803 struct cx231xx *dev = (struct cx231xx *)fh->dev;
805 cx231xx_isocdbg("cx231xx: called buffer_release\n");
807 free_buffer(vq, buf);
810 static struct videobuf_queue_ops cx231xx_video_qops = {
811 .buf_setup = buffer_setup,
812 .buf_prepare = buffer_prepare,
813 .buf_queue = buffer_queue,
814 .buf_release = buffer_release,
817 /********************* v4l2 interface **************************************/
819 void video_mux(struct cx231xx *dev, int index)
821 dev->video_input = index;
822 dev->ctl_ainput = INPUT(index)->amux;
824 cx231xx_set_video_input_mux(dev, index);
826 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
828 cx231xx_set_audio_input(dev, dev->ctl_ainput);
830 cx231xx_info("video_mux : %d\n", index);
832 /* do mode control overrides if required */
833 cx231xx_do_mode_ctrl_overrides(dev);
836 /* Usage lock check functions */
837 static int res_get(struct cx231xx_fh *fh)
839 struct cx231xx *dev = fh->dev;
840 int rc = 0;
842 /* This instance already has stream_on */
843 if (fh->stream_on)
844 return rc;
846 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
847 if (dev->stream_on)
848 return -EBUSY;
849 dev->stream_on = 1;
850 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
851 if (dev->vbi_stream_on)
852 return -EBUSY;
853 dev->vbi_stream_on = 1;
854 } else
855 return -EINVAL;
857 fh->stream_on = 1;
859 return rc;
862 static int res_check(struct cx231xx_fh *fh)
864 return fh->stream_on;
867 static void res_free(struct cx231xx_fh *fh)
869 struct cx231xx *dev = fh->dev;
871 fh->stream_on = 0;
873 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
874 dev->stream_on = 0;
875 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
876 dev->vbi_stream_on = 0;
879 static int check_dev(struct cx231xx *dev)
881 if (dev->state & DEV_DISCONNECTED) {
882 cx231xx_errdev("v4l2 ioctl: device not present\n");
883 return -ENODEV;
886 if (dev->state & DEV_MISCONFIGURED) {
887 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
888 "close and open it again\n");
889 return -EIO;
891 return 0;
894 static void get_scale(struct cx231xx *dev,
895 unsigned int width, unsigned int height,
896 unsigned int *hscale, unsigned int *vscale)
898 unsigned int maxw = norm_maxw(dev);
899 unsigned int maxh = norm_maxh(dev);
901 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
902 if (*hscale >= 0x4000)
903 *hscale = 0x3fff;
905 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
906 if (*vscale >= 0x4000)
907 *vscale = 0x3fff;
910 /* ------------------------------------------------------------------
911 IOCTL vidioc handling
912 ------------------------------------------------------------------*/
914 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
915 struct v4l2_format *f)
917 struct cx231xx_fh *fh = priv;
918 struct cx231xx *dev = fh->dev;
920 mutex_lock(&dev->lock);
922 f->fmt.pix.width = dev->width;
923 f->fmt.pix.height = dev->height;
924 f->fmt.pix.pixelformat = dev->format->fourcc;
925 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
926 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
927 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
929 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
931 mutex_unlock(&dev->lock);
933 return 0;
936 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
938 unsigned int i;
940 for (i = 0; i < ARRAY_SIZE(format); i++)
941 if (format[i].fourcc == fourcc)
942 return &format[i];
944 return NULL;
947 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
948 struct v4l2_format *f)
950 struct cx231xx_fh *fh = priv;
951 struct cx231xx *dev = fh->dev;
952 unsigned int width = f->fmt.pix.width;
953 unsigned int height = f->fmt.pix.height;
954 unsigned int maxw = norm_maxw(dev);
955 unsigned int maxh = norm_maxh(dev);
956 unsigned int hscale, vscale;
957 struct cx231xx_fmt *fmt;
959 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
960 if (!fmt) {
961 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
962 f->fmt.pix.pixelformat);
963 return -EINVAL;
966 /* width must even because of the YUYV format
967 height must be even because of interlacing */
968 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
970 get_scale(dev, width, height, &hscale, &vscale);
972 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
973 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
975 f->fmt.pix.width = width;
976 f->fmt.pix.height = height;
977 f->fmt.pix.pixelformat = fmt->fourcc;
978 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
979 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
980 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
981 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
983 return 0;
986 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
987 struct v4l2_format *f)
989 struct cx231xx_fh *fh = priv;
990 struct cx231xx *dev = fh->dev;
991 int rc;
992 struct cx231xx_fmt *fmt;
993 struct v4l2_mbus_framefmt mbus_fmt;
995 rc = check_dev(dev);
996 if (rc < 0)
997 return rc;
999 mutex_lock(&dev->lock);
1001 vidioc_try_fmt_vid_cap(file, priv, f);
1003 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1004 if (!fmt) {
1005 rc = -EINVAL;
1006 goto out;
1009 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1010 cx231xx_errdev("%s queue busy\n", __func__);
1011 rc = -EBUSY;
1012 goto out;
1015 if (dev->stream_on && !fh->stream_on) {
1016 cx231xx_errdev("%s device in use by another fh\n", __func__);
1017 rc = -EBUSY;
1018 goto out;
1021 /* set new image size */
1022 dev->width = f->fmt.pix.width;
1023 dev->height = f->fmt.pix.height;
1024 dev->format = fmt;
1025 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1027 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1028 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1029 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1031 /* Set the correct alternate setting for this resolution */
1032 cx231xx_resolution_set(dev);
1034 out:
1035 mutex_unlock(&dev->lock);
1036 return rc;
1039 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
1041 struct cx231xx_fh *fh = priv;
1042 struct cx231xx *dev = fh->dev;
1044 *id = dev->norm;
1045 return 0;
1048 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1050 struct cx231xx_fh *fh = priv;
1051 struct cx231xx *dev = fh->dev;
1052 struct v4l2_format f;
1053 int rc;
1055 rc = check_dev(dev);
1056 if (rc < 0)
1057 return rc;
1059 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1061 mutex_lock(&dev->lock);
1062 dev->norm = *norm;
1064 /* Adjusts width/height, if needed */
1065 f.fmt.pix.width = dev->width;
1066 f.fmt.pix.height = dev->height;
1067 vidioc_try_fmt_vid_cap(file, priv, &f);
1069 /* set new image size */
1070 dev->width = f.fmt.pix.width;
1071 dev->height = f.fmt.pix.height;
1072 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1074 call_all(dev, core, s_std, dev->norm);
1076 mutex_unlock(&dev->lock);
1078 cx231xx_resolution_set(dev);
1080 /* do mode control overrides */
1081 cx231xx_do_mode_ctrl_overrides(dev);
1083 return 0;
1086 static const char *iname[] = {
1087 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1088 [CX231XX_VMUX_SVIDEO] = "S-Video",
1089 [CX231XX_VMUX_TELEVISION] = "Television",
1090 [CX231XX_VMUX_CABLE] = "Cable TV",
1091 [CX231XX_VMUX_DVB] = "DVB",
1092 [CX231XX_VMUX_DEBUG] = "for debug only",
1095 static int vidioc_enum_input(struct file *file, void *priv,
1096 struct v4l2_input *i)
1098 struct cx231xx_fh *fh = priv;
1099 struct cx231xx *dev = fh->dev;
1100 unsigned int n;
1102 n = i->index;
1103 if (n >= MAX_CX231XX_INPUT)
1104 return -EINVAL;
1105 if (0 == INPUT(n)->type)
1106 return -EINVAL;
1108 i->index = n;
1109 i->type = V4L2_INPUT_TYPE_CAMERA;
1111 strcpy(i->name, iname[INPUT(n)->type]);
1113 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1114 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1115 i->type = V4L2_INPUT_TYPE_TUNER;
1117 i->std = dev->vdev->tvnorms;
1119 return 0;
1122 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1124 struct cx231xx_fh *fh = priv;
1125 struct cx231xx *dev = fh->dev;
1127 *i = dev->video_input;
1129 return 0;
1132 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1134 struct cx231xx_fh *fh = priv;
1135 struct cx231xx *dev = fh->dev;
1136 int rc;
1138 rc = check_dev(dev);
1139 if (rc < 0)
1140 return rc;
1142 if (i >= MAX_CX231XX_INPUT)
1143 return -EINVAL;
1144 if (0 == INPUT(i)->type)
1145 return -EINVAL;
1147 mutex_lock(&dev->lock);
1149 video_mux(dev, i);
1151 mutex_unlock(&dev->lock);
1152 return 0;
1155 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1157 struct cx231xx_fh *fh = priv;
1158 struct cx231xx *dev = fh->dev;
1160 switch (a->index) {
1161 case CX231XX_AMUX_VIDEO:
1162 strcpy(a->name, "Television");
1163 break;
1164 case CX231XX_AMUX_LINE_IN:
1165 strcpy(a->name, "Line In");
1166 break;
1167 default:
1168 return -EINVAL;
1171 a->index = dev->ctl_ainput;
1172 a->capability = V4L2_AUDCAP_STEREO;
1174 return 0;
1177 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1179 struct cx231xx_fh *fh = priv;
1180 struct cx231xx *dev = fh->dev;
1181 int status = 0;
1183 /* Doesn't allow manual routing */
1184 if (a->index != dev->ctl_ainput)
1185 return -EINVAL;
1187 dev->ctl_ainput = INPUT(a->index)->amux;
1188 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1190 return status;
1193 static int vidioc_queryctrl(struct file *file, void *priv,
1194 struct v4l2_queryctrl *qc)
1196 struct cx231xx_fh *fh = priv;
1197 struct cx231xx *dev = fh->dev;
1198 int id = qc->id;
1199 int i;
1200 int rc;
1202 rc = check_dev(dev);
1203 if (rc < 0)
1204 return rc;
1206 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1207 if (unlikely(qc->id == 0))
1208 return -EINVAL;
1210 memset(qc, 0, sizeof(*qc));
1212 qc->id = id;
1214 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1215 return -EINVAL;
1217 for (i = 0; i < CX231XX_CTLS; i++)
1218 if (cx231xx_ctls[i].v.id == qc->id)
1219 break;
1221 if (i == CX231XX_CTLS) {
1222 *qc = no_ctl;
1223 return 0;
1225 *qc = cx231xx_ctls[i].v;
1227 mutex_lock(&dev->lock);
1228 call_all(dev, core, queryctrl, qc);
1229 mutex_unlock(&dev->lock);
1231 if (qc->type)
1232 return 0;
1233 else
1234 return -EINVAL;
1237 static int vidioc_g_ctrl(struct file *file, void *priv,
1238 struct v4l2_control *ctrl)
1240 struct cx231xx_fh *fh = priv;
1241 struct cx231xx *dev = fh->dev;
1242 int rc;
1244 rc = check_dev(dev);
1245 if (rc < 0)
1246 return rc;
1248 mutex_lock(&dev->lock);
1249 call_all(dev, core, g_ctrl, ctrl);
1250 mutex_unlock(&dev->lock);
1251 return rc;
1254 static int vidioc_s_ctrl(struct file *file, void *priv,
1255 struct v4l2_control *ctrl)
1257 struct cx231xx_fh *fh = priv;
1258 struct cx231xx *dev = fh->dev;
1259 int rc;
1261 rc = check_dev(dev);
1262 if (rc < 0)
1263 return rc;
1265 mutex_lock(&dev->lock);
1266 call_all(dev, core, s_ctrl, ctrl);
1267 mutex_unlock(&dev->lock);
1268 return rc;
1271 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1273 struct cx231xx_fh *fh = priv;
1274 struct cx231xx *dev = fh->dev;
1275 int rc;
1277 rc = check_dev(dev);
1278 if (rc < 0)
1279 return rc;
1281 if (0 != t->index)
1282 return -EINVAL;
1284 strcpy(t->name, "Tuner");
1286 t->type = V4L2_TUNER_ANALOG_TV;
1287 t->capability = V4L2_TUNER_CAP_NORM;
1288 t->rangehigh = 0xffffffffUL;
1289 t->signal = 0xffff; /* LOCKED */
1291 return 0;
1294 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1296 struct cx231xx_fh *fh = priv;
1297 struct cx231xx *dev = fh->dev;
1298 int rc;
1300 rc = check_dev(dev);
1301 if (rc < 0)
1302 return rc;
1304 if (0 != t->index)
1305 return -EINVAL;
1306 return 0;
1309 static int vidioc_g_frequency(struct file *file, void *priv,
1310 struct v4l2_frequency *f)
1312 struct cx231xx_fh *fh = priv;
1313 struct cx231xx *dev = fh->dev;
1315 mutex_lock(&dev->lock);
1316 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1317 f->frequency = dev->ctl_freq;
1319 call_all(dev, tuner, g_frequency, f);
1321 mutex_unlock(&dev->lock);
1323 return 0;
1326 static int vidioc_s_frequency(struct file *file, void *priv,
1327 struct v4l2_frequency *f)
1329 struct cx231xx_fh *fh = priv;
1330 struct cx231xx *dev = fh->dev;
1331 int rc;
1333 rc = check_dev(dev);
1334 if (rc < 0)
1335 return rc;
1337 if (0 != f->tuner)
1338 return -EINVAL;
1340 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1341 return -EINVAL;
1342 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1343 return -EINVAL;
1345 /* set pre channel change settings in DIF first */
1346 rc = cx231xx_tuner_pre_channel_change(dev);
1348 mutex_lock(&dev->lock);
1350 dev->ctl_freq = f->frequency;
1352 if (dev->tuner_type == TUNER_XC5000) {
1353 if (dev->cx231xx_set_analog_freq != NULL)
1354 dev->cx231xx_set_analog_freq(dev, f->frequency);
1355 } else
1356 call_all(dev, tuner, s_frequency, f);
1358 mutex_unlock(&dev->lock);
1360 /* set post channel change settings in DIF first */
1361 rc = cx231xx_tuner_post_channel_change(dev);
1363 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1365 return rc;
1368 #ifdef CONFIG_VIDEO_ADV_DEBUG
1371 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1372 chip=<chip>[,min=<addr>,max=<addr>]
1373 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1374 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1375 chip=<chip>,reg=<addr>,val=<val>
1376 set the register [VIDIOC_DBG_S_REGISTER]
1378 if type == host, then <chip> is the hosts chip ID (default 0)
1379 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1380 if type == i2caddr, then <chip> is the 7-bit I2C address
1383 static int vidioc_g_register(struct file *file, void *priv,
1384 struct v4l2_dbg_register *reg)
1386 struct cx231xx_fh *fh = priv;
1387 struct cx231xx *dev = fh->dev;
1388 int ret = 0;
1389 u8 value[4] = { 0, 0, 0, 0 };
1390 u32 data = 0;
1392 switch (reg->match.type) {
1393 case V4L2_CHIP_MATCH_HOST:
1394 switch (reg->match.addr) {
1395 case 0: /* Cx231xx - internal registers */
1396 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1397 (u16)reg->reg, value, 4);
1398 reg->val = value[0] | value[1] << 8 |
1399 value[2] << 16 | value[3] << 24;
1400 break;
1401 case 1: /* AFE - read byte */
1402 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1403 (u16)reg->reg, 2, &data, 1);
1404 reg->val = le32_to_cpu(data & 0xff);
1405 break;
1406 case 14: /* AFE - read dword */
1407 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1408 (u16)reg->reg, 2, &data, 4);
1409 reg->val = le32_to_cpu(data);
1410 break;
1411 case 2: /* Video Block - read byte */
1412 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1413 (u16)reg->reg, 2, &data, 1);
1414 reg->val = le32_to_cpu(data & 0xff);
1415 break;
1416 case 24: /* Video Block - read dword */
1417 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1418 (u16)reg->reg, 2, &data, 4);
1419 reg->val = le32_to_cpu(data);
1420 break;
1421 case 3: /* I2S block - read byte */
1422 ret = cx231xx_read_i2c_data(dev,
1423 I2S_BLK_DEVICE_ADDRESS,
1424 (u16)reg->reg, 1,
1425 &data, 1);
1426 reg->val = le32_to_cpu(data & 0xff);
1427 break;
1428 case 34: /* I2S Block - read dword */
1429 ret =
1430 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1431 (u16)reg->reg, 1, &data, 4);
1432 reg->val = le32_to_cpu(data);
1433 break;
1435 return ret < 0 ? ret : 0;
1437 case V4L2_CHIP_MATCH_I2C_DRIVER:
1438 call_all(dev, core, g_register, reg);
1439 return 0;
1440 case V4L2_CHIP_MATCH_I2C_ADDR:
1441 /* Not supported yet */
1442 return -EINVAL;
1443 default:
1444 if (!v4l2_chip_match_host(&reg->match))
1445 return -EINVAL;
1448 mutex_lock(&dev->lock);
1449 call_all(dev, core, g_register, reg);
1450 mutex_unlock(&dev->lock);
1452 return ret;
1455 static int vidioc_s_register(struct file *file, void *priv,
1456 struct v4l2_dbg_register *reg)
1458 struct cx231xx_fh *fh = priv;
1459 struct cx231xx *dev = fh->dev;
1460 int ret = 0;
1461 __le64 buf;
1462 u32 value;
1463 u8 data[4] = { 0, 0, 0, 0 };
1465 buf = cpu_to_le64(reg->val);
1467 switch (reg->match.type) {
1468 case V4L2_CHIP_MATCH_HOST:
1470 value = (u32) buf & 0xffffffff;
1472 switch (reg->match.addr) {
1473 case 0: /* cx231xx internal registers */
1474 data[0] = (u8) value;
1475 data[1] = (u8) (value >> 8);
1476 data[2] = (u8) (value >> 16);
1477 data[3] = (u8) (value >> 24);
1478 ret = cx231xx_write_ctrl_reg(dev,
1479 VRT_SET_REGISTER,
1480 (u16)reg->reg, data,
1482 break;
1483 case 1: /* AFE - read byte */
1484 ret = cx231xx_write_i2c_data(dev,
1485 AFE_DEVICE_ADDRESS,
1486 (u16)reg->reg, 2,
1487 value, 1);
1488 break;
1489 case 14: /* AFE - read dword */
1490 ret = cx231xx_write_i2c_data(dev,
1491 AFE_DEVICE_ADDRESS,
1492 (u16)reg->reg, 2,
1493 value, 4);
1494 break;
1495 case 2: /* Video Block - read byte */
1496 ret =
1497 cx231xx_write_i2c_data(dev,
1498 VID_BLK_I2C_ADDRESS,
1499 (u16)reg->reg, 2,
1500 value, 1);
1501 break;
1502 case 24: /* Video Block - read dword */
1503 ret =
1504 cx231xx_write_i2c_data(dev,
1505 VID_BLK_I2C_ADDRESS,
1506 (u16)reg->reg, 2,
1507 value, 4);
1508 break;
1509 case 3: /* I2S block - read byte */
1510 ret =
1511 cx231xx_write_i2c_data(dev,
1512 I2S_BLK_DEVICE_ADDRESS,
1513 (u16)reg->reg, 1,
1514 value, 1);
1515 break;
1516 case 34: /* I2S block - read dword */
1517 ret =
1518 cx231xx_write_i2c_data(dev,
1519 I2S_BLK_DEVICE_ADDRESS,
1520 (u16)reg->reg, 1,
1521 value, 4);
1522 break;
1525 return ret < 0 ? ret : 0;
1527 default:
1528 break;
1531 mutex_lock(&dev->lock);
1532 call_all(dev, core, s_register, reg);
1533 mutex_unlock(&dev->lock);
1535 return ret;
1537 #endif
1539 static int vidioc_cropcap(struct file *file, void *priv,
1540 struct v4l2_cropcap *cc)
1542 struct cx231xx_fh *fh = priv;
1543 struct cx231xx *dev = fh->dev;
1545 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1546 return -EINVAL;
1548 cc->bounds.left = 0;
1549 cc->bounds.top = 0;
1550 cc->bounds.width = dev->width;
1551 cc->bounds.height = dev->height;
1552 cc->defrect = cc->bounds;
1553 cc->pixelaspect.numerator = 54;
1554 cc->pixelaspect.denominator = 59;
1556 return 0;
1559 static int vidioc_streamon(struct file *file, void *priv,
1560 enum v4l2_buf_type type)
1562 struct cx231xx_fh *fh = priv;
1563 struct cx231xx *dev = fh->dev;
1564 int rc;
1566 rc = check_dev(dev);
1567 if (rc < 0)
1568 return rc;
1570 mutex_lock(&dev->lock);
1571 rc = res_get(fh);
1573 if (likely(rc >= 0))
1574 rc = videobuf_streamon(&fh->vb_vidq);
1576 call_all(dev, video, s_stream, 1);
1578 mutex_unlock(&dev->lock);
1580 return rc;
1583 static int vidioc_streamoff(struct file *file, void *priv,
1584 enum v4l2_buf_type type)
1586 struct cx231xx_fh *fh = priv;
1587 struct cx231xx *dev = fh->dev;
1588 int rc;
1590 rc = check_dev(dev);
1591 if (rc < 0)
1592 return rc;
1594 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1595 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1596 return -EINVAL;
1597 if (type != fh->type)
1598 return -EINVAL;
1600 mutex_lock(&dev->lock);
1602 cx25840_call(dev, video, s_stream, 0);
1604 videobuf_streamoff(&fh->vb_vidq);
1605 res_free(fh);
1607 mutex_unlock(&dev->lock);
1609 return 0;
1612 static int vidioc_querycap(struct file *file, void *priv,
1613 struct v4l2_capability *cap)
1615 struct cx231xx_fh *fh = priv;
1616 struct cx231xx *dev = fh->dev;
1618 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1619 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1620 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1622 cap->version = CX231XX_VERSION_CODE;
1624 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1625 V4L2_CAP_VIDEO_CAPTURE |
1626 V4L2_CAP_AUDIO |
1627 V4L2_CAP_READWRITE |
1628 V4L2_CAP_STREAMING;
1630 if (dev->tuner_type != TUNER_ABSENT)
1631 cap->capabilities |= V4L2_CAP_TUNER;
1633 return 0;
1636 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1637 struct v4l2_fmtdesc *f)
1639 if (unlikely(f->index >= ARRAY_SIZE(format)))
1640 return -EINVAL;
1642 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1643 f->pixelformat = format[f->index].fourcc;
1645 return 0;
1648 /* Sliced VBI ioctls */
1649 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1650 struct v4l2_format *f)
1652 struct cx231xx_fh *fh = priv;
1653 struct cx231xx *dev = fh->dev;
1654 int rc;
1656 rc = check_dev(dev);
1657 if (rc < 0)
1658 return rc;
1660 mutex_lock(&dev->lock);
1662 f->fmt.sliced.service_set = 0;
1664 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1666 if (f->fmt.sliced.service_set == 0)
1667 rc = -EINVAL;
1669 mutex_unlock(&dev->lock);
1670 return rc;
1673 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1674 struct v4l2_format *f)
1676 struct cx231xx_fh *fh = priv;
1677 struct cx231xx *dev = fh->dev;
1678 int rc;
1680 rc = check_dev(dev);
1681 if (rc < 0)
1682 return rc;
1684 mutex_lock(&dev->lock);
1685 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1686 mutex_unlock(&dev->lock);
1688 if (f->fmt.sliced.service_set == 0)
1689 return -EINVAL;
1691 return 0;
1694 /* RAW VBI ioctls */
1696 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1697 struct v4l2_format *f)
1699 struct cx231xx_fh *fh = priv;
1700 struct cx231xx *dev = fh->dev;
1702 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1703 35468950 : 28636363;
1704 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1705 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1706 f->fmt.vbi.offset = 64 * 4;
1707 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1708 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1709 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1710 PAL_VBI_LINES : NTSC_VBI_LINES;
1711 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1712 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1713 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1715 return 0;
1719 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1720 struct v4l2_format *f)
1722 struct cx231xx_fh *fh = priv;
1723 struct cx231xx *dev = fh->dev;
1725 if (dev->vbi_stream_on && !fh->stream_on) {
1726 cx231xx_errdev("%s device in use by another fh\n", __func__);
1727 return -EBUSY;
1730 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1731 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1732 35468950 : 28636363;
1733 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1734 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1735 f->fmt.vbi.offset = 244;
1736 f->fmt.vbi.flags = 0;
1737 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1738 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1739 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1740 PAL_VBI_LINES : NTSC_VBI_LINES;
1741 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1742 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1743 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1745 return 0;
1749 static int vidioc_reqbufs(struct file *file, void *priv,
1750 struct v4l2_requestbuffers *rb)
1752 struct cx231xx_fh *fh = priv;
1753 struct cx231xx *dev = fh->dev;
1754 int rc;
1756 rc = check_dev(dev);
1757 if (rc < 0)
1758 return rc;
1760 return videobuf_reqbufs(&fh->vb_vidq, rb);
1763 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1765 struct cx231xx_fh *fh = priv;
1766 struct cx231xx *dev = fh->dev;
1767 int rc;
1769 rc = check_dev(dev);
1770 if (rc < 0)
1771 return rc;
1773 return videobuf_querybuf(&fh->vb_vidq, b);
1776 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1778 struct cx231xx_fh *fh = priv;
1779 struct cx231xx *dev = fh->dev;
1780 int rc;
1782 rc = check_dev(dev);
1783 if (rc < 0)
1784 return rc;
1786 return videobuf_qbuf(&fh->vb_vidq, b);
1789 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1791 struct cx231xx_fh *fh = priv;
1792 struct cx231xx *dev = fh->dev;
1793 int rc;
1795 rc = check_dev(dev);
1796 if (rc < 0)
1797 return rc;
1799 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1802 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1803 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1805 struct cx231xx_fh *fh = priv;
1807 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1809 #endif
1811 /* ----------------------------------------------------------- */
1812 /* RADIO ESPECIFIC IOCTLS */
1813 /* ----------------------------------------------------------- */
1815 static int radio_querycap(struct file *file, void *priv,
1816 struct v4l2_capability *cap)
1818 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1820 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1821 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1822 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1824 cap->version = CX231XX_VERSION_CODE;
1825 cap->capabilities = V4L2_CAP_TUNER;
1826 return 0;
1829 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1831 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1833 if (unlikely(t->index > 0))
1834 return -EINVAL;
1836 strcpy(t->name, "Radio");
1837 t->type = V4L2_TUNER_RADIO;
1839 mutex_lock(&dev->lock);
1840 call_all(dev, tuner, s_tuner, t);
1841 mutex_unlock(&dev->lock);
1843 return 0;
1846 static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1848 if (i->index != 0)
1849 return -EINVAL;
1850 strcpy(i->name, "Radio");
1851 i->type = V4L2_INPUT_TYPE_TUNER;
1853 return 0;
1856 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1858 if (unlikely(a->index))
1859 return -EINVAL;
1861 strcpy(a->name, "Radio");
1862 return 0;
1865 static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1867 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1869 if (0 != t->index)
1870 return -EINVAL;
1872 mutex_lock(&dev->lock);
1873 call_all(dev, tuner, s_tuner, t);
1874 mutex_unlock(&dev->lock);
1876 return 0;
1879 static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1881 return 0;
1884 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1886 return 0;
1889 static int radio_queryctrl(struct file *file, void *priv,
1890 struct v4l2_queryctrl *c)
1892 int i;
1894 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
1895 return -EINVAL;
1896 if (c->id == V4L2_CID_AUDIO_MUTE) {
1897 for (i = 0; i < CX231XX_CTLS; i++) {
1898 if (cx231xx_ctls[i].v.id == c->id)
1899 break;
1901 if (i == CX231XX_CTLS)
1902 return -EINVAL;
1903 *c = cx231xx_ctls[i].v;
1904 } else
1905 *c = no_ctl;
1906 return 0;
1910 * cx231xx_v4l2_open()
1911 * inits the device and starts isoc transfer
1913 static int cx231xx_v4l2_open(struct file *filp)
1915 int errCode = 0, radio = 0;
1916 struct video_device *vdev = video_devdata(filp);
1917 struct cx231xx *dev = video_drvdata(filp);
1918 struct cx231xx_fh *fh;
1919 enum v4l2_buf_type fh_type = 0;
1921 switch (vdev->vfl_type) {
1922 case VFL_TYPE_GRABBER:
1923 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1924 break;
1925 case VFL_TYPE_VBI:
1926 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1927 break;
1928 case VFL_TYPE_RADIO:
1929 radio = 1;
1930 break;
1933 mutex_lock(&dev->lock);
1935 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1936 video_device_node_name(vdev), v4l2_type_names[fh_type],
1937 dev->users);
1940 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1941 if (!fh) {
1942 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1943 mutex_unlock(&dev->lock);
1944 return -ENOMEM;
1946 fh->dev = dev;
1947 fh->radio = radio;
1948 fh->type = fh_type;
1949 filp->private_data = fh;
1951 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1952 dev->width = norm_maxw(dev);
1953 dev->height = norm_maxh(dev);
1954 dev->hscale = 0;
1955 dev->vscale = 0;
1957 /* Power up in Analog TV mode */
1958 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1960 cx231xx_resolution_set(dev);
1962 /* set video alternate setting */
1963 cx231xx_set_video_alternate(dev);
1965 /* Needed, since GPIO might have disabled power of
1966 some i2c device */
1967 cx231xx_config_i2c(dev);
1969 /* device needs to be initialized before isoc transfer */
1970 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1971 video_mux(dev, dev->video_input);
1974 if (fh->radio) {
1975 cx231xx_videodbg("video_open: setting radio device\n");
1977 /* cx231xx_start_radio(dev); */
1979 call_all(dev, tuner, s_radio);
1982 dev->users++;
1984 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1985 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1986 NULL, &dev->video_mode.slock,
1987 fh->type, V4L2_FIELD_INTERLACED,
1988 sizeof(struct cx231xx_buffer), fh);
1989 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1990 /* Set the required alternate setting VBI interface works in
1991 Bulk mode only */
1992 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1994 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1995 NULL, &dev->vbi_mode.slock,
1996 fh->type, V4L2_FIELD_SEQ_TB,
1997 sizeof(struct cx231xx_buffer), fh);
2000 mutex_unlock(&dev->lock);
2002 return errCode;
2006 * cx231xx_realease_resources()
2007 * unregisters the v4l2,i2c and usb devices
2008 * called when the device gets disconected or at module unload
2010 void cx231xx_release_analog_resources(struct cx231xx *dev)
2014 if (dev->radio_dev) {
2015 if (video_is_registered(dev->radio_dev))
2016 video_unregister_device(dev->radio_dev);
2017 else
2018 video_device_release(dev->radio_dev);
2019 dev->radio_dev = NULL;
2021 if (dev->vbi_dev) {
2022 cx231xx_info("V4L2 device %s deregistered\n",
2023 video_device_node_name(dev->vbi_dev));
2024 if (video_is_registered(dev->vbi_dev))
2025 video_unregister_device(dev->vbi_dev);
2026 else
2027 video_device_release(dev->vbi_dev);
2028 dev->vbi_dev = NULL;
2030 if (dev->vdev) {
2031 cx231xx_info("V4L2 device %s deregistered\n",
2032 video_device_node_name(dev->vdev));
2033 if (video_is_registered(dev->vdev))
2034 video_unregister_device(dev->vdev);
2035 else
2036 video_device_release(dev->vdev);
2037 dev->vdev = NULL;
2042 * cx231xx_v4l2_close()
2043 * stops streaming and deallocates all resources allocated by the v4l2
2044 * calls and ioctls
2046 static int cx231xx_v4l2_close(struct file *filp)
2048 struct cx231xx_fh *fh = filp->private_data;
2049 struct cx231xx *dev = fh->dev;
2051 cx231xx_videodbg("users=%d\n", dev->users);
2053 mutex_lock(&dev->lock);
2055 if (res_check(fh))
2056 res_free(fh);
2058 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2059 videobuf_stop(&fh->vb_vidq);
2060 videobuf_mmap_free(&fh->vb_vidq);
2062 /* the device is already disconnect,
2063 free the remaining resources */
2064 if (dev->state & DEV_DISCONNECTED) {
2065 cx231xx_release_resources(dev);
2066 mutex_unlock(&dev->lock);
2067 kfree(dev);
2068 return 0;
2071 /* do this before setting alternate! */
2072 cx231xx_uninit_vbi_isoc(dev);
2074 /* set alternate 0 */
2075 if (!dev->vbi_or_sliced_cc_mode)
2076 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2077 else
2078 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2080 kfree(fh);
2081 dev->users--;
2082 wake_up_interruptible_nr(&dev->open, 1);
2083 mutex_unlock(&dev->lock);
2084 return 0;
2087 if (dev->users == 1) {
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);
2096 kfree(dev);
2097 return 0;
2100 /* Save some power by putting tuner to sleep */
2101 call_all(dev, core, s_power, 0);
2103 /* do this before setting alternate! */
2104 cx231xx_uninit_isoc(dev);
2105 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2107 /* set alternate 0 */
2108 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2110 kfree(fh);
2111 dev->users--;
2112 wake_up_interruptible_nr(&dev->open, 1);
2113 mutex_unlock(&dev->lock);
2114 return 0;
2118 * cx231xx_v4l2_read()
2119 * will allocate buffers when called for the first time
2121 static ssize_t
2122 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2123 loff_t *pos)
2125 struct cx231xx_fh *fh = filp->private_data;
2126 struct cx231xx *dev = fh->dev;
2127 int rc;
2129 rc = check_dev(dev);
2130 if (rc < 0)
2131 return rc;
2133 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2134 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2135 mutex_lock(&dev->lock);
2136 rc = res_get(fh);
2137 mutex_unlock(&dev->lock);
2139 if (unlikely(rc < 0))
2140 return rc;
2142 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2143 filp->f_flags & O_NONBLOCK);
2145 return 0;
2149 * cx231xx_v4l2_poll()
2150 * will allocate buffers when called for the first time
2152 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2154 struct cx231xx_fh *fh = filp->private_data;
2155 struct cx231xx *dev = fh->dev;
2156 int rc;
2158 rc = check_dev(dev);
2159 if (rc < 0)
2160 return rc;
2162 mutex_lock(&dev->lock);
2163 rc = res_get(fh);
2164 mutex_unlock(&dev->lock);
2166 if (unlikely(rc < 0))
2167 return POLLERR;
2169 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2170 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2171 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2172 else
2173 return POLLERR;
2177 * cx231xx_v4l2_mmap()
2179 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2181 struct cx231xx_fh *fh = filp->private_data;
2182 struct cx231xx *dev = fh->dev;
2183 int rc;
2185 rc = check_dev(dev);
2186 if (rc < 0)
2187 return rc;
2189 mutex_lock(&dev->lock);
2190 rc = res_get(fh);
2191 mutex_unlock(&dev->lock);
2193 if (unlikely(rc < 0))
2194 return rc;
2196 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2198 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2199 (unsigned long)vma->vm_start,
2200 (unsigned long)vma->vm_end -
2201 (unsigned long)vma->vm_start, rc);
2203 return rc;
2206 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2207 .owner = THIS_MODULE,
2208 .open = cx231xx_v4l2_open,
2209 .release = cx231xx_v4l2_close,
2210 .read = cx231xx_v4l2_read,
2211 .poll = cx231xx_v4l2_poll,
2212 .mmap = cx231xx_v4l2_mmap,
2213 .ioctl = video_ioctl2,
2216 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2217 .vidioc_querycap = vidioc_querycap,
2218 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2219 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2220 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2221 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2222 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2223 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2224 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2225 .vidioc_g_audio = vidioc_g_audio,
2226 .vidioc_s_audio = vidioc_s_audio,
2227 .vidioc_cropcap = vidioc_cropcap,
2228 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2229 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2230 .vidioc_reqbufs = vidioc_reqbufs,
2231 .vidioc_querybuf = vidioc_querybuf,
2232 .vidioc_qbuf = vidioc_qbuf,
2233 .vidioc_dqbuf = vidioc_dqbuf,
2234 .vidioc_s_std = vidioc_s_std,
2235 .vidioc_g_std = vidioc_g_std,
2236 .vidioc_enum_input = vidioc_enum_input,
2237 .vidioc_g_input = vidioc_g_input,
2238 .vidioc_s_input = vidioc_s_input,
2239 .vidioc_queryctrl = vidioc_queryctrl,
2240 .vidioc_g_ctrl = vidioc_g_ctrl,
2241 .vidioc_s_ctrl = vidioc_s_ctrl,
2242 .vidioc_streamon = vidioc_streamon,
2243 .vidioc_streamoff = vidioc_streamoff,
2244 .vidioc_g_tuner = vidioc_g_tuner,
2245 .vidioc_s_tuner = vidioc_s_tuner,
2246 .vidioc_g_frequency = vidioc_g_frequency,
2247 .vidioc_s_frequency = vidioc_s_frequency,
2248 #ifdef CONFIG_VIDEO_ADV_DEBUG
2249 .vidioc_g_register = vidioc_g_register,
2250 .vidioc_s_register = vidioc_s_register,
2251 #endif
2252 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2253 .vidiocgmbuf = vidiocgmbuf,
2254 #endif
2257 static struct video_device cx231xx_vbi_template;
2259 static const struct video_device cx231xx_video_template = {
2260 .fops = &cx231xx_v4l_fops,
2261 .release = video_device_release,
2262 .ioctl_ops = &video_ioctl_ops,
2263 .tvnorms = V4L2_STD_ALL,
2264 .current_norm = V4L2_STD_PAL,
2267 static const struct v4l2_file_operations radio_fops = {
2268 .owner = THIS_MODULE,
2269 .open = cx231xx_v4l2_open,
2270 .release = cx231xx_v4l2_close,
2271 .ioctl = video_ioctl2,
2274 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2275 .vidioc_querycap = radio_querycap,
2276 .vidioc_g_tuner = radio_g_tuner,
2277 .vidioc_enum_input = radio_enum_input,
2278 .vidioc_g_audio = radio_g_audio,
2279 .vidioc_s_tuner = radio_s_tuner,
2280 .vidioc_s_audio = radio_s_audio,
2281 .vidioc_s_input = radio_s_input,
2282 .vidioc_queryctrl = radio_queryctrl,
2283 .vidioc_g_ctrl = vidioc_g_ctrl,
2284 .vidioc_s_ctrl = vidioc_s_ctrl,
2285 .vidioc_g_frequency = vidioc_g_frequency,
2286 .vidioc_s_frequency = vidioc_s_frequency,
2287 #ifdef CONFIG_VIDEO_ADV_DEBUG
2288 .vidioc_g_register = vidioc_g_register,
2289 .vidioc_s_register = vidioc_s_register,
2290 #endif
2293 static struct video_device cx231xx_radio_template = {
2294 .name = "cx231xx-radio",
2295 .fops = &radio_fops,
2296 .ioctl_ops = &radio_ioctl_ops,
2299 /******************************** usb interface ******************************/
2301 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2302 const struct video_device
2303 *template, const char *type_name)
2305 struct video_device *vfd;
2307 vfd = video_device_alloc();
2308 if (NULL == vfd)
2309 return NULL;
2311 *vfd = *template;
2312 vfd->v4l2_dev = &dev->v4l2_dev;
2313 vfd->release = video_device_release;
2314 vfd->debug = video_debug;
2316 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2318 video_set_drvdata(vfd, dev);
2319 return vfd;
2322 int cx231xx_register_analog_devices(struct cx231xx *dev)
2324 int ret;
2326 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2327 dev->name,
2328 (CX231XX_VERSION_CODE >> 16) & 0xff,
2329 (CX231XX_VERSION_CODE >> 8) & 0xff,
2330 CX231XX_VERSION_CODE & 0xff);
2332 /* set default norm */
2333 /*dev->norm = cx231xx_video_template.current_norm; */
2334 dev->width = norm_maxw(dev);
2335 dev->height = norm_maxh(dev);
2336 dev->interlaced = 0;
2337 dev->hscale = 0;
2338 dev->vscale = 0;
2340 /* Analog specific initialization */
2341 dev->format = &format[0];
2342 /* video_mux(dev, dev->video_input); */
2344 /* Audio defaults */
2345 dev->mute = 1;
2346 dev->volume = 0x1f;
2348 /* enable vbi capturing */
2349 /* write code here... */
2351 /* allocate and fill video video_device struct */
2352 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2353 if (!dev->vdev) {
2354 cx231xx_errdev("cannot allocate video_device.\n");
2355 return -ENODEV;
2358 /* register v4l2 video video_device */
2359 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2360 video_nr[dev->devno]);
2361 if (ret) {
2362 cx231xx_errdev("unable to register video device (error=%i).\n",
2363 ret);
2364 return ret;
2367 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2368 dev->name, video_device_node_name(dev->vdev));
2370 /* Initialize VBI template */
2371 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2372 sizeof(cx231xx_vbi_template));
2373 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2375 /* Allocate and fill vbi video_device struct */
2376 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2378 /* register v4l2 vbi video_device */
2379 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2380 vbi_nr[dev->devno]);
2381 if (ret < 0) {
2382 cx231xx_errdev("unable to register vbi device\n");
2383 return ret;
2386 cx231xx_info("%s/0: registered device %s\n",
2387 dev->name, video_device_node_name(dev->vbi_dev));
2389 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2390 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2391 "radio");
2392 if (!dev->radio_dev) {
2393 cx231xx_errdev("cannot allocate video_device.\n");
2394 return -ENODEV;
2396 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2397 radio_nr[dev->devno]);
2398 if (ret < 0) {
2399 cx231xx_errdev("can't register radio device\n");
2400 return ret;
2402 cx231xx_info("Registered radio device as %s\n",
2403 video_device_node_name(dev->radio_dev));
2406 cx231xx_info("V4L2 device registered as %s and %s\n",
2407 video_device_node_name(dev->vdev),
2408 video_device_node_name(dev->vbi_dev));
2410 return 0;