videobuf-core.c 24 KB

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