uvc_video.c 9.4 KB

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