uvc_video.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. */
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/gadget.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->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 = queue->mem + buf->buf.m.offset + queue->buf_used;
  43. nbytes = min((unsigned int)len, buf->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->buf.bytesused == video->queue.buf_used) {
  70. video->queue.buf_used = 0;
  71. buf->state = UVC_BUF_STATE_DONE;
  72. uvc_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->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->buf.bytesused == video->queue.buf_used) {
  96. video->queue.buf_used = 0;
  97. buf->state = UVC_BUF_STATE_DONE;
  98. uvc_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_buffer *buf;
  140. unsigned long flags;
  141. int ret;
  142. switch (req->status) {
  143. case 0:
  144. break;
  145. case -ESHUTDOWN:
  146. printk(KERN_INFO "VS request cancelled.\n");
  147. goto requeue;
  148. default:
  149. printk(KERN_INFO "VS request completed with status %d.\n",
  150. req->status);
  151. goto requeue;
  152. }
  153. spin_lock_irqsave(&video->queue.irqlock, flags);
  154. buf = uvc_queue_head(&video->queue);
  155. if (buf == NULL) {
  156. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  157. goto requeue;
  158. }
  159. video->encode(req, video, buf);
  160. if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
  161. printk(KERN_INFO "Failed to queue request (%d).\n", ret);
  162. usb_ep_set_halt(ep);
  163. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  164. goto requeue;
  165. }
  166. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  167. return;
  168. requeue:
  169. spin_lock_irqsave(&video->req_lock, flags);
  170. list_add_tail(&req->list, &video->req_free);
  171. spin_unlock_irqrestore(&video->req_lock, flags);
  172. }
  173. static int
  174. uvc_video_free_requests(struct uvc_video *video)
  175. {
  176. unsigned int i;
  177. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  178. if (video->req[i]) {
  179. usb_ep_free_request(video->ep, video->req[i]);
  180. video->req[i] = NULL;
  181. }
  182. if (video->req_buffer[i]) {
  183. kfree(video->req_buffer[i]);
  184. video->req_buffer[i] = NULL;
  185. }
  186. }
  187. INIT_LIST_HEAD(&video->req_free);
  188. video->req_size = 0;
  189. return 0;
  190. }
  191. static int
  192. uvc_video_alloc_requests(struct uvc_video *video)
  193. {
  194. unsigned int i;
  195. int ret = -ENOMEM;
  196. BUG_ON(video->req_size);
  197. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  198. video->req_buffer[i] = kmalloc(video->ep->maxpacket, GFP_KERNEL);
  199. if (video->req_buffer[i] == NULL)
  200. goto error;
  201. video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
  202. if (video->req[i] == NULL)
  203. goto error;
  204. video->req[i]->buf = video->req_buffer[i];
  205. video->req[i]->length = 0;
  206. video->req[i]->dma = DMA_ADDR_INVALID;
  207. video->req[i]->complete = uvc_video_complete;
  208. video->req[i]->context = video;
  209. list_add_tail(&video->req[i]->list, &video->req_free);
  210. }
  211. video->req_size = video->ep->maxpacket;
  212. return 0;
  213. error:
  214. uvc_video_free_requests(video);
  215. return ret;
  216. }
  217. /* --------------------------------------------------------------------------
  218. * Video streaming
  219. */
  220. /*
  221. * uvc_video_pump - Pump video data into the USB requests
  222. *
  223. * This function fills the available USB requests (listed in req_free) with
  224. * video data from the queued buffers.
  225. */
  226. static int
  227. uvc_video_pump(struct uvc_video *video)
  228. {
  229. struct usb_request *req;
  230. struct uvc_buffer *buf;
  231. unsigned long flags;
  232. int ret;
  233. /* FIXME TODO Race between uvc_video_pump and requests completion
  234. * handler ???
  235. */
  236. while (1) {
  237. /* Retrieve the first available USB request, protected by the
  238. * request lock.
  239. */
  240. spin_lock_irqsave(&video->req_lock, flags);
  241. if (list_empty(&video->req_free)) {
  242. spin_unlock_irqrestore(&video->req_lock, flags);
  243. return 0;
  244. }
  245. req = list_first_entry(&video->req_free, struct usb_request,
  246. list);
  247. list_del(&req->list);
  248. spin_unlock_irqrestore(&video->req_lock, flags);
  249. /* Retrieve the first available video buffer and fill the
  250. * request, protected by the video queue irqlock.
  251. */
  252. spin_lock_irqsave(&video->queue.irqlock, flags);
  253. buf = uvc_queue_head(&video->queue);
  254. if (buf == NULL) {
  255. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  256. break;
  257. }
  258. video->encode(req, video, buf);
  259. /* Queue the USB request */
  260. if ((ret = usb_ep_queue(video->ep, req, GFP_KERNEL)) < 0) {
  261. printk(KERN_INFO "Failed to queue request (%d)\n", ret);
  262. usb_ep_set_halt(video->ep);
  263. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  264. break;
  265. }
  266. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  267. }
  268. spin_lock_irqsave(&video->req_lock, flags);
  269. list_add_tail(&req->list, &video->req_free);
  270. spin_unlock_irqrestore(&video->req_lock, flags);
  271. return 0;
  272. }
  273. /*
  274. * Enable or disable the video stream.
  275. */
  276. static int
  277. uvc_video_enable(struct uvc_video *video, int enable)
  278. {
  279. unsigned int i;
  280. int ret;
  281. if (video->ep == NULL) {
  282. printk(KERN_INFO "Video enable failed, device is "
  283. "uninitialized.\n");
  284. return -ENODEV;
  285. }
  286. if (!enable) {
  287. for (i = 0; i < UVC_NUM_REQUESTS; ++i)
  288. usb_ep_dequeue(video->ep, video->req[i]);
  289. uvc_video_free_requests(video);
  290. uvc_queue_enable(&video->queue, 0);
  291. return 0;
  292. }
  293. if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
  294. return ret;
  295. if ((ret = uvc_video_alloc_requests(video)) < 0)
  296. return ret;
  297. if (video->max_payload_size) {
  298. video->encode = uvc_video_encode_bulk;
  299. video->payload_size = 0;
  300. } else
  301. video->encode = uvc_video_encode_isoc;
  302. return uvc_video_pump(video);
  303. }
  304. /*
  305. * Initialize the UVC video stream.
  306. */
  307. static int
  308. uvc_video_init(struct uvc_video *video)
  309. {
  310. INIT_LIST_HEAD(&video->req_free);
  311. spin_lock_init(&video->req_lock);
  312. video->fcc = V4L2_PIX_FMT_YUYV;
  313. video->bpp = 16;
  314. video->width = 320;
  315. video->height = 240;
  316. video->imagesize = 320 * 240 * 2;
  317. /* Initialize the video buffers queue. */
  318. uvc_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  319. return 0;
  320. }