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
;
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 1) check why we are getting them
111 * and 2) add ratelimit.
113 * UPDATE: One of the reasons (the only one?) for getting these
114 * is incorrect standard (mismatch between expected and configured).
115 * So perhaps, we could add a counter for errors. When the counter
116 * reaches some value, we simply stop streaming.
124 linesdone
= buf
->pos
/ bytesperline
;
125 lineoff
= buf
->pos
% bytesperline
; /* offset in current line */
130 /* Multiply linesdone by two, to take account of the other field */
131 dst
+= linesdone
* bytesperline
* 2 + lineoff
;
133 /* Copy the remaining of current line */
134 if (remain
< (bytesperline
- lineoff
))
137 lencopy
= bytesperline
- lineoff
;
140 * Check if we have enough space left in the buffer.
141 * In that case, we force loop exit after copy.
143 if (lencopy
> buf
->bytesused
- buf
->length
) {
144 lencopy
= buf
->bytesused
- buf
->length
;
148 /* Check if the copy is done */
149 if (lencopy
== 0 || remain
== 0)
152 /* Let the bug hunt begin! sanity checks! */
154 stk1160_dbg("copy skipped: negative lencopy\n");
158 if ((unsigned long)dst
+ lencopy
>
159 (unsigned long)buf
->mem
+ buf
->length
) {
160 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
164 memcpy(dst
, src
, lencopy
);
166 buf
->bytesused
+= lencopy
;
170 /* Copy current field line by line, interlacing with the other field */
173 dst
+= lencopy
+ bytesperline
;
176 /* Copy one line at a time */
177 if (remain
< bytesperline
)
180 lencopy
= bytesperline
;
183 * Check if we have enough space left in the buffer.
184 * In that case, we force loop exit after copy.
186 if (lencopy
> buf
->bytesused
- buf
->length
) {
187 lencopy
= buf
->bytesused
- buf
->length
;
191 /* Check if the copy is done */
192 if (lencopy
== 0 || remain
== 0)
196 printk_ratelimited(KERN_WARNING
"stk1160: negative lencopy detected\n");
200 if ((unsigned long)dst
+ lencopy
>
201 (unsigned long)buf
->mem
+ buf
->length
) {
202 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
206 memcpy(dst
, src
, lencopy
);
209 buf
->bytesused
+= lencopy
;
215 * Controls the isoc copy of each urb packet
217 static void stk1160_process_isoc(struct stk1160
*dev
, struct urb
*urb
)
223 stk1160_warn("%s called with null device\n", __func__
);
227 if (urb
->status
< 0) {
228 /* Print status and drop current packet (or field?) */
229 print_err_status(dev
, -1, urb
->status
);
233 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
234 status
= urb
->iso_frame_desc
[i
].status
;
236 print_err_status(dev
, i
, status
);
240 /* Get packet actual length and pointer to data */
241 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
242 len
= urb
->iso_frame_desc
[i
].actual_length
;
249 * An 8-byte packet sequence means end of field.
250 * So if we don't have any packet, we start receiving one now
251 * and if we do have a packet, then we are done with it.
253 * These end of field packets are always 0xc0 or 0x80,
254 * but not always 8-byte long so we don't check packet length.
259 * If first byte is 0xc0 then we received
260 * second field, and frame has ended.
262 if (dev
->isoc_ctl
.buf
!= NULL
)
263 stk1160_buffer_done(dev
);
265 dev
->isoc_ctl
.buf
= stk1160_next_buffer(dev
);
266 if (dev
->isoc_ctl
.buf
== NULL
)
271 * If we don't have a buffer here, then it means we
272 * haven't found the start mark sequence.
274 if (dev
->isoc_ctl
.buf
== NULL
)
277 if (p
[0] == 0xc0 || p
[0] == 0x80) {
279 /* We set next packet parity and
280 * continue to get next one
282 dev
->isoc_ctl
.buf
->odd
= *p
& 0x40;
283 dev
->isoc_ctl
.buf
->pos
= 0;
287 stk1160_copy_video(dev
, p
, len
);
293 * IRQ callback, called by URB callback
295 static void stk1160_isoc_irq(struct urb
*urb
)
298 struct stk1160
*dev
= urb
->context
;
300 switch (urb
->status
) {
303 case -ECONNRESET
: /* kill */
306 /* TODO: check uvc driver: he frees the queue here */
309 stk1160_err("urb error! status %d\n", urb
->status
);
313 stk1160_process_isoc(dev
, urb
);
315 /* Reset urb buffers */
316 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
317 urb
->iso_frame_desc
[i
].status
= 0;
318 urb
->iso_frame_desc
[i
].actual_length
= 0;
321 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
323 stk1160_err("urb re-submit failed (%d)\n", rc
);
328 * This function can't be called in atomic context
330 void stk1160_cancel_isoc(struct stk1160
*dev
)
332 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
335 * This check is not necessary, but we add it
336 * to avoid a spurious debug message
341 stk1160_dbg("killing %d urbs...\n", num_bufs
);
343 for (i
= 0; i
< num_bufs
; i
++) {
346 * To kill urbs we can't be in atomic context.
347 * We don't care for NULL pointer since
348 * usb_kill_urb allows it.
350 usb_kill_urb(dev
->isoc_ctl
.urb
[i
]);
353 stk1160_dbg("all urbs killed\n");
357 * Releases urb and transfer buffers
358 * Obviusly, associated urb must be killed before releasing it.
360 void stk1160_free_isoc(struct stk1160
*dev
)
363 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
365 stk1160_dbg("freeing %d urb buffers...\n", num_bufs
);
367 for (i
= 0; i
< num_bufs
; i
++) {
369 urb
= dev
->isoc_ctl
.urb
[i
];
372 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
373 #ifndef CONFIG_DMA_NONCOHERENT
374 usb_free_coherent(dev
->udev
,
375 urb
->transfer_buffer_length
,
376 dev
->isoc_ctl
.transfer_buffer
[i
],
379 kfree(dev
->isoc_ctl
.transfer_buffer
[i
]);
383 dev
->isoc_ctl
.urb
[i
] = NULL
;
385 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
388 kfree(dev
->isoc_ctl
.urb
);
389 kfree(dev
->isoc_ctl
.transfer_buffer
);
391 dev
->isoc_ctl
.urb
= NULL
;
392 dev
->isoc_ctl
.transfer_buffer
= NULL
;
393 dev
->isoc_ctl
.num_bufs
= 0;
395 stk1160_dbg("all urb buffers freed\n");
399 * Helper for cancelling and freeing urbs
400 * This function can't be called in atomic context
402 void stk1160_uninit_isoc(struct stk1160
*dev
)
404 stk1160_cancel_isoc(dev
);
405 stk1160_free_isoc(dev
);
411 int stk1160_alloc_isoc(struct stk1160
*dev
)
414 int i
, j
, k
, sb_size
, max_packets
, num_bufs
;
417 * It may be necessary to release isoc here,
418 * since isoc are only released on disconnection.
419 * (see new_pkt_size flag)
421 if (dev
->isoc_ctl
.num_bufs
)
422 stk1160_uninit_isoc(dev
);
424 stk1160_dbg("allocating urbs...\n");
426 num_bufs
= STK1160_NUM_BUFS
;
427 max_packets
= STK1160_NUM_PACKETS
;
428 sb_size
= max_packets
* dev
->max_pkt_size
;
430 dev
->isoc_ctl
.buf
= NULL
;
431 dev
->isoc_ctl
.max_pkt_size
= dev
->max_pkt_size
;
432 dev
->isoc_ctl
.urb
= kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
433 if (!dev
->isoc_ctl
.urb
) {
434 stk1160_err("out of memory for urb array\n");
438 dev
->isoc_ctl
.transfer_buffer
= kcalloc(num_bufs
, sizeof(void *),
440 if (!dev
->isoc_ctl
.transfer_buffer
) {
441 stk1160_err("out of memory for usb transfers\n");
442 kfree(dev
->isoc_ctl
.urb
);
446 /* allocate urbs and transfer buffers */
447 for (i
= 0; i
< num_bufs
; i
++) {
449 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
452 dev
->isoc_ctl
.urb
[i
] = urb
;
454 #ifndef CONFIG_DMA_NONCOHERENT
455 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->udev
,
456 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
458 dev
->isoc_ctl
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_KERNEL
);
460 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
461 stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
464 /* Not enough transfer buffers, so just give up */
465 if (i
< STK1160_MIN_BUFS
)
469 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
472 * FIXME: Where can I get the endpoint?
474 urb
->dev
= dev
->udev
;
475 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, STK1160_EP_VIDEO
);
476 urb
->transfer_buffer
= dev
->isoc_ctl
.transfer_buffer
[i
];
477 urb
->transfer_buffer_length
= sb_size
;
478 urb
->complete
= stk1160_isoc_irq
;
481 urb
->start_frame
= 0;
482 urb
->number_of_packets
= max_packets
;
483 #ifndef CONFIG_DMA_NONCOHERENT
484 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
486 urb
->transfer_flags
= URB_ISO_ASAP
;
490 for (j
= 0; j
< max_packets
; j
++) {
491 urb
->iso_frame_desc
[j
].offset
= k
;
492 urb
->iso_frame_desc
[j
].length
=
493 dev
->isoc_ctl
.max_pkt_size
;
494 k
+= dev
->isoc_ctl
.max_pkt_size
;
498 stk1160_dbg("%d urbs allocated\n", num_bufs
);
500 /* At last we can say we have some buffers */
501 dev
->isoc_ctl
.num_bufs
= num_bufs
;
507 * Failed to allocate desired buffer count. However, we may have
508 * enough to work fine, so we just free the extra urb,
509 * store the allocated count and keep going, fingers crossed!
511 usb_free_urb(dev
->isoc_ctl
.urb
[i
]);
512 dev
->isoc_ctl
.urb
[i
] = NULL
;
514 stk1160_warn("%d urbs allocated. Trying to continue...\n", i
- 1);
516 dev
->isoc_ctl
.num_bufs
= i
- 1;
521 /* Save the allocated buffers so far, so we can properly free them */
522 dev
->isoc_ctl
.num_bufs
= i
+1;
523 stk1160_free_isoc(dev
);