cx18-queue.c 5.2 KB

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