summaryrefslogtreecommitdiff
path: root/payloads/libpayload/drivers/usb/usb.c
blob: a6d5d43c2338aa0c4be1b1c8ef36cca035f811a5 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
 * This file is part of the libpayload project.
 *
 * Copyright (C) 2008-2010 coresystems GmbH
 *
 * 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * 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.
 */

//#define USB_DEBUG

#include <libpayload-config.h>
#include <usb/usb.h>

#define DR_DESC gen_bmRequestType(device_to_host, standard_type, dev_recp)

hci_t *usb_hcs = 0;

hci_t *
new_controller (void)
{
	hci_t *controller = xzalloc(sizeof (hci_t));
	controller->next = usb_hcs;
	usb_hcs = controller;
	return controller;
}

void
detach_controller (hci_t *controller)
{
	if (controller == NULL)
		return;

	usb_detach_device(controller, 0);	/* tear down root hub tree */

	if (usb_hcs == controller) {
		usb_hcs = controller->next;
	} else {
		hci_t *it = usb_hcs;
		while (it != NULL) {
			if (it->next == controller) {
				it->next = controller->next;
				return;
			}
			it = it->next;
		}
	}
}

/**
 * Shut down all controllers
 */
int
usb_exit (void)
{
	while (usb_hcs != NULL) {
		usb_hcs->shutdown(usb_hcs);
	}
	return 0;
}

/**
 * Polls all hubs on all USB controllers, to find out about device changes
 */
void
usb_poll (void)
{
	if (usb_hcs == 0)
		return;
	hci_t *controller = usb_hcs;
	while (controller != NULL) {
		int i;
		for (i = 0; i < 128; i++) {
			if (controller->devices[i] != 0) {
				controller->devices[i]->poll (controller->devices[i]);
			}
		}
		controller = controller->next;
	}
}

usbdev_t *
init_device_entry (hci_t *controller, int i)
{
	usbdev_t *dev = calloc(1, sizeof(usbdev_t));
	if (!dev) {
		usb_debug("no memory to allocate device structure\n");
		return NULL;
	}
	if (controller->devices[i] != 0)
		usb_debug("warning: device %d reassigned?\n", i);
	controller->devices[i] = dev;
	dev->controller = controller;
	dev->address = -1;
	dev->hub = -1;
	dev->port = -1;
	dev->init = usb_nop_init;
	dev->init (controller->devices[i]);
	return dev;
}

int
set_feature (usbdev_t *dev, int endp, int feature, int rtype)
{
	dev_req_t dr;

	dr.bmRequestType = rtype;
	dr.data_dir = host_to_device;
	dr.bRequest = SET_FEATURE;
	dr.wValue = feature;
	dr.wIndex = endp;
	dr.wLength = 0;

	return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
}

int
get_status (usbdev_t *dev, int intf, int rtype, int len, void *data)
{
	dev_req_t dr;

	dr.bmRequestType = rtype;
	dr.data_dir = device_to_host;
	dr.bRequest = GET_STATUS;
	dr.wValue = 0;
	dr.wIndex = intf;
	dr.wLength = len;

	return dev->controller->control (dev, IN, sizeof (dr), &dr, len, data);
}

/*
 * Certain Lexar / Micron USB 2.0 disks will fail the get_descriptor(DT_CFG)
 * call due to timing issues. Work around this by making extra attempts on
 * failure.
 */
#define GET_DESCRIPTOR_TRIES 3

int
get_descriptor(usbdev_t *dev, int rtype, int desc_type, int desc_idx,
		void *data, size_t len)
{
	dev_req_t dr;
	int fail_tries = 0;
	int ret = 0;

	while (fail_tries++ < GET_DESCRIPTOR_TRIES) {
		dr.bmRequestType = rtype;
		dr.bRequest = GET_DESCRIPTOR;
		dr.wValue = desc_type << 8 | desc_idx;
		dr.wIndex = 0;
		dr.wLength = len;

		ret = dev->controller->control(dev, IN,
				sizeof(dr), &dr, len, data);

		if (ret == len)
			break;
		udelay(10);
	}
	return ret;
}

