videobuf2-v4l2.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. /**
  431. * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
  432. * the memory and type values.
  433. * @q: videobuf2 queue
  434. * @req: struct passed from userspace to vidioc_reqbufs handler
  435. * in driver
  436. */
  437. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  438. {
  439. int ret = vb2_verify_memory_type(q, req->memory, req->type);
  440. return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
  441. }
  442. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  443. /**
  444. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  445. * @q: videobuf2 queue
  446. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  447. * handler in driver
  448. *
  449. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  450. * This function:
  451. * 1) verifies the passed buffer,
  452. * 2) calls buf_prepare callback in the driver (if provided), in which
  453. * driver-specific buffer initialization can be performed,
  454. *
  455. * The return values from this function are intended to be directly returned
  456. * from vidioc_prepare_buf handler in driver.
  457. */
  458. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  459. {
  460. int ret;
  461. if (vb2_fileio_is_active(q)) {
  462. dprintk(1, "file io in progress\n");
  463. return -EBUSY;
  464. }
  465. ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
  466. return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
  467. }
  468. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  469. /**
  470. * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
  471. * the memory and type values.
  472. * @q: videobuf2 queue
  473. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  474. * handler in driver
  475. */
  476. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  477. {
  478. unsigned requested_planes = 1;
  479. unsigned requested_sizes[VIDEO_MAX_PLANES];
  480. struct v4l2_format *f = &create->format;
  481. int ret = vb2_verify_memory_type(q, create->memory, f->type);
  482. unsigned i;
  483. create->index = q->num_buffers;
  484. if (create->count == 0)
  485. return ret != -EBUSY ? ret : 0;
  486. switch (f->type) {
  487. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  488. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  489. requested_planes = f->fmt.pix_mp.num_planes;
  490. if (requested_planes == 0 ||
  491. requested_planes > VIDEO_MAX_PLANES)
  492. return -EINVAL;
  493. for (i = 0; i < requested_planes; i++)
  494. requested_sizes[i] =
  495. f->fmt.pix_mp.plane_fmt[i].sizeimage;
  496. break;
  497. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  498. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  499. requested_sizes[0] = f->fmt.pix.sizeimage;
  500. break;
  501. case V4L2_BUF_TYPE_VBI_CAPTURE:
  502. case V4L2_BUF_TYPE_VBI_OUTPUT:
  503. requested_sizes[0] = f->fmt.vbi.samples_per_line *
  504. (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
  505. break;
  506. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  507. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  508. requested_sizes[0] = f->fmt.sliced.io_size;
  509. break;
  510. case V4L2_BUF_TYPE_SDR_CAPTURE:
  511. case V4L2_BUF_TYPE_SDR_OUTPUT:
  512. requested_sizes[0] = f->fmt.sdr.buffersize;
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. for (i = 0; i < requested_planes; i++)
  518. if (requested_sizes[i] == 0)
  519. return -EINVAL;
  520. return ret ? ret : vb2_core_create_bufs(q, create->memory,
  521. &create->count, requested_planes, requested_sizes);
  522. }
  523. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  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. int ret;
  544. if (vb2_fileio_is_active(q)) {
  545. dprintk(1, "file io in progress\n");
  546. return -EBUSY;
  547. }
  548. ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
  549. return ret ? ret : vb2_core_qbuf(q, b->index, b);
  550. }
  551. EXPORT_SYMBOL_GPL(vb2_qbuf);
  552. /**
  553. * vb2_dqbuf() - Dequeue a buffer to the userspace
  554. * @q: videobuf2 queue
  555. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  556. * in driver
  557. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  558. * buffers ready for dequeuing are present. Normally the driver
  559. * would be passing (file->f_flags & O_NONBLOCK) here
  560. *
  561. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  562. * This function:
  563. * 1) verifies the passed buffer,
  564. * 2) calls buf_finish callback in the driver (if provided), in which
  565. * driver can perform any additional operations that may be required before
  566. * returning the buffer to userspace, such as cache sync,
  567. * 3) the buffer struct members are filled with relevant information for
  568. * the userspace.
  569. *
  570. * The return values from this function are intended to be directly returned
  571. * from vidioc_dqbuf handler in driver.
  572. */
  573. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  574. {
  575. int ret;
  576. if (vb2_fileio_is_active(q)) {
  577. dprintk(1, "file io in progress\n");
  578. return -EBUSY;
  579. }
  580. if (b->type != q->type) {
  581. dprintk(1, "invalid buffer type\n");
  582. return -EINVAL;
  583. }
  584. ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
  585. /*
  586. * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
  587. * cleared.
  588. */
  589. b->flags &= ~V4L2_BUF_FLAG_DONE;
  590. return ret;
  591. }
  592. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  593. /**
  594. * vb2_streamon - start streaming
  595. * @q: videobuf2 queue
  596. * @type: type argument passed from userspace to vidioc_streamon handler
  597. *
  598. * Should be called from vidioc_streamon handler of a driver.
  599. * This function:
  600. * 1) verifies current state
  601. * 2) passes any previously queued buffers to the driver and starts streaming
  602. *
  603. * The return values from this function are intended to be directly returned
  604. * from vidioc_streamon handler in the driver.
  605. */
  606. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  607. {
  608. if (vb2_fileio_is_active(q)) {
  609. dprintk(1, "file io in progress\n");
  610. return -EBUSY;
  611. }
  612. return vb2_core_streamon(q, type);
  613. }
  614. EXPORT_SYMBOL_GPL(vb2_streamon);
  615. /**
  616. * vb2_streamoff - stop streaming
  617. * @q: videobuf2 queue
  618. * @type: type argument passed from userspace to vidioc_streamoff handler
  619. *
  620. * Should be called from vidioc_streamoff handler of a driver.
  621. * This function:
  622. * 1) verifies current state,
  623. * 2) stop streaming and dequeues any queued buffers, including those previously
  624. * passed to the driver (after waiting for the driver to finish).
  625. *
  626. * This call can be used for pausing playback.
  627. * The return values from this function are intended to be directly returned
  628. * from vidioc_streamoff handler in the driver
  629. */
  630. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  631. {
  632. if (vb2_fileio_is_active(q)) {
  633. dprintk(1, "file io in progress\n");
  634. return -EBUSY;
  635. }
  636. return vb2_core_streamoff(q, type);
  637. }
  638. EXPORT_SYMBOL_GPL(vb2_streamoff);
  639. /**
  640. * vb2_expbuf() - Export a buffer as a file descriptor
  641. * @q: videobuf2 queue
  642. * @eb: export buffer structure passed from userspace to vidioc_expbuf
  643. * handler in driver
  644. *
  645. * The return values from this function are intended to be directly returned
  646. * from vidioc_expbuf handler in driver.
  647. */
  648. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  649. {
  650. return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
  651. eb->plane, eb->flags);
  652. }
  653. EXPORT_SYMBOL_GPL(vb2_expbuf);
  654. /**
  655. * vb2_queue_init() - initialize a videobuf2 queue
  656. * @q: videobuf2 queue; this structure should be allocated in driver
  657. *
  658. * The vb2_queue structure should be allocated by the driver. The driver is
  659. * responsible of clearing it's content and setting initial values for some
  660. * required entries before calling this function.
  661. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  662. * to the struct vb2_queue description in include/media/videobuf2-core.h
  663. * for more information.
  664. */
  665. int vb2_queue_init(struct vb2_queue *q)
  666. {
  667. /*
  668. * Sanity check
  669. */
  670. if (WARN_ON(!q) ||
  671. WARN_ON(q->timestamp_flags &
  672. ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
  673. V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
  674. return -EINVAL;
  675. /* Warn that the driver should choose an appropriate timestamp type */
  676. WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  677. V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  678. /* Warn that vb2_memory should match with v4l2_memory */
  679. if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
  680. || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
  681. || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
  682. return -EINVAL;
  683. if (q->buf_struct_size == 0)
  684. q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
  685. q->buf_ops = &v4l2_buf_ops;
  686. q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  687. q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
  688. q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
  689. == V4L2_BUF_FLAG_TIMESTAMP_COPY;
  690. /*
  691. * For compatibility with vb1: if QBUF hasn't been called yet, then
  692. * return POLLERR as well. This only affects capture queues, output
  693. * queues will always initialize waiting_for_buffers to false.
  694. */
  695. q->quirk_poll_must_check_waiting_for_buffers = true;
  696. return vb2_core_queue_init(q);
  697. }
  698. EXPORT_SYMBOL_GPL(vb2_queue_init);
  699. /**
  700. * vb2_queue_release() - stop streaming, release the queue and free memory
  701. * @q: videobuf2 queue
  702. *
  703. * This function stops streaming and performs necessary clean ups, including
  704. * freeing video buffer memory. The driver is responsible for freeing
  705. * the vb2_queue structure itself.
  706. */
  707. void vb2_queue_release(struct vb2_queue *q)
  708. {
  709. vb2_core_queue_release(q);
  710. }
  711. EXPORT_SYMBOL_GPL(vb2_queue_release);
  712. /**
  713. * vb2_poll() - implements poll userspace operation
  714. * @q: videobuf2 queue
  715. * @file: file argument passed to the poll file operation handler
  716. * @wait: wait argument passed to the poll file operation handler
  717. *
  718. * This function implements poll file operation handler for a driver.
  719. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  720. * be informed that the file descriptor of a video device is available for
  721. * reading.
  722. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  723. * will be reported as available for writing.
  724. *
  725. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  726. * pending events.
  727. *
  728. * The return values from this function are intended to be directly returned
  729. * from poll handler in driver.
  730. */
  731. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  732. {
  733. struct video_device *vfd = video_devdata(file);
  734. unsigned long req_events = poll_requested_events(wait);
  735. unsigned int res = 0;
  736. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  737. struct v4l2_fh *fh = file->private_data;
  738. if (v4l2_event_pending(fh))
  739. res = POLLPRI;
  740. else if (req_events & POLLPRI)
  741. poll_wait(file, &fh->wait, wait);
  742. }
  743. return res | vb2_core_poll(q, file, wait);
  744. }
  745. EXPORT_SYMBOL_GPL(vb2_poll);
  746. /*
  747. * The following functions are not part of the vb2 core API, but are helper
  748. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  749. * and struct vb2_ops.
  750. * They contain boilerplate code that most if not all drivers have to do
  751. * and so they simplify the driver code.
  752. */
  753. /* The queue is busy if there is a owner and you are not that owner. */
  754. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  755. {
  756. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  757. }
  758. /* vb2 ioctl helpers */
  759. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  760. struct v4l2_requestbuffers *p)
  761. {
  762. struct video_device *vdev = video_devdata(file);
  763. int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
  764. if (res)
  765. return res;
  766. if (vb2_queue_is_busy(vdev, file))
  767. return -EBUSY;
  768. res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
  769. /* If count == 0, then the owner has released all buffers and he
  770. is no longer owner of the queue. Otherwise we have a new owner. */
  771. if (res == 0)
  772. vdev->queue->owner = p->count ? file->private_data : NULL;
  773. return res;
  774. }
  775. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  776. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  777. struct v4l2_create_buffers *p)
  778. {
  779. struct video_device *vdev = video_devdata(file);
  780. int res = vb2_verify_memory_type(vdev->queue, p->memory,
  781. p->format.type);
  782. p->index = vdev->queue->num_buffers;
  783. /*
  784. * If count == 0, then just check if memory and type are valid.
  785. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
  786. */
  787. if (p->count == 0)
  788. return res != -EBUSY ? res : 0;
  789. if (res)
  790. return res;
  791. if (vb2_queue_is_busy(vdev, file))
  792. return -EBUSY;
  793. res = vb2_create_bufs(vdev->queue, p);
  794. if (res == 0)
  795. vdev->queue->owner = file->private_data;
  796. return res;
  797. }
  798. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  799. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  800. struct v4l2_buffer *p)
  801. {
  802. struct video_device *vdev = video_devdata(file);
  803. if (vb2_queue_is_busy(vdev, file))
  804. return -EBUSY;
  805. return vb2_prepare_buf(vdev->queue, p);
  806. }
  807. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  808. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  809. {
  810. struct video_device *vdev = video_devdata(file);
  811. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  812. return vb2_querybuf(vdev->queue, p);
  813. }
  814. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  815. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  816. {
  817. struct video_device *vdev = video_devdata(file);
  818. if (vb2_queue_is_busy(vdev, file))
  819. return -EBUSY;
  820. return vb2_qbuf(vdev->queue, p);
  821. }
  822. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  823. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  824. {
  825. struct video_device *vdev = video_devdata(file);
  826. if (vb2_queue_is_busy(vdev, file))
  827. return -EBUSY;
  828. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  829. }
  830. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  831. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  832. {
  833. struct video_device *vdev = video_devdata(file);
  834. if (vb2_queue_is_busy(vdev, file))
  835. return -EBUSY;
  836. return vb2_streamon(vdev->queue, i);
  837. }
  838. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  839. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  840. {
  841. struct video_device *vdev = video_devdata(file);
  842. if (vb2_queue_is_busy(vdev, file))
  843. return -EBUSY;
  844. return vb2_streamoff(vdev->queue, i);
  845. }
  846. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  847. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  848. {
  849. struct video_device *vdev = video_devdata(file);
  850. if (vb2_queue_is_busy(vdev, file))
  851. return -EBUSY;
  852. return vb2_expbuf(vdev->queue, p);
  853. }
  854. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  855. /* v4l2_file_operations helpers */
  856. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  857. {
  858. struct video_device *vdev = video_devdata(file);
  859. return vb2_mmap(vdev->queue, vma);
  860. }
  861. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  862. int _vb2_fop_release(struct file *file, struct mutex *lock)
  863. {
  864. struct video_device *vdev = video_devdata(file);
  865. if (lock)
  866. mutex_lock(lock);
  867. if (file->private_data == vdev->queue->owner) {
  868. vb2_queue_release(vdev->queue);
  869. vdev->queue->owner = NULL;
  870. }
  871. if (lock)
  872. mutex_unlock(lock);
  873. return v4l2_fh_release(file);
  874. }
  875. EXPORT_SYMBOL_GPL(_vb2_fop_release);
  876. int vb2_fop_release(struct file *file)
  877. {
  878. struct video_device *vdev = video_devdata(file);
  879. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  880. return _vb2_fop_release(file, lock);
  881. }
  882. EXPORT_SYMBOL_GPL(vb2_fop_release);
  883. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  884. size_t count, loff_t *ppos)
  885. {
  886. struct video_device *vdev = video_devdata(file);
  887. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  888. int err = -EBUSY;
  889. if (!(vdev->queue->io_modes & VB2_WRITE))
  890. return -EINVAL;
  891. if (lock && mutex_lock_interruptible(lock))
  892. return -ERESTARTSYS;
  893. if (vb2_queue_is_busy(vdev, file))
  894. goto exit;
  895. err = vb2_write(vdev->queue, buf, count, ppos,
  896. file->f_flags & O_NONBLOCK);
  897. if (vdev->queue->fileio)
  898. vdev->queue->owner = file->private_data;
  899. exit:
  900. if (lock)
  901. mutex_unlock(lock);
  902. return err;
  903. }
  904. EXPORT_SYMBOL_GPL(vb2_fop_write);
  905. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  906. size_t count, loff_t *ppos)
  907. {
  908. struct video_device *vdev = video_devdata(file);
  909. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  910. int err = -EBUSY;
  911. if (!(vdev->queue->io_modes & VB2_READ))
  912. return -EINVAL;
  913. if (lock && mutex_lock_interruptible(lock))
  914. return -ERESTARTSYS;
  915. if (vb2_queue_is_busy(vdev, file))
  916. goto exit;
  917. err = vb2_read(vdev->queue, buf, count, ppos,
  918. file->f_flags & O_NONBLOCK);
  919. if (vdev->queue->fileio)
  920. vdev->queue->owner = file->private_data;
  921. exit:
  922. if (lock)
  923. mutex_unlock(lock);
  924. return err;
  925. }
  926. EXPORT_SYMBOL_GPL(vb2_fop_read);
  927. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  928. {
  929. struct video_device *vdev = video_devdata(file);
  930. struct vb2_queue *q = vdev->queue;
  931. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  932. unsigned res;
  933. void *fileio;
  934. /*
  935. * If this helper doesn't know how to lock, then you shouldn't be using
  936. * it but you should write your own.
  937. */
  938. WARN_ON(!lock);
  939. if (lock && mutex_lock_interruptible(lock))
  940. return POLLERR;
  941. fileio = q->fileio;
  942. res = vb2_poll(vdev->queue, file, wait);
  943. /* If fileio was started, then we have a new queue owner. */
  944. if (!fileio && q->fileio)
  945. q->owner = file->private_data;
  946. if (lock)
  947. mutex_unlock(lock);
  948. return res;
  949. }
  950. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  951. #ifndef CONFIG_MMU
  952. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  953. unsigned long len, unsigned long pgoff, unsigned long flags)
  954. {
  955. struct video_device *vdev = video_devdata(file);
  956. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  957. }
  958. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  959. #endif
  960. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  961. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  962. {
  963. mutex_unlock(vq->lock);
  964. }
  965. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  966. void vb2_ops_wait_finish(struct vb2_queue *vq)
  967. {
  968. mutex_lock(vq->lock);
  969. }
  970. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  971. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  972. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  973. MODULE_LICENSE("GPL");