[media] cx231xx: convert from pr_foo to dev_foo
[deliverable/linux.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>
e0d3bafd 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
e0d3bafd
SD
44#include "cx231xx-vbi.h"
45
1990d50b 46#define CX231XX_VERSION "0.0.2"
818fdf34 47
e0d3bafd
SD
48#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
49#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
50
e0d3bafd
SD
51#define cx231xx_videodbg(fmt, arg...) do {\
52 if (video_debug) \
53 printk(KERN_INFO "%s %s :"fmt, \
54 dev->name, __func__ , ##arg); } while (0)
55
56static unsigned int isoc_debug;
57module_param(isoc_debug, int, 0644);
58MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
59
60#define cx231xx_isocdbg(fmt, arg...) \
61do {\
62 if (isoc_debug) { \
63 printk(KERN_INFO "%s %s :"fmt, \
64 dev->name, __func__ , ##arg); \
65 } \
66 } while (0)
67
68MODULE_AUTHOR(DRIVER_AUTHOR);
69MODULE_DESCRIPTION(DRIVER_DESC);
70MODULE_LICENSE("GPL");
1990d50b 71MODULE_VERSION(CX231XX_VERSION);
e0d3bafd 72
e0d3bafd
SD
73static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
74static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77
84b5dbf3 78module_param_array(card, int, NULL, 0444);
e0d3bafd
SD
79module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
81module_param_array(radio_nr, int, NULL, 0444);
82
84b5dbf3 83MODULE_PARM_DESC(card, "card type");
e0d3bafd 84MODULE_PARM_DESC(video_nr, "video device numbers");
84b5dbf3 85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
e0d3bafd
SD
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
87
88static unsigned int video_debug;
89module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
91
e0d3bafd
SD
92/* supported video standards */
93static struct cx231xx_fmt format[] = {
94 {
84b5dbf3
MCC
95 .name = "16bpp YUY2, 4:2:2, packed",
96 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
98 .reg = 0,
99 },
e0d3bafd
SD
100};
101
e0d3bafd 102
e0d3bafd
SD
103/* ------------------------------------------------------------------
104 Video buffer and parser functions
105 ------------------------------------------------------------------*/
106
107/*
108 * Announces that a buffer were filled and request the next
109 */
110static inline void buffer_filled(struct cx231xx *dev,
84b5dbf3
MCC
111 struct cx231xx_dmaqueue *dma_q,
112 struct cx231xx_buffer *buf)
e0d3bafd
SD
113{
114 /* Advice that buffer was filled */
115 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
116 buf->vb.state = VIDEOBUF_DONE;
117 buf->vb.field_count++;
8e6057b5 118 v4l2_get_timestamp(&buf->vb.ts);
e0d3bafd 119
64fbf444
PB
120 if (dev->USE_ISO)
121 dev->video_mode.isoc_ctl.buf = NULL;
122 else
123 dev->video_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
124
125 list_del(&buf->vb.queue);
126 wake_up(&buf->vb.done);
127}
128
84b5dbf3 129static inline void print_err_status(struct cx231xx *dev, int packet, int status)
e0d3bafd
SD
130{
131 char *errmsg = "Unknown";
132
133 switch (status) {
134 case -ENOENT:
135 errmsg = "unlinked synchronuously";
136 break;
137 case -ECONNRESET:
138 errmsg = "unlinked asynchronuously";
139 break;
140 case -ENOSR:
141 errmsg = "Buffer error (overrun)";
142 break;
143 case -EPIPE:
144 errmsg = "Stalled (device not responding)";
145 break;
146 case -EOVERFLOW:
147 errmsg = "Babble (bad cable?)";
148 break;
149 case -EPROTO:
150 errmsg = "Bit-stuff error (bad cable?)";
151 break;
152 case -EILSEQ:
153 errmsg = "CRC/Timeout (could be anything)";
154 break;
155 case -ETIME:
156 errmsg = "Device does not respond";
157 break;
158 }
159 if (packet < 0) {
84b5dbf3 160 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
e0d3bafd
SD
161 } else {
162 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
84b5dbf3 163 packet, status, errmsg);
e0d3bafd
SD
164 }
165}
166
167/*
168 * video-buf generic routine to get the next available buffer
169 */
170static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
84b5dbf3 171 struct cx231xx_buffer **buf)
e0d3bafd 172{
84b5dbf3
MCC
173 struct cx231xx_video_mode *vmode =
174 container_of(dma_q, struct cx231xx_video_mode, vidq);
175 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
e0d3bafd
SD
176
177 char *outp;
178
e0d3bafd
SD
179 if (list_empty(&dma_q->active)) {
180 cx231xx_isocdbg("No active queue to serve\n");
64fbf444
PB
181 if (dev->USE_ISO)
182 dev->video_mode.isoc_ctl.buf = NULL;
183 else
184 dev->video_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
185 *buf = NULL;
186 return;
187 }
188
189 /* Get the next buffer */
190 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
191
25985edc 192 /* Cleans up buffer - Useful for testing for frame/URB loss */
e0d3bafd
SD
193 outp = videobuf_to_vmalloc(&(*buf)->vb);
194 memset(outp, 0, (*buf)->vb.size);
195
64fbf444
PB
196 if (dev->USE_ISO)
197 dev->video_mode.isoc_ctl.buf = *buf;
198 else
199 dev->video_mode.bulk_ctl.buf = *buf;
e0d3bafd
SD
200
201 return;
202}
203
204/*
205 * Controls the isoc copy of each urb packet
206 */
207static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
208{
84b5dbf3 209 struct cx231xx_dmaqueue *dma_q = urb->context;
4df16f70 210 int i;
e0d3bafd 211 unsigned char *p_buffer;
84b5dbf3
MCC
212 u32 bytes_parsed = 0, buffer_size = 0;
213 u8 sav_eav = 0;
e0d3bafd
SD
214
215 if (!dev)
216 return 0;
217
990862a2 218 if (dev->state & DEV_DISCONNECTED)
e0d3bafd
SD
219 return 0;
220
221 if (urb->status < 0) {
222 print_err_status(dev, -1, urb->status);
223 if (urb->status == -ENOENT)
224 return 0;
225 }
226
e0d3bafd
SD
227 for (i = 0; i < urb->number_of_packets; i++) {
228 int status = urb->iso_frame_desc[i].status;
229
230 if (status < 0) {
231 print_err_status(dev, i, status);
232 if (urb->iso_frame_desc[i].status != -EPROTO)
233 continue;
234 }
235
84b5dbf3 236 if (urb->iso_frame_desc[i].actual_length <= 0) {
e0d3bafd
SD
237 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
238 continue;
239 }
240 if (urb->iso_frame_desc[i].actual_length >
84b5dbf3 241 dev->video_mode.max_pkt_size) {
e0d3bafd
SD
242 cx231xx_isocdbg("packet bigger than packet size");
243 continue;
244 }
245
84b5dbf3 246 /* get buffer pointer and length */
e0d3bafd 247 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
84b5dbf3
MCC
248 buffer_size = urb->iso_frame_desc[i].actual_length;
249 bytes_parsed = 0;
250
251 if (dma_q->is_partial_line) {
b9255176 252 /* Handle the case of a partial line */
84b5dbf3
MCC
253 sav_eav = dma_q->last_sav;
254 } else {
b9255176
SD
255 /* Check for a SAV/EAV overlapping
256 the buffer boundary */
84b5dbf3
MCC
257 sav_eav =
258 cx231xx_find_boundary_SAV_EAV(p_buffer,
259 dma_q->partial_buf,
260 &bytes_parsed);
261 }
e0d3bafd 262
84b5dbf3 263 sav_eav &= 0xF0;
cde4362f
MCC
264 /* Get the first line if we have some portion of an SAV/EAV from
265 the last buffer or a partial line */
84b5dbf3 266 if (sav_eav) {
cde4362f 267 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
b9255176
SD
268 sav_eav, /* SAV/EAV */
269 p_buffer + bytes_parsed, /* p_buffer */
270 buffer_size - bytes_parsed);/* buf size */
84b5dbf3 271 }
e0d3bafd 272
84b5dbf3
MCC
273 /* Now parse data that is completely in this buffer */
274 /* dma_q->is_partial_line = 0; */
e0d3bafd 275
84b5dbf3
MCC
276 while (bytes_parsed < buffer_size) {
277 u32 bytes_used = 0;
e0d3bafd 278
b9255176
SD
279 sav_eav = cx231xx_find_next_SAV_EAV(
280 p_buffer + bytes_parsed, /* p_buffer */
281 buffer_size - bytes_parsed, /* buf size */
282 &bytes_used);/* bytes used to get SAV/EAV */
e0d3bafd 283
84b5dbf3 284 bytes_parsed += bytes_used;
e0d3bafd 285
84b5dbf3
MCC
286 sav_eav &= 0xF0;
287 if (sav_eav && (bytes_parsed < buffer_size)) {
cde4362f 288 bytes_parsed += cx231xx_get_video_line(dev,
b9255176
SD
289 dma_q, sav_eav, /* SAV/EAV */
290 p_buffer + bytes_parsed,/* p_buffer */
291 buffer_size - bytes_parsed);/*buf size*/
84b5dbf3
MCC
292 }
293 }
e0d3bafd 294
cde4362f
MCC
295 /* Save the last four bytes of the buffer so we can check the
296 buffer boundary condition next time */
84b5dbf3
MCC
297 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
298 bytes_parsed = 0;
e0d3bafd 299
84b5dbf3 300 }
4df16f70 301 return 1;
e0d3bafd
SD
302}
303
64fbf444
PB
304static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
305{
64fbf444 306 struct cx231xx_dmaqueue *dma_q = urb->context;
64fbf444
PB
307 unsigned char *p_buffer;
308 u32 bytes_parsed = 0, buffer_size = 0;
309 u8 sav_eav = 0;
310
311 if (!dev)
312 return 0;
313
990862a2 314 if (dev->state & DEV_DISCONNECTED)
64fbf444
PB
315 return 0;
316
317 if (urb->status < 0) {
318 print_err_status(dev, -1, urb->status);
319 if (urb->status == -ENOENT)
320 return 0;
321 }
322
64fbf444
PB
323 if (1) {
324
325 /* get buffer pointer and length */
326 p_buffer = urb->transfer_buffer;
327 buffer_size = urb->actual_length;
328 bytes_parsed = 0;
329
330 if (dma_q->is_partial_line) {
331 /* Handle the case of a partial line */
332 sav_eav = dma_q->last_sav;
333 } else {
334 /* Check for a SAV/EAV overlapping
335 the buffer boundary */
336 sav_eav =
337 cx231xx_find_boundary_SAV_EAV(p_buffer,
338 dma_q->partial_buf,
339 &bytes_parsed);
340 }
341
342 sav_eav &= 0xF0;
343 /* Get the first line if we have some portion of an SAV/EAV from
344 the last buffer or a partial line */
345 if (sav_eav) {
346 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
347 sav_eav, /* SAV/EAV */
348 p_buffer + bytes_parsed, /* p_buffer */
349 buffer_size - bytes_parsed);/* buf size */
350 }
351
352 /* Now parse data that is completely in this buffer */
353 /* dma_q->is_partial_line = 0; */
354
355 while (bytes_parsed < buffer_size) {
356 u32 bytes_used = 0;
357
358 sav_eav = cx231xx_find_next_SAV_EAV(
359 p_buffer + bytes_parsed, /* p_buffer */
360 buffer_size - bytes_parsed, /* buf size */
361 &bytes_used);/* bytes used to get SAV/EAV */
362
363 bytes_parsed += bytes_used;
364
365 sav_eav &= 0xF0;
366 if (sav_eav && (bytes_parsed < buffer_size)) {
367 bytes_parsed += cx231xx_get_video_line(dev,
368 dma_q, sav_eav, /* SAV/EAV */
369 p_buffer + bytes_parsed,/* p_buffer */
370 buffer_size - bytes_parsed);/*buf size*/
371 }
372 }
373
374 /* Save the last four bytes of the buffer so we can check the
375 buffer boundary condition next time */
376 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
377 bytes_parsed = 0;
378
379 }
4df16f70 380 return 1;
64fbf444
PB
381}
382
383
cde4362f
MCC
384u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
385 u32 *p_bytes_used)
e0d3bafd 386{
84b5dbf3
MCC
387 u32 bytes_used;
388 u8 boundary_bytes[8];
389 u8 sav_eav = 0;
390
391 *p_bytes_used = 0;
e0d3bafd 392
84b5dbf3
MCC
393 /* Create an array of the last 4 bytes of the last buffer and the first
394 4 bytes of the current buffer. */
e0d3bafd 395
84b5dbf3
MCC
396 memcpy(boundary_bytes, partial_buf, 4);
397 memcpy(boundary_bytes + 4, p_buffer, 4);
e0d3bafd 398
84b5dbf3 399 /* Check for the SAV/EAV in the boundary buffer */
cde4362f
MCC
400 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
401 &bytes_used);
e0d3bafd 402
84b5dbf3
MCC
403 if (sav_eav) {
404 /* found a boundary SAV/EAV. Updates the bytes used to reflect
405 only those used in the new buffer */
406 *p_bytes_used = bytes_used - 4;
407 }
e0d3bafd 408
84b5dbf3 409 return sav_eav;
e0d3bafd
SD
410}
411
cde4362f 412u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
84b5dbf3
MCC
413{
414 u32 i;
415 u8 sav_eav = 0;
e0d3bafd 416
cde4362f
MCC
417 /*
418 * Don't search if the buffer size is less than 4. It causes a page
419 * fault since buffer_size - 4 evaluates to a large number in that
420 * case.
421 */
84b5dbf3
MCC
422 if (buffer_size < 4) {
423 *p_bytes_used = buffer_size;
424 return 0;
425 }
e0d3bafd 426
84b5dbf3 427 for (i = 0; i < (buffer_size - 3); i++) {
e0d3bafd 428
84b5dbf3
MCC
429 if ((p_buffer[i] == 0xFF) &&
430 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
e0d3bafd 431
84b5dbf3
MCC
432 *p_bytes_used = i + 4;
433 sav_eav = p_buffer[i + 3];
434 return sav_eav;
435 }
436 }
e0d3bafd 437
84b5dbf3
MCC
438 *p_bytes_used = buffer_size;
439 return 0;
440}
e0d3bafd 441
cde4362f
MCC
442u32 cx231xx_get_video_line(struct cx231xx *dev,
443 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
444 u8 *p_buffer, u32 buffer_size)
84b5dbf3
MCC
445{
446 u32 bytes_copied = 0;
447 int current_field = -1;
448
449 switch (sav_eav) {
450 case SAV_ACTIVE_VIDEO_FIELD1:
cde4362f
MCC
451 /* looking for skipped line which occurred in PAL 720x480 mode.
452 In this case, there will be no active data contained
453 between the SAV and EAV */
454 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
455 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
456 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
457 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
458 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
459 (p_buffer[3] == EAV_VBLANK_FIELD2)))
84b5dbf3 460 return bytes_copied;
84b5dbf3
MCC
461 current_field = 1;
462 break;
e0d3bafd 463
84b5dbf3 464 case SAV_ACTIVE_VIDEO_FIELD2:
cde4362f
MCC
465 /* looking for skipped line which occurred in PAL 720x480 mode.
466 In this case, there will be no active data contained between
467 the SAV and EAV */
468 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
469 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
470 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
471 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
472 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
473 (p_buffer[3] == EAV_VBLANK_FIELD2)))
84b5dbf3 474 return bytes_copied;
84b5dbf3
MCC
475 current_field = 2;
476 break;
477 }
e0d3bafd 478
84b5dbf3 479 dma_q->last_sav = sav_eav;
e0d3bafd 480
cde4362f
MCC
481 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
482 buffer_size, current_field);
84b5dbf3
MCC
483
484 return bytes_copied;
e0d3bafd
SD
485}
486
cde4362f
MCC
487u32 cx231xx_copy_video_line(struct cx231xx *dev,
488 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
84b5dbf3 489 u32 length, int field_number)
e0d3bafd 490{
84b5dbf3
MCC
491 u32 bytes_to_copy;
492 struct cx231xx_buffer *buf;
493 u32 _line_size = dev->width * 2;
e0d3bafd 494
cde4362f 495 if (dma_q->current_field != field_number)
84b5dbf3 496 cx231xx_reset_video_buffer(dev, dma_q);
e0d3bafd 497
84b5dbf3 498 /* get the buffer pointer */
64fbf444
PB
499 if (dev->USE_ISO)
500 buf = dev->video_mode.isoc_ctl.buf;
501 else
502 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 503
84b5dbf3
MCC
504 /* Remember the field number for next time */
505 dma_q->current_field = field_number;
e0d3bafd 506
84b5dbf3
MCC
507 bytes_to_copy = dma_q->bytes_left_in_line;
508 if (bytes_to_copy > length)
509 bytes_to_copy = length;
e0d3bafd 510
84b5dbf3
MCC
511 if (dma_q->lines_completed >= dma_q->lines_per_field) {
512 dma_q->bytes_left_in_line -= bytes_to_copy;
cde4362f
MCC
513 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
514 0 : 1;
84b5dbf3
MCC
515 return 0;
516 }
e0d3bafd 517
84b5dbf3 518 dma_q->is_partial_line = 1;
e0d3bafd 519
84b5dbf3
MCC
520 /* If we don't have a buffer, just return the number of bytes we would
521 have copied if we had a buffer. */
522 if (!buf) {
523 dma_q->bytes_left_in_line -= bytes_to_copy;
cde4362f
MCC
524 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
525 ? 0 : 1;
84b5dbf3
MCC
526 return bytes_to_copy;
527 }
e0d3bafd 528
84b5dbf3
MCC
529 /* copy the data to video buffer */
530 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
e0d3bafd 531
84b5dbf3
MCC
532 dma_q->pos += bytes_to_copy;
533 dma_q->bytes_left_in_line -= bytes_to_copy;
e0d3bafd 534
84b5dbf3 535 if (dma_q->bytes_left_in_line == 0) {
84b5dbf3
MCC
536 dma_q->bytes_left_in_line = _line_size;
537 dma_q->lines_completed++;
538 dma_q->is_partial_line = 0;
e0d3bafd 539
84b5dbf3 540 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
84b5dbf3 541 buffer_filled(dev, dma_q, buf);
e0d3bafd 542
84b5dbf3
MCC
543 dma_q->pos = 0;
544 buf = NULL;
545 dma_q->lines_completed = 0;
546 }
547 }
e0d3bafd 548
84b5dbf3 549 return bytes_to_copy;
e0d3bafd
SD
550}
551
84b5dbf3
MCC
552void cx231xx_reset_video_buffer(struct cx231xx *dev,
553 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 554{
84b5dbf3 555 struct cx231xx_buffer *buf;
e0d3bafd 556
84b5dbf3
MCC
557 /* handle the switch from field 1 to field 2 */
558 if (dma_q->current_field == 1) {
cde4362f 559 if (dma_q->lines_completed >= dma_q->lines_per_field)
84b5dbf3 560 dma_q->field1_done = 1;
cde4362f 561 else
84b5dbf3 562 dma_q->field1_done = 0;
84b5dbf3 563 }
e0d3bafd 564
64fbf444
PB
565 if (dev->USE_ISO)
566 buf = dev->video_mode.isoc_ctl.buf;
567 else
568 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 569
84b5dbf3 570 if (buf == NULL) {
84b5dbf3
MCC
571 /* first try to get the buffer */
572 get_next_buf(dma_q, &buf);
e0d3bafd 573
84b5dbf3
MCC
574 dma_q->pos = 0;
575 dma_q->field1_done = 0;
576 dma_q->current_field = -1;
577 }
e0d3bafd 578
84b5dbf3
MCC
579 /* reset the counters */
580 dma_q->bytes_left_in_line = dev->width << 1;
581 dma_q->lines_completed = 0;
e0d3bafd
SD
582}
583
584int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 585 u8 *p_buffer, u32 bytes_to_copy)
e0d3bafd 586{
84b5dbf3
MCC
587 u8 *p_out_buffer = NULL;
588 u32 current_line_bytes_copied = 0;
589 struct cx231xx_buffer *buf;
590 u32 _line_size = dev->width << 1;
591 void *startwrite;
592 int offset, lencopy;
e0d3bafd 593
64fbf444
PB
594 if (dev->USE_ISO)
595 buf = dev->video_mode.isoc_ctl.buf;
596 else
597 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 598
84b5dbf3
MCC
599 if (buf == NULL)
600 return -1;
e0d3bafd
SD
601
602 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
603
84b5dbf3 604 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
e0d3bafd 605
84b5dbf3
MCC
606 /* Offset field 2 one line from the top of the buffer */
607 offset = (dma_q->current_field == 1) ? 0 : _line_size;
e0d3bafd 608
84b5dbf3
MCC
609 /* Offset for field 2 */
610 startwrite = p_out_buffer + offset;
e0d3bafd 611
84b5dbf3
MCC
612 /* lines already completed in the current field */
613 startwrite += (dma_q->lines_completed * _line_size * 2);
e0d3bafd 614
84b5dbf3
MCC
615 /* bytes already completed in the current line */
616 startwrite += current_line_bytes_copied;
e0d3bafd 617
cde4362f
MCC
618 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
619 bytes_to_copy : dma_q->bytes_left_in_line;
e0d3bafd 620
cde4362f 621 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
84b5dbf3 622 return 0;
e0d3bafd 623
84b5dbf3
MCC
624 /* The below copies the UYVY data straight into video buffer */
625 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
e0d3bafd 626
84b5dbf3 627 return 0;
e0d3bafd
SD
628}
629
cde4362f 630void cx231xx_swab(u16 *from, u16 *to, u16 len)
e0d3bafd 631{
84b5dbf3 632 u16 i;
e0d3bafd 633
84b5dbf3
MCC
634 if (len <= 0)
635 return;
e0d3bafd 636
cde4362f 637 for (i = 0; i < len / 2; i++)
84b5dbf3 638 to[i] = (from[i] << 8) | (from[i] >> 8);
e0d3bafd
SD
639}
640
84b5dbf3 641u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
e0d3bafd 642{
84b5dbf3 643 u8 buffer_complete = 0;
e0d3bafd 644
84b5dbf3 645 /* Dual field stream */
cde4362f
MCC
646 buffer_complete = ((dma_q->current_field == 2) &&
647 (dma_q->lines_completed >= dma_q->lines_per_field) &&
648 dma_q->field1_done);
e0d3bafd 649
84b5dbf3 650 return buffer_complete;
e0d3bafd
SD
651}
652
e0d3bafd
SD
653/* ------------------------------------------------------------------
654 Videobuf operations
655 ------------------------------------------------------------------*/
656
657static int
658buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
659{
660 struct cx231xx_fh *fh = vq->priv_data;
84b5dbf3 661 struct cx231xx *dev = fh->dev;
e0d3bafd 662
cde4362f 663 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
e0d3bafd
SD
664 if (0 == *count)
665 *count = CX231XX_DEF_BUF;
666
667 if (*count < CX231XX_MIN_BUF)
668 *count = CX231XX_MIN_BUF;
669
e0d3bafd
SD
670 return 0;
671}
672
673/* This is called *without* dev->slock held; please keep it that way */
674static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
675{
84b5dbf3
MCC
676 struct cx231xx_fh *fh = vq->priv_data;
677 struct cx231xx *dev = fh->dev;
e0d3bafd 678 unsigned long flags = 0;
cde4362f 679
e0d3bafd
SD
680 if (in_interrupt())
681 BUG();
682
683 /* We used to wait for the buffer to finish here, but this didn't work
684 because, as we were keeping the state as VIDEOBUF_QUEUED,
685 videobuf_queue_cancel marked it as finished for us.
686 (Also, it could wedge forever if the hardware was misconfigured.)
687
688 This should be safe; by the time we get here, the buffer isn't
689 queued anymore. If we ever start marking the buffers as
690 VIDEOBUF_ACTIVE, it won't be, though.
84b5dbf3 691 */
e0d3bafd 692 spin_lock_irqsave(&dev->video_mode.slock, flags);
64fbf444
PB
693 if (dev->USE_ISO) {
694 if (dev->video_mode.isoc_ctl.buf == buf)
695 dev->video_mode.isoc_ctl.buf = NULL;
696 } else {
697 if (dev->video_mode.bulk_ctl.buf == buf)
698 dev->video_mode.bulk_ctl.buf = NULL;
699 }
e0d3bafd
SD
700 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
701
702 videobuf_vmalloc_free(&buf->vb);
703 buf->vb.state = VIDEOBUF_NEEDS_INIT;
704}
705
706static int
707buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
84b5dbf3 708 enum v4l2_field field)
e0d3bafd 709{
84b5dbf3
MCC
710 struct cx231xx_fh *fh = vq->priv_data;
711 struct cx231xx_buffer *buf =
712 container_of(vb, struct cx231xx_buffer, vb);
713 struct cx231xx *dev = fh->dev;
714 int rc = 0, urb_init = 0;
e0d3bafd
SD
715
716 /* The only currently supported format is 16 bits/pixel */
cde4362f
MCC
717 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
718 + 7) >> 3;
84b5dbf3 719 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
e0d3bafd
SD
720 return -EINVAL;
721
84b5dbf3 722 buf->vb.width = dev->width;
e0d3bafd 723 buf->vb.height = dev->height;
84b5dbf3 724 buf->vb.field = field;
e0d3bafd
SD
725
726 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
727 rc = videobuf_iolock(vq, &buf->vb, NULL);
728 if (rc < 0)
729 goto fail;
730 }
731
64fbf444
PB
732 if (dev->USE_ISO) {
733 if (!dev->video_mode.isoc_ctl.num_bufs)
734 urb_init = 1;
735 } else {
736 if (!dev->video_mode.bulk_ctl.num_bufs)
737 urb_init = 1;
738 }
b7085c08
MCC
739 dev_dbg(&dev->udev->dev,
740 "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
b7085c08 812 dev_dbg(&dev->udev->dev, "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) {
b7085c08 864 dev_err(&dev->udev->dev, "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)) {
b7085c08 956 dev_err(&dev->udev->dev, "%s: queue busy\n", __func__);
643800d5 957 return -EBUSY;
e0d3bafd
SD
958 }
959
960 if (dev->stream_on && !fh->stream_on) {
b7085c08
MCC
961 dev_err(&dev->udev->dev,
962 "%s: device in use by another fh\n", __func__);
643800d5 963 return -EBUSY;
e0d3bafd
SD
964 }
965
966 /* set new image size */
967 dev->width = f->fmt.pix.width;
968 dev->height = f->fmt.pix.height;
84b5dbf3 969 dev->format = fmt;
e0d3bafd 970
112cb4a8
HV
971 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
972 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
973 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
e0d3bafd 974
e0d3bafd
SD
975 return rc;
976}
977
64fbf444 978static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
e0d3bafd 979{
84b5dbf3
MCC
980 struct cx231xx_fh *fh = priv;
981 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
982
983 *id = dev->norm;
984 return 0;
985}
986
314527ac 987static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
e0d3bafd 988{
84b5dbf3
MCC
989 struct cx231xx_fh *fh = priv;
990 struct cx231xx *dev = fh->dev;
435b4f78 991 struct v4l2_mbus_framefmt mbus_fmt;
84b5dbf3 992 int rc;
e0d3bafd
SD
993
994 rc = check_dev(dev);
995 if (rc < 0)
996 return rc;
997
314527ac 998 if (dev->norm == norm)
d61072a4
HV
999 return 0;
1000
1001 if (videobuf_queue_is_busy(&fh->vb_vidq))
1002 return -EBUSY;
e0d3bafd 1003
314527ac 1004 dev->norm = norm;
e0d3bafd 1005
e0d3bafd 1006 /* Adjusts width/height, if needed */
d61072a4
HV
1007 dev->width = 720;
1008 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
e0d3bafd 1009
8774bed9 1010 call_all(dev, video, s_std, dev->norm);
435b4f78
DH
1011
1012 /* We need to reset basic properties in the decoder related to
1013 resolution (since a standard change effects things like the number
1014 of lines in VACT, etc) */
d61072a4
HV
1015 memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1016 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1017 mbus_fmt.width = dev->width;
1018 mbus_fmt.height = dev->height;
435b4f78 1019 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
e0d3bafd 1020
84b5dbf3
MCC
1021 /* do mode control overrides */
1022 cx231xx_do_mode_ctrl_overrides(dev);
e0d3bafd
SD
1023
1024 return 0;
1025}
1026
1027static const char *iname[] = {
1028 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
cde4362f 1029 [CX231XX_VMUX_SVIDEO] = "S-Video",
e0d3bafd 1030 [CX231XX_VMUX_TELEVISION] = "Television",
cde4362f
MCC
1031 [CX231XX_VMUX_CABLE] = "Cable TV",
1032 [CX231XX_VMUX_DVB] = "DVB",
1033 [CX231XX_VMUX_DEBUG] = "for debug only",
e0d3bafd
SD
1034};
1035
b86d1544 1036int cx231xx_enum_input(struct file *file, void *priv,
84b5dbf3 1037 struct v4l2_input *i)
e0d3bafd 1038{
84b5dbf3
MCC
1039 struct cx231xx_fh *fh = priv;
1040 struct cx231xx *dev = fh->dev;
de99d532
DH
1041 u32 gen_stat;
1042 unsigned int ret, n;
e0d3bafd
SD
1043
1044 n = i->index;
1045 if (n >= MAX_CX231XX_INPUT)
1046 return -EINVAL;
1047 if (0 == INPUT(n)->type)
1048 return -EINVAL;
1049
1050 i->index = n;
1051 i->type = V4L2_INPUT_TYPE_CAMERA;
1052
1053 strcpy(i->name, iname[INPUT(n)->type]);
1054
1055 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
84b5dbf3 1056 (CX231XX_VMUX_CABLE == INPUT(n)->type))
e0d3bafd
SD
1057 i->type = V4L2_INPUT_TYPE_TUNER;
1058
1059 i->std = dev->vdev->tvnorms;
1060
de99d532
DH
1061 /* If they are asking about the active input, read signal status */
1062 if (n == dev->video_input) {
1063 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1064 GEN_STAT, 2, &gen_stat, 4);
1065 if (ret > 0) {
1066 if ((gen_stat & FLD_VPRES) == 0x00)
1067 i->status |= V4L2_IN_ST_NO_SIGNAL;
1068 if ((gen_stat & FLD_HLOCK) == 0x00)
1069 i->status |= V4L2_IN_ST_NO_H_LOCK;
1070 }
1071 }
1072
e0d3bafd
SD
1073 return 0;
1074}
1075
b86d1544 1076int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
e0d3bafd 1077{
84b5dbf3
MCC
1078 struct cx231xx_fh *fh = priv;
1079 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1080
1081 *i = dev->video_input;
1082
1083 return 0;
1084}
1085
b86d1544 1086int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
e0d3bafd 1087{
84b5dbf3
MCC
1088 struct cx231xx_fh *fh = priv;
1089 struct cx231xx *dev = fh->dev;
1090 int rc;
e0d3bafd 1091
64fbf444 1092 dev->mode_tv = 0;
e0d3bafd
SD
1093 rc = check_dev(dev);
1094 if (rc < 0)
1095 return rc;
1096
1097 if (i >= MAX_CX231XX_INPUT)
1098 return -EINVAL;
1099 if (0 == INPUT(i)->type)
1100 return -EINVAL;
1101
e0d3bafd
SD
1102 video_mux(dev, i);
1103
c09d6695
DH
1104 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1105 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1106 /* There's a tuner, so reset the standard and put it on the
1107 last known frequency (since it was probably powered down
1108 until now */
8774bed9 1109 call_all(dev, video, s_std, dev->norm);
c09d6695
DH
1110 }
1111
e0d3bafd
SD
1112 return 0;
1113}
1114
b86d1544 1115int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd 1116{
84b5dbf3
MCC
1117 struct cx231xx_fh *fh = priv;
1118 struct cx231xx *dev = fh->dev;
1119 int rc;
e0d3bafd
SD
1120
1121 rc = check_dev(dev);
1122 if (rc < 0)
1123 return rc;
1124
1125 if (0 != t->index)
1126 return -EINVAL;
1127
1128 strcpy(t->name, "Tuner");
1129
84b5dbf3 1130 t->type = V4L2_TUNER_ANALOG_TV;
e0d3bafd 1131 t->capability = V4L2_TUNER_CAP_NORM;
84b5dbf3
MCC
1132 t->rangehigh = 0xffffffffUL;
1133 t->signal = 0xffff; /* LOCKED */
b251f957 1134 call_all(dev, tuner, g_tuner, t);
e0d3bafd
SD
1135
1136 return 0;
1137}
1138
2f73c7c5 1139int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd 1140{
84b5dbf3
MCC
1141 struct cx231xx_fh *fh = priv;
1142 struct cx231xx *dev = fh->dev;
1143 int rc;
e0d3bafd
SD
1144
1145 rc = check_dev(dev);
1146 if (rc < 0)
1147 return rc;
1148
1149 if (0 != t->index)
1150 return -EINVAL;
1151#if 0
b1196126 1152 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1153#endif
1154 return 0;
1155}
1156
b86d1544 1157int cx231xx_g_frequency(struct file *file, void *priv,
84b5dbf3 1158 struct v4l2_frequency *f)
e0d3bafd 1159{
84b5dbf3
MCC
1160 struct cx231xx_fh *fh = priv;
1161 struct cx231xx *dev = fh->dev;
e0d3bafd 1162
b251f957
HV
1163 if (f->tuner)
1164 return -EINVAL;
1165
e0d3bafd
SD
1166 f->frequency = dev->ctl_freq;
1167
e0d3bafd
SD
1168 return 0;
1169}
1170
b86d1544 1171int cx231xx_s_frequency(struct file *file, void *priv,
b530a447 1172 const struct v4l2_frequency *f)
e0d3bafd 1173{
84b5dbf3
MCC
1174 struct cx231xx_fh *fh = priv;
1175 struct cx231xx *dev = fh->dev;
b530a447 1176 struct v4l2_frequency new_freq = *f;
84b5dbf3 1177 int rc;
64fbf444
PB
1178 u32 if_frequency = 5400000;
1179
b7085c08
MCC
1180 dev_dbg(&dev->udev->dev,
1181 "Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1182 f->frequency, f->type);
e0d3bafd
SD
1183
1184 rc = check_dev(dev);
1185 if (rc < 0)
1186 return rc;
1187
1188 if (0 != f->tuner)
1189 return -EINVAL;
1190
84b5dbf3
MCC
1191 /* set pre channel change settings in DIF first */
1192 rc = cx231xx_tuner_pre_channel_change(dev);
e0d3bafd 1193
64fbf444 1194 call_all(dev, tuner, s_frequency, f);
b530a447
HV
1195 call_all(dev, tuner, g_frequency, &new_freq);
1196 dev->ctl_freq = new_freq.frequency;
e0d3bafd 1197
84b5dbf3
MCC
1198 /* set post channel change settings in DIF first */
1199 rc = cx231xx_tuner_post_channel_change(dev);
e0d3bafd 1200
64fbf444
PB
1201 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1202 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1203 if_frequency = 5400000; /*5.4MHz */
1204 else if (dev->norm & V4L2_STD_B)
1205 if_frequency = 6000000; /*6.0MHz */
1206 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1207 if_frequency = 6900000; /*6.9MHz */
1208 else if (dev->norm & V4L2_STD_GH)
1209 if_frequency = 7100000; /*7.1MHz */
1210 else if (dev->norm & V4L2_STD_PAL_I)
1211 if_frequency = 7250000; /*7.25MHz */
1212 else if (dev->norm & V4L2_STD_SECAM_L)
1213 if_frequency = 6900000; /*6.9MHz */
1214 else if (dev->norm & V4L2_STD_SECAM_LC)
1215 if_frequency = 1250000; /*1.25MHz */
1216
b7085c08
MCC
1217 dev_dbg(&dev->udev->dev,
1218 "if_frequency is set to %d\n", if_frequency);
64fbf444
PB
1219 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1220
1221 update_HH_register_after_set_DIF(dev);
1222 }
1223
b7085c08 1224 dev_dbg(&dev->udev->dev, "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) {
b7085c08
MCC
1528 dev_err(&dev->udev->dev,
1529 "%s device in use by another fh\n", __func__);
6264722c
HV
1530 return -EBUSY;
1531 }
1532 return vidioc_try_fmt_vbi_cap(file, priv, f);
1533}
1534
e0d3bafd
SD
1535static int vidioc_reqbufs(struct file *file, void *priv,
1536 struct v4l2_requestbuffers *rb)
1537{
84b5dbf3
MCC
1538 struct cx231xx_fh *fh = priv;
1539 struct cx231xx *dev = fh->dev;
1540 int rc;
e0d3bafd
SD
1541
1542 rc = check_dev(dev);
1543 if (rc < 0)
1544 return rc;
1545
cde4362f 1546 return videobuf_reqbufs(&fh->vb_vidq, rb);
e0d3bafd
SD
1547}
1548
84b5dbf3 1549static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
e0d3bafd 1550{
84b5dbf3
MCC
1551 struct cx231xx_fh *fh = priv;
1552 struct cx231xx *dev = fh->dev;
1553 int rc;
e0d3bafd
SD
1554
1555 rc = check_dev(dev);
1556 if (rc < 0)
1557 return rc;
1558
cde4362f 1559 return videobuf_querybuf(&fh->vb_vidq, b);
e0d3bafd
SD
1560}
1561
1562static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1563{
84b5dbf3
MCC
1564 struct cx231xx_fh *fh = priv;
1565 struct cx231xx *dev = fh->dev;
1566 int rc;
e0d3bafd
SD
1567
1568 rc = check_dev(dev);
1569 if (rc < 0)
1570 return rc;
1571
cde4362f 1572 return videobuf_qbuf(&fh->vb_vidq, b);
e0d3bafd
SD
1573}
1574
1575static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1576{
84b5dbf3
MCC
1577 struct cx231xx_fh *fh = priv;
1578 struct cx231xx *dev = fh->dev;
1579 int rc;
e0d3bafd
SD
1580
1581 rc = check_dev(dev);
1582 if (rc < 0)
1583 return rc;
1584
cde4362f 1585 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
e0d3bafd
SD
1586}
1587
e0d3bafd
SD
1588/* ----------------------------------------------------------- */
1589/* RADIO ESPECIFIC IOCTLS */
1590/* ----------------------------------------------------------- */
1591
84b5dbf3 1592static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd
SD
1593{
1594 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1595
530e01e7 1596 if (t->index)
e0d3bafd
SD
1597 return -EINVAL;
1598
1599 strcpy(t->name, "Radio");
e0d3bafd 1600
530e01e7 1601 call_all(dev, tuner, g_tuner, t);
e0d3bafd 1602
e0d3bafd
SD
1603 return 0;
1604}
2f73c7c5 1605static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd
SD
1606{
1607 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1608
b86d1544 1609 if (t->index)
e0d3bafd
SD
1610 return -EINVAL;
1611
b1196126 1612 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1613
1614 return 0;
1615}
1616
e0d3bafd
SD
1617/*
1618 * cx231xx_v4l2_open()
1619 * inits the device and starts isoc transfer
1620 */
1621static int cx231xx_v4l2_open(struct file *filp)
1622{
4df16f70 1623 int radio = 0;
63b0d5ad
LP
1624 struct video_device *vdev = video_devdata(filp);
1625 struct cx231xx *dev = video_drvdata(filp);
e0d3bafd
SD
1626 struct cx231xx_fh *fh;
1627 enum v4l2_buf_type fh_type = 0;
1628
63b0d5ad
LP
1629 switch (vdev->vfl_type) {
1630 case VFL_TYPE_GRABBER:
1631 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1632 break;
1633 case VFL_TYPE_VBI:
1634 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1635 break;
1636 case VFL_TYPE_RADIO:
1637 radio = 1;
1638 break;
1639 }
e0d3bafd 1640
50462eb0
LP
1641 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1642 video_device_node_name(vdev), v4l2_type_names[fh_type],
1643 dev->users);
e0d3bafd
SD
1644
1645#if 0
1646 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1647 if (errCode < 0) {
b7085c08
MCC
1648 dev_err(&dev->udev->dev,
1649 "Device locked on digital mode. Can't open analog\n");
e0d3bafd
SD
1650 return -EBUSY;
1651 }
1652#endif
1653
1654 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
b7085c08 1655 if (!fh)
e0d3bafd 1656 return -ENOMEM;
1f7e073d
HV
1657 if (mutex_lock_interruptible(&dev->lock)) {
1658 kfree(fh);
1659 return -ERESTARTSYS;
1660 }
e0d3bafd 1661 fh->dev = dev;
e0d3bafd
SD
1662 fh->type = fh_type;
1663 filp->private_data = fh;
1d08a4fa 1664 v4l2_fh_init(&fh->fh, vdev);
e0d3bafd
SD
1665
1666 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
84b5dbf3 1667 /* Power up in Analog TV mode */
2f861387 1668 if (dev->board.external_av)
64fbf444
PB
1669 cx231xx_set_power_mode(dev,
1670 POLARIS_AVMODE_ENXTERNAL_AV);
1671 else
1672 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
e0d3bafd
SD
1673
1674#if 0
1675 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1676#endif
e0d3bafd 1677
84b5dbf3
MCC
1678 /* set video alternate setting */
1679 cx231xx_set_video_alternate(dev);
e0d3bafd
SD
1680
1681 /* Needed, since GPIO might have disabled power of
1682 some i2c device */
1683 cx231xx_config_i2c(dev);
1684
1685 /* device needs to be initialized before isoc transfer */
84b5dbf3 1686 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
e0d3bafd
SD
1687
1688 }
71590765 1689 if (radio) {
e0d3bafd
SD
1690 cx231xx_videodbg("video_open: setting radio device\n");
1691
1692 /* cx231xx_start_radio(dev); */
1693
b1196126 1694 call_all(dev, tuner, s_radio);
e0d3bafd
SD
1695 }
1696
1697 dev->users++;
1698
cde4362f
MCC
1699 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1700 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1701 NULL, &dev->video_mode.slock,
1702 fh->type, V4L2_FIELD_INTERLACED,
08bff03e 1703 sizeof(struct cx231xx_buffer),
643800d5 1704 fh, &dev->lock);
84b5dbf3 1705 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
cde4362f
MCC
1706 /* Set the required alternate setting VBI interface works in
1707 Bulk mode only */
2f861387 1708 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
e0d3bafd 1709
cde4362f
MCC
1710 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1711 NULL, &dev->vbi_mode.slock,
1712 fh->type, V4L2_FIELD_SEQ_TB,
08bff03e 1713 sizeof(struct cx231xx_buffer),
643800d5 1714 fh, &dev->lock);
84b5dbf3 1715 }
1f7e073d 1716 mutex_unlock(&dev->lock);
1d08a4fa 1717 v4l2_fh_add(&fh->fh);
e0d3bafd 1718
4df16f70 1719 return 0;
e0d3bafd
SD
1720}
1721
1722/*
1723 * cx231xx_realease_resources()
1724 * unregisters the v4l2,i2c and usb devices
1725 * called when the device gets disconected or at module unload
1726*/
1727void cx231xx_release_analog_resources(struct cx231xx *dev)
1728{
1729
1730 /*FIXME: I2C IR should be disconnected */
1731
1732 if (dev->radio_dev) {
f0813b4c 1733 if (video_is_registered(dev->radio_dev))
e0d3bafd
SD
1734 video_unregister_device(dev->radio_dev);
1735 else
1736 video_device_release(dev->radio_dev);
1737 dev->radio_dev = NULL;
1738 }
1739 if (dev->vbi_dev) {
b7085c08 1740 dev_info(&dev->udev->dev, "V4L2 device %s deregistered\n",
ed0e3729 1741 video_device_node_name(dev->vbi_dev));
f0813b4c 1742 if (video_is_registered(dev->vbi_dev))
e0d3bafd
SD
1743 video_unregister_device(dev->vbi_dev);
1744 else
1745 video_device_release(dev->vbi_dev);
1746 dev->vbi_dev = NULL;
1747 }
1748 if (dev->vdev) {
b7085c08 1749 dev_info(&dev->udev->dev, "V4L2 device %s deregistered\n",
ed0e3729 1750 video_device_node_name(dev->vdev));
64fbf444 1751
2f861387 1752 if (dev->board.has_417)
64fbf444
PB
1753 cx231xx_417_unregister(dev);
1754
f0813b4c 1755 if (video_is_registered(dev->vdev))
e0d3bafd
SD
1756 video_unregister_device(dev->vdev);
1757 else
1758 video_device_release(dev->vdev);
1759 dev->vdev = NULL;
1760 }
d2370f8e
HV
1761 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1762 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
e0d3bafd
SD
1763}
1764
1765/*
1f7e073d 1766 * cx231xx_close()
e0d3bafd
SD
1767 * stops streaming and deallocates all resources allocated by the v4l2
1768 * calls and ioctls
1769 */
1f7e073d 1770static int cx231xx_close(struct file *filp)
e0d3bafd 1771{
84b5dbf3
MCC
1772 struct cx231xx_fh *fh = filp->private_data;
1773 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1774
1775 cx231xx_videodbg("users=%d\n", dev->users);
1776
64fbf444 1777 cx231xx_videodbg("users=%d\n", dev->users);
e0d3bafd
SD
1778 if (res_check(fh))
1779 res_free(fh);
1780
2f861387
MCC
1781 /*
1782 * To workaround error number=-71 on EP0 for VideoGrabber,
1783 * need exclude following.
1784 * FIXME: It is probably safe to remove most of these, as we're
1785 * now avoiding the alternate setting for INDEX_VANC
1786 */
1787 if (!dev->board.no_alt_vanc)
64fbf444
PB
1788 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1789 videobuf_stop(&fh->vb_vidq);
1790 videobuf_mmap_free(&fh->vb_vidq);
1791
1792 /* the device is already disconnect,
1793 free the remaining resources */
1794 if (dev->state & DEV_DISCONNECTED) {
1795 if (atomic_read(&dev->devlist_count) > 0) {
1796 cx231xx_release_resources(dev);
266e8ae3 1797 fh->dev = NULL;
64fbf444
PB
1798 return 0;
1799 }
64fbf444
PB
1800 return 0;
1801 }
84b5dbf3 1802
64fbf444
PB
1803 /* do this before setting alternate! */
1804 cx231xx_uninit_vbi_isoc(dev);
1805
1806 /* set alternate 0 */
1807 if (!dev->vbi_or_sliced_cc_mode)
1808 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1809 else
1810 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1811
1d08a4fa
HV
1812 v4l2_fh_del(&fh->fh);
1813 v4l2_fh_exit(&fh->fh);
64fbf444
PB
1814 kfree(fh);
1815 dev->users--;
1816 wake_up_interruptible_nr(&dev->open, 1);
84b5dbf3
MCC
1817 return 0;
1818 }
1819
1d08a4fa 1820 v4l2_fh_del(&fh->fh);
990862a2
MCC
1821 dev->users--;
1822 if (!dev->users) {
e0d3bafd
SD
1823 videobuf_stop(&fh->vb_vidq);
1824 videobuf_mmap_free(&fh->vb_vidq);
1825
1826 /* the device is already disconnect,
1827 free the remaining resources */
1828 if (dev->state & DEV_DISCONNECTED) {
1829 cx231xx_release_resources(dev);
266e8ae3 1830 fh->dev = NULL;
e0d3bafd
SD
1831 return 0;
1832 }
1833
84b5dbf3 1834 /* Save some power by putting tuner to sleep */
622b828a 1835 call_all(dev, core, s_power, 0);
e0d3bafd
SD
1836
1837 /* do this before setting alternate! */
64fbf444
PB
1838 if (dev->USE_ISO)
1839 cx231xx_uninit_isoc(dev);
1840 else
1841 cx231xx_uninit_bulk(dev);
e0d3bafd
SD
1842 cx231xx_set_mode(dev, CX231XX_SUSPEND);
1843
1844 /* set alternate 0 */
1845 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1846 }
1d08a4fa 1847 v4l2_fh_exit(&fh->fh);
e0d3bafd 1848 kfree(fh);
e0d3bafd 1849 wake_up_interruptible_nr(&dev->open, 1);
e0d3bafd
SD
1850 return 0;
1851}
1852
1f7e073d
HV
1853static int cx231xx_v4l2_close(struct file *filp)
1854{
1855 struct cx231xx_fh *fh = filp->private_data;
1856 struct cx231xx *dev = fh->dev;
1857 int rc;
1858
1859 mutex_lock(&dev->lock);
1860 rc = cx231xx_close(filp);
1861 mutex_unlock(&dev->lock);
1862 return rc;
1863}
1864
e0d3bafd
SD
1865/*
1866 * cx231xx_v4l2_read()
1867 * will allocate buffers when called for the first time
1868 */
1869static ssize_t
cde4362f
MCC
1870cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1871 loff_t *pos)
e0d3bafd
SD
1872{
1873 struct cx231xx_fh *fh = filp->private_data;
1874 struct cx231xx *dev = fh->dev;
1875 int rc;
1876
1877 rc = check_dev(dev);
1878 if (rc < 0)
1879 return rc;
1880
84b5dbf3
MCC
1881 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1882 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
e0d3bafd 1883 rc = res_get(fh);
e0d3bafd
SD
1884
1885 if (unlikely(rc < 0))
1886 return rc;
1887
1f7e073d
HV
1888 if (mutex_lock_interruptible(&dev->lock))
1889 return -ERESTARTSYS;
1890 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
84b5dbf3 1891 filp->f_flags & O_NONBLOCK);
1f7e073d
HV
1892 mutex_unlock(&dev->lock);
1893 return rc;
e0d3bafd
SD
1894 }
1895 return 0;
1896}
1897
1898/*
1899 * cx231xx_v4l2_poll()
1900 * will allocate buffers when called for the first time
1901 */
64fbf444 1902static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
e0d3bafd 1903{
1d08a4fa 1904 unsigned long req_events = poll_requested_events(wait);
e0d3bafd
SD
1905 struct cx231xx_fh *fh = filp->private_data;
1906 struct cx231xx *dev = fh->dev;
1d08a4fa 1907 unsigned res = 0;
e0d3bafd
SD
1908 int rc;
1909
1910 rc = check_dev(dev);
1911 if (rc < 0)
1d08a4fa 1912 return POLLERR;
e0d3bafd 1913
e0d3bafd 1914 rc = res_get(fh);
e0d3bafd
SD
1915
1916 if (unlikely(rc < 0))
1917 return POLLERR;
1918
1d08a4fa
HV
1919 if (v4l2_event_pending(&fh->fh))
1920 res |= POLLPRI;
1921 else
1922 poll_wait(filp, &fh->fh.wait, wait);
1923
1924 if (!(req_events & (POLLIN | POLLRDNORM)))
1925 return res;
1926
84b5dbf3 1927 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
1f7e073d 1928 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
1f7e073d 1929 mutex_lock(&dev->lock);
1d08a4fa 1930 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1f7e073d
HV
1931 mutex_unlock(&dev->lock);
1932 return res;
1933 }
1d08a4fa 1934 return res | POLLERR;
e0d3bafd
SD
1935}
1936
1937/*
1938 * cx231xx_v4l2_mmap()
1939 */
1940static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1941{
84b5dbf3
MCC
1942 struct cx231xx_fh *fh = filp->private_data;
1943 struct cx231xx *dev = fh->dev;
1944 int rc;
e0d3bafd
SD
1945
1946 rc = check_dev(dev);
1947 if (rc < 0)
1948 return rc;
1949
e0d3bafd 1950 rc = res_get(fh);
e0d3bafd
SD
1951
1952 if (unlikely(rc < 0))
1953 return rc;
1954
1f7e073d
HV
1955 if (mutex_lock_interruptible(&dev->lock))
1956 return -ERESTARTSYS;
e0d3bafd 1957 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1f7e073d 1958 mutex_unlock(&dev->lock);
e0d3bafd
SD
1959
1960 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
84b5dbf3
MCC
1961 (unsigned long)vma->vm_start,
1962 (unsigned long)vma->vm_end -
1963 (unsigned long)vma->vm_start, rc);
e0d3bafd
SD
1964
1965 return rc;
1966}
1967
1968static const struct v4l2_file_operations cx231xx_v4l_fops = {
cde4362f
MCC
1969 .owner = THIS_MODULE,
1970 .open = cx231xx_v4l2_open,
84b5dbf3 1971 .release = cx231xx_v4l2_close,
cde4362f
MCC
1972 .read = cx231xx_v4l2_read,
1973 .poll = cx231xx_v4l2_poll,
1974 .mmap = cx231xx_v4l2_mmap,
643800d5 1975 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
1976};
1977
1978static const struct v4l2_ioctl_ops video_ioctl_ops = {
bc08734c 1979 .vidioc_querycap = cx231xx_querycap,
cde4362f
MCC
1980 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1981 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1982 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1983 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1984 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1985 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
6264722c 1986 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
cde4362f 1987 .vidioc_cropcap = vidioc_cropcap,
cde4362f
MCC
1988 .vidioc_reqbufs = vidioc_reqbufs,
1989 .vidioc_querybuf = vidioc_querybuf,
1990 .vidioc_qbuf = vidioc_qbuf,
1991 .vidioc_dqbuf = vidioc_dqbuf,
1992 .vidioc_s_std = vidioc_s_std,
1993 .vidioc_g_std = vidioc_g_std,
b86d1544
HV
1994 .vidioc_enum_input = cx231xx_enum_input,
1995 .vidioc_g_input = cx231xx_g_input,
1996 .vidioc_s_input = cx231xx_s_input,
cde4362f
MCC
1997 .vidioc_streamon = vidioc_streamon,
1998 .vidioc_streamoff = vidioc_streamoff,
b86d1544
HV
1999 .vidioc_g_tuner = cx231xx_g_tuner,
2000 .vidioc_s_tuner = cx231xx_s_tuner,
2001 .vidioc_g_frequency = cx231xx_g_frequency,
2002 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2003#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2004 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2005 .vidioc_g_register = cx231xx_g_register,
2006 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2007#endif
1d08a4fa
HV
2008 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2009 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2010};
2011
2012static struct video_device cx231xx_vbi_template;
2013
2014static const struct video_device cx231xx_video_template = {
cde4362f
MCC
2015 .fops = &cx231xx_v4l_fops,
2016 .release = video_device_release,
2017 .ioctl_ops = &video_ioctl_ops,
cde4362f 2018 .tvnorms = V4L2_STD_ALL,
e0d3bafd
SD
2019};
2020
2021static const struct v4l2_file_operations radio_fops = {
cde4362f
MCC
2022 .owner = THIS_MODULE,
2023 .open = cx231xx_v4l2_open,
84b5dbf3 2024 .release = cx231xx_v4l2_close,
1d08a4fa 2025 .poll = v4l2_ctrl_poll,
1265f080 2026 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
2027};
2028
2029static const struct v4l2_ioctl_ops radio_ioctl_ops = {
bc08734c 2030 .vidioc_querycap = cx231xx_querycap,
cde4362f 2031 .vidioc_g_tuner = radio_g_tuner,
cde4362f 2032 .vidioc_s_tuner = radio_s_tuner,
b86d1544
HV
2033 .vidioc_g_frequency = cx231xx_g_frequency,
2034 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2035#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2036 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2037 .vidioc_g_register = cx231xx_g_register,
2038 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2039#endif
1d08a4fa
HV
2040 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2041 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2042};
2043
2044static struct video_device cx231xx_radio_template = {
cde4362f
MCC
2045 .name = "cx231xx-radio",
2046 .fops = &radio_fops,
84b5dbf3 2047 .ioctl_ops = &radio_ioctl_ops,
e0d3bafd
SD
2048};
2049
2050/******************************** usb interface ******************************/
2051
b9255176
SD
2052static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2053 const struct video_device
2054 *template, const char *type_name)
e0d3bafd
SD
2055{
2056 struct video_device *vfd;
2057
2058 vfd = video_device_alloc();
2059 if (NULL == vfd)
2060 return NULL;
cde4362f 2061
e0d3bafd 2062 *vfd = *template;
b1196126 2063 vfd->v4l2_dev = &dev->v4l2_dev;
e0d3bafd
SD
2064 vfd->release = video_device_release;
2065 vfd->debug = video_debug;
643800d5 2066 vfd->lock = &dev->lock;
e0d3bafd 2067
84b5dbf3 2068 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
e0d3bafd 2069
63b0d5ad 2070 video_set_drvdata(vfd, dev);
06c46003
HV
2071 if (dev->tuner_type == TUNER_ABSENT) {
2072 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2073 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2074 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2075 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2076 }
e0d3bafd
SD
2077 return vfd;
2078}
2079
2080int cx231xx_register_analog_devices(struct cx231xx *dev)
2081{
2082 int ret;
2083
b7085c08 2084 dev_info(&dev->udev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
e0d3bafd
SD
2085
2086 /* set default norm */
a25a7012 2087 dev->norm = V4L2_STD_PAL;
e0d3bafd
SD
2088 dev->width = norm_maxw(dev);
2089 dev->height = norm_maxh(dev);
2090 dev->interlaced = 0;
e0d3bafd
SD
2091
2092 /* Analog specific initialization */
2093 dev->format = &format[0];
6e6a2ba9
DH
2094
2095 /* Set the initial input */
2096 video_mux(dev, dev->video_input);
e0d3bafd 2097
8774bed9 2098 call_all(dev, video, s_std, dev->norm);
d61072a4 2099
d2370f8e
HV
2100 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2101 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2102
2103 if (dev->sd_cx25840) {
2104 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2105 dev->sd_cx25840->ctrl_handler, NULL);
2106 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2107 dev->sd_cx25840->ctrl_handler,
2108 v4l2_ctrl_radio_filter);
2109 }
2110
2111 if (dev->ctrl_handler.error)
2112 return dev->ctrl_handler.error;
2113 if (dev->radio_ctrl_handler.error)
2114 return dev->radio_ctrl_handler.error;
e0d3bafd
SD
2115
2116 /* enable vbi capturing */
84b5dbf3 2117 /* write code here... */
e0d3bafd
SD
2118
2119 /* allocate and fill video video_device struct */
2120 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2121 if (!dev->vdev) {
b7085c08 2122 dev_err(&dev->udev->dev, "cannot allocate video_device.\n");
e0d3bafd
SD
2123 return -ENODEV;
2124 }
2125
d2370f8e 2126 dev->vdev->ctrl_handler = &dev->ctrl_handler;
e0d3bafd
SD
2127 /* register v4l2 video video_device */
2128 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
84b5dbf3 2129 video_nr[dev->devno]);
e0d3bafd 2130 if (ret) {
b7085c08
MCC
2131 dev_err(&dev->udev->dev,
2132 "unable to register video device (error=%i).\n",
ed0e3729 2133 ret);
e0d3bafd
SD
2134 return ret;
2135 }
2136
b7085c08 2137 dev_info(&dev->udev->dev, "Registered video device %s [v4l2]\n",
ed0e3729 2138 video_device_node_name(dev->vdev));
e0d3bafd 2139
84b5dbf3 2140 /* Initialize VBI template */
3724dde9 2141 cx231xx_vbi_template = cx231xx_video_template;
84b5dbf3 2142 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
e0d3bafd
SD
2143
2144 /* Allocate and fill vbi video_device struct */
2145 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2146
d2370f8e 2147 if (!dev->vbi_dev) {
b7085c08 2148 dev_err(&dev->udev->dev, "cannot allocate video_device.\n");
d2370f8e
HV
2149 return -ENODEV;
2150 }
2151 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
e0d3bafd
SD
2152 /* register v4l2 vbi video_device */
2153 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
84b5dbf3 2154 vbi_nr[dev->devno]);
e0d3bafd 2155 if (ret < 0) {
b7085c08 2156 dev_err(&dev->udev->dev, "unable to register vbi device\n");
e0d3bafd
SD
2157 return ret;
2158 }
2159
b7085c08 2160 dev_info(&dev->udev->dev, "Registered VBI device %s\n",
ed0e3729 2161 video_device_node_name(dev->vbi_dev));
e0d3bafd
SD
2162
2163 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
cde4362f
MCC
2164 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2165 "radio");
e0d3bafd 2166 if (!dev->radio_dev) {
b7085c08
MCC
2167 dev_err(&dev->udev->dev,
2168 "cannot allocate video_device.\n");
e0d3bafd
SD
2169 return -ENODEV;
2170 }
d2370f8e 2171 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
e0d3bafd
SD
2172 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2173 radio_nr[dev->devno]);
2174 if (ret < 0) {
b7085c08
MCC
2175 dev_err(&dev->udev->dev,
2176 "can't register radio device\n");
e0d3bafd
SD
2177 return ret;
2178 }
b7085c08 2179 dev_info(&dev->udev->dev, "Registered radio device as %s\n",
ed0e3729 2180 video_device_node_name(dev->radio_dev));
e0d3bafd
SD
2181 }
2182
e0d3bafd
SD
2183 return 0;
2184}
This page took 0.610112 seconds and 5 git commands to generate.