usbd_status uhub_explore(usbd_device_handle dev) { usb_hub_descriptor_t *hd = &dev->hub->hubdesc; device_t self = dev->hub->hubdev; struct uhub_softc *sc = device_get_softc(self); struct usbd_port *up; usbd_status err; int speed; int port; int change, status; DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); if (!sc->sc_running) return (USBD_NOT_STARTED); /* Ignore hubs that are too deep. */ if (dev->depth > USB_HUB_MAX_DEPTH) return (USBD_TOO_DEEP); for(port = 1; port <= hd->bNbrPorts; port++) { up = &dev->hub->ports[port-1]; err = usbd_get_port_status(dev, port, &up->status); if (err) { DPRINTF(("uhub_explore: get port status failed, " "error=%s\n", usbd_errstr(err))); continue; } status = UGETW(up->status.wPortStatus); change = UGETW(up->status.wPortChange); DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", device_get_nameunit(self), port, status, change)); if (change & UPS_C_PORT_ENABLED) { DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change)); usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); if (change & UPS_C_CONNECT_STATUS) { /* Ignore the port error if the device vanished. */ } else if (status & UPS_PORT_ENABLED) { device_printf(self, "illegal enable change, port %d\n", port); } else { /* Port error condition. */ if (up->restartcnt) /* no message first time */ device_printf(self, "port error, restarting " "port %d\n", port); if (up->restartcnt++ < USBD_RESTART_MAX) goto disco; else device_printf(self, "port error, giving up " "port %d\n", port); } } if (!(change & UPS_C_CONNECT_STATUS)) { DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" "STATUS\n", port)); /* No status change, just do recursive explore. */ if (up->device != NULL && up->device->hub != NULL) up->device->hub->explore(up->device); #if 0 && defined(DIAGNOSTIC) if (up->device == NULL && (status & UPS_CURRENT_CONNECT_STATUS)) device_printf(self, "connected, no device\n"); #endif continue; } /* We have a connect status change, handle it. */ DPRINTF(("uhub_explore: status change hub=%d port=%d\n", dev->address, port)); usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ /* * If there is already a device on the port the change status * must mean that is has disconnected. Looking at the * current connect status is not enough to figure this out * since a new unit may have been connected before we handle * the disconnect. */ disco: if (up->device != NULL) { /* Disconnected */ DPRINTF(("uhub_explore: device addr=%d disappeared " "on port %d\n", up->device->address, port)); usb_disconnect_port(up, self); usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); } if (!(status & UPS_CURRENT_CONNECT_STATUS)) { /* Nothing connected, just ignore it. */ DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" "_STATUS\n", port)); continue; } /* Connected */ if (!(status & UPS_PORT_POWER)) device_printf(self, "strange, connected port %d has no power\n", port); /* Wait for maximum device power up time. */ usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); /* Reset port, which implies enabling it. */ if (usbd_reset_port(dev, port, &up->status)) { device_printf(self, "port %d reset failed\n", port); continue; } /* Get port status again, it might have changed during reset */ err = usbd_get_port_status(dev, port, &up->status); if (err) { DPRINTF(("uhub_explore: get port status failed, " "error=%s\n", usbd_errstr(err))); continue; } status = UGETW(up->status.wPortStatus); change = UGETW(up->status.wPortChange); if (!(status & UPS_CURRENT_CONNECT_STATUS)) { /* Nothing connected, just ignore it. */ #ifdef DIAGNOSTIC device_printf(self, "port %d, device disappeared after reset\n", port); #endif continue; } #if 0 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) { device_printf(self, "port %d, transaction translation not " "implemented, low/full speed device ignored\n", port); continue; } #endif /* Figure out device speed */ if (status & UPS_HIGH_SPEED) speed = USB_SPEED_HIGH; else if (status & UPS_LOW_SPEED) speed = USB_SPEED_LOW; else speed = USB_SPEED_FULL; /* Get device info and set its address. */ err = usbd_new_device(self, dev->bus, dev->depth + 1, speed, port, up); /* XXX retry a few times? */ if (err) { DPRINTFN(-1,("uhub_explore: usb_new_device failed, " "error=%s\n", usbd_errstr(err))); /* Avoid addressing problems by disabling. */ /* usbd_reset_port(dev, port, &up->status); */ /* * The unit refused to accept a new address, or had * some other serious problem. Since we cannot leave * at 0 we have to disable the port instead. */ device_printf(self, "device problem (%s), disabling port %d\n", usbd_errstr(err), port); usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); } else { /* The port set up succeeded, reset error count. */ up->restartcnt = 0; if (up->device->hub) up->device->hub->explore(up->device); } } return (USBD_NORMAL_COMPLETION); }
/* * Called when a new device has been put in the powered state, * but not yet in the addressed state. * Get initial descriptor, set the address, get full descriptor, * and attach a driver. */ usbd_status usbd_new_device(device_t parent, struct usbd_bus* bus, int depth, int speed, int port, struct usbd_port *up) { USBHIST_FUNC(); USBHIST_CALLED(usbdebug); struct usbd_device *dev, *adev; struct usbd_device *hub; usb_device_descriptor_t *dd; usb_port_status_t ps; usbd_status err; int addr; int i; int p; DPRINTF("bus=%p port=%d depth=%d speed=%d", bus, port, depth, speed); if (bus->ub_methods->ubm_newdev != NULL) return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed, port, up); addr = usbd_getnewaddr(bus); if (addr < 0) { printf("%s: No free USB addresses, new device ignored.\n", device_xname(bus->ub_usbctl)); return USBD_NO_ADDR; } dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); if (dev == NULL) return USBD_NOMEM; dev->ud_bus = bus; /* Set up default endpoint handle. */ dev->ud_ep0.ue_edesc = &dev->ud_ep0desc; /* Set up default endpoint descriptor. */ dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT; dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT; dev->ud_ep0desc.bmAttributes = UE_CONTROL; /* * temporary, will be fixed after first descriptor fetch * (which uses 64 bytes so it shouldn't be less), * highspeed devices must support 64 byte packets anyway */ if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL) USETW(dev->ud_ep0desc.wMaxPacketSize, 64); else USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET); dev->ud_ep0desc.bInterval = 0; /* doesn't matter, just don't leave it uninitialized */ dev->ud_ep0.ue_toggle = 0; dev->ud_quirks = &usbd_no_quirk; dev->ud_addr = USB_START_ADDR; dev->ud_ddesc.bMaxPacketSize = 0; dev->ud_depth = depth; dev->ud_powersrc = up; dev->ud_myhub = up->up_parent; up->up_dev = dev; /* Locate port on upstream high speed hub */ for (adev = dev, hub = up->up_parent; hub != NULL && hub->ud_speed != USB_SPEED_HIGH; adev = hub, hub = hub->ud_myhub) ; if (hub) { for (p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) { if (hub->ud_hub->uh_ports[p].up_dev == adev) { dev->ud_myhsport = &hub->ud_hub->uh_ports[p]; goto found; } } panic("usbd_new_device: cannot find HS port"); found: DPRINTFN(1, "high speed port %d", p, 0, 0, 0); } else { dev->ud_myhsport = NULL; } dev->ud_speed = speed; dev->ud_langid = USBD_NOLANG; dev->ud_cookie.cookie = ++usb_cookie_no; /* Establish the default pipe. */ err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, &dev->ud_pipe0, USBD_MPSAFE); if (err) { usbd_remove_device(dev, up); return err; } dd = &dev->ud_ddesc; /* Try a few times in case the device is slow (i.e. outside specs.) */ for (i = 0; i < 10; i++) { /* Get the first 8 bytes of the device descriptor. */ err = usbd_get_initial_ddesc(dev, dd); if (!err) break; usbd_delay_ms(dev, 200); if ((i & 3) == 3) usbd_reset_port(up->up_parent, port, &ps); } if (err) { DPRINTF("addr=%d, getting first desc failed: %d", addr, err, 0, 0); usbd_remove_device(dev, up); return err; } /* Windows resets the port here, do likewise */ if (up->up_parent) usbd_reset_port(up->up_parent, port, &ps); if (speed == USB_SPEED_HIGH) { /* Max packet size must be 64 (sec 5.5.3). */ if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { #ifdef DIAGNOSTIC printf("usbd_new_device: addr=%d bad max packet " "size=%d. adjusting to %d.\n", addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET); #endif dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; } } DPRINTF("adding unit addr=%d, rev=%02x, class=%d, subclass=%d", addr, UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass); DPRINTF("protocol=%d, maxpacket=%d, len=%d, speed=%d", dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed); if (dd->bDescriptorType != UDESC_DEVICE) { /* Illegal device descriptor */ DPRINTF("illegal descriptor %d", dd->bDescriptorType, 0, 0, 0); usbd_remove_device(dev, up); return USBD_INVAL; } if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { DPRINTF("bad length %d", dd->bLength, 0, 0, 0); usbd_remove_device(dev, up); return USBD_INVAL; } USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize); /* Re-establish the default pipe with the new MPS. */ usbd_kill_pipe(dev->ud_pipe0); err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, &dev->ud_pipe0, USBD_MPSAFE); if (err) { DPRINTF("setup default pipe failed err %d", err, 0, 0, 0); usbd_remove_device(dev, up); return err; } /* Set the address */ DPRINTFN(5, "setting device address=%d", addr, 0, 0, 0); err = usbd_set_address(dev, addr); if (err) { DPRINTF("set address %d failed, err = %d", addr, err, 0, 0); err = USBD_SET_ADDR_FAILED; usbd_remove_device(dev, up); return err; } /* Allow device time to set new address */ usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); dev->ud_addr = addr; /* new device address now */ bus->ub_devices[addr] = dev; /* Re-establish the default pipe with the new address. */ usbd_kill_pipe(dev->ud_pipe0); err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, &dev->ud_pipe0, USBD_MPSAFE); if (err) { DPRINTF("setup default pipe failed, err = %d", err, 0, 0, 0); usbd_remove_device(dev, up); return err; } err = usbd_reload_device_desc(dev); if (err) { DPRINTF("addr=%d, getting full desc failed, err = %d", addr, err, 0, 0); usbd_remove_device(dev, up); return err; } /* Assume 100mA bus powered for now. Changed when configured. */ dev->ud_power = USB_MIN_POWER; dev->ud_selfpowered = 0; DPRINTF("new dev (addr %d), dev=%p, parent=%p", addr, dev, parent, 0); usbd_get_device_strings(dev); usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); if (port == 0) { /* root hub */ KASSERT(addr == 1); usbd_attach_roothub(parent, dev); return USBD_NORMAL_COMPLETION; } err = usbd_probe_and_attach(parent, dev, port, addr); if (err) { usbd_remove_device(dev, up); return err; } return USBD_NORMAL_COMPLETION; }
int uhub_port_connect(struct uhub_softc *sc, int port, int status, int change) { struct usbd_port *up = &sc->sc_hub->hub->ports[port-1]; int speed; /* We have a connect status change, handle it. */ usbd_clear_port_feature(sc->sc_hub, port, UHF_C_PORT_CONNECTION); /* * If there is already a device on the port the change status * must mean that is has disconnected. Looking at the * current connect status is not enough to figure this out * since a new unit may have been connected before we handle * the disconnect. */ if (up->device != NULL) { /* Disconnected */ usbd_detach(up->device, &sc->sc_dev); up->device = NULL; } /* Nothing connected, just ignore it. */ if ((status & UPS_CURRENT_CONNECT_STATUS) == 0) return (0); /* Connected */ if ((status & (UPS_PORT_POWER|UPS_PORT_POWER_SS)) == 0) { printf("%s: connected port %d has no power\n", DEVNAME(sc), port); return (-1); } /* Wait for maximum device power up time. */ usbd_delay_ms(sc->sc_hub, USB_PORT_POWERUP_DELAY); /* Reset port, which implies enabling it. */ if (usbd_reset_port(sc->sc_hub, port)) { printf("%s: port %d reset failed\n", DEVNAME(sc), port); return (-1); } /* Get port status again, it might have changed during reset */ if (usbd_get_port_status(sc->sc_hub, port, &up->status)) return (-1); status = UGETW(up->status.wPortStatus); change = UGETW(up->status.wPortChange); DPRINTF("%s: port %d status=0x%04x change=0x%04x\n", DEVNAME(sc), port, status, change); /* Nothing connected, just ignore it. */ if ((status & UPS_CURRENT_CONNECT_STATUS) == 0) { DPRINTF("%s: port %d, device disappeared after reset\n", DEVNAME(sc), port); return (-1); } /* * Figure out device speed. This is a bit tricky because * UPS_PORT_POWER_SS and UPS_LOW_SPEED share the same bit. */ if ((status & UPS_PORT_POWER) == 0) status &= ~UPS_PORT_POWER_SS; if (status & UPS_HIGH_SPEED) speed = USB_SPEED_HIGH; else if (status & UPS_LOW_SPEED) speed = USB_SPEED_LOW; else { /* * If there is no power bit set, it is certainly * a Super Speed device, so use the speed of its * parent hub. */ if (status & UPS_PORT_POWER) speed = USB_SPEED_FULL; else speed = sc->sc_hub->speed; } /* * Reduce the speed, otherwise we won't setup the proper * transfer methods. */ if (speed > sc->sc_hub->speed) speed = sc->sc_hub->speed; /* Get device info and set its address. */ if (usbd_new_device(&sc->sc_dev, sc->sc_hub->bus, sc->sc_hub->depth + 1, speed, port, up)) { /* * The unit refused to accept a new address, or had * some other serious problem. Since we cannot leave * at 0 we have to disable the port instead. */ printf("%s: device problem, disabling port %d\n", DEVNAME(sc), port); usbd_clear_port_feature(sc->sc_hub, port, UHF_PORT_ENABLE); return (-1); } return (0); }
/* * Called when a new device has been put in the powered state, * but not yet in the addressed state. * Get initial descriptor, set the address, get full descriptor, * and attach a driver. */ usbd_status usbd_new_device(device_t parent, usbd_bus_handle bus, int depth, int speed, int port, struct usbd_port *up) { usbd_device_handle dev, adev; struct usbd_device *hub; usb_device_descriptor_t *dd; usb_port_status_t ps; usbd_status err; int addr; int i; int p; DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n", bus, port, depth, speed)); addr = usbd_getnewaddr(bus); if (addr < 0) { kprintf("%s: No free USB addresses, new device ignored.\n", device_get_nameunit(bus->bdev)); return (USBD_NO_ADDR); } dev = kmalloc(sizeof *dev, M_USB, M_INTWAIT | M_ZERO); dev->bus = bus; /* Set up default endpoint handle. */ dev->def_ep.edesc = &dev->def_ep_desc; /* Set up default endpoint descriptor. */ dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; dev->def_ep_desc.bmAttributes = UE_CONTROL; USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); dev->def_ep_desc.bInterval = 0; dev->quirks = &usbd_no_quirk; dev->address = USB_START_ADDR; dev->ddesc.bMaxPacketSize = 0; dev->depth = depth; dev->powersrc = up; dev->myhub = up->parent; up->device = dev; /* Locate port on upstream high speed hub */ for (adev = dev, hub = up->parent; hub != NULL && hub->speed != USB_SPEED_HIGH; adev = hub, hub = hub->myhub) ; if (hub) { for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) { if (hub->hub->ports[p].device == adev) { dev->myhsport = &hub->hub->ports[p]; goto found; } } panic("usbd_new_device: cannot find HS port\n"); found: DPRINTFN(1,("usbd_new_device: high speed port %d\n", p)); } else { dev->myhsport = NULL; } dev->speed = speed; dev->langid = USBD_NOLANG; dev->cookie.cookie = ++usb_cookie_no; /* Establish the default pipe. */ err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, &dev->default_pipe); if (err) { usbd_remove_device(dev, up); return (err); } /* Set the address. Do this early; some devices need that. */ /* Try a few times in case the device is slow (i.e. outside specs.) */ DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr)); for (i = 0; i < 5; i++) { err = usbd_set_address(dev, addr); if (!err) break; usbd_delay_ms(dev, 200); if ((i & 3) == 3) { DPRINTFN(-1,("usb_new_device: set address %d " "failed - trying a port reset\n", addr)); usbd_reset_port(up->parent, port, &ps); } } if (err) { DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr)); err = USBD_SET_ADDR_FAILED; usbd_remove_device(dev, up); return (err); } /* Allow device time to set new address */ usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); /* Kill the pipe */ usbd_kill_pipe(dev->default_pipe); dev->default_pipe = NULL; dev->address = addr; /* New device address now */ bus->devices[addr] = dev; /* Re-establish the default pipe. */ err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, &dev->default_pipe); if (err) { usbd_remove_device(dev, up); return (err); } usbd_delay_ms(dev, 200); dd = &dev->ddesc; /* Get the first 8 bytes of the device descriptor. */ err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd); if (err) { DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc " "failed\n", addr)); usbd_remove_device(dev, up); return (err); } if (speed == USB_SPEED_HIGH) { /* Max packet size must be 64 (sec 5.5.3). */ if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { #ifdef DIAGNOSTIC kprintf("usbd_new_device: addr=%d bad max packet size\n", addr); #endif dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; } } DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, " "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->speed)); if (dd->bDescriptorType != UDESC_DEVICE) { /* Illegal device descriptor */ DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n", dd->bDescriptorType)); usbd_remove_device(dev, up); return (USBD_INVAL); } if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength)); usbd_remove_device(dev, up); return (USBD_INVAL); } USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize); err = usbd_reload_device_desc(dev); if (err) { DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc " "failed\n", addr)); usbd_remove_device(dev, up); return (err); } /* Assume 100mA bus powered for now. Changed when configured. */ dev->power = USB_MIN_POWER; dev->self_powered = 0; DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n", addr, dev, parent)); err = usbd_probe_and_attach(parent, dev, port, addr); if (err) { usbd_remove_device(dev, up); return (err); } usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); return (USBD_NORMAL_COMPLETION); }