videobuf-core.c 27 KB

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