videobuf-core.c 26 KB

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