cx18-queue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * cx18 buffer queues
  3. *
  4. * Derived from ivtv-queue.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-streams.h"
  25. #include "cx18-queue.h"
  26. #include "cx18-scb.h"
  27. void cx18_buf_swap(struct cx18_buffer *buf)
  28. {
  29. int i;
  30. for (i = 0; i < buf->bytesused; i += 4)
  31. swab32s((u32 *)(buf->buf + i));
  32. }
  33. void cx18_queue_init(struct cx18_queue *q)
  34. {
  35. INIT_LIST_HEAD(&q->list);
  36. atomic_set(&q->buffers, 0);
  37. q->bytesused = 0;
  38. }
  39. void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
  40. struct cx18_queue *q)
  41. {
  42. unsigned long flags = 0;
  43. /* clear the buffer if it is going to be enqueued to the free queue */
  44. if (q == &s->q_free) {
  45. buf->bytesused = 0;
  46. buf->readpos = 0;
  47. buf->b_flags = 0;
  48. }
  49. spin_lock_irqsave(&s->qlock, flags);
  50. list_add_tail(&buf->list, &q->list);
  51. atomic_inc(&q->buffers);
  52. q->bytesused += buf->bytesused - buf->readpos;
  53. spin_unlock_irqrestore(&s->qlock, flags);
  54. }
  55. struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
  56. {
  57. struct cx18_buffer *buf = NULL;
  58. unsigned long flags = 0;
  59. spin_lock_irqsave(&s->qlock, flags);
  60. if (!list_empty(&q->list)) {
  61. buf = list_entry(q->list.next, struct cx18_buffer, list);
  62. list_del_init(q->list.next);
  63. atomic_dec(&q->buffers);
  64. q->bytesused -= buf->bytesused - buf->readpos;
  65. }
  66. spin_unlock_irqrestore(&s->qlock, flags);
  67. return buf;
  68. }
  69. struct cx18_buffer *cx18_queue_get_buf_irq(struct cx18_stream *s, u32 id,
  70. u32 bytesused)
  71. {
  72. struct cx18 *cx = s->cx;
  73. struct list_head *p;
  74. spin_lock(&s->qlock);
  75. list_for_each(p, &s->q_free.list) {
  76. struct cx18_buffer *buf =
  77. list_entry(p, struct cx18_buffer, list);
  78. if (buf->id != id)
  79. continue;
  80. buf->bytesused = bytesused;
  81. atomic_dec(&s->q_free.buffers);
  82. atomic_inc(&s->q_full.buffers);
  83. s->q_full.bytesused += buf->bytesused;
  84. list_move_tail(&buf->list, &s->q_full.list);
  85. spin_unlock(&s->qlock);
  86. return buf;
  87. }
  88. spin_unlock(&s->qlock);
  89. CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name);
  90. return NULL;
  91. }
  92. /* Move all buffers of a queue to q_free, while flushing the buffers */
  93. static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
  94. {
  95. unsigned long flags;
  96. struct cx18_buffer *buf;
  97. if (q == &s->q_free)
  98. return;
  99. spin_lock_irqsave(&s->qlock, flags);
  100. while (!list_empty(&q->list)) {
  101. buf = list_entry(q->list.next, struct cx18_buffer, list);
  102. list_move_tail(q->list.next, &s->q_free.list);
  103. buf->bytesused = buf->readpos = buf->b_flags = 0;
  104. atomic_inc(&s->q_free.buffers);
  105. }
  106. cx18_queue_init(q);
  107. spin_unlock_irqrestore(&s->qlock, flags);
  108. }
  109. void cx18_flush_queues(struct cx18_stream *s)
  110. {
  111. cx18_queue_flush(s, &s->q_io);
  112. cx18_queue_flush(s, &s->q_full);
  113. }
  114. int cx18_stream_alloc(struct cx18_stream *s)
  115. {
  116. struct cx18 *cx = s->cx;
  117. int i;
  118. if (s->buffers == 0)
  119. return 0;
  120. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
  121. s->name, s->buffers, s->buf_size,
  122. s->buffers * s->buf_size / 1024);
  123. if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
  124. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  125. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  126. ((char __iomem *)cx->scb->cpu_mdl));
  127. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  128. CX18_ERR("Max buffers = %zd\n",
  129. bufsz / sizeof(struct cx18_mdl));
  130. return -ENOMEM;
  131. }
  132. s->mdl_offset = cx->mdl_offset;
  133. /* allocate stream buffers. Initially all buffers are in q_free. */
  134. for (i = 0; i < s->buffers; i++) {
  135. struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
  136. GFP_KERNEL|__GFP_NOWARN);
  137. if (buf == NULL)
  138. break;
  139. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  140. if (buf->buf == NULL) {
  141. kfree(buf);
  142. break;
  143. }
  144. buf->id = cx->buffer_id++;
  145. INIT_LIST_HEAD(&buf->list);
  146. buf->dma_handle = pci_map_single(s->cx->dev,
  147. buf->buf, s->buf_size, s->dma);
  148. cx18_buf_sync_for_cpu(s, buf);
  149. cx18_enqueue(s, buf, &s->q_free);
  150. }
  151. if (i == s->buffers) {
  152. cx->mdl_offset += s->buffers;
  153. return 0;
  154. }
  155. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  156. cx18_stream_free(s);
  157. return -ENOMEM;
  158. }
  159. void cx18_stream_free(struct cx18_stream *s)
  160. {
  161. struct cx18_buffer *buf;
  162. /* move all buffers to q_free */
  163. cx18_flush_queues(s);
  164. /* empty q_free */
  165. while ((buf = cx18_dequeue(s, &s->q_free))) {
  166. pci_unmap_single(s->cx->dev, buf->dma_handle,
  167. s->buf_size, s->dma);
  168. kfree(buf->buf);
  169. kfree(buf);
  170. }
  171. }