videobuf-core.c 24 KB

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