uvc_video.c 9.6 KB

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