]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/usb/cx231xx/cx231xx-video.c
[media] cx231xx: Cleanup printk at the driver
[mirror_ubuntu-artful-kernel.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
589dadf2 25#include "cx231xx.h"
e0d3bafd
SD
26#include <linux/init.h>
27#include <linux/list.h>
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/bitmap.h>
31#include <linux/usb.h>
32#include <linux/i2c.h>
e0d3bafd
SD
33#include <linux/mm.h>
34#include <linux/mutex.h>
5a0e3ad6 35#include <linux/slab.h>
e0d3bafd
SD
36
37#include <media/v4l2-common.h>
38#include <media/v4l2-ioctl.h>
1d08a4fa 39#include <media/v4l2-event.h>
e0d3bafd
SD
40#include <media/msp3400.h>
41#include <media/tuner.h>
42
43#include "dvb_frontend.h"
44
e0d3bafd
SD
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 }
ed0e3729 740 /*pr_debug("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
64fbf444 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
ed0e3729 812 pr_debug("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) {
589dadf2 864 pr_err("v4l2 ioctl: device not present\n");
e0d3bafd
SD
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;
888
e0d3bafd
SD
889 return 0;
890}
891
892static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
893{
894 unsigned int i;
895
896 for (i = 0; i < ARRAY_SIZE(format); i++)
897 if (format[i].fourcc == fourcc)
898 return &format[i];
899
900 return NULL;
901}
902
903static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
84b5dbf3 904 struct v4l2_format *f)
e0d3bafd 905{
84b5dbf3
MCC
906 struct cx231xx_fh *fh = priv;
907 struct cx231xx *dev = fh->dev;
9bd0e8d7
TP
908 unsigned int width = f->fmt.pix.width;
909 unsigned int height = f->fmt.pix.height;
84b5dbf3
MCC
910 unsigned int maxw = norm_maxw(dev);
911 unsigned int maxh = norm_maxh(dev);
84b5dbf3 912 struct cx231xx_fmt *fmt;
e0d3bafd
SD
913
914 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
915 if (!fmt) {
916 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
84b5dbf3 917 f->fmt.pix.pixelformat);
e0d3bafd
SD
918 return -EINVAL;
919 }
920
921 /* width must even because of the YUYV format
922 height must be even because of interlacing */
9bd0e8d7 923 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
e0d3bafd 924
e0d3bafd
SD
925 f->fmt.pix.width = width;
926 f->fmt.pix.height = height;
927 f->fmt.pix.pixelformat = fmt->fourcc;
8b735c13 928 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
e0d3bafd
SD
929 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
930 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
931 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
932
933 return 0;
934}
935
936static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
84b5dbf3 937 struct v4l2_format *f)
e0d3bafd 938{
84b5dbf3
MCC
939 struct cx231xx_fh *fh = priv;
940 struct cx231xx *dev = fh->dev;
941 int rc;
942 struct cx231xx_fmt *fmt;
112cb4a8 943 struct v4l2_mbus_framefmt mbus_fmt;
e0d3bafd
SD
944
945 rc = check_dev(dev);
946 if (rc < 0)
947 return rc;
948
84b5dbf3 949 vidioc_try_fmt_vid_cap(file, priv, f);
e0d3bafd 950
84b5dbf3 951 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
643800d5
MCC
952 if (!fmt)
953 return -EINVAL;
e0d3bafd
SD
954
955 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
ed0e3729 956 pr_err("%s: queue busy\n", __func__);
643800d5 957 return -EBUSY;
e0d3bafd
SD
958 }
959
960 if (dev->stream_on && !fh->stream_on) {
ed0e3729 961 pr_err("%s: device in use by another fh\n", __func__);
643800d5 962 return -EBUSY;
e0d3bafd
SD
963 }
964
965 /* set new image size */
966 dev->width = f->fmt.pix.width;
967 dev->height = f->fmt.pix.height;
84b5dbf3 968 dev->format = fmt;
e0d3bafd 969
112cb4a8
HV
970 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
971 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
972 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
e0d3bafd 973
e0d3bafd
SD
974 return rc;
975}
976
64fbf444 977static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
e0d3bafd 978{
84b5dbf3
MCC
979 struct cx231xx_fh *fh = priv;
980 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
981
982 *id = dev->norm;
983 return 0;
984}
985
314527ac 986static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
e0d3bafd 987{
84b5dbf3
MCC
988 struct cx231xx_fh *fh = priv;
989 struct cx231xx *dev = fh->dev;
435b4f78 990 struct v4l2_mbus_framefmt mbus_fmt;
84b5dbf3 991 int rc;
e0d3bafd
SD
992
993 rc = check_dev(dev);
994 if (rc < 0)
995 return rc;
996
314527ac 997 if (dev->norm == norm)
d61072a4
HV
998 return 0;
999
1000 if (videobuf_queue_is_busy(&fh->vb_vidq))
1001 return -EBUSY;
e0d3bafd 1002
314527ac 1003 dev->norm = norm;
e0d3bafd 1004
e0d3bafd 1005 /* Adjusts width/height, if needed */
d61072a4
HV
1006 dev->width = 720;
1007 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
e0d3bafd 1008
8774bed9 1009 call_all(dev, video, s_std, dev->norm);
435b4f78
DH
1010
1011 /* We need to reset basic properties in the decoder related to
1012 resolution (since a standard change effects things like the number
1013 of lines in VACT, etc) */
d61072a4
HV
1014 memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1015 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1016 mbus_fmt.width = dev->width;
1017 mbus_fmt.height = dev->height;
435b4f78 1018 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
e0d3bafd 1019
84b5dbf3
MCC
1020 /* do mode control overrides */
1021 cx231xx_do_mode_ctrl_overrides(dev);
e0d3bafd
SD
1022
1023 return 0;
1024}
1025
1026static const char *iname[] = {
1027 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
cde4362f 1028 [CX231XX_VMUX_SVIDEO] = "S-Video",
e0d3bafd 1029 [CX231XX_VMUX_TELEVISION] = "Television",
cde4362f
MCC
1030 [CX231XX_VMUX_CABLE] = "Cable TV",
1031 [CX231XX_VMUX_DVB] = "DVB",
1032 [CX231XX_VMUX_DEBUG] = "for debug only",
e0d3bafd
SD
1033};
1034
b86d1544 1035int cx231xx_enum_input(struct file *file, void *priv,
84b5dbf3 1036 struct v4l2_input *i)
e0d3bafd 1037{
84b5dbf3
MCC
1038 struct cx231xx_fh *fh = priv;
1039 struct cx231xx *dev = fh->dev;
de99d532
DH
1040 u32 gen_stat;
1041 unsigned int ret, n;
e0d3bafd
SD
1042
1043 n = i->index;
1044 if (n >= MAX_CX231XX_INPUT)
1045 return -EINVAL;
1046 if (0 == INPUT(n)->type)
1047 return -EINVAL;
1048
1049 i->index = n;
1050 i->type = V4L2_INPUT_TYPE_CAMERA;
1051
1052 strcpy(i->name, iname[INPUT(n)->type]);
1053
1054 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
84b5dbf3 1055 (CX231XX_VMUX_CABLE == INPUT(n)->type))
e0d3bafd
SD
1056 i->type = V4L2_INPUT_TYPE_TUNER;
1057
1058 i->std = dev->vdev->tvnorms;
1059
de99d532
DH
1060 /* If they are asking about the active input, read signal status */
1061 if (n == dev->video_input) {
1062 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1063 GEN_STAT, 2, &gen_stat, 4);
1064 if (ret > 0) {
1065 if ((gen_stat & FLD_VPRES) == 0x00)
1066 i->status |= V4L2_IN_ST_NO_SIGNAL;
1067 if ((gen_stat & FLD_HLOCK) == 0x00)
1068 i->status |= V4L2_IN_ST_NO_H_LOCK;
1069 }
1070 }
1071
e0d3bafd
SD
1072 return 0;
1073}
1074
b86d1544 1075int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
e0d3bafd 1076{
84b5dbf3
MCC
1077 struct cx231xx_fh *fh = priv;
1078 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1079
1080 *i = dev->video_input;
1081
1082 return 0;
1083}
1084
b86d1544 1085int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
e0d3bafd 1086{
84b5dbf3
MCC
1087 struct cx231xx_fh *fh = priv;
1088 struct cx231xx *dev = fh->dev;
1089 int rc;
e0d3bafd 1090
64fbf444 1091 dev->mode_tv = 0;
e0d3bafd
SD
1092 rc = check_dev(dev);
1093 if (rc < 0)
1094 return rc;
1095
1096 if (i >= MAX_CX231XX_INPUT)
1097 return -EINVAL;
1098 if (0 == INPUT(i)->type)
1099 return -EINVAL;
1100
e0d3bafd
SD
1101 video_mux(dev, i);
1102
c09d6695
DH
1103 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1104 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1105 /* There's a tuner, so reset the standard and put it on the
1106 last known frequency (since it was probably powered down
1107 until now */
8774bed9 1108 call_all(dev, video, s_std, dev->norm);
c09d6695
DH
1109 }
1110
e0d3bafd
SD
1111 return 0;
1112}
1113
b86d1544 1114int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd 1115{
84b5dbf3
MCC
1116 struct cx231xx_fh *fh = priv;
1117 struct cx231xx *dev = fh->dev;
1118 int rc;
e0d3bafd
SD
1119
1120 rc = check_dev(dev);
1121 if (rc < 0)
1122 return rc;
1123
1124 if (0 != t->index)
1125 return -EINVAL;
1126
1127 strcpy(t->name, "Tuner");
1128
84b5dbf3 1129 t->type = V4L2_TUNER_ANALOG_TV;
e0d3bafd 1130 t->capability = V4L2_TUNER_CAP_NORM;
84b5dbf3
MCC
1131 t->rangehigh = 0xffffffffUL;
1132 t->signal = 0xffff; /* LOCKED */
b251f957 1133 call_all(dev, tuner, g_tuner, t);
e0d3bafd
SD
1134
1135 return 0;
1136}
1137
2f73c7c5 1138int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd 1139{
84b5dbf3
MCC
1140 struct cx231xx_fh *fh = priv;
1141 struct cx231xx *dev = fh->dev;
1142 int rc;
e0d3bafd
SD
1143
1144 rc = check_dev(dev);
1145 if (rc < 0)
1146 return rc;
1147
1148 if (0 != t->index)
1149 return -EINVAL;
1150#if 0
b1196126 1151 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1152#endif
1153 return 0;
1154}
1155
b86d1544 1156int cx231xx_g_frequency(struct file *file, void *priv,
84b5dbf3 1157 struct v4l2_frequency *f)
e0d3bafd 1158{
84b5dbf3
MCC
1159 struct cx231xx_fh *fh = priv;
1160 struct cx231xx *dev = fh->dev;
e0d3bafd 1161
b251f957
HV
1162 if (f->tuner)
1163 return -EINVAL;
1164
e0d3bafd
SD
1165 f->frequency = dev->ctl_freq;
1166
e0d3bafd
SD
1167 return 0;
1168}
1169
b86d1544 1170int cx231xx_s_frequency(struct file *file, void *priv,
b530a447 1171 const struct v4l2_frequency *f)
e0d3bafd 1172{
84b5dbf3
MCC
1173 struct cx231xx_fh *fh = priv;
1174 struct cx231xx *dev = fh->dev;
b530a447 1175 struct v4l2_frequency new_freq = *f;
84b5dbf3 1176 int rc;
64fbf444
PB
1177 u32 if_frequency = 5400000;
1178
ed0e3729 1179 pr_debug("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
64fbf444 1180 f->frequency, f->type);
e0d3bafd
SD
1181
1182 rc = check_dev(dev);
1183 if (rc < 0)
1184 return rc;
1185
1186 if (0 != f->tuner)
1187 return -EINVAL;
1188
84b5dbf3
MCC
1189 /* set pre channel change settings in DIF first */
1190 rc = cx231xx_tuner_pre_channel_change(dev);
e0d3bafd 1191
64fbf444 1192 call_all(dev, tuner, s_frequency, f);
b530a447
HV
1193 call_all(dev, tuner, g_frequency, &new_freq);
1194 dev->ctl_freq = new_freq.frequency;
e0d3bafd 1195
84b5dbf3
MCC
1196 /* set post channel change settings in DIF first */
1197 rc = cx231xx_tuner_post_channel_change(dev);
e0d3bafd 1198
64fbf444
PB
1199 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1200 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1201 if_frequency = 5400000; /*5.4MHz */
1202 else if (dev->norm & V4L2_STD_B)
1203 if_frequency = 6000000; /*6.0MHz */
1204 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1205 if_frequency = 6900000; /*6.9MHz */
1206 else if (dev->norm & V4L2_STD_GH)
1207 if_frequency = 7100000; /*7.1MHz */
1208 else if (dev->norm & V4L2_STD_PAL_I)
1209 if_frequency = 7250000; /*7.25MHz */
1210 else if (dev->norm & V4L2_STD_SECAM_L)
1211 if_frequency = 6900000; /*6.9MHz */
1212 else if (dev->norm & V4L2_STD_SECAM_LC)
1213 if_frequency = 1250000; /*1.25MHz */
1214
ed0e3729 1215 pr_debug("if_frequency is set to %d\n", if_frequency);
64fbf444
PB
1216 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1217
1218 update_HH_register_after_set_DIF(dev);
1219 }
1220
ed0e3729 1221 pr_debug("Set New FREQUENCY to %d\n", f->frequency);
e0d3bafd
SD
1222
1223 return rc;
1224}
1225
08fe9f7d
HV
1226#ifdef CONFIG_VIDEO_ADV_DEBUG
1227
1228int cx231xx_g_chip_info(struct file *file, void *fh,
1229 struct v4l2_dbg_chip_info *chip)
fddd14c8 1230{
08fe9f7d
HV
1231 switch (chip->match.addr) {
1232 case 0: /* Cx231xx - internal registers */
1233 return 0;
1234 case 1: /* AFE - read byte */
1235 strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
1236 return 0;
1237 case 2: /* Video Block - read byte */
1238 strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
1239 return 0;
1240 case 3: /* I2S block - read byte */
1241 strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
1242 return 0;
1243 case 4: /* AFE - read dword */
1244 strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
1245 return 0;
1246 case 5: /* Video Block - read dword */
1247 strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
1248 return 0;
1249 case 6: /* I2S Block - read dword */
1250 strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
fddd14c8
HV
1251 return 0;
1252 }
1253 return -EINVAL;
1254}
1255
b86d1544 1256int cx231xx_g_register(struct file *file, void *priv,
e0d3bafd
SD
1257 struct v4l2_dbg_register *reg)
1258{
84b5dbf3
MCC
1259 struct cx231xx_fh *fh = priv;
1260 struct cx231xx *dev = fh->dev;
08fe9f7d 1261 int ret;
84b5dbf3
MCC
1262 u8 value[4] = { 0, 0, 0, 0 };
1263 u32 data = 0;
1264
08fe9f7d
HV
1265 switch (reg->match.addr) {
1266 case 0: /* Cx231xx - internal registers */
1267 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1268 (u16)reg->reg, value, 4);
1269 reg->val = value[0] | value[1] << 8 |
1270 value[2] << 16 | value[3] << 24;
04ae4cf2 1271 reg->size = 4;
08fe9f7d
HV
1272 break;
1273 case 1: /* AFE - read byte */
1274 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1275 (u16)reg->reg, 2, &data, 1);
1276 reg->val = data;
04ae4cf2 1277 reg->size = 1;
08fe9f7d
HV
1278 break;
1279 case 2: /* Video Block - read byte */
1280 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1281 (u16)reg->reg, 2, &data, 1);
1282 reg->val = data;
04ae4cf2 1283 reg->size = 1;
08fe9f7d
HV
1284 break;
1285 case 3: /* I2S block - read byte */
1286 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1287 (u16)reg->reg, 1, &data, 1);
1288 reg->val = data;
04ae4cf2 1289 reg->size = 1;
08fe9f7d
HV
1290 break;
1291 case 4: /* AFE - read dword */
1292 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1293 (u16)reg->reg, 2, &data, 4);
1294 reg->val = data;
04ae4cf2 1295 reg->size = 4;
08fe9f7d
HV
1296 break;
1297 case 5: /* Video Block - read dword */
1298 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1299 (u16)reg->reg, 2, &data, 4);
1300 reg->val = data;
04ae4cf2 1301 reg->size = 4;
08fe9f7d
HV
1302 break;
1303 case 6: /* I2S Block - read dword */
1304 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1305 (u16)reg->reg, 1, &data, 4);
1306 reg->val = data;
04ae4cf2 1307 reg->size = 4;
08fe9f7d 1308 break;
84b5dbf3 1309 default:
08fe9f7d 1310 return -EINVAL;
84b5dbf3 1311 }
08fe9f7d 1312 return ret < 0 ? ret : 0;
e0d3bafd
SD
1313}
1314
b86d1544 1315int cx231xx_s_register(struct file *file, void *priv,
977ba3b1 1316 const struct v4l2_dbg_register *reg)
e0d3bafd 1317{
84b5dbf3
MCC
1318 struct cx231xx_fh *fh = priv;
1319 struct cx231xx *dev = fh->dev;
08fe9f7d 1320 int ret;
84b5dbf3 1321 u8 data[4] = { 0, 0, 0, 0 };
e0d3bafd 1322
08fe9f7d
HV
1323 switch (reg->match.addr) {
1324 case 0: /* cx231xx internal registers */
1325 data[0] = (u8) reg->val;
1326 data[1] = (u8) (reg->val >> 8);
1327 data[2] = (u8) (reg->val >> 16);
1328 data[3] = (u8) (reg->val >> 24);
1329 ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1330 (u16)reg->reg, data, 4);
1331 break;
1332 case 1: /* AFE - write byte */
1333 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1334 (u16)reg->reg, 2, reg->val, 1);
1335 break;
1336 case 2: /* Video Block - write byte */
1337 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1338 (u16)reg->reg, 2, reg->val, 1);
1339 break;
1340 case 3: /* I2S block - write byte */
1341 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1342 (u16)reg->reg, 1, reg->val, 1);
1343 break;
1344 case 4: /* AFE - write dword */
1345 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1346 (u16)reg->reg, 2, reg->val, 4);
1347 break;
1348 case 5: /* Video Block - write dword */
1349 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1350 (u16)reg->reg, 2, reg->val, 4);
84b5dbf3 1351 break;
08fe9f7d
HV
1352 case 6: /* I2S block - write dword */
1353 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1354 (u16)reg->reg, 1, reg->val, 4);
1355 break;
1356 default:
1357 return -EINVAL;
84b5dbf3 1358 }
08fe9f7d 1359 return ret < 0 ? ret : 0;
e0d3bafd
SD
1360}
1361#endif
1362
e0d3bafd 1363static int vidioc_cropcap(struct file *file, void *priv,
84b5dbf3 1364 struct v4l2_cropcap *cc)
e0d3bafd 1365{
84b5dbf3
MCC
1366 struct cx231xx_fh *fh = priv;
1367 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1368
1369 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1370 return -EINVAL;
1371
1372 cc->bounds.left = 0;
1373 cc->bounds.top = 0;
1374 cc->bounds.width = dev->width;
1375 cc->bounds.height = dev->height;
1376 cc->defrect = cc->bounds;
1377 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1378 cc->pixelaspect.denominator = 59;
1379
1380 return 0;
1381}
1382
1383static int vidioc_streamon(struct file *file, void *priv,
84b5dbf3 1384 enum v4l2_buf_type type)
e0d3bafd 1385{
84b5dbf3
MCC
1386 struct cx231xx_fh *fh = priv;
1387 struct cx231xx *dev = fh->dev;
1388 int rc;
e0d3bafd
SD
1389
1390 rc = check_dev(dev);
1391 if (rc < 0)
1392 return rc;
1393
e0d3bafd
SD
1394 rc = res_get(fh);
1395
1396 if (likely(rc >= 0))
1397 rc = videobuf_streamon(&fh->vb_vidq);
1398
b1196126
SD
1399 call_all(dev, video, s_stream, 1);
1400
e0d3bafd
SD
1401 return rc;
1402}
1403
1404static int vidioc_streamoff(struct file *file, void *priv,
84b5dbf3 1405 enum v4l2_buf_type type)
e0d3bafd 1406{
84b5dbf3
MCC
1407 struct cx231xx_fh *fh = priv;
1408 struct cx231xx *dev = fh->dev;
1409 int rc;
e0d3bafd
SD
1410
1411 rc = check_dev(dev);
1412 if (rc < 0)
1413 return rc;
1414
e0d3bafd
SD
1415 if (type != fh->type)
1416 return -EINVAL;
1417
b1196126
SD
1418 cx25840_call(dev, video, s_stream, 0);
1419
e0d3bafd
SD
1420 videobuf_streamoff(&fh->vb_vidq);
1421 res_free(fh);
1422
e0d3bafd
SD
1423 return 0;
1424}
1425
bc08734c 1426int cx231xx_querycap(struct file *file, void *priv,
84b5dbf3 1427 struct v4l2_capability *cap)
e0d3bafd 1428{
4bc837d4 1429 struct video_device *vdev = video_devdata(file);
84b5dbf3
MCC
1430 struct cx231xx_fh *fh = priv;
1431 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1432
1433 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1434 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
2c6beca8 1435 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
e0d3bafd 1436
530e01e7
HV
1437 if (vdev->vfl_type == VFL_TYPE_RADIO)
1438 cap->device_caps = V4L2_CAP_RADIO;
1439 else {
06c46003 1440 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
530e01e7
HV
1441 if (vdev->vfl_type == VFL_TYPE_VBI)
1442 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1443 else
1444 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1445 }
e0d3bafd 1446 if (dev->tuner_type != TUNER_ABSENT)
4bc837d4 1447 cap->device_caps |= V4L2_CAP_TUNER;
06c46003 1448 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
4bc837d4 1449 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
530e01e7
HV
1450 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1451 if (dev->radio_dev)
1452 cap->capabilities |= V4L2_CAP_RADIO;
e0d3bafd
SD
1453
1454 return 0;
1455}
1456
84b5dbf3
MCC
1457static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1458 struct v4l2_fmtdesc *f)
e0d3bafd 1459{
84b5dbf3 1460 if (unlikely(f->index >= ARRAY_SIZE(format)))
e0d3bafd
SD
1461 return -EINVAL;
1462
1463 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1464 f->pixelformat = format[f->index].fourcc;
1465
1466 return 0;
1467}
1468
e0d3bafd
SD
1469/* RAW VBI ioctls */
1470
1471static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
84b5dbf3 1472 struct v4l2_format *f)
e0d3bafd 1473{
84b5dbf3
MCC
1474 struct cx231xx_fh *fh = priv;
1475 struct cx231xx *dev = fh->dev;
6264722c 1476
adc0356e 1477 f->fmt.vbi.sampling_rate = 6750000 * 4;
e0d3bafd
SD
1478 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1479 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
adc0356e 1480 f->fmt.vbi.offset = 0;
e0d3bafd 1481 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1482 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
e0d3bafd 1483 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1484 PAL_VBI_LINES : NTSC_VBI_LINES;
e0d3bafd 1485 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1486 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
e0d3bafd 1487 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
6264722c 1488 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
e0d3bafd
SD
1489
1490 return 0;
1491
1492}
1493
1494static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
84b5dbf3 1495 struct v4l2_format *f)
e0d3bafd 1496{
84b5dbf3
MCC
1497 struct cx231xx_fh *fh = priv;
1498 struct cx231xx *dev = fh->dev;
e0d3bafd 1499
adc0356e 1500 f->fmt.vbi.sampling_rate = 6750000 * 4;
e0d3bafd
SD
1501 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1502 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
adc0356e 1503 f->fmt.vbi.offset = 0;
e0d3bafd
SD
1504 f->fmt.vbi.flags = 0;
1505 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1506 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
e0d3bafd 1507 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1508 PAL_VBI_LINES : NTSC_VBI_LINES;
e0d3bafd 1509 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1510 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
e0d3bafd 1511 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
6264722c 1512 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
e0d3bafd
SD
1513
1514 return 0;
1515
1516}
1517
6264722c
HV
1518static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1519 struct v4l2_format *f)
1520{
1521 struct cx231xx_fh *fh = priv;
1522 struct cx231xx *dev = fh->dev;
1523
1524 if (dev->vbi_stream_on && !fh->stream_on) {
589dadf2 1525 pr_err("%s device in use by another fh\n", __func__);
6264722c
HV
1526 return -EBUSY;
1527 }
1528 return vidioc_try_fmt_vbi_cap(file, priv, f);
1529}
1530
e0d3bafd
SD
1531static int vidioc_reqbufs(struct file *file, void *priv,
1532 struct v4l2_requestbuffers *rb)
1533{
84b5dbf3
MCC
1534 struct cx231xx_fh *fh = priv;
1535 struct cx231xx *dev = fh->dev;
1536 int rc;
e0d3bafd
SD
1537
1538 rc = check_dev(dev);
1539 if (rc < 0)
1540 return rc;
1541
cde4362f 1542 return videobuf_reqbufs(&fh->vb_vidq, rb);
e0d3bafd
SD
1543}
1544
84b5dbf3 1545static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
e0d3bafd 1546{
84b5dbf3
MCC
1547 struct cx231xx_fh *fh = priv;
1548 struct cx231xx *dev = fh->dev;
1549 int rc;
e0d3bafd
SD
1550
1551 rc = check_dev(dev);
1552 if (rc < 0)
1553 return rc;
1554
cde4362f 1555 return videobuf_querybuf(&fh->vb_vidq, b);
e0d3bafd
SD
1556}
1557
1558static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1559{
84b5dbf3
MCC
1560 struct cx231xx_fh *fh = priv;
1561 struct cx231xx *dev = fh->dev;
1562 int rc;
e0d3bafd
SD
1563
1564 rc = check_dev(dev);
1565 if (rc < 0)
1566 return rc;
1567
cde4362f 1568 return videobuf_qbuf(&fh->vb_vidq, b);
e0d3bafd
SD
1569}
1570
1571static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1572{
84b5dbf3
MCC
1573 struct cx231xx_fh *fh = priv;
1574 struct cx231xx *dev = fh->dev;
1575 int rc;
e0d3bafd
SD
1576
1577 rc = check_dev(dev);
1578 if (rc < 0)
1579 return rc;
1580
cde4362f 1581 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
e0d3bafd
SD
1582}
1583
e0d3bafd
SD
1584/* ----------------------------------------------------------- */
1585/* RADIO ESPECIFIC IOCTLS */
1586/* ----------------------------------------------------------- */
1587
84b5dbf3 1588static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd
SD
1589{
1590 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1591
530e01e7 1592 if (t->index)
e0d3bafd
SD
1593 return -EINVAL;
1594
1595 strcpy(t->name, "Radio");
e0d3bafd 1596
530e01e7 1597 call_all(dev, tuner, g_tuner, t);
e0d3bafd 1598
e0d3bafd
SD
1599 return 0;
1600}
2f73c7c5 1601static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd
SD
1602{
1603 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1604
b86d1544 1605 if (t->index)
e0d3bafd
SD
1606 return -EINVAL;
1607
b1196126 1608 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1609
1610 return 0;
1611}
1612
e0d3bafd
SD
1613/*
1614 * cx231xx_v4l2_open()
1615 * inits the device and starts isoc transfer
1616 */
1617static int cx231xx_v4l2_open(struct file *filp)
1618{
4df16f70 1619 int radio = 0;
63b0d5ad
LP
1620 struct video_device *vdev = video_devdata(filp);
1621 struct cx231xx *dev = video_drvdata(filp);
e0d3bafd
SD
1622 struct cx231xx_fh *fh;
1623 enum v4l2_buf_type fh_type = 0;
1624
63b0d5ad
LP
1625 switch (vdev->vfl_type) {
1626 case VFL_TYPE_GRABBER:
1627 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1628 break;
1629 case VFL_TYPE_VBI:
1630 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1631 break;
1632 case VFL_TYPE_RADIO:
1633 radio = 1;
1634 break;
1635 }
e0d3bafd 1636
50462eb0
LP
1637 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1638 video_device_node_name(vdev), v4l2_type_names[fh_type],
1639 dev->users);
e0d3bafd
SD
1640
1641#if 0
1642 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1643 if (errCode < 0) {
ed0e3729 1644 pr_err("Device locked on digital mode. Can't open analog\n");
e0d3bafd
SD
1645 return -EBUSY;
1646 }
1647#endif
1648
1649 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1650 if (!fh) {
589dadf2 1651 pr_err("cx231xx-video.c: Out of memory?!\n");
e0d3bafd
SD
1652 return -ENOMEM;
1653 }
1f7e073d
HV
1654 if (mutex_lock_interruptible(&dev->lock)) {
1655 kfree(fh);
1656 return -ERESTARTSYS;
1657 }
e0d3bafd 1658 fh->dev = dev;
e0d3bafd
SD
1659 fh->type = fh_type;
1660 filp->private_data = fh;
1d08a4fa 1661 v4l2_fh_init(&fh->fh, vdev);
e0d3bafd
SD
1662
1663 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
84b5dbf3 1664 /* Power up in Analog TV mode */
2f861387 1665 if (dev->board.external_av)
64fbf444
PB
1666 cx231xx_set_power_mode(dev,
1667 POLARIS_AVMODE_ENXTERNAL_AV);
1668 else
1669 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
e0d3bafd
SD
1670
1671#if 0
1672 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1673#endif
e0d3bafd 1674
84b5dbf3
MCC
1675 /* set video alternate setting */
1676 cx231xx_set_video_alternate(dev);
e0d3bafd
SD
1677
1678 /* Needed, since GPIO might have disabled power of
1679 some i2c device */
1680 cx231xx_config_i2c(dev);
1681
1682 /* device needs to be initialized before isoc transfer */
84b5dbf3 1683 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
e0d3bafd
SD
1684
1685 }
71590765 1686 if (radio) {
e0d3bafd
SD
1687 cx231xx_videodbg("video_open: setting radio device\n");
1688
1689 /* cx231xx_start_radio(dev); */
1690
b1196126 1691 call_all(dev, tuner, s_radio);
e0d3bafd
SD
1692 }
1693
1694 dev->users++;
1695
cde4362f
MCC
1696 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1697 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1698 NULL, &dev->video_mode.slock,
1699 fh->type, V4L2_FIELD_INTERLACED,
08bff03e 1700 sizeof(struct cx231xx_buffer),
643800d5 1701 fh, &dev->lock);
84b5dbf3 1702 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
cde4362f
MCC
1703 /* Set the required alternate setting VBI interface works in
1704 Bulk mode only */
2f861387 1705 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
e0d3bafd 1706
cde4362f
MCC
1707 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1708 NULL, &dev->vbi_mode.slock,
1709 fh->type, V4L2_FIELD_SEQ_TB,
08bff03e 1710 sizeof(struct cx231xx_buffer),
643800d5 1711 fh, &dev->lock);
84b5dbf3 1712 }
1f7e073d 1713 mutex_unlock(&dev->lock);
1d08a4fa 1714 v4l2_fh_add(&fh->fh);
e0d3bafd 1715
4df16f70 1716 return 0;
e0d3bafd
SD
1717}
1718
1719/*
1720 * cx231xx_realease_resources()
1721 * unregisters the v4l2,i2c and usb devices
1722 * called when the device gets disconected or at module unload
1723*/
1724void cx231xx_release_analog_resources(struct cx231xx *dev)
1725{
1726
1727 /*FIXME: I2C IR should be disconnected */
1728
1729 if (dev->radio_dev) {
f0813b4c 1730 if (video_is_registered(dev->radio_dev))
e0d3bafd
SD
1731 video_unregister_device(dev->radio_dev);
1732 else
1733 video_device_release(dev->radio_dev);
1734 dev->radio_dev = NULL;
1735 }
1736 if (dev->vbi_dev) {
589dadf2 1737 pr_info("V4L2 device %s deregistered\n",
ed0e3729 1738 video_device_node_name(dev->vbi_dev));
f0813b4c 1739 if (video_is_registered(dev->vbi_dev))
e0d3bafd
SD
1740 video_unregister_device(dev->vbi_dev);
1741 else
1742 video_device_release(dev->vbi_dev);
1743 dev->vbi_dev = NULL;
1744 }
1745 if (dev->vdev) {
589dadf2 1746 pr_info("V4L2 device %s deregistered\n",
ed0e3729 1747 video_device_node_name(dev->vdev));
64fbf444 1748
2f861387 1749 if (dev->board.has_417)
64fbf444
PB
1750 cx231xx_417_unregister(dev);
1751
f0813b4c 1752 if (video_is_registered(dev->vdev))
e0d3bafd
SD
1753 video_unregister_device(dev->vdev);
1754 else
1755 video_device_release(dev->vdev);
1756 dev->vdev = NULL;
1757 }
d2370f8e
HV
1758 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1759 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
e0d3bafd
SD
1760}
1761
1762/*
1f7e073d 1763 * cx231xx_close()
e0d3bafd
SD
1764 * stops streaming and deallocates all resources allocated by the v4l2
1765 * calls and ioctls
1766 */
1f7e073d 1767static int cx231xx_close(struct file *filp)
e0d3bafd 1768{
84b5dbf3
MCC
1769 struct cx231xx_fh *fh = filp->private_data;
1770 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1771
1772 cx231xx_videodbg("users=%d\n", dev->users);
1773
64fbf444 1774 cx231xx_videodbg("users=%d\n", dev->users);
e0d3bafd
SD
1775 if (res_check(fh))
1776 res_free(fh);
1777
2f861387
MCC
1778 /*
1779 * To workaround error number=-71 on EP0 for VideoGrabber,
1780 * need exclude following.
1781 * FIXME: It is probably safe to remove most of these, as we're
1782 * now avoiding the alternate setting for INDEX_VANC
1783 */
1784 if (!dev->board.no_alt_vanc)
64fbf444
PB
1785 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1786 videobuf_stop(&fh->vb_vidq);
1787 videobuf_mmap_free(&fh->vb_vidq);
1788
1789 /* the device is already disconnect,
1790 free the remaining resources */
1791 if (dev->state & DEV_DISCONNECTED) {
1792 if (atomic_read(&dev->devlist_count) > 0) {
1793 cx231xx_release_resources(dev);
266e8ae3 1794 fh->dev = NULL;
64fbf444
PB
1795 return 0;
1796 }
64fbf444
PB
1797 return 0;
1798 }
84b5dbf3 1799
64fbf444
PB
1800 /* do this before setting alternate! */
1801 cx231xx_uninit_vbi_isoc(dev);
1802
1803 /* set alternate 0 */
1804 if (!dev->vbi_or_sliced_cc_mode)
1805 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1806 else
1807 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1808
1d08a4fa
HV
1809 v4l2_fh_del(&fh->fh);
1810 v4l2_fh_exit(&fh->fh);
64fbf444
PB
1811 kfree(fh);
1812 dev->users--;
1813 wake_up_interruptible_nr(&dev->open, 1);
84b5dbf3
MCC
1814 return 0;
1815 }
1816
1d08a4fa 1817 v4l2_fh_del(&fh->fh);
990862a2
MCC
1818 dev->users--;
1819 if (!dev->users) {
e0d3bafd
SD
1820 videobuf_stop(&fh->vb_vidq);
1821 videobuf_mmap_free(&fh->vb_vidq);
1822
1823 /* the device is already disconnect,
1824 free the remaining resources */
1825 if (dev->state & DEV_DISCONNECTED) {
1826 cx231xx_release_resources(dev);
266e8ae3 1827 fh->dev = NULL;
e0d3bafd
SD
1828 return 0;
1829 }
1830
84b5dbf3 1831 /* Save some power by putting tuner to sleep */
622b828a 1832 call_all(dev, core, s_power, 0);
e0d3bafd
SD
1833
1834 /* do this before setting alternate! */
64fbf444
PB
1835 if (dev->USE_ISO)
1836 cx231xx_uninit_isoc(dev);
1837 else
1838 cx231xx_uninit_bulk(dev);
e0d3bafd
SD
1839 cx231xx_set_mode(dev, CX231XX_SUSPEND);
1840
1841 /* set alternate 0 */
1842 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1843 }
1d08a4fa 1844 v4l2_fh_exit(&fh->fh);
e0d3bafd 1845 kfree(fh);
e0d3bafd 1846 wake_up_interruptible_nr(&dev->open, 1);
e0d3bafd
SD
1847 return 0;
1848}
1849
1f7e073d
HV
1850static int cx231xx_v4l2_close(struct file *filp)
1851{
1852 struct cx231xx_fh *fh = filp->private_data;
1853 struct cx231xx *dev = fh->dev;
1854 int rc;
1855
1856 mutex_lock(&dev->lock);
1857 rc = cx231xx_close(filp);
1858 mutex_unlock(&dev->lock);
1859 return rc;
1860}
1861
e0d3bafd
SD
1862/*
1863 * cx231xx_v4l2_read()
1864 * will allocate buffers when called for the first time
1865 */
1866static ssize_t
cde4362f
MCC
1867cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1868 loff_t *pos)
e0d3bafd
SD
1869{
1870 struct cx231xx_fh *fh = filp->private_data;
1871 struct cx231xx *dev = fh->dev;
1872 int rc;
1873
1874 rc = check_dev(dev);
1875 if (rc < 0)
1876 return rc;
1877
84b5dbf3
MCC
1878 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1879 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
e0d3bafd 1880 rc = res_get(fh);
e0d3bafd
SD
1881
1882 if (unlikely(rc < 0))
1883 return rc;
1884
1f7e073d
HV
1885 if (mutex_lock_interruptible(&dev->lock))
1886 return -ERESTARTSYS;
1887 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
84b5dbf3 1888 filp->f_flags & O_NONBLOCK);
1f7e073d
HV
1889 mutex_unlock(&dev->lock);
1890 return rc;
e0d3bafd
SD
1891 }
1892 return 0;
1893}
1894
1895/*
1896 * cx231xx_v4l2_poll()
1897 * will allocate buffers when called for the first time
1898 */
64fbf444 1899static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
e0d3bafd 1900{
1d08a4fa 1901 unsigned long req_events = poll_requested_events(wait);
e0d3bafd
SD
1902 struct cx231xx_fh *fh = filp->private_data;
1903 struct cx231xx *dev = fh->dev;
1d08a4fa 1904 unsigned res = 0;
e0d3bafd
SD
1905 int rc;
1906
1907 rc = check_dev(dev);
1908 if (rc < 0)
1d08a4fa 1909 return POLLERR;
e0d3bafd 1910
e0d3bafd 1911 rc = res_get(fh);
e0d3bafd
SD
1912
1913 if (unlikely(rc < 0))
1914 return POLLERR;
1915
1d08a4fa
HV
1916 if (v4l2_event_pending(&fh->fh))
1917 res |= POLLPRI;
1918 else
1919 poll_wait(filp, &fh->fh.wait, wait);
1920
1921 if (!(req_events & (POLLIN | POLLRDNORM)))
1922 return res;
1923
84b5dbf3 1924 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
1f7e073d 1925 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
1f7e073d 1926 mutex_lock(&dev->lock);
1d08a4fa 1927 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1f7e073d
HV
1928 mutex_unlock(&dev->lock);
1929 return res;
1930 }
1d08a4fa 1931 return res | POLLERR;
e0d3bafd
SD
1932}
1933
1934/*
1935 * cx231xx_v4l2_mmap()
1936 */
1937static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1938{
84b5dbf3
MCC
1939 struct cx231xx_fh *fh = filp->private_data;
1940 struct cx231xx *dev = fh->dev;
1941 int rc;
e0d3bafd
SD
1942
1943 rc = check_dev(dev);
1944 if (rc < 0)
1945 return rc;
1946
e0d3bafd 1947 rc = res_get(fh);
e0d3bafd
SD
1948
1949 if (unlikely(rc < 0))
1950 return rc;
1951
1f7e073d
HV
1952 if (mutex_lock_interruptible(&dev->lock))
1953 return -ERESTARTSYS;
e0d3bafd 1954 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1f7e073d 1955 mutex_unlock(&dev->lock);
e0d3bafd
SD
1956
1957 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
84b5dbf3
MCC
1958 (unsigned long)vma->vm_start,
1959 (unsigned long)vma->vm_end -
1960 (unsigned long)vma->vm_start, rc);
e0d3bafd
SD
1961
1962 return rc;
1963}
1964
1965static const struct v4l2_file_operations cx231xx_v4l_fops = {
cde4362f
MCC
1966 .owner = THIS_MODULE,
1967 .open = cx231xx_v4l2_open,
84b5dbf3 1968 .release = cx231xx_v4l2_close,
cde4362f
MCC
1969 .read = cx231xx_v4l2_read,
1970 .poll = cx231xx_v4l2_poll,
1971 .mmap = cx231xx_v4l2_mmap,
643800d5 1972 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
1973};
1974
1975static const struct v4l2_ioctl_ops video_ioctl_ops = {
bc08734c 1976 .vidioc_querycap = cx231xx_querycap,
cde4362f
MCC
1977 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1978 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1979 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1980 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1981 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1982 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
6264722c 1983 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
cde4362f 1984 .vidioc_cropcap = vidioc_cropcap,
cde4362f
MCC
1985 .vidioc_reqbufs = vidioc_reqbufs,
1986 .vidioc_querybuf = vidioc_querybuf,
1987 .vidioc_qbuf = vidioc_qbuf,
1988 .vidioc_dqbuf = vidioc_dqbuf,
1989 .vidioc_s_std = vidioc_s_std,
1990 .vidioc_g_std = vidioc_g_std,
b86d1544
HV
1991 .vidioc_enum_input = cx231xx_enum_input,
1992 .vidioc_g_input = cx231xx_g_input,
1993 .vidioc_s_input = cx231xx_s_input,
cde4362f
MCC
1994 .vidioc_streamon = vidioc_streamon,
1995 .vidioc_streamoff = vidioc_streamoff,
b86d1544
HV
1996 .vidioc_g_tuner = cx231xx_g_tuner,
1997 .vidioc_s_tuner = cx231xx_s_tuner,
1998 .vidioc_g_frequency = cx231xx_g_frequency,
1999 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2000#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2001 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2002 .vidioc_g_register = cx231xx_g_register,
2003 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2004#endif
1d08a4fa
HV
2005 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2006 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2007};
2008
2009static struct video_device cx231xx_vbi_template;
2010
2011static const struct video_device cx231xx_video_template = {
cde4362f
MCC
2012 .fops = &cx231xx_v4l_fops,
2013 .release = video_device_release,
2014 .ioctl_ops = &video_ioctl_ops,
cde4362f 2015 .tvnorms = V4L2_STD_ALL,
e0d3bafd
SD
2016};
2017
2018static const struct v4l2_file_operations radio_fops = {
cde4362f
MCC
2019 .owner = THIS_MODULE,
2020 .open = cx231xx_v4l2_open,
84b5dbf3 2021 .release = cx231xx_v4l2_close,
1d08a4fa 2022 .poll = v4l2_ctrl_poll,
1265f080 2023 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
2024};
2025
2026static const struct v4l2_ioctl_ops radio_ioctl_ops = {
bc08734c 2027 .vidioc_querycap = cx231xx_querycap,
cde4362f 2028 .vidioc_g_tuner = radio_g_tuner,
cde4362f 2029 .vidioc_s_tuner = radio_s_tuner,
b86d1544
HV
2030 .vidioc_g_frequency = cx231xx_g_frequency,
2031 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2032#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2033 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2034 .vidioc_g_register = cx231xx_g_register,
2035 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2036#endif
1d08a4fa
HV
2037 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2038 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2039};
2040
2041static struct video_device cx231xx_radio_template = {
cde4362f
MCC
2042 .name = "cx231xx-radio",
2043 .fops = &radio_fops,
84b5dbf3 2044 .ioctl_ops = &radio_ioctl_ops,
e0d3bafd
SD
2045};
2046
2047/******************************** usb interface ******************************/
2048
b9255176
SD
2049static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2050 const struct video_device
2051 *template, const char *type_name)
e0d3bafd
SD
2052{
2053 struct video_device *vfd;
2054
2055 vfd = video_device_alloc();
2056 if (NULL == vfd)
2057 return NULL;
cde4362f 2058
e0d3bafd 2059 *vfd = *template;
b1196126 2060 vfd->v4l2_dev = &dev->v4l2_dev;
e0d3bafd
SD
2061 vfd->release = video_device_release;
2062 vfd->debug = video_debug;
643800d5 2063 vfd->lock = &dev->lock;
e0d3bafd 2064
84b5dbf3 2065 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
e0d3bafd 2066
63b0d5ad 2067 video_set_drvdata(vfd, dev);
06c46003
HV
2068 if (dev->tuner_type == TUNER_ABSENT) {
2069 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2070 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2071 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2072 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2073 }
e0d3bafd
SD
2074 return vfd;
2075}
2076
2077int cx231xx_register_analog_devices(struct cx231xx *dev)
2078{
2079 int ret;
2080
ed0e3729 2081 pr_info("v4l2 driver version %s\n", CX231XX_VERSION);
e0d3bafd
SD
2082
2083 /* set default norm */
a25a7012 2084 dev->norm = V4L2_STD_PAL;
e0d3bafd
SD
2085 dev->width = norm_maxw(dev);
2086 dev->height = norm_maxh(dev);
2087 dev->interlaced = 0;
e0d3bafd
SD
2088
2089 /* Analog specific initialization */
2090 dev->format = &format[0];
6e6a2ba9
DH
2091
2092 /* Set the initial input */
2093 video_mux(dev, dev->video_input);
e0d3bafd 2094
8774bed9 2095 call_all(dev, video, s_std, dev->norm);
d61072a4 2096
d2370f8e
HV
2097 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2098 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2099
2100 if (dev->sd_cx25840) {
2101 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2102 dev->sd_cx25840->ctrl_handler, NULL);
2103 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2104 dev->sd_cx25840->ctrl_handler,
2105 v4l2_ctrl_radio_filter);
2106 }
2107
2108 if (dev->ctrl_handler.error)
2109 return dev->ctrl_handler.error;
2110 if (dev->radio_ctrl_handler.error)
2111 return dev->radio_ctrl_handler.error;
e0d3bafd
SD
2112
2113 /* enable vbi capturing */
84b5dbf3 2114 /* write code here... */
e0d3bafd
SD
2115
2116 /* allocate and fill video video_device struct */
2117 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2118 if (!dev->vdev) {
589dadf2 2119 pr_err("cannot allocate video_device.\n");
e0d3bafd
SD
2120 return -ENODEV;
2121 }
2122
d2370f8e 2123 dev->vdev->ctrl_handler = &dev->ctrl_handler;
e0d3bafd
SD
2124 /* register v4l2 video video_device */
2125 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
84b5dbf3 2126 video_nr[dev->devno]);
e0d3bafd 2127 if (ret) {
589dadf2 2128 pr_err("unable to register video device (error=%i).\n",
ed0e3729 2129 ret);
e0d3bafd
SD
2130 return ret;
2131 }
2132
ed0e3729
MCC
2133 pr_info("Registered video device %s [v4l2]\n",
2134 video_device_node_name(dev->vdev));
e0d3bafd 2135
84b5dbf3 2136 /* Initialize VBI template */
3724dde9 2137 cx231xx_vbi_template = cx231xx_video_template;
84b5dbf3 2138 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
e0d3bafd
SD
2139
2140 /* Allocate and fill vbi video_device struct */
2141 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2142
d2370f8e 2143 if (!dev->vbi_dev) {
589dadf2 2144 pr_err("cannot allocate video_device.\n");
d2370f8e
HV
2145 return -ENODEV;
2146 }
2147 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
e0d3bafd
SD
2148 /* register v4l2 vbi video_device */
2149 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
84b5dbf3 2150 vbi_nr[dev->devno]);
e0d3bafd 2151 if (ret < 0) {
589dadf2 2152 pr_err("unable to register vbi device\n");
e0d3bafd
SD
2153 return ret;
2154 }
2155
ed0e3729
MCC
2156 pr_info("Registered VBI device %s\n",
2157 video_device_node_name(dev->vbi_dev));
e0d3bafd
SD
2158
2159 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
cde4362f
MCC
2160 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2161 "radio");
e0d3bafd 2162 if (!dev->radio_dev) {
589dadf2 2163 pr_err("cannot allocate video_device.\n");
e0d3bafd
SD
2164 return -ENODEV;
2165 }
d2370f8e 2166 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
e0d3bafd
SD
2167 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2168 radio_nr[dev->devno]);
2169 if (ret < 0) {
589dadf2 2170 pr_err("can't register radio device\n");
e0d3bafd
SD
2171 return ret;
2172 }
589dadf2 2173 pr_info("Registered radio device as %s\n",
ed0e3729 2174 video_device_node_name(dev->radio_dev));
e0d3bafd
SD
2175 }
2176
e0d3bafd
SD
2177 return 0;
2178}