blob: 1b29158bbbe25be8c423b87257c65e60612eaf29 [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>
Sri Deevie0d3baf2009-03-03 14:37:50 -030032#include <linux/mm.h>
33#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030035
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 Chehab1990d502011-06-24 14:45:49 -030047#define CX231XX_VERSION "0.0.2"
Mauro Carvalho Chehab818fdf32009-03-13 07:41:58 -030048
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");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030072MODULE_VERSION(CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -030073
Sri Deevie0d3baf2009-03-03 14:37:50 -030074static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
78
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030079module_param_array(card, int, NULL, 0444);
Sri Deevie0d3baf2009-03-03 14:37:50 -030080module_param_array(video_nr, int, NULL, 0444);
81module_param_array(vbi_nr, int, NULL, 0444);
82module_param_array(radio_nr, int, NULL, 0444);
83
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030084MODULE_PARM_DESC(card, "card type");
Sri Deevie0d3baf2009-03-03 14:37:50 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030086MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
Sri Deevie0d3baf2009-03-03 14:37:50 -030087MODULE_PARM_DESC(radio_nr, "radio device numbers");
88
89static unsigned int video_debug;
90module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
92
Sri Deevie0d3baf2009-03-03 14:37:50 -030093/* supported video standards */
94static struct cx231xx_fmt format[] = {
95 {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030096 .name = "16bpp YUY2, 4:2:2, packed",
97 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
99 .reg = 0,
100 },
Sri Deevie0d3baf2009-03-03 14:37:50 -0300101};
102
Sri Deevie0d3baf2009-03-03 14:37:50 -0300103/* supported controls */
104/* Common to all boards */
105
106/* ------------------------------------------------------------------- */
107
108static const struct v4l2_queryctrl no_ctl = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300109 .name = "42",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300110 .flags = V4L2_CTRL_FLAG_DISABLED,
111};
112
113static struct cx231xx_ctrl cx231xx_ctls[] = {
114 /* --- video --- */
115 {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300116 .v = {
117 .id = V4L2_CID_BRIGHTNESS,
118 .name = "Brightness",
119 .minimum = 0x00,
120 .maximum = 0xff,
121 .step = 1,
122 .default_value = 0x7f,
123 .type = V4L2_CTRL_TYPE_INTEGER,
124 },
125 .off = 128,
126 .reg = LUMA_CTRL,
127 .mask = 0x00ff,
128 .shift = 0,
129 }, {
130 .v = {
131 .id = V4L2_CID_CONTRAST,
132 .name = "Contrast",
133 .minimum = 0,
134 .maximum = 0xff,
135 .step = 1,
136 .default_value = 0x3f,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 },
139 .off = 0,
140 .reg = LUMA_CTRL,
141 .mask = 0xff00,
142 .shift = 8,
143 }, {
144 .v = {
145 .id = V4L2_CID_HUE,
146 .name = "Hue",
147 .minimum = 0,
148 .maximum = 0xff,
149 .step = 1,
150 .default_value = 0x7f,
151 .type = V4L2_CTRL_TYPE_INTEGER,
152 },
153 .off = 128,
154 .reg = CHROMA_CTRL,
155 .mask = 0xff0000,
156 .shift = 16,
157 }, {
158 /* strictly, this only describes only U saturation.
159 * V saturation is handled specially through code.
160 */
161 .v = {
162 .id = V4L2_CID_SATURATION,
163 .name = "Saturation",
164 .minimum = 0,
165 .maximum = 0xff,
166 .step = 1,
167 .default_value = 0x7f,
168 .type = V4L2_CTRL_TYPE_INTEGER,
169 },
170 .off = 0,
171 .reg = CHROMA_CTRL,
172 .mask = 0x00ff,
173 .shift = 0,
174 }, {
175 /* --- audio --- */
176 .v = {
177 .id = V4L2_CID_AUDIO_MUTE,
178 .name = "Mute",
179 .minimum = 0,
180 .maximum = 1,
181 .default_value = 1,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
183 },
184 .reg = PATH1_CTL1,
185 .mask = (0x1f << 24),
186 .shift = 24,
187 }, {
188 .v = {
189 .id = V4L2_CID_AUDIO_VOLUME,
190 .name = "Volume",
191 .minimum = 0,
192 .maximum = 0x3f,
193 .step = 1,
194 .default_value = 0x3f,
195 .type = V4L2_CTRL_TYPE_INTEGER,
196 },
197 .reg = PATH1_VOL_CTL,
198 .mask = 0xff,
199 .shift = 0,
200 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300201};
202static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
203
204static const u32 cx231xx_user_ctrls[] = {
205 V4L2_CID_USER_CLASS,
206 V4L2_CID_BRIGHTNESS,
207 V4L2_CID_CONTRAST,
208 V4L2_CID_SATURATION,
209 V4L2_CID_HUE,
210 V4L2_CID_AUDIO_VOLUME,
211#if 0
212 V4L2_CID_AUDIO_BALANCE,
213#endif
214 V4L2_CID_AUDIO_MUTE,
215 0
216};
217
218static const u32 *ctrl_classes[] = {
219 cx231xx_user_ctrls,
220 NULL
221};
222
Sri Deevie0d3baf2009-03-03 14:37:50 -0300223/* ------------------------------------------------------------------
224 Video buffer and parser functions
225 ------------------------------------------------------------------*/
226
227/*
228 * Announces that a buffer were filled and request the next
229 */
230static inline void buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300231 struct cx231xx_dmaqueue *dma_q,
232 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300233{
234 /* Advice that buffer was filled */
235 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
236 buf->vb.state = VIDEOBUF_DONE;
237 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300238 v4l2_get_timestamp(&buf->vb.ts);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300239
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300240 if (dev->USE_ISO)
241 dev->video_mode.isoc_ctl.buf = NULL;
242 else
243 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300244
245 list_del(&buf->vb.queue);
246 wake_up(&buf->vb.done);
247}
248
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300249static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300250{
251 char *errmsg = "Unknown";
252
253 switch (status) {
254 case -ENOENT:
255 errmsg = "unlinked synchronuously";
256 break;
257 case -ECONNRESET:
258 errmsg = "unlinked asynchronuously";
259 break;
260 case -ENOSR:
261 errmsg = "Buffer error (overrun)";
262 break;
263 case -EPIPE:
264 errmsg = "Stalled (device not responding)";
265 break;
266 case -EOVERFLOW:
267 errmsg = "Babble (bad cable?)";
268 break;
269 case -EPROTO:
270 errmsg = "Bit-stuff error (bad cable?)";
271 break;
272 case -EILSEQ:
273 errmsg = "CRC/Timeout (could be anything)";
274 break;
275 case -ETIME:
276 errmsg = "Device does not respond";
277 break;
278 }
279 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300280 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300281 } else {
282 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300283 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300284 }
285}
286
287/*
288 * video-buf generic routine to get the next available buffer
289 */
290static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300291 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300292{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300293 struct cx231xx_video_mode *vmode =
294 container_of(dma_q, struct cx231xx_video_mode, vidq);
295 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300296
297 char *outp;
298
Sri Deevie0d3baf2009-03-03 14:37:50 -0300299 if (list_empty(&dma_q->active)) {
300 cx231xx_isocdbg("No active queue to serve\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300301 if (dev->USE_ISO)
302 dev->video_mode.isoc_ctl.buf = NULL;
303 else
304 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300305 *buf = NULL;
306 return;
307 }
308
309 /* Get the next buffer */
310 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
311
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300312 /* Cleans up buffer - Useful for testing for frame/URB loss */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300313 outp = videobuf_to_vmalloc(&(*buf)->vb);
314 memset(outp, 0, (*buf)->vb.size);
315
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300316 if (dev->USE_ISO)
317 dev->video_mode.isoc_ctl.buf = *buf;
318 else
319 dev->video_mode.bulk_ctl.buf = *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300320
321 return;
322}
323
324/*
325 * Controls the isoc copy of each urb packet
326 */
327static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
328{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300329 struct cx231xx_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300330 int i, rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300331 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300332 u32 bytes_parsed = 0, buffer_size = 0;
333 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300334
335 if (!dev)
336 return 0;
337
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200338 if (dev->state & DEV_DISCONNECTED)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300339 return 0;
340
341 if (urb->status < 0) {
342 print_err_status(dev, -1, urb->status);
343 if (urb->status == -ENOENT)
344 return 0;
345 }
346
Sri Deevie0d3baf2009-03-03 14:37:50 -0300347 for (i = 0; i < urb->number_of_packets; i++) {
348 int status = urb->iso_frame_desc[i].status;
349
350 if (status < 0) {
351 print_err_status(dev, i, status);
352 if (urb->iso_frame_desc[i].status != -EPROTO)
353 continue;
354 }
355
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300356 if (urb->iso_frame_desc[i].actual_length <= 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300357 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
358 continue;
359 }
360 if (urb->iso_frame_desc[i].actual_length >
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300361 dev->video_mode.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300362 cx231xx_isocdbg("packet bigger than packet size");
363 continue;
364 }
365
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300366 /* get buffer pointer and length */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300367 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300368 buffer_size = urb->iso_frame_desc[i].actual_length;
369 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300370
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300371 if (dma_q->is_partial_line) {
Sri Deevib9255172009-03-04 17:49:01 -0300372 /* Handle the case of a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300373 sav_eav = dma_q->last_sav;
374 } else {
Sri Deevib9255172009-03-04 17:49:01 -0300375 /* Check for a SAV/EAV overlapping
376 the buffer boundary */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300377 sav_eav =
378 cx231xx_find_boundary_SAV_EAV(p_buffer,
379 dma_q->partial_buf,
380 &bytes_parsed);
381 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300382
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300383 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300384 /* Get the first line if we have some portion of an SAV/EAV from
385 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300386 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300387 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
Sri Deevib9255172009-03-04 17:49:01 -0300388 sav_eav, /* SAV/EAV */
389 p_buffer + bytes_parsed, /* p_buffer */
390 buffer_size - bytes_parsed);/* buf size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300391 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300392
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300393 /* Now parse data that is completely in this buffer */
394 /* dma_q->is_partial_line = 0; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300395
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300396 while (bytes_parsed < buffer_size) {
397 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300398
Sri Deevib9255172009-03-04 17:49:01 -0300399 sav_eav = cx231xx_find_next_SAV_EAV(
400 p_buffer + bytes_parsed, /* p_buffer */
401 buffer_size - bytes_parsed, /* buf size */
402 &bytes_used);/* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300403
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300404 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300405
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300406 sav_eav &= 0xF0;
407 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300408 bytes_parsed += cx231xx_get_video_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300409 dma_q, sav_eav, /* SAV/EAV */
410 p_buffer + bytes_parsed,/* p_buffer */
411 buffer_size - bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300412 }
413 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300414
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300415 /* Save the last four bytes of the buffer so we can check the
416 buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300417 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
418 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300419
420 }
421 return rc;
422}
423
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300424static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
425{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300426 struct cx231xx_dmaqueue *dma_q = urb->context;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300427 int rc = 1;
428 unsigned char *p_buffer;
429 u32 bytes_parsed = 0, buffer_size = 0;
430 u8 sav_eav = 0;
431
432 if (!dev)
433 return 0;
434
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200435 if (dev->state & DEV_DISCONNECTED)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300436 return 0;
437
438 if (urb->status < 0) {
439 print_err_status(dev, -1, urb->status);
440 if (urb->status == -ENOENT)
441 return 0;
442 }
443
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300444 if (1) {
445
446 /* get buffer pointer and length */
447 p_buffer = urb->transfer_buffer;
448 buffer_size = urb->actual_length;
449 bytes_parsed = 0;
450
451 if (dma_q->is_partial_line) {
452 /* Handle the case of a partial line */
453 sav_eav = dma_q->last_sav;
454 } else {
455 /* Check for a SAV/EAV overlapping
456 the buffer boundary */
457 sav_eav =
458 cx231xx_find_boundary_SAV_EAV(p_buffer,
459 dma_q->partial_buf,
460 &bytes_parsed);
461 }
462
463 sav_eav &= 0xF0;
464 /* Get the first line if we have some portion of an SAV/EAV from
465 the last buffer or a partial line */
466 if (sav_eav) {
467 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
468 sav_eav, /* SAV/EAV */
469 p_buffer + bytes_parsed, /* p_buffer */
470 buffer_size - bytes_parsed);/* buf size */
471 }
472
473 /* Now parse data that is completely in this buffer */
474 /* dma_q->is_partial_line = 0; */
475
476 while (bytes_parsed < buffer_size) {
477 u32 bytes_used = 0;
478
479 sav_eav = cx231xx_find_next_SAV_EAV(
480 p_buffer + bytes_parsed, /* p_buffer */
481 buffer_size - bytes_parsed, /* buf size */
482 &bytes_used);/* bytes used to get SAV/EAV */
483
484 bytes_parsed += bytes_used;
485
486 sav_eav &= 0xF0;
487 if (sav_eav && (bytes_parsed < buffer_size)) {
488 bytes_parsed += cx231xx_get_video_line(dev,
489 dma_q, sav_eav, /* SAV/EAV */
490 p_buffer + bytes_parsed,/* p_buffer */
491 buffer_size - bytes_parsed);/*buf size*/
492 }
493 }
494
495 /* Save the last four bytes of the buffer so we can check the
496 buffer boundary condition next time */
497 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
498 bytes_parsed = 0;
499
500 }
501 return rc;
502}
503
504
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300505u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
506 u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300507{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300508 u32 bytes_used;
509 u8 boundary_bytes[8];
510 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300511
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300512 *p_bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300513
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300514 /* Create an array of the last 4 bytes of the last buffer and the first
515 4 bytes of the current buffer. */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300516
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300517 memcpy(boundary_bytes, partial_buf, 4);
518 memcpy(boundary_bytes + 4, p_buffer, 4);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300520 /* Check for the SAV/EAV in the boundary buffer */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300521 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
522 &bytes_used);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300523
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300524 if (sav_eav) {
525 /* found a boundary SAV/EAV. Updates the bytes used to reflect
526 only those used in the new buffer */
527 *p_bytes_used = bytes_used - 4;
528 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300529
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300530 return sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300531}
532
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300533u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300534{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300535 u32 i;
536 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300537
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300538 /*
539 * Don't search if the buffer size is less than 4. It causes a page
540 * fault since buffer_size - 4 evaluates to a large number in that
541 * case.
542 */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300543 if (buffer_size < 4) {
544 *p_bytes_used = buffer_size;
545 return 0;
546 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300547
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300548 for (i = 0; i < (buffer_size - 3); i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300549
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300550 if ((p_buffer[i] == 0xFF) &&
551 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300552
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300553 *p_bytes_used = i + 4;
554 sav_eav = p_buffer[i + 3];
555 return sav_eav;
556 }
557 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300558
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300559 *p_bytes_used = buffer_size;
560 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300561}
562
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300563u32 cx231xx_get_video_line(struct cx231xx *dev,
564 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
565 u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300566{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300567 u32 bytes_copied = 0;
568 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300569
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300570 switch (sav_eav) {
571 case SAV_ACTIVE_VIDEO_FIELD1:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300572 /* looking for skipped line which occurred in PAL 720x480 mode.
573 In this case, there will be no active data contained
574 between the SAV and EAV */
575 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
576 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
577 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
578 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
579 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
580 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300581 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300582 current_field = 1;
583 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300584
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300585 case SAV_ACTIVE_VIDEO_FIELD2:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300586 /* looking for skipped line which occurred in PAL 720x480 mode.
587 In this case, there will be no active data contained between
588 the SAV and EAV */
589 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
590 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
591 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
592 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
593 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
594 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300595 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300596 current_field = 2;
597 break;
598 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300599
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300600 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300601
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300602 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
603 buffer_size, current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300604
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300605 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300606}
607
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300608u32 cx231xx_copy_video_line(struct cx231xx *dev,
609 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300610 u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300611{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300612 u32 bytes_to_copy;
613 struct cx231xx_buffer *buf;
614 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300615
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300616 if (dma_q->current_field != field_number)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300617 cx231xx_reset_video_buffer(dev, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300618
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300619 /* get the buffer pointer */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300620 if (dev->USE_ISO)
621 buf = dev->video_mode.isoc_ctl.buf;
622 else
623 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300624
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300625 /* Remember the field number for next time */
626 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300627
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300628 bytes_to_copy = dma_q->bytes_left_in_line;
629 if (bytes_to_copy > length)
630 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300631
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300632 if (dma_q->lines_completed >= dma_q->lines_per_field) {
633 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300634 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
635 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300636 return 0;
637 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300638
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300639 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300640
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300641 /* If we don't have a buffer, just return the number of bytes we would
642 have copied if we had a buffer. */
643 if (!buf) {
644 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300645 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
646 ? 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300647 return bytes_to_copy;
648 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300649
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300650 /* copy the data to video buffer */
651 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300652
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300653 dma_q->pos += bytes_to_copy;
654 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300655
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300656 if (dma_q->bytes_left_in_line == 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300657 dma_q->bytes_left_in_line = _line_size;
658 dma_q->lines_completed++;
659 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300660
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300661 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300662 buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300663
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300664 dma_q->pos = 0;
665 buf = NULL;
666 dma_q->lines_completed = 0;
667 }
668 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300669
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300670 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300671}
672
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300673void cx231xx_reset_video_buffer(struct cx231xx *dev,
674 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300675{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300676 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300677
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300678 /* handle the switch from field 1 to field 2 */
679 if (dma_q->current_field == 1) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300680 if (dma_q->lines_completed >= dma_q->lines_per_field)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300681 dma_q->field1_done = 1;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300682 else
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300683 dma_q->field1_done = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300684 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300685
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300686 if (dev->USE_ISO)
687 buf = dev->video_mode.isoc_ctl.buf;
688 else
689 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300690
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300691 if (buf == NULL) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300692 /* first try to get the buffer */
693 get_next_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300694
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300695 dma_q->pos = 0;
696 dma_q->field1_done = 0;
697 dma_q->current_field = -1;
698 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300699
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300700 /* reset the counters */
701 dma_q->bytes_left_in_line = dev->width << 1;
702 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300703}
704
705int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300706 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300707{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300708 u8 *p_out_buffer = NULL;
709 u32 current_line_bytes_copied = 0;
710 struct cx231xx_buffer *buf;
711 u32 _line_size = dev->width << 1;
712 void *startwrite;
713 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300714
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300715 if (dev->USE_ISO)
716 buf = dev->video_mode.isoc_ctl.buf;
717 else
718 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300719
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300720 if (buf == NULL)
721 return -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300722
723 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
724
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300725 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300726
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300727 /* Offset field 2 one line from the top of the buffer */
728 offset = (dma_q->current_field == 1) ? 0 : _line_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300729
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300730 /* Offset for field 2 */
731 startwrite = p_out_buffer + offset;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300732
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300733 /* lines already completed in the current field */
734 startwrite += (dma_q->lines_completed * _line_size * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300735
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300736 /* bytes already completed in the current line */
737 startwrite += current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300738
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300739 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
740 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300741
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300742 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300743 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300744
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300745 /* The below copies the UYVY data straight into video buffer */
746 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300747
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300748 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300749}
750
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300751void cx231xx_swab(u16 *from, u16 *to, u16 len)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300752{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300753 u16 i;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300754
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300755 if (len <= 0)
756 return;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300757
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300758 for (i = 0; i < len / 2; i++)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300759 to[i] = (from[i] << 8) | (from[i] >> 8);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300760}
761
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300762u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300763{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300764 u8 buffer_complete = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300765
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300766 /* Dual field stream */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300767 buffer_complete = ((dma_q->current_field == 2) &&
768 (dma_q->lines_completed >= dma_q->lines_per_field) &&
769 dma_q->field1_done);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300770
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300771 return buffer_complete;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300772}
773
Sri Deevie0d3baf2009-03-03 14:37:50 -0300774/* ------------------------------------------------------------------
775 Videobuf operations
776 ------------------------------------------------------------------*/
777
778static int
779buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
780{
781 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300782 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300783
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300784 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300785 if (0 == *count)
786 *count = CX231XX_DEF_BUF;
787
788 if (*count < CX231XX_MIN_BUF)
789 *count = CX231XX_MIN_BUF;
790
Sri Deevie0d3baf2009-03-03 14:37:50 -0300791 return 0;
792}
793
794/* This is called *without* dev->slock held; please keep it that way */
795static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
796{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300797 struct cx231xx_fh *fh = vq->priv_data;
798 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300799 unsigned long flags = 0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300800
Sri Deevie0d3baf2009-03-03 14:37:50 -0300801 if (in_interrupt())
802 BUG();
803
804 /* We used to wait for the buffer to finish here, but this didn't work
805 because, as we were keeping the state as VIDEOBUF_QUEUED,
806 videobuf_queue_cancel marked it as finished for us.
807 (Also, it could wedge forever if the hardware was misconfigured.)
808
809 This should be safe; by the time we get here, the buffer isn't
810 queued anymore. If we ever start marking the buffers as
811 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300812 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300813 spin_lock_irqsave(&dev->video_mode.slock, flags);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300814 if (dev->USE_ISO) {
815 if (dev->video_mode.isoc_ctl.buf == buf)
816 dev->video_mode.isoc_ctl.buf = NULL;
817 } else {
818 if (dev->video_mode.bulk_ctl.buf == buf)
819 dev->video_mode.bulk_ctl.buf = NULL;
820 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300821 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
822
823 videobuf_vmalloc_free(&buf->vb);
824 buf->vb.state = VIDEOBUF_NEEDS_INIT;
825}
826
827static int
828buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300829 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300830{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300831 struct cx231xx_fh *fh = vq->priv_data;
832 struct cx231xx_buffer *buf =
833 container_of(vb, struct cx231xx_buffer, vb);
834 struct cx231xx *dev = fh->dev;
835 int rc = 0, urb_init = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300836
837 /* The only currently supported format is 16 bits/pixel */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300838 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
839 + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300840 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300841 return -EINVAL;
842
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300843 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300844 buf->vb.height = dev->height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300845 buf->vb.field = field;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300846
847 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
848 rc = videobuf_iolock(vq, &buf->vb, NULL);
849 if (rc < 0)
850 goto fail;
851 }
852
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300853 if (dev->USE_ISO) {
854 if (!dev->video_mode.isoc_ctl.num_bufs)
855 urb_init = 1;
856 } else {
857 if (!dev->video_mode.bulk_ctl.num_bufs)
858 urb_init = 1;
859 }
860 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
861 urb_init, dev->video_mode.max_pkt_size);*/
Sri Deevie0d3baf2009-03-03 14:37:50 -0300862 if (urb_init) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300863 dev->mode_tv = 0;
864 if (dev->USE_ISO)
865 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300866 CX231XX_NUM_BUFS,
867 dev->video_mode.max_pkt_size,
868 cx231xx_isoc_copy);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300869 else
870 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
871 CX231XX_NUM_BUFS,
872 dev->video_mode.max_pkt_size,
873 cx231xx_bulk_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300874 if (rc < 0)
875 goto fail;
876 }
877
878 buf->vb.state = VIDEOBUF_PREPARED;
879 return 0;
880
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300881fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300882 free_buffer(vq, buf);
883 return rc;
884}
885
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300886static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300887{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300888 struct cx231xx_buffer *buf =
889 container_of(vb, struct cx231xx_buffer, vb);
890 struct cx231xx_fh *fh = vq->priv_data;
891 struct cx231xx *dev = fh->dev;
892 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300893
894 buf->vb.state = VIDEOBUF_QUEUED;
895 list_add_tail(&buf->vb.queue, &vidq->active);
896
897}
898
899static void buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300900 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300901{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300902 struct cx231xx_buffer *buf =
903 container_of(vb, struct cx231xx_buffer, vb);
904 struct cx231xx_fh *fh = vq->priv_data;
905 struct cx231xx *dev = (struct cx231xx *)fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300906
907 cx231xx_isocdbg("cx231xx: called buffer_release\n");
908
909 free_buffer(vq, buf);
910}
911
912static struct videobuf_queue_ops cx231xx_video_qops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300913 .buf_setup = buffer_setup,
914 .buf_prepare = buffer_prepare,
915 .buf_queue = buffer_queue,
916 .buf_release = buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300917};
918
919/********************* v4l2 interface **************************************/
920
Sri Deevie0d3baf2009-03-03 14:37:50 -0300921void video_mux(struct cx231xx *dev, int index)
922{
Sri Deevie0d3baf2009-03-03 14:37:50 -0300923 dev->video_input = index;
924 dev->ctl_ainput = INPUT(index)->amux;
925
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300926 cx231xx_set_video_input_mux(dev, index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300927
Hans Verkuil5325b422009-04-02 11:26:22 -0300928 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300929
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300930 cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300931
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300932 cx231xx_info("video_mux : %d\n", index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300933
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300934 /* do mode control overrides if required */
935 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300936}
937
938/* Usage lock check functions */
939static int res_get(struct cx231xx_fh *fh)
940{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300941 struct cx231xx *dev = fh->dev;
942 int rc = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300943
944 /* This instance already has stream_on */
945 if (fh->stream_on)
946 return rc;
947
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300948 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
949 if (dev->stream_on)
950 return -EBUSY;
951 dev->stream_on = 1;
952 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
953 if (dev->vbi_stream_on)
954 return -EBUSY;
955 dev->vbi_stream_on = 1;
956 } else
957 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300958
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300959 fh->stream_on = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300960
961 return rc;
962}
963
964static int res_check(struct cx231xx_fh *fh)
965{
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300966 return fh->stream_on;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300967}
968
969static void res_free(struct cx231xx_fh *fh)
970{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300971 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300972
973 fh->stream_on = 0;
974
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300975 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
976 dev->stream_on = 0;
977 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
978 dev->vbi_stream_on = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300979}
980
981static int check_dev(struct cx231xx *dev)
982{
983 if (dev->state & DEV_DISCONNECTED) {
984 cx231xx_errdev("v4l2 ioctl: device not present\n");
985 return -ENODEV;
986 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300987 return 0;
988}
989
Sri Deevie0d3baf2009-03-03 14:37:50 -0300990/* ------------------------------------------------------------------
991 IOCTL vidioc handling
992 ------------------------------------------------------------------*/
993
994static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300995 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300996{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300997 struct cx231xx_fh *fh = priv;
998 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300999
Sri Deevie0d3baf2009-03-03 14:37:50 -03001000 f->fmt.pix.width = dev->width;
1001 f->fmt.pix.height = dev->height;
Joe Perches1ebcad72009-07-02 15:57:09 -03001002 f->fmt.pix.pixelformat = dev->format->fourcc;
1003 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001004 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001005 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1006
1007 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1008
Sri Deevie0d3baf2009-03-03 14:37:50 -03001009 return 0;
1010}
1011
1012static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
1013{
1014 unsigned int i;
1015
1016 for (i = 0; i < ARRAY_SIZE(format); i++)
1017 if (format[i].fourcc == fourcc)
1018 return &format[i];
1019
1020 return NULL;
1021}
1022
1023static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001024 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001025{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001026 struct cx231xx_fh *fh = priv;
1027 struct cx231xx *dev = fh->dev;
Trent Piepho9bd0e8d2009-05-30 21:45:46 -03001028 unsigned int width = f->fmt.pix.width;
1029 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001030 unsigned int maxw = norm_maxw(dev);
1031 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001032 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001033
1034 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1035 if (!fmt) {
1036 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001037 f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001038 return -EINVAL;
1039 }
1040
1041 /* width must even because of the YUYV format
1042 height must be even because of interlacing */
Trent Piepho9bd0e8d2009-05-30 21:45:46 -03001043 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001044
Sri Deevie0d3baf2009-03-03 14:37:50 -03001045 f->fmt.pix.width = width;
1046 f->fmt.pix.height = height;
1047 f->fmt.pix.pixelformat = fmt->fourcc;
1048 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1049 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1050 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1051 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1052
1053 return 0;
1054}
1055
1056static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001057 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001058{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001059 struct cx231xx_fh *fh = priv;
1060 struct cx231xx *dev = fh->dev;
1061 int rc;
1062 struct cx231xx_fmt *fmt;
Hans Verkuil112cb4a2010-05-09 10:11:01 -03001063 struct v4l2_mbus_framefmt mbus_fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001064
1065 rc = check_dev(dev);
1066 if (rc < 0)
1067 return rc;
1068
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001069 vidioc_try_fmt_vid_cap(file, priv, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001070
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001071 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001072 if (!fmt)
1073 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001074
1075 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1076 cx231xx_errdev("%s queue busy\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001077 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001078 }
1079
1080 if (dev->stream_on && !fh->stream_on) {
1081 cx231xx_errdev("%s device in use by another fh\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001082 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001083 }
1084
1085 /* set new image size */
1086 dev->width = f->fmt.pix.width;
1087 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001088 dev->format = fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001089
Hans Verkuil112cb4a2010-05-09 10:11:01 -03001090 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1091 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1092 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001093
Sri Deevie0d3baf2009-03-03 14:37:50 -03001094 return rc;
1095}
1096
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001097static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001098{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001099 struct cx231xx_fh *fh = priv;
1100 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001101
1102 *id = dev->norm;
1103 return 0;
1104}
1105
Sri Deevib1196122009-03-20 23:33:48 -03001106static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001107{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001108 struct cx231xx_fh *fh = priv;
1109 struct cx231xx *dev = fh->dev;
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001110 struct v4l2_mbus_framefmt mbus_fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001111 struct v4l2_format f;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001112 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001113
1114 rc = check_dev(dev);
1115 if (rc < 0)
1116 return rc;
1117
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001118 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001119
Sri Deevie0d3baf2009-03-03 14:37:50 -03001120 dev->norm = *norm;
1121
Sri Deevie0d3baf2009-03-03 14:37:50 -03001122 /* Adjusts width/height, if needed */
1123 f.fmt.pix.width = dev->width;
1124 f.fmt.pix.height = dev->height;
1125 vidioc_try_fmt_vid_cap(file, priv, &f);
1126
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001127 call_all(dev, core, s_std, dev->norm);
1128
1129 /* We need to reset basic properties in the decoder related to
1130 resolution (since a standard change effects things like the number
1131 of lines in VACT, etc) */
1132 v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
1133 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1134 v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
1135
Sri Deevie0d3baf2009-03-03 14:37:50 -03001136 /* set new image size */
1137 dev->width = f.fmt.pix.width;
1138 dev->height = f.fmt.pix.height;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001139
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001140 /* do mode control overrides */
1141 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001142
1143 return 0;
1144}
1145
1146static const char *iname[] = {
1147 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001148 [CX231XX_VMUX_SVIDEO] = "S-Video",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001149 [CX231XX_VMUX_TELEVISION] = "Television",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001150 [CX231XX_VMUX_CABLE] = "Cable TV",
1151 [CX231XX_VMUX_DVB] = "DVB",
1152 [CX231XX_VMUX_DEBUG] = "for debug only",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001153};
1154
1155static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001156 struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001157{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001158 struct cx231xx_fh *fh = priv;
1159 struct cx231xx *dev = fh->dev;
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001160 u32 gen_stat;
1161 unsigned int ret, n;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001162
1163 n = i->index;
1164 if (n >= MAX_CX231XX_INPUT)
1165 return -EINVAL;
1166 if (0 == INPUT(n)->type)
1167 return -EINVAL;
1168
1169 i->index = n;
1170 i->type = V4L2_INPUT_TYPE_CAMERA;
1171
1172 strcpy(i->name, iname[INPUT(n)->type]);
1173
1174 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001175 (CX231XX_VMUX_CABLE == INPUT(n)->type))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001176 i->type = V4L2_INPUT_TYPE_TUNER;
1177
1178 i->std = dev->vdev->tvnorms;
1179
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001180 /* If they are asking about the active input, read signal status */
1181 if (n == dev->video_input) {
1182 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1183 GEN_STAT, 2, &gen_stat, 4);
1184 if (ret > 0) {
1185 if ((gen_stat & FLD_VPRES) == 0x00)
1186 i->status |= V4L2_IN_ST_NO_SIGNAL;
1187 if ((gen_stat & FLD_HLOCK) == 0x00)
1188 i->status |= V4L2_IN_ST_NO_H_LOCK;
1189 }
1190 }
1191
Sri Deevie0d3baf2009-03-03 14:37:50 -03001192 return 0;
1193}
1194
1195static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1196{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001197 struct cx231xx_fh *fh = priv;
1198 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001199
1200 *i = dev->video_input;
1201
1202 return 0;
1203}
1204
1205static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1206{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001207 struct cx231xx_fh *fh = priv;
1208 struct cx231xx *dev = fh->dev;
1209 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001210
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001211 dev->mode_tv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001212 rc = check_dev(dev);
1213 if (rc < 0)
1214 return rc;
1215
1216 if (i >= MAX_CX231XX_INPUT)
1217 return -EINVAL;
1218 if (0 == INPUT(i)->type)
1219 return -EINVAL;
1220
Sri Deevie0d3baf2009-03-03 14:37:50 -03001221 video_mux(dev, i);
1222
Devin Heitmuellerc09d6692010-07-12 16:50:30 -03001223 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1224 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1225 /* There's a tuner, so reset the standard and put it on the
1226 last known frequency (since it was probably powered down
1227 until now */
1228 call_all(dev, core, s_std, dev->norm);
1229 }
1230
Sri Deevie0d3baf2009-03-03 14:37:50 -03001231 return 0;
1232}
1233
1234static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1235{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001236 struct cx231xx_fh *fh = priv;
1237 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001238
1239 switch (a->index) {
1240 case CX231XX_AMUX_VIDEO:
1241 strcpy(a->name, "Television");
1242 break;
1243 case CX231XX_AMUX_LINE_IN:
1244 strcpy(a->name, "Line In");
1245 break;
1246 default:
1247 return -EINVAL;
1248 }
1249
1250 a->index = dev->ctl_ainput;
1251 a->capability = V4L2_AUDCAP_STEREO;
1252
1253 return 0;
1254}
1255
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001256static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001257{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001258 struct cx231xx_fh *fh = priv;
1259 struct cx231xx *dev = fh->dev;
1260 int status = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001261
1262 /* Doesn't allow manual routing */
1263 if (a->index != dev->ctl_ainput)
1264 return -EINVAL;
1265
1266 dev->ctl_ainput = INPUT(a->index)->amux;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001267 status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001268
1269 return status;
1270}
1271
1272static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001273 struct v4l2_queryctrl *qc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001274{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001275 struct cx231xx_fh *fh = priv;
1276 struct cx231xx *dev = fh->dev;
1277 int id = qc->id;
1278 int i;
1279 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001280
1281 rc = check_dev(dev);
1282 if (rc < 0)
1283 return rc;
1284
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001285 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001286 if (unlikely(qc->id == 0))
1287 return -EINVAL;
1288
1289 memset(qc, 0, sizeof(*qc));
1290
1291 qc->id = id;
1292
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001293 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001294 return -EINVAL;
1295
1296 for (i = 0; i < CX231XX_CTLS; i++)
1297 if (cx231xx_ctls[i].v.id == qc->id)
1298 break;
1299
1300 if (i == CX231XX_CTLS) {
1301 *qc = no_ctl;
1302 return 0;
1303 }
1304 *qc = cx231xx_ctls[i].v;
1305
Sri Deevib1196122009-03-20 23:33:48 -03001306 call_all(dev, core, queryctrl, qc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001307
1308 if (qc->type)
1309 return 0;
1310 else
1311 return -EINVAL;
1312}
1313
1314static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001315 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001316{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001317 struct cx231xx_fh *fh = priv;
1318 struct cx231xx *dev = fh->dev;
1319 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001320
1321 rc = check_dev(dev);
1322 if (rc < 0)
1323 return rc;
1324
Sri Deevib1196122009-03-20 23:33:48 -03001325 call_all(dev, core, g_ctrl, ctrl);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001326 return rc;
1327}
1328
1329static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001330 struct v4l2_control *ctrl)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001331{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001332 struct cx231xx_fh *fh = priv;
1333 struct cx231xx *dev = fh->dev;
1334 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001335
1336 rc = check_dev(dev);
1337 if (rc < 0)
1338 return rc;
1339
Sri Deevib1196122009-03-20 23:33:48 -03001340 call_all(dev, core, s_ctrl, ctrl);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001341 return rc;
1342}
1343
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001344static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001345{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001346 struct cx231xx_fh *fh = priv;
1347 struct cx231xx *dev = fh->dev;
1348 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001349
1350 rc = check_dev(dev);
1351 if (rc < 0)
1352 return rc;
1353
1354 if (0 != t->index)
1355 return -EINVAL;
1356
1357 strcpy(t->name, "Tuner");
1358
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001359 t->type = V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001360 t->capability = V4L2_TUNER_CAP_NORM;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001361 t->rangehigh = 0xffffffffUL;
1362 t->signal = 0xffff; /* LOCKED */
Sri Deevie0d3baf2009-03-03 14:37:50 -03001363
1364 return 0;
1365}
1366
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001367static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001368{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001369 struct cx231xx_fh *fh = priv;
1370 struct cx231xx *dev = fh->dev;
1371 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001372
1373 rc = check_dev(dev);
1374 if (rc < 0)
1375 return rc;
1376
1377 if (0 != t->index)
1378 return -EINVAL;
1379#if 0
Sri Deevib1196122009-03-20 23:33:48 -03001380 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001381#endif
1382 return 0;
1383}
1384
1385static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001386 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001387{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001388 struct cx231xx_fh *fh = priv;
1389 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001390
Sri Deevie0d3baf2009-03-03 14:37:50 -03001391 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1392 f->frequency = dev->ctl_freq;
1393
Sri Deevib1196122009-03-20 23:33:48 -03001394 call_all(dev, tuner, g_frequency, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001395
Sri Deevie0d3baf2009-03-03 14:37:50 -03001396 return 0;
1397}
1398
1399static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001400 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001401{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001402 struct cx231xx_fh *fh = priv;
1403 struct cx231xx *dev = fh->dev;
1404 int rc;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001405 u32 if_frequency = 5400000;
1406
1407 cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1408 f->frequency, f->type);
1409 /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
Sri Deevie0d3baf2009-03-03 14:37:50 -03001410
1411 rc = check_dev(dev);
1412 if (rc < 0)
1413 return rc;
1414
1415 if (0 != f->tuner)
1416 return -EINVAL;
1417
1418 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1419 return -EINVAL;
1420 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1421 return -EINVAL;
1422
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001423 /* set pre channel change settings in DIF first */
1424 rc = cx231xx_tuner_pre_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001425
Sri Deevie0d3baf2009-03-03 14:37:50 -03001426 dev->ctl_freq = f->frequency;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001427 call_all(dev, tuner, s_frequency, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001428
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001429 /* set post channel change settings in DIF first */
1430 rc = cx231xx_tuner_post_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001431
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001432 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1433 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1434 if_frequency = 5400000; /*5.4MHz */
1435 else if (dev->norm & V4L2_STD_B)
1436 if_frequency = 6000000; /*6.0MHz */
1437 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1438 if_frequency = 6900000; /*6.9MHz */
1439 else if (dev->norm & V4L2_STD_GH)
1440 if_frequency = 7100000; /*7.1MHz */
1441 else if (dev->norm & V4L2_STD_PAL_I)
1442 if_frequency = 7250000; /*7.25MHz */
1443 else if (dev->norm & V4L2_STD_SECAM_L)
1444 if_frequency = 6900000; /*6.9MHz */
1445 else if (dev->norm & V4L2_STD_SECAM_LC)
1446 if_frequency = 1250000; /*1.25MHz */
1447
1448 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1449 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1450
1451 update_HH_register_after_set_DIF(dev);
1452 }
1453
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001454 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001455
1456 return rc;
1457}
1458
Hans Verkuilfddd14c2012-09-13 05:37:11 -03001459static int vidioc_g_chip_ident(struct file *file, void *fh,
1460 struct v4l2_dbg_chip_ident *chip)
1461{
1462 chip->ident = V4L2_IDENT_NONE;
1463 chip->revision = 0;
1464 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1465 if (v4l2_chip_match_host(&chip->match))
1466 chip->ident = V4L2_IDENT_CX23100;
1467 return 0;
1468 }
1469 return -EINVAL;
1470}
1471
Sri Deevie0d3baf2009-03-03 14:37:50 -03001472#ifdef CONFIG_VIDEO_ADV_DEBUG
1473
Sri Deevie0d3baf2009-03-03 14:37:50 -03001474/*
Sri Deevib9255172009-03-04 17:49:01 -03001475 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1476 chip=<chip>[,min=<addr>,max=<addr>]
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001477 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
Sri Deevib9255172009-03-04 17:49:01 -03001478 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1479 chip=<chip>,reg=<addr>,val=<val>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001480 set the register [VIDIOC_DBG_S_REGISTER]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001481
1482 if type == host, then <chip> is the hosts chip ID (default 0)
1483 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1484 if type == i2caddr, then <chip> is the 7-bit I2C address
1485*/
1486
Sri Deevie0d3baf2009-03-03 14:37:50 -03001487static int vidioc_g_register(struct file *file, void *priv,
1488 struct v4l2_dbg_register *reg)
1489{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001490 struct cx231xx_fh *fh = priv;
1491 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001492 int ret = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001493 u8 value[4] = { 0, 0, 0, 0 };
1494 u32 data = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001495
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001496 switch (reg->match.type) {
1497 case V4L2_CHIP_MATCH_HOST:
1498 switch (reg->match.addr) {
1499 case 0: /* Cx231xx - internal registers */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001500 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1501 (u16)reg->reg, value, 4);
1502 reg->val = value[0] | value[1] << 8 |
1503 value[2] << 16 | value[3] << 24;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001504 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001505 case 1: /* AFE - read byte */
1506 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001507 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001508 reg->val = le32_to_cpu(data & 0xff);
1509 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001510 case 14: /* AFE - read dword */
1511 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001512 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001513 reg->val = le32_to_cpu(data);
1514 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001515 case 2: /* Video Block - read byte */
1516 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001517 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001518 reg->val = le32_to_cpu(data & 0xff);
1519 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001520 case 24: /* Video Block - read dword */
1521 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001522 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001523 reg->val = le32_to_cpu(data);
1524 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001525 case 3: /* I2S block - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001526 ret = cx231xx_read_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 &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001530 reg->val = le32_to_cpu(data & 0xff);
1531 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001532 case 34: /* I2S Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001533 ret =
Sri Deeviecc67d12009-03-21 22:00:20 -03001534 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001535 (u16)reg->reg, 1, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001536 reg->val = le32_to_cpu(data);
1537 break;
1538 }
1539 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001540
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001541 case V4L2_CHIP_MATCH_I2C_DRIVER:
Sri Deevib1196122009-03-20 23:33:48 -03001542 call_all(dev, core, g_register, reg);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001543 return 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001544 case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
1545 switch (reg->match.addr) {
1546 case 0: /* Cx231xx - internal registers */
1547 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1548 (u16)reg->reg, value, 4);
1549 reg->val = value[0] | value[1] << 8 |
1550 value[2] << 16 | value[3] << 24;
1551
1552 break;
1553 case 0x600:/* AFE - read byte */
1554 ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
1555 (u16)reg->reg, 2,
1556 &data, 1 , 0);
1557 reg->val = le32_to_cpu(data & 0xff);
1558 break;
1559
1560 case 0x880:/* Video Block - read byte */
1561 if (reg->reg < 0x0b) {
1562 ret = cx231xx_read_i2c_master(dev,
1563 VID_BLK_I2C_ADDRESS,
1564 (u16)reg->reg, 2,
1565 &data, 1 , 0);
1566 reg->val = le32_to_cpu(data & 0xff);
1567 } else {
1568 ret = cx231xx_read_i2c_master(dev,
1569 VID_BLK_I2C_ADDRESS,
1570 (u16)reg->reg, 2,
1571 &data, 4 , 0);
1572 reg->val = le32_to_cpu(data);
1573 }
1574 break;
1575 case 0x980:
1576 ret = cx231xx_read_i2c_master(dev,
1577 I2S_BLK_DEVICE_ADDRESS,
1578 (u16)reg->reg, 1,
1579 &data, 1 , 0);
1580 reg->val = le32_to_cpu(data & 0xff);
1581 break;
1582 case 0x400:
1583 ret =
1584 cx231xx_read_i2c_master(dev, 0x40,
1585 (u16)reg->reg, 1,
1586 &data, 1 , 0);
1587 reg->val = le32_to_cpu(data & 0xff);
1588 break;
1589 case 0xc01:
1590 ret =
1591 cx231xx_read_i2c_master(dev, 0xc0,
1592 (u16)reg->reg, 2,
1593 &data, 38, 1);
1594 reg->val = le32_to_cpu(data);
1595 break;
1596 case 0x022:
1597 ret =
1598 cx231xx_read_i2c_master(dev, 0x02,
1599 (u16)reg->reg, 1,
1600 &data, 1, 2);
1601 reg->val = le32_to_cpu(data & 0xff);
1602 break;
1603 case 0x322:
1604 ret = cx231xx_read_i2c_master(dev,
1605 0x32,
1606 (u16)reg->reg, 1,
1607 &data, 4 , 2);
1608 reg->val = le32_to_cpu(data);
1609 break;
1610 case 0x342:
1611 ret = cx231xx_read_i2c_master(dev,
1612 0x34,
1613 (u16)reg->reg, 1,
1614 &data, 4 , 2);
1615 reg->val = le32_to_cpu(data);
1616 break;
1617
1618 default:
1619 cx231xx_info("no match device address!!\n");
1620 break;
1621 }
1622 return ret < 0 ? ret : 0;
1623 /*return -EINVAL;*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001624 default:
1625 if (!v4l2_chip_match_host(&reg->match))
1626 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001627 }
1628
Sri Deevib1196122009-03-20 23:33:48 -03001629 call_all(dev, core, g_register, reg);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001630
1631 return ret;
1632}
1633
1634static int vidioc_s_register(struct file *file, void *priv,
1635 struct v4l2_dbg_register *reg)
1636{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001637 struct cx231xx_fh *fh = priv;
1638 struct cx231xx *dev = fh->dev;
1639 int ret = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001640 __le64 buf;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001641 u32 value;
1642 u8 data[4] = { 0, 0, 0, 0 };
Sri Deevie0d3baf2009-03-03 14:37:50 -03001643
1644 buf = cpu_to_le64(reg->val);
1645
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001646 switch (reg->match.type) {
1647 case V4L2_CHIP_MATCH_HOST:
1648 {
1649 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001650
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001651 switch (reg->match.addr) {
1652 case 0: /* cx231xx internal registers */
1653 data[0] = (u8) value;
1654 data[1] = (u8) (value >> 8);
1655 data[2] = (u8) (value >> 16);
1656 data[3] = (u8) (value >> 24);
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001657 ret = cx231xx_write_ctrl_reg(dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001658 VRT_SET_REGISTER,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001659 (u16)reg->reg, data,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001660 4);
1661 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001662 case 1: /* AFE - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001663 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001664 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001665 (u16)reg->reg, 2,
1666 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001667 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001668 case 14: /* AFE - read dword */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001669 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001670 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001671 (u16)reg->reg, 2,
1672 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001673 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001674 case 2: /* Video Block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001675 ret =
1676 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001677 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001678 (u16)reg->reg, 2,
1679 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001680 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001681 case 24: /* Video Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001682 ret =
1683 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001684 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001685 (u16)reg->reg, 2,
1686 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001687 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001688 case 3: /* I2S block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001689 ret =
1690 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001691 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001692 (u16)reg->reg, 1,
1693 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001694 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001695 case 34: /* I2S block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001696 ret =
1697 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001698 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001699 (u16)reg->reg, 1,
1700 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001701 break;
1702 }
1703 }
1704 return ret < 0 ? ret : 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001705 case V4L2_CHIP_MATCH_I2C_ADDR:
1706 {
1707 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001708
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001709 switch (reg->match.addr) {
1710 case 0:/*cx231xx internal registers*/
1711 data[0] = (u8) value;
1712 data[1] = (u8) (value >> 8);
1713 data[2] = (u8) (value >> 16);
1714 data[3] = (u8) (value >> 24);
1715 ret = cx231xx_write_ctrl_reg(dev,
1716 VRT_SET_REGISTER,
1717 (u16)reg->reg, data,
1718 4);
1719 break;
1720 case 0x600:/* AFE - read byte */
1721 ret = cx231xx_write_i2c_master(dev,
1722 AFE_DEVICE_ADDRESS,
1723 (u16)reg->reg, 2,
1724 value, 1 , 0);
1725 break;
1726
1727 case 0x880:/* Video Block - read byte */
1728 if (reg->reg < 0x0b)
1729 cx231xx_write_i2c_master(dev,
1730 VID_BLK_I2C_ADDRESS,
1731 (u16)reg->reg, 2,
1732 value, 1, 0);
1733 else
1734 cx231xx_write_i2c_master(dev,
1735 VID_BLK_I2C_ADDRESS,
1736 (u16)reg->reg, 2,
1737 value, 4, 0);
1738 break;
1739 case 0x980:
1740 ret =
1741 cx231xx_write_i2c_master(dev,
1742 I2S_BLK_DEVICE_ADDRESS,
1743 (u16)reg->reg, 1,
1744 value, 1, 0);
1745 break;
1746 case 0x400:
1747 ret =
1748 cx231xx_write_i2c_master(dev,
1749 0x40,
1750 (u16)reg->reg, 1,
1751 value, 1, 0);
1752 break;
1753 case 0xc01:
1754 ret =
1755 cx231xx_write_i2c_master(dev,
1756 0xc0,
1757 (u16)reg->reg, 1,
1758 value, 1, 1);
1759 break;
1760
1761 case 0x022:
1762 ret =
1763 cx231xx_write_i2c_master(dev,
1764 0x02,
1765 (u16)reg->reg, 1,
1766 value, 1, 2);
Dan Carpenterf62436a2013-01-11 02:48:41 -03001767 break;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001768 case 0x322:
1769 ret =
1770 cx231xx_write_i2c_master(dev,
1771 0x32,
1772 (u16)reg->reg, 1,
1773 value, 4, 2);
1774 break;
1775
1776 case 0x342:
1777 ret =
1778 cx231xx_write_i2c_master(dev,
1779 0x34,
1780 (u16)reg->reg, 1,
1781 value, 4, 2);
1782 break;
1783 default:
1784 cx231xx_info("no match device address, "
1785 "the value is %x\n", reg->match.addr);
1786 break;
1787
1788 }
1789
1790 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001791 default:
1792 break;
1793 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001794
Sri Deevib1196122009-03-20 23:33:48 -03001795 call_all(dev, core, s_register, reg);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001796
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001797 return ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001798}
1799#endif
1800
Sri Deevie0d3baf2009-03-03 14:37:50 -03001801static int vidioc_cropcap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001802 struct v4l2_cropcap *cc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001803{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001804 struct cx231xx_fh *fh = priv;
1805 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001806
1807 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1808 return -EINVAL;
1809
1810 cc->bounds.left = 0;
1811 cc->bounds.top = 0;
1812 cc->bounds.width = dev->width;
1813 cc->bounds.height = dev->height;
1814 cc->defrect = cc->bounds;
1815 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1816 cc->pixelaspect.denominator = 59;
1817
1818 return 0;
1819}
1820
1821static int vidioc_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001822 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001823{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001824 struct cx231xx_fh *fh = priv;
1825 struct cx231xx *dev = fh->dev;
1826 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001827
1828 rc = check_dev(dev);
1829 if (rc < 0)
1830 return rc;
1831
Sri Deevie0d3baf2009-03-03 14:37:50 -03001832 rc = res_get(fh);
1833
1834 if (likely(rc >= 0))
1835 rc = videobuf_streamon(&fh->vb_vidq);
1836
Sri Deevib1196122009-03-20 23:33:48 -03001837 call_all(dev, video, s_stream, 1);
1838
Sri Deevie0d3baf2009-03-03 14:37:50 -03001839 return rc;
1840}
1841
1842static int vidioc_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001843 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001844{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001845 struct cx231xx_fh *fh = priv;
1846 struct cx231xx *dev = fh->dev;
1847 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001848
1849 rc = check_dev(dev);
1850 if (rc < 0)
1851 return rc;
1852
Mauro Carvalho Chehab92fcbd32009-03-21 22:16:34 -03001853 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001854 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001855 return -EINVAL;
1856 if (type != fh->type)
1857 return -EINVAL;
1858
Sri Deevib1196122009-03-20 23:33:48 -03001859 cx25840_call(dev, video, s_stream, 0);
1860
Sri Deevie0d3baf2009-03-03 14:37:50 -03001861 videobuf_streamoff(&fh->vb_vidq);
1862 res_free(fh);
1863
Sri Deevie0d3baf2009-03-03 14:37:50 -03001864 return 0;
1865}
1866
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001867static int vidioc_querycap(struct file *file, void *priv,
1868 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001869{
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001870 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001871 struct cx231xx_fh *fh = priv;
1872 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001873
1874 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1875 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab2c6beca2009-03-22 08:53:36 -03001876 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001877
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001878 cap->device_caps =
Sri Deevi6e4f5742009-03-10 21:16:26 -03001879 V4L2_CAP_AUDIO |
1880 V4L2_CAP_READWRITE |
1881 V4L2_CAP_STREAMING;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001882
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001883 if (vdev->vfl_type == VFL_TYPE_VBI)
1884 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1885 else
1886 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001887 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001888 cap->device_caps |= V4L2_CAP_TUNER;
1889 cap->capabilities = cap->device_caps |
1890 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1891 V4L2_CAP_DEVICE_CAPS;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001892
1893 return 0;
1894}
1895
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001896static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1897 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001898{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001899 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001900 return -EINVAL;
1901
1902 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1903 f->pixelformat = format[f->index].fourcc;
1904
1905 return 0;
1906}
1907
1908/* Sliced VBI ioctls */
1909static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001910 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001911{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001912 struct cx231xx_fh *fh = priv;
1913 struct cx231xx *dev = fh->dev;
1914 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001915
1916 rc = check_dev(dev);
1917 if (rc < 0)
1918 return rc;
1919
Sri Deevie0d3baf2009-03-03 14:37:50 -03001920 f->fmt.sliced.service_set = 0;
1921
Hans Verkuil7e87ddb2010-03-14 12:31:08 -03001922 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001923
1924 if (f->fmt.sliced.service_set == 0)
1925 rc = -EINVAL;
1926
Sri Deevie0d3baf2009-03-03 14:37:50 -03001927 return rc;
1928}
1929
1930static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001931 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001932{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001933 struct cx231xx_fh *fh = priv;
1934 struct cx231xx *dev = fh->dev;
1935 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001936
1937 rc = check_dev(dev);
1938 if (rc < 0)
1939 return rc;
1940
Hans Verkuil7e87ddb2010-03-14 12:31:08 -03001941 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001942
1943 if (f->fmt.sliced.service_set == 0)
1944 return -EINVAL;
1945
1946 return 0;
1947}
1948
Sri Deevie0d3baf2009-03-03 14:37:50 -03001949/* RAW VBI ioctls */
1950
1951static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001952 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001953{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001954 struct cx231xx_fh *fh = priv;
1955 struct cx231xx *dev = fh->dev;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001956 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001957 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1958 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001959 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001960 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001961 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001962 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001963 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001964 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001965 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001966 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1967
1968 return 0;
1969
1970}
1971
1972static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001973 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001974{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001975 struct cx231xx_fh *fh = priv;
1976 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001977
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001978 if (dev->vbi_stream_on && !fh->stream_on) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001979 cx231xx_errdev("%s device in use by another fh\n", __func__);
1980 return -EBUSY;
1981 }
1982
1983 f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001984 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001985 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1986 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001987 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001988 f->fmt.vbi.flags = 0;
1989 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001990 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001991 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001992 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001993 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001994 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001995 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1996
1997 return 0;
1998
1999}
2000
2001static int vidioc_reqbufs(struct file *file, void *priv,
2002 struct v4l2_requestbuffers *rb)
2003{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002004 struct cx231xx_fh *fh = priv;
2005 struct cx231xx *dev = fh->dev;
2006 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002007
2008 rc = check_dev(dev);
2009 if (rc < 0)
2010 return rc;
2011
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002012 return videobuf_reqbufs(&fh->vb_vidq, rb);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002013}
2014
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002015static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002016{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002017 struct cx231xx_fh *fh = priv;
2018 struct cx231xx *dev = fh->dev;
2019 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002020
2021 rc = check_dev(dev);
2022 if (rc < 0)
2023 return rc;
2024
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002025 return videobuf_querybuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002026}
2027
2028static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2029{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002030 struct cx231xx_fh *fh = priv;
2031 struct cx231xx *dev = fh->dev;
2032 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002033
2034 rc = check_dev(dev);
2035 if (rc < 0)
2036 return rc;
2037
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002038 return videobuf_qbuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002039}
2040
2041static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2042{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002043 struct cx231xx_fh *fh = priv;
2044 struct cx231xx *dev = fh->dev;
2045 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002046
2047 rc = check_dev(dev);
2048 if (rc < 0)
2049 return rc;
2050
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002051 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002052}
2053
Sri Deevie0d3baf2009-03-03 14:37:50 -03002054/* ----------------------------------------------------------- */
2055/* RADIO ESPECIFIC IOCTLS */
2056/* ----------------------------------------------------------- */
2057
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002058static int radio_querycap(struct file *file, void *priv,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002059 struct v4l2_capability *cap)
2060{
2061 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2062
2063 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
2064 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
2065 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2066
Sri Deevie0d3baf2009-03-03 14:37:50 -03002067 cap->capabilities = V4L2_CAP_TUNER;
2068 return 0;
2069}
2070
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002071static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002072{
2073 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2074
2075 if (unlikely(t->index > 0))
2076 return -EINVAL;
2077
2078 strcpy(t->name, "Radio");
2079 t->type = V4L2_TUNER_RADIO;
2080
Sri Deevib1196122009-03-20 23:33:48 -03002081 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002082
2083 return 0;
2084}
2085
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002086static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002087{
2088 if (i->index != 0)
2089 return -EINVAL;
2090 strcpy(i->name, "Radio");
2091 i->type = V4L2_INPUT_TYPE_TUNER;
2092
2093 return 0;
2094}
2095
2096static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2097{
2098 if (unlikely(a->index))
2099 return -EINVAL;
2100
2101 strcpy(a->name, "Radio");
2102 return 0;
2103}
2104
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002105static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002106{
2107 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2108
2109 if (0 != t->index)
2110 return -EINVAL;
2111
Sri Deevib1196122009-03-20 23:33:48 -03002112 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002113
2114 return 0;
2115}
2116
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002117static int radio_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002118{
2119 return 0;
2120}
2121
2122static int radio_s_input(struct file *file, void *fh, unsigned int i)
2123{
2124 return 0;
2125}
2126
2127static int radio_queryctrl(struct file *file, void *priv,
2128 struct v4l2_queryctrl *c)
2129{
2130 int i;
2131
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002132 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002133 return -EINVAL;
2134 if (c->id == V4L2_CID_AUDIO_MUTE) {
Dan Carpenter8e475672010-04-02 08:30:10 -03002135 for (i = 0; i < CX231XX_CTLS; i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002136 if (cx231xx_ctls[i].v.id == c->id)
2137 break;
Dan Carpenter8e475672010-04-02 08:30:10 -03002138 }
2139 if (i == CX231XX_CTLS)
2140 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002141 *c = cx231xx_ctls[i].v;
2142 } else
2143 *c = no_ctl;
2144 return 0;
2145}
2146
2147/*
2148 * cx231xx_v4l2_open()
2149 * inits the device and starts isoc transfer
2150 */
2151static int cx231xx_v4l2_open(struct file *filp)
2152{
Sri Deevie0d3baf2009-03-03 14:37:50 -03002153 int errCode = 0, radio = 0;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002154 struct video_device *vdev = video_devdata(filp);
2155 struct cx231xx *dev = video_drvdata(filp);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002156 struct cx231xx_fh *fh;
2157 enum v4l2_buf_type fh_type = 0;
2158
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002159 switch (vdev->vfl_type) {
2160 case VFL_TYPE_GRABBER:
2161 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2162 break;
2163 case VFL_TYPE_VBI:
2164 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2165 break;
2166 case VFL_TYPE_RADIO:
2167 radio = 1;
2168 break;
2169 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002170
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002171 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
2172 video_device_node_name(vdev), v4l2_type_names[fh_type],
2173 dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002174
2175#if 0
2176 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2177 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002178 cx231xx_errdev
2179 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002180 return -EBUSY;
2181 }
2182#endif
2183
2184 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
2185 if (!fh) {
2186 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002187 return -ENOMEM;
2188 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002189 if (mutex_lock_interruptible(&dev->lock)) {
2190 kfree(fh);
2191 return -ERESTARTSYS;
2192 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002193 fh->dev = dev;
2194 fh->radio = radio;
2195 fh->type = fh_type;
2196 filp->private_data = fh;
2197
2198 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2199 dev->width = norm_maxw(dev);
2200 dev->height = norm_maxh(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002201
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002202 /* Power up in Analog TV mode */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03002203 if (dev->board.external_av)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002204 cx231xx_set_power_mode(dev,
2205 POLARIS_AVMODE_ENXTERNAL_AV);
2206 else
2207 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002208
2209#if 0
2210 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2211#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -03002212
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002213 /* set video alternate setting */
2214 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002215
2216 /* Needed, since GPIO might have disabled power of
2217 some i2c device */
2218 cx231xx_config_i2c(dev);
2219
2220 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002221 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002222
2223 }
2224 if (fh->radio) {
2225 cx231xx_videodbg("video_open: setting radio device\n");
2226
2227 /* cx231xx_start_radio(dev); */
2228
Sri Deevib1196122009-03-20 23:33:48 -03002229 call_all(dev, tuner, s_radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002230 }
2231
2232 dev->users++;
2233
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002234 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2235 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2236 NULL, &dev->video_mode.slock,
2237 fh->type, V4L2_FIELD_INTERLACED,
Hans Verkuil08bff032010-09-20 17:39:46 -03002238 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002239 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002240 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002241 /* Set the required alternate setting VBI interface works in
2242 Bulk mode only */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03002243 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002244
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002245 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2246 NULL, &dev->vbi_mode.slock,
2247 fh->type, V4L2_FIELD_SEQ_TB,
Hans Verkuil08bff032010-09-20 17:39:46 -03002248 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002249 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002250 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002251 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002252
Sri Deevie0d3baf2009-03-03 14:37:50 -03002253 return errCode;
2254}
2255
2256/*
2257 * cx231xx_realease_resources()
2258 * unregisters the v4l2,i2c and usb devices
2259 * called when the device gets disconected or at module unload
2260*/
2261void cx231xx_release_analog_resources(struct cx231xx *dev)
2262{
2263
2264 /*FIXME: I2C IR should be disconnected */
2265
2266 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002267 if (video_is_registered(dev->radio_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03002268 video_unregister_device(dev->radio_dev);
2269 else
2270 video_device_release(dev->radio_dev);
2271 dev->radio_dev = NULL;
2272 }
2273 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002274 cx231xx_info("V4L2 device %s deregistered\n",
2275 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002276 if (video_is_registered(dev->vbi_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03002277 video_unregister_device(dev->vbi_dev);
2278 else
2279 video_device_release(dev->vbi_dev);
2280 dev->vbi_dev = NULL;
2281 }
2282 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002283 cx231xx_info("V4L2 device %s deregistered\n",
2284 video_device_node_name(dev->vdev));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002285
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03002286 if (dev->board.has_417)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002287 cx231xx_417_unregister(dev);
2288
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002289 if (video_is_registered(dev->vdev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03002290 video_unregister_device(dev->vdev);
2291 else
2292 video_device_release(dev->vdev);
2293 dev->vdev = NULL;
2294 }
2295}
2296
2297/*
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002298 * cx231xx_close()
Sri Deevie0d3baf2009-03-03 14:37:50 -03002299 * stops streaming and deallocates all resources allocated by the v4l2
2300 * calls and ioctls
2301 */
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002302static int cx231xx_close(struct file *filp)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002303{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002304 struct cx231xx_fh *fh = filp->private_data;
2305 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002306
2307 cx231xx_videodbg("users=%d\n", dev->users);
2308
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002309 cx231xx_videodbg("users=%d\n", dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002310 if (res_check(fh))
2311 res_free(fh);
2312
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03002313 /*
2314 * To workaround error number=-71 on EP0 for VideoGrabber,
2315 * need exclude following.
2316 * FIXME: It is probably safe to remove most of these, as we're
2317 * now avoiding the alternate setting for INDEX_VANC
2318 */
2319 if (!dev->board.no_alt_vanc)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002320 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2321 videobuf_stop(&fh->vb_vidq);
2322 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002323
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002324 /* the device is already disconnect,
2325 free the remaining resources */
2326 if (dev->state & DEV_DISCONNECTED) {
2327 if (atomic_read(&dev->devlist_count) > 0) {
2328 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002329 fh->dev = NULL;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002330 return 0;
2331 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002332 return 0;
2333 }
2334
2335 /* do this before setting alternate! */
2336 cx231xx_uninit_vbi_isoc(dev);
2337
2338 /* set alternate 0 */
2339 if (!dev->vbi_or_sliced_cc_mode)
2340 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2341 else
2342 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2343
2344 kfree(fh);
2345 dev->users--;
2346 wake_up_interruptible_nr(&dev->open, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002347 return 0;
2348 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002349
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -02002350 dev->users--;
2351 if (!dev->users) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002352 videobuf_stop(&fh->vb_vidq);
2353 videobuf_mmap_free(&fh->vb_vidq);
2354
2355 /* the device is already disconnect,
2356 free the remaining resources */
2357 if (dev->state & DEV_DISCONNECTED) {
2358 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002359 fh->dev = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002360 return 0;
2361 }
2362
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002363 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002364 call_all(dev, core, s_power, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002365
2366 /* do this before setting alternate! */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002367 if (dev->USE_ISO)
2368 cx231xx_uninit_isoc(dev);
2369 else
2370 cx231xx_uninit_bulk(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002371 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2372
2373 /* set alternate 0 */
2374 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2375 }
2376 kfree(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002377 wake_up_interruptible_nr(&dev->open, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002378 return 0;
2379}
2380
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002381static int cx231xx_v4l2_close(struct file *filp)
2382{
2383 struct cx231xx_fh *fh = filp->private_data;
2384 struct cx231xx *dev = fh->dev;
2385 int rc;
2386
2387 mutex_lock(&dev->lock);
2388 rc = cx231xx_close(filp);
2389 mutex_unlock(&dev->lock);
2390 return rc;
2391}
2392
Sri Deevie0d3baf2009-03-03 14:37:50 -03002393/*
2394 * cx231xx_v4l2_read()
2395 * will allocate buffers when called for the first time
2396 */
2397static ssize_t
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002398cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2399 loff_t *pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002400{
2401 struct cx231xx_fh *fh = filp->private_data;
2402 struct cx231xx *dev = fh->dev;
2403 int rc;
2404
2405 rc = check_dev(dev);
2406 if (rc < 0)
2407 return rc;
2408
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002409 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2410 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002411 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002412
2413 if (unlikely(rc < 0))
2414 return rc;
2415
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002416 if (mutex_lock_interruptible(&dev->lock))
2417 return -ERESTARTSYS;
2418 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002419 filp->f_flags & O_NONBLOCK);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002420 mutex_unlock(&dev->lock);
2421 return rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002422 }
2423 return 0;
2424}
2425
2426/*
2427 * cx231xx_v4l2_poll()
2428 * will allocate buffers when called for the first time
2429 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002430static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002431{
2432 struct cx231xx_fh *fh = filp->private_data;
2433 struct cx231xx *dev = fh->dev;
2434 int rc;
2435
2436 rc = check_dev(dev);
2437 if (rc < 0)
2438 return rc;
2439
Sri Deevie0d3baf2009-03-03 14:37:50 -03002440 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002441
2442 if (unlikely(rc < 0))
2443 return POLLERR;
2444
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002445 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002446 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
2447 unsigned int res;
2448
2449 mutex_lock(&dev->lock);
2450 res = videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2451 mutex_unlock(&dev->lock);
2452 return res;
2453 }
2454 return POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002455}
2456
2457/*
2458 * cx231xx_v4l2_mmap()
2459 */
2460static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2461{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002462 struct cx231xx_fh *fh = filp->private_data;
2463 struct cx231xx *dev = fh->dev;
2464 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002465
2466 rc = check_dev(dev);
2467 if (rc < 0)
2468 return rc;
2469
Sri Deevie0d3baf2009-03-03 14:37:50 -03002470 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002471
2472 if (unlikely(rc < 0))
2473 return rc;
2474
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002475 if (mutex_lock_interruptible(&dev->lock))
2476 return -ERESTARTSYS;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002477 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002478 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002479
2480 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002481 (unsigned long)vma->vm_start,
2482 (unsigned long)vma->vm_end -
2483 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002484
2485 return rc;
2486}
2487
2488static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002489 .owner = THIS_MODULE,
2490 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002491 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002492 .read = cx231xx_v4l2_read,
2493 .poll = cx231xx_v4l2_poll,
2494 .mmap = cx231xx_v4l2_mmap,
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002495 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002496};
2497
2498static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002499 .vidioc_querycap = vidioc_querycap,
2500 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2501 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2502 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2503 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2504 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2505 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2506 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
2507 .vidioc_g_audio = vidioc_g_audio,
2508 .vidioc_s_audio = vidioc_s_audio,
2509 .vidioc_cropcap = vidioc_cropcap,
2510 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002511 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002512 .vidioc_reqbufs = vidioc_reqbufs,
2513 .vidioc_querybuf = vidioc_querybuf,
2514 .vidioc_qbuf = vidioc_qbuf,
2515 .vidioc_dqbuf = vidioc_dqbuf,
2516 .vidioc_s_std = vidioc_s_std,
2517 .vidioc_g_std = vidioc_g_std,
2518 .vidioc_enum_input = vidioc_enum_input,
2519 .vidioc_g_input = vidioc_g_input,
2520 .vidioc_s_input = vidioc_s_input,
2521 .vidioc_queryctrl = vidioc_queryctrl,
2522 .vidioc_g_ctrl = vidioc_g_ctrl,
2523 .vidioc_s_ctrl = vidioc_s_ctrl,
2524 .vidioc_streamon = vidioc_streamon,
2525 .vidioc_streamoff = vidioc_streamoff,
2526 .vidioc_g_tuner = vidioc_g_tuner,
2527 .vidioc_s_tuner = vidioc_s_tuner,
2528 .vidioc_g_frequency = vidioc_g_frequency,
2529 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilfddd14c2012-09-13 05:37:11 -03002530 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002531#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002532 .vidioc_g_register = vidioc_g_register,
2533 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002534#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -03002535};
2536
2537static struct video_device cx231xx_vbi_template;
2538
2539static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002540 .fops = &cx231xx_v4l_fops,
2541 .release = video_device_release,
2542 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002543 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002544 .current_norm = V4L2_STD_PAL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002545};
2546
2547static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002548 .owner = THIS_MODULE,
2549 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002550 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002551 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002552};
2553
2554static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002555 .vidioc_querycap = radio_querycap,
2556 .vidioc_g_tuner = radio_g_tuner,
2557 .vidioc_enum_input = radio_enum_input,
2558 .vidioc_g_audio = radio_g_audio,
2559 .vidioc_s_tuner = radio_s_tuner,
2560 .vidioc_s_audio = radio_s_audio,
2561 .vidioc_s_input = radio_s_input,
2562 .vidioc_queryctrl = radio_queryctrl,
2563 .vidioc_g_ctrl = vidioc_g_ctrl,
2564 .vidioc_s_ctrl = vidioc_s_ctrl,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002565 .vidioc_g_frequency = vidioc_g_frequency,
2566 .vidioc_s_frequency = vidioc_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002567#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002568 .vidioc_g_register = vidioc_g_register,
2569 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002570#endif
2571};
2572
2573static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002574 .name = "cx231xx-radio",
2575 .fops = &radio_fops,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002576 .ioctl_ops = &radio_ioctl_ops,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002577};
2578
2579/******************************** usb interface ******************************/
2580
Sri Deevib9255172009-03-04 17:49:01 -03002581static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2582 const struct video_device
2583 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002584{
2585 struct video_device *vfd;
2586
2587 vfd = video_device_alloc();
2588 if (NULL == vfd)
2589 return NULL;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002590
Sri Deevie0d3baf2009-03-03 14:37:50 -03002591 *vfd = *template;
Sri Deevib1196122009-03-20 23:33:48 -03002592 vfd->v4l2_dev = &dev->v4l2_dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002593 vfd->release = video_device_release;
2594 vfd->debug = video_debug;
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002595 vfd->lock = &dev->lock;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002596
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002597 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002598
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002599 video_set_drvdata(vfd, dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002600 return vfd;
2601}
2602
2603int cx231xx_register_analog_devices(struct cx231xx *dev)
2604{
2605 int ret;
2606
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002607 cx231xx_info("%s: v4l2 driver version %s\n",
2608 dev->name, CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002609
2610 /* set default norm */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002611 /*dev->norm = cx231xx_video_template.current_norm; */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002612 dev->width = norm_maxw(dev);
2613 dev->height = norm_maxh(dev);
2614 dev->interlaced = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002615
2616 /* Analog specific initialization */
2617 dev->format = &format[0];
Devin Heitmueller6e6a2ba2010-07-12 15:34:57 -03002618
2619 /* Set the initial input */
2620 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002621
2622 /* Audio defaults */
2623 dev->mute = 1;
2624 dev->volume = 0x1f;
2625
2626 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002627 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002628
2629 /* allocate and fill video video_device struct */
2630 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2631 if (!dev->vdev) {
2632 cx231xx_errdev("cannot allocate video_device.\n");
2633 return -ENODEV;
2634 }
2635
2636 /* register v4l2 video video_device */
2637 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002638 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002639 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002640 cx231xx_errdev("unable to register video device (error=%i).\n",
2641 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002642 return ret;
2643 }
2644
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002645 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2646 dev->name, video_device_node_name(dev->vdev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002647
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002648 /* Initialize VBI template */
Ezequiel Garcia3724dde2012-10-23 15:57:05 -03002649 cx231xx_vbi_template = cx231xx_video_template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002650 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002651
2652 /* Allocate and fill vbi video_device struct */
2653 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2654
2655 /* register v4l2 vbi video_device */
2656 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002657 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002658 if (ret < 0) {
2659 cx231xx_errdev("unable to register vbi device\n");
2660 return ret;
2661 }
2662
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002663 cx231xx_info("%s/0: registered device %s\n",
2664 dev->name, video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002665
2666 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002667 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2668 "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002669 if (!dev->radio_dev) {
2670 cx231xx_errdev("cannot allocate video_device.\n");
2671 return -ENODEV;
2672 }
2673 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2674 radio_nr[dev->devno]);
2675 if (ret < 0) {
2676 cx231xx_errdev("can't register radio device\n");
2677 return ret;
2678 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002679 cx231xx_info("Registered radio device as %s\n",
2680 video_device_node_name(dev->radio_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002681 }
2682
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002683 cx231xx_info("V4L2 device registered as %s and %s\n",
2684 video_device_node_name(dev->vdev),
2685 video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002686
2687 return 0;
2688}