xref: /rk3399_rockchip-uboot/common/usb_hub.c (revision e7e982d69c43b89f7e19b63479c8b2e880fbd75c)
1 /*
2  *
3  * Most of this source has been derived from the Linux USB
4  * project:
5  * (C) Copyright Linus Torvalds 1999
6  * (C) Copyright Johannes Erdfelt 1999-2001
7  * (C) Copyright Andreas Gal 1999
8  * (C) Copyright Gregory P. Smith 1999
9  * (C) Copyright Deti Fliegl 1999 (new USB architecture)
10  * (C) Copyright Randy Dunlap 2000
11  * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
12  * (C) Copyright Yggdrasil Computing, Inc. 2000
13  *     (usb_device_id matching changes by Adam J. Richter)
14  *
15  * Adapted for U-Boot:
16  * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
17  *
18  * See file CREDITS for list of people who contributed to this
19  * project.
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License as
23  * published by the Free Software Foundation; either version 2 of
24  * the License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34  * MA 02111-1307 USA
35  *
36  */
37 
38 /****************************************************************************
39  * HUB "Driver"
40  * Probes device for being a hub and configurate it
41  */
42 
43 #include <common.h>
44 #include <command.h>
45 #include <asm/processor.h>
46 #include <linux/ctype.h>
47 #include <asm/byteorder.h>
48 #include <asm/unaligned.h>
49 
50 #include <usb.h>
51 #ifdef CONFIG_4xx
52 #include <asm/4xx_pci.h>
53 #endif
54 
55 #ifdef DEBUG
56 #define USB_DEBUG	1
57 #define USB_HUB_DEBUG	1
58 #else
59 #define USB_DEBUG	0
60 #define USB_HUB_DEBUG	0
61 #endif
62 
63 #define USB_PRINTF(fmt, args...)	debug_cond(USB_DEBUG, fmt, ##args)
64 #define USB_HUB_PRINTF(fmt, args...)	debug_cond(USB_HUB_DEBUG, fmt, ##args)
65 
66 #define USB_BUFSIZ	512
67 
68 static struct usb_hub_device hub_dev[USB_MAX_HUB];
69 static int usb_hub_index;
70 
71 
72 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
73 {
74 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
75 		USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
76 		USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
77 }
78 
79 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
80 {
81 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
82 				USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
83 				port, NULL, 0, USB_CNTL_TIMEOUT);
84 }
85 
86 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
87 {
88 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89 				USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
90 				port, NULL, 0, USB_CNTL_TIMEOUT);
91 }
92 
93 static int usb_get_hub_status(struct usb_device *dev, void *data)
94 {
95 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
96 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
97 			data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
98 }
99 
100 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
101 {
102 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
103 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
104 			data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
105 }
106 
107 
108 static void usb_hub_power_on(struct usb_hub_device *hub)
109 {
110 	int i;
111 	struct usb_device *dev;
112 
113 	dev = hub->pusb_dev;
114 	/* Enable power to the ports */
115 	USB_HUB_PRINTF("enabling power on all ports\n");
116 	for (i = 0; i < dev->maxchild; i++) {
117 		usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
118 		USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
119 		wait_ms(hub->desc.bPwrOn2PwrGood * 2);
120 	}
121 }
122 
123 void usb_hub_reset(void)
124 {
125 	usb_hub_index = 0;
126 }
127 
128 static struct usb_hub_device *usb_hub_allocate(void)
129 {
130 	if (usb_hub_index < USB_MAX_HUB)
131 		return &hub_dev[usb_hub_index++];
132 
133 	printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
134 	return NULL;
135 }
136 
137 #define MAX_TRIES 5
138 
139 static inline char *portspeed(int portstatus)
140 {
141 	if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
142 		return "480 Mb/s";
143 	else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
144 		return "1.5 Mb/s";
145 	else
146 		return "12 Mb/s";
147 }
148 
149 int hub_port_reset(struct usb_device *dev, int port,
150 			unsigned short *portstat)
151 {
152 	int tries;
153 	struct usb_port_status portsts;
154 	unsigned short portstatus, portchange;
155 
156 	USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
157 	for (tries = 0; tries < MAX_TRIES; tries++) {
158 
159 		usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
160 		wait_ms(200);
161 
162 		if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
163 			USB_HUB_PRINTF("get_port_status failed status %lX\n",
164 					dev->status);
165 			return -1;
166 		}
167 		portstatus = le16_to_cpu(portsts.wPortStatus);
168 		portchange = le16_to_cpu(portsts.wPortChange);
169 
170 		USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
171 				portstatus, portchange,
172 				portspeed(portstatus));
173 
174 		USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
175 			       "  USB_PORT_STAT_ENABLE %d\n",
176 			(portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
177 			(portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
178 			(portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
179 
180 		if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
181 		    !(portstatus & USB_PORT_STAT_CONNECTION))
182 			return -1;
183 
184 		if (portstatus & USB_PORT_STAT_ENABLE)
185 			break;
186 
187 		wait_ms(200);
188 	}
189 
190 	if (tries == MAX_TRIES) {
191 		USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
192 				"disabling port.\n", port + 1, MAX_TRIES);
193 		USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
194 		return -1;
195 	}
196 
197 	usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
198 	*portstat = portstatus;
199 	return 0;
200 }
201 
202 
203 void usb_hub_port_connect_change(struct usb_device *dev, int port)
204 {
205 	struct usb_device *usb;
206 	struct usb_port_status portsts;
207 	unsigned short portstatus;
208 
209 	/* Check status */
210 	if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
211 		USB_HUB_PRINTF("get_port_status failed\n");
212 		return;
213 	}
214 
215 	portstatus = le16_to_cpu(portsts.wPortStatus);
216 	USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
217 			portstatus,
218 			le16_to_cpu(portsts.wPortChange),
219 			portspeed(portstatus));
220 
221 	/* Clear the connection change status */
222 	usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
223 
224 	/* Disconnect any existing devices under this port */
225 	if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
226 	     (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
227 		USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
228 		/* Return now if nothing is connected */
229 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
230 			return;
231 	}
232 	wait_ms(200);
233 
234 	/* Reset the port */
235 	if (hub_port_reset(dev, port, &portstatus) < 0) {
236 		printf("cannot reset port %i!?\n", port + 1);
237 		return;
238 	}
239 
240 	wait_ms(200);
241 
242 	/* Allocate a new device struct for it */
243 	usb = usb_alloc_new_device();
244 
245 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
246 		usb->speed = USB_SPEED_HIGH;
247 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
248 		usb->speed = USB_SPEED_LOW;
249 	else
250 		usb->speed = USB_SPEED_FULL;
251 
252 	dev->children[port] = usb;
253 	usb->parent = dev;
254 	usb->portnr = port + 1;
255 	/* Run it through the hoops (find a driver, etc) */
256 	if (usb_new_device(usb)) {
257 		/* Woops, disable the port */
258 		USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
259 		usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
260 	}
261 }
262 
263 
264 static int usb_hub_configure(struct usb_device *dev)
265 {
266 	int i;
267 	unsigned char buffer[USB_BUFSIZ], *bitmap;
268 	struct usb_hub_descriptor *descriptor;
269 	struct usb_hub_device *hub;
270 #ifdef USB_HUB_DEBUG
271 	struct usb_hub_status *hubsts;
272 #endif
273 
274 	/* "allocate" Hub device */
275 	hub = usb_hub_allocate();
276 	if (hub == NULL)
277 		return -1;
278 	hub->pusb_dev = dev;
279 	/* Get the the hub descriptor */
280 	if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
281 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
282 				   "descriptor, giving up %lX\n", dev->status);
283 		return -1;
284 	}
285 	descriptor = (struct usb_hub_descriptor *)buffer;
286 
287 	/* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
288 	i = descriptor->bLength;
289 	if (i > USB_BUFSIZ) {
290 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
291 				"descriptor - too long: %d\n",
292 				descriptor->bLength);
293 		return -1;
294 	}
295 
296 	if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
297 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
298 				"descriptor 2nd giving up %lX\n", dev->status);
299 		return -1;
300 	}
301 	memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
302 	/* adjust 16bit values */
303 	hub->desc.wHubCharacteristics =
304 				le16_to_cpu(descriptor->wHubCharacteristics);
305 	/* set the bitmap */
306 	bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
307 	/* devices not removable by default */
308 	memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
309 	bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
310 	memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
311 
312 	for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
313 		hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
314 
315 	for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
316 		hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
317 
318 	dev->maxchild = descriptor->bNbrPorts;
319 	USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
320 
321 	switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
322 	case 0x00:
323 		USB_HUB_PRINTF("ganged power switching\n");
324 		break;
325 	case 0x01:
326 		USB_HUB_PRINTF("individual port power switching\n");
327 		break;
328 	case 0x02:
329 	case 0x03:
330 		USB_HUB_PRINTF("unknown reserved power switching mode\n");
331 		break;
332 	}
333 
334 	if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
335 		USB_HUB_PRINTF("part of a compound device\n");
336 	else
337 		USB_HUB_PRINTF("standalone hub\n");
338 
339 	switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
340 	case 0x00:
341 		USB_HUB_PRINTF("global over-current protection\n");
342 		break;
343 	case 0x08:
344 		USB_HUB_PRINTF("individual port over-current protection\n");
345 		break;
346 	case 0x10:
347 	case 0x18:
348 		USB_HUB_PRINTF("no over-current protection\n");
349 		break;
350 	}
351 
352 	USB_HUB_PRINTF("power on to power good time: %dms\n",
353 			descriptor->bPwrOn2PwrGood * 2);
354 	USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
355 			descriptor->bHubContrCurrent);
356 
357 	for (i = 0; i < dev->maxchild; i++)
358 		USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
359 			hub->desc.DeviceRemovable[(i + 1) / 8] & \
360 					   (1 << ((i + 1) % 8)) ? " not" : "");
361 
362 	if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
363 		USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
364 				"too long: %d\n", descriptor->bLength);
365 		return -1;
366 	}
367 
368 	if (usb_get_hub_status(dev, buffer) < 0) {
369 		USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
370 				dev->status);
371 		return -1;
372 	}
373 
374 #ifdef USB_HUB_DEBUG
375 	hubsts = (struct usb_hub_status *)buffer;
376 #endif
377 	USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
378 			le16_to_cpu(hubsts->wHubStatus),
379 			le16_to_cpu(hubsts->wHubChange));
380 	USB_HUB_PRINTF("local power source is %s\n",
381 		(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
382 		"lost (inactive)" : "good");
383 	USB_HUB_PRINTF("%sover-current condition exists\n",
384 		(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
385 		"" : "no ");
386 	usb_hub_power_on(hub);
387 
388 	for (i = 0; i < dev->maxchild; i++) {
389 		struct usb_port_status portsts;
390 		unsigned short portstatus, portchange;
391 
392 		if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
393 			USB_HUB_PRINTF("get_port_status failed\n");
394 			continue;
395 		}
396 
397 		portstatus = le16_to_cpu(portsts.wPortStatus);
398 		portchange = le16_to_cpu(portsts.wPortChange);
399 		USB_HUB_PRINTF("Port %d Status %X Change %X\n",
400 				i + 1, portstatus, portchange);
401 
402 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
403 			USB_HUB_PRINTF("port %d connection change\n", i + 1);
404 			usb_hub_port_connect_change(dev, i);
405 		}
406 		if (portchange & USB_PORT_STAT_C_ENABLE) {
407 			USB_HUB_PRINTF("port %d enable change, status %x\n",
408 					i + 1, portstatus);
409 			usb_clear_port_feature(dev, i + 1,
410 						USB_PORT_FEAT_C_ENABLE);
411 
412 			/* EM interference sometimes causes bad shielded USB
413 			 * devices to be shutdown by the hub, this hack enables
414 			 * them again. Works at least with mouse driver */
415 			if (!(portstatus & USB_PORT_STAT_ENABLE) &&
416 			     (portstatus & USB_PORT_STAT_CONNECTION) &&
417 			     ((dev->children[i]))) {
418 				USB_HUB_PRINTF("already running port %i "  \
419 						"disabled by hub (EMI?), " \
420 						"re-enabling...\n", i + 1);
421 					usb_hub_port_connect_change(dev, i);
422 			}
423 		}
424 		if (portstatus & USB_PORT_STAT_SUSPEND) {
425 			USB_HUB_PRINTF("port %d suspend change\n", i + 1);
426 			usb_clear_port_feature(dev, i + 1,
427 						USB_PORT_FEAT_SUSPEND);
428 		}
429 
430 		if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
431 			USB_HUB_PRINTF("port %d over-current change\n", i + 1);
432 			usb_clear_port_feature(dev, i + 1,
433 						USB_PORT_FEAT_C_OVER_CURRENT);
434 			usb_hub_power_on(hub);
435 		}
436 
437 		if (portchange & USB_PORT_STAT_C_RESET) {
438 			USB_HUB_PRINTF("port %d reset change\n", i + 1);
439 			usb_clear_port_feature(dev, i + 1,
440 						USB_PORT_FEAT_C_RESET);
441 		}
442 	} /* end for i all ports */
443 
444 	return 0;
445 }
446 
447 int usb_hub_probe(struct usb_device *dev, int ifnum)
448 {
449 	struct usb_interface *iface;
450 	struct usb_endpoint_descriptor *ep;
451 	int ret;
452 
453 	iface = &dev->config.if_desc[ifnum];
454 	/* Is it a hub? */
455 	if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
456 		return 0;
457 	/* Some hubs have a subclass of 1, which AFAICT according to the */
458 	/*  specs is not defined, but it works */
459 	if ((iface->desc.bInterfaceSubClass != 0) &&
460 	    (iface->desc.bInterfaceSubClass != 1))
461 		return 0;
462 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
463 	if (iface->desc.bNumEndpoints != 1)
464 		return 0;
465 	ep = &iface->ep_desc[0];
466 	/* Output endpoint? Curiousier and curiousier.. */
467 	if (!(ep->bEndpointAddress & USB_DIR_IN))
468 		return 0;
469 	/* If it's not an interrupt endpoint, we'd better punt! */
470 	if ((ep->bmAttributes & 3) != 3)
471 		return 0;
472 	/* We found a hub */
473 	USB_HUB_PRINTF("USB hub found\n");
474 	ret = usb_hub_configure(dev);
475 	return ret;
476 }
477