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