1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/usb.h>
25#include <linux/slab.h>
26#include <linux/ratelimit.h>
27
28#include "stk1160.h"
29
30static unsigned int debug;
31module_param(debug, int, 0644);
32MODULE_PARM_DESC(debug, "enable debug messages");
33
34static inline void print_err_status(struct stk1160 *dev,
35 int packet, int status)
36{
37 char *errmsg = "Unknown";
38
39 switch (status) {
40 case -ENOENT:
41 errmsg = "unlinked synchronuously";
42 break;
43 case -ECONNRESET:
44 errmsg = "unlinked asynchronuously";
45 break;
46 case -ENOSR:
47 errmsg = "Buffer error (overrun)";
48 break;
49 case -EPIPE:
50 errmsg = "Stalled (device not responding)";
51 break;
52 case -EOVERFLOW:
53 errmsg = "Babble (bad cable?)";
54 break;
55 case -EPROTO:
56 errmsg = "Bit-stuff error (bad cable?)";
57 break;
58 case -EILSEQ:
59 errmsg = "CRC/Timeout (could be anything)";
60 break;
61 case -ETIME:
62 errmsg = "Device does not respond";
63 break;
64 }
65
66 if (packet < 0)
67 printk_ratelimited(KERN_WARNING "URB status %d [%s].\n",
68 status, errmsg);
69 else
70 printk_ratelimited(KERN_INFO "URB packet %d, status %d [%s].\n",
71 packet, status, errmsg);
72}
73
74static inline
75struct stk1160_buffer *stk1160_next_buffer(struct stk1160 *dev)
76{
77 struct stk1160_buffer *buf = NULL;
78 unsigned long flags = 0;
79
80
81 WARN_ON(dev->isoc_ctl.buf);
82
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);
87 list_del(&buf->list);
88 }
89 spin_unlock_irqrestore(&dev->buf_lock, flags);
90
91 return buf;
92}
93
94static inline
95void stk1160_buffer_done(struct stk1160 *dev)
96{
97 struct stk1160_buffer *buf = dev->isoc_ctl.buf;
98
99 dev->field_count++;
100
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);
105
106 vb2_set_plane_payload(&buf->vb, 0, buf->bytesused);
107 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
108
109 dev->isoc_ctl.buf = NULL;
110}
111
112static inline
113void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len)
114{
115 int linesdone, lineoff, lencopy;
116 int bytesperline = dev->width * 2;
117 struct stk1160_buffer *buf = dev->isoc_ctl.buf;
118 u8 *dst = buf->mem;
119 int remain;
120
121
122
123
124
125
126
127
128
129
130
131
132 len -= 4;
133 src += 4;
134
135 remain = len;
136
137 linesdone = buf->pos / bytesperline;
138 lineoff = buf->pos % bytesperline;
139
140 if (!buf->odd)
141 dst += bytesperline;
142
143
144 dst += linesdone * bytesperline * 2 + lineoff;
145
146
147 if (remain < (bytesperline - lineoff))
148 lencopy = remain;
149 else
150 lencopy = bytesperline - lineoff;
151
152
153
154
155
156 if (lencopy > buf->bytesused - buf->length) {
157 lencopy = buf->bytesused - buf->length;
158 remain = lencopy;
159 }
160
161
162 if (lencopy == 0 || remain == 0)
163 return;
164
165
166 if (lencopy < 0) {
167 stk1160_dbg("copy skipped: negative lencopy\n");
168 return;
169 }
170
171 if ((unsigned long)dst + lencopy >
172 (unsigned long)buf->mem + buf->length) {
173 printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
174 return;
175 }
176
177 memcpy(dst, src, lencopy);
178
179 buf->bytesused += lencopy;
180 buf->pos += lencopy;
181 remain -= lencopy;
182
183
184 while (remain > 0) {
185
186 dst += lencopy + bytesperline;
187 src += lencopy;
188
189
190 if (remain < bytesperline)
191 lencopy = remain;
192 else
193 lencopy = bytesperline;
194
195
196
197
198
199 if (lencopy > buf->bytesused - buf->length) {
200 lencopy = buf->bytesused - buf->length;
201 remain = lencopy;
202 }
203
204
205 if (lencopy == 0 || remain == 0)
206 return;
207
208 if (lencopy < 0) {
209 printk_ratelimited(KERN_WARNING "stk1160: negative lencopy detected\n");
210 return;
211 }
212
213 if ((unsigned long)dst + lencopy >
214 (unsigned long)buf->mem + buf->length) {
215 printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
216 return;
217 }
218
219 memcpy(dst, src, lencopy);
220 remain -= lencopy;
221
222 buf->bytesused += lencopy;
223 buf->pos += lencopy;
224 }
225}
226
227
228
229
230static void stk1160_process_isoc(struct stk1160 *dev, struct urb *urb)
231{
232 int i, len, status;
233 u8 *p;
234
235 if (!dev) {
236 stk1160_warn("%s called with null device\n", __func__);
237 return;
238 }
239
240 if (urb->status < 0) {
241
242 print_err_status(dev, -1, urb->status);
243 return;
244 }
245
246 for (i = 0; i < urb->number_of_packets; i++) {
247 status = urb->iso_frame_desc[i].status;
248 if (status < 0) {
249 print_err_status(dev, i, status);
250 continue;
251 }
252
253
254 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
255 len = urb->iso_frame_desc[i].actual_length;
256
257
258 if (len <= 4)
259 continue;
260
261
262
263
264
265
266
267
268
269 if (p[0] == 0xc0) {
270
271
272
273
274
275 if (dev->isoc_ctl.buf != NULL)
276 stk1160_buffer_done(dev);
277
278 dev->isoc_ctl.buf = stk1160_next_buffer(dev);
279 if (dev->isoc_ctl.buf == NULL)
280 return;
281 }
282
283
284
285
286
287 if (dev->isoc_ctl.buf == NULL)
288 continue;
289
290 if (p[0] == 0xc0 || p[0] == 0x80) {
291
292
293
294
295 dev->isoc_ctl.buf->odd = *p & 0x40;
296 dev->isoc_ctl.buf->pos = 0;
297 continue;
298 }
299
300 stk1160_copy_video(dev, p, len);
301 }
302}
303
304
305
306
307
308static void stk1160_isoc_irq(struct urb *urb)
309{
310 int i, rc;
311 struct stk1160 *dev = urb->context;
312
313 switch (urb->status) {
314 case 0:
315 break;
316 case -ECONNRESET:
317 case -ENOENT:
318 case -ESHUTDOWN:
319
320 return;
321 default:
322 stk1160_err("urb error! status %d\n", urb->status);
323 return;
324 }
325
326 stk1160_process_isoc(dev, urb);
327
328
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;
332 }
333
334 rc = usb_submit_urb(urb, GFP_ATOMIC);
335 if (rc)
336 stk1160_err("urb re-submit failed (%d)\n", rc);
337}
338
339
340
341
342
343void stk1160_cancel_isoc(struct stk1160 *dev)
344{
345 int i, num_bufs = dev->isoc_ctl.num_bufs;
346
347
348
349
350
351 if (!num_bufs)
352 return;
353
354 stk1160_dbg("killing %d urbs...\n", num_bufs);
355
356 for (i = 0; i < num_bufs; i++) {
357
358
359
360
361
362
363 usb_kill_urb(dev->isoc_ctl.urb[i]);
364 }
365
366 stk1160_dbg("all urbs killed\n");
367}
368
369
370
371
372
373void stk1160_free_isoc(struct stk1160 *dev)
374{
375 struct urb *urb;
376 int i, num_bufs = dev->isoc_ctl.num_bufs;
377
378 stk1160_dbg("freeing %d urb buffers...\n", num_bufs);
379
380 for (i = 0; i < num_bufs; i++) {
381
382 urb = dev->isoc_ctl.urb[i];
383 if (urb) {
384
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],
390 urb->transfer_dma);
391#else
392 kfree(dev->isoc_ctl.transfer_buffer[i]);
393#endif
394 }
395 usb_free_urb(urb);
396 dev->isoc_ctl.urb[i] = NULL;
397 }
398 dev->isoc_ctl.transfer_buffer[i] = NULL;
399 }
400
401 kfree(dev->isoc_ctl.urb);
402 kfree(dev->isoc_ctl.transfer_buffer);
403
404 dev->isoc_ctl.urb = NULL;
405 dev->isoc_ctl.transfer_buffer = NULL;
406 dev->isoc_ctl.num_bufs = 0;
407
408 stk1160_dbg("all urb buffers freed\n");
409}
410
411
412
413
414
415void stk1160_uninit_isoc(struct stk1160 *dev)
416{
417 stk1160_cancel_isoc(dev);
418 stk1160_free_isoc(dev);
419}
420
421
422
423
424int stk1160_alloc_isoc(struct stk1160 *dev)
425{
426 struct urb *urb;
427 int i, j, k, sb_size, max_packets, num_bufs;
428
429
430
431
432
433
434 if (dev->isoc_ctl.num_bufs)
435 stk1160_uninit_isoc(dev);
436
437 stk1160_dbg("allocating urbs...\n");
438
439 num_bufs = STK1160_NUM_BUFS;
440 max_packets = STK1160_NUM_PACKETS;
441 sb_size = max_packets * dev->max_pkt_size;
442
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");
448 return -ENOMEM;
449 }
450
451 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
452 GFP_KERNEL);
453 if (!dev->isoc_ctl.transfer_buffer) {
454 stk1160_err("out of memory for usb transfers\n");
455 kfree(dev->isoc_ctl.urb);
456 return -ENOMEM;
457 }
458
459
460 for (i = 0; i < num_bufs; i++) {
461
462 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
463 if (!urb) {
464 stk1160_err("cannot alloc urb[%d]\n", i);
465 goto free_i_bufs;
466 }
467 dev->isoc_ctl.urb[i] = urb;
468
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);
472#else
473 dev->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
474#endif
475 if (!dev->isoc_ctl.transfer_buffer[i]) {
476 stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
477 sb_size, i);
478
479
480 if (i < STK1160_MIN_BUFS)
481 goto free_i_bufs;
482 goto nomore_tx_bufs;
483 }
484 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
485
486
487
488
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;
494 urb->context = dev;
495 urb->interval = 1;
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;
500#else
501 urb->transfer_flags = URB_ISO_ASAP;
502#endif
503
504 k = 0;
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;
510 }
511 }
512
513 stk1160_dbg("%d urbs allocated\n", num_bufs);
514
515
516 dev->isoc_ctl.num_bufs = num_bufs;
517
518 return 0;
519
520nomore_tx_bufs:
521
522
523
524
525
526 usb_free_urb(dev->isoc_ctl.urb[i]);
527 dev->isoc_ctl.urb[i] = NULL;
528
529 stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
530
531 dev->isoc_ctl.num_bufs = i - 1;
532
533 return 0;
534
535free_i_bufs:
536
537 dev->isoc_ctl.num_bufs = i+1;
538 stk1160_free_isoc(dev);
539 return -ENOMEM;
540}
541
542