stk1160-video.c 12 KB

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