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