videobuf-core.c 24 KB

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