summaryrefslogtreecommitdiffstats
path: root/src/roms/SLOF/lib/libusb/usb-hub.c
blob: 7059cd0199d91097edcdec717dcbc1732bde8187 (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
/*****************************************************************************
 * Copyright (c) 2013 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <stdio.h>
#include <string.h>
#include "usb-core.h"

#undef HUB_DEBUG
//#define HUB_DEBUG
#ifdef HUB_DEBUG
#define dprintf(_x ...) do { printf(_x); } while(0)
#else
#define dprintf(_x ...)
#endif

/*
 * USB Spec 1.1
 *            11.16.2 Class-specific Requests
 */
struct usb_hub_ps {
	uint16_t wPortStatus;
	uint16_t wPortChange;
} __attribute__((packed));

#define HUB_PS_CONNECTION            (1 << 0)
#define HUB_PS_ENABLE                (1 << 1)
#define HUB_PS_SUSPEND               (1 << 2)
#define HUB_PS_OVER_CURRENT          (1 << 3)
#define HUB_PS_RESET                 (1 << 4)
#define HUB_PS_POWER                 (1 << 8)
#define HUB_PS_LOW_SPEED             (1 << 9)

#define HUB_PF_CONNECTION        0
#define HUB_PF_ENABLE            1
#define HUB_PF_SUSPEND           2
#define HUB_PF_OVER_CURRENT      3
#define HUB_PF_RESET             4
#define HUB_PF_POWER             8
#define HUB_PF_LOWSPEED          9
#define HUB_PF_C_CONNECTION      16
#define HUB_PF_C_ENABLE          17
#define HUB_PF_C_SUSPEND         18
#define HUB_PF_C_OVER_CURRENT    19
#define HUB_PF_C_RESET           20

static int usb_get_hub_desc(struct usb_dev *dev, void *data, size_t size)
{
	struct usb_dev_req req;
	if (!dev)
		return false;
	req.bmRequestType = REQT_DIR_IN | REQT_TYPE_CLASS | REQT_REC_DEVICE;
	req.bRequest = REQ_GET_DESCRIPTOR;
	req.wIndex = 0;
	req.wLength = cpu_to_le16((uint16_t) size);
	req.wValue = cpu_to_le16(DESCR_TYPE_HUB << 8);
	return usb_send_ctrl(dev->control, &req, data);
}

static int hub_get_port_status(struct usb_dev *dev, int port, void *data, size_t size)
{
	struct usb_dev_req req;
	if (!dev)
		return false;
	req.bmRequestType = REQT_DIR_IN | REQT_TYPE_CLASS | REQT_REC_OTHER;
	req.bRequest = REQ_GET_STATUS;
	req.wValue = 0;
	req.wIndex = cpu_to_le16((uint16_t)(port + 1));
	req.wLength = cpu_to_le16((uint16_t)size);
	return usb_send_ctrl(dev->control, &req, data);
}

static int hub_set_port_feature(struct usb_dev *dev, int port, int feature)
{
	struct usb_dev_req req;
	if (!dev)
		return false;
	req.bmRequestType = REQT_DIR_OUT | REQT_TYPE_CLASS | REQT_REC_OTHER;
	req.bRequest = REQ_SET_FEATURE;
	req.wLength = 0;
	req.wValue = cpu_to_le16((uint16_t)feature);
	req.wIndex = cpu_to_le16((uint16_t)(port + 1));
	return usb_send_ctrl(dev->control, &req, NULL);
}

#if 0
static int hub_clear_port_feature(struct usb_dev *dev, int port, int feature)
{
	struct usb_dev_req req;
	if (!dev)
		return false;
	req.bmRequestType = REQT_DIR_OUT | REQT_TYPE_CLASS | REQT_REC_OTHER;
	req.bRequest = REQ_CLEAR_FEATURE;
	req.wLength = 0;
	req.wValue = cpu_to_le16((uint16_t)feature);
	req.wIndex = cpu_to_le16((uint16_t)(port + 1));
	return usb_send_ctrl(dev->control, &req, NULL);
}
#endif

static int hub_check_port(struct usb_dev *dev, int port)
{
	struct usb_hub_ps ps;
	uint32_t time;

	if (!hub_get_port_status(dev, port, &ps, sizeof(ps)))
		return false;
	dprintf("Port Status %04X Port Change %04X\n",
		le16_to_cpu(ps.wPortStatus),
		le16_to_cpu(ps.wPortChange));

	if (!(le16_to_cpu(ps.wPortStatus) & HUB_PS_POWER)) {
		hub_set_port_feature(dev, port, HUB_PF_POWER);
		SLOF_msleep(100);
		time = SLOF_GetTimer() + USB_TIMEOUT;
		while (time > SLOF_GetTimer()) {
			cpu_relax();
			hub_get_port_status(dev, port, &ps, sizeof(ps));
			if (le16_to_cpu(ps.wPortStatus) & HUB_PS_CONNECTION) {
				dprintf("power on Port Status %04X Port Change %04X\n",
					le16_to_cpu(ps.wPortStatus),
					le16_to_cpu(ps.wPortChange));
				break;
			}
		}
	}

	if (le16_to_cpu(ps.wPortStatus) & HUB_PS_CONNECTION) {
		hub_set_port_feature(dev, port, HUB_PF_RESET);
		SLOF_msleep(100);
		time = SLOF_GetTimer() + USB_TIMEOUT;
		while (time > SLOF_GetTimer()) {
			cpu_relax();
			hub_get_port_status(dev, port, &ps, sizeof(ps));
			if (!(le16_to_cpu(ps.wPortStatus) & HUB_PS_RESET)) {
				dprintf("reset Port Status %04X Port Change %04X\n",
					le16_to_cpu(ps.wPortStatus),
					le16_to_cpu(ps.wPortChange));
				return true;
			}
		}
	}
	return false;
}

unsigned int usb_hub_init(void *hubdev)
{
	struct usb_dev *dev = hubdev;
	struct usb_dev_hub_descr hub;
	struct usb_dev *newdev;
	int i;

	dprintf("%s: enter %p\n", __func__, dev);
	if (!dev) {
		printf("usb-hub: NULL\n");
		return false;
	}
	memset(&hub, 0, sizeof(hub));
	usb_get_hub_desc(dev, &hub, sizeof(hub));
	dprintf("usb-hub: ports connected %d\n", hub.bNbrPorts);
	for (i = 0; i < hub.bNbrPorts; i++) {
		dprintf("usb-hub: ports scanning %d\n", i);
		if (hub_check_port(dev, i)) {
			dprintf("***********************************************\n");
			dprintf("\t\tusb-hub: device found %d\n", i);
			dprintf("***********************************************\n");
			newdev = usb_devpool_get();
			dprintf("usb-hub: allocated device %p\n", newdev);
			newdev->hcidev = dev->hcidev;
			if (!setup_new_device(newdev, i))
				printf("usb-hub: unable to setup device on port %d\n", i);
		}
	}
	return true;
}
OpenPOWER on IntegriCloud