cx18-queue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(struct cx18_stream *s, u32 id,
  70. u32 bytesused)
  71. {
  72. struct cx18 *cx = s->cx;
  73. struct list_head *p;
  74. unsigned long flags = 0;
  75. spin_lock_irqsave(&s->qlock, flags);
  76. list_for_each(p, &s->q_free.list) {
  77. struct cx18_buffer *buf =
  78. list_entry(p, struct cx18_buffer, list);
  79. if (buf->id != id) {
  80. CX18_DEBUG_HI_DMA("Skipping buffer %d searching for %d "
  81. "in stream %s q_free\n", buf->id, id,
  82. s->name);
  83. continue;
  84. }
  85. buf->bytesused = bytesused;
  86. if (s->type != CX18_ENC_STREAM_TYPE_TS) {
  87. atomic_dec(&s->q_free.buffers);
  88. atomic_inc(&s->q_full.buffers);
  89. s->q_full.bytesused += buf->bytesused;
  90. list_move_tail(&buf->list, &s->q_full.list);
  91. }
  92. spin_unlock_irqrestore(&s->qlock, flags);
  93. return buf;
  94. }
  95. spin_unlock_irqrestore(&s->qlock, flags);
  96. CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name);
  97. return NULL;
  98. }
  99. /* Move all buffers of a queue to q_free, while flushing the buffers */
  100. static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
  101. {
  102. unsigned long flags;
  103. struct cx18_buffer *buf;
  104. if (q == &s->q_free)
  105. return;
  106. spin_lock_irqsave(&s->qlock, flags);
  107. while (!list_empty(&q->list)) {
  108. buf = list_entry(q->list.next, struct cx18_buffer, list);
  109. list_move_tail(q->list.next, &s->q_free.list);
  110. buf->bytesused = buf->readpos = buf->b_flags = 0;
  111. atomic_inc(&s->q_free.buffers);
  112. }
  113. cx18_queue_init(q);
  114. spin_unlock_irqrestore(&s->qlock, flags);
  115. }
  116. void cx18_flush_queues(struct cx18_stream *s)
  117. {
  118. cx18_queue_flush(s, &s->q_io);
  119. cx18_queue_flush(s, &s->q_full);
  120. }
  121. int cx18_stream_alloc(struct cx18_stream *s)
  122. {
  123. struct cx18 *cx = s->cx;
  124. int i;
  125. if (s->buffers == 0)
  126. return 0;
  127. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
  128. s->name, s->buffers, s->buf_size,
  129. s->buffers * s->buf_size / 1024);
  130. if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
  131. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  132. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  133. ((char __iomem *)cx->scb->cpu_mdl));
  134. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  135. CX18_ERR("Max buffers = %zd\n",
  136. bufsz / sizeof(struct cx18_mdl));
  137. return -ENOMEM;
  138. }
  139. s->mdl_offset = cx->mdl_offset;
  140. /* allocate stream buffers. Initially all buffers are in q_free. */
  141. for (i = 0; i < s->buffers; i++) {
  142. struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
  143. GFP_KERNEL|__GFP_NOWARN);
  144. if (buf == NULL)
  145. break;
  146. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  147. if (buf->buf == NULL) {
  148. kfree(buf);
  149. break;
  150. }
  151. buf->id = cx->buffer_id++;
  152. INIT_LIST_HEAD(&buf->list);
  153. buf->dma_handle = pci_map_single(s->cx->dev,
  154. buf->buf, s->buf_size, s->dma);
  155. cx18_buf_sync_for_cpu(s, buf);
  156. cx18_enqueue(s, buf, &s->q_free);
  157. }
  158. if (i == s->buffers) {
  159. cx->mdl_offset += s->buffers;
  160. return 0;
  161. }
  162. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  163. cx18_stream_free(s);
  164. return -ENOMEM;
  165. }
  166. void cx18_stream_free(struct cx18_stream *s)
  167. {
  168. struct cx18_buffer *buf;
  169. /* move all buffers to q_free */
  170. cx18_flush_queues(s);
  171. /* empty q_free */
  172. while ((buf = cx18_dequeue(s, &s->q_free))) {
  173. pci_unmap_single(s->cx->dev, buf->dma_handle,
  174. s->buf_size, s->dma);
  175. kfree(buf->buf);
  176. kfree(buf);
  177. }
  178. }