videobuf-core.c 24 KB

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