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