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 synchronuously";
44 errmsg
= "unlinked asynchronuously";
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
;
101 buf
->vb
.v4l2_buf
.sequence
= dev
->field_count
>> 1;
102 buf
->vb
.v4l2_buf
.field
= V4L2_FIELD_INTERLACED
;
103 buf
->vb
.v4l2_buf
.bytesused
= buf
->bytesused
;
104 v4l2_get_timestamp(&buf
->vb
.v4l2_buf
.timestamp
);
106 vb2_set_plane_payload(&buf
->vb
, 0, buf
->bytesused
);
107 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_DONE
);
109 dev
->isoc_ctl
.buf
= NULL
;
113 void stk1160_copy_video(struct stk1160
*dev
, u8
*src
, int len
)
115 int linesdone
, lineoff
, lencopy
;
116 int bytesperline
= dev
->width
* 2;
117 struct stk1160_buffer
*buf
= dev
->isoc_ctl
.buf
;
122 * TODO: These stk1160_dbg are very spammy!
123 * We should 1) check why we are getting them
124 * and 2) add ratelimit.
126 * UPDATE: One of the reasons (the only one?) for getting these
127 * is incorrect standard (mismatch between expected and configured).
128 * So perhaps, we could add a counter for errors. When the counter
129 * reaches some value, we simply stop streaming.
137 linesdone
= buf
->pos
/ bytesperline
;
138 lineoff
= buf
->pos
% bytesperline
; /* offset in current line */
143 /* Multiply linesdone by two, to take account of the other field */
144 dst
+= linesdone
* bytesperline
* 2 + lineoff
;
146 /* Copy the remaining of current line */
147 if (remain
< (bytesperline
- lineoff
))
150 lencopy
= bytesperline
- lineoff
;
153 * Check if we have enough space left in the buffer.
154 * In that case, we force loop exit after copy.
156 if (lencopy
> buf
->bytesused
- buf
->length
) {
157 lencopy
= buf
->bytesused
- buf
->length
;
161 /* Check if the copy is done */
162 if (lencopy
== 0 || remain
== 0)
165 /* Let the bug hunt begin! sanity checks! */
167 stk1160_dbg("copy skipped: negative lencopy\n");
171 if ((unsigned long)dst
+ lencopy
>
172 (unsigned long)buf
->mem
+ buf
->length
) {
173 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
177 memcpy(dst
, src
, lencopy
);
179 buf
->bytesused
+= lencopy
;
183 /* Copy current field line by line, interlacing with the other field */
186 dst
+= lencopy
+ bytesperline
;
189 /* Copy one line at a time */
190 if (remain
< bytesperline
)
193 lencopy
= bytesperline
;
196 * Check if we have enough space left in the buffer.
197 * In that case, we force loop exit after copy.
199 if (lencopy
> buf
->bytesused
- buf
->length
) {
200 lencopy
= buf
->bytesused
- buf
->length
;
204 /* Check if the copy is done */
205 if (lencopy
== 0 || remain
== 0)
209 printk_ratelimited(KERN_WARNING
"stk1160: negative lencopy detected\n");
213 if ((unsigned long)dst
+ lencopy
>
214 (unsigned long)buf
->mem
+ buf
->length
) {
215 printk_ratelimited(KERN_WARNING
"stk1160: buffer overflow detected\n");
219 memcpy(dst
, src
, lencopy
);
222 buf
->bytesused
+= lencopy
;
228 * Controls the isoc copy of each urb packet
230 static void stk1160_process_isoc(struct stk1160
*dev
, struct urb
*urb
)
236 stk1160_warn("%s called with null device\n", __func__
);
240 if (urb
->status
< 0) {
241 /* Print status and drop current packet (or field?) */
242 print_err_status(dev
, -1, urb
->status
);
246 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
247 status
= urb
->iso_frame_desc
[i
].status
;
249 print_err_status(dev
, i
, status
);
253 /* Get packet actual length and pointer to data */
254 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
255 len
= urb
->iso_frame_desc
[i
].actual_length
;
262 * An 8-byte packet sequence means end of field.
263 * So if we don't have any packet, we start receiving one now
264 * and if we do have a packet, then we are done with it.
266 * These end of field packets are always 0xc0 or 0x80,
267 * but not always 8-byte long so we don't check packet length.
272 * If first byte is 0xc0 then we received
273 * second field, and frame has ended.
275 if (dev
->isoc_ctl
.buf
!= NULL
)
276 stk1160_buffer_done(dev
);
278 dev
->isoc_ctl
.buf
= stk1160_next_buffer(dev
);
279 if (dev
->isoc_ctl
.buf
== NULL
)
284 * If we don't have a buffer here, then it means we
285 * haven't found the start mark sequence.
287 if (dev
->isoc_ctl
.buf
== NULL
)
290 if (p
[0] == 0xc0 || p
[0] == 0x80) {
292 /* We set next packet parity and
293 * continue to get next one
295 dev
->isoc_ctl
.buf
->odd
= *p
& 0x40;
296 dev
->isoc_ctl
.buf
->pos
= 0;
300 stk1160_copy_video(dev
, p
, len
);
306 * IRQ callback, called by URB callback
308 static void stk1160_isoc_irq(struct urb
*urb
)
311 struct stk1160
*dev
= urb
->context
;
313 switch (urb
->status
) {
316 case -ECONNRESET
: /* kill */
319 /* TODO: check uvc driver: he frees the queue here */
322 stk1160_err("urb error! status %d\n", urb
->status
);
326 stk1160_process_isoc(dev
, urb
);
328 /* Reset urb buffers */
329 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
330 urb
->iso_frame_desc
[i
].status
= 0;
331 urb
->iso_frame_desc
[i
].actual_length
= 0;
334 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
336 stk1160_err("urb re-submit failed (%d)\n", rc
);
341 * This function can't be called in atomic context
343 void stk1160_cancel_isoc(struct stk1160
*dev
)
345 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
348 * This check is not necessary, but we add it
349 * to avoid a spurious debug message
354 stk1160_dbg("killing %d urbs...\n", num_bufs
);
356 for (i
= 0; i
< num_bufs
; i
++) {
359 * To kill urbs we can't be in atomic context.
360 * We don't care for NULL pointer since
361 * usb_kill_urb allows it.
363 usb_kill_urb(dev
->isoc_ctl
.urb
[i
]);
366 stk1160_dbg("all urbs killed\n");
370 * Releases urb and transfer buffers
371 * Obviusly, associated urb must be killed before releasing it.
373 void stk1160_free_isoc(struct stk1160
*dev
)
376 int i
, num_bufs
= dev
->isoc_ctl
.num_bufs
;
378 stk1160_dbg("freeing %d urb buffers...\n", num_bufs
);
380 for (i
= 0; i
< num_bufs
; i
++) {
382 urb
= dev
->isoc_ctl
.urb
[i
];
385 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
386 #ifndef CONFIG_DMA_NONCOHERENT
387 usb_free_coherent(dev
->udev
,
388 urb
->transfer_buffer_length
,
389 dev
->isoc_ctl
.transfer_buffer
[i
],
392 kfree(dev
->isoc_ctl
.transfer_buffer
[i
]);
396 dev
->isoc_ctl
.urb
[i
] = NULL
;
398 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
401 kfree(dev
->isoc_ctl
.urb
);
402 kfree(dev
->isoc_ctl
.transfer_buffer
);
404 dev
->isoc_ctl
.urb
= NULL
;
405 dev
->isoc_ctl
.transfer_buffer
= NULL
;
406 dev
->isoc_ctl
.num_bufs
= 0;
408 stk1160_dbg("all urb buffers freed\n");
412 * Helper for cancelling and freeing urbs
413 * This function can't be called in atomic context
415 void stk1160_uninit_isoc(struct stk1160
*dev
)
417 stk1160_cancel_isoc(dev
);
418 stk1160_free_isoc(dev
);
424 int stk1160_alloc_isoc(struct stk1160
*dev
)
427 int i
, j
, k
, sb_size
, max_packets
, num_bufs
;
430 * It may be necessary to release isoc here,
431 * since isoc are only released on disconnection.
432 * (see new_pkt_size flag)
434 if (dev
->isoc_ctl
.num_bufs
)
435 stk1160_uninit_isoc(dev
);
437 stk1160_dbg("allocating urbs...\n");
439 num_bufs
= STK1160_NUM_BUFS
;
440 max_packets
= STK1160_NUM_PACKETS
;
441 sb_size
= max_packets
* dev
->max_pkt_size
;
443 dev
->isoc_ctl
.buf
= NULL
;
444 dev
->isoc_ctl
.max_pkt_size
= dev
->max_pkt_size
;
445 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
446 if (!dev
->isoc_ctl
.urb
) {
447 stk1160_err("out of memory for urb array\n");
451 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
453 if (!dev
->isoc_ctl
.transfer_buffer
) {
454 stk1160_err("out of memory for usb transfers\n");
455 kfree(dev
->isoc_ctl
.urb
);
459 /* allocate urbs and transfer buffers */
460 for (i
= 0; i
< num_bufs
; i
++) {
462 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
464 stk1160_err("cannot alloc urb[%d]\n", i
);
467 dev
->isoc_ctl
.urb
[i
] = urb
;
469 #ifndef CONFIG_DMA_NONCOHERENT
470 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->udev
,
471 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
473 dev
->isoc_ctl
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_KERNEL
);
475 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
476 stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
479 /* Not enough transfer buffers, so just give up */
480 if (i
< STK1160_MIN_BUFS
)
484 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
487 * FIXME: Where can I get the endpoint?
489 urb
->dev
= dev
->udev
;
490 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, STK1160_EP_VIDEO
);
491 urb
->transfer_buffer
= dev
->isoc_ctl
.transfer_buffer
[i
];
492 urb
->transfer_buffer_length
= sb_size
;
493 urb
->complete
= stk1160_isoc_irq
;
496 urb
->start_frame
= 0;
497 urb
->number_of_packets
= max_packets
;
498 #ifndef CONFIG_DMA_NONCOHERENT
499 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
501 urb
->transfer_flags
= URB_ISO_ASAP
;
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!
526 usb_free_urb(dev
->isoc_ctl
.urb
[i
]);
527 dev
->isoc_ctl
.urb
[i
] = NULL
;
529 stk1160_warn("%d urbs allocated. Trying to continue...\n", i
- 1);
531 dev
->isoc_ctl
.num_bufs
= i
- 1;
536 /* Save the allocated buffers so far, so we can properly free them */
537 dev
->isoc_ctl
.num_bufs
= i
+1;
538 stk1160_free_isoc(dev
);