Project

General

Profile

Download (6.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Wrapper/Extension code to libusb-0.1 to support asynchronous requests
2
 * on Linux platforns 
3
 *
4
 * (C) 2004-2005 by Harald Welte <laforge@gnumonks.org>
5
 *
6
 * Distributed and licensed under the terms of GNU LGPL, Version 2.1
7
 */
8

    
9
#include <string.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
#include <stdio.h>
13
#include <signal.h>
14
#include <errno.h>
15
#include <sys/utsname.h>
16
#include <sys/ioctl.h>
17

    
18
#include "ausb.h"
19
#include "usb.h"
20

    
21
#ifdef DEBUG_AUSB
22
#define DEBUGP(x, args...)	fprintf(stderr, "%s:%s():%u " x, __FILE__, \
23
					__FUNCTION__, __LINE__, ## args)
24
#else
25
#define DEBUGP(x, args...);
26
#endif
27

    
28
#define SIGAUSB		(SIGRTMIN+1)
29

    
30
static int ausb_get_fd(ausb_dev_handle *ah)
31
{
32
	return *((int *)ah->uh);
33
}
34

    
35

    
36
static int kernel_2_5;
37

    
38
/* FIXME: this has to go */
39
static struct ausb_dev_handle *last_handle = NULL;
40

    
41
static int is_kernel_2_5()
42
{
43
	struct utsname uts;
44
	int ret;
45

    
46
	uname(&uts);
47

    
48
	ret = (strncmp(uts.release, "2.5.", 4) == 0) ||
49
	       (strncmp(uts.release, "2.6.", 4) == 0);
50

    
51
	return ret;
52
}
53

    
54
void ausb_dump_urb(struct usbdevfs_urb *uurb)
55
{
56
	DEBUGP("urb(%p): type=%u, endpoint=0x%x, status=%d, flags=0x%x, number_of_packets=%d, error_count=%d\n", uurb, uurb->type, uurb->endpoint, uurb->status, uurb->flags, uurb->number_of_packets, uurb->error_count);
57
}
58

    
59
void ausb_fill_int_urb(struct usbdevfs_urb *uurb, unsigned char endpoint,
60
		      void *buffer, int buffer_length)
61
				    
62
{
63
	uurb->type = kernel_2_5 ? USBDEVFS_URB_TYPE_INTERRUPT : USBDEVFS_URB_TYPE_BULK;
64
	uurb->endpoint = endpoint; /* | USB_DIR_IN; */
65
	uurb->flags = kernel_2_5 ? 0 : 1 ; /* USBDEVFS_URB_QUEUE_BULK; */
66
	uurb->buffer = buffer;
67
	uurb->buffer_length = buffer_length;
68
	uurb->signr = SIGAUSB;
69
	uurb->start_frame = -1;
70
}
71

    
72
int ausb_submit_urb(ausb_dev_handle *ah, struct usbdevfs_urb *uurb)
73
{
74
	int ret;
75

    
76
	DEBUGP("ah=%p\n", ah);
77
	ausb_dump_urb(uurb);
78

    
79
	/* save ausb_dev_handle in opaque usercontext field */
80
	uurb->usercontext = ah;
81

    
82
	do {
83
		ret = ioctl(ausb_get_fd(ah), USBDEVFS_SUBMITURB, uurb);
84
	} while (ret < 0 && errno == EINTR);
85

    
86
	return ret;
87
}
88

    
89
int ausb_discard_urb(ausb_dev_handle *ah, struct usbdevfs_urb *uurb)
90
{
91
	int ret;
92

    
93
	DEBUGP("ah=%p, uurb=%p\n");
94
	ausb_dump_urb(uurb);
95

    
96
	do {
97
		ret = ioctl(ausb_get_fd(ah), USBDEVFS_DISCARDURB, uurb);
98
	} while (ret < 0 && errno == EINTR);
99

    
100
	return ret;
101
}
102

    
103
struct usbdevfs_urb *ausb_get_urb(ausb_dev_handle *ah)
104
{
105
	int ret;
106
	struct usbdevfs_urb *uurb;
107

    
108
	DEBUGP("entering\n");
109

    
110
	do {
111
		ret = ioctl(ausb_get_fd(ah), USBDEVFS_REAPURBNDELAY, &uurb);
112
	} while (ret < 0 && errno == EINTR);
113

    
114
	if (ret < 0 && errno == EAGAIN) {
115
		DEBUGP("ioctl returned %d (errno=%s)\n", ret,
116
			strerror(errno));
117
		return NULL;
118
	}
119

    
120
	DEBUGP("returning %p\n", uurb);
121
	return uurb;
122
}
123

    
124
static void handle_urb(struct usbdevfs_urb *uurb)
125
{
126
	struct ausb_dev_handle *ah = uurb->usercontext;
127

    
128
	DEBUGP("called, ah=%p\n", ah);
129

    
130
	if (uurb->type >= AUSB_USBDEVFS_URB_TYPES) {
131
		DEBUGP("unknown uurb type %u\n", uurb->type);
132
		return;
133
	}
134

    
135
	if (!ah) {
136
		DEBUGP("cant't call handler because missing ah ptr\n");
137
		return;
138
	}
139

    
140
	if (!ah->cb[uurb->type].handler) {
141
		DEBUGP("received URB type %u, but no handler\n", uurb->type);
142
		return;
143
	}
144
	ah->cb[uurb->type].handler(uurb, ah->cb[uurb->type].userdata);
145
}
146

    
147
static void sigact_rtmin(int sig, siginfo_t *siginfo, void *v)
148
{
149
	int count;
150
	struct usbdevfs_urb *urb;
151

    
152
	if (sig != SIGAUSB)
153
		return;
154
 
155
	//DEBUGP("errno=%d, si_addr=%p\n", siginfo->errno, siginfo->si_addr);
156

    
157
	DEBUGP("last_handle=%p\n", last_handle);
158

    
159
	if (!last_handle)
160
		return;
161

    
162
	for (count = 0; ; count++) {
163
		urb = ausb_get_urb(last_handle);
164

    
165
		if (urb == NULL) {
166
			DEBUGP("ausb_get_urb() returned urb==NULL\n");
167
			break;
168
		}
169

    
170
		DEBUGP("calling handle_urb(%p)\n", urb);
171
		handle_urb(urb);
172
	}
173
}
174

    
175

    
176
int ausb_init(void)
177
{
178
	struct sigaction act;
179

    
180
	DEBUGP("entering\n");
181

    
182
	memset(&act, 0, sizeof(act));
183
	act.sa_sigaction = sigact_rtmin;
184
	sigemptyset(&act.sa_mask);
185
	act.sa_flags = SA_SIGINFO;
186

    
187
	sigaction(SIGAUSB, &act, NULL);
188

    
189
	kernel_2_5 = is_kernel_2_5();
190

    
191
	DEBUGP("kernel 2.5+ = %d\n", kernel_2_5);
192

    
193
	usb_init();
194
	usb_find_busses();
195
	usb_find_devices();
196
	return 1;
197
}
198

    
199
ausb_dev_handle *ausb_open(struct usb_device *dev)
200
{
201
	ausb_dev_handle *dh = malloc(sizeof *dh);
202

    
203
	DEBUGP("entering, dh=%p\n", dh);
204

    
205
	memset(dh, 0, sizeof(*dh));
206

    
207
	dh->uh = usb_open(dev);
208

    
209
	if (!dh->uh) {
210
		DEBUGP("usb_open() failed\n");
211
		free(dh);
212
		return NULL;
213
	}
214

    
215
	last_handle = dh;
216
	DEBUGP("last_handle = %p\n", last_handle);
217

    
218
	return dh;
219
}
220

    
221
int ausb_register_callback(ausb_dev_handle *ah, unsigned char type,
222
			   void (*callback)(struct usbdevfs_urb *uurb,
223
					    void *userdata),
224
			   void *userdata)
225
{
226
	DEBUGP("registering callback for type %u:%p\n", type, callback);
227

    
228
	if (type >= AUSB_USBDEVFS_URB_TYPES) {
229
		errno = EINVAL;
230
		return -1;
231
	}
232

    
233
	if (!kernel_2_5 && type == USBDEVFS_URB_TYPE_INTERRUPT)
234
		type = USBDEVFS_URB_TYPE_BULK;
235

    
236
	ah->cb[type].handler = callback;
237
	ah->cb[type].userdata = userdata;
238

    
239
	return 0;
240
}
241

    
242
int ausb_claim_interface(ausb_dev_handle *ah, int interface)
243
{
244
	DEBUGP("entering\n");
245
	return usb_claim_interface(ah->uh, interface);
246
}
247

    
248
int ausb_release_interface(ausb_dev_handle *ah, int interface)
249
{
250
	DEBUGP("entering\n");
251
	/* what the hell? */
252
	if (ah == last_handle)
253
		last_handle = NULL;
254
	return usb_release_interface(ah->uh, interface);
255
}
256

    
257
int ausb_set_configuration(ausb_dev_handle *ah, int configuration)
258
{
259
	DEBUGP("entering\n");
260
	return usb_set_configuration(ah->uh, configuration);
261
}
262

    
263
int ausb_bulk_write(ausb_dev_handle *ah, int ep, char *bytes, int size, int timeout)
264
{
265
	DEBUGP("entering (ah=%p, ep=0x%x, bytes=%p, size=%d, timeout=%d\n",
266
		ah, ep, bytes, size, timeout);
267
	return __usb_bulk_write(ah->uh, ep, bytes, size, timeout);
268
}
269

    
270
int ausb_bulk_read(ausb_dev_handle *ah, int ep, char *bytes, int size, int timeout)
271
{
272
	DEBUGP("entering (ah=%p, ep=0x%x, bytes=%p, size=%d, timeout=%d\n",
273
		ah, ep, bytes, size, timeout);
274
	return __usb_bulk_read(ah->uh, ep, bytes, size, timeout);
275
}
276

    
277
int ausb_clear_halt(ausb_dev_handle *ah, unsigned int ep)
278
{
279
	DEBUGP("entering (ah=%p, ep=0x%x)\n", ah, ep);
280
	return usb_clear_halt(ah->uh, ep);
281
}
282

    
283
int ausb_reset(ausb_dev_handle *ah)
284
{
285
	DEBUGP("entering (ah=%p)\n", ah);
286
	return usb_reset(ah->uh);
287
}
288

    
289
int ausb_resetep(ausb_dev_handle *ah, int ep)
290
{
291
	DEBUGP("entering (ah=%pm ep=0x%x)\n", ah, ep);
292
	return usb_resetep(ah->uh, ep);
293
}
294

    
295
#ifdef LIBUSB_HAS_GET_DRIVER_NP
296
int ausb_get_driver_np(ausb_dev_handle *ah, int interface, char *name,
297
		       unsigned int namelen)
298
{
299
	DEBUGP("entering\n");
300
	return usb_get_driver_np(ah->uh, interface, name, namelen);
301
}
302
#endif
303

    
304
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
305
int ausb_detach_kernel_driver_np(ausb_dev_handle *ah, int interface)
306
{
307
	DEBUGP("entering\n");
308
	return usb_detach_kernel_driver_np(ah->uh, interface);
309
}
310
int ausb_reattach_kernel_driver_np(ausb_dev_handle *ah, int interface)
311
{
312
	DEBUGP("entering\n");
313
	return __usb_reattach_kernel_driver_np(ah->uh, interface);
314
}
315
#endif
316

    
317
int ausb_close(struct ausb_dev_handle *ah)
318
{
319
	DEBUGP("entering\n");
320

    
321
	if (ah == last_handle)
322
		last_handle = NULL;
323

    
324
	return usb_close(ah->uh);
325
}
326

    
327
void ausb_fini(void)
328
{
329
	DEBUGP("entering\n");
330
	sigaction(SIGAUSB, NULL, NULL);
331
}
(2-2/6)
Add picture from clipboard (Maximum size: 48.8 MB)