uvc_video.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * uvc_video.c -- USB Video Class Gadget driver
  3. *
  4. * Copyright (C) 2009-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/usb/ch9.h>
  16. #include <linux/usb/gadget.h>
  17. #include <linux/usb/video.h>
  18. #include <media/v4l2-dev.h>
  19. #include "uvc.h"
  20. #include "uvc_queue.h"
  21. /* --------------------------------------------------------------------------
  22. * Video codecs
  23. */
  24. static int
  25. uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
  26. u8 *data, int len)
  27. {
  28. data[0] = 2;
  29. data[1] = UVC_STREAM_EOH | video->fid;
  30. if (buf->bytesused - video->queue.buf_used <= len - 2)
  31. data[1] |= UVC_STREAM_EOF;
  32. return 2;
  33. }
  34. static int
  35. uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
  36. u8 *data, int len)
  37. {
  38. struct uvc_video_queue *queue = &video->queue;
  39. unsigned int nbytes;
  40. void *mem;
  41. /* Copy video data to the USB buffer. */
  42. mem = buf->mem + queue->buf_used;
  43. nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
  44. memcpy(data, mem, nbytes);
  45. queue->buf_used += nbytes;
  46. return nbytes;
  47. }
  48. static void
  49. uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
  50. struct uvc_buffer *buf)
  51. {
  52. void *mem = req->buf;
  53. int len = video->req_size;
  54. int ret;
  55. /* Add a header at the beginning of the payload. */
  56. if (video->payload_size == 0) {
  57. ret = uvc_video_encode_header(video, buf, mem, len);
  58. video->payload_size += ret;
  59. mem += ret;
  60. len -= ret;
  61. }
  62. /* Process video data. */
  63. len = min((int)(video->max_payload_size - video->payload_size), len);
  64. ret = uvc_video_encode_data(video, buf, mem, len);
  65. video->payload_size += ret;
  66. len -= ret;
  67. req->length = video->req_size - len;
  68. req->zero = video->payload_size == video->max_payload_size;
  69. if (buf->bytesused == video->queue.buf_used) {
  70. video->queue.buf_used = 0;
  71. buf->state = UVC_BUF_STATE_DONE;
  72. uvcg_queue_next_buffer(&video->queue, buf);
  73. video->fid ^= UVC_STREAM_FID;
  74. video->payload_size = 0;
  75. }
  76. if (video->payload_size == video->max_payload_size ||
  77. buf->bytesused == video->queue.buf_used)
  78. video->payload_size = 0;
  79. }
  80. static void
  81. uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
  82. struct uvc_buffer *buf)
  83. {
  84. void *mem = req->buf;
  85. int len = video->req_size;
  86. int ret;
  87. /* Add the header. */
  88. ret = uvc_video_encode_header(video, buf, mem, len);
  89. mem += ret;
  90. len -= ret;
  91. /* Process video data. */
  92. ret = uvc_video_encode_data(video, buf, mem, len);
  93. len -= ret;
  94. req->length = video->req_size - len;
  95. if (buf->bytesused == video->queue.buf_used) {
  96. video->queue.buf_used = 0;
  97. buf->state = UVC_BUF_STATE_DONE;
  98. uvcg_queue_next_buffer(&video->queue, buf);
  99. video->fid ^= UVC_STREAM_FID;
  100. }
  101. }
  102. /* --------------------------------------------------------------------------
  103. * Request handling
  104. */
  105. /*
  106. * I somehow feel that synchronisation won't be easy to achieve here. We have
  107. * three events that control USB requests submission:
  108. *
  109. * - USB request completion: the completion handler will resubmit the request
  110. * if a video buffer is available.
  111. *
  112. * - USB interface setting selection: in response to a SET_INTERFACE request,
  113. * the handler will start streaming if a video buffer is available and if
  114. * video is not currently streaming.
  115. *
  116. * - V4L2 buffer queueing: the driver will start streaming if video is not
  117. * currently streaming.
  118. *
  119. * Race conditions between those 3 events might lead to deadlocks or other
  120. * nasty side effects.
  121. *
  122. * The "video currently streaming" condition can't be detected by the irqqueue
  123. * being empty, as a request can still be in flight. A separate "queue paused"
  124. * flag is thus needed.
  125. *
  126. * The paused flag will be set when we try to retrieve the irqqueue head if the
  127. * queue is empty, and cleared when we queue a buffer.
  128. *
  129. * The USB request completion handler will get the buffer at the irqqueue head
  130. * under protection of the queue spinlock. If the queue is empty, the streaming
  131. * paused flag will be set. Right after releasing the spinlock a userspace
  132. * application can queue a buffer. The flag will then cleared, and the ioctl
  133. * handler will restart the video stream.
  134. */
  135. static void
  136. uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  137. {
  138. struct uvc_video *video = req->context;
  139. struct uvc_video_queue *queue = &video->queue;
  140. struct uvc_buffer *buf;
  141. unsigned long flags;
  142. int ret;
  143. switch (req->status) {
  144. case 0:
  145. break;
  146. case -ESHUTDOWN: /* disconnect from host. */
  147. printk(KERN_DEBUG "VS request cancelled.\n");
  148. uvcg_queue_cancel(queue, 1);
  149. goto requeue;
  150. default:
  151. printk(KERN_INFO "VS request completed with status %d.\n",
  152. req->status);
  153. uvcg_queue_cancel(queue, 0);
  154. goto requeue;
  155. }
  156. spin_lock_irqsave(&video->queue.irqlock, flags);
  157. buf = uvcg_queue_head(&video->queue);
  158. if (buf == NULL) {
  159. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  160. goto requeue;
  161. }
  162. video->encode(req, video, buf);
  163. if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
  164. printk(KERN_INFO "Failed to queue request (%d).\n", ret);
  165. usb_ep_set_halt(ep);
  166. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  167. uvcg_queue_cancel(queue, 0);
  168. goto requeue;
  169. }
  170. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  171. return;
  172. requeue:
  173. spin_lock_irqsave(&video->req_lock, flags);
  174. list_add_tail(&req->list, &video->req_free);
  175. spin_unlock_irqrestore(&video->req_lock, flags);
  176. }
  177. static int
  178. uvc_video_free_requests(struct uvc_video *video)
  179. {
  180. unsigned int i;
  181. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  182. if (video->req[i]) {
  183. usb_ep_free_request(video->ep, video->req[i]);
  184. video->req[i] = NULL;
  185. }
  186. if (video->req_buffer[i]) {
  187. kfree(video->req_buffer[i]);
  188. video->req_buffer[i] = NULL;
  189. }
  190. }
  191. INIT_LIST_HEAD(&video->req_free);
  192. video->req_size = 0;
  193. return 0;
  194. }
  195. static int
  196. uvc_video_alloc_requests(struct uvc_video *video)
  197. {
  198. unsigned int req_size;
  199. unsigned int i;
  200. int ret = -ENOMEM;
  201. BUG_ON(video->req_size);
  202. req_size = video->ep->maxpacket
  203. * max_t(unsigned int, video->ep->maxburst, 1)
  204. * (video->ep->mult + 1);
  205. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  206. video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
  207. if (video->req_buffer[i] == NULL)
  208. goto error;
  209. video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
  210. if (video->req[i] == NULL)
  211. goto error;
  212. video->req[i]->buf = video->req_buffer[i];
  213. video->req[i]->length = 0;
  214. video->req[i]->complete = uvc_video_complete;
  215. video->req[i]->context = video;
  216. list_add_tail(&video->req[i]->list, &video->req_free);
  217. }
  218. video->req_size = req_size;
  219. return 0;
  220. error:
  221. uvc_video_free_requests(video);
  222. return ret;
  223. }
  224. /* --------------------------------------------------------------------------
  225. * Video streaming
  226. */
  227. /*
  228. * uvcg_video_pump - Pump video data into the USB requests
  229. *
  230. * This function fills the available USB requests (listed in req_free) with
  231. * video data from the queued buffers.
  232. */
  233. int uvcg_video_pump(struct uvc_video *video)
  234. {
  235. struct uvc_video_queue *queue = &video->queue;
  236. struct usb_request *req;
  237. struct uvc_buffer *buf;
  238. unsigned long flags;
  239. int ret;
  240. /* FIXME TODO Race between uvcg_video_pump and requests completion
  241. * handler ???
  242. */
  243. while (1) {
  244. /* Retrieve the first available USB request, protected by the
  245. * request lock.
  246. */
  247. spin_lock_irqsave(&video->req_lock, flags);
  248. if (list_empty(&video->req_free)) {
  249. spin_unlock_irqrestore(&video->req_lock, flags);
  250. return 0;
  251. }
  252. req = list_first_entry(&video->req_free, struct usb_request,
  253. list);
  254. list_del(&req->list);
  255. spin_unlock_irqrestore(&video->req_lock, flags);
  256. /* Retrieve the first available video buffer and fill the
  257. * request, protected by the video queue irqlock.
  258. */
  259. spin_lock_irqsave(&queue->irqlock, flags);
  260. buf = uvcg_queue_head(queue);
  261. if (buf == NULL) {
  262. spin_unlock_irqrestore(&queue->irqlock, flags);
  263. break;
  264. }
  265. video->encode(req, video, buf);
  266. /* Queue the USB request */
  267. ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
  268. if (ret < 0) {
  269. printk(KERN_INFO "Failed to queue request (%d)\n", ret);
  270. usb_ep_set_halt(video->ep);
  271. spin_unlock_irqrestore(&queue->irqlock, flags);
  272. uvcg_queue_cancel(queue, 0);
  273. break;
  274. }
  275. spin_unlock_irqrestore(&queue->irqlock, flags);
  276. }
  277. spin_lock_irqsave(&video->req_lock, flags);
  278. list_add_tail(&req->list, &video->req_free);
  279. spin_unlock_irqrestore(&video->req_lock, flags);
  280. return 0;
  281. }
  282. /*
  283. * Enable or disable the video stream.
  284. */
  285. int uvcg_video_enable(struct uvc_video *video, int enable)
  286. {
  287. unsigned int i;
  288. int ret;
  289. if (video->ep == NULL) {
  290. printk(KERN_INFO "Video enable failed, device is "
  291. "uninitialized.\n");
  292. return -ENODEV;
  293. }
  294. if (!enable) {
  295. for (i = 0; i < UVC_NUM_REQUESTS; ++i)
  296. if (video->req[i])
  297. usb_ep_dequeue(video->ep, video->req[i]);
  298. uvc_video_free_requests(video);
  299. uvcg_queue_enable(&video->queue, 0);
  300. return 0;
  301. }
  302. if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  303. return ret;
  304. if ((ret = uvc_video_alloc_requests(video)) < 0)
  305. return ret;
  306. if (video->max_payload_size) {
  307. video->encode = uvc_video_encode_bulk;
  308. video->payload_size = 0;
  309. } else
  310. video->encode = uvc_video_encode_isoc;
  311. return uvcg_video_pump(video);
  312. }
  313. /*
  314. * Initialize the UVC video stream.
  315. */
  316. int uvcg_video_init(struct uvc_video *video)
  317. {
  318. INIT_LIST_HEAD(&video->req_free);
  319. spin_lock_init(&video->req_lock);
  320. video->fcc = V4L2_PIX_FMT_YUYV;
  321. video->bpp = 16;
  322. video->width = 320;
  323. video->height = 240;
  324. video->imagesize = 320 * 240 * 2;
  325. /* Initialize the video buffers queue. */
  326. uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  327. return 0;
  328. }