videobuf2-v4l2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. default:
  484. return -EINVAL;
  485. }
  486. for (i = 0; i < requested_planes; i++)
  487. if (requested_sizes[i] == 0)
  488. return -EINVAL;
  489. return ret ? ret : vb2_core_create_bufs(q, create->memory,
  490. &create->count, requested_planes, requested_sizes);
  491. }
  492. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  493. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  494. {
  495. int ret;
  496. if (vb2_fileio_is_active(q)) {
  497. dprintk(1, "file io in progress\n");
  498. return -EBUSY;
  499. }
  500. ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
  501. return ret ? ret : vb2_core_qbuf(q, b->index, b);
  502. }
  503. EXPORT_SYMBOL_GPL(vb2_qbuf);
  504. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  505. {
  506. int ret;
  507. if (vb2_fileio_is_active(q)) {
  508. dprintk(1, "file io in progress\n");
  509. return -EBUSY;
  510. }
  511. if (b->type != q->type) {
  512. dprintk(1, "invalid buffer type\n");
  513. return -EINVAL;
  514. }
  515. ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
  516. /*
  517. * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
  518. * cleared.
  519. */
  520. b->flags &= ~V4L2_BUF_FLAG_DONE;
  521. return ret;
  522. }
  523. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  524. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  525. {
  526. if (vb2_fileio_is_active(q)) {
  527. dprintk(1, "file io in progress\n");
  528. return -EBUSY;
  529. }
  530. return vb2_core_streamon(q, type);
  531. }
  532. EXPORT_SYMBOL_GPL(vb2_streamon);
  533. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  534. {
  535. if (vb2_fileio_is_active(q)) {
  536. dprintk(1, "file io in progress\n");
  537. return -EBUSY;
  538. }
  539. return vb2_core_streamoff(q, type);
  540. }
  541. EXPORT_SYMBOL_GPL(vb2_streamoff);
  542. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  543. {
  544. return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
  545. eb->plane, eb->flags);
  546. }
  547. EXPORT_SYMBOL_GPL(vb2_expbuf);
  548. int vb2_queue_init(struct vb2_queue *q)
  549. {
  550. /*
  551. * Sanity check
  552. */
  553. if (WARN_ON(!q) ||
  554. WARN_ON(q->timestamp_flags &
  555. ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
  556. V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
  557. return -EINVAL;
  558. /* Warn that the driver should choose an appropriate timestamp type */
  559. WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  560. V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  561. /* Warn that vb2_memory should match with v4l2_memory */
  562. if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
  563. || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
  564. || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
  565. return -EINVAL;
  566. if (q->buf_struct_size == 0)
  567. q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
  568. q->buf_ops = &v4l2_buf_ops;
  569. q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  570. q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
  571. q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
  572. == V4L2_BUF_FLAG_TIMESTAMP_COPY;
  573. /*
  574. * For compatibility with vb1: if QBUF hasn't been called yet, then
  575. * return POLLERR as well. This only affects capture queues, output
  576. * queues will always initialize waiting_for_buffers to false.
  577. */
  578. q->quirk_poll_must_check_waiting_for_buffers = true;
  579. return vb2_core_queue_init(q);
  580. }
  581. EXPORT_SYMBOL_GPL(vb2_queue_init);
  582. void vb2_queue_release(struct vb2_queue *q)
  583. {
  584. vb2_core_queue_release(q);
  585. }
  586. EXPORT_SYMBOL_GPL(vb2_queue_release);
  587. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  588. {
  589. struct video_device *vfd = video_devdata(file);
  590. unsigned long req_events = poll_requested_events(wait);
  591. unsigned int res = 0;
  592. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  593. struct v4l2_fh *fh = file->private_data;
  594. if (v4l2_event_pending(fh))
  595. res = POLLPRI;
  596. else if (req_events & POLLPRI)
  597. poll_wait(file, &fh->wait, wait);
  598. }
  599. return res | vb2_core_poll(q, file, wait);
  600. }
  601. EXPORT_SYMBOL_GPL(vb2_poll);
  602. /*
  603. * The following functions are not part of the vb2 core API, but are helper
  604. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  605. * and struct vb2_ops.
  606. * They contain boilerplate code that most if not all drivers have to do
  607. * and so they simplify the driver code.
  608. */
  609. /* The queue is busy if there is a owner and you are not that owner. */
  610. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  611. {
  612. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  613. }
  614. /* vb2 ioctl helpers */
  615. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  616. struct v4l2_requestbuffers *p)
  617. {
  618. struct video_device *vdev = video_devdata(file);
  619. int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
  620. if (res)
  621. return res;
  622. if (vb2_queue_is_busy(vdev, file))
  623. return -EBUSY;
  624. res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
  625. /* If count == 0, then the owner has released all buffers and he
  626. is no longer owner of the queue. Otherwise we have a new owner. */
  627. if (res == 0)
  628. vdev->queue->owner = p->count ? file->private_data : NULL;
  629. return res;
  630. }
  631. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  632. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  633. struct v4l2_create_buffers *p)
  634. {
  635. struct video_device *vdev = video_devdata(file);
  636. int res = vb2_verify_memory_type(vdev->queue, p->memory,
  637. p->format.type);
  638. p->index = vdev->queue->num_buffers;
  639. /*
  640. * If count == 0, then just check if memory and type are valid.
  641. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
  642. */
  643. if (p->count == 0)
  644. return res != -EBUSY ? res : 0;
  645. if (res)
  646. return res;
  647. if (vb2_queue_is_busy(vdev, file))
  648. return -EBUSY;
  649. res = vb2_create_bufs(vdev->queue, p);
  650. if (res == 0)
  651. vdev->queue->owner = file->private_data;
  652. return res;
  653. }
  654. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  655. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  656. struct v4l2_buffer *p)
  657. {
  658. struct video_device *vdev = video_devdata(file);
  659. if (vb2_queue_is_busy(vdev, file))
  660. return -EBUSY;
  661. return vb2_prepare_buf(vdev->queue, p);
  662. }
  663. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  664. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  665. {
  666. struct video_device *vdev = video_devdata(file);
  667. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  668. return vb2_querybuf(vdev->queue, p);
  669. }
  670. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  671. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  672. {
  673. struct video_device *vdev = video_devdata(file);
  674. if (vb2_queue_is_busy(vdev, file))
  675. return -EBUSY;
  676. return vb2_qbuf(vdev->queue, p);
  677. }
  678. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  679. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  680. {
  681. struct video_device *vdev = video_devdata(file);
  682. if (vb2_queue_is_busy(vdev, file))
  683. return -EBUSY;
  684. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  685. }
  686. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  687. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  688. {
  689. struct video_device *vdev = video_devdata(file);
  690. if (vb2_queue_is_busy(vdev, file))
  691. return -EBUSY;
  692. return vb2_streamon(vdev->queue, i);
  693. }
  694. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  695. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  696. {
  697. struct video_device *vdev = video_devdata(file);
  698. if (vb2_queue_is_busy(vdev, file))
  699. return -EBUSY;
  700. return vb2_streamoff(vdev->queue, i);
  701. }
  702. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  703. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  704. {
  705. struct video_device *vdev = video_devdata(file);
  706. if (vb2_queue_is_busy(vdev, file))
  707. return -EBUSY;
  708. return vb2_expbuf(vdev->queue, p);
  709. }
  710. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  711. /* v4l2_file_operations helpers */
  712. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  713. {
  714. struct video_device *vdev = video_devdata(file);
  715. return vb2_mmap(vdev->queue, vma);
  716. }
  717. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  718. int _vb2_fop_release(struct file *file, struct mutex *lock)
  719. {
  720. struct video_device *vdev = video_devdata(file);
  721. if (lock)
  722. mutex_lock(lock);
  723. if (file->private_data == vdev->queue->owner) {
  724. vb2_queue_release(vdev->queue);
  725. vdev->queue->owner = NULL;
  726. }
  727. if (lock)
  728. mutex_unlock(lock);
  729. return v4l2_fh_release(file);
  730. }
  731. EXPORT_SYMBOL_GPL(_vb2_fop_release);
  732. int vb2_fop_release(struct file *file)
  733. {
  734. struct video_device *vdev = video_devdata(file);
  735. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  736. return _vb2_fop_release(file, lock);
  737. }
  738. EXPORT_SYMBOL_GPL(vb2_fop_release);
  739. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  740. size_t count, loff_t *ppos)
  741. {
  742. struct video_device *vdev = video_devdata(file);
  743. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  744. int err = -EBUSY;
  745. if (!(vdev->queue->io_modes & VB2_WRITE))
  746. return -EINVAL;
  747. if (lock && mutex_lock_interruptible(lock))
  748. return -ERESTARTSYS;
  749. if (vb2_queue_is_busy(vdev, file))
  750. goto exit;
  751. err = vb2_write(vdev->queue, buf, count, ppos,
  752. file->f_flags & O_NONBLOCK);
  753. if (vdev->queue->fileio)
  754. vdev->queue->owner = file->private_data;
  755. exit:
  756. if (lock)
  757. mutex_unlock(lock);
  758. return err;
  759. }
  760. EXPORT_SYMBOL_GPL(vb2_fop_write);
  761. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  762. size_t count, loff_t *ppos)
  763. {
  764. struct video_device *vdev = video_devdata(file);
  765. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  766. int err = -EBUSY;
  767. if (!(vdev->queue->io_modes & VB2_READ))
  768. return -EINVAL;
  769. if (lock && mutex_lock_interruptible(lock))
  770. return -ERESTARTSYS;
  771. if (vb2_queue_is_busy(vdev, file))
  772. goto exit;
  773. err = vb2_read(vdev->queue, buf, count, ppos,
  774. file->f_flags & O_NONBLOCK);
  775. if (vdev->queue->fileio)
  776. vdev->queue->owner = file->private_data;
  777. exit:
  778. if (lock)
  779. mutex_unlock(lock);
  780. return err;
  781. }
  782. EXPORT_SYMBOL_GPL(vb2_fop_read);
  783. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  784. {
  785. struct video_device *vdev = video_devdata(file);
  786. struct vb2_queue *q = vdev->queue;
  787. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  788. unsigned res;
  789. void *fileio;
  790. /*
  791. * If this helper doesn't know how to lock, then you shouldn't be using
  792. * it but you should write your own.
  793. */
  794. WARN_ON(!lock);
  795. if (lock && mutex_lock_interruptible(lock))
  796. return POLLERR;
  797. fileio = q->fileio;
  798. res = vb2_poll(vdev->queue, file, wait);
  799. /* If fileio was started, then we have a new queue owner. */
  800. if (!fileio && q->fileio)
  801. q->owner = file->private_data;
  802. if (lock)
  803. mutex_unlock(lock);
  804. return res;
  805. }
  806. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  807. #ifndef CONFIG_MMU
  808. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  809. unsigned long len, unsigned long pgoff, unsigned long flags)
  810. {
  811. struct video_device *vdev = video_devdata(file);
  812. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  813. }
  814. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  815. #endif
  816. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  817. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  818. {
  819. mutex_unlock(vq->lock);
  820. }
  821. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  822. void vb2_ops_wait_finish(struct vb2_queue *vq)
  823. {
  824. mutex_lock(vq->lock);
  825. }
  826. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  827. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  828. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  829. MODULE_LICENSE("GPL");