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