au0828-vbi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. au0828-vbi.c - VBI driver for au0828
  3. Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
  4. This work was sponsored by GetWellNetwork Inc.
  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/init.h>
  21. #include <linux/slab.h>
  22. #include "au0828.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. /* ------------------------------------------------------------------ */
  27. static void
  28. free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
  29. {
  30. struct au0828_fh *fh = vq->priv_data;
  31. struct au0828_dev *dev = fh->dev;
  32. unsigned long flags = 0;
  33. if (in_interrupt())
  34. BUG();
  35. /* We used to wait for the buffer to finish here, but this didn't work
  36. because, as we were keeping the state as VIDEOBUF_QUEUED,
  37. videobuf_queue_cancel marked it as finished for us.
  38. (Also, it could wedge forever if the hardware was misconfigured.)
  39. This should be safe; by the time we get here, the buffer isn't
  40. queued anymore. If we ever start marking the buffers as
  41. VIDEOBUF_ACTIVE, it won't be, though.
  42. */
  43. spin_lock_irqsave(&dev->slock, flags);
  44. if (dev->isoc_ctl.vbi_buf == buf)
  45. dev->isoc_ctl.vbi_buf = NULL;
  46. spin_unlock_irqrestore(&dev->slock, flags);
  47. videobuf_vmalloc_free(&buf->vb);
  48. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  49. }
  50. static int
  51. vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  52. {
  53. struct au0828_fh *fh = q->priv_data;
  54. struct au0828_dev *dev = fh->dev;
  55. *size = dev->vbi_width * dev->vbi_height * 2;
  56. if (0 == *count)
  57. *count = vbibufs;
  58. if (*count < 2)
  59. *count = 2;
  60. if (*count > 32)
  61. *count = 32;
  62. return 0;
  63. }
  64. static int
  65. vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  66. enum v4l2_field field)
  67. {
  68. struct au0828_fh *fh = q->priv_data;
  69. struct au0828_dev *dev = fh->dev;
  70. struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
  71. int rc = 0;
  72. buf->vb.size = dev->vbi_width * dev->vbi_height * 2;
  73. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  74. return -EINVAL;
  75. buf->vb.width = dev->vbi_width;
  76. buf->vb.height = dev->vbi_height;
  77. buf->vb.field = field;
  78. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  79. rc = videobuf_iolock(q, &buf->vb, NULL);
  80. if (rc < 0)
  81. goto fail;
  82. }
  83. buf->vb.state = VIDEOBUF_PREPARED;
  84. return 0;
  85. fail:
  86. free_buffer(q, buf);
  87. return rc;
  88. }
  89. static void
  90. vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  91. {
  92. struct au0828_buffer *buf = container_of(vb,
  93. struct au0828_buffer,
  94. vb);
  95. struct au0828_fh *fh = vq->priv_data;
  96. struct au0828_dev *dev = fh->dev;
  97. struct au0828_dmaqueue *vbiq = &dev->vbiq;
  98. buf->vb.state = VIDEOBUF_QUEUED;
  99. list_add_tail(&buf->vb.queue, &vbiq->active);
  100. }
  101. static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  102. {
  103. struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
  104. free_buffer(q, buf);
  105. }
  106. struct videobuf_queue_ops au0828_vbi_qops = {
  107. .buf_setup = vbi_setup,
  108. .buf_prepare = vbi_prepare,
  109. .buf_queue = vbi_queue,
  110. .buf_release = vbi_release,
  111. };