blob: 54cdd4dc455e59cff49794785a566338ec52e808 [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>
Hans Verkuil1d08a4f2012-09-17 07:31:04 -030038#include <media/v4l2-event.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030039#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
Sri Deevie0d3baf2009-03-03 14:37:50 -0300104/* ------------------------------------------------------------------
105 Video buffer and parser functions
106 ------------------------------------------------------------------*/
107
108/*
109 * Announces that a buffer were filled and request the next
110 */
111static inline void buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300112 struct cx231xx_dmaqueue *dma_q,
113 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300114{
115 /* Advice that buffer was filled */
116 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
117 buf->vb.state = VIDEOBUF_DONE;
118 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300119 v4l2_get_timestamp(&buf->vb.ts);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300120
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300121 if (dev->USE_ISO)
122 dev->video_mode.isoc_ctl.buf = NULL;
123 else
124 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300125
126 list_del(&buf->vb.queue);
127 wake_up(&buf->vb.done);
128}
129
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300130static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300131{
132 char *errmsg = "Unknown";
133
134 switch (status) {
135 case -ENOENT:
136 errmsg = "unlinked synchronuously";
137 break;
138 case -ECONNRESET:
139 errmsg = "unlinked asynchronuously";
140 break;
141 case -ENOSR:
142 errmsg = "Buffer error (overrun)";
143 break;
144 case -EPIPE:
145 errmsg = "Stalled (device not responding)";
146 break;
147 case -EOVERFLOW:
148 errmsg = "Babble (bad cable?)";
149 break;
150 case -EPROTO:
151 errmsg = "Bit-stuff error (bad cable?)";
152 break;
153 case -EILSEQ:
154 errmsg = "CRC/Timeout (could be anything)";
155 break;
156 case -ETIME:
157 errmsg = "Device does not respond";
158 break;
159 }
160 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300161 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300162 } else {
163 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300164 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300165 }
166}
167
168/*
169 * video-buf generic routine to get the next available buffer
170 */
171static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300172 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300173{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300174 struct cx231xx_video_mode *vmode =
175 container_of(dma_q, struct cx231xx_video_mode, vidq);
176 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300177
178 char *outp;
179
Sri Deevie0d3baf2009-03-03 14:37:50 -0300180 if (list_empty(&dma_q->active)) {
181 cx231xx_isocdbg("No active queue to serve\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300182 if (dev->USE_ISO)
183 dev->video_mode.isoc_ctl.buf = NULL;
184 else
185 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300186 *buf = NULL;
187 return;
188 }
189
190 /* Get the next buffer */
191 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
192
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300193 /* Cleans up buffer - Useful for testing for frame/URB loss */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300194 outp = videobuf_to_vmalloc(&(*buf)->vb);
195 memset(outp, 0, (*buf)->vb.size);
196
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300197 if (dev->USE_ISO)
198 dev->video_mode.isoc_ctl.buf = *buf;
199 else
200 dev->video_mode.bulk_ctl.buf = *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300201
202 return;
203}
204
205/*
206 * Controls the isoc copy of each urb packet
207 */
208static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
209{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300210 struct cx231xx_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300211 int i, rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300212 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300213 u32 bytes_parsed = 0, buffer_size = 0;
214 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300215
216 if (!dev)
217 return 0;
218
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200219 if (dev->state & DEV_DISCONNECTED)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300220 return 0;
221
222 if (urb->status < 0) {
223 print_err_status(dev, -1, urb->status);
224 if (urb->status == -ENOENT)
225 return 0;
226 }
227
Sri Deevie0d3baf2009-03-03 14:37:50 -0300228 for (i = 0; i < urb->number_of_packets; i++) {
229 int status = urb->iso_frame_desc[i].status;
230
231 if (status < 0) {
232 print_err_status(dev, i, status);
233 if (urb->iso_frame_desc[i].status != -EPROTO)
234 continue;
235 }
236
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300237 if (urb->iso_frame_desc[i].actual_length <= 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300238 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
239 continue;
240 }
241 if (urb->iso_frame_desc[i].actual_length >
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300242 dev->video_mode.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300243 cx231xx_isocdbg("packet bigger than packet size");
244 continue;
245 }
246
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300247 /* get buffer pointer and length */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300248 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300249 buffer_size = urb->iso_frame_desc[i].actual_length;
250 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300251
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300252 if (dma_q->is_partial_line) {
Sri Deevib9255172009-03-04 17:49:01 -0300253 /* Handle the case of a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300254 sav_eav = dma_q->last_sav;
255 } else {
Sri Deevib9255172009-03-04 17:49:01 -0300256 /* Check for a SAV/EAV overlapping
257 the buffer boundary */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300258 sav_eav =
259 cx231xx_find_boundary_SAV_EAV(p_buffer,
260 dma_q->partial_buf,
261 &bytes_parsed);
262 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300263
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300264 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300265 /* Get the first line if we have some portion of an SAV/EAV from
266 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300267 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300268 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
Sri Deevib9255172009-03-04 17:49:01 -0300269 sav_eav, /* SAV/EAV */
270 p_buffer + bytes_parsed, /* p_buffer */
271 buffer_size - bytes_parsed);/* buf size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300272 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300273
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300274 /* Now parse data that is completely in this buffer */
275 /* dma_q->is_partial_line = 0; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300276
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300277 while (bytes_parsed < buffer_size) {
278 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300279
Sri Deevib9255172009-03-04 17:49:01 -0300280 sav_eav = cx231xx_find_next_SAV_EAV(
281 p_buffer + bytes_parsed, /* p_buffer */
282 buffer_size - bytes_parsed, /* buf size */
283 &bytes_used);/* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300284
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300285 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300286
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300287 sav_eav &= 0xF0;
288 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300289 bytes_parsed += cx231xx_get_video_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300290 dma_q, sav_eav, /* SAV/EAV */
291 p_buffer + bytes_parsed,/* p_buffer */
292 buffer_size - bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300293 }
294 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300295
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300296 /* Save the last four bytes of the buffer so we can check the
297 buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300298 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
299 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300300
301 }
302 return rc;
303}
304
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300305static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
306{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300307 struct cx231xx_dmaqueue *dma_q = urb->context;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300308 int rc = 1;
309 unsigned char *p_buffer;
310 u32 bytes_parsed = 0, buffer_size = 0;
311 u8 sav_eav = 0;
312
313 if (!dev)
314 return 0;
315
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200316 if (dev->state & DEV_DISCONNECTED)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300317 return 0;
318
319 if (urb->status < 0) {
320 print_err_status(dev, -1, urb->status);
321 if (urb->status == -ENOENT)
322 return 0;
323 }
324
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300325 if (1) {
326
327 /* get buffer pointer and length */
328 p_buffer = urb->transfer_buffer;
329 buffer_size = urb->actual_length;
330 bytes_parsed = 0;
331
332 if (dma_q->is_partial_line) {
333 /* Handle the case of a partial line */
334 sav_eav = dma_q->last_sav;
335 } else {
336 /* Check for a SAV/EAV overlapping
337 the buffer boundary */
338 sav_eav =
339 cx231xx_find_boundary_SAV_EAV(p_buffer,
340 dma_q->partial_buf,
341 &bytes_parsed);
342 }
343
344 sav_eav &= 0xF0;
345 /* Get the first line if we have some portion of an SAV/EAV from
346 the last buffer or a partial line */
347 if (sav_eav) {
348 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
349 sav_eav, /* SAV/EAV */
350 p_buffer + bytes_parsed, /* p_buffer */
351 buffer_size - bytes_parsed);/* buf size */
352 }
353
354 /* Now parse data that is completely in this buffer */
355 /* dma_q->is_partial_line = 0; */
356
357 while (bytes_parsed < buffer_size) {
358 u32 bytes_used = 0;
359
360 sav_eav = cx231xx_find_next_SAV_EAV(
361 p_buffer + bytes_parsed, /* p_buffer */
362 buffer_size - bytes_parsed, /* buf size */
363 &bytes_used);/* bytes used to get SAV/EAV */
364
365 bytes_parsed += bytes_used;
366
367 sav_eav &= 0xF0;
368 if (sav_eav && (bytes_parsed < buffer_size)) {
369 bytes_parsed += cx231xx_get_video_line(dev,
370 dma_q, sav_eav, /* SAV/EAV */
371 p_buffer + bytes_parsed,/* p_buffer */
372 buffer_size - bytes_parsed);/*buf size*/
373 }
374 }
375
376 /* Save the last four bytes of the buffer so we can check the
377 buffer boundary condition next time */
378 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
379 bytes_parsed = 0;
380
381 }
382 return rc;
383}
384
385
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300386u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
387 u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300388{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300389 u32 bytes_used;
390 u8 boundary_bytes[8];
391 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300392
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300393 *p_bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300394
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300395 /* Create an array of the last 4 bytes of the last buffer and the first
396 4 bytes of the current buffer. */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300397
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300398 memcpy(boundary_bytes, partial_buf, 4);
399 memcpy(boundary_bytes + 4, p_buffer, 4);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300400
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300401 /* Check for the SAV/EAV in the boundary buffer */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300402 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
403 &bytes_used);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300404
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300405 if (sav_eav) {
406 /* found a boundary SAV/EAV. Updates the bytes used to reflect
407 only those used in the new buffer */
408 *p_bytes_used = bytes_used - 4;
409 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300410
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300411 return sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300412}
413
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300414u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300415{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300416 u32 i;
417 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300418
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300419 /*
420 * Don't search if the buffer size is less than 4. It causes a page
421 * fault since buffer_size - 4 evaluates to a large number in that
422 * case.
423 */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300424 if (buffer_size < 4) {
425 *p_bytes_used = buffer_size;
426 return 0;
427 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300428
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300429 for (i = 0; i < (buffer_size - 3); i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300430
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300431 if ((p_buffer[i] == 0xFF) &&
432 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300433
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300434 *p_bytes_used = i + 4;
435 sav_eav = p_buffer[i + 3];
436 return sav_eav;
437 }
438 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300439
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300440 *p_bytes_used = buffer_size;
441 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300442}
443
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300444u32 cx231xx_get_video_line(struct cx231xx *dev,
445 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
446 u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300447{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300448 u32 bytes_copied = 0;
449 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300450
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300451 switch (sav_eav) {
452 case SAV_ACTIVE_VIDEO_FIELD1:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300453 /* looking for skipped line which occurred in PAL 720x480 mode.
454 In this case, there will be no active data contained
455 between the SAV and EAV */
456 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
457 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
458 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
459 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
460 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
461 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300462 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300463 current_field = 1;
464 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300465
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300466 case SAV_ACTIVE_VIDEO_FIELD2:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300467 /* looking for skipped line which occurred in PAL 720x480 mode.
468 In this case, there will be no active data contained between
469 the SAV and EAV */
470 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
471 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
472 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
473 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
474 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
475 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300476 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300477 current_field = 2;
478 break;
479 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300480
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300481 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300482
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300483 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
484 buffer_size, current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300485
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300486 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300487}
488
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300489u32 cx231xx_copy_video_line(struct cx231xx *dev,
490 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300491 u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300492{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300493 u32 bytes_to_copy;
494 struct cx231xx_buffer *buf;
495 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300496
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300497 if (dma_q->current_field != field_number)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300498 cx231xx_reset_video_buffer(dev, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300499
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300500 /* get the buffer pointer */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300501 if (dev->USE_ISO)
502 buf = dev->video_mode.isoc_ctl.buf;
503 else
504 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300505
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300506 /* Remember the field number for next time */
507 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300508
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300509 bytes_to_copy = dma_q->bytes_left_in_line;
510 if (bytes_to_copy > length)
511 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300512
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300513 if (dma_q->lines_completed >= dma_q->lines_per_field) {
514 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300515 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
516 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300517 return 0;
518 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300520 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300521
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300522 /* If we don't have a buffer, just return the number of bytes we would
523 have copied if we had a buffer. */
524 if (!buf) {
525 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300526 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
527 ? 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300528 return bytes_to_copy;
529 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300530
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300531 /* copy the data to video buffer */
532 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300533
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300534 dma_q->pos += bytes_to_copy;
535 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300536
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300537 if (dma_q->bytes_left_in_line == 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300538 dma_q->bytes_left_in_line = _line_size;
539 dma_q->lines_completed++;
540 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300541
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300542 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300543 buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300544
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300545 dma_q->pos = 0;
546 buf = NULL;
547 dma_q->lines_completed = 0;
548 }
549 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300550
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300551 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300552}
553
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300554void cx231xx_reset_video_buffer(struct cx231xx *dev,
555 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300556{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300557 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300558
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300559 /* handle the switch from field 1 to field 2 */
560 if (dma_q->current_field == 1) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300561 if (dma_q->lines_completed >= dma_q->lines_per_field)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300562 dma_q->field1_done = 1;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300563 else
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300564 dma_q->field1_done = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300565 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300566
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300567 if (dev->USE_ISO)
568 buf = dev->video_mode.isoc_ctl.buf;
569 else
570 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300571
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300572 if (buf == NULL) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300573 /* first try to get the buffer */
574 get_next_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300575
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300576 dma_q->pos = 0;
577 dma_q->field1_done = 0;
578 dma_q->current_field = -1;
579 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300580
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300581 /* reset the counters */
582 dma_q->bytes_left_in_line = dev->width << 1;
583 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300584}
585
586int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300587 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300588{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300589 u8 *p_out_buffer = NULL;
590 u32 current_line_bytes_copied = 0;
591 struct cx231xx_buffer *buf;
592 u32 _line_size = dev->width << 1;
593 void *startwrite;
594 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300595
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300596 if (dev->USE_ISO)
597 buf = dev->video_mode.isoc_ctl.buf;
598 else
599 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300600
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 if (buf == NULL)
602 return -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300603
604 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
605
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300606 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300607
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300608 /* Offset field 2 one line from the top of the buffer */
609 offset = (dma_q->current_field == 1) ? 0 : _line_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300610
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300611 /* Offset for field 2 */
612 startwrite = p_out_buffer + offset;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300613
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300614 /* lines already completed in the current field */
615 startwrite += (dma_q->lines_completed * _line_size * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300616
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300617 /* bytes already completed in the current line */
618 startwrite += current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300619
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300620 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
621 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300622
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300623 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300624 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300625
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300626 /* The below copies the UYVY data straight into video buffer */
627 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300628
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300629 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300630}
631
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300632void cx231xx_swab(u16 *from, u16 *to, u16 len)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300633{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300634 u16 i;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300635
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300636 if (len <= 0)
637 return;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300638
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300639 for (i = 0; i < len / 2; i++)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300640 to[i] = (from[i] << 8) | (from[i] >> 8);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300641}
642
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300643u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300644{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300645 u8 buffer_complete = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300646
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300647 /* Dual field stream */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300648 buffer_complete = ((dma_q->current_field == 2) &&
649 (dma_q->lines_completed >= dma_q->lines_per_field) &&
650 dma_q->field1_done);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300651
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300652 return buffer_complete;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300653}
654
Sri Deevie0d3baf2009-03-03 14:37:50 -0300655/* ------------------------------------------------------------------
656 Videobuf operations
657 ------------------------------------------------------------------*/
658
659static int
660buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
661{
662 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300663 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300664
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300665 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300666 if (0 == *count)
667 *count = CX231XX_DEF_BUF;
668
669 if (*count < CX231XX_MIN_BUF)
670 *count = CX231XX_MIN_BUF;
671
Sri Deevie0d3baf2009-03-03 14:37:50 -0300672 return 0;
673}
674
675/* This is called *without* dev->slock held; please keep it that way */
676static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
677{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300678 struct cx231xx_fh *fh = vq->priv_data;
679 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300680 unsigned long flags = 0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300681
Sri Deevie0d3baf2009-03-03 14:37:50 -0300682 if (in_interrupt())
683 BUG();
684
685 /* We used to wait for the buffer to finish here, but this didn't work
686 because, as we were keeping the state as VIDEOBUF_QUEUED,
687 videobuf_queue_cancel marked it as finished for us.
688 (Also, it could wedge forever if the hardware was misconfigured.)
689
690 This should be safe; by the time we get here, the buffer isn't
691 queued anymore. If we ever start marking the buffers as
692 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300693 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300694 spin_lock_irqsave(&dev->video_mode.slock, flags);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300695 if (dev->USE_ISO) {
696 if (dev->video_mode.isoc_ctl.buf == buf)
697 dev->video_mode.isoc_ctl.buf = NULL;
698 } else {
699 if (dev->video_mode.bulk_ctl.buf == buf)
700 dev->video_mode.bulk_ctl.buf = NULL;
701 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300702 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
703
704 videobuf_vmalloc_free(&buf->vb);
705 buf->vb.state = VIDEOBUF_NEEDS_INIT;
706}
707
708static int
709buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300710 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300711{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300712 struct cx231xx_fh *fh = vq->priv_data;
713 struct cx231xx_buffer *buf =
714 container_of(vb, struct cx231xx_buffer, vb);
715 struct cx231xx *dev = fh->dev;
716 int rc = 0, urb_init = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300717
718 /* The only currently supported format is 16 bits/pixel */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300719 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
720 + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300721 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300722 return -EINVAL;
723
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300724 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300725 buf->vb.height = dev->height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300726 buf->vb.field = field;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300727
728 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
729 rc = videobuf_iolock(vq, &buf->vb, NULL);
730 if (rc < 0)
731 goto fail;
732 }
733
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300734 if (dev->USE_ISO) {
735 if (!dev->video_mode.isoc_ctl.num_bufs)
736 urb_init = 1;
737 } else {
738 if (!dev->video_mode.bulk_ctl.num_bufs)
739 urb_init = 1;
740 }
741 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
742 urb_init, dev->video_mode.max_pkt_size);*/
Sri Deevie0d3baf2009-03-03 14:37:50 -0300743 if (urb_init) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300744 dev->mode_tv = 0;
745 if (dev->USE_ISO)
746 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300747 CX231XX_NUM_BUFS,
748 dev->video_mode.max_pkt_size,
749 cx231xx_isoc_copy);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300750 else
751 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
752 CX231XX_NUM_BUFS,
753 dev->video_mode.max_pkt_size,
754 cx231xx_bulk_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300755 if (rc < 0)
756 goto fail;
757 }
758
759 buf->vb.state = VIDEOBUF_PREPARED;
760 return 0;
761
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300762fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300763 free_buffer(vq, buf);
764 return rc;
765}
766
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300767static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300768{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300769 struct cx231xx_buffer *buf =
770 container_of(vb, struct cx231xx_buffer, vb);
771 struct cx231xx_fh *fh = vq->priv_data;
772 struct cx231xx *dev = fh->dev;
773 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300774
775 buf->vb.state = VIDEOBUF_QUEUED;
776 list_add_tail(&buf->vb.queue, &vidq->active);
777
778}
779
780static void buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300781 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300782{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300783 struct cx231xx_buffer *buf =
784 container_of(vb, struct cx231xx_buffer, vb);
785 struct cx231xx_fh *fh = vq->priv_data;
786 struct cx231xx *dev = (struct cx231xx *)fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300787
788 cx231xx_isocdbg("cx231xx: called buffer_release\n");
789
790 free_buffer(vq, buf);
791}
792
793static struct videobuf_queue_ops cx231xx_video_qops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300794 .buf_setup = buffer_setup,
795 .buf_prepare = buffer_prepare,
796 .buf_queue = buffer_queue,
797 .buf_release = buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300798};
799
800/********************* v4l2 interface **************************************/
801
Sri Deevie0d3baf2009-03-03 14:37:50 -0300802void video_mux(struct cx231xx *dev, int index)
803{
Sri Deevie0d3baf2009-03-03 14:37:50 -0300804 dev->video_input = index;
805 dev->ctl_ainput = INPUT(index)->amux;
806
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300807 cx231xx_set_video_input_mux(dev, index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300808
Hans Verkuil5325b422009-04-02 11:26:22 -0300809 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300810
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300811 cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300812
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300813 cx231xx_info("video_mux : %d\n", index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300814
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300815 /* do mode control overrides if required */
816 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300817}
818
819/* Usage lock check functions */
820static int res_get(struct cx231xx_fh *fh)
821{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300822 struct cx231xx *dev = fh->dev;
823 int rc = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300824
825 /* This instance already has stream_on */
826 if (fh->stream_on)
827 return rc;
828
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300829 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
830 if (dev->stream_on)
831 return -EBUSY;
832 dev->stream_on = 1;
833 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
834 if (dev->vbi_stream_on)
835 return -EBUSY;
836 dev->vbi_stream_on = 1;
837 } else
838 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300839
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300840 fh->stream_on = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300841
842 return rc;
843}
844
845static int res_check(struct cx231xx_fh *fh)
846{
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300847 return fh->stream_on;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300848}
849
850static void res_free(struct cx231xx_fh *fh)
851{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300852 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300853
854 fh->stream_on = 0;
855
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300856 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
857 dev->stream_on = 0;
858 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
859 dev->vbi_stream_on = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300860}
861
862static int check_dev(struct cx231xx *dev)
863{
864 if (dev->state & DEV_DISCONNECTED) {
865 cx231xx_errdev("v4l2 ioctl: device not present\n");
866 return -ENODEV;
867 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300868 return 0;
869}
870
Sri Deevie0d3baf2009-03-03 14:37:50 -0300871/* ------------------------------------------------------------------
872 IOCTL vidioc handling
873 ------------------------------------------------------------------*/
874
875static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300876 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300877{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300878 struct cx231xx_fh *fh = priv;
879 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300880
Sri Deevie0d3baf2009-03-03 14:37:50 -0300881 f->fmt.pix.width = dev->width;
882 f->fmt.pix.height = dev->height;
Joe Perches1ebcad72009-07-02 15:57:09 -0300883 f->fmt.pix.pixelformat = dev->format->fourcc;
884 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300885 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300886 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
887
888 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300889 f->fmt.pix.priv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300890
Sri Deevie0d3baf2009-03-03 14:37:50 -0300891 return 0;
892}
893
894static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
895{
896 unsigned int i;
897
898 for (i = 0; i < ARRAY_SIZE(format); i++)
899 if (format[i].fourcc == fourcc)
900 return &format[i];
901
902 return NULL;
903}
904
905static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300906 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300907{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300908 struct cx231xx_fh *fh = priv;
909 struct cx231xx *dev = fh->dev;
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300910 unsigned int width = f->fmt.pix.width;
911 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300912 unsigned int maxw = norm_maxw(dev);
913 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300914 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300915
916 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
917 if (!fmt) {
918 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300919 f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300920 return -EINVAL;
921 }
922
923 /* width must even because of the YUYV format
924 height must be even because of interlacing */
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300925 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300926
Sri Deevie0d3baf2009-03-03 14:37:50 -0300927 f->fmt.pix.width = width;
928 f->fmt.pix.height = height;
929 f->fmt.pix.pixelformat = fmt->fourcc;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300930 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300931 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
932 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
933 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300934 f->fmt.pix.priv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300935
936 return 0;
937}
938
939static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300940 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300941{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300942 struct cx231xx_fh *fh = priv;
943 struct cx231xx *dev = fh->dev;
944 int rc;
945 struct cx231xx_fmt *fmt;
Hans Verkuil112cb4a2010-05-09 10:11:01 -0300946 struct v4l2_mbus_framefmt mbus_fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300947
948 rc = check_dev(dev);
949 if (rc < 0)
950 return rc;
951
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300952 vidioc_try_fmt_vid_cap(file, priv, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300953
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300954 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300955 if (!fmt)
956 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300957
958 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
959 cx231xx_errdev("%s queue busy\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300960 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300961 }
962
963 if (dev->stream_on && !fh->stream_on) {
964 cx231xx_errdev("%s device in use by another fh\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300965 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300966 }
967
968 /* set new image size */
969 dev->width = f->fmt.pix.width;
970 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300971 dev->format = fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300972
Hans Verkuil112cb4a2010-05-09 10:11:01 -0300973 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
974 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
975 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300976
Sri Deevie0d3baf2009-03-03 14:37:50 -0300977 return rc;
978}
979
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300980static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300981{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300982 struct cx231xx_fh *fh = priv;
983 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300984
985 *id = dev->norm;
986 return 0;
987}
988
Hans Verkuil314527a2013-03-15 06:10:40 -0300989static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300990{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300991 struct cx231xx_fh *fh = priv;
992 struct cx231xx *dev = fh->dev;
Devin Heitmueller435b4f72010-07-09 13:29:31 -0300993 struct v4l2_mbus_framefmt mbus_fmt;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300994 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300995
996 rc = check_dev(dev);
997 if (rc < 0)
998 return rc;
999
Hans Verkuil314527a2013-03-15 06:10:40 -03001000 if (dev->norm == norm)
Hans Verkuild61072a2013-01-29 10:50:54 -03001001 return 0;
1002
1003 if (videobuf_queue_is_busy(&fh->vb_vidq))
1004 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001005
Hans Verkuil314527a2013-03-15 06:10:40 -03001006 dev->norm = norm;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001007
Sri Deevie0d3baf2009-03-03 14:37:50 -03001008 /* Adjusts width/height, if needed */
Hans Verkuild61072a2013-01-29 10:50:54 -03001009 dev->width = 720;
1010 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001011
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001012 call_all(dev, core, s_std, dev->norm);
1013
1014 /* We need to reset basic properties in the decoder related to
1015 resolution (since a standard change effects things like the number
1016 of lines in VACT, etc) */
Hans Verkuild61072a2013-01-29 10:50:54 -03001017 memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1018 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1019 mbus_fmt.width = dev->width;
1020 mbus_fmt.height = dev->height;
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001021 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001022
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001023 /* do mode control overrides */
1024 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001025
1026 return 0;
1027}
1028
1029static const char *iname[] = {
1030 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001031 [CX231XX_VMUX_SVIDEO] = "S-Video",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001032 [CX231XX_VMUX_TELEVISION] = "Television",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001033 [CX231XX_VMUX_CABLE] = "Cable TV",
1034 [CX231XX_VMUX_DVB] = "DVB",
1035 [CX231XX_VMUX_DEBUG] = "for debug only",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001036};
1037
Hans Verkuilb86d1542013-01-29 13:16:06 -03001038int cx231xx_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001039 struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001040{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001041 struct cx231xx_fh *fh = priv;
1042 struct cx231xx *dev = fh->dev;
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001043 u32 gen_stat;
1044 unsigned int ret, n;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001045
1046 n = i->index;
1047 if (n >= MAX_CX231XX_INPUT)
1048 return -EINVAL;
1049 if (0 == INPUT(n)->type)
1050 return -EINVAL;
1051
1052 i->index = n;
1053 i->type = V4L2_INPUT_TYPE_CAMERA;
1054
1055 strcpy(i->name, iname[INPUT(n)->type]);
1056
1057 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001058 (CX231XX_VMUX_CABLE == INPUT(n)->type))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001059 i->type = V4L2_INPUT_TYPE_TUNER;
1060
1061 i->std = dev->vdev->tvnorms;
1062
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001063 /* If they are asking about the active input, read signal status */
1064 if (n == dev->video_input) {
1065 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1066 GEN_STAT, 2, &gen_stat, 4);
1067 if (ret > 0) {
1068 if ((gen_stat & FLD_VPRES) == 0x00)
1069 i->status |= V4L2_IN_ST_NO_SIGNAL;
1070 if ((gen_stat & FLD_HLOCK) == 0x00)
1071 i->status |= V4L2_IN_ST_NO_H_LOCK;
1072 }
1073 }
1074
Sri Deevie0d3baf2009-03-03 14:37:50 -03001075 return 0;
1076}
1077
Hans Verkuilb86d1542013-01-29 13:16:06 -03001078int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001079{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001080 struct cx231xx_fh *fh = priv;
1081 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001082
1083 *i = dev->video_input;
1084
1085 return 0;
1086}
1087
Hans Verkuilb86d1542013-01-29 13:16:06 -03001088int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001089{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001090 struct cx231xx_fh *fh = priv;
1091 struct cx231xx *dev = fh->dev;
1092 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001093
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001094 dev->mode_tv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001095 rc = check_dev(dev);
1096 if (rc < 0)
1097 return rc;
1098
1099 if (i >= MAX_CX231XX_INPUT)
1100 return -EINVAL;
1101 if (0 == INPUT(i)->type)
1102 return -EINVAL;
1103
Sri Deevie0d3baf2009-03-03 14:37:50 -03001104 video_mux(dev, i);
1105
Devin Heitmuellerc09d6692010-07-12 16:50:30 -03001106 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1107 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1108 /* There's a tuner, so reset the standard and put it on the
1109 last known frequency (since it was probably powered down
1110 until now */
1111 call_all(dev, core, s_std, dev->norm);
1112 }
1113
Sri Deevie0d3baf2009-03-03 14:37:50 -03001114 return 0;
1115}
1116
Hans Verkuilb86d1542013-01-29 13:16:06 -03001117int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001118{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001119 struct cx231xx_fh *fh = priv;
1120 struct cx231xx *dev = fh->dev;
1121 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001122
1123 rc = check_dev(dev);
1124 if (rc < 0)
1125 return rc;
1126
1127 if (0 != t->index)
1128 return -EINVAL;
1129
1130 strcpy(t->name, "Tuner");
1131
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001132 t->type = V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001133 t->capability = V4L2_TUNER_CAP_NORM;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001134 t->rangehigh = 0xffffffffUL;
1135 t->signal = 0xffff; /* LOCKED */
Hans Verkuilb251f952012-09-13 12:54:36 -03001136 call_all(dev, tuner, g_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001137
1138 return 0;
1139}
1140
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001141int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001142{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001143 struct cx231xx_fh *fh = priv;
1144 struct cx231xx *dev = fh->dev;
1145 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001146
1147 rc = check_dev(dev);
1148 if (rc < 0)
1149 return rc;
1150
1151 if (0 != t->index)
1152 return -EINVAL;
1153#if 0
Sri Deevib1196122009-03-20 23:33:48 -03001154 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001155#endif
1156 return 0;
1157}
1158
Hans Verkuilb86d1542013-01-29 13:16:06 -03001159int cx231xx_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001160 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001161{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001162 struct cx231xx_fh *fh = priv;
1163 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001164
Hans Verkuilb251f952012-09-13 12:54:36 -03001165 if (f->tuner)
1166 return -EINVAL;
1167
Sri Deevie0d3baf2009-03-03 14:37:50 -03001168 f->frequency = dev->ctl_freq;
1169
Sri Deevie0d3baf2009-03-03 14:37:50 -03001170 return 0;
1171}
1172
Hans Verkuilb86d1542013-01-29 13:16:06 -03001173int cx231xx_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001174 const struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001175{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001176 struct cx231xx_fh *fh = priv;
1177 struct cx231xx *dev = fh->dev;
Hans Verkuilb530a442013-03-19 04:09:26 -03001178 struct v4l2_frequency new_freq = *f;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001179 int rc;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001180 u32 if_frequency = 5400000;
1181
1182 cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1183 f->frequency, f->type);
1184 /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
Sri Deevie0d3baf2009-03-03 14:37:50 -03001185
1186 rc = check_dev(dev);
1187 if (rc < 0)
1188 return rc;
1189
1190 if (0 != f->tuner)
1191 return -EINVAL;
1192
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001193 /* set pre channel change settings in DIF first */
1194 rc = cx231xx_tuner_pre_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001195
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001196 call_all(dev, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -03001197 call_all(dev, tuner, g_frequency, &new_freq);
1198 dev->ctl_freq = new_freq.frequency;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001199
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001200 /* set post channel change settings in DIF first */
1201 rc = cx231xx_tuner_post_channel_change(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001202
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001203 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1204 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1205 if_frequency = 5400000; /*5.4MHz */
1206 else if (dev->norm & V4L2_STD_B)
1207 if_frequency = 6000000; /*6.0MHz */
1208 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1209 if_frequency = 6900000; /*6.9MHz */
1210 else if (dev->norm & V4L2_STD_GH)
1211 if_frequency = 7100000; /*7.1MHz */
1212 else if (dev->norm & V4L2_STD_PAL_I)
1213 if_frequency = 7250000; /*7.25MHz */
1214 else if (dev->norm & V4L2_STD_SECAM_L)
1215 if_frequency = 6900000; /*6.9MHz */
1216 else if (dev->norm & V4L2_STD_SECAM_LC)
1217 if_frequency = 1250000; /*1.25MHz */
1218
1219 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1220 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1221
1222 update_HH_register_after_set_DIF(dev);
1223 }
1224
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001225 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001226
1227 return rc;
1228}
1229
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001230#ifdef CONFIG_VIDEO_ADV_DEBUG
1231
1232int cx231xx_g_chip_info(struct file *file, void *fh,
1233 struct v4l2_dbg_chip_info *chip)
Hans Verkuilfddd14c2012-09-13 05:37:11 -03001234{
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001235 switch (chip->match.addr) {
1236 case 0: /* Cx231xx - internal registers */
1237 return 0;
1238 case 1: /* AFE - read byte */
1239 strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
1240 return 0;
1241 case 2: /* Video Block - read byte */
1242 strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
1243 return 0;
1244 case 3: /* I2S block - read byte */
1245 strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
1246 return 0;
1247 case 4: /* AFE - read dword */
1248 strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
1249 return 0;
1250 case 5: /* Video Block - read dword */
1251 strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
1252 return 0;
1253 case 6: /* I2S Block - read dword */
1254 strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
Hans Verkuilfddd14c2012-09-13 05:37:11 -03001255 return 0;
1256 }
1257 return -EINVAL;
1258}
1259
Hans Verkuilb86d1542013-01-29 13:16:06 -03001260int cx231xx_g_register(struct file *file, void *priv,
Sri Deevie0d3baf2009-03-03 14:37:50 -03001261 struct v4l2_dbg_register *reg)
1262{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001263 struct cx231xx_fh *fh = priv;
1264 struct cx231xx *dev = fh->dev;
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001265 int ret;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001266 u8 value[4] = { 0, 0, 0, 0 };
1267 u32 data = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001268
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001269 switch (reg->match.addr) {
1270 case 0: /* Cx231xx - internal registers */
1271 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1272 (u16)reg->reg, value, 4);
1273 reg->val = value[0] | value[1] << 8 |
1274 value[2] << 16 | value[3] << 24;
1275 break;
1276 case 1: /* AFE - read byte */
1277 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1278 (u16)reg->reg, 2, &data, 1);
1279 reg->val = data;
1280 break;
1281 case 2: /* Video Block - read byte */
1282 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1283 (u16)reg->reg, 2, &data, 1);
1284 reg->val = data;
1285 break;
1286 case 3: /* I2S block - read byte */
1287 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1288 (u16)reg->reg, 1, &data, 1);
1289 reg->val = data;
1290 break;
1291 case 4: /* AFE - read dword */
1292 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1293 (u16)reg->reg, 2, &data, 4);
1294 reg->val = data;
1295 break;
1296 case 5: /* Video Block - read dword */
1297 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1298 (u16)reg->reg, 2, &data, 4);
1299 reg->val = data;
1300 break;
1301 case 6: /* I2S Block - read dword */
1302 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1303 (u16)reg->reg, 1, &data, 4);
1304 reg->val = data;
1305 break;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001306 default:
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001307 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001308 }
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001309 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001310}
1311
Hans Verkuilb86d1542013-01-29 13:16:06 -03001312int cx231xx_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001313 const struct v4l2_dbg_register *reg)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001314{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001315 struct cx231xx_fh *fh = priv;
1316 struct cx231xx *dev = fh->dev;
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001317 int ret;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001318 u8 data[4] = { 0, 0, 0, 0 };
Sri Deevie0d3baf2009-03-03 14:37:50 -03001319
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001320 switch (reg->match.addr) {
1321 case 0: /* cx231xx internal registers */
1322 data[0] = (u8) reg->val;
1323 data[1] = (u8) (reg->val >> 8);
1324 data[2] = (u8) (reg->val >> 16);
1325 data[3] = (u8) (reg->val >> 24);
1326 ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1327 (u16)reg->reg, data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001328 break;
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001329 case 1: /* AFE - write byte */
1330 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1331 (u16)reg->reg, 2, reg->val, 1);
1332 break;
1333 case 2: /* Video Block - write byte */
1334 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1335 (u16)reg->reg, 2, reg->val, 1);
1336 break;
1337 case 3: /* I2S block - write byte */
1338 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1339 (u16)reg->reg, 1, reg->val, 1);
1340 break;
1341 case 4: /* AFE - write dword */
1342 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1343 (u16)reg->reg, 2, reg->val, 4);
1344 break;
1345 case 5: /* Video Block - write dword */
1346 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1347 (u16)reg->reg, 2, reg->val, 4);
1348 break;
1349 case 6: /* I2S block - write dword */
1350 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1351 (u16)reg->reg, 1, reg->val, 4);
1352 break;
1353 default:
1354 return -EINVAL;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001355 }
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001356 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001357}
1358#endif
1359
Sri Deevie0d3baf2009-03-03 14:37:50 -03001360static int vidioc_cropcap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001361 struct v4l2_cropcap *cc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001362{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001363 struct cx231xx_fh *fh = priv;
1364 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001365
1366 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1367 return -EINVAL;
1368
1369 cc->bounds.left = 0;
1370 cc->bounds.top = 0;
1371 cc->bounds.width = dev->width;
1372 cc->bounds.height = dev->height;
1373 cc->defrect = cc->bounds;
1374 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1375 cc->pixelaspect.denominator = 59;
1376
1377 return 0;
1378}
1379
1380static int vidioc_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001381 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001382{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001383 struct cx231xx_fh *fh = priv;
1384 struct cx231xx *dev = fh->dev;
1385 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001386
1387 rc = check_dev(dev);
1388 if (rc < 0)
1389 return rc;
1390
Sri Deevie0d3baf2009-03-03 14:37:50 -03001391 rc = res_get(fh);
1392
1393 if (likely(rc >= 0))
1394 rc = videobuf_streamon(&fh->vb_vidq);
1395
Sri Deevib1196122009-03-20 23:33:48 -03001396 call_all(dev, video, s_stream, 1);
1397
Sri Deevie0d3baf2009-03-03 14:37:50 -03001398 return rc;
1399}
1400
1401static int vidioc_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001402 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001403{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001404 struct cx231xx_fh *fh = priv;
1405 struct cx231xx *dev = fh->dev;
1406 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001407
1408 rc = check_dev(dev);
1409 if (rc < 0)
1410 return rc;
1411
Sri Deevie0d3baf2009-03-03 14:37:50 -03001412 if (type != fh->type)
1413 return -EINVAL;
1414
Sri Deevib1196122009-03-20 23:33:48 -03001415 cx25840_call(dev, video, s_stream, 0);
1416
Sri Deevie0d3baf2009-03-03 14:37:50 -03001417 videobuf_streamoff(&fh->vb_vidq);
1418 res_free(fh);
1419
Sri Deevie0d3baf2009-03-03 14:37:50 -03001420 return 0;
1421}
1422
Hans Verkuilbc087342013-01-29 12:52:33 -03001423int cx231xx_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001424 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001425{
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001426 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001427 struct cx231xx_fh *fh = priv;
1428 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001429
1430 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1431 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab2c6beca2009-03-22 08:53:36 -03001432 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001433
Hans Verkuil530e01e2013-01-29 12:32:20 -03001434 if (vdev->vfl_type == VFL_TYPE_RADIO)
1435 cap->device_caps = V4L2_CAP_RADIO;
1436 else {
Hans Verkuil06c46002012-09-13 06:17:47 -03001437 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Hans Verkuil530e01e2013-01-29 12:32:20 -03001438 if (vdev->vfl_type == VFL_TYPE_VBI)
1439 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1440 else
1441 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1442 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001443 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001444 cap->device_caps |= V4L2_CAP_TUNER;
Hans Verkuil06c46002012-09-13 06:17:47 -03001445 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001446 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
Hans Verkuil530e01e2013-01-29 12:32:20 -03001447 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1448 if (dev->radio_dev)
1449 cap->capabilities |= V4L2_CAP_RADIO;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001450
1451 return 0;
1452}
1453
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001454static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1455 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001456{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001457 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001458 return -EINVAL;
1459
1460 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1461 f->pixelformat = format[f->index].fourcc;
1462
1463 return 0;
1464}
1465
Sri Deevie0d3baf2009-03-03 14:37:50 -03001466/* RAW VBI ioctls */
1467
1468static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001469 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001470{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001471 struct cx231xx_fh *fh = priv;
1472 struct cx231xx *dev = fh->dev;
Hans Verkuil62647222013-02-09 06:41:11 -03001473
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001474 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001475 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1476 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001477 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001478 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001479 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001480 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001481 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001482 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001483 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001484 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001485 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001486
1487 return 0;
1488
1489}
1490
1491static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001492 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001493{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001494 struct cx231xx_fh *fh = priv;
1495 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001496
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001497 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001498 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1499 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001500 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001501 f->fmt.vbi.flags = 0;
1502 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001503 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001504 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001505 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001506 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001507 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001508 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001509 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001510
1511 return 0;
1512
1513}
1514
Hans Verkuil62647222013-02-09 06:41:11 -03001515static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1516 struct v4l2_format *f)
1517{
1518 struct cx231xx_fh *fh = priv;
1519 struct cx231xx *dev = fh->dev;
1520
1521 if (dev->vbi_stream_on && !fh->stream_on) {
1522 cx231xx_errdev("%s device in use by another fh\n", __func__);
1523 return -EBUSY;
1524 }
1525 return vidioc_try_fmt_vbi_cap(file, priv, f);
1526}
1527
Sri Deevie0d3baf2009-03-03 14:37:50 -03001528static int vidioc_reqbufs(struct file *file, void *priv,
1529 struct v4l2_requestbuffers *rb)
1530{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001531 struct cx231xx_fh *fh = priv;
1532 struct cx231xx *dev = fh->dev;
1533 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001534
1535 rc = check_dev(dev);
1536 if (rc < 0)
1537 return rc;
1538
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001539 return videobuf_reqbufs(&fh->vb_vidq, rb);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001540}
1541
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001542static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001543{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001544 struct cx231xx_fh *fh = priv;
1545 struct cx231xx *dev = fh->dev;
1546 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001547
1548 rc = check_dev(dev);
1549 if (rc < 0)
1550 return rc;
1551
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001552 return videobuf_querybuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001553}
1554
1555static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1556{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001557 struct cx231xx_fh *fh = priv;
1558 struct cx231xx *dev = fh->dev;
1559 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001560
1561 rc = check_dev(dev);
1562 if (rc < 0)
1563 return rc;
1564
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001565 return videobuf_qbuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001566}
1567
1568static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1569{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001570 struct cx231xx_fh *fh = priv;
1571 struct cx231xx *dev = fh->dev;
1572 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001573
1574 rc = check_dev(dev);
1575 if (rc < 0)
1576 return rc;
1577
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001578 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001579}
1580
Sri Deevie0d3baf2009-03-03 14:37:50 -03001581/* ----------------------------------------------------------- */
1582/* RADIO ESPECIFIC IOCTLS */
1583/* ----------------------------------------------------------- */
1584
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001585static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001586{
1587 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1588
Hans Verkuil530e01e2013-01-29 12:32:20 -03001589 if (t->index)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001590 return -EINVAL;
1591
1592 strcpy(t->name, "Radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001593
Hans Verkuil530e01e2013-01-29 12:32:20 -03001594 call_all(dev, tuner, g_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001595
1596 return 0;
1597}
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001598static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001599{
1600 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1601
Hans Verkuilb86d1542013-01-29 13:16:06 -03001602 if (t->index)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001603 return -EINVAL;
1604
Sri Deevib1196122009-03-20 23:33:48 -03001605 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001606
1607 return 0;
1608}
1609
Sri Deevie0d3baf2009-03-03 14:37:50 -03001610/*
1611 * cx231xx_v4l2_open()
1612 * inits the device and starts isoc transfer
1613 */
1614static int cx231xx_v4l2_open(struct file *filp)
1615{
Sri Deevie0d3baf2009-03-03 14:37:50 -03001616 int errCode = 0, radio = 0;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001617 struct video_device *vdev = video_devdata(filp);
1618 struct cx231xx *dev = video_drvdata(filp);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001619 struct cx231xx_fh *fh;
1620 enum v4l2_buf_type fh_type = 0;
1621
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001622 switch (vdev->vfl_type) {
1623 case VFL_TYPE_GRABBER:
1624 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1625 break;
1626 case VFL_TYPE_VBI:
1627 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1628 break;
1629 case VFL_TYPE_RADIO:
1630 radio = 1;
1631 break;
1632 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001633
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001634 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1635 video_device_node_name(vdev), v4l2_type_names[fh_type],
1636 dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001637
1638#if 0
1639 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1640 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001641 cx231xx_errdev
1642 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001643 return -EBUSY;
1644 }
1645#endif
1646
1647 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1648 if (!fh) {
1649 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001650 return -ENOMEM;
1651 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001652 if (mutex_lock_interruptible(&dev->lock)) {
1653 kfree(fh);
1654 return -ERESTARTSYS;
1655 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001656 fh->dev = dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001657 fh->type = fh_type;
1658 filp->private_data = fh;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001659 v4l2_fh_init(&fh->fh, vdev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001660
1661 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001662 /* Power up in Analog TV mode */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001663 if (dev->board.external_av)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001664 cx231xx_set_power_mode(dev,
1665 POLARIS_AVMODE_ENXTERNAL_AV);
1666 else
1667 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001668
1669#if 0
1670 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1671#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -03001672
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001673 /* set video alternate setting */
1674 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001675
1676 /* Needed, since GPIO might have disabled power of
1677 some i2c device */
1678 cx231xx_config_i2c(dev);
1679
1680 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001681 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001682
1683 }
Hans Verkuil71590762013-01-28 12:57:47 -03001684 if (radio) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001685 cx231xx_videodbg("video_open: setting radio device\n");
1686
1687 /* cx231xx_start_radio(dev); */
1688
Sri Deevib1196122009-03-20 23:33:48 -03001689 call_all(dev, tuner, s_radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001690 }
1691
1692 dev->users++;
1693
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001694 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1695 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1696 NULL, &dev->video_mode.slock,
1697 fh->type, V4L2_FIELD_INTERLACED,
Hans Verkuil08bff032010-09-20 17:39:46 -03001698 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001699 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001700 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001701 /* Set the required alternate setting VBI interface works in
1702 Bulk mode only */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001703 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001704
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001705 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1706 NULL, &dev->vbi_mode.slock,
1707 fh->type, V4L2_FIELD_SEQ_TB,
Hans Verkuil08bff032010-09-20 17:39:46 -03001708 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001709 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001710 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001711 mutex_unlock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001712 v4l2_fh_add(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001713
Sri Deevie0d3baf2009-03-03 14:37:50 -03001714 return errCode;
1715}
1716
1717/*
1718 * cx231xx_realease_resources()
1719 * unregisters the v4l2,i2c and usb devices
1720 * called when the device gets disconected or at module unload
1721*/
1722void cx231xx_release_analog_resources(struct cx231xx *dev)
1723{
1724
1725 /*FIXME: I2C IR should be disconnected */
1726
1727 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001728 if (video_is_registered(dev->radio_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001729 video_unregister_device(dev->radio_dev);
1730 else
1731 video_device_release(dev->radio_dev);
1732 dev->radio_dev = NULL;
1733 }
1734 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001735 cx231xx_info("V4L2 device %s deregistered\n",
1736 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001737 if (video_is_registered(dev->vbi_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001738 video_unregister_device(dev->vbi_dev);
1739 else
1740 video_device_release(dev->vbi_dev);
1741 dev->vbi_dev = NULL;
1742 }
1743 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001744 cx231xx_info("V4L2 device %s deregistered\n",
1745 video_device_node_name(dev->vdev));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001746
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001747 if (dev->board.has_417)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001748 cx231xx_417_unregister(dev);
1749
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001750 if (video_is_registered(dev->vdev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001751 video_unregister_device(dev->vdev);
1752 else
1753 video_device_release(dev->vdev);
1754 dev->vdev = NULL;
1755 }
Hans Verkuild2370f82012-09-17 07:22:09 -03001756 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1757 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001758}
1759
1760/*
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001761 * cx231xx_close()
Sri Deevie0d3baf2009-03-03 14:37:50 -03001762 * stops streaming and deallocates all resources allocated by the v4l2
1763 * calls and ioctls
1764 */
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001765static int cx231xx_close(struct file *filp)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001766{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001767 struct cx231xx_fh *fh = filp->private_data;
1768 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001769
1770 cx231xx_videodbg("users=%d\n", dev->users);
1771
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001772 cx231xx_videodbg("users=%d\n", dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001773 if (res_check(fh))
1774 res_free(fh);
1775
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001776 /*
1777 * To workaround error number=-71 on EP0 for VideoGrabber,
1778 * need exclude following.
1779 * FIXME: It is probably safe to remove most of these, as we're
1780 * now avoiding the alternate setting for INDEX_VANC
1781 */
1782 if (!dev->board.no_alt_vanc)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001783 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1784 videobuf_stop(&fh->vb_vidq);
1785 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001786
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001787 /* the device is already disconnect,
1788 free the remaining resources */
1789 if (dev->state & DEV_DISCONNECTED) {
1790 if (atomic_read(&dev->devlist_count) > 0) {
1791 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03001792 fh->dev = NULL;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001793 return 0;
1794 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001795 return 0;
1796 }
1797
1798 /* do this before setting alternate! */
1799 cx231xx_uninit_vbi_isoc(dev);
1800
1801 /* set alternate 0 */
1802 if (!dev->vbi_or_sliced_cc_mode)
1803 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1804 else
1805 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1806
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001807 v4l2_fh_del(&fh->fh);
1808 v4l2_fh_exit(&fh->fh);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001809 kfree(fh);
1810 dev->users--;
1811 wake_up_interruptible_nr(&dev->open, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001812 return 0;
1813 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001814
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001815 v4l2_fh_del(&fh->fh);
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -02001816 dev->users--;
1817 if (!dev->users) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001818 videobuf_stop(&fh->vb_vidq);
1819 videobuf_mmap_free(&fh->vb_vidq);
1820
1821 /* the device is already disconnect,
1822 free the remaining resources */
1823 if (dev->state & DEV_DISCONNECTED) {
1824 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03001825 fh->dev = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001826 return 0;
1827 }
1828
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001829 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001830 call_all(dev, core, s_power, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001831
1832 /* do this before setting alternate! */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001833 if (dev->USE_ISO)
1834 cx231xx_uninit_isoc(dev);
1835 else
1836 cx231xx_uninit_bulk(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001837 cx231xx_set_mode(dev, CX231XX_SUSPEND);
1838
1839 /* set alternate 0 */
1840 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1841 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001842 v4l2_fh_exit(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001843 kfree(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001844 wake_up_interruptible_nr(&dev->open, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001845 return 0;
1846}
1847
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001848static int cx231xx_v4l2_close(struct file *filp)
1849{
1850 struct cx231xx_fh *fh = filp->private_data;
1851 struct cx231xx *dev = fh->dev;
1852 int rc;
1853
1854 mutex_lock(&dev->lock);
1855 rc = cx231xx_close(filp);
1856 mutex_unlock(&dev->lock);
1857 return rc;
1858}
1859
Sri Deevie0d3baf2009-03-03 14:37:50 -03001860/*
1861 * cx231xx_v4l2_read()
1862 * will allocate buffers when called for the first time
1863 */
1864static ssize_t
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001865cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1866 loff_t *pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001867{
1868 struct cx231xx_fh *fh = filp->private_data;
1869 struct cx231xx *dev = fh->dev;
1870 int rc;
1871
1872 rc = check_dev(dev);
1873 if (rc < 0)
1874 return rc;
1875
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001876 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1877 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001878 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001879
1880 if (unlikely(rc < 0))
1881 return rc;
1882
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001883 if (mutex_lock_interruptible(&dev->lock))
1884 return -ERESTARTSYS;
1885 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001886 filp->f_flags & O_NONBLOCK);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001887 mutex_unlock(&dev->lock);
1888 return rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001889 }
1890 return 0;
1891}
1892
1893/*
1894 * cx231xx_v4l2_poll()
1895 * will allocate buffers when called for the first time
1896 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001897static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001898{
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001899 unsigned long req_events = poll_requested_events(wait);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001900 struct cx231xx_fh *fh = filp->private_data;
1901 struct cx231xx *dev = fh->dev;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001902 unsigned res = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001903 int rc;
1904
1905 rc = check_dev(dev);
1906 if (rc < 0)
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001907 return POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001908
Sri Deevie0d3baf2009-03-03 14:37:50 -03001909 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001910
1911 if (unlikely(rc < 0))
1912 return POLLERR;
1913
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001914 if (v4l2_event_pending(&fh->fh))
1915 res |= POLLPRI;
1916 else
1917 poll_wait(filp, &fh->fh.wait, wait);
1918
1919 if (!(req_events & (POLLIN | POLLRDNORM)))
1920 return res;
1921
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001922 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001923 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001924 mutex_lock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001925 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001926 mutex_unlock(&dev->lock);
1927 return res;
1928 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001929 return res | POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001930}
1931
1932/*
1933 * cx231xx_v4l2_mmap()
1934 */
1935static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1936{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001937 struct cx231xx_fh *fh = filp->private_data;
1938 struct cx231xx *dev = fh->dev;
1939 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001940
1941 rc = check_dev(dev);
1942 if (rc < 0)
1943 return rc;
1944
Sri Deevie0d3baf2009-03-03 14:37:50 -03001945 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001946
1947 if (unlikely(rc < 0))
1948 return rc;
1949
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001950 if (mutex_lock_interruptible(&dev->lock))
1951 return -ERESTARTSYS;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001952 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001953 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001954
1955 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001956 (unsigned long)vma->vm_start,
1957 (unsigned long)vma->vm_end -
1958 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001959
1960 return rc;
1961}
1962
1963static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001964 .owner = THIS_MODULE,
1965 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001966 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001967 .read = cx231xx_v4l2_read,
1968 .poll = cx231xx_v4l2_poll,
1969 .mmap = cx231xx_v4l2_mmap,
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001970 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03001971};
1972
1973static const struct v4l2_ioctl_ops video_ioctl_ops = {
Hans Verkuilbc087342013-01-29 12:52:33 -03001974 .vidioc_querycap = cx231xx_querycap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001975 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1976 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1977 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1978 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1979 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1980 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
Hans Verkuil62647222013-02-09 06:41:11 -03001981 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001982 .vidioc_cropcap = vidioc_cropcap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001983 .vidioc_reqbufs = vidioc_reqbufs,
1984 .vidioc_querybuf = vidioc_querybuf,
1985 .vidioc_qbuf = vidioc_qbuf,
1986 .vidioc_dqbuf = vidioc_dqbuf,
1987 .vidioc_s_std = vidioc_s_std,
1988 .vidioc_g_std = vidioc_g_std,
Hans Verkuilb86d1542013-01-29 13:16:06 -03001989 .vidioc_enum_input = cx231xx_enum_input,
1990 .vidioc_g_input = cx231xx_g_input,
1991 .vidioc_s_input = cx231xx_s_input,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001992 .vidioc_streamon = vidioc_streamon,
1993 .vidioc_streamoff = vidioc_streamoff,
Hans Verkuilb86d1542013-01-29 13:16:06 -03001994 .vidioc_g_tuner = cx231xx_g_tuner,
1995 .vidioc_s_tuner = cx231xx_s_tuner,
1996 .vidioc_g_frequency = cx231xx_g_frequency,
1997 .vidioc_s_frequency = cx231xx_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03001998#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil08fe9f72013-05-29 06:59:43 -03001999 .vidioc_g_chip_info = cx231xx_g_chip_info,
Hans Verkuilb86d1542013-01-29 13:16:06 -03002000 .vidioc_g_register = cx231xx_g_register,
2001 .vidioc_s_register = cx231xx_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002002#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002003 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2004 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002005};
2006
2007static struct video_device cx231xx_vbi_template;
2008
2009static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002010 .fops = &cx231xx_v4l_fops,
2011 .release = video_device_release,
2012 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002013 .tvnorms = V4L2_STD_ALL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002014};
2015
2016static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002017 .owner = THIS_MODULE,
2018 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002019 .release = cx231xx_v4l2_close,
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002020 .poll = v4l2_ctrl_poll,
Hans Verkuil1265f082012-09-17 09:26:46 -03002021 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002022};
2023
2024static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuilbc087342013-01-29 12:52:33 -03002025 .vidioc_querycap = cx231xx_querycap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002026 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002027 .vidioc_s_tuner = radio_s_tuner,
Hans Verkuilb86d1542013-01-29 13:16:06 -03002028 .vidioc_g_frequency = cx231xx_g_frequency,
2029 .vidioc_s_frequency = cx231xx_s_frequency,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002030#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil08fe9f72013-05-29 06:59:43 -03002031 .vidioc_g_chip_info = cx231xx_g_chip_info,
Hans Verkuilb86d1542013-01-29 13:16:06 -03002032 .vidioc_g_register = cx231xx_g_register,
2033 .vidioc_s_register = cx231xx_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002034#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002035 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2036 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002037};
2038
2039static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002040 .name = "cx231xx-radio",
2041 .fops = &radio_fops,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002042 .ioctl_ops = &radio_ioctl_ops,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002043};
2044
2045/******************************** usb interface ******************************/
2046
Sri Deevib9255172009-03-04 17:49:01 -03002047static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2048 const struct video_device
2049 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002050{
2051 struct video_device *vfd;
2052
2053 vfd = video_device_alloc();
2054 if (NULL == vfd)
2055 return NULL;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002056
Sri Deevie0d3baf2009-03-03 14:37:50 -03002057 *vfd = *template;
Sri Deevib1196122009-03-20 23:33:48 -03002058 vfd->v4l2_dev = &dev->v4l2_dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002059 vfd->release = video_device_release;
2060 vfd->debug = video_debug;
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002061 vfd->lock = &dev->lock;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002062 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002063
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002064 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002065
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002066 video_set_drvdata(vfd, dev);
Hans Verkuil06c46002012-09-13 06:17:47 -03002067 if (dev->tuner_type == TUNER_ABSENT) {
2068 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2069 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2070 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2071 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2072 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002073 return vfd;
2074}
2075
2076int cx231xx_register_analog_devices(struct cx231xx *dev)
2077{
2078 int ret;
2079
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002080 cx231xx_info("%s: v4l2 driver version %s\n",
2081 dev->name, CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002082
2083 /* set default norm */
Hans Verkuila25a70122012-09-17 08:30:07 -03002084 dev->norm = V4L2_STD_PAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002085 dev->width = norm_maxw(dev);
2086 dev->height = norm_maxh(dev);
2087 dev->interlaced = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002088
2089 /* Analog specific initialization */
2090 dev->format = &format[0];
Devin Heitmueller6e6a2ba2010-07-12 15:34:57 -03002091
2092 /* Set the initial input */
2093 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002094
Hans Verkuild61072a2013-01-29 10:50:54 -03002095 call_all(dev, core, s_std, dev->norm);
2096
Hans Verkuild2370f82012-09-17 07:22:09 -03002097 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2098 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2099
2100 if (dev->sd_cx25840) {
2101 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2102 dev->sd_cx25840->ctrl_handler, NULL);
2103 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2104 dev->sd_cx25840->ctrl_handler,
2105 v4l2_ctrl_radio_filter);
2106 }
2107
2108 if (dev->ctrl_handler.error)
2109 return dev->ctrl_handler.error;
2110 if (dev->radio_ctrl_handler.error)
2111 return dev->radio_ctrl_handler.error;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002112
2113 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002114 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002115
2116 /* allocate and fill video video_device struct */
2117 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2118 if (!dev->vdev) {
2119 cx231xx_errdev("cannot allocate video_device.\n");
2120 return -ENODEV;
2121 }
2122
Hans Verkuild2370f82012-09-17 07:22:09 -03002123 dev->vdev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002124 /* register v4l2 video video_device */
2125 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002126 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002127 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002128 cx231xx_errdev("unable to register video device (error=%i).\n",
2129 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002130 return ret;
2131 }
2132
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002133 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2134 dev->name, video_device_node_name(dev->vdev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002135
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002136 /* Initialize VBI template */
Ezequiel Garcia3724dde2012-10-23 15:57:05 -03002137 cx231xx_vbi_template = cx231xx_video_template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002138 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002139
2140 /* Allocate and fill vbi video_device struct */
2141 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2142
Hans Verkuild2370f82012-09-17 07:22:09 -03002143 if (!dev->vbi_dev) {
2144 cx231xx_errdev("cannot allocate video_device.\n");
2145 return -ENODEV;
2146 }
2147 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002148 /* register v4l2 vbi video_device */
2149 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002150 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002151 if (ret < 0) {
2152 cx231xx_errdev("unable to register vbi device\n");
2153 return ret;
2154 }
2155
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002156 cx231xx_info("%s/0: registered device %s\n",
2157 dev->name, video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002158
2159 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002160 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2161 "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002162 if (!dev->radio_dev) {
2163 cx231xx_errdev("cannot allocate video_device.\n");
2164 return -ENODEV;
2165 }
Hans Verkuild2370f82012-09-17 07:22:09 -03002166 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002167 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2168 radio_nr[dev->devno]);
2169 if (ret < 0) {
2170 cx231xx_errdev("can't register radio device\n");
2171 return ret;
2172 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002173 cx231xx_info("Registered radio device as %s\n",
2174 video_device_node_name(dev->radio_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002175 }
2176
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002177 cx231xx_info("V4L2 device registered as %s and %s\n",
2178 video_device_node_name(dev->vdev),
2179 video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002180
2181 return 0;
2182}