videobuf-core.c 24 KB

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