blob: 18919e0f01948198c8ed37bc9f00bdeffedf4dc5 [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
2 cx231xx-video.c - driver for Conexant Cx23100/101/102 USB video capture devices
3
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03005 Based on em28xx driver
6 Based on cx23885 driver
7 Based on cx88 driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Sri Deevie0d3baf2009-03-03 14:37:50 -030024#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/bitmap.h>
29#include <linux/usb.h>
30#include <linux/i2c.h>
31#include <linux/version.h>
32#include <linux/mm.h>
33#include <linux/mutex.h>
34
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-chip-ident.h>
38#include <media/msp3400.h>
39#include <media/tuner.h>
40
41#include "dvb_frontend.h"
42
43#include "cx231xx.h"
44#include "cx231xx-vbi.h"
45
Sri Deevie0d3baf2009-03-03 14:37:50 -030046#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
47#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
48
Sri Deevie0d3baf2009-03-03 14:37:50 -030049#define cx231xx_videodbg(fmt, arg...) do {\
50 if (video_debug) \
51 printk(KERN_INFO "%s %s :"fmt, \
52 dev->name, __func__ , ##arg); } while (0)
53
54static unsigned int isoc_debug;
55module_param(isoc_debug, int, 0644);
56MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
57
58#define cx231xx_isocdbg(fmt, arg...) \
59do {\
60 if (isoc_debug) { \
61 printk(KERN_INFO "%s %s :"fmt, \
62 dev->name, __func__ , ##arg); \
63 } \
64 } while (0)
65
66MODULE_AUTHOR(DRIVER_AUTHOR);
67MODULE_DESCRIPTION(DRIVER_DESC);
68MODULE_LICENSE("GPL");
69
Sri Deevie0d3baf2009-03-03 14:37:50 -030070static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
71static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
72static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
73static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
74
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030075module_param_array(card, int, NULL, 0444);
Sri Deevie0d3baf2009-03-03 14:37:50 -030076module_param_array(video_nr, int, NULL, 0444);
77module_param_array(vbi_nr, int, NULL, 0444);
78module_param_array(radio_nr, int, NULL, 0444);
79
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030080MODULE_PARM_DESC(card, "card type");
Sri Deevie0d3baf2009-03-03 14:37:50 -030081MODULE_PARM_DESC(video_nr, "video device numbers");
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030082MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
Sri Deevie0d3baf2009-03-03 14:37:50 -030083MODULE_PARM_DESC(radio_nr, "radio device numbers");
84
85static unsigned int video_debug;
86module_param(video_debug, int, 0644);
87MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
88
Sri Deevie0d3baf2009-03-03 14:37:50 -030089/* supported video standards */
90static struct cx231xx_fmt format[] = {
91 {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030092 .name = "16bpp YUY2, 4:2:2, packed",
93 .fourcc = V4L2_PIX_FMT_YUYV,
94 .depth = 16,
95 .reg = 0,
96 },
Sri Deevie0d3baf2009-03-03 14:37:50 -030097};
98
Sri Deevie0d3baf2009-03-03 14:37:50 -030099/* supported controls */
100/* Common to all boards */
101
102/* ------------------------------------------------------------------- */
103
104static const struct v4l2_queryctrl no_ctl = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300105 .name = "42",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300106 .flags = V4L2_CTRL_FLAG_DISABLED,
107};
108
109static struct cx231xx_ctrl cx231xx_ctls[] = {
110 /* --- video --- */
111 {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300112 .v = {
113 .id = V4L2_CID_BRIGHTNESS,
114 .name = "Brightness",
115 .minimum = 0x00,
116 .maximum = 0xff,
117 .step = 1,
118 .default_value = 0x7f,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 },
121 .off = 128,
122 .reg = LUMA_CTRL,
123 .mask = 0x00ff,
124 .shift = 0,
125 }, {
126 .v = {
127 .id = V4L2_CID_CONTRAST,
128 .name = "Contrast",
129 .minimum = 0,
130 .maximum = 0xff,
131 .step = 1,
132 .default_value = 0x3f,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 },
135 .off = 0,
136 .reg = LUMA_CTRL,
137 .mask = 0xff00,
138 .shift = 8,
139 }, {
140 .v = {
141 .id = V4L2_CID_HUE,
142 .name = "Hue",
143 .minimum = 0,
144 .maximum = 0xff,
145 .step = 1,
146 .default_value = 0x7f,
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 },
149 .off = 128,
150 .reg = CHROMA_CTRL,
151 .mask = 0xff0000,
152 .shift = 16,
153 }, {
154 /* strictly, this only describes only U saturation.
155 * V saturation is handled specially through code.
156 */
157 .v = {
158 .id = V4L2_CID_SATURATION,
159 .name = "Saturation",
160 .minimum = 0,
161 .maximum = 0xff,
162 .step = 1,
163 .default_value = 0x7f,
164 .type = V4L2_CTRL_TYPE_INTEGER,
165 },
166 .off = 0,
167 .reg = CHROMA_CTRL,
168 .mask = 0x00ff,
169 .shift = 0,
170 }, {
171 /* --- audio --- */
172 .v = {
173 .id = V4L2_CID_AUDIO_MUTE,
174 .name = "Mute",
175 .minimum = 0,
176 .maximum = 1,
177 .default_value = 1,
178 .type = V4L2_CTRL_TYPE_BOOLEAN,
179 },
180 .reg = PATH1_CTL1,
181 .mask = (0x1f << 24),
182 .shift = 24,
183 }, {
184 .v = {
185 .id = V4L2_CID_AUDIO_VOLUME,
186 .name = "Volume",
187 .minimum = 0,
188 .maximum = 0x3f,
189 .step = 1,
190 .default_value = 0x3f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 },
193 .reg = PATH1_VOL_CTL,
194 .mask = 0xff,
195 .shift = 0,
196 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300197};
198static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
199
200static const u32 cx231xx_user_ctrls[] = {
201 V4L2_CID_USER_CLASS,
202 V4L2_CID_BRIGHTNESS,
203 V4L2_CID_CONTRAST,
204 V4L2_CID_SATURATION,
205 V4L2_CID_HUE,
206 V4L2_CID_AUDIO_VOLUME,
207#if 0
208 V4L2_CID_AUDIO_BALANCE,
209#endif
210 V4L2_CID_AUDIO_MUTE,
211 0
212};
213
214static const u32 *ctrl_classes[] = {
215 cx231xx_user_ctrls,
216 NULL
217};
218
Sri Deevie0d3baf2009-03-03 14:37:50 -0300219/* ------------------------------------------------------------------
220 Video buffer and parser functions
221 ------------------------------------------------------------------*/
222
223/*
224 * Announces that a buffer were filled and request the next
225 */
226static inline void buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300227 struct cx231xx_dmaqueue *dma_q,
228 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300229{
230 /* Advice that buffer was filled */
231 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
232 buf->vb.state = VIDEOBUF_DONE;
233 buf->vb.field_count++;
234 do_gettimeofday(&buf->vb.ts);
235
236 dev->video_mode.isoc_ctl.buf = NULL;
237
238 list_del(&buf->vb.queue);
239 wake_up(&buf->vb.done);
240}
241
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300242static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300243{
244 char *errmsg = "Unknown";
245
246 switch (status) {
247 case -ENOENT:
248 errmsg = "unlinked synchronuously";
249 break;
250 case -ECONNRESET:
251 errmsg = "unlinked asynchronuously";
252 break;
253 case -ENOSR:
254 errmsg = "Buffer error (overrun)";
255 break;
256 case -EPIPE:
257 errmsg = "Stalled (device not responding)";
258 break;
259 case -EOVERFLOW:
260 errmsg = "Babble (bad cable?)";
261 break;
262 case -EPROTO:
263 errmsg = "Bit-stuff error (bad cable?)";
264 break;
265 case -EILSEQ:
266 errmsg = "CRC/Timeout (could be anything)";
267 break;
268 case -ETIME:
269 errmsg = "Device does not respond";
270 break;
271 }
272 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300273 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300274 } else {
275 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300276 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300277 }
278}
279
280/*
281 * video-buf generic routine to get the next available buffer
282 */
283static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300284 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300285{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300286 struct cx231xx_video_mode *vmode =
287 container_of(dma_q, struct cx231xx_video_mode, vidq);
288 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300289
290 char *outp;
291
Sri Deevie0d3baf2009-03-03 14:37:50 -0300292 if (list_empty(&dma_q->active)) {
293 cx231xx_isocdbg("No active queue to serve\n");
294 dev->video_mode.isoc_ctl.buf = NULL;
295 *buf = NULL;
296 return;
297 }
298
299 /* Get the next buffer */
300 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
301
302 /* Cleans up buffer - Usefull for testing for frame/URB loss */
303 outp = videobuf_to_vmalloc(&(*buf)->vb);
304 memset(outp, 0, (*buf)->vb.size);
305
306 dev->video_mode.isoc_ctl.buf = *buf;
307
308 return;
309}
310
311/*
312 * Controls the isoc copy of each urb packet
313 */
314static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
315{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300316 struct cx231xx_buffer *buf;
317 struct cx231xx_dmaqueue *dma_q = urb->context;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300318 unsigned char *outp = NULL;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300319 int i, rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300320 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300321 u32 bytes_parsed = 0, buffer_size = 0;
322 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300323
324 if (!dev)
325 return 0;
326
327 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
328 return 0;
329
330 if (urb->status < 0) {
331 print_err_status(dev, -1, urb->status);
332 if (urb->status == -ENOENT)
333 return 0;
334 }
335
336 buf = dev->video_mode.isoc_ctl.buf;
337 if (buf != NULL)
338 outp = videobuf_to_vmalloc(&buf->vb);
339
340 for (i = 0; i < urb->number_of_packets; i++) {
341 int status = urb->iso_frame_desc[i].status;
342
343 if (status < 0) {
344 print_err_status(dev, i, status);
345 if (urb->iso_frame_desc[i].status != -EPROTO)
346 continue;
347 }
348
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300349 if (urb->iso_frame_desc[i].actual_length <= 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300350 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
351 continue;
352 }
353 if (urb->iso_frame_desc[i].actual_length >
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300354 dev->video_mode.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300355 cx231xx_isocdbg("packet bigger than packet size");
356 continue;
357 }
358
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300359 /* get buffer pointer and length */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300360 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300361 buffer_size = urb->iso_frame_desc[i].actual_length;
362 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300363
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300364 if (dma_q->is_partial_line) {
365 /* Handle the case where we were working on a partial line */
366 sav_eav = dma_q->last_sav;
367 } else {
368 /* Check for a SAV/EAV overlapping the buffer boundary */
369 sav_eav =
370 cx231xx_find_boundary_SAV_EAV(p_buffer,
371 dma_q->partial_buf,
372 &bytes_parsed);
373 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300374
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300375 sav_eav &= 0xF0;
376 /* Get the first line if we have some portion of an SAV/EAV from the last buffer
377 or a partial line */
378 if (sav_eav) {
379 bytes_parsed += cx231xx_get_video_line(dev, dma_q, sav_eav, /* SAV/EAV */
380 p_buffer + bytes_parsed, /* p_buffer */
381 buffer_size - bytes_parsed); /* buffer size */
382 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300383
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300384 /* Now parse data that is completely in this buffer */
385 /* dma_q->is_partial_line = 0; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300386
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300387 while (bytes_parsed < buffer_size) {
388 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300389
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300390 sav_eav = cx231xx_find_next_SAV_EAV(p_buffer + bytes_parsed, /* p_buffer */
391 buffer_size - bytes_parsed, /* buffer size */
392 &bytes_used); /* Receives bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300393
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300394 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300395
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300396 sav_eav &= 0xF0;
397 if (sav_eav && (bytes_parsed < buffer_size)) {
398 bytes_parsed += cx231xx_get_video_line(dev, dma_q, sav_eav, /* SAV/EAV */
399 p_buffer + bytes_parsed, /* p_buffer */
400 buffer_size - bytes_parsed); /* buffer size */
401 }
402 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300403
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300404 /* Save the last four bytes of the buffer so we can check the buffer boundary
405 condition next time */
406 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
407 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300408
409 }
410 return rc;
411}
412
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300413u8 cx231xx_find_boundary_SAV_EAV(u8 * p_buffer, u8 * partial_buf,
414 u32 * p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300415{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300416 u32 bytes_used;
417 u8 boundary_bytes[8];
418 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300419
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300420 *p_bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300421
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300422 /* Create an array of the last 4 bytes of the last buffer and the first
423 4 bytes of the current buffer. */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300424
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300425 memcpy(boundary_bytes, partial_buf, 4);
426 memcpy(boundary_bytes + 4, p_buffer, 4);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300427
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300428 /* Check for the SAV/EAV in the boundary buffer */
429 sav_eav =
430 cx231xx_find_next_SAV_EAV((u8 *) & boundary_bytes, 8, &bytes_used);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300431
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300432 if (sav_eav) {
433 /* found a boundary SAV/EAV. Updates the bytes used to reflect
434 only those used in the new buffer */
435 *p_bytes_used = bytes_used - 4;
436 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300437
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300438 return sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300439}
440
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300441u8 cx231xx_find_next_SAV_EAV(u8 * p_buffer, u32 buffer_size, u32 * p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300442{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300443 u32 i;
444 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300445
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300446 /* Don't search if the buffer size is less than 4. It causes a page fault since
447 buffer_size - 4 evaluates to a large number in that case. */
448 if (buffer_size < 4) {
449 *p_bytes_used = buffer_size;
450 return 0;
451 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300452
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300453 for (i = 0; i < (buffer_size - 3); i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300454
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300455 if ((p_buffer[i] == 0xFF) &&
456 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300457
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300458 *p_bytes_used = i + 4;
459 sav_eav = p_buffer[i + 3];
460 return sav_eav;
461 }
462 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300463
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300464 *p_bytes_used = buffer_size;
465 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300466}
467
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300468u32 cx231xx_get_video_line(struct cx231xx * dev,
469 struct cx231xx_dmaqueue * dma_q, u8 sav_eav,
470 u8 * p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300471{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300472 u32 bytes_copied = 0;
473 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300474
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300475 switch (sav_eav) {
476 case SAV_ACTIVE_VIDEO_FIELD1:
477 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
478 there will be no active data contained between the SAV and EAV */
479 if ((buffer_size > 3) &&
480 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00)
481 && (p_buffer[2] == 0x00)
482 && ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1)
483 || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2)
484 || (p_buffer[3] == EAV_VBLANK_FIELD1)
485 || (p_buffer[3] == EAV_VBLANK_FIELD2)
486 )
487 ) {
488 return bytes_copied;
489 }
490 current_field = 1;
491 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300492
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300493 case SAV_ACTIVE_VIDEO_FIELD2:
494 /* looking for skipped line which occurred in PAL 720x480 mode. In this case,
495 there will be no active data contained between the SAV and EAV */
496 if ((buffer_size > 3) &&
497 (p_buffer[0] == 0xFF) && (p_buffer[1] == 0x00)
498 && (p_buffer[2] == 0x00)
499 && ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1)
500 || (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2)
501 || (p_buffer[3] == EAV_VBLANK_FIELD1)
502 || (p_buffer[3] == EAV_VBLANK_FIELD2)
503 )
504 ) {
505 return bytes_copied;
506 }
507 current_field = 2;
508 break;
509 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300510
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300511 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300512
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300513 bytes_copied =
514 cx231xx_copy_video_line(dev, dma_q, p_buffer, buffer_size,
515 current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300516
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300517 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300518}
519
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300520u32 cx231xx_copy_video_line(struct cx231xx * dev,
521 struct cx231xx_dmaqueue * dma_q, u8 * p_line,
522 u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300523{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300524 u32 bytes_to_copy;
525 struct cx231xx_buffer *buf;
526 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300527
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300528 if (dma_q->current_field != field_number) {
529 cx231xx_reset_video_buffer(dev, dma_q);
530 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300531
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300532 /* get the buffer pointer */
533 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300534
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300535 /* Remember the field number for next time */
536 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300537
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300538 bytes_to_copy = dma_q->bytes_left_in_line;
539 if (bytes_to_copy > length)
540 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300541
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300542 if (dma_q->lines_completed >= dma_q->lines_per_field) {
543 dma_q->bytes_left_in_line -= bytes_to_copy;
544 dma_q->is_partial_line =
545 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
546 return 0;
547 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300548
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300549 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300550
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300551 /* If we don't have a buffer, just return the number of bytes we would
552 have copied if we had a buffer. */
553 if (!buf) {
554 dma_q->bytes_left_in_line -= bytes_to_copy;
555 dma_q->is_partial_line =
556 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
557 return bytes_to_copy;
558 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300559
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300560 /* copy the data to video buffer */
561 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300562
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300563 dma_q->pos += bytes_to_copy;
564 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300565
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300566 if (dma_q->bytes_left_in_line == 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300567
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300568 dma_q->bytes_left_in_line = _line_size;
569 dma_q->lines_completed++;
570 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300571
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300572 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300573
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300574 buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300575
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300576 dma_q->pos = 0;
577 buf = NULL;
578 dma_q->lines_completed = 0;
579 }
580 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300581
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300582 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300583}
584
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300585void cx231xx_reset_video_buffer(struct cx231xx *dev,
586 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300587{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300588 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300589
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300590 /* handle the switch from field 1 to field 2 */
591 if (dma_q->current_field == 1) {
592 if (dma_q->lines_completed >= dma_q->lines_per_field) {
593 dma_q->field1_done = 1;
594 } else {
595 dma_q->field1_done = 0;
596 }
597 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300598
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300599 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300600
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 if (buf == NULL) {
602 u8 *outp = NULL;
603 /* first try to get the buffer */
604 get_next_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300605
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300606 if (buf)
607 outp = videobuf_to_vmalloc(&buf->vb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300608
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300609 dma_q->pos = 0;
610 dma_q->field1_done = 0;
611 dma_q->current_field = -1;
612 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300613
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300614 /* reset the counters */
615 dma_q->bytes_left_in_line = dev->width << 1;
616 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300617}
618
619int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300620 u8 * p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300621{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300622 u8 *p_out_buffer = NULL;
623 u32 current_line_bytes_copied = 0;
624 struct cx231xx_buffer *buf;
625 u32 _line_size = dev->width << 1;
626 void *startwrite;
627 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300628
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300629 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300630
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300631 if (buf == NULL)
632 return -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300633
634 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
635
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300636 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300637
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300638 /* Offset field 2 one line from the top of the buffer */
639 offset = (dma_q->current_field == 1) ? 0 : _line_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300640
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300641 /* Offset for field 2 */
642 startwrite = p_out_buffer + offset;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300643
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300644 /* lines already completed in the current field */
645 startwrite += (dma_q->lines_completed * _line_size * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300646
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300647 /* bytes already completed in the current line */
648 startwrite += current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300649
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300650 lencopy =
651 dma_q->bytes_left_in_line >
652 bytes_to_copy ? bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300653
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300654 if ((u8 *) (startwrite + lencopy) >
655 (u8 *) (p_out_buffer + buf->vb.size)) {
656 return 0;
657 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300658
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300659 /* The below copies the UYVY data straight into video buffer */
660 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300661
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300662 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300663}
664
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300665void cx231xx_swab(u16 * from, u16 * to, u16 len)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300666{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300667 u16 i;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300668
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300669 if (len <= 0)
670 return;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300671
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300672 for (i = 0; i < len / 2; i++) {
673 to[i] = (from[i] << 8) | (from[i] >> 8);
674 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300675}
676
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300677u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300678{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300679 u8 buffer_complete = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300680
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300681 /* Dual field stream */
682 buffer_complete =
683 ((dma_q->current_field == 2) &&
684 (dma_q->lines_completed >= dma_q->lines_per_field) &&
685 dma_q->field1_done);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300686
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300687 return buffer_complete;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300688}
689
Sri Deevie0d3baf2009-03-03 14:37:50 -0300690/* ------------------------------------------------------------------
691 Videobuf operations
692 ------------------------------------------------------------------*/
693
694static int
695buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
696{
697 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300698 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300699 struct v4l2_frequency f;
700
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300701 *size =
702 (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300703 if (0 == *count)
704 *count = CX231XX_DEF_BUF;
705
706 if (*count < CX231XX_MIN_BUF)
707 *count = CX231XX_MIN_BUF;
708
709 /* Ask tuner to go to analog mode */
710 memset(&f, 0, sizeof(f));
711 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300712 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300713
714 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
715
716 return 0;
717}
718
719/* This is called *without* dev->slock held; please keep it that way */
720static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
721{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300722 struct cx231xx_fh *fh = vq->priv_data;
723 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300724 unsigned long flags = 0;
725 if (in_interrupt())
726 BUG();
727
728 /* We used to wait for the buffer to finish here, but this didn't work
729 because, as we were keeping the state as VIDEOBUF_QUEUED,
730 videobuf_queue_cancel marked it as finished for us.
731 (Also, it could wedge forever if the hardware was misconfigured.)
732
733 This should be safe; by the time we get here, the buffer isn't
734 queued anymore. If we ever start marking the buffers as
735 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300736 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300737 spin_lock_irqsave(&dev->video_mode.slock, flags);
738 if (dev->video_mode.isoc_ctl.buf == buf)
739 dev->video_mode.isoc_ctl.buf = NULL;
740 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
741
742 videobuf_vmalloc_free(&buf->vb);
743 buf->vb.state = VIDEOBUF_NEEDS_INIT;
744}
745
746static int
747buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300748 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300749{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300750 struct cx231xx_fh *fh = vq->priv_data;
751 struct cx231xx_buffer *buf =
752 container_of(vb, struct cx231xx_buffer, vb);
753 struct cx231xx *dev = fh->dev;
754 int rc = 0, urb_init = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300755
756 /* The only currently supported format is 16 bits/pixel */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300757 buf->vb.size =
758 (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300759
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300760 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300761 return -EINVAL;
762
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300763 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300764 buf->vb.height = dev->height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300765 buf->vb.field = field;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300766
767 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
768 rc = videobuf_iolock(vq, &buf->vb, NULL);
769 if (rc < 0)
770 goto fail;
771 }
772
773 if (!dev->video_mode.isoc_ctl.num_bufs)
774 urb_init = 1;
775
776 if (urb_init) {
777 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300778 CX231XX_NUM_BUFS,
779 dev->video_mode.max_pkt_size,
780 cx231xx_isoc_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300781 if (rc < 0)
782 goto fail;
783 }
784
785 buf->vb.state = VIDEOBUF_PREPARED;
786 return 0;
787
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300788 fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300789 free_buffer(vq, buf);
790 return rc;
791}
792
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300793static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300794{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300795 struct cx231xx_buffer *buf =
796 container_of(vb, struct cx231xx_buffer, vb);
797 struct cx231xx_fh *fh = vq->priv_data;
798 struct cx231xx *dev = fh->dev;
799 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300800
801 buf->vb.state = VIDEOBUF_QUEUED;
802 list_add_tail(&buf->vb.queue, &vidq->active);
803
804}
805
806static void buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300807 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300808{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300809 struct cx231xx_buffer *buf =
810 container_of(vb, struct cx231xx_buffer, vb);
811 struct cx231xx_fh *fh = vq->priv_data;
812 struct cx231xx *dev = (struct cx231xx *)fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300813
814 cx231xx_isocdbg("cx231xx: called buffer_release\n");
815
816 free_buffer(vq, buf);
817}
818
819static struct videobuf_queue_ops cx231xx_video_qops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300820 .buf_setup = buffer_setup,
821 .buf_prepare = buffer_prepare,
822 .buf_queue = buffer_queue,
823 .buf_release = buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300824};
825
826/********************* v4l2 interface **************************************/
827
Sri Deevie0d3baf2009-03-03 14:37:50 -0300828void video_mux(struct cx231xx *dev, int index)
829{
830
831 struct v4l2_routing route;
832
833 route.input = INPUT(index)->vmux;
834 route.output = 0;
835 dev->video_input = index;
836 dev->ctl_ainput = INPUT(index)->amux;
837
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300838 cx231xx_set_video_input_mux(dev, index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300839
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300840 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_INT_S_VIDEO_ROUTING,
841 &route);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300842
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300843 cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300844
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300845 cx231xx_info("video_mux : %d\n", index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300846
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300847 /* do mode control overrides if required */
848 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300849}
850
851/* Usage lock check functions */
852static int res_get(struct cx231xx_fh *fh)
853{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300854 struct cx231xx *dev = fh->dev;
855 int rc = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300856
857 /* This instance already has stream_on */
858 if (fh->stream_on)
859 return rc;
860
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300861 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
862 if (dev->stream_on)
863 return -EBUSY;
864 dev->stream_on = 1;
865 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
866 if (dev->vbi_stream_on)
867 return -EBUSY;
868 dev->vbi_stream_on = 1;
869 } else
870 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300871
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300872 fh->stream_on = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300873
874 return rc;
875}
876
877static int res_check(struct cx231xx_fh *fh)
878{
879 return (fh->stream_on);
880}
881
882static void res_free(struct cx231xx_fh *fh)
883{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300884 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300885
886 fh->stream_on = 0;
887
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300888 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
889 dev->stream_on = 0;
890 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
891 dev->vbi_stream_on = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300892}
893
894static int check_dev(struct cx231xx *dev)
895{
896 if (dev->state & DEV_DISCONNECTED) {
897 cx231xx_errdev("v4l2 ioctl: device not present\n");
898 return -ENODEV;
899 }
900
901 if (dev->state & DEV_MISCONFIGURED) {
902 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300903 "close and open it again\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300904 return -EIO;
905 }
906 return 0;
907}
908
909void get_scale(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300910 unsigned int width, unsigned int height,
911 unsigned int *hscale, unsigned int *vscale)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300912{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300913 unsigned int maxw = norm_maxw(dev);
914 unsigned int maxh = norm_maxh(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300915
916 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
917 if (*hscale >= 0x4000)
918 *hscale = 0x3fff;
919
920 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
921 if (*vscale >= 0x4000)
922 *vscale = 0x3fff;
923
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300924 dev->hscale = *hscale;
925 dev->vscale = *vscale;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300926
927}
928
929/* ------------------------------------------------------------------
930 IOCTL vidioc handling
931 ------------------------------------------------------------------*/
932
933static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300934 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300935{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300936 struct cx231xx_fh *fh = priv;
937 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300938
939 mutex_lock(&dev->lock);
940
941 f->fmt.pix.width = dev->width;
942 f->fmt.pix.height = dev->height;
943 f->fmt.pix.pixelformat = dev->format->fourcc;;
944 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300945 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300946 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
947
948 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
949
950 mutex_unlock(&dev->lock);
951 return 0;
952}
953
954static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
955{
956 unsigned int i;
957
958 for (i = 0; i < ARRAY_SIZE(format); i++)
959 if (format[i].fourcc == fourcc)
960 return &format[i];
961
962 return NULL;
963}
964
965static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300966 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300967{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300968 struct cx231xx_fh *fh = priv;
969 struct cx231xx *dev = fh->dev;
970 int width = f->fmt.pix.width;
971 int height = f->fmt.pix.height;
972 unsigned int maxw = norm_maxw(dev);
973 unsigned int maxh = norm_maxh(dev);
974 unsigned int hscale, vscale;
975 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300976
977 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
978 if (!fmt) {
979 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300980 f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300981 return -EINVAL;
982 }
983
984 /* width must even because of the YUYV format
985 height must be even because of interlacing */
986 height &= 0xfffe;
987 width &= 0xfffe;
988
989 if (unlikely(height < 32))
990 height = 32;
991 if (unlikely(height > maxh))
992 height = maxh;
993 if (unlikely(width < 48))
994 width = 48;
995 if (unlikely(width > maxw))
996 width = maxw;
997
998 get_scale(dev, width, height, &hscale, &vscale);
999
1000 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1001 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1002
1003 f->fmt.pix.width = width;
1004 f->fmt.pix.height = height;
1005 f->fmt.pix.pixelformat = fmt->fourcc;
1006 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1007 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1008 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1009 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1010
1011 return 0;
1012}
1013
1014static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001015 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001016{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001017 struct cx231xx_fh *fh = priv;
1018 struct cx231xx *dev = fh->dev;
1019 int rc;
1020 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001021
1022 rc = check_dev(dev);
1023 if (rc < 0)
1024 return rc;
1025
Sri Deevie0d3baf2009-03-03 14:37:50 -03001026 mutex_lock(&dev->lock);
1027
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001028 vidioc_try_fmt_vid_cap(file, priv, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001029
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001030 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001031 if (!fmt) {
1032 rc = -EINVAL;
1033 goto out;
1034 }
1035
1036 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1037 cx231xx_errdev("%s queue busy\n", __func__);
1038 rc = -EBUSY;
1039 goto out;
1040 }
1041
1042 if (dev->stream_on && !fh->stream_on) {
1043 cx231xx_errdev("%s device in use by another fh\n", __func__);
1044 rc = -EBUSY;
1045 goto out;
1046 }
1047
1048 /* set new image size */
1049 dev->width = f->fmt.pix.width;
1050 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001051 dev->format = fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001052 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1053
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001054 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_FMT, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001055
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001056 /* Set the correct alternate setting for this resolution */
Sri Deevie0d3baf2009-03-03 14:37:50 -03001057 cx231xx_resolution_set(dev);
1058
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001059 out:
Sri Deevie0d3baf2009-03-03 14:37:50 -03001060 mutex_unlock(&dev->lock);
1061 return rc;
1062}
1063
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001064static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001065{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001066 struct cx231xx_fh *fh = priv;
1067 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001068
1069 *id = dev->norm;
1070 return 0;
1071}
1072
1073static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1074{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001075 struct cx231xx_fh *fh = priv;
1076 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001077 struct v4l2_format f;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001078 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001079
1080 rc = check_dev(dev);
1081 if (rc < 0)
1082 return rc;
1083
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001084 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001085
1086 mutex_lock(&dev->lock);
1087 dev->norm = *norm;
1088
Sri Deevie0d3baf2009-03-03 14:37:50 -03001089 /* Adjusts width/height, if needed */
1090 f.fmt.pix.width = dev->width;
1091 f.fmt.pix.height = dev->height;
1092 vidioc_try_fmt_vid_cap(file, priv, &f);
1093
1094 /* set new image size */
1095 dev->width = f.fmt.pix.width;
1096 dev->height = f.fmt.pix.height;
1097 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1098
1099 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_STD, &dev->norm);
1100
1101 mutex_unlock(&dev->lock);
1102
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001103 cx231xx_resolution_set(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001104
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001105 /* do mode control overrides */
1106 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001107
1108 return 0;
1109}
1110
1111static const char *iname[] = {
1112 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001113 [CX231XX_VMUX_SVIDEO] = "S-Video",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001114 [CX231XX_VMUX_TELEVISION] = "Television",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001115 [CX231XX_VMUX_CABLE] = "Cable TV",
1116 [CX231XX_VMUX_DVB] = "DVB",
1117 [CX231XX_VMUX_DEBUG] = "for debug only",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001118};
1119
1120static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001121 struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001122{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001123 struct cx231xx_fh *fh = priv;
1124 struct cx231xx *dev = fh->dev;
1125 unsigned int n;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001126
1127 n = i->index;
1128 if (n >= MAX_CX231XX_INPUT)
1129 return -EINVAL;
1130 if (0 == INPUT(n)->type)
1131 return -EINVAL;
1132
1133 i->index = n;
1134 i->type = V4L2_INPUT_TYPE_CAMERA;
1135
1136 strcpy(i->name, iname[INPUT(n)->type]);
1137
1138 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001139 (CX231XX_VMUX_CABLE == INPUT(n)->type))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001140 i->type = V4L2_INPUT_TYPE_TUNER;
1141
1142 i->std = dev->vdev->tvnorms;
1143
1144 return 0;
1145}
1146
1147static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1148{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001149 struct cx231xx_fh *fh = priv;
1150 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001151
1152 *i = dev->video_input;
1153
1154 return 0;
1155}
1156
1157static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1158{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001159 struct cx231xx_fh *fh = priv;
1160 struct cx231xx *dev = fh->dev;
1161 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001162
1163 rc = check_dev(dev);
1164 if (rc < 0)
1165 return rc;
1166
1167 if (i >= MAX_CX231XX_INPUT)
1168 return -EINVAL;
1169 if (0 == INPUT(i)->type)
1170 return -EINVAL;
1171
1172 mutex_lock(&dev->lock);
1173
1174 video_mux(dev, i);
1175
1176 mutex_unlock(&dev->lock);
1177 return 0;
1178}
1179
1180static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1181{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001182 struct cx231xx_fh *fh = priv;
1183 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001184
1185 switch (a->index) {
1186 case CX231XX_AMUX_VIDEO:
1187 strcpy(a->name, "Television");
1188 break;
1189 case CX231XX_AMUX_LINE_IN:
1190 strcpy(a->name, "Line In");
1191 break;
1192 default:
1193 return -EINVAL;
1194 }
1195
1196 a->index = dev->ctl_ainput;
1197 a->capability = V4L2_AUDCAP_STEREO;
1198
1199 return 0;
1200}
1201
1202static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1203{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001204 struct cx231xx_fh *fh = priv;
1205 struct cx231xx *dev = fh->dev;
1206 int status = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001207
1208 /* Doesn't allow manual routing */
1209 if (a->index != dev->ctl_ainput)
1210 return -EINVAL;
1211
1212 dev->ctl_ainput = INPUT(a->index)->amux;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001213 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001214
1215 return status;
1216}
1217
1218static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001219 struct v4l2_queryctrl *qc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001220{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001221 struct cx231xx_fh *fh = priv;
1222 struct cx231xx *dev = fh->dev;
1223 int id = qc->id;
1224 int i;
1225 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001226
1227 rc = check_dev(dev);
1228 if (rc < 0)
1229 return rc;
1230
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001231 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001232 if (unlikely(qc->id == 0))
1233 return -EINVAL;
1234
1235 memset(qc, 0, sizeof(*qc));
1236
1237 qc->id = id;
1238
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001239 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001240 return -EINVAL;
1241
1242 for (i = 0; i < CX231XX_CTLS; i++)
1243 if (cx231xx_ctls[i].v.id == qc->id)
1244 break;
1245
1246 if (i == CX231XX_CTLS) {
1247 *qc = no_ctl;
1248 return 0;
1249 }
1250 *qc = cx231xx_ctls[i].v;
1251
1252 mutex_lock(&dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001253 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_QUERYCTRL, qc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001254 mutex_unlock(&dev->lock);
1255
1256 if (qc->type)
1257 return 0;
1258 else
1259 return -EINVAL;
1260}
1261
1262static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001263 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001264{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001265 struct cx231xx_fh *fh = priv;
1266 struct cx231xx *dev = fh->dev;
1267 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001268
1269 rc = check_dev(dev);
1270 if (rc < 0)
1271 return rc;
1272
1273 mutex_lock(&dev->lock);
1274
1275 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_CTRL, ctrl);
1276
1277 mutex_unlock(&dev->lock);
1278 return rc;
1279}
1280
1281static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001282 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001283{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001284 struct cx231xx_fh *fh = priv;
1285 struct cx231xx *dev = fh->dev;
1286 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001287
1288 rc = check_dev(dev);
1289 if (rc < 0)
1290 return rc;
1291
1292 mutex_lock(&dev->lock);
1293
1294 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_S_CTRL, ctrl);
1295
1296 mutex_unlock(&dev->lock);
1297 return rc;
1298}
1299
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001300static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001301{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001302 struct cx231xx_fh *fh = priv;
1303 struct cx231xx *dev = fh->dev;
1304 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001305
1306 rc = check_dev(dev);
1307 if (rc < 0)
1308 return rc;
1309
1310 if (0 != t->index)
1311 return -EINVAL;
1312
1313 strcpy(t->name, "Tuner");
1314
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001315 t->type = V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001316 t->capability = V4L2_TUNER_CAP_NORM;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001317 t->rangehigh = 0xffffffffUL;
1318 t->signal = 0xffff; /* LOCKED */
Sri Deevie0d3baf2009-03-03 14:37:50 -03001319
1320 return 0;
1321}
1322
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001323static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001324{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001325 struct cx231xx_fh *fh = priv;
1326 struct cx231xx *dev = fh->dev;
1327 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001328
1329 rc = check_dev(dev);
1330 if (rc < 0)
1331 return rc;
1332
1333 if (0 != t->index)
1334 return -EINVAL;
1335#if 0
1336 mutex_lock(&dev->lock);
1337
1338 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1339
1340 mutex_unlock(&dev->lock);
1341#endif
1342 return 0;
1343}
1344
1345static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001346 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001347{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001348 struct cx231xx_fh *fh = priv;
1349 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001350
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001351 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001352 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1353 f->frequency = dev->ctl_freq;
1354
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001355 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001356
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001357 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001358
1359 return 0;
1360}
1361
1362static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001363 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001364{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001365 struct cx231xx_fh *fh = priv;
1366 struct cx231xx *dev = fh->dev;
1367 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001368
1369 rc = check_dev(dev);
1370 if (rc < 0)
1371 return rc;
1372
1373 if (0 != f->tuner)
1374 return -EINVAL;
1375
1376 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1377 return -EINVAL;
1378 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1379 return -EINVAL;
1380
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001381 /* set pre channel change settings in DIF first */
1382 rc = cx231xx_tuner_pre_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001383
1384 mutex_lock(&dev->lock);
1385
1386 dev->ctl_freq = f->frequency;
1387
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001388 if (dev->tuner_type == TUNER_XC5000) {
1389 if (dev->cx231xx_set_analog_freq != NULL) {
1390 dev->cx231xx_set_analog_freq(dev, f->frequency);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001391 }
1392 } else {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001393 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY,
1394 f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001395 }
1396
1397 mutex_unlock(&dev->lock);
1398
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001399 /* set post channel change settings in DIF first */
1400 rc = cx231xx_tuner_post_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001401
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001402 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001403
1404 return rc;
1405}
1406
1407#ifdef CONFIG_VIDEO_ADV_DEBUG
1408
Sri Deevie0d3baf2009-03-03 14:37:50 -03001409/*
1410 -R, --list-registers=type=<host/i2cdrv/i2caddr>,chip=<chip>[,min=<addr>,max=<addr>]
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001411 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001412 -r, --set-register=type=<host/i2cdrv/i2caddr>,chip=<chip>,reg=<addr>,val=<val>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001413 set the register [VIDIOC_DBG_S_REGISTER]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001414
1415 if type == host, then <chip> is the hosts chip ID (default 0)
1416 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1417 if type == i2caddr, then <chip> is the 7-bit I2C address
1418*/
1419
Sri Deevie0d3baf2009-03-03 14:37:50 -03001420static int vidioc_g_register(struct file *file, void *priv,
1421 struct v4l2_dbg_register *reg)
1422{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001423 struct cx231xx_fh *fh = priv;
1424 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001425 int ret = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001426 u8 value[4] = { 0, 0, 0, 0 };
1427 u32 data = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001428
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001429 switch (reg->match.type) {
1430 case V4L2_CHIP_MATCH_HOST:
1431 switch (reg->match.addr) {
1432 case 0: /* Cx231xx - internal registers */
1433 ret =
1434 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1435 (u16) reg->reg, value, 4);
1436 reg->val =
1437 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1438 << 24;
1439 break;
1440 case 1: /* Colibri - read byte */
1441 ret =
1442 cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1443 (u16) reg->reg, 2, &data, 1);
1444 reg->val = le32_to_cpu(data & 0xff);
1445 break;
1446 case 14: /* Colibri - read dword */
1447 ret =
1448 cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
1449 (u16) reg->reg, 2, &data, 4);
1450 reg->val = le32_to_cpu(data);
1451 break;
1452 case 2: /* Hammerhead - read byte */
1453 ret =
1454 cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1455 (u16) reg->reg, 2, &data, 1);
1456 reg->val = le32_to_cpu(data & 0xff);
1457 break;
1458 case 24: /* Hammerhead - read dword */
1459 ret =
1460 cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1461 (u16) reg->reg, 2, &data, 4);
1462 reg->val = le32_to_cpu(data);
1463 break;
1464 case 3: /* flatiron - read byte */
1465 ret =
1466 cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1467 (u16) reg->reg, 1, &data, 1);
1468 reg->val = le32_to_cpu(data & 0xff);
1469 break;
1470 case 34: /* flatiron - read dword */
1471 ret =
1472 cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1473 (u16) reg->reg, 1, &data, 4);
1474 reg->val = le32_to_cpu(data);
1475 break;
1476 }
1477 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001478
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001479 case V4L2_CHIP_MATCH_I2C_DRIVER:
1480 cx231xx_i2c_call_clients(&dev->i2c_bus[0],
1481 VIDIOC_DBG_G_REGISTER, reg);
1482 return 0;
1483 case V4L2_CHIP_MATCH_I2C_ADDR:
1484 /* Not supported yet */
1485 return -EINVAL;
1486 default:
1487 if (!v4l2_chip_match_host(&reg->match))
1488 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001489 }
1490
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001491 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001492
1493 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_G_REGISTER, reg);
1494
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001495 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001496
1497 return ret;
1498}
1499
1500static int vidioc_s_register(struct file *file, void *priv,
1501 struct v4l2_dbg_register *reg)
1502{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001503 struct cx231xx_fh *fh = priv;
1504 struct cx231xx *dev = fh->dev;
1505 int ret = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001506 __le64 buf;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001507 u32 value;
1508 u8 data[4] = { 0, 0, 0, 0 };
Sri Deevie0d3baf2009-03-03 14:37:50 -03001509
1510 buf = cpu_to_le64(reg->val);
1511
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001512 switch (reg->match.type) {
1513 case V4L2_CHIP_MATCH_HOST:
1514 {
1515 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001516
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001517 switch (reg->match.addr) {
1518 case 0: /* cx231xx internal registers */
1519 data[0] = (u8) value;
1520 data[1] = (u8) (value >> 8);
1521 data[2] = (u8) (value >> 16);
1522 data[3] = (u8) (value >> 24);
1523 ret =
1524 cx231xx_write_ctrl_reg(dev,
1525 VRT_SET_REGISTER,
1526 (u16) reg->reg, data,
1527 4);
1528 break;
1529 case 1: /* Colibri - read byte */
1530 ret =
1531 cx231xx_write_i2c_data(dev,
1532 Colibri_DEVICE_ADDRESS,
1533 (u16) reg->reg, 2,
1534 value, 1);
1535 break;
1536 case 14: /* Colibri - read dword */
1537 ret =
1538 cx231xx_write_i2c_data(dev,
1539 Colibri_DEVICE_ADDRESS,
1540 (u16) reg->reg, 2,
1541 value, 4);
1542 break;
1543 case 2: /* Hammerhead - read byte */
1544 ret =
1545 cx231xx_write_i2c_data(dev,
1546 HAMMERHEAD_I2C_ADDRESS,
1547 (u16) reg->reg, 2,
1548 value, 1);
1549 break;
1550 case 24: /* Hammerhead - read dword */
1551 ret =
1552 cx231xx_write_i2c_data(dev,
1553 HAMMERHEAD_I2C_ADDRESS,
1554 (u16) reg->reg, 2,
1555 value, 4);
1556 break;
1557 case 3: /* flatiron - read byte */
1558 ret =
1559 cx231xx_write_i2c_data(dev,
1560 Flatrion_DEVICE_ADDRESS,
1561 (u16) reg->reg, 1,
1562 value, 1);
1563 break;
1564 case 34: /* flatiron - read dword */
1565 ret =
1566 cx231xx_write_i2c_data(dev,
1567 Flatrion_DEVICE_ADDRESS,
1568 (u16) reg->reg, 1,
1569 value, 4);
1570 break;
1571 }
1572 }
1573 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001574
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001575 default:
1576 break;
1577 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001578
1579 mutex_lock(&dev->lock);
1580
1581 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_DBG_S_REGISTER, reg);
1582
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001583 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001584
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001585 return ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001586}
1587#endif
1588
Sri Deevie0d3baf2009-03-03 14:37:50 -03001589static int vidioc_cropcap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001590 struct v4l2_cropcap *cc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001591{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001592 struct cx231xx_fh *fh = priv;
1593 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001594
1595 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1596 return -EINVAL;
1597
1598 cc->bounds.left = 0;
1599 cc->bounds.top = 0;
1600 cc->bounds.width = dev->width;
1601 cc->bounds.height = dev->height;
1602 cc->defrect = cc->bounds;
1603 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1604 cc->pixelaspect.denominator = 59;
1605
1606 return 0;
1607}
1608
1609static int vidioc_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001610 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001611{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001612 struct cx231xx_fh *fh = priv;
1613 struct cx231xx *dev = fh->dev;
1614 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001615
1616 rc = check_dev(dev);
1617 if (rc < 0)
1618 return rc;
1619
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001620 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001621 rc = res_get(fh);
1622
1623 if (likely(rc >= 0))
1624 rc = videobuf_streamon(&fh->vb_vidq);
1625
1626 mutex_unlock(&dev->lock);
1627
1628 return rc;
1629}
1630
1631static int vidioc_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001632 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001633{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001634 struct cx231xx_fh *fh = priv;
1635 struct cx231xx *dev = fh->dev;
1636 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001637
1638 rc = check_dev(dev);
1639 if (rc < 0)
1640 return rc;
1641
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001642 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1643 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001644 return -EINVAL;
1645 if (type != fh->type)
1646 return -EINVAL;
1647
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001648 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001649
1650 videobuf_streamoff(&fh->vb_vidq);
1651 res_free(fh);
1652
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001653 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001654
1655 return 0;
1656}
1657
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001658static int vidioc_querycap(struct file *file, void *priv,
1659 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001660{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001661 struct cx231xx_fh *fh = priv;
1662 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001663
1664 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1665 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001666 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
1667 sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001668
1669 cap->version = CX231XX_VERSION_CODE;
1670
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001671 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
Sri Deevie0d3baf2009-03-03 14:37:50 -03001672#if 0
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001673 V4L2_CAP_SLICED_VBI_CAPTURE |
Sri Deevie0d3baf2009-03-03 14:37:50 -03001674#endif
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001675 V4L2_CAP_VIDEO_CAPTURE |
1676 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001677
1678 if (dev->tuner_type != TUNER_ABSENT)
1679 cap->capabilities |= V4L2_CAP_TUNER;
1680
1681 return 0;
1682}
1683
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001684static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1685 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001686{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001687 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001688 return -EINVAL;
1689
1690 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1691 f->pixelformat = format[f->index].fourcc;
1692
1693 return 0;
1694}
1695
1696/* Sliced VBI ioctls */
1697static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001698 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001699{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001700 struct cx231xx_fh *fh = priv;
1701 struct cx231xx *dev = fh->dev;
1702 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001703
1704 rc = check_dev(dev);
1705 if (rc < 0)
1706 return rc;
1707
1708 mutex_lock(&dev->lock);
1709
1710 f->fmt.sliced.service_set = 0;
1711
1712 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1713
1714 if (f->fmt.sliced.service_set == 0)
1715 rc = -EINVAL;
1716
1717 mutex_unlock(&dev->lock);
1718 return rc;
1719}
1720
1721static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001722 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001723{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001724 struct cx231xx_fh *fh = priv;
1725 struct cx231xx *dev = fh->dev;
1726 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001727
1728 rc = check_dev(dev);
1729 if (rc < 0)
1730 return rc;
1731
1732 mutex_lock(&dev->lock);
1733 cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_G_FMT, f);
1734 mutex_unlock(&dev->lock);
1735
1736 if (f->fmt.sliced.service_set == 0)
1737 return -EINVAL;
1738
1739 return 0;
1740}
1741
Sri Deevie0d3baf2009-03-03 14:37:50 -03001742/* RAW VBI ioctls */
1743
1744static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001745 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001746{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001747 struct cx231xx_fh *fh = priv;
1748 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001749
1750 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001751 35468950 : 28636363;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001752 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1753 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1754 f->fmt.vbi.offset = 64 * 4;
1755 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001756 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001757 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001758 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001759 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001760 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001761 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1762
1763 return 0;
1764
1765}
1766
1767static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001768 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001769{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001770 struct cx231xx_fh *fh = priv;
1771 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001772
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001773 if (dev->vbi_stream_on && !fh->stream_on) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001774 cx231xx_errdev("%s device in use by another fh\n", __func__);
1775 return -EBUSY;
1776 }
1777
1778 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1779 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001780 35468950 : 28636363;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001781 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1782 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1783 f->fmt.vbi.offset = 244;
1784 f->fmt.vbi.flags = 0;
1785 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001786 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001787 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001788 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001789 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001790 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001791 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1792
1793 return 0;
1794
1795}
1796
1797static int vidioc_reqbufs(struct file *file, void *priv,
1798 struct v4l2_requestbuffers *rb)
1799{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001800 struct cx231xx_fh *fh = priv;
1801 struct cx231xx *dev = fh->dev;
1802 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001803
1804 rc = check_dev(dev);
1805 if (rc < 0)
1806 return rc;
1807
1808 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1809}
1810
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001811static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001812{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001813 struct cx231xx_fh *fh = priv;
1814 struct cx231xx *dev = fh->dev;
1815 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001816
1817 rc = check_dev(dev);
1818 if (rc < 0)
1819 return rc;
1820
1821 return (videobuf_querybuf(&fh->vb_vidq, b));
1822}
1823
1824static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1825{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001826 struct cx231xx_fh *fh = priv;
1827 struct cx231xx *dev = fh->dev;
1828 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001829
1830 rc = check_dev(dev);
1831 if (rc < 0)
1832 return rc;
1833
1834 return (videobuf_qbuf(&fh->vb_vidq, b));
1835}
1836
1837static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1838{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001839 struct cx231xx_fh *fh = priv;
1840 struct cx231xx *dev = fh->dev;
1841 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001842
1843 rc = check_dev(dev);
1844 if (rc < 0)
1845 return rc;
1846
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001847 return (videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001848}
1849
1850#ifdef CONFIG_VIDEO_V4L1_COMPAT
1851static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1852{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001853 struct cx231xx_fh *fh = priv;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001854
1855 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1856}
1857#endif
1858
Sri Deevie0d3baf2009-03-03 14:37:50 -03001859/* ----------------------------------------------------------- */
1860/* RADIO ESPECIFIC IOCTLS */
1861/* ----------------------------------------------------------- */
1862
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001863static int radio_querycap(struct file *file, void *priv,
Sri Deevie0d3baf2009-03-03 14:37:50 -03001864 struct v4l2_capability *cap)
1865{
1866 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1867
1868 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1869 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1870 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1871
1872 cap->version = CX231XX_VERSION_CODE;
1873 cap->capabilities = V4L2_CAP_TUNER;
1874 return 0;
1875}
1876
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001877static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001878{
1879 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1880
1881 if (unlikely(t->index > 0))
1882 return -EINVAL;
1883
1884 strcpy(t->name, "Radio");
1885 t->type = V4L2_TUNER_RADIO;
1886
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001887 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001888 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001889 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001890
1891 return 0;
1892}
1893
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001894static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001895{
1896 if (i->index != 0)
1897 return -EINVAL;
1898 strcpy(i->name, "Radio");
1899 i->type = V4L2_INPUT_TYPE_TUNER;
1900
1901 return 0;
1902}
1903
1904static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1905{
1906 if (unlikely(a->index))
1907 return -EINVAL;
1908
1909 strcpy(a->name, "Radio");
1910 return 0;
1911}
1912
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001913static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001914{
1915 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1916
1917 if (0 != t->index)
1918 return -EINVAL;
1919
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001920 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001921 cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001922 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001923
1924 return 0;
1925}
1926
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001927static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001928{
1929 return 0;
1930}
1931
1932static int radio_s_input(struct file *file, void *fh, unsigned int i)
1933{
1934 return 0;
1935}
1936
1937static int radio_queryctrl(struct file *file, void *priv,
1938 struct v4l2_queryctrl *c)
1939{
1940 int i;
1941
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001942 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001943 return -EINVAL;
1944 if (c->id == V4L2_CID_AUDIO_MUTE) {
1945 for (i = 0; i < CX231XX_CTLS; i++)
1946 if (cx231xx_ctls[i].v.id == c->id)
1947 break;
1948 *c = cx231xx_ctls[i].v;
1949 } else
1950 *c = no_ctl;
1951 return 0;
1952}
1953
1954/*
1955 * cx231xx_v4l2_open()
1956 * inits the device and starts isoc transfer
1957 */
1958static int cx231xx_v4l2_open(struct file *filp)
1959{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001960 int minor = video_devdata(filp)->minor;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001961 int errCode = 0, radio = 0;
1962 struct cx231xx *dev = NULL;
1963 struct cx231xx_fh *fh;
1964 enum v4l2_buf_type fh_type = 0;
1965
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001966 dev = cx231xx_get_device(minor, &fh_type, &radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001967 if (NULL == dev)
1968 return -ENODEV;
1969
1970 mutex_lock(&dev->lock);
1971
1972 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001973 minor, v4l2_type_names[fh_type], dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001974
1975#if 0
1976 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1977 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001978 cx231xx_errdev
1979 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001980 mutex_unlock(&dev->lock);
1981 return -EBUSY;
1982 }
1983#endif
1984
1985 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1986 if (!fh) {
1987 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1988 mutex_unlock(&dev->lock);
1989 return -ENOMEM;
1990 }
1991 fh->dev = dev;
1992 fh->radio = radio;
1993 fh->type = fh_type;
1994 filp->private_data = fh;
1995
1996 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1997 dev->width = norm_maxw(dev);
1998 dev->height = norm_maxh(dev);
1999 dev->hscale = 0;
2000 dev->vscale = 0;
2001
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002002 /* Power up in Analog TV mode */
2003 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002004
2005#if 0
2006 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2007#endif
2008 cx231xx_resolution_set(dev);
2009
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002010 /* set video alternate setting */
2011 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002012
2013 /* Needed, since GPIO might have disabled power of
2014 some i2c device */
2015 cx231xx_config_i2c(dev);
2016
2017 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002018 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
2019 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002020
2021 }
2022 if (fh->radio) {
2023 cx231xx_videodbg("video_open: setting radio device\n");
2024
2025 /* cx231xx_start_radio(dev); */
2026
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002027 cx231xx_i2c_call_clients(&dev->i2c_bus[1], AUDC_SET_RADIO,
2028 NULL);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002029 }
2030
2031 dev->users++;
2032
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002033 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2034 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops, NULL, &dev->video_mode.slock, fh->type, V4L2_FIELD_INTERLACED, /* V4L2_FIELD_SEQ_TB, */
2035 sizeof(struct cx231xx_buffer), fh);
2036 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002037
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002038 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002039
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002040 /* Set the required alternate setting VBI interface works in Bulk mode only */
2041 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002042
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002043 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops, NULL, &dev->vbi_mode.slock, fh->type, V4L2_FIELD_SEQ_TB, /* V4L2_FIELD_INTERLACED, */
2044 sizeof(struct cx231xx_buffer), fh);
2045 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002046
2047 mutex_unlock(&dev->lock);
2048
2049 return errCode;
2050}
2051
2052/*
2053 * cx231xx_realease_resources()
2054 * unregisters the v4l2,i2c and usb devices
2055 * called when the device gets disconected or at module unload
2056*/
2057void cx231xx_release_analog_resources(struct cx231xx *dev)
2058{
2059
2060 /*FIXME: I2C IR should be disconnected */
2061
2062 if (dev->radio_dev) {
2063 if (-1 != dev->radio_dev->minor)
2064 video_unregister_device(dev->radio_dev);
2065 else
2066 video_device_release(dev->radio_dev);
2067 dev->radio_dev = NULL;
2068 }
2069 if (dev->vbi_dev) {
2070 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002071 dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002072 if (-1 != dev->vbi_dev->minor)
2073 video_unregister_device(dev->vbi_dev);
2074 else
2075 video_device_release(dev->vbi_dev);
2076 dev->vbi_dev = NULL;
2077 }
2078 if (dev->vdev) {
2079 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002080 dev->vdev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002081 if (-1 != dev->vdev->minor)
2082 video_unregister_device(dev->vdev);
2083 else
2084 video_device_release(dev->vdev);
2085 dev->vdev = NULL;
2086 }
2087}
2088
2089/*
2090 * cx231xx_v4l2_close()
2091 * stops streaming and deallocates all resources allocated by the v4l2
2092 * calls and ioctls
2093 */
2094static int cx231xx_v4l2_close(struct file *filp)
2095{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002096 struct cx231xx_fh *fh = filp->private_data;
2097 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002098
2099 cx231xx_videodbg("users=%d\n", dev->users);
2100
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002101 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002102
2103 if (res_check(fh))
2104 res_free(fh);
2105
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002106 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2107 videobuf_stop(&fh->vb_vidq);
2108 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002109
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002110 /* the device is already disconnect,
2111 free the remaining resources */
2112 if (dev->state & DEV_DISCONNECTED) {
2113 cx231xx_release_resources(dev);
2114 mutex_unlock(&dev->lock);
2115 kfree(dev);
2116 return 0;
2117 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002118
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002119 /* do this before setting alternate! */
2120 cx231xx_uninit_vbi_isoc(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002121
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002122 /* set alternate 0 */
2123 if (!dev->vbi_or_sliced_cc_mode) {
2124 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2125 } else {
2126 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2127 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002128
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002129 kfree(fh);
2130 dev->users--;
2131 wake_up_interruptible_nr(&dev->open, 1);
2132 mutex_unlock(&dev->lock);
2133 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002134 }
2135
2136 if (dev->users == 1) {
2137 videobuf_stop(&fh->vb_vidq);
2138 videobuf_mmap_free(&fh->vb_vidq);
2139
2140 /* the device is already disconnect,
2141 free the remaining resources */
2142 if (dev->state & DEV_DISCONNECTED) {
2143 cx231xx_release_resources(dev);
2144 mutex_unlock(&dev->lock);
2145 kfree(dev);
2146 return 0;
2147 }
2148
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002149 /* Save some power by putting tuner to sleep */
2150 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_STANDBY,
2151 NULL);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002152
2153 /* do this before setting alternate! */
2154 cx231xx_uninit_isoc(dev);
2155 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2156
2157 /* set alternate 0 */
2158 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2159 }
2160 kfree(fh);
2161 dev->users--;
2162 wake_up_interruptible_nr(&dev->open, 1);
2163 mutex_unlock(&dev->lock);
2164 return 0;
2165}
2166
2167/*
2168 * cx231xx_v4l2_read()
2169 * will allocate buffers when called for the first time
2170 */
2171static ssize_t
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002172cx231xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
2173 loff_t * pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002174{
2175 struct cx231xx_fh *fh = filp->private_data;
2176 struct cx231xx *dev = fh->dev;
2177 int rc;
2178
2179 rc = check_dev(dev);
2180 if (rc < 0)
2181 return rc;
2182
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002183 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2184 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002185 mutex_lock(&dev->lock);
2186 rc = res_get(fh);
2187 mutex_unlock(&dev->lock);
2188
2189 if (unlikely(rc < 0))
2190 return rc;
2191
2192 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002193 filp->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002194 }
2195 return 0;
2196}
2197
2198/*
2199 * cx231xx_v4l2_poll()
2200 * will allocate buffers when called for the first time
2201 */
2202static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2203{
2204 struct cx231xx_fh *fh = filp->private_data;
2205 struct cx231xx *dev = fh->dev;
2206 int rc;
2207
2208 rc = check_dev(dev);
2209 if (rc < 0)
2210 return rc;
2211
2212 mutex_lock(&dev->lock);
2213 rc = res_get(fh);
2214 mutex_unlock(&dev->lock);
2215
2216 if (unlikely(rc < 0))
2217 return POLLERR;
2218
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002219 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2220 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2221 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2222 else
Sri Deevie0d3baf2009-03-03 14:37:50 -03002223 return POLLERR;
2224}
2225
2226/*
2227 * cx231xx_v4l2_mmap()
2228 */
2229static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2230{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002231 struct cx231xx_fh *fh = filp->private_data;
2232 struct cx231xx *dev = fh->dev;
2233 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002234
2235 rc = check_dev(dev);
2236 if (rc < 0)
2237 return rc;
2238
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002239 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002240 rc = res_get(fh);
2241 mutex_unlock(&dev->lock);
2242
2243 if (unlikely(rc < 0))
2244 return rc;
2245
2246 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2247
2248 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002249 (unsigned long)vma->vm_start,
2250 (unsigned long)vma->vm_end -
2251 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002252
2253 return rc;
2254}
2255
2256static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002257 .owner = THIS_MODULE,
2258 .open = cx231xx_v4l2_open,
2259 .release = cx231xx_v4l2_close,
2260 .read = cx231xx_v4l2_read,
2261 .poll = cx231xx_v4l2_poll,
2262 .mmap = cx231xx_v4l2_mmap,
2263 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002264};
2265
2266static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002267 .vidioc_querycap = vidioc_querycap,
2268 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2269 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2270 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2271 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2272 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2273 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2274 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2275 .vidioc_g_audio = vidioc_g_audio,
2276 .vidioc_s_audio = vidioc_s_audio,
2277 .vidioc_cropcap = vidioc_cropcap,
2278 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002279 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002280 .vidioc_reqbufs = vidioc_reqbufs,
2281 .vidioc_querybuf = vidioc_querybuf,
2282 .vidioc_qbuf = vidioc_qbuf,
2283 .vidioc_dqbuf = vidioc_dqbuf,
2284 .vidioc_s_std = vidioc_s_std,
2285 .vidioc_g_std = vidioc_g_std,
2286 .vidioc_enum_input = vidioc_enum_input,
2287 .vidioc_g_input = vidioc_g_input,
2288 .vidioc_s_input = vidioc_s_input,
2289 .vidioc_queryctrl = vidioc_queryctrl,
2290 .vidioc_g_ctrl = vidioc_g_ctrl,
2291 .vidioc_s_ctrl = vidioc_s_ctrl,
2292 .vidioc_streamon = vidioc_streamon,
2293 .vidioc_streamoff = vidioc_streamoff,
2294 .vidioc_g_tuner = vidioc_g_tuner,
2295 .vidioc_s_tuner = vidioc_s_tuner,
2296 .vidioc_g_frequency = vidioc_g_frequency,
2297 .vidioc_s_frequency = vidioc_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002298#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002299 .vidioc_g_register = vidioc_g_register,
2300 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002301#endif
2302#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002303 .vidiocgmbuf = vidiocgmbuf,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002304#endif
2305};
2306
2307static struct video_device cx231xx_vbi_template;
2308
2309static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002310 .fops = &cx231xx_v4l_fops,
2311 .release = video_device_release,
2312 .ioctl_ops = &video_ioctl_ops,
2313 .minor = -1,
2314 .tvnorms = V4L2_STD_ALL,
2315 .current_norm = V4L2_STD_PAL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002316};
2317
2318static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002319 .owner = THIS_MODULE,
2320 .open = cx231xx_v4l2_open,
2321 .release = cx231xx_v4l2_close,
2322 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002323};
2324
2325static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002326 .vidioc_querycap = radio_querycap,
2327 .vidioc_g_tuner = radio_g_tuner,
2328 .vidioc_enum_input = radio_enum_input,
2329 .vidioc_g_audio = radio_g_audio,
2330 .vidioc_s_tuner = radio_s_tuner,
2331 .vidioc_s_audio = radio_s_audio,
2332 .vidioc_s_input = radio_s_input,
2333 .vidioc_queryctrl = radio_queryctrl,
2334 .vidioc_g_ctrl = vidioc_g_ctrl,
2335 .vidioc_s_ctrl = vidioc_s_ctrl,
2336 .vidioc_g_frequency = vidioc_g_frequency,
2337 .vidioc_s_frequency = vidioc_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002338#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002339 .vidioc_g_register = vidioc_g_register,
2340 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002341#endif
2342};
2343
2344static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002345 .name = "cx231xx-radio",
2346 .fops = &radio_fops,
2347 .ioctl_ops = &radio_ioctl_ops,
2348 .minor = -1,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002349};
2350
2351/******************************** usb interface ******************************/
2352
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002353static struct video_device *cx231xx_vdev_init(struct cx231xx *dev, const struct video_device
2354 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002355{
2356 struct video_device *vfd;
2357
2358 vfd = video_device_alloc();
2359 if (NULL == vfd)
2360 return NULL;
2361 *vfd = *template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002362 vfd->minor = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002363 vfd->parent = &dev->udev->dev;
2364 vfd->release = video_device_release;
2365 vfd->debug = video_debug;
2366
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002367 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002368
2369 return vfd;
2370}
2371
2372int cx231xx_register_analog_devices(struct cx231xx *dev)
2373{
2374 int ret;
2375
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002376 cx231xx_info("%s()\n", __func__);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002377
2378 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002379 dev->name,
2380 (CX231XX_VERSION_CODE >> 16) & 0xff,
2381 (CX231XX_VERSION_CODE >> 8) & 0xff,
2382 CX231XX_VERSION_CODE & 0xff);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002383
2384 /* set default norm */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002385 /*dev->norm = cx231xx_video_template.current_norm; */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002386 dev->width = norm_maxw(dev);
2387 dev->height = norm_maxh(dev);
2388 dev->interlaced = 0;
2389 dev->hscale = 0;
2390 dev->vscale = 0;
2391
2392 /* Analog specific initialization */
2393 dev->format = &format[0];
2394 /* video_mux(dev, dev->video_input); */
2395
2396 /* Audio defaults */
2397 dev->mute = 1;
2398 dev->volume = 0x1f;
2399
2400 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002401 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002402
2403 /* allocate and fill video video_device struct */
2404 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2405 if (!dev->vdev) {
2406 cx231xx_errdev("cannot allocate video_device.\n");
2407 return -ENODEV;
2408 }
2409
2410 /* register v4l2 video video_device */
2411 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002412 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002413 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002414 cx231xx_errdev("unable to register video device (error=%i).\n",
2415 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002416 return ret;
2417 }
2418
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002419 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2420 dev->name, dev->vdev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002421
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002422 /* Initialize VBI template */
2423 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2424 sizeof(cx231xx_vbi_template));
2425 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002426
2427 /* Allocate and fill vbi video_device struct */
2428 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2429
2430 /* register v4l2 vbi video_device */
2431 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002432 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002433 if (ret < 0) {
2434 cx231xx_errdev("unable to register vbi device\n");
2435 return ret;
2436 }
2437
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002438 cx231xx_info("%s/0: registered device vbi%d\n",
2439 dev->name, dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002440
2441 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002442 dev->radio_dev =
2443 cx231xx_vdev_init(dev, &cx231xx_radio_template, "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002444 if (!dev->radio_dev) {
2445 cx231xx_errdev("cannot allocate video_device.\n");
2446 return -ENODEV;
2447 }
2448 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2449 radio_nr[dev->devno]);
2450 if (ret < 0) {
2451 cx231xx_errdev("can't register radio device\n");
2452 return ret;
2453 }
2454 cx231xx_info("Registered radio device as /dev/radio%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002455 dev->radio_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002456 }
2457
2458 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002459 dev->vdev->num, dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002460
2461 return 0;
2462}