int
set_configuration (usbdev_t *dev)
{
	dev_req_t dr;

	dr.bmRequestType = 0;
	dr.bRequest = SET_CONFIGURATION;
	dr.wValue = dev->configuration->bConfigurationValue;
	dr.wIndex = 0;
	dr.wLength = 0;

	return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
}

int
clear_feature (usbdev_t *dev, int endp, int feature, int rtype)
{
	dev_req_t dr;

	dr.bmRequestType = rtype;
	dr.data_dir = host_to_device;
	dr.bRequest = CLEAR_FEATURE;
	dr.wValue = feature;
	dr.wIndex = endp;
	dr.wLength = 0;

	return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0) < 0;
}

int
clear_stall (endpoint_t *ep)
{
	int ret = clear_feature (ep->dev, ep->endpoint, ENDPOINT_HALT,
		gen_bmRequestType (host_to_device, standard_type, endp_recp));
	ep->toggle = 0;
	return ret;
}

/* returns free address or -1 */
static int
get_free_address (hci_t *controller)
{
	int i = controller->latest_address + 1;
	for (; i != controller->latest_address; i++) {
		if (i >= ARRAY_SIZE(controller->devices) || i < 1) {
			usb_debug("WARNING: Device addresses for controller %#x"
				  " wrapped around!\n", controller->reg_base);
			i = 0;
			continue;
		}
		if (controller->devices[i] == 0) {
			controller->latest_address = i;
			return i;
		}
	}
	usb_debug ("no free address found\n");
	return -1;		// no free address
}

int
usb_decode_mps0(usb_speed speed, u8 bMaxPacketSize0)
{
	switch (speed) {
	case LOW_SPEED:
		if (bMaxPacketSize0 != 8) {
			usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0);
			bMaxPacketSize0 = 8;
		}
		return bMaxPacketSize0;
	case FULL_SPEED:
		switch (bMaxPacketSize0) {
		case 8: case 16: case 32: case 64:
			return bMaxPacketSize0;
		default:
			usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0);
			return 8;
		}
	case HIGH_SPEED:
		if (bMaxPacketSize0 != 64) {
			usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0);
			bMaxPacketSize0 = 64;
		}
		return bMaxPacketSize0;
	case SUPER_SPEED:
		if (bMaxPacketSize0 != 9) {
			usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0);
			bMaxPacketSize0 = 9;
		}
		return 1 << bMaxPacketSize0;
	default: 	/* GCC is stupid and cannot deal with enums correctly */
		return 8;
	}
}

int speed_to_default_mps(usb_speed speed)
{
	switch (speed) {
	case LOW_SPEED:
		return 8;
	case FULL_SPEED:
	case HIGH_SPEED:
		return 64;
	case SUPER_SPEED:
	default:
		return 512;
	}
}

/* Normalize bInterval to log2 of microframes */
static int
usb_decode_interval(usb_speed speed, const endpoint_type type, const unsigned char bInterval)
{
#define LOG2(a) ((sizeof(unsigned) << 3) - __builtin_clz(a) - 1)
	switch (speed) {
	case LOW_SPEED:
		switch (type) {
		case ISOCHRONOUS: case INTERRUPT:
			return LOG2(bInterval) + 3;
		default:
			return 0;
		}
	case FULL_SPEED:
		switch (type) {
		case ISOCHRONOUS:
			return (bInterval - 1) + 3;
		case INTERRUPT:
			return LOG2(bInterval) + 3;
		default:
			return 0;
		}
	case HIGH_SPEED:
		switch (type) {
		case ISOCHRONOUS: case INTERRUPT:
			return bInterval - 1;
		default:
			return LOG2(bInterval);
		}
	case SUPER_SPEED:
		switch (type) {
		case ISOCHRONOUS: case INTERRUPT:
			return bInterval - 1;
		default:
			return 0;
		}
	default:
		return 0;
	}
#undef LOG2
}

