videobuf-core.c 27 KB

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