videobuf-core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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->reserved = vb->boff;
  282. b->length = vb->bsize;
  283. break;
  284. case V4L2_MEMORY_OVERLAY:
  285. b->m.offset = vb->boff;
  286. break;
  287. }
  288. b->flags = 0;
  289. if (vb->map)
  290. b->flags |= V4L2_BUF_FLAG_MAPPED;
  291. switch (vb->state) {
  292. case VIDEOBUF_PREPARED:
  293. case VIDEOBUF_QUEUED:
  294. case VIDEOBUF_ACTIVE:
  295. b->flags |= V4L2_BUF_FLAG_QUEUED;
  296. break;
  297. case VIDEOBUF_ERROR:
  298. b->flags |= V4L2_BUF_FLAG_ERROR;
  299. /* fall through */
  300. case VIDEOBUF_DONE:
  301. b->flags |= V4L2_BUF_FLAG_DONE;
  302. break;
  303. case VIDEOBUF_NEEDS_INIT:
  304. case VIDEOBUF_IDLE:
  305. /* nothing */
  306. break;
  307. }
  308. if (vb->input != UNSET) {
  309. b->flags |= V4L2_BUF_FLAG_INPUT;
  310. b->input = vb->input;
  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]->input = UNSET;
  344. q->bufs[i]->memory = memory;
  345. q->bufs[i]->bsize = bsize;
  346. switch (memory) {
  347. case V4L2_MEMORY_MMAP:
  348. q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
  349. break;
  350. case V4L2_MEMORY_USERPTR:
  351. case V4L2_MEMORY_OVERLAY:
  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->count < 1) {
  379. dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
  380. return -EINVAL;
  381. }
  382. if (req->memory != V4L2_MEMORY_MMAP &&
  383. req->memory != V4L2_MEMORY_USERPTR &&
  384. req->memory != V4L2_MEMORY_OVERLAY) {
  385. dprintk(1, "reqbufs: memory type invalid\n");
  386. return -EINVAL;
  387. }
  388. videobuf_queue_lock(q);
  389. if (req->type != q->type) {
  390. dprintk(1, "reqbufs: queue type invalid\n");
  391. retval = -EINVAL;
  392. goto done;
  393. }
  394. if (q->streaming) {
  395. dprintk(1, "reqbufs: streaming already exists\n");
  396. retval = -EBUSY;
  397. goto done;
  398. }
  399. if (!list_empty(&q->stream)) {
  400. dprintk(1, "reqbufs: stream running\n");
  401. retval = -EBUSY;
  402. goto done;
  403. }
  404. count = req->count;
  405. if (count > VIDEO_MAX_FRAME)
  406. count = VIDEO_MAX_FRAME;
  407. size = 0;
  408. q->ops->buf_setup(q, &count, &size);
  409. dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
  410. count, size,
  411. (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
  412. retval = __videobuf_mmap_setup(q, count, size, req->memory);
  413. if (retval < 0) {
  414. dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
  415. goto done;
  416. }
  417. req->count = retval;
  418. retval = 0;
  419. done:
  420. videobuf_queue_unlock(q);
  421. return retval;
  422. }
  423. EXPORT_SYMBOL_GPL(videobuf_reqbufs);
  424. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
  425. {
  426. int ret = -EINVAL;
  427. videobuf_queue_lock(q);
  428. if (unlikely(b->type != q->type)) {
  429. dprintk(1, "querybuf: Wrong type.\n");
  430. goto done;
  431. }
  432. if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
  433. dprintk(1, "querybuf: index out of range.\n");
  434. goto done;
  435. }
  436. if (unlikely(NULL == q->bufs[b->index])) {
  437. dprintk(1, "querybuf: buffer is null.\n");
  438. goto done;
  439. }
  440. videobuf_status(q, b, q->bufs[b->index], q->type);
  441. ret = 0;
  442. done:
  443. videobuf_queue_unlock(q);
  444. return ret;
  445. }
  446. EXPORT_SYMBOL_GPL(videobuf_querybuf);
  447. int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
  448. {
  449. struct videobuf_buffer *buf;
  450. enum v4l2_field field;
  451. unsigned long flags = 0;
  452. int retval;
  453. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  454. if (b->memory == V4L2_MEMORY_MMAP)
  455. down_read(&current->mm->mmap_sem);
  456. videobuf_queue_lock(q);
  457. retval = -EBUSY;
  458. if (q->reading) {
  459. dprintk(1, "qbuf: Reading running...\n");
  460. goto done;
  461. }
  462. retval = -EINVAL;
  463. if (b->type != q->type) {
  464. dprintk(1, "qbuf: Wrong type.\n");
  465. goto done;
  466. }
  467. if (b->index >= VIDEO_MAX_FRAME) {
  468. dprintk(1, "qbuf: index out of range.\n");
  469. goto done;
  470. }
  471. buf = q->bufs[b->index];
  472. if (NULL == buf) {
  473. dprintk(1, "qbuf: buffer is null.\n");
  474. goto done;
  475. }
  476. MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
  477. if (buf->memory != b->memory) {
  478. dprintk(1, "qbuf: memory type is wrong.\n");
  479. goto done;
  480. }
  481. if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
  482. dprintk(1, "qbuf: buffer is already queued or active.\n");
  483. goto done;
  484. }
  485. if (b->flags & V4L2_BUF_FLAG_INPUT) {
  486. if (b->input >= q->inputs) {
  487. dprintk(1, "qbuf: wrong input.\n");
  488. goto done;
  489. }
  490. buf->input = b->input;
  491. } else {
  492. buf->input = UNSET;
  493. }
  494. switch (b->memory) {
  495. case V4L2_MEMORY_MMAP:
  496. if (0 == buf->baddr) {
  497. dprintk(1, "qbuf: mmap requested "
  498. "but buffer addr is zero!\n");
  499. goto done;
  500. }
  501. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
  502. || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
  503. || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  504. buf->size = b->bytesused;
  505. buf->field = b->field;
  506. buf->ts = b->timestamp;
  507. }
  508. break;
  509. case V4L2_MEMORY_USERPTR:
  510. if (b->length < buf->bsize) {
  511. dprintk(1, "qbuf: buffer length is not enough\n");
  512. goto done;
  513. }
  514. if (VIDEOBUF_NEEDS_INIT != buf->state &&
  515. buf->baddr != b->m.userptr)
  516. q->ops->buf_release(q, buf);
  517. buf->baddr = b->m.userptr;
  518. buf->boff = b->reserved;
  519. break;
  520. case V4L2_MEMORY_OVERLAY:
  521. buf->boff = b->m.offset;
  522. break;
  523. default:
  524. dprintk(1, "qbuf: wrong memory type\n");
  525. goto done;
  526. }
  527. dprintk(1, "qbuf: requesting next field\n");
  528. field = videobuf_next_field(q);
  529. retval = q->ops->buf_prepare(q, buf, field);
  530. if (0 != retval) {
  531. dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
  532. goto done;
  533. }
  534. list_add_tail(&buf->stream, &q->stream);
  535. if (q->streaming) {
  536. spin_lock_irqsave(q->irqlock, flags);
  537. q->ops->buf_queue(q, buf);
  538. spin_unlock_irqrestore(q->irqlock, flags);
  539. }
  540. dprintk(1, "qbuf: succeeded\n");
  541. retval = 0;
  542. wake_up_interruptible_sync(&q->wait);
  543. done:
  544. videobuf_queue_unlock(q);
  545. if (b->memory == V4L2_MEMORY_MMAP)
  546. up_read(&current->mm->mmap_sem);
  547. return retval;
  548. }
  549. EXPORT_SYMBOL_GPL(videobuf_qbuf);
  550. /* Locking: Caller holds q->vb_lock */
  551. static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
  552. {
  553. int retval;
  554. checks:
  555. if (!q->streaming) {
  556. dprintk(1, "next_buffer: Not streaming\n");
  557. retval = -EINVAL;
  558. goto done;
  559. }
  560. if (list_empty(&q->stream)) {
  561. if (noblock) {
  562. retval = -EAGAIN;
  563. dprintk(2, "next_buffer: no buffers to dequeue\n");
  564. goto done;
  565. } else {
  566. dprintk(2, "next_buffer: waiting on buffer\n");
  567. /* Drop lock to avoid deadlock with qbuf */
  568. videobuf_queue_unlock(q);
  569. /* Checking list_empty and streaming is safe without
  570. * locks because we goto checks to validate while
  571. * holding locks before proceeding */
  572. retval = wait_event_interruptible(q->wait,
  573. !list_empty(&q->stream) || !q->streaming);
  574. videobuf_queue_lock(q);
  575. if (retval)
  576. goto done;
  577. goto checks;
  578. }
  579. }
  580. retval = 0;
  581. done:
  582. return retval;
  583. }
  584. /* Locking: Caller holds q->vb_lock */
  585. static int stream_next_buffer(struct videobuf_queue *q,
  586. struct videobuf_buffer **vb, int nonblocking)
  587. {
  588. int retval;
  589. struct videobuf_buffer *buf = NULL;
  590. retval = stream_next_buffer_check_queue(q, nonblocking);
  591. if (retval)
  592. goto done;
  593. buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
  594. retval = videobuf_waiton(q, buf, nonblocking, 1);
  595. if (retval < 0)
  596. goto done;
  597. *vb = buf;
  598. done:
  599. return retval;
  600. }
  601. int videobuf_dqbuf(struct videobuf_queue *q,
  602. struct v4l2_buffer *b, int nonblocking)
  603. {
  604. struct videobuf_buffer *buf = NULL;
  605. int retval;
  606. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  607. memset(b, 0, sizeof(*b));
  608. videobuf_queue_lock(q);
  609. retval = stream_next_buffer(q, &buf, nonblocking);
  610. if (retval < 0) {
  611. dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
  612. goto done;
  613. }
  614. switch (buf->state) {
  615. case VIDEOBUF_ERROR:
  616. dprintk(1, "dqbuf: state is error\n");
  617. break;
  618. case VIDEOBUF_DONE:
  619. dprintk(1, "dqbuf: state is done\n");
  620. break;
  621. default:
  622. dprintk(1, "dqbuf: state invalid\n");
  623. retval = -EINVAL;
  624. goto done;
  625. }
  626. CALL(q, sync, q, buf);
  627. videobuf_status(q, b, buf, q->type);
  628. list_del(&buf->stream);
  629. buf->state = VIDEOBUF_IDLE;
  630. b->flags &= ~V4L2_BUF_FLAG_DONE;
  631. done:
  632. videobuf_queue_unlock(q);
  633. return retval;
  634. }
  635. EXPORT_SYMBOL_GPL(videobuf_dqbuf);
  636. int videobuf_streamon(struct videobuf_queue *q)
  637. {
  638. struct videobuf_buffer *buf;
  639. unsigned long flags = 0;
  640. int retval;
  641. videobuf_queue_lock(q);
  642. retval = -EBUSY;
  643. if (q->reading)
  644. goto done;
  645. retval = 0;
  646. if (q->streaming)
  647. goto done;
  648. q->streaming = 1;
  649. spin_lock_irqsave(q->irqlock, flags);
  650. list_for_each_entry(buf, &q->stream, stream)
  651. if (buf->state == VIDEOBUF_PREPARED)
  652. q->ops->buf_queue(q, buf);
  653. spin_unlock_irqrestore(q->irqlock, flags);
  654. wake_up_interruptible_sync(&q->wait);
  655. done:
  656. videobuf_queue_unlock(q);
  657. return retval;
  658. }
  659. EXPORT_SYMBOL_GPL(videobuf_streamon);
  660. /* Locking: Caller holds q->vb_lock */
  661. static int __videobuf_streamoff(struct videobuf_queue *q)
  662. {
  663. if (!q->streaming)
  664. return -EINVAL;
  665. videobuf_queue_cancel(q);
  666. return 0;
  667. }
  668. int videobuf_streamoff(struct videobuf_queue *q)
  669. {
  670. int retval;
  671. videobuf_queue_lock(q);
  672. retval = __videobuf_streamoff(q);
  673. videobuf_queue_unlock(q);
  674. return retval;
  675. }
  676. EXPORT_SYMBOL_GPL(videobuf_streamoff);
  677. /* Locking: Caller holds q->vb_lock */
  678. static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
  679. char __user *data,
  680. size_t count, loff_t *ppos)
  681. {
  682. enum v4l2_field field;
  683. unsigned long flags = 0;
  684. int retval;
  685. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  686. /* setup stuff */
  687. q->read_buf = videobuf_alloc_vb(q);
  688. if (NULL == q->read_buf)
  689. return -ENOMEM;
  690. q->read_buf->memory = V4L2_MEMORY_USERPTR;
  691. q->read_buf->baddr = (unsigned long)data;
  692. q->read_buf->bsize = count;
  693. field = videobuf_next_field(q);
  694. retval = q->ops->buf_prepare(q, q->read_buf, field);
  695. if (0 != retval)
  696. goto done;
  697. /* start capture & wait */
  698. spin_lock_irqsave(q->irqlock, flags);
  699. q->ops->buf_queue(q, q->read_buf);
  700. spin_unlock_irqrestore(q->irqlock, flags);
  701. retval = videobuf_waiton(q, q->read_buf, 0, 0);
  702. if (0 == retval) {
  703. CALL(q, sync, q, q->read_buf);
  704. if (VIDEOBUF_ERROR == q->read_buf->state)
  705. retval = -EIO;
  706. else
  707. retval = q->read_buf->size;
  708. }
  709. done:
  710. /* cleanup */
  711. q->ops->buf_release(q, q->read_buf);
  712. kfree(q->read_buf);
  713. q->read_buf = NULL;
  714. return retval;
  715. }
  716. static int __videobuf_copy_to_user(struct videobuf_queue *q,
  717. struct videobuf_buffer *buf,
  718. char __user *data, size_t count,
  719. int nonblocking)
  720. {
  721. void *vaddr = CALL(q, vaddr, buf);
  722. /* copy to userspace */
  723. if (count > buf->size - q->read_off)
  724. count = buf->size - q->read_off;
  725. if (copy_to_user(data, vaddr + q->read_off, count))
  726. return -EFAULT;
  727. return count;
  728. }
  729. static int __videobuf_copy_stream(struct videobuf_queue *q,
  730. struct videobuf_buffer *buf,
  731. char __user *data, size_t count, size_t pos,
  732. int vbihack, int nonblocking)
  733. {
  734. unsigned int *fc = CALL(q, vaddr, buf);
  735. if (vbihack) {
  736. /* dirty, undocumented hack -- pass the frame counter
  737. * within the last four bytes of each vbi data block.
  738. * We need that one to maintain backward compatibility
  739. * to all vbi decoding software out there ... */
  740. fc += (buf->size >> 2) - 1;
  741. *fc = buf->field_count >> 1;
  742. dprintk(1, "vbihack: %d\n", *fc);
  743. }
  744. /* copy stuff using the common method */
  745. count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
  746. if ((count == -EFAULT) && (pos == 0))
  747. return -EFAULT;
  748. return count;
  749. }
  750. ssize_t videobuf_read_one(struct videobuf_queue *q,
  751. char __user *data, size_t count, loff_t *ppos,
  752. int nonblocking)
  753. {
  754. enum v4l2_field field;
  755. unsigned long flags = 0;
  756. unsigned size = 0, nbufs = 1;
  757. int retval;
  758. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  759. videobuf_queue_lock(q);
  760. q->ops->buf_setup(q, &nbufs, &size);
  761. if (NULL == q->read_buf &&
  762. count >= size &&
  763. !nonblocking) {
  764. retval = videobuf_read_zerocopy(q, data, count, ppos);
  765. if (retval >= 0 || retval == -EIO)
  766. /* ok, all done */
  767. goto done;
  768. /* fallback to kernel bounce buffer on failures */
  769. }
  770. if (NULL == q->read_buf) {
  771. /* need to capture a new frame */
  772. retval = -ENOMEM;
  773. q->read_buf = videobuf_alloc_vb(q);
  774. dprintk(1, "video alloc=0x%p\n", q->read_buf);
  775. if (NULL == q->read_buf)
  776. goto done;
  777. q->read_buf->memory = V4L2_MEMORY_USERPTR;
  778. q->read_buf->bsize = count; /* preferred size */
  779. field = videobuf_next_field(q);
  780. retval = q->ops->buf_prepare(q, q->read_buf, field);
  781. if (0 != retval) {
  782. kfree(q->read_buf);
  783. q->read_buf = NULL;
  784. goto done;
  785. }
  786. spin_lock_irqsave(q->irqlock, flags);
  787. q->ops->buf_queue(q, q->read_buf);
  788. spin_unlock_irqrestore(q->irqlock, flags);
  789. q->read_off = 0;
  790. }
  791. /* wait until capture is done */
  792. retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
  793. if (0 != retval)
  794. goto done;
  795. CALL(q, sync, q, q->read_buf);
  796. if (VIDEOBUF_ERROR == q->read_buf->state) {
  797. /* catch I/O errors */
  798. q->ops->buf_release(q, q->read_buf);
  799. kfree(q->read_buf);
  800. q->read_buf = NULL;
  801. retval = -EIO;
  802. goto done;
  803. }
  804. /* Copy to userspace */
  805. retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
  806. if (retval < 0)
  807. goto done;
  808. q->read_off += retval;
  809. if (q->read_off == q->read_buf->size) {
  810. /* all data copied, cleanup */
  811. q->ops->buf_release(q, q->read_buf);
  812. kfree(q->read_buf);
  813. q->read_buf = NULL;
  814. }
  815. done:
  816. videobuf_queue_unlock(q);
  817. return retval;
  818. }
  819. EXPORT_SYMBOL_GPL(videobuf_read_one);
  820. /* Locking: Caller holds q->vb_lock */
  821. static int __videobuf_read_start(struct videobuf_queue *q)
  822. {
  823. enum v4l2_field field;
  824. unsigned long flags = 0;
  825. unsigned int count = 0, size = 0;
  826. int err, i;
  827. q->ops->buf_setup(q, &count, &size);
  828. if (count < 2)
  829. count = 2;
  830. if (count > VIDEO_MAX_FRAME)
  831. count = VIDEO_MAX_FRAME;
  832. size = PAGE_ALIGN(size);
  833. err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
  834. if (err < 0)
  835. return err;
  836. count = err;
  837. for (i = 0; i < count; i++) {
  838. field = videobuf_next_field(q);
  839. err = q->ops->buf_prepare(q, q->bufs[i], field);
  840. if (err)
  841. return err;
  842. list_add_tail(&q->bufs[i]->stream, &q->stream);
  843. }
  844. spin_lock_irqsave(q->irqlock, flags);
  845. for (i = 0; i < count; i++)
  846. q->ops->buf_queue(q, q->bufs[i]);
  847. spin_unlock_irqrestore(q->irqlock, flags);
  848. q->reading = 1;
  849. return 0;
  850. }
  851. static void __videobuf_read_stop(struct videobuf_queue *q)
  852. {
  853. int i;
  854. videobuf_queue_cancel(q);
  855. __videobuf_free(q);
  856. INIT_LIST_HEAD(&q->stream);
  857. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  858. if (NULL == q->bufs[i])
  859. continue;
  860. kfree(q->bufs[i]);
  861. q->bufs[i] = NULL;
  862. }
  863. q->read_buf = NULL;
  864. }
  865. int videobuf_read_start(struct videobuf_queue *q)
  866. {
  867. int rc;
  868. videobuf_queue_lock(q);
  869. rc = __videobuf_read_start(q);
  870. videobuf_queue_unlock(q);
  871. return rc;
  872. }
  873. EXPORT_SYMBOL_GPL(videobuf_read_start);
  874. void videobuf_read_stop(struct videobuf_queue *q)
  875. {
  876. videobuf_queue_lock(q);
  877. __videobuf_read_stop(q);
  878. videobuf_queue_unlock(q);
  879. }
  880. EXPORT_SYMBOL_GPL(videobuf_read_stop);
  881. void videobuf_stop(struct videobuf_queue *q)
  882. {
  883. videobuf_queue_lock(q);
  884. if (q->streaming)
  885. __videobuf_streamoff(q);
  886. if (q->reading)
  887. __videobuf_read_stop(q);
  888. videobuf_queue_unlock(q);
  889. }
  890. EXPORT_SYMBOL_GPL(videobuf_stop);
  891. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  892. char __user *data, size_t count, loff_t *ppos,
  893. int vbihack, int nonblocking)
  894. {
  895. int rc, retval;
  896. unsigned long flags = 0;
  897. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  898. dprintk(2, "%s\n", __func__);
  899. videobuf_queue_lock(q);
  900. retval = -EBUSY;
  901. if (q->streaming)
  902. goto done;
  903. if (!q->reading) {
  904. retval = __videobuf_read_start(q);
  905. if (retval < 0)
  906. goto done;
  907. }
  908. retval = 0;
  909. while (count > 0) {
  910. /* get / wait for data */
  911. if (NULL == q->read_buf) {
  912. q->read_buf = list_entry(q->stream.next,
  913. struct videobuf_buffer,
  914. stream);
  915. list_del(&q->read_buf->stream);
  916. q->read_off = 0;
  917. }
  918. rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
  919. if (rc < 0) {
  920. if (0 == retval)
  921. retval = rc;
  922. break;
  923. }
  924. if (q->read_buf->state == VIDEOBUF_DONE) {
  925. rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
  926. retval, vbihack, nonblocking);
  927. if (rc < 0) {
  928. retval = rc;
  929. break;
  930. }
  931. retval += rc;
  932. count -= rc;
  933. q->read_off += rc;
  934. } else {
  935. /* some error */
  936. q->read_off = q->read_buf->size;
  937. if (0 == retval)
  938. retval = -EIO;
  939. }
  940. /* requeue buffer when done with copying */
  941. if (q->read_off == q->read_buf->size) {
  942. list_add_tail(&q->read_buf->stream,
  943. &q->stream);
  944. spin_lock_irqsave(q->irqlock, flags);
  945. q->ops->buf_queue(q, q->read_buf);
  946. spin_unlock_irqrestore(q->irqlock, flags);
  947. q->read_buf = NULL;
  948. }
  949. if (retval < 0)
  950. break;
  951. }
  952. done:
  953. videobuf_queue_unlock(q);
  954. return retval;
  955. }
  956. EXPORT_SYMBOL_GPL(videobuf_read_stream);
  957. unsigned int videobuf_poll_stream(struct file *file,
  958. struct videobuf_queue *q,
  959. poll_table *wait)
  960. {
  961. struct videobuf_buffer *buf = NULL;
  962. unsigned int rc = 0;
  963. videobuf_queue_lock(q);
  964. if (q->streaming) {
  965. if (!list_empty(&q->stream))
  966. buf = list_entry(q->stream.next,
  967. struct videobuf_buffer, stream);
  968. } else {
  969. if (!q->reading) {
  970. rc = POLLERR;
  971. } else if (NULL == q->read_buf) {
  972. q->read_buf = list_entry(q->stream.next,
  973. struct videobuf_buffer,
  974. stream);
  975. list_del(&q->read_buf->stream);
  976. q->read_off = 0;
  977. }
  978. buf = q->read_buf;
  979. }
  980. if (!buf)
  981. rc = POLLERR;
  982. if (0 == rc) {
  983. poll_wait(file, &buf->done, wait);
  984. if (buf->state == VIDEOBUF_DONE ||
  985. buf->state == VIDEOBUF_ERROR) {
  986. switch (q->type) {
  987. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  988. case V4L2_BUF_TYPE_VBI_OUTPUT:
  989. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  990. rc = POLLOUT | POLLWRNORM;
  991. break;
  992. default:
  993. rc = POLLIN | POLLRDNORM;
  994. break;
  995. }
  996. }
  997. }
  998. videobuf_queue_unlock(q);
  999. return rc;
  1000. }
  1001. EXPORT_SYMBOL_GPL(videobuf_poll_stream);
  1002. int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
  1003. {
  1004. int rc = -EINVAL;
  1005. int i;
  1006. MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
  1007. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
  1008. dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
  1009. return -EINVAL;
  1010. }
  1011. videobuf_queue_lock(q);
  1012. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1013. struct videobuf_buffer *buf = q->bufs[i];
  1014. if (buf && buf->memory == V4L2_MEMORY_MMAP &&
  1015. buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
  1016. rc = CALL(q, mmap_mapper, q, buf, vma);
  1017. break;
  1018. }
  1019. }
  1020. videobuf_queue_unlock(q);
  1021. return rc;
  1022. }
  1023. EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);