summaryrefslogtreecommitdiffstats
path: root/tools/tools/bus_autoconf/bus_usb.c
blob: ab794c13a2ae4ec30c817c79a73d4afe1fc781a7 (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
/* $FreeBSD$ */

/*-
 * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/queue.h>

#include "bus_autoconf.h"
#include "bus_sections.h"
#include "bus_usb.h"

struct usb_blob;
typedef TAILQ_HEAD(,usb_blob) usb_blob_head_t;
typedef TAILQ_ENTRY(usb_blob) usb_blob_entry_t;

static usb_blob_head_t usb_blob_head = TAILQ_HEAD_INITIALIZER(usb_blob_head);
static uint32_t usb_blob_count;

struct usb_blob {
	usb_blob_entry_t entry;
	struct usb_device_id temp;
};

/*
 * To ensure that the correct USB driver is loaded, the driver having
 * the most information about the device must be probed first. Then
 * more generic drivers shall be probed.
 */
static int
usb_compare(const void *_a, const void *_b)
{
	const struct usb_device_id *a = _a;
	const struct usb_device_id *b = _b;
	int retval;

	/* vendor matches first */

	if (a->match_flag_vendor > b->match_flag_vendor)
		return (-1);
	if (a->match_flag_vendor < b->match_flag_vendor)
		return (1);

	/* product matches first */

	if (a->match_flag_product > b->match_flag_product)
		return (-1);
	if (a->match_flag_product < b->match_flag_product)
		return (1);

	/* device class matches first */

	if (a->match_flag_dev_class > b->match_flag_dev_class)
		return (-1);
	if (a->match_flag_dev_class < b->match_flag_dev_class)
		return (1);

	if (a->match_flag_dev_subclass > b->match_flag_dev_subclass)
		return (-1);
	if (a->match_flag_dev_subclass < b->match_flag_dev_subclass)
		return (1);

	/* interface class matches first */

	if (a->match_flag_int_class > b->match_flag_int_class)
		return (-1);
	if (a->match_flag_int_class < b->match_flag_int_class)
		return (1);

	if (a->match_flag_int_subclass > b->match_flag_int_subclass)
		return (-1);
	if (a->match_flag_int_subclass < b->match_flag_int_subclass)
		return (1);

	if (a->match_flag_int_protocol > b->match_flag_int_protocol)
		return (-1);
	if (a->match_flag_int_protocol < b->match_flag_int_protocol)
		return (1);

	/* then sort according to value */

	if (a->idVendor > b->idVendor)
		return (1);
	if (a->idVendor < b->idVendor)
		return (-1);
	if (a->idProduct > b->idProduct)
		return (1);
	if (a->idProduct < b->idProduct)
		return (-1);
	if (a->bDeviceClass > b->bDeviceClass)
		return (1);
	if (a->bDeviceClass < b->bDeviceClass)
		return (-1);
	if (a->bDeviceSubClass > b->bDeviceSubClass)
		return (1);
	if (a->bDeviceSubClass < b->bDeviceSubClass)
		return (-1);
	if (a->bDeviceProtocol > b->bDeviceProtocol)
		return (1);
	if (a->bDeviceProtocol < b->bDeviceProtocol)
		return (-1);
	if (a->bInterfaceClass > b->bInterfaceClass)
		return (1);
	if (a->bInterfaceClass < b->bInterfaceClass)
		return (-1);
	if (a->bInterfaceSubClass > b->bInterfaceSubClass)
		return (1);
	if (a->bInterfaceSubClass < b->bInterfaceSubClass)
		return (-1);
	if (a->bInterfaceProtocol > b->bInterfaceProtocol)
		return (1);
	if (a->bInterfaceProtocol < b->bInterfaceProtocol)
		return (-1);

	/* in the end sort by module name and mode */

	retval = strcmp(a->module_name, b->module_name);
	if (retval == 0)
		retval = strcmp(a->module_mode, b->module_mode);
	return (retval);
}

static void
usb_sort_entries(struct usb_device_id *id, uint32_t nid)
{
	qsort(id, nid, sizeof(*id), &usb_compare);
}

static void
usb_import_entry(struct usb_device_id *id, const char *type,
    const char *module, const uint8_t *ptr, uint16_t size)
{
	const char *mode;

	if (strstr(type, "_host_"))
		mode = "host";
	else if (strstr(type, "_device_"))
		mode = "device";
	else
		mode = "(host|device)";

	strlcpy(id->module_name, module, sizeof(id->module_name));
	strlcpy(id->module_mode, mode, sizeof(id->module_mode));

	/* import data from binary object */

	if (format_get_field(type, "mfl_vendor", ptr, size))
		id->match_flag_vendor = 1;
	if (format_get_field(type, "mfl_product", ptr, size))
		id->match_flag_product = 1;
	if (format_get_field(type, "mfl_dev_lo", ptr, size))
		id->match_flag_dev_lo = 1;
	if (format_get_field(type, "mfl_dev_hi", ptr, size))
		id->match_flag_dev_hi = 1;
	if (format_get_field(type, "mfl_dev_class", ptr, size))
		id->match_flag_dev_class = 1;
	if (format_get_field(type, "mfl_dev_subclass", ptr, size))
		id->match_flag_dev_subclass = 1;
	if (format_get_field(type, "mfl_dev_protocol", ptr, size))
		id->match_flag_dev_protocol = 1;
	if (format_get_field(type, "mfl_int_class", ptr, size))
		id->match_flag_int_class = 1;
	if (format_get_field(type, "mfl_int_subclass", ptr, size))
		id->match_flag_int_subclass = 1;
	if (format_get_field(type, "mfl_int_protocol", ptr, size))
		id->match_flag_int_protocol = 1;

	id->idVendor = format_get_field(type, "idVendor[0]", ptr, size) |
	    (format_get_field(type, "idVendor[1]", ptr, size) << 8);
	id->idProduct = format_get_field(type, "idProduct[0]", ptr, size) |
	    (format_get_field(type, "idProduct[1]", ptr, size) << 8);

	id->bcdDevice_lo = format_get_field(type, "bcdDevice_lo[0]", ptr, size) |
	    (format_get_field(type, "bcdDevice_lo[1]", ptr, size) << 8);

	id->bcdDevice_hi = format_get_field(type, "bcdDevice_hi[0]", ptr, size) |
	    (format_get_field(type, "bcdDevice_hi[1]", ptr, size) << 8);

	id->bDeviceClass = format_get_field(type, "bDeviceClass", ptr, size);
	id->bDeviceSubClass = format_get_field(type, "bDeviceSubClass", ptr, size);
	id->bDeviceProtocol = format_get_field(type, "bDeviceProtocol", ptr, size);

	id->bInterfaceClass = format_get_field(type, "bInterfaceClass", ptr, size);
	id->bInterfaceSubClass = format_get_field(type, "bInterfaceSubClass", ptr, size);
	id->bInterfaceProtocol = format_get_field(type, "bInterfaceProtocol", ptr, size);

	if (format_get_field(type, "mf_vendor", ptr, size))
		id->match_flag_vendor = 1;
	if (format_get_field(type, "mf_product", ptr, size))
		id->match_flag_product = 1;
	if (format_get_field(type, "mf_dev_lo", ptr, size))
		id->match_flag_dev_lo = 1;
	if (format_get_field(type, "mf_dev_hi", ptr, size))
		id->match_flag_dev_hi = 1;
	if (format_get_field(type, "mf_dev_class", ptr, size))
		id->match_flag_dev_class = 1;
	if (format_get_field(type, "mf_dev_subclass", ptr, size))
		id->match_flag_dev_subclass = 1;
	if (format_get_field(type, "mf_dev_protocol", ptr, size))
		id->match_flag_dev_protocol = 1;
	if (format_get_field(type, "mf_int_class", ptr, size))
		id->match_flag_int_class = 1;
	if (format_get_field(type, "mf_int_subclass", ptr, size))
		id->match_flag_int_subclass = 1;
	if (format_get_field(type, "mf_int_protocol", ptr, size))
		id->match_flag_int_protocol = 1;

	/* compute some internal fields */
	id->is_iface = id->match_flag_int_class |
	    id->match_flag_int_protocol |
	    id->match_flag_int_subclass;

	id->is_dev = id->match_flag_dev_class |
	    id->match_flag_dev_subclass;

	id->is_vp = id->match_flag_vendor |
	    id->match_flag_product;

	id->is_any = id->is_vp + id->is_dev + id->is_iface;
}

static uint32_t
usb_dump(struct usb_device_id *id, uint32_t nid)
{
	uint32_t n = 1;

	if (id->is_any) {
		printf("nomatch 32 {\n"
		    "	match \"bus\" \"uhub[0-9]+\";\n"
		    "	match \"mode\" \"%s\";\n", id->module_mode);
	} else {
		printf("# skipped entry on module %s\n",
		    id->module_name);
		return (n);
	}

	if (id->match_flag_vendor) {
		printf("	match \"vendor\" \"0x%04x\";\n",
		    id->idVendor);
	}
	if (id->match_flag_product) {
		uint32_t x;

		if (id->is_any == 1 && id->is_vp == 1) {
			/* try to join similar entries */
			while (n < nid) {
				if (id[n].is_any != 1 || id[n].is_vp != 1)
					break;
				if (id[n].idVendor != id[0].idVendor)
					break;
				if (strcmp(id[n].module_name, id[0].module_name))
					break;
				if (strcmp(id[n].module_mode, id[0].module_mode))
					break;
				n++;
			}
		}
		if (n == 1) {
			printf("	match \"product\" \"0x%04x\";\n",
			    id->idProduct);
		} else {
			printf("	match \"product\" \"(");

			for (x = 0; x != n; x++) {
				printf("0x%04x%s", id[x].idProduct,
				    (x == (n - 1)) ? "" : "|");
			}

			printf(")\";\n");
		}
	}
	if (id->match_flag_dev_class) {
		printf("	match \"devclass\" \"0x%02x\";\n",
		    id->bDeviceClass);
	}
	if (id->match_flag_dev_subclass) {
		printf("	match \"devsubclass\" \"0x%02x\";\n",
		    id->bDeviceSubClass);
	}
	if (id->match_flag_int_class) {
		printf("	match \"intclass\" \"0x%02x\";\n",
		    id->bInterfaceClass);
	}
	if (id->match_flag_int_subclass) {
		printf("	match \"intsubclass\" \"0x%02x\";\n",
		    id->bInterfaceSubClass);
	}
	if (id->match_flag_int_protocol) {
		printf("	match \"intprotocol\" \"0x%02x\";\n",
		    id->bInterfaceProtocol);
	}
	printf("	action \"kldload %s\";\n"
	    "};\n\n", id->module_name);

	return (n);
}

void
usb_import_entries(const char *section, const char *module,
    const uint8_t *ptr, uint32_t len)
{
	struct usb_blob *pub;
	uint32_t section_size;
	uint32_t off;

	section_size = format_get_section_size(section);
	if (section_size == 0) {
		errx(EX_DATAERR, "Invalid or non-existing "
		    "section format '%s'", section);
	}
	if (len % section_size) {
		errx(EX_DATAERR, "Length %d is not "
		    "divisible by %d. Section format '%s'",
		    len, section_size, section);
	}
	for (off = 0; off != len; off += section_size) {
		pub = malloc(sizeof(*pub));
		if (pub == NULL)
			errx(EX_SOFTWARE, "Out of memory");

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

		usb_import_entry(&pub->temp, section,
		    module, ptr + off, section_size);

		TAILQ_INSERT_TAIL(&usb_blob_head, pub, entry);

		usb_blob_count++;
		if (usb_blob_count == 0)
			errx(EX_SOFTWARE, "Too many entries");
	}
}

void
usb_dump_entries(void)
{
	struct usb_blob *pub;
	struct usb_device_id *id;
	uint32_t x;

	id = malloc(usb_blob_count * sizeof(*id));
	if (id == NULL)
		errx(EX_SOFTWARE, "Out of memory");

	/* make linear array of all USB blobs */
	x = 0;
	TAILQ_FOREACH(pub, &usb_blob_head, entry)
	    id[x++] = pub->temp;

	usb_sort_entries(id, usb_blob_count);

	for (x = 0; x != usb_blob_count;)
		x += usb_dump(id + x, usb_blob_count - x);

	free(id);

	printf("# %d USB entries processed\n\n", usb_blob_count);
}
OpenPOWER on IntegriCloud