usbdev_t *
generic_set_address (hci_t *controller, usb_speed speed,
		     int hubport, int hubaddr)
{
	int adr = get_free_address (controller);	// address to set
	if (adr < 0)
		return NULL;
	dev_req_t dr;

	memset (&dr, 0, sizeof (dr));
	dr.data_dir = host_to_device;
	dr.req_type = standard_type;
	dr.req_recp = dev_recp;
	dr.bRequest = SET_ADDRESS;
	dr.wValue = adr;
	dr.wIndex = 0;
	dr.wLength = 0;

	usbdev_t *dev = init_device_entry(controller, adr);
	if (!dev)
		return NULL;

	// dummy values for registering the address
	dev->address = 0;
	dev->hub = hubaddr;
	dev->port = hubport;
	dev->speed = speed;
	dev->endpoints[0].dev = dev;
	dev->endpoints[0].endpoint = 0;
	dev->endpoints[0].maxpacketsize = 8;
	dev->endpoints[0].toggle = 0;
	dev->endpoints[0].direction = SETUP;
	dev->endpoints[0].type = CONTROL;
	if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0) < 0) {
		usb_debug ("set_address failed\n");
		usb_detach_device (controller, adr);
		return NULL;
	}
	mdelay (SET_ADDRESS_MDELAY);

	u8 buf[8];
	dev->address = adr;
	if (get_descriptor (dev, DR_DESC, DT_DEV, 0, buf, sizeof(buf))
			!= sizeof(buf)) {
		usb_debug("first get_descriptor(DT_DEV) failed\n");
		usb_detach_device (controller, adr);
		return NULL;
	}
	dev->endpoints[0].maxpacketsize = usb_decode_mps0(speed, buf[7]);

	return dev;
}

