blob: b6cde46131c7de5c3f617b4f03d8e37fb84e5ea6 [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/v4l2-chip-ident.h>
40#include <media/msp3400.h>
41#include <media/tuner.h>
42
43#include "dvb_frontend.h"
44
45#include "cx231xx.h"
46#include "cx231xx-vbi.h"
47
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030048#define CX231XX_VERSION "0.0.2"
Mauro Carvalho Chehab818fdf32009-03-13 07:41:58 -030049
Sri Deevie0d3baf2009-03-03 14:37:50 -030050#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
51#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
52
Sri Deevie0d3baf2009-03-03 14:37:50 -030053#define cx231xx_videodbg(fmt, arg...) do {\
54 if (video_debug) \
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
57
58static unsigned int isoc_debug;
59module_param(isoc_debug, int, 0644);
60MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61
62#define cx231xx_isocdbg(fmt, arg...) \
63do {\
64 if (isoc_debug) { \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
67 } \
68 } while (0)
69
70MODULE_AUTHOR(DRIVER_AUTHOR);
71MODULE_DESCRIPTION(DRIVER_DESC);
72MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030073MODULE_VERSION(CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -030074
Sri Deevie0d3baf2009-03-03 14:37:50 -030075static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
79
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030080module_param_array(card, int, NULL, 0444);
Sri Deevie0d3baf2009-03-03 14:37:50 -030081module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
83module_param_array(radio_nr, int, NULL, 0444);
84
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030085MODULE_PARM_DESC(card, "card type");
Sri Deevie0d3baf2009-03-03 14:37:50 -030086MODULE_PARM_DESC(video_nr, "video device numbers");
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030087MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
Sri Deevie0d3baf2009-03-03 14:37:50 -030088MODULE_PARM_DESC(radio_nr, "radio device numbers");
89
90static unsigned int video_debug;
91module_param(video_debug, int, 0644);
92MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
93
Sri Deevie0d3baf2009-03-03 14:37:50 -030094/* supported video standards */
95static struct cx231xx_fmt format[] = {
96 {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030097 .name = "16bpp YUY2, 4:2:2, packed",
98 .fourcc = V4L2_PIX_FMT_YUYV,
99 .depth = 16,
100 .reg = 0,
101 },
Sri Deevie0d3baf2009-03-03 14:37:50 -0300102};
103
Sri Deevie0d3baf2009-03-03 14:37:50 -0300104
Sri Deevie0d3baf2009-03-03 14:37:50 -0300105/* ------------------------------------------------------------------
106 Video buffer and parser functions
107 ------------------------------------------------------------------*/
108
109/*
110 * Announces that a buffer were filled and request the next
111 */
112static inline void buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300113 struct cx231xx_dmaqueue *dma_q,
114 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300115{
116 /* Advice that buffer was filled */
117 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
118 buf->vb.state = VIDEOBUF_DONE;
119 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300120 v4l2_get_timestamp(&buf->vb.ts);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300121
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300122 if (dev->USE_ISO)
123 dev->video_mode.isoc_ctl.buf = NULL;
124 else
125 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300126
127 list_del(&buf->vb.queue);
128 wake_up(&buf->vb.done);
129}
130
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300131static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300132{
133 char *errmsg = "Unknown";
134
135 switch (status) {
136 case -ENOENT:
137 errmsg = "unlinked synchronuously";
138 break;
139 case -ECONNRESET:
140 errmsg = "unlinked asynchronuously";
141 break;
142 case -ENOSR:
143 errmsg = "Buffer error (overrun)";
144 break;
145 case -EPIPE:
146 errmsg = "Stalled (device not responding)";
147 break;
148 case -EOVERFLOW:
149 errmsg = "Babble (bad cable?)";
150 break;
151 case -EPROTO:
152 errmsg = "Bit-stuff error (bad cable?)";
153 break;
154 case -EILSEQ:
155 errmsg = "CRC/Timeout (could be anything)";
156 break;
157 case -ETIME:
158 errmsg = "Device does not respond";
159 break;
160 }
161 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300162 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300163 } else {
164 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300165 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300166 }
167}
168
169/*
170 * video-buf generic routine to get the next available buffer
171 */
172static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300173 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300174{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300175 struct cx231xx_video_mode *vmode =
176 container_of(dma_q, struct cx231xx_video_mode, vidq);
177 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300178
179 char *outp;
180
Sri Deevie0d3baf2009-03-03 14:37:50 -0300181 if (list_empty(&dma_q->active)) {
182 cx231xx_isocdbg("No active queue to serve\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300183 if (dev->USE_ISO)
184 dev->video_mode.isoc_ctl.buf = NULL;
185 else
186 dev->video_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300187 *buf = NULL;
188 return;
189 }
190
191 /* Get the next buffer */
192 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
193
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300194 /* Cleans up buffer - Useful for testing for frame/URB loss */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300195 outp = videobuf_to_vmalloc(&(*buf)->vb);
196 memset(outp, 0, (*buf)->vb.size);
197
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300198 if (dev->USE_ISO)
199 dev->video_mode.isoc_ctl.buf = *buf;
200 else
201 dev->video_mode.bulk_ctl.buf = *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300202
203 return;
204}
205
206/*
207 * Controls the isoc copy of each urb packet
208 */
209static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
210{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300211 struct cx231xx_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300212 int i, rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300213 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300214 u32 bytes_parsed = 0, buffer_size = 0;
215 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300216
217 if (!dev)
218 return 0;
219
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200220 if (dev->state & DEV_DISCONNECTED)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300221 return 0;
222
223 if (urb->status < 0) {
224 print_err_status(dev, -1, urb->status);
225 if (urb->status == -ENOENT)
226 return 0;
227 }
228
Sri Deevie0d3baf2009-03-03 14:37:50 -0300229 for (i = 0; i < urb->number_of_packets; i++) {
230 int status = urb->iso_frame_desc[i].status;
231
232 if (status < 0) {
233 print_err_status(dev, i, status);
234 if (urb->iso_frame_desc[i].status != -EPROTO)
235 continue;
236 }
237
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300238 if (urb->iso_frame_desc[i].actual_length <= 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300239 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
240 continue;
241 }
242 if (urb->iso_frame_desc[i].actual_length >
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300243 dev->video_mode.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300244 cx231xx_isocdbg("packet bigger than packet size");
245 continue;
246 }
247
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300248 /* get buffer pointer and length */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300249 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300250 buffer_size = urb->iso_frame_desc[i].actual_length;
251 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300252
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300253 if (dma_q->is_partial_line) {
Sri Deevib9255172009-03-04 17:49:01 -0300254 /* Handle the case of a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300255 sav_eav = dma_q->last_sav;
256 } else {
Sri Deevib9255172009-03-04 17:49:01 -0300257 /* Check for a SAV/EAV overlapping
258 the buffer boundary */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300259 sav_eav =
260 cx231xx_find_boundary_SAV_EAV(p_buffer,
261 dma_q->partial_buf,
262 &bytes_parsed);
263 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300264
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300265 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300266 /* Get the first line if we have some portion of an SAV/EAV from
267 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300268 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300269 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
Sri Deevib9255172009-03-04 17:49:01 -0300270 sav_eav, /* SAV/EAV */
271 p_buffer + bytes_parsed, /* p_buffer */
272 buffer_size - bytes_parsed);/* buf size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300273 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300274
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300275 /* Now parse data that is completely in this buffer */
276 /* dma_q->is_partial_line = 0; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300277
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300278 while (bytes_parsed < buffer_size) {
279 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300280
Sri Deevib9255172009-03-04 17:49:01 -0300281 sav_eav = cx231xx_find_next_SAV_EAV(
282 p_buffer + bytes_parsed, /* p_buffer */
283 buffer_size - bytes_parsed, /* buf size */
284 &bytes_used);/* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300285
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300286 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300287
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300288 sav_eav &= 0xF0;
289 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300290 bytes_parsed += cx231xx_get_video_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300291 dma_q, sav_eav, /* SAV/EAV */
292 p_buffer + bytes_parsed,/* p_buffer */
293 buffer_size - bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300294 }
295 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300296
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300297 /* Save the last four bytes of the buffer so we can check the
298 buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300299 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
300 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300301
302 }
303 return rc;
304}
305
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300306static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
307{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300308 struct cx231xx_dmaqueue *dma_q = urb->context;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300309 int rc = 1;
310 unsigned char *p_buffer;
311 u32 bytes_parsed = 0, buffer_size = 0;
312 u8 sav_eav = 0;
313
314 if (!dev)
315 return 0;
316
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -0200317 if (dev->state & DEV_DISCONNECTED)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300318 return 0;
319
320 if (urb->status < 0) {
321 print_err_status(dev, -1, urb->status);
322 if (urb->status == -ENOENT)
323 return 0;
324 }
325
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300326 if (1) {
327
328 /* get buffer pointer and length */
329 p_buffer = urb->transfer_buffer;
330 buffer_size = urb->actual_length;
331 bytes_parsed = 0;
332
333 if (dma_q->is_partial_line) {
334 /* Handle the case of a partial line */
335 sav_eav = dma_q->last_sav;
336 } else {
337 /* Check for a SAV/EAV overlapping
338 the buffer boundary */
339 sav_eav =
340 cx231xx_find_boundary_SAV_EAV(p_buffer,
341 dma_q->partial_buf,
342 &bytes_parsed);
343 }
344
345 sav_eav &= 0xF0;
346 /* Get the first line if we have some portion of an SAV/EAV from
347 the last buffer or a partial line */
348 if (sav_eav) {
349 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
350 sav_eav, /* SAV/EAV */
351 p_buffer + bytes_parsed, /* p_buffer */
352 buffer_size - bytes_parsed);/* buf size */
353 }
354
355 /* Now parse data that is completely in this buffer */
356 /* dma_q->is_partial_line = 0; */
357
358 while (bytes_parsed < buffer_size) {
359 u32 bytes_used = 0;
360
361 sav_eav = cx231xx_find_next_SAV_EAV(
362 p_buffer + bytes_parsed, /* p_buffer */
363 buffer_size - bytes_parsed, /* buf size */
364 &bytes_used);/* bytes used to get SAV/EAV */
365
366 bytes_parsed += bytes_used;
367
368 sav_eav &= 0xF0;
369 if (sav_eav && (bytes_parsed < buffer_size)) {
370 bytes_parsed += cx231xx_get_video_line(dev,
371 dma_q, sav_eav, /* SAV/EAV */
372 p_buffer + bytes_parsed,/* p_buffer */
373 buffer_size - bytes_parsed);/*buf size*/
374 }
375 }
376
377 /* Save the last four bytes of the buffer so we can check the
378 buffer boundary condition next time */
379 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
380 bytes_parsed = 0;
381
382 }
383 return rc;
384}
385
386
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300387u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
388 u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300389{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300390 u32 bytes_used;
391 u8 boundary_bytes[8];
392 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300393
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300394 *p_bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300395
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300396 /* Create an array of the last 4 bytes of the last buffer and the first
397 4 bytes of the current buffer. */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300398
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300399 memcpy(boundary_bytes, partial_buf, 4);
400 memcpy(boundary_bytes + 4, p_buffer, 4);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300401
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300402 /* Check for the SAV/EAV in the boundary buffer */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300403 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
404 &bytes_used);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300405
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300406 if (sav_eav) {
407 /* found a boundary SAV/EAV. Updates the bytes used to reflect
408 only those used in the new buffer */
409 *p_bytes_used = bytes_used - 4;
410 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300411
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300412 return sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300413}
414
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300415u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300416{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300417 u32 i;
418 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300419
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300420 /*
421 * Don't search if the buffer size is less than 4. It causes a page
422 * fault since buffer_size - 4 evaluates to a large number in that
423 * case.
424 */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300425 if (buffer_size < 4) {
426 *p_bytes_used = buffer_size;
427 return 0;
428 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300429
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300430 for (i = 0; i < (buffer_size - 3); i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300431
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300432 if ((p_buffer[i] == 0xFF) &&
433 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300434
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300435 *p_bytes_used = i + 4;
436 sav_eav = p_buffer[i + 3];
437 return sav_eav;
438 }
439 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300440
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300441 *p_bytes_used = buffer_size;
442 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300443}
444
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300445u32 cx231xx_get_video_line(struct cx231xx *dev,
446 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
447 u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300448{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300449 u32 bytes_copied = 0;
450 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300451
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300452 switch (sav_eav) {
453 case SAV_ACTIVE_VIDEO_FIELD1:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300454 /* looking for skipped line which occurred in PAL 720x480 mode.
455 In this case, there will be no active data contained
456 between the SAV and EAV */
457 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
458 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
459 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
460 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
461 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
462 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300463 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300464 current_field = 1;
465 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300466
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300467 case SAV_ACTIVE_VIDEO_FIELD2:
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300468 /* looking for skipped line which occurred in PAL 720x480 mode.
469 In this case, there will be no active data contained between
470 the SAV and EAV */
471 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
472 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
473 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
474 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
475 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
476 (p_buffer[3] == EAV_VBLANK_FIELD2)))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300477 return bytes_copied;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300478 current_field = 2;
479 break;
480 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300481
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300482 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300483
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300484 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
485 buffer_size, current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300486
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300487 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300488}
489
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300490u32 cx231xx_copy_video_line(struct cx231xx *dev,
491 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300492 u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300493{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300494 u32 bytes_to_copy;
495 struct cx231xx_buffer *buf;
496 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300497
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300498 if (dma_q->current_field != field_number)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300499 cx231xx_reset_video_buffer(dev, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300500
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300501 /* get the buffer pointer */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300502 if (dev->USE_ISO)
503 buf = dev->video_mode.isoc_ctl.buf;
504 else
505 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300506
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300507 /* Remember the field number for next time */
508 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300509
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300510 bytes_to_copy = dma_q->bytes_left_in_line;
511 if (bytes_to_copy > length)
512 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300513
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300514 if (dma_q->lines_completed >= dma_q->lines_per_field) {
515 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300516 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
517 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300518 return 0;
519 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300520
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300521 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300522
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300523 /* If we don't have a buffer, just return the number of bytes we would
524 have copied if we had a buffer. */
525 if (!buf) {
526 dma_q->bytes_left_in_line -= bytes_to_copy;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300527 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
528 ? 0 : 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300529 return bytes_to_copy;
530 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300531
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300532 /* copy the data to video buffer */
533 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300534
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300535 dma_q->pos += bytes_to_copy;
536 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300537
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300538 if (dma_q->bytes_left_in_line == 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300539 dma_q->bytes_left_in_line = _line_size;
540 dma_q->lines_completed++;
541 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300542
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300543 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300544 buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300545
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300546 dma_q->pos = 0;
547 buf = NULL;
548 dma_q->lines_completed = 0;
549 }
550 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300551
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300552 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300553}
554
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300555void cx231xx_reset_video_buffer(struct cx231xx *dev,
556 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300557{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300558 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300559
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300560 /* handle the switch from field 1 to field 2 */
561 if (dma_q->current_field == 1) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300562 if (dma_q->lines_completed >= dma_q->lines_per_field)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300563 dma_q->field1_done = 1;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300564 else
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300565 dma_q->field1_done = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300566 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300567
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300568 if (dev->USE_ISO)
569 buf = dev->video_mode.isoc_ctl.buf;
570 else
571 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300572
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300573 if (buf == NULL) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300574 /* first try to get the buffer */
575 get_next_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300576
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300577 dma_q->pos = 0;
578 dma_q->field1_done = 0;
579 dma_q->current_field = -1;
580 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300581
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300582 /* reset the counters */
583 dma_q->bytes_left_in_line = dev->width << 1;
584 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300585}
586
587int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300588 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300589{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300590 u8 *p_out_buffer = NULL;
591 u32 current_line_bytes_copied = 0;
592 struct cx231xx_buffer *buf;
593 u32 _line_size = dev->width << 1;
594 void *startwrite;
595 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300596
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300597 if (dev->USE_ISO)
598 buf = dev->video_mode.isoc_ctl.buf;
599 else
600 buf = dev->video_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300601
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300602 if (buf == NULL)
603 return -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300604
605 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
606
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300607 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300608
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300609 /* Offset field 2 one line from the top of the buffer */
610 offset = (dma_q->current_field == 1) ? 0 : _line_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300611
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300612 /* Offset for field 2 */
613 startwrite = p_out_buffer + offset;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300614
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300615 /* lines already completed in the current field */
616 startwrite += (dma_q->lines_completed * _line_size * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300617
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300618 /* bytes already completed in the current line */
619 startwrite += current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300620
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300621 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
622 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300623
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300624 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300625 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300626
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300627 /* The below copies the UYVY data straight into video buffer */
628 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300629
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300630 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300631}
632
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300633void cx231xx_swab(u16 *from, u16 *to, u16 len)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300634{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300635 u16 i;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300636
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300637 if (len <= 0)
638 return;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300639
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300640 for (i = 0; i < len / 2; i++)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300641 to[i] = (from[i] << 8) | (from[i] >> 8);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300642}
643
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300644u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300645{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300646 u8 buffer_complete = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300647
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300648 /* Dual field stream */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300649 buffer_complete = ((dma_q->current_field == 2) &&
650 (dma_q->lines_completed >= dma_q->lines_per_field) &&
651 dma_q->field1_done);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300652
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300653 return buffer_complete;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300654}
655
Sri Deevie0d3baf2009-03-03 14:37:50 -0300656/* ------------------------------------------------------------------
657 Videobuf operations
658 ------------------------------------------------------------------*/
659
660static int
661buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
662{
663 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300664 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300665
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300666 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300667 if (0 == *count)
668 *count = CX231XX_DEF_BUF;
669
670 if (*count < CX231XX_MIN_BUF)
671 *count = CX231XX_MIN_BUF;
672
Sri Deevie0d3baf2009-03-03 14:37:50 -0300673 return 0;
674}
675
676/* This is called *without* dev->slock held; please keep it that way */
677static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
678{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300679 struct cx231xx_fh *fh = vq->priv_data;
680 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300681 unsigned long flags = 0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300682
Sri Deevie0d3baf2009-03-03 14:37:50 -0300683 if (in_interrupt())
684 BUG();
685
686 /* We used to wait for the buffer to finish here, but this didn't work
687 because, as we were keeping the state as VIDEOBUF_QUEUED,
688 videobuf_queue_cancel marked it as finished for us.
689 (Also, it could wedge forever if the hardware was misconfigured.)
690
691 This should be safe; by the time we get here, the buffer isn't
692 queued anymore. If we ever start marking the buffers as
693 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300694 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300695 spin_lock_irqsave(&dev->video_mode.slock, flags);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300696 if (dev->USE_ISO) {
697 if (dev->video_mode.isoc_ctl.buf == buf)
698 dev->video_mode.isoc_ctl.buf = NULL;
699 } else {
700 if (dev->video_mode.bulk_ctl.buf == buf)
701 dev->video_mode.bulk_ctl.buf = NULL;
702 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300703 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
704
705 videobuf_vmalloc_free(&buf->vb);
706 buf->vb.state = VIDEOBUF_NEEDS_INIT;
707}
708
709static int
710buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300711 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300712{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300713 struct cx231xx_fh *fh = vq->priv_data;
714 struct cx231xx_buffer *buf =
715 container_of(vb, struct cx231xx_buffer, vb);
716 struct cx231xx *dev = fh->dev;
717 int rc = 0, urb_init = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300718
719 /* The only currently supported format is 16 bits/pixel */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300720 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
721 + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300722 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300723 return -EINVAL;
724
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300725 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300726 buf->vb.height = dev->height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300727 buf->vb.field = field;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300728
729 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
730 rc = videobuf_iolock(vq, &buf->vb, NULL);
731 if (rc < 0)
732 goto fail;
733 }
734
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300735 if (dev->USE_ISO) {
736 if (!dev->video_mode.isoc_ctl.num_bufs)
737 urb_init = 1;
738 } else {
739 if (!dev->video_mode.bulk_ctl.num_bufs)
740 urb_init = 1;
741 }
742 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
743 urb_init, dev->video_mode.max_pkt_size);*/
Sri Deevie0d3baf2009-03-03 14:37:50 -0300744 if (urb_init) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300745 dev->mode_tv = 0;
746 if (dev->USE_ISO)
747 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300748 CX231XX_NUM_BUFS,
749 dev->video_mode.max_pkt_size,
750 cx231xx_isoc_copy);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300751 else
752 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
753 CX231XX_NUM_BUFS,
754 dev->video_mode.max_pkt_size,
755 cx231xx_bulk_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300756 if (rc < 0)
757 goto fail;
758 }
759
760 buf->vb.state = VIDEOBUF_PREPARED;
761 return 0;
762
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300763fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300764 free_buffer(vq, buf);
765 return rc;
766}
767
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300768static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300769{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300770 struct cx231xx_buffer *buf =
771 container_of(vb, struct cx231xx_buffer, vb);
772 struct cx231xx_fh *fh = vq->priv_data;
773 struct cx231xx *dev = fh->dev;
774 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300775
776 buf->vb.state = VIDEOBUF_QUEUED;
777 list_add_tail(&buf->vb.queue, &vidq->active);
778
779}
780
781static void buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300782 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300783{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300784 struct cx231xx_buffer *buf =
785 container_of(vb, struct cx231xx_buffer, vb);
786 struct cx231xx_fh *fh = vq->priv_data;
787 struct cx231xx *dev = (struct cx231xx *)fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300788
789 cx231xx_isocdbg("cx231xx: called buffer_release\n");
790
791 free_buffer(vq, buf);
792}
793
794static struct videobuf_queue_ops cx231xx_video_qops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300795 .buf_setup = buffer_setup,
796 .buf_prepare = buffer_prepare,
797 .buf_queue = buffer_queue,
798 .buf_release = buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300799};
800
801/********************* v4l2 interface **************************************/
802
Sri Deevie0d3baf2009-03-03 14:37:50 -0300803void video_mux(struct cx231xx *dev, int index)
804{
Sri Deevie0d3baf2009-03-03 14:37:50 -0300805 dev->video_input = index;
806 dev->ctl_ainput = INPUT(index)->amux;
807
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300808 cx231xx_set_video_input_mux(dev, index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300809
Hans Verkuil5325b422009-04-02 11:26:22 -0300810 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300811
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300812 cx231xx_set_audio_input(dev, dev->ctl_ainput);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300813
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300814 cx231xx_info("video_mux : %d\n", index);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300815
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300816 /* do mode control overrides if required */
817 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300818}
819
820/* Usage lock check functions */
821static int res_get(struct cx231xx_fh *fh)
822{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300823 struct cx231xx *dev = fh->dev;
824 int rc = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300825
826 /* This instance already has stream_on */
827 if (fh->stream_on)
828 return rc;
829
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300830 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
831 if (dev->stream_on)
832 return -EBUSY;
833 dev->stream_on = 1;
834 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
835 if (dev->vbi_stream_on)
836 return -EBUSY;
837 dev->vbi_stream_on = 1;
838 } else
839 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300840
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300841 fh->stream_on = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300842
843 return rc;
844}
845
846static int res_check(struct cx231xx_fh *fh)
847{
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300848 return fh->stream_on;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300849}
850
851static void res_free(struct cx231xx_fh *fh)
852{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300853 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300854
855 fh->stream_on = 0;
856
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300857 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
858 dev->stream_on = 0;
859 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
860 dev->vbi_stream_on = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300861}
862
863static int check_dev(struct cx231xx *dev)
864{
865 if (dev->state & DEV_DISCONNECTED) {
866 cx231xx_errdev("v4l2 ioctl: device not present\n");
867 return -ENODEV;
868 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300869 return 0;
870}
871
Sri Deevie0d3baf2009-03-03 14:37:50 -0300872/* ------------------------------------------------------------------
873 IOCTL vidioc handling
874 ------------------------------------------------------------------*/
875
876static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300877 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300878{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300879 struct cx231xx_fh *fh = priv;
880 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300881
Sri Deevie0d3baf2009-03-03 14:37:50 -0300882 f->fmt.pix.width = dev->width;
883 f->fmt.pix.height = dev->height;
Joe Perches1ebcad72009-07-02 15:57:09 -0300884 f->fmt.pix.pixelformat = dev->format->fourcc;
885 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300886 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300887 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
888
889 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300890 f->fmt.pix.priv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300891
Sri Deevie0d3baf2009-03-03 14:37:50 -0300892 return 0;
893}
894
895static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
896{
897 unsigned int i;
898
899 for (i = 0; i < ARRAY_SIZE(format); i++)
900 if (format[i].fourcc == fourcc)
901 return &format[i];
902
903 return NULL;
904}
905
906static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300907 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300908{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300909 struct cx231xx_fh *fh = priv;
910 struct cx231xx *dev = fh->dev;
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300911 unsigned int width = f->fmt.pix.width;
912 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300913 unsigned int maxw = norm_maxw(dev);
914 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300915 struct cx231xx_fmt *fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300916
917 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
918 if (!fmt) {
919 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300920 f->fmt.pix.pixelformat);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300921 return -EINVAL;
922 }
923
924 /* width must even because of the YUYV format
925 height must be even because of interlacing */
Trent Piepho9bd0e8d2009-05-30 21:45:46 -0300926 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300927
Sri Deevie0d3baf2009-03-03 14:37:50 -0300928 f->fmt.pix.width = width;
929 f->fmt.pix.height = height;
930 f->fmt.pix.pixelformat = fmt->fourcc;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300931 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300932 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
933 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
934 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8b735c12013-02-09 06:35:02 -0300935 f->fmt.pix.priv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300936
937 return 0;
938}
939
940static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300941 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300942{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300943 struct cx231xx_fh *fh = priv;
944 struct cx231xx *dev = fh->dev;
945 int rc;
946 struct cx231xx_fmt *fmt;
Hans Verkuil112cb4a2010-05-09 10:11:01 -0300947 struct v4l2_mbus_framefmt mbus_fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300948
949 rc = check_dev(dev);
950 if (rc < 0)
951 return rc;
952
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300953 vidioc_try_fmt_vid_cap(file, priv, f);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300954
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300955 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300956 if (!fmt)
957 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300958
959 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
960 cx231xx_errdev("%s queue busy\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300961 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300962 }
963
964 if (dev->stream_on && !fh->stream_on) {
965 cx231xx_errdev("%s device in use by another fh\n", __func__);
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -0300966 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300967 }
968
969 /* set new image size */
970 dev->width = f->fmt.pix.width;
971 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300972 dev->format = fmt;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300973
Hans Verkuil112cb4a2010-05-09 10:11:01 -0300974 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
975 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
976 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300977
Sri Deevie0d3baf2009-03-03 14:37:50 -0300978 return rc;
979}
980
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300981static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300982{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300983 struct cx231xx_fh *fh = priv;
984 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300985
986 *id = dev->norm;
987 return 0;
988}
989
Sri Deevib1196122009-03-20 23:33:48 -0300990static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300991{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300992 struct cx231xx_fh *fh = priv;
993 struct cx231xx *dev = fh->dev;
Devin Heitmueller435b4f72010-07-09 13:29:31 -0300994 struct v4l2_mbus_framefmt mbus_fmt;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300995 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300996
997 rc = check_dev(dev);
998 if (rc < 0)
999 return rc;
1000
Hans Verkuild61072a2013-01-29 10:50:54 -03001001 if (dev->norm == *norm)
1002 return 0;
1003
1004 if (videobuf_queue_is_busy(&fh->vb_vidq))
1005 return -EBUSY;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001006
Sri Deevie0d3baf2009-03-03 14:37:50 -03001007 dev->norm = *norm;
1008
Sri Deevie0d3baf2009-03-03 14:37:50 -03001009 /* Adjusts width/height, if needed */
Hans Verkuild61072a2013-01-29 10:50:54 -03001010 dev->width = 720;
1011 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001012
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001013 call_all(dev, core, s_std, dev->norm);
1014
1015 /* We need to reset basic properties in the decoder related to
1016 resolution (since a standard change effects things like the number
1017 of lines in VACT, etc) */
Hans Verkuild61072a2013-01-29 10:50:54 -03001018 memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1019 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1020 mbus_fmt.width = dev->width;
1021 mbus_fmt.height = dev->height;
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001022 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001023
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001024 /* do mode control overrides */
1025 cx231xx_do_mode_ctrl_overrides(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001026
1027 return 0;
1028}
1029
1030static const char *iname[] = {
1031 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001032 [CX231XX_VMUX_SVIDEO] = "S-Video",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001033 [CX231XX_VMUX_TELEVISION] = "Television",
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001034 [CX231XX_VMUX_CABLE] = "Cable TV",
1035 [CX231XX_VMUX_DVB] = "DVB",
1036 [CX231XX_VMUX_DEBUG] = "for debug only",
Sri Deevie0d3baf2009-03-03 14:37:50 -03001037};
1038
1039static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001040 struct v4l2_input *i)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001041{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001042 struct cx231xx_fh *fh = priv;
1043 struct cx231xx *dev = fh->dev;
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001044 u32 gen_stat;
1045 unsigned int ret, n;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001046
1047 n = i->index;
1048 if (n >= MAX_CX231XX_INPUT)
1049 return -EINVAL;
1050 if (0 == INPUT(n)->type)
1051 return -EINVAL;
1052
1053 i->index = n;
1054 i->type = V4L2_INPUT_TYPE_CAMERA;
1055
1056 strcpy(i->name, iname[INPUT(n)->type]);
1057
1058 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001059 (CX231XX_VMUX_CABLE == INPUT(n)->type))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001060 i->type = V4L2_INPUT_TYPE_TUNER;
1061
1062 i->std = dev->vdev->tvnorms;
1063
Devin Heitmuellerde99d532011-07-24 17:07:07 -03001064 /* If they are asking about the active input, read signal status */
1065 if (n == dev->video_input) {
1066 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1067 GEN_STAT, 2, &gen_stat, 4);
1068 if (ret > 0) {
1069 if ((gen_stat & FLD_VPRES) == 0x00)
1070 i->status |= V4L2_IN_ST_NO_SIGNAL;
1071 if ((gen_stat & FLD_HLOCK) == 0x00)
1072 i->status |= V4L2_IN_ST_NO_H_LOCK;
1073 }
1074 }
1075
Sri Deevie0d3baf2009-03-03 14:37:50 -03001076 return 0;
1077}
1078
1079static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1080{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001081 struct cx231xx_fh *fh = priv;
1082 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001083
1084 *i = dev->video_input;
1085
1086 return 0;
1087}
1088
1089static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1090{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001091 struct cx231xx_fh *fh = priv;
1092 struct cx231xx *dev = fh->dev;
1093 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001094
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001095 dev->mode_tv = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001096 rc = check_dev(dev);
1097 if (rc < 0)
1098 return rc;
1099
1100 if (i >= MAX_CX231XX_INPUT)
1101 return -EINVAL;
1102 if (0 == INPUT(i)->type)
1103 return -EINVAL;
1104
Sri Deevie0d3baf2009-03-03 14:37:50 -03001105 video_mux(dev, i);
1106
Devin Heitmuellerc09d6692010-07-12 16:50:30 -03001107 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1108 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1109 /* There's a tuner, so reset the standard and put it on the
1110 last known frequency (since it was probably powered down
1111 until now */
1112 call_all(dev, core, s_std, dev->norm);
1113 }
1114
Sri Deevie0d3baf2009-03-03 14:37:50 -03001115 return 0;
1116}
1117
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001118static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001119{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001120 struct cx231xx_fh *fh = priv;
1121 struct cx231xx *dev = fh->dev;
1122 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001123
1124 rc = check_dev(dev);
1125 if (rc < 0)
1126 return rc;
1127
1128 if (0 != t->index)
1129 return -EINVAL;
1130
1131 strcpy(t->name, "Tuner");
1132
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001133 t->type = V4L2_TUNER_ANALOG_TV;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001134 t->capability = V4L2_TUNER_CAP_NORM;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001135 t->rangehigh = 0xffffffffUL;
1136 t->signal = 0xffff; /* LOCKED */
Hans Verkuilb251f952012-09-13 12:54:36 -03001137 call_all(dev, tuner, g_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001138
1139 return 0;
1140}
1141
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001142static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001143{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001144 struct cx231xx_fh *fh = priv;
1145 struct cx231xx *dev = fh->dev;
1146 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001147
1148 rc = check_dev(dev);
1149 if (rc < 0)
1150 return rc;
1151
1152 if (0 != t->index)
1153 return -EINVAL;
1154#if 0
Sri Deevib1196122009-03-20 23:33:48 -03001155 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001156#endif
1157 return 0;
1158}
1159
1160static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001161 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001162{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001163 struct cx231xx_fh *fh = priv;
1164 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001165
Hans Verkuilb251f952012-09-13 12:54:36 -03001166 if (f->tuner)
1167 return -EINVAL;
1168
Sri Deevie0d3baf2009-03-03 14:37:50 -03001169 f->frequency = dev->ctl_freq;
1170
Sri Deevie0d3baf2009-03-03 14:37:50 -03001171 return 0;
1172}
1173
1174static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001175 struct v4l2_frequency *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001176{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001177 struct cx231xx_fh *fh = priv;
1178 struct cx231xx *dev = fh->dev;
1179 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 Verkuil0752d982013-02-09 06:40:33 -03001197 call_all(dev, tuner, g_frequency, f);
1198 dev->ctl_freq = f->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 Verkuilfddd14c2012-09-13 05:37:11 -03001230static int vidioc_g_chip_ident(struct file *file, void *fh,
1231 struct v4l2_dbg_chip_ident *chip)
1232{
1233 chip->ident = V4L2_IDENT_NONE;
1234 chip->revision = 0;
1235 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1236 if (v4l2_chip_match_host(&chip->match))
1237 chip->ident = V4L2_IDENT_CX23100;
1238 return 0;
1239 }
1240 return -EINVAL;
1241}
1242
Sri Deevie0d3baf2009-03-03 14:37:50 -03001243#ifdef CONFIG_VIDEO_ADV_DEBUG
1244
Sri Deevie0d3baf2009-03-03 14:37:50 -03001245/*
Sri Deevib9255172009-03-04 17:49:01 -03001246 -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1247 chip=<chip>[,min=<addr>,max=<addr>]
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001248 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
Sri Deevib9255172009-03-04 17:49:01 -03001249 -r, --set-register=type=<host/i2cdrv/i2caddr>,
1250 chip=<chip>,reg=<addr>,val=<val>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001251 set the register [VIDIOC_DBG_S_REGISTER]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001252
1253 if type == host, then <chip> is the hosts chip ID (default 0)
1254 if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1255 if type == i2caddr, then <chip> is the 7-bit I2C address
1256*/
1257
Sri Deevie0d3baf2009-03-03 14:37:50 -03001258static int vidioc_g_register(struct file *file, void *priv,
1259 struct v4l2_dbg_register *reg)
1260{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001261 struct cx231xx_fh *fh = priv;
1262 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001263 int ret = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001264 u8 value[4] = { 0, 0, 0, 0 };
1265 u32 data = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001266
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001267 switch (reg->match.type) {
1268 case V4L2_CHIP_MATCH_HOST:
1269 switch (reg->match.addr) {
1270 case 0: /* Cx231xx - internal registers */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001271 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;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001275 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001276 case 1: /* AFE - read byte */
1277 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001278 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001279 reg->val = le32_to_cpu(data & 0xff);
1280 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001281 case 14: /* AFE - read dword */
1282 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001283 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001284 reg->val = le32_to_cpu(data);
1285 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001286 case 2: /* Video Block - read byte */
1287 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001288 (u16)reg->reg, 2, &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001289 reg->val = le32_to_cpu(data & 0xff);
1290 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001291 case 24: /* Video Block - read dword */
1292 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001293 (u16)reg->reg, 2, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001294 reg->val = le32_to_cpu(data);
1295 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001296 case 3: /* I2S block - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001297 ret = cx231xx_read_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001298 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001299 (u16)reg->reg, 1,
1300 &data, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001301 reg->val = le32_to_cpu(data & 0xff);
1302 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001303 case 34: /* I2S Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001304 ret =
Sri Deeviecc67d12009-03-21 22:00:20 -03001305 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001306 (u16)reg->reg, 1, &data, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001307 reg->val = le32_to_cpu(data);
1308 break;
1309 }
1310 return ret < 0 ? ret : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001311
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001312 case V4L2_CHIP_MATCH_I2C_DRIVER:
Sri Deevib1196122009-03-20 23:33:48 -03001313 call_all(dev, core, g_register, reg);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001314 return 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001315 case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
1316 switch (reg->match.addr) {
1317 case 0: /* Cx231xx - internal registers */
1318 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1319 (u16)reg->reg, value, 4);
1320 reg->val = value[0] | value[1] << 8 |
1321 value[2] << 16 | value[3] << 24;
1322
1323 break;
1324 case 0x600:/* AFE - read byte */
1325 ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
1326 (u16)reg->reg, 2,
1327 &data, 1 , 0);
1328 reg->val = le32_to_cpu(data & 0xff);
1329 break;
1330
1331 case 0x880:/* Video Block - read byte */
1332 if (reg->reg < 0x0b) {
1333 ret = cx231xx_read_i2c_master(dev,
1334 VID_BLK_I2C_ADDRESS,
1335 (u16)reg->reg, 2,
1336 &data, 1 , 0);
1337 reg->val = le32_to_cpu(data & 0xff);
1338 } else {
1339 ret = cx231xx_read_i2c_master(dev,
1340 VID_BLK_I2C_ADDRESS,
1341 (u16)reg->reg, 2,
1342 &data, 4 , 0);
1343 reg->val = le32_to_cpu(data);
1344 }
1345 break;
1346 case 0x980:
1347 ret = cx231xx_read_i2c_master(dev,
1348 I2S_BLK_DEVICE_ADDRESS,
1349 (u16)reg->reg, 1,
1350 &data, 1 , 0);
1351 reg->val = le32_to_cpu(data & 0xff);
1352 break;
1353 case 0x400:
1354 ret =
1355 cx231xx_read_i2c_master(dev, 0x40,
1356 (u16)reg->reg, 1,
1357 &data, 1 , 0);
1358 reg->val = le32_to_cpu(data & 0xff);
1359 break;
1360 case 0xc01:
1361 ret =
1362 cx231xx_read_i2c_master(dev, 0xc0,
1363 (u16)reg->reg, 2,
1364 &data, 38, 1);
1365 reg->val = le32_to_cpu(data);
1366 break;
1367 case 0x022:
1368 ret =
1369 cx231xx_read_i2c_master(dev, 0x02,
1370 (u16)reg->reg, 1,
1371 &data, 1, 2);
1372 reg->val = le32_to_cpu(data & 0xff);
1373 break;
1374 case 0x322:
1375 ret = cx231xx_read_i2c_master(dev,
1376 0x32,
1377 (u16)reg->reg, 1,
1378 &data, 4 , 2);
1379 reg->val = le32_to_cpu(data);
1380 break;
1381 case 0x342:
1382 ret = cx231xx_read_i2c_master(dev,
1383 0x34,
1384 (u16)reg->reg, 1,
1385 &data, 4 , 2);
1386 reg->val = le32_to_cpu(data);
1387 break;
1388
1389 default:
1390 cx231xx_info("no match device address!!\n");
1391 break;
1392 }
1393 return ret < 0 ? ret : 0;
1394 /*return -EINVAL;*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001395 default:
1396 if (!v4l2_chip_match_host(&reg->match))
1397 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001398 }
1399
Sri Deevib1196122009-03-20 23:33:48 -03001400 call_all(dev, core, g_register, reg);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001401
1402 return ret;
1403}
1404
1405static int vidioc_s_register(struct file *file, void *priv,
1406 struct v4l2_dbg_register *reg)
1407{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001408 struct cx231xx_fh *fh = priv;
1409 struct cx231xx *dev = fh->dev;
1410 int ret = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001411 __le64 buf;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001412 u32 value;
1413 u8 data[4] = { 0, 0, 0, 0 };
Sri Deevie0d3baf2009-03-03 14:37:50 -03001414
1415 buf = cpu_to_le64(reg->val);
1416
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001417 switch (reg->match.type) {
1418 case V4L2_CHIP_MATCH_HOST:
1419 {
1420 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001421
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001422 switch (reg->match.addr) {
1423 case 0: /* cx231xx internal registers */
1424 data[0] = (u8) value;
1425 data[1] = (u8) (value >> 8);
1426 data[2] = (u8) (value >> 16);
1427 data[3] = (u8) (value >> 24);
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001428 ret = cx231xx_write_ctrl_reg(dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001429 VRT_SET_REGISTER,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001430 (u16)reg->reg, data,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001431 4);
1432 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001433 case 1: /* AFE - read byte */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001434 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001435 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001436 (u16)reg->reg, 2,
1437 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001438 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001439 case 14: /* AFE - read dword */
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001440 ret = cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001441 AFE_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001442 (u16)reg->reg, 2,
1443 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001444 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001445 case 2: /* Video Block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001446 ret =
1447 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001448 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001449 (u16)reg->reg, 2,
1450 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001451 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001452 case 24: /* Video Block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001453 ret =
1454 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001455 VID_BLK_I2C_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001456 (u16)reg->reg, 2,
1457 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001458 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001459 case 3: /* I2S block - read byte */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001460 ret =
1461 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001462 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001463 (u16)reg->reg, 1,
1464 value, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001465 break;
Sri Deeviecc67d12009-03-21 22:00:20 -03001466 case 34: /* I2S block - read dword */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001467 ret =
1468 cx231xx_write_i2c_data(dev,
Sri Deeviecc67d12009-03-21 22:00:20 -03001469 I2S_BLK_DEVICE_ADDRESS,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001470 (u16)reg->reg, 1,
1471 value, 4);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001472 break;
1473 }
1474 }
1475 return ret < 0 ? ret : 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001476 case V4L2_CHIP_MATCH_I2C_ADDR:
1477 {
1478 value = (u32) buf & 0xffffffff;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001479
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001480 switch (reg->match.addr) {
1481 case 0:/*cx231xx internal registers*/
1482 data[0] = (u8) value;
1483 data[1] = (u8) (value >> 8);
1484 data[2] = (u8) (value >> 16);
1485 data[3] = (u8) (value >> 24);
1486 ret = cx231xx_write_ctrl_reg(dev,
1487 VRT_SET_REGISTER,
1488 (u16)reg->reg, data,
1489 4);
1490 break;
1491 case 0x600:/* AFE - read byte */
1492 ret = cx231xx_write_i2c_master(dev,
1493 AFE_DEVICE_ADDRESS,
1494 (u16)reg->reg, 2,
1495 value, 1 , 0);
1496 break;
1497
1498 case 0x880:/* Video Block - read byte */
1499 if (reg->reg < 0x0b)
1500 cx231xx_write_i2c_master(dev,
1501 VID_BLK_I2C_ADDRESS,
1502 (u16)reg->reg, 2,
1503 value, 1, 0);
1504 else
1505 cx231xx_write_i2c_master(dev,
1506 VID_BLK_I2C_ADDRESS,
1507 (u16)reg->reg, 2,
1508 value, 4, 0);
1509 break;
1510 case 0x980:
1511 ret =
1512 cx231xx_write_i2c_master(dev,
1513 I2S_BLK_DEVICE_ADDRESS,
1514 (u16)reg->reg, 1,
1515 value, 1, 0);
1516 break;
1517 case 0x400:
1518 ret =
1519 cx231xx_write_i2c_master(dev,
1520 0x40,
1521 (u16)reg->reg, 1,
1522 value, 1, 0);
1523 break;
1524 case 0xc01:
1525 ret =
1526 cx231xx_write_i2c_master(dev,
1527 0xc0,
1528 (u16)reg->reg, 1,
1529 value, 1, 1);
1530 break;
1531
1532 case 0x022:
1533 ret =
1534 cx231xx_write_i2c_master(dev,
1535 0x02,
1536 (u16)reg->reg, 1,
1537 value, 1, 2);
Dan Carpenterf62436a2013-01-11 02:48:41 -03001538 break;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001539 case 0x322:
1540 ret =
1541 cx231xx_write_i2c_master(dev,
1542 0x32,
1543 (u16)reg->reg, 1,
1544 value, 4, 2);
1545 break;
1546
1547 case 0x342:
1548 ret =
1549 cx231xx_write_i2c_master(dev,
1550 0x34,
1551 (u16)reg->reg, 1,
1552 value, 4, 2);
1553 break;
1554 default:
1555 cx231xx_info("no match device address, "
1556 "the value is %x\n", reg->match.addr);
1557 break;
1558
1559 }
1560
1561 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001562 default:
1563 break;
1564 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001565
Sri Deevib1196122009-03-20 23:33:48 -03001566 call_all(dev, core, s_register, reg);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001567
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001568 return ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001569}
1570#endif
1571
Sri Deevie0d3baf2009-03-03 14:37:50 -03001572static int vidioc_cropcap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001573 struct v4l2_cropcap *cc)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001574{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001575 struct cx231xx_fh *fh = priv;
1576 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001577
1578 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1579 return -EINVAL;
1580
1581 cc->bounds.left = 0;
1582 cc->bounds.top = 0;
1583 cc->bounds.width = dev->width;
1584 cc->bounds.height = dev->height;
1585 cc->defrect = cc->bounds;
1586 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1587 cc->pixelaspect.denominator = 59;
1588
1589 return 0;
1590}
1591
1592static int vidioc_streamon(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001593 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001594{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001595 struct cx231xx_fh *fh = priv;
1596 struct cx231xx *dev = fh->dev;
1597 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001598
1599 rc = check_dev(dev);
1600 if (rc < 0)
1601 return rc;
1602
Sri Deevie0d3baf2009-03-03 14:37:50 -03001603 rc = res_get(fh);
1604
1605 if (likely(rc >= 0))
1606 rc = videobuf_streamon(&fh->vb_vidq);
1607
Sri Deevib1196122009-03-20 23:33:48 -03001608 call_all(dev, video, s_stream, 1);
1609
Sri Deevie0d3baf2009-03-03 14:37:50 -03001610 return rc;
1611}
1612
1613static int vidioc_streamoff(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001614 enum v4l2_buf_type type)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001615{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001616 struct cx231xx_fh *fh = priv;
1617 struct cx231xx *dev = fh->dev;
1618 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001619
1620 rc = check_dev(dev);
1621 if (rc < 0)
1622 return rc;
1623
Sri Deevie0d3baf2009-03-03 14:37:50 -03001624 if (type != fh->type)
1625 return -EINVAL;
1626
Sri Deevib1196122009-03-20 23:33:48 -03001627 cx25840_call(dev, video, s_stream, 0);
1628
Sri Deevie0d3baf2009-03-03 14:37:50 -03001629 videobuf_streamoff(&fh->vb_vidq);
1630 res_free(fh);
1631
Sri Deevie0d3baf2009-03-03 14:37:50 -03001632 return 0;
1633}
1634
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001635static int vidioc_querycap(struct file *file, void *priv,
1636 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001637{
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001638 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001639 struct cx231xx_fh *fh = priv;
1640 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001641
1642 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1643 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab2c6beca2009-03-22 08:53:36 -03001644 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001645
Hans Verkuil530e01e2013-01-29 12:32:20 -03001646 if (vdev->vfl_type == VFL_TYPE_RADIO)
1647 cap->device_caps = V4L2_CAP_RADIO;
1648 else {
Hans Verkuil06c46002012-09-13 06:17:47 -03001649 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Hans Verkuil530e01e2013-01-29 12:32:20 -03001650 if (vdev->vfl_type == VFL_TYPE_VBI)
1651 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1652 else
1653 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1654 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001655 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001656 cap->device_caps |= V4L2_CAP_TUNER;
Hans Verkuil06c46002012-09-13 06:17:47 -03001657 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001658 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
Hans Verkuil530e01e2013-01-29 12:32:20 -03001659 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1660 if (dev->radio_dev)
1661 cap->capabilities |= V4L2_CAP_RADIO;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001662
1663 return 0;
1664}
1665
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001666static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1667 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001668{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001669 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001670 return -EINVAL;
1671
1672 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1673 f->pixelformat = format[f->index].fourcc;
1674
1675 return 0;
1676}
1677
Sri Deevie0d3baf2009-03-03 14:37:50 -03001678/* RAW VBI ioctls */
1679
1680static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001681 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001682{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001683 struct cx231xx_fh *fh = priv;
1684 struct cx231xx *dev = fh->dev;
Hans Verkuil62647222013-02-09 06:41:11 -03001685
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001686 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001687 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1688 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001689 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001690 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001691 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001692 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001693 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001694 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001695 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001696 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001697 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001698
1699 return 0;
1700
1701}
1702
1703static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001704 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001705{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001706 struct cx231xx_fh *fh = priv;
1707 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001708
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001709 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001710 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1711 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001712 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001713 f->fmt.vbi.flags = 0;
1714 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001715 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001716 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001717 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001718 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001719 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001720 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001721 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001722
1723 return 0;
1724
1725}
1726
Hans Verkuil62647222013-02-09 06:41:11 -03001727static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1728 struct v4l2_format *f)
1729{
1730 struct cx231xx_fh *fh = priv;
1731 struct cx231xx *dev = fh->dev;
1732
1733 if (dev->vbi_stream_on && !fh->stream_on) {
1734 cx231xx_errdev("%s device in use by another fh\n", __func__);
1735 return -EBUSY;
1736 }
1737 return vidioc_try_fmt_vbi_cap(file, priv, f);
1738}
1739
Sri Deevie0d3baf2009-03-03 14:37:50 -03001740static int vidioc_reqbufs(struct file *file, void *priv,
1741 struct v4l2_requestbuffers *rb)
1742{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001743 struct cx231xx_fh *fh = priv;
1744 struct cx231xx *dev = fh->dev;
1745 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001746
1747 rc = check_dev(dev);
1748 if (rc < 0)
1749 return rc;
1750
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001751 return videobuf_reqbufs(&fh->vb_vidq, rb);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001752}
1753
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001754static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001755{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001756 struct cx231xx_fh *fh = priv;
1757 struct cx231xx *dev = fh->dev;
1758 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001759
1760 rc = check_dev(dev);
1761 if (rc < 0)
1762 return rc;
1763
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001764 return videobuf_querybuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001765}
1766
1767static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1768{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001769 struct cx231xx_fh *fh = priv;
1770 struct cx231xx *dev = fh->dev;
1771 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001772
1773 rc = check_dev(dev);
1774 if (rc < 0)
1775 return rc;
1776
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001777 return videobuf_qbuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001778}
1779
1780static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1781{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001782 struct cx231xx_fh *fh = priv;
1783 struct cx231xx *dev = fh->dev;
1784 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001785
1786 rc = check_dev(dev);
1787 if (rc < 0)
1788 return rc;
1789
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001790 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001791}
1792
Sri Deevie0d3baf2009-03-03 14:37:50 -03001793/* ----------------------------------------------------------- */
1794/* RADIO ESPECIFIC IOCTLS */
1795/* ----------------------------------------------------------- */
1796
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001797static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001798{
1799 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1800
Hans Verkuil530e01e2013-01-29 12:32:20 -03001801 if (t->index)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001802 return -EINVAL;
1803
1804 strcpy(t->name, "Radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001805
Hans Verkuil530e01e2013-01-29 12:32:20 -03001806 call_all(dev, tuner, g_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001807
1808 return 0;
1809}
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001810static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001811{
1812 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1813
1814 if (0 != t->index)
1815 return -EINVAL;
1816
Sri Deevib1196122009-03-20 23:33:48 -03001817 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001818
1819 return 0;
1820}
1821
Sri Deevie0d3baf2009-03-03 14:37:50 -03001822/*
1823 * cx231xx_v4l2_open()
1824 * inits the device and starts isoc transfer
1825 */
1826static int cx231xx_v4l2_open(struct file *filp)
1827{
Sri Deevie0d3baf2009-03-03 14:37:50 -03001828 int errCode = 0, radio = 0;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001829 struct video_device *vdev = video_devdata(filp);
1830 struct cx231xx *dev = video_drvdata(filp);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001831 struct cx231xx_fh *fh;
1832 enum v4l2_buf_type fh_type = 0;
1833
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001834 switch (vdev->vfl_type) {
1835 case VFL_TYPE_GRABBER:
1836 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1837 break;
1838 case VFL_TYPE_VBI:
1839 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1840 break;
1841 case VFL_TYPE_RADIO:
1842 radio = 1;
1843 break;
1844 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001845
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001846 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1847 video_device_node_name(vdev), v4l2_type_names[fh_type],
1848 dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001849
1850#if 0
1851 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1852 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001853 cx231xx_errdev
1854 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001855 return -EBUSY;
1856 }
1857#endif
1858
1859 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1860 if (!fh) {
1861 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001862 return -ENOMEM;
1863 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001864 if (mutex_lock_interruptible(&dev->lock)) {
1865 kfree(fh);
1866 return -ERESTARTSYS;
1867 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001868 fh->dev = dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001869 fh->type = fh_type;
1870 filp->private_data = fh;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001871 v4l2_fh_init(&fh->fh, vdev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001872
1873 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1874 dev->width = norm_maxw(dev);
1875 dev->height = norm_maxh(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001876
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001877 /* Power up in Analog TV mode */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001878 if (dev->board.external_av)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001879 cx231xx_set_power_mode(dev,
1880 POLARIS_AVMODE_ENXTERNAL_AV);
1881 else
1882 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001883
1884#if 0
1885 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1886#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -03001887
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001888 /* set video alternate setting */
1889 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001890
1891 /* Needed, since GPIO might have disabled power of
1892 some i2c device */
1893 cx231xx_config_i2c(dev);
1894
1895 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001896 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001897
1898 }
Hans Verkuil71590762013-01-28 12:57:47 -03001899 if (radio) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03001900 cx231xx_videodbg("video_open: setting radio device\n");
1901
1902 /* cx231xx_start_radio(dev); */
1903
Sri Deevib1196122009-03-20 23:33:48 -03001904 call_all(dev, tuner, s_radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001905 }
1906
1907 dev->users++;
1908
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001909 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1910 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1911 NULL, &dev->video_mode.slock,
1912 fh->type, V4L2_FIELD_INTERLACED,
Hans Verkuil08bff032010-09-20 17:39:46 -03001913 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001914 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001915 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001916 /* Set the required alternate setting VBI interface works in
1917 Bulk mode only */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001918 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001919
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001920 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1921 NULL, &dev->vbi_mode.slock,
1922 fh->type, V4L2_FIELD_SEQ_TB,
Hans Verkuil08bff032010-09-20 17:39:46 -03001923 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001924 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001925 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001926 mutex_unlock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001927 v4l2_fh_add(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001928
Sri Deevie0d3baf2009-03-03 14:37:50 -03001929 return errCode;
1930}
1931
1932/*
1933 * cx231xx_realease_resources()
1934 * unregisters the v4l2,i2c and usb devices
1935 * called when the device gets disconected or at module unload
1936*/
1937void cx231xx_release_analog_resources(struct cx231xx *dev)
1938{
1939
1940 /*FIXME: I2C IR should be disconnected */
1941
1942 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001943 if (video_is_registered(dev->radio_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001944 video_unregister_device(dev->radio_dev);
1945 else
1946 video_device_release(dev->radio_dev);
1947 dev->radio_dev = NULL;
1948 }
1949 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001950 cx231xx_info("V4L2 device %s deregistered\n",
1951 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001952 if (video_is_registered(dev->vbi_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001953 video_unregister_device(dev->vbi_dev);
1954 else
1955 video_device_release(dev->vbi_dev);
1956 dev->vbi_dev = NULL;
1957 }
1958 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001959 cx231xx_info("V4L2 device %s deregistered\n",
1960 video_device_node_name(dev->vdev));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001961
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001962 if (dev->board.has_417)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001963 cx231xx_417_unregister(dev);
1964
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001965 if (video_is_registered(dev->vdev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001966 video_unregister_device(dev->vdev);
1967 else
1968 video_device_release(dev->vdev);
1969 dev->vdev = NULL;
1970 }
Hans Verkuild2370f82012-09-17 07:22:09 -03001971 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1972 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001973}
1974
1975/*
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001976 * cx231xx_close()
Sri Deevie0d3baf2009-03-03 14:37:50 -03001977 * stops streaming and deallocates all resources allocated by the v4l2
1978 * calls and ioctls
1979 */
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001980static int cx231xx_close(struct file *filp)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001981{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001982 struct cx231xx_fh *fh = filp->private_data;
1983 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001984
1985 cx231xx_videodbg("users=%d\n", dev->users);
1986
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001987 cx231xx_videodbg("users=%d\n", dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001988 if (res_check(fh))
1989 res_free(fh);
1990
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001991 /*
1992 * To workaround error number=-71 on EP0 for VideoGrabber,
1993 * need exclude following.
1994 * FIXME: It is probably safe to remove most of these, as we're
1995 * now avoiding the alternate setting for INDEX_VANC
1996 */
1997 if (!dev->board.no_alt_vanc)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001998 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1999 videobuf_stop(&fh->vb_vidq);
2000 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002001
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002002 /* the device is already disconnect,
2003 free the remaining resources */
2004 if (dev->state & DEV_DISCONNECTED) {
2005 if (atomic_read(&dev->devlist_count) > 0) {
2006 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002007 fh->dev = NULL;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002008 return 0;
2009 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002010 return 0;
2011 }
2012
2013 /* do this before setting alternate! */
2014 cx231xx_uninit_vbi_isoc(dev);
2015
2016 /* set alternate 0 */
2017 if (!dev->vbi_or_sliced_cc_mode)
2018 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2019 else
2020 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2021
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002022 v4l2_fh_del(&fh->fh);
2023 v4l2_fh_exit(&fh->fh);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002024 kfree(fh);
2025 dev->users--;
2026 wake_up_interruptible_nr(&dev->open, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002027 return 0;
2028 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002029
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002030 v4l2_fh_del(&fh->fh);
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -02002031 dev->users--;
2032 if (!dev->users) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002033 videobuf_stop(&fh->vb_vidq);
2034 videobuf_mmap_free(&fh->vb_vidq);
2035
2036 /* the device is already disconnect,
2037 free the remaining resources */
2038 if (dev->state & DEV_DISCONNECTED) {
2039 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002040 fh->dev = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002041 return 0;
2042 }
2043
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002044 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002045 call_all(dev, core, s_power, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002046
2047 /* do this before setting alternate! */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002048 if (dev->USE_ISO)
2049 cx231xx_uninit_isoc(dev);
2050 else
2051 cx231xx_uninit_bulk(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002052 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2053
2054 /* set alternate 0 */
2055 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2056 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002057 v4l2_fh_exit(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002058 kfree(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002059 wake_up_interruptible_nr(&dev->open, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002060 return 0;
2061}
2062
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002063static int cx231xx_v4l2_close(struct file *filp)
2064{
2065 struct cx231xx_fh *fh = filp->private_data;
2066 struct cx231xx *dev = fh->dev;
2067 int rc;
2068
2069 mutex_lock(&dev->lock);
2070 rc = cx231xx_close(filp);
2071 mutex_unlock(&dev->lock);
2072 return rc;
2073}
2074
Sri Deevie0d3baf2009-03-03 14:37:50 -03002075/*
2076 * cx231xx_v4l2_read()
2077 * will allocate buffers when called for the first time
2078 */
2079static ssize_t
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002080cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2081 loff_t *pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002082{
2083 struct cx231xx_fh *fh = filp->private_data;
2084 struct cx231xx *dev = fh->dev;
2085 int rc;
2086
2087 rc = check_dev(dev);
2088 if (rc < 0)
2089 return rc;
2090
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002091 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2092 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002093 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002094
2095 if (unlikely(rc < 0))
2096 return rc;
2097
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002098 if (mutex_lock_interruptible(&dev->lock))
2099 return -ERESTARTSYS;
2100 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002101 filp->f_flags & O_NONBLOCK);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002102 mutex_unlock(&dev->lock);
2103 return rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002104 }
2105 return 0;
2106}
2107
2108/*
2109 * cx231xx_v4l2_poll()
2110 * will allocate buffers when called for the first time
2111 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002112static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002113{
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002114 unsigned long req_events = poll_requested_events(wait);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002115 struct cx231xx_fh *fh = filp->private_data;
2116 struct cx231xx *dev = fh->dev;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002117 unsigned res = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002118 int rc;
2119
2120 rc = check_dev(dev);
2121 if (rc < 0)
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002122 return POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002123
Sri Deevie0d3baf2009-03-03 14:37:50 -03002124 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002125
2126 if (unlikely(rc < 0))
2127 return POLLERR;
2128
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002129 if (v4l2_event_pending(&fh->fh))
2130 res |= POLLPRI;
2131 else
2132 poll_wait(filp, &fh->fh.wait, wait);
2133
2134 if (!(req_events & (POLLIN | POLLRDNORM)))
2135 return res;
2136
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002137 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002138 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002139 mutex_lock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002140 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002141 mutex_unlock(&dev->lock);
2142 return res;
2143 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002144 return res | POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002145}
2146
2147/*
2148 * cx231xx_v4l2_mmap()
2149 */
2150static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2151{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002152 struct cx231xx_fh *fh = filp->private_data;
2153 struct cx231xx *dev = fh->dev;
2154 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002155
2156 rc = check_dev(dev);
2157 if (rc < 0)
2158 return rc;
2159
Sri Deevie0d3baf2009-03-03 14:37:50 -03002160 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002161
2162 if (unlikely(rc < 0))
2163 return rc;
2164
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002165 if (mutex_lock_interruptible(&dev->lock))
2166 return -ERESTARTSYS;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002167 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002168 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002169
2170 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002171 (unsigned long)vma->vm_start,
2172 (unsigned long)vma->vm_end -
2173 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002174
2175 return rc;
2176}
2177
2178static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002179 .owner = THIS_MODULE,
2180 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002181 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002182 .read = cx231xx_v4l2_read,
2183 .poll = cx231xx_v4l2_poll,
2184 .mmap = cx231xx_v4l2_mmap,
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002185 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002186};
2187
2188static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002189 .vidioc_querycap = vidioc_querycap,
2190 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2191 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2192 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2193 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2194 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2195 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
Hans Verkuil62647222013-02-09 06:41:11 -03002196 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002197 .vidioc_cropcap = vidioc_cropcap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002198 .vidioc_reqbufs = vidioc_reqbufs,
2199 .vidioc_querybuf = vidioc_querybuf,
2200 .vidioc_qbuf = vidioc_qbuf,
2201 .vidioc_dqbuf = vidioc_dqbuf,
2202 .vidioc_s_std = vidioc_s_std,
2203 .vidioc_g_std = vidioc_g_std,
2204 .vidioc_enum_input = vidioc_enum_input,
2205 .vidioc_g_input = vidioc_g_input,
2206 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002207 .vidioc_streamon = vidioc_streamon,
2208 .vidioc_streamoff = vidioc_streamoff,
2209 .vidioc_g_tuner = vidioc_g_tuner,
2210 .vidioc_s_tuner = vidioc_s_tuner,
2211 .vidioc_g_frequency = vidioc_g_frequency,
2212 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilfddd14c2012-09-13 05:37:11 -03002213 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002214#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002215 .vidioc_g_register = vidioc_g_register,
2216 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002217#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002218 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2219 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002220};
2221
2222static struct video_device cx231xx_vbi_template;
2223
2224static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002225 .fops = &cx231xx_v4l_fops,
2226 .release = video_device_release,
2227 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002228 .tvnorms = V4L2_STD_ALL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002229};
2230
2231static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002232 .owner = THIS_MODULE,
2233 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002234 .release = cx231xx_v4l2_close,
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002235 .poll = v4l2_ctrl_poll,
Hans Verkuil1265f082012-09-17 09:26:46 -03002236 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002237};
2238
2239static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil530e01e2013-01-29 12:32:20 -03002240 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002241 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002242 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002243 .vidioc_g_frequency = vidioc_g_frequency,
2244 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil530e01e2013-01-29 12:32:20 -03002245 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002246#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002247 .vidioc_g_register = vidioc_g_register,
2248 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002249#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002250 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2251 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002252};
2253
2254static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002255 .name = "cx231xx-radio",
2256 .fops = &radio_fops,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002257 .ioctl_ops = &radio_ioctl_ops,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002258};
2259
2260/******************************** usb interface ******************************/
2261
Sri Deevib9255172009-03-04 17:49:01 -03002262static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2263 const struct video_device
2264 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002265{
2266 struct video_device *vfd;
2267
2268 vfd = video_device_alloc();
2269 if (NULL == vfd)
2270 return NULL;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002271
Sri Deevie0d3baf2009-03-03 14:37:50 -03002272 *vfd = *template;
Sri Deevib1196122009-03-20 23:33:48 -03002273 vfd->v4l2_dev = &dev->v4l2_dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002274 vfd->release = video_device_release;
2275 vfd->debug = video_debug;
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002276 vfd->lock = &dev->lock;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002277 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002278
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002279 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002280
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002281 video_set_drvdata(vfd, dev);
Hans Verkuil06c46002012-09-13 06:17:47 -03002282 if (dev->tuner_type == TUNER_ABSENT) {
2283 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2284 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2285 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2286 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2287 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002288 return vfd;
2289}
2290
2291int cx231xx_register_analog_devices(struct cx231xx *dev)
2292{
2293 int ret;
2294
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002295 cx231xx_info("%s: v4l2 driver version %s\n",
2296 dev->name, CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002297
2298 /* set default norm */
Hans Verkuila25a70122012-09-17 08:30:07 -03002299 dev->norm = V4L2_STD_PAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002300 dev->width = norm_maxw(dev);
2301 dev->height = norm_maxh(dev);
2302 dev->interlaced = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002303
2304 /* Analog specific initialization */
2305 dev->format = &format[0];
Devin Heitmueller6e6a2ba2010-07-12 15:34:57 -03002306
2307 /* Set the initial input */
2308 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002309
Hans Verkuild61072a2013-01-29 10:50:54 -03002310 call_all(dev, core, s_std, dev->norm);
2311
Hans Verkuild2370f82012-09-17 07:22:09 -03002312 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2313 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2314
2315 if (dev->sd_cx25840) {
2316 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2317 dev->sd_cx25840->ctrl_handler, NULL);
2318 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2319 dev->sd_cx25840->ctrl_handler,
2320 v4l2_ctrl_radio_filter);
2321 }
2322
2323 if (dev->ctrl_handler.error)
2324 return dev->ctrl_handler.error;
2325 if (dev->radio_ctrl_handler.error)
2326 return dev->radio_ctrl_handler.error;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002327
2328 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002329 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002330
2331 /* allocate and fill video video_device struct */
2332 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2333 if (!dev->vdev) {
2334 cx231xx_errdev("cannot allocate video_device.\n");
2335 return -ENODEV;
2336 }
2337
Hans Verkuild2370f82012-09-17 07:22:09 -03002338 dev->vdev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002339 /* register v4l2 video video_device */
2340 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002341 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002342 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002343 cx231xx_errdev("unable to register video device (error=%i).\n",
2344 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002345 return ret;
2346 }
2347
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002348 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2349 dev->name, video_device_node_name(dev->vdev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002350
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002351 /* Initialize VBI template */
Ezequiel Garcia3724dde2012-10-23 15:57:05 -03002352 cx231xx_vbi_template = cx231xx_video_template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002353 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002354
2355 /* Allocate and fill vbi video_device struct */
2356 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2357
Hans Verkuild2370f82012-09-17 07:22:09 -03002358 if (!dev->vbi_dev) {
2359 cx231xx_errdev("cannot allocate video_device.\n");
2360 return -ENODEV;
2361 }
2362 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002363 /* register v4l2 vbi video_device */
2364 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002365 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002366 if (ret < 0) {
2367 cx231xx_errdev("unable to register vbi device\n");
2368 return ret;
2369 }
2370
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002371 cx231xx_info("%s/0: registered device %s\n",
2372 dev->name, video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002373
2374 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002375 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2376 "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002377 if (!dev->radio_dev) {
2378 cx231xx_errdev("cannot allocate video_device.\n");
2379 return -ENODEV;
2380 }
Hans Verkuild2370f82012-09-17 07:22:09 -03002381 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002382 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2383 radio_nr[dev->devno]);
2384 if (ret < 0) {
2385 cx231xx_errdev("can't register radio device\n");
2386 return ret;
2387 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002388 cx231xx_info("Registered radio device as %s\n",
2389 video_device_node_name(dev->radio_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002390 }
2391
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002392 cx231xx_info("V4L2 device registered as %s and %s\n",
2393 video_device_node_name(dev->vdev),
2394 video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002395
2396 return 0;
2397}