-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhdcapm-buffer.c
218 lines (183 loc) · 5.9 KB
/
hdcapm-buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Driver for the Startech USB2HDCAPM USB capture device
*
* Copyright (c) 2017 Steven Toth <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* GNU General Public License for more details.
*/
#include "hdcapm.h"
struct hdcapm_buffer *hdcapm_buffer_alloc(struct hdcapm_dev *dev, u32 nr, u32 maxsize)
{
struct hdcapm_buffer *buf;
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf)
return NULL;
buf->nr = nr;
buf->dev = dev;
buf->maxsize = maxsize;
buf->ptr = kzalloc(maxsize, GFP_KERNEL);
if (!buf->ptr) {
kfree(buf);
return NULL;
}
return buf;
}
void hdcapm_buffer_free(struct hdcapm_buffer *buf)
{
if (buf->ptr) {
kfree(buf->ptr);
buf->ptr = NULL;
}
if (buf->urb) {
usb_free_urb(buf->urb);
buf->urb = NULL;
}
kfree(buf);
}
/* Helper macros for managing the device lists.
* Caller is responsible for holding the mutex.
*/
void hdcapm_buffers_move_all(struct hdcapm_dev *dev, struct list_head *to, struct list_head *from)
{
struct hdcapm_buffer *buf;
mutex_lock(&dev->dmaqueue_lock);
while (!list_empty(from)) {
buf = list_first_entry(from, struct hdcapm_buffer, list);
if (buf)
list_move_tail(&buf->list, to);
}
mutex_unlock(&dev->dmaqueue_lock);
}
/* Helper macros for managing the device lists.
* Caller is responsible for holding the mutex.
*/
void hdcapm_buffers_free_all(struct hdcapm_dev *dev, struct list_head *head)
{
struct hdcapm_buffer *buf;
mutex_lock(&dev->dmaqueue_lock);
while (!list_empty(head)) {
buf = list_first_entry(head, struct hdcapm_buffer, list);
if (buf) {
list_del(&buf->list);
hdcapm_buffer_free(buf);
}
}
mutex_unlock(&dev->dmaqueue_lock);
}
/* Helper macros for managing the device lists.
* We WILL take the mutex in this func.
* Return a reference to the top most used buffer, we're going to
* read some or all of it (probably). Don't delete it from the list.
*/
struct hdcapm_buffer *hdcapm_buffer_peek_used(struct hdcapm_dev *dev)
{
struct hdcapm_buffer *buf = NULL;
mutex_lock(&dev->dmaqueue_lock);
if (!list_empty(&dev->list_buf_used)) {
buf = list_first_entry(&dev->list_buf_used, struct hdcapm_buffer, list);
}
mutex_unlock(&dev->dmaqueue_lock);
dprintk(3, "%s() returns %p\n", __func__, buf);
return buf;
}
static struct hdcapm_buffer *hdcapm_buffer_next_used(struct hdcapm_dev *dev)
{
struct hdcapm_buffer *buf = NULL;
mutex_lock(&dev->dmaqueue_lock);
if (!list_empty(&dev->list_buf_used)) {
buf = list_first_entry(&dev->list_buf_used, struct hdcapm_buffer, list);
list_del(&buf->list);
}
mutex_unlock(&dev->dmaqueue_lock);
dprintk(3, "%s() returns %p\n", __func__, buf);
return buf;
}
/* Pull the top buffer from the free list, but don't specifically remove it from the
* list. If no buffer exists, steal one from the used list.
* We WILL take the mutex in this func.
* Return the buffer at the top of the free list, delete the list node.
* We're probably going to fill it and move it to the used list.
* IF no free buffers exist, steal one from the used list and flag an internal
* data loss statistic.
*/
struct hdcapm_buffer *hdcapm_buffer_next_free(struct hdcapm_dev *dev)
{
struct hdcapm_buffer *buf = NULL;
mutex_lock(&dev->dmaqueue_lock);
if (!list_empty(&dev->list_buf_free)) {
buf = list_first_entry(&dev->list_buf_free, struct hdcapm_buffer, list);
list_del(&buf->list);
}
mutex_unlock(&dev->dmaqueue_lock);
if (!buf) {
printk(KERN_WARNING "%s() No empty buffers, data loss will occur. Increase param buffer_count.\n", __func__);
buf = hdcapm_buffer_next_used(dev);
if (!buf) {
printk(KERN_ERR "%s() Driver madness, no free or empty buffers.\n", __func__);
}
dev->stats->buffer_overrun++;
}
dprintk(3, "%s() returns %p\n", __func__, buf);
return buf;
}
static void hdcapm_buffer_add_to_list(struct hdcapm_dev *dev, struct hdcapm_buffer *buf, struct list_head *list)
{
mutex_lock(&dev->dmaqueue_lock);
list_add_tail(&buf->list, list);
mutex_unlock(&dev->dmaqueue_lock);
}
__inline__ void hdcapm_buffer_add_to_free(struct hdcapm_dev *dev, struct hdcapm_buffer *buf)
{
hdcapm_buffer_add_to_list(dev, buf, &dev->list_buf_free);
}
__inline__ void hdcapm_buffer_add_to_used(struct hdcapm_dev *dev, struct hdcapm_buffer *buf)
{
hdcapm_buffer_add_to_list(dev, buf, &dev->list_buf_used);
}
static __inline__ void hdcapm_buffer_move(struct hdcapm_dev *dev, struct hdcapm_buffer *buf, struct list_head *list)
{
mutex_lock(&dev->dmaqueue_lock);
list_move_tail(&buf->list, list);
mutex_unlock(&dev->dmaqueue_lock);
}
/* Helper macros for moving a buffer to the free list.
* We WILL take the mutex in this func.
*/
void hdcapm_buffer_move_to_free(struct hdcapm_dev *dev, struct hdcapm_buffer *buf)
{
hdcapm_buffer_move(dev, buf, &dev->list_buf_free);
}
/* Helper macros for moving a buffer to the used list.
* We WILL take the mutex in this func.
*/
void hdcapm_buffer_move_to_used(struct hdcapm_dev *dev, struct hdcapm_buffer *buf)
{
hdcapm_buffer_move(dev, buf, &dev->list_buf_used);
}
/* For debugging. Lock the buffer queue and measure how much data (in bytes)
* and how many items are on the list.
*/
int hdcapm_buffer_used_queue_stats(struct hdcapm_dev *dev, u64 *bytes, u64 *items)
{
struct hdcapm_buffer *buf = NULL;
struct list_head *p = NULL, *q = NULL;
*bytes = 0;
*items = 0;
mutex_lock(&dev->dmaqueue_lock);
list_for_each_safe(p, q, &dev->list_buf_used) {
buf = list_entry(p, struct hdcapm_buffer, list);
(*bytes) += (buf->actual_size - buf->readpos);
(*items)++;
}
mutex_unlock(&dev->dmaqueue_lock);
return 0;
}