videobuf-core.c 24 KB

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