em28xx-vbi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. em28xx-vbi.c - VBI driver for em28xx
  3. Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
  4. This work was sponsored by EyeMagnet Limited.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/init.h>
  22. #include "em28xx.h"
  23. static unsigned int vbibufs = 5;
  24. module_param(vbibufs, int, 0644);
  25. MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
  26. static unsigned int vbi_debug;
  27. module_param(vbi_debug, int, 0644);
  28. MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
  29. #define dprintk(level, fmt, arg...) if (vbi_debug >= level) \
  30. printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
  31. /* ------------------------------------------------------------------ */
  32. static void
  33. free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  34. {
  35. struct em28xx_fh *fh = vq->priv_data;
  36. struct em28xx *dev = fh->dev;
  37. unsigned long flags = 0;
  38. if (in_interrupt())
  39. BUG();
  40. /* We used to wait for the buffer to finish here, but this didn't work
  41. because, as we were keeping the state as VIDEOBUF_QUEUED,
  42. videobuf_queue_cancel marked it as finished for us.
  43. (Also, it could wedge forever if the hardware was misconfigured.)
  44. This should be safe; by the time we get here, the buffer isn't
  45. queued anymore. If we ever start marking the buffers as
  46. VIDEOBUF_ACTIVE, it won't be, though.
  47. */
  48. spin_lock_irqsave(&dev->slock, flags);
  49. if (dev->isoc_ctl.vbi_buf == buf)
  50. dev->isoc_ctl.vbi_buf = NULL;
  51. spin_unlock_irqrestore(&dev->slock, flags);
  52. videobuf_vmalloc_free(&buf->vb);
  53. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  54. }
  55. static int
  56. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  57. {
  58. struct em28xx_fh *fh = q->priv_data;
  59. struct em28xx *dev = fh->dev;
  60. *size = dev->vbi_width * dev->vbi_height * 2;
  61. if (0 == *count)
  62. *count = vbibufs;
  63. if (*count < 2)
  64. *count = 2;
  65. if (*count > 32)
  66. *count = 32;
  67. return 0;
  68. }
  69. static int
  70. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  71. enum v4l2_field field)
  72. {
  73. struct em28xx_fh *fh = q->priv_data;
  74. struct em28xx *dev = fh->dev;
  75. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  76. int rc = 0;
  77. buf->vb.size = dev->vbi_width * dev->vbi_height * 2;
  78. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  79. return -EINVAL;
  80. buf->vb.width = dev->vbi_width;
  81. buf->vb.height = dev->vbi_height;
  82. buf->vb.field = field;
  83. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  84. rc = videobuf_iolock(q, &buf->vb, NULL);
  85. if (rc < 0)
  86. goto fail;
  87. }
  88. buf->vb.state = VIDEOBUF_PREPARED;
  89. return 0;
  90. fail:
  91. free_buffer(q, buf);
  92. return rc;
  93. }
  94. static void
  95. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  96. {
  97. struct em28xx_buffer *buf = container_of(vb,
  98. struct em28xx_buffer,
  99. vb);
  100. struct em28xx_fh *fh = vq->priv_data;
  101. struct em28xx *dev = fh->dev;
  102. struct em28xx_dmaqueue *vbiq = &dev->vbiq;
  103. buf->vb.state = VIDEOBUF_QUEUED;
  104. list_add_tail(&buf->vb.queue, &vbiq->active);
  105. }
  106. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  107. {
  108. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  109. free_buffer(q, buf);
  110. }
  111. struct videobuf_queue_ops em28xx_vbi_qops = {
  112. .buf_setup = vbi_setup,
  113. .buf_prepare = vbi_prepare,
  114. .buf_queue = vbi_queue,
  115. .buf_release = vbi_release,
  116. };