videobuf-core.c 27 KB

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