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