blob: 6a524d847110f7691daf0cf60a4f1bfbf47e64dc [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002 cx231xx-video.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
Sri Deevie0d3baf2009-03-03 14:37:50 -03004
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03006 Based on em28xx driver
7 Based on cx23885 driver
8 Based on cx88 driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
Sri Deevie0d3baf2009-03-03 14:37:50 -030025#include <linux/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/bitmap.h>
30#include <linux/usb.h>
31#include <linux/i2c.h>
32#include <linux/version.h>
33#include <linux/mm.h>
34#include <linux/mutex.h>
35
36#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-chip-ident.h>
39#include <media/msp3400.h>
40#include <media/tuner.h>
41
42#include "dvb_frontend.h"
43
44#include "cx231xx.h"
45#include "cx231xx-vbi.h"
46
Mauro Carvalho Chehab818fdf32009-03-13 07:41:58 -030047#define CX231XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
48
Sri Deevie0d3baf2009-03-03 14:37:50 -030049#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
50#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
51
Sri Deevie0d3baf2009-03-03 14:37:50 -030052#define cx231xx_videodbg(fmt, arg...) do {\
53 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0)
56
57static unsigned int isoc_debug;
58module_param(isoc_debug, int, 0644);
59MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
60
61#define cx231xx_isocdbg(fmt, arg...) \
62do {\
63 if (isoc_debug) { \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); \
66 } \
67 } while (0)
68
69MODULE_AUTHOR(DRIVER_AUTHOR);
70MODULE_DESCRIPTION(DRIVER_DESC);
71MODULE_LICENSE("GPL");
72
Sri Deevie0d3baf2009-03-03 14:37:50 -030073static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
74static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030078module_param_array(card, int, NULL, 0444);
Sri Deevie0d3baf2009-03-03 14:37:50 -030079module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
81module_param_array(radio_nr, int, NULL, 0444);
82
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030083MODULE_PARM_DESC(card, "card type");
Sri Deevie0d3baf2009-03-03 14:37:50 -030084MODULE_PARM_DESC(video_nr, "video device numbers");
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030085MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
Sri Deevie0d3baf2009-03-03 14:37:50 -030086MODULE_PARM_DESC(radio_nr, "radio device numbers");
87
88static unsigned int video_debug;
89module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
91
Sri Deevie0d3baf2009-03-03 14:37:50 -030092/* supported video standards */
93static struct cx231xx_fmt format[] = {
94 {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030095 .name = "16bpp YUY2, 4:2:2, packed",
96 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
98 .reg = 0,
99 },
Sri Deevie0d3baf2009-03-03 14:37:50 -0300100};
101
Sri Deevie0d3baf2009-03-03 14:37:50 -0300102/* supported controls */
103/* Common to all boards */
104
105/* ------------------------------------------------------------------- */
106
107static const struct v4l2_queryctrl no_ctl = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300108 .name = "42",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300109 .flags = V4L2_CTRL_FLAG_DISABLED,
110};
111
112static struct cx231xx_ctrl cx231xx_ctls[] = {
113 /* --- video --- */
114 {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300115 .v = {
116 .id = V4L2_CID_BRIGHTNESS,
117 .name = "Brightness",
118 .minimum = 0x00,
119 .maximum = 0xff,
120 .step = 1,
121 .default_value = 0x7f,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 },
124 .off = 128,
125 .reg = LUMA_CTRL,
126 .mask = 0x00ff,
127 .shift = 0,
128 }, {
129 .v = {
130 .id = V4L2_CID_CONTRAST,
131 .name = "Contrast",
132 .minimum = 0,
133 .maximum = 0xff,
134 .step = 1,
135 .default_value = 0x3f,
136 .type = V4L2_CTRL_TYPE_INTEGER,
137 },
138 .off = 0,
139 .reg = LUMA_CTRL,
140 .mask = 0xff00,
141 .shift = 8,
142 }, {
143 .v = {
144 .id = V4L2_CID_HUE,
145 .name = "Hue",
146 .minimum = 0,
147 .maximum = 0xff,
148 .step = 1,
149 .default_value = 0x7f,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 },
152 .off = 128,
153 .reg = CHROMA_CTRL,
154 .mask = 0xff0000,
155 .shift = 16,
156 }, {
157 /* strictly, this only describes only U saturation.
158 * V saturation is handled specially through code.
159 */
160 .v = {
161 .id = V4L2_CID_SATURATION,
162 .name = "Saturation",
163 .minimum = 0,
164 .maximum = 0xff,
165 .step = 1,
166 .default_value = 0x7f,
167 .type = V4L2_CTRL_TYPE_INTEGER,
168 },
169 .off = 0,
170 .reg = CHROMA_CTRL,
171 .mask = 0x00ff,
172 .shift = 0,
173 }, {
174 /* --- audio --- */
175 .v = {
176 .id = V4L2_CID_AUDIO_MUTE,
177 .name = "Mute",
178 .minimum = 0,
179 .maximum = 1,
180 .default_value = 1,
181 .type = V4L2_CTRL_TYPE_BOOLEAN,
182 },
183 .reg = PATH1_CTL1,
184 .mask = (0x1f << 24),
185 .shift = 24,
186 }, {
187 .v = {
188 .id = V4L2_CID_AUDIO_VOLUME,
189 .name = "Volume",
190 .minimum = 0,
191 .maximum = 0x3f,
192 .step = 1,
193 .default_value = 0x3f,
194 .type = V4L2_CTRL_TYPE_INTEGER,
195 },
196 .reg = PATH1_VOL_CTL,
197 .mask = 0xff,
198 .shift = 0,
199 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300200};
201static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
202
203static const u32 cx231xx_user_ctrls[] = {
204 V4L2_CID_USER_CLASS,
205 V4L2_CID_BRIGHTNESS,
206 V4L2_CID_CONTRAST,
207 V4L2_CID_SATURATION,
208 V4L2_CID_HUE,
209 V4L2_CID_AUDIO_VOLUME,
210#if 0
211 V4L2_CID_AUDIO_BALANCE,
212#endif
213 V4L2_CID_AUDIO_MUTE,
214 0
215};
216
217static const u32 *ctrl_classes[] = {
218 cx231xx_user_ctrls,
219 NULL
220};
221
Sri Deevie0d3baf2009-03-03 14:37:50 -0300222/* ------------------------------------------------------------------
223 Video buffer and parser functions
224 ------------------------------------------------------------------*/
225
226/*
227 * Announces that a buffer were filled and request the next
228 */
229static inline void buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300230 struct cx231xx_dmaqueue *dma_q,
231 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300232{
233 /* Advice that buffer was filled */
234 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
235 buf->vb.state = VIDEOBUF_DONE;
236 buf->vb.field_count++;
237 do_gettimeofday(&buf->vb.ts);
238
239 dev->video_mode.isoc_ctl.buf = NULL;
240
241 list_del(&buf->vb.queue);
242 wake_up(&buf->vb.done);
243}
244
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300245static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300246{
247 char *errmsg = "Unknown";
248
249 switch (status) {
250 case -ENOENT:
251 errmsg = "unlinked synchronuously";
252 break;
253 case -ECONNRESET:
254 errmsg = "unlinked asynchronuously";
255 break;
256 case -ENOSR:
257 errmsg = "Buffer error (overrun)";
258 break;
259 case -EPIPE:
260 errmsg = "Stalled (device not responding)";
261 break;
262 case -EOVERFLOW:
263 errmsg = "Babble (bad cable?)";
264 break;
265 case -EPROTO:
266 errmsg = "Bit-stuff error (bad cable?)";
267 break;
268 case -EILSEQ:
269 errmsg = "CRC/Timeout (could be anything)";
270 break;
271 case -ETIME:
272 errmsg = "Device does not respond";
273 break;
274 }
275 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300276 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300277 } else {
278 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300279 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300280 }
281}
282
283/*
284 * video-buf generic routine to get the next available buffer
285 */
286static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300287 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300288{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300289 struct cx231xx_video_mode *vmode =
290 container_of(dma_q, struct cx231xx_video_mode, vidq);
291 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300292
293 char *outp;
294
Sri Deevie0d3baf2009-03-03 14:37:50 -0300295 if (list_empty(&dma_q->active)) {
296 cx231xx_isocdbg("No active queue to serve\n");
297 dev->video_mode.isoc_ctl.buf = NULL;
298 *buf = NULL;
299 return;
300 }
301
302 /* Get the next buffer */
303 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
304
305 /* Cleans up buffer - Usefull for testing for frame/URB loss */
306 outp = videobuf_to_vmalloc(&(*buf)->vb);
307 memset(outp, 0, (*buf)->vb.size);
308
309 dev->video_mode.isoc_ctl.buf = *buf;
310
311 return;
312}
313
314/*
315 * Controls the isoc copy of each urb packet
316 */
317static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
318{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300319 struct cx231xx_buffer *buf;
320 struct cx231xx_dmaqueue *dma_q = urb->context;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300321 unsigned char *outp = NULL;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300322 int i, rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300323 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300324 u32 bytes_parsed = 0, buffer_size = 0;
325 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300326
327 if (!dev)
328 return 0;
329
330 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
331 return 0;
332
333 if (urb->status < 0) {
334 print_err_status(dev, -1, urb->status);
335 if (urb->status == -ENOENT)
336 return 0;
337 }
338
339 buf = dev->video_mode.isoc_ctl.buf;
340 if (buf != NULL)
341 outp = videobuf_to_vmalloc(&buf->vb);
342
343 for (i = 0; i < urb->number_of_packets; i++) {
344 int status = urb->iso_frame_desc[i].status;
345
346 if (status < 0) {
347 print_err_status(dev, i, status);
348 if (urb->iso_frame_desc[i].status != -EPROTO)
349 continue;
350 }
351
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300352 if (urb->iso_frame_desc[i].actual_length <= 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300353 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
354 continue;
355 }
356 if (urb->iso_frame_desc[i].actual_length >
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300357 dev->video_mode.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300358 cx231xx_isocdbg("packet bigger than packet size");
359 continue;
360 }
361
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300362 /* get buffer pointer and length */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300363 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300364 buffer_size = urb->iso_frame_desc[i].actual_length;
365 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300366
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300367 if (dma_q->is_partial_line) {
Sri Deevib9255172009-03-04 17:49:01 -0300368 /* Handle the case of a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300369 sav_eav = dma_q->last_sav;
370 } else {
Sri Deevib9255172009-03-04 17:49:01 -0300371 /* Check for a SAV/EAV overlapping
372 the buffer boundary */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300373 sav_eav =
374 cx231xx_find_boundary_SAV_EAV(p_buffer,
375 dma_q->partial_buf,
376 &bytes_parsed);
377 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300378
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300379 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300380 /* Get the first line if we have some portion of an SAV/EAV from
381 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300382 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300383 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
Sri Deevib9255172009-03-04 17:49:01 -0300384 sav_eav, /* SAV/EAV */
385 p_buffer + bytes_parsed, /* p_buffer */
386 buffer_size - bytes_parsed);/* buf size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300387 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300388
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300389 /* Now parse data that is completely in this buffer */
390 /* dma_q->is_partial_line = 0; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300391
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300392 while (bytes_parsed < buffer_size) {
393 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300394
Sri Deevib9255172009-03-04 17:49:01 -0300395 sav_eav = cx231xx_find_next_SAV_EAV(
396 p_buffer + bytes_parsed, /* p_buffer */
397 buffer_size - bytes_parsed, /* buf size */
398 &bytes_used);/* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300399
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300400 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300401
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300402 sav_eav &= 0xF0;
403 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300404 bytes_parsed += cx231xx_get_video_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300405 dma_q, sav_eav, /* SAV/EAV */
406 p_buffer + bytes_parsed,/* p_buffer */
407 buffer_size - bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300408 }
409 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300410
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300411 /* Save the last four bytes of the buffer so we can check the
412 buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300413 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
414 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300415
416 }
417 return rc;
418}
419
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300420u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
421 u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300422{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300423 u32 bytes_used;
424 u8 boundary_bytes[8];
425 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300426
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300427 *p_bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300428
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300429 /* Create an array of the last 4 bytes of the last buffer and the first
430 4 bytes of the current buffer. */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300431
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300432 memcpy(boundary_bytes, partial_buf, 4);
433 memcpy(boundary_bytes + 4, p_buffer, 4);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300434
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300435 /* Check for the SAV/EAV in the boundary buffer */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300436 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
437 &bytes_used);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300438
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300439 if (sav_eav) {
440 /* found a boundary SAV/EAV. Updates the bytes used to reflect
441 only those used in the new buffer */
442 *p_bytes_used = bytes_used - 4;
443 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300444
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300445 return sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300446}
447
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300448u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300449{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300450 u32 i;
451 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300452
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300453 /*
454 * Don't search if the buffer size is less than 4. It causes a page
455 * fault since buffer_size - 4 evaluates to a large number in that
456 * case.
457 */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300458 if (buffer_size < 4) {
459 *p_bytes_used = buffer_size;
460 return 0;
461 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300462
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300463 for (i = 0; i < (buffer_size - 3); i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300464
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300465 if ((p_buffer[i] == 0xFF) &&
466 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300467
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300468 *p_bytes_used = i + 4;
469 sav_eav = p_buffer[i + 3];
470 return sav_eav;
471 }
472 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300473
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300474 *p_bytes_used = buffer_size;
475 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300476}
477
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300478u32 cx231xx_get_video_line(struct cx231xx *dev,
479 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
480 u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300481{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300482 u32 bytes_copied = 0;
483 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300484
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300485 switch (sav_eav) {
486 case SAV_ACTIVE_VIDEO_FIELD1:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300487 /* looking for skipped line which occurred in PAL 720x480 mode.
488 In this case, there will be no active data contained
489 between the SAV and EAV */
490 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
491 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
492 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
493 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
494 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
495 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300496 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300497 current_field = 1;
498 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300499
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300500 case SAV_ACTIVE_VIDEO_FIELD2:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300501 /* looking for skipped line which occurred in PAL 720x480 mode.
502 In this case, there will be no active data contained between
503 the SAV and EAV */
504 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
505 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
506 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
507 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
508 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
509 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300510 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300511 current_field = 2;
512 break;
513 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300514
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300515 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300516
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300517 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
518 buffer_size, current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300520 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300521}
522
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300523u32 cx231xx_copy_video_line(struct cx231xx *dev,
524 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300525 u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300526{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300527 u32 bytes_to_copy;
528 struct cx231xx_buffer *buf;
529 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300530
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300531 if (dma_q->current_field != field_number)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300532 cx231xx_reset_video_buffer(dev, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300533
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300534 /* get the buffer pointer */
535 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300536
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300537 /* Remember the field number for next time */
538 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300539
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300540 bytes_to_copy = dma_q->bytes_left_in_line;
541 if (bytes_to_copy > length)
542 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300543
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300544 if (dma_q->lines_completed >= dma_q->lines_per_field) {
545 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300546 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
547 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300548 return 0;
549 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300550
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300551 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300552
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300553 /* If we don't have a buffer, just return the number of bytes we would
554 have copied if we had a buffer. */
555 if (!buf) {
556 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300557 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
558 ? 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300559 return bytes_to_copy;
560 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300561
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300562 /* copy the data to video buffer */
563 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300564
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300565 dma_q->pos += bytes_to_copy;
566 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300567
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300568 if (dma_q->bytes_left_in_line == 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300569 dma_q->bytes_left_in_line = _line_size;
570 dma_q->lines_completed++;
571 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300572
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300573 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
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) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300592 if (dma_q->lines_completed >= dma_q->lines_per_field)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300593 dma_q->field1_done = 1;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300594 else
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300595 dma_q->field1_done = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300596 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300597
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300598 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300599
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300600 if (buf == NULL) {
601 u8 *outp = NULL;
602 /* first try to get the buffer */
603 get_next_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300604
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300605 if (buf)
606 outp = videobuf_to_vmalloc(&buf->vb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300607
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300608 dma_q->pos = 0;
609 dma_q->field1_done = 0;
610 dma_q->current_field = -1;
611 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300612
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300613 /* reset the counters */
614 dma_q->bytes_left_in_line = dev->width << 1;
615 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300616}
617
618int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300619 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300620{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300621 u8 *p_out_buffer = NULL;
622 u32 current_line_bytes_copied = 0;
623 struct cx231xx_buffer *buf;
624 u32 _line_size = dev->width << 1;
625 void *startwrite;
626 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300627
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300628 buf = dev->video_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300629
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300630 if (buf == NULL)
631 return -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300632
633 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
634
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300635 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300636
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300637 /* Offset field 2 one line from the top of the buffer */
638 offset = (dma_q->current_field == 1) ? 0 : _line_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300639
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300640 /* Offset for field 2 */
641 startwrite = p_out_buffer + offset;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300642
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300643 /* lines already completed in the current field */
644 startwrite += (dma_q->lines_completed * _line_size * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300645
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300646 /* bytes already completed in the current line */
647 startwrite += current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300648
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300649 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
650 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300651
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300652 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300653 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300654
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300655 /* The below copies the UYVY data straight into video buffer */
656 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300657
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300658 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300659}
660
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300661void cx231xx_swab(u16 *from, u16 *to, u16 len)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300662{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300663 u16 i;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300664
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300665 if (len <= 0)
666 return;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300667
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300668 for (i = 0; i < len / 2; i++)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300669 to[i] = (from[i] << 8) | (from[i] >> 8);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300670}
671
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300672u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300673{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300674 u8 buffer_complete = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300675
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300676 /* Dual field stream */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300677 buffer_complete = ((dma_q->current_field == 2) &&
678 (dma_q->lines_completed >= dma_q->lines_per_field) &&
679 dma_q->field1_done);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300680
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300681 return buffer_complete;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300682}
683
Sri Deevie0d3baf2009-03-03 14:37:50 -0300684/* ------------------------------------------------------------------
685 Videobuf operations
686 ------------------------------------------------------------------*/
687
688static int
689buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
690{
691 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300692 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300693 struct v4l2_frequency f;
694
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300695 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300696 if (0 == *count)
697 *count = CX231XX_DEF_BUF;
698
699 if (*count < CX231XX_MIN_BUF)
700 *count = CX231XX_MIN_BUF;
701
702 /* Ask tuner to go to analog mode */
703 memset(&f, 0, sizeof(f));
704 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300705 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300706
Sri Deevib1196122009-03-20 23:33:48 -0300707 call_all(dev, tuner, s_frequency, &f);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300708
709 return 0;
710}
711
712/* This is called *without* dev->slock held; please keep it that way */
713static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
714{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300715 struct cx231xx_fh *fh = vq->priv_data;
716 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300717 unsigned long flags = 0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300718
Sri Deevie0d3baf2009-03-03 14:37:50 -0300719 if (in_interrupt())
720 BUG();
721
722 /* We used to wait for the buffer to finish here, but this didn't work
723 because, as we were keeping the state as VIDEOBUF_QUEUED,
724 videobuf_queue_cancel marked it as finished for us.
725 (Also, it could wedge forever if the hardware was misconfigured.)
726
727 This should be safe; by the time we get here, the buffer isn't
728 queued anymore. If we ever start marking the buffers as
729 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300730 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300731 spin_lock_irqsave(&dev->video_mode.slock, flags);
732 if (dev->video_mode.isoc_ctl.buf == buf)
733 dev->video_mode.isoc_ctl.buf = NULL;
734 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
735
736 videobuf_vmalloc_free(&buf->vb);
737 buf->vb.state = VIDEOBUF_NEEDS_INIT;
738}
739
740static int
741buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300742 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300743{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300744 struct cx231xx_fh *fh = vq->priv_data;
745 struct cx231xx_buffer *buf =
746 container_of(vb, struct cx231xx_buffer, vb);
747 struct cx231xx *dev = fh->dev;
748 int rc = 0, urb_init = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300749
750 /* The only currently supported format is 16 bits/pixel */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300751 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
752 + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300753 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300754 return -EINVAL;
755
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300756 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300757 buf->vb.height = dev->height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300758 buf->vb.field = field;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300759
760 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
761 rc = videobuf_iolock(vq, &buf->vb, NULL);
762 if (rc < 0)
763 goto fail;
764 }
765
766 if (!dev->video_mode.isoc_ctl.num_bufs)
767 urb_init = 1;
768
769 if (urb_init) {
770 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300771 CX231XX_NUM_BUFS,
772 dev->video_mode.max_pkt_size,
773 cx231xx_isoc_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300774 if (rc < 0)
775 goto fail;
776 }
777
778 buf->vb.state = VIDEOBUF_PREPARED;
779 return 0;
780
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300781fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300782 free_buffer(vq, buf);
783 return rc;
784}
785
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300786static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300787{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300788 struct cx231xx_buffer *buf =
789 container_of(vb, struct cx231xx_buffer, vb);
790 struct cx231xx_fh *fh = vq->priv_data;
791 struct cx231xx *dev = fh->dev;
792 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300793
794 buf->vb.state = VIDEOBUF_QUEUED;
795 list_add_tail(&buf->vb.queue, &vidq->active);
796
797}
798
799static void buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300800 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300801{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300802 struct cx231xx_buffer *buf =
803 container_of(vb, struct cx231xx_buffer, vb);
804 struct cx231xx_fh *fh = vq->priv_data;
805 struct cx231xx *dev = (struct cx231xx *)fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300806
807 cx231xx_isocdbg("cx231xx: called buffer_release\n");
808
809 free_buffer(vq, buf);
810}
811
812static struct videobuf_queue_ops cx231xx_video_qops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300813 .buf_setup = buffer_setup,
814 .buf_prepare = buffer_prepare,
815 .buf_queue = buffer_queue,
816 .buf_release = buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300817};
818
819/********************* v4l2 interface **************************************/
820
Sri Deevie0d3baf2009-03-03 14:37:50 -0300821void video_mux(struct cx231xx *dev, int index)
822{
Sri Deevie0d3baf2009-03-03 14:37:50 -0300823 dev->video_input = index;
824 dev->ctl_ainput = INPUT(index)->amux;
825
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300826 cx231xx_set_video_input_mux(dev, index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300827
Hans Verkuil5325b422009-04-02 11:26:22 -0300828 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300829
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300830 cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300831
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300832 cx231xx_info("video_mux : %d\n", index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300833
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300834 /* do mode control overrides if required */
835 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300836}
837
838/* Usage lock check functions */
839static int res_get(struct cx231xx_fh *fh)
840{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300841 struct cx231xx *dev = fh->dev;
842 int rc = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300843
844 /* This instance already has stream_on */
845 if (fh->stream_on)
846 return rc;
847
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300848 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
849 if (dev->stream_on)
850 return -EBUSY;
851 dev->stream_on = 1;
852 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
853 if (dev->vbi_stream_on)
854 return -EBUSY;
855 dev->vbi_stream_on = 1;
856 } else
857 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300858
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300859 fh->stream_on = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300860
861 return rc;
862}
863
864static int res_check(struct cx231xx_fh *fh)
865{
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300866 return fh->stream_on;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300867}
868
869static void res_free(struct cx231xx_fh *fh)
870{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300871 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300872
873 fh->stream_on = 0;
874
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300875 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
876 dev->stream_on = 0;
877 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
878 dev->vbi_stream_on = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300879}
880
881static int check_dev(struct cx231xx *dev)
882{
883 if (dev->state & DEV_DISCONNECTED) {
884 cx231xx_errdev("v4l2 ioctl: device not present\n");
885 return -ENODEV;
886 }
887
888 if (dev->state & DEV_MISCONFIGURED) {
889 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300890 "close and open it again\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300891 return -EIO;
892 }
893 return 0;
894}
895
896void get_scale(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300897 unsigned int width, unsigned int height,
898 unsigned int *hscale, unsigned int *vscale)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300899{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300900 unsigned int maxw = norm_maxw(dev);
901 unsigned int maxh = norm_maxh(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300902
903 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
904 if (*hscale >= 0x4000)
905 *hscale = 0x3fff;
906
907 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
908 if (*vscale >= 0x4000)
909 *vscale = 0x3fff;
910
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300911 dev->hscale = *hscale;
912 dev->vscale = *vscale;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300913
914}
915
916/* ------------------------------------------------------------------
917 IOCTL vidioc handling
918 ------------------------------------------------------------------*/
919
920static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300921 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300922{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300923 struct cx231xx_fh *fh = priv;
924 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300925
926 mutex_lock(&dev->lock);
927
928 f->fmt.pix.width = dev->width;
929 f->fmt.pix.height = dev->height;
930 f->fmt.pix.pixelformat = dev->format->fourcc;;
931 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300932 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300933 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
934
935 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
936
937 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300938
Sri Deevie0d3baf2009-03-03 14:37:50 -0300939 return 0;
940}
941
942static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
943{
944 unsigned int i;
945
946 for (i = 0; i < ARRAY_SIZE(format); i++)
947 if (format[i].fourcc == fourcc)
948 return &format[i];
949
950 return NULL;
951}
952
953static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300954 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300955{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300956 struct cx231xx_fh *fh = priv;
957 struct cx231xx *dev = fh->dev;
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300958 unsigned int width = f->fmt.pix.width;
959 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300960 unsigned int maxw = norm_maxw(dev);
961 unsigned int maxh = norm_maxh(dev);
962 unsigned int hscale, vscale;
963 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300964
965 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
966 if (!fmt) {
967 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300968 f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300969 return -EINVAL;
970 }
971
972 /* width must even because of the YUYV format
973 height must be even because of interlacing */
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300974 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300975
976 get_scale(dev, width, height, &hscale, &vscale);
977
978 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
979 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
980
981 f->fmt.pix.width = width;
982 f->fmt.pix.height = height;
983 f->fmt.pix.pixelformat = fmt->fourcc;
984 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
985 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
986 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
987 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
988
989 return 0;
990}
991
992static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300993 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300994{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300995 struct cx231xx_fh *fh = priv;
996 struct cx231xx *dev = fh->dev;
997 int rc;
998 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300999
1000 rc = check_dev(dev);
1001 if (rc < 0)
1002 return rc;
1003
Sri Deevie0d3baf2009-03-03 14:37:50 -03001004 mutex_lock(&dev->lock);
1005
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001006 vidioc_try_fmt_vid_cap(file, priv, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001007
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001008 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001009 if (!fmt) {
1010 rc = -EINVAL;
1011 goto out;
1012 }
1013
1014 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1015 cx231xx_errdev("%s queue busy\n", __func__);
1016 rc = -EBUSY;
1017 goto out;
1018 }
1019
1020 if (dev->stream_on && !fh->stream_on) {
1021 cx231xx_errdev("%s device in use by another fh\n", __func__);
1022 rc = -EBUSY;
1023 goto out;
1024 }
1025
1026 /* set new image size */
1027 dev->width = f->fmt.pix.width;
1028 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001029 dev->format = fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001030 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1031
Sri Deevib1196122009-03-20 23:33:48 -03001032 call_all(dev, video, s_fmt, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001033
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001034 /* Set the correct alternate setting for this resolution */
Sri Deevie0d3baf2009-03-03 14:37:50 -03001035 cx231xx_resolution_set(dev);
1036
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001037out:
Sri Deevie0d3baf2009-03-03 14:37:50 -03001038 mutex_unlock(&dev->lock);
1039 return rc;
1040}
1041
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001042static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001043{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001044 struct cx231xx_fh *fh = priv;
1045 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001046
1047 *id = dev->norm;
1048 return 0;
1049}
1050
Sri Deevib1196122009-03-20 23:33:48 -03001051static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001052{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001053 struct cx231xx_fh *fh = priv;
1054 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001055 struct v4l2_format f;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001056 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001057
1058 rc = check_dev(dev);
1059 if (rc < 0)
1060 return rc;
1061
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001062 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001063
1064 mutex_lock(&dev->lock);
1065 dev->norm = *norm;
1066
Sri Deevie0d3baf2009-03-03 14:37:50 -03001067 /* Adjusts width/height, if needed */
1068 f.fmt.pix.width = dev->width;
1069 f.fmt.pix.height = dev->height;
1070 vidioc_try_fmt_vid_cap(file, priv, &f);
1071
1072 /* set new image size */
1073 dev->width = f.fmt.pix.width;
1074 dev->height = f.fmt.pix.height;
1075 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1076
Hans Verkuilf41737e2009-04-01 03:52:39 -03001077 call_all(dev, core, s_std, dev->norm);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001078
1079 mutex_unlock(&dev->lock);
1080
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001081 cx231xx_resolution_set(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001082
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001083 /* do mode control overrides */
1084 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001085
1086 return 0;
1087}
1088
1089static const char *iname[] = {
1090 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001091 [CX231XX_VMUX_SVIDEO] = "S-Video",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001092 [CX231XX_VMUX_TELEVISION] = "Television",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001093 [CX231XX_VMUX_CABLE] = "Cable TV",
1094 [CX231XX_VMUX_DVB] = "DVB",
1095 [CX231XX_VMUX_DEBUG] = "for debug only",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001096};
1097
1098static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001099 struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001100{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001101 struct cx231xx_fh *fh = priv;
1102 struct cx231xx *dev = fh->dev;
1103 unsigned int n;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001104
1105 n = i->index;
1106 if (n >= MAX_CX231XX_INPUT)
1107 return -EINVAL;
1108 if (0 == INPUT(n)->type)
1109 return -EINVAL;
1110
1111 i->index = n;
1112 i->type = V4L2_INPUT_TYPE_CAMERA;
1113
1114 strcpy(i->name, iname[INPUT(n)->type]);
1115
1116 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001117 (CX231XX_VMUX_CABLE == INPUT(n)->type))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001118 i->type = V4L2_INPUT_TYPE_TUNER;
1119
1120 i->std = dev->vdev->tvnorms;
1121
1122 return 0;
1123}
1124
1125static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1126{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001127 struct cx231xx_fh *fh = priv;
1128 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001129
1130 *i = dev->video_input;
1131
1132 return 0;
1133}
1134
1135static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1136{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001137 struct cx231xx_fh *fh = priv;
1138 struct cx231xx *dev = fh->dev;
1139 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001140
1141 rc = check_dev(dev);
1142 if (rc < 0)
1143 return rc;
1144
1145 if (i >= MAX_CX231XX_INPUT)
1146 return -EINVAL;
1147 if (0 == INPUT(i)->type)
1148 return -EINVAL;
1149
1150 mutex_lock(&dev->lock);
1151
1152 video_mux(dev, i);
1153
1154 mutex_unlock(&dev->lock);
1155 return 0;
1156}
1157
1158static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1159{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001160 struct cx231xx_fh *fh = priv;
1161 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001162
1163 switch (a->index) {
1164 case CX231XX_AMUX_VIDEO:
1165 strcpy(a->name, "Television");
1166 break;
1167 case CX231XX_AMUX_LINE_IN:
1168 strcpy(a->name, "Line In");
1169 break;
1170 default:
1171 return -EINVAL;
1172 }
1173
1174 a->index = dev->ctl_ainput;
1175 a->capability = V4L2_AUDCAP_STEREO;
1176
1177 return 0;
1178}
1179
1180static int vidioc_s_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;
1184 int status = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001185
1186 /* Doesn't allow manual routing */
1187 if (a->index != dev->ctl_ainput)
1188 return -EINVAL;
1189
1190 dev->ctl_ainput = INPUT(a->index)->amux;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001191 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001192
1193 return status;
1194}
1195
1196static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001197 struct v4l2_queryctrl *qc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001198{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001199 struct cx231xx_fh *fh = priv;
1200 struct cx231xx *dev = fh->dev;
1201 int id = qc->id;
1202 int i;
1203 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001204
1205 rc = check_dev(dev);
1206 if (rc < 0)
1207 return rc;
1208
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001209 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001210 if (unlikely(qc->id == 0))
1211 return -EINVAL;
1212
1213 memset(qc, 0, sizeof(*qc));
1214
1215 qc->id = id;
1216
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001217 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001218 return -EINVAL;
1219
1220 for (i = 0; i < CX231XX_CTLS; i++)
1221 if (cx231xx_ctls[i].v.id == qc->id)
1222 break;
1223
1224 if (i == CX231XX_CTLS) {
1225 *qc = no_ctl;
1226 return 0;
1227 }
1228 *qc = cx231xx_ctls[i].v;
1229
1230 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001231 call_all(dev, core, queryctrl, qc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001232 mutex_unlock(&dev->lock);
1233
1234 if (qc->type)
1235 return 0;
1236 else
1237 return -EINVAL;
1238}
1239
1240static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001241 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001242{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001243 struct cx231xx_fh *fh = priv;
1244 struct cx231xx *dev = fh->dev;
1245 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001246
1247 rc = check_dev(dev);
1248 if (rc < 0)
1249 return rc;
1250
1251 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001252 call_all(dev, core, g_ctrl, ctrl);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001253 mutex_unlock(&dev->lock);
1254 return rc;
1255}
1256
1257static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001258 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001259{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001260 struct cx231xx_fh *fh = priv;
1261 struct cx231xx *dev = fh->dev;
1262 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001263
1264 rc = check_dev(dev);
1265 if (rc < 0)
1266 return rc;
1267
1268 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001269 call_all(dev, core, s_ctrl, ctrl);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001270 mutex_unlock(&dev->lock);
1271 return rc;
1272}
1273
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001274static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001275{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001276 struct cx231xx_fh *fh = priv;
1277 struct cx231xx *dev = fh->dev;
1278 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001279
1280 rc = check_dev(dev);
1281 if (rc < 0)
1282 return rc;
1283
1284 if (0 != t->index)
1285 return -EINVAL;
1286
1287 strcpy(t->name, "Tuner");
1288
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001289 t->type = V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001290 t->capability = V4L2_TUNER_CAP_NORM;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001291 t->rangehigh = 0xffffffffUL;
1292 t->signal = 0xffff; /* LOCKED */
Sri Deevie0d3baf2009-03-03 14:37:50 -03001293
1294 return 0;
1295}
1296
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001297static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001298{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001299 struct cx231xx_fh *fh = priv;
1300 struct cx231xx *dev = fh->dev;
1301 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001302
1303 rc = check_dev(dev);
1304 if (rc < 0)
1305 return rc;
1306
1307 if (0 != t->index)
1308 return -EINVAL;
1309#if 0
1310 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001311 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001312 mutex_unlock(&dev->lock);
1313#endif
1314 return 0;
1315}
1316
1317static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001318 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001319{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001320 struct cx231xx_fh *fh = priv;
1321 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001322
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001323 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001324 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1325 f->frequency = dev->ctl_freq;
1326
Sri Deevib1196122009-03-20 23:33:48 -03001327 call_all(dev, tuner, g_frequency, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001328
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001329 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001330
1331 return 0;
1332}
1333
1334static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001335 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001336{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001337 struct cx231xx_fh *fh = priv;
1338 struct cx231xx *dev = fh->dev;
1339 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001340
1341 rc = check_dev(dev);
1342 if (rc < 0)
1343 return rc;
1344
1345 if (0 != f->tuner)
1346 return -EINVAL;
1347
1348 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1349 return -EINVAL;
1350 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1351 return -EINVAL;
1352
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001353 /* set pre channel change settings in DIF first */
1354 rc = cx231xx_tuner_pre_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001355
1356 mutex_lock(&dev->lock);
1357
1358 dev->ctl_freq = f->frequency;
1359
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001360 if (dev->tuner_type == TUNER_XC5000) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001361 if (dev->cx231xx_set_analog_freq != NULL)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001362 dev->cx231xx_set_analog_freq(dev, f->frequency);
Sri Deevib1196122009-03-20 23:33:48 -03001363 } else
1364 call_all(dev, tuner, s_frequency, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001365
1366 mutex_unlock(&dev->lock);
1367
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001368 /* set post channel change settings in DIF first */
1369 rc = cx231xx_tuner_post_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001370
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001371 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001372
1373 return rc;
1374}
1375
1376#ifdef CONFIG_VIDEO_ADV_DEBUG
1377
Sri Deevie0d3baf2009-03-03 14:37:50 -03001378/*
Sri Deevib9255172009-03-04 17:49:01 -03001379 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1380 chip=<chip>[,min=<addr>,max=<addr>]
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001381 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
Sri Deevib9255172009-03-04 17:49:01 -03001382 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1383 chip=<chip>,reg=<addr>,val=<val>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001384 set the register [VIDIOC_DBG_S_REGISTER]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001385
1386 if type == host, then <chip> is the hosts chip ID (default 0)
1387 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1388 if type == i2caddr, then <chip> is the 7-bit I2C address
1389*/
1390
Sri Deevie0d3baf2009-03-03 14:37:50 -03001391static int vidioc_g_register(struct file *file, void *priv,
1392 struct v4l2_dbg_register *reg)
1393{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001394 struct cx231xx_fh *fh = priv;
1395 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001396 int ret = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001397 u8 value[4] = { 0, 0, 0, 0 };
1398 u32 data = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001399
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001400 switch (reg->match.type) {
1401 case V4L2_CHIP_MATCH_HOST:
1402 switch (reg->match.addr) {
1403 case 0: /* Cx231xx - internal registers */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001404 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1405 (u16)reg->reg, value, 4);
1406 reg->val = value[0] | value[1] << 8 |
1407 value[2] << 16 | value[3] << 24;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001408 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001409 case 1: /* AFE - read byte */
1410 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001411 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001412 reg->val = le32_to_cpu(data & 0xff);
1413 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001414 case 14: /* AFE - read dword */
1415 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001416 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001417 reg->val = le32_to_cpu(data);
1418 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001419 case 2: /* Video Block - read byte */
1420 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001421 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001422 reg->val = le32_to_cpu(data & 0xff);
1423 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001424 case 24: /* Video Block - read dword */
1425 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001426 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001427 reg->val = le32_to_cpu(data);
1428 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001429 case 3: /* I2S block - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001430 ret = cx231xx_read_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001431 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001432 (u16)reg->reg, 1,
1433 &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001434 reg->val = le32_to_cpu(data & 0xff);
1435 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001436 case 34: /* I2S Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001437 ret =
Sri Deeviecc67d12009-03-21 22:00:20 -03001438 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001439 (u16)reg->reg, 1, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001440 reg->val = le32_to_cpu(data);
1441 break;
1442 }
1443 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001444
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001445 case V4L2_CHIP_MATCH_I2C_DRIVER:
Sri Deevib1196122009-03-20 23:33:48 -03001446 call_all(dev, core, g_register, reg);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001447 return 0;
1448 case V4L2_CHIP_MATCH_I2C_ADDR:
1449 /* Not supported yet */
1450 return -EINVAL;
1451 default:
1452 if (!v4l2_chip_match_host(&reg->match))
1453 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001454 }
1455
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001456 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001457 call_all(dev, core, g_register, reg);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001458 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001459
1460 return ret;
1461}
1462
1463static int vidioc_s_register(struct file *file, void *priv,
1464 struct v4l2_dbg_register *reg)
1465{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001466 struct cx231xx_fh *fh = priv;
1467 struct cx231xx *dev = fh->dev;
1468 int ret = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001469 __le64 buf;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001470 u32 value;
1471 u8 data[4] = { 0, 0, 0, 0 };
Sri Deevie0d3baf2009-03-03 14:37:50 -03001472
1473 buf = cpu_to_le64(reg->val);
1474
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001475 switch (reg->match.type) {
1476 case V4L2_CHIP_MATCH_HOST:
1477 {
1478 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001479
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001480 switch (reg->match.addr) {
1481 case 0: /* cx231xx internal registers */
1482 data[0] = (u8) value;
1483 data[1] = (u8) (value >> 8);
1484 data[2] = (u8) (value >> 16);
1485 data[3] = (u8) (value >> 24);
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001486 ret = cx231xx_write_ctrl_reg(dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001487 VRT_SET_REGISTER,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001488 (u16)reg->reg, data,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001489 4);
1490 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001491 case 1: /* AFE - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001492 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001493 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001494 (u16)reg->reg, 2,
1495 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001496 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001497 case 14: /* AFE - read dword */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001498 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001499 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001500 (u16)reg->reg, 2,
1501 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001502 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001503 case 2: /* Video Block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001504 ret =
1505 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001506 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001507 (u16)reg->reg, 2,
1508 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001509 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001510 case 24: /* Video Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001511 ret =
1512 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001513 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001514 (u16)reg->reg, 2,
1515 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001516 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001517 case 3: /* I2S block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001518 ret =
1519 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001520 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001521 (u16)reg->reg, 1,
1522 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001523 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001524 case 34: /* I2S block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001525 ret =
1526 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001527 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001528 (u16)reg->reg, 1,
1529 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001530 break;
1531 }
1532 }
1533 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001534
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001535 default:
1536 break;
1537 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001538
1539 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001540 call_all(dev, core, s_register, reg);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001541 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001542
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001543 return ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001544}
1545#endif
1546
Sri Deevie0d3baf2009-03-03 14:37:50 -03001547static int vidioc_cropcap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001548 struct v4l2_cropcap *cc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001549{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001550 struct cx231xx_fh *fh = priv;
1551 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001552
1553 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1554 return -EINVAL;
1555
1556 cc->bounds.left = 0;
1557 cc->bounds.top = 0;
1558 cc->bounds.width = dev->width;
1559 cc->bounds.height = dev->height;
1560 cc->defrect = cc->bounds;
1561 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1562 cc->pixelaspect.denominator = 59;
1563
1564 return 0;
1565}
1566
1567static int vidioc_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001568 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001569{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001570 struct cx231xx_fh *fh = priv;
1571 struct cx231xx *dev = fh->dev;
1572 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001573
1574 rc = check_dev(dev);
1575 if (rc < 0)
1576 return rc;
1577
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001578 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001579 rc = res_get(fh);
1580
1581 if (likely(rc >= 0))
1582 rc = videobuf_streamon(&fh->vb_vidq);
1583
Sri Deevib1196122009-03-20 23:33:48 -03001584 call_all(dev, video, s_stream, 1);
1585
Sri Deevie0d3baf2009-03-03 14:37:50 -03001586 mutex_unlock(&dev->lock);
1587
1588 return rc;
1589}
1590
1591static int vidioc_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001592 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001593{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001594 struct cx231xx_fh *fh = priv;
1595 struct cx231xx *dev = fh->dev;
1596 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001597
1598 rc = check_dev(dev);
1599 if (rc < 0)
1600 return rc;
1601
Mauro Carvalho Chehab92fcbd32009-03-21 22:16:34 -03001602 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001603 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001604 return -EINVAL;
1605 if (type != fh->type)
1606 return -EINVAL;
1607
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001608 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001609
Sri Deevib1196122009-03-20 23:33:48 -03001610 cx25840_call(dev, video, s_stream, 0);
1611
Sri Deevie0d3baf2009-03-03 14:37:50 -03001612 videobuf_streamoff(&fh->vb_vidq);
1613 res_free(fh);
1614
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001615 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001616
1617 return 0;
1618}
1619
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001620static int vidioc_querycap(struct file *file, void *priv,
1621 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001622{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001623 struct cx231xx_fh *fh = priv;
1624 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001625
1626 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1627 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab2c6beca2009-03-22 08:53:36 -03001628 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001629
1630 cap->version = CX231XX_VERSION_CODE;
1631
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001632 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
Sri Deevie0d3baf2009-03-03 14:37:50 -03001633#if 0
Sri Deevi6e4f5742009-03-10 21:16:26 -03001634 V4L2_CAP_SLICED_VBI_CAPTURE |
Sri Deevie0d3baf2009-03-03 14:37:50 -03001635#endif
Sri Deevi6e4f5742009-03-10 21:16:26 -03001636 V4L2_CAP_VIDEO_CAPTURE |
1637 V4L2_CAP_AUDIO |
1638 V4L2_CAP_READWRITE |
1639 V4L2_CAP_STREAMING;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001640
1641 if (dev->tuner_type != TUNER_ABSENT)
1642 cap->capabilities |= V4L2_CAP_TUNER;
1643
1644 return 0;
1645}
1646
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001647static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1648 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001649{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001650 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001651 return -EINVAL;
1652
1653 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1654 f->pixelformat = format[f->index].fourcc;
1655
1656 return 0;
1657}
1658
1659/* Sliced VBI ioctls */
1660static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001661 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001662{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001663 struct cx231xx_fh *fh = priv;
1664 struct cx231xx *dev = fh->dev;
1665 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001666
1667 rc = check_dev(dev);
1668 if (rc < 0)
1669 return rc;
1670
1671 mutex_lock(&dev->lock);
1672
1673 f->fmt.sliced.service_set = 0;
1674
Sri Deevib1196122009-03-20 23:33:48 -03001675 call_all(dev, video, g_fmt, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001676
1677 if (f->fmt.sliced.service_set == 0)
1678 rc = -EINVAL;
1679
1680 mutex_unlock(&dev->lock);
1681 return rc;
1682}
1683
1684static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001685 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001686{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001687 struct cx231xx_fh *fh = priv;
1688 struct cx231xx *dev = fh->dev;
1689 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001690
1691 rc = check_dev(dev);
1692 if (rc < 0)
1693 return rc;
1694
1695 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001696 call_all(dev, video, g_fmt, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001697 mutex_unlock(&dev->lock);
1698
1699 if (f->fmt.sliced.service_set == 0)
1700 return -EINVAL;
1701
1702 return 0;
1703}
1704
Sri Deevie0d3baf2009-03-03 14:37:50 -03001705/* RAW VBI ioctls */
1706
1707static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001708 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001709{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001710 struct cx231xx_fh *fh = priv;
1711 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001712
1713 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001714 35468950 : 28636363;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001715 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1716 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1717 f->fmt.vbi.offset = 64 * 4;
1718 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001719 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001720 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001721 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001722 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001723 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001724 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1725
1726 return 0;
1727
1728}
1729
1730static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001731 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001732{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001733 struct cx231xx_fh *fh = priv;
1734 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001735
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001736 if (dev->vbi_stream_on && !fh->stream_on) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001737 cx231xx_errdev("%s device in use by another fh\n", __func__);
1738 return -EBUSY;
1739 }
1740
1741 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1742 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001743 35468950 : 28636363;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001744 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1745 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1746 f->fmt.vbi.offset = 244;
1747 f->fmt.vbi.flags = 0;
1748 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001749 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001750 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001751 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001752 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001753 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001754 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1755
1756 return 0;
1757
1758}
1759
1760static int vidioc_reqbufs(struct file *file, void *priv,
1761 struct v4l2_requestbuffers *rb)
1762{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001763 struct cx231xx_fh *fh = priv;
1764 struct cx231xx *dev = fh->dev;
1765 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001766
1767 rc = check_dev(dev);
1768 if (rc < 0)
1769 return rc;
1770
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001771 return videobuf_reqbufs(&fh->vb_vidq, rb);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001772}
1773
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001774static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001775{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001776 struct cx231xx_fh *fh = priv;
1777 struct cx231xx *dev = fh->dev;
1778 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001779
1780 rc = check_dev(dev);
1781 if (rc < 0)
1782 return rc;
1783
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001784 return videobuf_querybuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001785}
1786
1787static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1788{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001789 struct cx231xx_fh *fh = priv;
1790 struct cx231xx *dev = fh->dev;
1791 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001792
1793 rc = check_dev(dev);
1794 if (rc < 0)
1795 return rc;
1796
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001797 return videobuf_qbuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001798}
1799
1800static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1801{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001802 struct cx231xx_fh *fh = priv;
1803 struct cx231xx *dev = fh->dev;
1804 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001805
1806 rc = check_dev(dev);
1807 if (rc < 0)
1808 return rc;
1809
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001810 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001811}
1812
1813#ifdef CONFIG_VIDEO_V4L1_COMPAT
1814static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1815{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001816 struct cx231xx_fh *fh = priv;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001817
1818 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1819}
1820#endif
1821
Sri Deevie0d3baf2009-03-03 14:37:50 -03001822/* ----------------------------------------------------------- */
1823/* RADIO ESPECIFIC IOCTLS */
1824/* ----------------------------------------------------------- */
1825
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001826static int radio_querycap(struct file *file, void *priv,
Sri Deevie0d3baf2009-03-03 14:37:50 -03001827 struct v4l2_capability *cap)
1828{
1829 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1830
1831 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1832 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1833 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1834
1835 cap->version = CX231XX_VERSION_CODE;
1836 cap->capabilities = V4L2_CAP_TUNER;
1837 return 0;
1838}
1839
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001840static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001841{
1842 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1843
1844 if (unlikely(t->index > 0))
1845 return -EINVAL;
1846
1847 strcpy(t->name, "Radio");
1848 t->type = V4L2_TUNER_RADIO;
1849
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001850 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001851 call_all(dev, tuner, s_tuner, t);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001852 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001853
1854 return 0;
1855}
1856
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001857static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001858{
1859 if (i->index != 0)
1860 return -EINVAL;
1861 strcpy(i->name, "Radio");
1862 i->type = V4L2_INPUT_TYPE_TUNER;
1863
1864 return 0;
1865}
1866
1867static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1868{
1869 if (unlikely(a->index))
1870 return -EINVAL;
1871
1872 strcpy(a->name, "Radio");
1873 return 0;
1874}
1875
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001876static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001877{
1878 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1879
1880 if (0 != t->index)
1881 return -EINVAL;
1882
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001883 mutex_lock(&dev->lock);
Sri Deevib1196122009-03-20 23:33:48 -03001884 call_all(dev, tuner, s_tuner, t);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001885 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001886
1887 return 0;
1888}
1889
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001890static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001891{
1892 return 0;
1893}
1894
1895static int radio_s_input(struct file *file, void *fh, unsigned int i)
1896{
1897 return 0;
1898}
1899
1900static int radio_queryctrl(struct file *file, void *priv,
1901 struct v4l2_queryctrl *c)
1902{
1903 int i;
1904
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001905 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001906 return -EINVAL;
1907 if (c->id == V4L2_CID_AUDIO_MUTE) {
1908 for (i = 0; i < CX231XX_CTLS; i++)
1909 if (cx231xx_ctls[i].v.id == c->id)
1910 break;
1911 *c = cx231xx_ctls[i].v;
1912 } else
1913 *c = no_ctl;
1914 return 0;
1915}
1916
1917/*
1918 * cx231xx_v4l2_open()
1919 * inits the device and starts isoc transfer
1920 */
1921static int cx231xx_v4l2_open(struct file *filp)
1922{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001923 int minor = video_devdata(filp)->minor;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001924 int errCode = 0, radio = 0;
1925 struct cx231xx *dev = NULL;
1926 struct cx231xx_fh *fh;
1927 enum v4l2_buf_type fh_type = 0;
1928
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001929 dev = cx231xx_get_device(minor, &fh_type, &radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001930 if (NULL == dev)
1931 return -ENODEV;
1932
1933 mutex_lock(&dev->lock);
1934
1935 cx231xx_videodbg("open minor=%d type=%s users=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001936 minor, v4l2_type_names[fh_type], dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001937
1938#if 0
1939 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1940 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001941 cx231xx_errdev
1942 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001943 mutex_unlock(&dev->lock);
1944 return -EBUSY;
1945 }
1946#endif
1947
1948 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1949 if (!fh) {
1950 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1951 mutex_unlock(&dev->lock);
1952 return -ENOMEM;
1953 }
1954 fh->dev = dev;
1955 fh->radio = radio;
1956 fh->type = fh_type;
1957 filp->private_data = fh;
1958
1959 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1960 dev->width = norm_maxw(dev);
1961 dev->height = norm_maxh(dev);
1962 dev->hscale = 0;
1963 dev->vscale = 0;
1964
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001965 /* Power up in Analog TV mode */
1966 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001967
1968#if 0
1969 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1970#endif
1971 cx231xx_resolution_set(dev);
1972
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001973 /* set video alternate setting */
1974 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001975
1976 /* Needed, since GPIO might have disabled power of
1977 some i2c device */
1978 cx231xx_config_i2c(dev);
1979
1980 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001981 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1982 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001983
1984 }
1985 if (fh->radio) {
1986 cx231xx_videodbg("video_open: setting radio device\n");
1987
1988 /* cx231xx_start_radio(dev); */
1989
Sri Deevib1196122009-03-20 23:33:48 -03001990 call_all(dev, tuner, s_radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001991 }
1992
1993 dev->users++;
1994
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001995 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1996 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1997 NULL, &dev->video_mode.slock,
1998 fh->type, V4L2_FIELD_INTERLACED,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001999 sizeof(struct cx231xx_buffer), fh);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002000 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002001 /* Set the required alternate setting VBI interface works in
2002 Bulk mode only */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002003 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002004
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002005 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2006 NULL, &dev->vbi_mode.slock,
2007 fh->type, V4L2_FIELD_SEQ_TB,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002008 sizeof(struct cx231xx_buffer), fh);
2009 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002010
2011 mutex_unlock(&dev->lock);
2012
2013 return errCode;
2014}
2015
2016/*
2017 * cx231xx_realease_resources()
2018 * unregisters the v4l2,i2c and usb devices
2019 * called when the device gets disconected or at module unload
2020*/
2021void cx231xx_release_analog_resources(struct cx231xx *dev)
2022{
2023
2024 /*FIXME: I2C IR should be disconnected */
2025
2026 if (dev->radio_dev) {
2027 if (-1 != dev->radio_dev->minor)
2028 video_unregister_device(dev->radio_dev);
2029 else
2030 video_device_release(dev->radio_dev);
2031 dev->radio_dev = NULL;
2032 }
2033 if (dev->vbi_dev) {
2034 cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002035 dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002036 if (-1 != dev->vbi_dev->minor)
2037 video_unregister_device(dev->vbi_dev);
2038 else
2039 video_device_release(dev->vbi_dev);
2040 dev->vbi_dev = NULL;
2041 }
2042 if (dev->vdev) {
2043 cx231xx_info("V4L2 device /dev/video%d deregistered\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002044 dev->vdev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002045 if (-1 != dev->vdev->minor)
2046 video_unregister_device(dev->vdev);
2047 else
2048 video_device_release(dev->vdev);
2049 dev->vdev = NULL;
2050 }
2051}
2052
2053/*
2054 * cx231xx_v4l2_close()
2055 * stops streaming and deallocates all resources allocated by the v4l2
2056 * calls and ioctls
2057 */
2058static int cx231xx_v4l2_close(struct file *filp)
2059{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002060 struct cx231xx_fh *fh = filp->private_data;
2061 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002062
2063 cx231xx_videodbg("users=%d\n", dev->users);
2064
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002065 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002066
2067 if (res_check(fh))
2068 res_free(fh);
2069
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002070 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2071 videobuf_stop(&fh->vb_vidq);
2072 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002073
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002074 /* the device is already disconnect,
2075 free the remaining resources */
2076 if (dev->state & DEV_DISCONNECTED) {
2077 cx231xx_release_resources(dev);
2078 mutex_unlock(&dev->lock);
2079 kfree(dev);
2080 return 0;
2081 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002082
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002083 /* do this before setting alternate! */
2084 cx231xx_uninit_vbi_isoc(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002085
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002086 /* set alternate 0 */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002087 if (!dev->vbi_or_sliced_cc_mode)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002088 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002089 else
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002090 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002091
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002092 kfree(fh);
2093 dev->users--;
2094 wake_up_interruptible_nr(&dev->open, 1);
2095 mutex_unlock(&dev->lock);
2096 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002097 }
2098
2099 if (dev->users == 1) {
2100 videobuf_stop(&fh->vb_vidq);
2101 videobuf_mmap_free(&fh->vb_vidq);
2102
2103 /* the device is already disconnect,
2104 free the remaining resources */
2105 if (dev->state & DEV_DISCONNECTED) {
2106 cx231xx_release_resources(dev);
2107 mutex_unlock(&dev->lock);
2108 kfree(dev);
2109 return 0;
2110 }
2111
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002112 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03002113 call_all(dev, tuner, s_standby);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002114
2115 /* do this before setting alternate! */
2116 cx231xx_uninit_isoc(dev);
2117 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2118
2119 /* set alternate 0 */
2120 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2121 }
2122 kfree(fh);
2123 dev->users--;
2124 wake_up_interruptible_nr(&dev->open, 1);
2125 mutex_unlock(&dev->lock);
2126 return 0;
2127}
2128
2129/*
2130 * cx231xx_v4l2_read()
2131 * will allocate buffers when called for the first time
2132 */
2133static ssize_t
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002134cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2135 loff_t *pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002136{
2137 struct cx231xx_fh *fh = filp->private_data;
2138 struct cx231xx *dev = fh->dev;
2139 int rc;
2140
2141 rc = check_dev(dev);
2142 if (rc < 0)
2143 return rc;
2144
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002145 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2146 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002147 mutex_lock(&dev->lock);
2148 rc = res_get(fh);
2149 mutex_unlock(&dev->lock);
2150
2151 if (unlikely(rc < 0))
2152 return rc;
2153
2154 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002155 filp->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002156 }
2157 return 0;
2158}
2159
2160/*
2161 * cx231xx_v4l2_poll()
2162 * will allocate buffers when called for the first time
2163 */
2164static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
2165{
2166 struct cx231xx_fh *fh = filp->private_data;
2167 struct cx231xx *dev = fh->dev;
2168 int rc;
2169
2170 rc = check_dev(dev);
2171 if (rc < 0)
2172 return rc;
2173
2174 mutex_lock(&dev->lock);
2175 rc = res_get(fh);
2176 mutex_unlock(&dev->lock);
2177
2178 if (unlikely(rc < 0))
2179 return POLLERR;
2180
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002181 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2182 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2183 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2184 else
Sri Deevie0d3baf2009-03-03 14:37:50 -03002185 return POLLERR;
2186}
2187
2188/*
2189 * cx231xx_v4l2_mmap()
2190 */
2191static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2192{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002193 struct cx231xx_fh *fh = filp->private_data;
2194 struct cx231xx *dev = fh->dev;
2195 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002196
2197 rc = check_dev(dev);
2198 if (rc < 0)
2199 return rc;
2200
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002201 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002202 rc = res_get(fh);
2203 mutex_unlock(&dev->lock);
2204
2205 if (unlikely(rc < 0))
2206 return rc;
2207
2208 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2209
2210 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002211 (unsigned long)vma->vm_start,
2212 (unsigned long)vma->vm_end -
2213 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002214
2215 return rc;
2216}
2217
2218static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002219 .owner = THIS_MODULE,
2220 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002221 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002222 .read = cx231xx_v4l2_read,
2223 .poll = cx231xx_v4l2_poll,
2224 .mmap = cx231xx_v4l2_mmap,
2225 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002226};
2227
2228static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002229 .vidioc_querycap = vidioc_querycap,
2230 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2231 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2232 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2233 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2234 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2235 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2236 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2237 .vidioc_g_audio = vidioc_g_audio,
2238 .vidioc_s_audio = vidioc_s_audio,
2239 .vidioc_cropcap = vidioc_cropcap,
2240 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002241 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002242 .vidioc_reqbufs = vidioc_reqbufs,
2243 .vidioc_querybuf = vidioc_querybuf,
2244 .vidioc_qbuf = vidioc_qbuf,
2245 .vidioc_dqbuf = vidioc_dqbuf,
2246 .vidioc_s_std = vidioc_s_std,
2247 .vidioc_g_std = vidioc_g_std,
2248 .vidioc_enum_input = vidioc_enum_input,
2249 .vidioc_g_input = vidioc_g_input,
2250 .vidioc_s_input = vidioc_s_input,
2251 .vidioc_queryctrl = vidioc_queryctrl,
2252 .vidioc_g_ctrl = vidioc_g_ctrl,
2253 .vidioc_s_ctrl = vidioc_s_ctrl,
2254 .vidioc_streamon = vidioc_streamon,
2255 .vidioc_streamoff = vidioc_streamoff,
2256 .vidioc_g_tuner = vidioc_g_tuner,
2257 .vidioc_s_tuner = vidioc_s_tuner,
2258 .vidioc_g_frequency = vidioc_g_frequency,
2259 .vidioc_s_frequency = vidioc_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002260#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002261 .vidioc_g_register = vidioc_g_register,
2262 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002263#endif
2264#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002265 .vidiocgmbuf = vidiocgmbuf,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002266#endif
2267};
2268
2269static struct video_device cx231xx_vbi_template;
2270
2271static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002272 .fops = &cx231xx_v4l_fops,
2273 .release = video_device_release,
2274 .ioctl_ops = &video_ioctl_ops,
2275 .minor = -1,
2276 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002277 .current_norm = V4L2_STD_PAL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002278};
2279
2280static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002281 .owner = THIS_MODULE,
2282 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002283 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002284 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002285};
2286
2287static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002288 .vidioc_querycap = radio_querycap,
2289 .vidioc_g_tuner = radio_g_tuner,
2290 .vidioc_enum_input = radio_enum_input,
2291 .vidioc_g_audio = radio_g_audio,
2292 .vidioc_s_tuner = radio_s_tuner,
2293 .vidioc_s_audio = radio_s_audio,
2294 .vidioc_s_input = radio_s_input,
2295 .vidioc_queryctrl = radio_queryctrl,
2296 .vidioc_g_ctrl = vidioc_g_ctrl,
2297 .vidioc_s_ctrl = vidioc_s_ctrl,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002298 .vidioc_g_frequency = vidioc_g_frequency,
2299 .vidioc_s_frequency = vidioc_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002300#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002301 .vidioc_g_register = vidioc_g_register,
2302 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002303#endif
2304};
2305
2306static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002307 .name = "cx231xx-radio",
2308 .fops = &radio_fops,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002309 .ioctl_ops = &radio_ioctl_ops,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002310 .minor = -1,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002311};
2312
2313/******************************** usb interface ******************************/
2314
Sri Deevib9255172009-03-04 17:49:01 -03002315static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2316 const struct video_device
2317 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002318{
2319 struct video_device *vfd;
2320
2321 vfd = video_device_alloc();
2322 if (NULL == vfd)
2323 return NULL;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002324
Sri Deevie0d3baf2009-03-03 14:37:50 -03002325 *vfd = *template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002326 vfd->minor = -1;
Sri Deevib1196122009-03-20 23:33:48 -03002327 vfd->v4l2_dev = &dev->v4l2_dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002328 vfd->release = video_device_release;
2329 vfd->debug = video_debug;
2330
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002331 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002332
2333 return vfd;
2334}
2335
2336int cx231xx_register_analog_devices(struct cx231xx *dev)
2337{
2338 int ret;
2339
Sri Deevie0d3baf2009-03-03 14:37:50 -03002340 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002341 dev->name,
2342 (CX231XX_VERSION_CODE >> 16) & 0xff,
2343 (CX231XX_VERSION_CODE >> 8) & 0xff,
2344 CX231XX_VERSION_CODE & 0xff);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002345
2346 /* set default norm */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002347 /*dev->norm = cx231xx_video_template.current_norm; */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002348 dev->width = norm_maxw(dev);
2349 dev->height = norm_maxh(dev);
2350 dev->interlaced = 0;
2351 dev->hscale = 0;
2352 dev->vscale = 0;
2353
2354 /* Analog specific initialization */
2355 dev->format = &format[0];
2356 /* video_mux(dev, dev->video_input); */
2357
2358 /* Audio defaults */
2359 dev->mute = 1;
2360 dev->volume = 0x1f;
2361
2362 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002363 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002364
2365 /* allocate and fill video video_device struct */
2366 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2367 if (!dev->vdev) {
2368 cx231xx_errdev("cannot allocate video_device.\n");
2369 return -ENODEV;
2370 }
2371
2372 /* register v4l2 video video_device */
2373 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002374 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002375 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002376 cx231xx_errdev("unable to register video device (error=%i).\n",
2377 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002378 return ret;
2379 }
2380
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002381 cx231xx_info("%s/0: registered device video%d [v4l2]\n",
2382 dev->name, dev->vdev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002383
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002384 /* Initialize VBI template */
2385 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2386 sizeof(cx231xx_vbi_template));
2387 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002388
2389 /* Allocate and fill vbi video_device struct */
2390 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2391
2392 /* register v4l2 vbi video_device */
2393 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002394 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002395 if (ret < 0) {
2396 cx231xx_errdev("unable to register vbi device\n");
2397 return ret;
2398 }
2399
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002400 cx231xx_info("%s/0: registered device vbi%d\n",
2401 dev->name, dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002402
2403 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002404 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2405 "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002406 if (!dev->radio_dev) {
2407 cx231xx_errdev("cannot allocate video_device.\n");
2408 return -ENODEV;
2409 }
2410 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2411 radio_nr[dev->devno]);
2412 if (ret < 0) {
2413 cx231xx_errdev("can't register radio device\n");
2414 return ret;
2415 }
2416 cx231xx_info("Registered radio device as /dev/radio%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002417 dev->radio_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002418 }
2419
2420 cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002421 dev->vdev->num, dev->vbi_dev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002422
2423 return 0;
2424}