Merge remote-tracking branch 'iommu/next'
[deliverable/linux.git] / drivers / media / usb / cx231xx / cx231xx-vbi.c
CommitLineData
e0d3bafd
SD
1/*
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>
84b5dbf3 5 Based on cx88 driver
e0d3bafd
SD
6
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
589dadf2 22#include "cx231xx.h"
e0d3bafd
SD
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/bitmap.h>
e0d3bafd 28#include <linux/i2c.h>
e0d3bafd
SD
29#include <linux/mm.h>
30#include <linux/mutex.h>
5a0e3ad6 31#include <linux/slab.h>
e0d3bafd
SD
32
33#include <media/v4l2-common.h>
34#include <media/v4l2-ioctl.h>
d647f0b7 35#include <media/drv-intf/msp3400.h>
e0d3bafd
SD
36#include <media/tuner.h>
37
e0d3bafd
SD
38#include "cx231xx-vbi.h"
39
84b5dbf3 40static inline void print_err_status(struct cx231xx *dev, int packet, int status)
e0d3bafd
SD
41{
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) {
336fea92 71 dev_err(dev->dev,
b7085c08 72 "URB status %d [%s].\n", status, errmsg);
e0d3bafd 73 } else {
336fea92 74 dev_err(dev->dev,
b7085c08 75 "URB packet %d, status %d [%s].\n",
ed0e3729 76 packet, status, errmsg);
e0d3bafd
SD
77 }
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{
84b5dbf3
MCC
85 struct cx231xx_dmaqueue *dma_q = urb->context;
86 int rc = 1;
e0d3bafd 87 unsigned char *p_buffer;
84b5dbf3
MCC
88 u32 bytes_parsed = 0, buffer_size = 0;
89 u8 sav_eav = 0;
e0d3bafd
SD
90
91 if (!dev)
92 return 0;
93
990862a2 94 if (dev->state & DEV_DISCONNECTED)
e0d3bafd
SD
95 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
84b5dbf3
MCC
103 /* get buffer pointer and length */
104 p_buffer = urb->transfer_buffer;
105 buffer_size = urb->actual_length;
106
107 if (buffer_size > 0) {
84b5dbf3
MCC
108 bytes_parsed = 0;
109
110 if (dma_q->is_partial_line) {
cde4362f
MCC
111 /* Handle the case where we were working on a partial
112 line */
84b5dbf3
MCC
113 sav_eav = dma_q->last_sav;
114 } else {
cde4362f
MCC
115 /* Check for a SAV/EAV overlapping the
116 buffer boundary */
117
118 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
84b5dbf3
MCC
119 dma_q->partial_buf,
120 &bytes_parsed);
121 }
122
123 sav_eav &= 0xF0;
cde4362f
MCC
124 /* Get the first line if we have some portion of an SAV/EAV from
125 the last buffer or a partial line */
84b5dbf3 126 if (sav_eav) {
cde4362f
MCC
127 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 */
84b5dbf3
MCC
131 }
132
133 /* Now parse data that is completely in this buffer */
134 dma_q->is_partial_line = 0;
135
136 while (bytes_parsed < buffer_size) {
137 u32 bytes_used = 0;
138
b9255176
SD
139 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 */
84b5dbf3
MCC
143
144 bytes_parsed += bytes_used;
145
146 sav_eav &= 0xF0;
147 if (sav_eav && (bytes_parsed < buffer_size)) {
cde4362f 148 bytes_parsed += cx231xx_get_vbi_line(dev,
b9255176
SD
149 dma_q, sav_eav, /* SAV/EAV */
150 p_buffer+bytes_parsed, /* p_buffer */
151 buffer_size-bytes_parsed);/*buf size*/
84b5dbf3
MCC
152 }
153 }
154
b9255176
SD
155 /* Save the last four bytes of the buffer so we can
156 check the buffer boundary condition next time */
84b5dbf3
MCC
157 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
158 bytes_parsed = 0;
e0d3bafd
SD
159 }
160
161 return rc;
162}
163
164/* ------------------------------------------------------------------
165 Vbi buf operations
166 ------------------------------------------------------------------*/
167
168static int
84b5dbf3
MCC
169vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
170 unsigned int *size)
e0d3bafd
SD
171{
172 struct cx231xx_fh *fh = vq->priv_data;
84b5dbf3
MCC
173 struct cx231xx *dev = fh->dev;
174 u32 height = 0;
e0d3bafd
SD
175
176 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 177 PAL_VBI_LINES : NTSC_VBI_LINES);
e0d3bafd 178
99d35a0e 179 *size = (dev->width * height * 2 * 2);
e0d3bafd
SD
180 if (0 == *count)
181 *count = CX231XX_DEF_VBI_BUF;
182
183 if (*count < CX231XX_MIN_BUF)
184 *count = CX231XX_MIN_BUF;
185
e0d3bafd
SD
186 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{
84b5dbf3
MCC
192 struct cx231xx_fh *fh = vq->priv_data;
193 struct cx231xx *dev = fh->dev;
e0d3bafd 194 unsigned long flags = 0;
09f2082e 195 BUG_ON(in_interrupt());
e0d3bafd
SD
196
197 /* We used to wait for the buffer to finish here, but this didn't work
198 because, as we were keeping the state as VIDEOBUF_QUEUED,
199 videobuf_queue_cancel marked it as finished for us.
200 (Also, it could wedge forever if the hardware was misconfigured.)
201
202 This should be safe; by the time we get here, the buffer isn't
203 queued anymore. If we ever start marking the buffers as
204 VIDEOBUF_ACTIVE, it won't be, though.
84b5dbf3 205 */
e0d3bafd 206 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
64fbf444
PB
207 if (dev->vbi_mode.bulk_ctl.buf == buf)
208 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
209 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
210
211 videobuf_vmalloc_free(&buf->vb);
212 buf->vb.state = VIDEOBUF_NEEDS_INIT;
213}
214
215static int
216vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
84b5dbf3 217 enum v4l2_field field)
e0d3bafd 218{
84b5dbf3
MCC
219 struct cx231xx_fh *fh = vq->priv_data;
220 struct cx231xx_buffer *buf =
221 container_of(vb, struct cx231xx_buffer, vb);
222 struct cx231xx *dev = fh->dev;
223 int rc = 0, urb_init = 0;
224 u32 height = 0;
e0d3bafd
SD
225
226 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 227 PAL_VBI_LINES : NTSC_VBI_LINES);
99d35a0e 228 buf->vb.size = ((dev->width << 1) * height * 2);
e0d3bafd 229
84b5dbf3 230 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
e0d3bafd
SD
231 return -EINVAL;
232
84b5dbf3 233 buf->vb.width = dev->width;
e0d3bafd 234 buf->vb.height = height;
84b5dbf3
MCC
235 buf->vb.field = field;
236 buf->vb.field = V4L2_FIELD_SEQ_TB;
e0d3bafd
SD
237
238 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
239 rc = videobuf_iolock(vq, &buf->vb, NULL);
240 if (rc < 0)
241 goto fail;
242 }
243
64fbf444 244 if (!dev->vbi_mode.bulk_ctl.num_bufs)
e0d3bafd
SD
245 urb_init = 1;
246
247 if (urb_init) {
248 rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
84b5dbf3
MCC
249 CX231XX_NUM_VBI_BUFS,
250 dev->vbi_mode.alt_max_pkt_size[0],
251 cx231xx_isoc_vbi_copy);
e0d3bafd
SD
252 if (rc < 0)
253 goto fail;
254 }
255
256 buf->vb.state = VIDEOBUF_PREPARED;
257 return 0;
258
cde4362f 259fail:
e0d3bafd
SD
260 free_buffer(vq, buf);
261 return rc;
262}
263
264static void
265vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
266{
84b5dbf3
MCC
267 struct cx231xx_buffer *buf =
268 container_of(vb, struct cx231xx_buffer, vb);
269 struct cx231xx_fh *fh = vq->priv_data;
270 struct cx231xx *dev = fh->dev;
271 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
e0d3bafd
SD
272
273 buf->vb.state = VIDEOBUF_QUEUED;
274 list_add_tail(&buf->vb.queue, &vidq->active);
275
276}
277
278static void vbi_buffer_release(struct videobuf_queue *vq,
84b5dbf3 279 struct videobuf_buffer *vb)
e0d3bafd 280{
84b5dbf3
MCC
281 struct cx231xx_buffer *buf =
282 container_of(vb, struct cx231xx_buffer, vb);
e0d3bafd 283
e0d3bafd
SD
284
285 free_buffer(vq, buf);
286}
287
e0d3bafd 288struct videobuf_queue_ops cx231xx_vbi_qops = {
cde4362f 289 .buf_setup = vbi_buffer_setup,
84b5dbf3 290 .buf_prepare = vbi_buffer_prepare,
cde4362f 291 .buf_queue = vbi_buffer_queue,
84b5dbf3 292 .buf_release = vbi_buffer_release,
e0d3bafd
SD
293};
294
e0d3bafd
SD
295/* ------------------------------------------------------------------
296 URB control
297 ------------------------------------------------------------------*/
298
299/*
300 * IRQ callback, called by URB callback
301 */
302static void cx231xx_irq_vbi_callback(struct urb *urb)
303{
84b5dbf3
MCC
304 struct cx231xx_dmaqueue *dma_q = urb->context;
305 struct cx231xx_video_mode *vmode =
306 container_of(dma_q, struct cx231xx_video_mode, vidq);
307 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
e0d3bafd 308
84b5dbf3
MCC
309 switch (urb->status) {
310 case 0: /* success */
311 case -ETIMEDOUT: /* NAK */
312 break;
313 case -ECONNRESET: /* kill */
314 case -ENOENT:
315 case -ESHUTDOWN:
316 return;
317 default: /* error */
336fea92 318 dev_err(dev->dev,
b7085c08 319 "urb completition error %d.\n", urb->status);
84b5dbf3 320 break;
e0d3bafd
SD
321 }
322
323 /* Copy data from URB */
324 spin_lock(&dev->vbi_mode.slock);
da983503 325 dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
e0d3bafd
SD
326 spin_unlock(&dev->vbi_mode.slock);
327
328 /* Reset status */
329 urb->status = 0;
330
331 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
332 if (urb->status) {
336fea92 333 dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
ed0e3729 334 urb->status);
e0d3bafd
SD
335 }
336}
337
338/*
339 * Stop and Deallocate URBs
340 */
341void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
342{
343 struct urb *urb;
344 int i;
345
336fea92 346 dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
e0d3bafd 347
64fbf444
PB
348 dev->vbi_mode.bulk_ctl.nfields = -1;
349 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
350 urb = dev->vbi_mode.bulk_ctl.urb[i];
e0d3bafd 351 if (urb) {
84b5dbf3
MCC
352 if (!irqs_disabled())
353 usb_kill_urb(urb);
354 else
355 usb_unlink_urb(urb);
e0d3bafd 356
64fbf444 357 if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
e0d3bafd 358
64fbf444 359 kfree(dev->vbi_mode.bulk_ctl.
84b5dbf3 360 transfer_buffer[i]);
64fbf444 361 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
84b5dbf3 362 NULL;
e0d3bafd
SD
363 }
364 usb_free_urb(urb);
64fbf444 365 dev->vbi_mode.bulk_ctl.urb[i] = NULL;
e0d3bafd 366 }
64fbf444 367 dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
e0d3bafd
SD
368 }
369
64fbf444
PB
370 kfree(dev->vbi_mode.bulk_ctl.urb);
371 kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
e0d3bafd 372
64fbf444
PB
373 dev->vbi_mode.bulk_ctl.urb = NULL;
374 dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
375 dev->vbi_mode.bulk_ctl.num_bufs = 0;
e0d3bafd
SD
376
377 cx231xx_capture_start(dev, 0, Vbi);
378}
379EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
380
381/*
382 * Allocate URBs and start IRQ
383 */
384int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
84b5dbf3 385 int num_bufs, int max_pkt_size,
64fbf444 386 int (*bulk_copy) (struct cx231xx *dev,
cde4362f 387 struct urb *urb))
e0d3bafd
SD
388{
389 struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
390 int i;
391 int sb_size, pipe;
392 struct urb *urb;
393 int rc;
394
336fea92 395 dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
e0d3bafd
SD
396
397 /* De-allocates all pending stuff */
398 cx231xx_uninit_vbi_isoc(dev);
399
84b5dbf3
MCC
400 /* clear if any halt */
401 usb_clear_halt(dev->udev,
402 usb_rcvbulkpipe(dev->udev,
403 dev->vbi_mode.end_point_addr));
e0d3bafd 404
64fbf444
PB
405 dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
406 dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
84b5dbf3
MCC
407 dma_q->pos = 0;
408 dma_q->is_partial_line = 0;
409 dma_q->last_sav = 0;
410 dma_q->current_field = -1;
411 dma_q->bytes_left_in_line = dev->width << 1;
412 dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
413 PAL_VBI_LINES : NTSC_VBI_LINES);
414 dma_q->lines_completed = 0;
415 for (i = 0; i < 8; i++)
416 dma_q->partial_buf[i] = 0;
417
64fbf444 418 dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
cde4362f 419 GFP_KERNEL);
64fbf444 420 if (!dev->vbi_mode.bulk_ctl.urb) {
336fea92 421 dev_err(dev->dev,
b7085c08 422 "cannot alloc memory for usb buffers\n");
e0d3bafd
SD
423 return -ENOMEM;
424 }
425
64fbf444 426 dev->vbi_mode.bulk_ctl.transfer_buffer =
84b5dbf3 427 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
64fbf444 428 if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
336fea92 429 dev_err(dev->dev,
b7085c08 430 "cannot allocate memory for usbtransfer\n");
64fbf444 431 kfree(dev->vbi_mode.bulk_ctl.urb);
e0d3bafd
SD
432 return -ENOMEM;
433 }
434
64fbf444
PB
435 dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
436 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd 437
64fbf444 438 sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
e0d3bafd
SD
439
440 /* allocate urbs and transfer buffers */
64fbf444 441 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
e0d3bafd
SD
442
443 urb = usb_alloc_urb(0, GFP_KERNEL);
444 if (!urb) {
e0d3bafd
SD
445 cx231xx_uninit_vbi_isoc(dev);
446 return -ENOMEM;
447 }
64fbf444 448 dev->vbi_mode.bulk_ctl.urb[i] = urb;
cd5534be 449 urb->transfer_flags = 0;
e0d3bafd 450
64fbf444 451 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
84b5dbf3 452 kzalloc(sb_size, GFP_KERNEL);
64fbf444 453 if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
336fea92 454 dev_err(dev->dev,
b7085c08 455 "unable to allocate %i bytes for transfer buffer %i%s\n",
ed0e3729
MCC
456 sb_size, i,
457 in_interrupt() ? " while in int" : "");
e0d3bafd
SD
458 cx231xx_uninit_vbi_isoc(dev);
459 return -ENOMEM;
460 }
461
462 pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
84b5dbf3 463 usb_fill_bulk_urb(urb, dev->udev, pipe,
64fbf444 464 dev->vbi_mode.bulk_ctl.transfer_buffer[i],
84b5dbf3 465 sb_size, cx231xx_irq_vbi_callback, dma_q);
e0d3bafd
SD
466 }
467
468 init_waitqueue_head(&dma_q->wq);
469
470 /* submit urbs and enables IRQ */
64fbf444
PB
471 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
472 rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
e0d3bafd 473 if (rc) {
336fea92 474 dev_err(dev->dev,
b7085c08 475 "submit of urb %i failed (error=%i)\n", i, rc);
e0d3bafd
SD
476 cx231xx_uninit_vbi_isoc(dev);
477 return rc;
478 }
479 }
480
84b5dbf3 481 cx231xx_capture_start(dev, 1, Vbi);
e0d3bafd
SD
482
483 return 0;
484}
84b5dbf3 485EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
e0d3bafd 486
cde4362f
MCC
487u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
488 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
e0d3bafd 489{
84b5dbf3
MCC
490 u32 bytes_copied = 0;
491 int current_field = -1;
e0d3bafd 492
84b5dbf3 493 switch (sav_eav) {
e0d3bafd 494
84b5dbf3
MCC
495 case SAV_VBI_FIELD1:
496 current_field = 1;
497 break;
e0d3bafd 498
84b5dbf3
MCC
499 case SAV_VBI_FIELD2:
500 current_field = 2;
501 break;
502 default:
503 break;
504 }
e0d3bafd 505
84b5dbf3
MCC
506 if (current_field < 0)
507 return bytes_copied;
e0d3bafd 508
84b5dbf3 509 dma_q->last_sav = sav_eav;
e0d3bafd 510
84b5dbf3
MCC
511 bytes_copied =
512 cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
513 current_field);
e0d3bafd 514
84b5dbf3 515 return bytes_copied;
e0d3bafd
SD
516}
517
518/*
519 * Announces that a buffer were filled and request the next
520 */
521static inline void vbi_buffer_filled(struct cx231xx *dev,
84b5dbf3
MCC
522 struct cx231xx_dmaqueue *dma_q,
523 struct cx231xx_buffer *buf)
e0d3bafd
SD
524{
525 /* Advice that buffer was filled */
336fea92 526 /* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
e0d3bafd
SD
527
528 buf->vb.state = VIDEOBUF_DONE;
529 buf->vb.field_count++;
8e6057b5 530 v4l2_get_timestamp(&buf->vb.ts);
e0d3bafd 531
64fbf444 532 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
533
534 list_del(&buf->vb.queue);
535 wake_up(&buf->vb.done);
536}
537
84b5dbf3 538u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 539 u8 *p_line, u32 length, int field_number)
e0d3bafd 540{
84b5dbf3
MCC
541 u32 bytes_to_copy;
542 struct cx231xx_buffer *buf;
543 u32 _line_size = dev->width * 2;
e0d3bafd 544
99d35a0e
DH
545 if (dma_q->current_field == -1) {
546 /* Just starting up */
84b5dbf3 547 cx231xx_reset_vbi_buffer(dev, dma_q);
99d35a0e
DH
548 }
549
550 if (dma_q->current_field != field_number)
551 dma_q->lines_completed = 0;
e0d3bafd 552
84b5dbf3 553 /* get the buffer pointer */
64fbf444 554 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 555
84b5dbf3
MCC
556 /* Remember the field number for next time */
557 dma_q->current_field = field_number;
e0d3bafd 558
84b5dbf3
MCC
559 bytes_to_copy = dma_q->bytes_left_in_line;
560 if (bytes_to_copy > length)
561 bytes_to_copy = length;
e0d3bafd 562
84b5dbf3
MCC
563 if (dma_q->lines_completed >= dma_q->lines_per_field) {
564 dma_q->bytes_left_in_line -= bytes_to_copy;
565 dma_q->is_partial_line =
566 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
567 return 0;
568 }
e0d3bafd 569
84b5dbf3 570 dma_q->is_partial_line = 1;
e0d3bafd 571
84b5dbf3
MCC
572 /* If we don't have a buffer, just return the number of bytes we would
573 have copied if we had a buffer. */
574 if (!buf) {
575 dma_q->bytes_left_in_line -= bytes_to_copy;
576 dma_q->is_partial_line =
577 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
578 return bytes_to_copy;
579 }
e0d3bafd 580
84b5dbf3
MCC
581 /* copy the data to video buffer */
582 cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
e0d3bafd 583
84b5dbf3
MCC
584 dma_q->pos += bytes_to_copy;
585 dma_q->bytes_left_in_line -= bytes_to_copy;
e0d3bafd 586
84b5dbf3 587 if (dma_q->bytes_left_in_line == 0) {
e0d3bafd 588
84b5dbf3
MCC
589 dma_q->bytes_left_in_line = _line_size;
590 dma_q->lines_completed++;
591 dma_q->is_partial_line = 0;
e0d3bafd 592
84b5dbf3 593 if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
e0d3bafd 594
84b5dbf3 595 vbi_buffer_filled(dev, dma_q, buf);
e0d3bafd 596
84b5dbf3 597 dma_q->pos = 0;
84b5dbf3 598 dma_q->lines_completed = 0;
99d35a0e 599 cx231xx_reset_vbi_buffer(dev, dma_q);
84b5dbf3
MCC
600 }
601 }
e0d3bafd 602
84b5dbf3 603 return bytes_to_copy;
e0d3bafd
SD
604}
605
606/*
607 * video-buf generic routine to get the next available buffer
608 */
609static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
84b5dbf3 610 struct cx231xx_buffer **buf)
e0d3bafd 611{
84b5dbf3
MCC
612 struct cx231xx_video_mode *vmode =
613 container_of(dma_q, struct cx231xx_video_mode, vidq);
614 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
e0d3bafd
SD
615 char *outp;
616
617 if (list_empty(&dma_q->active)) {
336fea92 618 dev_err(dev->dev, "No active queue to serve\n");
64fbf444 619 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
620 *buf = NULL;
621 return;
622 }
623
624 /* Get the next buffer */
625 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
626
25985edc 627 /* Cleans up buffer - Useful for testing for frame/URB loss */
e0d3bafd
SD
628 outp = videobuf_to_vmalloc(&(*buf)->vb);
629 memset(outp, 0, (*buf)->vb.size);
630
64fbf444 631 dev->vbi_mode.bulk_ctl.buf = *buf;
e0d3bafd
SD
632
633 return;
634}
635
84b5dbf3
MCC
636void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
637 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 638{
84b5dbf3 639 struct cx231xx_buffer *buf;
e0d3bafd 640
64fbf444 641 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 642
84b5dbf3 643 if (buf == NULL) {
84b5dbf3
MCC
644 /* first try to get the buffer */
645 get_next_vbi_buf(dma_q, &buf);
e0d3bafd 646
84b5dbf3
MCC
647 dma_q->pos = 0;
648 dma_q->current_field = -1;
649 }
e0d3bafd 650
84b5dbf3
MCC
651 dma_q->bytes_left_in_line = dev->width << 1;
652 dma_q->lines_completed = 0;
e0d3bafd
SD
653}
654
655int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 656 u8 *p_buffer, u32 bytes_to_copy)
e0d3bafd 657{
84b5dbf3
MCC
658 u8 *p_out_buffer = NULL;
659 u32 current_line_bytes_copied = 0;
660 struct cx231xx_buffer *buf;
661 u32 _line_size = dev->width << 1;
662 void *startwrite;
663 int offset, lencopy;
e0d3bafd 664
64fbf444 665 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 666
cde4362f
MCC
667 if (buf == NULL)
668 return -EINVAL;
e0d3bafd
SD
669
670 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
671
84b5dbf3
MCC
672 if (dma_q->bytes_left_in_line != _line_size) {
673 current_line_bytes_copied =
674 _line_size - dma_q->bytes_left_in_line;
675 }
e0d3bafd 676
cde4362f
MCC
677 offset = (dma_q->lines_completed * _line_size) +
678 current_line_bytes_copied;
e0d3bafd 679
99d35a0e
DH
680 if (dma_q->current_field == 2) {
681 /* Populate the second half of the frame */
682 offset += (dev->width * 2 * dma_q->lines_per_field);
683 }
684
84b5dbf3 685 /* prepare destination address */
e0d3bafd
SD
686 startwrite = p_out_buffer + offset;
687
cde4362f
MCC
688 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
689 bytes_to_copy : dma_q->bytes_left_in_line;
e0d3bafd 690
84b5dbf3 691 memcpy(startwrite, p_buffer, lencopy);
e0d3bafd 692
84b5dbf3 693 return 0;
e0d3bafd
SD
694}
695
cde4362f
MCC
696u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
697 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 698{
84b5dbf3 699 u32 height = 0;
e0d3bafd
SD
700
701 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 702 PAL_VBI_LINES : NTSC_VBI_LINES);
99d35a0e
DH
703 if (dma_q->lines_completed == height && dma_q->current_field == 2)
704 return 1;
705 else
706 return 0;
e0d3bafd 707}
This page took 0.501388 seconds and 5 git commands to generate.