stk1160-video.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * STK1160 driver
  3. *
  4. * Copyright (C) 2012 Ezequiel Garcia
  5. * <elezegarcia--a.t--gmail.com>
  6. *
  7. * Based on Easycap driver by R.M. Thomas
  8. * Copyright (C) 2010 R.M. Thomas
  9. * <rmthomas--a.t--sciolus.org>
  10. *
  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.
  15. *
  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.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/usb.h>
  24. #include <linux/slab.h>
  25. #include <linux/ratelimit.h>
  26. #include "stk1160.h"
  27. static unsigned int debug;
  28. module_param(debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "enable debug messages");
  30. static inline void print_err_status(struct stk1160 *dev,
  31. int packet, int status)
  32. {
  33. char *errmsg = "Unknown";
  34. switch (status) {
  35. case -ENOENT:
  36. errmsg = "unlinked synchronously";
  37. break;
  38. case -ECONNRESET:
  39. errmsg = "unlinked asynchronously";
  40. break;
  41. case -ENOSR:
  42. errmsg = "Buffer error (overrun)";
  43. break;
  44. case -EPIPE:
  45. errmsg = "Stalled (device not responding)";
  46. break;
  47. case -EOVERFLOW:
  48. errmsg = "Babble (bad cable?)";
  49. break;
  50. case -EPROTO:
  51. errmsg = "Bit-stuff error (bad cable?)";
  52. break;
  53. case -EILSEQ:
  54. errmsg = "CRC/Timeout (could be anything)";
  55. break;
  56. case -ETIME:
  57. errmsg = "Device does not respond";
  58. break;
  59. }
  60. if (packet < 0)
  61. printk_ratelimited(KERN_WARNING "URB status %d [%s].\n",
  62. status, errmsg);
  63. else
  64. printk_ratelimited(KERN_INFO "URB packet %d, status %d [%s].\n",
  65. packet, status, errmsg);
  66. }
  67. static inline
  68. struct stk1160_buffer *stk1160_next_buffer(struct stk1160 *dev)
  69. {
  70. struct stk1160_buffer *buf = NULL;
  71. unsigned long flags = 0;
  72. /* Current buffer must be NULL when this functions gets called */
  73. WARN_ON(dev->isoc_ctl.buf);
  74. spin_lock_irqsave(&dev->buf_lock, flags);
  75. if (!list_empty(&dev->avail_bufs)) {
  76. buf = list_first_entry(&dev->avail_bufs,
  77. struct stk1160_buffer, list);
  78. list_del(&buf->list);
  79. }
  80. spin_unlock_irqrestore(&dev->buf_lock, flags);
  81. return buf;
  82. }
  83. static inline
  84. void stk1160_buffer_done(struct stk1160 *dev)
  85. {
  86. struct stk1160_buffer *buf = dev->isoc_ctl.buf;
  87. buf->vb.sequence = dev->sequence++;
  88. buf->vb.field = V4L2_FIELD_INTERLACED;
  89. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  90. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->bytesused);
  91. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  92. dev->isoc_ctl.buf = NULL;
  93. }
  94. static inline
  95. void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len)
  96. {
  97. int linesdone, lineoff, lencopy;
  98. int bytesperline = dev->width * 2;
  99. struct stk1160_buffer *buf = dev->isoc_ctl.buf;
  100. u8 *dst = buf->mem;
  101. int remain;
  102. /*
  103. * TODO: These stk1160_dbg are very spammy!
  104. * We should 1) check why we are getting them
  105. * and 2) add ratelimit.
  106. *
  107. * UPDATE: One of the reasons (the only one?) for getting these
  108. * is incorrect standard (mismatch between expected and configured).
  109. * So perhaps, we could add a counter for errors. When the counter
  110. * reaches some value, we simply stop streaming.
  111. */
  112. len -= 4;
  113. src += 4;
  114. remain = len;
  115. linesdone = buf->pos / bytesperline;
  116. lineoff = buf->pos % bytesperline; /* offset in current line */
  117. if (!buf->odd)
  118. dst += bytesperline;
  119. /* Multiply linesdone by two, to take account of the other field */
  120. dst += linesdone * bytesperline * 2 + lineoff;
  121. /* Copy the remaining of current line */
  122. if (remain < (bytesperline - lineoff))
  123. lencopy = remain;
  124. else
  125. lencopy = bytesperline - lineoff;
  126. /*
  127. * Check if we have enough space left in the buffer.
  128. * In that case, we force loop exit after copy.
  129. */
  130. if (lencopy > buf->bytesused - buf->length) {
  131. lencopy = buf->bytesused - buf->length;
  132. remain = lencopy;
  133. }
  134. /* Check if the copy is done */
  135. if (lencopy == 0 || remain == 0)
  136. return;
  137. /* Let the bug hunt begin! sanity checks! */
  138. if (lencopy < 0) {
  139. stk1160_dbg("copy skipped: negative lencopy\n");
  140. return;
  141. }
  142. if ((unsigned long)dst + lencopy >
  143. (unsigned long)buf->mem + buf->length) {
  144. printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
  145. return;
  146. }
  147. memcpy(dst, src, lencopy);
  148. buf->bytesused += lencopy;
  149. buf->pos += lencopy;
  150. remain -= lencopy;
  151. /* Copy current field line by line, interlacing with the other field */
  152. while (remain > 0) {
  153. dst += lencopy + bytesperline;
  154. src += lencopy;
  155. /* Copy one line at a time */
  156. if (remain < bytesperline)
  157. lencopy = remain;
  158. else
  159. lencopy = bytesperline;
  160. /*
  161. * Check if we have enough space left in the buffer.
  162. * In that case, we force loop exit after copy.
  163. */
  164. if (lencopy > buf->bytesused - buf->length) {
  165. lencopy = buf->bytesused - buf->length;
  166. remain = lencopy;
  167. }
  168. /* Check if the copy is done */
  169. if (lencopy == 0 || remain == 0)
  170. return;
  171. if (lencopy < 0) {
  172. printk_ratelimited(KERN_WARNING "stk1160: negative lencopy detected\n");
  173. return;
  174. }
  175. if ((unsigned long)dst + lencopy >
  176. (unsigned long)buf->mem + buf->length) {
  177. printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
  178. return;
  179. }
  180. memcpy(dst, src, lencopy);
  181. remain -= lencopy;
  182. buf->bytesused += lencopy;
  183. buf->pos += lencopy;
  184. }
  185. }
  186. /*
  187. * Controls the isoc copy of each urb packet
  188. */
  189. static void stk1160_process_isoc(struct stk1160 *dev, struct urb *urb)
  190. {
  191. int i, len, status;
  192. u8 *p;
  193. if (!dev) {
  194. stk1160_warn("%s called with null device\n", __func__);
  195. return;
  196. }
  197. if (urb->status < 0) {
  198. /* Print status and drop current packet (or field?) */
  199. print_err_status(dev, -1, urb->status);
  200. return;
  201. }
  202. for (i = 0; i < urb->number_of_packets; i++) {
  203. status = urb->iso_frame_desc[i].status;
  204. if (status < 0) {
  205. print_err_status(dev, i, status);
  206. continue;
  207. }
  208. /* Get packet actual length and pointer to data */
  209. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  210. len = urb->iso_frame_desc[i].actual_length;
  211. /* Empty packet */
  212. if (len <= 4)
  213. continue;
  214. /*
  215. * An 8-byte packet sequence means end of field.
  216. * So if we don't have any packet, we start receiving one now
  217. * and if we do have a packet, then we are done with it.
  218. *
  219. * These end of field packets are always 0xc0 or 0x80,
  220. * but not always 8-byte long so we don't check packet length.
  221. */
  222. if (p[0] == 0xc0) {
  223. /*
  224. * If first byte is 0xc0 then we received
  225. * second field, and frame has ended.
  226. */
  227. if (dev->isoc_ctl.buf != NULL)
  228. stk1160_buffer_done(dev);
  229. dev->isoc_ctl.buf = stk1160_next_buffer(dev);
  230. if (dev->isoc_ctl.buf == NULL)
  231. return;
  232. }
  233. /*
  234. * If we don't have a buffer here, then it means we
  235. * haven't found the start mark sequence.
  236. */
  237. if (dev->isoc_ctl.buf == NULL)
  238. continue;
  239. if (p[0] == 0xc0 || p[0] == 0x80) {
  240. /* We set next packet parity and
  241. * continue to get next one
  242. */
  243. dev->isoc_ctl.buf->odd = *p & 0x40;
  244. dev->isoc_ctl.buf->pos = 0;
  245. continue;
  246. }
  247. stk1160_copy_video(dev, p, len);
  248. }
  249. }
  250. /*
  251. * IRQ callback, called by URB callback
  252. */
  253. static void stk1160_isoc_irq(struct urb *urb)
  254. {
  255. int i, rc;
  256. struct stk1160 *dev = urb->context;
  257. switch (urb->status) {
  258. case 0:
  259. break;
  260. case -ECONNRESET: /* kill */
  261. case -ENOENT:
  262. case -ESHUTDOWN:
  263. /* TODO: check uvc driver: he frees the queue here */
  264. return;
  265. default:
  266. stk1160_err("urb error! status %d\n", urb->status);
  267. return;
  268. }
  269. stk1160_process_isoc(dev, urb);
  270. /* Reset urb buffers */
  271. for (i = 0; i < urb->number_of_packets; i++) {
  272. urb->iso_frame_desc[i].status = 0;
  273. urb->iso_frame_desc[i].actual_length = 0;
  274. }
  275. rc = usb_submit_urb(urb, GFP_ATOMIC);
  276. if (rc)
  277. stk1160_err("urb re-submit failed (%d)\n", rc);
  278. }
  279. /*
  280. * Cancel urbs
  281. * This function can't be called in atomic context
  282. */
  283. void stk1160_cancel_isoc(struct stk1160 *dev)
  284. {
  285. int i, num_bufs = dev->isoc_ctl.num_bufs;
  286. /*
  287. * This check is not necessary, but we add it
  288. * to avoid a spurious debug message
  289. */
  290. if (!num_bufs)
  291. return;
  292. stk1160_dbg("killing %d urbs...\n", num_bufs);
  293. for (i = 0; i < num_bufs; i++) {
  294. /*
  295. * To kill urbs we can't be in atomic context.
  296. * We don't care for NULL pointer since
  297. * usb_kill_urb allows it.
  298. */
  299. usb_kill_urb(dev->isoc_ctl.urb[i]);
  300. }
  301. stk1160_dbg("all urbs killed\n");
  302. }
  303. /*
  304. * Releases urb and transfer buffers
  305. * Obviusly, associated urb must be killed before releasing it.
  306. */
  307. void stk1160_free_isoc(struct stk1160 *dev)
  308. {
  309. struct urb *urb;
  310. int i, num_bufs = dev->isoc_ctl.num_bufs;
  311. stk1160_dbg("freeing %d urb buffers...\n", num_bufs);
  312. for (i = 0; i < num_bufs; i++) {
  313. urb = dev->isoc_ctl.urb[i];
  314. if (urb) {
  315. if (dev->isoc_ctl.transfer_buffer[i]) {
  316. #ifndef CONFIG_DMA_NONCOHERENT
  317. usb_free_coherent(dev->udev,
  318. urb->transfer_buffer_length,
  319. dev->isoc_ctl.transfer_buffer[i],
  320. urb->transfer_dma);
  321. #else
  322. kfree(dev->isoc_ctl.transfer_buffer[i]);
  323. #endif
  324. }
  325. usb_free_urb(urb);
  326. dev->isoc_ctl.urb[i] = NULL;
  327. }
  328. dev->isoc_ctl.transfer_buffer[i] = NULL;
  329. }
  330. kfree(dev->isoc_ctl.urb);
  331. kfree(dev->isoc_ctl.transfer_buffer);
  332. dev->isoc_ctl.urb = NULL;
  333. dev->isoc_ctl.transfer_buffer = NULL;
  334. dev->isoc_ctl.num_bufs = 0;
  335. stk1160_dbg("all urb buffers freed\n");
  336. }
  337. /*
  338. * Helper for cancelling and freeing urbs
  339. * This function can't be called in atomic context
  340. */
  341. void stk1160_uninit_isoc(struct stk1160 *dev)
  342. {
  343. stk1160_cancel_isoc(dev);
  344. stk1160_free_isoc(dev);
  345. }
  346. /*
  347. * Allocate URBs
  348. */
  349. int stk1160_alloc_isoc(struct stk1160 *dev)
  350. {
  351. struct urb *urb;
  352. int i, j, k, sb_size, max_packets, num_bufs;
  353. /*
  354. * It may be necessary to release isoc here,
  355. * since isoc are only released on disconnection.
  356. * (see new_pkt_size flag)
  357. */
  358. if (dev->isoc_ctl.num_bufs)
  359. stk1160_uninit_isoc(dev);
  360. stk1160_dbg("allocating urbs...\n");
  361. num_bufs = STK1160_NUM_BUFS;
  362. max_packets = STK1160_NUM_PACKETS;
  363. sb_size = max_packets * dev->max_pkt_size;
  364. dev->isoc_ctl.buf = NULL;
  365. dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
  366. dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
  367. if (!dev->isoc_ctl.urb) {
  368. stk1160_err("out of memory for urb array\n");
  369. return -ENOMEM;
  370. }
  371. dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
  372. GFP_KERNEL);
  373. if (!dev->isoc_ctl.transfer_buffer) {
  374. stk1160_err("out of memory for usb transfers\n");
  375. kfree(dev->isoc_ctl.urb);
  376. return -ENOMEM;
  377. }
  378. /* allocate urbs and transfer buffers */
  379. for (i = 0; i < num_bufs; i++) {
  380. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  381. if (!urb)
  382. goto free_i_bufs;
  383. dev->isoc_ctl.urb[i] = urb;
  384. #ifndef CONFIG_DMA_NONCOHERENT
  385. dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
  386. sb_size, GFP_KERNEL, &urb->transfer_dma);
  387. #else
  388. dev->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
  389. #endif
  390. if (!dev->isoc_ctl.transfer_buffer[i]) {
  391. stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
  392. sb_size, i);
  393. /* Not enough transfer buffers, so just give up */
  394. if (i < STK1160_MIN_BUFS)
  395. goto free_i_bufs;
  396. goto nomore_tx_bufs;
  397. }
  398. memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
  399. /*
  400. * FIXME: Where can I get the endpoint?
  401. */
  402. urb->dev = dev->udev;
  403. urb->pipe = usb_rcvisocpipe(dev->udev, STK1160_EP_VIDEO);
  404. urb->transfer_buffer = dev->isoc_ctl.transfer_buffer[i];
  405. urb->transfer_buffer_length = sb_size;
  406. urb->complete = stk1160_isoc_irq;
  407. urb->context = dev;
  408. urb->interval = 1;
  409. urb->start_frame = 0;
  410. urb->number_of_packets = max_packets;
  411. #ifndef CONFIG_DMA_NONCOHERENT
  412. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  413. #else
  414. urb->transfer_flags = URB_ISO_ASAP;
  415. #endif
  416. k = 0;
  417. for (j = 0; j < max_packets; j++) {
  418. urb->iso_frame_desc[j].offset = k;
  419. urb->iso_frame_desc[j].length =
  420. dev->isoc_ctl.max_pkt_size;
  421. k += dev->isoc_ctl.max_pkt_size;
  422. }
  423. }
  424. stk1160_dbg("%d urbs allocated\n", num_bufs);
  425. /* At last we can say we have some buffers */
  426. dev->isoc_ctl.num_bufs = num_bufs;
  427. return 0;
  428. nomore_tx_bufs:
  429. /*
  430. * Failed to allocate desired buffer count. However, we may have
  431. * enough to work fine, so we just free the extra urb,
  432. * store the allocated count and keep going, fingers crossed!
  433. */
  434. usb_free_urb(dev->isoc_ctl.urb[i]);
  435. dev->isoc_ctl.urb[i] = NULL;
  436. stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
  437. dev->isoc_ctl.num_bufs = i - 1;
  438. return 0;
  439. free_i_bufs:
  440. /* Save the allocated buffers so far, so we can properly free them */
  441. dev->isoc_ctl.num_bufs = i+1;
  442. stk1160_free_isoc(dev);
  443. return -ENOMEM;
  444. }