videobuf-core.c 27 KB

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