summaryrefslogtreecommitdiff
path: root/libusb/core.c
blob: dc54720ed17d1d861ac47a8da101b2dbf2fb286e (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Core functions for libusb
 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "libusb.h"
#include "libusbi.h"

static struct list_head usb_devs;
struct list_head open_devs;

static int scan_device(char *busdir, const char *devnum)
{
	char path[PATH_MAX + 1];
	unsigned char raw_desc[DEVICE_DESC_LENGTH];
	struct libusb_dev *dev = malloc(sizeof(*dev));
	int fd = 0;
	int i;
	int r;
	int tmp;

	if (!dev)
		return -1;

	snprintf(path, PATH_MAX, "%s/%s", busdir, devnum);
	fp_dbg("%s", path);
	fd = open(path, O_RDWR);
	if (!fd) {
		fp_dbg("open '%s' failed, ret=%d errno=%d", path, fd, errno);
		r = -1;
		goto err;
	}

	r = read(fd, raw_desc, DEVICE_DESC_LENGTH);
	if (r < 0) {
		fp_err("read failed ret=%d errno=%d", r, errno);
		goto err;
	}
	/* FIXME: short read handling? */

	fpi_parse_descriptor(raw_desc, "bbWbbbbWWWbbbb", &dev->desc);

	/* Now try to fetch the rest of the descriptors */
	if (dev->desc.bNumConfigurations > USB_MAXCONFIG) {
		fp_err("too many configurations");
		r = -1;
		goto err;
	}

	if (dev->desc.bNumConfigurations < 1) {
		fp_dbg("no configurations?");
		r = -1;
		goto err;
	}

	tmp = dev->desc.bNumConfigurations * sizeof(struct usb_config_descriptor);
	dev->config = malloc(tmp);
	if (!dev->config) {
		r = -1;
		goto err;
	}

	memset(dev->config, 0, tmp);

	for (i = 0; i < dev->desc.bNumConfigurations; i++) {
		unsigned char buffer[8], *bigbuffer;
		struct usb_config_descriptor config;

		/* Get the first 8 bytes to figure out what the total length is */
		r = read(fd, buffer, sizeof(buffer));
		if (r < sizeof(buffer)) {
			fp_err("short descriptor read (%d/%d)", r, sizeof(buffer));
			goto err;
		}

		fpi_parse_descriptor(buffer, "bbw", &config);

		bigbuffer = malloc(config.wTotalLength);
		if (!bigbuffer)
			goto err;

		/* Read the rest of the config descriptor */
		memcpy(bigbuffer, buffer, sizeof(buffer));

		tmp = config.wTotalLength - 8;
		r = read(fd, bigbuffer + 8, tmp);
		if (r < tmp) {
			fp_err("short descriptor read (%d/%d)", r, tmp);
			free(bigbuffer);
			goto err;
		}

		r = fpi_parse_configuration(&dev->config[i], bigbuffer);
		if (r > 0)
			fp_warn("descriptor data still left\n");
		free(bigbuffer);
	}

	dev->nodepath = strdup(path);
	if (!dev->nodepath)
		goto err;

	fp_dbg("found device %04x:%04x", dev->desc.idVendor, dev->desc.idProduct);
	list_add(&dev->list, &usb_devs);
	r = 0;

err:
	if (fd)
		close(fd);
	if (r < 0 && dev)
		free(dev);
	return r;
}

static int scan_busdir(const char *busnum)
{
	DIR *dir;
	char dirpath[PATH_MAX + 1];
	struct dirent *entry;

	snprintf(dirpath, PATH_MAX, "%s/%s", USBFS_PATH, busnum);
	fp_dbg("%s", dirpath);
	dir = opendir(dirpath);
	if (!dir) {
		fp_err("opendir '%s' failed, errno=%d", dirpath, errno);
		return -1;
	}

	while ((entry = readdir(dir))) {
		if (entry->d_name[0] == '.')
			continue;
		/* deliberately ignoring errors due to valid unplug race conditions */
		scan_device(dirpath, entry->d_name);
	}

	return 0;
}

API_EXPORTED int libusb_find_devices(void)
{
	DIR *busses;
	struct dirent *entry;
	fp_dbg("");

	busses = opendir(USBFS_PATH);
	if (!busses) {
		fp_err("opendir busses failed errno=%d", errno);
		return -1;
	}

	while ((entry = readdir(busses))) {
		if (entry->d_name[0] == '.')
			continue;
		/* deliberately ignoring errors, valid race conditions exist
		 * e.g. unplugging of hubs in the middle of this loop*/
		scan_busdir(entry->d_name);
	}

	return 0;
}

API_EXPORTED struct libusb_dev *libusb_get_devices(void)
{
	if (list_empty(&usb_devs))
		return NULL;
	return list_entry(usb_devs.next, struct libusb_dev, list);
}

API_EXPORTED struct libusb_dev *libusb_dev_next(struct libusb_dev *dev)
{
	struct list_head *head = &dev->list;
	if (!head || head->next == &usb_devs)
		return NULL;
	return list_entry(head->next, struct libusb_dev, list);
}

API_EXPORTED struct usb_dev_descriptor *libusb_dev_get_descriptor(
	struct libusb_dev *dev)
{
	return &dev->desc;
}

API_EXPORTED struct usb_config_descriptor *libusb_dev_get_config(
	struct libusb_dev *dev)
{
	return dev->config;
}

API_EXPORTED struct libusb_dev_handle *libusb_devh_open(struct libusb_dev *dev)
{
	struct libusb_dev_handle *devh;
	int fd;
	fp_dbg("open %04x:%04x", dev->desc.idVendor, dev->desc.idProduct);

	fd = open(dev->nodepath, O_RDWR);
	if (!fd) {
		fp_err("open failed, code %d errno %d", fd, errno);
		return NULL;
	}

	devh = malloc(sizeof(*devh));
	if (!devh) {
		close(fd);
		return NULL;
	}

	devh->fd = fd;
	devh->dev = dev;
	list_add(&devh->list, &open_devs);
	return devh;
}

static void do_close(struct libusb_dev_handle *devh)
{
	close(devh->fd);	
}

API_EXPORTED void libusb_devh_close(struct libusb_dev_handle *devh)
{
	if (!devh)
		return;
	fp_dbg("");

	list_del(&devh->list);
	do_close(devh);
	free(devh);
}

API_EXPORTED struct libusb_dev *libusb_devh_get_dev(struct libusb_dev_handle *devh)
{
	return devh->dev;
}

API_EXPORTED int libusb_devh_claim_intf(struct libusb_dev_handle *dev,
	int iface)
{
	int r;
	fp_dbg("interface %d", iface);
	
	r = ioctl(dev->fd, IOCTL_USB_CLAIMINTF, &iface);
	if (r < 0)
		fp_err("claim interface failed, error %d", r);
	return r;
}

API_EXPORTED int libusb_devh_release_intf(struct libusb_dev_handle *dev,
	int iface)
{
	int r;
	fp_dbg("interface %d", iface);

	r = ioctl(dev->fd, IOCTL_USB_RELEASEINTF, &iface);
	if (r < 0)
		fp_err("release interface failed, error %d", r);
	return r;
}

API_EXPORTED int libusb_init(int signum)
{
	/* FIXME: find correct usb node path */
	fp_dbg("");
	list_init(&usb_devs);
	list_init(&open_devs);
	return fpi_io_init(signum);
}

API_EXPORTED void libusb_exit(void)
{
	struct libusb_dev_handle *devh;
	fp_dbg("");
	if (!list_empty(&open_devs)) {
		fp_dbg("naughty app left some devices open!\n");
		list_for_each_entry(devh, &open_devs, list)
			do_close(devh);
	}
	fpi_io_exit();
}

void fpi_log(enum fpi_log_level level, const char *function,
	const char *format, ...)
{
	va_list args;
	FILE *stream = stdout;
	const char *prefix;

	switch (level) {
	case LOG_LEVEL_INFO:
		prefix = "info";
		break;
	case LOG_LEVEL_WARNING:
		stream = stderr;
		prefix = "warning";
		break;
	case LOG_LEVEL_ERROR:
		stream = stderr;
		prefix = "error";
		break;
	case LOG_LEVEL_DEBUG:
		stream = stderr;
		prefix = "debug";
		break;
	default:
		stream = stderr;
		prefix = "unknown";
		break;
	}

	fprintf(stream, "libusb:%s [%s] ", prefix, function);

	va_start (args, format);
	vfprintf(stream, format, args);
	va_end (args);

	fprintf(stream, "\n");
}