blob: ae27c8287d21fd00e0ed72d7de879ce5eaf4af84 [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;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300995 struct v4l2_format f;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300996 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300997
998 rc = check_dev(dev);
999 if (rc < 0)
1000 return rc;
1001
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001002 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001003
Sri Deevie0d3baf2009-03-03 14:37:50 -03001004 dev->norm = *norm;
1005
Sri Deevie0d3baf2009-03-03 14:37:50 -03001006 /* Adjusts width/height, if needed */
1007 f.fmt.pix.width = dev->width;
1008 f.fmt.pix.height = dev->height;
1009 vidioc_try_fmt_vid_cap(file, priv, &f);
1010
Devin Heitmueller435b4f72010-07-09 13:29:31 -03001011 call_all(dev, core, s_std, dev->norm);
1012
1013 /* We need to reset basic properties in the decoder related to
1014 resolution (since a standard change effects things like the number
1015 of lines in VACT, etc) */
1016 v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
1017 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1018 v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
1019
Sri Deevie0d3baf2009-03-03 14:37:50 -03001020 /* set new image size */
1021 dev->width = f.fmt.pix.width;
1022 dev->height = f.fmt.pix.height;
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
Mauro Carvalho Chehab92fcbd32009-03-21 22:16:34 -03001624 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001625 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001626 return -EINVAL;
1627 if (type != fh->type)
1628 return -EINVAL;
1629
Sri Deevib1196122009-03-20 23:33:48 -03001630 cx25840_call(dev, video, s_stream, 0);
1631
Sri Deevie0d3baf2009-03-03 14:37:50 -03001632 videobuf_streamoff(&fh->vb_vidq);
1633 res_free(fh);
1634
Sri Deevie0d3baf2009-03-03 14:37:50 -03001635 return 0;
1636}
1637
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001638static int vidioc_querycap(struct file *file, void *priv,
1639 struct v4l2_capability *cap)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001640{
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001641 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001642 struct cx231xx_fh *fh = priv;
1643 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001644
1645 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1646 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
Mauro Carvalho Chehab2c6beca2009-03-22 08:53:36 -03001647 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001648
Hans Verkuil530e01e2013-01-29 12:32:20 -03001649 if (vdev->vfl_type == VFL_TYPE_RADIO)
1650 cap->device_caps = V4L2_CAP_RADIO;
1651 else {
Hans Verkuil06c46002012-09-13 06:17:47 -03001652 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Hans Verkuil530e01e2013-01-29 12:32:20 -03001653 if (vdev->vfl_type == VFL_TYPE_VBI)
1654 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1655 else
1656 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1657 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001658 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001659 cap->device_caps |= V4L2_CAP_TUNER;
Hans Verkuil06c46002012-09-13 06:17:47 -03001660 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
Hans Verkuil4bc837d2012-09-13 05:29:12 -03001661 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
Hans Verkuil530e01e2013-01-29 12:32:20 -03001662 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1663 if (dev->radio_dev)
1664 cap->capabilities |= V4L2_CAP_RADIO;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001665
1666 return 0;
1667}
1668
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001669static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1670 struct v4l2_fmtdesc *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001671{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001672 if (unlikely(f->index >= ARRAY_SIZE(format)))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001673 return -EINVAL;
1674
1675 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1676 f->pixelformat = format[f->index].fourcc;
1677
1678 return 0;
1679}
1680
Sri Deevie0d3baf2009-03-03 14:37:50 -03001681/* RAW VBI ioctls */
1682
1683static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001684 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001685{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001686 struct cx231xx_fh *fh = priv;
1687 struct cx231xx *dev = fh->dev;
Hans Verkuil62647222013-02-09 06:41:11 -03001688
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001689 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001690 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1691 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001692 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001693 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001694 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001695 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001696 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001697 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001698 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001699 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001700 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001701
1702 return 0;
1703
1704}
1705
1706static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001707 struct v4l2_format *f)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001708{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001709 struct cx231xx_fh *fh = priv;
1710 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001711
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001712 f->fmt.vbi.sampling_rate = 6750000 * 4;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001713 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1714 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
Devin Heitmuelleradc03562010-07-08 13:12:47 -03001715 f->fmt.vbi.offset = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001716 f->fmt.vbi.flags = 0;
1717 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001718 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001719 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001720 PAL_VBI_LINES : NTSC_VBI_LINES;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001721 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001722 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001723 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
Hans Verkuil62647222013-02-09 06:41:11 -03001724 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
Sri Deevie0d3baf2009-03-03 14:37:50 -03001725
1726 return 0;
1727
1728}
1729
Hans Verkuil62647222013-02-09 06:41:11 -03001730static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1731 struct v4l2_format *f)
1732{
1733 struct cx231xx_fh *fh = priv;
1734 struct cx231xx *dev = fh->dev;
1735
1736 if (dev->vbi_stream_on && !fh->stream_on) {
1737 cx231xx_errdev("%s device in use by another fh\n", __func__);
1738 return -EBUSY;
1739 }
1740 return vidioc_try_fmt_vbi_cap(file, priv, f);
1741}
1742
Sri Deevie0d3baf2009-03-03 14:37:50 -03001743static int vidioc_reqbufs(struct file *file, void *priv,
1744 struct v4l2_requestbuffers *rb)
1745{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001746 struct cx231xx_fh *fh = priv;
1747 struct cx231xx *dev = fh->dev;
1748 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001749
1750 rc = check_dev(dev);
1751 if (rc < 0)
1752 return rc;
1753
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001754 return videobuf_reqbufs(&fh->vb_vidq, rb);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001755}
1756
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001757static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001758{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001759 struct cx231xx_fh *fh = priv;
1760 struct cx231xx *dev = fh->dev;
1761 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001762
1763 rc = check_dev(dev);
1764 if (rc < 0)
1765 return rc;
1766
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001767 return videobuf_querybuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001768}
1769
1770static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1771{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001772 struct cx231xx_fh *fh = priv;
1773 struct cx231xx *dev = fh->dev;
1774 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001775
1776 rc = check_dev(dev);
1777 if (rc < 0)
1778 return rc;
1779
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001780 return videobuf_qbuf(&fh->vb_vidq, b);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001781}
1782
1783static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1784{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001785 struct cx231xx_fh *fh = priv;
1786 struct cx231xx *dev = fh->dev;
1787 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001788
1789 rc = check_dev(dev);
1790 if (rc < 0)
1791 return rc;
1792
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001793 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001794}
1795
Sri Deevie0d3baf2009-03-03 14:37:50 -03001796/* ----------------------------------------------------------- */
1797/* RADIO ESPECIFIC IOCTLS */
1798/* ----------------------------------------------------------- */
1799
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001800static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001801{
1802 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1803
Hans Verkuil530e01e2013-01-29 12:32:20 -03001804 if (t->index)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001805 return -EINVAL;
1806
1807 strcpy(t->name, "Radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001808
Hans Verkuil530e01e2013-01-29 12:32:20 -03001809 call_all(dev, tuner, g_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001810
1811 return 0;
1812}
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001813static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001814{
1815 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1816
1817 if (0 != t->index)
1818 return -EINVAL;
1819
Sri Deevib1196122009-03-20 23:33:48 -03001820 call_all(dev, tuner, s_tuner, t);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001821
1822 return 0;
1823}
1824
Sri Deevie0d3baf2009-03-03 14:37:50 -03001825/*
1826 * cx231xx_v4l2_open()
1827 * inits the device and starts isoc transfer
1828 */
1829static int cx231xx_v4l2_open(struct file *filp)
1830{
Sri Deevie0d3baf2009-03-03 14:37:50 -03001831 int errCode = 0, radio = 0;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001832 struct video_device *vdev = video_devdata(filp);
1833 struct cx231xx *dev = video_drvdata(filp);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001834 struct cx231xx_fh *fh;
1835 enum v4l2_buf_type fh_type = 0;
1836
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001837 switch (vdev->vfl_type) {
1838 case VFL_TYPE_GRABBER:
1839 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1840 break;
1841 case VFL_TYPE_VBI:
1842 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1843 break;
1844 case VFL_TYPE_RADIO:
1845 radio = 1;
1846 break;
1847 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001848
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001849 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1850 video_device_node_name(vdev), v4l2_type_names[fh_type],
1851 dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001852
1853#if 0
1854 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1855 if (errCode < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001856 cx231xx_errdev
1857 ("Device locked on digital mode. Can't open analog\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001858 return -EBUSY;
1859 }
1860#endif
1861
1862 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1863 if (!fh) {
1864 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -03001865 return -ENOMEM;
1866 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001867 if (mutex_lock_interruptible(&dev->lock)) {
1868 kfree(fh);
1869 return -ERESTARTSYS;
1870 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03001871 fh->dev = dev;
1872 fh->radio = radio;
1873 fh->type = fh_type;
1874 filp->private_data = fh;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001875 v4l2_fh_init(&fh->fh, vdev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001876
1877 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1878 dev->width = norm_maxw(dev);
1879 dev->height = norm_maxh(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001880
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001881 /* Power up in Analog TV mode */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001882 if (dev->board.external_av)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001883 cx231xx_set_power_mode(dev,
1884 POLARIS_AVMODE_ENXTERNAL_AV);
1885 else
1886 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001887
1888#if 0
1889 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1890#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -03001891
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001892 /* set video alternate setting */
1893 cx231xx_set_video_alternate(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001894
1895 /* Needed, since GPIO might have disabled power of
1896 some i2c device */
1897 cx231xx_config_i2c(dev);
1898
1899 /* device needs to be initialized before isoc transfer */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001900 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001901
1902 }
1903 if (fh->radio) {
1904 cx231xx_videodbg("video_open: setting radio device\n");
1905
1906 /* cx231xx_start_radio(dev); */
1907
Sri Deevib1196122009-03-20 23:33:48 -03001908 call_all(dev, tuner, s_radio);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001909 }
1910
1911 dev->users++;
1912
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001913 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1914 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1915 NULL, &dev->video_mode.slock,
1916 fh->type, V4L2_FIELD_INTERLACED,
Hans Verkuil08bff032010-09-20 17:39:46 -03001917 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001918 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001919 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001920 /* Set the required alternate setting VBI interface works in
1921 Bulk mode only */
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001922 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001923
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03001924 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1925 NULL, &dev->vbi_mode.slock,
1926 fh->type, V4L2_FIELD_SEQ_TB,
Hans Verkuil08bff032010-09-20 17:39:46 -03001927 sizeof(struct cx231xx_buffer),
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03001928 fh, &dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001929 }
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001930 mutex_unlock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03001931 v4l2_fh_add(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001932
Sri Deevie0d3baf2009-03-03 14:37:50 -03001933 return errCode;
1934}
1935
1936/*
1937 * cx231xx_realease_resources()
1938 * unregisters the v4l2,i2c and usb devices
1939 * called when the device gets disconected or at module unload
1940*/
1941void cx231xx_release_analog_resources(struct cx231xx *dev)
1942{
1943
1944 /*FIXME: I2C IR should be disconnected */
1945
1946 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001947 if (video_is_registered(dev->radio_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001948 video_unregister_device(dev->radio_dev);
1949 else
1950 video_device_release(dev->radio_dev);
1951 dev->radio_dev = NULL;
1952 }
1953 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001954 cx231xx_info("V4L2 device %s deregistered\n",
1955 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001956 if (video_is_registered(dev->vbi_dev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001957 video_unregister_device(dev->vbi_dev);
1958 else
1959 video_device_release(dev->vbi_dev);
1960 dev->vbi_dev = NULL;
1961 }
1962 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001963 cx231xx_info("V4L2 device %s deregistered\n",
1964 video_device_node_name(dev->vdev));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001965
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001966 if (dev->board.has_417)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001967 cx231xx_417_unregister(dev);
1968
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001969 if (video_is_registered(dev->vdev))
Sri Deevie0d3baf2009-03-03 14:37:50 -03001970 video_unregister_device(dev->vdev);
1971 else
1972 video_device_release(dev->vdev);
1973 dev->vdev = NULL;
1974 }
Hans Verkuild2370f82012-09-17 07:22:09 -03001975 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1976 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001977}
1978
1979/*
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001980 * cx231xx_close()
Sri Deevie0d3baf2009-03-03 14:37:50 -03001981 * stops streaming and deallocates all resources allocated by the v4l2
1982 * calls and ioctls
1983 */
Hans Verkuil1f7e0732012-06-24 06:43:02 -03001984static int cx231xx_close(struct file *filp)
Sri Deevie0d3baf2009-03-03 14:37:50 -03001985{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03001986 struct cx231xx_fh *fh = filp->private_data;
1987 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03001988
1989 cx231xx_videodbg("users=%d\n", dev->users);
1990
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001991 cx231xx_videodbg("users=%d\n", dev->users);
Sri Deevie0d3baf2009-03-03 14:37:50 -03001992 if (res_check(fh))
1993 res_free(fh);
1994
Mauro Carvalho Chehab2f861382011-01-31 22:12:15 -03001995 /*
1996 * To workaround error number=-71 on EP0 for VideoGrabber,
1997 * need exclude following.
1998 * FIXME: It is probably safe to remove most of these, as we're
1999 * now avoiding the alternate setting for INDEX_VANC
2000 */
2001 if (!dev->board.no_alt_vanc)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002002 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2003 videobuf_stop(&fh->vb_vidq);
2004 videobuf_mmap_free(&fh->vb_vidq);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002005
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002006 /* the device is already disconnect,
2007 free the remaining resources */
2008 if (dev->state & DEV_DISCONNECTED) {
2009 if (atomic_read(&dev->devlist_count) > 0) {
2010 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002011 fh->dev = NULL;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002012 return 0;
2013 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002014 return 0;
2015 }
2016
2017 /* do this before setting alternate! */
2018 cx231xx_uninit_vbi_isoc(dev);
2019
2020 /* set alternate 0 */
2021 if (!dev->vbi_or_sliced_cc_mode)
2022 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2023 else
2024 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2025
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002026 v4l2_fh_del(&fh->fh);
2027 v4l2_fh_exit(&fh->fh);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002028 kfree(fh);
2029 dev->users--;
2030 wake_up_interruptible_nr(&dev->open, 1);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002031 return 0;
2032 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002033
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002034 v4l2_fh_del(&fh->fh);
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -02002035 dev->users--;
2036 if (!dev->users) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002037 videobuf_stop(&fh->vb_vidq);
2038 videobuf_mmap_free(&fh->vb_vidq);
2039
2040 /* the device is already disconnect,
2041 free the remaining resources */
2042 if (dev->state & DEV_DISCONNECTED) {
2043 cx231xx_release_resources(dev);
Jesper Juhl266e8ae2012-03-04 16:25:04 -03002044 fh->dev = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002045 return 0;
2046 }
2047
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002048 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002049 call_all(dev, core, s_power, 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002050
2051 /* do this before setting alternate! */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002052 if (dev->USE_ISO)
2053 cx231xx_uninit_isoc(dev);
2054 else
2055 cx231xx_uninit_bulk(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002056 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2057
2058 /* set alternate 0 */
2059 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2060 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002061 v4l2_fh_exit(&fh->fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002062 kfree(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002063 wake_up_interruptible_nr(&dev->open, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002064 return 0;
2065}
2066
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002067static int cx231xx_v4l2_close(struct file *filp)
2068{
2069 struct cx231xx_fh *fh = filp->private_data;
2070 struct cx231xx *dev = fh->dev;
2071 int rc;
2072
2073 mutex_lock(&dev->lock);
2074 rc = cx231xx_close(filp);
2075 mutex_unlock(&dev->lock);
2076 return rc;
2077}
2078
Sri Deevie0d3baf2009-03-03 14:37:50 -03002079/*
2080 * cx231xx_v4l2_read()
2081 * will allocate buffers when called for the first time
2082 */
2083static ssize_t
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002084cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2085 loff_t *pos)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002086{
2087 struct cx231xx_fh *fh = filp->private_data;
2088 struct cx231xx *dev = fh->dev;
2089 int rc;
2090
2091 rc = check_dev(dev);
2092 if (rc < 0)
2093 return rc;
2094
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002095 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2096 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -03002097 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002098
2099 if (unlikely(rc < 0))
2100 return rc;
2101
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002102 if (mutex_lock_interruptible(&dev->lock))
2103 return -ERESTARTSYS;
2104 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002105 filp->f_flags & O_NONBLOCK);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002106 mutex_unlock(&dev->lock);
2107 return rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002108 }
2109 return 0;
2110}
2111
2112/*
2113 * cx231xx_v4l2_poll()
2114 * will allocate buffers when called for the first time
2115 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03002116static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002117{
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002118 unsigned long req_events = poll_requested_events(wait);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002119 struct cx231xx_fh *fh = filp->private_data;
2120 struct cx231xx *dev = fh->dev;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002121 unsigned res = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002122 int rc;
2123
2124 rc = check_dev(dev);
2125 if (rc < 0)
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002126 return POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002127
Sri Deevie0d3baf2009-03-03 14:37:50 -03002128 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002129
2130 if (unlikely(rc < 0))
2131 return POLLERR;
2132
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002133 if (v4l2_event_pending(&fh->fh))
2134 res |= POLLPRI;
2135 else
2136 poll_wait(filp, &fh->fh.wait, wait);
2137
2138 if (!(req_events & (POLLIN | POLLRDNORM)))
2139 return res;
2140
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002141 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002142 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002143 mutex_lock(&dev->lock);
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002144 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002145 mutex_unlock(&dev->lock);
2146 return res;
2147 }
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002148 return res | POLLERR;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002149}
2150
2151/*
2152 * cx231xx_v4l2_mmap()
2153 */
2154static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2155{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002156 struct cx231xx_fh *fh = filp->private_data;
2157 struct cx231xx *dev = fh->dev;
2158 int rc;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002159
2160 rc = check_dev(dev);
2161 if (rc < 0)
2162 return rc;
2163
Sri Deevie0d3baf2009-03-03 14:37:50 -03002164 rc = res_get(fh);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002165
2166 if (unlikely(rc < 0))
2167 return rc;
2168
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002169 if (mutex_lock_interruptible(&dev->lock))
2170 return -ERESTARTSYS;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002171 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Hans Verkuil1f7e0732012-06-24 06:43:02 -03002172 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002173
2174 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002175 (unsigned long)vma->vm_start,
2176 (unsigned long)vma->vm_end -
2177 (unsigned long)vma->vm_start, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002178
2179 return rc;
2180}
2181
2182static const struct v4l2_file_operations cx231xx_v4l_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002183 .owner = THIS_MODULE,
2184 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002185 .release = cx231xx_v4l2_close,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002186 .read = cx231xx_v4l2_read,
2187 .poll = cx231xx_v4l2_poll,
2188 .mmap = cx231xx_v4l2_mmap,
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002189 .unlocked_ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002190};
2191
2192static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002193 .vidioc_querycap = vidioc_querycap,
2194 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2195 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2196 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2197 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2198 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2199 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
Hans Verkuil62647222013-02-09 06:41:11 -03002200 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002201 .vidioc_cropcap = vidioc_cropcap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002202 .vidioc_reqbufs = vidioc_reqbufs,
2203 .vidioc_querybuf = vidioc_querybuf,
2204 .vidioc_qbuf = vidioc_qbuf,
2205 .vidioc_dqbuf = vidioc_dqbuf,
2206 .vidioc_s_std = vidioc_s_std,
2207 .vidioc_g_std = vidioc_g_std,
2208 .vidioc_enum_input = vidioc_enum_input,
2209 .vidioc_g_input = vidioc_g_input,
2210 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002211 .vidioc_streamon = vidioc_streamon,
2212 .vidioc_streamoff = vidioc_streamoff,
2213 .vidioc_g_tuner = vidioc_g_tuner,
2214 .vidioc_s_tuner = vidioc_s_tuner,
2215 .vidioc_g_frequency = vidioc_g_frequency,
2216 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilfddd14c2012-09-13 05:37:11 -03002217 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002218#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002219 .vidioc_g_register = vidioc_g_register,
2220 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002221#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002222 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2223 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002224};
2225
2226static struct video_device cx231xx_vbi_template;
2227
2228static const struct video_device cx231xx_video_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002229 .fops = &cx231xx_v4l_fops,
2230 .release = video_device_release,
2231 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002232 .tvnorms = V4L2_STD_ALL,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002233};
2234
2235static const struct v4l2_file_operations radio_fops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002236 .owner = THIS_MODULE,
2237 .open = cx231xx_v4l2_open,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002238 .release = cx231xx_v4l2_close,
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002239 .poll = v4l2_ctrl_poll,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002240 .ioctl = video_ioctl2,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002241};
2242
2243static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil530e01e2013-01-29 12:32:20 -03002244 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002245 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002246 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002247 .vidioc_g_frequency = vidioc_g_frequency,
2248 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil530e01e2013-01-29 12:32:20 -03002249 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002250#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002251 .vidioc_g_register = vidioc_g_register,
2252 .vidioc_s_register = vidioc_s_register,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002253#endif
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002254 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2255 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002256};
2257
2258static struct video_device cx231xx_radio_template = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002259 .name = "cx231xx-radio",
2260 .fops = &radio_fops,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002261 .ioctl_ops = &radio_ioctl_ops,
Sri Deevie0d3baf2009-03-03 14:37:50 -03002262};
2263
2264/******************************** usb interface ******************************/
2265
Sri Deevib9255172009-03-04 17:49:01 -03002266static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2267 const struct video_device
2268 *template, const char *type_name)
Sri Deevie0d3baf2009-03-03 14:37:50 -03002269{
2270 struct video_device *vfd;
2271
2272 vfd = video_device_alloc();
2273 if (NULL == vfd)
2274 return NULL;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002275
Sri Deevie0d3baf2009-03-03 14:37:50 -03002276 *vfd = *template;
Sri Deevib1196122009-03-20 23:33:48 -03002277 vfd->v4l2_dev = &dev->v4l2_dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002278 vfd->release = video_device_release;
2279 vfd->debug = video_debug;
Mauro Carvalho Chehab643800d2010-10-09 13:13:35 -03002280 vfd->lock = &dev->lock;
Hans Verkuil1d08a4f2012-09-17 07:31:04 -03002281 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002282
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002283 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002284
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002285 video_set_drvdata(vfd, dev);
Hans Verkuil06c46002012-09-13 06:17:47 -03002286 if (dev->tuner_type == TUNER_ABSENT) {
2287 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2288 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2289 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2290 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2291 }
Sri Deevie0d3baf2009-03-03 14:37:50 -03002292 return vfd;
2293}
2294
2295int cx231xx_register_analog_devices(struct cx231xx *dev)
2296{
2297 int ret;
2298
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002299 cx231xx_info("%s: v4l2 driver version %s\n",
2300 dev->name, CX231XX_VERSION);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002301
2302 /* set default norm */
Hans Verkuila25a70122012-09-17 08:30:07 -03002303 dev->norm = V4L2_STD_PAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002304 dev->width = norm_maxw(dev);
2305 dev->height = norm_maxh(dev);
2306 dev->interlaced = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002307
2308 /* Analog specific initialization */
2309 dev->format = &format[0];
Devin Heitmueller6e6a2ba2010-07-12 15:34:57 -03002310
2311 /* Set the initial input */
2312 video_mux(dev, dev->video_input);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002313
Hans Verkuild2370f82012-09-17 07:22:09 -03002314 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2315 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2316
2317 if (dev->sd_cx25840) {
2318 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2319 dev->sd_cx25840->ctrl_handler, NULL);
2320 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2321 dev->sd_cx25840->ctrl_handler,
2322 v4l2_ctrl_radio_filter);
2323 }
2324
2325 if (dev->ctrl_handler.error)
2326 return dev->ctrl_handler.error;
2327 if (dev->radio_ctrl_handler.error)
2328 return dev->radio_ctrl_handler.error;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002329
2330 /* enable vbi capturing */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002331 /* write code here... */
Sri Deevie0d3baf2009-03-03 14:37:50 -03002332
2333 /* allocate and fill video video_device struct */
2334 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2335 if (!dev->vdev) {
2336 cx231xx_errdev("cannot allocate video_device.\n");
2337 return -ENODEV;
2338 }
2339
Hans Verkuild2370f82012-09-17 07:22:09 -03002340 dev->vdev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002341 /* register v4l2 video video_device */
2342 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002343 video_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002344 if (ret) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002345 cx231xx_errdev("unable to register video device (error=%i).\n",
2346 ret);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002347 return ret;
2348 }
2349
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002350 cx231xx_info("%s/0: registered device %s [v4l2]\n",
2351 dev->name, video_device_node_name(dev->vdev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002352
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002353 /* Initialize VBI template */
Ezequiel Garcia3724dde2012-10-23 15:57:05 -03002354 cx231xx_vbi_template = cx231xx_video_template;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002355 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002356
2357 /* Allocate and fill vbi video_device struct */
2358 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2359
Hans Verkuild2370f82012-09-17 07:22:09 -03002360 if (!dev->vbi_dev) {
2361 cx231xx_errdev("cannot allocate video_device.\n");
2362 return -ENODEV;
2363 }
2364 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002365 /* register v4l2 vbi video_device */
2366 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03002367 vbi_nr[dev->devno]);
Sri Deevie0d3baf2009-03-03 14:37:50 -03002368 if (ret < 0) {
2369 cx231xx_errdev("unable to register vbi device\n");
2370 return ret;
2371 }
2372
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002373 cx231xx_info("%s/0: registered device %s\n",
2374 dev->name, video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002375
2376 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -03002377 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2378 "radio");
Sri Deevie0d3baf2009-03-03 14:37:50 -03002379 if (!dev->radio_dev) {
2380 cx231xx_errdev("cannot allocate video_device.\n");
2381 return -ENODEV;
2382 }
Hans Verkuild2370f82012-09-17 07:22:09 -03002383 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
Sri Deevie0d3baf2009-03-03 14:37:50 -03002384 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2385 radio_nr[dev->devno]);
2386 if (ret < 0) {
2387 cx231xx_errdev("can't register radio device\n");
2388 return ret;
2389 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002390 cx231xx_info("Registered radio device as %s\n",
2391 video_device_node_name(dev->radio_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002392 }
2393
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002394 cx231xx_info("V4L2 device registered as %s and %s\n",
2395 video_device_node_name(dev->vdev),
2396 video_device_node_name(dev->vbi_dev));
Sri Deevie0d3baf2009-03-03 14:37:50 -03002397
2398 return 0;
2399}