videobuf2-v4l2.c 26 KB

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