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