libusbi.c 8.66 KB
Newer Older
Igor Pečovnik's avatar
Igor Pečovnik committed
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
332
333
334
335
336
337
// libusbi.h: libusb wrapper
// This file is part of scanbuttond.
// Copyleft )c( 2004-2006 by Bernhard Stiftner
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <usb.h>
#include <syslog.h>
#include "scanbuttond/libusbi.h"

#define TIMEOUT	   	10 * 1000	/* 10 seconds */

int invocation_count = 0;


libusb_handle_t* libusb_init(void)
{
	libusb_handle_t* handle;
	invocation_count++;
	if (invocation_count == 1) {
		syslog(LOG_INFO, "libusbi: initializing...");
		usb_init();
	}
	handle = (libusb_handle_t*)malloc(sizeof(libusb_handle_t));
	handle->devices = NULL;
	libusb_rescan(handle);
	return handle;
}


int libusb_search_interface(struct usb_device* device)
{
	int found = 0;
	int interface;
	for (interface = 0; interface < device->config[0].bNumInterfaces && !found; interface++) {
		switch (device->descriptor.bDeviceClass) {
			case USB_CLASS_VENDOR_SPEC:
				found = 1;
				break;
			case USB_CLASS_PER_INTERFACE:
				switch (device->config[0].interface[interface].altsetting[0].bInterfaceClass) {
					case USB_CLASS_VENDOR_SPEC:
					case USB_CLASS_PER_INTERFACE:
						case 16: /* data? */
							found = 1;
							break;
				}
				break;
		}
	}
	interface--;
	if (!found) return -1;
	return interface;
}


int libusb_search_in_endpoint(struct usb_device* device)
{
	int usb_in_ep = 0;
	int usb_out_ep = 0;
	struct usb_interface_descriptor *interface;
	interface = &device->config[0].interface->altsetting[0];

	int num;
	for (num = 0; num < interface->bNumEndpoints; num++) {
		struct usb_endpoint_descriptor *endpoint;
		int address, direction, transfer_type;

		endpoint = &interface->endpoint[num];
		address = endpoint->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
		direction = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
		transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK;

		if (transfer_type == USB_ENDPOINT_TYPE_BULK) {
			if (direction) {	/* in */
				if (!usb_in_ep)
					usb_in_ep = endpoint->bEndpointAddress;
			} else {	/* out */
				if (!usb_out_ep)
					usb_out_ep = endpoint->bEndpointAddress;
			}
		}
	}
	return usb_in_ep;
}


int libusb_search_out_endpoint(struct usb_device* device)
{
	int usb_in_ep = 0;
	int usb_out_ep = 0;
	struct usb_interface_descriptor *interface;
	interface = &device->config[0].interface->altsetting[0];

	int num;
	for (num = 0; num < interface->bNumEndpoints; num++) {
		struct usb_endpoint_descriptor *endpoint;
		int address, direction, transfer_type;

		endpoint = &interface->endpoint[num];
		address = endpoint->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
		direction = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
		transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK;

		if (transfer_type == USB_ENDPOINT_TYPE_BULK) {
			if (direction) {	/* in */
				if (!usb_in_ep)
					usb_in_ep = endpoint->bEndpointAddress;
			} else {	/* out */
				if (!usb_out_ep)
					usb_out_ep = endpoint->bEndpointAddress;
			}
		}
	}
	return usb_out_ep;
}


void libusb_attach_device(struct usb_device* device, libusb_handle_t* handle)
{
	libusb_device_t* libusb_device = (libusb_device_t*)malloc(sizeof(libusb_device_t));
	libusb_device->vendorID = device->descriptor.idVendor;
	libusb_device->productID = device->descriptor.idProduct;

	// the location string consists of bus number, followed by a colon (":"), and the device number
	libusb_device->location = (char*)malloc(strlen(device->bus->dirname) + strlen(device->filename) + 2);
	strcpy(libusb_device->location, device->bus->dirname);
	strcat(libusb_device->location, ":");
	strcat(libusb_device->location, device->filename);

	libusb_device->device = device;
	libusb_device->handle = NULL;
	libusb_device->interface = libusb_search_interface(device);
	if (libusb_device->interface < 0) {
		free(libusb_device->location);
		free(libusb_device);
		return;
	}
	libusb_device->out_endpoint = libusb_search_out_endpoint(device);
	if (libusb_device->out_endpoint < 0) {
		free(libusb_device->location);
		free(libusb_device);
		return;
	}
	libusb_device->in_endpoint = libusb_search_in_endpoint(device);
	if (libusb_device->in_endpoint < 0) {
		free(libusb_device->location);
		free(libusb_device);
		return;
	}
	libusb_device->next = handle->devices;
	handle->devices = libusb_device;
}


