uvc_video.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * uvc_video.c -- USB Video Class Gadget driver
  4. *
  5. * Copyright (C) 2009-2010
  6. * Laurent Pinchart ([email protected])
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/usb/ch9.h>
  12. #include <linux/usb/gadget.h>
  13. #include <linux/usb/video.h>
  14. #include <asm/unaligned.h>
  15. #include <media/v4l2-dev.h>
  16. #include "uvc.h"
  17. #include "uvc_queue.h"
  18. #include "uvc_video.h"
  19. /* --------------------------------------------------------------------------
  20. * Video codecs
  21. */
  22. static int
  23. uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
  24. u8 *data, int len)
  25. {
  26. struct uvc_device *uvc = container_of(video, struct uvc_device, video);
  27. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  28. struct timespec64 ts = ns_to_timespec64(buf->buf.vb2_buf.timestamp);
  29. int pos = 2;
  30. data[1] = UVC_STREAM_EOH | video->fid;
  31. if (video->queue.buf_used == 0 && ts.tv_sec) {
  32. /* dwClockFrequency is 48 MHz */
  33. u32 pts = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
  34. data[1] |= UVC_STREAM_PTS;
  35. put_unaligned_le32(pts, &data[pos]);
  36. pos += 4;
  37. }
  38. if (cdev->gadget->ops->get_frame) {
  39. u32 sof, stc;
  40. sof = usb_gadget_frame_number(cdev->gadget);
  41. ktime_get_ts64(&ts);
  42. stc = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
  43. data[1] |= UVC_STREAM_SCR;
  44. put_unaligned_le32(stc, &data[pos]);
  45. put_unaligned_le16(sof, &data[pos+4]);
  46. pos += 6;
  47. }
  48. data[0] = pos;
  49. if (buf->bytesused - video->queue.buf_used <= len - pos)
  50. data[1] |= UVC_STREAM_EOF;
  51. return pos;
  52. }
  53. static int
  54. uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
  55. u8 *data, int len)
  56. {
  57. struct uvc_video_queue *queue = &video->queue;
  58. unsigned int nbytes;
  59. void *mem;
  60. /* Copy video data to the USB buffer. */
  61. mem = buf->mem + queue->buf_used;
  62. nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
  63. memcpy(data, mem, nbytes);
  64. queue->buf_used += nbytes;
  65. return nbytes;
  66. }
  67. static void
  68. uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
  69. struct uvc_buffer *buf)
  70. {
  71. void *mem = req->buf;
  72. struct uvc_request *ureq = req->context;
  73. int len = video->req_size;
  74. int ret;
  75. /* Add a header at the beginning of the payload. */
  76. if (video->payload_size == 0) {
  77. ret = uvc_video_encode_header(video, buf, mem, len);
  78. video->payload_size += ret;
  79. mem += ret;
  80. len -= ret;
  81. }
  82. /* Process video data. */
  83. len = min((int)(video->max_payload_size - video->payload_size), len);
  84. ret = uvc_video_encode_data(video, buf, mem, len);
  85. video->payload_size += ret;
  86. len -= ret;
  87. req->length = video->req_size - len;
  88. req->zero = video->payload_size == video->max_payload_size;
  89. if (buf->bytesused == video->queue.buf_used) {
  90. video->queue.buf_used = 0;
  91. buf->state = UVC_BUF_STATE_DONE;
  92. list_del(&buf->queue);
  93. video->fid ^= UVC_STREAM_FID;
  94. ureq->last_buf = buf;
  95. video->payload_size = 0;
  96. }
  97. if (video->payload_size == video->max_payload_size ||
  98. video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE ||
  99. buf->bytesused == video->queue.buf_used)
  100. video->payload_size = 0;
  101. }
  102. static void
  103. uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
  104. struct uvc_buffer *buf)
  105. {
  106. unsigned int pending = buf->bytesused - video->queue.buf_used;
  107. struct uvc_request *ureq = req->context;
  108. struct scatterlist *sg, *iter;
  109. unsigned int len = video->req_size;
  110. unsigned int sg_left, part = 0;
  111. unsigned int i;
  112. int header_len;
  113. sg = ureq->sgt.sgl;
  114. sg_init_table(sg, ureq->sgt.nents);
  115. /* Init the header. */
  116. header_len = uvc_video_encode_header(video, buf, ureq->header,
  117. video->req_size);
  118. sg_set_buf(sg, ureq->header, header_len);
  119. len -= header_len;
  120. if (pending <= len)
  121. len = pending;
  122. req->length = (len == pending) ?
  123. len + header_len : video->req_size;
  124. /* Init the pending sgs with payload */
  125. sg = sg_next(sg);
  126. for_each_sg(sg, iter, ureq->sgt.nents - 1, i) {
  127. if (!len || !buf->sg || !buf->sg->length)
  128. break;
  129. sg_left = buf->sg->length - buf->offset;
  130. part = min_t(unsigned int, len, sg_left);
  131. sg_set_page(iter, sg_page(buf->sg), part, buf->offset);
  132. if (part == sg_left) {
  133. buf->offset = 0;
  134. buf->sg = sg_next(buf->sg);
  135. } else {
  136. buf->offset += part;
  137. }
  138. len -= part;
  139. }
  140. /* Assign the video data with header. */
  141. req->buf = NULL;
  142. req->sg = ureq->sgt.sgl;
  143. req->num_sgs = i + 1;
  144. req->length -= len;
  145. video->queue.buf_used += req->length - header_len;
  146. if (buf->bytesused == video->queue.buf_used || !buf->sg ||
  147. video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
  148. video->queue.buf_used = 0;
  149. buf->state = UVC_BUF_STATE_DONE;
  150. buf->offset = 0;
  151. list_del(&buf->queue);
  152. video->fid ^= UVC_STREAM_FID;
  153. ureq->last_buf = buf;
  154. }
  155. }
  156. static void
  157. uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
  158. struct uvc_buffer *buf)
  159. {
  160. void *mem = req->buf;
  161. struct uvc_request *ureq = req->context;
  162. int len = video->req_size;
  163. int ret;
  164. /* Add the header. */
  165. ret = uvc_video_encode_header(video, buf, mem, len);
  166. mem += ret;
  167. len -= ret;
  168. /* Process video data. */
  169. ret = uvc_video_encode_data(video, buf, mem, len);
  170. len -= ret;
  171. req->length = video->req_size - len;
  172. if (buf->bytesused == video->queue.buf_used ||
  173. video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
  174. video->queue.buf_used = 0;
  175. buf->state = UVC_BUF_STATE_DONE;
  176. list_del(&buf->queue);
  177. video->fid ^= UVC_STREAM_FID;
  178. ureq->last_buf = buf;
  179. }
  180. }
  181. /* --------------------------------------------------------------------------
  182. * Request handling
  183. */
  184. /*
  185. * Callers must take care to hold req_lock when this function may be called
  186. * from multiple threads. For example, when frames are streaming to the host.
  187. */
  188. static void
  189. uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
  190. {
  191. sg_free_table(&ureq->sgt);
  192. if (ureq->req && ep) {
  193. usb_ep_free_request(ep, ureq->req);
  194. ureq->req = NULL;
  195. }
  196. kfree(ureq->req_buffer);
  197. ureq->req_buffer = NULL;
  198. if (!list_empty(&ureq->list))
  199. list_del_init(&ureq->list);
  200. kfree(ureq);
  201. }
  202. static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  203. {
  204. int ret;
  205. ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
  206. if (ret < 0) {
  207. uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
  208. ret);
  209. /* If the endpoint is disabled the descriptor may be NULL. */
  210. if (video->ep->desc) {
  211. /* Isochronous endpoints can't be halted. */
  212. if (usb_endpoint_xfer_bulk(video->ep->desc))
  213. usb_ep_set_halt(video->ep);
  214. }
  215. }
  216. return ret;
  217. }
  218. /* This function must be called with video->req_lock held. */
  219. static int uvcg_video_usb_req_queue(struct uvc_video *video,
  220. struct usb_request *req, bool queue_to_ep)
  221. {
  222. bool is_bulk = video->max_payload_size;
  223. struct list_head *list = NULL;
  224. if (!video->is_enabled)
  225. return -ENODEV;
  226. if (queue_to_ep) {
  227. struct uvc_request *ureq = req->context;
  228. /*
  229. * With USB3 handling more requests at a higher speed, we can't
  230. * afford to generate an interrupt for every request. Decide to
  231. * interrupt:
  232. *
  233. * - When no more requests are available in the free queue, as
  234. * this may be our last chance to refill the endpoint's
  235. * request queue.
  236. *
  237. * - When this is request is the last request for the video
  238. * buffer, as we want to start sending the next video buffer
  239. * ASAP in case it doesn't get started already in the next
  240. * iteration of this loop.
  241. *
  242. * - Four times over the length of the requests queue (as
  243. * indicated by video->uvc_num_requests), as a trade-off
  244. * between latency and interrupt load.
  245. */
  246. if (list_empty(&video->req_free) || ureq->last_buf ||
  247. !(video->req_int_count %
  248. DIV_ROUND_UP(video->uvc_num_requests, 4))) {
  249. video->req_int_count = 0;
  250. req->no_interrupt = 0;
  251. } else {
  252. req->no_interrupt = 1;
  253. }
  254. video->req_int_count++;
  255. return uvcg_video_ep_queue(video, req);
  256. }
  257. /*
  258. * If we're not queuing to the ep, for isoc we're queuing
  259. * to the req_ready list, otherwise req_free.
  260. */
  261. list = is_bulk ? &video->req_free : &video->req_ready;
  262. list_add_tail(&req->list, list);
  263. return 0;
  264. }
  265. /*
  266. * Must only be called from uvcg_video_enable - since after that we only want to
  267. * queue requests to the endpoint from the uvc_video_complete complete handler.
  268. * This function is needed in order to 'kick start' the flow of requests from
  269. * gadget driver to the usb controller.
  270. */
  271. static void uvc_video_ep_queue_initial_requests(struct uvc_video *video)
  272. {
  273. struct usb_request *req = NULL;
  274. unsigned long flags = 0;
  275. unsigned int count = 0;
  276. int ret = 0;
  277. /*
  278. * We only queue half of the free list since we still want to have
  279. * some free usb_requests in the free list for the video_pump async_wq
  280. * thread to encode uvc buffers into. Otherwise we could get into a
  281. * situation where the free list does not have any usb requests to
  282. * encode into - we always end up queueing 0 length requests to the
  283. * end point.
  284. */
  285. unsigned int half_list_size = video->uvc_num_requests / 2;
  286. spin_lock_irqsave(&video->req_lock, flags);
  287. /*
  288. * Take these requests off the free list and queue them all to the
  289. * endpoint. Since we queue 0 length requests with the req_lock held,
  290. * there isn't any 'data' race involved here with the complete handler.
  291. */
  292. while (count < half_list_size) {
  293. req = list_first_entry(&video->req_free, struct usb_request,
  294. list);
  295. list_del(&req->list);
  296. req->length = 0;
  297. ret = uvcg_video_ep_queue(video, req);
  298. if (ret < 0) {
  299. uvcg_queue_cancel(&video->queue, 0);
  300. break;
  301. }
  302. count++;
  303. }
  304. spin_unlock_irqrestore(&video->req_lock, flags);
  305. }
  306. static void
  307. uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  308. {
  309. struct uvc_request *ureq = req->context;
  310. struct uvc_video *video = ureq->video;
  311. struct uvc_video_queue *queue = &video->queue;
  312. struct uvc_buffer *last_buf;
  313. unsigned long flags;
  314. bool is_bulk = video->max_payload_size;
  315. int ret = 0;
  316. spin_lock_irqsave(&video->req_lock, flags);
  317. if (!video->is_enabled) {
  318. /*
  319. * When is_enabled is false, uvcg_video_disable() ensures
  320. * that in-flight uvc_buffers are returned, so we can
  321. * safely call free_request without worrying about
  322. * last_buf.
  323. */
  324. uvc_video_free_request(ureq, ep);
  325. spin_unlock_irqrestore(&video->req_lock, flags);
  326. return;
  327. }
  328. last_buf = ureq->last_buf;
  329. ureq->last_buf = NULL;
  330. spin_unlock_irqrestore(&video->req_lock, flags);
  331. switch (req->status) {
  332. case 0:
  333. break;
  334. case -EXDEV:
  335. uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n");
  336. queue->flags |= UVC_QUEUE_DROP_INCOMPLETE;
  337. break;
  338. case -ESHUTDOWN: /* disconnect from host. */
  339. uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
  340. uvcg_queue_cancel(queue, 1);
  341. break;
  342. default:
  343. uvcg_warn(&video->uvc->func,
  344. "VS request completed with status %d.\n",
  345. req->status);
  346. uvcg_queue_cancel(queue, 0);
  347. }
  348. if (last_buf) {
  349. spin_lock_irqsave(&queue->irqlock, flags);
  350. uvcg_complete_buffer(queue, last_buf);
  351. spin_unlock_irqrestore(&queue->irqlock, flags);
  352. }
  353. spin_lock_irqsave(&video->req_lock, flags);
  354. /*
  355. * Video stream might have been disabled while we were
  356. * processing the current usb_request. So make sure
  357. * we're still streaming before queueing the usb_request
  358. * back to req_free
  359. */
  360. if (video->is_enabled) {
  361. /*
  362. * Here we check whether any request is available in the ready
  363. * list. If it is, queue it to the ep and add the current
  364. * usb_request to the req_free list - for video_pump to fill in.
  365. * Otherwise, just use the current usb_request to queue a 0
  366. * length request to the ep. Since we always add to the req_free
  367. * list if we dequeue from the ready list, there will never
  368. * be a situation where the req_free list is completely out of
  369. * requests and cannot recover.
  370. */
  371. struct usb_request *to_queue = req;
  372. to_queue->length = 0;
  373. if (!list_empty(&video->req_ready)) {
  374. to_queue = list_first_entry(&video->req_ready,
  375. struct usb_request, list);
  376. list_del(&to_queue->list);
  377. list_add_tail(&req->list, &video->req_free);
  378. /*
  379. * Queue work to the wq as well since it is possible that a
  380. * buffer may not have been completely encoded with the set of
  381. * in-flight usb requests for whih the complete callbacks are
  382. * firing.
  383. * In that case, if we do not queue work to the worker thread,
  384. * the buffer will never be marked as complete - and therefore
  385. * not be returned to userpsace. As a result,
  386. * dequeue -> queue -> dequeue flow of uvc buffers will not
  387. * happen.
  388. */
  389. queue_work(video->async_wq, &video->pump);
  390. }
  391. /*
  392. * Queue to the endpoint. The actual queueing to ep will
  393. * only happen on one thread - the async_wq for bulk endpoints
  394. * and this thread for isoc endpoints.
  395. */
  396. ret = uvcg_video_usb_req_queue(video, to_queue, !is_bulk);
  397. if (ret < 0) {
  398. /*
  399. * Endpoint error, but the stream is still enabled.
  400. * Put request back in req_free for it to be cleaned
  401. * up later.
  402. */
  403. list_add_tail(&to_queue->list, &video->req_free);
  404. }
  405. } else {
  406. uvc_video_free_request(ureq, ep);
  407. ret = 0;
  408. }
  409. spin_unlock_irqrestore(&video->req_lock, flags);
  410. if (ret < 0)
  411. uvcg_queue_cancel(queue, 0);
  412. }
  413. static int
  414. uvc_video_free_requests(struct uvc_video *video)
  415. {
  416. struct uvc_request *ureq, *temp;
  417. list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
  418. uvc_video_free_request(ureq, video->ep);
  419. INIT_LIST_HEAD(&video->ureqs);
  420. INIT_LIST_HEAD(&video->req_free);
  421. INIT_LIST_HEAD(&video->req_ready);
  422. video->req_size = 0;
  423. return 0;
  424. }
  425. static int
  426. uvc_video_alloc_requests(struct uvc_video *video)
  427. {
  428. struct uvc_request *ureq;
  429. unsigned int req_size;
  430. unsigned int i;
  431. int ret = -ENOMEM;
  432. BUG_ON(video->req_size);
  433. req_size = video->ep->maxpacket
  434. * max_t(unsigned int, video->ep->maxburst, 1)
  435. * (video->ep->mult);
  436. for (i = 0; i < video->uvc_num_requests; i++) {
  437. ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
  438. if (ureq == NULL)
  439. goto error;
  440. INIT_LIST_HEAD(&ureq->list);
  441. list_add_tail(&ureq->list, &video->ureqs);
  442. ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
  443. if (ureq->req_buffer == NULL)
  444. goto error;
  445. ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
  446. if (ureq->req == NULL)
  447. goto error;
  448. ureq->req->buf = ureq->req_buffer;
  449. ureq->req->length = 0;
  450. ureq->req->complete = uvc_video_complete;
  451. ureq->req->context = ureq;
  452. ureq->video = video;
  453. ureq->last_buf = NULL;
  454. list_add_tail(&ureq->req->list, &video->req_free);
  455. /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
  456. sg_alloc_table(&ureq->sgt,
  457. DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
  458. PAGE_SIZE) + 2, GFP_KERNEL);
  459. }
  460. video->req_size = req_size;
  461. return 0;
  462. error:
  463. uvc_video_free_requests(video);
  464. return ret;
  465. }
  466. /* --------------------------------------------------------------------------
  467. * Video streaming
  468. */
  469. /*
  470. * uvcg_video_pump - Pump video data into the USB requests
  471. *
  472. * This function fills the available USB requests (listed in req_free) with
  473. * video data from the queued buffers.
  474. */
  475. static void uvcg_video_pump(struct work_struct *work)
  476. {
  477. struct uvc_video *video = container_of(work, struct uvc_video, pump);
  478. struct uvc_video_queue *queue = &video->queue;
  479. /* video->max_payload_size is only set when using bulk transfer */
  480. bool is_bulk = video->max_payload_size;
  481. struct usb_request *req = NULL;
  482. struct uvc_buffer *buf;
  483. unsigned long flags;
  484. int ret = 0;
  485. while (true) {
  486. if (!video->ep->enabled)
  487. return;
  488. /*
  489. * Check is_enabled and retrieve the first available USB
  490. * request, protected by the request lock.
  491. */
  492. spin_lock_irqsave(&video->req_lock, flags);
  493. if (!video->is_enabled || list_empty(&video->req_free)) {
  494. spin_unlock_irqrestore(&video->req_lock, flags);
  495. return;
  496. }
  497. req = list_first_entry(&video->req_free, struct usb_request,
  498. list);
  499. list_del(&req->list);
  500. spin_unlock_irqrestore(&video->req_lock, flags);
  501. /*
  502. * Retrieve the first available video buffer and fill the
  503. * request, protected by the video queue irqlock.
  504. */
  505. spin_lock_irqsave(&queue->irqlock, flags);
  506. buf = uvcg_queue_head(queue);
  507. if (buf != NULL) {
  508. video->encode(req, video, buf);
  509. } else {
  510. /*
  511. * Either the queue has been disconnected or no video buffer
  512. * available for bulk transfer. Either way, stop processing
  513. * further.
  514. */
  515. spin_unlock_irqrestore(&queue->irqlock, flags);
  516. break;
  517. }
  518. spin_unlock_irqrestore(&queue->irqlock, flags);
  519. spin_lock_irqsave(&video->req_lock, flags);
  520. /* For bulk end points we queue from the worker thread
  521. * since we would preferably not want to wait on requests
  522. * to be ready, in the uvcg_video_complete() handler.
  523. * For isoc endpoints we add the request to the ready list
  524. * and only queue it to the endpoint from the complete handler.
  525. */
  526. ret = uvcg_video_usb_req_queue(video, req, is_bulk);
  527. spin_unlock_irqrestore(&video->req_lock, flags);
  528. if (ret < 0) {
  529. uvcg_queue_cancel(queue, 0);
  530. break;
  531. }
  532. /* The request is owned by the endpoint / ready list. */
  533. req = NULL;
  534. }
  535. if (!req)
  536. return;
  537. spin_lock_irqsave(&video->req_lock, flags);
  538. if (video->is_enabled)
  539. list_add_tail(&req->list, &video->req_free);
  540. else
  541. uvc_video_free_request(req->context, video->ep);
  542. spin_unlock_irqrestore(&video->req_lock, flags);
  543. }
  544. /*
  545. * Disable the video stream
  546. */
  547. int
  548. uvcg_video_disable(struct uvc_video *video)
  549. {
  550. unsigned long flags;
  551. struct list_head inflight_bufs;
  552. struct usb_request *req, *temp;
  553. struct uvc_buffer *buf, *btemp;
  554. struct uvc_request *ureq, *utemp;
  555. if (video->ep == NULL) {
  556. uvcg_info(&video->uvc->func,
  557. "Video disable failed, device is uninitialized.\n");
  558. return -ENODEV;
  559. }
  560. INIT_LIST_HEAD(&inflight_bufs);
  561. spin_lock_irqsave(&video->req_lock, flags);
  562. video->is_enabled = false;
  563. /*
  564. * Remove any in-flight buffers from the uvc_requests
  565. * because we want to return them before cancelling the
  566. * queue. This ensures that we aren't stuck waiting for
  567. * all complete callbacks to come through before disabling
  568. * vb2 queue.
  569. */
  570. list_for_each_entry(ureq, &video->ureqs, list) {
  571. if (ureq->last_buf) {
  572. list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
  573. ureq->last_buf = NULL;
  574. }
  575. }
  576. spin_unlock_irqrestore(&video->req_lock, flags);
  577. cancel_work_sync(&video->pump);
  578. uvcg_queue_cancel(&video->queue, 0);
  579. spin_lock_irqsave(&video->req_lock, flags);
  580. /*
  581. * Remove all uvc_requests from ureqs with list_del_init
  582. * This lets uvc_video_free_request correctly identify
  583. * if the uvc_request is attached to a list or not when freeing
  584. * memory.
  585. */
  586. list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
  587. list_del_init(&ureq->list);
  588. list_for_each_entry_safe(req, temp, &video->req_free, list) {
  589. list_del(&req->list);
  590. uvc_video_free_request(req->context, video->ep);
  591. }
  592. list_for_each_entry_safe(req, temp, &video->req_ready, list) {
  593. list_del(&req->list);
  594. uvc_video_free_request(req->context, video->ep);
  595. }
  596. INIT_LIST_HEAD(&video->ureqs);
  597. INIT_LIST_HEAD(&video->req_free);
  598. INIT_LIST_HEAD(&video->req_ready);
  599. video->req_size = 0;
  600. spin_unlock_irqrestore(&video->req_lock, flags);
  601. /*
  602. * Return all the video buffers before disabling the queue.
  603. */
  604. spin_lock_irqsave(&video->queue.irqlock, flags);
  605. list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
  606. list_del(&buf->queue);
  607. uvcg_complete_buffer(&video->queue, buf);
  608. }
  609. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  610. uvcg_queue_enable(&video->queue, 0);
  611. return 0;
  612. }
  613. /*
  614. * Enable the video stream.
  615. */
  616. int uvcg_video_enable(struct uvc_video *video)
  617. {
  618. int ret;
  619. if (video->ep == NULL) {
  620. uvcg_info(&video->uvc->func,
  621. "Video enable failed, device is uninitialized.\n");
  622. return -ENODEV;
  623. }
  624. /*
  625. * Safe to access request related fields without req_lock because
  626. * this is the only thread currently active, and no other
  627. * request handling thread will become active until this function
  628. * returns.
  629. */
  630. video->is_enabled = true;
  631. if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  632. return ret;
  633. if ((ret = uvc_video_alloc_requests(video)) < 0)
  634. return ret;
  635. if (video->max_payload_size) {
  636. video->encode = uvc_video_encode_bulk;
  637. video->payload_size = 0;
  638. } else
  639. video->encode = video->queue.use_sg ?
  640. uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
  641. video->req_int_count = 0;
  642. uvc_video_ep_queue_initial_requests(video);
  643. return ret;
  644. }
  645. /*
  646. * Initialize the UVC video stream.
  647. */
  648. int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  649. {
  650. video->is_enabled = false;
  651. INIT_LIST_HEAD(&video->ureqs);
  652. INIT_LIST_HEAD(&video->req_free);
  653. INIT_LIST_HEAD(&video->req_ready);
  654. spin_lock_init(&video->req_lock);
  655. INIT_WORK(&video->pump, uvcg_video_pump);
  656. /* Allocate a work queue for asynchronous video pump handler. */
  657. video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
  658. if (!video->async_wq)
  659. return -EINVAL;
  660. video->uvc = uvc;
  661. video->fcc = V4L2_PIX_FMT_YUYV;
  662. video->bpp = 16;
  663. video->width = 320;
  664. video->height = 240;
  665. video->imagesize = 320 * 240 * 2;
  666. /* Initialize the video buffers queue. */
  667. uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
  668. V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);
  669. return 0;
  670. }