978
965
USB_PRINTF("scan end\n");
982
/****************************************************************************
984
* Probes device for being a hub and configurate it
987
static struct usb_hub_device hub_dev[USB_MAX_HUB];
988
static int usb_hub_index;
991
int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
993
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
994
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
995
USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
998
int usb_clear_hub_feature(struct usb_device *dev, int feature)
1000
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1001
USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature,
1002
0, NULL, 0, USB_CNTL_TIMEOUT);
1005
int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
1007
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1008
USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
1009
port, NULL, 0, USB_CNTL_TIMEOUT);
1012
int usb_set_port_feature(struct usb_device *dev, int port, int feature)
1014
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1015
USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
1016
port, NULL, 0, USB_CNTL_TIMEOUT);
1019
int usb_get_hub_status(struct usb_device *dev, void *data)
1021
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1022
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
1023
data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
1026
int usb_get_port_status(struct usb_device *dev, int port, void *data)
1028
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1029
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
1030
data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
1034
static void usb_hub_power_on(struct usb_hub_device *hub)
1037
struct usb_device *dev;
1039
dev = hub->pusb_dev;
1040
/* Enable power to the ports */
1041
USB_HUB_PRINTF("enabling power on all ports\n");
1042
for (i = 0; i < dev->maxchild; i++) {
1043
usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
1044
USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
1045
wait_ms(hub->desc.bPwrOn2PwrGood * 2);
1049
void usb_hub_reset(void)
1054
struct usb_hub_device *usb_hub_allocate(void)
1056
if (usb_hub_index < USB_MAX_HUB)
1057
return &hub_dev[usb_hub_index++];
1059
printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
1065
static inline char *portspeed(int portstatus)
1067
if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1069
else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1075
static int hub_port_reset(struct usb_device *dev, int port,
1076
unsigned short *portstat)
1079
struct usb_port_status portsts;
1080
unsigned short portstatus, portchange;
1082
USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
1083
for (tries = 0; tries < MAX_TRIES; tries++) {
1085
usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
1088
if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1089
USB_HUB_PRINTF("get_port_status failed status %lX\n",
1093
portstatus = le16_to_cpu(portsts.wPortStatus);
1094
portchange = le16_to_cpu(portsts.wPortChange);
1096
USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
1097
portstatus, portchange,
1098
portspeed(portstatus));
1100
USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
1101
" USB_PORT_STAT_ENABLE %d\n",
1102
(portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
1103
(portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
1104
(portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
1106
if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
1107
!(portstatus & USB_PORT_STAT_CONNECTION))
1110
if (portstatus & USB_PORT_STAT_ENABLE)
1116
if (tries == MAX_TRIES) {
1117
USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
1118
"disabling port.\n", port + 1, MAX_TRIES);
1119
USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
1123
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
1124
*portstat = portstatus;
1129
void usb_hub_port_connect_change(struct usb_device *dev, int port)
1131
struct usb_device *usb;
1132
struct usb_port_status portsts;
1133
unsigned short portstatus;
1136
if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1137
USB_HUB_PRINTF("get_port_status failed\n");
1141
portstatus = le16_to_cpu(portsts.wPortStatus);
1142
USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
1144
le16_to_cpu(portsts.wPortChange),
1145
portspeed(portstatus));
1147
/* Clear the connection change status */
1148
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
1150
/* Disconnect any existing devices under this port */
1151
if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
1152
(!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
1153
USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
1154
/* Return now if nothing is connected */
1155
if (!(portstatus & USB_PORT_STAT_CONNECTION))
1160
/* Reset the port */
1161
if (hub_port_reset(dev, port, &portstatus) < 0) {
1162
printf("cannot reset port %i!?\n", port + 1);
1168
/* Allocate a new device struct for it */
1169
usb = usb_alloc_new_device();
1171
if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1172
usb->speed = USB_SPEED_HIGH;
1173
else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1174
usb->speed = USB_SPEED_LOW;
1176
usb->speed = USB_SPEED_FULL;
1178
dev->children[port] = usb;
1180
usb->portnr = port + 1;
1181
/* Run it through the hoops (find a driver, etc) */
1182
if (usb_new_device(usb)) {
1183
/* Woops, disable the port */
1184
USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
1185
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
1190
int usb_hub_configure(struct usb_device *dev)
1193
unsigned char buffer[USB_BUFSIZ], *bitmap;
1194
struct usb_hub_descriptor *descriptor;
1195
struct usb_hub_device *hub;
1196
#ifdef USB_HUB_DEBUG
1197
struct usb_hub_status *hubsts;
1200
/* "allocate" Hub device */
1201
hub = usb_hub_allocate();
1204
hub->pusb_dev = dev;
1205
/* Get the the hub descriptor */
1206
if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
1207
USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1208
"descriptor, giving up %lX\n", dev->status);
1211
descriptor = (struct usb_hub_descriptor *)buffer;
1213
/* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
1214
i = descriptor->bLength;
1215
if (i > USB_BUFSIZ) {
1216
USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1217
"descriptor - too long: %d\n",
1218
descriptor->bLength);
1222
if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
1223
USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1224
"descriptor 2nd giving up %lX\n", dev->status);
1227
memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
1228
/* adjust 16bit values */
1229
hub->desc.wHubCharacteristics =
1230
le16_to_cpu(descriptor->wHubCharacteristics);
1231
/* set the bitmap */
1232
bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
1233
/* devices not removable by default */
1234
memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
1235
bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
1236
memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
1238
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1239
hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
1241
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1242
hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
1244
dev->maxchild = descriptor->bNbrPorts;
1245
USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
1247
switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
1249
USB_HUB_PRINTF("ganged power switching\n");
1252
USB_HUB_PRINTF("individual port power switching\n");
1256
USB_HUB_PRINTF("unknown reserved power switching mode\n");
1260
if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
1261
USB_HUB_PRINTF("part of a compound device\n");
1263
USB_HUB_PRINTF("standalone hub\n");
1265
switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
1267
USB_HUB_PRINTF("global over-current protection\n");
1270
USB_HUB_PRINTF("individual port over-current protection\n");
1274
USB_HUB_PRINTF("no over-current protection\n");
1278
USB_HUB_PRINTF("power on to power good time: %dms\n",
1279
descriptor->bPwrOn2PwrGood * 2);
1280
USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
1281
descriptor->bHubContrCurrent);
1283
for (i = 0; i < dev->maxchild; i++)
1284
USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
1285
hub->desc.DeviceRemovable[(i + 1) / 8] & \
1286
(1 << ((i + 1) % 8)) ? " not" : "");
1288
if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
1289
USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
1290
"too long: %d\n", descriptor->bLength);
1294
if (usb_get_hub_status(dev, buffer) < 0) {
1295
USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
1300
#ifdef USB_HUB_DEBUG
1301
hubsts = (struct usb_hub_status *)buffer;
1303
USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
1304
le16_to_cpu(hubsts->wHubStatus),
1305
le16_to_cpu(hubsts->wHubChange));
1306
USB_HUB_PRINTF("local power source is %s\n",
1307
(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
1308
"lost (inactive)" : "good");
1309
USB_HUB_PRINTF("%sover-current condition exists\n",
1310
(le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
1312
usb_hub_power_on(hub);
1314
for (i = 0; i < dev->maxchild; i++) {
1315
struct usb_port_status portsts;
1316
unsigned short portstatus, portchange;
1318
if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
1319
USB_HUB_PRINTF("get_port_status failed\n");
1323
portstatus = le16_to_cpu(portsts.wPortStatus);
1324
portchange = le16_to_cpu(portsts.wPortChange);
1325
USB_HUB_PRINTF("Port %d Status %X Change %X\n",
1326
i + 1, portstatus, portchange);
1328
if (portchange & USB_PORT_STAT_C_CONNECTION) {
1329
USB_HUB_PRINTF("port %d connection change\n", i + 1);
1330
usb_hub_port_connect_change(dev, i);
1332
if (portchange & USB_PORT_STAT_C_ENABLE) {
1333
USB_HUB_PRINTF("port %d enable change, status %x\n",
1335
usb_clear_port_feature(dev, i + 1,
1336
USB_PORT_FEAT_C_ENABLE);
1338
/* EM interference sometimes causes bad shielded USB
1339
* devices to be shutdown by the hub, this hack enables
1340
* them again. Works at least with mouse driver */
1341
if (!(portstatus & USB_PORT_STAT_ENABLE) &&
1342
(portstatus & USB_PORT_STAT_CONNECTION) &&
1343
((dev->children[i]))) {
1344
USB_HUB_PRINTF("already running port %i " \
1345
"disabled by hub (EMI?), " \
1346
"re-enabling...\n", i + 1);
1347
usb_hub_port_connect_change(dev, i);
1350
if (portstatus & USB_PORT_STAT_SUSPEND) {
1351
USB_HUB_PRINTF("port %d suspend change\n", i + 1);
1352
usb_clear_port_feature(dev, i + 1,
1353
USB_PORT_FEAT_SUSPEND);
1356
if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
1357
USB_HUB_PRINTF("port %d over-current change\n", i + 1);
1358
usb_clear_port_feature(dev, i + 1,
1359
USB_PORT_FEAT_C_OVER_CURRENT);
1360
usb_hub_power_on(hub);
1363
if (portchange & USB_PORT_STAT_C_RESET) {
1364
USB_HUB_PRINTF("port %d reset change\n", i + 1);
1365
usb_clear_port_feature(dev, i + 1,
1366
USB_PORT_FEAT_C_RESET);
1368
} /* end for i all ports */
1373
int usb_hub_probe(struct usb_device *dev, int ifnum)
1375
struct usb_interface *iface;
1376
struct usb_endpoint_descriptor *ep;
1379
iface = &dev->config.if_desc[ifnum];
1381
if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
1383
/* Some hubs have a subclass of 1, which AFAICT according to the */
1384
/* specs is not defined, but it works */
1385
if ((iface->desc.bInterfaceSubClass != 0) &&
1386
(iface->desc.bInterfaceSubClass != 1))
1388
/* Multiple endpoints? What kind of mutant ninja-hub is this? */
1389
if (iface->desc.bNumEndpoints != 1)
1391
ep = &iface->ep_desc[0];
1392
/* Output endpoint? Curiousier and curiousier.. */
1393
if (!(ep->bEndpointAddress & USB_DIR_IN))
1395
/* If it's not an interrupt endpoint, we'd better punt! */
1396
if ((ep->bmAttributes & 3) != 3)
1398
/* We found a hub */
1399
USB_HUB_PRINTF("USB hub found\n");
1400
ret = usb_hub_configure(dev);