4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/slab.h>
26 #include <linux/ratelimit.h>
30 static unsigned int debug
;
31 module_param(debug
, int, 0644);
32 MODULE_PARM_DESC(debug
, "enable debug messages");
34 static inline void print_err_status(struct stk1160
*dev
,
35 int packet
, int status
)
37 char *errmsg
= "Unknown";
41 errmsg
= "unlinked synchronously";
44 errmsg
= "unlinked asynchronously";
47 errmsg
= "Buffer error (overrun)";
50 errmsg
= "Stalled (device not responding)";
53 errmsg
= "Babble (bad cable?)";
56 errmsg
= "Bit-stuff error (bad cable?)";
59 errmsg
= "CRC/Timeout (could be anything)";
62 errmsg
= "Device does not respond";
67 printk_ratelimited(KERN_WARNING
"URB status %d [%s].\n",
70 printk_ratelimited(KERN_INFO
"URB packet %d, status %d [%s].\n",
71 packet
, status
, errmsg
);
75 struct stk1160_buffer
*stk1160_next_buffer(struct stk1160
*dev
)
77 struct stk1160_buffer
*buf
= NULL
;
78 unsigned long flags
= 0;
80 /* Current buffer must be NULL when this functions gets called */
81 WARN_ON(dev
->isoc_ctl
.buf
);
83 spin_lock_irqsave(&dev
->buf_lock
, flags
);
84 if (!list_empty(&dev
->avail_bufs
)) {
85 buf
= list_first_entry(&dev
->avail_bufs
,
86 struct stk1160_buffer
, list
);
89 spin_unlock_irqrestore(&dev
->buf_lock
, flags
);
95 void stk1160_buffer_done(struct stk1160
*dev
)
97 struct stk1160_buffer
*buf
= dev
->isoc_ctl
.buf
;
99 buf
->vb
.sequence
= dev
->sequence
++;
100 buf
->vb
.field
= V4L2_FIELD_INTERLACED
;
101 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
103 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, buf
->bytesused
);
104 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
106 dev
->isoc_ctl
.buf
= NULL
;
110 void stk1160_copy_video(struct stk1160
*dev
, u8
*src
, int len
)
112 int linesdone
, lineoff
, lencopy
;
113 int bytesperline
= dev
->width
* 2;
114 struct stk1160_buffer
*buf
= dev
->isoc_ctl
.buf
;
119 * TODO: These stk1160_dbg are very spammy!
120 * We should 1) check why we are getting them
121 * and 2) add ratelimit.
123 * UPDATE: One of the reasons (the only one?) for getting these
124 * is incorrect standard (mismatch between expected and configured).
125 * So perhaps, we could add a counter for errors. When the counter
126 * reaches some value, we simply stop streaming.
134 linesdone
= buf
->pos
/ bytesperline
;
135 lineoff
= buf
->pos
% bytesperline
; /* offset in current line */
140 /* Multiply linesdone by two, to take account of the other field */
141 dst
+= linesdone
* bytesperline
* 2 + lineoff
;
143 /* Copy the remaining of current line */
144 if (remain
< (bytesperline
- lineoff
))
147 lencopy
= bytesperline
- lineoff
;
150 * Check if we have enough space left in the buffer.
151 * In that case, we force loop exit after copy.
153 if (lencopy
> buf
->bytesused
- buf
->length
) {
154 lencopy
= buf
->bytesused
- buf
->length
;
158 /* Check if the copy is done */
159 if (lencopy
== 0 || remain
== 0)
162 /* Let the bug hunt begin! sanity checks! */
164 stk1160_dbg("copy skipped: negative lencopy\n");
168 if ((unsigned long)dst
+ lencopy
>
169 (unsigned long)buf
->mem
+ buf
->length
) {
170 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
174 memcpy(dst
, src
, lencopy
);
176 buf
->bytesused
+= lencopy
;
180 /* Copy current field line by line, interlacing with the other field */
183 dst
+= lencopy
+ bytesperline
;
186 /* Copy one line at a time */
187 if (remain
< bytesperline
)
190 lencopy
= bytesperline
;
193 * Check if we have enough space left in the buffer.
194 * In that case, we force loop exit after copy.
196 if (lencopy
> buf
->bytesused
- buf
->length
) {
197 lencopy
= buf
->bytesused
- buf
->length
;
201 /* Check if the copy is done */
202 if (lencopy
== 0 || remain
== 0)
206 printk_ratelimited(KERN_WARNING
"stk1160: negative lencopy detected\n");
210 if ((unsigned long)dst
+ lencopy
>
211 (unsigned long)buf
->mem
+ buf
->length
) {
212 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
216 memcpy(dst
, src
, lencopy
);
219 buf
->bytesused
+= lencopy
;
225 * Controls the isoc copy of each urb packet
227 static void stk1160_process_isoc(struct stk1160
*dev
, struct urb
*urb
)
233 stk1160_warn("%s called with null device\n", __func__
);
237 if (urb
->status
< 0) {
238 /* Print status and drop current packet (or field?) */
239 print_err_status(dev
, -1, urb
->status
);
243 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
244 status
= urb
->iso_frame_desc
[i
].status
;
246 print_err_status(dev
, i
, status
);
250 /* Get packet actual length and pointer to data */
251 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
252 len
= urb
->iso_frame_desc
[i
].actual_length
;
259 * An 8-byte packet sequence means end of field.
260 * So if we don't have any packet, we start receiving one now
261 * and if we do have a packet, then we are done with it.
263 * These end of field packets are always 0xc0 or 0x80,
264 * but not always 8-byte long so we don't check packet length.
269 * If first byte is 0xc0 then we received
270 * second field, and frame has ended.
272 if (dev
->isoc_ctl
.buf
!= NULL
)
273 stk1160_buffer_done(dev
);
275 dev
->isoc_ctl
.buf
= stk1160_next_buffer(dev
);
276 if (dev
->isoc_ctl
.buf
== NULL
)
281 * If we don't have a buffer here, then it means we
282 * haven't found the start mark sequence.
284 if (dev
->isoc_ctl
.buf
== NULL
)
287 if (p
[0] == 0xc0 || p
[0] == 0x80) {
289 /* We set next packet parity and
290 * continue to get next one
292 dev
->isoc_ctl
.buf
->odd
= *p
& 0x40;
293 dev
->isoc_ctl
.buf
->pos
= 0;
297 stk1160_copy_video(dev
, p
, len
);
303 * IRQ callback, called by URB callback
305 static void stk1160_isoc_irq(struct urb
*urb
)
308 struct stk1160
*dev
= urb
->context
;
310 switch (urb
->status
) {
313 case -ECONNRESET
: /* kill */
316 /* TODO: check uvc driver: he frees the queue here */
319 stk1160_err("urb error! status %d\n", urb
->status
);
323 stk1160_process_isoc(dev
, urb
);
325 /* Reset urb buffers */
326 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
327 urb
->iso_frame_desc
[i
].status
= 0;
328 urb
->iso_frame_desc
[i
].actual_length
= 0;
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
[i
]);
363 stk1160_dbg("all urbs killed\n");
367 * Releases urb and transfer buffers
368 * Obviusly, associated urb must be killed before releasing it.
370 void stk1160_free_isoc(struct stk1160
*dev
)
373 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
375 stk1160_dbg("freeing %d urb buffers...\n", num_bufs
);
377 for (i
= 0; i
< num_bufs
; i
++) {
379 urb
= dev
->isoc_ctl
.urb
[i
];
382 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
383 #ifndef CONFIG_DMA_NONCOHERENT
384 usb_free_coherent(dev
->udev
,
385 urb
->transfer_buffer_length
,
386 dev
->isoc_ctl
.transfer_buffer
[i
],
389 kfree(dev
->isoc_ctl
.transfer_buffer
[i
]);
393 dev
->isoc_ctl
.urb
[i
] = NULL
;
395 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
398 kfree(dev
->isoc_ctl
.urb
);
399 kfree(dev
->isoc_ctl
.transfer_buffer
);
401 dev
->isoc_ctl
.urb
= NULL
;
402 dev
->isoc_ctl
.transfer_buffer
= NULL
;
403 dev
->isoc_ctl
.num_bufs
= 0;
405 stk1160_dbg("all urb buffers freed\n");
409 * Helper for cancelling and freeing urbs
410 * This function can't be called in atomic context
412 void stk1160_uninit_isoc(struct stk1160
*dev
)
414 stk1160_cancel_isoc(dev
);
415 stk1160_free_isoc(dev
);
421 int stk1160_alloc_isoc(struct stk1160
*dev
)
424 int i
, j
, k
, sb_size
, max_packets
, num_bufs
;
427 * It may be necessary to release isoc here,
428 * since isoc are only released on disconnection.
429 * (see new_pkt_size flag)
431 if (dev
->isoc_ctl
.num_bufs
)
432 stk1160_uninit_isoc(dev
);
434 stk1160_dbg("allocating urbs...\n");
436 num_bufs
= STK1160_NUM_BUFS
;
437 max_packets
= STK1160_NUM_PACKETS
;
438 sb_size
= max_packets
* dev
->max_pkt_size
;
440 dev
->isoc_ctl
.buf
= NULL
;
441 dev
->isoc_ctl
.max_pkt_size
= dev
->max_pkt_size
;
442 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
443 if (!dev
->isoc_ctl
.urb
) {
444 stk1160_err("out of memory for urb array\n");
448 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
450 if (!dev
->isoc_ctl
.transfer_buffer
) {
451 stk1160_err("out of memory for usb transfers\n");
452 kfree(dev
->isoc_ctl
.urb
);
456 /* allocate urbs and transfer buffers */
457 for (i
= 0; i
< num_bufs
; i
++) {
459 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
462 dev
->isoc_ctl
.urb
[i
] = urb
;
464 #ifndef CONFIG_DMA_NONCOHERENT
465 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->udev
,
466 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
468 dev
->isoc_ctl
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_KERNEL
);
470 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
471 stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
474 /* Not enough transfer buffers, so just give up */
475 if (i
< STK1160_MIN_BUFS
)
479 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
482 * FIXME: Where can I get the endpoint?
484 urb
->dev
= dev
->udev
;
485 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, STK1160_EP_VIDEO
);
486 urb
->transfer_buffer
= dev
->isoc_ctl
.transfer_buffer
[i
];
487 urb
->transfer_buffer_length
= sb_size
;
488 urb
->complete
= stk1160_isoc_irq
;
491 urb
->start_frame
= 0;
492 urb
->number_of_packets
= max_packets
;
493 #ifndef CONFIG_DMA_NONCOHERENT
494 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
496 urb
->transfer_flags
= URB_ISO_ASAP
;
500 for (j
= 0; j
< max_packets
; j
++) {
501 urb
->iso_frame_desc
[j
].offset
= k
;
502 urb
->iso_frame_desc
[j
].length
=
503 dev
->isoc_ctl
.max_pkt_size
;
504 k
+= dev
->isoc_ctl
.max_pkt_size
;
508 stk1160_dbg("%d urbs allocated\n", num_bufs
);
510 /* At last we can say we have some buffers */
511 dev
->isoc_ctl
.num_bufs
= num_bufs
;
517 * Failed to allocate desired buffer count. However, we may have
518 * enough to work fine, so we just free the extra urb,
519 * store the allocated count and keep going, fingers crossed!
521 usb_free_urb(dev
->isoc_ctl
.urb
[i
]);
522 dev
->isoc_ctl
.urb
[i
] = NULL
;
524 stk1160_warn("%d urbs allocated. Trying to continue...\n", i
- 1);
526 dev
->isoc_ctl
.num_bufs
= i
- 1;
531 /* Save the allocated buffers so far, so we can properly free them */
532 dev
->isoc_ctl
.num_bufs
= i
+1;
533 stk1160_free_isoc(dev
);