videobuf-core.c 24 KB

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