xref: /openbmc/u-boot/common/usb_hub.c (revision 7dfb0602)
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 	unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
113 
114 	dev = hub->pusb_dev;
115 	/* Enable power to the ports */
116 	USB_HUB_PRINTF("enabling power on all ports\n");
117 	for (i = 0; i < dev->maxchild; i++) {
118 		usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
119 		USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
120 	}
121 
122 	/* Wait at least 100 msec for power to become stable */
123 	mdelay(max(pgood_delay, (unsigned)100));
124 }
125 
126 void usb_hub_reset(void)
127 {
128 	usb_hub_index = 0;
129 }
130 
131 static struct usb_hub_device *usb_hub_allocate(void)
132 {
133 	if (usb_hub_index < USB_MAX_HUB)
134 		return &hub_dev[usb_hub_index++];
135 
136 	printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
137 	return NULL;
138 }
139 
140 #define MAX_TRIES 5
141 
142 static inline char *portspeed(int portstatus)
143 {
144 	if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
145 		return "480 Mb/s";
146 	else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
147 		return "1.5 Mb/s";
148 	else
149 		return "12 Mb/s";
150 }
151 
152 int hub_port_reset(struct usb_device *dev, int port,
153 			unsigned short *portstat)
154 {
155 	int tries;
156 	ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
157 	unsigned short portstatus, portchange;
158 
159 	USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
160 	for (tries = 0; tries < MAX_TRIES; tries++) {
161 
162 		usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
163 		mdelay(200);
164 
165 		if (usb_get_port_status(dev, port + 1, portsts) < 0) {
166 			USB_HUB_PRINTF("get_port_status failed status %lX\n",
167 					dev->status);
168 			return -1;
169 		}
170 		portstatus = le16_to_cpu(portsts->wPortStatus);
171 		portchange = le16_to_cpu(portsts->wPortChange);
172 
173 		USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
174 				portstatus, portchange,
175 				portspeed(portstatus));
176 
177 		USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
178 			       "  USB_PORT_STAT_ENABLE %d\n",
179 			(portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
180 			(portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
181 			(portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
182 
183 		if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
184 		    !(portstatus & USB_PORT_STAT_CONNECTION))
185 			return -1;
186 
187 		if (portstatus & USB_PORT_STAT_ENABLE)
188 			break;
189 
190 		mdelay(200);
191 	}
192 
193 	if (tries == MAX_TRIES) {
194 		USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
195 				"disabling port.\n", port + 1, MAX_TRIES);
196 		USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
197 		return -1;
198 	}
199 
200 	usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
201 	*portstat = portstatus;
202 	return 0;
203 }
204 
205 
206 void usb_hub_port_connect_change(struct usb_device *dev, int port)
207 {
208 	struct usb_device *usb;
209 	ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
210 	unsigned short portstatus;
211 
212 	/* Check status */
213 	if (usb_get_port_status(dev, port + 1, portsts) < 0) {
214 		USB_HUB_PRINTF("get_port_status failed\n");
215 		return;
216 	}
217 
218 	portstatus = le16_to_cpu(portsts->wPortStatus);
219 	USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
220 			portstatus,
221 			le16_to_cpu(portsts->wPortChange),
222 			portspeed(portstatus));
223 
224 	/* Clear the connection change status */
225 	usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
226 
227 	/* Disconnect any existing devices under this port */
228 	if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
229 	     (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
230 		USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
231 		/* Return now if nothing is connected */
232 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
233 			return;
234 	}
235 	mdelay(200);
236 
237 	/* Reset the port */
238 	if (hub_port_reset(dev, port, &portstatus) < 0) {
239 		printf("cannot reset port %i!?\n", port + 1);
240 		return;
241 	}
242 
243 	mdelay(200);
244 
245 	/* Allocate a new device struct for it */
246 	usb = usb_alloc_new_device();
247 
248 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
249 		usb->speed = USB_SPEED_HIGH;
250 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
251 		usb->speed = USB_SPEED_LOW;
252 	else
253 		usb->speed = USB_SPEED_FULL;
254 
255 	dev->children[port] = usb;
256 	usb->parent = dev;
257 	usb->portnr = port + 1;
258 	/* Run it through the hoops (find a driver, etc) */
259 	if (usb_new_device(usb)) {
260 		/* Woops, disable the port */
261 		USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
262 		usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
263 	}
264 }
265 
266 
267 static int usb_hub_configure(struct usb_device *dev)
268 {
269 	int i;
270 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
271 	unsigned char *bitmap;
272 	struct usb_hub_descriptor *descriptor;
273 	struct usb_hub_device *hub;
274 #ifdef USB_HUB_DEBUG
275 	struct usb_hub_status *hubsts;
276 #endif
277 
278 	/* "allocate" Hub device */
279 	hub = usb_hub_allocate();
280 	if (hub == NULL)
281 		return -1;
282 	hub->pusb_dev = dev;
283 	/* Get the the hub descriptor */
284 	if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
285 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
286 				   "descriptor, giving up %lX\n", dev->status);
287 		return -1;
288 	}
289 	descriptor = (struct usb_hub_descriptor *)buffer;
290 
291 	/* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
292 	i = descriptor->bLength;
293 	if (i > USB_BUFSIZ) {
294 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
295 				"descriptor - too long: %d\n",
296 				descriptor->bLength);
297 		return -1;
298 	}
299 
300 	if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
301 		USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
302 				"descriptor 2nd giving up %lX\n", dev->status);
303 		return -1;
304 	}
305 	memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
306 	/* adjust 16bit values */
307 	hub->desc.wHubCharacteristics =
308 				le16_to_cpu(descriptor->wHubCharacteristics);
309 	/* set the bitmap */
310 	bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
311 	/* devices not removable by default */
312 	memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
313 	bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
314 	memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
315 
316 	for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
317 		hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
318 
319 	for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
320 		hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
321 
322 	dev->maxchild = descriptor->bNbrPorts;
323 	USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
324 
325 	switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
326 	case 0x00:
327 		USB_HUB_PRINTF("ganged power switching\n");
328 		break;
329 	case 0x01:
330 		USB_HUB_PRINTF("individual port power switching\n");
331 		break;
332 	case 0x02:
333 	case 0x03:
334 		USB_HUB_PRINTF("unknown reserved power switching mode\n");
335 		break;
336 	}
337 
338 	if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
339 		USB_HUB_PRINTF("part of a compound device\n");
340 	else
341 		USB_HUB_PRINTF("standalone hub\n");
342 
343 	switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
344 	case 0x00:
345 		USB_HUB_PRINTF("global over-current protection\n");
346 		break;
347 	case 0x08:
348 		USB_HUB_PRINTF("individual port over-current protection\n");
349 		break;
350 	case 0x10:
351 	case 0x18:
352 		USB_HUB_PRINTF("no over-current protection\n");
353 		break;
354 	}
355 
356 	USB_HUB_PRINTF("power on to power good time: %dms\n",
357 			descriptor->bPwrOn2PwrGood * 2);
358 	USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
359 			descriptor->bHubContrCurrent);
360 
361 	for (i = 0; i < dev->maxchild; i++)
362 		USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
363 			hub->desc.DeviceRemovable[(i + 1) / 8] & \
364 					   (1 << ((i + 1) % 8)) ? " not" : "");
365 
366 	if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
367 		USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
368 				"too long: %d\n", descriptor->bLength);
369 		return -1;
370 	}
371 
372 	if (usb_get_hub_status(dev, buffer) < 0) {
373 		USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
374 				dev->status);
375 		return -1;
376 	}
377 
378 #ifdef USB_HUB_DEBUG
379 	hubsts = (struct usb_hub_status *)buffer;
380 #endif
381 	USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
382 			le16_to_cpu(hubsts->wHubStatus),
383 			le16_to_cpu(hubsts->wHubChange));
384 	USB_HUB_PRINTF("local power source is %s\n",
385 		(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
386 		"lost (inactive)" : "good");
387 	USB_HUB_PRINTF("%sover-current condition exists\n",
388 		(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
389 		"" : "no ");
390 	usb_hub_power_on(hub);
391 
392 	for (i = 0; i < dev->maxchild; i++) {
393 		ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
394 		unsigned short portstatus, portchange;
395 
396 		if (usb_get_port_status(dev, i + 1, portsts) < 0) {
397 			USB_HUB_PRINTF("get_port_status failed\n");
398 			continue;
399 		}
400 
401 		portstatus = le16_to_cpu(portsts->wPortStatus);
402 		portchange = le16_to_cpu(portsts->wPortChange);
403 		USB_HUB_PRINTF("Port %d Status %X Change %X\n",
404 				i + 1, portstatus, portchange);
405 
406 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
407 			USB_HUB_PRINTF("port %d connection change\n", i + 1);
408 			usb_hub_port_connect_change(dev, i);
409 		}
410 		if (portchange & USB_PORT_STAT_C_ENABLE) {
411 			USB_HUB_PRINTF("port %d enable change, status %x\n",
412 					i + 1, portstatus);
413 			usb_clear_port_feature(dev, i + 1,
414 						USB_PORT_FEAT_C_ENABLE);
415 
416 			/* EM interference sometimes causes bad shielded USB
417 			 * devices to be shutdown by the hub, this hack enables
418 			 * them again. Works at least with mouse driver */
419 			if (!(portstatus & USB_PORT_STAT_ENABLE) &&
420 			     (portstatus & USB_PORT_STAT_CONNECTION) &&
421 			     ((dev->children[i]))) {
422 				USB_HUB_PRINTF("already running port %i "  \
423 						"disabled by hub (EMI?), " \
424 						"re-enabling...\n", i + 1);
425 					usb_hub_port_connect_change(dev, i);
426 			}
427 		}
428 		if (portstatus & USB_PORT_STAT_SUSPEND) {
429 			USB_HUB_PRINTF("port %d suspend change\n", i + 1);
430 			usb_clear_port_feature(dev, i + 1,
431 						USB_PORT_FEAT_SUSPEND);
432 		}
433 
434 		if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
435 			USB_HUB_PRINTF("port %d over-current change\n", i + 1);
436 			usb_clear_port_feature(dev, i + 1,
437 						USB_PORT_FEAT_C_OVER_CURRENT);
438 			usb_hub_power_on(hub);
439 		}
440 
441 		if (portchange & USB_PORT_STAT_C_RESET) {
442 			USB_HUB_PRINTF("port %d reset change\n", i + 1);
443 			usb_clear_port_feature(dev, i + 1,
444 						USB_PORT_FEAT_C_RESET);
445 		}
446 	} /* end for i all ports */
447 
448 	return 0;
449 }
450 
451 int usb_hub_probe(struct usb_device *dev, int ifnum)
452 {
453 	struct usb_interface *iface;
454 	struct usb_endpoint_descriptor *ep;
455 	int ret;
456 
457 	iface = &dev->config.if_desc[ifnum];
458 	/* Is it a hub? */
459 	if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
460 		return 0;
461 	/* Some hubs have a subclass of 1, which AFAICT according to the */
462 	/*  specs is not defined, but it works */
463 	if ((iface->desc.bInterfaceSubClass != 0) &&
464 	    (iface->desc.bInterfaceSubClass != 1))
465 		return 0;
466 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
467 	if (iface->desc.bNumEndpoints != 1)
468 		return 0;
469 	ep = &iface->ep_desc[0];
470 	/* Output endpoint? Curiousier and curiousier.. */
471 	if (!(ep->bEndpointAddress & USB_DIR_IN))
472 		return 0;
473 	/* If it's not an interrupt endpoint, we'd better punt! */
474 	if ((ep->bmAttributes & 3) != 3)
475 		return 0;
476 	/* We found a hub */
477 	USB_HUB_PRINTF("USB hub found\n");
478 	ret = usb_hub_configure(dev);
479 	return ret;
480 }
481