1 // SPDX-License-Identifier: GPL-2.0-or-later
4 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
5 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
6 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
10 #include "ivtv-driver.h"
11 #include "ivtv-queue.h"
13 int ivtv_buf_copy_from_user(struct ivtv_stream
*s
, struct ivtv_buffer
*buf
, const char __user
*src
, int copybytes
)
15 if (s
->buf_size
- buf
->bytesused
< copybytes
)
16 copybytes
= s
->buf_size
- buf
->bytesused
;
17 if (copy_from_user(buf
->buf
+ buf
->bytesused
, src
, copybytes
)) {
20 buf
->bytesused
+= copybytes
;
24 void ivtv_buf_swap(struct ivtv_buffer
*buf
)
28 for (i
= 0; i
< buf
->bytesused
; i
+= 4)
29 swab32s((u32
*)(buf
->buf
+ i
));
32 void ivtv_queue_init(struct ivtv_queue
*q
)
34 INIT_LIST_HEAD(&q
->list
);
40 void ivtv_enqueue(struct ivtv_stream
*s
, struct ivtv_buffer
*buf
, struct ivtv_queue
*q
)
44 /* clear the buffer if it is going to be enqueued to the free queue */
45 if (q
== &s
->q_free
) {
49 buf
->dma_xfer_cnt
= 0;
51 spin_lock_irqsave(&s
->qlock
, flags
);
52 list_add_tail(&buf
->list
, &q
->list
);
54 q
->length
+= s
->buf_size
;
55 q
->bytesused
+= buf
->bytesused
- buf
->readpos
;
56 spin_unlock_irqrestore(&s
->qlock
, flags
);
59 struct ivtv_buffer
*ivtv_dequeue(struct ivtv_stream
*s
, struct ivtv_queue
*q
)
61 struct ivtv_buffer
*buf
= NULL
;
64 spin_lock_irqsave(&s
->qlock
, flags
);
65 if (!list_empty(&q
->list
)) {
66 buf
= list_entry(q
->list
.next
, struct ivtv_buffer
, list
);
67 list_del_init(q
->list
.next
);
69 q
->length
-= s
->buf_size
;
70 q
->bytesused
-= buf
->bytesused
- buf
->readpos
;
72 spin_unlock_irqrestore(&s
->qlock
, flags
);
76 static void ivtv_queue_move_buf(struct ivtv_stream
*s
, struct ivtv_queue
*from
,
77 struct ivtv_queue
*to
, int clear
)
79 struct ivtv_buffer
*buf
= list_entry(from
->list
.next
, struct ivtv_buffer
, list
);
81 list_move_tail(from
->list
.next
, &to
->list
);
83 from
->length
-= s
->buf_size
;
84 from
->bytesused
-= buf
->bytesused
- buf
->readpos
;
85 /* special handling for q_free */
87 buf
->bytesused
= buf
->readpos
= buf
->b_flags
= buf
->dma_xfer_cnt
= 0;
89 to
->length
+= s
->buf_size
;
90 to
->bytesused
+= buf
->bytesused
- buf
->readpos
;
93 /* Move 'needed_bytes' worth of buffers from queue 'from' into queue 'to'.
94 If 'needed_bytes' == 0, then move all buffers from 'from' into 'to'.
95 If 'steal' != NULL, then buffers may also taken from that queue if
96 needed, but only if 'from' is the free queue.
98 The buffer is automatically cleared if it goes to the free queue. It is
99 also cleared if buffers need to be taken from the 'steal' queue and
100 the 'from' queue is the free queue.
102 When 'from' is q_free, then needed_bytes is compared to the total
103 available buffer length, otherwise needed_bytes is compared to the
104 bytesused value. For the 'steal' queue the total available buffer
105 length is always used.
107 -ENOMEM is returned if the buffers could not be obtained, 0 if all
108 buffers where obtained from the 'from' list and if non-zero then
109 the number of stolen buffers is returned. */
110 int ivtv_queue_move(struct ivtv_stream
*s
, struct ivtv_queue
*from
, struct ivtv_queue
*steal
,
111 struct ivtv_queue
*to
, int needed_bytes
)
115 int from_free
= from
== &s
->q_free
;
116 int to_free
= to
== &s
->q_free
;
117 int bytes_available
, bytes_steal
;
119 spin_lock_irqsave(&s
->qlock
, flags
);
120 if (needed_bytes
== 0) {
122 needed_bytes
= from
->length
;
125 bytes_available
= from_free
? from
->length
: from
->bytesused
;
126 bytes_steal
= (from_free
&& steal
) ? steal
->length
: 0;
128 if (bytes_available
+ bytes_steal
< needed_bytes
) {
129 spin_unlock_irqrestore(&s
->qlock
, flags
);
132 while (steal
&& bytes_available
< needed_bytes
) {
133 struct ivtv_buffer
*buf
= list_entry(steal
->list
.prev
, struct ivtv_buffer
, list
);
134 u16 dma_xfer_cnt
= buf
->dma_xfer_cnt
;
136 /* move buffers from the tail of the 'steal' queue to the tail of the
137 'from' queue. Always copy all the buffers with the same dma_xfer_cnt
138 value, this ensures that you do not end up with partial frame data
139 if one frame is stored in multiple buffers. */
140 while (dma_xfer_cnt
== buf
->dma_xfer_cnt
) {
141 list_move_tail(steal
->list
.prev
, &from
->list
);
144 steal
->length
-= s
->buf_size
;
145 steal
->bytesused
-= buf
->bytesused
- buf
->readpos
;
146 buf
->bytesused
= buf
->readpos
= buf
->b_flags
= buf
->dma_xfer_cnt
= 0;
148 from
->length
+= s
->buf_size
;
149 bytes_available
+= s
->buf_size
;
150 if (list_empty(&steal
->list
))
152 buf
= list_entry(steal
->list
.prev
, struct ivtv_buffer
, list
);
156 u32 old_length
= to
->length
;
158 while (to
->length
- old_length
< needed_bytes
) {
159 ivtv_queue_move_buf(s
, from
, to
, 1);
163 u32 old_bytesused
= to
->bytesused
;
165 while (to
->bytesused
- old_bytesused
< needed_bytes
) {
166 ivtv_queue_move_buf(s
, from
, to
, to_free
);
169 spin_unlock_irqrestore(&s
->qlock
, flags
);
173 void ivtv_flush_queues(struct ivtv_stream
*s
)
175 ivtv_queue_move(s
, &s
->q_io
, NULL
, &s
->q_free
, 0);
176 ivtv_queue_move(s
, &s
->q_full
, NULL
, &s
->q_free
, 0);
177 ivtv_queue_move(s
, &s
->q_dma
, NULL
, &s
->q_free
, 0);
178 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_free
, 0);
181 int ivtv_stream_alloc(struct ivtv_stream
*s
)
183 struct ivtv
*itv
= s
->itv
;
184 int SGsize
= sizeof(struct ivtv_sg_host_element
) * s
->buffers
;
190 IVTV_DEBUG_INFO("Allocate %s%s stream: %d x %d buffers (%dkB total)\n",
191 s
->dma
!= PCI_DMA_NONE
? "DMA " : "",
192 s
->name
, s
->buffers
, s
->buf_size
, s
->buffers
* s
->buf_size
/ 1024);
194 s
->sg_pending
= kzalloc(SGsize
, GFP_KERNEL
|__GFP_NOWARN
);
195 if (s
->sg_pending
== NULL
) {
196 IVTV_ERR("Could not allocate sg_pending for %s stream\n", s
->name
);
199 s
->sg_pending_size
= 0;
201 s
->sg_processing
= kzalloc(SGsize
, GFP_KERNEL
|__GFP_NOWARN
);
202 if (s
->sg_processing
== NULL
) {
203 IVTV_ERR("Could not allocate sg_processing for %s stream\n", s
->name
);
204 kfree(s
->sg_pending
);
205 s
->sg_pending
= NULL
;
208 s
->sg_processing_size
= 0;
210 s
->sg_dma
= kzalloc(sizeof(struct ivtv_sg_element
),
211 GFP_KERNEL
|__GFP_NOWARN
);
212 if (s
->sg_dma
== NULL
) {
213 IVTV_ERR("Could not allocate sg_dma for %s stream\n", s
->name
);
214 kfree(s
->sg_pending
);
215 s
->sg_pending
= NULL
;
216 kfree(s
->sg_processing
);
217 s
->sg_processing
= NULL
;
220 if (ivtv_might_use_dma(s
)) {
221 s
->sg_handle
= pci_map_single(itv
->pdev
, s
->sg_dma
,
222 sizeof(struct ivtv_sg_element
), PCI_DMA_TODEVICE
);
223 ivtv_stream_sync_for_cpu(s
);
226 /* allocate stream buffers. Initially all buffers are in q_free. */
227 for (i
= 0; i
< s
->buffers
; i
++) {
228 struct ivtv_buffer
*buf
= kzalloc(sizeof(struct ivtv_buffer
),
229 GFP_KERNEL
|__GFP_NOWARN
);
233 buf
->buf
= kmalloc(s
->buf_size
+ 256, GFP_KERNEL
|__GFP_NOWARN
);
234 if (buf
->buf
== NULL
) {
238 INIT_LIST_HEAD(&buf
->list
);
239 if (ivtv_might_use_dma(s
)) {
240 buf
->dma_handle
= pci_map_single(s
->itv
->pdev
,
241 buf
->buf
, s
->buf_size
+ 256, s
->dma
);
242 ivtv_buf_sync_for_cpu(s
, buf
);
244 ivtv_enqueue(s
, buf
, &s
->q_free
);
248 IVTV_ERR("Couldn't allocate buffers for %s stream\n", s
->name
);
253 void ivtv_stream_free(struct ivtv_stream
*s
)
255 struct ivtv_buffer
*buf
;
257 /* move all buffers to q_free */
258 ivtv_flush_queues(s
);
261 while ((buf
= ivtv_dequeue(s
, &s
->q_free
))) {
262 if (ivtv_might_use_dma(s
))
263 pci_unmap_single(s
->itv
->pdev
, buf
->dma_handle
,
264 s
->buf_size
+ 256, s
->dma
);
269 /* Free SG Array/Lists */
270 if (s
->sg_dma
!= NULL
) {
271 if (s
->sg_handle
!= IVTV_DMA_UNMAPPED
) {
272 pci_unmap_single(s
->itv
->pdev
, s
->sg_handle
,
273 sizeof(struct ivtv_sg_element
), PCI_DMA_TODEVICE
);
274 s
->sg_handle
= IVTV_DMA_UNMAPPED
;
276 kfree(s
->sg_pending
);
277 kfree(s
->sg_processing
);
279 s
->sg_pending
= NULL
;
280 s
->sg_processing
= NULL
;
282 s
->sg_pending_size
= 0;
283 s
->sg_processing_size
= 0;