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/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <media/videobuf-core.h>
  22. #define MAGIC_BUFFER 0x20070728
  23. #define MAGIC_CHECK(is, should) do { \
  24. if (unlikely((is) != (should))) { \
  25. printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
  26. BUG(); } } while (0)
  27. static int debug;
  28. module_param(debug, int, 0644);
  29. MODULE_DESCRIPTION("helper module to manage video4linux buffers");
  30. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  31. MODULE_LICENSE("GPL");
  32. #define dprintk(level, fmt, arg...) do { \
  33. if (debug >= level) \
  34. printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
  35. /* --------------------------------------------------------------------- */
  36. #define CALL(q, f, arg...) \
  37. ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
  38. void *videobuf_alloc(struct videobuf_queue *q)
  39. {
  40. struct videobuf_buffer *vb;
  41. BUG_ON(q->msize < sizeof(*vb));
  42. if (!q->int_ops || !q->int_ops->alloc) {
  43. printk(KERN_ERR "No specific ops defined!\n");
  44. BUG();
  45. }
  46. vb = q->int_ops->alloc(q->msize);
  47. if (NULL != vb) {
  48. init_waitqueue_head(&vb->done);
  49. vb->magic = MAGIC_BUFFER;
  50. }
  51. return vb;
  52. }
  53. #define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
  54. vb->state != VIDEOBUF_QUEUED)
  55. int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
  56. {
  57. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  58. if (non_blocking) {
  59. if (WAITON_CONDITION)
  60. return 0;
  61. else
  62. return -EAGAIN;
  63. }
  64. if (intr)
  65. return wait_event_interruptible(vb->done, WAITON_CONDITION);
  66. else
  67. wait_event(vb->done, WAITON_CONDITION);
  68. return 0;
  69. }
  70. int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
  71. struct v4l2_framebuffer *fbuf)
  72. {
  73. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  74. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  75. return CALL(q, iolock, q, vb, fbuf);
  76. }
  77. void *videobuf_queue_to_vmalloc (struct videobuf_queue *q,
  78. struct videobuf_buffer *buf)
  79. {
  80. if (q->int_ops->vmalloc)
  81. return q->int_ops->vmalloc(buf);
  82. else
  83. return NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
  86. /* --------------------------------------------------------------------- */
  87. void videobuf_queue_core_init(struct videobuf_queue *q,
  88. struct videobuf_queue_ops *ops,
  89. struct device *dev,
  90. spinlock_t *irqlock,
  91. enum v4l2_buf_type type,
  92. enum v4l2_field field,
  93. unsigned int msize,
  94. void *priv,
  95. struct videobuf_qtype_ops *int_ops)
  96. {
  97. BUG_ON(!q);
  98. memset(q, 0, sizeof(*q));
  99. q->irqlock = irqlock;
  100. q->dev = dev;
  101. q->type = type;
  102. q->field = field;
  103. q->msize = msize;
  104. q->ops = ops;
  105. q->priv_data = priv;
  106. q->int_ops = int_ops;
  107. /* All buffer operations are mandatory */
  108. BUG_ON(!q->ops->buf_setup);
  109. BUG_ON(!q->ops->buf_prepare);
  110. BUG_ON(!q->ops->buf_queue);
  111. BUG_ON(!q->ops->buf_release);
  112. /* Lock is mandatory for queue_cancel to work */
  113. BUG_ON(!irqlock);
  114. /* Having implementations for abstract methods are mandatory */
  115. BUG_ON(!q->int_ops);
  116. mutex_init(&q->vb_lock);
  117. init_waitqueue_head(&q->wait);
  118. INIT_LIST_HEAD(&q->stream);
  119. }
  120. /* Locking: Only usage in bttv unsafe find way to remove */
  121. int videobuf_queue_is_busy(struct videobuf_queue *q)
  122. {
  123. int i;
  124. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  125. if (q->streaming) {
  126. dprintk(1, "busy: streaming active\n");
  127. return 1;
  128. }
  129. if (q->reading) {
  130. dprintk(1, "busy: pending read #1\n");
  131. return 1;
  132. }
  133. if (q->read_buf) {
  134. dprintk(1, "busy: pending read #2\n");
  135. return 1;
  136. }
  137. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  138. if (NULL == q->bufs[i])
  139. continue;
  140. if (q->bufs[i]->map) {
  141. dprintk(1, "busy: buffer #%d mapped\n", i);
  142. return 1;
  143. }
  144. if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
  145. dprintk(1, "busy: buffer #%d queued\n", i);
  146. return 1;
  147. }
  148. if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
  149. dprintk(1, "busy: buffer #%d avtive\n", i);
  150. return 1;
  151. }
  152. }
  153. return 0;
  154. }
  155. /* Locking: Caller holds q->vb_lock */
  156. void videobuf_queue_cancel(struct videobuf_queue *q)
  157. {
  158. unsigned long flags = 0;
  159. int i;
  160. q->streaming = 0;
  161. q->reading = 0;
  162. wake_up_interruptible_sync(&q->wait);
  163. /* remove queued buffers from list */
  164. spin_lock_irqsave(q->irqlock, flags);
  165. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  166. if (NULL == q->bufs[i])
  167. continue;
  168. if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
  169. list_del(&q->bufs[i]->queue);
  170. q->bufs[i]->state = VIDEOBUF_ERROR;
  171. wake_up_all(&q->bufs[i]->done);
  172. }
  173. }
  174. spin_unlock_irqrestore(q->irqlock, flags);
  175. /* free all buffers + clear queue */
  176. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  177. if (NULL == q->bufs[i])
  178. continue;
  179. q->ops->buf_release(q, q->bufs[i]);
  180. }
  181. INIT_LIST_HEAD(&q->stream);
  182. }
  183. /* --------------------------------------------------------------------- */
  184. /* Locking: Caller holds q->vb_lock */
  185. enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
  186. {
  187. enum v4l2_field field = q->field;
  188. BUG_ON(V4L2_FIELD_ANY == field);
  189. if (V4L2_FIELD_ALTERNATE == field) {
  190. if (V4L2_FIELD_TOP == q->last) {
  191. field = V4L2_FIELD_BOTTOM;
  192. q->last = V4L2_FIELD_BOTTOM;
  193. } else {
  194. field = V4L2_FIELD_TOP;
  195. q->last = V4L2_FIELD_TOP;
  196. }
  197. }
  198. return field;
  199. }
  200. /* Locking: Caller holds q->vb_lock */
  201. static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
  202. struct videobuf_buffer *vb, enum v4l2_buf_type type)
  203. {
  204. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  205. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  206. b->index = vb->i;
  207. b->type = type;
  208. b->memory = vb->memory;
  209. switch (b->memory) {
  210. case V4L2_MEMORY_MMAP:
  211. b->m.offset = vb->boff;
  212. b->length = vb->bsize;
  213. break;
  214. case V4L2_MEMORY_USERPTR:
  215. b->m.userptr = vb->baddr;
  216. b->length = vb->bsize;
  217. break;
  218. case V4L2_MEMORY_OVERLAY:
  219. b->m.offset = vb->boff;
  220. break;
  221. }
  222. b->flags = 0;
  223. if (vb->map)
  224. b->flags |= V4L2_BUF_FLAG_MAPPED;
  225. switch (vb->state) {
  226. case VIDEOBUF_PREPARED:
  227. case VIDEOBUF_QUEUED:
  228. case VIDEOBUF_ACTIVE:
  229. b->flags |= V4L2_BUF_FLAG_QUEUED;
  230. break;
  231. case VIDEOBUF_DONE:
  232. case VIDEOBUF_ERROR:
  233. b->flags |= V4L2_BUF_FLAG_DONE;
  234. break;
  235. case VIDEOBUF_NEEDS_INIT:
  236. case VIDEOBUF_IDLE:
  237. /* nothing */
  238. break;
  239. }
  240. if (vb->input != UNSET) {
  241. b->flags |= V4L2_BUF_FLAG_INPUT;
  242. b->input = vb->input;
  243. }
  244. b->field = vb->field;
  245. b->timestamp = vb->ts;
  246. b->bytesused = vb->size;
  247. b->sequence = vb->field_count >> 1;
  248. }
  249. /* Locking: Caller holds q->vb_lock */
  250. static int __videobuf_mmap_free(struct videobuf_queue *q)
  251. {
  252. int i;
  253. int rc;
  254. if (!q)
  255. return 0;
  256. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  257. rc = CALL(q, mmap_free, q);
  258. q->is_mmapped = 0;
  259. if (rc < 0)
  260. return rc;
  261. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  262. if (NULL == q->bufs[i])
  263. continue;
  264. q->ops->buf_release(q, q->bufs[i]);
  265. kfree(q->bufs[i]);
  266. q->bufs[i] = NULL;
  267. }
  268. return rc;
  269. }
  270. int videobuf_mmap_free(struct videobuf_queue *q)
  271. {
  272. int ret;
  273. mutex_lock(&q->vb_lock);
  274. ret = __videobuf_mmap_free(q);
  275. mutex_unlock(&q->vb_lock);
  276. return ret;
  277. }
  278. /* Locking: Caller holds q->vb_lock */
  279. int __videobuf_mmap_setup(struct videobuf_queue *q,
  280. unsigned int bcount, unsigned int bsize,
  281. enum v4l2_memory memory)
  282. {
  283. unsigned int i;
  284. int err;
  285. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  286. err = __videobuf_mmap_free(q);
  287. if (0 != err)
  288. return err;
  289. /* Allocate and initialize buffers */
  290. for (i = 0; i < bcount; i++) {
  291. q->bufs[i] = videobuf_alloc(q);
  292. if (q->bufs[i] == NULL)
  293. break;
  294. q->bufs[i]->i = i;
  295. q->bufs[i]->input = UNSET;
  296. q->bufs[i]->memory = memory;
  297. q->bufs[i]->bsize = bsize;
  298. switch (memory) {
  299. case V4L2_MEMORY_MMAP:
  300. q->bufs[i]->boff = bsize * i;
  301. break;
  302. case V4L2_MEMORY_USERPTR:
  303. case V4L2_MEMORY_OVERLAY:
  304. /* nothing */
  305. break;
  306. }
  307. }
  308. if (!i)
  309. return -ENOMEM;
  310. dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
  311. i, bsize);
  312. return i;
  313. }
  314. int videobuf_mmap_setup(struct videobuf_queue *q,
  315. unsigned int bcount, unsigned int bsize,
  316. enum v4l2_memory memory)
  317. {
  318. int ret;
  319. mutex_lock(&q->vb_lock);
  320. ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
  321. mutex_unlock(&q->vb_lock);
  322. return ret;
  323. }
  324. int videobuf_reqbufs(struct videobuf_queue *q,
  325. struct v4l2_requestbuffers *req)
  326. {
  327. unsigned int size, count;
  328. int retval;
  329. if (req->count < 1) {
  330. dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
  331. return -EINVAL;
  332. }
  333. if (req->memory != V4L2_MEMORY_MMAP &&
  334. req->memory != V4L2_MEMORY_USERPTR &&
  335. req->memory != V4L2_MEMORY_OVERLAY) {
  336. dprintk(1, "reqbufs: memory type invalid\n");
  337. return -EINVAL;
  338. }
  339. mutex_lock(&q->vb_lock);
  340. if (req->type != q->type) {
  341. dprintk(1, "reqbufs: queue type invalid\n");
  342. retval = -EINVAL;
  343. goto done;
  344. }
  345. if (q->streaming) {
  346. dprintk(1, "reqbufs: streaming already exists\n");
  347. retval = -EBUSY;
  348. goto done;
  349. }
  350. if (!list_empty(&q->stream)) {
  351. dprintk(1, "reqbufs: stream running\n");
  352. retval = -EBUSY;
  353. goto done;
  354. }
  355. count = req->count;
  356. if (count > VIDEO_MAX_FRAME)
  357. count = VIDEO_MAX_FRAME;
  358. size = 0;
  359. q->ops->buf_setup(q, &count, &size);
  360. size = PAGE_ALIGN(size);
  361. dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
  362. count, size, (count*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 += 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);