uvc_video.c 9.7 KB

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