blob: 82db39d339e10552df36c8ab867091c2b135f23b [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
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/bitmap.h>
27#include <linux/usb.h>
28#include <linux/i2c.h>
29#include <linux/version.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32
33#include <media/v4l2-common.h>
34#include <media/v4l2-ioctl.h>
35#include <media/v4l2-chip-ident.h>
36#include <media/msp3400.h>
37#include <media/tuner.h>
38
39#include "cx231xx.h"
40#include "cx231xx-vbi.h"
41
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030042static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -030043{
44 char *errmsg = "Unknown";
45
46 switch (status) {
47 case -ENOENT:
48 errmsg = "unlinked synchronuously";
49 break;
50 case -ECONNRESET:
51 errmsg = "unlinked asynchronuously";
52 break;
53 case -ENOSR:
54 errmsg = "Buffer error (overrun)";
55 break;
56 case -EPIPE:
57 errmsg = "Stalled (device not responding)";
58 break;
59 case -EOVERFLOW:
60 errmsg = "Babble (bad cable?)";
61 break;
62 case -EPROTO:
63 errmsg = "Bit-stuff error (bad cable?)";
64 break;
65 case -EILSEQ:
66 errmsg = "CRC/Timeout (could be anything)";
67 break;
68 case -ETIME:
69 errmsg = "Device does not respond";
70 break;
71 }
72 if (packet < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030073 cx231xx_err(DRIVER_NAME "URB status %d [%s].\n", status,
74 errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -030075 } else {
76 cx231xx_err(DRIVER_NAME "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030077 packet, status, errmsg);
Sri Deevie0d3baf2009-03-03 14:37:50 -030078 }
79}
80
81/*
82 * Controls the isoc copy of each urb packet
83 */
84static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
85{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030086 struct cx231xx_buffer *buf;
87 struct cx231xx_dmaqueue *dma_q = urb->context;
88 int rc = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -030089 unsigned char *p_buffer;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030090 u32 bytes_parsed = 0, buffer_size = 0;
91 u8 sav_eav = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -030092
93 if (!dev)
94 return 0;
95
96 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
97 return 0;
98
99 if (urb->status < 0) {
100 print_err_status(dev, -1, urb->status);
101 if (urb->status == -ENOENT)
102 return 0;
103 }
104
105 buf = dev->vbi_mode.isoc_ctl.buf;
106
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300107 /* get buffer pointer and length */
108 p_buffer = urb->transfer_buffer;
109 buffer_size = urb->actual_length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300110
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300111 if (buffer_size > 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300112 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300113
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300114 if (dma_q->is_partial_line) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300115 /* Handle the case where we were working on a partial
116 line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300117 sav_eav = dma_q->last_sav;
118 } else {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300119 /* Check for a SAV/EAV overlapping the
120 buffer boundary */
121
122 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300123 dma_q->partial_buf,
124 &bytes_parsed);
125 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300126
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300127 sav_eav &= 0xF0;
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300128 /* Get the first line if we have some portion of an SAV/EAV from
129 the last buffer or a partial line */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300130 if (sav_eav) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300131 bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
132 sav_eav, /* SAV/EAV */
133 p_buffer + bytes_parsed, /* p_buffer */
134 buffer_size - bytes_parsed); /* buffer size */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300135 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300136
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300137 /* Now parse data that is completely in this buffer */
138 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300139
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300140 while (bytes_parsed < buffer_size) {
141 u32 bytes_used = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300142
Sri Deevib9255172009-03-04 17:49:01 -0300143 sav_eav = cx231xx_find_next_SAV_EAV(
144 p_buffer + bytes_parsed, /* p_buffer */
145 buffer_size - bytes_parsed, /* buffer size */
146 &bytes_used); /* bytes used to get SAV/EAV */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300147
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300148 bytes_parsed += bytes_used;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300149
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300150 sav_eav &= 0xF0;
151 if (sav_eav && (bytes_parsed < buffer_size)) {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300152 bytes_parsed += cx231xx_get_vbi_line(dev,
Sri Deevib9255172009-03-04 17:49:01 -0300153 dma_q, sav_eav, /* SAV/EAV */
154 p_buffer+bytes_parsed, /* p_buffer */
155 buffer_size-bytes_parsed);/*buf size*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300156 }
157 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300158
Sri Deevib9255172009-03-04 17:49:01 -0300159 /* Save the last four bytes of the buffer so we can
160 check the buffer boundary condition next time */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300161 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
162 bytes_parsed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300163 }
164
165 return rc;
166}
167
168/* ------------------------------------------------------------------
169 Vbi buf operations
170 ------------------------------------------------------------------*/
171
172static int
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300173vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
174 unsigned int *size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300175{
176 struct cx231xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300177 struct cx231xx *dev = fh->dev;
178 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300179
180 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300181 PAL_VBI_LINES : NTSC_VBI_LINES);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300182
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300183 *size = (dev->width * height * 2);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300184 if (0 == *count)
185 *count = CX231XX_DEF_VBI_BUF;
186
187 if (*count < CX231XX_MIN_BUF)
188 *count = CX231XX_MIN_BUF;
189
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300190 /* call VBI setup if required */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300191 /* cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300192 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300193
194 return 0;
195}
196
197/* This is called *without* dev->slock held; please keep it that way */
198static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
199{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300200 struct cx231xx_fh *fh = vq->priv_data;
201 struct cx231xx *dev = fh->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300202 unsigned long flags = 0;
203 if (in_interrupt())
204 BUG();
205
206 /* We used to wait for the buffer to finish here, but this didn't work
207 because, as we were keeping the state as VIDEOBUF_QUEUED,
208 videobuf_queue_cancel marked it as finished for us.
209 (Also, it could wedge forever if the hardware was misconfigured.)
210
211 This should be safe; by the time we get here, the buffer isn't
212 queued anymore. If we ever start marking the buffers as
213 VIDEOBUF_ACTIVE, it won't be, though.
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300214 */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300215 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
216 if (dev->vbi_mode.isoc_ctl.buf == buf)
217 dev->vbi_mode.isoc_ctl.buf = NULL;
218 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
219
220 videobuf_vmalloc_free(&buf->vb);
221 buf->vb.state = VIDEOBUF_NEEDS_INIT;
222}
223
224static int
225vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300226 enum v4l2_field field)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300227{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300228 struct cx231xx_fh *fh = vq->priv_data;
229 struct cx231xx_buffer *buf =
230 container_of(vb, struct cx231xx_buffer, vb);
231 struct cx231xx *dev = fh->dev;
232 int rc = 0, urb_init = 0;
233 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300234
235 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300236 PAL_VBI_LINES : NTSC_VBI_LINES);
237 buf->vb.size = ((dev->width << 1) * height);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300238
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300239 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300240 return -EINVAL;
241
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300242 buf->vb.width = dev->width;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300243 buf->vb.height = height;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300244 buf->vb.field = field;
245 buf->vb.field = V4L2_FIELD_SEQ_TB;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300246
247 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
248 rc = videobuf_iolock(vq, &buf->vb, NULL);
249 if (rc < 0)
250 goto fail;
251 }
252
253 if (!dev->vbi_mode.isoc_ctl.num_bufs)
254 urb_init = 1;
255
256 if (urb_init) {
257 rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300258 CX231XX_NUM_VBI_BUFS,
259 dev->vbi_mode.alt_max_pkt_size[0],
260 cx231xx_isoc_vbi_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300261 if (rc < 0)
262 goto fail;
263 }
264
265 buf->vb.state = VIDEOBUF_PREPARED;
266 return 0;
267
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300268fail:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300269 free_buffer(vq, buf);
270 return rc;
271}
272
273static void
274vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
275{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300276 struct cx231xx_buffer *buf =
277 container_of(vb, struct cx231xx_buffer, vb);
278 struct cx231xx_fh *fh = vq->priv_data;
279 struct cx231xx *dev = fh->dev;
280 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300281
282 buf->vb.state = VIDEOBUF_QUEUED;
283 list_add_tail(&buf->vb.queue, &vidq->active);
284
285}
286
287static void vbi_buffer_release(struct videobuf_queue *vq,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300288 struct videobuf_buffer *vb)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300289{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300290 struct cx231xx_buffer *buf =
291 container_of(vb, struct cx231xx_buffer, vb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300292
Sri Deevie0d3baf2009-03-03 14:37:50 -0300293
294 free_buffer(vq, buf);
295}
296
Sri Deevie0d3baf2009-03-03 14:37:50 -0300297struct videobuf_queue_ops cx231xx_vbi_qops = {
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300298 .buf_setup = vbi_buffer_setup,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300299 .buf_prepare = vbi_buffer_prepare,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300300 .buf_queue = vbi_buffer_queue,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300301 .buf_release = vbi_buffer_release,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300302};
303
Sri Deevie0d3baf2009-03-03 14:37:50 -0300304/* ------------------------------------------------------------------
305 URB control
306 ------------------------------------------------------------------*/
307
308/*
309 * IRQ callback, called by URB callback
310 */
311static void cx231xx_irq_vbi_callback(struct urb *urb)
312{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300313 struct cx231xx_dmaqueue *dma_q = urb->context;
314 struct cx231xx_video_mode *vmode =
315 container_of(dma_q, struct cx231xx_video_mode, vidq);
316 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300317 int rc;
318
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300319 switch (urb->status) {
320 case 0: /* success */
321 case -ETIMEDOUT: /* NAK */
322 break;
323 case -ECONNRESET: /* kill */
324 case -ENOENT:
325 case -ESHUTDOWN:
326 return;
327 default: /* error */
328 cx231xx_err(DRIVER_NAME "urb completition error %d.\n",
329 urb->status);
330 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300331 }
332
333 /* Copy data from URB */
334 spin_lock(&dev->vbi_mode.slock);
335 rc = dev->vbi_mode.isoc_ctl.isoc_copy(dev, urb);
336 spin_unlock(&dev->vbi_mode.slock);
337
338 /* Reset status */
339 urb->status = 0;
340
341 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
342 if (urb->status) {
343 cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300344 urb->status);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300345 }
346}
347
348/*
349 * Stop and Deallocate URBs
350 */
351void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
352{
353 struct urb *urb;
354 int i;
355
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300356 cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_uninit_vbi_isoc\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300357
358 dev->vbi_mode.isoc_ctl.nfields = -1;
359 for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
360 urb = dev->vbi_mode.isoc_ctl.urb[i];
361 if (urb) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300362 if (!irqs_disabled())
363 usb_kill_urb(urb);
364 else
365 usb_unlink_urb(urb);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300366
367 if (dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
368
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300369 kfree(dev->vbi_mode.isoc_ctl.
370 transfer_buffer[i]);
371 dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
372 NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300373 }
374 usb_free_urb(urb);
375 dev->vbi_mode.isoc_ctl.urb[i] = NULL;
376 }
377 dev->vbi_mode.isoc_ctl.transfer_buffer[i] = NULL;
378 }
379
380 kfree(dev->vbi_mode.isoc_ctl.urb);
381 kfree(dev->vbi_mode.isoc_ctl.transfer_buffer);
382
383 dev->vbi_mode.isoc_ctl.urb = NULL;
384 dev->vbi_mode.isoc_ctl.transfer_buffer = NULL;
385 dev->vbi_mode.isoc_ctl.num_bufs = 0;
386
387 cx231xx_capture_start(dev, 0, Vbi);
388}
389EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
390
391/*
392 * Allocate URBs and start IRQ
393 */
394int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300395 int num_bufs, int max_pkt_size,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300396 int (*isoc_copy) (struct cx231xx *dev,
397 struct urb *urb))
Sri Deevie0d3baf2009-03-03 14:37:50 -0300398{
399 struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
400 int i;
401 int sb_size, pipe;
402 struct urb *urb;
403 int rc;
404
405 cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n");
406
407 /* De-allocates all pending stuff */
408 cx231xx_uninit_vbi_isoc(dev);
409
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300410 /* clear if any halt */
411 usb_clear_halt(dev->udev,
412 usb_rcvbulkpipe(dev->udev,
413 dev->vbi_mode.end_point_addr));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300414
415 dev->vbi_mode.isoc_ctl.isoc_copy = isoc_copy;
416 dev->vbi_mode.isoc_ctl.num_bufs = num_bufs;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300417 dma_q->pos = 0;
418 dma_q->is_partial_line = 0;
419 dma_q->last_sav = 0;
420 dma_q->current_field = -1;
421 dma_q->bytes_left_in_line = dev->width << 1;
422 dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
423 PAL_VBI_LINES : NTSC_VBI_LINES);
424 dma_q->lines_completed = 0;
425 for (i = 0; i < 8; i++)
426 dma_q->partial_buf[i] = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300427
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300428 dev->vbi_mode.isoc_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
429 GFP_KERNEL);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300430 if (!dev->vbi_mode.isoc_ctl.urb) {
431 cx231xx_errdev("cannot alloc memory for usb buffers\n");
432 return -ENOMEM;
433 }
434
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300435 dev->vbi_mode.isoc_ctl.transfer_buffer =
436 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300437 if (!dev->vbi_mode.isoc_ctl.transfer_buffer) {
438 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
439 kfree(dev->vbi_mode.isoc_ctl.urb);
440 return -ENOMEM;
441 }
442
443 dev->vbi_mode.isoc_ctl.max_pkt_size = max_pkt_size;
444 dev->vbi_mode.isoc_ctl.buf = NULL;
445
446 sb_size = max_packets * dev->vbi_mode.isoc_ctl.max_pkt_size;
447
448 /* allocate urbs and transfer buffers */
449 for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
450
451 urb = usb_alloc_urb(0, GFP_KERNEL);
452 if (!urb) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300453 cx231xx_err(DRIVER_NAME
454 ": cannot alloc isoc_ctl.urb %i\n", i);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300455 cx231xx_uninit_vbi_isoc(dev);
456 return -ENOMEM;
457 }
458 dev->vbi_mode.isoc_ctl.urb[i] = urb;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300459 urb->transfer_flags = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300460
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300461 dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
462 kzalloc(sb_size, GFP_KERNEL);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300463 if (!dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300464 cx231xx_err(DRIVER_NAME
465 ": unable to allocate %i bytes for transfer"
466 " buffer %i%s\n", sb_size, i,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300467 in_interrupt() ? " while in int" : "");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300468 cx231xx_uninit_vbi_isoc(dev);
469 return -ENOMEM;
470 }
471
472 pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300473 usb_fill_bulk_urb(urb, dev->udev, pipe,
474 dev->vbi_mode.isoc_ctl.transfer_buffer[i],
475 sb_size, cx231xx_irq_vbi_callback, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300476 }
477
478 init_waitqueue_head(&dma_q->wq);
479
480 /* submit urbs and enables IRQ */
481 for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
482 rc = usb_submit_urb(dev->vbi_mode.isoc_ctl.urb[i], GFP_ATOMIC);
483 if (rc) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300484 cx231xx_err(DRIVER_NAME
485 ": submit of urb %i failed (error=%i)\n", i,
486 rc);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300487 cx231xx_uninit_vbi_isoc(dev);
488 return rc;
489 }
490 }
491
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300492 cx231xx_capture_start(dev, 1, Vbi);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300493
494 return 0;
495}
496EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
497
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300498u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
499 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300500{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300501 u32 bytes_copied = 0;
502 int current_field = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300503
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300504 switch (sav_eav) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300505
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300506 case SAV_VBI_FIELD1:
507 current_field = 1;
508 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300509
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300510 case SAV_VBI_FIELD2:
511 current_field = 2;
512 break;
513 default:
514 break;
515 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300516
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300517 if (current_field < 0)
518 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300520 dma_q->last_sav = sav_eav;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300521
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300522 bytes_copied =
523 cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
524 current_field);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300525
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300526 return bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300527}
528
529/*
530 * Announces that a buffer were filled and request the next
531 */
532static inline void vbi_buffer_filled(struct cx231xx *dev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300533 struct cx231xx_dmaqueue *dma_q,
534 struct cx231xx_buffer *buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300535{
536 /* Advice that buffer was filled */
537 /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
538
539 buf->vb.state = VIDEOBUF_DONE;
540 buf->vb.field_count++;
541 do_gettimeofday(&buf->vb.ts);
542
543 dev->vbi_mode.isoc_ctl.buf = NULL;
544
545 list_del(&buf->vb.queue);
546 wake_up(&buf->vb.done);
547}
548
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300549u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300550 u8 *p_line, u32 length, int field_number)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300551{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300552 u32 bytes_to_copy;
553 struct cx231xx_buffer *buf;
554 u32 _line_size = dev->width * 2;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300555
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300556 if (dma_q->current_field != field_number)
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300557 cx231xx_reset_vbi_buffer(dev, dma_q);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300558
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300559 /* get the buffer pointer */
560 buf = dev->vbi_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300561
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300562 /* Remember the field number for next time */
563 dma_q->current_field = field_number;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300564
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300565 bytes_to_copy = dma_q->bytes_left_in_line;
566 if (bytes_to_copy > length)
567 bytes_to_copy = length;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300568
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300569 if (dma_q->lines_completed >= dma_q->lines_per_field) {
570 dma_q->bytes_left_in_line -= bytes_to_copy;
571 dma_q->is_partial_line =
572 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
573 return 0;
574 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300575
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300576 dma_q->is_partial_line = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300577
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300578 /* If we don't have a buffer, just return the number of bytes we would
579 have copied if we had a buffer. */
580 if (!buf) {
581 dma_q->bytes_left_in_line -= bytes_to_copy;
582 dma_q->is_partial_line =
583 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
584 return bytes_to_copy;
585 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300586
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300587 /* copy the data to video buffer */
588 cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300589
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300590 dma_q->pos += bytes_to_copy;
591 dma_q->bytes_left_in_line -= bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300592
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300593 if (dma_q->bytes_left_in_line == 0) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300594
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300595 dma_q->bytes_left_in_line = _line_size;
596 dma_q->lines_completed++;
597 dma_q->is_partial_line = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300598
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300599 if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300600
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 vbi_buffer_filled(dev, dma_q, buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300602
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300603 dma_q->pos = 0;
604 buf = NULL;
605 dma_q->lines_completed = 0;
606 }
607 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300608
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300609 return bytes_to_copy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300610}
611
612/*
613 * video-buf generic routine to get the next available buffer
614 */
615static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300616 struct cx231xx_buffer **buf)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300617{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300618 struct cx231xx_video_mode *vmode =
619 container_of(dma_q, struct cx231xx_video_mode, vidq);
620 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300621 char *outp;
622
623 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300624 cx231xx_err(DRIVER_NAME ": No active queue to serve\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300625 dev->vbi_mode.isoc_ctl.buf = NULL;
626 *buf = NULL;
627 return;
628 }
629
630 /* Get the next buffer */
631 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
632
633 /* Cleans up buffer - Usefull for testing for frame/URB loss */
634 outp = videobuf_to_vmalloc(&(*buf)->vb);
635 memset(outp, 0, (*buf)->vb.size);
636
637 dev->vbi_mode.isoc_ctl.buf = *buf;
638
639 return;
640}
641
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300642void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
643 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300644{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300645 struct cx231xx_buffer *buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300646
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300647 buf = dev->vbi_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300648
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300649 if (buf == NULL) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300650 /* first try to get the buffer */
651 get_next_vbi_buf(dma_q, &buf);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300652
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300653 dma_q->pos = 0;
654 dma_q->current_field = -1;
655 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300656
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300657 dma_q->bytes_left_in_line = dev->width << 1;
658 dma_q->lines_completed = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300659}
660
661int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300662 u8 *p_buffer, u32 bytes_to_copy)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300663{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300664 u8 *p_out_buffer = NULL;
665 u32 current_line_bytes_copied = 0;
666 struct cx231xx_buffer *buf;
667 u32 _line_size = dev->width << 1;
668 void *startwrite;
669 int offset, lencopy;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300670
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300671 buf = dev->vbi_mode.isoc_ctl.buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300672
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300673 if (buf == NULL)
674 return -EINVAL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300675
676 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
677
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300678 if (dma_q->bytes_left_in_line != _line_size) {
679 current_line_bytes_copied =
680 _line_size - dma_q->bytes_left_in_line;
681 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300682
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300683 offset = (dma_q->lines_completed * _line_size) +
684 current_line_bytes_copied;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300685
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300686 /* prepare destination address */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300687 startwrite = p_out_buffer + offset;
688
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300689 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
690 bytes_to_copy : dma_q->bytes_left_in_line;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300691
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300692 memcpy(startwrite, p_buffer, lencopy);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300693
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300694 return 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300695}
696
Mauro Carvalho Chehabcde43622009-03-03 13:31:36 -0300697u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
698 struct cx231xx_dmaqueue *dma_q)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300699{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300700 u32 height = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300701
702 height = ((dev->norm & V4L2_STD_625_50) ?
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300703 PAL_VBI_LINES : NTSC_VBI_LINES);
704 return (dma_q->lines_completed == height) ? 1 : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300705}