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 buf->vb.sequence = dev->sequence++;
100 buf->vb.field = V4L2_FIELD_INTERLACED;
101 buf->vb.vb2_buf.planes[0].bytesused = buf->bytesused;
102 v4l2_get_timestamp(&buf->vb.timestamp);
103
104 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->bytesused);
105 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
106
107 dev->isoc_ctl.buf = NULL;
108}
109
110static inline
111void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len)
112{
113 int linesdone, lineoff, lencopy;
114 int bytesperline = dev->width * 2;
115 struct stk1160_buffer *buf = dev->isoc_ctl.buf;
116 u8 *dst = buf->mem;
117 int remain;
118
119
120
121
122
123
124
125
126
127
128
129
130 len -= 4;
131 src += 4;
132
133 remain = len;
134
135 linesdone = buf->pos / bytesperline;
136 lineoff = buf->pos % bytesperline;
137
138 if (!buf->odd)
139 dst += bytesperline;
140
141
142 dst += linesdone * bytesperline * 2 + lineoff;
143
144
145 if (remain < (bytesperline - lineoff))
146 lencopy = remain;
147 else
148 lencopy = bytesperline - lineoff;
149
150
151
152
153
154 if (lencopy > buf->bytesused - buf->length) {
155 lencopy = buf->bytesused - buf->length;
156 remain = lencopy;
157 }
158
159
160 if (lencopy == 0 || remain == 0)
161 return;
162
163
164 if (lencopy < 0) {
165 stk1160_dbg("copy skipped: negative lencopy\n");
166 return;
167 }
168
169 if ((unsigned long)dst + lencopy >
170 (unsigned long)buf->mem + buf->length) {
171 printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
172 return;
173 }
174
175 memcpy(dst, src, lencopy);
176
177 buf->bytesused += lencopy;
178 buf->pos += lencopy;
179 remain -= lencopy;
180
181
182 while (remain > 0) {
183
184 dst += lencopy + bytesperline;
185 src += lencopy;
186
187
188 if (remain < bytesperline)
189 lencopy = remain;
190 else
191 lencopy = bytesperline;
192
193
194
195
196
197 if (lencopy > buf->bytesused - buf->length) {
198 lencopy = buf->bytesused - buf->length;
199 remain = lencopy;
200 }
201
202
203 if (lencopy == 0 || remain == 0)
204 return;
205
206 if (lencopy < 0) {
207 printk_ratelimited(KERN_WARNING "stk1160: negative lencopy detected\n");
208 return;
209 }
210
211 if ((unsigned long)dst + lencopy >
212 (unsigned long)buf->mem + buf->length) {
213 printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
214 return;
215 }
216
217 memcpy(dst, src, lencopy);
218 remain -= lencopy;
219
220 buf->bytesused += lencopy;
221 buf->pos += lencopy;
222 }
223}
224
225
226
227
228static void stk1160_process_isoc(struct stk1160 *dev, struct urb *urb)
229{
230 int i, len, status;
231 u8 *p;
232
233 if (!dev) {
234 stk1160_warn("%s called with null device\n", __func__);
235 return;
236 }
237
238 if (urb->status < 0) {
239
240 print_err_status(dev, -1, urb->status);
241 return;
242 }
243
244 for (i = 0; i < urb->number_of_packets; i++) {
245 status = urb->iso_frame_desc[i].status;
246 if (status < 0) {
247 print_err_status(dev, i, status);
248 continue;
249 }
250
251
252 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
253 len = urb->iso_frame_desc[i].actual_length;
254
255
256 if (len <= 4)
257 continue;
258
259
260
261
262
263
264
265
266
267 if (p[0] == 0xc0) {
268
269
270
271
272
273 if (dev->isoc_ctl.buf != NULL)
274 stk1160_buffer_done(dev);
275
276 dev->isoc_ctl.buf = stk1160_next_buffer(dev);
277 if (dev->isoc_ctl.buf == NULL)
278 return;
279 }
280
281
282
283
284
285 if (dev->isoc_ctl.buf == NULL)
286 continue;
287
288 if (p[0] == 0xc0 || p[0] == 0x80) {
289
290
291
292
293 dev->isoc_ctl.buf->odd = *p & 0x40;
294 dev->isoc_ctl.buf->pos = 0;
295 continue;
296 }
297
298 stk1160_copy_video(dev, p, len);
299 }
300}
301
302
303
304
305
306static void stk1160_isoc_irq(struct urb *urb)
307{
308 int i, rc;
309 struct stk1160 *dev = urb->context;
310
311 switch (urb->status) {
312 case 0:
313 break;
314 case -ECONNRESET:
315 case -ENOENT:
316 case -ESHUTDOWN:
317
318 return;
319 default:
320 stk1160_err("urb error! status %d\n", urb->status);
321 return;
322 }
323
324 stk1160_process_isoc(dev, urb);
325
326
327 for (i = 0; i < urb->number_of_packets; i++) {
328 urb->iso_frame_desc[i].status = 0;
329 urb->iso_frame_desc[i].actual_length = 0;
330 }
331
332 rc = usb_submit_urb(urb, GFP_ATOMIC);
333 if (rc)
334 stk1160_err("urb re-submit failed (%d)\n", rc);
335}
336
337
338
339
340
341void stk1160_cancel_isoc(struct stk1160 *dev)
342{
343 int i, num_bufs = dev->isoc_ctl.num_bufs;
344
345
346
347
348
349 if (!num_bufs)
350 return;
351
352 stk1160_dbg("killing %d urbs...\n", num_bufs);
353
354 for (i = 0; i < num_bufs; i++) {
355
356
357
358
359
360
361 usb_kill_urb(dev->isoc_ctl.urb[i]);
362 }
363
364 stk1160_dbg("all urbs killed\n");
365}
366
367
368
369
370
371void stk1160_free_isoc(struct stk1160 *dev)
372{
373 struct urb *urb;
374 int i, num_bufs = dev->isoc_ctl.num_bufs;
375
376 stk1160_dbg("freeing %d urb buffers...\n", num_bufs);
377
378 for (i = 0; i < num_bufs; i++) {
379
380 urb = dev->isoc_ctl.urb[i];
381 if (urb) {
382
383 if (dev->isoc_ctl.transfer_buffer[i]) {
384#ifndef CONFIG_DMA_NONCOHERENT
385 usb_free_coherent(dev->udev,
386 urb->transfer_buffer_length,
387 dev->isoc_ctl.transfer_buffer[i],
388 urb->transfer_dma);
389#else
390 kfree(dev->isoc_ctl.transfer_buffer[i]);
391#endif
392 }
393 usb_free_urb(urb);
394 dev->isoc_ctl.urb[i] = NULL;
395 }
396 dev->isoc_ctl.transfer_buffer[i] = NULL;
397 }
398
399 kfree(dev->isoc_ctl.urb);
400 kfree(dev->isoc_ctl.transfer_buffer);
401
402 dev->isoc_ctl.urb = NULL;
403 dev->isoc_ctl.transfer_buffer = NULL;
404 dev->isoc_ctl.num_bufs = 0;
405
406 stk1160_dbg("all urb buffers freed\n");
407}
408
409
410
411
412
413void stk1160_uninit_isoc(struct stk1160 *dev)
414{
415 stk1160_cancel_isoc(dev);
416 stk1160_free_isoc(dev);
417}
418
419
420
421
422int stk1160_alloc_isoc(struct stk1160 *dev)
423{
424 struct urb *urb;
425 int i, j, k, sb_size, max_packets, num_bufs;
426
427
428
429
430
431
432 if (dev->isoc_ctl.num_bufs)
433 stk1160_uninit_isoc(dev);
434
435 stk1160_dbg("allocating urbs...\n");
436
437 num_bufs = STK1160_NUM_BUFS;
438 max_packets = STK1160_NUM_PACKETS;
439 sb_size = max_packets * dev->max_pkt_size;
440
441 dev->isoc_ctl.buf = NULL;
442 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
443 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
444 if (!dev->isoc_ctl.urb) {
445 stk1160_err("out of memory for urb array\n");
446 return -ENOMEM;
447 }
448
449 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
450 GFP_KERNEL);
451 if (!dev->isoc_ctl.transfer_buffer) {
452 stk1160_err("out of memory for usb transfers\n");
453 kfree(dev->isoc_ctl.urb);
454 return -ENOMEM;
455 }
456
457
458 for (i = 0; i < num_bufs; i++) {
459
460 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
461 if (!urb) {
462 stk1160_err("cannot alloc urb[%d]\n", i);
463 goto free_i_bufs;
464 }
465 dev->isoc_ctl.urb[i] = urb;
466
467#ifndef CONFIG_DMA_NONCOHERENT
468 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
469 sb_size, GFP_KERNEL, &urb->transfer_dma);
470#else
471 dev->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
472#endif
473 if (!dev->isoc_ctl.transfer_buffer[i]) {
474 stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
475 sb_size, i);
476
477
478 if (i < STK1160_MIN_BUFS)
479 goto free_i_bufs;
480 goto nomore_tx_bufs;
481 }
482 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
483
484
485
486
487 urb->dev = dev->udev;
488 urb->pipe = usb_rcvisocpipe(dev->udev, STK1160_EP_VIDEO);
489 urb->transfer_buffer = dev->isoc_ctl.transfer_buffer[i];
490 urb->transfer_buffer_length = sb_size;
491 urb->complete = stk1160_isoc_irq;
492 urb->context = dev;
493 urb->interval = 1;
494 urb->start_frame = 0;
495 urb->number_of_packets = max_packets;
496#ifndef CONFIG_DMA_NONCOHERENT
497 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
498#else
499 urb->transfer_flags = URB_ISO_ASAP;
500#endif
501
502 k = 0;
503 for (j = 0; j < max_packets; j++) {
504 urb->iso_frame_desc[j].offset = k;
505 urb->iso_frame_desc[j].length =
506 dev->isoc_ctl.max_pkt_size;
507 k += dev->isoc_ctl.max_pkt_size;
508 }
509 }
510
511 stk1160_dbg("%d urbs allocated\n", num_bufs);
512
513
514 dev->isoc_ctl.num_bufs = num_bufs;
515
516 return 0;
517
518nomore_tx_bufs:
519
520
521
522
523
524 usb_free_urb(dev->isoc_ctl.urb[i]);
525 dev->isoc_ctl.urb[i] = NULL;
526
527 stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
528
529 dev->isoc_ctl.num_bufs = i - 1;
530
531 return 0;
532
533free_i_bufs:
534
535 dev->isoc_ctl.num_bufs = i+1;
536 stk1160_free_isoc(dev);
537 return -ENOMEM;
538}
539
540