aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/src/usbh/hal_usbh_hub.c
blob: 6a83c663e0f5d0c533775f4dee09325ff525c589 (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
/*
    ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
              Copyright (C) 2015..2017 Diego Ismirlian, (dismirlian (at) google's mail)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "hal.h"

#if HAL_USBH_USE_HUB

#if !HAL_USE_USBH
#error "USBHHUB needs HAL_USE_USBH"
#endif

#include <string.h>
#include "usbh/dev/hub.h"
#include "usbh/internal.h"

#if USBHHUB_DEBUG_ENABLE_TRACE
#define udbgf(f, ...)  usbDbgPrintf(f, ##__VA_ARGS__)
#define udbg(f, ...)  usbDbgPuts(f, ##__VA_ARGS__)
#else
#define udbgf(f, ...)  do {} while(0)
#define udbg(f, ...)   do {} while(0)
#endif

#if USBHHUB_DEBUG_ENABLE_INFO
#define uinfof(f, ...)  usbDbgPrintf(f, ##__VA_ARGS__)
#define uinfo(f, ...)  usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uinfof(f, ...)  do {} while(0)
#define uinfo(f, ...)   do {} while(0)
#endif

#if USBHHUB_DEBUG_ENABLE_WARNINGS
#define uwarnf(f, ...)  usbDbgPrintf(f, ##__VA_ARGS__)
#define uwarn(f, ...)  usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uwarnf(f, ...)  do {} while(0)
#define uwarn(f, ...)   do {} while(0)
#endif

#if USBHHUB_DEBUG_ENABLE_ERRORS
#define uerrf(f, ...)  usbDbgPrintf(f, ##__VA_ARGS__)
#define uerr(f, ...)  usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uerrf(f, ...)  do {} while(0)
#define uerr(f, ...)   do {} while(0)
#endif


USBHHubDriver USBHHUBD[HAL_USBHHUB_MAX_INSTANCES];
static usbh_port_t USBHPorts[HAL_USBHHUB_MAX_PORTS];

static void _hub_init(void);
static usbh_baseclassdriver_t *_hub_load(usbh_device_t *dev, const uint8_t *descriptor, uint16_t rem);
static void _hub_unload(usbh_baseclassdriver_t *drv);
static const usbh_classdriver_vmt_t usbhhubClassDriverVMT = {
	_hub_init,
	_hub_load,
	_hub_unload
};

const usbh_classdriverinfo_t usbhhubClassDriverInfo = {
	"HUB", &usbhhubClassDriverVMT
};


void _usbhub_port_object_init(usbh_port_t *port, USBHDriver *usbh,
		USBHHubDriver *hub, uint8_t number) {
	memset(port, 0, sizeof(*port));
	port->number = number;
	port->device.host = usbh;
	port->hub = hub;
}

usbh_urbstatus_t usbhhubControlRequest(USBHDriver *host, USBHHubDriver *hub,
											uint8_t bmRequestType,
											uint8_t bRequest,
											uint16_t wValue,
											uint16_t wIndex,
											uint16_t wLength,
											uint8_t *buf) {
	if (hub == NULL)
		return usbh_lld_root_hub_request(host, bmRequestType, bRequest, wValue, wIndex, wLength, buf);

	return usbhControlRequest(hub->dev,
			bmRequestType, bRequest, wValue, wIndex, wLength, buf);
}


static void _urb_complete(usbh_urb_t *urb) {

	USBHHubDriver *const hubdp = (USBHHubDriver *)urb->userData;
	switch (urb->status) {
	case USBH_URBSTATUS_TIMEOUT:
		/* the device NAKed */
		udbg("HUB: no info");
		//hubdp->statuschange = 0;
		break;
	case USBH_URBSTATUS_OK: {
		uint8_t len = hubdp->hubDesc.bNbrPorts / 8 + 1;
		if (urb->actualLength != len) {
			uwarnf("Expected %d status change bytes but got %d", len, urb->actualLength);
		}

		if (urb->actualLength < len)
			len = urb->actualLength;

		if (len > 4)
			len = 4;

		uint8_t *sc = (uint8_t *)&hubdp->statuschange;
		uint8_t *r = hubdp->scbuff;
		while (len--)
			*sc++ |= *r++;

		uinfof("HUB: change, %08x", hubdp->statuschange);
	}	break;
	case USBH_URBSTATUS_DISCONNECTED:
		uwarn("HUB: URB disconnected, aborting poll");
		return;
	default:
		uerrf("HUB: URB status unexpected = %d", urb->status);
		break;
	}

	usbhURBObjectResetI(urb);
	usbhURBSubmitI(urb);
}

