summaryrefslogtreecommitdiffstats
path: root/usb_device.c
blob: 5a468dd55a7b749cc46ce07a720b0ec7c83a014a (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2020, Google Inc. All rights reserved.
 *
 * 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.
 */

#include "programmer.h"
#include "spi.h"
#include "usb_device.h"

#include <assert.h>
#include <libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Possibly extract a programmer parameter and use it to initialize the given
 * match value structure.
 */
static void usb_match_value_init(struct usb_match_value *match,
				 char const *parameter)
{
	char *string = extract_programmer_param(parameter);

	match->name = parameter;

	if (string) {
		match->set = 1;
		match->value = strtol(string, NULL, 0);
	} else {
		match->set = 0;
	}

	free(string);
}

#define USB_MATCH_VALUE_INIT(NAME)			\
	usb_match_value_init(&match->NAME, #NAME)

void usb_match_init(struct usb_match *match)
{
	USB_MATCH_VALUE_INIT(vid);
	USB_MATCH_VALUE_INIT(pid);
	USB_MATCH_VALUE_INIT(bus);
	USB_MATCH_VALUE_INIT(address);
	USB_MATCH_VALUE_INIT(config);
	USB_MATCH_VALUE_INIT(interface);
	USB_MATCH_VALUE_INIT(altsetting);
	USB_MATCH_VALUE_INIT(class);
	USB_MATCH_VALUE_INIT(subclass);
	USB_MATCH_VALUE_INIT(protocol);
}

void usb_match_value_default(struct usb_match_value *value,
			     long int default_value)
{
	if (value->set)
		return;

	value->set   = 1;
	value->value = default_value;
}

/*
 * Match the value against a possible user supplied parameter.
 *
 * Return:
 *     0: The user supplied the given parameter and it did not match the value.
 *     1: Either the user didn't supply the parameter, or they did and it
 *        matches the given value.
 */
static int check_match(struct usb_match_value const *match_value, int value)
{
	int reject = match_value->set && (match_value->value != value);

	if (reject)
		msg_pdbg("USB: Rejecting device because %s = %d != %d\n",
			 match_value->name,
			 value,
			 match_value->value);

	return !reject;
}

/*
 * Allocate a copy of device and add it to the head of the devices list.
 */
static void add_device(struct usb_device *device,
		       struct usb_device **devices)
{
	struct usb_device *copy = malloc(sizeof(struct usb_device));

	assert(copy != NULL);

	*copy = *device;

	copy->next = *devices;
	*devices = copy;

	libusb_ref_device(copy->device);
}

/*
 * Look through the interfaces of the current device config for a match. Stop
 * looking after the first valid match is found.
 *
 * Return:
 *     0: No matching interface was found.
 *     1: A complete match was found and added to the devices list.
 */
static int find_interface(struct usb_match const *match,
			  struct usb_device *current,
			  struct usb_device **devices)
{
	int i, j;

	for (i = 0; i < current->config_descriptor->bNumInterfaces; ++i) {
		struct libusb_interface const *interface;

		interface = &current->config_descriptor->interface[i];

		for (j = 0; j < interface->num_altsetting; ++j) {
			struct libusb_interface_descriptor const *descriptor;

			descriptor = &interface->altsetting[j];

			if (check_match(&match->interface,
					descriptor->bInterfaceNumber) &&
			    check_match(&match->altsetting,
					descriptor->bAlternateSetting) &&
			    check_match(&match->class,
					descriptor->bInterfaceClass) &&
			    check_match(&match->subclass,
					descriptor->bInterfaceSubClass) &&
			    check_match(&match->protocol,
					descriptor->bInterfaceProtocol)) {
				current->interface_descriptor = descriptor;
				add_device(current, devices);
				msg_pdbg("USB: Found matching device\n");
				return 1;
			}
		}
	}

	return 0;
}

/*
 * Look through the configs of the current device for a match.  Stop looking
 * after the first valid match is found.
 *
 * Return:
 *     0: All configurations successfully checked, one may have been added to
 *        the list.
 *     non-zero: There was a failure while checking for a match.
 */
static int find_config(struct usb_match const *match,
		       struct usb_device *current,
		       struct libusb_device_descriptor const *device_descriptor,
		       struct usb_device **devices)
{
	int i;

	for (i = 0; i < device_descriptor->bNumConfigurations; ++i) {
		int ret = LIBUSB(libusb_get_config_descriptor(
				current->device, i,
				&current->config_descriptor));
		if (ret != 0) {
			msg_perr("USB: Failed to get config descriptor");
			return ret;
		}

		if (check_match(&match->config,
				current->config_descriptor->
				bConfigurationValue) &&
		    find_interface(match, current, devices))
			break;

		libusb_free_config_descriptor(current->config_descriptor);
	}

	return 0;
}

int usb_device_find(struct usb_match const *match, struct usb_device **devices)
{
	libusb_device **list;
	ssize_t         count;
	ssize_t         i;

	*devices = NULL;

	int ret = LIBUSB(count = libusb_get_device_list(NULL, &list));
	if (ret != 0) {
		msg_perr("USB: Failed to get device list");
		return ret;
	}

	for (i = 0; i < count; ++i) {
		struct libusb_device_descriptor descriptor;
		struct usb_device               current = {
			.device = list[i],
			.handle = NULL,
			.next   = NULL,
		};

		uint8_t bus     = libusb_get_bus_number(list[i]);
		uint8_t address = libusb_get_device_address(list[i]);

		msg_pdbg("USB: Inspecting device (Bus %d, Address %d)\n",
			 bus,
			 address);

		ret = LIBUSB(libusb_get_device_descriptor(list[i],
							  &descriptor));
		if (ret != 0) {
			msg_perr("USB: Failed to get device descriptor");
			free(*devices);
			*devices = NULL;
			return ret;
		}

		if (check_match(&match->vid,     descriptor.idVendor) &&
		    check_match(&match->pid,     descriptor.idProduct) &&
		    check_match(&match->bus,     bus) &&
		    check_match(&match->address, address)) {
			ret = find_config(match,
					  &current,
					  &descriptor,
					  devices);
			if (ret != 0) {
				msg_perr("USB: Failed to find config");
				return ret;
			}
		}
	}

	libusb_free_device_list(list, 1);

	return (*devices == NULL);
}

/*
 * If the underlying libusb device is not open, open it.
 *
 * Return:
 *     0: The device was already open or was successfully opened.
 *     non-zero: There was a failure while opening the device.
 */
static int usb_device_open(struct usb_device *device)
{
	if (device->handle == NULL) {
		int ret = LIBUSB(libusb_open(device->device, &device->handle));
		if (ret != 0) {
			msg_perr("USB: Failed to open device\n");
			return ret;
		}
	}

	return 0;
}

int usb_device_show(char const *prefix, struct usb_device *device)
{
	struct libusb_device_descriptor descriptor;
	unsigned char                   product[256];
	int ret;

	ret = usb_device_open(device);
	if (ret != 0) {
		msg_perr("USB: Failed to open device\n");
		return ret;
	}

	ret = LIBUSB(libusb_get_device_descriptor(device->device, &descriptor));
	if (ret != 0) {
		msg_perr("USB: Failed to get device descriptor\n");
		return ret;
	}

	ret = LIBUSB(libusb_get_string_descriptor_ascii(
			     device->handle,
			     descriptor.iProduct,
			     product,
			     sizeof(product)));
	if (ret != 0) {
		msg_perr("USB: Failed to get device product string\n");
		return ret;
	}

	product[255] = '\0';

	msg_perr("%sbus=0x%02x,address=0x%02x | %s\n",
		 prefix,
		 libusb_get_bus_number(device->device),
		 libusb_get_device_address(device->device),
		 product);

	return 0;
}

int usb_device_claim(struct usb_device *device)
{
	int current_config;

	int ret = usb_device_open(device);
	if (ret != 0) {
		msg_perr("USB: Failed to open device\n");
		return ret;
	}

	ret = LIBUSB(libusb_get_configuration(device->handle,
					      &current_config));
	if (ret != 0) {
		msg_perr("USB: Failed to get current device configuration\n");
		return ret;
	}

	if (current_config != device->config_descriptor->bConfigurationValue) {
		ret = LIBUSB(libusb_set_configuration(
				     device->handle,
				     device->
				     config_descriptor->
				     bConfigurationValue));
		if (ret != 0) {
			msg_perr("USB: Failed to set new configuration from %d to %d\n",
					current_config,
					device->config_descriptor->bConfigurationValue);
			return ret;
		}
	}

	ret = LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1));
	if (ret != 0) {
		msg_perr("USB: Failed to enable auto kernel driver detach\n");
		return ret;
	}

	ret = LIBUSB(libusb_claim_interface(device->handle,
					    device->
					    interface_descriptor->
					    bInterfaceNumber));
	if (ret != 0) {
		msg_perr("USB: Could not claim device interface %d\n",
				device->interface_descriptor->bInterfaceNumber);
		return ret;
	}

	if (device->interface_descriptor->bAlternateSetting != 0) {
		ret = LIBUSB(libusb_set_interface_alt_setting(
				     device->handle,
				     device->
				     interface_descriptor->
				     bInterfaceNumber,
				     device->
				     interface_descriptor->
				     bAlternateSetting));
		if (ret != 0) {
			msg_perr("USB: Failed to set alternate setting %d\n",
					device->interface_descriptor->bAlternateSetting);
			return ret;
		}
	}

	return 0;
}

struct usb_device *usb_device_free(struct usb_device *device)
{
	struct usb_device *next = device->next;

	if (device->handle != NULL)
		libusb_close(device->handle);

	/*
	 * This unref balances the ref added in the add_device function.
	 */
	libusb_unref_device(device->device);
	libusb_free_config_descriptor(device->config_descriptor);

	free(device);

	return next;
}