videobuf-core.c 24 KB

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