]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/usb/cx231xx/cx231xx-vbi.c
UBUNTU: Ubuntu-5.15.0-39.42
[mirror_ubuntu-jammy-kernel.git] / drivers / media / usb / cx231xx / cx231xx-vbi.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
e0d3bafd
SD
2/*
3 cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
4
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
84b5dbf3 6 Based on cx88 driver
e0d3bafd 7
e0d3bafd
SD
8 */
9
589dadf2 10#include "cx231xx.h"
e0d3bafd
SD
11#include <linux/init.h>
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/bitmap.h>
e0d3bafd 16#include <linux/i2c.h>
e0d3bafd
SD
17#include <linux/mm.h>
18#include <linux/mutex.h>
5a0e3ad6 19#include <linux/slab.h>
e0d3bafd
SD
20
21#include <media/v4l2-common.h>
22#include <media/v4l2-ioctl.h>
d647f0b7 23#include <media/drv-intf/msp3400.h>
e0d3bafd
SD
24#include <media/tuner.h>
25
e0d3bafd
SD
26#include "cx231xx-vbi.h"
27
84b5dbf3 28static inline void print_err_status(struct cx231xx *dev, int packet, int status)
e0d3bafd
SD
29{
30 char *errmsg = "Unknown";
31
32 switch (status) {
33 case -ENOENT:
b436e26e 34 errmsg = "unlinked synchronously";
e0d3bafd
SD
35 break;
36 case -ECONNRESET:
b436e26e 37 errmsg = "unlinked asynchronously";
e0d3bafd
SD
38 break;
39 case -ENOSR:
40 errmsg = "Buffer error (overrun)";
41 break;
42 case -EPIPE:
43 errmsg = "Stalled (device not responding)";
44 break;
45 case -EOVERFLOW:
46 errmsg = "Babble (bad cable?)";
47 break;
48 case -EPROTO:
49 errmsg = "Bit-stuff error (bad cable?)";
50 break;
51 case -EILSEQ:
52 errmsg = "CRC/Timeout (could be anything)";
53 break;
54 case -ETIME:
55 errmsg = "Device does not respond";
56 break;
57 }
58 if (packet < 0) {
336fea92 59 dev_err(dev->dev,
b7085c08 60 "URB status %d [%s].\n", status, errmsg);
e0d3bafd 61 } else {
336fea92 62 dev_err(dev->dev,
b7085c08 63 "URB packet %d, status %d [%s].\n",
ed0e3729 64 packet, status, errmsg);
e0d3bafd
SD
65 }
66}
67
68/*
69 * Controls the isoc copy of each urb packet
70 */
71static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
72{
84b5dbf3
MCC
73 struct cx231xx_dmaqueue *dma_q = urb->context;
74 int rc = 1;
e0d3bafd 75 unsigned char *p_buffer;
84b5dbf3
MCC
76 u32 bytes_parsed = 0, buffer_size = 0;
77 u8 sav_eav = 0;
e0d3bafd
SD
78
79 if (!dev)
80 return 0;
81
990862a2 82 if (dev->state & DEV_DISCONNECTED)
e0d3bafd
SD
83 return 0;
84
85 if (urb->status < 0) {
86 print_err_status(dev, -1, urb->status);
87 if (urb->status == -ENOENT)
88 return 0;
89 }
90
84b5dbf3
MCC
91 /* get buffer pointer and length */
92 p_buffer = urb->transfer_buffer;
93 buffer_size = urb->actual_length;
94
95 if (buffer_size > 0) {
84b5dbf3
MCC
96 bytes_parsed = 0;
97
98 if (dma_q->is_partial_line) {
cde4362f
MCC
99 /* Handle the case where we were working on a partial
100 line */
84b5dbf3
MCC
101 sav_eav = dma_q->last_sav;
102 } else {
cde4362f
MCC
103 /* Check for a SAV/EAV overlapping the
104 buffer boundary */
105
106 sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
84b5dbf3
MCC
107 dma_q->partial_buf,
108 &bytes_parsed);
109 }
110
111 sav_eav &= 0xF0;
cde4362f
MCC
112 /* Get the first line if we have some portion of an SAV/EAV from
113 the last buffer or a partial line */
84b5dbf3 114 if (sav_eav) {
cde4362f
MCC
115 bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
116 sav_eav, /* SAV/EAV */
117 p_buffer + bytes_parsed, /* p_buffer */
118 buffer_size - bytes_parsed); /* buffer size */
84b5dbf3
MCC
119 }
120
121 /* Now parse data that is completely in this buffer */
122 dma_q->is_partial_line = 0;
123
124 while (bytes_parsed < buffer_size) {
125 u32 bytes_used = 0;
126
b9255176
SD
127 sav_eav = cx231xx_find_next_SAV_EAV(
128 p_buffer + bytes_parsed, /* p_buffer */
129 buffer_size - bytes_parsed, /* buffer size */
130 &bytes_used); /* bytes used to get SAV/EAV */
84b5dbf3
MCC
131
132 bytes_parsed += bytes_used;
133
134 sav_eav &= 0xF0;
135 if (sav_eav && (bytes_parsed < buffer_size)) {
cde4362f 136 bytes_parsed += cx231xx_get_vbi_line(dev,
b9255176
SD
137 dma_q, sav_eav, /* SAV/EAV */
138 p_buffer+bytes_parsed, /* p_buffer */
139 buffer_size-bytes_parsed);/*buf size*/
84b5dbf3
MCC
140 }
141 }
142
b9255176
SD
143 /* Save the last four bytes of the buffer so we can
144 check the buffer boundary condition next time */
84b5dbf3
MCC
145 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
146 bytes_parsed = 0;
e0d3bafd
SD
147 }
148
149 return rc;
150}
151
152/* ------------------------------------------------------------------
153 Vbi buf operations
154 ------------------------------------------------------------------*/
155
7c617138
HV
156static int vbi_queue_setup(struct vb2_queue *vq,
157 unsigned int *nbuffers, unsigned int *nplanes,
158 unsigned int sizes[], struct device *alloc_devs[])
e0d3bafd 159{
7c617138 160 struct cx231xx *dev = vb2_get_drv_priv(vq);
84b5dbf3 161 u32 height = 0;
e0d3bafd
SD
162
163 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 164 PAL_VBI_LINES : NTSC_VBI_LINES);
e0d3bafd 165
7c617138
HV
166 *nplanes = 1;
167 sizes[0] = (dev->width * height * 2 * 2);
e0d3bafd
SD
168 return 0;
169}
170
171/* This is called *without* dev->slock held; please keep it that way */
7c617138 172static int vbi_buf_prepare(struct vb2_buffer *vb)
e0d3bafd 173{
7c617138 174 struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
84b5dbf3 175 u32 height = 0;
7c617138 176 u32 size;
e0d3bafd
SD
177
178 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 179 PAL_VBI_LINES : NTSC_VBI_LINES);
7c617138 180 size = ((dev->width << 1) * height * 2);
e0d3bafd 181
7c617138 182 if (vb2_plane_size(vb, 0) < size)
e0d3bafd 183 return -EINVAL;
7c617138 184 vb2_set_plane_payload(vb, 0, size);
e0d3bafd 185 return 0;
e0d3bafd
SD
186}
187
7c617138 188static void vbi_buf_queue(struct vb2_buffer *vb)
e0d3bafd 189{
7c617138 190 struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
84b5dbf3 191 struct cx231xx_buffer *buf =
7c617138 192 container_of(vb, struct cx231xx_buffer, vb.vb2_buf);
84b5dbf3 193 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
7c617138 194 unsigned long flags;
e0d3bafd 195
7c617138
HV
196 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
197 list_add_tail(&buf->list, &vidq->active);
198 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
199}
200
201static void return_all_buffers(struct cx231xx *dev,
202 enum vb2_buffer_state state)
203{
204 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
205 struct cx231xx_buffer *buf, *node;
206 unsigned long flags;
e0d3bafd 207
7c617138
HV
208 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
209 dev->vbi_mode.bulk_ctl.buf = NULL;
210 list_for_each_entry_safe(buf, node, &vidq->active, list) {
211 list_del(&buf->list);
212 vb2_buffer_done(&buf->vb.vb2_buf, state);
213 }
214 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
e0d3bafd
SD
215}
216
7c617138 217static int vbi_start_streaming(struct vb2_queue *vq, unsigned int count)
e0d3bafd 218{
7c617138
HV
219 struct cx231xx *dev = vb2_get_drv_priv(vq);
220 struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
221 int ret;
222
223 vidq->sequence = 0;
224 ret = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
225 CX231XX_NUM_VBI_BUFS,
226 dev->vbi_mode.alt_max_pkt_size[0],
227 cx231xx_isoc_vbi_copy);
228 if (ret)
229 return_all_buffers(dev, VB2_BUF_STATE_QUEUED);
230 return ret;
231}
e0d3bafd 232
7c617138
HV
233static void vbi_stop_streaming(struct vb2_queue *vq)
234{
235 struct cx231xx *dev = vb2_get_drv_priv(vq);
e0d3bafd 236
7c617138 237 return_all_buffers(dev, VB2_BUF_STATE_ERROR);
e0d3bafd
SD
238}
239
7c617138
HV
240struct vb2_ops cx231xx_vbi_qops = {
241 .queue_setup = vbi_queue_setup,
242 .buf_prepare = vbi_buf_prepare,
243 .buf_queue = vbi_buf_queue,
244 .start_streaming = vbi_start_streaming,
245 .stop_streaming = vbi_stop_streaming,
246 .wait_prepare = vb2_ops_wait_prepare,
247 .wait_finish = vb2_ops_wait_finish,
e0d3bafd
SD
248};
249
e0d3bafd
SD
250/* ------------------------------------------------------------------
251 URB control
252 ------------------------------------------------------------------*/
253
254/*
255 * IRQ callback, called by URB callback
256 */
257static void cx231xx_irq_vbi_callback(struct urb *urb)
258{
84b5dbf3
MCC
259 struct cx231xx_dmaqueue *dma_q = urb->context;
260 struct cx231xx_video_mode *vmode =
261 container_of(dma_q, struct cx231xx_video_mode, vidq);
262 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
aa53bf0b 263 unsigned long flags;
e0d3bafd 264
84b5dbf3
MCC
265 switch (urb->status) {
266 case 0: /* success */
267 case -ETIMEDOUT: /* NAK */
268 break;
269 case -ECONNRESET: /* kill */
270 case -ENOENT:
271 case -ESHUTDOWN:
272 return;
273 default: /* error */
336fea92 274 dev_err(dev->dev,
854bb4ec 275 "urb completion error %d.\n", urb->status);
84b5dbf3 276 break;
e0d3bafd
SD
277 }
278
279 /* Copy data from URB */
aa53bf0b 280 spin_lock_irqsave(&dev->vbi_mode.slock, flags);
da983503 281 dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
aa53bf0b 282 spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
e0d3bafd
SD
283
284 /* Reset status */
285 urb->status = 0;
286
287 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
288 if (urb->status) {
336fea92 289 dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
ed0e3729 290 urb->status);
e0d3bafd
SD
291 }
292}
293
294/*
295 * Stop and Deallocate URBs
296 */
297void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
298{
299 struct urb *urb;
300 int i;
301
336fea92 302 dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
e0d3bafd 303
64fbf444
PB
304 dev->vbi_mode.bulk_ctl.nfields = -1;
305 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
306 urb = dev->vbi_mode.bulk_ctl.urb[i];
e0d3bafd 307 if (urb) {
84b5dbf3
MCC
308 if (!irqs_disabled())
309 usb_kill_urb(urb);
310 else
311 usb_unlink_urb(urb);
e0d3bafd 312
64fbf444 313 if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
e0d3bafd 314
64fbf444 315 kfree(dev->vbi_mode.bulk_ctl.
84b5dbf3 316 transfer_buffer[i]);
64fbf444 317 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
84b5dbf3 318 NULL;
e0d3bafd
SD
319 }
320 usb_free_urb(urb);
64fbf444 321 dev->vbi_mode.bulk_ctl.urb[i] = NULL;
e0d3bafd 322 }
64fbf444 323 dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
e0d3bafd
SD
324 }
325
64fbf444
PB
326 kfree(dev->vbi_mode.bulk_ctl.urb);
327 kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
e0d3bafd 328
64fbf444
PB
329 dev->vbi_mode.bulk_ctl.urb = NULL;
330 dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
331 dev->vbi_mode.bulk_ctl.num_bufs = 0;
e0d3bafd
SD
332
333 cx231xx_capture_start(dev, 0, Vbi);
334}
335EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
336
337/*
338 * Allocate URBs and start IRQ
339 */
340int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
84b5dbf3 341 int num_bufs, int max_pkt_size,
64fbf444 342 int (*bulk_copy) (struct cx231xx *dev,
cde4362f 343 struct urb *urb))
e0d3bafd
SD
344{
345 struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
346 int i;
347 int sb_size, pipe;
348 struct urb *urb;
349 int rc;
350
336fea92 351 dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
e0d3bafd
SD
352
353 /* De-allocates all pending stuff */
354 cx231xx_uninit_vbi_isoc(dev);
355
84b5dbf3
MCC
356 /* clear if any halt */
357 usb_clear_halt(dev->udev,
358 usb_rcvbulkpipe(dev->udev,
359 dev->vbi_mode.end_point_addr));
e0d3bafd 360
64fbf444
PB
361 dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
362 dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
84b5dbf3
MCC
363 dma_q->pos = 0;
364 dma_q->is_partial_line = 0;
365 dma_q->last_sav = 0;
366 dma_q->current_field = -1;
367 dma_q->bytes_left_in_line = dev->width << 1;
368 dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
369 PAL_VBI_LINES : NTSC_VBI_LINES);
370 dma_q->lines_completed = 0;
371 for (i = 0; i < 8; i++)
372 dma_q->partial_buf[i] = 0;
373
6396bb22 374 dev->vbi_mode.bulk_ctl.urb = kcalloc(num_bufs, sizeof(void *),
cde4362f 375 GFP_KERNEL);
64fbf444 376 if (!dev->vbi_mode.bulk_ctl.urb) {
336fea92 377 dev_err(dev->dev,
b7085c08 378 "cannot alloc memory for usb buffers\n");
e0d3bafd
SD
379 return -ENOMEM;
380 }
381
64fbf444 382 dev->vbi_mode.bulk_ctl.transfer_buffer =
6396bb22 383 kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
64fbf444 384 if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
336fea92 385 dev_err(dev->dev,
b7085c08 386 "cannot allocate memory for usbtransfer\n");
64fbf444 387 kfree(dev->vbi_mode.bulk_ctl.urb);
e0d3bafd
SD
388 return -ENOMEM;
389 }
390
64fbf444
PB
391 dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
392 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd 393
64fbf444 394 sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
e0d3bafd
SD
395
396 /* allocate urbs and transfer buffers */
64fbf444 397 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
e0d3bafd
SD
398
399 urb = usb_alloc_urb(0, GFP_KERNEL);
400 if (!urb) {
e0d3bafd
SD
401 cx231xx_uninit_vbi_isoc(dev);
402 return -ENOMEM;
403 }
64fbf444 404 dev->vbi_mode.bulk_ctl.urb[i] = urb;
cd5534be 405 urb->transfer_flags = 0;
e0d3bafd 406
64fbf444 407 dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
84b5dbf3 408 kzalloc(sb_size, GFP_KERNEL);
64fbf444 409 if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
336fea92 410 dev_err(dev->dev,
8b77d1f9
TG
411 "unable to allocate %i bytes for transfer buffer %i\n",
412 sb_size, i);
e0d3bafd
SD
413 cx231xx_uninit_vbi_isoc(dev);
414 return -ENOMEM;
415 }
416
417 pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
84b5dbf3 418 usb_fill_bulk_urb(urb, dev->udev, pipe,
64fbf444 419 dev->vbi_mode.bulk_ctl.transfer_buffer[i],
84b5dbf3 420 sb_size, cx231xx_irq_vbi_callback, dma_q);
e0d3bafd
SD
421 }
422
423 init_waitqueue_head(&dma_q->wq);
424
425 /* submit urbs and enables IRQ */
64fbf444
PB
426 for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
427 rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
e0d3bafd 428 if (rc) {
336fea92 429 dev_err(dev->dev,
b7085c08 430 "submit of urb %i failed (error=%i)\n", i, rc);
e0d3bafd
SD
431 cx231xx_uninit_vbi_isoc(dev);
432 return rc;
433 }
434 }
435
84b5dbf3 436 cx231xx_capture_start(dev, 1, Vbi);
e0d3bafd
SD
437
438 return 0;
439}
84b5dbf3 440EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
e0d3bafd 441
cde4362f
MCC
442u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
443 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
e0d3bafd 444{
84b5dbf3
MCC
445 u32 bytes_copied = 0;
446 int current_field = -1;
e0d3bafd 447
84b5dbf3 448 switch (sav_eav) {
e0d3bafd 449
84b5dbf3
MCC
450 case SAV_VBI_FIELD1:
451 current_field = 1;
452 break;
e0d3bafd 453
84b5dbf3
MCC
454 case SAV_VBI_FIELD2:
455 current_field = 2;
456 break;
457 default:
458 break;
459 }
e0d3bafd 460
84b5dbf3
MCC
461 if (current_field < 0)
462 return bytes_copied;
e0d3bafd 463
84b5dbf3 464 dma_q->last_sav = sav_eav;
e0d3bafd 465
84b5dbf3
MCC
466 bytes_copied =
467 cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
468 current_field);
e0d3bafd 469
84b5dbf3 470 return bytes_copied;
e0d3bafd
SD
471}
472
473/*
474 * Announces that a buffer were filled and request the next
475 */
476static inline void vbi_buffer_filled(struct cx231xx *dev,
84b5dbf3
MCC
477 struct cx231xx_dmaqueue *dma_q,
478 struct cx231xx_buffer *buf)
e0d3bafd
SD
479{
480 /* Advice that buffer was filled */
7c617138 481 /* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.index); */
e0d3bafd 482
7c617138
HV
483 buf->vb.sequence = dma_q->sequence++;
484 buf->vb.vb2_buf.timestamp = ktime_get_ns();
e0d3bafd 485
64fbf444 486 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd 487
7c617138
HV
488 list_del(&buf->list);
489 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
e0d3bafd
SD
490}
491
84b5dbf3 492u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 493 u8 *p_line, u32 length, int field_number)
e0d3bafd 494{
84b5dbf3
MCC
495 u32 bytes_to_copy;
496 struct cx231xx_buffer *buf;
497 u32 _line_size = dev->width * 2;
e0d3bafd 498
99d35a0e
DH
499 if (dma_q->current_field == -1) {
500 /* Just starting up */
84b5dbf3 501 cx231xx_reset_vbi_buffer(dev, dma_q);
99d35a0e
DH
502 }
503
504 if (dma_q->current_field != field_number)
505 dma_q->lines_completed = 0;
e0d3bafd 506
84b5dbf3 507 /* get the buffer pointer */
64fbf444 508 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 509
84b5dbf3
MCC
510 /* Remember the field number for next time */
511 dma_q->current_field = field_number;
e0d3bafd 512
84b5dbf3
MCC
513 bytes_to_copy = dma_q->bytes_left_in_line;
514 if (bytes_to_copy > length)
515 bytes_to_copy = length;
e0d3bafd 516
84b5dbf3
MCC
517 if (dma_q->lines_completed >= dma_q->lines_per_field) {
518 dma_q->bytes_left_in_line -= bytes_to_copy;
519 dma_q->is_partial_line =
520 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
521 return 0;
522 }
e0d3bafd 523
84b5dbf3 524 dma_q->is_partial_line = 1;
e0d3bafd 525
84b5dbf3
MCC
526 /* If we don't have a buffer, just return the number of bytes we would
527 have copied if we had a buffer. */
528 if (!buf) {
529 dma_q->bytes_left_in_line -= bytes_to_copy;
530 dma_q->is_partial_line =
531 (dma_q->bytes_left_in_line == 0) ? 0 : 1;
532 return bytes_to_copy;
533 }
e0d3bafd 534
84b5dbf3
MCC
535 /* copy the data to video buffer */
536 cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
e0d3bafd 537
84b5dbf3
MCC
538 dma_q->pos += bytes_to_copy;
539 dma_q->bytes_left_in_line -= bytes_to_copy;
e0d3bafd 540
84b5dbf3 541 if (dma_q->bytes_left_in_line == 0) {
e0d3bafd 542
84b5dbf3
MCC
543 dma_q->bytes_left_in_line = _line_size;
544 dma_q->lines_completed++;
545 dma_q->is_partial_line = 0;
e0d3bafd 546
84b5dbf3 547 if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
e0d3bafd 548
84b5dbf3 549 vbi_buffer_filled(dev, dma_q, buf);
e0d3bafd 550
84b5dbf3 551 dma_q->pos = 0;
84b5dbf3 552 dma_q->lines_completed = 0;
99d35a0e 553 cx231xx_reset_vbi_buffer(dev, dma_q);
84b5dbf3
MCC
554 }
555 }
e0d3bafd 556
84b5dbf3 557 return bytes_to_copy;
e0d3bafd
SD
558}
559
560/*
561 * video-buf generic routine to get the next available buffer
562 */
563static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
84b5dbf3 564 struct cx231xx_buffer **buf)
e0d3bafd 565{
84b5dbf3
MCC
566 struct cx231xx_video_mode *vmode =
567 container_of(dma_q, struct cx231xx_video_mode, vidq);
568 struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
e0d3bafd
SD
569 char *outp;
570
571 if (list_empty(&dma_q->active)) {
336fea92 572 dev_err(dev->dev, "No active queue to serve\n");
64fbf444 573 dev->vbi_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
574 *buf = NULL;
575 return;
576 }
577
578 /* Get the next buffer */
7c617138 579 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, list);
e0d3bafd 580
25985edc 581 /* Cleans up buffer - Useful for testing for frame/URB loss */
7c617138
HV
582 outp = vb2_plane_vaddr(&(*buf)->vb.vb2_buf, 0);
583 memset(outp, 0, vb2_plane_size(&(*buf)->vb.vb2_buf, 0));
e0d3bafd 584
64fbf444 585 dev->vbi_mode.bulk_ctl.buf = *buf;
e0d3bafd
SD
586
587 return;
588}
589
84b5dbf3
MCC
590void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
591 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 592{
84b5dbf3 593 struct cx231xx_buffer *buf;
e0d3bafd 594
64fbf444 595 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 596
84b5dbf3 597 if (buf == NULL) {
84b5dbf3
MCC
598 /* first try to get the buffer */
599 get_next_vbi_buf(dma_q, &buf);
e0d3bafd 600
84b5dbf3
MCC
601 dma_q->pos = 0;
602 dma_q->current_field = -1;
603 }
e0d3bafd 604
84b5dbf3
MCC
605 dma_q->bytes_left_in_line = dev->width << 1;
606 dma_q->lines_completed = 0;
e0d3bafd
SD
607}
608
609int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 610 u8 *p_buffer, u32 bytes_to_copy)
e0d3bafd 611{
84b5dbf3
MCC
612 u8 *p_out_buffer = NULL;
613 u32 current_line_bytes_copied = 0;
614 struct cx231xx_buffer *buf;
615 u32 _line_size = dev->width << 1;
616 void *startwrite;
617 int offset, lencopy;
e0d3bafd 618
64fbf444 619 buf = dev->vbi_mode.bulk_ctl.buf;
e0d3bafd 620
cde4362f
MCC
621 if (buf == NULL)
622 return -EINVAL;
e0d3bafd 623
7c617138 624 p_out_buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
e0d3bafd 625
84b5dbf3
MCC
626 if (dma_q->bytes_left_in_line != _line_size) {
627 current_line_bytes_copied =
628 _line_size - dma_q->bytes_left_in_line;
629 }
e0d3bafd 630
cde4362f
MCC
631 offset = (dma_q->lines_completed * _line_size) +
632 current_line_bytes_copied;
e0d3bafd 633
99d35a0e
DH
634 if (dma_q->current_field == 2) {
635 /* Populate the second half of the frame */
636 offset += (dev->width * 2 * dma_q->lines_per_field);
637 }
638
84b5dbf3 639 /* prepare destination address */
e0d3bafd
SD
640 startwrite = p_out_buffer + offset;
641
cde4362f
MCC
642 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
643 bytes_to_copy : dma_q->bytes_left_in_line;
e0d3bafd 644
84b5dbf3 645 memcpy(startwrite, p_buffer, lencopy);
e0d3bafd 646
84b5dbf3 647 return 0;
e0d3bafd
SD
648}
649
cde4362f
MCC
650u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
651 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 652{
84b5dbf3 653 u32 height = 0;
e0d3bafd
SD
654
655 height = ((dev->norm & V4L2_STD_625_50) ?
84b5dbf3 656 PAL_VBI_LINES : NTSC_VBI_LINES);
99d35a0e
DH
657 if (dma_q->lines_completed == height && dma_q->current_field == 2)
658 return 1;
659 else
660 return 0;
e0d3bafd 661}