static usbh_baseclassdriver_t *_hub_load(usbh_device_t *dev,
		const uint8_t *descriptor, uint16_t rem) {
	int i;

	USBHHubDriver *hubdp;

	if (_usbh_match_descriptor(descriptor, rem, USBH_DT_DEVICE,
			0x09, 0x00, 0x00) != HAL_SUCCESS)
		return NULL;

	generic_iterator_t iep, icfg;
	if_iterator_t iif;

	cfg_iter_init(&icfg, dev->fullConfigurationDescriptor,
			dev->basicConfigDesc.wTotalLength);

	if_iter_init(&iif, &icfg);
	if (!iif.valid)
		return NULL;

	if (_usbh_match_descriptor(iif.curr, iif.rem, USBH_DT_INTERFACE,
			0x09, 0x00, 0x00) != HAL_SUCCESS)
		return NULL;

	ep_iter_init(&iep, &iif);
	if (!iep.valid)
		return NULL;
	const usbh_endpoint_descriptor_t *const epdesc = ep_get(&iep);
	if ((epdesc->bmAttributes & 0x03) != USBH_EPTYPE_INT) {
		return NULL;
	}


	/* alloc driver */
	for (i = 0; i < HAL_USBHHUB_MAX_INSTANCES; i++) {
		if (USBHHUBD[i].dev == NULL) {
			hubdp = &USBHHUBD[i];
			goto alloc_ok;
		}
	}

	uwarn("Can't alloc HUB driver");

	/* can't alloc */
	return NULL;

alloc_ok:
	/* initialize the driver's variables */
	hubdp->epint.status = USBH_EPSTATUS_UNINITIALIZED;
	hubdp->dev = dev;
	hubdp->ports = 0;

	usbhEPSetName(&dev->ctrl, "HUB[CTRL]");

	/* read Hub descriptor */
	uinfo("Read Hub descriptor");
	if (usbhhubControlRequest(dev->host, hubdp,
			USBH_REQTYPE_DIR_IN | USBH_REQTYPE_TYPE_CLASS | USBH_REQTYPE_RECIP_DEVICE,
			USBH_REQ_GET_DESCRIPTOR,
			(USBH_DT_HUB << 8), 0, sizeof(hubdp->hubDesc),
			(uint8_t *)&hubdp->hubDesc) != USBH_URBSTATUS_OK) {
		hubdp->dev = NULL;
		return NULL;
	}

	const usbh_hub_descriptor_t *const hubdesc = &hubdp->hubDesc;

	uinfof("Hub descriptor loaded; %d ports, wHubCharacteristics=%04x, bPwrOn2PwrGood=%d, bHubContrCurrent=%d",
			hubdesc->bNbrPorts,
			hubdesc->wHubCharacteristics,
			hubdesc->bPwrOn2PwrGood,
			hubdesc->bHubContrCurrent);

	/* Alloc ports */
	uint8_t ports = hubdesc->bNbrPorts;
	for (i = 0; (ports > 0) && (i < HAL_USBHHUB_MAX_PORTS); i++) {
		if (USBHPorts[i].hub == NULL) {
			uinfof("Alloc port %d", ports);
			_usbhub_port_object_init(&USBHPorts[i], dev->host, hubdp, ports);
			USBHPorts[i].next = hubdp->ports;
			hubdp->ports = &USBHPorts[i];
			--ports;
		}
	}

	if (ports) {
		uwarn("Could not alloc all ports");
	}

	/* link hub to the host's list */
	list_add_tail(&hubdp->node, &dev->host->hubs);

	/* enable power to ports */
	usbh_port_t *port = hubdp->ports;
	while (port) {
		uinfof("Enable power for port %d", port->number);
		usbhhubSetFeaturePort(port, USBH_PORT_FEAT_POWER);
		port = port->next;
	}

	if (hubdesc->bPwrOn2PwrGood)
		osalThreadSleepMilliseconds(2 * hubdesc->bPwrOn2PwrGood);

	/* initialize the status change endpoint and trigger the first transfer */
	usbhEPObjectInit(&hubdp->epint, dev, epdesc);
	usbhEPSetName(&hubdp->epint, "HUB[INT ]");
	usbhEPOpen(&hubdp->epint);

	usbhURBObjectInit(&hubdp->urb, &hubdp->epint,
			_urb_complete, hubdp, hubdp->scbuff,
			(hubdesc->bNbrPorts + 8) / 8);

	usbhURBSubmit(&hubdp->urb);

	hubdp->dev = NULL;
	return (usbh_baseclassdriver_t *)hubdp;
}

static void _hub_unload(usbh_baseclassdriver_t *drv) {
	osalDbgCheck(drv != NULL);
	USBHHubDriver *const hubdp = (USBHHubDriver *)drv;

	/* close the status change endpoint (this cancels ongoing URBs) */
	usbhEPClose(&hubdp->epint);

	/* de-alloc ports and unload drivers */
	usbh_port_t *port = hubdp->ports;
	while (port) {
		_usbh_port_disconnected(port);
		port->hub = NULL;
		port = port->next;
	}

	/* unlink the hub from the host's list */
	list_del(&hubdp->node);

}

static void _object_init(USBHHubDriver *hubdp) {
	osalDbgCheck(hubdp != NULL);
	memset(hubdp, 0, sizeof(*hubdp));
	hubdp->info = &usbhhubClassDriverInfo;
}

static void _hub_init(void) {
	uint8_t i;
	for (i = 0; i < HAL_USBHHUB_MAX_INSTANCES; i++) {
		_object_init(&USBHHUBD[i]);
	}
}

#else

#if HAL_USE_USBH
#include <string.h>
void _usbhub_port_object_init(usbh_port_t *port, USBHDriver *usbh, uint8_t number) {
	memset(port, 0, sizeof(*port));
	port->number = number;
	port->device.host = usbh;
}
#endif

#endif