videobuf-core.c 24 KB

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