1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2012 Ezequiel Garcia
6 * <elezegarcia--a.t--gmail.com>
8 * Based on Easycap driver by R.M. Thomas
9 * Copyright (C) 2010 R.M. Thomas
10 * <rmthomas--a.t--sciolus.org>
13 #include <linux/module.h>
14 #include <linux/usb.h>
15 #include <linux/slab.h>
16 #include <linux/ratelimit.h>
20 static unsigned int debug
;
21 module_param(debug
, int, 0644);
22 MODULE_PARM_DESC(debug
, "enable debug messages");
24 static inline void print_err_status(struct stk1160
*dev
,
25 int packet
, int status
)
27 char *errmsg
= "Unknown";
31 errmsg
= "unlinked synchronously";
34 errmsg
= "unlinked asynchronously";
37 errmsg
= "Buffer error (overrun)";
40 errmsg
= "Stalled (device not responding)";
43 errmsg
= "Babble (bad cable?)";
46 errmsg
= "Bit-stuff error (bad cable?)";
49 errmsg
= "CRC/Timeout (could be anything)";
52 errmsg
= "Device does not respond";
57 printk_ratelimited(KERN_WARNING
"URB status %d [%s].\n",
60 printk_ratelimited(KERN_INFO
"URB packet %d, status %d [%s].\n",
61 packet
, status
, errmsg
);
65 struct stk1160_buffer
*stk1160_next_buffer(struct stk1160
*dev
)
67 struct stk1160_buffer
*buf
= NULL
;
68 unsigned long flags
= 0;
70 /* Current buffer must be NULL when this functions gets called */
71 WARN_ON(dev
->isoc_ctl
.buf
);
73 spin_lock_irqsave(&dev
->buf_lock
, flags
);
74 if (!list_empty(&dev
->avail_bufs
)) {
75 buf
= list_first_entry(&dev
->avail_bufs
,
76 struct stk1160_buffer
, list
);
79 spin_unlock_irqrestore(&dev
->buf_lock
, flags
);
85 void stk1160_buffer_done(struct stk1160
*dev
)
87 struct stk1160_buffer
*buf
= dev
->isoc_ctl
.buf
;
89 buf
->vb
.sequence
= dev
->sequence
++;
90 buf
->vb
.field
= V4L2_FIELD_INTERLACED
;
91 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
93 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, buf
->bytesused
);
94 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
96 dev
->isoc_ctl
.buf
= NULL
;
100 void stk1160_copy_video(struct stk1160
*dev
, u8
*src
, int len
)
102 int linesdone
, lineoff
, lencopy
, offset
;
103 int bytesperline
= dev
->width
* 2;
104 struct stk1160_buffer
*buf
= dev
->isoc_ctl
.buf
;
109 * TODO: These stk1160_dbg are very spammy!
110 * We should check why we are getting them.
112 * UPDATE: One of the reasons (the only one?) for getting these
113 * is incorrect standard (mismatch between expected and configured).
114 * So perhaps, we could add a counter for errors. When the counter
115 * reaches some value, we simply stop streaming.
123 linesdone
= buf
->pos
/ bytesperline
;
124 lineoff
= buf
->pos
% bytesperline
; /* offset in current line */
129 /* Multiply linesdone by two, to take account of the other field */
130 dst
+= linesdone
* bytesperline
* 2 + lineoff
;
132 /* Copy the remaining of current line */
133 lencopy
= min(remain
, bytesperline
- lineoff
);
136 * Check if we have enough space left in the buffer.
137 * In that case, we force loop exit after copy.
139 offset
= dst
- (u8
*)buf
->mem
;
140 if (offset
> buf
->length
) {
141 dev_warn_ratelimited(dev
->dev
, "out of bounds offset\n");
144 if (lencopy
> buf
->length
- offset
) {
145 lencopy
= buf
->length
- offset
;
149 /* Check if the copy is done */
150 if (lencopy
== 0 || remain
== 0)
153 /* Let the bug hunt begin! sanity checks! */
155 printk_ratelimited(KERN_DEBUG
"copy skipped: negative lencopy\n");
159 if ((unsigned long)dst
+ lencopy
>
160 (unsigned long)buf
->mem
+ buf
->length
) {
161 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
165 memcpy(dst
, src
, lencopy
);
167 buf
->bytesused
+= lencopy
;
171 /* Copy current field line by line, interlacing with the other field */
174 dst
+= lencopy
+ bytesperline
;
177 /* Copy one line at a time */
178 lencopy
= min(remain
, bytesperline
);
181 * Check if we have enough space left in the buffer.
182 * In that case, we force loop exit after copy.
184 offset
= dst
- (u8
*)buf
->mem
;
185 if (offset
> buf
->length
) {
186 dev_warn_ratelimited(dev
->dev
, "offset out of bounds\n");
189 if (lencopy
> buf
->length
- offset
) {
190 lencopy
= buf
->length
- offset
;
194 /* Check if the copy is done */
195 if (lencopy
== 0 || remain
== 0)
199 printk_ratelimited(KERN_WARNING
"stk1160: negative lencopy detected\n");
203 if ((unsigned long)dst
+ lencopy
>
204 (unsigned long)buf
->mem
+ buf
->length
) {
205 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
209 memcpy(dst
, src
, lencopy
);
212 buf
->bytesused
+= lencopy
;
218 * Controls the isoc copy of each urb packet
220 static void stk1160_process_isoc(struct stk1160
*dev
, struct urb
*urb
)
226 stk1160_warn("%s called with null device\n", __func__
);
230 if (urb
->status
< 0) {
231 /* Print status and drop current packet (or field?) */
232 print_err_status(dev
, -1, urb
->status
);
236 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
237 status
= urb
->iso_frame_desc
[i
].status
;
239 print_err_status(dev
, i
, status
);
243 /* Get packet actual length and pointer to data */
244 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
245 len
= urb
->iso_frame_desc
[i
].actual_length
;
252 * An 8-byte packet sequence means end of field.
253 * So if we don't have any packet, we start receiving one now
254 * and if we do have a packet, then we are done with it.
256 * These end of field packets are always 0xc0 or 0x80,
257 * but not always 8-byte long so we don't check packet length.
262 * If first byte is 0xc0 then we received
263 * second field, and frame has ended.
265 if (dev
->isoc_ctl
.buf
!= NULL
)
266 stk1160_buffer_done(dev
);
268 dev
->isoc_ctl
.buf
= stk1160_next_buffer(dev
);
269 if (dev
->isoc_ctl
.buf
== NULL
)
274 * If we don't have a buffer here, then it means we
275 * haven't found the start mark sequence.
277 if (dev
->isoc_ctl
.buf
== NULL
)
280 if (p
[0] == 0xc0 || p
[0] == 0x80) {
282 /* We set next packet parity and
283 * continue to get next one
285 dev
->isoc_ctl
.buf
->odd
= *p
& 0x40;
286 dev
->isoc_ctl
.buf
->pos
= 0;
290 stk1160_copy_video(dev
, p
, len
);
296 * IRQ callback, called by URB callback
298 static void stk1160_isoc_irq(struct urb
*urb
)
301 struct stk1160_urb
*stk_urb
= urb
->context
;
302 struct stk1160
*dev
= stk_urb
->dev
;
303 struct device
*dma_dev
= stk1160_get_dmadev(dev
);
305 switch (urb
->status
) {
308 case -ECONNRESET
: /* kill */
311 /* TODO: check uvc driver: he frees the queue here */
314 stk1160_err("urb error! status %d\n", urb
->status
);
318 invalidate_kernel_vmap_range(stk_urb
->transfer_buffer
,
319 urb
->transfer_buffer_length
);
320 dma_sync_sgtable_for_cpu(dma_dev
, stk_urb
->sgt
, DMA_FROM_DEVICE
);
322 stk1160_process_isoc(dev
, urb
);
324 /* Reset urb buffers */
325 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
326 urb
->iso_frame_desc
[i
].status
= 0;
327 urb
->iso_frame_desc
[i
].actual_length
= 0;
330 dma_sync_sgtable_for_device(dma_dev
, stk_urb
->sgt
, DMA_FROM_DEVICE
);
331 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
333 stk1160_err("urb re-submit failed (%d)\n", rc
);
338 * This function can't be called in atomic context
340 void stk1160_cancel_isoc(struct stk1160
*dev
)
342 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
345 * This check is not necessary, but we add it
346 * to avoid a spurious debug message
351 stk1160_dbg("killing %d urbs...\n", num_bufs
);
353 for (i
= 0; i
< num_bufs
; i
++) {
356 * To kill urbs we can't be in atomic context.
357 * We don't care for NULL pointer since
358 * usb_kill_urb allows it.
360 usb_kill_urb(dev
->isoc_ctl
.urb_ctl
[i
].urb
);
363 stk1160_dbg("all urbs killed\n");
366 static void stk_free_urb(struct stk1160
*dev
, struct stk1160_urb
*stk_urb
)
368 struct device
*dma_dev
= stk1160_get_dmadev(dev
);
370 dma_vunmap_noncontiguous(dma_dev
, stk_urb
->transfer_buffer
);
371 dma_free_noncontiguous(dma_dev
, stk_urb
->urb
->transfer_buffer_length
,
372 stk_urb
->sgt
, DMA_FROM_DEVICE
);
373 usb_free_urb(stk_urb
->urb
);
375 stk_urb
->transfer_buffer
= NULL
;
383 * Releases urb and transfer buffers
384 * Obviusly, associated urb must be killed before releasing it.
386 void stk1160_free_isoc(struct stk1160
*dev
)
388 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
390 stk1160_dbg("freeing %d urb buffers...\n", num_bufs
);
392 for (i
= 0; i
< num_bufs
; i
++)
393 stk_free_urb(dev
, &dev
->isoc_ctl
.urb_ctl
[i
]);
395 dev
->isoc_ctl
.num_bufs
= 0;
397 stk1160_dbg("all urb buffers freed\n");
401 * Helper for cancelling and freeing urbs
402 * This function can't be called in atomic context
404 void stk1160_uninit_isoc(struct stk1160
*dev
)
406 stk1160_cancel_isoc(dev
);
407 stk1160_free_isoc(dev
);
410 static int stk1160_fill_urb(struct stk1160
*dev
, struct stk1160_urb
*stk_urb
,
411 int sb_size
, int max_packets
)
413 struct device
*dma_dev
= stk1160_get_dmadev(dev
);
415 stk_urb
->urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
418 stk_urb
->sgt
= dma_alloc_noncontiguous(dma_dev
, sb_size
,
419 DMA_FROM_DEVICE
, GFP_KERNEL
, 0);
422 * If the buffer allocation failed, we exit but return 0 since
423 * we allow the driver working with less buffers
428 stk_urb
->transfer_buffer
= dma_vmap_noncontiguous(dma_dev
, sb_size
,
430 if (!stk_urb
->transfer_buffer
)
433 stk_urb
->dma
= stk_urb
->sgt
->sgl
->dma_address
;
437 dma_free_noncontiguous(dma_dev
, sb_size
, stk_urb
->sgt
, DMA_FROM_DEVICE
);
440 usb_free_urb(stk_urb
->urb
);
448 int stk1160_alloc_isoc(struct stk1160
*dev
)
451 int i
, j
, k
, sb_size
, max_packets
, num_bufs
;
455 * It may be necessary to release isoc here,
456 * since isoc are only released on disconnection.
457 * (see new_pkt_size flag)
459 if (dev
->isoc_ctl
.num_bufs
)
460 stk1160_uninit_isoc(dev
);
462 stk1160_dbg("allocating urbs...\n");
464 num_bufs
= STK1160_NUM_BUFS
;
465 max_packets
= STK1160_NUM_PACKETS
;
466 sb_size
= max_packets
* dev
->max_pkt_size
;
468 dev
->isoc_ctl
.buf
= NULL
;
469 dev
->isoc_ctl
.max_pkt_size
= dev
->max_pkt_size
;
471 /* allocate urbs and transfer buffers */
472 for (i
= 0; i
< num_bufs
; i
++) {
474 ret
= stk1160_fill_urb(dev
, &dev
->isoc_ctl
.urb_ctl
[i
],
475 sb_size
, max_packets
);
479 urb
= dev
->isoc_ctl
.urb_ctl
[i
].urb
;
482 /* Not enough transfer buffers, so just give up */
483 if (i
< STK1160_MIN_BUFS
)
487 memset(dev
->isoc_ctl
.urb_ctl
[i
].transfer_buffer
, 0, sb_size
);
490 * FIXME: Where can I get the endpoint?
492 urb
->dev
= dev
->udev
;
493 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, STK1160_EP_VIDEO
);
494 urb
->transfer_buffer
= dev
->isoc_ctl
.urb_ctl
[i
].transfer_buffer
;
495 urb
->transfer_buffer_length
= sb_size
;
496 urb
->complete
= stk1160_isoc_irq
;
497 urb
->context
= &dev
->isoc_ctl
.urb_ctl
[i
];
499 urb
->start_frame
= 0;
500 urb
->number_of_packets
= max_packets
;
501 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
502 urb
->transfer_dma
= dev
->isoc_ctl
.urb_ctl
[i
].dma
;
505 for (j
= 0; j
< max_packets
; j
++) {
506 urb
->iso_frame_desc
[j
].offset
= k
;
507 urb
->iso_frame_desc
[j
].length
=
508 dev
->isoc_ctl
.max_pkt_size
;
509 k
+= dev
->isoc_ctl
.max_pkt_size
;
513 stk1160_dbg("%d urbs allocated\n", num_bufs
);
515 /* At last we can say we have some buffers */
516 dev
->isoc_ctl
.num_bufs
= num_bufs
;
522 * Failed to allocate desired buffer count. However, we may have
523 * enough to work fine, so we just free the extra urb,
524 * store the allocated count and keep going, fingers crossed!
527 stk1160_warn("%d urbs allocated. Trying to continue...\n", i
);
529 dev
->isoc_ctl
.num_bufs
= i
;
534 /* Save the allocated buffers so far, so we can properly free them */
535 dev
->isoc_ctl
.num_bufs
= i
;
536 stk1160_free_isoc(dev
);