11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * USB hub driver. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 51da177e4SLinus Torvalds * (C) Copyright 1999 Johannes Erdfelt 61da177e4SLinus Torvalds * (C) Copyright 1999 Gregory P. Smith 71da177e4SLinus Torvalds * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/moduleparam.h> 151da177e4SLinus Torvalds #include <linux/completion.h> 161da177e4SLinus Torvalds #include <linux/sched.h> 171da177e4SLinus Torvalds #include <linux/list.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/ioctl.h> 201da177e4SLinus Torvalds #include <linux/usb.h> 211da177e4SLinus Torvalds #include <linux/usbdevice_fs.h> 229c8d6178Sakpm@osdl.org #include <linux/kthread.h> 234186ecf8SArjan van de Ven #include <linux/mutex.h> 247dfb7103SNigel Cunningham #include <linux/freezer.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <asm/uaccess.h> 271da177e4SLinus Torvalds #include <asm/byteorder.h> 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include "usb.h" 301da177e4SLinus Torvalds #include "hcd.h" 311da177e4SLinus Torvalds #include "hub.h" 321da177e4SLinus Torvalds 33f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */ 34f2a383e4SGreg Kroah-Hartman #ifdef DEBUG 35f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES 36f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES 37f2a383e4SGreg Kroah-Hartman #endif 38f2a383e4SGreg Kroah-Hartman #endif 39f2a383e4SGreg Kroah-Hartman 401bb5f66bSAlan Stern struct usb_hub { 411bb5f66bSAlan Stern struct device *intfdev; /* the "interface" device */ 421bb5f66bSAlan Stern struct usb_device *hdev; 43e8054854SAlan Stern struct kref kref; 441bb5f66bSAlan Stern struct urb *urb; /* for interrupt polling pipe */ 451bb5f66bSAlan Stern 461bb5f66bSAlan Stern /* buffer for urb ... with extra space in case of babble */ 471bb5f66bSAlan Stern char (*buffer)[8]; 481bb5f66bSAlan Stern dma_addr_t buffer_dma; /* DMA address for buffer */ 491bb5f66bSAlan Stern union { 501bb5f66bSAlan Stern struct usb_hub_status hub; 511bb5f66bSAlan Stern struct usb_port_status port; 521bb5f66bSAlan Stern } *status; /* buffer for status reports */ 53db90e7a1SAlan Stern struct mutex status_mutex; /* for the status buffer */ 541bb5f66bSAlan Stern 551bb5f66bSAlan Stern int error; /* last reported error */ 561bb5f66bSAlan Stern int nerrors; /* track consecutive errors */ 571bb5f66bSAlan Stern 581bb5f66bSAlan Stern struct list_head event_list; /* hubs w/data or errs ready */ 591bb5f66bSAlan Stern unsigned long event_bits[1]; /* status change bitmask */ 601bb5f66bSAlan Stern unsigned long change_bits[1]; /* ports with logical connect 611bb5f66bSAlan Stern status change */ 621bb5f66bSAlan Stern unsigned long busy_bits[1]; /* ports being reset or 631bb5f66bSAlan Stern resumed */ 641bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 651bb5f66bSAlan Stern #error event_bits[] is too short! 661bb5f66bSAlan Stern #endif 671bb5f66bSAlan Stern 681bb5f66bSAlan Stern struct usb_hub_descriptor *descriptor; /* class descriptor */ 691bb5f66bSAlan Stern struct usb_tt tt; /* Transaction Translator */ 701bb5f66bSAlan Stern 711bb5f66bSAlan Stern unsigned mA_per_port; /* current for each child */ 721bb5f66bSAlan Stern 731bb5f66bSAlan Stern unsigned limited_power:1; 741bb5f66bSAlan Stern unsigned quiescing:1; 75e8054854SAlan Stern unsigned disconnected:1; 761bb5f66bSAlan Stern 771bb5f66bSAlan Stern unsigned has_indicators:1; 781bb5f66bSAlan Stern u8 indicator[USB_MAXCHILDREN]; 796d5aefb8SDavid Howells struct delayed_work leds; 808520f380SAlan Stern struct delayed_work init_work; 811bb5f66bSAlan Stern }; 821bb5f66bSAlan Stern 831bb5f66bSAlan Stern 841da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members 859ad3d6ccSAlan Stern * Note: Both are also protected by ->dev.sem, except that ->state can 861da177e4SLinus Torvalds * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 871da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* khubd's worklist and its lock */ 901da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock); 911da177e4SLinus Torvalds static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* Wakes up khubd */ 941da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); 951da177e4SLinus Torvalds 969c8d6178Sakpm@osdl.org static struct task_struct *khubd_task; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */ 991da177e4SLinus Torvalds static int blinkenlights = 0; 1001da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO); 1011da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 104fd7c519dSJaroslav Kysela * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 105fd7c519dSJaroslav Kysela * 10 seconds to send reply for the initial 64-byte descriptor request. 106fd7c519dSJaroslav Kysela */ 107fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */ 108fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 109fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 110b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout, 111b9cef6c3SWu Fengguang "initial 64-byte descriptor request timeout in milliseconds " 112b9cef6c3SWu Fengguang "(default 5000 - 5.0 seconds)"); 113fd7c519dSJaroslav Kysela 114fd7c519dSJaroslav Kysela /* 1151da177e4SLinus Torvalds * As of 2.6.10 we introduce a new USB device initialization scheme which 1161da177e4SLinus Torvalds * closely resembles the way Windows works. Hopefully it will be compatible 1171da177e4SLinus Torvalds * with a wider range of devices than the old scheme. However some previously 1181da177e4SLinus Torvalds * working devices may start giving rise to "device not accepting address" 1191da177e4SLinus Torvalds * errors; if that happens the user can try the old scheme by adjusting the 1201da177e4SLinus Torvalds * following module parameters. 1211da177e4SLinus Torvalds * 1221da177e4SLinus Torvalds * For maximum flexibility there are two boolean parameters to control the 1231da177e4SLinus Torvalds * hub driver's behavior. On the first initialization attempt, if the 1241da177e4SLinus Torvalds * "old_scheme_first" parameter is set then the old scheme will be used, 1251da177e4SLinus Torvalds * otherwise the new scheme is used. If that fails and "use_both_schemes" 1261da177e4SLinus Torvalds * is set, then the driver will make another attempt, using the other scheme. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds static int old_scheme_first = 0; 1291da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 1301da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first, 1311da177e4SLinus Torvalds "start with the old device initialization scheme"); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static int use_both_schemes = 1; 1341da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 1351da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes, 1361da177e4SLinus Torvalds "try the other device initialization scheme if the " 1371da177e4SLinus Torvalds "first one fails"); 1381da177e4SLinus Torvalds 13932fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization. This interferes with 14032fe0198SAlan Stern * port reset on some companion controllers. 14132fe0198SAlan Stern */ 14232fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 14332fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 14432fe0198SAlan Stern 145948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT 1500 146948fea37SAlan Stern #define HUB_DEBOUNCE_STEP 25 147948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE 100 148948fea37SAlan Stern 1491da177e4SLinus Torvalds 150742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev); 151742120c6SMing Lei 1521da177e4SLinus Torvalds static inline char *portspeed(int portstatus) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED)) 1551da177e4SLinus Torvalds return "480 Mb/s"; 1561da177e4SLinus Torvalds else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED)) 1571da177e4SLinus Torvalds return "1.5 Mb/s"; 1581da177e4SLinus Torvalds else 1591da177e4SLinus Torvalds return "12 Mb/s"; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */ 1631da177e4SLinus Torvalds static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds return usb_get_intfdata(hdev->actconfig->interface[0]); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */ 1691da177e4SLinus Torvalds static int get_hub_descriptor(struct usb_device *hdev, void *data, int size) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds int i, ret; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds for (i = 0; i < 3; i++) { 1741da177e4SLinus Torvalds ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 1751da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 1761da177e4SLinus Torvalds USB_DT_HUB << 8, 0, data, size, 1771da177e4SLinus Torvalds USB_CTRL_GET_TIMEOUT); 1781da177e4SLinus Torvalds if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 1791da177e4SLinus Torvalds return ret; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds return -EINVAL; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 1851da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.1 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 1901da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* 1941da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.2 1951da177e4SLinus Torvalds */ 1961da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 1991da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 2001da177e4SLinus Torvalds NULL, 0, 1000); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.13 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 2091da177e4SLinus Torvalds USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 2101da177e4SLinus Torvalds NULL, 0, 1000); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /* 2141da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 2151da177e4SLinus Torvalds * for info about using port indicators 2161da177e4SLinus Torvalds */ 2171da177e4SLinus Torvalds static void set_port_led( 2181da177e4SLinus Torvalds struct usb_hub *hub, 2191da177e4SLinus Torvalds int port1, 2201da177e4SLinus Torvalds int selector 2211da177e4SLinus Torvalds ) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds int status = set_port_feature(hub->hdev, (selector << 8) | port1, 2241da177e4SLinus Torvalds USB_PORT_FEAT_INDICATOR); 2251da177e4SLinus Torvalds if (status < 0) 2261da177e4SLinus Torvalds dev_dbg (hub->intfdev, 2271da177e4SLinus Torvalds "port %d indicator %s status %d\n", 2281da177e4SLinus Torvalds port1, 2291da177e4SLinus Torvalds ({ char *s; switch (selector) { 2301da177e4SLinus Torvalds case HUB_LED_AMBER: s = "amber"; break; 2311da177e4SLinus Torvalds case HUB_LED_GREEN: s = "green"; break; 2321da177e4SLinus Torvalds case HUB_LED_OFF: s = "off"; break; 2331da177e4SLinus Torvalds case HUB_LED_AUTO: s = "auto"; break; 2341da177e4SLinus Torvalds default: s = "??"; break; 2351da177e4SLinus Torvalds }; s; }), 2361da177e4SLinus Torvalds status); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds #define LED_CYCLE_PERIOD ((2*HZ)/3) 2401da177e4SLinus Torvalds 241c4028958SDavid Howells static void led_work (struct work_struct *work) 2421da177e4SLinus Torvalds { 243c4028958SDavid Howells struct usb_hub *hub = 244c4028958SDavid Howells container_of(work, struct usb_hub, leds.work); 2451da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 2461da177e4SLinus Torvalds unsigned i; 2471da177e4SLinus Torvalds unsigned changed = 0; 2481da177e4SLinus Torvalds int cursor = -1; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 2511da177e4SLinus Torvalds return; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds for (i = 0; i < hub->descriptor->bNbrPorts; i++) { 2541da177e4SLinus Torvalds unsigned selector, mode; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 30%-50% duty cycle */ 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds switch (hub->indicator[i]) { 2591da177e4SLinus Torvalds /* cycle marker */ 2601da177e4SLinus Torvalds case INDICATOR_CYCLE: 2611da177e4SLinus Torvalds cursor = i; 2621da177e4SLinus Torvalds selector = HUB_LED_AUTO; 2631da177e4SLinus Torvalds mode = INDICATOR_AUTO; 2641da177e4SLinus Torvalds break; 2651da177e4SLinus Torvalds /* blinking green = sw attention */ 2661da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK: 2671da177e4SLinus Torvalds selector = HUB_LED_GREEN; 2681da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK_OFF; 2691da177e4SLinus Torvalds break; 2701da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK_OFF: 2711da177e4SLinus Torvalds selector = HUB_LED_OFF; 2721da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK; 2731da177e4SLinus Torvalds break; 2741da177e4SLinus Torvalds /* blinking amber = hw attention */ 2751da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK: 2761da177e4SLinus Torvalds selector = HUB_LED_AMBER; 2771da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK_OFF; 2781da177e4SLinus Torvalds break; 2791da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK_OFF: 2801da177e4SLinus Torvalds selector = HUB_LED_OFF; 2811da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK; 2821da177e4SLinus Torvalds break; 2831da177e4SLinus Torvalds /* blink green/amber = reserved */ 2841da177e4SLinus Torvalds case INDICATOR_ALT_BLINK: 2851da177e4SLinus Torvalds selector = HUB_LED_GREEN; 2861da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK_OFF; 2871da177e4SLinus Torvalds break; 2881da177e4SLinus Torvalds case INDICATOR_ALT_BLINK_OFF: 2891da177e4SLinus Torvalds selector = HUB_LED_AMBER; 2901da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK; 2911da177e4SLinus Torvalds break; 2921da177e4SLinus Torvalds default: 2931da177e4SLinus Torvalds continue; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds if (selector != HUB_LED_AUTO) 2961da177e4SLinus Torvalds changed = 1; 2971da177e4SLinus Torvalds set_port_led(hub, i + 1, selector); 2981da177e4SLinus Torvalds hub->indicator[i] = mode; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds if (!changed && blinkenlights) { 3011da177e4SLinus Torvalds cursor++; 3021da177e4SLinus Torvalds cursor %= hub->descriptor->bNbrPorts; 3031da177e4SLinus Torvalds set_port_led(hub, cursor + 1, HUB_LED_GREEN); 3041da177e4SLinus Torvalds hub->indicator[cursor] = INDICATOR_CYCLE; 3051da177e4SLinus Torvalds changed++; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds if (changed) 3081da177e4SLinus Torvalds schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */ 3121da177e4SLinus Torvalds #define USB_STS_TIMEOUT 1000 3131da177e4SLinus Torvalds #define USB_STS_RETRIES 5 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.6 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev, 3191da177e4SLinus Torvalds struct usb_hub_status *data) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 3241da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 3251da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 3261da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds return status; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1, 3351da177e4SLinus Torvalds struct usb_port_status *data) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 3401da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 3411da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, 3421da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds return status; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3473eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1, 3483eb14915SAlan Stern u16 *status, u16 *change) 3493eb14915SAlan Stern { 3503eb14915SAlan Stern int ret; 3513eb14915SAlan Stern 3523eb14915SAlan Stern mutex_lock(&hub->status_mutex); 3533eb14915SAlan Stern ret = get_port_status(hub->hdev, port1, &hub->status->port); 3543eb14915SAlan Stern if (ret < 4) { 3553eb14915SAlan Stern dev_err(hub->intfdev, 3563eb14915SAlan Stern "%s failed (err = %d)\n", __func__, ret); 3573eb14915SAlan Stern if (ret >= 0) 3583eb14915SAlan Stern ret = -EIO; 3593eb14915SAlan Stern } else { 3603eb14915SAlan Stern *status = le16_to_cpu(hub->status->port.wPortStatus); 3613eb14915SAlan Stern *change = le16_to_cpu(hub->status->port.wPortChange); 3623eb14915SAlan Stern ret = 0; 3633eb14915SAlan Stern } 3643eb14915SAlan Stern mutex_unlock(&hub->status_mutex); 3653eb14915SAlan Stern return ret; 3663eb14915SAlan Stern } 3673eb14915SAlan Stern 3681da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds unsigned long flags; 3711da177e4SLinus Torvalds 37240f122f3SAlan Stern /* Suppress autosuspend until khubd runs */ 37340f122f3SAlan Stern to_usb_interface(hub->intfdev)->pm_usage_cnt = 1; 37440f122f3SAlan Stern 3751da177e4SLinus Torvalds spin_lock_irqsave(&hub_event_lock, flags); 3762e2c5eeaSRoel Kluin if (!hub->disconnected && list_empty(&hub->event_list)) { 3771da177e4SLinus Torvalds list_add_tail(&hub->event_list, &hub_event_list); 3781da177e4SLinus Torvalds wake_up(&khubd_wait); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds spin_unlock_irqrestore(&hub_event_lock, flags); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev) 3841da177e4SLinus Torvalds { 385e8054854SAlan Stern /* FIXME: What if hdev isn't bound to the hub driver? */ 3861da177e4SLinus Torvalds kick_khubd(hdev_to_hub(hdev)); 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */ 3917d12e780SDavid Howells static void hub_irq(struct urb *urb) 3921da177e4SLinus Torvalds { 393ec17cf1cSTobias Klauser struct usb_hub *hub = urb->context; 394e015268dSAlan Stern int status = urb->status; 39571d2718fSRoel Kluin unsigned i; 3961da177e4SLinus Torvalds unsigned long bits; 3971da177e4SLinus Torvalds 398e015268dSAlan Stern switch (status) { 3991da177e4SLinus Torvalds case -ENOENT: /* synchronous unlink */ 4001da177e4SLinus Torvalds case -ECONNRESET: /* async unlink */ 4011da177e4SLinus Torvalds case -ESHUTDOWN: /* hardware going away */ 4021da177e4SLinus Torvalds return; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds default: /* presumably an error */ 4051da177e4SLinus Torvalds /* Cause a hub reset after 10 consecutive errors */ 406e015268dSAlan Stern dev_dbg (hub->intfdev, "transfer --> %d\n", status); 4071da177e4SLinus Torvalds if ((++hub->nerrors < 10) || hub->error) 4081da177e4SLinus Torvalds goto resubmit; 409e015268dSAlan Stern hub->error = status; 4101da177e4SLinus Torvalds /* FALL THROUGH */ 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* let khubd handle things */ 4131da177e4SLinus Torvalds case 0: /* we got data: port status changed */ 4141da177e4SLinus Torvalds bits = 0; 4151da177e4SLinus Torvalds for (i = 0; i < urb->actual_length; ++i) 4161da177e4SLinus Torvalds bits |= ((unsigned long) ((*hub->buffer)[i])) 4171da177e4SLinus Torvalds << (i*8); 4181da177e4SLinus Torvalds hub->event_bits[0] = bits; 4191da177e4SLinus Torvalds break; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds hub->nerrors = 0; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* Something happened, let khubd figure it out */ 4251da177e4SLinus Torvalds kick_khubd(hub); 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds resubmit: 4281da177e4SLinus Torvalds if (hub->quiescing) 4291da177e4SLinus Torvalds return; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 4321da177e4SLinus Torvalds && status != -ENODEV && status != -EPERM) 4331da177e4SLinus Torvalds dev_err (hub->intfdev, "resubmit --> %d\n", status); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */ 4371da177e4SLinus Torvalds static inline int 4381da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 4411da177e4SLinus Torvalds HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 4421da177e4SLinus Torvalds tt, NULL, 0, 1000); 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds /* 4461da177e4SLinus Torvalds * enumeration blocks khubd for a long time. we use keventd instead, since 4471da177e4SLinus Torvalds * long blocking there is the exception, not the rule. accordingly, HCDs 4481da177e4SLinus Torvalds * talking to TTs must queue control transfers (not just bulk and iso), so 4491da177e4SLinus Torvalds * both can talk to the same hub concurrently. 4501da177e4SLinus Torvalds */ 451c4028958SDavid Howells static void hub_tt_kevent (struct work_struct *work) 4521da177e4SLinus Torvalds { 453c4028958SDavid Howells struct usb_hub *hub = 454c4028958SDavid Howells container_of(work, struct usb_hub, tt.kevent); 4551da177e4SLinus Torvalds unsigned long flags; 45655e5fdfaSMark Lord int limit = 100; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds spin_lock_irqsave (&hub->tt.lock, flags); 45955e5fdfaSMark Lord while (--limit && !list_empty (&hub->tt.clear_list)) { 460d0f830d3SH Hartley Sweeten struct list_head *next; 4611da177e4SLinus Torvalds struct usb_tt_clear *clear; 4621da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 4631da177e4SLinus Torvalds int status; 4641da177e4SLinus Torvalds 465d0f830d3SH Hartley Sweeten next = hub->tt.clear_list.next; 466d0f830d3SH Hartley Sweeten clear = list_entry (next, struct usb_tt_clear, clear_list); 4671da177e4SLinus Torvalds list_del (&clear->clear_list); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* drop lock so HCD can concurrently report other TT errors */ 4701da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 4711da177e4SLinus Torvalds status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 4721da177e4SLinus Torvalds spin_lock_irqsave (&hub->tt.lock, flags); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (status) 4751da177e4SLinus Torvalds dev_err (&hdev->dev, 4761da177e4SLinus Torvalds "clear tt %d (%04x) error %d\n", 4771da177e4SLinus Torvalds clear->tt, clear->devinfo, status); 4781da177e4SLinus Torvalds kfree(clear); 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /** 4841da177e4SLinus Torvalds * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub 4851da177e4SLinus Torvalds * @udev: the device whose split transaction failed 4861da177e4SLinus Torvalds * @pipe: identifies the endpoint of the failed transaction 4871da177e4SLinus Torvalds * 4881da177e4SLinus Torvalds * High speed HCDs use this to tell the hub driver that some split control or 4891da177e4SLinus Torvalds * bulk transaction failed in a way that requires clearing internal state of 4901da177e4SLinus Torvalds * a transaction translator. This is normally detected (and reported) from 4911da177e4SLinus Torvalds * interrupt context. 4921da177e4SLinus Torvalds * 4931da177e4SLinus Torvalds * It may not be possible for that hub to handle additional full (or low) 4941da177e4SLinus Torvalds * speed transactions until that state is fully cleared out. 4951da177e4SLinus Torvalds */ 4961da177e4SLinus Torvalds void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds struct usb_tt *tt = udev->tt; 4991da177e4SLinus Torvalds unsigned long flags; 5001da177e4SLinus Torvalds struct usb_tt_clear *clear; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* we've got to cope with an arbitrary number of pending TT clears, 5031da177e4SLinus Torvalds * since each TT has "at least two" buffers that can need it (and 5041da177e4SLinus Torvalds * there can be many TTs per hub). even if they're uncommon. 5051da177e4SLinus Torvalds */ 50654e6ecb2SChristoph Lameter if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { 5071da177e4SLinus Torvalds dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 5081da177e4SLinus Torvalds /* FIXME recover somehow ... RESET_TT? */ 5091da177e4SLinus Torvalds return; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /* info that CLEAR_TT_BUFFER needs */ 5131da177e4SLinus Torvalds clear->tt = tt->multi ? udev->ttport : 1; 5141da177e4SLinus Torvalds clear->devinfo = usb_pipeendpoint (pipe); 5151da177e4SLinus Torvalds clear->devinfo |= udev->devnum << 4; 5161da177e4SLinus Torvalds clear->devinfo |= usb_pipecontrol (pipe) 5171da177e4SLinus Torvalds ? (USB_ENDPOINT_XFER_CONTROL << 11) 5181da177e4SLinus Torvalds : (USB_ENDPOINT_XFER_BULK << 11); 5191da177e4SLinus Torvalds if (usb_pipein (pipe)) 5201da177e4SLinus Torvalds clear->devinfo |= 1 << 15; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* tell keventd to clear state for this TT */ 5231da177e4SLinus Torvalds spin_lock_irqsave (&tt->lock, flags); 5241da177e4SLinus Torvalds list_add_tail (&clear->clear_list, &tt->clear_list); 5251da177e4SLinus Torvalds schedule_work (&tt->kevent); 5261da177e4SLinus Torvalds spin_unlock_irqrestore (&tt->lock, flags); 5271da177e4SLinus Torvalds } 528782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_hub_tt_clear_buffer); 5291da177e4SLinus Torvalds 5308520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller 5318520f380SAlan Stern * needs to delay. 5328520f380SAlan Stern */ 5338520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds int port1; 536b789696aSDavid Brownell unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; 5378520f380SAlan Stern unsigned delay; 5384489a571SAlan Stern u16 wHubCharacteristics = 5394489a571SAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 5401da177e4SLinus Torvalds 5414489a571SAlan Stern /* Enable power on each port. Some hubs have reserved values 5424489a571SAlan Stern * of LPSM (> 2) in their descriptors, even though they are 5434489a571SAlan Stern * USB 2.0 hubs. Some hubs do not implement port-power switching 5444489a571SAlan Stern * but only emulate it. In all cases, the ports won't work 5454489a571SAlan Stern * unless we send these messages to the hub. 5464489a571SAlan Stern */ 5474489a571SAlan Stern if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) 5481da177e4SLinus Torvalds dev_dbg(hub->intfdev, "enabling power on all ports\n"); 5494489a571SAlan Stern else 5504489a571SAlan Stern dev_dbg(hub->intfdev, "trying to enable port power on " 5514489a571SAlan Stern "non-switchable hub\n"); 5521da177e4SLinus Torvalds for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 5534489a571SAlan Stern set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 5541da177e4SLinus Torvalds 555b789696aSDavid Brownell /* Wait at least 100 msec for power to become stable */ 5568520f380SAlan Stern delay = max(pgood_delay, (unsigned) 100); 5578520f380SAlan Stern if (do_delay) 5588520f380SAlan Stern msleep(delay); 5598520f380SAlan Stern return delay; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub, 5631da177e4SLinus Torvalds u16 *status, u16 *change) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds int ret; 5661da177e4SLinus Torvalds 567db90e7a1SAlan Stern mutex_lock(&hub->status_mutex); 5681da177e4SLinus Torvalds ret = get_hub_status(hub->hdev, &hub->status->hub); 5691da177e4SLinus Torvalds if (ret < 0) 5701da177e4SLinus Torvalds dev_err (hub->intfdev, 571441b62c1SHarvey Harrison "%s failed (err = %d)\n", __func__, ret); 5721da177e4SLinus Torvalds else { 5731da177e4SLinus Torvalds *status = le16_to_cpu(hub->status->hub.wHubStatus); 5741da177e4SLinus Torvalds *change = le16_to_cpu(hub->status->hub.wHubChange); 5751da177e4SLinus Torvalds ret = 0; 5761da177e4SLinus Torvalds } 577db90e7a1SAlan Stern mutex_unlock(&hub->status_mutex); 5781da177e4SLinus Torvalds return ret; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5818b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 5828b28c752SAlan Stern { 5838b28c752SAlan Stern struct usb_device *hdev = hub->hdev; 5840458d5b4SAlan Stern int ret = 0; 5858b28c752SAlan Stern 5860458d5b4SAlan Stern if (hdev->children[port1-1] && set_state) 5878b28c752SAlan Stern usb_set_device_state(hdev->children[port1-1], 5888b28c752SAlan Stern USB_STATE_NOTATTACHED); 5890458d5b4SAlan Stern if (!hub->error) 5908b28c752SAlan Stern ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); 5918b28c752SAlan Stern if (ret) 5928b28c752SAlan Stern dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 5938b28c752SAlan Stern port1, ret); 5948b28c752SAlan Stern return ret; 5958b28c752SAlan Stern } 5968b28c752SAlan Stern 5970458d5b4SAlan Stern /* 5980458d5b4SAlan Stern * Disable a port and mark a logical connnect-change event, so that some 5990458d5b4SAlan Stern * time later khubd will disconnect() any existing usb_device on the port 6000458d5b4SAlan Stern * and will re-enumerate if there actually is a device attached. 6010458d5b4SAlan Stern */ 6020458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 6037d069b7dSAlan Stern { 6040458d5b4SAlan Stern dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 6050458d5b4SAlan Stern hub_port_disable(hub, port1, 1); 6060458d5b4SAlan Stern 6070458d5b4SAlan Stern /* FIXME let caller ask to power down the port: 6080458d5b4SAlan Stern * - some devices won't enumerate without a VBUS power cycle 6090458d5b4SAlan Stern * - SRP saves power that way 6100458d5b4SAlan Stern * - ... new call, TBD ... 6110458d5b4SAlan Stern * That's easy if this hub can switch power per-port, and 6120458d5b4SAlan Stern * khubd reactivates the port later (timer, SRP, etc). 6130458d5b4SAlan Stern * Powerdown must be optional, because of reset/DFU. 6140458d5b4SAlan Stern */ 6150458d5b4SAlan Stern 6160458d5b4SAlan Stern set_bit(port1, hub->change_bits); 6170458d5b4SAlan Stern kick_khubd(hub); 6180458d5b4SAlan Stern } 6190458d5b4SAlan Stern 6206ee0b270SAlan Stern enum hub_activation_type { 6218520f380SAlan Stern HUB_INIT, HUB_INIT2, HUB_INIT3, 6228520f380SAlan Stern HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 6236ee0b270SAlan Stern }; 6245e6effaeSAlan Stern 6258520f380SAlan Stern static void hub_init_func2(struct work_struct *ws); 6268520f380SAlan Stern static void hub_init_func3(struct work_struct *ws); 6278520f380SAlan Stern 628f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 6295e6effaeSAlan Stern { 6305e6effaeSAlan Stern struct usb_device *hdev = hub->hdev; 6315e6effaeSAlan Stern int port1; 632f2835219SAlan Stern int status; 633948fea37SAlan Stern bool need_debounce_delay = false; 6348520f380SAlan Stern unsigned delay; 6358520f380SAlan Stern 6368520f380SAlan Stern /* Continue a partial initialization */ 6378520f380SAlan Stern if (type == HUB_INIT2) 6388520f380SAlan Stern goto init2; 6398520f380SAlan Stern if (type == HUB_INIT3) 6408520f380SAlan Stern goto init3; 6415e6effaeSAlan Stern 642f2835219SAlan Stern /* After a resume, port power should still be on. 643f2835219SAlan Stern * For any other type of activation, turn it on. 644f2835219SAlan Stern */ 6458520f380SAlan Stern if (type != HUB_RESUME) { 6468520f380SAlan Stern 6478520f380SAlan Stern /* Speed up system boot by using a delayed_work for the 6488520f380SAlan Stern * hub's initial power-up delays. This is pretty awkward 6498520f380SAlan Stern * and the implementation looks like a home-brewed sort of 6508520f380SAlan Stern * setjmp/longjmp, but it saves at least 100 ms for each 6518520f380SAlan Stern * root hub (assuming usbcore is compiled into the kernel 6528520f380SAlan Stern * rather than as a module). It adds up. 6538520f380SAlan Stern * 6548520f380SAlan Stern * This can't be done for HUB_RESUME or HUB_RESET_RESUME 6558520f380SAlan Stern * because for those activation types the ports have to be 6568520f380SAlan Stern * operational when we return. In theory this could be done 6578520f380SAlan Stern * for HUB_POST_RESET, but it's easier not to. 6588520f380SAlan Stern */ 6598520f380SAlan Stern if (type == HUB_INIT) { 6608520f380SAlan Stern delay = hub_power_on(hub, false); 6618520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); 6628520f380SAlan Stern schedule_delayed_work(&hub->init_work, 6638520f380SAlan Stern msecs_to_jiffies(delay)); 66461fbeba1SAlan Stern 66561fbeba1SAlan Stern /* Suppress autosuspend until init is done */ 66661fbeba1SAlan Stern to_usb_interface(hub->intfdev)->pm_usage_cnt = 1; 6678520f380SAlan Stern return; /* Continues at init2: below */ 6688520f380SAlan Stern } else { 6698520f380SAlan Stern hub_power_on(hub, true); 6708520f380SAlan Stern } 6718520f380SAlan Stern } 6728520f380SAlan Stern init2: 673f2835219SAlan Stern 6746ee0b270SAlan Stern /* Check each port and set hub->change_bits to let khubd know 6756ee0b270SAlan Stern * which ports need attention. 6765e6effaeSAlan Stern */ 6775e6effaeSAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 6785e6effaeSAlan Stern struct usb_device *udev = hdev->children[port1-1]; 6795e6effaeSAlan Stern u16 portstatus, portchange; 6805e6effaeSAlan Stern 6816ee0b270SAlan Stern portstatus = portchange = 0; 6826ee0b270SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 6836ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 6846ee0b270SAlan Stern dev_dbg(hub->intfdev, 6856ee0b270SAlan Stern "port %d: status %04x change %04x\n", 6866ee0b270SAlan Stern port1, portstatus, portchange); 6875e6effaeSAlan Stern 6886ee0b270SAlan Stern /* After anything other than HUB_RESUME (i.e., initialization 6896ee0b270SAlan Stern * or any sort of reset), every port should be disabled. 6906ee0b270SAlan Stern * Unconnected ports should likewise be disabled (paranoia), 6916ee0b270SAlan Stern * and so should ports for which we have no usb_device. 6925e6effaeSAlan Stern */ 6936ee0b270SAlan Stern if ((portstatus & USB_PORT_STAT_ENABLE) && ( 6946ee0b270SAlan Stern type != HUB_RESUME || 6956ee0b270SAlan Stern !(portstatus & USB_PORT_STAT_CONNECTION) || 6966ee0b270SAlan Stern !udev || 6976ee0b270SAlan Stern udev->state == USB_STATE_NOTATTACHED)) { 6986ee0b270SAlan Stern clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); 6996ee0b270SAlan Stern portstatus &= ~USB_PORT_STAT_ENABLE; 7006ee0b270SAlan Stern } 7016ee0b270SAlan Stern 702948fea37SAlan Stern /* Clear status-change flags; we'll debounce later */ 703948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) { 704948fea37SAlan Stern need_debounce_delay = true; 705948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 706948fea37SAlan Stern USB_PORT_FEAT_C_CONNECTION); 707948fea37SAlan Stern } 708948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) { 709948fea37SAlan Stern need_debounce_delay = true; 710948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 711948fea37SAlan Stern USB_PORT_FEAT_C_ENABLE); 712948fea37SAlan Stern } 713948fea37SAlan Stern 7146ee0b270SAlan Stern if (!udev || udev->state == USB_STATE_NOTATTACHED) { 7156ee0b270SAlan Stern /* Tell khubd to disconnect the device or 7166ee0b270SAlan Stern * check for a new connection 7176ee0b270SAlan Stern */ 7186ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 7196ee0b270SAlan Stern set_bit(port1, hub->change_bits); 7206ee0b270SAlan Stern 7216ee0b270SAlan Stern } else if (portstatus & USB_PORT_STAT_ENABLE) { 7226ee0b270SAlan Stern /* The power session apparently survived the resume. 7236ee0b270SAlan Stern * If there was an overcurrent or suspend change 7246ee0b270SAlan Stern * (i.e., remote wakeup request), have khubd 7256ee0b270SAlan Stern * take care of it. 7266ee0b270SAlan Stern */ 7276ee0b270SAlan Stern if (portchange) 7286ee0b270SAlan Stern set_bit(port1, hub->change_bits); 7296ee0b270SAlan Stern 7306ee0b270SAlan Stern } else if (udev->persist_enabled) { 7316ee0b270SAlan Stern #ifdef CONFIG_PM 7325e6effaeSAlan Stern udev->reset_resume = 1; 7336ee0b270SAlan Stern #endif 7348808f00cSAlan Stern set_bit(port1, hub->change_bits); 7358808f00cSAlan Stern 7366ee0b270SAlan Stern } else { 7376ee0b270SAlan Stern /* The power session is gone; tell khubd */ 7386ee0b270SAlan Stern usb_set_device_state(udev, USB_STATE_NOTATTACHED); 7396ee0b270SAlan Stern set_bit(port1, hub->change_bits); 7405e6effaeSAlan Stern } 7415e6effaeSAlan Stern } 7425e6effaeSAlan Stern 743948fea37SAlan Stern /* If no port-status-change flags were set, we don't need any 744948fea37SAlan Stern * debouncing. If flags were set we can try to debounce the 745948fea37SAlan Stern * ports all at once right now, instead of letting khubd do them 746948fea37SAlan Stern * one at a time later on. 747948fea37SAlan Stern * 748948fea37SAlan Stern * If any port-status changes do occur during this delay, khubd 749948fea37SAlan Stern * will see them later and handle them normally. 750948fea37SAlan Stern */ 7518520f380SAlan Stern if (need_debounce_delay) { 7528520f380SAlan Stern delay = HUB_DEBOUNCE_STABLE; 753f2835219SAlan Stern 7548520f380SAlan Stern /* Don't do a long sleep inside a workqueue routine */ 7558520f380SAlan Stern if (type == HUB_INIT2) { 7568520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); 7578520f380SAlan Stern schedule_delayed_work(&hub->init_work, 7588520f380SAlan Stern msecs_to_jiffies(delay)); 7598520f380SAlan Stern return; /* Continues at init3: below */ 7608520f380SAlan Stern } else { 7618520f380SAlan Stern msleep(delay); 7628520f380SAlan Stern } 7638520f380SAlan Stern } 7648520f380SAlan Stern init3: 765f2835219SAlan Stern hub->quiescing = 0; 766f2835219SAlan Stern 767f2835219SAlan Stern status = usb_submit_urb(hub->urb, GFP_NOIO); 768f2835219SAlan Stern if (status < 0) 769f2835219SAlan Stern dev_err(hub->intfdev, "activate --> %d\n", status); 770f2835219SAlan Stern if (hub->has_indicators && blinkenlights) 771f2835219SAlan Stern schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 772f2835219SAlan Stern 773f2835219SAlan Stern /* Scan all ports that need attention */ 774f2835219SAlan Stern kick_khubd(hub); 7755e6effaeSAlan Stern } 7765e6effaeSAlan Stern 7778520f380SAlan Stern /* Implement the continuations for the delays above */ 7788520f380SAlan Stern static void hub_init_func2(struct work_struct *ws) 7798520f380SAlan Stern { 7808520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 7818520f380SAlan Stern 7828520f380SAlan Stern hub_activate(hub, HUB_INIT2); 7838520f380SAlan Stern } 7848520f380SAlan Stern 7858520f380SAlan Stern static void hub_init_func3(struct work_struct *ws) 7868520f380SAlan Stern { 7878520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 7888520f380SAlan Stern 7898520f380SAlan Stern hub_activate(hub, HUB_INIT3); 7908520f380SAlan Stern } 7918520f380SAlan Stern 7924330354fSAlan Stern enum hub_quiescing_type { 7934330354fSAlan Stern HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 7944330354fSAlan Stern }; 7954330354fSAlan Stern 7964330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 7974330354fSAlan Stern { 7984330354fSAlan Stern struct usb_device *hdev = hub->hdev; 7994330354fSAlan Stern int i; 8004330354fSAlan Stern 8018520f380SAlan Stern cancel_delayed_work_sync(&hub->init_work); 8028520f380SAlan Stern 8034330354fSAlan Stern /* khubd and related activity won't re-trigger */ 8044330354fSAlan Stern hub->quiescing = 1; 8054330354fSAlan Stern 8064330354fSAlan Stern if (type != HUB_SUSPEND) { 8074330354fSAlan Stern /* Disconnect all the children */ 8084330354fSAlan Stern for (i = 0; i < hdev->maxchild; ++i) { 8094330354fSAlan Stern if (hdev->children[i]) 8104330354fSAlan Stern usb_disconnect(&hdev->children[i]); 8114330354fSAlan Stern } 8124330354fSAlan Stern } 8134330354fSAlan Stern 8144330354fSAlan Stern /* Stop khubd and related activity */ 8154330354fSAlan Stern usb_kill_urb(hub->urb); 8164330354fSAlan Stern if (hub->has_indicators) 8174330354fSAlan Stern cancel_delayed_work_sync(&hub->leds); 8184330354fSAlan Stern if (hub->tt.hub) 8194330354fSAlan Stern cancel_work_sync(&hub->tt.kevent); 8204330354fSAlan Stern } 8214330354fSAlan Stern 8223eb14915SAlan Stern /* caller has locked the hub device */ 8233eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf) 8243eb14915SAlan Stern { 8253eb14915SAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 8263eb14915SAlan Stern 8274330354fSAlan Stern hub_quiesce(hub, HUB_PRE_RESET); 828f07600cfSAlan Stern return 0; 8297d069b7dSAlan Stern } 8307d069b7dSAlan Stern 8317d069b7dSAlan Stern /* caller has locked the hub device */ 832f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf) 8337d069b7dSAlan Stern { 8347de18d8bSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 8357de18d8bSAlan Stern 836f2835219SAlan Stern hub_activate(hub, HUB_POST_RESET); 837f07600cfSAlan Stern return 0; 8387d069b7dSAlan Stern } 8397d069b7dSAlan Stern 8401da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub, 8411da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint) 8421da177e4SLinus Torvalds { 8431da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 8441da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 8451da177e4SLinus Torvalds u16 hubstatus, hubchange; 84674ad9bd2SGreg Kroah-Hartman u16 wHubCharacteristics; 8471da177e4SLinus Torvalds unsigned int pipe; 8481da177e4SLinus Torvalds int maxp, ret; 8491da177e4SLinus Torvalds char *message; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL, 8521da177e4SLinus Torvalds &hub->buffer_dma); 8531da177e4SLinus Torvalds if (!hub->buffer) { 8541da177e4SLinus Torvalds message = "can't allocate hub irq buffer"; 8551da177e4SLinus Torvalds ret = -ENOMEM; 8561da177e4SLinus Torvalds goto fail; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 8601da177e4SLinus Torvalds if (!hub->status) { 8611da177e4SLinus Torvalds message = "can't kmalloc hub status buffer"; 8621da177e4SLinus Torvalds ret = -ENOMEM; 8631da177e4SLinus Torvalds goto fail; 8641da177e4SLinus Torvalds } 865db90e7a1SAlan Stern mutex_init(&hub->status_mutex); 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 8681da177e4SLinus Torvalds if (!hub->descriptor) { 8691da177e4SLinus Torvalds message = "can't kmalloc hub descriptor"; 8701da177e4SLinus Torvalds ret = -ENOMEM; 8711da177e4SLinus Torvalds goto fail; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /* Request the entire hub descriptor. 8751da177e4SLinus Torvalds * hub->descriptor can handle USB_MAXCHILDREN ports, 8761da177e4SLinus Torvalds * but the hub can/will return fewer bytes here. 8771da177e4SLinus Torvalds */ 8781da177e4SLinus Torvalds ret = get_hub_descriptor(hdev, hub->descriptor, 8791da177e4SLinus Torvalds sizeof(*hub->descriptor)); 8801da177e4SLinus Torvalds if (ret < 0) { 8811da177e4SLinus Torvalds message = "can't read hub descriptor"; 8821da177e4SLinus Torvalds goto fail; 8831da177e4SLinus Torvalds } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 8841da177e4SLinus Torvalds message = "hub has too many ports!"; 8851da177e4SLinus Torvalds ret = -ENODEV; 8861da177e4SLinus Torvalds goto fail; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds hdev->maxchild = hub->descriptor->bNbrPorts; 8901da177e4SLinus Torvalds dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 8911da177e4SLinus Torvalds (hdev->maxchild == 1) ? "" : "s"); 8921da177e4SLinus Torvalds 89374ad9bd2SGreg Kroah-Hartman wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 8941da177e4SLinus Torvalds 89574ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_COMPOUND) { 8961da177e4SLinus Torvalds int i; 8971da177e4SLinus Torvalds char portstr [USB_MAXCHILDREN + 1]; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds for (i = 0; i < hdev->maxchild; i++) 9001da177e4SLinus Torvalds portstr[i] = hub->descriptor->DeviceRemovable 9011da177e4SLinus Torvalds [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 9021da177e4SLinus Torvalds ? 'F' : 'R'; 9031da177e4SLinus Torvalds portstr[hdev->maxchild] = 0; 9041da177e4SLinus Torvalds dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 9051da177e4SLinus Torvalds } else 9061da177e4SLinus Torvalds dev_dbg(hub_dev, "standalone hub\n"); 9071da177e4SLinus Torvalds 90874ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_LPSM) { 9091da177e4SLinus Torvalds case 0x00: 9101da177e4SLinus Torvalds dev_dbg(hub_dev, "ganged power switching\n"); 9111da177e4SLinus Torvalds break; 9121da177e4SLinus Torvalds case 0x01: 9131da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port power switching\n"); 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds case 0x02: 9161da177e4SLinus Torvalds case 0x03: 9171da177e4SLinus Torvalds dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 92174ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_OCPM) { 9221da177e4SLinus Torvalds case 0x00: 9231da177e4SLinus Torvalds dev_dbg(hub_dev, "global over-current protection\n"); 9241da177e4SLinus Torvalds break; 9251da177e4SLinus Torvalds case 0x08: 9261da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port over-current protection\n"); 9271da177e4SLinus Torvalds break; 9281da177e4SLinus Torvalds case 0x10: 9291da177e4SLinus Torvalds case 0x18: 9301da177e4SLinus Torvalds dev_dbg(hub_dev, "no over-current protection\n"); 9311da177e4SLinus Torvalds break; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds spin_lock_init (&hub->tt.lock); 9351da177e4SLinus Torvalds INIT_LIST_HEAD (&hub->tt.clear_list); 936c4028958SDavid Howells INIT_WORK (&hub->tt.kevent, hub_tt_kevent); 9371da177e4SLinus Torvalds switch (hdev->descriptor.bDeviceProtocol) { 9381da177e4SLinus Torvalds case 0: 9391da177e4SLinus Torvalds break; 9401da177e4SLinus Torvalds case 1: 9411da177e4SLinus Torvalds dev_dbg(hub_dev, "Single TT\n"); 9421da177e4SLinus Torvalds hub->tt.hub = hdev; 9431da177e4SLinus Torvalds break; 9441da177e4SLinus Torvalds case 2: 9451da177e4SLinus Torvalds ret = usb_set_interface(hdev, 0, 1); 9461da177e4SLinus Torvalds if (ret == 0) { 9471da177e4SLinus Torvalds dev_dbg(hub_dev, "TT per port\n"); 9481da177e4SLinus Torvalds hub->tt.multi = 1; 9491da177e4SLinus Torvalds } else 9501da177e4SLinus Torvalds dev_err(hub_dev, "Using single TT (err %d)\n", 9511da177e4SLinus Torvalds ret); 9521da177e4SLinus Torvalds hub->tt.hub = hdev; 9531da177e4SLinus Torvalds break; 9541da177e4SLinus Torvalds default: 9551da177e4SLinus Torvalds dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 9561da177e4SLinus Torvalds hdev->descriptor.bDeviceProtocol); 9571da177e4SLinus Torvalds break; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 960e09711aeSdavid-b@pacbell.net /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 96174ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_TTTT) { 962e09711aeSdavid-b@pacbell.net case HUB_TTTT_8_BITS: 963e09711aeSdavid-b@pacbell.net if (hdev->descriptor.bDeviceProtocol != 0) { 964e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666; 965e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 966e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 967e09711aeSdavid-b@pacbell.net 8, hub->tt.think_time); 968e09711aeSdavid-b@pacbell.net } 9691da177e4SLinus Torvalds break; 970e09711aeSdavid-b@pacbell.net case HUB_TTTT_16_BITS: 971e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 2; 972e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 973e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 974e09711aeSdavid-b@pacbell.net 16, hub->tt.think_time); 9751da177e4SLinus Torvalds break; 976e09711aeSdavid-b@pacbell.net case HUB_TTTT_24_BITS: 977e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 3; 978e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 979e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 980e09711aeSdavid-b@pacbell.net 24, hub->tt.think_time); 9811da177e4SLinus Torvalds break; 982e09711aeSdavid-b@pacbell.net case HUB_TTTT_32_BITS: 983e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 4; 984e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 985e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 986e09711aeSdavid-b@pacbell.net 32, hub->tt.think_time); 9871da177e4SLinus Torvalds break; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* probe() zeroes hub->indicator[] */ 99174ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_PORTIND) { 9921da177e4SLinus Torvalds hub->has_indicators = 1; 9931da177e4SLinus Torvalds dev_dbg(hub_dev, "Port indicators are supported\n"); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds dev_dbg(hub_dev, "power on to power good time: %dms\n", 9971da177e4SLinus Torvalds hub->descriptor->bPwrOn2PwrGood * 2); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* power budgeting mostly matters with bus-powered hubs, 10001da177e4SLinus Torvalds * and battery-powered root hubs (may provide just 8 mA). 10011da177e4SLinus Torvalds */ 10021da177e4SLinus Torvalds ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 100355c52718SAlan Stern if (ret < 2) { 10041da177e4SLinus Torvalds message = "can't get hub status"; 10051da177e4SLinus Torvalds goto fail; 10061da177e4SLinus Torvalds } 10077d35b929SAlan Stern le16_to_cpus(&hubstatus); 10087d35b929SAlan Stern if (hdev == hdev->bus->root_hub) { 100955c52718SAlan Stern if (hdev->bus_mA == 0 || hdev->bus_mA >= 500) 101055c52718SAlan Stern hub->mA_per_port = 500; 101155c52718SAlan Stern else { 101255c52718SAlan Stern hub->mA_per_port = hdev->bus_mA; 101355c52718SAlan Stern hub->limited_power = 1; 101455c52718SAlan Stern } 10157d35b929SAlan Stern } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 10161da177e4SLinus Torvalds dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 10171da177e4SLinus Torvalds hub->descriptor->bHubContrCurrent); 101855c52718SAlan Stern hub->limited_power = 1; 101955c52718SAlan Stern if (hdev->maxchild > 0) { 102055c52718SAlan Stern int remaining = hdev->bus_mA - 102155c52718SAlan Stern hub->descriptor->bHubContrCurrent; 10221da177e4SLinus Torvalds 102355c52718SAlan Stern if (remaining < hdev->maxchild * 100) 102455c52718SAlan Stern dev_warn(hub_dev, 102555c52718SAlan Stern "insufficient power available " 102655c52718SAlan Stern "to use all downstream ports\n"); 102755c52718SAlan Stern hub->mA_per_port = 100; /* 7.2.1.1 */ 102855c52718SAlan Stern } 102955c52718SAlan Stern } else { /* Self-powered external hub */ 103055c52718SAlan Stern /* FIXME: What about battery-powered external hubs that 103155c52718SAlan Stern * provide less current per port? */ 103255c52718SAlan Stern hub->mA_per_port = 500; 103355c52718SAlan Stern } 103455c52718SAlan Stern if (hub->mA_per_port < 500) 103555c52718SAlan Stern dev_dbg(hub_dev, "%umA bus power budget for each child\n", 103655c52718SAlan Stern hub->mA_per_port); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds ret = hub_hub_status(hub, &hubstatus, &hubchange); 10391da177e4SLinus Torvalds if (ret < 0) { 10401da177e4SLinus Torvalds message = "can't get hub status"; 10411da177e4SLinus Torvalds goto fail; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* local power status reports aren't always correct */ 10451da177e4SLinus Torvalds if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 10461da177e4SLinus Torvalds dev_dbg(hub_dev, "local power source is %s\n", 10471da177e4SLinus Torvalds (hubstatus & HUB_STATUS_LOCAL_POWER) 10481da177e4SLinus Torvalds ? "lost (inactive)" : "good"); 10491da177e4SLinus Torvalds 105074ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 10511da177e4SLinus Torvalds dev_dbg(hub_dev, "%sover-current condition exists\n", 10521da177e4SLinus Torvalds (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 10531da177e4SLinus Torvalds 105488fafff9Sinaky@linux.intel.com /* set up the interrupt endpoint 105588fafff9Sinaky@linux.intel.com * We use the EP's maxpacket size instead of (PORTS+1+7)/8 105688fafff9Sinaky@linux.intel.com * bytes as USB2.0[11.12.3] says because some hubs are known 105788fafff9Sinaky@linux.intel.com * to send more data (and thus cause overflow). For root hubs, 105888fafff9Sinaky@linux.intel.com * maxpktsize is defined in hcd.c's fake endpoint descriptors 105988fafff9Sinaky@linux.intel.com * to be big enough for at least USB_MAXCHILDREN ports. */ 10601da177e4SLinus Torvalds pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 10611da177e4SLinus Torvalds maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds if (maxp > sizeof(*hub->buffer)) 10641da177e4SLinus Torvalds maxp = sizeof(*hub->buffer); 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds hub->urb = usb_alloc_urb(0, GFP_KERNEL); 10671da177e4SLinus Torvalds if (!hub->urb) { 10681da177e4SLinus Torvalds message = "couldn't allocate interrupt urb"; 10691da177e4SLinus Torvalds ret = -ENOMEM; 10701da177e4SLinus Torvalds goto fail; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 10741da177e4SLinus Torvalds hub, endpoint->bInterval); 10751da177e4SLinus Torvalds hub->urb->transfer_dma = hub->buffer_dma; 10761da177e4SLinus Torvalds hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds /* maybe cycle the hub leds */ 10791da177e4SLinus Torvalds if (hub->has_indicators && blinkenlights) 10801da177e4SLinus Torvalds hub->indicator [0] = INDICATOR_CYCLE; 10811da177e4SLinus Torvalds 1082f2835219SAlan Stern hub_activate(hub, HUB_INIT); 10831da177e4SLinus Torvalds return 0; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds fail: 10861da177e4SLinus Torvalds dev_err (hub_dev, "config failed, %s (err %d)\n", 10871da177e4SLinus Torvalds message, ret); 10881da177e4SLinus Torvalds /* hub_disconnect() frees urb and descriptor */ 10891da177e4SLinus Torvalds return ret; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 1092e8054854SAlan Stern static void hub_release(struct kref *kref) 1093e8054854SAlan Stern { 1094e8054854SAlan Stern struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 1095e8054854SAlan Stern 1096e8054854SAlan Stern usb_put_intf(to_usb_interface(hub->intfdev)); 1097e8054854SAlan Stern kfree(hub); 1098e8054854SAlan Stern } 1099e8054854SAlan Stern 11001da177e4SLinus Torvalds static unsigned highspeed_hubs; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf) 11031da177e4SLinus Torvalds { 11041da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata (intf); 1105e8054854SAlan Stern 1106e8054854SAlan Stern /* Take the hub off the event list and don't let it be added again */ 1107e8054854SAlan Stern spin_lock_irq(&hub_event_lock); 1108e8054854SAlan Stern list_del_init(&hub->event_list); 1109e8054854SAlan Stern hub->disconnected = 1; 1110e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 11111da177e4SLinus Torvalds 11127de18d8bSAlan Stern /* Disconnect all children and quiesce the hub */ 11137de18d8bSAlan Stern hub->error = 0; 11144330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 11157de18d8bSAlan Stern 11168b28c752SAlan Stern usb_set_intfdata (intf, NULL); 11171da177e4SLinus Torvalds 1118e8054854SAlan Stern if (hub->hdev->speed == USB_SPEED_HIGH) 11191da177e4SLinus Torvalds highspeed_hubs--; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds usb_free_urb(hub->urb); 11221da177e4SLinus Torvalds kfree(hub->descriptor); 11231da177e4SLinus Torvalds kfree(hub->status); 1124e8054854SAlan Stern usb_buffer_free(hub->hdev, sizeof(*hub->buffer), hub->buffer, 11251da177e4SLinus Torvalds hub->buffer_dma); 11261da177e4SLinus Torvalds 1127e8054854SAlan Stern kref_put(&hub->kref, hub_release); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 11311da177e4SLinus Torvalds { 11321da177e4SLinus Torvalds struct usb_host_interface *desc; 11331da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint; 11341da177e4SLinus Torvalds struct usb_device *hdev; 11351da177e4SLinus Torvalds struct usb_hub *hub; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds desc = intf->cur_altsetting; 11381da177e4SLinus Torvalds hdev = interface_to_usbdev(intf); 11391da177e4SLinus Torvalds 114038f3ad5eSFelipe Balbi if (hdev->level == MAX_TOPO_LEVEL) { 1141b9cef6c3SWu Fengguang dev_err(&intf->dev, 1142b9cef6c3SWu Fengguang "Unsupported bus topology: hub nested too deep\n"); 114338f3ad5eSFelipe Balbi return -E2BIG; 114438f3ad5eSFelipe Balbi } 114538f3ad5eSFelipe Balbi 114689ccbdc9SDavid Brownell #ifdef CONFIG_USB_OTG_BLACKLIST_HUB 114789ccbdc9SDavid Brownell if (hdev->parent) { 114889ccbdc9SDavid Brownell dev_warn(&intf->dev, "ignoring external hub\n"); 114989ccbdc9SDavid Brownell return -ENODEV; 115089ccbdc9SDavid Brownell } 115189ccbdc9SDavid Brownell #endif 115289ccbdc9SDavid Brownell 11531da177e4SLinus Torvalds /* Some hubs have a subclass of 1, which AFAICT according to the */ 11541da177e4SLinus Torvalds /* specs is not defined, but it works */ 11551da177e4SLinus Torvalds if ((desc->desc.bInterfaceSubClass != 0) && 11561da177e4SLinus Torvalds (desc->desc.bInterfaceSubClass != 1)) { 11571da177e4SLinus Torvalds descriptor_error: 11581da177e4SLinus Torvalds dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); 11591da177e4SLinus Torvalds return -EIO; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 11631da177e4SLinus Torvalds if (desc->desc.bNumEndpoints != 1) 11641da177e4SLinus Torvalds goto descriptor_error; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds endpoint = &desc->endpoint[0].desc; 11671da177e4SLinus Torvalds 1168fbf81c29SLuiz Fernando N. Capitulino /* If it's not an interrupt in endpoint, we'd better punt! */ 1169fbf81c29SLuiz Fernando N. Capitulino if (!usb_endpoint_is_int_in(endpoint)) 11701da177e4SLinus Torvalds goto descriptor_error; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds /* We found a hub */ 11731da177e4SLinus Torvalds dev_info (&intf->dev, "USB hub found\n"); 11741da177e4SLinus Torvalds 11750a1ef3b5SAlan Stern hub = kzalloc(sizeof(*hub), GFP_KERNEL); 11761da177e4SLinus Torvalds if (!hub) { 11771da177e4SLinus Torvalds dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 11781da177e4SLinus Torvalds return -ENOMEM; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 1181e8054854SAlan Stern kref_init(&hub->kref); 11821da177e4SLinus Torvalds INIT_LIST_HEAD(&hub->event_list); 11831da177e4SLinus Torvalds hub->intfdev = &intf->dev; 11841da177e4SLinus Torvalds hub->hdev = hdev; 1185c4028958SDavid Howells INIT_DELAYED_WORK(&hub->leds, led_work); 11868520f380SAlan Stern INIT_DELAYED_WORK(&hub->init_work, NULL); 1187e8054854SAlan Stern usb_get_intf(intf); 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds usb_set_intfdata (intf, hub); 119040f122f3SAlan Stern intf->needs_remote_wakeup = 1; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds if (hdev->speed == USB_SPEED_HIGH) 11931da177e4SLinus Torvalds highspeed_hubs++; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds if (hub_configure(hub, endpoint) >= 0) 11961da177e4SLinus Torvalds return 0; 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds hub_disconnect (intf); 11991da177e4SLinus Torvalds return -ENODEV; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds static int 12031da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds struct usb_device *hdev = interface_to_usbdev (intf); 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* assert ifno == 0 (part of hub spec) */ 12081da177e4SLinus Torvalds switch (code) { 12091da177e4SLinus Torvalds case USBDEVFS_HUB_PORTINFO: { 12101da177e4SLinus Torvalds struct usbdevfs_hub_portinfo *info = user_data; 12111da177e4SLinus Torvalds int i; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 12141da177e4SLinus Torvalds if (hdev->devnum <= 0) 12151da177e4SLinus Torvalds info->nports = 0; 12161da177e4SLinus Torvalds else { 12171da177e4SLinus Torvalds info->nports = hdev->maxchild; 12181da177e4SLinus Torvalds for (i = 0; i < info->nports; i++) { 12191da177e4SLinus Torvalds if (hdev->children[i] == NULL) 12201da177e4SLinus Torvalds info->port[i] = 0; 12211da177e4SLinus Torvalds else 12221da177e4SLinus Torvalds info->port[i] = 12231da177e4SLinus Torvalds hdev->children[i]->devnum; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds return info->nports + 1; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds default: 12321da177e4SLinus Torvalds return -ENOSYS; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev) 12381da177e4SLinus Torvalds { 12391da177e4SLinus Torvalds int i; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds for (i = 0; i < udev->maxchild; ++i) { 12421da177e4SLinus Torvalds if (udev->children[i]) 12431da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev->children[i]); 12441da177e4SLinus Torvalds } 124515123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED) { 1246ee49fb5dSAlan Stern udev->discon_suspended = 1; 124715123006SSarah Sharp udev->active_duration -= jiffies; 124815123006SSarah Sharp } 12491da177e4SLinus Torvalds udev->state = USB_STATE_NOTATTACHED; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /** 12531da177e4SLinus Torvalds * usb_set_device_state - change a device's current state (usbcore, hcds) 12541da177e4SLinus Torvalds * @udev: pointer to device whose state should be changed 12551da177e4SLinus Torvalds * @new_state: new state value to be stored 12561da177e4SLinus Torvalds * 12571da177e4SLinus Torvalds * udev->state is _not_ fully protected by the device lock. Although 12581da177e4SLinus Torvalds * most transitions are made only while holding the lock, the state can 12591da177e4SLinus Torvalds * can change to USB_STATE_NOTATTACHED at almost any time. This 12601da177e4SLinus Torvalds * is so that devices can be marked as disconnected as soon as possible, 12611da177e4SLinus Torvalds * without having to wait for any semaphores to be released. As a result, 12621da177e4SLinus Torvalds * all changes to any device's state must be protected by the 12631da177e4SLinus Torvalds * device_state_lock spinlock. 12641da177e4SLinus Torvalds * 12651da177e4SLinus Torvalds * Once a device has been added to the device tree, all changes to its state 12661da177e4SLinus Torvalds * should be made using this routine. The state should _not_ be set directly. 12671da177e4SLinus Torvalds * 12681da177e4SLinus Torvalds * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 12691da177e4SLinus Torvalds * Otherwise udev->state is set to new_state, and if new_state is 12701da177e4SLinus Torvalds * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 12711da177e4SLinus Torvalds * to USB_STATE_NOTATTACHED. 12721da177e4SLinus Torvalds */ 12731da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev, 12741da177e4SLinus Torvalds enum usb_device_state new_state) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds unsigned long flags; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds spin_lock_irqsave(&device_state_lock, flags); 12791da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED) 12801da177e4SLinus Torvalds ; /* do nothing */ 1281b94dc6b5SDavid Brownell else if (new_state != USB_STATE_NOTATTACHED) { 1282fb669cc0SDavid Brownell 1283fb669cc0SDavid Brownell /* root hub wakeup capabilities are managed out-of-band 1284fb669cc0SDavid Brownell * and may involve silicon errata ... ignore them here. 1285fb669cc0SDavid Brownell */ 1286fb669cc0SDavid Brownell if (udev->parent) { 1287645daaabSAlan Stern if (udev->state == USB_STATE_SUSPENDED 1288645daaabSAlan Stern || new_state == USB_STATE_SUSPENDED) 1289645daaabSAlan Stern ; /* No change to wakeup settings */ 1290645daaabSAlan Stern else if (new_state == USB_STATE_CONFIGURED) 1291b94dc6b5SDavid Brownell device_init_wakeup(&udev->dev, 1292b94dc6b5SDavid Brownell (udev->actconfig->desc.bmAttributes 1293b94dc6b5SDavid Brownell & USB_CONFIG_ATT_WAKEUP)); 1294645daaabSAlan Stern else 1295b94dc6b5SDavid Brownell device_init_wakeup(&udev->dev, 0); 1296fb669cc0SDavid Brownell } 129715123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED && 129815123006SSarah Sharp new_state != USB_STATE_SUSPENDED) 129915123006SSarah Sharp udev->active_duration -= jiffies; 130015123006SSarah Sharp else if (new_state == USB_STATE_SUSPENDED && 130115123006SSarah Sharp udev->state != USB_STATE_SUSPENDED) 130215123006SSarah Sharp udev->active_duration += jiffies; 1303645daaabSAlan Stern udev->state = new_state; 1304b94dc6b5SDavid Brownell } else 13051da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev); 13061da177e4SLinus Torvalds spin_unlock_irqrestore(&device_state_lock, flags); 13071da177e4SLinus Torvalds } 13086da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state); 13091da177e4SLinus Torvalds 13108af548dcSInaky Perez-Gonzalez /* 13118af548dcSInaky Perez-Gonzalez * WUSB devices are simple: they have no hubs behind, so the mapping 13128af548dcSInaky Perez-Gonzalez * device <-> virtual port number becomes 1:1. Why? to simplify the 13138af548dcSInaky Perez-Gonzalez * life of the device connection logic in 13148af548dcSInaky Perez-Gonzalez * drivers/usb/wusbcore/devconnect.c. When we do the initial secret 13158af548dcSInaky Perez-Gonzalez * handshake we need to assign a temporary address in the unauthorized 13168af548dcSInaky Perez-Gonzalez * space. For simplicity we use the first virtual port number found to 13178af548dcSInaky Perez-Gonzalez * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] 13188af548dcSInaky Perez-Gonzalez * and that becomes it's address [X < 128] or its unauthorized address 13198af548dcSInaky Perez-Gonzalez * [X | 0x80]. 13208af548dcSInaky Perez-Gonzalez * 13218af548dcSInaky Perez-Gonzalez * We add 1 as an offset to the one-based USB-stack port number 13228af548dcSInaky Perez-Gonzalez * (zero-based wusb virtual port index) for two reasons: (a) dev addr 13238af548dcSInaky Perez-Gonzalez * 0 is reserved by USB for default address; (b) Linux's USB stack 13248af548dcSInaky Perez-Gonzalez * uses always #1 for the root hub of the controller. So USB stack's 13258af548dcSInaky Perez-Gonzalez * port #1, which is wusb virtual-port #0 has address #2. 13268af548dcSInaky Perez-Gonzalez */ 13271da177e4SLinus Torvalds static void choose_address(struct usb_device *udev) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds int devnum; 13301da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds /* If khubd ever becomes multithreaded, this will need a lock */ 13338af548dcSInaky Perez-Gonzalez if (udev->wusb) { 13348af548dcSInaky Perez-Gonzalez devnum = udev->portnum + 1; 13358af548dcSInaky Perez-Gonzalez BUG_ON(test_bit(devnum, bus->devmap.devicemap)); 13368af548dcSInaky Perez-Gonzalez } else { 13378af548dcSInaky Perez-Gonzalez /* Try to allocate the next devnum beginning at 13388af548dcSInaky Perez-Gonzalez * bus->devnum_next. */ 13391da177e4SLinus Torvalds devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 13401da177e4SLinus Torvalds bus->devnum_next); 13411da177e4SLinus Torvalds if (devnum >= 128) 13428af548dcSInaky Perez-Gonzalez devnum = find_next_zero_bit(bus->devmap.devicemap, 13438af548dcSInaky Perez-Gonzalez 128, 1); 13441da177e4SLinus Torvalds bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); 13458af548dcSInaky Perez-Gonzalez } 13461da177e4SLinus Torvalds if (devnum < 128) { 13471da177e4SLinus Torvalds set_bit(devnum, bus->devmap.devicemap); 13481da177e4SLinus Torvalds udev->devnum = devnum; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds static void release_address(struct usb_device *udev) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds if (udev->devnum > 0) { 13551da177e4SLinus Torvalds clear_bit(udev->devnum, udev->bus->devmap.devicemap); 13561da177e4SLinus Torvalds udev->devnum = -1; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13604953d141SDavid Vrabel static void update_address(struct usb_device *udev, int devnum) 13614953d141SDavid Vrabel { 13624953d141SDavid Vrabel /* The address for a WUSB device is managed by wusbcore. */ 13634953d141SDavid Vrabel if (!udev->wusb) 13644953d141SDavid Vrabel udev->devnum = devnum; 13654953d141SDavid Vrabel } 13664953d141SDavid Vrabel 1367d5d4db70SAlan Stern #ifdef CONFIG_USB_SUSPEND 1368d5d4db70SAlan Stern 1369d5d4db70SAlan Stern static void usb_stop_pm(struct usb_device *udev) 1370d5d4db70SAlan Stern { 1371d5d4db70SAlan Stern /* Synchronize with the ksuspend thread to prevent any more 1372d5d4db70SAlan Stern * autosuspend requests from being submitted, and decrement 1373d5d4db70SAlan Stern * the parent's count of unsuspended children. 1374d5d4db70SAlan Stern */ 1375d5d4db70SAlan Stern usb_pm_lock(udev); 1376d5d4db70SAlan Stern if (udev->parent && !udev->discon_suspended) 1377d5d4db70SAlan Stern usb_autosuspend_device(udev->parent); 1378d5d4db70SAlan Stern usb_pm_unlock(udev); 1379d5d4db70SAlan Stern 13809ac39f28SAlan Stern /* Stop any autosuspend or autoresume requests already submitted */ 13819ac39f28SAlan Stern cancel_delayed_work_sync(&udev->autosuspend); 13829ac39f28SAlan Stern cancel_work_sync(&udev->autoresume); 1383d5d4db70SAlan Stern } 1384d5d4db70SAlan Stern 1385d5d4db70SAlan Stern #else 1386d5d4db70SAlan Stern 1387d5d4db70SAlan Stern static inline void usb_stop_pm(struct usb_device *udev) 1388d5d4db70SAlan Stern { } 1389d5d4db70SAlan Stern 1390d5d4db70SAlan Stern #endif 1391d5d4db70SAlan Stern 13921da177e4SLinus Torvalds /** 13931da177e4SLinus Torvalds * usb_disconnect - disconnect a device (usbcore-internal) 13941da177e4SLinus Torvalds * @pdev: pointer to device being disconnected 13951da177e4SLinus Torvalds * Context: !in_interrupt () 13961da177e4SLinus Torvalds * 13971da177e4SLinus Torvalds * Something got disconnected. Get rid of it and all of its children. 13981da177e4SLinus Torvalds * 13991da177e4SLinus Torvalds * If *pdev is a normal device then the parent hub must already be locked. 14001da177e4SLinus Torvalds * If *pdev is a root hub then this routine will acquire the 14011da177e4SLinus Torvalds * usb_bus_list_lock on behalf of the caller. 14021da177e4SLinus Torvalds * 14031da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host 14041da177e4SLinus Torvalds * controllers) should ever call this. 14051da177e4SLinus Torvalds * 14061da177e4SLinus Torvalds * This call is synchronous, and may not be used in an interrupt context. 14071da177e4SLinus Torvalds */ 14081da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds struct usb_device *udev = *pdev; 14111da177e4SLinus Torvalds int i; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds if (!udev) { 1414441b62c1SHarvey Harrison pr_debug ("%s nodev\n", __func__); 14151da177e4SLinus Torvalds return; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds /* mark the device as inactive, so any further urb submissions for 14191da177e4SLinus Torvalds * this device (and any of its children) will fail immediately. 14201da177e4SLinus Torvalds * this quiesces everyting except pending urbs. 14211da177e4SLinus Torvalds */ 14221da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 14231da177e4SLinus Torvalds dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum); 14241da177e4SLinus Torvalds 14259ad3d6ccSAlan Stern usb_lock_device(udev); 14269ad3d6ccSAlan Stern 14271da177e4SLinus Torvalds /* Free up all the children before we remove this device */ 14281da177e4SLinus Torvalds for (i = 0; i < USB_MAXCHILDREN; i++) { 14291da177e4SLinus Torvalds if (udev->children[i]) 14301da177e4SLinus Torvalds usb_disconnect(&udev->children[i]); 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /* deallocate hcd/hardware state ... nuking all pending urbs and 14341da177e4SLinus Torvalds * cleaning up all state associated with the current configuration 14351da177e4SLinus Torvalds * so that the hardware is now fully quiesced. 14361da177e4SLinus Torvalds */ 1437782da727SAlan Stern dev_dbg (&udev->dev, "unregistering device\n"); 14381da177e4SLinus Torvalds usb_disable_device(udev, 0); 1439cde217a5SAlan Stern usb_hcd_synchronize_unlinks(udev); 14401da177e4SLinus Torvalds 14413b23dd6fSAlan Stern usb_remove_ep_devs(&udev->ep0); 1442782da727SAlan Stern usb_unlock_device(udev); 14433099e75aSGreg Kroah-Hartman 1444782da727SAlan Stern /* Unregister the device. The device driver is responsible 14453b23dd6fSAlan Stern * for de-configuring the device and invoking the remove-device 14463b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 1447782da727SAlan Stern */ 1448782da727SAlan Stern device_del(&udev->dev); 1449782da727SAlan Stern 1450782da727SAlan Stern /* Free the device number and delete the parent's children[] 14511da177e4SLinus Torvalds * (or root_hub) pointer. 14521da177e4SLinus Torvalds */ 14531da177e4SLinus Torvalds release_address(udev); 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds /* Avoid races with recursively_mark_NOTATTACHED() */ 14561da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 14571da177e4SLinus Torvalds *pdev = NULL; 14581da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 14591da177e4SLinus Torvalds 1460d5d4db70SAlan Stern usb_stop_pm(udev); 1461ee49fb5dSAlan Stern 1462782da727SAlan Stern put_device(&udev->dev); 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 1465f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 14661da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds if (!string) 14691da177e4SLinus Torvalds return; 14701da177e4SLinus Torvalds dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 1473f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev) 1474f2a383e4SGreg Kroah-Hartman { 1475f2a383e4SGreg Kroah-Hartman dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", 1476f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idVendor), 1477f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idProduct)); 1478b9cef6c3SWu Fengguang dev_info(&udev->dev, 1479b9cef6c3SWu Fengguang "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 1480f2a383e4SGreg Kroah-Hartman udev->descriptor.iManufacturer, 1481f2a383e4SGreg Kroah-Hartman udev->descriptor.iProduct, 1482f2a383e4SGreg Kroah-Hartman udev->descriptor.iSerialNumber); 1483f2a383e4SGreg Kroah-Hartman show_string(udev, "Product", udev->product); 1484f2a383e4SGreg Kroah-Hartman show_string(udev, "Manufacturer", udev->manufacturer); 1485f2a383e4SGreg Kroah-Hartman show_string(udev, "SerialNumber", udev->serial); 1486f2a383e4SGreg Kroah-Hartman } 14871da177e4SLinus Torvalds #else 1488f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { } 14891da177e4SLinus Torvalds #endif 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 14921da177e4SLinus Torvalds #include "otg_whitelist.h" 14931da177e4SLinus Torvalds #endif 14941da177e4SLinus Torvalds 14953ede760fSOliver Neukum /** 1496d9d16e8aSInaky Perez-Gonzalez * usb_configure_device_otg - FIXME (usbcore-internal) 14973ede760fSOliver Neukum * @udev: newly addressed device (in ADDRESS state) 14983ede760fSOliver Neukum * 1499d9d16e8aSInaky Perez-Gonzalez * Do configuration for On-The-Go devices 15003ede760fSOliver Neukum */ 1501d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device_otg(struct usb_device *udev) 15021da177e4SLinus Torvalds { 1503d9d16e8aSInaky Perez-Gonzalez int err = 0; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 15061da177e4SLinus Torvalds /* 15071da177e4SLinus Torvalds * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 15081da177e4SLinus Torvalds * to wake us after we've powered off VBUS; and HNP, switching roles 15091da177e4SLinus Torvalds * "host" to "peripheral". The OTG descriptor helps figure this out. 15101da177e4SLinus Torvalds */ 15111da177e4SLinus Torvalds if (!udev->bus->is_b_host 15121da177e4SLinus Torvalds && udev->config 15131da177e4SLinus Torvalds && udev->parent == udev->bus->root_hub) { 15141da177e4SLinus Torvalds struct usb_otg_descriptor *desc = 0; 15151da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds /* descriptor may appear anywhere in config */ 15181da177e4SLinus Torvalds if (__usb_get_extra_descriptor (udev->rawdescriptors[0], 15191da177e4SLinus Torvalds le16_to_cpu(udev->config[0].desc.wTotalLength), 15201da177e4SLinus Torvalds USB_DT_OTG, (void **) &desc) == 0) { 15211da177e4SLinus Torvalds if (desc->bmAttributes & USB_OTG_HNP) { 152212c3da34SAlan Stern unsigned port1 = udev->portnum; 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds dev_info(&udev->dev, 15251da177e4SLinus Torvalds "Dual-Role OTG device on %sHNP port\n", 15261da177e4SLinus Torvalds (port1 == bus->otg_port) 15271da177e4SLinus Torvalds ? "" : "non-"); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* enable HNP before suspend, it's simpler */ 15301da177e4SLinus Torvalds if (port1 == bus->otg_port) 15311da177e4SLinus Torvalds bus->b_hnp_enable = 1; 15321da177e4SLinus Torvalds err = usb_control_msg(udev, 15331da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 15341da177e4SLinus Torvalds USB_REQ_SET_FEATURE, 0, 15351da177e4SLinus Torvalds bus->b_hnp_enable 15361da177e4SLinus Torvalds ? USB_DEVICE_B_HNP_ENABLE 15371da177e4SLinus Torvalds : USB_DEVICE_A_ALT_HNP_SUPPORT, 15381da177e4SLinus Torvalds 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 15391da177e4SLinus Torvalds if (err < 0) { 15401da177e4SLinus Torvalds /* OTG MESSAGE: report errors here, 15411da177e4SLinus Torvalds * customize to match your product. 15421da177e4SLinus Torvalds */ 15431da177e4SLinus Torvalds dev_info(&udev->dev, 1544b9cef6c3SWu Fengguang "can't set HNP mode: %d\n", 15451da177e4SLinus Torvalds err); 15461da177e4SLinus Torvalds bus->b_hnp_enable = 0; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds if (!is_targeted(udev)) { 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds /* Maybe it can talk to us, though we can't talk to it. 15551da177e4SLinus Torvalds * (Includes HNP test device.) 15561da177e4SLinus Torvalds */ 15571da177e4SLinus Torvalds if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 1558634a84f8SDavid Brownell err = usb_port_suspend(udev, PMSG_SUSPEND); 15591da177e4SLinus Torvalds if (err < 0) 15601da177e4SLinus Torvalds dev_dbg(&udev->dev, "HNP fail, %d\n", err); 15611da177e4SLinus Torvalds } 1562ffcdc18dSVikram Pandita err = -ENOTSUPP; 15631da177e4SLinus Torvalds goto fail; 15641da177e4SLinus Torvalds } 1565d9d16e8aSInaky Perez-Gonzalez fail: 15661da177e4SLinus Torvalds #endif 1567d9d16e8aSInaky Perez-Gonzalez return err; 1568d9d16e8aSInaky Perez-Gonzalez } 15691da177e4SLinus Torvalds 1570d9d16e8aSInaky Perez-Gonzalez 1571d9d16e8aSInaky Perez-Gonzalez /** 1572d9d16e8aSInaky Perez-Gonzalez * usb_configure_device - Detect and probe device intfs/otg (usbcore-internal) 1573d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 1574d9d16e8aSInaky Perez-Gonzalez * 1575d9d16e8aSInaky Perez-Gonzalez * This is only called by usb_new_device() and usb_authorize_device() 1576d9d16e8aSInaky Perez-Gonzalez * and FIXME -- all comments that apply to them apply here wrt to 1577d9d16e8aSInaky Perez-Gonzalez * environment. 1578d9d16e8aSInaky Perez-Gonzalez * 1579d9d16e8aSInaky Perez-Gonzalez * If the device is WUSB and not authorized, we don't attempt to read 1580d9d16e8aSInaky Perez-Gonzalez * the string descriptors, as they will be errored out by the device 1581d9d16e8aSInaky Perez-Gonzalez * until it has been authorized. 1582d9d16e8aSInaky Perez-Gonzalez */ 1583d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device(struct usb_device *udev) 1584d9d16e8aSInaky Perez-Gonzalez { 1585d9d16e8aSInaky Perez-Gonzalez int err; 1586d9d16e8aSInaky Perez-Gonzalez 1587d9d16e8aSInaky Perez-Gonzalez if (udev->config == NULL) { 1588d9d16e8aSInaky Perez-Gonzalez err = usb_get_configuration(udev); 1589d9d16e8aSInaky Perez-Gonzalez if (err < 0) { 1590d9d16e8aSInaky Perez-Gonzalez dev_err(&udev->dev, "can't read configurations, error %d\n", 1591d9d16e8aSInaky Perez-Gonzalez err); 1592d9d16e8aSInaky Perez-Gonzalez goto fail; 1593d9d16e8aSInaky Perez-Gonzalez } 1594d9d16e8aSInaky Perez-Gonzalez } 1595d9d16e8aSInaky Perez-Gonzalez if (udev->wusb == 1 && udev->authorized == 0) { 1596d9d16e8aSInaky Perez-Gonzalez udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 1597d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 1598d9d16e8aSInaky Perez-Gonzalez udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 1599d9d16e8aSInaky Perez-Gonzalez } 1600d9d16e8aSInaky Perez-Gonzalez else { 1601d9d16e8aSInaky Perez-Gonzalez /* read the standard strings and cache them if present */ 1602d9d16e8aSInaky Perez-Gonzalez udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 1603d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = usb_cache_string(udev, 1604d9d16e8aSInaky Perez-Gonzalez udev->descriptor.iManufacturer); 1605d9d16e8aSInaky Perez-Gonzalez udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 1606d9d16e8aSInaky Perez-Gonzalez } 1607d9d16e8aSInaky Perez-Gonzalez err = usb_configure_device_otg(udev); 1608d9d16e8aSInaky Perez-Gonzalez fail: 1609d9d16e8aSInaky Perez-Gonzalez return err; 1610d9d16e8aSInaky Perez-Gonzalez } 1611d9d16e8aSInaky Perez-Gonzalez 1612d9d16e8aSInaky Perez-Gonzalez 1613d9d16e8aSInaky Perez-Gonzalez /** 1614d9d16e8aSInaky Perez-Gonzalez * usb_new_device - perform initial device setup (usbcore-internal) 1615d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 1616d9d16e8aSInaky Perez-Gonzalez * 1617d9d16e8aSInaky Perez-Gonzalez * This is called with devices which have been enumerated, but not yet 1618d9d16e8aSInaky Perez-Gonzalez * configured. The device descriptor is available, but not descriptors 1619d9d16e8aSInaky Perez-Gonzalez * for any device configuration. The caller must have locked either 1620d9d16e8aSInaky Perez-Gonzalez * the parent hub (if udev is a normal device) or else the 1621d9d16e8aSInaky Perez-Gonzalez * usb_bus_list_lock (if udev is a root hub). The parent's pointer to 1622d9d16e8aSInaky Perez-Gonzalez * udev has already been installed, but udev is not yet visible through 1623d9d16e8aSInaky Perez-Gonzalez * sysfs or other filesystem code. 1624d9d16e8aSInaky Perez-Gonzalez * 1625d9d16e8aSInaky Perez-Gonzalez * It will return if the device is configured properly or not. Zero if 1626d9d16e8aSInaky Perez-Gonzalez * the interface was registered with the driver core; else a negative 1627d9d16e8aSInaky Perez-Gonzalez * errno value. 1628d9d16e8aSInaky Perez-Gonzalez * 1629d9d16e8aSInaky Perez-Gonzalez * This call is synchronous, and may not be used in an interrupt context. 1630d9d16e8aSInaky Perez-Gonzalez * 1631d9d16e8aSInaky Perez-Gonzalez * Only the hub driver or root-hub registrar should ever call this. 1632d9d16e8aSInaky Perez-Gonzalez */ 1633d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev) 1634d9d16e8aSInaky Perez-Gonzalez { 1635d9d16e8aSInaky Perez-Gonzalez int err; 1636d9d16e8aSInaky Perez-Gonzalez 16376cd13201SAlan Stern /* Increment the parent's count of unsuspended children */ 16386cd13201SAlan Stern if (udev->parent) 16396cd13201SAlan Stern usb_autoresume_device(udev->parent); 16406cd13201SAlan Stern 1641d9d16e8aSInaky Perez-Gonzalez usb_detect_quirks(udev); /* Determine quirks */ 1642d9d16e8aSInaky Perez-Gonzalez err = usb_configure_device(udev); /* detect & probe dev/intfs */ 1643d9d16e8aSInaky Perez-Gonzalez if (err < 0) 1644d9d16e8aSInaky Perez-Gonzalez goto fail; 16459f8b17e6SKay Sievers /* export the usbdev device-node for libusb */ 16469f8b17e6SKay Sievers udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 16479f8b17e6SKay Sievers (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 16489f8b17e6SKay Sievers 16496cd13201SAlan Stern /* Tell the world! */ 16506cd13201SAlan Stern announce_device(udev); 1651195af2ccSAlan Stern 1652782da727SAlan Stern /* Register the device. The device driver is responsible 16533b23dd6fSAlan Stern * for configuring the device and invoking the add-device 16543b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 1655782da727SAlan Stern */ 16561da177e4SLinus Torvalds err = device_add(&udev->dev); 16571da177e4SLinus Torvalds if (err) { 16581da177e4SLinus Torvalds dev_err(&udev->dev, "can't device_add, error %d\n", err); 16591da177e4SLinus Torvalds goto fail; 16601da177e4SLinus Torvalds } 16619ad3d6ccSAlan Stern 16623b23dd6fSAlan Stern (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 1663c066475eSGreg Kroah-Hartman return err; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds fail: 16661da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 16676cd13201SAlan Stern usb_stop_pm(udev); 1668d9d16e8aSInaky Perez-Gonzalez return err; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 167193993a0aSInaky Perez-Gonzalez 167293993a0aSInaky Perez-Gonzalez /** 1673fd39c86bSRandy Dunlap * usb_deauthorize_device - deauthorize a device (usbcore-internal) 1674fd39c86bSRandy Dunlap * @usb_dev: USB device 1675fd39c86bSRandy Dunlap * 1676fd39c86bSRandy Dunlap * Move the USB device to a very basic state where interfaces are disabled 1677fd39c86bSRandy Dunlap * and the device is in fact unconfigured and unusable. 167893993a0aSInaky Perez-Gonzalez * 167993993a0aSInaky Perez-Gonzalez * We share a lock (that we have) with device_del(), so we need to 168093993a0aSInaky Perez-Gonzalez * defer its call. 168193993a0aSInaky Perez-Gonzalez */ 168293993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev) 168393993a0aSInaky Perez-Gonzalez { 168493993a0aSInaky Perez-Gonzalez unsigned cnt; 168593993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 168693993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 0) 168793993a0aSInaky Perez-Gonzalez goto out_unauthorized; 168893993a0aSInaky Perez-Gonzalez usb_dev->authorized = 0; 168993993a0aSInaky Perez-Gonzalez usb_set_configuration(usb_dev, -1); 169093993a0aSInaky Perez-Gonzalez usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 169193993a0aSInaky Perez-Gonzalez usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 169293993a0aSInaky Perez-Gonzalez usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 169393993a0aSInaky Perez-Gonzalez kfree(usb_dev->config); 169493993a0aSInaky Perez-Gonzalez usb_dev->config = NULL; 169593993a0aSInaky Perez-Gonzalez for (cnt = 0; cnt < usb_dev->descriptor.bNumConfigurations; cnt++) 169693993a0aSInaky Perez-Gonzalez kfree(usb_dev->rawdescriptors[cnt]); 169793993a0aSInaky Perez-Gonzalez usb_dev->descriptor.bNumConfigurations = 0; 169893993a0aSInaky Perez-Gonzalez kfree(usb_dev->rawdescriptors); 169993993a0aSInaky Perez-Gonzalez out_unauthorized: 170093993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); 170193993a0aSInaky Perez-Gonzalez return 0; 170293993a0aSInaky Perez-Gonzalez } 170393993a0aSInaky Perez-Gonzalez 170493993a0aSInaky Perez-Gonzalez 170593993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev) 170693993a0aSInaky Perez-Gonzalez { 170793993a0aSInaky Perez-Gonzalez int result = 0, c; 170893993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 170993993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 1) 171093993a0aSInaky Perez-Gonzalez goto out_authorized; 171193993a0aSInaky Perez-Gonzalez kfree(usb_dev->product); 171293993a0aSInaky Perez-Gonzalez usb_dev->product = NULL; 171393993a0aSInaky Perez-Gonzalez kfree(usb_dev->manufacturer); 171493993a0aSInaky Perez-Gonzalez usb_dev->manufacturer = NULL; 171593993a0aSInaky Perez-Gonzalez kfree(usb_dev->serial); 171693993a0aSInaky Perez-Gonzalez usb_dev->serial = NULL; 171793993a0aSInaky Perez-Gonzalez result = usb_autoresume_device(usb_dev); 171893993a0aSInaky Perez-Gonzalez if (result < 0) { 171993993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 172093993a0aSInaky Perez-Gonzalez "can't autoresume for authorization: %d\n", result); 172193993a0aSInaky Perez-Gonzalez goto error_autoresume; 172293993a0aSInaky Perez-Gonzalez } 172393993a0aSInaky Perez-Gonzalez result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); 172493993a0aSInaky Perez-Gonzalez if (result < 0) { 172593993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, "can't re-read device descriptor for " 172693993a0aSInaky Perez-Gonzalez "authorization: %d\n", result); 172793993a0aSInaky Perez-Gonzalez goto error_device_descriptor; 172893993a0aSInaky Perez-Gonzalez } 172993993a0aSInaky Perez-Gonzalez usb_dev->authorized = 1; 173093993a0aSInaky Perez-Gonzalez result = usb_configure_device(usb_dev); 173193993a0aSInaky Perez-Gonzalez if (result < 0) 173293993a0aSInaky Perez-Gonzalez goto error_configure; 173393993a0aSInaky Perez-Gonzalez /* Choose and set the configuration. This registers the interfaces 173493993a0aSInaky Perez-Gonzalez * with the driver core and lets interface drivers bind to them. 173593993a0aSInaky Perez-Gonzalez */ 1736b5ea060fSGreg Kroah-Hartman c = usb_choose_configuration(usb_dev); 173793993a0aSInaky Perez-Gonzalez if (c >= 0) { 173893993a0aSInaky Perez-Gonzalez result = usb_set_configuration(usb_dev, c); 173993993a0aSInaky Perez-Gonzalez if (result) { 174093993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 174193993a0aSInaky Perez-Gonzalez "can't set config #%d, error %d\n", c, result); 174293993a0aSInaky Perez-Gonzalez /* This need not be fatal. The user can try to 174393993a0aSInaky Perez-Gonzalez * set other configurations. */ 174493993a0aSInaky Perez-Gonzalez } 174593993a0aSInaky Perez-Gonzalez } 174693993a0aSInaky Perez-Gonzalez dev_info(&usb_dev->dev, "authorized to connect\n"); 174793993a0aSInaky Perez-Gonzalez error_configure: 174893993a0aSInaky Perez-Gonzalez error_device_descriptor: 174993993a0aSInaky Perez-Gonzalez error_autoresume: 175093993a0aSInaky Perez-Gonzalez out_authorized: 175193993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); // complements locktree 175293993a0aSInaky Perez-Gonzalez return result; 175393993a0aSInaky Perez-Gonzalez } 175493993a0aSInaky Perez-Gonzalez 175593993a0aSInaky Perez-Gonzalez 17560165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ 17570165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub) 17580165de09SInaky Perez-Gonzalez { 17590165de09SInaky Perez-Gonzalez struct usb_hcd *hcd; 17600165de09SInaky Perez-Gonzalez if (hub->hdev->parent != NULL) /* not a root hub? */ 17610165de09SInaky Perez-Gonzalez return 0; 17620165de09SInaky Perez-Gonzalez hcd = container_of(hub->hdev->bus, struct usb_hcd, self); 17630165de09SInaky Perez-Gonzalez return hcd->wireless; 17640165de09SInaky Perez-Gonzalez } 17650165de09SInaky Perez-Gonzalez 17660165de09SInaky Perez-Gonzalez 17671da177e4SLinus Torvalds #define PORT_RESET_TRIES 5 17681da177e4SLinus Torvalds #define SET_ADDRESS_TRIES 2 17691da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES 2 17701da177e4SLinus Torvalds #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 17711da177e4SLinus Torvalds #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first) 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 17741da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME 10 17751da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME 200 17761da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT 500 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1, 17791da177e4SLinus Torvalds struct usb_device *udev, unsigned int delay) 17801da177e4SLinus Torvalds { 17811da177e4SLinus Torvalds int delay_time, ret; 17821da177e4SLinus Torvalds u16 portstatus; 17831da177e4SLinus Torvalds u16 portchange; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds for (delay_time = 0; 17861da177e4SLinus Torvalds delay_time < HUB_RESET_TIMEOUT; 17871da177e4SLinus Torvalds delay_time += delay) { 17881da177e4SLinus Torvalds /* wait to give the device a chance to reset */ 17891da177e4SLinus Torvalds msleep(delay); 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /* read and decode port status */ 17921da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 17931da177e4SLinus Torvalds if (ret < 0) 17941da177e4SLinus Torvalds return ret; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds /* Device went away? */ 17971da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION)) 17981da177e4SLinus Torvalds return -ENOTCONN; 17991da177e4SLinus Torvalds 1800dd4dd19eSAlan Stern /* bomb out completely if the connection bounced */ 18011da177e4SLinus Torvalds if ((portchange & USB_PORT_STAT_C_CONNECTION)) 1802dd4dd19eSAlan Stern return -ENOTCONN; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* if we`ve finished resetting, then break out of the loop */ 18051da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_RESET) && 18061da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_ENABLE)) { 18070165de09SInaky Perez-Gonzalez if (hub_is_wusb(hub)) 18080165de09SInaky Perez-Gonzalez udev->speed = USB_SPEED_VARIABLE; 18090165de09SInaky Perez-Gonzalez else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 18101da177e4SLinus Torvalds udev->speed = USB_SPEED_HIGH; 18111da177e4SLinus Torvalds else if (portstatus & USB_PORT_STAT_LOW_SPEED) 18121da177e4SLinus Torvalds udev->speed = USB_SPEED_LOW; 18131da177e4SLinus Torvalds else 18141da177e4SLinus Torvalds udev->speed = USB_SPEED_FULL; 18151da177e4SLinus Torvalds return 0; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds /* switch to the long delay after two short delay failures */ 18191da177e4SLinus Torvalds if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 18201da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds dev_dbg (hub->intfdev, 18231da177e4SLinus Torvalds "port %d not reset yet, waiting %dms\n", 18241da177e4SLinus Torvalds port1, delay); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds return -EBUSY; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1, 18311da177e4SLinus Torvalds struct usb_device *udev, unsigned int delay) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds int i, status; 18341da177e4SLinus Torvalds 183532fe0198SAlan Stern /* Block EHCI CF initialization during the port reset. 183632fe0198SAlan Stern * Some companion controllers don't like it when they mix. 183732fe0198SAlan Stern */ 183832fe0198SAlan Stern down_read(&ehci_cf_port_reset_rwsem); 183932fe0198SAlan Stern 18401da177e4SLinus Torvalds /* Reset the port */ 18411da177e4SLinus Torvalds for (i = 0; i < PORT_RESET_TRIES; i++) { 18421da177e4SLinus Torvalds status = set_port_feature(hub->hdev, 18431da177e4SLinus Torvalds port1, USB_PORT_FEAT_RESET); 18441da177e4SLinus Torvalds if (status) 18451da177e4SLinus Torvalds dev_err(hub->intfdev, 18461da177e4SLinus Torvalds "cannot reset port %d (err = %d)\n", 18471da177e4SLinus Torvalds port1, status); 18481da177e4SLinus Torvalds else { 18491da177e4SLinus Torvalds status = hub_port_wait_reset(hub, port1, udev, delay); 1850dd16525bSDavid Brownell if (status && status != -ENOTCONN) 18511da177e4SLinus Torvalds dev_dbg(hub->intfdev, 18521da177e4SLinus Torvalds "port_wait_reset: err = %d\n", 18531da177e4SLinus Torvalds status); 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds /* return on disconnect or reset */ 18571da177e4SLinus Torvalds switch (status) { 18581da177e4SLinus Torvalds case 0: 1859b789696aSDavid Brownell /* TRSTRCY = 10 ms; plus some extra */ 1860b789696aSDavid Brownell msleep(10 + 40); 18614953d141SDavid Vrabel update_address(udev, 0); 18621da177e4SLinus Torvalds /* FALL THROUGH */ 18631da177e4SLinus Torvalds case -ENOTCONN: 18641da177e4SLinus Torvalds case -ENODEV: 18651da177e4SLinus Torvalds clear_port_feature(hub->hdev, 18661da177e4SLinus Torvalds port1, USB_PORT_FEAT_C_RESET); 18671da177e4SLinus Torvalds /* FIXME need disconnect() for NOTATTACHED device */ 18681da177e4SLinus Torvalds usb_set_device_state(udev, status 18691da177e4SLinus Torvalds ? USB_STATE_NOTATTACHED 18701da177e4SLinus Torvalds : USB_STATE_DEFAULT); 187132fe0198SAlan Stern goto done; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds dev_dbg (hub->intfdev, 18751da177e4SLinus Torvalds "port %d not enabled, trying reset again...\n", 18761da177e4SLinus Torvalds port1); 18771da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds dev_err (hub->intfdev, 18811da177e4SLinus Torvalds "Cannot enable port %i. Maybe the USB cable is bad?\n", 18821da177e4SLinus Torvalds port1); 18831da177e4SLinus Torvalds 188432fe0198SAlan Stern done: 188532fe0198SAlan Stern up_read(&ehci_cf_port_reset_rwsem); 18861da177e4SLinus Torvalds return status; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 1889d388dab7SAlan Stern #ifdef CONFIG_PM 18901da177e4SLinus Torvalds 1891b01b03f3SAlan Stern #define MASK_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \ 1892b01b03f3SAlan Stern USB_PORT_STAT_SUSPEND) 1893b01b03f3SAlan Stern #define WANT_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION) 1894b01b03f3SAlan Stern 1895b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume, 1896b01b03f3SAlan Stern * is ready for a reset-resume, or should be disconnected. 1897b01b03f3SAlan Stern */ 1898b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev, 1899b01b03f3SAlan Stern struct usb_hub *hub, int port1, 1900b01b03f3SAlan Stern int status, unsigned portchange, unsigned portstatus) 1901b01b03f3SAlan Stern { 1902b01b03f3SAlan Stern /* Is the device still present? */ 1903b01b03f3SAlan Stern if (status || (portstatus & MASK_BITS) != WANT_BITS) { 1904b01b03f3SAlan Stern if (status >= 0) 1905b01b03f3SAlan Stern status = -ENODEV; 1906b01b03f3SAlan Stern } 1907b01b03f3SAlan Stern 190886c57edfSAlan Stern /* Can't do a normal resume if the port isn't enabled, 190986c57edfSAlan Stern * so try a reset-resume instead. 191086c57edfSAlan Stern */ 191186c57edfSAlan Stern else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 191286c57edfSAlan Stern if (udev->persist_enabled) 191386c57edfSAlan Stern udev->reset_resume = 1; 191486c57edfSAlan Stern else 1915b01b03f3SAlan Stern status = -ENODEV; 191686c57edfSAlan Stern } 1917b01b03f3SAlan Stern 1918b01b03f3SAlan Stern if (status) { 1919b01b03f3SAlan Stern dev_dbg(hub->intfdev, 1920b01b03f3SAlan Stern "port %d status %04x.%04x after resume, %d\n", 1921b01b03f3SAlan Stern port1, portchange, portstatus, status); 1922b01b03f3SAlan Stern } else if (udev->reset_resume) { 1923b01b03f3SAlan Stern 1924b01b03f3SAlan Stern /* Late port handoff can set status-change bits */ 1925b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) 1926b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 1927b01b03f3SAlan Stern USB_PORT_FEAT_C_CONNECTION); 1928b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) 1929b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 1930b01b03f3SAlan Stern USB_PORT_FEAT_C_ENABLE); 1931b01b03f3SAlan Stern } 1932b01b03f3SAlan Stern 1933b01b03f3SAlan Stern return status; 1934b01b03f3SAlan Stern } 1935b01b03f3SAlan Stern 19361da177e4SLinus Torvalds #ifdef CONFIG_USB_SUSPEND 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds /* 1939140d8f68SAlan Stern * usb_port_suspend - suspend a usb device's upstream port 1940624d6c07SAlan Stern * @udev: device that's no longer in active use, not a root hub 19415edbfb7cSDavid Brownell * Context: must be able to sleep; device not locked; pm locks held 19421da177e4SLinus Torvalds * 19431da177e4SLinus Torvalds * Suspends a USB device that isn't in active use, conserving power. 19441da177e4SLinus Torvalds * Devices may wake out of a suspend, if anything important happens, 19451da177e4SLinus Torvalds * using the remote wakeup mechanism. They may also be taken out of 1946140d8f68SAlan Stern * suspend by the host, using usb_port_resume(). It's also routine 19471da177e4SLinus Torvalds * to disconnect devices while they are suspended. 19481da177e4SLinus Torvalds * 1949390a8c34SDavid Brownell * This only affects the USB hardware for a device; its interfaces 1950390a8c34SDavid Brownell * (and, for hubs, child devices) must already have been suspended. 1951390a8c34SDavid Brownell * 1952624d6c07SAlan Stern * Selective port suspend reduces power; most suspended devices draw 1953624d6c07SAlan Stern * less than 500 uA. It's also used in OTG, along with remote wakeup. 1954624d6c07SAlan Stern * All devices below the suspended port are also suspended. 1955624d6c07SAlan Stern * 1956624d6c07SAlan Stern * Devices leave suspend state when the host wakes them up. Some devices 1957624d6c07SAlan Stern * also support "remote wakeup", where the device can activate the USB 1958624d6c07SAlan Stern * tree above them to deliver data, such as a keypress or packet. In 1959624d6c07SAlan Stern * some cases, this wakes the USB host. 1960624d6c07SAlan Stern * 19611da177e4SLinus Torvalds * Suspending OTG devices may trigger HNP, if that's been enabled 19621da177e4SLinus Torvalds * between a pair of dual-role devices. That will change roles, such 19631da177e4SLinus Torvalds * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 19641da177e4SLinus Torvalds * 19654956eccdSAlan Stern * Devices on USB hub ports have only one "suspend" state, corresponding 19664956eccdSAlan Stern * to ACPI D2, "may cause the device to lose some context". 19674956eccdSAlan Stern * State transitions include: 19684956eccdSAlan Stern * 19694956eccdSAlan Stern * - suspend, resume ... when the VBUS power link stays live 19704956eccdSAlan Stern * - suspend, disconnect ... VBUS lost 19714956eccdSAlan Stern * 19724956eccdSAlan Stern * Once VBUS drop breaks the circuit, the port it's using has to go through 19734956eccdSAlan Stern * normal re-enumeration procedures, starting with enabling VBUS power. 19744956eccdSAlan Stern * Other than re-initializing the hub (plug/unplug, except for root hubs), 19754956eccdSAlan Stern * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 19764956eccdSAlan Stern * timer, no SRP, no requests through sysfs. 19774956eccdSAlan Stern * 19784956eccdSAlan Stern * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when 19794956eccdSAlan Stern * the root hub for their bus goes into global suspend ... so we don't 19804956eccdSAlan Stern * (falsely) update the device power state to say it suspended. 19814956eccdSAlan Stern * 19821da177e4SLinus Torvalds * Returns 0 on success, else negative errno. 19831da177e4SLinus Torvalds */ 198465bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 19851da177e4SLinus Torvalds { 1986624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 1987624d6c07SAlan Stern int port1 = udev->portnum; 1988624d6c07SAlan Stern int status; 19894956eccdSAlan Stern 1990624d6c07SAlan Stern // dev_dbg(hub->intfdev, "suspend port %d\n", port1); 1991624d6c07SAlan Stern 1992624d6c07SAlan Stern /* enable remote wakeup when appropriate; this lets the device 1993624d6c07SAlan Stern * wake up the upstream hub (including maybe the root hub). 1994624d6c07SAlan Stern * 1995624d6c07SAlan Stern * NOTE: OTG devices may issue remote wakeup (or SRP) even when 1996624d6c07SAlan Stern * we don't explicitly enable it here. 1997624d6c07SAlan Stern */ 1998624d6c07SAlan Stern if (udev->do_remote_wakeup) { 1999624d6c07SAlan Stern status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2000624d6c07SAlan Stern USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 2001624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2002624d6c07SAlan Stern NULL, 0, 2003624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2004624d6c07SAlan Stern if (status) 2005624d6c07SAlan Stern dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 2006624d6c07SAlan Stern status); 2007624d6c07SAlan Stern } 2008624d6c07SAlan Stern 2009624d6c07SAlan Stern /* see 7.1.7.6 */ 2010624d6c07SAlan Stern status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND); 2011624d6c07SAlan Stern if (status) { 2012624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", 2013624d6c07SAlan Stern port1, status); 2014624d6c07SAlan Stern /* paranoia: "should not happen" */ 2015624d6c07SAlan Stern (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2016624d6c07SAlan Stern USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2017624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2018624d6c07SAlan Stern NULL, 0, 2019624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2020624d6c07SAlan Stern } else { 2021624d6c07SAlan Stern /* device has up to 10 msec to fully suspend */ 2022624d6c07SAlan Stern dev_dbg(&udev->dev, "usb %ssuspend\n", 202365bfd296SAlan Stern (msg.event & PM_EVENT_AUTO ? "auto-" : "")); 2024624d6c07SAlan Stern usb_set_device_state(udev, USB_STATE_SUSPENDED); 2025624d6c07SAlan Stern msleep(10); 2026624d6c07SAlan Stern } 20274956eccdSAlan Stern return status; 20281da177e4SLinus Torvalds } 2029f3f3253dSDavid Brownell 20301da177e4SLinus Torvalds /* 2031390a8c34SDavid Brownell * If the USB "suspend" state is in use (rather than "global suspend"), 2032390a8c34SDavid Brownell * many devices will be individually taken out of suspend state using 203354515fe5SAlan Stern * special "resume" signaling. This routine kicks in shortly after 20341da177e4SLinus Torvalds * hardware resume signaling is finished, either because of selective 20351da177e4SLinus Torvalds * resume (by host) or remote wakeup (by device) ... now see what changed 20361da177e4SLinus Torvalds * in the tree that's rooted at this device. 203754515fe5SAlan Stern * 203854515fe5SAlan Stern * If @udev->reset_resume is set then the device is reset before the 203954515fe5SAlan Stern * status check is done. 20401da177e4SLinus Torvalds */ 2041140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev) 20421da177e4SLinus Torvalds { 204354515fe5SAlan Stern int status = 0; 20441da177e4SLinus Torvalds u16 devstatus; 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* caller owns the udev device lock */ 2047b9cef6c3SWu Fengguang dev_dbg(&udev->dev, "%s\n", 2048b9cef6c3SWu Fengguang udev->reset_resume ? "finish reset-resume" : "finish resume"); 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds /* usb ch9 identifies four variants of SUSPENDED, based on what 20511da177e4SLinus Torvalds * state the device resumes to. Linux currently won't see the 20521da177e4SLinus Torvalds * first two on the host side; they'd be inside hub_port_init() 20531da177e4SLinus Torvalds * during many timeouts, but khubd can't suspend until later. 20541da177e4SLinus Torvalds */ 20551da177e4SLinus Torvalds usb_set_device_state(udev, udev->actconfig 20561da177e4SLinus Torvalds ? USB_STATE_CONFIGURED 20571da177e4SLinus Torvalds : USB_STATE_ADDRESS); 20581da177e4SLinus Torvalds 205954515fe5SAlan Stern /* 10.5.4.5 says not to reset a suspended port if the attached 206054515fe5SAlan Stern * device is enabled for remote wakeup. Hence the reset 206154515fe5SAlan Stern * operation is carried out here, after the port has been 206254515fe5SAlan Stern * resumed. 206354515fe5SAlan Stern */ 206454515fe5SAlan Stern if (udev->reset_resume) 206586c57edfSAlan Stern retry_reset_resume: 2066742120c6SMing Lei status = usb_reset_and_verify_device(udev); 206754515fe5SAlan Stern 20681da177e4SLinus Torvalds /* 10.5.4.5 says be sure devices in the tree are still there. 20691da177e4SLinus Torvalds * For now let's assume the device didn't go crazy on resume, 20701da177e4SLinus Torvalds * and device drivers will know about any resume quirks. 20711da177e4SLinus Torvalds */ 207254515fe5SAlan Stern if (status == 0) { 207346dede46SAlan Stern devstatus = 0; 20741da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 2075b40b7a90SAlan Stern if (status >= 0) 207646dede46SAlan Stern status = (status > 0 ? 0 : -ENODEV); 207786c57edfSAlan Stern 207886c57edfSAlan Stern /* If a normal resume failed, try doing a reset-resume */ 207986c57edfSAlan Stern if (status && !udev->reset_resume && udev->persist_enabled) { 208086c57edfSAlan Stern dev_dbg(&udev->dev, "retry with reset-resume\n"); 208186c57edfSAlan Stern udev->reset_resume = 1; 208286c57edfSAlan Stern goto retry_reset_resume; 208386c57edfSAlan Stern } 208454515fe5SAlan Stern } 2085b40b7a90SAlan Stern 2086624d6c07SAlan Stern if (status) { 2087624d6c07SAlan Stern dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 20881da177e4SLinus Torvalds status); 2089624d6c07SAlan Stern } else if (udev->actconfig) { 20901da177e4SLinus Torvalds le16_to_cpus(&devstatus); 2091686314cfSAlan Stern if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 20921da177e4SLinus Torvalds status = usb_control_msg(udev, 20931da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 20941da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, 20951da177e4SLinus Torvalds USB_RECIP_DEVICE, 20961da177e4SLinus Torvalds USB_DEVICE_REMOTE_WAKEUP, 0, 20971da177e4SLinus Torvalds NULL, 0, 20981da177e4SLinus Torvalds USB_CTRL_SET_TIMEOUT); 2099a8e7c565SAlan Stern if (status) 2100b9cef6c3SWu Fengguang dev_dbg(&udev->dev, 2101b9cef6c3SWu Fengguang "disable remote wakeup, status %d\n", 2102b9cef6c3SWu Fengguang status); 21034bf0ba86SAlan Stern } 21041da177e4SLinus Torvalds status = 0; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds return status; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109624d6c07SAlan Stern /* 2110624d6c07SAlan Stern * usb_port_resume - re-activate a suspended usb device's upstream port 2111624d6c07SAlan Stern * @udev: device to re-activate, not a root hub 2112624d6c07SAlan Stern * Context: must be able to sleep; device not locked; pm locks held 2113624d6c07SAlan Stern * 2114624d6c07SAlan Stern * This will re-activate the suspended device, increasing power usage 2115624d6c07SAlan Stern * while letting drivers communicate again with its endpoints. 2116624d6c07SAlan Stern * USB resume explicitly guarantees that the power session between 2117624d6c07SAlan Stern * the host and the device is the same as it was when the device 2118624d6c07SAlan Stern * suspended. 2119624d6c07SAlan Stern * 2120feccc30dSAlan Stern * If @udev->reset_resume is set then this routine won't check that the 2121feccc30dSAlan Stern * port is still enabled. Furthermore, finish_port_resume() above will 212254515fe5SAlan Stern * reset @udev. The end result is that a broken power session can be 212354515fe5SAlan Stern * recovered and @udev will appear to persist across a loss of VBUS power. 212454515fe5SAlan Stern * 212554515fe5SAlan Stern * For example, if a host controller doesn't maintain VBUS suspend current 212654515fe5SAlan Stern * during a system sleep or is reset when the system wakes up, all the USB 212754515fe5SAlan Stern * power sessions below it will be broken. This is especially troublesome 212854515fe5SAlan Stern * for mass-storage devices containing mounted filesystems, since the 212954515fe5SAlan Stern * device will appear to have disconnected and all the memory mappings 213054515fe5SAlan Stern * to it will be lost. Using the USB_PERSIST facility, the device can be 213154515fe5SAlan Stern * made to appear as if it had not disconnected. 213254515fe5SAlan Stern * 2133742120c6SMing Lei * This facility can be dangerous. Although usb_reset_and_verify_device() makes 2134feccc30dSAlan Stern * every effort to insure that the same device is present after the 213554515fe5SAlan Stern * reset as before, it cannot provide a 100% guarantee. Furthermore it's 213654515fe5SAlan Stern * quite possible for a device to remain unaltered but its media to be 213754515fe5SAlan Stern * changed. If the user replaces a flash memory card while the system is 213854515fe5SAlan Stern * asleep, he will have only himself to blame when the filesystem on the 213954515fe5SAlan Stern * new card is corrupted and the system crashes. 214054515fe5SAlan Stern * 2141624d6c07SAlan Stern * Returns 0 on success, else negative errno. 2142624d6c07SAlan Stern */ 214365bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 21441da177e4SLinus Torvalds { 2145624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2146624d6c07SAlan Stern int port1 = udev->portnum; 21471da177e4SLinus Torvalds int status; 2148d25450c6SAlan Stern u16 portchange, portstatus; 2149d25450c6SAlan Stern 2150d25450c6SAlan Stern /* Skip the initial Clear-Suspend step for a remote wakeup */ 2151d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 2152d25450c6SAlan Stern if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND)) 2153d25450c6SAlan Stern goto SuspendCleared; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds // dev_dbg(hub->intfdev, "resume port %d\n", port1); 21561da177e4SLinus Torvalds 2157d5cbad4bSAlan Stern set_bit(port1, hub->busy_bits); 2158d5cbad4bSAlan Stern 21591da177e4SLinus Torvalds /* see 7.1.7.7; affects power usage, but not budgeting */ 21601da177e4SLinus Torvalds status = clear_port_feature(hub->hdev, 21611da177e4SLinus Torvalds port1, USB_PORT_FEAT_SUSPEND); 21621da177e4SLinus Torvalds if (status) { 2163624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", 21641da177e4SLinus Torvalds port1, status); 21651da177e4SLinus Torvalds } else { 21661da177e4SLinus Torvalds /* drive resume for at least 20 msec */ 2167645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", 216865bfd296SAlan Stern (msg.event & PM_EVENT_AUTO ? "auto-" : "")); 21691da177e4SLinus Torvalds msleep(25); 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds /* Virtual root hubs can trigger on GET_PORT_STATUS to 21721da177e4SLinus Torvalds * stop resume signaling. Then finish the resume 21731da177e4SLinus Torvalds * sequence. 21741da177e4SLinus Torvalds */ 2175d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 217654515fe5SAlan Stern 21771da177e4SLinus Torvalds /* TRSMRCY = 10 msec */ 21781da177e4SLinus Torvalds msleep(10); 21791da177e4SLinus Torvalds } 2180b01b03f3SAlan Stern 2181b01b03f3SAlan Stern SuspendCleared: 2182b01b03f3SAlan Stern if (status == 0) { 2183b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_SUSPEND) 2184b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2185b01b03f3SAlan Stern USB_PORT_FEAT_C_SUSPEND); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 2188d5cbad4bSAlan Stern clear_bit(port1, hub->busy_bits); 2189d5cbad4bSAlan Stern 2190b01b03f3SAlan Stern status = check_port_resume_type(udev, 2191b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 219254515fe5SAlan Stern if (status == 0) 219354515fe5SAlan Stern status = finish_port_resume(udev); 219454515fe5SAlan Stern if (status < 0) { 219554515fe5SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 219654515fe5SAlan Stern hub_port_logical_disconnect(hub, port1); 219754515fe5SAlan Stern } 21981da177e4SLinus Torvalds return status; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 22018808f00cSAlan Stern /* caller has locked udev */ 22021da177e4SLinus Torvalds static int remote_wakeup(struct usb_device *udev) 22031da177e4SLinus Torvalds { 22041da177e4SLinus Torvalds int status = 0; 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED) { 2207645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 22081941044aSAlan Stern usb_mark_last_busy(udev); 220965bfd296SAlan Stern status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME); 2210d25450c6SAlan Stern } 22111da177e4SLinus Torvalds return status; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 2214d388dab7SAlan Stern #else /* CONFIG_USB_SUSPEND */ 2215d388dab7SAlan Stern 2216d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */ 2217d388dab7SAlan Stern 221865bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 2219d388dab7SAlan Stern { 2220d388dab7SAlan Stern return 0; 2221d388dab7SAlan Stern } 2222d388dab7SAlan Stern 2223b01b03f3SAlan Stern /* However we may need to do a reset-resume */ 2224b01b03f3SAlan Stern 222565bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 2226d388dab7SAlan Stern { 2227b01b03f3SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2228b01b03f3SAlan Stern int port1 = udev->portnum; 2229b01b03f3SAlan Stern int status; 2230b01b03f3SAlan Stern u16 portchange, portstatus; 223154515fe5SAlan Stern 2232b01b03f3SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 2233b01b03f3SAlan Stern status = check_port_resume_type(udev, 2234b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 2235b01b03f3SAlan Stern 2236b01b03f3SAlan Stern if (status) { 2237b01b03f3SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 2238b01b03f3SAlan Stern hub_port_logical_disconnect(hub, port1); 2239b01b03f3SAlan Stern } else if (udev->reset_resume) { 224054515fe5SAlan Stern dev_dbg(&udev->dev, "reset-resume\n"); 2241742120c6SMing Lei status = usb_reset_and_verify_device(udev); 224254515fe5SAlan Stern } 224354515fe5SAlan Stern return status; 2244d388dab7SAlan Stern } 2245d388dab7SAlan Stern 2246d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev) 2247d388dab7SAlan Stern { 2248d388dab7SAlan Stern return 0; 2249d388dab7SAlan Stern } 2250d388dab7SAlan Stern 2251d388dab7SAlan Stern #endif 2252d388dab7SAlan Stern 2253db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata (intf); 22561da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 22571da177e4SLinus Torvalds unsigned port1; 22581da177e4SLinus Torvalds 2259c9f89fa4SDavid Brownell /* fail if children aren't already suspended */ 22601da177e4SLinus Torvalds for (port1 = 1; port1 <= hdev->maxchild; port1++) { 22611da177e4SLinus Torvalds struct usb_device *udev; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds udev = hdev->children [port1-1]; 22646840d255SAlan Stern if (udev && udev->can_submit) { 226565bfd296SAlan Stern if (!(msg.event & PM_EVENT_AUTO)) 2266645daaabSAlan Stern dev_dbg(&intf->dev, "port %d nyet suspended\n", 2267645daaabSAlan Stern port1); 2268c9f89fa4SDavid Brownell return -EBUSY; 2269c9f89fa4SDavid Brownell } 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 2272441b62c1SHarvey Harrison dev_dbg(&intf->dev, "%s\n", __func__); 227340f122f3SAlan Stern 2274c9f89fa4SDavid Brownell /* stop khubd and related activity */ 22754330354fSAlan Stern hub_quiesce(hub, HUB_SUSPEND); 2276b6f6436dSAlan Stern return 0; 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf) 22801da177e4SLinus Torvalds { 22811da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 22821da177e4SLinus Torvalds 22835e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 2284f2835219SAlan Stern hub_activate(hub, HUB_RESUME); 22851da177e4SLinus Torvalds return 0; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds 2288b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf) 2289f07600cfSAlan Stern { 2290b41a60ecSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 2291f07600cfSAlan Stern 22925e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 2293f2835219SAlan Stern hub_activate(hub, HUB_RESET_RESUME); 2294f07600cfSAlan Stern return 0; 2295f07600cfSAlan Stern } 2296f07600cfSAlan Stern 229754515fe5SAlan Stern /** 229854515fe5SAlan Stern * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 229954515fe5SAlan Stern * @rhdev: struct usb_device for the root hub 230054515fe5SAlan Stern * 230154515fe5SAlan Stern * The USB host controller driver calls this function when its root hub 230254515fe5SAlan Stern * is resumed and Vbus power has been interrupted or the controller 2303feccc30dSAlan Stern * has been reset. The routine marks @rhdev as having lost power. 2304feccc30dSAlan Stern * When the hub driver is resumed it will take notice and carry out 2305feccc30dSAlan Stern * power-session recovery for all the "USB-PERSIST"-enabled child devices; 2306feccc30dSAlan Stern * the others will be disconnected. 230754515fe5SAlan Stern */ 230854515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev) 230954515fe5SAlan Stern { 231054515fe5SAlan Stern dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); 231154515fe5SAlan Stern rhdev->reset_resume = 1; 231254515fe5SAlan Stern } 231354515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 231454515fe5SAlan Stern 2315d388dab7SAlan Stern #else /* CONFIG_PM */ 2316d388dab7SAlan Stern 2317d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev) 2318d388dab7SAlan Stern { 2319d388dab7SAlan Stern return 0; 2320d388dab7SAlan Stern } 2321d388dab7SAlan Stern 2322511366daSAndrew Morton #define hub_suspend NULL 2323511366daSAndrew Morton #define hub_resume NULL 2324f07600cfSAlan Stern #define hub_reset_resume NULL 2325d388dab7SAlan Stern #endif 2326d388dab7SAlan Stern 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 23291da177e4SLinus Torvalds * 23301da177e4SLinus Torvalds * Between connect detection and reset signaling there must be a delay 23311da177e4SLinus Torvalds * of 100ms at least for debounce and power-settling. The corresponding 23321da177e4SLinus Torvalds * timer shall restart whenever the downstream port detects a disconnect. 23331da177e4SLinus Torvalds * 23341da177e4SLinus Torvalds * Apparently there are some bluetooth and irda-dongles and a number of 23351da177e4SLinus Torvalds * low-speed devices for which this debounce period may last over a second. 23361da177e4SLinus Torvalds * Not covered by the spec - but easy to deal with. 23371da177e4SLinus Torvalds * 23381da177e4SLinus Torvalds * This implementation uses a 1500ms total debounce timeout; if the 23391da177e4SLinus Torvalds * connection isn't stable by then it returns -ETIMEDOUT. It checks 23401da177e4SLinus Torvalds * every 25ms for transient disconnects. When the port status has been 23411da177e4SLinus Torvalds * unchanged for 100ms it returns the port status. 23421da177e4SLinus Torvalds */ 23431da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1) 23441da177e4SLinus Torvalds { 23451da177e4SLinus Torvalds int ret; 23461da177e4SLinus Torvalds int total_time, stable_time = 0; 23471da177e4SLinus Torvalds u16 portchange, portstatus; 23481da177e4SLinus Torvalds unsigned connection = 0xffff; 23491da177e4SLinus Torvalds 23501da177e4SLinus Torvalds for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 23511da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 23521da177e4SLinus Torvalds if (ret < 0) 23531da177e4SLinus Torvalds return ret; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 23561da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 23571da177e4SLinus Torvalds stable_time += HUB_DEBOUNCE_STEP; 23581da177e4SLinus Torvalds if (stable_time >= HUB_DEBOUNCE_STABLE) 23591da177e4SLinus Torvalds break; 23601da177e4SLinus Torvalds } else { 23611da177e4SLinus Torvalds stable_time = 0; 23621da177e4SLinus Torvalds connection = portstatus & USB_PORT_STAT_CONNECTION; 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 23661da177e4SLinus Torvalds clear_port_feature(hub->hdev, port1, 23671da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds if (total_time >= HUB_DEBOUNCE_TIMEOUT) 23711da177e4SLinus Torvalds break; 23721da177e4SLinus Torvalds msleep(HUB_DEBOUNCE_STEP); 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds dev_dbg (hub->intfdev, 23761da177e4SLinus Torvalds "debounce: port %d: total %dms stable %dms status 0x%x\n", 23771da177e4SLinus Torvalds port1, total_time, stable_time, portstatus); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds if (stable_time < HUB_DEBOUNCE_STABLE) 23801da177e4SLinus Torvalds return -ETIMEDOUT; 23811da177e4SLinus Torvalds return portstatus; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 2384fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev) 23851da177e4SLinus Torvalds { 2386ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 2387ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 23882caf7fcdSAlan Stern usb_enable_endpoint(udev, &udev->ep0, true); 23891da177e4SLinus Torvalds } 2390fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit); 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 23931da177e4SLinus Torvalds #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 23941da177e4SLinus Torvalds 23954326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum) 23961da177e4SLinus Torvalds { 23971da177e4SLinus Torvalds int retval; 23981da177e4SLinus Torvalds 23994326ed0bSAlan Stern if (devnum <= 1) 24001da177e4SLinus Torvalds return -EINVAL; 24011da177e4SLinus Torvalds if (udev->state == USB_STATE_ADDRESS) 24021da177e4SLinus Torvalds return 0; 24031da177e4SLinus Torvalds if (udev->state != USB_STATE_DEFAULT) 24041da177e4SLinus Torvalds return -EINVAL; 24051da177e4SLinus Torvalds retval = usb_control_msg(udev, usb_sndaddr0pipe(), 24064326ed0bSAlan Stern USB_REQ_SET_ADDRESS, 0, devnum, 0, 24071da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 24081da177e4SLinus Torvalds if (retval == 0) { 24094953d141SDavid Vrabel /* Device now using proper address. */ 24104953d141SDavid Vrabel update_address(udev, devnum); 24111da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_ADDRESS); 2412fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds return retval; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor. 24181da177e4SLinus Torvalds * Device connection must be stable, no more debouncing needed. 24191da177e4SLinus Torvalds * Returns device in USB_STATE_ADDRESS, except on error. 24201da177e4SLinus Torvalds * 24211da177e4SLinus Torvalds * If this is called for an already-existing device (as part of 2422742120c6SMing Lei * usb_reset_and_verify_device), the caller must own the device lock. For a 24231da177e4SLinus Torvalds * newly detected device that is not accessible through any global 24241da177e4SLinus Torvalds * pointers, it's not necessary to lock the device. 24251da177e4SLinus Torvalds */ 24261da177e4SLinus Torvalds static int 24271da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 24281da177e4SLinus Torvalds int retry_counter) 24291da177e4SLinus Torvalds { 24304186ecf8SArjan van de Ven static DEFINE_MUTEX(usb_address0_mutex); 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 24331da177e4SLinus Torvalds int i, j, retval; 24341da177e4SLinus Torvalds unsigned delay = HUB_SHORT_RESET_TIME; 24351da177e4SLinus Torvalds enum usb_device_speed oldspeed = udev->speed; 243683a07196SInaky Perez-Gonzalez char *speed, *type; 24374326ed0bSAlan Stern int devnum = udev->devnum; 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds /* root hub ports have a slightly longer reset period 24401da177e4SLinus Torvalds * (from USB 2.0 spec, section 7.1.7.5) 24411da177e4SLinus Torvalds */ 24421da177e4SLinus Torvalds if (!hdev->parent) { 24431da177e4SLinus Torvalds delay = HUB_ROOT_RESET_TIME; 24441da177e4SLinus Torvalds if (port1 == hdev->bus->otg_port) 24451da177e4SLinus Torvalds hdev->bus->b_hnp_enable = 0; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds /* Some low speed devices have problems with the quick delay, so */ 24491da177e4SLinus Torvalds /* be a bit pessimistic with those devices. RHbug #23670 */ 24501da177e4SLinus Torvalds if (oldspeed == USB_SPEED_LOW) 24511da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 24521da177e4SLinus Torvalds 24534186ecf8SArjan van de Ven mutex_lock(&usb_address0_mutex); 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds /* Reset the device; full speed may morph to high speed */ 24561da177e4SLinus Torvalds retval = hub_port_reset(hub, port1, udev, delay); 24571da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 24581da177e4SLinus Torvalds goto fail; 24591da177e4SLinus Torvalds /* success, speed is known */ 24601da177e4SLinus Torvalds retval = -ENODEV; 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 24631da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset changed speed!\n"); 24641da177e4SLinus Torvalds goto fail; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds oldspeed = udev->speed; 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 24691da177e4SLinus Torvalds * it's fixed size except for full speed devices. 24705bb6e0aeSInaky Perez-Gonzalez * For Wireless USB devices, ep0 max packet is always 512 (tho 24715bb6e0aeSInaky Perez-Gonzalez * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 24721da177e4SLinus Torvalds */ 24731da177e4SLinus Torvalds switch (udev->speed) { 24745bb6e0aeSInaky Perez-Gonzalez case USB_SPEED_VARIABLE: /* fixed at 512 */ 2475551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 24765bb6e0aeSInaky Perez-Gonzalez break; 24771da177e4SLinus Torvalds case USB_SPEED_HIGH: /* fixed at 64 */ 2478551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 24791da177e4SLinus Torvalds break; 24801da177e4SLinus Torvalds case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 24811da177e4SLinus Torvalds /* to determine the ep0 maxpacket size, try to read 24821da177e4SLinus Torvalds * the device descriptor to get bMaxPacketSize0 and 24831da177e4SLinus Torvalds * then correct our initial guess. 24841da177e4SLinus Torvalds */ 2485551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 24861da177e4SLinus Torvalds break; 24871da177e4SLinus Torvalds case USB_SPEED_LOW: /* fixed at 8 */ 2488551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 24891da177e4SLinus Torvalds break; 24901da177e4SLinus Torvalds default: 24911da177e4SLinus Torvalds goto fail; 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds 249483a07196SInaky Perez-Gonzalez type = ""; 249583a07196SInaky Perez-Gonzalez switch (udev->speed) { 24961da177e4SLinus Torvalds case USB_SPEED_LOW: speed = "low"; break; 24971da177e4SLinus Torvalds case USB_SPEED_FULL: speed = "full"; break; 24981da177e4SLinus Torvalds case USB_SPEED_HIGH: speed = "high"; break; 249983a07196SInaky Perez-Gonzalez case USB_SPEED_VARIABLE: 250083a07196SInaky Perez-Gonzalez speed = "variable"; 250183a07196SInaky Perez-Gonzalez type = "Wireless "; 250283a07196SInaky Perez-Gonzalez break; 25031da177e4SLinus Torvalds default: speed = "?"; break; 250483a07196SInaky Perez-Gonzalez } 250583a07196SInaky Perez-Gonzalez dev_info (&udev->dev, 250683a07196SInaky Perez-Gonzalez "%s %s speed %sUSB device using %s and address %d\n", 250783a07196SInaky Perez-Gonzalez (udev->config) ? "reset" : "new", speed, type, 25084326ed0bSAlan Stern udev->bus->controller->driver->name, devnum); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds /* Set up TT records, if needed */ 25111da177e4SLinus Torvalds if (hdev->tt) { 25121da177e4SLinus Torvalds udev->tt = hdev->tt; 25131da177e4SLinus Torvalds udev->ttport = hdev->ttport; 25141da177e4SLinus Torvalds } else if (udev->speed != USB_SPEED_HIGH 25151da177e4SLinus Torvalds && hdev->speed == USB_SPEED_HIGH) { 25161da177e4SLinus Torvalds udev->tt = &hub->tt; 25171da177e4SLinus Torvalds udev->ttport = port1; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 25211da177e4SLinus Torvalds * Because device hardware and firmware is sometimes buggy in 25221da177e4SLinus Torvalds * this area, and this is how Linux has done it for ages. 25231da177e4SLinus Torvalds * Change it cautiously. 25241da177e4SLinus Torvalds * 25251da177e4SLinus Torvalds * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 25261da177e4SLinus Torvalds * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 25271da177e4SLinus Torvalds * so it may help with some non-standards-compliant devices. 25281da177e4SLinus Torvalds * Otherwise we start with SET_ADDRESS and then try to read the 25291da177e4SLinus Torvalds * first 8 bytes of the device descriptor to get the ep0 maxpacket 25301da177e4SLinus Torvalds * value. 25311da177e4SLinus Torvalds */ 25321da177e4SLinus Torvalds for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 25331da177e4SLinus Torvalds if (USE_NEW_SCHEME(retry_counter)) { 25341da177e4SLinus Torvalds struct usb_device_descriptor *buf; 25351da177e4SLinus Torvalds int r = 0; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE 64 25381da177e4SLinus Torvalds buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 25391da177e4SLinus Torvalds if (!buf) { 25401da177e4SLinus Torvalds retval = -ENOMEM; 25411da177e4SLinus Torvalds continue; 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 2544b89ee19aSAlan Stern /* Retry on all errors; some devices are flakey. 2545b89ee19aSAlan Stern * 255 is for WUSB devices, we actually need to use 2546b89ee19aSAlan Stern * 512 (WUSB1.0[4.8.1]). 25471da177e4SLinus Torvalds */ 25481da177e4SLinus Torvalds for (j = 0; j < 3; ++j) { 25491da177e4SLinus Torvalds buf->bMaxPacketSize0 = 0; 25501da177e4SLinus Torvalds r = usb_control_msg(udev, usb_rcvaddr0pipe(), 25511da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 25521da177e4SLinus Torvalds USB_DT_DEVICE << 8, 0, 25531da177e4SLinus Torvalds buf, GET_DESCRIPTOR_BUFSIZE, 2554fd7c519dSJaroslav Kysela initial_descriptor_timeout); 25551da177e4SLinus Torvalds switch (buf->bMaxPacketSize0) { 25565bb6e0aeSInaky Perez-Gonzalez case 8: case 16: case 32: case 64: case 255: 25571da177e4SLinus Torvalds if (buf->bDescriptorType == 25581da177e4SLinus Torvalds USB_DT_DEVICE) { 25591da177e4SLinus Torvalds r = 0; 25601da177e4SLinus Torvalds break; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds /* FALL THROUGH */ 25631da177e4SLinus Torvalds default: 25641da177e4SLinus Torvalds if (r == 0) 25651da177e4SLinus Torvalds r = -EPROTO; 25661da177e4SLinus Torvalds break; 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds if (r == 0) 25691da177e4SLinus Torvalds break; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds udev->descriptor.bMaxPacketSize0 = 25721da177e4SLinus Torvalds buf->bMaxPacketSize0; 25731da177e4SLinus Torvalds kfree(buf); 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds retval = hub_port_reset(hub, port1, udev, delay); 25761da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 25771da177e4SLinus Torvalds goto fail; 25781da177e4SLinus Torvalds if (oldspeed != udev->speed) { 25791da177e4SLinus Torvalds dev_dbg(&udev->dev, 25801da177e4SLinus Torvalds "device reset changed speed!\n"); 25811da177e4SLinus Torvalds retval = -ENODEV; 25821da177e4SLinus Torvalds goto fail; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds if (r) { 2585b9cef6c3SWu Fengguang dev_err(&udev->dev, 2586b9cef6c3SWu Fengguang "device descriptor read/64, error %d\n", 2587b9cef6c3SWu Fengguang r); 25881da177e4SLinus Torvalds retval = -EMSGSIZE; 25891da177e4SLinus Torvalds continue; 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 25946c529cdcSInaky Perez-Gonzalez /* 25956c529cdcSInaky Perez-Gonzalez * If device is WUSB, we already assigned an 25966c529cdcSInaky Perez-Gonzalez * unauthorized address in the Connect Ack sequence; 25976c529cdcSInaky Perez-Gonzalez * authorization will assign the final address. 25986c529cdcSInaky Perez-Gonzalez */ 25996c529cdcSInaky Perez-Gonzalez if (udev->wusb == 0) { 26001da177e4SLinus Torvalds for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 26014326ed0bSAlan Stern retval = hub_set_address(udev, devnum); 26021da177e4SLinus Torvalds if (retval >= 0) 26031da177e4SLinus Torvalds break; 26041da177e4SLinus Torvalds msleep(200); 26051da177e4SLinus Torvalds } 26061da177e4SLinus Torvalds if (retval < 0) { 26071da177e4SLinus Torvalds dev_err(&udev->dev, 26081da177e4SLinus Torvalds "device not accepting address %d, error %d\n", 26094326ed0bSAlan Stern devnum, retval); 26101da177e4SLinus Torvalds goto fail; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds /* cope with hardware quirkiness: 26141da177e4SLinus Torvalds * - let SET_ADDRESS settle, some device hardware wants it 26151da177e4SLinus Torvalds * - read ep0 maxpacket even for high and low speed, 26161da177e4SLinus Torvalds */ 26171da177e4SLinus Torvalds msleep(10); 26181da177e4SLinus Torvalds if (USE_NEW_SCHEME(retry_counter)) 26191da177e4SLinus Torvalds break; 26206c529cdcSInaky Perez-Gonzalez } 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, 8); 26231da177e4SLinus Torvalds if (retval < 8) { 2624b9cef6c3SWu Fengguang dev_err(&udev->dev, 2625b9cef6c3SWu Fengguang "device descriptor read/8, error %d\n", 2626b9cef6c3SWu Fengguang retval); 26271da177e4SLinus Torvalds if (retval >= 0) 26281da177e4SLinus Torvalds retval = -EMSGSIZE; 26291da177e4SLinus Torvalds } else { 26301da177e4SLinus Torvalds retval = 0; 26311da177e4SLinus Torvalds break; 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds if (retval) 26351da177e4SLinus Torvalds goto fail; 26361da177e4SLinus Torvalds 26376c529cdcSInaky Perez-Gonzalez i = udev->descriptor.bMaxPacketSize0 == 0xff? /* wusb device? */ 26385bb6e0aeSInaky Perez-Gonzalez 512 : udev->descriptor.bMaxPacketSize0; 26391da177e4SLinus Torvalds if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) { 26401da177e4SLinus Torvalds if (udev->speed != USB_SPEED_FULL || 26411da177e4SLinus Torvalds !(i == 8 || i == 16 || i == 32 || i == 64)) { 26421da177e4SLinus Torvalds dev_err(&udev->dev, "ep0 maxpacket = %d\n", i); 26431da177e4SLinus Torvalds retval = -EMSGSIZE; 26441da177e4SLinus Torvalds goto fail; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 26471da177e4SLinus Torvalds udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 2648fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 26521da177e4SLinus Torvalds if (retval < (signed)sizeof(udev->descriptor)) { 2653b9cef6c3SWu Fengguang dev_err(&udev->dev, "device descriptor read/all, error %d\n", 2654b9cef6c3SWu Fengguang retval); 26551da177e4SLinus Torvalds if (retval >= 0) 26561da177e4SLinus Torvalds retval = -ENOMSG; 26571da177e4SLinus Torvalds goto fail; 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds retval = 0; 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds fail: 26634326ed0bSAlan Stern if (retval) { 26641da177e4SLinus Torvalds hub_port_disable(hub, port1, 0); 26654953d141SDavid Vrabel update_address(udev, devnum); /* for disconnect processing */ 26664326ed0bSAlan Stern } 26674186ecf8SArjan van de Ven mutex_unlock(&usb_address0_mutex); 26681da177e4SLinus Torvalds return retval; 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds static void 26721da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) 26731da177e4SLinus Torvalds { 26741da177e4SLinus Torvalds struct usb_qualifier_descriptor *qual; 26751da177e4SLinus Torvalds int status; 26761da177e4SLinus Torvalds 2677e94b1766SChristoph Lameter qual = kmalloc (sizeof *qual, GFP_KERNEL); 26781da177e4SLinus Torvalds if (qual == NULL) 26791da177e4SLinus Torvalds return; 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, 26821da177e4SLinus Torvalds qual, sizeof *qual); 26831da177e4SLinus Torvalds if (status == sizeof *qual) { 26841da177e4SLinus Torvalds dev_info(&udev->dev, "not running at top speed; " 26851da177e4SLinus Torvalds "connect to a high speed hub\n"); 26861da177e4SLinus Torvalds /* hub LEDs are probably harder to miss than syslog */ 26871da177e4SLinus Torvalds if (hub->has_indicators) { 26881da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 2689c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds kfree(qual); 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds static unsigned 26961da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub) 26971da177e4SLinus Torvalds { 26981da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 26991da177e4SLinus Torvalds int remaining; 270055c52718SAlan Stern int port1; 27011da177e4SLinus Torvalds 270255c52718SAlan Stern if (!hub->limited_power) 27031da177e4SLinus Torvalds return 0; 27041da177e4SLinus Torvalds 270555c52718SAlan Stern remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 270655c52718SAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 270755c52718SAlan Stern struct usb_device *udev = hdev->children[port1 - 1]; 270855c52718SAlan Stern int delta; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds if (!udev) 27111da177e4SLinus Torvalds continue; 27121da177e4SLinus Torvalds 271355c52718SAlan Stern /* Unconfigured devices may not use more than 100mA, 271455c52718SAlan Stern * or 8mA for OTG ports */ 27151da177e4SLinus Torvalds if (udev->actconfig) 271655c52718SAlan Stern delta = udev->actconfig->desc.bMaxPower * 2; 271755c52718SAlan Stern else if (port1 != udev->bus->otg_port || hdev->parent) 271855c52718SAlan Stern delta = 100; 27191da177e4SLinus Torvalds else 272055c52718SAlan Stern delta = 8; 272155c52718SAlan Stern if (delta > hub->mA_per_port) 2722b9cef6c3SWu Fengguang dev_warn(&udev->dev, 2723b9cef6c3SWu Fengguang "%dmA is over %umA budget for port %d!\n", 272455c52718SAlan Stern delta, hub->mA_per_port, port1); 27251da177e4SLinus Torvalds remaining -= delta; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds if (remaining < 0) { 272855c52718SAlan Stern dev_warn(hub->intfdev, "%dmA over power budget!\n", 272955c52718SAlan Stern - remaining); 27301da177e4SLinus Torvalds remaining = 0; 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds return remaining; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds /* Handle physical or logical connection change events. 27361da177e4SLinus Torvalds * This routine is called when: 27371da177e4SLinus Torvalds * a port connection-change occurs; 27381da177e4SLinus Torvalds * a port enable-change occurs (often caused by EMI); 2739742120c6SMing Lei * usb_reset_and_verify_device() encounters changed descriptors (as from 27401da177e4SLinus Torvalds * a firmware download) 27411da177e4SLinus Torvalds * caller already locked the hub 27421da177e4SLinus Torvalds */ 27431da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1, 27441da177e4SLinus Torvalds u16 portstatus, u16 portchange) 27451da177e4SLinus Torvalds { 27461da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 27471da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 274890da096eSBalaji Rao struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 274924618b0cSAlan Stern unsigned wHubCharacteristics = 275024618b0cSAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 27518808f00cSAlan Stern struct usb_device *udev; 27521da177e4SLinus Torvalds int status, i; 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds dev_dbg (hub_dev, 27551da177e4SLinus Torvalds "port %d, status %04x, change %04x, %s\n", 27561da177e4SLinus Torvalds port1, portstatus, portchange, portspeed (portstatus)); 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds if (hub->has_indicators) { 27591da177e4SLinus Torvalds set_port_led(hub, port1, HUB_LED_AUTO); 27601da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_AUTO; 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 27641da177e4SLinus Torvalds /* during HNP, don't repeat the debounce */ 27651da177e4SLinus Torvalds if (hdev->bus->is_b_host) 276624618b0cSAlan Stern portchange &= ~(USB_PORT_STAT_C_CONNECTION | 276724618b0cSAlan Stern USB_PORT_STAT_C_ENABLE); 27681da177e4SLinus Torvalds #endif 27691da177e4SLinus Torvalds 27708808f00cSAlan Stern /* Try to resuscitate an existing device */ 27718808f00cSAlan Stern udev = hdev->children[port1-1]; 27728808f00cSAlan Stern if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 27738808f00cSAlan Stern udev->state != USB_STATE_NOTATTACHED) { 27748808f00cSAlan Stern usb_lock_device(udev); 27758808f00cSAlan Stern if (portstatus & USB_PORT_STAT_ENABLE) { 27768808f00cSAlan Stern status = 0; /* Nothing to do */ 27778808f00cSAlan Stern 27788808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND 27795257d97aSAlan Stern } else if (udev->state == USB_STATE_SUSPENDED && 27805257d97aSAlan Stern udev->persist_enabled) { 27818808f00cSAlan Stern /* For a suspended device, treat this as a 27828808f00cSAlan Stern * remote wakeup event. 27838808f00cSAlan Stern */ 27848808f00cSAlan Stern if (udev->do_remote_wakeup) 27858808f00cSAlan Stern status = remote_wakeup(udev); 27868808f00cSAlan Stern 27878808f00cSAlan Stern /* Otherwise leave it be; devices can't tell the 27888808f00cSAlan Stern * difference between suspended and disabled. 27898808f00cSAlan Stern */ 27908808f00cSAlan Stern else 27918808f00cSAlan Stern status = 0; 27928808f00cSAlan Stern #endif 27938808f00cSAlan Stern 27948808f00cSAlan Stern } else { 27955257d97aSAlan Stern status = -ENODEV; /* Don't resuscitate */ 27968808f00cSAlan Stern } 27978808f00cSAlan Stern usb_unlock_device(udev); 27988808f00cSAlan Stern 27998808f00cSAlan Stern if (status == 0) { 28008808f00cSAlan Stern clear_bit(port1, hub->change_bits); 28018808f00cSAlan Stern return; 28028808f00cSAlan Stern } 28038808f00cSAlan Stern } 28048808f00cSAlan Stern 280524618b0cSAlan Stern /* Disconnect any existing devices under this port */ 28068808f00cSAlan Stern if (udev) 280724618b0cSAlan Stern usb_disconnect(&hdev->children[port1-1]); 280824618b0cSAlan Stern clear_bit(port1, hub->change_bits); 280924618b0cSAlan Stern 28105257d97aSAlan Stern if (portchange & (USB_PORT_STAT_C_CONNECTION | 28115257d97aSAlan Stern USB_PORT_STAT_C_ENABLE)) { 28125257d97aSAlan Stern status = hub_port_debounce(hub, port1); 28135257d97aSAlan Stern if (status < 0) { 28145257d97aSAlan Stern if (printk_ratelimit()) 28155257d97aSAlan Stern dev_err(hub_dev, "connect-debounce failed, " 28165257d97aSAlan Stern "port %d disabled\n", port1); 28175257d97aSAlan Stern portstatus &= ~USB_PORT_STAT_CONNECTION; 28185257d97aSAlan Stern } else { 28195257d97aSAlan Stern portstatus = status; 28205257d97aSAlan Stern } 28215257d97aSAlan Stern } 28225257d97aSAlan Stern 282324618b0cSAlan Stern /* Return now if debouncing failed or nothing is connected */ 28241da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds /* maybe switch power back on (e.g. root hub was reset) */ 282774ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 28281da177e4SLinus Torvalds && !(portstatus & (1 << USB_PORT_FEAT_POWER))) 28291da177e4SLinus Torvalds set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds if (portstatus & USB_PORT_STAT_ENABLE) 28321da177e4SLinus Torvalds goto done; 28331da177e4SLinus Torvalds return; 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; i++) { 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds /* reallocate for each attempt, since references 28391da177e4SLinus Torvalds * to the previous one can escape in various ways 28401da177e4SLinus Torvalds */ 28411da177e4SLinus Torvalds udev = usb_alloc_dev(hdev, hdev->bus, port1); 28421da177e4SLinus Torvalds if (!udev) { 28431da177e4SLinus Torvalds dev_err (hub_dev, 28441da177e4SLinus Torvalds "couldn't allocate port %d usb_device\n", 28451da177e4SLinus Torvalds port1); 28461da177e4SLinus Torvalds goto done; 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_POWERED); 28501da177e4SLinus Torvalds udev->speed = USB_SPEED_UNKNOWN; 285155c52718SAlan Stern udev->bus_mA = hub->mA_per_port; 2852b6956ffaSAlan Stern udev->level = hdev->level + 1; 28538af548dcSInaky Perez-Gonzalez udev->wusb = hub_is_wusb(hub); 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds /* set the address */ 28561da177e4SLinus Torvalds choose_address(udev); 28571da177e4SLinus Torvalds if (udev->devnum <= 0) { 28581da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */ 28591da177e4SLinus Torvalds goto loop; 28601da177e4SLinus Torvalds } 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds /* reset and get descriptor */ 28631da177e4SLinus Torvalds status = hub_port_init(hub, udev, port1, i); 28641da177e4SLinus Torvalds if (status < 0) 28651da177e4SLinus Torvalds goto loop; 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds /* consecutive bus-powered hubs aren't reliable; they can 28681da177e4SLinus Torvalds * violate the voltage drop budget. if the new child has 28691da177e4SLinus Torvalds * a "powered" LED, users should notice we didn't enable it 28701da177e4SLinus Torvalds * (without reading syslog), even without per-port LEDs 28711da177e4SLinus Torvalds * on the parent. 28721da177e4SLinus Torvalds */ 28731da177e4SLinus Torvalds if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 287455c52718SAlan Stern && udev->bus_mA <= 100) { 28751da177e4SLinus Torvalds u16 devstat; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 28781da177e4SLinus Torvalds &devstat); 287955c52718SAlan Stern if (status < 2) { 28801da177e4SLinus Torvalds dev_dbg(&udev->dev, "get status %d ?\n", status); 28811da177e4SLinus Torvalds goto loop_disable; 28821da177e4SLinus Torvalds } 288355c52718SAlan Stern le16_to_cpus(&devstat); 28841da177e4SLinus Torvalds if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 28851da177e4SLinus Torvalds dev_err(&udev->dev, 28861da177e4SLinus Torvalds "can't connect bus-powered hub " 28871da177e4SLinus Torvalds "to this port\n"); 28881da177e4SLinus Torvalds if (hub->has_indicators) { 28891da177e4SLinus Torvalds hub->indicator[port1-1] = 28901da177e4SLinus Torvalds INDICATOR_AMBER_BLINK; 2891c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */ 28941da177e4SLinus Torvalds goto loop_disable; 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds /* check for devices running slower than they could */ 28991da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 29001da177e4SLinus Torvalds && udev->speed == USB_SPEED_FULL 29011da177e4SLinus Torvalds && highspeed_hubs != 0) 29021da177e4SLinus Torvalds check_highspeed (hub, udev, port1); 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds /* Store the parent's children[] pointer. At this point 29051da177e4SLinus Torvalds * udev becomes globally accessible, although presumably 29061da177e4SLinus Torvalds * no one will look at it until hdev is unlocked. 29071da177e4SLinus Torvalds */ 29081da177e4SLinus Torvalds status = 0; 29091da177e4SLinus Torvalds 29101da177e4SLinus Torvalds /* We mustn't add new devices if the parent hub has 29111da177e4SLinus Torvalds * been disconnected; we would race with the 29121da177e4SLinus Torvalds * recursively_mark_NOTATTACHED() routine. 29131da177e4SLinus Torvalds */ 29141da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 29151da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) 29161da177e4SLinus Torvalds status = -ENOTCONN; 29171da177e4SLinus Torvalds else 29181da177e4SLinus Torvalds hdev->children[port1-1] = udev; 29191da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds /* Run it through the hoops (find a driver, etc) */ 29221da177e4SLinus Torvalds if (!status) { 29231da177e4SLinus Torvalds status = usb_new_device(udev); 29241da177e4SLinus Torvalds if (status) { 29251da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 29261da177e4SLinus Torvalds hdev->children[port1-1] = NULL; 29271da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds } 29301da177e4SLinus Torvalds 29311da177e4SLinus Torvalds if (status) 29321da177e4SLinus Torvalds goto loop_disable; 29331da177e4SLinus Torvalds 29341da177e4SLinus Torvalds status = hub_power_remaining(hub); 29351da177e4SLinus Torvalds if (status) 293655c52718SAlan Stern dev_dbg(hub_dev, "%dmA power budget left\n", status); 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds return; 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds loop_disable: 29411da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 29421da177e4SLinus Torvalds loop: 2943fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 29441da177e4SLinus Torvalds release_address(udev); 29451da177e4SLinus Torvalds usb_put_dev(udev); 2946ffcdc18dSVikram Pandita if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 29471da177e4SLinus Torvalds break; 29481da177e4SLinus Torvalds } 29493a31155cSAlan Stern if (hub->hdev->parent || 29503a31155cSAlan Stern !hcd->driver->port_handed_over || 29513a31155cSAlan Stern !(hcd->driver->port_handed_over)(hcd, port1)) 29523a31155cSAlan Stern dev_err(hub_dev, "unable to enumerate USB device on port %d\n", 29533a31155cSAlan Stern port1); 29541da177e4SLinus Torvalds 29551da177e4SLinus Torvalds done: 29561da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 295790da096eSBalaji Rao if (hcd->driver->relinquish_port && !hub->hdev->parent) 295890da096eSBalaji Rao hcd->driver->relinquish_port(hcd, port1); 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds static void hub_events(void) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds struct list_head *tmp; 29641da177e4SLinus Torvalds struct usb_device *hdev; 29651da177e4SLinus Torvalds struct usb_interface *intf; 29661da177e4SLinus Torvalds struct usb_hub *hub; 29671da177e4SLinus Torvalds struct device *hub_dev; 29681da177e4SLinus Torvalds u16 hubstatus; 29691da177e4SLinus Torvalds u16 hubchange; 29701da177e4SLinus Torvalds u16 portstatus; 29711da177e4SLinus Torvalds u16 portchange; 29721da177e4SLinus Torvalds int i, ret; 29731da177e4SLinus Torvalds int connect_change; 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds /* 29761da177e4SLinus Torvalds * We restart the list every time to avoid a deadlock with 29771da177e4SLinus Torvalds * deleting hubs downstream from this one. This should be 29781da177e4SLinus Torvalds * safe since we delete the hub from the event list. 29791da177e4SLinus Torvalds * Not the most efficient, but avoids deadlocks. 29801da177e4SLinus Torvalds */ 29811da177e4SLinus Torvalds while (1) { 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds /* Grab the first entry at the beginning of the list */ 29841da177e4SLinus Torvalds spin_lock_irq(&hub_event_lock); 29851da177e4SLinus Torvalds if (list_empty(&hub_event_list)) { 29861da177e4SLinus Torvalds spin_unlock_irq(&hub_event_lock); 29871da177e4SLinus Torvalds break; 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds 29901da177e4SLinus Torvalds tmp = hub_event_list.next; 29911da177e4SLinus Torvalds list_del_init(tmp); 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds hub = list_entry(tmp, struct usb_hub, event_list); 2994e8054854SAlan Stern kref_get(&hub->kref); 2995e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 29961da177e4SLinus Torvalds 2997e8054854SAlan Stern hdev = hub->hdev; 2998e8054854SAlan Stern hub_dev = hub->intfdev; 2999e8054854SAlan Stern intf = to_usb_interface(hub_dev); 300040f122f3SAlan Stern dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 30011da177e4SLinus Torvalds hdev->state, hub->descriptor 30021da177e4SLinus Torvalds ? hub->descriptor->bNbrPorts 30031da177e4SLinus Torvalds : 0, 30041da177e4SLinus Torvalds /* NOTE: expects max 15 ports... */ 30051da177e4SLinus Torvalds (u16) hub->change_bits[0], 300640f122f3SAlan Stern (u16) hub->event_bits[0]); 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds /* Lock the device, then check to see if we were 30091da177e4SLinus Torvalds * disconnected while waiting for the lock to succeed. */ 301006b84e8aSAlan Stern usb_lock_device(hdev); 3011e8054854SAlan Stern if (unlikely(hub->disconnected)) 30121da177e4SLinus Torvalds goto loop; 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds /* If the hub has died, clean up after it */ 30151da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) { 30167de18d8bSAlan Stern hub->error = -ENODEV; 30174330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 30181da177e4SLinus Torvalds goto loop; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 302140f122f3SAlan Stern /* Autoresume */ 302240f122f3SAlan Stern ret = usb_autopm_get_interface(intf); 302340f122f3SAlan Stern if (ret) { 302440f122f3SAlan Stern dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 30251da177e4SLinus Torvalds goto loop; 302640f122f3SAlan Stern } 302740f122f3SAlan Stern 302840f122f3SAlan Stern /* If this is an inactive hub, do nothing */ 302940f122f3SAlan Stern if (hub->quiescing) 303040f122f3SAlan Stern goto loop_autopm; 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds if (hub->error) { 30331da177e4SLinus Torvalds dev_dbg (hub_dev, "resetting for error %d\n", 30341da177e4SLinus Torvalds hub->error); 30351da177e4SLinus Torvalds 3036742120c6SMing Lei ret = usb_reset_device(hdev); 30371da177e4SLinus Torvalds if (ret) { 30381da177e4SLinus Torvalds dev_dbg (hub_dev, 30391da177e4SLinus Torvalds "error resetting hub: %d\n", ret); 304040f122f3SAlan Stern goto loop_autopm; 30411da177e4SLinus Torvalds } 30421da177e4SLinus Torvalds 30431da177e4SLinus Torvalds hub->nerrors = 0; 30441da177e4SLinus Torvalds hub->error = 0; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds /* deal with port status changes */ 30481da177e4SLinus Torvalds for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { 30491da177e4SLinus Torvalds if (test_bit(i, hub->busy_bits)) 30501da177e4SLinus Torvalds continue; 30511da177e4SLinus Torvalds connect_change = test_bit(i, hub->change_bits); 30521da177e4SLinus Torvalds if (!test_and_clear_bit(i, hub->event_bits) && 30536ee0b270SAlan Stern !connect_change) 30541da177e4SLinus Torvalds continue; 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds ret = hub_port_status(hub, i, 30571da177e4SLinus Torvalds &portstatus, &portchange); 30581da177e4SLinus Torvalds if (ret < 0) 30591da177e4SLinus Torvalds continue; 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 30621da177e4SLinus Torvalds clear_port_feature(hdev, i, 30631da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 30641da177e4SLinus Torvalds connect_change = 1; 30651da177e4SLinus Torvalds } 30661da177e4SLinus Torvalds 30671da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_ENABLE) { 30681da177e4SLinus Torvalds if (!connect_change) 30691da177e4SLinus Torvalds dev_dbg (hub_dev, 30701da177e4SLinus Torvalds "port %d enable change, " 30711da177e4SLinus Torvalds "status %08x\n", 30721da177e4SLinus Torvalds i, portstatus); 30731da177e4SLinus Torvalds clear_port_feature(hdev, i, 30741da177e4SLinus Torvalds USB_PORT_FEAT_C_ENABLE); 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds /* 30771da177e4SLinus Torvalds * EM interference sometimes causes badly 30781da177e4SLinus Torvalds * shielded USB devices to be shutdown by 30791da177e4SLinus Torvalds * the hub, this hack enables them again. 30801da177e4SLinus Torvalds * Works at least with mouse driver. 30811da177e4SLinus Torvalds */ 30821da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_ENABLE) 30831da177e4SLinus Torvalds && !connect_change 30841da177e4SLinus Torvalds && hdev->children[i-1]) { 30851da177e4SLinus Torvalds dev_err (hub_dev, 30861da177e4SLinus Torvalds "port %i " 30871da177e4SLinus Torvalds "disabled by hub (EMI?), " 30881da177e4SLinus Torvalds "re-enabling...\n", 30891da177e4SLinus Torvalds i); 30901da177e4SLinus Torvalds connect_change = 1; 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds 30941da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_SUSPEND) { 30958808f00cSAlan Stern struct usb_device *udev; 30968808f00cSAlan Stern 30971da177e4SLinus Torvalds clear_port_feature(hdev, i, 30981da177e4SLinus Torvalds USB_PORT_FEAT_C_SUSPEND); 30998808f00cSAlan Stern udev = hdev->children[i-1]; 31008808f00cSAlan Stern if (udev) { 31018808f00cSAlan Stern usb_lock_device(udev); 31021da177e4SLinus Torvalds ret = remote_wakeup(hdev-> 31031da177e4SLinus Torvalds children[i-1]); 31048808f00cSAlan Stern usb_unlock_device(udev); 31051da177e4SLinus Torvalds if (ret < 0) 31061da177e4SLinus Torvalds connect_change = 1; 31071da177e4SLinus Torvalds } else { 31081da177e4SLinus Torvalds ret = -ENODEV; 31091da177e4SLinus Torvalds hub_port_disable(hub, i, 1); 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds dev_dbg (hub_dev, 31121da177e4SLinus Torvalds "resume on port %d, status %d\n", 31131da177e4SLinus Torvalds i, ret); 31141da177e4SLinus Torvalds } 31151da177e4SLinus Torvalds 31161da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 31171da177e4SLinus Torvalds dev_err (hub_dev, 31181da177e4SLinus Torvalds "over-current change on port %d\n", 31191da177e4SLinus Torvalds i); 31201da177e4SLinus Torvalds clear_port_feature(hdev, i, 31211da177e4SLinus Torvalds USB_PORT_FEAT_C_OVER_CURRENT); 31228520f380SAlan Stern hub_power_on(hub, true); 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_RESET) { 31261da177e4SLinus Torvalds dev_dbg (hub_dev, 31271da177e4SLinus Torvalds "reset change on port %d\n", 31281da177e4SLinus Torvalds i); 31291da177e4SLinus Torvalds clear_port_feature(hdev, i, 31301da177e4SLinus Torvalds USB_PORT_FEAT_C_RESET); 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds if (connect_change) 31341da177e4SLinus Torvalds hub_port_connect_change(hub, i, 31351da177e4SLinus Torvalds portstatus, portchange); 31361da177e4SLinus Torvalds } /* end for i */ 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds /* deal with hub status changes */ 31391da177e4SLinus Torvalds if (test_and_clear_bit(0, hub->event_bits) == 0) 31401da177e4SLinus Torvalds ; /* do nothing */ 31411da177e4SLinus Torvalds else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 31421da177e4SLinus Torvalds dev_err (hub_dev, "get_hub_status failed\n"); 31431da177e4SLinus Torvalds else { 31441da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_LOCAL_POWER) { 31451da177e4SLinus Torvalds dev_dbg (hub_dev, "power change\n"); 31461da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 314755c52718SAlan Stern if (hubstatus & HUB_STATUS_LOCAL_POWER) 314855c52718SAlan Stern /* FIXME: Is this always true? */ 314955c52718SAlan Stern hub->limited_power = 1; 3150403fae78Sjidong xiao else 3151403fae78Sjidong xiao hub->limited_power = 0; 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_OVERCURRENT) { 31541da177e4SLinus Torvalds dev_dbg (hub_dev, "overcurrent change\n"); 31551da177e4SLinus Torvalds msleep(500); /* Cool down */ 31561da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 31578520f380SAlan Stern hub_power_on(hub, true); 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 316140f122f3SAlan Stern loop_autopm: 316240f122f3SAlan Stern /* Allow autosuspend if we're not going to run again */ 316340f122f3SAlan Stern if (list_empty(&hub->event_list)) 316440f122f3SAlan Stern usb_autopm_enable(intf); 31651da177e4SLinus Torvalds loop: 31661da177e4SLinus Torvalds usb_unlock_device(hdev); 3167e8054854SAlan Stern kref_put(&hub->kref, hub_release); 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds } /* end while (1) */ 31701da177e4SLinus Torvalds } 31711da177e4SLinus Torvalds 31721da177e4SLinus Torvalds static int hub_thread(void *__unused) 31731da177e4SLinus Torvalds { 31743bb1af52SAlan Stern /* khubd needs to be freezable to avoid intefering with USB-PERSIST 31753bb1af52SAlan Stern * port handover. Otherwise it might see that a full-speed device 31763bb1af52SAlan Stern * was gone before the EHCI controller had handed its port over to 31773bb1af52SAlan Stern * the companion full-speed controller. 31783bb1af52SAlan Stern */ 317983144186SRafael J. Wysocki set_freezable(); 31803bb1af52SAlan Stern 31811da177e4SLinus Torvalds do { 31821da177e4SLinus Torvalds hub_events(); 3183e42837bcSRafael J. Wysocki wait_event_freezable(khubd_wait, 31849c8d6178Sakpm@osdl.org !list_empty(&hub_event_list) || 31859c8d6178Sakpm@osdl.org kthread_should_stop()); 31869c8d6178Sakpm@osdl.org } while (!kthread_should_stop() || !list_empty(&hub_event_list)); 31871da177e4SLinus Torvalds 31881da177e4SLinus Torvalds pr_debug("%s: khubd exiting\n", usbcore_name); 31899c8d6178Sakpm@osdl.org return 0; 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 31921da177e4SLinus Torvalds static struct usb_device_id hub_id_table [] = { 31931da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 31941da177e4SLinus Torvalds .bDeviceClass = USB_CLASS_HUB}, 31951da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 31961da177e4SLinus Torvalds .bInterfaceClass = USB_CLASS_HUB}, 31971da177e4SLinus Torvalds { } /* Terminating entry */ 31981da177e4SLinus Torvalds }; 31991da177e4SLinus Torvalds 32001da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table); 32011da177e4SLinus Torvalds 32021da177e4SLinus Torvalds static struct usb_driver hub_driver = { 32031da177e4SLinus Torvalds .name = "hub", 32041da177e4SLinus Torvalds .probe = hub_probe, 32051da177e4SLinus Torvalds .disconnect = hub_disconnect, 32061da177e4SLinus Torvalds .suspend = hub_suspend, 32071da177e4SLinus Torvalds .resume = hub_resume, 3208f07600cfSAlan Stern .reset_resume = hub_reset_resume, 32097de18d8bSAlan Stern .pre_reset = hub_pre_reset, 32107de18d8bSAlan Stern .post_reset = hub_post_reset, 32111da177e4SLinus Torvalds .ioctl = hub_ioctl, 32121da177e4SLinus Torvalds .id_table = hub_id_table, 321340f122f3SAlan Stern .supports_autosuspend = 1, 32141da177e4SLinus Torvalds }; 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds int usb_hub_init(void) 32171da177e4SLinus Torvalds { 32181da177e4SLinus Torvalds if (usb_register(&hub_driver) < 0) { 32191da177e4SLinus Torvalds printk(KERN_ERR "%s: can't register hub driver\n", 32201da177e4SLinus Torvalds usbcore_name); 32211da177e4SLinus Torvalds return -1; 32221da177e4SLinus Torvalds } 32231da177e4SLinus Torvalds 32249c8d6178Sakpm@osdl.org khubd_task = kthread_run(hub_thread, NULL, "khubd"); 32259c8d6178Sakpm@osdl.org if (!IS_ERR(khubd_task)) 32261da177e4SLinus Torvalds return 0; 32271da177e4SLinus Torvalds 32281da177e4SLinus Torvalds /* Fall through if kernel_thread failed */ 32291da177e4SLinus Torvalds usb_deregister(&hub_driver); 32301da177e4SLinus Torvalds printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds return -1; 32331da177e4SLinus Torvalds } 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds void usb_hub_cleanup(void) 32361da177e4SLinus Torvalds { 32379c8d6178Sakpm@osdl.org kthread_stop(khubd_task); 32381da177e4SLinus Torvalds 32391da177e4SLinus Torvalds /* 32401da177e4SLinus Torvalds * Hub resources are freed for us by usb_deregister. It calls 32411da177e4SLinus Torvalds * usb_driver_purge on every device which in turn calls that 32421da177e4SLinus Torvalds * devices disconnect function if it is using this driver. 32431da177e4SLinus Torvalds * The hub_disconnect function takes care of releasing the 32441da177e4SLinus Torvalds * individual hub resources. -greg 32451da177e4SLinus Torvalds */ 32461da177e4SLinus Torvalds usb_deregister(&hub_driver); 32471da177e4SLinus Torvalds } /* usb_hub_cleanup() */ 32481da177e4SLinus Torvalds 3249eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev, 3250eb764c4bSAlan Stern struct usb_device_descriptor *old_device_descriptor) 32511da177e4SLinus Torvalds { 3252eb764c4bSAlan Stern int changed = 0; 32531da177e4SLinus Torvalds unsigned index; 3254eb764c4bSAlan Stern unsigned serial_len = 0; 3255eb764c4bSAlan Stern unsigned len; 3256eb764c4bSAlan Stern unsigned old_length; 3257eb764c4bSAlan Stern int length; 3258eb764c4bSAlan Stern char *buf; 32591da177e4SLinus Torvalds 3260eb764c4bSAlan Stern if (memcmp(&udev->descriptor, old_device_descriptor, 3261eb764c4bSAlan Stern sizeof(*old_device_descriptor)) != 0) 3262eb764c4bSAlan Stern return 1; 3263eb764c4bSAlan Stern 3264eb764c4bSAlan Stern /* Since the idVendor, idProduct, and bcdDevice values in the 3265eb764c4bSAlan Stern * device descriptor haven't changed, we will assume the 3266eb764c4bSAlan Stern * Manufacturer and Product strings haven't changed either. 3267eb764c4bSAlan Stern * But the SerialNumber string could be different (e.g., a 3268eb764c4bSAlan Stern * different flash card of the same brand). 3269eb764c4bSAlan Stern */ 3270eb764c4bSAlan Stern if (udev->serial) 3271eb764c4bSAlan Stern serial_len = strlen(udev->serial) + 1; 3272eb764c4bSAlan Stern 3273eb764c4bSAlan Stern len = serial_len; 32741da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 3275eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 3276eb764c4bSAlan Stern len = max(len, old_length); 32771da177e4SLinus Torvalds } 3278eb764c4bSAlan Stern 32790cc1a51fSOliver Neukum buf = kmalloc(len, GFP_NOIO); 32801da177e4SLinus Torvalds if (buf == NULL) { 32811da177e4SLinus Torvalds dev_err(&udev->dev, "no mem to re-read configs after reset\n"); 32821da177e4SLinus Torvalds /* assume the worst */ 32831da177e4SLinus Torvalds return 1; 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 3286eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 32871da177e4SLinus Torvalds length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 32881da177e4SLinus Torvalds old_length); 3289eb764c4bSAlan Stern if (length != old_length) { 32901da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d, error %d\n", 32911da177e4SLinus Torvalds index, length); 3292eb764c4bSAlan Stern changed = 1; 32931da177e4SLinus Torvalds break; 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds if (memcmp (buf, udev->rawdescriptors[index], old_length) 32961da177e4SLinus Torvalds != 0) { 32971da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 3298eb764c4bSAlan Stern index, 3299eb764c4bSAlan Stern ((struct usb_config_descriptor *) buf)-> 3300eb764c4bSAlan Stern bConfigurationValue); 3301eb764c4bSAlan Stern changed = 1; 33021da177e4SLinus Torvalds break; 33031da177e4SLinus Torvalds } 33041da177e4SLinus Torvalds } 3305eb764c4bSAlan Stern 3306eb764c4bSAlan Stern if (!changed && serial_len) { 3307eb764c4bSAlan Stern length = usb_string(udev, udev->descriptor.iSerialNumber, 3308eb764c4bSAlan Stern buf, serial_len); 3309eb764c4bSAlan Stern if (length + 1 != serial_len) { 3310eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string error %d\n", 3311eb764c4bSAlan Stern length); 3312eb764c4bSAlan Stern changed = 1; 3313eb764c4bSAlan Stern } else if (memcmp(buf, udev->serial, length) != 0) { 3314eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string changed\n"); 3315eb764c4bSAlan Stern changed = 1; 3316eb764c4bSAlan Stern } 3317eb764c4bSAlan Stern } 3318eb764c4bSAlan Stern 33191da177e4SLinus Torvalds kfree(buf); 3320eb764c4bSAlan Stern return changed; 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 33231da177e4SLinus Torvalds /** 3324742120c6SMing Lei * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 33251da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 33261da177e4SLinus Torvalds * 332779efa097SAlan Stern * WARNING - don't use this routine to reset a composite device 332879efa097SAlan Stern * (one with multiple interfaces owned by separate drivers)! 3329742120c6SMing Lei * Use usb_reset_device() instead. 33301da177e4SLinus Torvalds * 33311da177e4SLinus Torvalds * Do a port reset, reassign the device's address, and establish its 33321da177e4SLinus Torvalds * former operating configuration. If the reset fails, or the device's 33331da177e4SLinus Torvalds * descriptors change from their values before the reset, or the original 33341da177e4SLinus Torvalds * configuration and altsettings cannot be restored, a flag will be set 33351da177e4SLinus Torvalds * telling khubd to pretend the device has been disconnected and then 33361da177e4SLinus Torvalds * re-connected. All drivers will be unbound, and the device will be 33371da177e4SLinus Torvalds * re-enumerated and probed all over again. 33381da177e4SLinus Torvalds * 33391da177e4SLinus Torvalds * Returns 0 if the reset succeeded, -ENODEV if the device has been 33401da177e4SLinus Torvalds * flagged for logical disconnection, or some other negative error code 33411da177e4SLinus Torvalds * if the reset wasn't even attempted. 33421da177e4SLinus Torvalds * 33431da177e4SLinus Torvalds * The caller must own the device lock. For example, it's safe to use 33441da177e4SLinus Torvalds * this from a driver probe() routine after downloading new firmware. 33451da177e4SLinus Torvalds * For calls that might not occur during probe(), drivers should lock 33461da177e4SLinus Torvalds * the device using usb_lock_device_for_reset(). 33476bc6cff5SAlan Stern * 33486bc6cff5SAlan Stern * Locking exception: This routine may also be called from within an 33496bc6cff5SAlan Stern * autoresume handler. Such usage won't conflict with other tasks 33506bc6cff5SAlan Stern * holding the device lock because these tasks should always call 33516bc6cff5SAlan Stern * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. 33521da177e4SLinus Torvalds */ 3353742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev) 33541da177e4SLinus Torvalds { 33551da177e4SLinus Torvalds struct usb_device *parent_hdev = udev->parent; 33561da177e4SLinus Torvalds struct usb_hub *parent_hub; 33571da177e4SLinus Torvalds struct usb_device_descriptor descriptor = udev->descriptor; 335812c3da34SAlan Stern int i, ret = 0; 335912c3da34SAlan Stern int port1 = udev->portnum; 33601da177e4SLinus Torvalds 33611da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED || 33621da177e4SLinus Torvalds udev->state == USB_STATE_SUSPENDED) { 33631da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 33641da177e4SLinus Torvalds udev->state); 33651da177e4SLinus Torvalds return -EINVAL; 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds if (!parent_hdev) { 33691da177e4SLinus Torvalds /* this requires hcd-specific logic; see OHCI hc_restart() */ 3370441b62c1SHarvey Harrison dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 33711da177e4SLinus Torvalds return -EISDIR; 33721da177e4SLinus Torvalds } 33731da177e4SLinus Torvalds parent_hub = hdev_to_hub(parent_hdev); 33741da177e4SLinus Torvalds 33751da177e4SLinus Torvalds set_bit(port1, parent_hub->busy_bits); 33761da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; ++i) { 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds /* ep0 maxpacket size may change; let the HCD know about it. 33791da177e4SLinus Torvalds * Other endpoints will be handled by re-enumeration. */ 3380fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 33811da177e4SLinus Torvalds ret = hub_port_init(parent_hub, udev, port1, i); 3382dd4dd19eSAlan Stern if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 33831da177e4SLinus Torvalds break; 33841da177e4SLinus Torvalds } 33851da177e4SLinus Torvalds clear_bit(port1, parent_hub->busy_bits); 3386d5cbad4bSAlan Stern 33871da177e4SLinus Torvalds if (ret < 0) 33881da177e4SLinus Torvalds goto re_enumerate; 33891da177e4SLinus Torvalds 33901da177e4SLinus Torvalds /* Device might have changed firmware (DFU or similar) */ 3391eb764c4bSAlan Stern if (descriptors_changed(udev, &descriptor)) { 33921da177e4SLinus Torvalds dev_info(&udev->dev, "device firmware changed\n"); 33931da177e4SLinus Torvalds udev->descriptor = descriptor; /* for disconnect() calls */ 33941da177e4SLinus Torvalds goto re_enumerate; 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds 33974fe0387aSAlan Stern /* Restore the device's previous configuration */ 33981da177e4SLinus Torvalds if (!udev->actconfig) 33991da177e4SLinus Torvalds goto done; 34001da177e4SLinus Torvalds ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 34011da177e4SLinus Torvalds USB_REQ_SET_CONFIGURATION, 0, 34021da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, 0, 34031da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 34041da177e4SLinus Torvalds if (ret < 0) { 34051da177e4SLinus Torvalds dev_err(&udev->dev, 34061da177e4SLinus Torvalds "can't restore configuration #%d (error=%d)\n", 34071da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, ret); 34081da177e4SLinus Torvalds goto re_enumerate; 34091da177e4SLinus Torvalds } 34101da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_CONFIGURED); 34111da177e4SLinus Torvalds 34124fe0387aSAlan Stern /* Put interfaces back into the same altsettings as before. 34134fe0387aSAlan Stern * Don't bother to send the Set-Interface request for interfaces 34144fe0387aSAlan Stern * that were already in altsetting 0; besides being unnecessary, 34154fe0387aSAlan Stern * many devices can't handle it. Instead just reset the host-side 34164fe0387aSAlan Stern * endpoint state. 34174fe0387aSAlan Stern */ 34181da177e4SLinus Torvalds for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 34191da177e4SLinus Torvalds struct usb_interface *intf = udev->actconfig->interface[i]; 34201da177e4SLinus Torvalds struct usb_interface_descriptor *desc; 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds desc = &intf->cur_altsetting->desc; 34234fe0387aSAlan Stern if (desc->bAlternateSetting == 0) { 34244fe0387aSAlan Stern usb_disable_interface(udev, intf, true); 34254fe0387aSAlan Stern usb_enable_interface(udev, intf, true); 34264fe0387aSAlan Stern ret = 0; 34274fe0387aSAlan Stern } else { 34281da177e4SLinus Torvalds ret = usb_set_interface(udev, desc->bInterfaceNumber, 34291da177e4SLinus Torvalds desc->bAlternateSetting); 34304fe0387aSAlan Stern } 34311da177e4SLinus Torvalds if (ret < 0) { 34321da177e4SLinus Torvalds dev_err(&udev->dev, "failed to restore interface %d " 34331da177e4SLinus Torvalds "altsetting %d (error=%d)\n", 34341da177e4SLinus Torvalds desc->bInterfaceNumber, 34351da177e4SLinus Torvalds desc->bAlternateSetting, 34361da177e4SLinus Torvalds ret); 34371da177e4SLinus Torvalds goto re_enumerate; 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds } 34401da177e4SLinus Torvalds 34411da177e4SLinus Torvalds done: 34421da177e4SLinus Torvalds return 0; 34431da177e4SLinus Torvalds 34441da177e4SLinus Torvalds re_enumerate: 34451da177e4SLinus Torvalds hub_port_logical_disconnect(parent_hub, port1); 34461da177e4SLinus Torvalds return -ENODEV; 34471da177e4SLinus Torvalds } 344879efa097SAlan Stern 344979efa097SAlan Stern /** 3450742120c6SMing Lei * usb_reset_device - warn interface drivers and perform a USB port reset 345179efa097SAlan Stern * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 345279efa097SAlan Stern * 345379efa097SAlan Stern * Warns all drivers bound to registered interfaces (using their pre_reset 345479efa097SAlan Stern * method), performs the port reset, and then lets the drivers know that 345579efa097SAlan Stern * the reset is over (using their post_reset method). 345679efa097SAlan Stern * 3457742120c6SMing Lei * Return value is the same as for usb_reset_and_verify_device(). 345879efa097SAlan Stern * 345979efa097SAlan Stern * The caller must own the device lock. For example, it's safe to use 346079efa097SAlan Stern * this from a driver probe() routine after downloading new firmware. 346179efa097SAlan Stern * For calls that might not occur during probe(), drivers should lock 346279efa097SAlan Stern * the device using usb_lock_device_for_reset(). 346378d9a487SAlan Stern * 346478d9a487SAlan Stern * If an interface is currently being probed or disconnected, we assume 346578d9a487SAlan Stern * its driver knows how to handle resets. For all other interfaces, 346678d9a487SAlan Stern * if the driver doesn't have pre_reset and post_reset methods then 346778d9a487SAlan Stern * we attempt to unbind it and rebind afterward. 346879efa097SAlan Stern */ 3469742120c6SMing Lei int usb_reset_device(struct usb_device *udev) 347079efa097SAlan Stern { 347179efa097SAlan Stern int ret; 3472852c4b43SAlan Stern int i; 347379efa097SAlan Stern struct usb_host_config *config = udev->actconfig; 347479efa097SAlan Stern 347579efa097SAlan Stern if (udev->state == USB_STATE_NOTATTACHED || 347679efa097SAlan Stern udev->state == USB_STATE_SUSPENDED) { 347779efa097SAlan Stern dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 347879efa097SAlan Stern udev->state); 347979efa097SAlan Stern return -EINVAL; 348079efa097SAlan Stern } 348179efa097SAlan Stern 3482645daaabSAlan Stern /* Prevent autosuspend during the reset */ 348394fcda1fSAlan Stern usb_autoresume_device(udev); 3484645daaabSAlan Stern 348579efa097SAlan Stern if (config) { 3486852c4b43SAlan Stern for (i = 0; i < config->desc.bNumInterfaces; ++i) { 3487852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 348879efa097SAlan Stern struct usb_driver *drv; 348978d9a487SAlan Stern int unbind = 0; 349079efa097SAlan Stern 3491852c4b43SAlan Stern if (cintf->dev.driver) { 349279efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 349378d9a487SAlan Stern if (drv->pre_reset && drv->post_reset) 349478d9a487SAlan Stern unbind = (drv->pre_reset)(cintf); 349578d9a487SAlan Stern else if (cintf->condition == 349678d9a487SAlan Stern USB_INTERFACE_BOUND) 349778d9a487SAlan Stern unbind = 1; 349878d9a487SAlan Stern if (unbind) 349978d9a487SAlan Stern usb_forced_unbind_intf(cintf); 350079efa097SAlan Stern } 350179efa097SAlan Stern } 350279efa097SAlan Stern } 350379efa097SAlan Stern 3504742120c6SMing Lei ret = usb_reset_and_verify_device(udev); 350579efa097SAlan Stern 350679efa097SAlan Stern if (config) { 3507852c4b43SAlan Stern for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 3508852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 350979efa097SAlan Stern struct usb_driver *drv; 351078d9a487SAlan Stern int rebind = cintf->needs_binding; 351179efa097SAlan Stern 351278d9a487SAlan Stern if (!rebind && cintf->dev.driver) { 351379efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 351479efa097SAlan Stern if (drv->post_reset) 351578d9a487SAlan Stern rebind = (drv->post_reset)(cintf); 351678d9a487SAlan Stern else if (cintf->condition == 351778d9a487SAlan Stern USB_INTERFACE_BOUND) 351878d9a487SAlan Stern rebind = 1; 351979efa097SAlan Stern } 35206c640945SAlan Stern if (ret == 0 && rebind) 352178d9a487SAlan Stern usb_rebind_intf(cintf); 352279efa097SAlan Stern } 352379efa097SAlan Stern } 352479efa097SAlan Stern 352594fcda1fSAlan Stern usb_autosuspend_device(udev); 352679efa097SAlan Stern return ret; 352779efa097SAlan Stern } 3528742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device); 3529dc023dceSInaky Perez-Gonzalez 3530dc023dceSInaky Perez-Gonzalez 3531dc023dceSInaky Perez-Gonzalez /** 3532dc023dceSInaky Perez-Gonzalez * usb_queue_reset_device - Reset a USB device from an atomic context 3533dc023dceSInaky Perez-Gonzalez * @iface: USB interface belonging to the device to reset 3534dc023dceSInaky Perez-Gonzalez * 3535dc023dceSInaky Perez-Gonzalez * This function can be used to reset a USB device from an atomic 3536dc023dceSInaky Perez-Gonzalez * context, where usb_reset_device() won't work (as it blocks). 3537dc023dceSInaky Perez-Gonzalez * 3538dc023dceSInaky Perez-Gonzalez * Doing a reset via this method is functionally equivalent to calling 3539dc023dceSInaky Perez-Gonzalez * usb_reset_device(), except for the fact that it is delayed to a 3540dc023dceSInaky Perez-Gonzalez * workqueue. This means that any drivers bound to other interfaces 3541dc023dceSInaky Perez-Gonzalez * might be unbound, as well as users from usbfs in user space. 3542dc023dceSInaky Perez-Gonzalez * 3543dc023dceSInaky Perez-Gonzalez * Corner cases: 3544dc023dceSInaky Perez-Gonzalez * 3545dc023dceSInaky Perez-Gonzalez * - Scheduling two resets at the same time from two different drivers 3546dc023dceSInaky Perez-Gonzalez * attached to two different interfaces of the same device is 3547dc023dceSInaky Perez-Gonzalez * possible; depending on how the driver attached to each interface 3548dc023dceSInaky Perez-Gonzalez * handles ->pre_reset(), the second reset might happen or not. 3549dc023dceSInaky Perez-Gonzalez * 3550dc023dceSInaky Perez-Gonzalez * - If a driver is unbound and it had a pending reset, the reset will 3551dc023dceSInaky Perez-Gonzalez * be cancelled. 3552dc023dceSInaky Perez-Gonzalez * 3553dc023dceSInaky Perez-Gonzalez * - This function can be called during .probe() or .disconnect() 3554dc023dceSInaky Perez-Gonzalez * times. On return from .disconnect(), any pending resets will be 3555dc023dceSInaky Perez-Gonzalez * cancelled. 3556dc023dceSInaky Perez-Gonzalez * 3557dc023dceSInaky Perez-Gonzalez * There is no no need to lock/unlock the @reset_ws as schedule_work() 3558dc023dceSInaky Perez-Gonzalez * does its own. 3559dc023dceSInaky Perez-Gonzalez * 3560dc023dceSInaky Perez-Gonzalez * NOTE: We don't do any reference count tracking because it is not 3561dc023dceSInaky Perez-Gonzalez * needed. The lifecycle of the work_struct is tied to the 3562dc023dceSInaky Perez-Gonzalez * usb_interface. Before destroying the interface we cancel the 3563dc023dceSInaky Perez-Gonzalez * work_struct, so the fact that work_struct is queued and or 3564dc023dceSInaky Perez-Gonzalez * running means the interface (and thus, the device) exist and 3565dc023dceSInaky Perez-Gonzalez * are referenced. 3566dc023dceSInaky Perez-Gonzalez */ 3567dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface) 3568dc023dceSInaky Perez-Gonzalez { 3569dc023dceSInaky Perez-Gonzalez schedule_work(&iface->reset_ws); 3570dc023dceSInaky Perez-Gonzalez } 3571dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device); 3572