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. struct device *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. wake_up_all(&q->bufs[i]->done);
  178. }
  179. }
  180. if (q->irqlock)
  181. spin_unlock_irqrestore(q->irqlock, flags);
  182. /* free all buffers + clear queue */
  183. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  184. if (NULL == q->bufs[i])
  185. continue;
  186. q->ops->buf_release(q, q->bufs[i]);
  187. }
  188. INIT_LIST_HEAD(&q->stream);
  189. }
  190. /* --------------------------------------------------------------------- */
  191. /* Locking: Caller holds q->vb_lock */
  192. enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
  193. {
  194. enum v4l2_field field = q->field;
  195. BUG_ON(V4L2_FIELD_ANY == field);
  196. if (V4L2_FIELD_ALTERNATE == field) {
  197. if (V4L2_FIELD_TOP == q->last) {
  198. field = V4L2_FIELD_BOTTOM;
  199. q->last = V4L2_FIELD_BOTTOM;
  200. } else {
  201. field = V4L2_FIELD_TOP;
  202. q->last = V4L2_FIELD_TOP;
  203. }
  204. }
  205. return field;
  206. }
  207. /* Locking: Caller holds q->vb_lock */
  208. static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
  209. struct videobuf_buffer *vb, enum v4l2_buf_type type)
  210. {
  211. MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  212. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  213. b->index = vb->i;
  214. b->type = type;
  215. b->memory = vb->memory;
  216. switch (b->memory) {
  217. case V4L2_MEMORY_MMAP:
  218. b->m.offset = vb->boff;
  219. b->length = vb->bsize;
  220. break;
  221. case V4L2_MEMORY_USERPTR:
  222. b->m.userptr = vb->baddr;
  223. b->length = vb->bsize;
  224. break;
  225. case V4L2_MEMORY_OVERLAY:
  226. b->m.offset = vb->boff;
  227. break;
  228. }
  229. b->flags = 0;
  230. if (vb->map)
  231. b->flags |= V4L2_BUF_FLAG_MAPPED;
  232. switch (vb->state) {
  233. case VIDEOBUF_PREPARED:
  234. case VIDEOBUF_QUEUED:
  235. case VIDEOBUF_ACTIVE:
  236. b->flags |= V4L2_BUF_FLAG_QUEUED;
  237. break;
  238. case VIDEOBUF_DONE:
  239. case VIDEOBUF_ERROR:
  240. b->flags |= V4L2_BUF_FLAG_DONE;
  241. break;
  242. case VIDEOBUF_NEEDS_INIT:
  243. case VIDEOBUF_IDLE:
  244. /* nothing */
  245. break;
  246. }
  247. if (vb->input != UNSET) {
  248. b->flags |= V4L2_BUF_FLAG_INPUT;
  249. b->input = vb->input;
  250. }
  251. b->field = vb->field;
  252. b->timestamp = vb->ts;
  253. b->bytesused = vb->size;
  254. b->sequence = vb->field_count >> 1;
  255. }
  256. /* Locking: Caller holds q->vb_lock */
  257. static int __videobuf_mmap_free(struct videobuf_queue *q)
  258. {
  259. int i;
  260. int rc;
  261. if (!q)
  262. return 0;
  263. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  264. rc = CALL(q, mmap_free, q);
  265. q->is_mmapped = 0;
  266. if (rc < 0)
  267. return rc;
  268. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  269. if (NULL == q->bufs[i])
  270. continue;
  271. q->ops->buf_release(q, q->bufs[i]);
  272. kfree(q->bufs[i]);
  273. q->bufs[i] = NULL;
  274. }
  275. return rc;
  276. }
  277. int videobuf_mmap_free(struct videobuf_queue *q)
  278. {
  279. int ret;
  280. mutex_lock(&q->vb_lock);
  281. ret = __videobuf_mmap_free(q);
  282. mutex_unlock(&q->vb_lock);
  283. return ret;
  284. }
  285. /* Locking: Caller holds q->vb_lock */
  286. static int __videobuf_mmap_setup(struct videobuf_queue *q,
  287. unsigned int bcount, unsigned int bsize,
  288. enum v4l2_memory memory)
  289. {
  290. unsigned int i;
  291. int err;
  292. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  293. err = __videobuf_mmap_free(q);
  294. if (0 != err)
  295. return err;
  296. /* Allocate and initialize buffers */
  297. for (i = 0; i < bcount; i++) {
  298. q->bufs[i] = videobuf_alloc(q);
  299. if (q->bufs[i] == NULL)
  300. break;
  301. q->bufs[i]->i = i;
  302. q->bufs[i]->input = UNSET;
  303. q->bufs[i]->memory = memory;
  304. q->bufs[i]->bsize = bsize;
  305. switch (memory) {
  306. case V4L2_MEMORY_MMAP:
  307. q->bufs[i]->boff = bsize * i;
  308. break;
  309. case V4L2_MEMORY_USERPTR:
  310. case V4L2_MEMORY_OVERLAY:
  311. /* nothing */
  312. break;
  313. }
  314. }
  315. if (!i)
  316. return -ENOMEM;
  317. dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
  318. i, bsize);
  319. return i;
  320. }
  321. int videobuf_mmap_setup(struct videobuf_queue *q,
  322. unsigned int bcount, unsigned int bsize,
  323. enum v4l2_memory memory)
  324. {
  325. int ret;
  326. mutex_lock(&q->vb_lock);
  327. ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
  328. mutex_unlock(&q->vb_lock);
  329. return ret;
  330. }
  331. int videobuf_reqbufs(struct videobuf_queue *q,
  332. struct v4l2_requestbuffers *req)
  333. {
  334. unsigned int size, count;
  335. int retval;
  336. if (req->count < 1) {
  337. dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
  338. return -EINVAL;
  339. }
  340. if (req->memory != V4L2_MEMORY_MMAP &&
  341. req->memory != V4L2_MEMORY_USERPTR &&
  342. req->memory != V4L2_MEMORY_OVERLAY) {
  343. dprintk(1, "reqbufs: memory type invalid\n");
  344. return -EINVAL;
  345. }
  346. mutex_lock(&q->vb_lock);
  347. if (req->type != q->type) {
  348. dprintk(1, "reqbufs: queue type invalid\n");
  349. retval = -EINVAL;
  350. goto done;
  351. }
  352. if (q->streaming) {
  353. dprintk(1, "reqbufs: streaming already exists\n");
  354. retval = -EBUSY;
  355. goto done;
  356. }
  357. if (!list_empty(&q->stream)) {
  358. dprintk(1, "reqbufs: stream running\n");
  359. retval = -EBUSY;
  360. goto done;
  361. }
  362. count = req->count;
  363. if (count > VIDEO_MAX_FRAME)
  364. count = VIDEO_MAX_FRAME;
  365. size = 0;
  366. q->ops->buf_setup(q, &count, &size);
  367. size = PAGE_ALIGN(size);
  368. dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
  369. count, size, (count*size)>>PAGE_SHIFT);
  370. retval = __videobuf_mmap_setup(q, count, size, req->memory);
  371. if (retval < 0) {
  372. dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
  373. goto done;
  374. }
  375. req->count = retval;
  376. done:
  377. mutex_unlock(&q->vb_lock);
  378. return retval;
  379. }
  380. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
  381. {
  382. int ret = -EINVAL;
  383. mutex_lock(&q->vb_lock);
  384. if (unlikely(b->type != q->type)) {
  385. dprintk(1, "querybuf: Wrong type.\n");
  386. goto done;
  387. }
  388. if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
  389. dprintk(1, "querybuf: index out of range.\n");
  390. goto done;
  391. }
  392. if (unlikely(NULL == q->bufs[b->index])) {
  393. dprintk(1, "querybuf: buffer is null.\n");
  394. goto done;
  395. }
  396. videobuf_status(q, b, q->bufs[b->index], q->type);
  397. ret = 0;
  398. done:
  399. mutex_unlock(&q->vb_lock);
  400. return ret;
  401. }
  402. int videobuf_qbuf(struct videobuf_queue *q,
  403. struct v4l2_buffer *b)
  404. {
  405. struct videobuf_buffer *buf;
  406. enum v4l2_field field;
  407. unsigned long flags = 0;
  408. int retval;
  409. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  410. if (b->memory == V4L2_MEMORY_MMAP)
  411. down_read(&current->mm->mmap_sem);
  412. mutex_lock(&q->vb_lock);
  413. retval = -EBUSY;
  414. if (q->reading) {
  415. dprintk(1, "qbuf: Reading running...\n");
  416. goto done;
  417. }
  418. retval = -EINVAL;
  419. if (b->type != q->type) {
  420. dprintk(1, "qbuf: Wrong type.\n");
  421. goto done;
  422. }
  423. if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
  424. dprintk(1, "qbuf: index out of range.\n");
  425. goto done;
  426. }
  427. buf = q->bufs[b->index];
  428. if (NULL == buf) {
  429. dprintk(1, "qbuf: buffer is null.\n");
  430. goto done;
  431. }
  432. MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
  433. if (buf->memory != b->memory) {
  434. dprintk(1, "qbuf: memory type is wrong.\n");
  435. goto done;
  436. }
  437. if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
  438. dprintk(1, "qbuf: buffer is already queued or active.\n");
  439. goto done;
  440. }
  441. if (b->flags & V4L2_BUF_FLAG_INPUT) {
  442. if (b->input >= q->inputs) {
  443. dprintk(1, "qbuf: wrong input.\n");
  444. goto done;
  445. }
  446. buf->input = b->input;
  447. } else {
  448. buf->input = UNSET;
  449. }
  450. switch (b->memory) {
  451. case V4L2_MEMORY_MMAP:
  452. if (0 == buf->baddr) {
  453. dprintk(1, "qbuf: mmap requested "
  454. "but buffer addr is zero!\n");
  455. goto done;
  456. }
  457. break;
  458. case V4L2_MEMORY_USERPTR:
  459. if (b->length < buf->bsize) {
  460. dprintk(1, "qbuf: buffer length is not enough\n");
  461. goto done;
  462. }
  463. if (VIDEOBUF_NEEDS_INIT != buf->state &&
  464. buf->baddr != b->m.userptr)
  465. q->ops->buf_release(q, buf);
  466. buf->baddr = b->m.userptr;
  467. break;
  468. case V4L2_MEMORY_OVERLAY:
  469. buf->boff = b->m.offset;
  470. break;
  471. default:
  472. dprintk(1, "qbuf: wrong memory type\n");
  473. goto done;
  474. }
  475. dprintk(1, "qbuf: requesting next field\n");
  476. field = videobuf_next_field(q);
  477. retval = q->ops->buf_prepare(q, buf, field);
  478. if (0 != retval) {
  479. dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
  480. goto done;
  481. }
  482. list_add_tail(&buf->stream, &q->stream);
  483. if (q->streaming) {
  484. if (q->irqlock)
  485. spin_lock_irqsave(q->irqlock, flags);
  486. q->ops->buf_queue(q, buf);
  487. if (q->irqlock)
  488. spin_unlock_irqrestore(q->irqlock, flags);
  489. }
  490. dprintk(1, "qbuf: succeded\n");
  491. retval = 0;
  492. done:
  493. mutex_unlock(&q->vb_lock);
  494. if (b->memory == V4L2_MEMORY_MMAP)
  495. up_read(&current->mm->mmap_sem);
  496. return retval;
  497. }
  498. int videobuf_dqbuf(struct videobuf_queue *q,
  499. struct v4l2_buffer *b, int nonblocking)
  500. {
  501. struct videobuf_buffer *buf;
  502. int retval;
  503. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  504. mutex_lock(&q->vb_lock);
  505. retval = -EBUSY;
  506. if (q->reading) {
  507. dprintk(1, "dqbuf: Reading running...\n");
  508. goto done;
  509. }
  510. retval = -EINVAL;
  511. if (b->type != q->type) {
  512. dprintk(1, "dqbuf: Wrong type.\n");
  513. goto done;
  514. }
  515. if (list_empty(&q->stream)) {
  516. dprintk(1, "dqbuf: stream running\n");
  517. goto done;
  518. }
  519. buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
  520. mutex_unlock(&q->vb_lock);
  521. retval = videobuf_waiton(buf, nonblocking, 1);
  522. mutex_lock(&q->vb_lock);
  523. if (retval < 0) {
  524. dprintk(1, "dqbuf: waiton returned %d\n", retval);
  525. goto done;
  526. }
  527. switch (buf->state) {
  528. case VIDEOBUF_ERROR:
  529. dprintk(1, "dqbuf: state is error\n");
  530. retval = -EIO;
  531. CALL(q, sync, q, buf);
  532. buf->state = VIDEOBUF_IDLE;
  533. break;
  534. case VIDEOBUF_DONE:
  535. dprintk(1, "dqbuf: state is done\n");
  536. CALL(q, sync, q, buf);
  537. buf->state = VIDEOBUF_IDLE;
  538. break;
  539. default:
  540. dprintk(1, "dqbuf: state invalid\n");
  541. retval = -EINVAL;
  542. goto done;
  543. }
  544. list_del(&buf->stream);
  545. memset(b, 0, sizeof(*b));
  546. videobuf_status(q, b, buf, q->type);
  547. done:
  548. mutex_unlock(&q->vb_lock);
  549. return retval;
  550. }
  551. int videobuf_streamon(struct videobuf_queue *q)
  552. {
  553. struct videobuf_buffer *buf;
  554. unsigned long flags = 0;
  555. int retval;
  556. mutex_lock(&q->vb_lock);
  557. retval = -EBUSY;
  558. if (q->reading)
  559. goto done;
  560. retval = 0;
  561. if (q->streaming)
  562. goto done;
  563. q->streaming = 1;
  564. if (q->irqlock)
  565. spin_lock_irqsave(q->irqlock, flags);
  566. list_for_each_entry(buf, &q->stream, stream)
  567. if (buf->state == VIDEOBUF_PREPARED)
  568. q->ops->buf_queue(q, buf);
  569. if (q->irqlock)
  570. spin_unlock_irqrestore(q->irqlock, flags);
  571. done:
  572. mutex_unlock(&q->vb_lock);
  573. return retval;
  574. }
  575. /* Locking: Caller holds q->vb_lock */
  576. static int __videobuf_streamoff(struct videobuf_queue *q)
  577. {
  578. if (!q->streaming)
  579. return -EINVAL;
  580. videobuf_queue_cancel(q);
  581. q->streaming = 0;
  582. return 0;
  583. }
  584. int videobuf_streamoff(struct videobuf_queue *q)
  585. {
  586. int retval;
  587. mutex_lock(&q->vb_lock);
  588. retval = __videobuf_streamoff(q);
  589. mutex_unlock(&q->vb_lock);
  590. return retval;
  591. }
  592. /* Locking: Caller holds q->vb_lock */
  593. static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
  594. char __user *data,
  595. size_t count, loff_t *ppos)
  596. {
  597. enum v4l2_field field;
  598. unsigned long flags = 0;
  599. int retval;
  600. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  601. /* setup stuff */
  602. q->read_buf = videobuf_alloc(q);
  603. if (NULL == q->read_buf)
  604. return -ENOMEM;
  605. q->read_buf->memory = V4L2_MEMORY_USERPTR;
  606. q->read_buf->baddr = (unsigned long)data;
  607. q->read_buf->bsize = count;
  608. field = videobuf_next_field(q);
  609. retval = q->ops->buf_prepare(q, q->read_buf, field);
  610. if (0 != retval)
  611. goto done;
  612. /* start capture & wait */
  613. if (q->irqlock)
  614. spin_lock_irqsave(q->irqlock, flags);
  615. q->ops->buf_queue(q, q->read_buf);
  616. if (q->irqlock)
  617. spin_unlock_irqrestore(q->irqlock, flags);
  618. retval = videobuf_waiton(q->read_buf, 0, 0);
  619. if (0 == retval) {
  620. CALL(q, sync, q, q->read_buf);
  621. if (VIDEOBUF_ERROR == q->read_buf->state)
  622. retval = -EIO;
  623. else
  624. retval = q->read_buf->size;
  625. }
  626. done:
  627. /* cleanup */
  628. q->ops->buf_release(q, q->read_buf);
  629. kfree(q->read_buf);
  630. q->read_buf = NULL;
  631. return retval;
  632. }
  633. ssize_t videobuf_read_one(struct videobuf_queue *q,
  634. char __user *data, size_t count, loff_t *ppos,
  635. int nonblocking)
  636. {
  637. enum v4l2_field field;
  638. unsigned long flags = 0;
  639. unsigned size = 0, nbufs = 1;
  640. int retval;
  641. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  642. mutex_lock(&q->vb_lock);
  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);