/
tsthread.c
331 lines (297 loc) · 8.11 KB
/
tsthread.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* SunPTV-USB (c) 2016 trinity19683
TS USB I/O thread (Linux OSes)
tsthread.c
2016-02-05
*/
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <string.h>
#include <poll.h>
#include <pthread.h>
#include "usbops.h"
#include "osdepend.h"
#include "tsbuff.h"
#include "tsthread.h"
#include "message.h"
#define ROUNDUP(n,w) (((n) + (w)) & ~(unsigned)(w))
#define NUM_ISOC_PACKET 16
struct tsthread_param {
void* thread_ptr; //# pointer to thread data
unsigned char volatile flags;
/* if 0x01 flagged, issue a new request.
if 0x02 flagged, cancel requests and stop thread.
*/
const struct usb_endpoint_st* pUSB;
void* buffer; //# data buffer (in heap memory)
int* actual_length; //# actual length of each buffer block
unsigned buff_unitSize;
int buff_num;
int buff_push;
int buff_pop;
};
static int submitNextURB(struct tsthread_param* const ps, struct usbdevfs_urb* const urbp)
{
//# isochronous URB request
int ret = 0, j;
memset(urbp, 0, sizeof(struct usbdevfs_urb) + sizeof(struct usbdevfs_iso_packet_desc) * NUM_ISOC_PACKET );
urbp->type = (ps->pUSB->endpoint & 0x100) ? USBDEVFS_URB_TYPE_ISO : USBDEVFS_URB_TYPE_BULK;
urbp->endpoint = ps->pUSB->endpoint & 0xFF;
urbp->usercontext = &(ps->actual_length[ps->buff_push]);
urbp->buffer = ps->buffer + (ps->buff_push * ps->buff_unitSize);
if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
urbp->buffer_length = ps->buff_unitSize;
ps->actual_length[ps->buff_push] = -2;
}else{
urbp->number_of_packets = NUM_ISOC_PACKET;
urbp->flags = USBDEVFS_URB_ISO_ASAP;
for(j = 0; j < NUM_ISOC_PACKET; j++ ) {
urbp->iso_frame_desc[j].length = ps->buff_unitSize;
ps->actual_length[ps->buff_push + j] = -2;
}
}
if(ioctl(ps->pUSB->fd, USBDEVFS_SUBMITURB, urbp) < 0) {
ret = -errno;
warn_info(errno,"submitURB failed");
urbp->buffer = NULL;
}else{
int next_index = ps->buff_push;
if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
next_index ++;
}else{
next_index += NUM_ISOC_PACKET;
}
ps->buff_push = (next_index < ps->buff_num) ? next_index : 0;
}
return ret;
}
static int reapURB(struct tsthread_param* const ps)
{
struct usbdevfs_urb *urbp;
int i = 0, j, ret;
for(;;) {
if((ret = ioctl(ps->pUSB->fd, USBDEVFS_REAPURBNDELAY, (void *)(&urbp)) ) < 0) {
if(EAGAIN == errno || EWOULDBLOCK == errno) {
//# no more data to be received
ret = -EAGAIN;
}else{
warn_info(errno,"reapURB_ndelay failed");
ret = -errno;
}
}
if(0 > ret) break;
//# received
int* const pLen = urbp->usercontext;
if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
if(0 == urbp->status) {
//# success
int actual_length = urbp->actual_length;
if(ps->buff_unitSize < actual_length) {
warn_info(actual_length,"reapURB overflow");
actual_length = ps->buff_unitSize;
}
pLen[0] = actual_length;
}else{
//# failed
pLen[0] = 0;
warn_info(urbp->status,"reapURB");
}
}else{
const int numPkts = urbp->number_of_packets;
for(j = 0; j < numPkts; j++ ) {
struct usbdevfs_iso_packet_desc* const isocDesc = &(urbp->iso_frame_desc[j]);
if(0 == isocDesc->status) {
//# success
int actual_length = isocDesc->actual_length;
if(ps->buff_unitSize < actual_length) {
warn_info(actual_length,"reapURB overflow");
actual_length = ps->buff_unitSize;
}
pLen[j] = actual_length;
}else{
//# failed
pLen[j] = 0;
warn_info(isocDesc->status,"reapURB");
}
}
}
//# mark this URB unused
urbp->buffer = NULL;
i++;
}
if(-EAGAIN != ret) {
return ret;
}
return i;
}
/* TS thread function issues URB requests. */
static void tsthread(void* const param)
{
struct tsthread_param* const ps = param;
int ret, i;
const unsigned UrbSize = ROUNDUP( sizeof(struct usbdevfs_urb) + sizeof(struct usbdevfs_iso_packet_desc) * NUM_ISOC_PACKET ,0xF);
void* ptrUrb;
ptrUrb = uHeapAlloc(UrbSize * TS_MaxNumIO);
if(! ptrUrb) {
warn_info(errno,"failed to allocate");
return;
}
ps->buff_push = 0;
for(i = 0; i < TS_MaxNumIO; i++) {
struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
//# mark this URB unused
pUrb->buffer = NULL;
}
for(;;) {
if(!(ps->flags & 0x01)) {
for(i = 0; i < TS_MaxNumIO; i++) {
struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
if(! pUrb->buffer) {
//# continue to issue a new URB request
submitNextURB(ps, pUrb);
}
}
}
if(ps->flags & 0x02) {
//# canceled
reapURB(ps);
for(i = 0; i < TS_MaxNumIO; i++) {
struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
if(! pUrb->buffer) continue;
if(ioctl(ps->pUSB->fd, USBDEVFS_DISCARDURB, pUrb) < 0) {
warn_info(errno,"failed to discard URB");
}
}
break;
}
struct pollfd pfd;
pfd.fd = ps->pUSB->fd;
pfd.events = POLLOUT | POLLERR;
ret = poll(&pfd, 1, TS_PollTimeout);
if(0 > ret) {
warn_info(errno,"poll failed");
break;
}else{
if(0 == ret) {
//# timeout
if(!(ps->flags & 0x01)) dmsg("poll timeout");
}
if(reapURB(ps) < 0) break;
}
}
uHeapFree(ptrUrb);
ps->flags |= 0x01; //# continue = F
}
/* public function */
int tsthread_create(tsthread_ptr* const tptr, const struct usb_endpoint_st* const pusbep)
{
struct tsthread_param* ps;
{//#
const unsigned param_size = ROUNDUP(sizeof(struct tsthread_param), 0xF);
const unsigned buffer_size = ROUNDUP(TS_BufSize ,0xF);
const unsigned unitSize = ROUNDUP(pusbep->xfer_size ,0x1FF);
const unsigned unitNum = TS_BufSize / unitSize;
const unsigned actlen_size = sizeof(int) * unitNum;
unsigned totalSize = param_size + actlen_size + buffer_size;
void* ptr = uHeapAlloc( totalSize );
if(! ptr) {
warn_msg(errno,"failed to allocate TS buffer");
return -1;
}
void* const buffer_ptr = ptr;
ptr += buffer_size;
ps = ptr;
ps->buffer = buffer_ptr;
ptr += param_size;
ps->actual_length = ptr;
//ptr += actlen_size;
ps->buff_num = unitNum;
ps->actual_length[0] = -1; //# the first block is pending
if(pusbep->endpoint & 0x100) { //# Isochronous
ps->buff_unitSize = pusbep->xfer_size;
}else{
ps->buff_unitSize = unitSize;
}
}
ps->pUSB = pusbep;
ps->flags = 0;
ps->buff_pop = 0;
tsthread_start(ps);
if(pthread_create((pthread_t *)&ps->thread_ptr, NULL, (void *)tsthread, ps) != 0) {
warn_info(errno,"pthread_create failed");
uHeapFree(ps->buffer);
return -1;
}
*tptr = ps;
return 0;
}
void tsthread_destroy(const tsthread_ptr ptr)
{
struct tsthread_param* const p = ptr;
p->flags |= 0x03; //# continue = F, canceled = T
if(pthread_join((pthread_t)p->thread_ptr, NULL) != 0) {
warn_info(errno,"pthread_join failed");
}
uHeapFree(p->buffer);
}
void tsthread_start(const tsthread_ptr ptr)
{
struct tsthread_param* const p = ptr;
p->flags &= ~0x03; //# continue = T, canceled = F
if(p->pUSB->startstopFunc)
p->pUSB->startstopFunc(p->pUSB->dev, 1);
}
void tsthread_stop(const tsthread_ptr ptr)
{
struct tsthread_param* const p = ptr;
p->flags |= 0x01; //# continue = F
if(p->pUSB->startstopFunc)
p->pUSB->startstopFunc(p->pUSB->dev, 0);
}
int tsthread_read(const tsthread_ptr tptr, void ** const ptr)
{
struct tsthread_param* const ps = tptr;
int i, j;
i = tsthread_readable(tptr);
if(0 >= i) return 0;
j = ps->buff_pop;
ps->actual_length[ps->buff_pop] = -1;
if(ptr) {
*ptr = ps->buffer + (j * ps->buff_unitSize);
ps->buff_pop = (ps->buff_num - 1 > j) ? j + 1 : 0;
}else{
ps->actual_length[ps->buff_push] = -1;
ps->buff_pop = ps->buff_push;
}
return i;
}
int tsthread_readable(const tsthread_ptr tptr)
{
struct tsthread_param* const ps = tptr;
int j = ps->buff_pop;
if(0 > j || ps->buff_num <= j) { //# bug check
warn_info(j,"ts.buff_pop Out of range");
ps->buff_pop = 0;
return 0;
}
do { //# skip empty blocks
if(0 != ps->actual_length[j]) break;
j = (ps->buff_num -1 > j) ? j + 1 : 0;
} while(j != ps->buff_pop);
ps->buff_pop = j;
return ps->actual_length[j];
}
int tsthread_wait(const tsthread_ptr tptr, const int timeout)
{
struct tsthread_param* const ps = tptr;
int ret;
struct pollfd pfd;
pfd.fd = ps->pUSB->fd;
pfd.events = POLLOUT | POLLERR;
ret = poll(&pfd, 1, timeout);
if(0 > ret) {
warn_info(errno,"poll failed");
}
return ret;
}
/*EOF*/