videobuf2-v4l2.c 31 KB

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