videobuf2-v4l2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * videobuf2-v4l2.c - V4L2 driver helper framework
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. * Marek Szyprowski <m.szyprowski@samsung.com>
  8. *
  9. * The vb2_thread implementation was based on code from videobuf-dvb.c:
  10. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <linux/freezer.h>
  24. #include <linux/kthread.h>
  25. #include <media/v4l2-dev.h>
  26. #include <media/v4l2-fh.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/videobuf2-v4l2.h>
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. #define dprintk(level, fmt, arg...) \
  33. do { \
  34. if (debug >= level) \
  35. pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
  36. } while (0)
  37. /* Flags that are set by the vb2 core */
  38. #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  39. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  40. V4L2_BUF_FLAG_PREPARED | \
  41. V4L2_BUF_FLAG_TIMESTAMP_MASK)
  42. /* Output buffer flags that should be passed on to the driver */
  43. #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
  44. V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
  45. /*
  46. * __verify_planes_array() - verify that the planes array passed in struct
  47. * v4l2_buffer from userspace can be safely used
  48. */
  49. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  50. {
  51. if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  52. return 0;
  53. /* Is memory for copying plane information present? */
  54. if (b->m.planes == NULL) {
  55. dprintk(1, "multi-planar buffer passed but planes array not provided\n");
  56. return -EINVAL;
  57. }
  58. if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
  59. dprintk(1, "incorrect planes array length, expected %d, got %d\n",
  60. vb->num_planes, b->length);
  61. return -EINVAL;
  62. }
  63. return 0;
  64. }
  65. static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
  66. {
  67. return __verify_planes_array(vb, pb);
  68. }
  69. /*
  70. * __verify_length() - Verify that the bytesused value for each plane fits in
  71. * the plane length and that the data offset doesn't exceed the bytesused value.
  72. */
  73. static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  74. {
  75. unsigned int length;
  76. unsigned int bytesused;
  77. unsigned int plane;
  78. if (!V4L2_TYPE_IS_OUTPUT(b->type))
  79. return 0;
  80. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  81. for (plane = 0; plane < vb->num_planes; ++plane) {
  82. length = (b->memory == VB2_MEMORY_USERPTR ||
  83. b->memory == VB2_MEMORY_DMABUF)
  84. ? b->m.planes[plane].length
  85. : vb->planes[plane].length;
  86. bytesused = b->m.planes[plane].bytesused
  87. ? b->m.planes[plane].bytesused : length;
  88. if (b->m.planes[plane].bytesused > length)
  89. return -EINVAL;
  90. if (b->m.planes[plane].data_offset > 0 &&
  91. b->m.planes[plane].data_offset >= bytesused)
  92. return -EINVAL;
  93. }
  94. } else {
  95. length = (b->memory == VB2_MEMORY_USERPTR)
  96. ? b->length : vb->planes[0].length;
  97. if (b->bytesused > length)
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
  103. {
  104. const struct v4l2_buffer *b = pb;
  105. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  106. struct vb2_queue *q = vb->vb2_queue;
  107. if (q->is_output) {
  108. /*
  109. * For output buffers copy the timestamp if needed,
  110. * and the timecode field and flag if needed.
  111. */
  112. if (q->copy_timestamp)
  113. vb->timestamp = timeval_to_ns(&b->timestamp);
  114. vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
  115. if (b->flags & V4L2_BUF_FLAG_TIMECODE)
  116. vbuf->timecode = b->timecode;
  117. }
  118. };
  119. static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
  120. {
  121. static bool check_once;
  122. if (check_once)
  123. return;
  124. check_once = true;
  125. pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
  126. if (vb->vb2_queue->allow_zero_bytesused)
  127. pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
  128. else
  129. pr_warn("use the actual size instead.\n");
  130. }
  131. static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
  132. const char *opname)
  133. {
  134. if (b->type != q->type) {
  135. dprintk(1, "%s: invalid buffer type\n", opname);
  136. return -EINVAL;
  137. }
  138. if (b->index >= q->num_buffers) {
  139. dprintk(1, "%s: buffer index out of range\n", opname);
  140. return -EINVAL;
  141. }
  142. if (q->bufs[b->index] == NULL) {
  143. /* Should never happen */
  144. dprintk(1, "%s: buffer is NULL\n", opname);
  145. return -EINVAL;
  146. }
  147. if (b->memory != q->memory) {
  148. dprintk(1, "%s: invalid memory type\n", opname);
  149. return -EINVAL;
  150. }
  151. return __verify_planes_array(q->bufs[b->index], b);
  152. }
  153. /*
  154. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  155. * returned to userspace
  156. */
  157. static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
  158. {
  159. struct v4l2_buffer *b = pb;
  160. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  161. struct vb2_queue *q = vb->vb2_queue;
  162. unsigned int plane;
  163. /* Copy back data such as timestamp, flags, etc. */
  164. b->index = vb->index;
  165. b->type = vb->type;
  166. b->memory = vb->memory;
  167. b->bytesused = 0;
  168. b->flags = vbuf->flags;
  169. b->field = vbuf->field;
  170. b->timestamp = ns_to_timeval(vb->timestamp);
  171. b->timecode = vbuf->timecode;
  172. b->sequence = vbuf->sequence;
  173. b->reserved2 = 0;
  174. b->reserved = 0;
  175. if (q->is_multiplanar) {
  176. /*
  177. * Fill in plane-related data if userspace provided an array
  178. * for it. The caller has already verified memory and size.
  179. */
  180. b->length = vb->num_planes;
  181. for (plane = 0; plane < vb->num_planes; ++plane) {
  182. struct v4l2_plane *pdst = &b->m.planes[plane];
  183. struct vb2_plane *psrc = &vb->planes[plane];
  184. pdst->bytesused = psrc->bytesused;
  185. pdst->length = psrc->length;
  186. if (q->memory == VB2_MEMORY_MMAP)
  187. pdst->m.mem_offset = psrc->m.offset;
  188. else if (q->memory == VB2_MEMORY_USERPTR)
  189. pdst->m.userptr = psrc->m.userptr;
  190. else if (q->memory == VB2_MEMORY_DMABUF)
  191. pdst->m.fd = psrc->m.fd;
  192. pdst->data_offset = psrc->data_offset;
  193. memset(pdst->reserved, 0, sizeof(pdst->reserved));
  194. }
  195. } else {
  196. /*
  197. * We use length and offset in v4l2_planes array even for
  198. * single-planar buffers, but userspace does not.
  199. */
  200. b->length = vb->planes[0].length;
  201. b->bytesused = vb->planes[0].bytesused;
  202. if (q->memory == VB2_MEMORY_MMAP)
  203. b->m.offset = vb->planes[0].m.offset;
  204. else if (q->memory == VB2_MEMORY_USERPTR)
  205. b->m.userptr = vb->planes[0].m.userptr;
  206. else if (q->memory == VB2_MEMORY_DMABUF)
  207. b->m.fd = vb->planes[0].m.fd;
  208. }
  209. /*
  210. * Clear any buffer state related flags.
  211. */
  212. b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
  213. b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
  214. if (!q->copy_timestamp) {
  215. /*
  216. * For non-COPY timestamps, drop timestamp source bits
  217. * and obtain the timestamp source from the queue.
  218. */
  219. b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  220. b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  221. }
  222. switch (vb->state) {
  223. case VB2_BUF_STATE_QUEUED:
  224. case VB2_BUF_STATE_ACTIVE:
  225. b->flags |= V4L2_BUF_FLAG_QUEUED;
  226. break;
  227. case VB2_BUF_STATE_ERROR:
  228. b->flags |= V4L2_BUF_FLAG_ERROR;
  229. /* fall through */
  230. case VB2_BUF_STATE_DONE:
  231. b->flags |= V4L2_BUF_FLAG_DONE;
  232. break;
  233. case VB2_BUF_STATE_PREPARED:
  234. b->flags |= V4L2_BUF_FLAG_PREPARED;
  235. break;
  236. case VB2_BUF_STATE_PREPARING:
  237. case VB2_BUF_STATE_DEQUEUED:
  238. case VB2_BUF_STATE_REQUEUEING:
  239. /* nothing */
  240. break;
  241. }
  242. if (vb2_buffer_in_use(q, vb))
  243. b->flags |= V4L2_BUF_FLAG_MAPPED;
  244. if (!q->is_output &&
  245. b->flags & V4L2_BUF_FLAG_DONE &&
  246. b->flags & V4L2_BUF_FLAG_LAST)
  247. q->last_buffer_dequeued = true;
  248. }
  249. /*
  250. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
  251. * v4l2_buffer by the userspace. It also verifies that struct
  252. * v4l2_buffer has a valid number of planes.
  253. */
  254. static int __fill_vb2_buffer(struct vb2_buffer *vb,
  255. const void *pb, struct vb2_plane *planes)
  256. {
  257. struct vb2_queue *q = vb->vb2_queue;
  258. const struct v4l2_buffer *b = pb;
  259. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  260. unsigned int plane;
  261. int ret;
  262. ret = __verify_length(vb, b);
  263. if (ret < 0) {
  264. dprintk(1, "plane parameters verification failed: %d\n", ret);
  265. return ret;
  266. }
  267. if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
  268. /*
  269. * If the format's field is ALTERNATE, then the buffer's field
  270. * should be either TOP or BOTTOM, not ALTERNATE since that
  271. * makes no sense. The driver has to know whether the
  272. * buffer represents a top or a bottom field in order to
  273. * program any DMA correctly. Using ALTERNATE is wrong, since
  274. * that just says that it is either a top or a bottom field,
  275. * but not which of the two it is.
  276. */
  277. dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
  278. return -EINVAL;
  279. }
  280. vb->timestamp = 0;
  281. vbuf->sequence = 0;
  282. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  283. if (b->memory == VB2_MEMORY_USERPTR) {
  284. for (plane = 0; plane < vb->num_planes; ++plane) {
  285. planes[plane].m.userptr =
  286. b->m.planes[plane].m.userptr;
  287. planes[plane].length =
  288. b->m.planes[plane].length;
  289. }
  290. }
  291. if (b->memory == VB2_MEMORY_DMABUF) {
  292. for (plane = 0; plane < vb->num_planes; ++plane) {
  293. planes[plane].m.fd =
  294. b->m.planes[plane].m.fd;
  295. planes[plane].length =
  296. b->m.planes[plane].length;
  297. }
  298. }
  299. /* Fill in driver-provided information for OUTPUT types */
  300. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  301. /*
  302. * Will have to go up to b->length when API starts
  303. * accepting variable number of planes.
  304. *
  305. * If bytesused == 0 for the output buffer, then fall
  306. * back to the full buffer size. In that case
  307. * userspace clearly never bothered to set it and
  308. * it's a safe assumption that they really meant to
  309. * use the full plane sizes.
  310. *
  311. * Some drivers, e.g. old codec drivers, use bytesused == 0
  312. * as a way to indicate that streaming is finished.
  313. * In that case, the driver should use the
  314. * allow_zero_bytesused flag to keep old userspace
  315. * applications working.
  316. */
  317. for (plane = 0; plane < vb->num_planes; ++plane) {
  318. struct vb2_plane *pdst = &planes[plane];
  319. struct v4l2_plane *psrc = &b->m.planes[plane];
  320. if (psrc->bytesused == 0)
  321. vb2_warn_zero_bytesused(vb);
  322. if (vb->vb2_queue->allow_zero_bytesused)
  323. pdst->bytesused = psrc->bytesused;
  324. else
  325. pdst->bytesused = psrc->bytesused ?
  326. psrc->bytesused : pdst->length;
  327. pdst->data_offset = psrc->data_offset;
  328. }
  329. }
  330. } else {
  331. /*
  332. * Single-planar buffers do not use planes array,
  333. * so fill in relevant v4l2_buffer struct fields instead.
  334. * In videobuf we use our internal V4l2_planes struct for
  335. * single-planar buffers as well, for simplicity.
  336. *
  337. * If bytesused == 0 for the output buffer, then fall back
  338. * to the full buffer size as that's a sensible default.
  339. *
  340. * Some drivers, e.g. old codec drivers, use bytesused == 0 as
  341. * a way to indicate that streaming is finished. In that case,
  342. * the driver should use the allow_zero_bytesused flag to keep
  343. * old userspace applications working.
  344. */
  345. if (b->memory == VB2_MEMORY_USERPTR) {
  346. planes[0].m.userptr = b->m.userptr;
  347. planes[0].length = b->length;
  348. }
  349. if (b->memory == VB2_MEMORY_DMABUF) {
  350. planes[0].m.fd = b->m.fd;
  351. planes[0].length = b->length;
  352. }
  353. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  354. if (b->bytesused == 0)
  355. vb2_warn_zero_bytesused(vb);
  356. if (vb->vb2_queue->allow_zero_bytesused)
  357. planes[0].bytesused = b->bytesused;
  358. else
  359. planes[0].bytesused = b->bytesused ?
  360. b->bytesused : planes[0].length;
  361. } else
  362. planes[0].bytesused = 0;
  363. }
  364. /* Zero flags that the vb2 core handles */
  365. vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
  366. if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
  367. /*
  368. * Non-COPY timestamps and non-OUTPUT queues will get
  369. * their timestamp and timestamp source flags from the
  370. * queue.
  371. */
  372. vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  373. }
  374. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  375. /*
  376. * For output buffers mask out the timecode flag:
  377. * this will be handled later in vb2_qbuf().
  378. * The 'field' is valid metadata for this output buffer
  379. * and so that needs to be copied here.
  380. */
  381. vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
  382. vbuf->field = b->field;
  383. } else {
  384. /* Zero any output buffer flags as this is a capture buffer */
  385. vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
  386. /* Zero last flag, this is a signal from driver to userspace */
  387. vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
  388. }
  389. return 0;
  390. }
  391. static const struct vb2_buf_ops v4l2_buf_ops = {
  392. .verify_planes_array = __verify_planes_array_core,
  393. .fill_user_buffer = __fill_v4l2_buffer,
  394. .fill_vb2_buffer = __fill_vb2_buffer,
  395. .copy_timestamp = __copy_timestamp,
  396. };
  397. /*
  398. * vb2_querybuf() - query video buffer information
  399. * @q: videobuf queue
  400. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  401. * in driver
  402. *
  403. * Should be called from vidioc_querybuf ioctl handler in driver.
  404. * This function will verify the passed v4l2_buffer structure and fill the
  405. * relevant information for the userspace.
  406. *
  407. * The return values from this function are intended to be directly returned
  408. * from vidioc_querybuf handler in driver.
  409. */
  410. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  411. {
  412. struct vb2_buffer *vb;
  413. int ret;
  414. if (b->type != q->type) {
  415. dprintk(1, "wrong buffer type\n");
  416. return -EINVAL;
  417. }
  418. if (b->index >= q->num_buffers) {
  419. dprintk(1, "buffer index out of range\n");
  420. return -EINVAL;
  421. }
  422. vb = q->bufs[b->index];
  423. ret = __verify_planes_array(vb, b);
  424. if (!ret)
  425. vb2_core_querybuf(q, b->index, b);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(vb2_querybuf);
  429. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  430. {
  431. int ret = vb2_verify_memory_type(q, req->memory, req->type);
  432. return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
  433. }
  434. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  435. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  436. {
  437. int ret;
  438. if (vb2_fileio_is_active(q)) {
  439. dprintk(1, "file io in progress\n");
  440. return -EBUSY;
  441. }
  442. ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
  443. return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
  444. }
  445. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  446. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  447. {
  448. unsigned requested_planes = 1;
  449. unsigned requested_sizes[VIDEO_MAX_PLANES];
  450. struct v4l2_format *f = &create->format;
  451. int ret = vb2_verify_memory_type(q, create->memory, f->type);
  452. unsigned i;
  453. create->index = q->num_buffers;
  454. if (create->count == 0)
  455. return ret != -EBUSY ? ret : 0;
  456. switch (f->type) {
  457. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  458. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  459. requested_planes = f->fmt.pix_mp.num_planes;
  460. if (requested_planes == 0 ||
  461. requested_planes > VIDEO_MAX_PLANES)
  462. return -EINVAL;
  463. for (i = 0; i < requested_planes; i++)
  464. requested_sizes[i] =
  465. f->fmt.pix_mp.plane_fmt[i].sizeimage;
  466. break;
  467. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  468. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  469. requested_sizes[0] = f->fmt.pix.sizeimage;
  470. break;
  471. case V4L2_BUF_TYPE_VBI_CAPTURE:
  472. case V4L2_BUF_TYPE_VBI_OUTPUT:
  473. requested_sizes[0] = f->fmt.vbi.samples_per_line *
  474. (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
  475. break;
  476. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  477. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  478. requested_sizes[0] = f->fmt.sliced.io_size;
  479. break;
  480. case V4L2_BUF_TYPE_SDR_CAPTURE:
  481. case V4L2_BUF_TYPE_SDR_OUTPUT:
  482. requested_sizes[0] = f->fmt.sdr.buffersize;
  483. break;
  484. case V4L2_BUF_TYPE_META_CAPTURE:
  485. requested_sizes[0] = f->fmt.meta.buffersize;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. for (i = 0; i < requested_planes; i++)
  491. if (requested_sizes[i] == 0)
  492. return -EINVAL;
  493. return ret ? ret : vb2_core_create_bufs(q, create->memory,
  494. &create->count, requested_planes, requested_sizes);
  495. }
  496. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  497. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  498. {
  499. int ret;
  500. if (vb2_fileio_is_active(q)) {
  501. dprintk(1, "file io in progress\n");
  502. return -EBUSY;
  503. }
  504. ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
  505. return ret ? ret : vb2_core_qbuf(q, b->index, b);
  506. }
  507. EXPORT_SYMBOL_GPL(vb2_qbuf);
  508. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  509. {
  510. int ret;
  511. if (vb2_fileio_is_active(q)) {
  512. dprintk(1, "file io in progress\n");
  513. return -EBUSY;
  514. }
  515. if (b->type != q->type) {
  516. dprintk(1, "invalid buffer type\n");
  517. return -EINVAL;
  518. }
  519. ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
  520. /*
  521. * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
  522. * cleared.
  523. */
  524. b->flags &= ~V4L2_BUF_FLAG_DONE;
  525. return ret;
  526. }
  527. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  528. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  529. {
  530. if (vb2_fileio_is_active(q)) {
  531. dprintk(1, "file io in progress\n");
  532. return -EBUSY;
  533. }
  534. return vb2_core_streamon(q, type);
  535. }
  536. EXPORT_SYMBOL_GPL(vb2_streamon);
  537. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  538. {
  539. if (vb2_fileio_is_active(q)) {
  540. dprintk(1, "file io in progress\n");
  541. return -EBUSY;
  542. }
  543. return vb2_core_streamoff(q, type);
  544. }
  545. EXPORT_SYMBOL_GPL(vb2_streamoff);
  546. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  547. {
  548. return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
  549. eb->plane, eb->flags);
  550. }
  551. EXPORT_SYMBOL_GPL(vb2_expbuf);
  552. int vb2_queue_init(struct vb2_queue *q)
  553. {
  554. /*
  555. * Sanity check
  556. */
  557. if (WARN_ON(!q) ||
  558. WARN_ON(q->timestamp_flags &
  559. ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
  560. V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
  561. return -EINVAL;
  562. /* Warn that the driver should choose an appropriate timestamp type */
  563. WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  564. V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  565. /* Warn that vb2_memory should match with v4l2_memory */
  566. if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
  567. || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
  568. || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
  569. return -EINVAL;
  570. if (q->buf_struct_size == 0)
  571. q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
  572. q->buf_ops = &v4l2_buf_ops;
  573. q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  574. q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
  575. q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
  576. == V4L2_BUF_FLAG_TIMESTAMP_COPY;
  577. /*
  578. * For compatibility with vb1: if QBUF hasn't been called yet, then
  579. * return EPOLLERR as well. This only affects capture queues, output
  580. * queues will always initialize waiting_for_buffers to false.
  581. */
  582. q->quirk_poll_must_check_waiting_for_buffers = true;
  583. return vb2_core_queue_init(q);
  584. }
  585. EXPORT_SYMBOL_GPL(vb2_queue_init);
  586. void vb2_queue_release(struct vb2_queue *q)
  587. {
  588. vb2_core_queue_release(q);
  589. }
  590. EXPORT_SYMBOL_GPL(vb2_queue_release);
  591. __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  592. {
  593. struct video_device *vfd = video_devdata(file);
  594. __poll_t req_events = poll_requested_events(wait);
  595. __poll_t res = 0;
  596. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  597. struct v4l2_fh *fh = file->private_data;
  598. if (v4l2_event_pending(fh))
  599. res = EPOLLPRI;
  600. else if (req_events & EPOLLPRI)
  601. poll_wait(file, &fh->wait, wait);
  602. }
  603. return res | vb2_core_poll(q, file, wait);
  604. }
  605. EXPORT_SYMBOL_GPL(vb2_poll);
  606. /*
  607. * The following functions are not part of the vb2 core API, but are helper
  608. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  609. * and struct vb2_ops.
  610. * They contain boilerplate code that most if not all drivers have to do
  611. * and so they simplify the driver code.
  612. */
  613. /* The queue is busy if there is a owner and you are not that owner. */
  614. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  615. {
  616. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  617. }
  618. /* vb2 ioctl helpers */
  619. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  620. struct v4l2_requestbuffers *p)
  621. {
  622. struct video_device *vdev = video_devdata(file);
  623. int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
  624. if (res)
  625. return res;
  626. if (vb2_queue_is_busy(vdev, file))
  627. return -EBUSY;
  628. res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
  629. /* If count == 0, then the owner has released all buffers and he
  630. is no longer owner of the queue. Otherwise we have a new owner. */
  631. if (res == 0)
  632. vdev->queue->owner = p->count ? file->private_data : NULL;
  633. return res;
  634. }
  635. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  636. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  637. struct v4l2_create_buffers *p)
  638. {
  639. struct video_device *vdev = video_devdata(file);
  640. int res = vb2_verify_memory_type(vdev->queue, p->memory,
  641. p->format.type);
  642. p->index = vdev->queue->num_buffers;
  643. /*
  644. * If count == 0, then just check if memory and type are valid.
  645. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
  646. */
  647. if (p->count == 0)
  648. return res != -EBUSY ? res : 0;
  649. if (res)
  650. return res;
  651. if (vb2_queue_is_busy(vdev, file))
  652. return -EBUSY;
  653. res = vb2_create_bufs(vdev->queue, p);
  654. if (res == 0)
  655. vdev->queue->owner = file->private_data;
  656. return res;
  657. }
  658. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  659. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  660. struct v4l2_buffer *p)
  661. {
  662. struct video_device *vdev = video_devdata(file);
  663. if (vb2_queue_is_busy(vdev, file))
  664. return -EBUSY;
  665. return vb2_prepare_buf(vdev->queue, p);
  666. }
  667. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  668. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  669. {
  670. struct video_device *vdev = video_devdata(file);
  671. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  672. return vb2_querybuf(vdev->queue, p);
  673. }
  674. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  675. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  676. {
  677. struct video_device *vdev = video_devdata(file);
  678. if (vb2_queue_is_busy(vdev, file))
  679. return -EBUSY;
  680. return vb2_qbuf(vdev->queue, p);
  681. }
  682. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  683. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  684. {
  685. struct video_device *vdev = video_devdata(file);
  686. if (vb2_queue_is_busy(vdev, file))
  687. return -EBUSY;
  688. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  689. }
  690. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  691. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  692. {
  693. struct video_device *vdev = video_devdata(file);
  694. if (vb2_queue_is_busy(vdev, file))
  695. return -EBUSY;
  696. return vb2_streamon(vdev->queue, i);
  697. }
  698. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  699. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  700. {
  701. struct video_device *vdev = video_devdata(file);
  702. if (vb2_queue_is_busy(vdev, file))
  703. return -EBUSY;
  704. return vb2_streamoff(vdev->queue, i);
  705. }
  706. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  707. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  708. {
  709. struct video_device *vdev = video_devdata(file);
  710. if (vb2_queue_is_busy(vdev, file))
  711. return -EBUSY;
  712. return vb2_expbuf(vdev->queue, p);
  713. }
  714. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  715. /* v4l2_file_operations helpers */
  716. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  717. {
  718. struct video_device *vdev = video_devdata(file);
  719. return vb2_mmap(vdev->queue, vma);
  720. }
  721. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  722. int _vb2_fop_release(struct file *file, struct mutex *lock)
  723. {
  724. struct video_device *vdev = video_devdata(file);
  725. if (lock)
  726. mutex_lock(lock);
  727. if (file->private_data == vdev->queue->owner) {
  728. vb2_queue_release(vdev->queue);
  729. vdev->queue->owner = NULL;
  730. }
  731. if (lock)
  732. mutex_unlock(lock);
  733. return v4l2_fh_release(file);
  734. }
  735. EXPORT_SYMBOL_GPL(_vb2_fop_release);
  736. int vb2_fop_release(struct file *file)
  737. {
  738. struct video_device *vdev = video_devdata(file);
  739. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  740. return _vb2_fop_release(file, lock);
  741. }
  742. EXPORT_SYMBOL_GPL(vb2_fop_release);
  743. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  744. size_t count, loff_t *ppos)
  745. {
  746. struct video_device *vdev = video_devdata(file);
  747. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  748. int err = -EBUSY;
  749. if (!(vdev->queue->io_modes & VB2_WRITE))
  750. return -EINVAL;
  751. if (lock && mutex_lock_interruptible(lock))
  752. return -ERESTARTSYS;
  753. if (vb2_queue_is_busy(vdev, file))
  754. goto exit;
  755. err = vb2_write(vdev->queue, buf, count, ppos,
  756. file->f_flags & O_NONBLOCK);
  757. if (vdev->queue->fileio)
  758. vdev->queue->owner = file->private_data;
  759. exit:
  760. if (lock)
  761. mutex_unlock(lock);
  762. return err;
  763. }
  764. EXPORT_SYMBOL_GPL(vb2_fop_write);
  765. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  766. size_t count, loff_t *ppos)
  767. {
  768. struct video_device *vdev = video_devdata(file);
  769. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  770. int err = -EBUSY;
  771. if (!(vdev->queue->io_modes & VB2_READ))
  772. return -EINVAL;
  773. if (lock && mutex_lock_interruptible(lock))
  774. return -ERESTARTSYS;
  775. if (vb2_queue_is_busy(vdev, file))
  776. goto exit;
  777. err = vb2_read(vdev->queue, buf, count, ppos,
  778. file->f_flags & O_NONBLOCK);
  779. if (vdev->queue->fileio)
  780. vdev->queue->owner = file->private_data;
  781. exit:
  782. if (lock)
  783. mutex_unlock(lock);
  784. return err;
  785. }
  786. EXPORT_SYMBOL_GPL(vb2_fop_read);
  787. __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
  788. {
  789. struct video_device *vdev = video_devdata(file);
  790. struct vb2_queue *q = vdev->queue;
  791. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  792. __poll_t res;
  793. void *fileio;
  794. /*
  795. * If this helper doesn't know how to lock, then you shouldn't be using
  796. * it but you should write your own.
  797. */
  798. WARN_ON(!lock);
  799. if (lock && mutex_lock_interruptible(lock))
  800. return EPOLLERR;
  801. fileio = q->fileio;
  802. res = vb2_poll(vdev->queue, file, wait);
  803. /* If fileio was started, then we have a new queue owner. */
  804. if (!fileio && q->fileio)
  805. q->owner = file->private_data;
  806. if (lock)
  807. mutex_unlock(lock);
  808. return res;
  809. }
  810. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  811. #ifndef CONFIG_MMU
  812. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  813. unsigned long len, unsigned long pgoff, unsigned long flags)
  814. {
  815. struct video_device *vdev = video_devdata(file);
  816. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  817. }
  818. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  819. #endif
  820. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  821. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  822. {
  823. mutex_unlock(vq->lock);
  824. }
  825. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  826. void vb2_ops_wait_finish(struct vb2_queue *vq)
  827. {
  828. mutex_lock(vq->lock);
  829. }
  830. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  831. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  832. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  833. MODULE_LICENSE("GPL");