uvc_video.c 21 KB

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