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