summaryrefslogtreecommitdiff
path: root/host/ausb/ausb.c
blob: 8b0117f26a6b83f18aa0c0a117b5212753cad0f5 (plain)
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
/* Wrapper/Extension code to libusb-0.1 to support asynchronous requests
 * on Linux platforns 
 *
 * (C) 2004-2005 by Harald Welte <laforge@gnumonks.org>
 *
 * Distributed and licensed under the terms of GNU LGPL, Version 2.1
 */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>

#include "ausb.h"
#include "usb.h"

#ifdef DEBUG_AUSB
#define DEBUGP(x, args...)	fprintf(stderr, "%s:%s():%u " x, __FILE__, \
					__FUNCTION__, __LINE__, ## args)
#else
#define DEBUGP(x, args...);
#endif

#define SIGAUSB		(SIGRTMIN+1)

static int ausb_get_fd(ausb_dev_handle *ah)
{
	return *((int *)ah->uh);
}


static int kernel_2_5;

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

static int is_kernel_2_5()
{
	struct utsname uts;
	int ret;

	uname(&uts);

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

	return ret;
}

void ausb_dump_urb(struct usbdevfs_urb *uurb)
{
	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);
}

void ausb_fill_int_urb(struct usbdevfs_urb *uurb, unsigned char endpoint,
		      void *buffer, int buffer_length)
				    
{
	uurb->type = kernel_2_5 ? USBDEVFS_URB_TYPE_INTERRUPT : USBDEVFS_URB_TYPE_BULK;
	uurb->endpoint = endpoint; /* | USB_DIR_IN; */
	uurb->flags = kernel_2_5 ? 0 : 1 ; /* USBDEVFS_URB_QUEUE_BULK; */
	uurb->buffer = buffer;
	uurb->buffer_length = buffer_length;
	uurb->signr = SIGAUSB;
	uurb->start_frame = -1;
}

int ausb_submit_urb(ausb_dev_handle *ah, struct usbdevfs_urb *uurb)
{
	int ret;

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

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

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

	return ret;
}

int ausb_discard_urb(ausb_dev_handle *ah, struct usbdevfs_urb *uurb)
{
	int ret;

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

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

	return ret;
}

struct usbdevfs_urb *ausb_get_urb(ausb_dev_handle *ah)
{
	int ret;
	struct usbdevfs_urb *uurb;

	DEBUGP("entering\n");

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

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

	DEBUGP("returning %p\n", uurb);
	return uurb;
}

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

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

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

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

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

static void sigact_rtmin(int sig, siginfo_t *siginfo, void *v)
{
	int count;
	struct usbdevfs_urb *urb;

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

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

	if (!last_handle)
		return;

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

		if (urb == NULL) {
			DEBUGP("ausb_get_urb() returned urb==NULL\n");
			break;
		}

		DEBUGP("calling handle_urb(%p)\n", urb);
		handle_urb(urb);
	}
}


int ausb_init(void)
{
	struct sigaction act;

	DEBUGP("entering\n");

	memset(&act, 0, sizeof(act));
	act.sa_sigaction = sigact_rtmin;
	sigemptyset(&act.sa_mask);
	act.sa_flags = SA_SIGINFO;

	sigaction(SIGAUSB, &act, NULL);

	kernel_2_5 = is_kernel_2_5();

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

	usb_init();
	usb_find_busses();
	usb_find_devices();
	return 1;
}

ausb_dev_handle *ausb_open(struct usb_device *dev)
{
	ausb_dev_handle *dh = malloc(sizeof *dh);

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

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

	dh->uh = usb_open(dev);

	if (!dh->uh) {
		DEBUGP("usb_open() failed\n");
		free(dh);
		return NULL;
	}

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

	return dh;
}

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

	if (type >= AUSB_USBDEVFS_URB_TYPES) {
		errno = EINVAL;
		return -1;
	}

	if (!kernel_2_5 && type == USBDEVFS_URB_TYPE_INTERRUPT)
		type = USBDEVFS_URB_TYPE_BULK;

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

	return 0;
}

int ausb_claim_interface(ausb_dev_handle *ah, int interface)
{
	DEBUGP("entering\n");
	return usb_claim_interface(ah->uh, interface);
}

int ausb_release_interface(ausb_dev_handle *ah, int interface)
{
	DEBUGP("entering\n");
	/* what the hell? */
	if (ah == last_handle)
		last_handle = NULL;
	return usb_release_interface(ah->uh, interface);
}

int ausb_set_configuration(ausb_dev_handle *ah, int configuration)
{
	DEBUGP("entering\n");
	return usb_set_configuration(ah->uh, configuration);
}

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

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

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

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

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

#ifdef LIBUSB_HAS_GET_DRIVER_NP
int ausb_get_driver_np(ausb_dev_handle *ah, int interface, char *name,
		       unsigned int namelen)
{
	DEBUGP("entering\n");
	return usb_get_driver_np(ah->uh, interface, name, namelen);
}
#endif

#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
int ausb_detach_kernel_driver_np(ausb_dev_handle *ah, int interface)
{
	DEBUGP("entering\n");
	return usb_detach_kernel_driver_np(ah->uh, interface);
}
int ausb_reattach_kernel_driver_np(ausb_dev_handle *ah, int interface)
{
	DEBUGP("entering\n");
	return __usb_reattach_kernel_driver_np(ah->uh, interface);
}
#endif

int ausb_close(struct ausb_dev_handle *ah)
{
	DEBUGP("entering\n");

	if (ah == last_handle)
		last_handle = NULL;

	return usb_close(ah->uh);
}

void ausb_fini(void)
{
	DEBUGP("entering\n");
	sigaction(SIGAUSB, NULL, NULL);
}
personal git repositories of Harald Welte. Your mileage may vary