videobuf-core.c 27 KB

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