videobuf-core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * generic helper functions for handling video4linux capture buffers
  3. *
  4. * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
  5. *
  6. * Highly based on video-buf written originally by:
  7. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
  8. * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
  9. * (c) 2006 Ted Walther and John Sokol
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/mm.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <media/videobuf-core.h>
  23. #define MAGIC_BUFFER 0x20070728
  24. #define MAGIC_CHECK(is, should) \
  25. do { \
  26. if (unlikely((is) != (should))) { \
  27. printk(KERN_ERR \
  28. "magic mismatch: %x (expected %x)\n", \
  29. is, should); \
  30. BUG(); \
  31. } \
  32. } while (0)
  33. static int debug;
  34. module_param(debug, int, 0644);
  35. MODULE_DESCRIPTION("helper module to manage video4linux buffers");
  36. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  37. MODULE_LICENSE("GPL");
  38. #define dprintk(level, fmt, arg...) \
  39. do { \
  40. if (debug >= level) \
  41. printk(KERN_DEBUG "vbuf: " fmt, ## arg); \
  42. } while (0)
  43. /* --------------------------------------------------------------------- */
  44. #define CALL(q, f, arg...) \
  45. ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
  46. #define CALLPTR(q, f, arg...) \
  47. ((q->int_ops->f) ? q->int_ops->f(arg) : NULL)
  48. struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q)
  49. {
  50. struct videobuf_buffer *vb;
  51. BUG_ON(q->msize < sizeof(*vb));
  52. if (!q->int_ops || !q->int_ops->alloc_vb) {
  53. printk(KERN_ERR "No specific ops defined!\n");
  54. BUG();
  55. }
  56. vb = q->int_ops->alloc_vb(q->msize);
  57. if (NULL != vb) {
  58. init_waitqueue_head(&vb->done);
  59. vb->magic = MAGIC_BUFFER;
  60. }
  61. return vb;
  62. }
  63. EXPORT_SYMBOL_GPL(videobuf_alloc_vb);
  64. static int state_neither_active_nor_queued(struct videobuf_queue *q,
  65. struct videobuf_buffer *vb)
  66. {
  67. unsigned long flags;
  68. bool rc;
  69. spin_lock_irqsave(q->irqlock, flags);
  70. rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED;
  71. spin_unlock_irqrestore(q->irqlock, flags);
  72. return rc;
  73. };
  74. int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
  75. int non_blocking, int intr)
  76. {
  77. bool is_ext_locked;
  78. int ret = 0;
  79. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  80. if (non_blocking) {
  81. if (state_neither_active_nor_queued(q, vb))
  82. return 0;
  83. return -EAGAIN;
  84. }
  85. is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock);
  86. /* Release vdev lock to prevent this wait from blocking outside access to
  87. the device. */
  88. if (is_ext_locked)
  89. mutex_unlock(q->ext_lock);
  90. if (intr)
  91. ret = wait_event_interruptible(vb->done,
  92. state_neither_active_nor_queued(q, vb));
  93. else
  94. wait_event(vb->done, state_neither_active_nor_queued(q, vb));
  95. /* Relock */
  96. if (is_ext_locked)
  97. mutex_lock(q->ext_lock);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(videobuf_waiton);
  101. int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
  102. struct v4l2_framebuffer *fbuf)
  103. {
  104. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  105. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  106. return CALL(q, iolock, q, vb, fbuf);
  107. }
  108. EXPORT_SYMBOL_GPL(videobuf_iolock);
  109. void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
  110. struct videobuf_buffer *buf)
  111. {
  112. if (q->int_ops->vaddr)
  113. return q->int_ops->vaddr(buf);
  114. return NULL;
  115. }
  116. EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
  117. /* --------------------------------------------------------------------- */
  118. void videobuf_queue_core_init(struct videobuf_queue *q,
  119. const struct videobuf_queue_ops *ops,
  120. struct device *dev,
  121. spinlock_t *irqlock,
  122. enum v4l2_buf_type type,
  123. enum v4l2_field field,
  124. unsigned int msize,
  125. void *priv,
  126. struct videobuf_qtype_ops *int_ops,
  127. struct mutex *ext_lock)
  128. {
  129. BUG_ON(!q);
  130. memset(q, 0, sizeof(*q));
  131. q->irqlock = irqlock;
  132. q->ext_lock = ext_lock;
  133. q->dev = dev;
  134. q->type = type;
  135. q->field = field;
  136. q->msize = msize;
  137. q->ops = ops;
  138. q->priv_data = priv;
  139. q->int_ops = int_ops;
  140. /* All buffer operations are mandatory */
  141. BUG_ON(!q->ops->buf_setup);
  142. BUG_ON(!q->ops->buf_prepare);
  143. BUG_ON(!q->ops->buf_queue);
  144. BUG_ON(!q->ops->buf_release);
  145. /* Lock is mandatory for queue_cancel to work */
  146. BUG_ON(!irqlock);
  147. /* Having implementations for abstract methods are mandatory */
  148. BUG_ON(!q->int_ops);
  149. mutex_init(&q->vb_lock);
  150. init_waitqueue_head(&q->wait);
  151. INIT_LIST_HEAD(&q->stream);
  152. }
  153. EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
  154. /* Locking: Only usage in bttv unsafe find way to remove */
  155. int videobuf_queue_is_busy(struct videobuf_queue *q)
  156. {
  157. int i;
  158. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  159. if (q->streaming) {
  160. dprintk(1, "busy: streaming active\n");
  161. return 1;
  162. }
  163. if (q->reading) {
  164. dprintk(1, "busy: pending read #1\n");
  165. return 1;
  166. }
  167. if (q->read_buf) {
  168. dprintk(1, "busy: pending read #2\n");
  169. return 1;
  170. }
  171. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  172. if (NULL == q->bufs[i])
  173. continue;
  174. if (q->bufs[i]->map) {
  175. dprintk(1, "busy: buffer #%d mapped\n", i);
  176. return 1;
  177. }
  178. if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
  179. dprintk(1, "busy: buffer #%d queued\n", i);
  180. return 1;
  181. }
  182. if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
  183. dprintk(1, "busy: buffer #%d avtive\n", i);
  184. return 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
  190. /**
  191. * __videobuf_free() - free all the buffers and their control structures
  192. *
  193. * This function can only be called if streaming/reading is off, i.e. no buffers
  194. * are under control of the driver.
  195. */
  196. /* Locking: Caller holds q->vb_lock */
  197. static int __videobuf_free(struct videobuf_queue *q)
  198. {
  199. int i;
  200. dprintk(1, "%s\n", __func__);
  201. if (!q)
  202. return 0;
  203. if (q->streaming || q->reading) {
  204. dprintk(1, "Cannot free buffers when streaming or reading\n");
  205. return -EBUSY;
  206. }
  207. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  208. for (i = 0; i < VIDEO_MAX_FRAME; i++)
  209. if (q->bufs[i] && q->bufs[i]->map) {
  210. dprintk(1, "Cannot free mmapped buffers\n");
  211. return -EBUSY;
  212. }
  213. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  214. if (NULL == q->bufs[i])
  215. continue;
  216. q->ops->buf_release(q, q->bufs[i]);
  217. kfree(q->bufs[i]);
  218. q->bufs[i] = NULL;
  219. }
  220. return 0;
  221. }
  222. /* Locking: Caller holds q->vb_lock */
  223. void videobuf_queue_cancel(struct videobuf_queue *q)
  224. {
  225. unsigned long flags = 0;
  226. int i;
  227. q->streaming = 0;
  228. q->reading = 0;
  229. wake_up_interruptible_sync(&q->wait);
  230. /* remove queued buffers from list */
  231. spin_lock_irqsave(q->irqlock, flags);
  232. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  233. if (NULL == q->bufs[i])
  234. continue;
  235. if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
  236. list_del(&q->bufs[i]->queue);
  237. q->bufs[i]->state = VIDEOBUF_ERROR;
  238. wake_up_all(&q->bufs[i]->done);
  239. }
  240. }
  241. spin_unlock_irqrestore(q->irqlock, flags);
  242. /* free all buffers + clear queue */
  243. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  244. if (NULL == q->bufs[i])
  245. continue;
  246. q->ops->buf_release(q, q->bufs[i]);
  247. }
  248. INIT_LIST_HEAD(&q->stream);
  249. }
  250. EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
  251. /* --------------------------------------------------------------------- */
  252. /* Locking: Caller holds q->vb_lock */
  253. enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
  254. {
  255. enum v4l2_field field = q->field;
  256. BUG_ON(V4L2_FIELD_ANY == field);
  257. if (V4L2_FIELD_ALTERNATE == field) {
  258. if (V4L2_FIELD_TOP == q->last) {
  259. field = V4L2_FIELD_BOTTOM;
  260. q->last = V4L2_FIELD_BOTTOM;
  261. } else {
  262. field = V4L2_FIELD_TOP;
  263. q->last = V4L2_FIELD_TOP;
  264. }
  265. }
  266. return field;
  267. }
  268. EXPORT_SYMBOL_GPL(videobuf_next_field);
  269. /* Locking: Caller holds q->vb_lock */
  270. static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
  271. struct videobuf_buffer *vb, enum v4l2_buf_type type)
  272. {
  273. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  274. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  275. b->index = vb->i;
  276. b->type = type;
  277. b->memory = vb->memory;
  278. switch (b->memory) {
  279. case V4L2_MEMORY_MMAP:
  280. b->m.offset = vb->boff;
  281. b->length = vb->bsize;
  282. break;
  283. case V4L2_MEMORY_USERPTR:
  284. b->m.userptr = vb->baddr;
  285. b->length = vb->bsize;
  286. break;
  287. case V4L2_MEMORY_OVERLAY:
  288. b->m.offset = vb->boff;
  289. break;
  290. case V4L2_MEMORY_DMABUF:
  291. /* DMABUF is not handled in videobuf framework */
  292. break;
  293. }
  294. b->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  295. if (vb->map)
  296. b->flags |= V4L2_BUF_FLAG_MAPPED;
  297. switch (vb->state) {
  298. case VIDEOBUF_PREPARED:
  299. case VIDEOBUF_QUEUED:
  300. case VIDEOBUF_ACTIVE:
  301. b->flags |= V4L2_BUF_FLAG_QUEUED;
  302. break;
  303. case VIDEOBUF_ERROR:
  304. b->flags |= V4L2_BUF_FLAG_ERROR;
  305. /* fall through */
  306. case VIDEOBUF_DONE:
  307. b->flags |= V4L2_BUF_FLAG_DONE;
  308. break;
  309. case VIDEOBUF_NEEDS_INIT:
  310. case VIDEOBUF_IDLE:
  311. /* nothing */
  312. break;
  313. }
  314. b->field = vb->field;
  315. b->timestamp = vb->ts;
  316. b->bytesused = vb->size;
  317. b->sequence = vb->field_count >> 1;
  318. }
  319. int videobuf_mmap_free(struct videobuf_queue *q)
  320. {
  321. int ret;
  322. videobuf_queue_lock(q);
  323. ret = __videobuf_free(q);
  324. videobuf_queue_unlock(q);
  325. return ret;
  326. }
  327. EXPORT_SYMBOL_GPL(videobuf_mmap_free);
  328. /* Locking: Caller holds q->vb_lock */
  329. int __videobuf_mmap_setup(struct videobuf_queue *q,
  330. unsigned int bcount, unsigned int bsize,
  331. enum v4l2_memory memory)
  332. {
  333. unsigned int i;
  334. int err;
  335. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  336. err = __videobuf_free(q);
  337. if (0 != err)
  338. return err;
  339. /* Allocate and initialize buffers */
  340. for (i = 0; i < bcount; i++) {
  341. q->bufs[i] = videobuf_alloc_vb(q);
  342. if (NULL == q->bufs[i])
  343. break;
  344. q->bufs[i]->i = i;
  345. q->bufs[i]->memory = memory;
  346. q->bufs[i]->bsize = bsize;
  347. switch (memory) {
  348. case V4L2_MEMORY_MMAP:
  349. q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
  350. break;
  351. case V4L2_MEMORY_USERPTR:
  352. case V4L2_MEMORY_OVERLAY:
  353. case V4L2_MEMORY_DMABUF:
  354. /* nothing */
  355. break;
  356. }
  357. }
  358. if (!i)
  359. return -ENOMEM;
  360. dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
  361. return i;
  362. }
  363. EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
  364. int videobuf_mmap_setup(struct videobuf_queue *q,
  365. unsigned int bcount, unsigned int bsize,
  366. enum v4l2_memory memory)
  367. {
  368. int ret;
  369. videobuf_queue_lock(q);
  370. ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
  371. videobuf_queue_unlock(q);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
  375. int videobuf_reqbufs(struct videobuf_queue *q,
  376. struct v4l2_requestbuffers *req)
  377. {
  378. unsigned int size, count;
  379. int retval;
  380. if (req->memory != V4L2_MEMORY_MMAP &&
  381. req->memory != V4L2_MEMORY_USERPTR &&
  382. req->memory != V4L2_MEMORY_OVERLAY) {
  383. dprintk(1, "reqbufs: memory type invalid\n");
  384. return -EINVAL;
  385. }
  386. videobuf_queue_lock(q);
  387. if (req->type != q->type) {
  388. dprintk(1, "reqbufs: queue type invalid\n");
  389. retval = -EINVAL;
  390. goto done;
  391. }
  392. if (q->streaming) {
  393. dprintk(1, "reqbufs: streaming already exists\n");
  394. retval = -EBUSY;
  395. goto done;
  396. }
  397. if (!list_empty(&q->stream)) {
  398. dprintk(1, "reqbufs: stream running\n");
  399. retval = -EBUSY;
  400. goto done;
  401. }
  402. if (req->count == 0) {
  403. dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
  404. retval = __videobuf_free(q);
  405. goto done;
  406. }
  407. count = req->count;
  408. if (count > VIDEO_MAX_FRAME)
  409. count = VIDEO_MAX_FRAME;
  410. size = 0;
  411. q->ops->buf_setup(q, &count, &size);
  412. dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
  413. count, size,
  414. (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
  415. retval = __videobuf_mmap_setup(q, count, size, req->memory);
  416. if (retval < 0) {
  417. dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
  418. goto done;
  419. }
  420. req->count = retval;
  421. retval = 0;
  422. done:
  423. videobuf_queue_unlock(q);
  424. return retval;
  425. }
  426. EXPORT_SYMBOL_GPL(videobuf_reqbufs);
  427. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
  428. {
  429. int ret = -EINVAL;
  430. videobuf_queue_lock(q);
  431. if (unlikely(b->type != q->type)) {
  432. dprintk(1, "querybuf: Wrong type.\n");
  433. goto done;
  434. }
  435. if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
  436. dprintk(1, "querybuf: index out of range.\n");
  437. goto done;
  438. }
  439. if (unlikely(NULL == q->bufs[b->index])) {
  440. dprintk(1, "querybuf: buffer is null.\n");
  441. goto done;
  442. }
  443. videobuf_status(q, b, q->bufs[b->index], q->type);
  444. ret = 0;
  445. done:
  446. videobuf_queue_unlock(q);
  447. return ret;
  448. }
  449. EXPORT_SYMBOL_GPL(videobuf_querybuf);
  450. int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
  451. {
  452. struct videobuf_buffer *buf;
  453. enum v4l2_field field;
  454. unsigned long flags = 0;
  455. int retval;
  456. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  457. if (b->memory == V4L2_MEMORY_MMAP)
  458. down_read(&current->mm->mmap_sem);
  459. videobuf_queue_lock(q);
  460. retval = -EBUSY;
  461. if (q->reading) {
  462. dprintk(1, "qbuf: Reading running...\n");
  463. goto done;
  464. }
  465. retval = -EINVAL;
  466. if (b->type != q->type) {
  467. dprintk(1, "qbuf: Wrong type.\n");
  468. goto done;
  469. }
  470. if (b->index >= VIDEO_MAX_FRAME) {
  471. dprintk(1, "qbuf: index out of range.\n");
  472. goto done;
  473. }
  474. buf = q->bufs[b->index];
  475. if (NULL == buf) {
  476. dprintk(1, "qbuf: buffer is null.\n");
  477. goto done;
  478. }
  479. MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
  480. if (buf->memory != b->memory) {
  481. dprintk(1, "qbuf: memory type is wrong.\n");
  482. goto done;
  483. }
  484. if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
  485. dprintk(1, "qbuf: buffer is already queued or active.\n");
  486. goto done;
  487. }
  488. switch (b->memory) {
  489. case V4L2_MEMORY_MMAP:
  490. if (0 == buf->baddr) {
  491. dprintk(1, "qbuf: mmap requested "
  492. "but buffer addr is zero!\n");
  493. goto done;
  494. }
  495. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
  496. || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
  497. || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
  498. || q->type == V4L2_BUF_TYPE_SDR_OUTPUT) {
  499. buf->size = b->bytesused;
  500. buf->field = b->field;
  501. buf->ts = b->timestamp;
  502. }
  503. break;
  504. case V4L2_MEMORY_USERPTR:
  505. if (b->length < buf->bsize) {
  506. dprintk(1, "qbuf: buffer length is not enough\n");
  507. goto done;
  508. }
  509. if (VIDEOBUF_NEEDS_INIT != buf->state &&
  510. buf->baddr != b->m.userptr)
  511. q->ops->buf_release(q, buf);
  512. buf->baddr = b->m.userptr;
  513. break;
  514. case V4L2_MEMORY_OVERLAY:
  515. buf->boff = b->m.offset;
  516. break;
  517. default:
  518. dprintk(1, "qbuf: wrong memory type\n");
  519. goto done;
  520. }
  521. dprintk(1, "qbuf: requesting next field\n");
  522. field = videobuf_next_field(q);
  523. retval = q->ops->buf_prepare(q, buf, field);
  524. if (0 != retval) {
  525. dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
  526. goto done;
  527. }
  528. list_add_tail(&buf->stream, &q->stream);
  529. if (q->streaming) {
  530. spin_lock_irqsave(q->irqlock, flags);
  531. q->ops->buf_queue(q, buf);
  532. spin_unlock_irqrestore(q->irqlock, flags);
  533. }
  534. dprintk(1, "qbuf: succeeded\n");
  535. retval = 0;
  536. wake_up_interruptible_sync(&q->wait);
  537. done:
  538. videobuf_queue_unlock(q);
  539. if (b->memory == V4L2_MEMORY_MMAP)
  540. up_read(&current->mm->mmap_sem);
  541. return retval;
  542. }
  543. EXPORT_SYMBOL_GPL(videobuf_qbuf);
  544. /* Locking: Caller holds q->vb_lock */
  545. static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
  546. {
  547. int retval;
  548. checks:
  549. if (!q->streaming) {
  550. dprintk(1, "next_buffer: Not streaming\n");
  551. retval = -EINVAL;
  552. goto done;
  553. }
  554. if (list_empty(&q->stream)) {
  555. if (noblock) {
  556. retval = -EAGAIN;
  557. dprintk(2, "next_buffer: no buffers to dequeue\n");
  558. goto done;
  559. } else {
  560. dprintk(2, "next_buffer: waiting on buffer\n");
  561. /* Drop lock to avoid deadlock with qbuf */
  562. videobuf_queue_unlock(q);
  563. /* Checking list_empty and streaming is safe without
  564. * locks because we goto checks to validate while
  565. * holding locks before proceeding */
  566. retval = wait_event_interruptible(q->wait,
  567. !list_empty(&q->stream) || !q->streaming);
  568. videobuf_queue_lock(q);
  569. if (retval)
  570. goto done;
  571. goto checks;
  572. }
  573. }
  574. retval = 0;
  575. done:
  576. return retval;
  577. }
  578. /* Locking: Caller holds q->vb_lock */
  579. static int stream_next_buffer(struct videobuf_queue *q,
  580. struct videobuf_buffer **vb, int nonblocking)
  581. {
  582. int retval;
  583. struct videobuf_buffer *buf = NULL;
  584. retval = stream_next_buffer_check_queue(q, nonblocking);
  585. if (retval)
  586. goto done;
  587. buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
  588. retval = videobuf_waiton(q, buf, nonblocking, 1);
  589. if (retval < 0)
  590. goto done;
  591. *vb = buf;
  592. done:
  593. return retval;
  594. }
  595. int videobuf_dqbuf(struct videobuf_queue *q,
  596. struct v4l2_buffer *b, int nonblocking)
  597. {
  598. struct videobuf_buffer *buf = NULL;
  599. int retval;
  600. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  601. memset(b, 0, sizeof(*b));
  602. videobuf_queue_lock(q);
  603. retval = stream_next_buffer(q, &buf, nonblocking);
  604. if (retval < 0) {
  605. dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
  606. goto done;
  607. }
  608. switch (buf->state) {
  609. case VIDEOBUF_ERROR:
  610. dprintk(1, "dqbuf: state is error\n");
  611. break;
  612. case VIDEOBUF_DONE:
  613. dprintk(1, "dqbuf: state is done\n");
  614. break;
  615. default:
  616. dprintk(1, "dqbuf: state invalid\n");
  617. retval = -EINVAL;
  618. goto done;
  619. }
  620. CALL(q, sync, q, buf);
  621. videobuf_status(q, b, buf, q->type);
  622. list_del(&buf->stream);
  623. buf->state = VIDEOBUF_IDLE;
  624. b->flags &= ~V4L2_BUF_FLAG_DONE;
  625. done:
  626. videobuf_queue_unlock(q);
  627. return retval;
  628. }
  629. EXPORT_SYMBOL_GPL(videobuf_dqbuf);
  630. int videobuf_streamon(struct videobuf_queue *q)
  631. {
  632. struct videobuf_buffer *buf;
  633. unsigned long flags = 0;
  634. int retval;
  635. videobuf_queue_lock(q);
  636. retval = -EBUSY;
  637. if (q->reading)
  638. goto done;
  639. retval = 0;
  640. if (q->streaming)
  641. goto done;
  642. q->streaming = 1;
  643. spin_lock_irqsave(q->irqlock, flags);
  644. list_for_each_entry(buf, &q->stream, stream)
  645. if (buf->state == VIDEOBUF_PREPARED)
  646. q->ops->buf_queue(q, buf);
  647. spin_unlock_irqrestore(q->irqlock, flags);
  648. wake_up_interruptible_sync(&q->wait);
  649. done:
  650. videobuf_queue_unlock(q);
  651. return retval;
  652. }
  653. EXPORT_SYMBOL_GPL(videobuf_streamon);
  654. /* Locking: Caller holds q->vb_lock */
  655. static int __videobuf_streamoff(struct videobuf_queue *q)
  656. {
  657. if (!q->streaming)
  658. return -EINVAL;
  659. videobuf_queue_cancel(q);
  660. return 0;
  661. }
  662. int videobuf_streamoff(struct videobuf_queue *q)
  663. {
  664. int retval;
  665. videobuf_queue_lock(q);
  666. retval = __videobuf_streamoff(q);
  667. videobuf_queue_unlock(q);
  668. return retval;
  669. }
  670. EXPORT_SYMBOL_GPL(videobuf_streamoff);
  671. /* Locking: Caller holds q->vb_lock */
  672. static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
  673. char __user *data,
  674. size_t count, loff_t *ppos)
  675. {
  676. enum v4l2_field field;
  677. unsigned long flags = 0;
  678. int retval;
  679. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  680. /* setup stuff */
  681. q->read_buf = videobuf_alloc_vb(q);
  682. if (NULL == q->read_buf)
  683. return -ENOMEM;
  684. q->read_buf->memory = V4L2_MEMORY_USERPTR;
  685. q->read_buf->baddr = (unsigned long)data;
  686. q->read_buf->bsize = count;
  687. field = videobuf_next_field(q);
  688. retval = q->ops->buf_prepare(q, q->read_buf, field);
  689. if (0 != retval)
  690. goto done;
  691. /* start capture & wait */
  692. spin_lock_irqsave(q->irqlock, flags);
  693. q->ops->buf_queue(q, q->read_buf);
  694. spin_unlock_irqrestore(q->irqlock, flags);
  695. retval = videobuf_waiton(q, q->read_buf, 0, 0);
  696. if (0 == retval) {
  697. CALL(q, sync, q, q->read_buf);
  698. if (VIDEOBUF_ERROR == q->read_buf->state)
  699. retval = -EIO;
  700. else
  701. retval = q->read_buf->size;
  702. }
  703. done:
  704. /* cleanup */
  705. q->ops->buf_release(q, q->read_buf);
  706. kfree(q->read_buf);
  707. q->read_buf = NULL;
  708. return retval;
  709. }
  710. static int __videobuf_copy_to_user(struct videobuf_queue *q,
  711. struct videobuf_buffer *buf,
  712. char __user *data, size_t count,
  713. int nonblocking)
  714. {
  715. void *vaddr = CALLPTR(q, vaddr, buf);
  716. /* copy to userspace */
  717. if (count > buf->size - q->read_off)
  718. count = buf->size - q->read_off;
  719. if (copy_to_user(data, vaddr + q->read_off, count))
  720. return -EFAULT;
  721. return count;
  722. }
  723. static int __videobuf_copy_stream(struct videobuf_queue *q,
  724. struct videobuf_buffer *buf,
  725. char __user *data, size_t count, size_t pos,
  726. int vbihack, int nonblocking)
  727. {
  728. unsigned int *fc = CALLPTR(q, vaddr, buf);
  729. if (vbihack) {
  730. /* dirty, undocumented hack -- pass the frame counter
  731. * within the last four bytes of each vbi data block.
  732. * We need that one to maintain backward compatibility
  733. * to all vbi decoding software out there ... */
  734. fc += (buf->size >> 2) - 1;
  735. *fc = buf->field_count >> 1;
  736. dprintk(1, "vbihack: %d\n", *fc);
  737. }
  738. /* copy stuff using the common method */
  739. count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
  740. if ((count == -EFAULT) && (pos == 0))
  741. return -EFAULT;
  742. return count;
  743. }
  744. ssize_t videobuf_read_one(struct videobuf_queue *q,
  745. char __user *data, size_t count, loff_t *ppos,
  746. int nonblocking)
  747. {
  748. enum v4l2_field field;
  749. unsigned long flags = 0;
  750. unsigned size = 0, nbufs = 1;
  751. int retval;
  752. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  753. videobuf_queue_lock(q);
  754. q->ops->buf_setup(q, &nbufs, &size);
  755. if (NULL == q->read_buf &&
  756. count >= size &&
  757. !nonblocking) {
  758. retval = videobuf_read_zerocopy(q, data, count, ppos);
  759. if (retval >= 0 || retval == -EIO)
  760. /* ok, all done */
  761. goto done;
  762. /* fallback to kernel bounce buffer on failures */
  763. }
  764. if (NULL == q->read_buf) {
  765. /* need to capture a new frame */
  766. retval = -ENOMEM;
  767. q->read_buf = videobuf_alloc_vb(q);
  768. dprintk(1, "video alloc=0x%p\n", q->read_buf);
  769. if (NULL == q->read_buf)
  770. goto done;
  771. q->read_buf->memory = V4L2_MEMORY_USERPTR;
  772. q->read_buf->bsize = count; /* preferred size */
  773. field = videobuf_next_field(q);
  774. retval = q->ops->buf_prepare(q, q->read_buf, field);
  775. if (0 != retval) {
  776. kfree(q->read_buf);
  777. q->read_buf = NULL;
  778. goto done;
  779. }
  780. spin_lock_irqsave(q->irqlock, flags);
  781. q->ops->buf_queue(q, q->read_buf);
  782. spin_unlock_irqrestore(q->irqlock, flags);
  783. q->read_off = 0;
  784. }
  785. /* wait until capture is done */
  786. retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
  787. if (0 != retval)
  788. goto done;
  789. CALL(q, sync, q, q->read_buf);
  790. if (VIDEOBUF_ERROR == q->read_buf->state) {
  791. /* catch I/O errors */
  792. q->ops->buf_release(q, q->read_buf);
  793. kfree(q->read_buf);
  794. q->read_buf = NULL;
  795. retval = -EIO;
  796. goto done;
  797. }
  798. /* Copy to userspace */
  799. retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
  800. if (retval < 0)
  801. goto done;
  802. q->read_off += retval;
  803. if (q->read_off == q->read_buf->size) {
  804. /* all data copied, cleanup */
  805. q->ops->buf_release(q, q->read_buf);
  806. kfree(q->read_buf);
  807. q->read_buf = NULL;
  808. }
  809. done:
  810. videobuf_queue_unlock(q);
  811. return retval;
  812. }
  813. EXPORT_SYMBOL_GPL(videobuf_read_one);
  814. /* Locking: Caller holds q->vb_lock */
  815. static int __videobuf_read_start(struct videobuf_queue *q)
  816. {
  817. enum v4l2_field field;
  818. unsigned long flags = 0;
  819. unsigned int count = 0, size = 0;
  820. int err, i;
  821. q->ops->buf_setup(q, &count, &size);
  822. if (count < 2)
  823. count = 2;
  824. if (count > VIDEO_MAX_FRAME)
  825. count = VIDEO_MAX_FRAME;
  826. size = PAGE_ALIGN(size);
  827. err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
  828. if (err < 0)
  829. return err;
  830. count = err;
  831. for (i = 0; i < count; i++) {
  832. field = videobuf_next_field(q);
  833. err = q->ops->buf_prepare(q, q->bufs[i], field);
  834. if (err)
  835. return err;
  836. list_add_tail(&q->bufs[i]->stream, &q->stream);
  837. }
  838. spin_lock_irqsave(q->irqlock, flags);
  839. for (i = 0; i < count; i++)
  840. q->ops->buf_queue(q, q->bufs[i]);
  841. spin_unlock_irqrestore(q->irqlock, flags);
  842. q->reading = 1;
  843. return 0;
  844. }
  845. static void __videobuf_read_stop(struct videobuf_queue *q)
  846. {
  847. int i;
  848. videobuf_queue_cancel(q);
  849. __videobuf_free(q);
  850. INIT_LIST_HEAD(&q->stream);
  851. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  852. if (NULL == q->bufs[i])
  853. continue;
  854. kfree(q->bufs[i]);
  855. q->bufs[i] = NULL;
  856. }
  857. q->read_buf = NULL;
  858. }
  859. int videobuf_read_start(struct videobuf_queue *q)
  860. {
  861. int rc;
  862. videobuf_queue_lock(q);
  863. rc = __videobuf_read_start(q);
  864. videobuf_queue_unlock(q);
  865. return rc;
  866. }
  867. EXPORT_SYMBOL_GPL(videobuf_read_start);
  868. void videobuf_read_stop(struct videobuf_queue *q)
  869. {
  870. videobuf_queue_lock(q);
  871. __videobuf_read_stop(q);
  872. videobuf_queue_unlock(q);
  873. }
  874. EXPORT_SYMBOL_GPL(videobuf_read_stop);
  875. void videobuf_stop(struct videobuf_queue *q)
  876. {
  877. videobuf_queue_lock(q);
  878. if (q->streaming)
  879. __videobuf_streamoff(q);
  880. if (q->reading)
  881. __videobuf_read_stop(q);
  882. videobuf_queue_unlock(q);
  883. }
  884. EXPORT_SYMBOL_GPL(videobuf_stop);
  885. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  886. char __user *data, size_t count, loff_t *ppos,
  887. int vbihack, int nonblocking)
  888. {
  889. int rc, retval;
  890. unsigned long flags = 0;
  891. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  892. dprintk(2, "%s\n", __func__);
  893. videobuf_queue_lock(q);
  894. retval = -EBUSY;
  895. if (q->streaming)
  896. goto done;
  897. if (!q->reading) {
  898. retval = __videobuf_read_start(q);
  899. if (retval < 0)
  900. goto done;
  901. }
  902. retval = 0;
  903. while (count > 0) {
  904. /* get / wait for data */
  905. if (NULL == q->read_buf) {
  906. q->read_buf = list_entry(q->stream.next,
  907. struct videobuf_buffer,
  908. stream);
  909. list_del(&q->read_buf->stream);
  910. q->read_off = 0;
  911. }
  912. rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
  913. if (rc < 0) {
  914. if (0 == retval)
  915. retval = rc;
  916. break;
  917. }
  918. if (q->read_buf->state == VIDEOBUF_DONE) {
  919. rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
  920. retval, vbihack, nonblocking);
  921. if (rc < 0) {
  922. retval = rc;
  923. break;
  924. }
  925. retval += rc;
  926. count -= rc;
  927. q->read_off += rc;
  928. } else {
  929. /* some error */
  930. q->read_off = q->read_buf->size;
  931. if (0 == retval)
  932. retval = -EIO;
  933. }
  934. /* requeue buffer when done with copying */
  935. if (q->read_off == q->read_buf->size) {
  936. list_add_tail(&q->read_buf->stream,
  937. &q->stream);
  938. spin_lock_irqsave(q->irqlock, flags);
  939. q->ops->buf_queue(q, q->read_buf);
  940. spin_unlock_irqrestore(q->irqlock, flags);
  941. q->read_buf = NULL;
  942. }
  943. if (retval < 0)
  944. break;
  945. }
  946. done:
  947. videobuf_queue_unlock(q);
  948. return retval;
  949. }
  950. EXPORT_SYMBOL_GPL(videobuf_read_stream);
  951. unsigned int videobuf_poll_stream(struct file *file,
  952. struct videobuf_queue *q,
  953. poll_table *wait)
  954. {
  955. unsigned long req_events = poll_requested_events(wait);
  956. struct videobuf_buffer *buf = NULL;
  957. unsigned int rc = 0;
  958. videobuf_queue_lock(q);
  959. if (q->streaming) {
  960. if (!list_empty(&q->stream))
  961. buf = list_entry(q->stream.next,
  962. struct videobuf_buffer, stream);
  963. } else if (req_events & (POLLIN | POLLRDNORM)) {
  964. if (!q->reading)
  965. __videobuf_read_start(q);
  966. if (!q->reading) {
  967. rc = POLLERR;
  968. } else if (NULL == q->read_buf) {
  969. q->read_buf = list_entry(q->stream.next,
  970. struct videobuf_buffer,
  971. stream);
  972. list_del(&q->read_buf->stream);
  973. q->read_off = 0;
  974. }
  975. buf = q->read_buf;
  976. }
  977. if (!buf)
  978. rc = POLLERR;
  979. if (0 == rc) {
  980. poll_wait(file, &buf->done, wait);
  981. if (buf->state == VIDEOBUF_DONE ||
  982. buf->state == VIDEOBUF_ERROR) {
  983. switch (q->type) {
  984. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  985. case V4L2_BUF_TYPE_VBI_OUTPUT:
  986. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  987. case V4L2_BUF_TYPE_SDR_OUTPUT:
  988. rc = POLLOUT | POLLWRNORM;
  989. break;
  990. default:
  991. rc = POLLIN | POLLRDNORM;
  992. break;
  993. }
  994. }
  995. }
  996. videobuf_queue_unlock(q);
  997. return rc;
  998. }
  999. EXPORT_SYMBOL_GPL(videobuf_poll_stream);
  1000. int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
  1001. {
  1002. int rc = -EINVAL;
  1003. int i;
  1004. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  1005. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
  1006. dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
  1007. return -EINVAL;
  1008. }
  1009. videobuf_queue_lock(q);
  1010. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1011. struct videobuf_buffer *buf = q->bufs[i];
  1012. if (buf && buf->memory == V4L2_MEMORY_MMAP &&
  1013. buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
  1014. rc = CALL(q, mmap_mapper, q, buf, vma);
  1015. break;
  1016. }
  1017. }
  1018. videobuf_queue_unlock(q);
  1019. return rc;
  1020. }
  1021. EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);