ASoC: tlv312aic23: unbreak resume
[zen-stable.git] / drivers / media / video / cx231xx / cx231xx-video.c
blob829a41b0c9ef5c77edc7095164debac2120f284c
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/mm.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/msp3400.h>
40 #include <media/tuner.h>
42 #include "dvb_frontend.h"
44 #include "cx231xx.h"
45 #include "cx231xx-vbi.h"
47 #define CX231XX_VERSION "0.0.2"
49 #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
50 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
52 #define cx231xx_videodbg(fmt, arg...) do {\
53 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0)
57 static unsigned int isoc_debug;
58 module_param(isoc_debug, int, 0644);
59 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61 #define cx231xx_isocdbg(fmt, arg...) \
62 do {\
63 if (isoc_debug) { \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); \
66 } \
67 } while (0)
69 MODULE_AUTHOR(DRIVER_AUTHOR);
70 MODULE_DESCRIPTION(DRIVER_DESC);
71 MODULE_LICENSE("GPL");
72 MODULE_VERSION(CX231XX_VERSION);
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 #if 0
212 V4L2_CID_AUDIO_BALANCE,
213 #endif
214 V4L2_CID_AUDIO_MUTE,
218 static const u32 *ctrl_classes[] = {
219 cx231xx_user_ctrls,
220 NULL
223 /* ------------------------------------------------------------------
224 Video buffer and parser functions
225 ------------------------------------------------------------------*/
228 * Announces that a buffer were filled and request the next
230 static inline void buffer_filled(struct cx231xx *dev,
231 struct cx231xx_dmaqueue *dma_q,
232 struct cx231xx_buffer *buf)
234 /* Advice that buffer was filled */
235 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
236 buf->vb.state = VIDEOBUF_DONE;
237 buf->vb.field_count++;
238 do_gettimeofday(&buf->vb.ts);
240 if (dev->USE_ISO)
241 dev->video_mode.isoc_ctl.buf = NULL;
242 else
243 dev->video_mode.bulk_ctl.buf = NULL;
245 list_del(&buf->vb.queue);
246 wake_up(&buf->vb.done);
249 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
251 char *errmsg = "Unknown";
253 switch (status) {
254 case -ENOENT:
255 errmsg = "unlinked synchronuously";
256 break;
257 case -ECONNRESET:
258 errmsg = "unlinked asynchronuously";
259 break;
260 case -ENOSR:
261 errmsg = "Buffer error (overrun)";
262 break;
263 case -EPIPE:
264 errmsg = "Stalled (device not responding)";
265 break;
266 case -EOVERFLOW:
267 errmsg = "Babble (bad cable?)";
268 break;
269 case -EPROTO:
270 errmsg = "Bit-stuff error (bad cable?)";
271 break;
272 case -EILSEQ:
273 errmsg = "CRC/Timeout (could be anything)";
274 break;
275 case -ETIME:
276 errmsg = "Device does not respond";
277 break;
279 if (packet < 0) {
280 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
281 } else {
282 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
283 packet, status, errmsg);
288 * video-buf generic routine to get the next available buffer
290 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
291 struct cx231xx_buffer **buf)
293 struct cx231xx_video_mode *vmode =
294 container_of(dma_q, struct cx231xx_video_mode, vidq);
295 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
297 char *outp;
299 if (list_empty(&dma_q->active)) {
300 cx231xx_isocdbg("No active queue to serve\n");
301 if (dev->USE_ISO)
302 dev->video_mode.isoc_ctl.buf = NULL;
303 else
304 dev->video_mode.bulk_ctl.buf = NULL;
305 *buf = NULL;
306 return;
309 /* Get the next buffer */
310 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
312 /* Cleans up buffer - Useful for testing for frame/URB loss */
313 outp = videobuf_to_vmalloc(&(*buf)->vb);
314 memset(outp, 0, (*buf)->vb.size);
316 if (dev->USE_ISO)
317 dev->video_mode.isoc_ctl.buf = *buf;
318 else
319 dev->video_mode.bulk_ctl.buf = *buf;
321 return;
325 * Controls the isoc copy of each urb packet
327 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
329 struct cx231xx_buffer *buf;
330 struct cx231xx_dmaqueue *dma_q = urb->context;
331 unsigned char *outp = NULL;
332 int i, rc = 1;
333 unsigned char *p_buffer;
334 u32 bytes_parsed = 0, buffer_size = 0;
335 u8 sav_eav = 0;
337 if (!dev)
338 return 0;
340 if (dev->state & DEV_DISCONNECTED)
341 return 0;
343 if (urb->status < 0) {
344 print_err_status(dev, -1, urb->status);
345 if (urb->status == -ENOENT)
346 return 0;
349 buf = dev->video_mode.isoc_ctl.buf;
350 if (buf != NULL)
351 outp = videobuf_to_vmalloc(&buf->vb);
353 for (i = 0; i < urb->number_of_packets; i++) {
354 int status = urb->iso_frame_desc[i].status;
356 if (status < 0) {
357 print_err_status(dev, i, status);
358 if (urb->iso_frame_desc[i].status != -EPROTO)
359 continue;
362 if (urb->iso_frame_desc[i].actual_length <= 0) {
363 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
364 continue;
366 if (urb->iso_frame_desc[i].actual_length >
367 dev->video_mode.max_pkt_size) {
368 cx231xx_isocdbg("packet bigger than packet size");
369 continue;
372 /* get buffer pointer and length */
373 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
374 buffer_size = urb->iso_frame_desc[i].actual_length;
375 bytes_parsed = 0;
377 if (dma_q->is_partial_line) {
378 /* Handle the case of a partial line */
379 sav_eav = dma_q->last_sav;
380 } else {
381 /* Check for a SAV/EAV overlapping
382 the buffer boundary */
383 sav_eav =
384 cx231xx_find_boundary_SAV_EAV(p_buffer,
385 dma_q->partial_buf,
386 &bytes_parsed);
389 sav_eav &= 0xF0;
390 /* Get the first line if we have some portion of an SAV/EAV from
391 the last buffer or a partial line */
392 if (sav_eav) {
393 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
394 sav_eav, /* SAV/EAV */
395 p_buffer + bytes_parsed, /* p_buffer */
396 buffer_size - bytes_parsed);/* buf size */
399 /* Now parse data that is completely in this buffer */
400 /* dma_q->is_partial_line = 0; */
402 while (bytes_parsed < buffer_size) {
403 u32 bytes_used = 0;
405 sav_eav = cx231xx_find_next_SAV_EAV(
406 p_buffer + bytes_parsed, /* p_buffer */
407 buffer_size - bytes_parsed, /* buf size */
408 &bytes_used);/* bytes used to get SAV/EAV */
410 bytes_parsed += bytes_used;
412 sav_eav &= 0xF0;
413 if (sav_eav && (bytes_parsed < buffer_size)) {
414 bytes_parsed += cx231xx_get_video_line(dev,
415 dma_q, sav_eav, /* SAV/EAV */
416 p_buffer + bytes_parsed,/* p_buffer */
417 buffer_size - bytes_parsed);/*buf size*/
421 /* Save the last four bytes of the buffer so we can check the
422 buffer boundary condition next time */
423 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
424 bytes_parsed = 0;
427 return rc;
430 static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
432 struct cx231xx_buffer *buf;
433 struct cx231xx_dmaqueue *dma_q = urb->context;
434 unsigned char *outp = NULL;
435 int rc = 1;
436 unsigned char *p_buffer;
437 u32 bytes_parsed = 0, buffer_size = 0;
438 u8 sav_eav = 0;
440 if (!dev)
441 return 0;
443 if (dev->state & DEV_DISCONNECTED)
444 return 0;
446 if (urb->status < 0) {
447 print_err_status(dev, -1, urb->status);
448 if (urb->status == -ENOENT)
449 return 0;
452 buf = dev->video_mode.bulk_ctl.buf;
453 if (buf != NULL)
454 outp = videobuf_to_vmalloc(&buf->vb);
456 if (1) {
458 /* get buffer pointer and length */
459 p_buffer = urb->transfer_buffer;
460 buffer_size = urb->actual_length;
461 bytes_parsed = 0;
463 if (dma_q->is_partial_line) {
464 /* Handle the case of a partial line */
465 sav_eav = dma_q->last_sav;
466 } else {
467 /* Check for a SAV/EAV overlapping
468 the buffer boundary */
469 sav_eav =
470 cx231xx_find_boundary_SAV_EAV(p_buffer,
471 dma_q->partial_buf,
472 &bytes_parsed);
475 sav_eav &= 0xF0;
476 /* Get the first line if we have some portion of an SAV/EAV from
477 the last buffer or a partial line */
478 if (sav_eav) {
479 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
480 sav_eav, /* SAV/EAV */
481 p_buffer + bytes_parsed, /* p_buffer */
482 buffer_size - bytes_parsed);/* buf size */
485 /* Now parse data that is completely in this buffer */
486 /* dma_q->is_partial_line = 0; */
488 while (bytes_parsed < buffer_size) {
489 u32 bytes_used = 0;
491 sav_eav = cx231xx_find_next_SAV_EAV(
492 p_buffer + bytes_parsed, /* p_buffer */
493 buffer_size - bytes_parsed, /* buf size */
494 &bytes_used);/* bytes used to get SAV/EAV */
496 bytes_parsed += bytes_used;
498 sav_eav &= 0xF0;
499 if (sav_eav && (bytes_parsed < buffer_size)) {
500 bytes_parsed += cx231xx_get_video_line(dev,
501 dma_q, sav_eav, /* SAV/EAV */
502 p_buffer + bytes_parsed,/* p_buffer */
503 buffer_size - bytes_parsed);/*buf size*/
507 /* Save the last four bytes of the buffer so we can check the
508 buffer boundary condition next time */
509 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
510 bytes_parsed = 0;
513 return rc;
517 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
518 u32 *p_bytes_used)
520 u32 bytes_used;
521 u8 boundary_bytes[8];
522 u8 sav_eav = 0;
524 *p_bytes_used = 0;
526 /* Create an array of the last 4 bytes of the last buffer and the first
527 4 bytes of the current buffer. */
529 memcpy(boundary_bytes, partial_buf, 4);
530 memcpy(boundary_bytes + 4, p_buffer, 4);
532 /* Check for the SAV/EAV in the boundary buffer */
533 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
534 &bytes_used);
536 if (sav_eav) {
537 /* found a boundary SAV/EAV. Updates the bytes used to reflect
538 only those used in the new buffer */
539 *p_bytes_used = bytes_used - 4;
542 return sav_eav;
545 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
547 u32 i;
548 u8 sav_eav = 0;
551 * Don't search if the buffer size is less than 4. It causes a page
552 * fault since buffer_size - 4 evaluates to a large number in that
553 * case.
555 if (buffer_size < 4) {
556 *p_bytes_used = buffer_size;
557 return 0;
560 for (i = 0; i < (buffer_size - 3); i++) {
562 if ((p_buffer[i] == 0xFF) &&
563 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
565 *p_bytes_used = i + 4;
566 sav_eav = p_buffer[i + 3];
567 return sav_eav;
571 *p_bytes_used = buffer_size;
572 return 0;
575 u32 cx231xx_get_video_line(struct cx231xx *dev,
576 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
577 u8 *p_buffer, u32 buffer_size)
579 u32 bytes_copied = 0;
580 int current_field = -1;
582 switch (sav_eav) {
583 case SAV_ACTIVE_VIDEO_FIELD1:
584 /* looking for skipped line which occurred in PAL 720x480 mode.
585 In this case, there will be no active data contained
586 between the SAV and EAV */
587 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
588 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
589 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
590 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
591 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
592 (p_buffer[3] == EAV_VBLANK_FIELD2)))
593 return bytes_copied;
594 current_field = 1;
595 break;
597 case SAV_ACTIVE_VIDEO_FIELD2:
598 /* looking for skipped line which occurred in PAL 720x480 mode.
599 In this case, there will be no active data contained between
600 the SAV and EAV */
601 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
602 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
603 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
604 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
605 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
606 (p_buffer[3] == EAV_VBLANK_FIELD2)))
607 return bytes_copied;
608 current_field = 2;
609 break;
612 dma_q->last_sav = sav_eav;
614 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
615 buffer_size, current_field);
617 return bytes_copied;
620 u32 cx231xx_copy_video_line(struct cx231xx *dev,
621 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
622 u32 length, int field_number)
624 u32 bytes_to_copy;
625 struct cx231xx_buffer *buf;
626 u32 _line_size = dev->width * 2;
628 if (dma_q->current_field != field_number)
629 cx231xx_reset_video_buffer(dev, dma_q);
631 /* get the buffer pointer */
632 if (dev->USE_ISO)
633 buf = dev->video_mode.isoc_ctl.buf;
634 else
635 buf = dev->video_mode.bulk_ctl.buf;
637 /* Remember the field number for next time */
638 dma_q->current_field = field_number;
640 bytes_to_copy = dma_q->bytes_left_in_line;
641 if (bytes_to_copy > length)
642 bytes_to_copy = length;
644 if (dma_q->lines_completed >= dma_q->lines_per_field) {
645 dma_q->bytes_left_in_line -= bytes_to_copy;
646 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
647 0 : 1;
648 return 0;
651 dma_q->is_partial_line = 1;
653 /* If we don't have a buffer, just return the number of bytes we would
654 have copied if we had a buffer. */
655 if (!buf) {
656 dma_q->bytes_left_in_line -= bytes_to_copy;
657 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
658 ? 0 : 1;
659 return bytes_to_copy;
662 /* copy the data to video buffer */
663 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
665 dma_q->pos += bytes_to_copy;
666 dma_q->bytes_left_in_line -= bytes_to_copy;
668 if (dma_q->bytes_left_in_line == 0) {
669 dma_q->bytes_left_in_line = _line_size;
670 dma_q->lines_completed++;
671 dma_q->is_partial_line = 0;
673 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
674 buffer_filled(dev, dma_q, buf);
676 dma_q->pos = 0;
677 buf = NULL;
678 dma_q->lines_completed = 0;
682 return bytes_to_copy;
685 void cx231xx_reset_video_buffer(struct cx231xx *dev,
686 struct cx231xx_dmaqueue *dma_q)
688 struct cx231xx_buffer *buf;
690 /* handle the switch from field 1 to field 2 */
691 if (dma_q->current_field == 1) {
692 if (dma_q->lines_completed >= dma_q->lines_per_field)
693 dma_q->field1_done = 1;
694 else
695 dma_q->field1_done = 0;
698 if (dev->USE_ISO)
699 buf = dev->video_mode.isoc_ctl.buf;
700 else
701 buf = dev->video_mode.bulk_ctl.buf;
703 if (buf == NULL) {
704 u8 *outp = NULL;
705 /* first try to get the buffer */
706 get_next_buf(dma_q, &buf);
708 if (buf)
709 outp = videobuf_to_vmalloc(&buf->vb);
711 dma_q->pos = 0;
712 dma_q->field1_done = 0;
713 dma_q->current_field = -1;
716 /* reset the counters */
717 dma_q->bytes_left_in_line = dev->width << 1;
718 dma_q->lines_completed = 0;
721 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
722 u8 *p_buffer, u32 bytes_to_copy)
724 u8 *p_out_buffer = NULL;
725 u32 current_line_bytes_copied = 0;
726 struct cx231xx_buffer *buf;
727 u32 _line_size = dev->width << 1;
728 void *startwrite;
729 int offset, lencopy;
731 if (dev->USE_ISO)
732 buf = dev->video_mode.isoc_ctl.buf;
733 else
734 buf = dev->video_mode.bulk_ctl.buf;
736 if (buf == NULL)
737 return -1;
739 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
741 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
743 /* Offset field 2 one line from the top of the buffer */
744 offset = (dma_q->current_field == 1) ? 0 : _line_size;
746 /* Offset for field 2 */
747 startwrite = p_out_buffer + offset;
749 /* lines already completed in the current field */
750 startwrite += (dma_q->lines_completed * _line_size * 2);
752 /* bytes already completed in the current line */
753 startwrite += current_line_bytes_copied;
755 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
756 bytes_to_copy : dma_q->bytes_left_in_line;
758 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
759 return 0;
761 /* The below copies the UYVY data straight into video buffer */
762 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
764 return 0;
767 void cx231xx_swab(u16 *from, u16 *to, u16 len)
769 u16 i;
771 if (len <= 0)
772 return;
774 for (i = 0; i < len / 2; i++)
775 to[i] = (from[i] << 8) | (from[i] >> 8);
778 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
780 u8 buffer_complete = 0;
782 /* Dual field stream */
783 buffer_complete = ((dma_q->current_field == 2) &&
784 (dma_q->lines_completed >= dma_q->lines_per_field) &&
785 dma_q->field1_done);
787 return buffer_complete;
790 /* ------------------------------------------------------------------
791 Videobuf operations
792 ------------------------------------------------------------------*/
794 static int
795 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
797 struct cx231xx_fh *fh = vq->priv_data;
798 struct cx231xx *dev = fh->dev;
800 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
801 if (0 == *count)
802 *count = CX231XX_DEF_BUF;
804 if (*count < CX231XX_MIN_BUF)
805 *count = CX231XX_MIN_BUF;
807 return 0;
810 /* This is called *without* dev->slock held; please keep it that way */
811 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
813 struct cx231xx_fh *fh = vq->priv_data;
814 struct cx231xx *dev = fh->dev;
815 unsigned long flags = 0;
817 if (in_interrupt())
818 BUG();
820 /* We used to wait for the buffer to finish here, but this didn't work
821 because, as we were keeping the state as VIDEOBUF_QUEUED,
822 videobuf_queue_cancel marked it as finished for us.
823 (Also, it could wedge forever if the hardware was misconfigured.)
825 This should be safe; by the time we get here, the buffer isn't
826 queued anymore. If we ever start marking the buffers as
827 VIDEOBUF_ACTIVE, it won't be, though.
829 spin_lock_irqsave(&dev->video_mode.slock, flags);
830 if (dev->USE_ISO) {
831 if (dev->video_mode.isoc_ctl.buf == buf)
832 dev->video_mode.isoc_ctl.buf = NULL;
833 } else {
834 if (dev->video_mode.bulk_ctl.buf == buf)
835 dev->video_mode.bulk_ctl.buf = NULL;
837 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
839 videobuf_vmalloc_free(&buf->vb);
840 buf->vb.state = VIDEOBUF_NEEDS_INIT;
843 static int
844 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
845 enum v4l2_field field)
847 struct cx231xx_fh *fh = vq->priv_data;
848 struct cx231xx_buffer *buf =
849 container_of(vb, struct cx231xx_buffer, vb);
850 struct cx231xx *dev = fh->dev;
851 int rc = 0, urb_init = 0;
853 /* The only currently supported format is 16 bits/pixel */
854 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
855 + 7) >> 3;
856 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
857 return -EINVAL;
859 buf->vb.width = dev->width;
860 buf->vb.height = dev->height;
861 buf->vb.field = field;
863 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
864 rc = videobuf_iolock(vq, &buf->vb, NULL);
865 if (rc < 0)
866 goto fail;
869 if (dev->USE_ISO) {
870 if (!dev->video_mode.isoc_ctl.num_bufs)
871 urb_init = 1;
872 } else {
873 if (!dev->video_mode.bulk_ctl.num_bufs)
874 urb_init = 1;
876 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
877 urb_init, dev->video_mode.max_pkt_size);*/
878 if (urb_init) {
879 dev->mode_tv = 0;
880 if (dev->USE_ISO)
881 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
882 CX231XX_NUM_BUFS,
883 dev->video_mode.max_pkt_size,
884 cx231xx_isoc_copy);
885 else
886 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
887 CX231XX_NUM_BUFS,
888 dev->video_mode.max_pkt_size,
889 cx231xx_bulk_copy);
890 if (rc < 0)
891 goto fail;
894 buf->vb.state = VIDEOBUF_PREPARED;
895 return 0;
897 fail:
898 free_buffer(vq, buf);
899 return rc;
902 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
904 struct cx231xx_buffer *buf =
905 container_of(vb, struct cx231xx_buffer, vb);
906 struct cx231xx_fh *fh = vq->priv_data;
907 struct cx231xx *dev = fh->dev;
908 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
910 buf->vb.state = VIDEOBUF_QUEUED;
911 list_add_tail(&buf->vb.queue, &vidq->active);
915 static void buffer_release(struct videobuf_queue *vq,
916 struct videobuf_buffer *vb)
918 struct cx231xx_buffer *buf =
919 container_of(vb, struct cx231xx_buffer, vb);
920 struct cx231xx_fh *fh = vq->priv_data;
921 struct cx231xx *dev = (struct cx231xx *)fh->dev;
923 cx231xx_isocdbg("cx231xx: called buffer_release\n");
925 free_buffer(vq, buf);
928 static struct videobuf_queue_ops cx231xx_video_qops = {
929 .buf_setup = buffer_setup,
930 .buf_prepare = buffer_prepare,
931 .buf_queue = buffer_queue,
932 .buf_release = buffer_release,
935 /********************* v4l2 interface **************************************/
937 void video_mux(struct cx231xx *dev, int index)
939 dev->video_input = index;
940 dev->ctl_ainput = INPUT(index)->amux;
942 cx231xx_set_video_input_mux(dev, index);
944 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
946 cx231xx_set_audio_input(dev, dev->ctl_ainput);
948 cx231xx_info("video_mux : %d\n", index);
950 /* do mode control overrides if required */
951 cx231xx_do_mode_ctrl_overrides(dev);
954 /* Usage lock check functions */
955 static int res_get(struct cx231xx_fh *fh)
957 struct cx231xx *dev = fh->dev;
958 int rc = 0;
960 /* This instance already has stream_on */
961 if (fh->stream_on)
962 return rc;
964 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
965 if (dev->stream_on)
966 return -EBUSY;
967 dev->stream_on = 1;
968 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
969 if (dev->vbi_stream_on)
970 return -EBUSY;
971 dev->vbi_stream_on = 1;
972 } else
973 return -EINVAL;
975 fh->stream_on = 1;
977 return rc;
980 static int res_check(struct cx231xx_fh *fh)
982 return fh->stream_on;
985 static void res_free(struct cx231xx_fh *fh)
987 struct cx231xx *dev = fh->dev;
989 fh->stream_on = 0;
991 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
992 dev->stream_on = 0;
993 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
994 dev->vbi_stream_on = 0;
997 static int check_dev(struct cx231xx *dev)
999 if (dev->state & DEV_DISCONNECTED) {
1000 cx231xx_errdev("v4l2 ioctl: device not present\n");
1001 return -ENODEV;
1003 return 0;
1006 /* ------------------------------------------------------------------
1007 IOCTL vidioc handling
1008 ------------------------------------------------------------------*/
1010 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1011 struct v4l2_format *f)
1013 struct cx231xx_fh *fh = priv;
1014 struct cx231xx *dev = fh->dev;
1016 f->fmt.pix.width = dev->width;
1017 f->fmt.pix.height = dev->height;
1018 f->fmt.pix.pixelformat = dev->format->fourcc;
1019 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1020 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
1021 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1023 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1025 return 0;
1028 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
1030 unsigned int i;
1032 for (i = 0; i < ARRAY_SIZE(format); i++)
1033 if (format[i].fourcc == fourcc)
1034 return &format[i];
1036 return NULL;
1039 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1040 struct v4l2_format *f)
1042 struct cx231xx_fh *fh = priv;
1043 struct cx231xx *dev = fh->dev;
1044 unsigned int width = f->fmt.pix.width;
1045 unsigned int height = f->fmt.pix.height;
1046 unsigned int maxw = norm_maxw(dev);
1047 unsigned int maxh = norm_maxh(dev);
1048 struct cx231xx_fmt *fmt;
1050 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1051 if (!fmt) {
1052 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
1053 f->fmt.pix.pixelformat);
1054 return -EINVAL;
1057 /* width must even because of the YUYV format
1058 height must be even because of interlacing */
1059 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
1061 f->fmt.pix.width = width;
1062 f->fmt.pix.height = height;
1063 f->fmt.pix.pixelformat = fmt->fourcc;
1064 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1065 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1066 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1067 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1069 return 0;
1072 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1073 struct v4l2_format *f)
1075 struct cx231xx_fh *fh = priv;
1076 struct cx231xx *dev = fh->dev;
1077 int rc;
1078 struct cx231xx_fmt *fmt;
1079 struct v4l2_mbus_framefmt mbus_fmt;
1081 rc = check_dev(dev);
1082 if (rc < 0)
1083 return rc;
1085 vidioc_try_fmt_vid_cap(file, priv, f);
1087 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1088 if (!fmt)
1089 return -EINVAL;
1091 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1092 cx231xx_errdev("%s queue busy\n", __func__);
1093 return -EBUSY;
1096 if (dev->stream_on && !fh->stream_on) {
1097 cx231xx_errdev("%s device in use by another fh\n", __func__);
1098 return -EBUSY;
1101 /* set new image size */
1102 dev->width = f->fmt.pix.width;
1103 dev->height = f->fmt.pix.height;
1104 dev->format = fmt;
1106 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1107 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1108 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1110 return rc;
1113 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1115 struct cx231xx_fh *fh = priv;
1116 struct cx231xx *dev = fh->dev;
1118 *id = dev->norm;
1119 return 0;
1122 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1124 struct cx231xx_fh *fh = priv;
1125 struct cx231xx *dev = fh->dev;
1126 struct v4l2_mbus_framefmt mbus_fmt;
1127 struct v4l2_format f;
1128 int rc;
1130 rc = check_dev(dev);
1131 if (rc < 0)
1132 return rc;
1134 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1136 dev->norm = *norm;
1138 /* Adjusts width/height, if needed */
1139 f.fmt.pix.width = dev->width;
1140 f.fmt.pix.height = dev->height;
1141 vidioc_try_fmt_vid_cap(file, priv, &f);
1143 call_all(dev, core, s_std, dev->norm);
1145 /* We need to reset basic properties in the decoder related to
1146 resolution (since a standard change effects things like the number
1147 of lines in VACT, etc) */
1148 v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
1149 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1150 v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
1152 /* set new image size */
1153 dev->width = f.fmt.pix.width;
1154 dev->height = f.fmt.pix.height;
1156 /* do mode control overrides */
1157 cx231xx_do_mode_ctrl_overrides(dev);
1159 return 0;
1162 static const char *iname[] = {
1163 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1164 [CX231XX_VMUX_SVIDEO] = "S-Video",
1165 [CX231XX_VMUX_TELEVISION] = "Television",
1166 [CX231XX_VMUX_CABLE] = "Cable TV",
1167 [CX231XX_VMUX_DVB] = "DVB",
1168 [CX231XX_VMUX_DEBUG] = "for debug only",
1171 static int vidioc_enum_input(struct file *file, void *priv,
1172 struct v4l2_input *i)
1174 struct cx231xx_fh *fh = priv;
1175 struct cx231xx *dev = fh->dev;
1176 u32 gen_stat;
1177 unsigned int ret, n;
1179 n = i->index;
1180 if (n >= MAX_CX231XX_INPUT)
1181 return -EINVAL;
1182 if (0 == INPUT(n)->type)
1183 return -EINVAL;
1185 i->index = n;
1186 i->type = V4L2_INPUT_TYPE_CAMERA;
1188 strcpy(i->name, iname[INPUT(n)->type]);
1190 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1191 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1192 i->type = V4L2_INPUT_TYPE_TUNER;
1194 i->std = dev->vdev->tvnorms;
1196 /* If they are asking about the active input, read signal status */
1197 if (n == dev->video_input) {
1198 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1199 GEN_STAT, 2, &gen_stat, 4);
1200 if (ret > 0) {
1201 if ((gen_stat & FLD_VPRES) == 0x00)
1202 i->status |= V4L2_IN_ST_NO_SIGNAL;
1203 if ((gen_stat & FLD_HLOCK) == 0x00)
1204 i->status |= V4L2_IN_ST_NO_H_LOCK;
1208 return 0;
1211 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1213 struct cx231xx_fh *fh = priv;
1214 struct cx231xx *dev = fh->dev;
1216 *i = dev->video_input;
1218 return 0;
1221 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1223 struct cx231xx_fh *fh = priv;
1224 struct cx231xx *dev = fh->dev;
1225 int rc;
1227 dev->mode_tv = 0;
1228 rc = check_dev(dev);
1229 if (rc < 0)
1230 return rc;
1232 if (i >= MAX_CX231XX_INPUT)
1233 return -EINVAL;
1234 if (0 == INPUT(i)->type)
1235 return -EINVAL;
1237 video_mux(dev, i);
1239 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1240 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1241 /* There's a tuner, so reset the standard and put it on the
1242 last known frequency (since it was probably powered down
1243 until now */
1244 call_all(dev, core, s_std, dev->norm);
1247 return 0;
1250 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1252 struct cx231xx_fh *fh = priv;
1253 struct cx231xx *dev = fh->dev;
1255 switch (a->index) {
1256 case CX231XX_AMUX_VIDEO:
1257 strcpy(a->name, "Television");
1258 break;
1259 case CX231XX_AMUX_LINE_IN:
1260 strcpy(a->name, "Line In");
1261 break;
1262 default:
1263 return -EINVAL;
1266 a->index = dev->ctl_ainput;
1267 a->capability = V4L2_AUDCAP_STEREO;
1269 return 0;
1272 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1274 struct cx231xx_fh *fh = priv;
1275 struct cx231xx *dev = fh->dev;
1276 int status = 0;
1278 /* Doesn't allow manual routing */
1279 if (a->index != dev->ctl_ainput)
1280 return -EINVAL;
1282 dev->ctl_ainput = INPUT(a->index)->amux;
1283 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1285 return status;
1288 static int vidioc_queryctrl(struct file *file, void *priv,
1289 struct v4l2_queryctrl *qc)
1291 struct cx231xx_fh *fh = priv;
1292 struct cx231xx *dev = fh->dev;
1293 int id = qc->id;
1294 int i;
1295 int rc;
1297 rc = check_dev(dev);
1298 if (rc < 0)
1299 return rc;
1301 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1302 if (unlikely(qc->id == 0))
1303 return -EINVAL;
1305 memset(qc, 0, sizeof(*qc));
1307 qc->id = id;
1309 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1310 return -EINVAL;
1312 for (i = 0; i < CX231XX_CTLS; i++)
1313 if (cx231xx_ctls[i].v.id == qc->id)
1314 break;
1316 if (i == CX231XX_CTLS) {
1317 *qc = no_ctl;
1318 return 0;
1320 *qc = cx231xx_ctls[i].v;
1322 call_all(dev, core, queryctrl, qc);
1324 if (qc->type)
1325 return 0;
1326 else
1327 return -EINVAL;
1330 static int vidioc_g_ctrl(struct file *file, void *priv,
1331 struct v4l2_control *ctrl)
1333 struct cx231xx_fh *fh = priv;
1334 struct cx231xx *dev = fh->dev;
1335 int rc;
1337 rc = check_dev(dev);
1338 if (rc < 0)
1339 return rc;
1341 call_all(dev, core, g_ctrl, ctrl);
1342 return rc;
1345 static int vidioc_s_ctrl(struct file *file, void *priv,
1346 struct v4l2_control *ctrl)
1348 struct cx231xx_fh *fh = priv;
1349 struct cx231xx *dev = fh->dev;
1350 int rc;
1352 rc = check_dev(dev);
1353 if (rc < 0)
1354 return rc;
1356 call_all(dev, core, s_ctrl, ctrl);
1357 return rc;
1360 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1362 struct cx231xx_fh *fh = priv;
1363 struct cx231xx *dev = fh->dev;
1364 int rc;
1366 rc = check_dev(dev);
1367 if (rc < 0)
1368 return rc;
1370 if (0 != t->index)
1371 return -EINVAL;
1373 strcpy(t->name, "Tuner");
1375 t->type = V4L2_TUNER_ANALOG_TV;
1376 t->capability = V4L2_TUNER_CAP_NORM;
1377 t->rangehigh = 0xffffffffUL;
1378 t->signal = 0xffff; /* LOCKED */
1380 return 0;
1383 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1385 struct cx231xx_fh *fh = priv;
1386 struct cx231xx *dev = fh->dev;
1387 int rc;
1389 rc = check_dev(dev);
1390 if (rc < 0)
1391 return rc;
1393 if (0 != t->index)
1394 return -EINVAL;
1395 #if 0
1396 call_all(dev, tuner, s_tuner, t);
1397 #endif
1398 return 0;
1401 static int vidioc_g_frequency(struct file *file, void *priv,
1402 struct v4l2_frequency *f)
1404 struct cx231xx_fh *fh = priv;
1405 struct cx231xx *dev = fh->dev;
1407 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1408 f->frequency = dev->ctl_freq;
1410 call_all(dev, tuner, g_frequency, f);
1412 return 0;
1415 static int vidioc_s_frequency(struct file *file, void *priv,
1416 struct v4l2_frequency *f)
1418 struct cx231xx_fh *fh = priv;
1419 struct cx231xx *dev = fh->dev;
1420 int rc;
1421 u32 if_frequency = 5400000;
1423 cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1424 f->frequency, f->type);
1425 /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
1427 rc = check_dev(dev);
1428 if (rc < 0)
1429 return rc;
1431 if (0 != f->tuner)
1432 return -EINVAL;
1434 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1435 return -EINVAL;
1436 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1437 return -EINVAL;
1439 /* set pre channel change settings in DIF first */
1440 rc = cx231xx_tuner_pre_channel_change(dev);
1442 dev->ctl_freq = f->frequency;
1443 call_all(dev, tuner, s_frequency, f);
1445 /* set post channel change settings in DIF first */
1446 rc = cx231xx_tuner_post_channel_change(dev);
1448 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1449 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1450 if_frequency = 5400000; /*5.4MHz */
1451 else if (dev->norm & V4L2_STD_B)
1452 if_frequency = 6000000; /*6.0MHz */
1453 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1454 if_frequency = 6900000; /*6.9MHz */
1455 else if (dev->norm & V4L2_STD_GH)
1456 if_frequency = 7100000; /*7.1MHz */
1457 else if (dev->norm & V4L2_STD_PAL_I)
1458 if_frequency = 7250000; /*7.25MHz */
1459 else if (dev->norm & V4L2_STD_SECAM_L)
1460 if_frequency = 6900000; /*6.9MHz */
1461 else if (dev->norm & V4L2_STD_SECAM_LC)
1462 if_frequency = 1250000; /*1.25MHz */
1464 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1465 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1467 update_HH_register_after_set_DIF(dev);
1470 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1472 return rc;
1475 #ifdef CONFIG_VIDEO_ADV_DEBUG
1478 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1479 chip=<chip>[,min=<addr>,max=<addr>]
1480 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1481 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1482 chip=<chip>,reg=<addr>,val=<val>
1483 set the register [VIDIOC_DBG_S_REGISTER]
1485 if type == host, then <chip> is the hosts chip ID (default 0)
1486 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1487 if type == i2caddr, then <chip> is the 7-bit I2C address
1490 static int vidioc_g_register(struct file *file, void *priv,
1491 struct v4l2_dbg_register *reg)
1493 struct cx231xx_fh *fh = priv;
1494 struct cx231xx *dev = fh->dev;
1495 int ret = 0;
1496 u8 value[4] = { 0, 0, 0, 0 };
1497 u32 data = 0;
1499 switch (reg->match.type) {
1500 case V4L2_CHIP_MATCH_HOST:
1501 switch (reg->match.addr) {
1502 case 0: /* Cx231xx - internal registers */
1503 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1504 (u16)reg->reg, value, 4);
1505 reg->val = value[0] | value[1] << 8 |
1506 value[2] << 16 | value[3] << 24;
1507 break;
1508 case 1: /* AFE - read byte */
1509 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1510 (u16)reg->reg, 2, &data, 1);
1511 reg->val = le32_to_cpu(data & 0xff);
1512 break;
1513 case 14: /* AFE - read dword */
1514 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1515 (u16)reg->reg, 2, &data, 4);
1516 reg->val = le32_to_cpu(data);
1517 break;
1518 case 2: /* Video Block - read byte */
1519 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1520 (u16)reg->reg, 2, &data, 1);
1521 reg->val = le32_to_cpu(data & 0xff);
1522 break;
1523 case 24: /* Video Block - read dword */
1524 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1525 (u16)reg->reg, 2, &data, 4);
1526 reg->val = le32_to_cpu(data);
1527 break;
1528 case 3: /* I2S block - read byte */
1529 ret = cx231xx_read_i2c_data(dev,
1530 I2S_BLK_DEVICE_ADDRESS,
1531 (u16)reg->reg, 1,
1532 &data, 1);
1533 reg->val = le32_to_cpu(data & 0xff);
1534 break;
1535 case 34: /* I2S Block - read dword */
1536 ret =
1537 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1538 (u16)reg->reg, 1, &data, 4);
1539 reg->val = le32_to_cpu(data);
1540 break;
1542 return ret < 0 ? ret : 0;
1544 case V4L2_CHIP_MATCH_I2C_DRIVER:
1545 call_all(dev, core, g_register, reg);
1546 return 0;
1547 case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
1548 switch (reg->match.addr) {
1549 case 0: /* Cx231xx - internal registers */
1550 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1551 (u16)reg->reg, value, 4);
1552 reg->val = value[0] | value[1] << 8 |
1553 value[2] << 16 | value[3] << 24;
1555 break;
1556 case 0x600:/* AFE - read byte */
1557 ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
1558 (u16)reg->reg, 2,
1559 &data, 1 , 0);
1560 reg->val = le32_to_cpu(data & 0xff);
1561 break;
1563 case 0x880:/* Video Block - read byte */
1564 if (reg->reg < 0x0b) {
1565 ret = cx231xx_read_i2c_master(dev,
1566 VID_BLK_I2C_ADDRESS,
1567 (u16)reg->reg, 2,
1568 &data, 1 , 0);
1569 reg->val = le32_to_cpu(data & 0xff);
1570 } else {
1571 ret = cx231xx_read_i2c_master(dev,
1572 VID_BLK_I2C_ADDRESS,
1573 (u16)reg->reg, 2,
1574 &data, 4 , 0);
1575 reg->val = le32_to_cpu(data);
1577 break;
1578 case 0x980:
1579 ret = cx231xx_read_i2c_master(dev,
1580 I2S_BLK_DEVICE_ADDRESS,
1581 (u16)reg->reg, 1,
1582 &data, 1 , 0);
1583 reg->val = le32_to_cpu(data & 0xff);
1584 break;
1585 case 0x400:
1586 ret =
1587 cx231xx_read_i2c_master(dev, 0x40,
1588 (u16)reg->reg, 1,
1589 &data, 1 , 0);
1590 reg->val = le32_to_cpu(data & 0xff);
1591 break;
1592 case 0xc01:
1593 ret =
1594 cx231xx_read_i2c_master(dev, 0xc0,
1595 (u16)reg->reg, 2,
1596 &data, 38, 1);
1597 reg->val = le32_to_cpu(data);
1598 break;
1599 case 0x022:
1600 ret =
1601 cx231xx_read_i2c_master(dev, 0x02,
1602 (u16)reg->reg, 1,
1603 &data, 1, 2);
1604 reg->val = le32_to_cpu(data & 0xff);
1605 break;
1606 case 0x322:
1607 ret = cx231xx_read_i2c_master(dev,
1608 0x32,
1609 (u16)reg->reg, 1,
1610 &data, 4 , 2);
1611 reg->val = le32_to_cpu(data);
1612 break;
1613 case 0x342:
1614 ret = cx231xx_read_i2c_master(dev,
1615 0x34,
1616 (u16)reg->reg, 1,
1617 &data, 4 , 2);
1618 reg->val = le32_to_cpu(data);
1619 break;
1621 default:
1622 cx231xx_info("no match device address!!\n");
1623 break;
1625 return ret < 0 ? ret : 0;
1626 /*return -EINVAL;*/
1627 default:
1628 if (!v4l2_chip_match_host(&reg->match))
1629 return -EINVAL;
1632 call_all(dev, core, g_register, reg);
1634 return ret;
1637 static int vidioc_s_register(struct file *file, void *priv,
1638 struct v4l2_dbg_register *reg)
1640 struct cx231xx_fh *fh = priv;
1641 struct cx231xx *dev = fh->dev;
1642 int ret = 0;
1643 __le64 buf;
1644 u32 value;
1645 u8 data[4] = { 0, 0, 0, 0 };
1647 buf = cpu_to_le64(reg->val);
1649 switch (reg->match.type) {
1650 case V4L2_CHIP_MATCH_HOST:
1652 value = (u32) buf & 0xffffffff;
1654 switch (reg->match.addr) {
1655 case 0: /* cx231xx internal registers */
1656 data[0] = (u8) value;
1657 data[1] = (u8) (value >> 8);
1658 data[2] = (u8) (value >> 16);
1659 data[3] = (u8) (value >> 24);
1660 ret = cx231xx_write_ctrl_reg(dev,
1661 VRT_SET_REGISTER,
1662 (u16)reg->reg, data,
1664 break;
1665 case 1: /* AFE - read byte */
1666 ret = cx231xx_write_i2c_data(dev,
1667 AFE_DEVICE_ADDRESS,
1668 (u16)reg->reg, 2,
1669 value, 1);
1670 break;
1671 case 14: /* AFE - read dword */
1672 ret = cx231xx_write_i2c_data(dev,
1673 AFE_DEVICE_ADDRESS,
1674 (u16)reg->reg, 2,
1675 value, 4);
1676 break;
1677 case 2: /* Video Block - read byte */
1678 ret =
1679 cx231xx_write_i2c_data(dev,
1680 VID_BLK_I2C_ADDRESS,
1681 (u16)reg->reg, 2,
1682 value, 1);
1683 break;
1684 case 24: /* Video Block - read dword */
1685 ret =
1686 cx231xx_write_i2c_data(dev,
1687 VID_BLK_I2C_ADDRESS,
1688 (u16)reg->reg, 2,
1689 value, 4);
1690 break;
1691 case 3: /* I2S block - read byte */
1692 ret =
1693 cx231xx_write_i2c_data(dev,
1694 I2S_BLK_DEVICE_ADDRESS,
1695 (u16)reg->reg, 1,
1696 value, 1);
1697 break;
1698 case 34: /* I2S block - read dword */
1699 ret =
1700 cx231xx_write_i2c_data(dev,
1701 I2S_BLK_DEVICE_ADDRESS,
1702 (u16)reg->reg, 1,
1703 value, 4);
1704 break;
1707 return ret < 0 ? ret : 0;
1708 case V4L2_CHIP_MATCH_I2C_ADDR:
1710 value = (u32) buf & 0xffffffff;
1712 switch (reg->match.addr) {
1713 case 0:/*cx231xx internal registers*/
1714 data[0] = (u8) value;
1715 data[1] = (u8) (value >> 8);
1716 data[2] = (u8) (value >> 16);
1717 data[3] = (u8) (value >> 24);
1718 ret = cx231xx_write_ctrl_reg(dev,
1719 VRT_SET_REGISTER,
1720 (u16)reg->reg, data,
1722 break;
1723 case 0x600:/* AFE - read byte */
1724 ret = cx231xx_write_i2c_master(dev,
1725 AFE_DEVICE_ADDRESS,
1726 (u16)reg->reg, 2,
1727 value, 1 , 0);
1728 break;
1730 case 0x880:/* Video Block - read byte */
1731 if (reg->reg < 0x0b)
1732 cx231xx_write_i2c_master(dev,
1733 VID_BLK_I2C_ADDRESS,
1734 (u16)reg->reg, 2,
1735 value, 1, 0);
1736 else
1737 cx231xx_write_i2c_master(dev,
1738 VID_BLK_I2C_ADDRESS,
1739 (u16)reg->reg, 2,
1740 value, 4, 0);
1741 break;
1742 case 0x980:
1743 ret =
1744 cx231xx_write_i2c_master(dev,
1745 I2S_BLK_DEVICE_ADDRESS,
1746 (u16)reg->reg, 1,
1747 value, 1, 0);
1748 break;
1749 case 0x400:
1750 ret =
1751 cx231xx_write_i2c_master(dev,
1752 0x40,
1753 (u16)reg->reg, 1,
1754 value, 1, 0);
1755 break;
1756 case 0xc01:
1757 ret =
1758 cx231xx_write_i2c_master(dev,
1759 0xc0,
1760 (u16)reg->reg, 1,
1761 value, 1, 1);
1762 break;
1764 case 0x022:
1765 ret =
1766 cx231xx_write_i2c_master(dev,
1767 0x02,
1768 (u16)reg->reg, 1,
1769 value, 1, 2);
1770 case 0x322:
1771 ret =
1772 cx231xx_write_i2c_master(dev,
1773 0x32,
1774 (u16)reg->reg, 1,
1775 value, 4, 2);
1776 break;
1778 case 0x342:
1779 ret =
1780 cx231xx_write_i2c_master(dev,
1781 0x34,
1782 (u16)reg->reg, 1,
1783 value, 4, 2);
1784 break;
1785 default:
1786 cx231xx_info("no match device address, "
1787 "the value is %x\n", reg->match.addr);
1788 break;
1793 default:
1794 break;
1797 call_all(dev, core, s_register, reg);
1799 return ret;
1801 #endif
1803 static int vidioc_cropcap(struct file *file, void *priv,
1804 struct v4l2_cropcap *cc)
1806 struct cx231xx_fh *fh = priv;
1807 struct cx231xx *dev = fh->dev;
1809 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1810 return -EINVAL;
1812 cc->bounds.left = 0;
1813 cc->bounds.top = 0;
1814 cc->bounds.width = dev->width;
1815 cc->bounds.height = dev->height;
1816 cc->defrect = cc->bounds;
1817 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1818 cc->pixelaspect.denominator = 59;
1820 return 0;
1823 static int vidioc_streamon(struct file *file, void *priv,
1824 enum v4l2_buf_type type)
1826 struct cx231xx_fh *fh = priv;
1827 struct cx231xx *dev = fh->dev;
1828 int rc;
1830 rc = check_dev(dev);
1831 if (rc < 0)
1832 return rc;
1834 rc = res_get(fh);
1836 if (likely(rc >= 0))
1837 rc = videobuf_streamon(&fh->vb_vidq);
1839 call_all(dev, video, s_stream, 1);
1841 return rc;
1844 static int vidioc_streamoff(struct file *file, void *priv,
1845 enum v4l2_buf_type type)
1847 struct cx231xx_fh *fh = priv;
1848 struct cx231xx *dev = fh->dev;
1849 int rc;
1851 rc = check_dev(dev);
1852 if (rc < 0)
1853 return rc;
1855 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1856 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1857 return -EINVAL;
1858 if (type != fh->type)
1859 return -EINVAL;
1861 cx25840_call(dev, video, s_stream, 0);
1863 videobuf_streamoff(&fh->vb_vidq);
1864 res_free(fh);
1866 return 0;
1869 static int vidioc_querycap(struct file *file, void *priv,
1870 struct v4l2_capability *cap)
1872 struct cx231xx_fh *fh = priv;
1873 struct cx231xx *dev = fh->dev;
1875 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1876 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1877 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1879 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1880 #if 0
1881 V4L2_CAP_SLICED_VBI_CAPTURE |
1882 #endif
1883 V4L2_CAP_VIDEO_CAPTURE |
1884 V4L2_CAP_AUDIO |
1885 V4L2_CAP_READWRITE |
1886 V4L2_CAP_STREAMING;
1888 if (dev->tuner_type != TUNER_ABSENT)
1889 cap->capabilities |= V4L2_CAP_TUNER;
1891 return 0;
1894 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1895 struct v4l2_fmtdesc *f)
1897 if (unlikely(f->index >= ARRAY_SIZE(format)))
1898 return -EINVAL;
1900 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1901 f->pixelformat = format[f->index].fourcc;
1903 return 0;
1906 /* Sliced VBI ioctls */
1907 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1908 struct v4l2_format *f)
1910 struct cx231xx_fh *fh = priv;
1911 struct cx231xx *dev = fh->dev;
1912 int rc;
1914 rc = check_dev(dev);
1915 if (rc < 0)
1916 return rc;
1918 f->fmt.sliced.service_set = 0;
1920 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1922 if (f->fmt.sliced.service_set == 0)
1923 rc = -EINVAL;
1925 return rc;
1928 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1929 struct v4l2_format *f)
1931 struct cx231xx_fh *fh = priv;
1932 struct cx231xx *dev = fh->dev;
1933 int rc;
1935 rc = check_dev(dev);
1936 if (rc < 0)
1937 return rc;
1939 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1941 if (f->fmt.sliced.service_set == 0)
1942 return -EINVAL;
1944 return 0;
1947 /* RAW VBI ioctls */
1949 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1950 struct v4l2_format *f)
1952 struct cx231xx_fh *fh = priv;
1953 struct cx231xx *dev = fh->dev;
1954 f->fmt.vbi.sampling_rate = 6750000 * 4;
1955 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1956 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1957 f->fmt.vbi.offset = 0;
1958 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1959 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1960 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1961 PAL_VBI_LINES : NTSC_VBI_LINES;
1962 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1963 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1964 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1966 return 0;
1970 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1971 struct v4l2_format *f)
1973 struct cx231xx_fh *fh = priv;
1974 struct cx231xx *dev = fh->dev;
1976 if (dev->vbi_stream_on && !fh->stream_on) {
1977 cx231xx_errdev("%s device in use by another fh\n", __func__);
1978 return -EBUSY;
1981 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1982 f->fmt.vbi.sampling_rate = 6750000 * 4;
1983 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1984 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1985 f->fmt.vbi.offset = 0;
1986 f->fmt.vbi.flags = 0;
1987 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1988 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1989 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1990 PAL_VBI_LINES : NTSC_VBI_LINES;
1991 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1992 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1993 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1995 return 0;
1999 static int vidioc_reqbufs(struct file *file, void *priv,
2000 struct v4l2_requestbuffers *rb)
2002 struct cx231xx_fh *fh = priv;
2003 struct cx231xx *dev = fh->dev;
2004 int rc;
2006 rc = check_dev(dev);
2007 if (rc < 0)
2008 return rc;
2010 return videobuf_reqbufs(&fh->vb_vidq, rb);
2013 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
2015 struct cx231xx_fh *fh = priv;
2016 struct cx231xx *dev = fh->dev;
2017 int rc;
2019 rc = check_dev(dev);
2020 if (rc < 0)
2021 return rc;
2023 return videobuf_querybuf(&fh->vb_vidq, b);
2026 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2028 struct cx231xx_fh *fh = priv;
2029 struct cx231xx *dev = fh->dev;
2030 int rc;
2032 rc = check_dev(dev);
2033 if (rc < 0)
2034 return rc;
2036 return videobuf_qbuf(&fh->vb_vidq, b);
2039 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2041 struct cx231xx_fh *fh = priv;
2042 struct cx231xx *dev = fh->dev;
2043 int rc;
2045 rc = check_dev(dev);
2046 if (rc < 0)
2047 return rc;
2049 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
2052 /* ----------------------------------------------------------- */
2053 /* RADIO ESPECIFIC IOCTLS */
2054 /* ----------------------------------------------------------- */
2056 static int radio_querycap(struct file *file, void *priv,
2057 struct v4l2_capability *cap)
2059 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2061 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
2062 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
2063 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2065 cap->capabilities = V4L2_CAP_TUNER;
2066 return 0;
2069 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2071 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2073 if (unlikely(t->index > 0))
2074 return -EINVAL;
2076 strcpy(t->name, "Radio");
2077 t->type = V4L2_TUNER_RADIO;
2079 call_all(dev, tuner, s_tuner, t);
2081 return 0;
2084 static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
2086 if (i->index != 0)
2087 return -EINVAL;
2088 strcpy(i->name, "Radio");
2089 i->type = V4L2_INPUT_TYPE_TUNER;
2091 return 0;
2094 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2096 if (unlikely(a->index))
2097 return -EINVAL;
2099 strcpy(a->name, "Radio");
2100 return 0;
2103 static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2105 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2107 if (0 != t->index)
2108 return -EINVAL;
2110 call_all(dev, tuner, s_tuner, t);
2112 return 0;
2115 static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
2117 return 0;
2120 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2122 return 0;
2125 static int radio_queryctrl(struct file *file, void *priv,
2126 struct v4l2_queryctrl *c)
2128 int i;
2130 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
2131 return -EINVAL;
2132 if (c->id == V4L2_CID_AUDIO_MUTE) {
2133 for (i = 0; i < CX231XX_CTLS; i++) {
2134 if (cx231xx_ctls[i].v.id == c->id)
2135 break;
2137 if (i == CX231XX_CTLS)
2138 return -EINVAL;
2139 *c = cx231xx_ctls[i].v;
2140 } else
2141 *c = no_ctl;
2142 return 0;
2146 * cx231xx_v4l2_open()
2147 * inits the device and starts isoc transfer
2149 static int cx231xx_v4l2_open(struct file *filp)
2151 int errCode = 0, radio = 0;
2152 struct video_device *vdev = video_devdata(filp);
2153 struct cx231xx *dev = video_drvdata(filp);
2154 struct cx231xx_fh *fh;
2155 enum v4l2_buf_type fh_type = 0;
2157 switch (vdev->vfl_type) {
2158 case VFL_TYPE_GRABBER:
2159 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2160 break;
2161 case VFL_TYPE_VBI:
2162 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2163 break;
2164 case VFL_TYPE_RADIO:
2165 radio = 1;
2166 break;
2169 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
2170 video_device_node_name(vdev), v4l2_type_names[fh_type],
2171 dev->users);
2173 #if 0
2174 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2175 if (errCode < 0) {
2176 cx231xx_errdev
2177 ("Device locked on digital mode. Can't open analog\n");
2178 return -EBUSY;
2180 #endif
2182 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
2183 if (!fh) {
2184 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
2185 return -ENOMEM;
2187 fh->dev = dev;
2188 fh->radio = radio;
2189 fh->type = fh_type;
2190 filp->private_data = fh;
2192 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2193 dev->width = norm_maxw(dev);
2194 dev->height = norm_maxh(dev);
2196 /* Power up in Analog TV mode */
2197 if (dev->board.external_av)
2198 cx231xx_set_power_mode(dev,
2199 POLARIS_AVMODE_ENXTERNAL_AV);
2200 else
2201 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
2203 #if 0
2204 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2205 #endif
2207 /* set video alternate setting */
2208 cx231xx_set_video_alternate(dev);
2210 /* Needed, since GPIO might have disabled power of
2211 some i2c device */
2212 cx231xx_config_i2c(dev);
2214 /* device needs to be initialized before isoc transfer */
2215 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
2218 if (fh->radio) {
2219 cx231xx_videodbg("video_open: setting radio device\n");
2221 /* cx231xx_start_radio(dev); */
2223 call_all(dev, tuner, s_radio);
2226 dev->users++;
2228 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2229 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2230 NULL, &dev->video_mode.slock,
2231 fh->type, V4L2_FIELD_INTERLACED,
2232 sizeof(struct cx231xx_buffer),
2233 fh, &dev->lock);
2234 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2235 /* Set the required alternate setting VBI interface works in
2236 Bulk mode only */
2237 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2239 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2240 NULL, &dev->vbi_mode.slock,
2241 fh->type, V4L2_FIELD_SEQ_TB,
2242 sizeof(struct cx231xx_buffer),
2243 fh, &dev->lock);
2246 return errCode;
2250 * cx231xx_realease_resources()
2251 * unregisters the v4l2,i2c and usb devices
2252 * called when the device gets disconected or at module unload
2254 void cx231xx_release_analog_resources(struct cx231xx *dev)
2257 /*FIXME: I2C IR should be disconnected */
2259 if (dev->radio_dev) {
2260 if (video_is_registered(dev->radio_dev))
2261 video_unregister_device(dev->radio_dev);
2262 else
2263 video_device_release(dev->radio_dev);
2264 dev->radio_dev = NULL;
2266 if (dev->vbi_dev) {
2267 cx231xx_info("V4L2 device %s deregistered\n",
2268 video_device_node_name(dev->vbi_dev));
2269 if (video_is_registered(dev->vbi_dev))
2270 video_unregister_device(dev->vbi_dev);
2271 else
2272 video_device_release(dev->vbi_dev);
2273 dev->vbi_dev = NULL;
2275 if (dev->vdev) {
2276 cx231xx_info("V4L2 device %s deregistered\n",
2277 video_device_node_name(dev->vdev));
2279 if (dev->board.has_417)
2280 cx231xx_417_unregister(dev);
2282 if (video_is_registered(dev->vdev))
2283 video_unregister_device(dev->vdev);
2284 else
2285 video_device_release(dev->vdev);
2286 dev->vdev = NULL;
2291 * cx231xx_v4l2_close()
2292 * stops streaming and deallocates all resources allocated by the v4l2
2293 * calls and ioctls
2295 static int cx231xx_v4l2_close(struct file *filp)
2297 struct cx231xx_fh *fh = filp->private_data;
2298 struct cx231xx *dev = fh->dev;
2300 cx231xx_videodbg("users=%d\n", dev->users);
2302 cx231xx_videodbg("users=%d\n", dev->users);
2303 if (res_check(fh))
2304 res_free(fh);
2307 * To workaround error number=-71 on EP0 for VideoGrabber,
2308 * need exclude following.
2309 * FIXME: It is probably safe to remove most of these, as we're
2310 * now avoiding the alternate setting for INDEX_VANC
2312 if (!dev->board.no_alt_vanc)
2313 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2314 videobuf_stop(&fh->vb_vidq);
2315 videobuf_mmap_free(&fh->vb_vidq);
2317 /* the device is already disconnect,
2318 free the remaining resources */
2319 if (dev->state & DEV_DISCONNECTED) {
2320 if (atomic_read(&dev->devlist_count) > 0) {
2321 cx231xx_release_resources(dev);
2322 kfree(dev);
2323 dev = NULL;
2324 return 0;
2326 return 0;
2329 /* do this before setting alternate! */
2330 cx231xx_uninit_vbi_isoc(dev);
2332 /* set alternate 0 */
2333 if (!dev->vbi_or_sliced_cc_mode)
2334 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2335 else
2336 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2338 kfree(fh);
2339 dev->users--;
2340 wake_up_interruptible_nr(&dev->open, 1);
2341 return 0;
2344 dev->users--;
2345 if (!dev->users) {
2346 videobuf_stop(&fh->vb_vidq);
2347 videobuf_mmap_free(&fh->vb_vidq);
2349 /* the device is already disconnect,
2350 free the remaining resources */
2351 if (dev->state & DEV_DISCONNECTED) {
2352 cx231xx_release_resources(dev);
2353 kfree(dev);
2354 dev = NULL;
2355 return 0;
2358 /* Save some power by putting tuner to sleep */
2359 call_all(dev, core, s_power, 0);
2361 /* do this before setting alternate! */
2362 if (dev->USE_ISO)
2363 cx231xx_uninit_isoc(dev);
2364 else
2365 cx231xx_uninit_bulk(dev);
2366 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2368 /* set alternate 0 */
2369 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2371 kfree(fh);
2372 wake_up_interruptible_nr(&dev->open, 1);
2373 return 0;
2377 * cx231xx_v4l2_read()
2378 * will allocate buffers when called for the first time
2380 static ssize_t
2381 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2382 loff_t *pos)
2384 struct cx231xx_fh *fh = filp->private_data;
2385 struct cx231xx *dev = fh->dev;
2386 int rc;
2388 rc = check_dev(dev);
2389 if (rc < 0)
2390 return rc;
2392 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2393 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2394 rc = res_get(fh);
2396 if (unlikely(rc < 0))
2397 return rc;
2399 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2400 filp->f_flags & O_NONBLOCK);
2402 return 0;
2406 * cx231xx_v4l2_poll()
2407 * will allocate buffers when called for the first time
2409 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
2411 struct cx231xx_fh *fh = filp->private_data;
2412 struct cx231xx *dev = fh->dev;
2413 int rc;
2415 rc = check_dev(dev);
2416 if (rc < 0)
2417 return rc;
2419 rc = res_get(fh);
2421 if (unlikely(rc < 0))
2422 return POLLERR;
2424 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2425 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2426 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2427 else
2428 return POLLERR;
2432 * cx231xx_v4l2_mmap()
2434 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2436 struct cx231xx_fh *fh = filp->private_data;
2437 struct cx231xx *dev = fh->dev;
2438 int rc;
2440 rc = check_dev(dev);
2441 if (rc < 0)
2442 return rc;
2444 rc = res_get(fh);
2446 if (unlikely(rc < 0))
2447 return rc;
2449 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2451 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2452 (unsigned long)vma->vm_start,
2453 (unsigned long)vma->vm_end -
2454 (unsigned long)vma->vm_start, rc);
2456 return rc;
2459 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2460 .owner = THIS_MODULE,
2461 .open = cx231xx_v4l2_open,
2462 .release = cx231xx_v4l2_close,
2463 .read = cx231xx_v4l2_read,
2464 .poll = cx231xx_v4l2_poll,
2465 .mmap = cx231xx_v4l2_mmap,
2466 .unlocked_ioctl = video_ioctl2,
2469 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2470 .vidioc_querycap = vidioc_querycap,
2471 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2472 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2473 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2474 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2475 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2476 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2477 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2478 .vidioc_g_audio = vidioc_g_audio,
2479 .vidioc_s_audio = vidioc_s_audio,
2480 .vidioc_cropcap = vidioc_cropcap,
2481 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2482 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2483 .vidioc_reqbufs = vidioc_reqbufs,
2484 .vidioc_querybuf = vidioc_querybuf,
2485 .vidioc_qbuf = vidioc_qbuf,
2486 .vidioc_dqbuf = vidioc_dqbuf,
2487 .vidioc_s_std = vidioc_s_std,
2488 .vidioc_g_std = vidioc_g_std,
2489 .vidioc_enum_input = vidioc_enum_input,
2490 .vidioc_g_input = vidioc_g_input,
2491 .vidioc_s_input = vidioc_s_input,
2492 .vidioc_queryctrl = vidioc_queryctrl,
2493 .vidioc_g_ctrl = vidioc_g_ctrl,
2494 .vidioc_s_ctrl = vidioc_s_ctrl,
2495 .vidioc_streamon = vidioc_streamon,
2496 .vidioc_streamoff = vidioc_streamoff,
2497 .vidioc_g_tuner = vidioc_g_tuner,
2498 .vidioc_s_tuner = vidioc_s_tuner,
2499 .vidioc_g_frequency = vidioc_g_frequency,
2500 .vidioc_s_frequency = vidioc_s_frequency,
2501 #ifdef CONFIG_VIDEO_ADV_DEBUG
2502 .vidioc_g_register = vidioc_g_register,
2503 .vidioc_s_register = vidioc_s_register,
2504 #endif
2507 static struct video_device cx231xx_vbi_template;
2509 static const struct video_device cx231xx_video_template = {
2510 .fops = &cx231xx_v4l_fops,
2511 .release = video_device_release,
2512 .ioctl_ops = &video_ioctl_ops,
2513 .tvnorms = V4L2_STD_ALL,
2514 .current_norm = V4L2_STD_PAL,
2517 static const struct v4l2_file_operations radio_fops = {
2518 .owner = THIS_MODULE,
2519 .open = cx231xx_v4l2_open,
2520 .release = cx231xx_v4l2_close,
2521 .ioctl = video_ioctl2,
2524 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2525 .vidioc_querycap = radio_querycap,
2526 .vidioc_g_tuner = radio_g_tuner,
2527 .vidioc_enum_input = radio_enum_input,
2528 .vidioc_g_audio = radio_g_audio,
2529 .vidioc_s_tuner = radio_s_tuner,
2530 .vidioc_s_audio = radio_s_audio,
2531 .vidioc_s_input = radio_s_input,
2532 .vidioc_queryctrl = radio_queryctrl,
2533 .vidioc_g_ctrl = vidioc_g_ctrl,
2534 .vidioc_s_ctrl = vidioc_s_ctrl,
2535 .vidioc_g_frequency = vidioc_g_frequency,
2536 .vidioc_s_frequency = vidioc_s_frequency,
2537 #ifdef CONFIG_VIDEO_ADV_DEBUG
2538 .vidioc_g_register = vidioc_g_register,
2539 .vidioc_s_register = vidioc_s_register,
2540 #endif
2543 static struct video_device cx231xx_radio_template = {
2544 .name = "cx231xx-radio",
2545 .fops = &radio_fops,
2546 .ioctl_ops = &radio_ioctl_ops,
2549 /******************************** usb interface ******************************/
2551 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2552 const struct video_device
2553 *template, const char *type_name)
2555 struct video_device *vfd;
2557 vfd = video_device_alloc();
2558 if (NULL == vfd)
2559 return NULL;
2561 *vfd = *template;
2562 vfd->v4l2_dev = &dev->v4l2_dev;
2563 vfd->release = video_device_release;
2564 vfd->debug = video_debug;
2565 vfd->lock = &dev->lock;
2567 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2569 video_set_drvdata(vfd, dev);
2570 return vfd;
2573 int cx231xx_register_analog_devices(struct cx231xx *dev)
2575 int ret;
2577 cx231xx_info("%s: v4l2 driver version %s\n",
2578 dev->name, CX231XX_VERSION);
2580 /* set default norm */
2581 /*dev->norm = cx231xx_video_template.current_norm; */
2582 dev->width = norm_maxw(dev);
2583 dev->height = norm_maxh(dev);
2584 dev->interlaced = 0;
2586 /* Analog specific initialization */
2587 dev->format = &format[0];
2589 /* Set the initial input */
2590 video_mux(dev, dev->video_input);
2592 /* Audio defaults */
2593 dev->mute = 1;
2594 dev->volume = 0x1f;
2596 /* enable vbi capturing */
2597 /* write code here... */
2599 /* allocate and fill video video_device struct */
2600 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2601 if (!dev->vdev) {
2602 cx231xx_errdev("cannot allocate video_device.\n");
2603 return -ENODEV;
2606 /* register v4l2 video video_device */
2607 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2608 video_nr[dev->devno]);
2609 if (ret) {
2610 cx231xx_errdev("unable to register video device (error=%i).\n",
2611 ret);
2612 return ret;
2615 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2616 dev->name, video_device_node_name(dev->vdev));
2618 /* Initialize VBI template */
2619 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2620 sizeof(cx231xx_vbi_template));
2621 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2623 /* Allocate and fill vbi video_device struct */
2624 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2626 /* register v4l2 vbi video_device */
2627 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2628 vbi_nr[dev->devno]);
2629 if (ret < 0) {
2630 cx231xx_errdev("unable to register vbi device\n");
2631 return ret;
2634 cx231xx_info("%s/0: registered device %s\n",
2635 dev->name, video_device_node_name(dev->vbi_dev));
2637 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2638 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2639 "radio");
2640 if (!dev->radio_dev) {
2641 cx231xx_errdev("cannot allocate video_device.\n");
2642 return -ENODEV;
2644 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2645 radio_nr[dev->devno]);
2646 if (ret < 0) {
2647 cx231xx_errdev("can't register radio device\n");
2648 return ret;
2650 cx231xx_info("Registered radio device as %s\n",
2651 video_device_node_name(dev->radio_dev));
2654 cx231xx_info("V4L2 device registered as %s and %s\n",
2655 video_device_node_name(dev->vdev),
2656 video_device_node_name(dev->vbi_dev));
2658 return 0;