videobuf-core.c 24 KB

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