videobuf-core.c 24 KB

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