videobuf-core.c 24 KB

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