blob: 80261ac40208ecf11bde7f3945e96e0f659aa986 [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
2 cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
3
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -03005 Based on cx88 driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
Mauro Carvalho Chehab589dadf2014-11-01 08:09:44 -030022#include "cx231xx.h"
Sri Deevie0d3baf2009-03-03 14:37:50 -030023#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/bitmap.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030028#include <linux/i2c.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030029#include <linux/mm.h>
30#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030032
33#include <media/v4l2-common.h>
34#include <media/v4l2-ioctl.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030035#include <media/msp3400.h>
36#include <media/tuner.h>
37
Sri Deevie0d3baf2009-03-03 14:37:50 -030038#include "cx231xx-vbi.h"
39
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030040static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -030041{
42 char *errmsg = "Unknown";
43
44 switch (status) {
45 case -ENOENT:
46 errmsg = "unlinked synchronuously";
47 break;
48 case -ECONNRESET:
49 errmsg = "unlinked asynchronuously";
50 break;
51 case -ENOSR:
52 errmsg = "Buffer error (overrun)";
53 break;
54 case -EPIPE:
55 errmsg = "Stalled (device not responding)";
56 break;
57 case -EOVERFLOW:
58 errmsg = "Babble (bad cable?)";
59 break;
60 case -EPROTO:
61 errmsg = "Bit-stuff error (bad cable?)";
62 break;
63 case -EILSEQ:
64 errmsg = "CRC/Timeout (could be anything)";
65 break;
66 case -ETIME:
67 errmsg = "Device does not respond";
68 break;
69 }
70 if (packet < 0) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -030071 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -030072 "URB status %d [%s].\n", status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -030073 } else {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -030074 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -030075 "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehabed0e3722014-11-01 08:59:03 -030076 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -030077 }
78}
79
80/*
81 * Controls the isoc copy of each urb packet
82 */
83static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
84{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030085 struct cx231xx_dmaqueue *dma_q = urb->context;
86 int rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -030087 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030088 u32 bytes_parsed = 0, buffer_size = 0;
89 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -030090
91 if (!dev)
92 return 0;
93
Mauro Carvalho Chehab990862a2012-01-10 09:48:50 -020094 if (dev->state & DEV_DISCONNECTED)
Sri Deevie0d3baf2009-03-03 14:37:50 -030095 return 0;
96
97 if (urb->status < 0) {
98 print_err_status(dev, -1, urb->status);
99 if (urb->status == -ENOENT)
100 return 0;
101 }
102
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300103 /* get buffer pointer and length */
104 p_buffer = urb->transfer_buffer;
105 buffer_size = urb->actual_length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300106
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300107 if (buffer_size > 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300108 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300109
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300110 if (dma_q->is_partial_line) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300111 /* Handle the case where we were working on a partial
112 line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300113 sav_eav = dma_q->last_sav;
114 } else {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300115 /* Check for a SAV/EAV overlapping the
116 buffer boundary */
117
118 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300119 dma_q->partial_buf,
120 &bytes_parsed);
121 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300122
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300123 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300124 /* Get the first line if we have some portion of an SAV/EAV from
125 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300126 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300127 bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
128 sav_eav, /* SAV/EAV */
129 p_buffer + bytes_parsed, /* p_buffer */
130 buffer_size - bytes_parsed); /* buffer size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300131 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300132
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300133 /* Now parse data that is completely in this buffer */
134 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300135
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300136 while (bytes_parsed < buffer_size) {
137 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300138
Sri Deevib9255172009-03-04 17:49:01 -0300139 sav_eav = cx231xx_find_next_SAV_EAV(
140 p_buffer + bytes_parsed, /* p_buffer */
141 buffer_size - bytes_parsed, /* buffer size */
142 &bytes_used); /* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300143
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300144 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300145
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300146 sav_eav &= 0xF0;
147 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300148 bytes_parsed += cx231xx_get_vbi_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300149 dma_q, sav_eav, /* SAV/EAV */
150 p_buffer+bytes_parsed, /* p_buffer */
151 buffer_size-bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300152 }
153 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300154
Sri Deevib9255172009-03-04 17:49:01 -0300155 /* Save the last four bytes of the buffer so we can
156 check the buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300157 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
158 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300159 }
160
161 return rc;
162}
163
164/* ------------------------------------------------------------------
165 Vbi buf operations
166 ------------------------------------------------------------------*/
167
168static int
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300169vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
170 unsigned int *size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300171{
172 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300173 struct cx231xx *dev = fh->dev;
174 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300175
176 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300177 PAL_VBI_LINES : NTSC_VBI_LINES);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300178
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300179 *size = (dev->width * height * 2 * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300180 if (0 == *count)
181 *count = CX231XX_DEF_VBI_BUF;
182
183 if (*count < CX231XX_MIN_BUF)
184 *count = CX231XX_MIN_BUF;
185
Sri Deevie0d3baf2009-03-03 14:37:50 -0300186 return 0;
187}
188
189/* This is called *without* dev->slock held; please keep it that way */
190static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
191{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300192 struct cx231xx_fh *fh = vq->priv_data;
193 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300194 unsigned long flags = 0;
195 if (in_interrupt())
196 BUG();
197
198 /* We used to wait for the buffer to finish here, but this didn't work
199 because, as we were keeping the state as VIDEOBUF_QUEUED,
200 videobuf_queue_cancel marked it as finished for us.
201 (Also, it could wedge forever if the hardware was misconfigured.)
202
203 This should be safe; by the time we get here, the buffer isn't
204 queued anymore. If we ever start marking the buffers as
205 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300206 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300207 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300208 if (dev->vbi_mode.bulk_ctl.buf == buf)
209 dev->vbi_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300210 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
211
212 videobuf_vmalloc_free(&buf->vb);
213 buf->vb.state = VIDEOBUF_NEEDS_INIT;
214}
215
216static int
217vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300218 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300219{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300220 struct cx231xx_fh *fh = vq->priv_data;
221 struct cx231xx_buffer *buf =
222 container_of(vb, struct cx231xx_buffer, vb);
223 struct cx231xx *dev = fh->dev;
224 int rc = 0, urb_init = 0;
225 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300226
227 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300228 PAL_VBI_LINES : NTSC_VBI_LINES);
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300229 buf->vb.size = ((dev->width << 1) * height * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300230
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300231 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300232 return -EINVAL;
233
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300234 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300235 buf->vb.height = height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300236 buf->vb.field = field;
237 buf->vb.field = V4L2_FIELD_SEQ_TB;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300238
239 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
240 rc = videobuf_iolock(vq, &buf->vb, NULL);
241 if (rc < 0)
242 goto fail;
243 }
244
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300245 if (!dev->vbi_mode.bulk_ctl.num_bufs)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300246 urb_init = 1;
247
248 if (urb_init) {
249 rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300250 CX231XX_NUM_VBI_BUFS,
251 dev->vbi_mode.alt_max_pkt_size[0],
252 cx231xx_isoc_vbi_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300253 if (rc < 0)
254 goto fail;
255 }
256
257 buf->vb.state = VIDEOBUF_PREPARED;
258 return 0;
259
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300260fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300261 free_buffer(vq, buf);
262 return rc;
263}
264
265static void
266vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
267{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300268 struct cx231xx_buffer *buf =
269 container_of(vb, struct cx231xx_buffer, vb);
270 struct cx231xx_fh *fh = vq->priv_data;
271 struct cx231xx *dev = fh->dev;
272 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300273
274 buf->vb.state = VIDEOBUF_QUEUED;
275 list_add_tail(&buf->vb.queue, &vidq->active);
276
277}
278
279static void vbi_buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300280 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300281{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300282 struct cx231xx_buffer *buf =
283 container_of(vb, struct cx231xx_buffer, vb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300284
Sri Deevie0d3baf2009-03-03 14:37:50 -0300285
286 free_buffer(vq, buf);
287}
288
Sri Deevie0d3baf2009-03-03 14:37:50 -0300289struct videobuf_queue_ops cx231xx_vbi_qops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300290 .buf_setup = vbi_buffer_setup,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300291 .buf_prepare = vbi_buffer_prepare,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300292 .buf_queue = vbi_buffer_queue,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300293 .buf_release = vbi_buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300294};
295
Sri Deevie0d3baf2009-03-03 14:37:50 -0300296/* ------------------------------------------------------------------
297 URB control
298 ------------------------------------------------------------------*/
299
300/*
301 * IRQ callback, called by URB callback
302 */
303static void cx231xx_irq_vbi_callback(struct urb *urb)
304{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300305 struct cx231xx_dmaqueue *dma_q = urb->context;
306 struct cx231xx_video_mode *vmode =
307 container_of(dma_q, struct cx231xx_video_mode, vidq);
308 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300309
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300310 switch (urb->status) {
311 case 0: /* success */
312 case -ETIMEDOUT: /* NAK */
313 break;
314 case -ECONNRESET: /* kill */
315 case -ENOENT:
316 case -ESHUTDOWN:
317 return;
318 default: /* error */
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300319 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300320 "urb completition error %d.\n", urb->status);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300321 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300322 }
323
324 /* Copy data from URB */
325 spin_lock(&dev->vbi_mode.slock);
Hans Verkuilda983502012-05-14 10:14:53 -0300326 dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300327 spin_unlock(&dev->vbi_mode.slock);
328
329 /* Reset status */
330 urb->status = 0;
331
332 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
333 if (urb->status) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300334 dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
Mauro Carvalho Chehabed0e3722014-11-01 08:59:03 -0300335 urb->status);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300336 }
337}
338
339/*
340 * Stop and Deallocate URBs
341 */
342void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
343{
344 struct urb *urb;
345 int i;
346
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300347 dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300348
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300349 dev->vbi_mode.bulk_ctl.nfields = -1;
350 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
351 urb = dev->vbi_mode.bulk_ctl.urb[i];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300352 if (urb) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300353 if (!irqs_disabled())
354 usb_kill_urb(urb);
355 else
356 usb_unlink_urb(urb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300357
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300358 if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300359
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300360 kfree(dev->vbi_mode.bulk_ctl.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300361 transfer_buffer[i]);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300362 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300363 NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300364 }
365 usb_free_urb(urb);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300366 dev->vbi_mode.bulk_ctl.urb[i] = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300367 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300368 dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300369 }
370
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300371 kfree(dev->vbi_mode.bulk_ctl.urb);
372 kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300373
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300374 dev->vbi_mode.bulk_ctl.urb = NULL;
375 dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
376 dev->vbi_mode.bulk_ctl.num_bufs = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300377
378 cx231xx_capture_start(dev, 0, Vbi);
379}
380EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
381
382/*
383 * Allocate URBs and start IRQ
384 */
385int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300386 int num_bufs, int max_pkt_size,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300387 int (*bulk_copy) (struct cx231xx *dev,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300388 struct urb *urb))
Sri Deevie0d3baf2009-03-03 14:37:50 -0300389{
390 struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
391 int i;
392 int sb_size, pipe;
393 struct urb *urb;
394 int rc;
395
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300396 dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300397
398 /* De-allocates all pending stuff */
399 cx231xx_uninit_vbi_isoc(dev);
400
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300401 /* clear if any halt */
402 usb_clear_halt(dev->udev,
403 usb_rcvbulkpipe(dev->udev,
404 dev->vbi_mode.end_point_addr));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300405
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300406 dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
407 dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300408 dma_q->pos = 0;
409 dma_q->is_partial_line = 0;
410 dma_q->last_sav = 0;
411 dma_q->current_field = -1;
412 dma_q->bytes_left_in_line = dev->width << 1;
413 dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
414 PAL_VBI_LINES : NTSC_VBI_LINES);
415 dma_q->lines_completed = 0;
416 for (i = 0; i < 8; i++)
417 dma_q->partial_buf[i] = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300418
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300419 dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300420 GFP_KERNEL);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300421 if (!dev->vbi_mode.bulk_ctl.urb) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300422 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300423 "cannot alloc memory for usb buffers\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300424 return -ENOMEM;
425 }
426
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300427 dev->vbi_mode.bulk_ctl.transfer_buffer =
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300428 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300429 if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300430 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300431 "cannot allocate memory for usbtransfer\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300432 kfree(dev->vbi_mode.bulk_ctl.urb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300433 return -ENOMEM;
434 }
435
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300436 dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
437 dev->vbi_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300438
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300439 sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300440
441 /* allocate urbs and transfer buffers */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300442 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300443
444 urb = usb_alloc_urb(0, GFP_KERNEL);
445 if (!urb) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300446 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300447 "cannot alloc bulk_ctl.urb %i\n", i);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300448 cx231xx_uninit_vbi_isoc(dev);
449 return -ENOMEM;
450 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300451 dev->vbi_mode.bulk_ctl.urb[i] = urb;
David Dillowcd5534b2012-06-18 00:15:21 -0300452 urb->transfer_flags = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300453
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300454 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300455 kzalloc(sb_size, GFP_KERNEL);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300456 if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300457 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300458 "unable to allocate %i bytes for transfer buffer %i%s\n",
Mauro Carvalho Chehabed0e3722014-11-01 08:59:03 -0300459 sb_size, i,
460 in_interrupt() ? " while in int" : "");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300461 cx231xx_uninit_vbi_isoc(dev);
462 return -ENOMEM;
463 }
464
465 pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300466 usb_fill_bulk_urb(urb, dev->udev, pipe,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300467 dev->vbi_mode.bulk_ctl.transfer_buffer[i],
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300468 sb_size, cx231xx_irq_vbi_callback, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300469 }
470
471 init_waitqueue_head(&dma_q->wq);
472
473 /* submit urbs and enables IRQ */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300474 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
475 rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300476 if (rc) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300477 dev_err(dev->dev,
Mauro Carvalho Chehabb7085c02014-11-02 07:21:44 -0300478 "submit of urb %i failed (error=%i)\n", i, rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300479 cx231xx_uninit_vbi_isoc(dev);
480 return rc;
481 }
482 }
483
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300484 cx231xx_capture_start(dev, 1, Vbi);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300485
486 return 0;
487}
488EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
489
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300490u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
491 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300492{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300493 u32 bytes_copied = 0;
494 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300495
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300496 switch (sav_eav) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300497
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300498 case SAV_VBI_FIELD1:
499 current_field = 1;
500 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300501
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300502 case SAV_VBI_FIELD2:
503 current_field = 2;
504 break;
505 default:
506 break;
507 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300508
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300509 if (current_field < 0)
510 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300511
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300512 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300513
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300514 bytes_copied =
515 cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
516 current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300517
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300518 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519}
520
521/*
522 * Announces that a buffer were filled and request the next
523 */
524static inline void vbi_buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300525 struct cx231xx_dmaqueue *dma_q,
526 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300527{
528 /* Advice that buffer was filled */
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300529 /* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300530
531 buf->vb.state = VIDEOBUF_DONE;
532 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300533 v4l2_get_timestamp(&buf->vb.ts);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300534
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300535 dev->vbi_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300536
537 list_del(&buf->vb.queue);
538 wake_up(&buf->vb.done);
539}
540
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300541u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300542 u8 *p_line, u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300543{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300544 u32 bytes_to_copy;
545 struct cx231xx_buffer *buf;
546 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300547
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300548 if (dma_q->current_field == -1) {
549 /* Just starting up */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300550 cx231xx_reset_vbi_buffer(dev, dma_q);
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300551 }
552
553 if (dma_q->current_field != field_number)
554 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300555
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300556 /* get the buffer pointer */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300557 buf = dev->vbi_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300558
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300559 /* Remember the field number for next time */
560 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300561
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300562 bytes_to_copy = dma_q->bytes_left_in_line;
563 if (bytes_to_copy > length)
564 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300565
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300566 if (dma_q->lines_completed >= dma_q->lines_per_field) {
567 dma_q->bytes_left_in_line -= bytes_to_copy;
568 dma_q->is_partial_line =
569 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
570 return 0;
571 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300572
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300573 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300574
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300575 /* If we don't have a buffer, just return the number of bytes we would
576 have copied if we had a buffer. */
577 if (!buf) {
578 dma_q->bytes_left_in_line -= bytes_to_copy;
579 dma_q->is_partial_line =
580 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
581 return bytes_to_copy;
582 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300583
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300584 /* copy the data to video buffer */
585 cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300586
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300587 dma_q->pos += bytes_to_copy;
588 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300589
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300590 if (dma_q->bytes_left_in_line == 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300591
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300592 dma_q->bytes_left_in_line = _line_size;
593 dma_q->lines_completed++;
594 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300595
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300596 if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300597
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300598 vbi_buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300599
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300600 dma_q->pos = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 dma_q->lines_completed = 0;
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300602 cx231xx_reset_vbi_buffer(dev, dma_q);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300603 }
604 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300605
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300606 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300607}
608
609/*
610 * video-buf generic routine to get the next available buffer
611 */
612static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300613 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300614{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300615 struct cx231xx_video_mode *vmode =
616 container_of(dma_q, struct cx231xx_video_mode, vidq);
617 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300618 char *outp;
619
620 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300621 dev_err(dev->dev, "No active queue to serve\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300622 dev->vbi_mode.bulk_ctl.buf = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300623 *buf = NULL;
624 return;
625 }
626
627 /* Get the next buffer */
628 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
629
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300630 /* Cleans up buffer - Useful for testing for frame/URB loss */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300631 outp = videobuf_to_vmalloc(&(*buf)->vb);
632 memset(outp, 0, (*buf)->vb.size);
633
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300634 dev->vbi_mode.bulk_ctl.buf = *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300635
636 return;
637}
638
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300639void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
640 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300641{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300642 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300643
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300644 buf = dev->vbi_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300645
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300646 if (buf == NULL) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300647 /* first try to get the buffer */
648 get_next_vbi_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300649
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300650 dma_q->pos = 0;
651 dma_q->current_field = -1;
652 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300653
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300654 dma_q->bytes_left_in_line = dev->width << 1;
655 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300656}
657
658int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300659 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300660{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300661 u8 *p_out_buffer = NULL;
662 u32 current_line_bytes_copied = 0;
663 struct cx231xx_buffer *buf;
664 u32 _line_size = dev->width << 1;
665 void *startwrite;
666 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300667
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300668 buf = dev->vbi_mode.bulk_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300669
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300670 if (buf == NULL)
671 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300672
673 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
674
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300675 if (dma_q->bytes_left_in_line != _line_size) {
676 current_line_bytes_copied =
677 _line_size - dma_q->bytes_left_in_line;
678 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300679
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300680 offset = (dma_q->lines_completed * _line_size) +
681 current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300682
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300683 if (dma_q->current_field == 2) {
684 /* Populate the second half of the frame */
685 offset += (dev->width * 2 * dma_q->lines_per_field);
686 }
687
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300688 /* prepare destination address */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300689 startwrite = p_out_buffer + offset;
690
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300691 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
692 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300693
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300694 memcpy(startwrite, p_buffer, lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300695
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300696 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300697}
698
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300699u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
700 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300701{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300702 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300703
704 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300705 PAL_VBI_LINES : NTSC_VBI_LINES);
Devin Heitmueller99d35a02010-07-08 15:45:13 -0300706 if (dma_q->lines_completed == height && dma_q->current_field == 2)
707 return 1;
708 else
709 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300710}