cx18-queue.c 5.3 KB

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