static int
set_address (hci_t *controller, usb_speed speed, int hubport, int hubaddr)
{
	usbdev_t *dev = controller->set_address(controller, speed,
						hubport, hubaddr);
	if (!dev) {
		usb_debug ("set_address failed\n");
		return -1;
	}

	dev->descriptor = malloc(sizeof(*dev->descriptor));
	if (!dev->descriptor || get_descriptor (dev, DR_DESC, DT_DEV, 0,
			dev->descriptor, sizeof(*dev->descriptor))
			!= sizeof(*dev->descriptor)) {
		usb_debug ("get_descriptor(DT_DEV) failed\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}

	usb_debug ("* found device (0x%04x:0x%04x, USB %x.%x, MPS0: %d)\n",
		 dev->descriptor->idVendor, dev->descriptor->idProduct,
		 dev->descriptor->bcdUSB >> 8, dev->descriptor->bcdUSB & 0xff,
		 dev->endpoints[0].maxpacketsize);
	dev->quirks = usb_quirk_check(dev->descriptor->idVendor,
				      dev->descriptor->idProduct);

	usb_debug ("device has %d configurations\n",
		   dev->descriptor->bNumConfigurations);
	if (dev->descriptor->bNumConfigurations == 0) {
		/* device isn't usable */
		usb_debug ("... no usable configuration!\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}

	u16 buf[2];
	if (get_descriptor (dev, DR_DESC, DT_CFG, 0, buf, sizeof(buf))
			!= sizeof(buf)) {
		usb_debug ("first get_descriptor(DT_CFG) failed\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}
	/* workaround for some USB devices: wait until they're ready, or
	 * they send a NAK when they're not allowed to do. 1ms is enough */
	mdelay(1);
	dev->configuration = malloc(buf[1]);
	if (!dev->configuration) {
		usb_debug ("could not allocate %d bytes for DT_CFG\n", buf[1]);
		usb_detach_device (controller, dev->address);
		return -1;
	}
	if (get_descriptor (dev, DR_DESC, DT_CFG, 0, dev->configuration,
			    buf[1]) != buf[1]) {
		usb_debug ("get_descriptor(DT_CFG) failed\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}
	configuration_descriptor_t *cd = dev->configuration;
	if (cd->wTotalLength != buf[1]) {
		usb_debug ("configuration descriptor size changed, aborting\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}

	/*
	 * If the device is not well known (ifnum == -1), we use the first
	 * interface we encounter, as there was no need to implement something
	 * else for the time being. If you need it, see the SetInterface and
	 * GetInterface functions in the USB specification and set it yourself.
	 */
	usb_debug ("device has %x interfaces\n", cd->bNumInterfaces);
	int ifnum = usb_interface_check(dev->descriptor->idVendor,
					dev->descriptor->idProduct);
	if (cd->bNumInterfaces > 1 && ifnum < 0)
		usb_debug ("NOTICE: Your device has multiple interfaces and\n"
			   "this driver will only use the first one. That may\n"
			   "be the wrong choice and cause the device to not\n"
			   "work correctly. Please report this case\n"
			   "(including the above debugging output) to\n"
			   "coreboot@coreboot.org to have the device added to\n"
			   "the list of well-known quirks.\n");

	u8 *end = (void *)dev->configuration + cd->wTotalLength;
	interface_descriptor_t *intf;
	u8 *ptr;

	/* Find our interface (or the first good one if we don't know) */
	for (ptr = (void *)dev->configuration + sizeof(*cd); ; ptr += ptr[0]) {
		if (ptr + 2 > end || !ptr[0] || ptr + ptr[0] > end) {
			usb_debug ("Couldn't find usable DT_INTF\n");
			usb_detach_device (controller, dev->address);
			return -1;
		}
		if (ptr[1] != DT_INTF)
			continue;
		intf = (void *)ptr;
		if (intf->bLength != sizeof(*intf)) {
			usb_debug ("Skipping broken DT_INTF\n");
			continue;
		}
		if (ifnum >= 0 && intf->bInterfaceNumber != ifnum)
			continue;
		usb_debug ("Interface %d: class 0x%x, sub 0x%x. proto 0x%x\n",
			intf->bInterfaceNumber, intf->bInterfaceClass,
			intf->bInterfaceSubClass, intf->bInterfaceProtocol);
		ptr += sizeof(*intf);
		break;
	}

	/* Gather up all endpoints belonging to this inteface */
	dev->num_endp = 1;
	for (; ptr + 2 <= end && ptr[0] && ptr + ptr[0] <= end; ptr += ptr[0]) {
		if (ptr[1] == DT_INTF || ptr[1] == DT_CFG ||
				dev->num_endp >= ARRAY_SIZE(dev->endpoints))
			break;
		if (ptr[1] != DT_ENDP)
			continue;

		endpoint_descriptor_t *desc = (void *)ptr;
		static const char *transfertypes[4] = {
			"control", "isochronous", "bulk", "interrupt"
		};
		usb_debug (" #Endpoint %d (%s), max packet size %x, type %s\n",
			desc->bEndpointAddress & 0x7f,
			(desc->bEndpointAddress & 0x80) ? "in" : "out",
			desc->wMaxPacketSize,
			transfertypes[desc->bmAttributes & 0x3]);

		endpoint_t *ep = &dev->endpoints[dev->num_endp++];
		ep->dev = dev;
		ep->endpoint = desc->bEndpointAddress;
		ep->toggle = 0;
		ep->maxpacketsize = desc->wMaxPacketSize;
		ep->direction = (desc->bEndpointAddress & 0x80) ? IN : OUT;
		ep->type = desc->bmAttributes & 0x3;
		ep->interval = usb_decode_interval (dev->speed, ep->type,
						    desc->bInterval);
	}

	if ((controller->finish_device_config &&
			controller->finish_device_config(dev)) ||
			set_configuration(dev) < 0) {
		usb_debug ("Could not finalize device configuration\n");
		usb_detach_device (controller, dev->address);
		return -1;
	}

	int class = dev->descriptor->bDeviceClass;
	if (class == 0)
		class = intf->bInterfaceClass;

	enum {
		audio_device      = 0x01,
		comm_device       = 0x02,
		hid_device        = 0x03,
		physical_device   = 0x05,
		imaging_device    = 0x06,
		printer_device    = 0x07,
		msc_device        = 0x08,
		hub_device        = 0x09,
		cdc_device        = 0x0a,
		ccid_device       = 0x0b,
		security_device   = 0x0d,
		video_device      = 0x0e,
		healthcare_device = 0x0f,
		diagnostic_device = 0xdc,
		wireless_device   = 0xe0,
		misc_device       = 0xef,
	};
	usb_debug("Class: ");
	switch (class) {
	case audio_device:
		usb_debug("audio\n");
		break;
	case comm_device:
		usb_debug("communication\n");
		break;
	case hid_device:
		usb_debug ("HID\n");
#if IS_ENABLED(CONFIG_LP_USB_HID)
		dev->init = usb_hid_init;
		return dev->address;
#else
		usb_debug ("NOTICE: USB HID support not compiled in\n");
#endif
		break;
	case physical_device:
		usb_debug("physical\n");
		break;
	case imaging_device:
		usb_debug("camera\n");
		break;
	case printer_device:
		usb_debug("printer\n");
		break;
	case msc_device:
		usb_debug ("MSC\n");
#if IS_ENABLED(CONFIG_LP_USB_MSC)
		dev->init = usb_msc_init;
		return dev->address;
#else
		usb_debug ("NOTICE: USB MSC support not compiled in\n");
#endif
		break;
	case hub_device:
		usb_debug ("hub\n");
#if IS_ENABLED(CONFIG_LP_USB_HUB)
		dev->init = usb_hub_init;
		return dev->address;
#else
		usb_debug ("NOTICE: USB hub support not compiled in\n");
#endif
		break;
	case cdc_device:
		usb_debug("CDC\n");
		break;
	case ccid_device:
		usb_debug("smartcard / CCID\n");
		break;
	case security_device:
		usb_debug("content security\n");
		break;
	case video_device:
		usb_debug("video\n");
		break;
	case healthcare_device:
		usb_debug("healthcare\n");
		break;
	case diagnostic_device:
		usb_debug("diagnostic\n");
		break;
	case wireless_device:
		usb_debug("wireless\n");
		break;
	default:
		usb_debug("unsupported class %x\n", class);
		break;
	}
	dev->init = usb_generic_init;
	return dev->address;
}

/*
 * Should be called by the hub drivers whenever a physical detach occurs
 * and can be called by usb class drivers if they are unsatisfied with a
 * malfunctioning device.
 */
void
usb_detach_device(hci_t *controller, int devno)
{
	/* check if device exists, as we may have
	   been called yet by the usb class driver */
	if (controller->devices[devno]) {
		controller->devices[devno]->destroy (controller->devices[devno]);
		if (controller->destroy_device)
			controller->destroy_device(controller, devno);
		/* Tear down the device itself *after* destroy_device()
		 * has had a chance to interoogate it. */
		free(controller->devices[devno]);
		controller->devices[devno] = NULL;
	}
}

int
usb_attach_device(hci_t *controller, int hubaddress, int port, usb_speed speed)
{
	static const char* speeds[] = { "full", "low", "high", "super" };
	usb_debug ("%sspeed device\n", (speed < sizeof(speeds) / sizeof(char*))
		? speeds[speed] : "invalid value - no");
	int newdev = set_address (controller, speed, port, hubaddress);
	if (newdev == -1)
		return -1;
	usbdev_t *newdev_t = controller->devices[newdev];
	// determine responsible driver - current done in set_address
	newdev_t->init (newdev_t);
	/* init() may have called usb_detach_device() yet, so check */
	return controller->devices[newdev] ? newdev : -1;
}

static void
usb_generic_destroy (usbdev_t *dev)
{
	if (usb_generic_remove)
		usb_generic_remove(dev);
}

void
usb_generic_init (usbdev_t *dev)
{
	dev->data = NULL;
	dev->destroy = usb_generic_destroy;

	if (usb_generic_create)
		usb_generic_create(dev);

	if (dev->data == NULL) {
		usb_debug("Detaching device not used by payload\n");
		usb_detach_device(dev->controller, dev->address);
	}
}

/*
 * returns the address of the closest USB2.0 hub, which is responsible for
 * split transactions, along with the number of the used downstream port
 */
int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port)
{
	const usbdev_t *usb1dev;

	do {
		usb1dev = dev;
		if ((dev->hub >= 0) && (dev->hub < 128))
			dev = dev->controller->devices[dev->hub];
		else
			dev = NULL;
	} while (dev && (dev->speed < 2));

	if (dev) {
		*addr = usb1dev->hub;
		*port = usb1dev->port;
		return 0;
	}

	usb_debug("Couldn't find closest USB2.0 hub.\n");
	return 1;
}