Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Host Controller Interface driver for USB. |
| 3 | * |
| 4 | * Maintainer: Alan Stern <stern@rowland.harvard.edu> |
| 5 | * |
| 6 | * (C) Copyright 1999 Linus Torvalds |
| 7 | * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com |
| 8 | * (C) Copyright 1999 Randy Dunlap |
| 9 | * (C) Copyright 1999 Georg Acher, acher@in.tum.de |
| 10 | * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de |
| 11 | * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch |
| 12 | * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu |
| 13 | */ |
| 14 | |
| 15 | static __u8 root_hub_hub_des[] = |
| 16 | { |
| 17 | 0x09, /* __u8 bLength; */ |
| 18 | 0x29, /* __u8 bDescriptorType; Hub-descriptor */ |
| 19 | 0x02, /* __u8 bNbrPorts; */ |
| 20 | 0x0a, /* __u16 wHubCharacteristics; */ |
| 21 | 0x00, /* (per-port OC, no power switching) */ |
| 22 | 0x01, /* __u8 bPwrOn2pwrGood; 2ms */ |
| 23 | 0x00, /* __u8 bHubContrCurrent; 0 mA */ |
| 24 | 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */ |
| 25 | 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */ |
| 26 | }; |
| 27 | |
| 28 | #define UHCI_RH_MAXCHILD 7 |
| 29 | |
| 30 | /* must write as zeroes */ |
| 31 | #define WZ_BITS (USBPORTSC_RES2 | USBPORTSC_RES3 | USBPORTSC_RES4) |
| 32 | |
| 33 | /* status change bits: nonzero writes will clear */ |
| 34 | #define RWC_BITS (USBPORTSC_OCC | USBPORTSC_PEC | USBPORTSC_CSC) |
| 35 | |
Alan Stern | f5946f8 | 2005-04-09 17:26:00 -0400 | [diff] [blame] | 36 | /* A port that either is connected or has a changed-bit set will prevent |
| 37 | * us from AUTO_STOPPING. |
| 38 | */ |
| 39 | static int any_ports_active(struct uhci_hcd *uhci) |
| 40 | { |
| 41 | int port; |
| 42 | |
| 43 | for (port = 0; port < uhci->rh_numports; ++port) { |
| 44 | if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & |
| 45 | (USBPORTSC_CCS | RWC_BITS)) || |
| 46 | test_bit(port, &uhci->port_c_suspend)) |
| 47 | return 1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 52 | static inline int get_hub_status_data(struct uhci_hcd *uhci, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int port; |
| 55 | |
| 56 | *buf = 0; |
| 57 | for (port = 0; port < uhci->rh_numports; ++port) { |
| 58 | if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & RWC_BITS) || |
| 59 | test_bit(port, &uhci->port_c_suspend)) |
| 60 | *buf |= (1 << (port + 1)); |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return !!*buf; |
| 63 | } |
| 64 | |
| 65 | #define OK(x) len = (x); break |
| 66 | |
| 67 | #define CLR_RH_PORTSTAT(x) \ |
| 68 | status = inw(port_addr); \ |
| 69 | status &= ~(RWC_BITS|WZ_BITS); \ |
| 70 | status &= ~(x); \ |
| 71 | status |= RWC_BITS & (x); \ |
| 72 | outw(status, port_addr) |
| 73 | |
| 74 | #define SET_RH_PORTSTAT(x) \ |
| 75 | status = inw(port_addr); \ |
| 76 | status |= (x); \ |
| 77 | status &= ~(RWC_BITS|WZ_BITS); \ |
| 78 | outw(status, port_addr) |
| 79 | |
| 80 | /* UHCI controllers don't automatically stop resume signalling after 20 msec, |
| 81 | * so we have to poll and check timeouts in order to take care of it. |
| 82 | */ |
| 83 | static void uhci_finish_suspend(struct uhci_hcd *uhci, int port, |
| 84 | unsigned long port_addr) |
| 85 | { |
| 86 | int status; |
| 87 | |
| 88 | if (test_bit(port, &uhci->suspended_ports)) { |
| 89 | CLR_RH_PORTSTAT(USBPORTSC_SUSP | USBPORTSC_RD); |
| 90 | clear_bit(port, &uhci->suspended_ports); |
| 91 | clear_bit(port, &uhci->resuming_ports); |
| 92 | set_bit(port, &uhci->port_c_suspend); |
| 93 | |
| 94 | /* The controller won't actually turn off the RD bit until |
| 95 | * it has had a chance to send a low-speed EOP sequence, |
| 96 | * which takes 3 bit times (= 2 microseconds). We'll delay |
| 97 | * slightly longer for good luck. */ |
| 98 | udelay(4); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static void uhci_check_ports(struct uhci_hcd *uhci) |
| 103 | { |
| 104 | unsigned int port; |
| 105 | unsigned long port_addr; |
| 106 | int status; |
| 107 | |
| 108 | for (port = 0; port < uhci->rh_numports; ++port) { |
| 109 | port_addr = uhci->io_addr + USBPORTSC1 + 2 * port; |
| 110 | status = inw(port_addr); |
| 111 | if (unlikely(status & USBPORTSC_PR)) { |
| 112 | if (time_after_eq(jiffies, uhci->ports_timeout)) { |
| 113 | CLR_RH_PORTSTAT(USBPORTSC_PR); |
| 114 | udelay(10); |
| 115 | |
| 116 | /* If the port was enabled before, turning |
| 117 | * reset on caused a port enable change. |
| 118 | * Turning reset off causes a port connect |
| 119 | * status change. Clear these changes. */ |
| 120 | CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC); |
| 121 | SET_RH_PORTSTAT(USBPORTSC_PE); |
| 122 | } |
| 123 | } |
| 124 | if (unlikely(status & USBPORTSC_RD)) { |
| 125 | if (!test_bit(port, &uhci->resuming_ports)) { |
| 126 | |
| 127 | /* Port received a wakeup request */ |
| 128 | set_bit(port, &uhci->resuming_ports); |
| 129 | uhci->ports_timeout = jiffies + |
| 130 | msecs_to_jiffies(20); |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 131 | |
| 132 | /* Make sure we see the port again |
| 133 | * after the resuming period is over. */ |
| 134 | mod_timer(&uhci_to_hcd(uhci)->rh_timer, |
| 135 | uhci->ports_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } else if (time_after_eq(jiffies, |
| 137 | uhci->ports_timeout)) { |
| 138 | uhci_finish_suspend(uhci, port, port_addr); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 144 | static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf) |
| 145 | { |
| 146 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); |
| 147 | unsigned long flags; |
Alan Stern | 1f09df8 | 2005-09-05 13:59:51 -0400 | [diff] [blame^] | 148 | int status = 0; |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 149 | |
| 150 | spin_lock_irqsave(&uhci->lock, flags); |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 151 | |
Alan Stern | 1f09df8 | 2005-09-05 13:59:51 -0400 | [diff] [blame^] | 152 | uhci_scan_schedule(uhci, NULL); |
| 153 | if (uhci->hc_inaccessible) |
| 154 | goto done; |
| 155 | check_fsbr(uhci); |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 156 | uhci_check_ports(uhci); |
Alan Stern | 1f09df8 | 2005-09-05 13:59:51 -0400 | [diff] [blame^] | 157 | |
Alan Stern | 6c1b445 | 2005-04-21 16:04:58 -0400 | [diff] [blame] | 158 | status = get_hub_status_data(uhci, buf); |
| 159 | |
| 160 | switch (uhci->rh_state) { |
| 161 | case UHCI_RH_SUSPENDING: |
| 162 | case UHCI_RH_SUSPENDED: |
| 163 | /* if port change, ask to be resumed */ |
| 164 | if (status) |
| 165 | usb_hcd_resume_root_hub(hcd); |
| 166 | break; |
| 167 | |
| 168 | case UHCI_RH_AUTO_STOPPED: |
| 169 | /* if port change, auto start */ |
| 170 | if (status) |
| 171 | wakeup_rh(uhci); |
| 172 | break; |
| 173 | |
| 174 | case UHCI_RH_RUNNING: |
| 175 | /* are any devices attached? */ |
| 176 | if (!any_ports_active(uhci)) { |
| 177 | uhci->rh_state = UHCI_RH_RUNNING_NODEVS; |
| 178 | uhci->auto_stop_time = jiffies + HZ; |
| 179 | } |
| 180 | break; |
| 181 | |
| 182 | case UHCI_RH_RUNNING_NODEVS: |
| 183 | /* auto-stop if nothing connected for 1 second */ |
| 184 | if (any_ports_active(uhci)) |
| 185 | uhci->rh_state = UHCI_RH_RUNNING; |
| 186 | else if (time_after_eq(jiffies, uhci->auto_stop_time)) |
| 187 | suspend_rh(uhci, UHCI_RH_AUTO_STOPPED); |
| 188 | break; |
| 189 | |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | done: |
| 195 | spin_unlock_irqrestore(&uhci->lock, flags); |
| 196 | return status; |
| 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* size of returned buffer is part of USB spec */ |
| 200 | static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 201 | u16 wIndex, char *buf, u16 wLength) |
| 202 | { |
| 203 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); |
| 204 | int status, lstatus, retval = 0, len = 0; |
| 205 | unsigned int port = wIndex - 1; |
| 206 | unsigned long port_addr = uhci->io_addr + USBPORTSC1 + 2 * port; |
| 207 | u16 wPortChange, wPortStatus; |
| 208 | unsigned long flags; |
| 209 | |
Alan Stern | a8bed8b | 2005-04-09 17:29:00 -0400 | [diff] [blame] | 210 | if (uhci->hc_inaccessible) |
| 211 | return -ETIMEDOUT; |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | spin_lock_irqsave(&uhci->lock, flags); |
| 214 | switch (typeReq) { |
| 215 | |
| 216 | case GetHubStatus: |
| 217 | *(__le32 *)buf = cpu_to_le32(0); |
| 218 | OK(4); /* hub power */ |
| 219 | case GetPortStatus: |
| 220 | if (port >= uhci->rh_numports) |
| 221 | goto err; |
| 222 | |
| 223 | uhci_check_ports(uhci); |
| 224 | status = inw(port_addr); |
| 225 | |
| 226 | /* Intel controllers report the OverCurrent bit active on. |
| 227 | * VIA controllers report it active off, so we'll adjust the |
| 228 | * bit value. (It's not standardized in the UHCI spec.) |
| 229 | */ |
| 230 | if (to_pci_dev(hcd->self.controller)->vendor == |
| 231 | PCI_VENDOR_ID_VIA) |
| 232 | status ^= USBPORTSC_OC; |
| 233 | |
| 234 | /* UHCI doesn't support C_RESET (always false) */ |
| 235 | wPortChange = lstatus = 0; |
| 236 | if (status & USBPORTSC_CSC) |
| 237 | wPortChange |= USB_PORT_STAT_C_CONNECTION; |
| 238 | if (status & USBPORTSC_PEC) |
| 239 | wPortChange |= USB_PORT_STAT_C_ENABLE; |
| 240 | if (status & USBPORTSC_OCC) |
| 241 | wPortChange |= USB_PORT_STAT_C_OVERCURRENT; |
| 242 | |
| 243 | if (test_bit(port, &uhci->port_c_suspend)) { |
| 244 | wPortChange |= USB_PORT_STAT_C_SUSPEND; |
| 245 | lstatus |= 1; |
| 246 | } |
| 247 | if (test_bit(port, &uhci->suspended_ports)) |
| 248 | lstatus |= 2; |
| 249 | if (test_bit(port, &uhci->resuming_ports)) |
| 250 | lstatus |= 4; |
| 251 | |
| 252 | /* UHCI has no power switching (always on) */ |
| 253 | wPortStatus = USB_PORT_STAT_POWER; |
| 254 | if (status & USBPORTSC_CCS) |
| 255 | wPortStatus |= USB_PORT_STAT_CONNECTION; |
| 256 | if (status & USBPORTSC_PE) { |
| 257 | wPortStatus |= USB_PORT_STAT_ENABLE; |
| 258 | if (status & (USBPORTSC_SUSP | USBPORTSC_RD)) |
| 259 | wPortStatus |= USB_PORT_STAT_SUSPEND; |
| 260 | } |
| 261 | if (status & USBPORTSC_OC) |
| 262 | wPortStatus |= USB_PORT_STAT_OVERCURRENT; |
| 263 | if (status & USBPORTSC_PR) |
| 264 | wPortStatus |= USB_PORT_STAT_RESET; |
| 265 | if (status & USBPORTSC_LSDA) |
| 266 | wPortStatus |= USB_PORT_STAT_LOW_SPEED; |
| 267 | |
| 268 | if (wPortChange) |
| 269 | dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n", |
| 270 | wIndex, status, lstatus); |
| 271 | |
| 272 | *(__le16 *)buf = cpu_to_le16(wPortStatus); |
| 273 | *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange); |
| 274 | OK(4); |
| 275 | case SetHubFeature: /* We don't implement these */ |
| 276 | case ClearHubFeature: |
| 277 | switch (wValue) { |
| 278 | case C_HUB_OVER_CURRENT: |
| 279 | case C_HUB_LOCAL_POWER: |
| 280 | OK(0); |
| 281 | default: |
| 282 | goto err; |
| 283 | } |
| 284 | break; |
| 285 | case SetPortFeature: |
| 286 | if (port >= uhci->rh_numports) |
| 287 | goto err; |
| 288 | |
| 289 | switch (wValue) { |
| 290 | case USB_PORT_FEAT_SUSPEND: |
| 291 | set_bit(port, &uhci->suspended_ports); |
| 292 | SET_RH_PORTSTAT(USBPORTSC_SUSP); |
| 293 | OK(0); |
| 294 | case USB_PORT_FEAT_RESET: |
| 295 | SET_RH_PORTSTAT(USBPORTSC_PR); |
| 296 | |
| 297 | /* Reset terminates Resume signalling */ |
| 298 | uhci_finish_suspend(uhci, port, port_addr); |
| 299 | |
| 300 | /* USB v2.0 7.1.7.5 */ |
| 301 | uhci->ports_timeout = jiffies + msecs_to_jiffies(50); |
| 302 | OK(0); |
| 303 | case USB_PORT_FEAT_POWER: |
| 304 | /* UHCI has no power switching */ |
| 305 | OK(0); |
| 306 | default: |
| 307 | goto err; |
| 308 | } |
| 309 | break; |
| 310 | case ClearPortFeature: |
| 311 | if (port >= uhci->rh_numports) |
| 312 | goto err; |
| 313 | |
| 314 | switch (wValue) { |
| 315 | case USB_PORT_FEAT_ENABLE: |
| 316 | CLR_RH_PORTSTAT(USBPORTSC_PE); |
| 317 | |
| 318 | /* Disable terminates Resume signalling */ |
| 319 | uhci_finish_suspend(uhci, port, port_addr); |
| 320 | OK(0); |
| 321 | case USB_PORT_FEAT_C_ENABLE: |
| 322 | CLR_RH_PORTSTAT(USBPORTSC_PEC); |
| 323 | OK(0); |
| 324 | case USB_PORT_FEAT_SUSPEND: |
| 325 | if (test_bit(port, &uhci->suspended_ports) && |
| 326 | !test_and_set_bit(port, |
| 327 | &uhci->resuming_ports)) { |
| 328 | SET_RH_PORTSTAT(USBPORTSC_RD); |
| 329 | |
| 330 | /* The controller won't allow RD to be set |
| 331 | * if the port is disabled. When this happens |
| 332 | * just skip the Resume signalling. |
| 333 | */ |
| 334 | if (!(inw(port_addr) & USBPORTSC_RD)) |
| 335 | uhci_finish_suspend(uhci, port, |
| 336 | port_addr); |
| 337 | else |
| 338 | /* USB v2.0 7.1.7.7 */ |
| 339 | uhci->ports_timeout = jiffies + |
| 340 | msecs_to_jiffies(20); |
| 341 | } |
| 342 | OK(0); |
| 343 | case USB_PORT_FEAT_C_SUSPEND: |
| 344 | clear_bit(port, &uhci->port_c_suspend); |
| 345 | OK(0); |
| 346 | case USB_PORT_FEAT_POWER: |
| 347 | /* UHCI has no power switching */ |
| 348 | goto err; |
| 349 | case USB_PORT_FEAT_C_CONNECTION: |
| 350 | CLR_RH_PORTSTAT(USBPORTSC_CSC); |
| 351 | OK(0); |
| 352 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 353 | CLR_RH_PORTSTAT(USBPORTSC_OCC); |
| 354 | OK(0); |
| 355 | case USB_PORT_FEAT_C_RESET: |
| 356 | /* this driver won't report these */ |
| 357 | OK(0); |
| 358 | default: |
| 359 | goto err; |
| 360 | } |
| 361 | break; |
| 362 | case GetHubDescriptor: |
| 363 | len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength); |
| 364 | memcpy(buf, root_hub_hub_des, len); |
| 365 | if (len > 2) |
| 366 | buf[2] = uhci->rh_numports; |
| 367 | OK(len); |
| 368 | default: |
| 369 | err: |
| 370 | retval = -EPIPE; |
| 371 | } |
| 372 | spin_unlock_irqrestore(&uhci->lock, flags); |
| 373 | |
| 374 | return retval; |
| 375 | } |