]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/cx231xx/cx231xx-video.c
V4L/DVB (10954): Add cx231xx USB driver
[net-next-2.6.git] / drivers / media / video / cx231xx / cx231xx-video.c
CommitLineData
e0d3bafd
SD
1/*
2 cx231xx-video.c - driver for Conexant Cx23100/101/102 USB video capture devices
3
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 Based on em28xx driver
6 Based on cx23885 driver
7 Based on cx88 driver
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
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
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>
41
42#include "dvb_frontend.h"
43
44#include "cx231xx.h"
45#include "cx231xx-vbi.h"
46
47
48#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
49#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
50
51
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)
56
57static unsigned int isoc_debug;
58module_param(isoc_debug, int, 0644);
59MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
60
61#define cx231xx_isocdbg(fmt, arg...) \
62do {\
63 if (isoc_debug) { \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); \
66 } \
67 } while (0)
68
69MODULE_AUTHOR(DRIVER_AUTHOR);
70MODULE_DESCRIPTION(DRIVER_DESC);
71MODULE_LICENSE("GPL");
72
73
74
75static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
79
80module_param_array(card, int, NULL, 0444);
81module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
83module_param_array(radio_nr, int, NULL, 0444);
84
85MODULE_PARM_DESC(card, "card type");
86MODULE_PARM_DESC(video_nr, "video device numbers");
87MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88MODULE_PARM_DESC(radio_nr, "radio device numbers");
89
90static unsigned int video_debug;
91module_param(video_debug, int, 0644);
92MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
93
94
95
96/* supported video standards */
97static struct cx231xx_fmt format[] = {
98 {
99 .name = "16bpp YUY2, 4:2:2, packed",
100 .fourcc = V4L2_PIX_FMT_YUYV,
101 .depth = 16,
102 .reg = 0,
103 },
104};
105
106
107/* supported controls */
108/* Common to all boards */
109
110/* ------------------------------------------------------------------- */
111
112static const struct v4l2_queryctrl no_ctl = {
113 .name = "42",
114 .flags = V4L2_CTRL_FLAG_DISABLED,
115};
116
117static struct cx231xx_ctrl cx231xx_ctls[] = {
118 /* --- video --- */
119 {
120 .v = {
121 .id = V4L2_CID_BRIGHTNESS,
122 .name = "Brightness",
123 .minimum = 0x00,
124 .maximum = 0xff,
125 .step = 1,
126 .default_value = 0x7f,
127 .type = V4L2_CTRL_TYPE_INTEGER,
128 },
129 .off = 128,
130 .reg = LUMA_CTRL,
131 .mask = 0x00ff,
132 .shift = 0,
133 }, {
134 .v = {
135 .id = V4L2_CID_CONTRAST,
136 .name = "Contrast",
137 .minimum = 0,
138 .maximum = 0xff,
139 .step = 1,
140 .default_value = 0x3f,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 },
143 .off = 0,
144 .reg = LUMA_CTRL,
145 .mask = 0xff00,
146 .shift = 8,
147 }, {
148 .v = {
149 .id = V4L2_CID_HUE,
150 .name = "Hue",
151 .minimum = 0,
152 .maximum = 0xff,
153 .step = 1,
154 .default_value = 0x7f,
155 .type = V4L2_CTRL_TYPE_INTEGER,
156 },
157 .off = 128,
158 .reg = CHROMA_CTRL,
159 .mask = 0xff0000,
160 .shift = 16,
161 }, {
162 /* strictly, this only describes only U saturation.
163 * V saturation is handled specially through code.
164 */
165 .v = {
166 .id = V4L2_CID_SATURATION,
167 .name = "Saturation",
168 .minimum = 0,
169 .maximum = 0xff,
170 .step = 1,
171 .default_value = 0x7f,
172 .type = V4L2_CTRL_TYPE_INTEGER,
173 },
174 .off = 0,
175 .reg = CHROMA_CTRL,
176 .mask = 0x00ff,
177 .shift = 0,
178 }, {
179 /* --- audio --- */
180 .v = {
181 .id = V4L2_CID_AUDIO_MUTE,
182 .name = "Mute",
183 .minimum = 0,
184 .maximum = 1,
185 .default_value = 1,
186 .type = V4L2_CTRL_TYPE_BOOLEAN,
187 },
188 .reg = PATH1_CTL1,
189 .mask = (0x1f << 24),
190 .shift = 24,
191 }, {
192 .v = {
193 .id = V4L2_CID_AUDIO_VOLUME,
194 .name = "Volume",
195 .minimum = 0,
196 .maximum = 0x3f,
197 .step = 1,
198 .default_value = 0x3f,
199 .type = V4L2_CTRL_TYPE_INTEGER,
200 },
201 .reg = PATH1_VOL_CTL,
202 .mask = 0xff,
203 .shift = 0,
204 }
205};
206static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
207
208static const u32 cx231xx_user_ctrls[] = {
209 V4L2_CID_USER_CLASS,
210 V4L2_CID_BRIGHTNESS,
211 V4L2_CID_CONTRAST,
212 V4L2_CID_SATURATION,
213 V4L2_CID_HUE,
214 V4L2_CID_AUDIO_VOLUME,
215#if 0
216 V4L2_CID_AUDIO_BALANCE,
217#endif
218 V4L2_CID_AUDIO_MUTE,
219 0
220};
221
222static const u32 *ctrl_classes[] = {
223 cx231xx_user_ctrls,
224 NULL
225};
226
227
228/* ------------------------------------------------------------------
229 Video buffer and parser functions
230 ------------------------------------------------------------------*/
231
232/*
233 * Announces that a buffer were filled and request the next
234 */
235static inline void buffer_filled(struct cx231xx *dev,
236 struct cx231xx_dmaqueue *dma_q,
237 struct cx231xx_buffer *buf)
238{
239 /* Advice that buffer was filled */
240 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
241 buf->vb.state = VIDEOBUF_DONE;
242 buf->vb.field_count++;
243 do_gettimeofday(&buf->vb.ts);
244
245 dev->video_mode.isoc_ctl.buf = NULL;
246
247 list_del(&buf->vb.queue);
248 wake_up(&buf->vb.done);
249}
250
251
252static inline void print_err_status(struct cx231xx *dev,
253 int packet, int status)
254{
255 char *errmsg = "Unknown";
256
257 switch (status) {
258 case -ENOENT:
259 errmsg = "unlinked synchronuously";
260 break;
261 case -ECONNRESET:
262 errmsg = "unlinked asynchronuously";
263 break;
264 case -ENOSR:
265 errmsg = "Buffer error (overrun)";
266 break;
267 case -EPIPE:
268 errmsg = "Stalled (device not responding)";
269 break;
270 case -EOVERFLOW:
271 errmsg = "Babble (bad cable?)";
272 break;
273 case -EPROTO:
274 errmsg = "Bit-stuff error (bad cable?)";
275 break;
276 case -EILSEQ:
277 errmsg = "CRC/Timeout (could be anything)";
278 break;
279 case -ETIME:
280 errmsg = "Device does not respond";
281 break;
282 }
283 if (packet < 0) {
284 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
285 } else {
286 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
287 packet, status, errmsg);
288 }
289}
290
291/*
292 * video-buf generic routine to get the next available buffer
293 */
294static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
295 struct cx231xx_buffer **buf)
296{
297 struct cx231xx_video_mode *vmode = container_of(dma_q, struct cx231xx_video_mode, vidq);
298 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
299
300 char *outp;
301
302
303 if (list_empty(&dma_q->active)) {
304 cx231xx_isocdbg("No active queue to serve\n");
305 dev->video_mode.isoc_ctl.buf = NULL;
306 *buf = NULL;
307 return;
308 }
309
310 /* Get the next buffer */
311 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
312
313 /* Cleans up buffer - Usefull for testing for frame/URB loss */
314 outp = videobuf_to_vmalloc(&(*buf)->vb);
315 memset(outp, 0, (*buf)->vb.size);
316
317 dev->video_mode.isoc_ctl.buf = *buf;
318
319 return;
320}
321
322/*
323 * Controls the isoc copy of each urb packet
324 */
325static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
326{
327 struct cx231xx_buffer *buf;
328 struct cx231xx_dmaqueue *dma_q = urb->context;
329 unsigned char *outp = NULL;
330 int i, rc = 1;
331 unsigned char *p_buffer;
332 u32 bytes_parsed = 0, buffer_size = 0;
333 u8 sav_eav = 0;
334
335 if (!dev)
336 return 0;
337
338 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
339 return 0;
340
341 if (urb->status < 0) {
342 print_err_status(dev, -1, urb->status);
343 if (urb->status == -ENOENT)
344 return 0;
345 }
346
347 buf = dev->video_mode.isoc_ctl.buf;
348 if (buf != NULL)
349 outp = videobuf_to_vmalloc(&buf->vb);
350
351 for (i = 0; i < urb->number_of_packets; i++) {
352 int status = urb->iso_frame_desc[i].status;
353
354 if (status < 0) {
355 print_err_status(dev, i, status);
356 if (urb->iso_frame_desc[i].status != -EPROTO)
357 continue;
358 }
359
360 if (urb->iso_frame_desc[i].actual_length <= 0) {
361 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
362 continue;
363 }
364 if (urb->iso_frame_desc[i].actual_length >
365 dev->video_mode.max_pkt_size) {
366 cx231xx_isocdbg("packet bigger than packet size");
367 continue;
368 }
369
370 /* get buffer pointer and length */
371 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
372 buffer_size = urb->iso_frame_desc[i].actual_length;
373 bytes_parsed = 0;
374
375 if(dma_q->is_partial_line)
376 {
377 /* Handle the case where we were working on a partial line */
378 sav_eav = dma_q->last_sav;
379 } else {
380 /* Check for a SAV/EAV overlapping the buffer boundary */
381 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer, dma_q->partial_buf, &bytes_parsed);
382 }
383
384 sav_eav &= 0xF0;
385 /* Get the first line if we have some portion of an SAV/EAV from the last buffer
386 or a partial line */
387 if(sav_eav) {
388 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
389 sav_eav, /* SAV/EAV */
390 p_buffer + bytes_parsed, /* p_buffer */
391 buffer_size - bytes_parsed); /* buffer size */
392 }
393
394 /* Now parse data that is completely in this buffer */
395 /* dma_q->is_partial_line = 0; */
396
397 while(bytes_parsed < buffer_size)
398 {
399 u32 bytes_used = 0;
400
401 sav_eav = cx231xx_find_next_SAV_EAV(
402 p_buffer + bytes_parsed, /* p_buffer */
403 buffer_size - bytes_parsed, /* buffer size */
404 &bytes_used); /* Receives bytes used to get SAV/EAV */
405
406 bytes_parsed += bytes_used;
407
408 sav_eav &= 0xF0;
409 if(sav_eav && (bytes_parsed < buffer_size))
410 {
411 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
412 sav_eav, /* SAV/EAV */
413 p_buffer + bytes_parsed, /* p_buffer */
414 buffer_size - bytes_parsed); /* buffer size */
415 }
416 }
417
418 /* Save the last four bytes of the buffer so we can check the buffer boundary
419 condition next time */
420 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
421 bytes_parsed = 0;
422
423 }
424 return rc;
425}
426
427u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf, u32 *p_bytes_used)
428{
429 u32 bytes_used;
430 u8 boundary_bytes[8];
431 u8 sav_eav = 0;
432
433 *p_bytes_used = 0;
434
435 /* Create an array of the last 4 bytes of the last buffer and the first
436 4 bytes of the current buffer. */
437
438 memcpy(boundary_bytes, partial_buf, 4);
439 memcpy(boundary_bytes + 4, p_buffer, 4);
440
441 /* Check for the SAV/EAV in the boundary buffer */
442 sav_eav = cx231xx_find_next_SAV_EAV((u8*)&boundary_bytes, 8, &bytes_used);
443
444 if(sav_eav) {
445 /* found a boundary SAV/EAV. Updates the bytes used to reflect
446 only those used in the new buffer */
447 *p_bytes_used = bytes_used - 4;
448 }
449
450 return sav_eav;
451}
452
453u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
454{
455 u32 i;
456 u8 sav_eav = 0;
457
458 /* Don't search if the buffer size is less than 4. It causes a page fault since
459 buffer_size - 4 evaluates to a large number in that case. */
460 if(buffer_size < 4) {
461 *p_bytes_used = buffer_size;
462 return 0;
463 }
464
465 for(i = 0;i < (buffer_size - 3); i++) {
466
467 if((p_buffer[i] == 0xFF) &&
468 (p_buffer[i+1] == 0x00) &&
469 (p_buffer[i+2] == 0x00)) {
470
471 *p_bytes_used = i+4;
472 sav_eav = p_buffer[i+3];
473 return sav_eav;
474 }
475 }
476
477 *p_bytes_used = buffer_size;
478 return 0;
479}
480
481
482
483
484u32 cx231xx_get_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
485 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
486{
487 u32 bytes_copied = 0;
488 int current_field = -1;
489
490
491 switch(sav_eav) {
492 case SAV_ACTIVE_VIDEO_FIELD1:
493 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
494 there will be no active data contained between the SAV and EAV */
495 if ( (buffer_size > 3) &&
496 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
497 ( (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
498 (p_buffer[3] == EAV_VBLANK_FIELD1) || (p_buffer[3] == EAV_VBLANK_FIELD2)
499 )
500 )
501 {
502 return bytes_copied;
503 }
504 current_field = 1;
505 break;
506
507 case SAV_ACTIVE_VIDEO_FIELD2:
508 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
509 there will be no active data contained between the SAV and EAV */
510 if ( (buffer_size > 3) &&
511 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
512 ( (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
513 (p_buffer[3] == EAV_VBLANK_FIELD1) || (p_buffer[3] == EAV_VBLANK_FIELD2)
514 )
515 )
516 {
517 return bytes_copied;
518 }
519 current_field = 2;
520 break;
521 }
522
523 dma_q->last_sav = sav_eav;
524
525 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer, buffer_size, current_field);
526
527 return bytes_copied;
528}
529
530u32 cx231xx_copy_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
531 u8 *p_line, u32 length, int field_number)
532{
533 u32 bytes_to_copy;
534 struct cx231xx_buffer *buf;
535 u32 _line_size = dev->width * 2;
536
537 if( dma_q->current_field != field_number ) {
538 cx231xx_reset_video_buffer(dev, dma_q);
539 }
540
541 /* get the buffer pointer */
542 buf = dev->video_mode.isoc_ctl.buf;
543
544 /* Remember the field number for next time */
545 dma_q->current_field = field_number;
546
547 bytes_to_copy = dma_q->bytes_left_in_line;
548 if(bytes_to_copy > length)
549 bytes_to_copy = length;
550
551
552 if(dma_q->lines_completed >= dma_q->lines_per_field) {
553 dma_q->bytes_left_in_line -= bytes_to_copy;
554 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
555 return 0;
556 }
557
558 dma_q->is_partial_line = 1;
559
560 /* If we don't have a buffer, just return the number of bytes we would
561 have copied if we had a buffer. */
562 if(!buf)
563 {
564 dma_q->bytes_left_in_line -= bytes_to_copy;
565 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
566 return bytes_to_copy;
567 }
568
569 /* copy the data to video buffer */
570 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
571
572 dma_q->pos += bytes_to_copy;
573 dma_q->bytes_left_in_line -= bytes_to_copy;
574
575 if(dma_q->bytes_left_in_line == 0) {
576
577 dma_q->bytes_left_in_line = _line_size;
578 dma_q->lines_completed++;
579 dma_q->is_partial_line = 0;
580
581 if(cx231xx_is_buffer_done(dev, dma_q) && buf) {
582
583 buffer_filled(dev, dma_q, buf);
584
585 dma_q->pos = 0;
586 buf = NULL;
587 dma_q->lines_completed = 0;
588 }
589 }
590
591 return bytes_to_copy;
592}
593
594void cx231xx_reset_video_buffer(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
595{
596 struct cx231xx_buffer *buf;
597
598 /* handle the switch from field 1 to field 2 */
599 if(dma_q->current_field == 1) {
600 if(dma_q->lines_completed >= dma_q->lines_per_field ) {
601 dma_q->field1_done = 1;
602 } else {
603 dma_q->field1_done = 0;
604 }
605 }
606
607 buf = dev->video_mode.isoc_ctl.buf;
608
609 if(buf == NULL) {
610 u8* outp = NULL;
611 /* first try to get the buffer */
612 get_next_buf(dma_q, &buf);
613
614 if(buf)
615 outp = videobuf_to_vmalloc(&buf->vb);
616
617 dma_q->pos = 0;
618 dma_q->field1_done = 0;
619 dma_q->current_field = -1;
620 }
621
622 /* reset the counters */
623 dma_q->bytes_left_in_line = dev->width << 1;
624 dma_q->lines_completed = 0;
625}
626
627int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
628 u8 *p_buffer, u32 bytes_to_copy)
629{
630 u8 *p_out_buffer = NULL;
631 u32 current_line_bytes_copied = 0;
632 struct cx231xx_buffer *buf;
633 u32 _line_size = dev->width << 1;
634 void *startwrite;
635 int offset, lencopy;
636
637 buf = dev->video_mode.isoc_ctl.buf;
638
639 if (buf == NULL)
640 return -1;
641
642 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
643
644 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
645
646 /* Offset field 2 one line from the top of the buffer */
647 offset = (dma_q->current_field == 1)? 0: _line_size;
648
649 /* Offset for field 2 */
650 startwrite = p_out_buffer + offset;
651
652 /* lines already completed in the current field */
653 startwrite += (dma_q->lines_completed * _line_size * 2);
654
655 /* bytes already completed in the current line */
656 startwrite += current_line_bytes_copied;
657
658 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ? bytes_to_copy : dma_q->bytes_left_in_line;
659
660 if( (u8*)(startwrite +lencopy) > (u8*)(p_out_buffer+ buf->vb.size) ) {
661 return 0;
662 }
663
664 /* The below copies the UYVY data straight into video buffer */
665 cx231xx_swab( (u16*)p_buffer, (u16*)startwrite, (u16)lencopy);
666
667 return 0;
668}
669
670void cx231xx_swab(u16 *from, u16 *to, u16 len)
671{
672 u16 i;
673
674 if( len <= 0)
675 return;
676
677 for(i = 0; i < len/2; i++) {
678 to[i] = (from[i] << 8) | (from[i] >> 8);
679 }
680}
681
682u8 cx231xx_is_buffer_done(struct cx231xx *dev,struct cx231xx_dmaqueue *dma_q)
683{
684 u8 buffer_complete = 0;
685
686 /* Dual field stream */
687 buffer_complete =
688 ((dma_q->current_field == 2) &&
689 (dma_q->lines_completed >= dma_q->lines_per_field) &&
690 dma_q->field1_done);
691
692 return buffer_complete;
693}
694
695
696/* ------------------------------------------------------------------
697 Videobuf operations
698 ------------------------------------------------------------------*/
699
700static int
701buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
702{
703 struct cx231xx_fh *fh = vq->priv_data;
704 struct cx231xx *dev = fh->dev;
705 struct v4l2_frequency f;
706
707 *size = ( fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
708 if (0 == *count)
709 *count = CX231XX_DEF_BUF;
710
711 if (*count < CX231XX_MIN_BUF)
712 *count = CX231XX_MIN_BUF;
713
714 /* Ask tuner to go to analog mode */
715 memset(&f, 0, sizeof(f));
716 f.frequency = dev->ctl_freq;
717 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
718
719 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
720
721 return 0;
722}
723
724/* This is called *without* dev->slock held; please keep it that way */
725static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
726{
727 struct cx231xx_fh *fh = vq->priv_data;
728 struct cx231xx *dev = fh->dev;
729 unsigned long flags = 0;
730 if (in_interrupt())
731 BUG();
732
733 /* We used to wait for the buffer to finish here, but this didn't work
734 because, as we were keeping the state as VIDEOBUF_QUEUED,
735 videobuf_queue_cancel marked it as finished for us.
736 (Also, it could wedge forever if the hardware was misconfigured.)
737
738 This should be safe; by the time we get here, the buffer isn't
739 queued anymore. If we ever start marking the buffers as
740 VIDEOBUF_ACTIVE, it won't be, though.
741 */
742 spin_lock_irqsave(&dev->video_mode.slock, flags);
743 if (dev->video_mode.isoc_ctl.buf == buf)
744 dev->video_mode.isoc_ctl.buf = NULL;
745 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
746
747 videobuf_vmalloc_free(&buf->vb);
748 buf->vb.state = VIDEOBUF_NEEDS_INIT;
749}
750
751static int
752buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
753 enum v4l2_field field)
754{
755 struct cx231xx_fh *fh = vq->priv_data;
756 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
757 struct cx231xx *dev = fh->dev;
758 int rc = 0, urb_init = 0;
759
760 /* The only currently supported format is 16 bits/pixel */
761 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
762
763 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
764 return -EINVAL;
765
766 buf->vb.width = dev->width;
767 buf->vb.height = dev->height;
768 buf->vb.field = field;
769
770 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
771 rc = videobuf_iolock(vq, &buf->vb, NULL);
772 if (rc < 0)
773 goto fail;
774 }
775
776 if (!dev->video_mode.isoc_ctl.num_bufs)
777 urb_init = 1;
778
779 if (urb_init) {
780 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
781 CX231XX_NUM_BUFS, dev->video_mode.max_pkt_size,
782 cx231xx_isoc_copy);
783 if (rc < 0)
784 goto fail;
785 }
786
787 buf->vb.state = VIDEOBUF_PREPARED;
788 return 0;
789
790fail:
791 free_buffer(vq, buf);
792 return rc;
793}
794
795static void
796buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
797{
798 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
799 struct cx231xx_fh *fh = vq->priv_data;
800 struct cx231xx *dev = fh->dev;
801 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
802
803 buf->vb.state = VIDEOBUF_QUEUED;
804 list_add_tail(&buf->vb.queue, &vidq->active);
805
806}
807
808static void buffer_release(struct videobuf_queue *vq,
809 struct videobuf_buffer *vb)
810{
811 struct cx231xx_buffer *buf = container_of(vb, struct cx231xx_buffer, vb);
812 struct cx231xx_fh *fh = vq->priv_data;
813 struct cx231xx *dev = (struct cx231xx *)fh->dev;
814
815 cx231xx_isocdbg("cx231xx: called buffer_release\n");
816
817 free_buffer(vq, buf);
818}
819
820static struct videobuf_queue_ops cx231xx_video_qops = {
821 .buf_setup = buffer_setup,
822 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue,
824 .buf_release = buffer_release,
825};
826
827/********************* v4l2 interface **************************************/
828
829
830void video_mux(struct cx231xx *dev, int index)
831{
832
833 struct v4l2_routing route;
834
835 route.input = INPUT(index)->vmux;
836 route.output = 0;
837 dev->video_input = index;
838 dev->ctl_ainput = INPUT(index)->amux;
839
840 cx231xx_set_video_input_mux(dev,index);
841
842 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_INT_S_VIDEO_ROUTING, &route);
843
844 cx231xx_set_audio_input(dev, dev->ctl_ainput );
845
846 cx231xx_info("video_mux : %d\n", index);
847
848 /* do mode control overrides if required */
849 cx231xx_do_mode_ctrl_overrides(dev);
850}
851
852/* Usage lock check functions */
853static int res_get(struct cx231xx_fh *fh)
854{
855 struct cx231xx *dev = fh->dev;
856 int rc = 0;
857
858 /* This instance already has stream_on */
859 if (fh->stream_on)
860 return rc;
861
862 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
863 if (dev->stream_on)
864 return -EBUSY;
865 dev->stream_on = 1;
866 } else if(fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
867 if (dev->vbi_stream_on)
868 return -EBUSY;
869 dev->vbi_stream_on = 1;
870 } else
871 return -EINVAL;
872
873 fh->stream_on = 1;
874
875 return rc;
876}
877
878static int res_check(struct cx231xx_fh *fh)
879{
880 return (fh->stream_on);
881}
882
883static void res_free(struct cx231xx_fh *fh)
884{
885 struct cx231xx *dev = fh->dev;
886
887 fh->stream_on = 0;
888
889 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 dev->stream_on = 0;
891 if(fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
892 dev->vbi_stream_on = 0;
893}
894
895static int check_dev(struct cx231xx *dev)
896{
897 if (dev->state & DEV_DISCONNECTED) {
898 cx231xx_errdev("v4l2 ioctl: device not present\n");
899 return -ENODEV;
900 }
901
902 if (dev->state & DEV_MISCONFIGURED) {
903 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
904 "close and open it again\n");
905 return -EIO;
906 }
907 return 0;
908}
909
910void get_scale(struct cx231xx *dev,
911 unsigned int width, unsigned int height,
912 unsigned int *hscale, unsigned int *vscale)
913{
914 unsigned int maxw = norm_maxw(dev);
915 unsigned int maxh = norm_maxh(dev);
916
917 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
918 if (*hscale >= 0x4000)
919 *hscale = 0x3fff;
920
921 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
922 if (*vscale >= 0x4000)
923 *vscale = 0x3fff;
924
925 dev->hscale = *hscale;
926 dev->vscale = *vscale;
927
928}
929
930/* ------------------------------------------------------------------
931 IOCTL vidioc handling
932 ------------------------------------------------------------------*/
933
934static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
935 struct v4l2_format *f)
936{
937 struct cx231xx_fh *fh = priv;
938 struct cx231xx *dev = fh->dev;
939
940 mutex_lock(&dev->lock);
941
942 f->fmt.pix.width = dev->width;
943 f->fmt.pix.height = dev->height;
944 f->fmt.pix.pixelformat = dev->format->fourcc;;
945 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
946 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
947 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
948
949 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
950
951 mutex_unlock(&dev->lock);
952 return 0;
953}
954
955static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
956{
957 unsigned int i;
958
959 for (i = 0; i < ARRAY_SIZE(format); i++)
960 if (format[i].fourcc == fourcc)
961 return &format[i];
962
963 return NULL;
964}
965
966static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
967 struct v4l2_format *f)
968{
969 struct cx231xx_fh *fh = priv;
970 struct cx231xx *dev = fh->dev;
971 int width = f->fmt.pix.width;
972 int height = f->fmt.pix.height;
973 unsigned int maxw = norm_maxw(dev);
974 unsigned int maxh = norm_maxh(dev);
975 unsigned int hscale, vscale;
976 struct cx231xx_fmt *fmt;
977
978 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
979 if (!fmt) {
980 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
981 f->fmt.pix.pixelformat);
982 return -EINVAL;
983 }
984
985 /* width must even because of the YUYV format
986 height must be even because of interlacing */
987 height &= 0xfffe;
988 width &= 0xfffe;
989
990 if (unlikely(height < 32))
991 height = 32;
992 if (unlikely(height > maxh))
993 height = maxh;
994 if (unlikely(width < 48))
995 width = 48;
996 if (unlikely(width > maxw))
997 width = maxw;
998
999 get_scale(dev, width, height, &hscale, &vscale);
1000
1001 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1002 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1003
1004 f->fmt.pix.width = width;
1005 f->fmt.pix.height = height;
1006 f->fmt.pix.pixelformat = fmt->fourcc;
1007 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1008 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1009 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1010 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1011
1012 return 0;
1013}
1014
1015static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1016 struct v4l2_format *f)
1017{
1018 struct cx231xx_fh *fh = priv;
1019 struct cx231xx *dev = fh->dev;
1020 int rc;
1021 struct cx231xx_fmt *fmt;
1022
1023 rc = check_dev(dev);
1024 if (rc < 0)
1025 return rc;
1026
1027
1028 mutex_lock(&dev->lock);
1029
1030 vidioc_try_fmt_vid_cap(file, priv, f);
1031
1032 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1033 if (!fmt) {
1034 rc = -EINVAL;
1035 goto out;
1036 }
1037
1038 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1039 cx231xx_errdev("%s queue busy\n", __func__);
1040 rc = -EBUSY;
1041 goto out;
1042 }
1043
1044 if (dev->stream_on && !fh->stream_on) {
1045 cx231xx_errdev("%s device in use by another fh\n", __func__);
1046 rc = -EBUSY;
1047 goto out;
1048 }
1049
1050 /* set new image size */
1051 dev->width = f->fmt.pix.width;
1052 dev->height = f->fmt.pix.height;
1053 dev->format = fmt;
1054 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1055
1056 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_FMT, f);
1057
1058 /* Set the correct alternate setting for this resolution */
1059 cx231xx_resolution_set(dev);
1060
1061out:
1062 mutex_unlock(&dev->lock);
1063 return rc;
1064}
1065
1066static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1067{
1068 struct cx231xx_fh *fh = priv;
1069 struct cx231xx *dev = fh->dev;
1070
1071 *id = dev->norm;
1072 return 0;
1073}
1074
1075static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1076{
1077 struct cx231xx_fh *fh = priv;
1078 struct cx231xx *dev = fh->dev;
1079 struct v4l2_format f;
1080 int rc;
1081
1082 rc = check_dev(dev);
1083 if (rc < 0)
1084 return rc;
1085
1086 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1087
1088 mutex_lock(&dev->lock);
1089 dev->norm = *norm;
1090
1091
1092 /* Adjusts width/height, if needed */
1093 f.fmt.pix.width = dev->width;
1094 f.fmt.pix.height = dev->height;
1095 vidioc_try_fmt_vid_cap(file, priv, &f);
1096
1097 /* set new image size */
1098 dev->width = f.fmt.pix.width;
1099 dev->height = f.fmt.pix.height;
1100 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1101
1102 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_STD, &dev->norm);
1103
1104 mutex_unlock(&dev->lock);
1105
1106 cx231xx_resolution_set(dev);
1107
1108 /* do mode control overrides */
1109 cx231xx_do_mode_ctrl_overrides(dev);
1110
1111 return 0;
1112}
1113
1114static const char *iname[] = {
1115 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1116 [CX231XX_VMUX_SVIDEO] = "S-Video",
1117 [CX231XX_VMUX_TELEVISION] = "Television",
1118 [CX231XX_VMUX_CABLE] = "Cable TV",
1119 [CX231XX_VMUX_DVB] = "DVB",
1120 [CX231XX_VMUX_DEBUG] = "for debug only",
1121};
1122
1123static int vidioc_enum_input(struct file *file, void *priv,
1124 struct v4l2_input *i)
1125{
1126 struct cx231xx_fh *fh = priv;
1127 struct cx231xx *dev = fh->dev;
1128 unsigned int n;
1129
1130 n = i->index;
1131 if (n >= MAX_CX231XX_INPUT)
1132 return -EINVAL;
1133 if (0 == INPUT(n)->type)
1134 return -EINVAL;
1135
1136 i->index = n;
1137 i->type = V4L2_INPUT_TYPE_CAMERA;
1138
1139 strcpy(i->name, iname[INPUT(n)->type]);
1140
1141 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1142 (CX231XX_VMUX_CABLE == INPUT(n)->type))
1143 i->type = V4L2_INPUT_TYPE_TUNER;
1144
1145 i->std = dev->vdev->tvnorms;
1146
1147 return 0;
1148}
1149
1150static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1151{
1152 struct cx231xx_fh *fh = priv;
1153 struct cx231xx *dev = fh->dev;
1154
1155 *i = dev->video_input;
1156
1157 return 0;
1158}
1159
1160static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1161{
1162 struct cx231xx_fh *fh = priv;
1163 struct cx231xx *dev = fh->dev;
1164 int rc;
1165
1166 rc = check_dev(dev);
1167 if (rc < 0)
1168 return rc;
1169
1170 if (i >= MAX_CX231XX_INPUT)
1171 return -EINVAL;
1172 if (0 == INPUT(i)->type)
1173 return -EINVAL;
1174
1175 mutex_lock(&dev->lock);
1176
1177 video_mux(dev, i);
1178
1179 mutex_unlock(&dev->lock);
1180 return 0;
1181}
1182
1183static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1184{
1185 struct cx231xx_fh *fh = priv;
1186 struct cx231xx *dev = fh->dev;
1187
1188 switch (a->index) {
1189 case CX231XX_AMUX_VIDEO:
1190 strcpy(a->name, "Television");
1191 break;
1192 case CX231XX_AMUX_LINE_IN:
1193 strcpy(a->name, "Line In");
1194 break;
1195 default:
1196 return -EINVAL;
1197 }
1198
1199 a->index = dev->ctl_ainput;
1200 a->capability = V4L2_AUDCAP_STEREO;
1201
1202 return 0;
1203}
1204
1205static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1206{
1207 struct cx231xx_fh *fh = priv;
1208 struct cx231xx *dev = fh->dev;
1209 int status = 0;
1210
1211
1212 /* Doesn't allow manual routing */
1213 if (a->index != dev->ctl_ainput)
1214 return -EINVAL;
1215
1216 dev->ctl_ainput = INPUT(a->index)->amux;
1217 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1218
1219 return status;
1220}
1221
1222static int vidioc_queryctrl(struct file *file, void *priv,
1223 struct v4l2_queryctrl *qc)
1224{
1225 struct cx231xx_fh *fh = priv;
1226 struct cx231xx *dev = fh->dev;
1227 int id = qc->id;
1228 int i;
1229 int rc;
1230
1231 rc = check_dev(dev);
1232 if (rc < 0)
1233 return rc;
1234
1235 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1236 if (unlikely(qc->id == 0))
1237 return -EINVAL;
1238
1239 memset(qc, 0, sizeof(*qc));
1240
1241 qc->id = id;
1242
1243 if (qc->id < V4L2_CID_BASE ||
1244 qc->id >= V4L2_CID_LASTP1)
1245 return -EINVAL;
1246
1247 for (i = 0; i < CX231XX_CTLS; i++)
1248 if (cx231xx_ctls[i].v.id == qc->id)
1249 break;
1250
1251 if (i == CX231XX_CTLS) {
1252 *qc = no_ctl;
1253 return 0;
1254 }
1255 *qc = cx231xx_ctls[i].v;
1256
1257 mutex_lock(&dev->lock);
1258 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_QUERYCTRL, qc);
1259 mutex_unlock(&dev->lock);
1260
1261 if (qc->type)
1262 return 0;
1263 else
1264 return -EINVAL;
1265}
1266
1267static int vidioc_g_ctrl(struct file *file, void *priv,
1268 struct v4l2_control *ctrl)
1269{
1270 struct cx231xx_fh *fh = priv;
1271 struct cx231xx *dev = fh->dev;
1272 int rc;
1273
1274 rc = check_dev(dev);
1275 if (rc < 0)
1276 return rc;
1277
1278 mutex_lock(&dev->lock);
1279
1280 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_CTRL, ctrl);
1281
1282 mutex_unlock(&dev->lock);
1283 return rc;
1284}
1285
1286static int vidioc_s_ctrl(struct file *file, void *priv,
1287 struct v4l2_control *ctrl)
1288{
1289 struct cx231xx_fh *fh = priv;
1290 struct cx231xx *dev = fh->dev;
1291 int rc;
1292
1293 rc = check_dev(dev);
1294 if (rc < 0)
1295 return rc;
1296
1297 mutex_lock(&dev->lock);
1298
1299 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_CTRL, ctrl);
1300
1301 mutex_unlock(&dev->lock);
1302 return rc;
1303}
1304
1305static int vidioc_g_tuner(struct file *file, void *priv,
1306 struct v4l2_tuner *t)
1307{
1308 struct cx231xx_fh *fh = priv;
1309 struct cx231xx *dev = fh->dev;
1310 int rc;
1311
1312 rc = check_dev(dev);
1313 if (rc < 0)
1314 return rc;
1315
1316 if (0 != t->index)
1317 return -EINVAL;
1318
1319 strcpy(t->name, "Tuner");
1320
1321 t->type = V4L2_TUNER_ANALOG_TV;
1322 t->capability = V4L2_TUNER_CAP_NORM;
1323 t->rangehigh = 0xffffffffUL;
1324 t->signal = 0xffff ; /* LOCKED */
1325
1326 return 0;
1327}
1328
1329static int vidioc_s_tuner(struct file *file, void *priv,
1330 struct v4l2_tuner *t)
1331{
1332 struct cx231xx_fh *fh = priv;
1333 struct cx231xx *dev = fh->dev;
1334 int rc;
1335
1336 rc = check_dev(dev);
1337 if (rc < 0)
1338 return rc;
1339
1340 if (0 != t->index)
1341 return -EINVAL;
1342#if 0
1343 mutex_lock(&dev->lock);
1344
1345 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1346
1347 mutex_unlock(&dev->lock);
1348#endif
1349 return 0;
1350}
1351
1352static int vidioc_g_frequency(struct file *file, void *priv,
1353 struct v4l2_frequency *f)
1354{
1355 struct cx231xx_fh *fh = priv;
1356 struct cx231xx *dev = fh->dev;
1357
1358 mutex_lock(&dev->lock);
1359 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1360 f->frequency = dev->ctl_freq;
1361
1362 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1363
1364 mutex_unlock(&dev->lock);
1365
1366 return 0;
1367}
1368
1369static int vidioc_s_frequency(struct file *file, void *priv,
1370 struct v4l2_frequency *f)
1371{
1372 struct cx231xx_fh *fh = priv;
1373 struct cx231xx *dev = fh->dev;
1374 int rc;
1375
1376 rc = check_dev(dev);
1377 if (rc < 0)
1378 return rc;
1379
1380 if (0 != f->tuner)
1381 return -EINVAL;
1382
1383 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1384 return -EINVAL;
1385 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1386 return -EINVAL;
1387
1388 /* set pre channel change settings in DIF first */
1389 rc = cx231xx_tuner_pre_channel_change(dev);
1390
1391 mutex_lock(&dev->lock);
1392
1393 dev->ctl_freq = f->frequency;
1394
1395 if(dev->tuner_type == TUNER_XC5000) {
1396 if( dev->cx231xx_set_analog_freq != NULL ) {
1397 dev->cx231xx_set_analog_freq(dev, f->frequency );
1398 }
1399 } else {
1400 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1401 }
1402
1403 mutex_unlock(&dev->lock);
1404
1405 /* set post channel change settings in DIF first */
1406 rc = cx231xx_tuner_post_channel_change(dev);
1407
1408 cx231xx_info("Set New FREQUENCY to %d\n",f->frequency);
1409
1410 return rc;
1411}
1412
1413#ifdef CONFIG_VIDEO_ADV_DEBUG
1414
1415
1416/*
1417 -R, --list-registers=type=<host/i2cdrv/i2caddr>,chip=<chip>[,min=<addr>,max=<addr>]
1418 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1419 -r, --set-register=type=<host/i2cdrv/i2caddr>,chip=<chip>,reg=<addr>,val=<val>
1420 set the register [VIDIOC_DBG_S_REGISTER]
1421
1422 if type == host, then <chip> is the hosts chip ID (default 0)
1423 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1424 if type == i2caddr, then <chip> is the 7-bit I2C address
1425*/
1426
1427
1428static int vidioc_g_register(struct file *file, void *priv,
1429 struct v4l2_dbg_register *reg)
1430{
1431 struct cx231xx_fh *fh = priv;
1432 struct cx231xx *dev = fh->dev;
1433 int ret = 0;
1434 u8 value[4] ={0,0,0,0};
1435 u32 data = 0;
1436
1437 switch (reg->match.type) {
1438 case V4L2_CHIP_MATCH_HOST:
1439 switch(reg->match.addr) {
1440 case 0: /* Cx231xx - internal registers */
1441 ret = cx231xx_read_ctrl_reg(dev,VRT_GET_REGISTER, (u16) reg->reg, value, 4);
1442 reg->val = value[0] | value[1] << 8 | value[2] << 16 | value[3] << 24;
1443 break;
1444 case 1: /* Colibri - read byte */
1445 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, &data, 1);
1446 reg->val = le32_to_cpu(data & 0xff);
1447 break;
1448 case 14: /* Colibri - read dword */
1449 ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, &data, 4);
1450 reg->val = le32_to_cpu(data);
1451 break;
1452 case 2: /* Hammerhead - read byte */
1453 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, &data, 1);
1454 reg->val = le32_to_cpu(data & 0xff);
1455 break;
1456 case 24: /* Hammerhead - read dword */
1457 ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, &data, 4);
1458 reg->val = le32_to_cpu(data);
1459 break;
1460 case 3: /* flatiron - read byte */
1461 ret = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, &data, 1);
1462 reg->val = le32_to_cpu(data & 0xff);
1463 break;
1464 case 34: /* flatiron - read dword */
1465 ret = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, &data, 4);
1466 reg->val = le32_to_cpu(data);
1467 break;
1468 }
1469 return ret < 0?ret:0;
1470
1471 case V4L2_CHIP_MATCH_I2C_DRIVER:
1472 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1473 return 0;
1474 case V4L2_CHIP_MATCH_I2C_ADDR:
1475 /* Not supported yet */
1476 return -EINVAL;
1477 default:
1478 if (!v4l2_chip_match_host(&reg->match))
1479 return -EINVAL;
1480 }
1481
1482
1483 mutex_lock(&dev->lock);
1484
1485 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1486
1487 mutex_unlock(&dev->lock);
1488
1489 return ret;
1490}
1491
1492static int vidioc_s_register(struct file *file, void *priv,
1493 struct v4l2_dbg_register *reg)
1494{
1495 struct cx231xx_fh *fh = priv;
1496 struct cx231xx *dev = fh->dev;
1497 int ret = 0;
1498 __le64 buf;
1499 u32 value;
1500 u8 data[4] ={0,0,0,0};
1501
1502 buf = cpu_to_le64(reg->val);
1503
1504 switch (reg->match.type) {
1505 case V4L2_CHIP_MATCH_HOST:
1506 {
1507 value = (u32) buf & 0xffffffff;
1508
1509 switch(reg->match.addr) {
1510 case 0: /* cx231xx internal registers */
1511 data[0]=(u8)value;
1512 data[1]=(u8)(value>>8);
1513 data[2]=(u8)(value>>16);
1514 data[3]=(u8)(value>>24);
1515 ret = cx231xx_write_ctrl_reg(dev,VRT_SET_REGISTER, (u16) reg->reg, data, 4);
1516 break;
1517 case 1: /* Colibri - read byte */
1518 ret = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, value, 1);
1519 break;
1520 case 14: /* Colibri - read dword */
1521 ret = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS, (u16) reg->reg, 2, value, 4);
1522 break;
1523 case 2: /* Hammerhead - read byte */
1524 ret = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, value, 1);
1525 break;
1526 case 24: /* Hammerhead - read dword */
1527 ret = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, (u16) reg->reg, 2, value, 4);
1528 break;
1529 case 3: /* flatiron - read byte */
1530 ret = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, value, 1);
1531 break;
1532 case 34: /* flatiron - read dword */
1533 ret = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS, (u16) reg->reg, 1, value, 4);
1534 break;
1535 }
1536 }
1537 return ret < 0?ret:0;
1538
1539 default:
1540 break;
1541 }
1542
1543 mutex_lock(&dev->lock);
1544
1545 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_S_REGISTER, reg);
1546
1547 mutex_unlock(&dev->lock);
1548
1549 return ret;
1550}
1551#endif
1552
1553
1554static int vidioc_cropcap(struct file *file, void *priv,
1555 struct v4l2_cropcap *cc)
1556{
1557 struct cx231xx_fh *fh = priv;
1558 struct cx231xx *dev = fh->dev;
1559
1560 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1561 return -EINVAL;
1562
1563 cc->bounds.left = 0;
1564 cc->bounds.top = 0;
1565 cc->bounds.width = dev->width;
1566 cc->bounds.height = dev->height;
1567 cc->defrect = cc->bounds;
1568 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1569 cc->pixelaspect.denominator = 59;
1570
1571 return 0;
1572}
1573
1574static int vidioc_streamon(struct file *file, void *priv,
1575 enum v4l2_buf_type type)
1576{
1577 struct cx231xx_fh *fh = priv;
1578 struct cx231xx *dev = fh->dev;
1579 int rc;
1580
1581 rc = check_dev(dev);
1582 if (rc < 0)
1583 return rc;
1584
1585 mutex_lock(&dev->lock);
1586 rc = res_get(fh);
1587
1588 if (likely(rc >= 0))
1589 rc = videobuf_streamon(&fh->vb_vidq);
1590
1591 mutex_unlock(&dev->lock);
1592
1593 return rc;
1594}
1595
1596static int vidioc_streamoff(struct file *file, void *priv,
1597 enum v4l2_buf_type type)
1598{
1599 struct cx231xx_fh *fh = priv;
1600 struct cx231xx *dev = fh->dev;
1601 int rc;
1602
1603 rc = check_dev(dev);
1604 if (rc < 0)
1605 return rc;
1606
1607 if ( (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1608 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) )
1609 return -EINVAL;
1610 if (type != fh->type)
1611 return -EINVAL;
1612
1613 mutex_lock(&dev->lock);
1614
1615 videobuf_streamoff(&fh->vb_vidq);
1616 res_free(fh);
1617
1618 mutex_unlock(&dev->lock);
1619
1620 return 0;
1621}
1622
1623static int vidioc_querycap(struct file *file, void *priv,
1624 struct v4l2_capability *cap)
1625{
1626 struct cx231xx_fh *fh = priv;
1627 struct cx231xx *dev = fh->dev;
1628
1629 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1630 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1631 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1632
1633 cap->version = CX231XX_VERSION_CODE;
1634
1635 cap->capabilities =
1636 V4L2_CAP_VBI_CAPTURE |
1637#if 0
1638 V4L2_CAP_SLICED_VBI_CAPTURE |
1639#endif
1640 V4L2_CAP_VIDEO_CAPTURE |
1641 V4L2_CAP_AUDIO |
1642 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1643
1644 if (dev->tuner_type != TUNER_ABSENT)
1645 cap->capabilities |= V4L2_CAP_TUNER;
1646
1647 return 0;
1648}
1649
1650static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1651 struct v4l2_fmtdesc *f)
1652{
1653 if (unlikely(f->index >= ARRAY_SIZE(format)))
1654 return -EINVAL;
1655
1656 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1657 f->pixelformat = format[f->index].fourcc;
1658
1659 return 0;
1660}
1661
1662/* Sliced VBI ioctls */
1663static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1664 struct v4l2_format *f)
1665{
1666 struct cx231xx_fh *fh = priv;
1667 struct cx231xx *dev = fh->dev;
1668 int rc;
1669
1670 rc = check_dev(dev);
1671 if (rc < 0)
1672 return rc;
1673
1674 mutex_lock(&dev->lock);
1675
1676 f->fmt.sliced.service_set = 0;
1677
1678 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1679
1680 if (f->fmt.sliced.service_set == 0)
1681 rc = -EINVAL;
1682
1683 mutex_unlock(&dev->lock);
1684 return rc;
1685}
1686
1687static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1688 struct v4l2_format *f)
1689{
1690 struct cx231xx_fh *fh = priv;
1691 struct cx231xx *dev = fh->dev;
1692 int rc;
1693
1694 rc = check_dev(dev);
1695 if (rc < 0)
1696 return rc;
1697
1698 mutex_lock(&dev->lock);
1699 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1700 mutex_unlock(&dev->lock);
1701
1702 if (f->fmt.sliced.service_set == 0)
1703 return -EINVAL;
1704
1705 return 0;
1706}
1707
1708
1709/* RAW VBI ioctls */
1710
1711static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1712 struct v4l2_format *f)
1713{
1714 struct cx231xx_fh *fh = priv;
1715 struct cx231xx *dev = fh->dev;
1716
1717 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1718 35468950:28636363;
1719 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1720 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1721 f->fmt.vbi.offset = 64 * 4;
1722 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1723 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1724 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1725 PAL_VBI_LINES : NTSC_VBI_LINES;
1726 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1727 PAL_VBI_START_LINE+312 : NTSC_VBI_START_LINE + 263;
1728 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1729
1730 return 0;
1731
1732}
1733
1734static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1735 struct v4l2_format *f)
1736{
1737 struct cx231xx_fh *fh = priv;
1738 struct cx231xx *dev = fh->dev;
1739
1740 if (dev->vbi_stream_on && !fh->stream_on) {
1741 cx231xx_errdev("%s device in use by another fh\n", __func__);
1742 return -EBUSY;
1743 }
1744
1745 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1746 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1747 35468950:28636363;
1748 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1749 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1750 f->fmt.vbi.offset = 244;
1751 f->fmt.vbi.flags = 0;
1752 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1753 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1754 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1755 PAL_VBI_LINES : NTSC_VBI_LINES;
1756 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1757 PAL_VBI_START_LINE+312 : NTSC_VBI_START_LINE + 263;
1758 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1759
1760 return 0;
1761
1762}
1763
1764static int vidioc_reqbufs(struct file *file, void *priv,
1765 struct v4l2_requestbuffers *rb)
1766{
1767 struct cx231xx_fh *fh = priv;
1768 struct cx231xx *dev = fh->dev;
1769 int rc;
1770
1771 rc = check_dev(dev);
1772 if (rc < 0)
1773 return rc;
1774
1775 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1776}
1777
1778static int vidioc_querybuf(struct file *file, void *priv,
1779 struct v4l2_buffer *b)
1780{
1781 struct cx231xx_fh *fh = priv;
1782 struct cx231xx *dev = fh->dev;
1783 int rc;
1784
1785 rc = check_dev(dev);
1786 if (rc < 0)
1787 return rc;
1788
1789 return (videobuf_querybuf(&fh->vb_vidq, b));
1790}
1791
1792static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1793{
1794 struct cx231xx_fh *fh = priv;
1795 struct cx231xx *dev = fh->dev;
1796 int rc;
1797
1798 rc = check_dev(dev);
1799 if (rc < 0)
1800 return rc;
1801
1802 return (videobuf_qbuf(&fh->vb_vidq, b));
1803}
1804
1805static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1806{
1807 struct cx231xx_fh *fh = priv;
1808 struct cx231xx *dev = fh->dev;
1809 int rc;
1810
1811 rc = check_dev(dev);
1812 if (rc < 0)
1813 return rc;
1814
1815 return (videobuf_dqbuf(&fh->vb_vidq, b,
1816 file->f_flags & O_NONBLOCK));
1817}
1818
1819#ifdef CONFIG_VIDEO_V4L1_COMPAT
1820static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1821{
1822 struct cx231xx_fh *fh = priv;
1823
1824 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1825}
1826#endif
1827
1828
1829/* ----------------------------------------------------------- */
1830/* RADIO ESPECIFIC IOCTLS */
1831/* ----------------------------------------------------------- */
1832
1833static int radio_querycap(struct file *file, void *priv,
1834 struct v4l2_capability *cap)
1835{
1836 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1837
1838 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1839 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1840 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1841
1842 cap->version = CX231XX_VERSION_CODE;
1843 cap->capabilities = V4L2_CAP_TUNER;
1844 return 0;
1845}
1846
1847static int radio_g_tuner(struct file *file, void *priv,
1848 struct v4l2_tuner *t)
1849{
1850 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1851
1852 if (unlikely(t->index > 0))
1853 return -EINVAL;
1854
1855 strcpy(t->name, "Radio");
1856 t->type = V4L2_TUNER_RADIO;
1857
1858 mutex_lock(&dev->lock);
1859 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
1860 mutex_unlock(&dev->lock);
1861
1862 return 0;
1863}
1864
1865static int radio_enum_input(struct file *file, void *priv,
1866 struct v4l2_input *i)
1867{
1868 if (i->index != 0)
1869 return -EINVAL;
1870 strcpy(i->name, "Radio");
1871 i->type = V4L2_INPUT_TYPE_TUNER;
1872
1873 return 0;
1874}
1875
1876static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1877{
1878 if (unlikely(a->index))
1879 return -EINVAL;
1880
1881 strcpy(a->name, "Radio");
1882 return 0;
1883}
1884
1885static int radio_s_tuner(struct file *file, void *priv,
1886 struct v4l2_tuner *t)
1887{
1888 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1889
1890 if (0 != t->index)
1891 return -EINVAL;
1892
1893 mutex_lock(&dev->lock);
1894 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1895 mutex_unlock(&dev->lock);
1896
1897 return 0;
1898}
1899
1900static int radio_s_audio(struct file *file, void *fh,
1901 struct v4l2_audio *a)
1902{
1903 return 0;
1904}
1905
1906static int radio_s_input(struct file *file, void *fh, unsigned int i)
1907{
1908 return 0;
1909}
1910
1911static int radio_queryctrl(struct file *file, void *priv,
1912 struct v4l2_queryctrl *c)
1913{
1914 int i;
1915
1916 if (c->id < V4L2_CID_BASE ||
1917 c->id >= V4L2_CID_LASTP1)
1918 return -EINVAL;
1919 if (c->id == V4L2_CID_AUDIO_MUTE) {
1920 for (i = 0; i < CX231XX_CTLS; i++)
1921 if (cx231xx_ctls[i].v.id == c->id)
1922 break;
1923 *c = cx231xx_ctls[i].v;
1924 } else
1925 *c = no_ctl;
1926 return 0;
1927}
1928
1929/*
1930 * cx231xx_v4l2_open()
1931 * inits the device and starts isoc transfer
1932 */
1933static int cx231xx_v4l2_open(struct file *filp)
1934{
1935 int minor = video_devdata(filp)->minor;
1936 int errCode = 0, radio = 0;
1937 struct cx231xx *dev = NULL;
1938 struct cx231xx_fh *fh;
1939 enum v4l2_buf_type fh_type = 0;
1940
1941 dev = cx231xx_get_device(minor, &fh_type, &radio);
1942 if (NULL == dev)
1943 return -ENODEV;
1944
1945 mutex_lock(&dev->lock);
1946
1947 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
1948 minor, v4l2_type_names[fh_type], dev->users);
1949
1950#if 0
1951 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1952 if (errCode < 0) {
1953 cx231xx_errdev("Device locked on digital mode. Can't open analog\n");
1954 mutex_unlock(&dev->lock);
1955 return -EBUSY;
1956 }
1957#endif
1958
1959 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1960 if (!fh) {
1961 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1962 mutex_unlock(&dev->lock);
1963 return -ENOMEM;
1964 }
1965 fh->dev = dev;
1966 fh->radio = radio;
1967 fh->type = fh_type;
1968 filp->private_data = fh;
1969
1970 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1971 dev->width = norm_maxw(dev);
1972 dev->height = norm_maxh(dev);
1973 dev->hscale = 0;
1974 dev->vscale = 0;
1975
1976
1977 /* Power up in Analog TV mode */
1978 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1979
1980#if 0
1981 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1982#endif
1983 cx231xx_resolution_set(dev);
1984
1985 /* set video alternate setting */
1986 cx231xx_set_video_alternate(dev);
1987
1988 /* Needed, since GPIO might have disabled power of
1989 some i2c device */
1990 cx231xx_config_i2c(dev);
1991
1992 /* device needs to be initialized before isoc transfer */
1993 dev->video_input = dev->video_input > 2 ? 2: dev->video_input;
1994 video_mux(dev, dev->video_input );
1995
1996 }
1997 if (fh->radio) {
1998 cx231xx_videodbg("video_open: setting radio device\n");
1999
2000 /* cx231xx_start_radio(dev); */
2001
2002 cx231xx_i2c_call_clients(&dev->i2c_bus[1], AUDC_SET_RADIO, NULL);
2003 }
2004
2005 dev->users++;
2006
2007 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2008 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2009 NULL, &dev->video_mode.slock, fh->type, V4L2_FIELD_INTERLACED, /* V4L2_FIELD_SEQ_TB, */
2010 sizeof(struct cx231xx_buffer), fh);
2011 }
2012
2013 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2014
2015 /* Set the required alternate setting VBI interface works in Bulk mode only */
2016 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2017
2018 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2019 NULL, &dev->vbi_mode.slock, fh->type, V4L2_FIELD_SEQ_TB, /* V4L2_FIELD_INTERLACED, */
2020 sizeof(struct cx231xx_buffer), fh);
2021 }
2022
2023 mutex_unlock(&dev->lock);
2024
2025 return errCode;
2026}
2027
2028/*
2029 * cx231xx_realease_resources()
2030 * unregisters the v4l2,i2c and usb devices
2031 * called when the device gets disconected or at module unload
2032*/
2033void cx231xx_release_analog_resources(struct cx231xx *dev)
2034{
2035
2036 /*FIXME: I2C IR should be disconnected */
2037
2038 if (dev->radio_dev) {
2039 if (-1 != dev->radio_dev->minor)
2040 video_unregister_device(dev->radio_dev);
2041 else
2042 video_device_release(dev->radio_dev);
2043 dev->radio_dev = NULL;
2044 }
2045 if (dev->vbi_dev) {
2046 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
2047 dev->vbi_dev->num);
2048 if (-1 != dev->vbi_dev->minor)
2049 video_unregister_device(dev->vbi_dev);
2050 else
2051 video_device_release(dev->vbi_dev);
2052 dev->vbi_dev = NULL;
2053 }
2054 if (dev->vdev) {
2055 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
2056 dev->vdev->num);
2057 if (-1 != dev->vdev->minor)
2058 video_unregister_device(dev->vdev);
2059 else
2060 video_device_release(dev->vdev);
2061 dev->vdev = NULL;
2062 }
2063}
2064
2065/*
2066 * cx231xx_v4l2_close()
2067 * stops streaming and deallocates all resources allocated by the v4l2
2068 * calls and ioctls
2069 */
2070static int cx231xx_v4l2_close(struct file *filp)
2071{
2072 struct cx231xx_fh *fh = filp->private_data;
2073 struct cx231xx *dev = fh->dev;
2074
2075 cx231xx_videodbg("users=%d\n", dev->users);
2076
2077 mutex_lock(&dev->lock);
2078
2079 if (res_check(fh))
2080 res_free(fh);
2081
2082 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2083 videobuf_stop(&fh->vb_vidq);
2084 videobuf_mmap_free(&fh->vb_vidq);
2085
2086 /* the device is already disconnect,
2087 free the remaining resources */
2088 if (dev->state & DEV_DISCONNECTED) {
2089 cx231xx_release_resources(dev);
2090 mutex_unlock(&dev->lock);
2091 kfree(dev);
2092 return 0;
2093 }
2094
2095 /* do this before setting alternate! */
2096 cx231xx_uninit_vbi_isoc(dev);
2097
2098 /* set alternate 0 */
2099 if( !dev->vbi_or_sliced_cc_mode) {
2100 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2101 } else {
2102 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2103 }
2104
2105 kfree(fh);
2106 dev->users--;
2107 wake_up_interruptible_nr(&dev->open, 1);
2108 mutex_unlock(&dev->lock);
2109 return 0;
2110 }
2111
2112 if (dev->users == 1) {
2113 videobuf_stop(&fh->vb_vidq);
2114 videobuf_mmap_free(&fh->vb_vidq);
2115
2116 /* the device is already disconnect,
2117 free the remaining resources */
2118 if (dev->state & DEV_DISCONNECTED) {
2119 cx231xx_release_resources(dev);
2120 mutex_unlock(&dev->lock);
2121 kfree(dev);
2122 return 0;
2123 }
2124
2125 /* Save some power by putting tuner to sleep */
2126 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_STANDBY, NULL);
2127
2128 /* do this before setting alternate! */
2129 cx231xx_uninit_isoc(dev);
2130 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2131
2132 /* set alternate 0 */
2133 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2134 }
2135 kfree(fh);
2136 dev->users--;
2137 wake_up_interruptible_nr(&dev->open, 1);
2138 mutex_unlock(&dev->lock);
2139 return 0;
2140}
2141
2142/*
2143 * cx231xx_v4l2_read()
2144 * will allocate buffers when called for the first time
2145 */
2146static ssize_t
2147cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2148 loff_t *pos)
2149{
2150 struct cx231xx_fh *fh = filp->private_data;
2151 struct cx231xx *dev = fh->dev;
2152 int rc;
2153
2154 rc = check_dev(dev);
2155 if (rc < 0)
2156 return rc;
2157
2158 if ( (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2159 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) ) {
2160 mutex_lock(&dev->lock);
2161 rc = res_get(fh);
2162 mutex_unlock(&dev->lock);
2163
2164 if (unlikely(rc < 0))
2165 return rc;
2166
2167 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2168 filp->f_flags & O_NONBLOCK);
2169 }
2170 return 0;
2171}
2172
2173/*
2174 * cx231xx_v4l2_poll()
2175 * will allocate buffers when called for the first time
2176 */
2177static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2178{
2179 struct cx231xx_fh *fh = filp->private_data;
2180 struct cx231xx *dev = fh->dev;
2181 int rc;
2182
2183 rc = check_dev(dev);
2184 if (rc < 0)
2185 return rc;
2186
2187 mutex_lock(&dev->lock);
2188 rc = res_get(fh);
2189 mutex_unlock(&dev->lock);
2190
2191 if (unlikely(rc < 0))
2192 return POLLERR;
2193
2194 if ( (V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2195 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) )
2196 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2197 else
2198 return POLLERR;
2199}
2200
2201/*
2202 * cx231xx_v4l2_mmap()
2203 */
2204static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2205{
2206 struct cx231xx_fh *fh = filp->private_data;
2207 struct cx231xx *dev = fh->dev;
2208 int rc;
2209
2210 rc = check_dev(dev);
2211 if (rc < 0)
2212 return rc;
2213
2214 mutex_lock(&dev->lock);
2215 rc = res_get(fh);
2216 mutex_unlock(&dev->lock);
2217
2218 if (unlikely(rc < 0))
2219 return rc;
2220
2221 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2222
2223 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2224 (unsigned long)vma->vm_start,
2225 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2226 rc);
2227
2228 return rc;
2229}
2230
2231static const struct v4l2_file_operations cx231xx_v4l_fops = {
2232 .owner = THIS_MODULE,
2233 .open = cx231xx_v4l2_open,
2234 .release = cx231xx_v4l2_close,
2235 .read = cx231xx_v4l2_read,
2236 .poll = cx231xx_v4l2_poll,
2237 .mmap = cx231xx_v4l2_mmap,
2238 .ioctl = video_ioctl2,
2239};
2240
2241static const struct v4l2_ioctl_ops video_ioctl_ops = {
2242 .vidioc_querycap = vidioc_querycap,
2243 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2244 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2245 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2246 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2247 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2248 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2249 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2250 .vidioc_g_audio = vidioc_g_audio,
2251 .vidioc_s_audio = vidioc_s_audio,
2252 .vidioc_cropcap = vidioc_cropcap,
2253 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2254 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2255 .vidioc_reqbufs = vidioc_reqbufs,
2256 .vidioc_querybuf = vidioc_querybuf,
2257 .vidioc_qbuf = vidioc_qbuf,
2258 .vidioc_dqbuf = vidioc_dqbuf,
2259 .vidioc_s_std = vidioc_s_std,
2260 .vidioc_g_std = vidioc_g_std,
2261 .vidioc_enum_input = vidioc_enum_input,
2262 .vidioc_g_input = vidioc_g_input,
2263 .vidioc_s_input = vidioc_s_input,
2264 .vidioc_queryctrl = vidioc_queryctrl,
2265 .vidioc_g_ctrl = vidioc_g_ctrl,
2266 .vidioc_s_ctrl = vidioc_s_ctrl,
2267 .vidioc_streamon = vidioc_streamon,
2268 .vidioc_streamoff = vidioc_streamoff,
2269 .vidioc_g_tuner = vidioc_g_tuner,
2270 .vidioc_s_tuner = vidioc_s_tuner,
2271 .vidioc_g_frequency = vidioc_g_frequency,
2272 .vidioc_s_frequency = vidioc_s_frequency,
2273#ifdef CONFIG_VIDEO_ADV_DEBUG
2274 .vidioc_g_register = vidioc_g_register,
2275 .vidioc_s_register = vidioc_s_register,
2276#endif
2277#ifdef CONFIG_VIDEO_V4L1_COMPAT
2278 .vidiocgmbuf = vidiocgmbuf,
2279#endif
2280};
2281
2282static struct video_device cx231xx_vbi_template;
2283
2284static const struct video_device cx231xx_video_template = {
2285 .fops = &cx231xx_v4l_fops,
2286 .release = video_device_release,
2287 .ioctl_ops = &video_ioctl_ops,
2288 .minor = -1,
2289 .tvnorms = V4L2_STD_ALL,
2290 .current_norm = V4L2_STD_PAL,
2291};
2292
2293static const struct v4l2_file_operations radio_fops = {
2294 .owner = THIS_MODULE,
2295 .open = cx231xx_v4l2_open,
2296 .release = cx231xx_v4l2_close,
2297 .ioctl = video_ioctl2,
2298};
2299
2300static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2301 .vidioc_querycap = radio_querycap,
2302 .vidioc_g_tuner = radio_g_tuner,
2303 .vidioc_enum_input = radio_enum_input,
2304 .vidioc_g_audio = radio_g_audio,
2305 .vidioc_s_tuner = radio_s_tuner,
2306 .vidioc_s_audio = radio_s_audio,
2307 .vidioc_s_input = radio_s_input,
2308 .vidioc_queryctrl = radio_queryctrl,
2309 .vidioc_g_ctrl = vidioc_g_ctrl,
2310 .vidioc_s_ctrl = vidioc_s_ctrl,
2311 .vidioc_g_frequency = vidioc_g_frequency,
2312 .vidioc_s_frequency = vidioc_s_frequency,
2313#ifdef CONFIG_VIDEO_ADV_DEBUG
2314 .vidioc_g_register = vidioc_g_register,
2315 .vidioc_s_register = vidioc_s_register,
2316#endif
2317};
2318
2319static struct video_device cx231xx_radio_template = {
2320 .name = "cx231xx-radio",
2321 .fops = &radio_fops,
2322 .ioctl_ops = &radio_ioctl_ops,
2323 .minor = -1,
2324};
2325
2326/******************************** usb interface ******************************/
2327
2328
2329static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2330 const struct video_device *template,
2331 const char *type_name)
2332{
2333 struct video_device *vfd;
2334
2335 vfd = video_device_alloc();
2336 if (NULL == vfd)
2337 return NULL;
2338 *vfd = *template;
2339 vfd->minor = -1;
2340 vfd->parent = &dev->udev->dev;
2341 vfd->release = video_device_release;
2342 vfd->debug = video_debug;
2343
2344 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2345 dev->name, type_name);
2346
2347 return vfd;
2348}
2349
2350int cx231xx_register_analog_devices(struct cx231xx *dev)
2351{
2352 int ret;
2353
2354 cx231xx_info("%s()\n", __func__);
2355
2356 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2357 dev->name,
2358 (CX231XX_VERSION_CODE >> 16) & 0xff,
2359 (CX231XX_VERSION_CODE >> 8) & 0xff, CX231XX_VERSION_CODE & 0xff);
2360
2361 /* set default norm */
2362 /*dev->norm = cx231xx_video_template.current_norm;*/
2363 dev->width = norm_maxw(dev);
2364 dev->height = norm_maxh(dev);
2365 dev->interlaced = 0;
2366 dev->hscale = 0;
2367 dev->vscale = 0;
2368
2369 /* Analog specific initialization */
2370 dev->format = &format[0];
2371 /* video_mux(dev, dev->video_input); */
2372
2373 /* Audio defaults */
2374 dev->mute = 1;
2375 dev->volume = 0x1f;
2376
2377 /* enable vbi capturing */
2378 /* write code here... */
2379
2380 /* allocate and fill video video_device struct */
2381 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2382 if (!dev->vdev) {
2383 cx231xx_errdev("cannot allocate video_device.\n");
2384 return -ENODEV;
2385 }
2386
2387 /* register v4l2 video video_device */
2388 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2389 video_nr[dev->devno]);
2390 if (ret) {
2391 cx231xx_errdev("unable to register video device (error=%i).\n", ret);
2392 return ret;
2393 }
2394
2395 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2396 dev->name, dev->vdev->num);
2397
2398 /* Initialize VBI template */
2399 memcpy( &cx231xx_vbi_template, &cx231xx_video_template,
2400 sizeof(cx231xx_vbi_template) );
2401 strcpy(cx231xx_vbi_template.name,"cx231xx-vbi");
2402
2403
2404 /* Allocate and fill vbi video_device struct */
2405 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2406
2407 /* register v4l2 vbi video_device */
2408 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2409 vbi_nr[dev->devno]);
2410 if (ret < 0) {
2411 cx231xx_errdev("unable to register vbi device\n");
2412 return ret;
2413 }
2414
2415 cx231xx_info("%s/0: registered device vbi%d\n",
2416 dev->name, dev->vbi_dev->num);
2417
2418 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2419 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template, "radio");
2420 if (!dev->radio_dev) {
2421 cx231xx_errdev("cannot allocate video_device.\n");
2422 return -ENODEV;
2423 }
2424 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2425 radio_nr[dev->devno]);
2426 if (ret < 0) {
2427 cx231xx_errdev("can't register radio device\n");
2428 return ret;
2429 }
2430 cx231xx_info("Registered radio device as /dev/radio%d\n",
2431 dev->radio_dev->num);
2432 }
2433
2434 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2435 dev->vdev->num, dev->vbi_dev->num);
2436
2437 return 0;
2438}
2439
2440