videobuf-core.c 24 KB

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