void libusb_detach_devices(libusb_handle_t* handle)
{
	libusb_device_t* next;
	while (handle->devices != NULL) {
		next = handle->devices->next;
		free(handle->devices->location);
		free(handle->devices);
		handle->devices = next;
	}
}


void libusb_rescan(libusb_handle_t* handle)
{
	struct usb_bus *bus;
	struct usb_device *device;

	libusb_detach_devices(handle);

	usb_find_busses();
	usb_find_devices();
	handle->devices = NULL;

	bus = usb_busses;
	while (bus != NULL) {
		device = bus->devices;
		while (device != NULL) {
			libusb_attach_device(device, handle);
			device = device->next;
		}
		bus = bus->next;
	}

}


int libusb_get_changed_device_count(void)
{
	usb_find_busses();
	return usb_find_devices();
}


libusb_device_t* libusb_get_devices(libusb_handle_t* handle)
{
	return handle->devices;
}


int libusb_open(libusb_device_t* device)
{
	int result;

	if (!device || !device->device)
		return -ENODEV;

	device->handle = usb_open(device->device);
	if (device->handle == NULL) {
		syslog(LOG_ERR, "libusbi: could not open device %s", device->location);
		return -ENODEV;
	}

	// Calling usb_set_configuration should not be necessary.
	// It is even considered harmful, since it may disturb other processes
	// which are currently communicating with the scanner!
	//
	// usb_set_configuration(device->handle,
	//     usb_device(device->handle)->config[0].bConfigurationValue);

	result = usb_claim_interface(device->handle, device->interface);
	switch (result) {
		case 0:
			return 0;
		case -ENOMEM:
			syslog(LOG_ERR, "libusbi: could not claim interface for device %s. (ENOMEM)",
				   device->location);
			usb_close(device->handle);
			return -ENODEV;
		case -EBUSY:
			syslog(LOG_ERR, "libusbi: could not claim interface for device %s. (EBUSY)",
				   device->location);
			usb_close(device->handle);
			return -EBUSY;
		default:
			syslog(LOG_ERR, "libusbi: could not claim interface for device %s. (code=%d)",
				   device->location, result);
			usb_close(device->handle);
			return -ENODEV;
	}
}


int libusb_close(libusb_device_t* device)
{
	int result;
	result = usb_release_interface(device->handle, device->interface);
	if (result < 0) {
		syslog(LOG_ERR, "libusbi: could not release interface, error code=%d, device=%s",
			   result, device->location);
		return result;
	}
	result = usb_close(device->handle);
	if (result < 0) {
		syslog(LOG_ERR, "libusbi: could not close usb device, error code=%d, device=%s",
			   result, device->location);
		return result;
	}
	return 0;
}


int libusb_read(libusb_device_t* device, void* buffer, int bytecount)
{
	int num_bytes = usb_bulk_read(device->handle, device->in_endpoint,
								  buffer, bytecount, TIMEOUT);
	if (num_bytes<0) {
		usb_clear_halt(device->handle, device->in_endpoint);
		return 0;
	}
	return num_bytes;
}


int libusb_write(libusb_device_t* device, void* buffer, int bytecount)
{
	int num_bytes = usb_bulk_write(device->handle, device->out_endpoint,
								   buffer, bytecount, TIMEOUT);
	if (num_bytes<0) {
		usb_clear_halt(device->handle, device->in_endpoint);
		return 0;
	}
	return num_bytes;
}


void libusb_flush(libusb_device_t* device)
{
	char buffer[16];
	while (usb_bulk_read(device->handle, device->in_endpoint, buffer, 16, 500) > 0) {};
}


int libusb_control_msg(libusb_device_t* device, int requesttype, int request,
					   int value, int index, void* bytes, int size)
{
	int num_bytes = usb_control_msg(device->handle, requesttype, request, value,
									index, bytes, size, TIMEOUT);
	if (num_bytes<0) {
		// Doesn't seem to be needed... (bs, Jun 07 2005)
		// usb_clear_halt(device->handle, device->in_endpoint);
		return 0;
	}
	return num_bytes;
}


void libusb_exit(libusb_handle_t* handle)
{
	invocation_count--;
	if (invocation_count == 0)
		syslog(LOG_INFO, "libusbi: shutting down...");
	libusb_detach_devices(handle);
	free(handle);
}