videobuf2-v4l2.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. #include "videobuf2-internal.h"
  31. /* Flags that are set by the vb2 core */
  32. #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  33. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  34. V4L2_BUF_FLAG_PREPARED | \
  35. V4L2_BUF_FLAG_TIMESTAMP_MASK)
  36. /* Output buffer flags that should be passed on to the driver */
  37. #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
  38. V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
  39. /**
  40. * __verify_planes_array() - verify that the planes array passed in struct
  41. * v4l2_buffer from userspace can be safely used
  42. */
  43. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  44. {
  45. if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  46. return 0;
  47. /* Is memory for copying plane information present? */
  48. if (NULL == b->m.planes) {
  49. dprintk(1, "multi-planar buffer passed but "
  50. "planes array not provided\n");
  51. return -EINVAL;
  52. }
  53. if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
  54. dprintk(1, "incorrect planes array length, "
  55. "expected %d, got %d\n", vb->num_planes, b->length);
  56. return -EINVAL;
  57. }
  58. return 0;
  59. }
  60. static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
  61. {
  62. return __verify_planes_array(vb, pb);
  63. }
  64. /**
  65. * __verify_length() - Verify that the bytesused value for each plane fits in
  66. * the plane length and that the data offset doesn't exceed the bytesused value.
  67. */
  68. static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  69. {
  70. unsigned int length;
  71. unsigned int bytesused;
  72. unsigned int plane;
  73. if (!V4L2_TYPE_IS_OUTPUT(b->type))
  74. return 0;
  75. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  76. for (plane = 0; plane < vb->num_planes; ++plane) {
  77. length = (b->memory == VB2_MEMORY_USERPTR ||
  78. b->memory == VB2_MEMORY_DMABUF)
  79. ? b->m.planes[plane].length
  80. : vb->planes[plane].length;
  81. bytesused = b->m.planes[plane].bytesused
  82. ? b->m.planes[plane].bytesused : length;
  83. if (b->m.planes[plane].bytesused > length)
  84. return -EINVAL;
  85. if (b->m.planes[plane].data_offset > 0 &&
  86. b->m.planes[plane].data_offset >= bytesused)
  87. return -EINVAL;
  88. }
  89. } else {
  90. length = (b->memory == VB2_MEMORY_USERPTR)
  91. ? b->length : vb->planes[0].length;
  92. if (b->bytesused > length)
  93. return -EINVAL;
  94. }
  95. return 0;
  96. }
  97. static int __set_timestamp(struct vb2_buffer *vb, const void *pb)
  98. {
  99. const struct v4l2_buffer *b = pb;
  100. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  101. struct vb2_queue *q = vb->vb2_queue;
  102. if (q->is_output) {
  103. /*
  104. * For output buffers copy the timestamp if needed,
  105. * and the timecode field and flag if needed.
  106. */
  107. if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  108. V4L2_BUF_FLAG_TIMESTAMP_COPY)
  109. vbuf->timestamp = b->timestamp;
  110. vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
  111. if (b->flags & V4L2_BUF_FLAG_TIMECODE)
  112. vbuf->timecode = b->timecode;
  113. }
  114. return 0;
  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. pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
  123. if (vb->vb2_queue->allow_zero_bytesused)
  124. pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
  125. else
  126. pr_warn("use the actual size instead.\n");
  127. }
  128. static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
  129. const char *opname)
  130. {
  131. if (b->type != q->type) {
  132. dprintk(1, "%s: invalid buffer type\n", opname);
  133. return -EINVAL;
  134. }
  135. if (b->index >= q->num_buffers) {
  136. dprintk(1, "%s: buffer index out of range\n", opname);
  137. return -EINVAL;
  138. }
  139. if (q->bufs[b->index] == NULL) {
  140. /* Should never happen */
  141. dprintk(1, "%s: buffer is NULL\n", opname);
  142. return -EINVAL;
  143. }
  144. if (b->memory != q->memory) {
  145. dprintk(1, "%s: invalid memory type\n", opname);
  146. return -EINVAL;
  147. }
  148. return __verify_planes_array(q->bufs[b->index], b);
  149. }
  150. /**
  151. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  152. * returned to userspace
  153. */
  154. static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
  155. {
  156. struct v4l2_buffer *b = pb;
  157. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  158. struct vb2_queue *q = vb->vb2_queue;
  159. unsigned int plane;
  160. /* Copy back data such as timestamp, flags, etc. */
  161. b->index = vb->index;
  162. b->type = vb->type;
  163. b->memory = vb->memory;
  164. b->bytesused = 0;
  165. b->flags = vbuf->flags;
  166. b->field = vbuf->field;
  167. b->timestamp = vbuf->timestamp;
  168. b->timecode = vbuf->timecode;
  169. b->sequence = vbuf->sequence;
  170. b->reserved2 = 0;
  171. b->reserved = 0;
  172. if (q->is_multiplanar) {
  173. /*
  174. * Fill in plane-related data if userspace provided an array
  175. * for it. The caller has already verified memory and size.
  176. */
  177. b->length = vb->num_planes;
  178. for (plane = 0; plane < vb->num_planes; ++plane) {
  179. struct v4l2_plane *pdst = &b->m.planes[plane];
  180. struct vb2_plane *psrc = &vb->planes[plane];
  181. pdst->bytesused = psrc->bytesused;
  182. pdst->length = psrc->length;
  183. if (q->memory == VB2_MEMORY_MMAP)
  184. pdst->m.mem_offset = psrc->m.offset;
  185. else if (q->memory == VB2_MEMORY_USERPTR)
  186. pdst->m.userptr = psrc->m.userptr;
  187. else if (q->memory == VB2_MEMORY_DMABUF)
  188. pdst->m.fd = psrc->m.fd;
  189. pdst->data_offset = psrc->data_offset;
  190. memset(pdst->reserved, 0, sizeof(pdst->reserved));
  191. }
  192. } else {
  193. /*
  194. * We use length and offset in v4l2_planes array even for
  195. * single-planar buffers, but userspace does not.
  196. */
  197. b->length = vb->planes[0].length;
  198. b->bytesused = vb->planes[0].bytesused;
  199. if (q->memory == VB2_MEMORY_MMAP)
  200. b->m.offset = vb->planes[0].m.offset;
  201. else if (q->memory == VB2_MEMORY_USERPTR)
  202. b->m.userptr = vb->planes[0].m.userptr;
  203. else if (q->memory == VB2_MEMORY_DMABUF)
  204. b->m.fd = vb->planes[0].m.fd;
  205. }
  206. /*
  207. * Clear any buffer state related flags.
  208. */
  209. b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
  210. b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
  211. if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
  212. V4L2_BUF_FLAG_TIMESTAMP_COPY) {
  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. return 0;
  243. }
  244. /**
  245. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
  246. * v4l2_buffer by the userspace. It also verifies that struct
  247. * v4l2_buffer has a valid number of planes.
  248. */
  249. static int __fill_vb2_buffer(struct vb2_buffer *vb,
  250. const void *pb, struct vb2_plane *planes)
  251. {
  252. struct vb2_queue *q = vb->vb2_queue;
  253. const struct v4l2_buffer *b = pb;
  254. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  255. unsigned int plane;
  256. int ret;
  257. ret = __verify_length(vb, b);
  258. if (ret < 0) {
  259. dprintk(1, "plane parameters verification failed: %d\n", ret);
  260. return ret;
  261. }
  262. if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
  263. /*
  264. * If the format's field is ALTERNATE, then the buffer's field
  265. * should be either TOP or BOTTOM, not ALTERNATE since that
  266. * makes no sense. The driver has to know whether the
  267. * buffer represents a top or a bottom field in order to
  268. * program any DMA correctly. Using ALTERNATE is wrong, since
  269. * that just says that it is either a top or a bottom field,
  270. * but not which of the two it is.
  271. */
  272. dprintk(1, "the field is incorrectly set to ALTERNATE "
  273. "for an output buffer\n");
  274. return -EINVAL;
  275. }
  276. vbuf->timestamp.tv_sec = 0;
  277. vbuf->timestamp.tv_usec = 0;
  278. vbuf->sequence = 0;
  279. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  280. if (b->memory == VB2_MEMORY_USERPTR) {
  281. for (plane = 0; plane < vb->num_planes; ++plane) {
  282. planes[plane].m.userptr =
  283. b->m.planes[plane].m.userptr;
  284. planes[plane].length =
  285. b->m.planes[plane].length;
  286. }
  287. }
  288. if (b->memory == VB2_MEMORY_DMABUF) {
  289. for (plane = 0; plane < vb->num_planes; ++plane) {
  290. planes[plane].m.fd =
  291. b->m.planes[plane].m.fd;
  292. planes[plane].length =
  293. b->m.planes[plane].length;
  294. }
  295. }
  296. /* Fill in driver-provided information for OUTPUT types */
  297. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  298. /*
  299. * Will have to go up to b->length when API starts
  300. * accepting variable number of planes.
  301. *
  302. * If bytesused == 0 for the output buffer, then fall
  303. * back to the full buffer size. In that case
  304. * userspace clearly never bothered to set it and
  305. * it's a safe assumption that they really meant to
  306. * use the full plane sizes.
  307. *
  308. * Some drivers, e.g. old codec drivers, use bytesused == 0
  309. * as a way to indicate that streaming is finished.
  310. * In that case, the driver should use the
  311. * allow_zero_bytesused flag to keep old userspace
  312. * applications working.
  313. */
  314. for (plane = 0; plane < vb->num_planes; ++plane) {
  315. struct vb2_plane *pdst = &planes[plane];
  316. struct v4l2_plane *psrc = &b->m.planes[plane];
  317. if (psrc->bytesused == 0)
  318. vb2_warn_zero_bytesused(vb);
  319. if (vb->vb2_queue->allow_zero_bytesused)
  320. pdst->bytesused = psrc->bytesused;
  321. else
  322. pdst->bytesused = psrc->bytesused ?
  323. psrc->bytesused : pdst->length;
  324. pdst->data_offset = psrc->data_offset;
  325. }
  326. }
  327. } else {
  328. /*
  329. * Single-planar buffers do not use planes array,
  330. * so fill in relevant v4l2_buffer struct fields instead.
  331. * In videobuf we use our internal V4l2_planes struct for
  332. * single-planar buffers as well, for simplicity.
  333. *
  334. * If bytesused == 0 for the output buffer, then fall back
  335. * to the full buffer size as that's a sensible default.
  336. *
  337. * Some drivers, e.g. old codec drivers, use bytesused == 0 as
  338. * a way to indicate that streaming is finished. In that case,
  339. * the driver should use the allow_zero_bytesused flag to keep
  340. * old userspace applications working.
  341. */
  342. if (b->memory == VB2_MEMORY_USERPTR) {
  343. planes[0].m.userptr = b->m.userptr;
  344. planes[0].length = b->length;
  345. }
  346. if (b->memory == VB2_MEMORY_DMABUF) {
  347. planes[0].m.fd = b->m.fd;
  348. planes[0].length = b->length;
  349. }
  350. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  351. if (b->bytesused == 0)
  352. vb2_warn_zero_bytesused(vb);
  353. if (vb->vb2_queue->allow_zero_bytesused)
  354. planes[0].bytesused = b->bytesused;
  355. else
  356. planes[0].bytesused = b->bytesused ?
  357. b->bytesused : planes[0].length;
  358. } else
  359. planes[0].bytesused = 0;
  360. }
  361. /* Zero flags that the vb2 core handles */
  362. vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
  363. if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
  364. V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
  365. /*
  366. * Non-COPY timestamps and non-OUTPUT queues will get
  367. * their timestamp and timestamp source flags from the
  368. * queue.
  369. */
  370. vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  371. }
  372. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  373. /*
  374. * For output buffers mask out the timecode flag:
  375. * this will be handled later in vb2_internal_qbuf().
  376. * The 'field' is valid metadata for this output buffer
  377. * and so that needs to be copied here.
  378. */
  379. vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
  380. vbuf->field = b->field;
  381. } else {
  382. /* Zero any output buffer flags as this is a capture buffer */
  383. vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
  384. }
  385. return 0;
  386. }
  387. static const struct vb2_buf_ops v4l2_buf_ops = {
  388. .verify_planes_array = __verify_planes_array_core,
  389. .fill_user_buffer = __fill_v4l2_buffer,
  390. .fill_vb2_buffer = __fill_vb2_buffer,
  391. .set_timestamp = __set_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. return ret ? ret : vb2_core_querybuf(q, b->index, b);
  421. }
  422. EXPORT_SYMBOL(vb2_querybuf);
  423. /**
  424. * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
  425. * the memory and type values.
  426. * @q: videobuf2 queue
  427. * @req: struct passed from userspace to vidioc_reqbufs handler
  428. * in driver
  429. */
  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. /**
  437. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  438. * @q: videobuf2 queue
  439. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  440. * handler in driver
  441. *
  442. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  443. * This function:
  444. * 1) verifies the passed buffer,
  445. * 2) calls buf_prepare callback in the driver (if provided), in which
  446. * driver-specific buffer initialization can be performed,
  447. *
  448. * The return values from this function are intended to be directly returned
  449. * from vidioc_prepare_buf handler in driver.
  450. */
  451. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  452. {
  453. int ret;
  454. if (vb2_fileio_is_active(q)) {
  455. dprintk(1, "file io in progress\n");
  456. return -EBUSY;
  457. }
  458. ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
  459. return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
  460. }
  461. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  462. /**
  463. * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
  464. * the memory and type values.
  465. * @q: videobuf2 queue
  466. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  467. * handler in driver
  468. */
  469. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  470. {
  471. int ret = vb2_verify_memory_type(q, create->memory,
  472. create->format.type);
  473. create->index = q->num_buffers;
  474. if (create->count == 0)
  475. return ret != -EBUSY ? ret : 0;
  476. return ret ? ret : vb2_core_create_bufs(q, create->memory,
  477. &create->count, &create->format);
  478. }
  479. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  480. static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  481. {
  482. int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
  483. return ret ? ret : vb2_core_qbuf(q, b->index, b);
  484. }
  485. /**
  486. * vb2_qbuf() - Queue a buffer from userspace
  487. * @q: videobuf2 queue
  488. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  489. * in driver
  490. *
  491. * Should be called from vidioc_qbuf ioctl handler of a driver.
  492. * This function:
  493. * 1) verifies the passed buffer,
  494. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  495. * which driver-specific buffer initialization can be performed,
  496. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  497. * callback for processing.
  498. *
  499. * The return values from this function are intended to be directly returned
  500. * from vidioc_qbuf handler in driver.
  501. */
  502. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  503. {
  504. if (vb2_fileio_is_active(q)) {
  505. dprintk(1, "file io in progress\n");
  506. return -EBUSY;
  507. }
  508. return vb2_internal_qbuf(q, b);
  509. }
  510. EXPORT_SYMBOL_GPL(vb2_qbuf);
  511. static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
  512. bool nonblocking)
  513. {
  514. int ret;
  515. if (b->type != q->type) {
  516. dprintk(1, "invalid buffer type\n");
  517. return -EINVAL;
  518. }
  519. ret = vb2_core_dqbuf(q, b, nonblocking);
  520. if (!ret && !q->is_output &&
  521. b->flags & V4L2_BUF_FLAG_LAST)
  522. q->last_buffer_dequeued = true;
  523. /*
  524. * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
  525. * cleared.
  526. */
  527. b->flags &= ~V4L2_BUF_FLAG_DONE;
  528. return ret;
  529. }
  530. /**
  531. * vb2_dqbuf() - Dequeue a buffer to the userspace
  532. * @q: videobuf2 queue
  533. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  534. * in driver
  535. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  536. * buffers ready for dequeuing are present. Normally the driver
  537. * would be passing (file->f_flags & O_NONBLOCK) here
  538. *
  539. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  540. * This function:
  541. * 1) verifies the passed buffer,
  542. * 2) calls buf_finish callback in the driver (if provided), in which
  543. * driver can perform any additional operations that may be required before
  544. * returning the buffer to userspace, such as cache sync,
  545. * 3) the buffer struct members are filled with relevant information for
  546. * the userspace.
  547. *
  548. * The return values from this function are intended to be directly returned
  549. * from vidioc_dqbuf handler in driver.
  550. */
  551. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  552. {
  553. if (vb2_fileio_is_active(q)) {
  554. dprintk(1, "file io in progress\n");
  555. return -EBUSY;
  556. }
  557. return vb2_internal_dqbuf(q, b, nonblocking);
  558. }
  559. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  560. /**
  561. * vb2_streamon - start streaming
  562. * @q: videobuf2 queue
  563. * @type: type argument passed from userspace to vidioc_streamon handler
  564. *
  565. * Should be called from vidioc_streamon handler of a driver.
  566. * This function:
  567. * 1) verifies current state
  568. * 2) passes any previously queued buffers to the driver and starts streaming
  569. *
  570. * The return values from this function are intended to be directly returned
  571. * from vidioc_streamon handler in the driver.
  572. */
  573. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  574. {
  575. if (vb2_fileio_is_active(q)) {
  576. dprintk(1, "file io in progress\n");
  577. return -EBUSY;
  578. }
  579. return vb2_core_streamon(q, type);
  580. }
  581. EXPORT_SYMBOL_GPL(vb2_streamon);
  582. /**
  583. * vb2_streamoff - stop streaming
  584. * @q: videobuf2 queue
  585. * @type: type argument passed from userspace to vidioc_streamoff handler
  586. *
  587. * Should be called from vidioc_streamoff handler of a driver.
  588. * This function:
  589. * 1) verifies current state,
  590. * 2) stop streaming and dequeues any queued buffers, including those previously
  591. * passed to the driver (after waiting for the driver to finish).
  592. *
  593. * This call can be used for pausing playback.
  594. * The return values from this function are intended to be directly returned
  595. * from vidioc_streamoff handler in the driver
  596. */
  597. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  598. {
  599. if (vb2_fileio_is_active(q)) {
  600. dprintk(1, "file io in progress\n");
  601. return -EBUSY;
  602. }
  603. return vb2_core_streamoff(q, type);
  604. }
  605. EXPORT_SYMBOL_GPL(vb2_streamoff);
  606. /**
  607. * vb2_expbuf() - Export a buffer as a file descriptor
  608. * @q: videobuf2 queue
  609. * @eb: export buffer structure passed from userspace to vidioc_expbuf
  610. * handler in driver
  611. *
  612. * The return values from this function are intended to be directly returned
  613. * from vidioc_expbuf handler in driver.
  614. */
  615. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  616. {
  617. return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
  618. eb->plane, eb->flags);
  619. }
  620. EXPORT_SYMBOL_GPL(vb2_expbuf);
  621. /**
  622. * vb2_queue_init() - initialize a videobuf2 queue
  623. * @q: videobuf2 queue; this structure should be allocated in driver
  624. *
  625. * The vb2_queue structure should be allocated by the driver. The driver is
  626. * responsible of clearing it's content and setting initial values for some
  627. * required entries before calling this function.
  628. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  629. * to the struct vb2_queue description in include/media/videobuf2-core.h
  630. * for more information.
  631. */
  632. int vb2_queue_init(struct vb2_queue *q)
  633. {
  634. /*
  635. * Sanity check
  636. */
  637. if (WARN_ON(!q) ||
  638. WARN_ON(q->timestamp_flags &
  639. ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
  640. V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
  641. return -EINVAL;
  642. /* Warn that the driver should choose an appropriate timestamp type */
  643. WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  644. V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  645. /* Warn that vb2_memory should match with v4l2_memory */
  646. if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
  647. || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
  648. || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
  649. return -EINVAL;
  650. if (q->buf_struct_size == 0)
  651. q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
  652. q->buf_ops = &v4l2_buf_ops;
  653. q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  654. q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
  655. return vb2_core_queue_init(q);
  656. }
  657. EXPORT_SYMBOL_GPL(vb2_queue_init);
  658. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  659. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  660. /**
  661. * vb2_queue_release() - stop streaming, release the queue and free memory
  662. * @q: videobuf2 queue
  663. *
  664. * This function stops streaming and performs necessary clean ups, including
  665. * freeing video buffer memory. The driver is responsible for freeing
  666. * the vb2_queue structure itself.
  667. */
  668. void vb2_queue_release(struct vb2_queue *q)
  669. {
  670. __vb2_cleanup_fileio(q);
  671. vb2_core_queue_release(q);
  672. }
  673. EXPORT_SYMBOL_GPL(vb2_queue_release);
  674. /**
  675. * vb2_poll() - implements poll userspace operation
  676. * @q: videobuf2 queue
  677. * @file: file argument passed to the poll file operation handler
  678. * @wait: wait argument passed to the poll file operation handler
  679. *
  680. * This function implements poll file operation handler for a driver.
  681. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  682. * be informed that the file descriptor of a video device is available for
  683. * reading.
  684. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  685. * will be reported as available for writing.
  686. *
  687. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  688. * pending events.
  689. *
  690. * The return values from this function are intended to be directly returned
  691. * from poll handler in driver.
  692. */
  693. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  694. {
  695. struct video_device *vfd = video_devdata(file);
  696. unsigned long req_events = poll_requested_events(wait);
  697. struct vb2_buffer *vb = NULL;
  698. unsigned int res = 0;
  699. unsigned long flags;
  700. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  701. struct v4l2_fh *fh = file->private_data;
  702. if (v4l2_event_pending(fh))
  703. res = POLLPRI;
  704. else if (req_events & POLLPRI)
  705. poll_wait(file, &fh->wait, wait);
  706. }
  707. if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
  708. return res;
  709. if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
  710. return res;
  711. /*
  712. * Start file I/O emulator only if streaming API has not been used yet.
  713. */
  714. if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
  715. if (!q->is_output && (q->io_modes & VB2_READ) &&
  716. (req_events & (POLLIN | POLLRDNORM))) {
  717. if (__vb2_init_fileio(q, 1))
  718. return res | POLLERR;
  719. }
  720. if (q->is_output && (q->io_modes & VB2_WRITE) &&
  721. (req_events & (POLLOUT | POLLWRNORM))) {
  722. if (__vb2_init_fileio(q, 0))
  723. return res | POLLERR;
  724. /*
  725. * Write to OUTPUT queue can be done immediately.
  726. */
  727. return res | POLLOUT | POLLWRNORM;
  728. }
  729. }
  730. /*
  731. * There is nothing to wait for if the queue isn't streaming, or if the
  732. * error flag is set.
  733. */
  734. if (!vb2_is_streaming(q) || q->error)
  735. return res | POLLERR;
  736. /*
  737. * For compatibility with vb1: if QBUF hasn't been called yet, then
  738. * return POLLERR as well. This only affects capture queues, output
  739. * queues will always initialize waiting_for_buffers to false.
  740. */
  741. if (q->waiting_for_buffers)
  742. return res | POLLERR;
  743. /*
  744. * For output streams you can call write() as long as there are fewer
  745. * buffers queued than there are buffers available.
  746. */
  747. if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
  748. return res | POLLOUT | POLLWRNORM;
  749. if (list_empty(&q->done_list)) {
  750. /*
  751. * If the last buffer was dequeued from a capture queue,
  752. * return immediately. DQBUF will return -EPIPE.
  753. */
  754. if (q->last_buffer_dequeued)
  755. return res | POLLIN | POLLRDNORM;
  756. poll_wait(file, &q->done_wq, wait);
  757. }
  758. /*
  759. * Take first buffer available for dequeuing.
  760. */
  761. spin_lock_irqsave(&q->done_lock, flags);
  762. if (!list_empty(&q->done_list))
  763. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  764. done_entry);
  765. spin_unlock_irqrestore(&q->done_lock, flags);
  766. if (vb && (vb->state == VB2_BUF_STATE_DONE
  767. || vb->state == VB2_BUF_STATE_ERROR)) {
  768. return (q->is_output) ?
  769. res | POLLOUT | POLLWRNORM :
  770. res | POLLIN | POLLRDNORM;
  771. }
  772. return res;
  773. }
  774. EXPORT_SYMBOL_GPL(vb2_poll);
  775. /**
  776. * struct vb2_fileio_buf - buffer context used by file io emulator
  777. *
  778. * vb2 provides a compatibility layer and emulator of file io (read and
  779. * write) calls on top of streaming API. This structure is used for
  780. * tracking context related to the buffers.
  781. */
  782. struct vb2_fileio_buf {
  783. void *vaddr;
  784. unsigned int size;
  785. unsigned int pos;
  786. unsigned int queued:1;
  787. };
  788. /**
  789. * struct vb2_fileio_data - queue context used by file io emulator
  790. *
  791. * @cur_index: the index of the buffer currently being read from or
  792. * written to. If equal to q->num_buffers then a new buffer
  793. * must be dequeued.
  794. * @initial_index: in the read() case all buffers are queued up immediately
  795. * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
  796. * buffers. However, in the write() case no buffers are initially
  797. * queued, instead whenever a buffer is full it is queued up by
  798. * __vb2_perform_fileio(). Only once all available buffers have
  799. * been queued up will __vb2_perform_fileio() start to dequeue
  800. * buffers. This means that initially __vb2_perform_fileio()
  801. * needs to know what buffer index to use when it is queuing up
  802. * the buffers for the first time. That initial index is stored
  803. * in this field. Once it is equal to q->num_buffers all
  804. * available buffers have been queued and __vb2_perform_fileio()
  805. * should start the normal dequeue/queue cycle.
  806. *
  807. * vb2 provides a compatibility layer and emulator of file io (read and
  808. * write) calls on top of streaming API. For proper operation it required
  809. * this structure to save the driver state between each call of the read
  810. * or write function.
  811. */
  812. struct vb2_fileio_data {
  813. struct v4l2_requestbuffers req;
  814. struct v4l2_plane p;
  815. struct v4l2_buffer b;
  816. struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
  817. unsigned int cur_index;
  818. unsigned int initial_index;
  819. unsigned int q_count;
  820. unsigned int dq_count;
  821. unsigned read_once:1;
  822. unsigned write_immediately:1;
  823. };
  824. /**
  825. * __vb2_init_fileio() - initialize file io emulator
  826. * @q: videobuf2 queue
  827. * @read: mode selector (1 means read, 0 means write)
  828. */
  829. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  830. {
  831. struct vb2_fileio_data *fileio;
  832. int i, ret;
  833. unsigned int count = 0;
  834. /*
  835. * Sanity check
  836. */
  837. if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
  838. (!read && !(q->io_modes & VB2_WRITE))))
  839. return -EINVAL;
  840. /*
  841. * Check if device supports mapping buffers to kernel virtual space.
  842. */
  843. if (!q->mem_ops->vaddr)
  844. return -EBUSY;
  845. /*
  846. * Check if streaming api has not been already activated.
  847. */
  848. if (q->streaming || q->num_buffers > 0)
  849. return -EBUSY;
  850. /*
  851. * Start with count 1, driver can increase it in queue_setup()
  852. */
  853. count = 1;
  854. dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
  855. (read) ? "read" : "write", count, q->fileio_read_once,
  856. q->fileio_write_immediately);
  857. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  858. if (fileio == NULL)
  859. return -ENOMEM;
  860. fileio->read_once = q->fileio_read_once;
  861. fileio->write_immediately = q->fileio_write_immediately;
  862. /*
  863. * Request buffers and use MMAP type to force driver
  864. * to allocate buffers by itself.
  865. */
  866. fileio->req.count = count;
  867. fileio->req.memory = VB2_MEMORY_MMAP;
  868. fileio->req.type = q->type;
  869. q->fileio = fileio;
  870. ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
  871. if (ret)
  872. goto err_kfree;
  873. /*
  874. * Check if plane_count is correct
  875. * (multiplane buffers are not supported).
  876. */
  877. if (q->bufs[0]->num_planes != 1) {
  878. ret = -EBUSY;
  879. goto err_reqbufs;
  880. }
  881. /*
  882. * Get kernel address of each buffer.
  883. */
  884. for (i = 0; i < q->num_buffers; i++) {
  885. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  886. if (fileio->bufs[i].vaddr == NULL) {
  887. ret = -EINVAL;
  888. goto err_reqbufs;
  889. }
  890. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  891. }
  892. /*
  893. * Read mode requires pre queuing of all buffers.
  894. */
  895. if (read) {
  896. bool is_multiplanar = q->is_multiplanar;
  897. /*
  898. * Queue all buffers.
  899. */
  900. for (i = 0; i < q->num_buffers; i++) {
  901. struct v4l2_buffer *b = &fileio->b;
  902. memset(b, 0, sizeof(*b));
  903. b->type = q->type;
  904. if (is_multiplanar) {
  905. memset(&fileio->p, 0, sizeof(fileio->p));
  906. b->m.planes = &fileio->p;
  907. b->length = 1;
  908. }
  909. b->memory = q->memory;
  910. b->index = i;
  911. ret = vb2_internal_qbuf(q, b);
  912. if (ret)
  913. goto err_reqbufs;
  914. fileio->bufs[i].queued = 1;
  915. }
  916. /*
  917. * All buffers have been queued, so mark that by setting
  918. * initial_index to q->num_buffers
  919. */
  920. fileio->initial_index = q->num_buffers;
  921. fileio->cur_index = q->num_buffers;
  922. }
  923. /*
  924. * Start streaming.
  925. */
  926. ret = vb2_core_streamon(q, q->type);
  927. if (ret)
  928. goto err_reqbufs;
  929. return ret;
  930. err_reqbufs:
  931. fileio->req.count = 0;
  932. vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
  933. err_kfree:
  934. q->fileio = NULL;
  935. kfree(fileio);
  936. return ret;
  937. }
  938. /**
  939. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  940. * @q: videobuf2 queue
  941. */
  942. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  943. {
  944. struct vb2_fileio_data *fileio = q->fileio;
  945. if (fileio) {
  946. vb2_core_streamoff(q, q->type);
  947. q->fileio = NULL;
  948. fileio->req.count = 0;
  949. vb2_reqbufs(q, &fileio->req);
  950. kfree(fileio);
  951. dprintk(3, "file io emulator closed\n");
  952. }
  953. return 0;
  954. }
  955. /**
  956. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  957. * @q: videobuf2 queue
  958. * @data: pointed to target userspace buffer
  959. * @count: number of bytes to read or write
  960. * @ppos: file handle position tracking pointer
  961. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  962. * @read: access mode selector (1 means read, 0 means write)
  963. */
  964. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  965. loff_t *ppos, int nonblock, int read)
  966. {
  967. struct vb2_fileio_data *fileio;
  968. struct vb2_fileio_buf *buf;
  969. bool is_multiplanar = q->is_multiplanar;
  970. /*
  971. * When using write() to write data to an output video node the vb2 core
  972. * should set timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
  973. * else is able to provide this information with the write() operation.
  974. */
  975. bool set_timestamp = !read &&
  976. (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  977. V4L2_BUF_FLAG_TIMESTAMP_COPY;
  978. int ret, index;
  979. dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
  980. read ? "read" : "write", (long)*ppos, count,
  981. nonblock ? "non" : "");
  982. if (!data)
  983. return -EINVAL;
  984. /*
  985. * Initialize emulator on first call.
  986. */
  987. if (!vb2_fileio_is_active(q)) {
  988. ret = __vb2_init_fileio(q, read);
  989. dprintk(3, "vb2_init_fileio result: %d\n", ret);
  990. if (ret)
  991. return ret;
  992. }
  993. fileio = q->fileio;
  994. /*
  995. * Check if we need to dequeue the buffer.
  996. */
  997. index = fileio->cur_index;
  998. if (index >= q->num_buffers) {
  999. /*
  1000. * Call vb2_dqbuf to get buffer back.
  1001. */
  1002. memset(&fileio->b, 0, sizeof(fileio->b));
  1003. fileio->b.type = q->type;
  1004. fileio->b.memory = q->memory;
  1005. if (is_multiplanar) {
  1006. memset(&fileio->p, 0, sizeof(fileio->p));
  1007. fileio->b.m.planes = &fileio->p;
  1008. fileio->b.length = 1;
  1009. }
  1010. ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
  1011. dprintk(5, "vb2_dqbuf result: %d\n", ret);
  1012. if (ret)
  1013. return ret;
  1014. fileio->dq_count += 1;
  1015. fileio->cur_index = index = fileio->b.index;
  1016. buf = &fileio->bufs[index];
  1017. /*
  1018. * Get number of bytes filled by the driver
  1019. */
  1020. buf->pos = 0;
  1021. buf->queued = 0;
  1022. buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
  1023. : vb2_plane_size(q->bufs[index], 0);
  1024. /* Compensate for data_offset on read in the multiplanar case. */
  1025. if (is_multiplanar && read &&
  1026. fileio->b.m.planes[0].data_offset < buf->size) {
  1027. buf->pos = fileio->b.m.planes[0].data_offset;
  1028. buf->size -= buf->pos;
  1029. }
  1030. } else {
  1031. buf = &fileio->bufs[index];
  1032. }
  1033. /*
  1034. * Limit count on last few bytes of the buffer.
  1035. */
  1036. if (buf->pos + count > buf->size) {
  1037. count = buf->size - buf->pos;
  1038. dprintk(5, "reducing read count: %zd\n", count);
  1039. }
  1040. /*
  1041. * Transfer data to userspace.
  1042. */
  1043. dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
  1044. count, index, buf->pos);
  1045. if (read)
  1046. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  1047. else
  1048. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  1049. if (ret) {
  1050. dprintk(3, "error copying data\n");
  1051. return -EFAULT;
  1052. }
  1053. /*
  1054. * Update counters.
  1055. */
  1056. buf->pos += count;
  1057. *ppos += count;
  1058. /*
  1059. * Queue next buffer if required.
  1060. */
  1061. if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
  1062. /*
  1063. * Check if this is the last buffer to read.
  1064. */
  1065. if (read && fileio->read_once && fileio->dq_count == 1) {
  1066. dprintk(3, "read limit reached\n");
  1067. return __vb2_cleanup_fileio(q);
  1068. }
  1069. /*
  1070. * Call vb2_qbuf and give buffer to the driver.
  1071. */
  1072. memset(&fileio->b, 0, sizeof(fileio->b));
  1073. fileio->b.type = q->type;
  1074. fileio->b.memory = q->memory;
  1075. fileio->b.index = index;
  1076. fileio->b.bytesused = buf->pos;
  1077. if (is_multiplanar) {
  1078. memset(&fileio->p, 0, sizeof(fileio->p));
  1079. fileio->p.bytesused = buf->pos;
  1080. fileio->b.m.planes = &fileio->p;
  1081. fileio->b.length = 1;
  1082. }
  1083. if (set_timestamp)
  1084. v4l2_get_timestamp(&fileio->b.timestamp);
  1085. ret = vb2_internal_qbuf(q, &fileio->b);
  1086. dprintk(5, "vb2_dbuf result: %d\n", ret);
  1087. if (ret)
  1088. return ret;
  1089. /*
  1090. * Buffer has been queued, update the status
  1091. */
  1092. buf->pos = 0;
  1093. buf->queued = 1;
  1094. buf->size = vb2_plane_size(q->bufs[index], 0);
  1095. fileio->q_count += 1;
  1096. /*
  1097. * If we are queuing up buffers for the first time, then
  1098. * increase initial_index by one.
  1099. */
  1100. if (fileio->initial_index < q->num_buffers)
  1101. fileio->initial_index++;
  1102. /*
  1103. * The next buffer to use is either a buffer that's going to be
  1104. * queued for the first time (initial_index < q->num_buffers)
  1105. * or it is equal to q->num_buffers, meaning that the next
  1106. * time we need to dequeue a buffer since we've now queued up
  1107. * all the 'first time' buffers.
  1108. */
  1109. fileio->cur_index = fileio->initial_index;
  1110. }
  1111. /*
  1112. * Return proper number of bytes processed.
  1113. */
  1114. if (ret == 0)
  1115. ret = count;
  1116. return ret;
  1117. }
  1118. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1119. loff_t *ppos, int nonblocking)
  1120. {
  1121. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  1122. }
  1123. EXPORT_SYMBOL_GPL(vb2_read);
  1124. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  1125. loff_t *ppos, int nonblocking)
  1126. {
  1127. return __vb2_perform_fileio(q, (char __user *) data, count,
  1128. ppos, nonblocking, 0);
  1129. }
  1130. EXPORT_SYMBOL_GPL(vb2_write);
  1131. struct vb2_threadio_data {
  1132. struct task_struct *thread;
  1133. vb2_thread_fnc fnc;
  1134. void *priv;
  1135. bool stop;
  1136. };
  1137. static int vb2_thread(void *data)
  1138. {
  1139. struct vb2_queue *q = data;
  1140. struct vb2_threadio_data *threadio = q->threadio;
  1141. struct vb2_fileio_data *fileio = q->fileio;
  1142. bool set_timestamp = false;
  1143. int prequeue = 0;
  1144. int index = 0;
  1145. int ret = 0;
  1146. if (q->is_output) {
  1147. prequeue = q->num_buffers;
  1148. set_timestamp =
  1149. (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  1150. V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1151. }
  1152. set_freezable();
  1153. for (;;) {
  1154. struct vb2_buffer *vb;
  1155. /*
  1156. * Call vb2_dqbuf to get buffer back.
  1157. */
  1158. memset(&fileio->b, 0, sizeof(fileio->b));
  1159. fileio->b.type = q->type;
  1160. fileio->b.memory = q->memory;
  1161. if (prequeue) {
  1162. fileio->b.index = index++;
  1163. prequeue--;
  1164. } else {
  1165. call_void_qop(q, wait_finish, q);
  1166. if (!threadio->stop)
  1167. ret = vb2_internal_dqbuf(q, &fileio->b, 0);
  1168. call_void_qop(q, wait_prepare, q);
  1169. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  1170. }
  1171. if (ret || threadio->stop)
  1172. break;
  1173. try_to_freeze();
  1174. vb = q->bufs[fileio->b.index];
  1175. if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
  1176. if (threadio->fnc(vb, threadio->priv))
  1177. break;
  1178. call_void_qop(q, wait_finish, q);
  1179. if (set_timestamp)
  1180. v4l2_get_timestamp(&fileio->b.timestamp);
  1181. if (!threadio->stop)
  1182. ret = vb2_internal_qbuf(q, &fileio->b);
  1183. call_void_qop(q, wait_prepare, q);
  1184. if (ret || threadio->stop)
  1185. break;
  1186. }
  1187. /* Hmm, linux becomes *very* unhappy without this ... */
  1188. while (!kthread_should_stop()) {
  1189. set_current_state(TASK_INTERRUPTIBLE);
  1190. schedule();
  1191. }
  1192. return 0;
  1193. }
  1194. /*
  1195. * This function should not be used for anything else but the videobuf2-dvb
  1196. * support. If you think you have another good use-case for this, then please
  1197. * contact the linux-media mailinglist first.
  1198. */
  1199. int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
  1200. const char *thread_name)
  1201. {
  1202. struct vb2_threadio_data *threadio;
  1203. int ret = 0;
  1204. if (q->threadio)
  1205. return -EBUSY;
  1206. if (vb2_is_busy(q))
  1207. return -EBUSY;
  1208. if (WARN_ON(q->fileio))
  1209. return -EBUSY;
  1210. threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
  1211. if (threadio == NULL)
  1212. return -ENOMEM;
  1213. threadio->fnc = fnc;
  1214. threadio->priv = priv;
  1215. ret = __vb2_init_fileio(q, !q->is_output);
  1216. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  1217. if (ret)
  1218. goto nomem;
  1219. q->threadio = threadio;
  1220. threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
  1221. if (IS_ERR(threadio->thread)) {
  1222. ret = PTR_ERR(threadio->thread);
  1223. threadio->thread = NULL;
  1224. goto nothread;
  1225. }
  1226. return 0;
  1227. nothread:
  1228. __vb2_cleanup_fileio(q);
  1229. nomem:
  1230. kfree(threadio);
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL_GPL(vb2_thread_start);
  1234. int vb2_thread_stop(struct vb2_queue *q)
  1235. {
  1236. struct vb2_threadio_data *threadio = q->threadio;
  1237. int err;
  1238. if (threadio == NULL)
  1239. return 0;
  1240. threadio->stop = true;
  1241. /* Wake up all pending sleeps in the thread */
  1242. vb2_queue_error(q);
  1243. err = kthread_stop(threadio->thread);
  1244. __vb2_cleanup_fileio(q);
  1245. threadio->thread = NULL;
  1246. kfree(threadio);
  1247. q->threadio = NULL;
  1248. return err;
  1249. }
  1250. EXPORT_SYMBOL_GPL(vb2_thread_stop);
  1251. /*
  1252. * The following functions are not part of the vb2 core API, but are helper
  1253. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  1254. * and struct vb2_ops.
  1255. * They contain boilerplate code that most if not all drivers have to do
  1256. * and so they simplify the driver code.
  1257. */
  1258. /* The queue is busy if there is a owner and you are not that owner. */
  1259. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  1260. {
  1261. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  1262. }
  1263. /* vb2 ioctl helpers */
  1264. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  1265. struct v4l2_requestbuffers *p)
  1266. {
  1267. struct video_device *vdev = video_devdata(file);
  1268. int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
  1269. if (res)
  1270. return res;
  1271. if (vb2_queue_is_busy(vdev, file))
  1272. return -EBUSY;
  1273. res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
  1274. /* If count == 0, then the owner has released all buffers and he
  1275. is no longer owner of the queue. Otherwise we have a new owner. */
  1276. if (res == 0)
  1277. vdev->queue->owner = p->count ? file->private_data : NULL;
  1278. return res;
  1279. }
  1280. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  1281. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  1282. struct v4l2_create_buffers *p)
  1283. {
  1284. struct video_device *vdev = video_devdata(file);
  1285. int res = vb2_verify_memory_type(vdev->queue, p->memory,
  1286. p->format.type);
  1287. p->index = vdev->queue->num_buffers;
  1288. /*
  1289. * If count == 0, then just check if memory and type are valid.
  1290. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
  1291. */
  1292. if (p->count == 0)
  1293. return res != -EBUSY ? res : 0;
  1294. if (res)
  1295. return res;
  1296. if (vb2_queue_is_busy(vdev, file))
  1297. return -EBUSY;
  1298. res = vb2_core_create_bufs(vdev->queue, p->memory, &p->count,
  1299. &p->format);
  1300. if (res == 0)
  1301. vdev->queue->owner = file->private_data;
  1302. return res;
  1303. }
  1304. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  1305. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  1306. struct v4l2_buffer *p)
  1307. {
  1308. struct video_device *vdev = video_devdata(file);
  1309. if (vb2_queue_is_busy(vdev, file))
  1310. return -EBUSY;
  1311. return vb2_prepare_buf(vdev->queue, p);
  1312. }
  1313. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  1314. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1315. {
  1316. struct video_device *vdev = video_devdata(file);
  1317. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  1318. return vb2_querybuf(vdev->queue, p);
  1319. }
  1320. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  1321. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1322. {
  1323. struct video_device *vdev = video_devdata(file);
  1324. if (vb2_queue_is_busy(vdev, file))
  1325. return -EBUSY;
  1326. return vb2_qbuf(vdev->queue, p);
  1327. }
  1328. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  1329. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1330. {
  1331. struct video_device *vdev = video_devdata(file);
  1332. if (vb2_queue_is_busy(vdev, file))
  1333. return -EBUSY;
  1334. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  1335. }
  1336. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  1337. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1338. {
  1339. struct video_device *vdev = video_devdata(file);
  1340. if (vb2_queue_is_busy(vdev, file))
  1341. return -EBUSY;
  1342. return vb2_streamon(vdev->queue, i);
  1343. }
  1344. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  1345. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1346. {
  1347. struct video_device *vdev = video_devdata(file);
  1348. if (vb2_queue_is_busy(vdev, file))
  1349. return -EBUSY;
  1350. return vb2_streamoff(vdev->queue, i);
  1351. }
  1352. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  1353. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  1354. {
  1355. struct video_device *vdev = video_devdata(file);
  1356. if (vb2_queue_is_busy(vdev, file))
  1357. return -EBUSY;
  1358. return vb2_expbuf(vdev->queue, p);
  1359. }
  1360. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  1361. /* v4l2_file_operations helpers */
  1362. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  1363. {
  1364. struct video_device *vdev = video_devdata(file);
  1365. return vb2_mmap(vdev->queue, vma);
  1366. }
  1367. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  1368. int _vb2_fop_release(struct file *file, struct mutex *lock)
  1369. {
  1370. struct video_device *vdev = video_devdata(file);
  1371. if (lock)
  1372. mutex_lock(lock);
  1373. if (file->private_data == vdev->queue->owner) {
  1374. vb2_queue_release(vdev->queue);
  1375. vdev->queue->owner = NULL;
  1376. }
  1377. if (lock)
  1378. mutex_unlock(lock);
  1379. return v4l2_fh_release(file);
  1380. }
  1381. EXPORT_SYMBOL_GPL(_vb2_fop_release);
  1382. int vb2_fop_release(struct file *file)
  1383. {
  1384. struct video_device *vdev = video_devdata(file);
  1385. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  1386. return _vb2_fop_release(file, lock);
  1387. }
  1388. EXPORT_SYMBOL_GPL(vb2_fop_release);
  1389. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  1390. size_t count, loff_t *ppos)
  1391. {
  1392. struct video_device *vdev = video_devdata(file);
  1393. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  1394. int err = -EBUSY;
  1395. if (!(vdev->queue->io_modes & VB2_WRITE))
  1396. return -EINVAL;
  1397. if (lock && mutex_lock_interruptible(lock))
  1398. return -ERESTARTSYS;
  1399. if (vb2_queue_is_busy(vdev, file))
  1400. goto exit;
  1401. err = vb2_write(vdev->queue, buf, count, ppos,
  1402. file->f_flags & O_NONBLOCK);
  1403. if (vdev->queue->fileio)
  1404. vdev->queue->owner = file->private_data;
  1405. exit:
  1406. if (lock)
  1407. mutex_unlock(lock);
  1408. return err;
  1409. }
  1410. EXPORT_SYMBOL_GPL(vb2_fop_write);
  1411. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  1412. size_t count, loff_t *ppos)
  1413. {
  1414. struct video_device *vdev = video_devdata(file);
  1415. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  1416. int err = -EBUSY;
  1417. if (!(vdev->queue->io_modes & VB2_READ))
  1418. return -EINVAL;
  1419. if (lock && mutex_lock_interruptible(lock))
  1420. return -ERESTARTSYS;
  1421. if (vb2_queue_is_busy(vdev, file))
  1422. goto exit;
  1423. err = vb2_read(vdev->queue, buf, count, ppos,
  1424. file->f_flags & O_NONBLOCK);
  1425. if (vdev->queue->fileio)
  1426. vdev->queue->owner = file->private_data;
  1427. exit:
  1428. if (lock)
  1429. mutex_unlock(lock);
  1430. return err;
  1431. }
  1432. EXPORT_SYMBOL_GPL(vb2_fop_read);
  1433. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  1434. {
  1435. struct video_device *vdev = video_devdata(file);
  1436. struct vb2_queue *q = vdev->queue;
  1437. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  1438. unsigned res;
  1439. void *fileio;
  1440. /*
  1441. * If this helper doesn't know how to lock, then you shouldn't be using
  1442. * it but you should write your own.
  1443. */
  1444. WARN_ON(!lock);
  1445. if (lock && mutex_lock_interruptible(lock))
  1446. return POLLERR;
  1447. fileio = q->fileio;
  1448. res = vb2_poll(vdev->queue, file, wait);
  1449. /* If fileio was started, then we have a new queue owner. */
  1450. if (!fileio && q->fileio)
  1451. q->owner = file->private_data;
  1452. if (lock)
  1453. mutex_unlock(lock);
  1454. return res;
  1455. }
  1456. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  1457. #ifndef CONFIG_MMU
  1458. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  1459. unsigned long len, unsigned long pgoff, unsigned long flags)
  1460. {
  1461. struct video_device *vdev = video_devdata(file);
  1462. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  1463. }
  1464. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  1465. #endif
  1466. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  1467. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  1468. {
  1469. mutex_unlock(vq->lock);
  1470. }
  1471. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  1472. void vb2_ops_wait_finish(struct vb2_queue *vq)
  1473. {
  1474. mutex_lock(vq->lock);
  1475. }
  1476. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  1477. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1478. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  1479. MODULE_LICENSE("GPL");