videobuf-core.c 27 KB

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