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