stk1160-video.c 13 KB

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