Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OHCI HCD (Host Controller Driver) for USB. |
| 3 | * |
| 4 | * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> |
| 5 | * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net> |
| 6 | * |
| 7 | * This file is licenced under GPL |
| 8 | */ |
| 9 | |
| 10 | /*-------------------------------------------------------------------------*/ |
| 11 | |
| 12 | /* |
| 13 | * OHCI Root Hub ... the nonsharable stuff |
| 14 | */ |
| 15 | |
| 16 | #define dbg_port(hc,label,num,value) \ |
| 17 | ohci_dbg (hc, \ |
| 18 | "%s roothub.portstatus [%d] " \ |
| 19 | "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \ |
| 20 | label, num, temp, \ |
| 21 | (temp & RH_PS_PRSC) ? " PRSC" : "", \ |
| 22 | (temp & RH_PS_OCIC) ? " OCIC" : "", \ |
| 23 | (temp & RH_PS_PSSC) ? " PSSC" : "", \ |
| 24 | (temp & RH_PS_PESC) ? " PESC" : "", \ |
| 25 | (temp & RH_PS_CSC) ? " CSC" : "", \ |
| 26 | \ |
| 27 | (temp & RH_PS_LSDA) ? " LSDA" : "", \ |
| 28 | (temp & RH_PS_PPS) ? " PPS" : "", \ |
| 29 | (temp & RH_PS_PRS) ? " PRS" : "", \ |
| 30 | (temp & RH_PS_POCI) ? " POCI" : "", \ |
| 31 | (temp & RH_PS_PSS) ? " PSS" : "", \ |
| 32 | \ |
| 33 | (temp & RH_PS_PES) ? " PES" : "", \ |
| 34 | (temp & RH_PS_CCS) ? " CCS" : "" \ |
| 35 | ); |
| 36 | |
| 37 | /*-------------------------------------------------------------------------*/ |
| 38 | |
David Brownell | d413984 | 2006-08-04 11:31:55 -0700 | [diff] [blame] | 39 | /* hcd->hub_irq_enable() */ |
| 40 | static void ohci_rhsc_enable (struct usb_hcd *hcd) |
| 41 | { |
| 42 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 43 | |
| 44 | ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable); |
| 45 | } |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define OHCI_SCHED_ENABLES \ |
| 48 | (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE) |
| 49 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 50 | static void dl_done_list (struct ohci_hcd *); |
| 51 | static void finish_unlinks (struct ohci_hcd *, u16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Alan Stern | b187844 | 2006-10-24 12:02:31 -0400 | [diff] [blame^] | 53 | #ifdef CONFIG_PM |
| 54 | static int ohci_restart(struct ohci_hcd *ohci); |
| 55 | #endif |
| 56 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 57 | static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop) |
| 58 | __releases(ohci->lock) |
| 59 | __acquires(ohci->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int status = 0; |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); |
| 64 | switch (ohci->hc_control & OHCI_CTRL_HCFS) { |
| 65 | case OHCI_USB_RESUME: |
| 66 | ohci_dbg (ohci, "resume/suspend?\n"); |
| 67 | ohci->hc_control &= ~OHCI_CTRL_HCFS; |
| 68 | ohci->hc_control |= OHCI_USB_RESET; |
| 69 | ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); |
| 70 | (void) ohci_readl (ohci, &ohci->regs->control); |
| 71 | /* FALL THROUGH */ |
| 72 | case OHCI_USB_RESET: |
| 73 | status = -EBUSY; |
| 74 | ohci_dbg (ohci, "needs reinit!\n"); |
| 75 | goto done; |
| 76 | case OHCI_USB_SUSPEND: |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 77 | if (!ohci->autostop) { |
| 78 | ohci_dbg (ohci, "already suspended\n"); |
| 79 | goto done; |
| 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 82 | ohci_dbg (ohci, "%s root hub\n", |
| 83 | autostop ? "auto-stop" : "suspend"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* First stop any processing */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 86 | if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | ohci->hc_control &= ~OHCI_SCHED_ENABLES; |
| 88 | ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); |
| 89 | ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); |
| 90 | ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus); |
| 91 | |
| 92 | /* sched disables take effect on the next frame, |
| 93 | * then the last WDH could take 6+ msec |
| 94 | */ |
| 95 | ohci_dbg (ohci, "stopping schedules ...\n"); |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 96 | ohci->autostop = 0; |
| 97 | spin_unlock_irq (&ohci->lock); |
| 98 | msleep (8); |
| 99 | spin_lock_irq (&ohci->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 101 | dl_done_list (ohci); |
| 102 | finish_unlinks (ohci, ohci_frame_no(ohci)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* maybe resume can wake root hub */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 105 | if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) || |
| 106 | autostop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ohci->hc_control |= OHCI_CTRL_RWE; |
Alan Stern | 1f7e1a3 | 2006-09-25 15:41:21 -0400 | [diff] [blame] | 108 | else { |
| 109 | ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | ohci->hc_control &= ~OHCI_CTRL_RWE; |
Alan Stern | 1f7e1a3 | 2006-09-25 15:41:21 -0400 | [diff] [blame] | 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
David Brownell | f197b2c | 2005-09-22 22:42:53 -0700 | [diff] [blame] | 113 | /* Suspend hub ... this is the "global (to this bus) suspend" mode, |
| 114 | * which doesn't imply ports will first be individually suspended. |
| 115 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ohci->hc_control &= ~OHCI_CTRL_HCFS; |
| 117 | ohci->hc_control |= OHCI_USB_SUSPEND; |
| 118 | ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); |
| 119 | (void) ohci_readl (ohci, &ohci->regs->control); |
| 120 | |
| 121 | /* no resumes until devices finish suspending */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 122 | if (!autostop) { |
| 123 | ohci->next_statechange = jiffies + msecs_to_jiffies (5); |
| 124 | ohci->autostop = 0; |
| 125 | } |
David Brownell | d413984 | 2006-08-04 11:31:55 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return status; |
| 129 | } |
| 130 | |
| 131 | static inline struct ed *find_head (struct ed *ed) |
| 132 | { |
| 133 | /* for bulk and control lists */ |
| 134 | while (ed->ed_prev) |
| 135 | ed = ed->ed_prev; |
| 136 | return ed; |
| 137 | } |
| 138 | |
| 139 | /* caller has locked the root hub */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 140 | static int ohci_rh_resume (struct ohci_hcd *ohci) |
| 141 | __releases(ohci->lock) |
| 142 | __acquires(ohci->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 144 | struct usb_hcd *hcd = ohci_to_hcd (ohci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | u32 temp, enables; |
| 146 | int status = -EINPROGRESS; |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 147 | int autostopped = ohci->autostop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 149 | ohci->autostop = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | ohci->hc_control = ohci_readl (ohci, &ohci->regs->control); |
| 151 | |
| 152 | if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) { |
David Brownell | f197b2c | 2005-09-22 22:42:53 -0700 | [diff] [blame] | 153 | /* this can happen after resuming a swsusp snapshot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (hcd->state == HC_STATE_RESUMING) { |
| 155 | ohci_dbg (ohci, "BIOS/SMM active, control %03x\n", |
| 156 | ohci->hc_control); |
| 157 | status = -EBUSY; |
| 158 | /* this happens when pmcore resumes HC then root */ |
| 159 | } else { |
| 160 | ohci_dbg (ohci, "duplicate resume\n"); |
| 161 | status = 0; |
| 162 | } |
| 163 | } else switch (ohci->hc_control & OHCI_CTRL_HCFS) { |
| 164 | case OHCI_USB_SUSPEND: |
| 165 | ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES); |
| 166 | ohci->hc_control |= OHCI_USB_RESUME; |
| 167 | ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); |
| 168 | (void) ohci_readl (ohci, &ohci->regs->control); |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 169 | ohci_dbg (ohci, "%s root hub\n", |
| 170 | autostopped ? "auto-start" : "resume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | break; |
| 172 | case OHCI_USB_RESUME: |
| 173 | /* HCFS changes sometime after INTR_RD */ |
Alan Stern | 583cead | 2006-10-24 12:04:22 -0400 | [diff] [blame] | 174 | ohci_info(ohci, "%swakeup\n", |
| 175 | autostopped ? "auto-" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | break; |
| 177 | case OHCI_USB_OPER: |
David Brownell | f197b2c | 2005-09-22 22:42:53 -0700 | [diff] [blame] | 178 | /* this can happen after resuming a swsusp snapshot */ |
| 179 | ohci_dbg (ohci, "snapshot resume? reinit\n"); |
| 180 | status = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | default: /* RESET, we lost power */ |
David Brownell | f197b2c | 2005-09-22 22:42:53 -0700 | [diff] [blame] | 183 | ohci_dbg (ohci, "lost power\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | status = -EBUSY; |
| 185 | } |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 186 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (status == -EBUSY) { |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 188 | if (!autostopped) { |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 189 | spin_unlock_irq (&ohci->lock); |
| 190 | (void) ohci_init (ohci); |
| 191 | status = ohci_restart (ohci); |
| 192 | spin_lock_irq (&ohci->lock); |
| 193 | } |
| 194 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 196 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (status != -EINPROGRESS) |
| 198 | return status; |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 199 | if (autostopped) |
| 200 | goto skip_resume; |
| 201 | spin_unlock_irq (&ohci->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 203 | temp = ohci->num_ports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | while (temp--) { |
| 205 | u32 stat = ohci_readl (ohci, |
| 206 | &ohci->regs->roothub.portstatus [temp]); |
| 207 | |
| 208 | /* force global, not selective, resume */ |
| 209 | if (!(stat & RH_PS_PSS)) |
| 210 | continue; |
| 211 | ohci_writel (ohci, RH_PS_POCI, |
| 212 | &ohci->regs->roothub.portstatus [temp]); |
| 213 | } |
| 214 | |
| 215 | /* Some controllers (lucent erratum) need extra-long delays */ |
David Brownell | f197b2c | 2005-09-22 22:42:53 -0700 | [diff] [blame] | 216 | msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | temp = ohci_readl (ohci, &ohci->regs->control); |
| 219 | temp &= OHCI_CTRL_HCFS; |
| 220 | if (temp != OHCI_USB_RESUME) { |
| 221 | ohci_err (ohci, "controller won't resume\n"); |
| 222 | return -EBUSY; |
| 223 | } |
| 224 | |
| 225 | /* disable old schedule state, reinit from scratch */ |
| 226 | ohci_writel (ohci, 0, &ohci->regs->ed_controlhead); |
| 227 | ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent); |
| 228 | ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead); |
| 229 | ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent); |
| 230 | ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent); |
| 231 | ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca); |
| 232 | |
| 233 | /* Sometimes PCI D3 suspend trashes frame timings ... */ |
| 234 | periodic_reinit (ohci); |
| 235 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 236 | /* the following code is executed with ohci->lock held and |
| 237 | * irqs disabled if and only if autostopped is true |
| 238 | */ |
| 239 | |
| 240 | skip_resume: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | /* interrupts might have been disabled */ |
| 242 | ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable); |
| 243 | if (ohci->ed_rm_list) |
| 244 | ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | /* Then re-enable operations */ |
| 247 | ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control); |
| 248 | (void) ohci_readl (ohci, &ohci->regs->control); |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 249 | if (!autostopped) |
| 250 | msleep (3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David Brownell | 6a9062f | 2006-01-23 15:28:07 -0800 | [diff] [blame] | 252 | temp = ohci->hc_control; |
| 253 | temp &= OHCI_CTRL_RWC; |
| 254 | temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | ohci->hc_control = temp; |
| 256 | ohci_writel (ohci, temp, &ohci->regs->control); |
| 257 | (void) ohci_readl (ohci, &ohci->regs->control); |
| 258 | |
| 259 | /* TRSMRCY */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 260 | if (!autostopped) { |
| 261 | msleep (10); |
| 262 | spin_lock_irq (&ohci->lock); |
| 263 | } |
| 264 | /* now ohci->lock is always held and irqs are always disabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
David Brownell | d413984 | 2006-08-04 11:31:55 -0700 | [diff] [blame] | 266 | /* keep it alive for more than ~5x suspend + resume costs */ |
| 267 | ohci->next_statechange = jiffies + STATECHANGE_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | /* maybe turn schedules back on */ |
| 270 | enables = 0; |
| 271 | temp = 0; |
| 272 | if (!ohci->ed_rm_list) { |
| 273 | if (ohci->ed_controltail) { |
| 274 | ohci_writel (ohci, |
| 275 | find_head (ohci->ed_controltail)->dma, |
| 276 | &ohci->regs->ed_controlhead); |
| 277 | enables |= OHCI_CTRL_CLE; |
| 278 | temp |= OHCI_CLF; |
| 279 | } |
| 280 | if (ohci->ed_bulktail) { |
| 281 | ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma, |
| 282 | &ohci->regs->ed_bulkhead); |
| 283 | enables |= OHCI_CTRL_BLE; |
| 284 | temp |= OHCI_BLF; |
| 285 | } |
| 286 | } |
| 287 | if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs) |
| 288 | enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE; |
| 289 | if (enables) { |
| 290 | ohci_dbg (ohci, "restarting schedules ... %08x\n", enables); |
| 291 | ohci->hc_control |= enables; |
| 292 | ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); |
| 293 | if (temp) |
| 294 | ohci_writel (ohci, temp, &ohci->regs->cmdstatus); |
| 295 | (void) ohci_readl (ohci, &ohci->regs->control); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 301 | #ifdef CONFIG_PM |
| 302 | |
| 303 | static int ohci_bus_suspend (struct usb_hcd *hcd) |
| 304 | { |
| 305 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 306 | int rc; |
| 307 | |
| 308 | spin_lock_irq (&ohci->lock); |
| 309 | |
| 310 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) |
| 311 | rc = -ESHUTDOWN; |
| 312 | else |
| 313 | rc = ohci_rh_suspend (ohci, 0); |
| 314 | spin_unlock_irq (&ohci->lock); |
| 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | static int ohci_bus_resume (struct usb_hcd *hcd) |
| 319 | { |
| 320 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 321 | int rc; |
| 322 | |
| 323 | if (time_before (jiffies, ohci->next_statechange)) |
| 324 | msleep(5); |
| 325 | |
| 326 | spin_lock_irq (&ohci->lock); |
| 327 | |
| 328 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) |
| 329 | rc = -ESHUTDOWN; |
| 330 | else |
| 331 | rc = ohci_rh_resume (ohci); |
| 332 | spin_unlock_irq (&ohci->lock); |
| 333 | |
| 334 | /* poll until we know a device is connected or we autostop */ |
| 335 | if (rc == 0) |
| 336 | usb_hcd_poll_rh_status(hcd); |
| 337 | return rc; |
| 338 | } |
| 339 | |
David Brownell | 8ad7fe1 | 2005-09-13 19:59:11 -0700 | [diff] [blame] | 340 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | /*-------------------------------------------------------------------------*/ |
| 343 | |
| 344 | /* build "status change" packet (one or two bytes) from HC registers */ |
| 345 | |
| 346 | static int |
| 347 | ohci_hub_status_data (struct usb_hcd *hcd, char *buf) |
| 348 | { |
| 349 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 350 | int i, changed = 0, length = 1; |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 351 | int any_connected = 0, rhsc_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | unsigned long flags; |
| 353 | |
| 354 | spin_lock_irqsave (&ohci->lock, flags); |
| 355 | |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 356 | /* undocumented erratum seen on at least rev D */ |
| 357 | if ((ohci->flags & OHCI_QUIRK_AMD756) |
| 358 | && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) { |
| 359 | ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP); |
| 361 | /* retry later; "should not happen" */ |
| 362 | goto done; |
| 363 | } |
| 364 | |
| 365 | /* init status */ |
| 366 | if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC)) |
| 367 | buf [0] = changed = 1; |
| 368 | else |
| 369 | buf [0] = 0; |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 370 | if (ohci->num_ports > 7) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | buf [1] = 0; |
| 372 | length++; |
| 373 | } |
| 374 | |
| 375 | /* look at each port */ |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 376 | for (i = 0; i < ohci->num_ports; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | u32 status = roothub_portstatus (ohci, i); |
| 378 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 379 | /* can't autostop if ports are connected */ |
| 380 | any_connected |= (status & RH_PS_CCS); |
| 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC |
| 383 | | RH_PS_OCIC | RH_PS_PRSC)) { |
| 384 | changed = 1; |
| 385 | if (i < 7) |
| 386 | buf [0] |= 1 << (i + 1); |
| 387 | else |
| 388 | buf [1] |= 1 << (i - 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 392 | /* NOTE: vendors didn't always make the same implementation |
| 393 | * choices for RHSC. Sometimes it triggers on an edge (like |
| 394 | * setting and maybe clearing a port status change bit); and |
| 395 | * it's level-triggered on other silicon, active until khubd |
| 396 | * clears all active port status change bits. If it's still |
| 397 | * set (level-triggered) we must disable it and rely on |
| 398 | * polling until khubd re-enables it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | */ |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 400 | if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) { |
| 401 | ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable); |
| 402 | (void) ohci_readl (ohci, &ohci->regs->intrdisable); |
| 403 | rhsc_enabled = 0; |
| 404 | } |
| 405 | hcd->poll_rh = 1; |
| 406 | |
| 407 | /* carry out appropriate state changes */ |
| 408 | switch (ohci->hc_control & OHCI_CTRL_HCFS) { |
| 409 | |
| 410 | case OHCI_USB_OPER: |
| 411 | /* keep on polling until we know a device is connected |
| 412 | * and RHSC is enabled */ |
| 413 | if (!ohci->autostop) { |
| 414 | if (any_connected) { |
| 415 | if (rhsc_enabled) |
| 416 | hcd->poll_rh = 0; |
| 417 | } else { |
| 418 | ohci->autostop = 1; |
| 419 | ohci->next_statechange = jiffies + HZ; |
| 420 | } |
| 421 | |
| 422 | /* if no devices have been attached for one second, autostop */ |
| 423 | } else { |
| 424 | if (changed || any_connected) { |
| 425 | ohci->autostop = 0; |
| 426 | ohci->next_statechange = jiffies + |
| 427 | STATECHANGE_DELAY; |
Alan Stern | 3da2495 | 2006-11-14 16:28:01 -0500 | [diff] [blame] | 428 | } else if (device_may_wakeup(&hcd->self.root_hub->dev) |
| 429 | && time_after_eq(jiffies, |
Alan Stern | 8d1a243 | 2006-09-26 14:46:16 -0400 | [diff] [blame] | 430 | ohci->next_statechange) |
| 431 | && !ohci->ed_rm_list |
| 432 | && !(ohci->hc_control & |
| 433 | OHCI_SCHED_ENABLES)) { |
| 434 | ohci_rh_suspend (ohci, 1); |
| 435 | } |
| 436 | } |
| 437 | break; |
| 438 | |
| 439 | /* if there is a port change, autostart or ask to be resumed */ |
| 440 | case OHCI_USB_SUSPEND: |
| 441 | case OHCI_USB_RESUME: |
| 442 | if (changed) { |
| 443 | if (ohci->autostop) |
| 444 | ohci_rh_resume (ohci); |
| 445 | else |
| 446 | usb_hcd_resume_root_hub (hcd); |
| 447 | } else { |
| 448 | /* everything is idle, no need for polling */ |
| 449 | hcd->poll_rh = 0; |
| 450 | } |
| 451 | break; |
| 452 | } |
David Brownell | d413984 | 2006-08-04 11:31:55 -0700 | [diff] [blame] | 453 | |
| 454 | done: |
| 455 | spin_unlock_irqrestore (&ohci->lock, flags); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return changed ? length : 0; |
| 458 | } |
| 459 | |
| 460 | /*-------------------------------------------------------------------------*/ |
| 461 | |
| 462 | static void |
| 463 | ohci_hub_descriptor ( |
| 464 | struct ohci_hcd *ohci, |
| 465 | struct usb_hub_descriptor *desc |
| 466 | ) { |
| 467 | u32 rh = roothub_a (ohci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | u16 temp; |
| 469 | |
| 470 | desc->bDescriptorType = 0x29; |
| 471 | desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24; |
| 472 | desc->bHubContrCurrent = 0; |
| 473 | |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 474 | desc->bNbrPorts = ohci->num_ports; |
| 475 | temp = 1 + (ohci->num_ports / 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | desc->bDescLength = 7 + 2 * temp; |
| 477 | |
| 478 | temp = 0; |
| 479 | if (rh & RH_A_NPS) /* no power switching? */ |
| 480 | temp |= 0x0002; |
| 481 | if (rh & RH_A_PSM) /* per-port power switching? */ |
| 482 | temp |= 0x0001; |
| 483 | if (rh & RH_A_NOCP) /* no overcurrent reporting? */ |
| 484 | temp |= 0x0010; |
| 485 | else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */ |
| 486 | temp |= 0x0008; |
| 487 | desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp); |
| 488 | |
| 489 | /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ |
| 490 | rh = roothub_b (ohci); |
KAMBAROV, ZAUR | b2134bc | 2005-06-24 22:20:35 -0700 | [diff] [blame] | 491 | memset(desc->bitmap, 0xff, sizeof(desc->bitmap)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | desc->bitmap [0] = rh & RH_B_DR; |
David Brownell | fdd13b3 | 2005-08-31 11:52:57 -0700 | [diff] [blame] | 493 | if (ohci->num_ports > 7) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | desc->bitmap [1] = (rh & RH_B_DR) >> 8; |
KAMBAROV, ZAUR | b2134bc | 2005-06-24 22:20:35 -0700 | [diff] [blame] | 495 | desc->bitmap [2] = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } else |
| 497 | desc->bitmap [1] = 0xff; |
| 498 | } |
| 499 | |
| 500 | /*-------------------------------------------------------------------------*/ |
| 501 | |
| 502 | #ifdef CONFIG_USB_OTG |
| 503 | |
| 504 | static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port) |
| 505 | { |
| 506 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 507 | u32 status; |
| 508 | |
| 509 | if (!port) |
| 510 | return -EINVAL; |
| 511 | port--; |
| 512 | |
| 513 | /* start port reset before HNP protocol times out */ |
| 514 | status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]); |
| 515 | if (!(status & RH_PS_CCS)) |
| 516 | return -ENODEV; |
| 517 | |
| 518 | /* khubd will finish the reset later */ |
| 519 | ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static void start_hnp(struct ohci_hcd *ohci); |
| 524 | |
| 525 | #else |
| 526 | |
| 527 | #define ohci_start_port_reset NULL |
| 528 | |
| 529 | #endif |
| 530 | |
| 531 | /*-------------------------------------------------------------------------*/ |
| 532 | |
| 533 | |
| 534 | /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling, |
| 535 | * not necessarily continuous ... to guard against resume signaling. |
| 536 | * The short timeout is safe for non-root hubs, and is backward-compatible |
| 537 | * with earlier Linux hosts. |
| 538 | */ |
| 539 | #ifdef CONFIG_USB_SUSPEND |
| 540 | #define PORT_RESET_MSEC 50 |
| 541 | #else |
| 542 | #define PORT_RESET_MSEC 10 |
| 543 | #endif |
| 544 | |
| 545 | /* this timer value might be vendor-specific ... */ |
| 546 | #define PORT_RESET_HW_MSEC 10 |
| 547 | |
| 548 | /* wrap-aware logic morphed from <linux/jiffies.h> */ |
| 549 | #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0) |
| 550 | |
| 551 | /* called from some task, normally khubd */ |
| 552 | static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port) |
| 553 | { |
| 554 | __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port]; |
| 555 | u32 temp; |
| 556 | u16 now = ohci_readl(ohci, &ohci->regs->fmnumber); |
| 557 | u16 reset_done = now + PORT_RESET_MSEC; |
| 558 | |
| 559 | /* build a "continuous enough" reset signal, with up to |
| 560 | * 3msec gap between pulses. scheduler HZ==100 must work; |
| 561 | * this might need to be deadline-scheduled. |
| 562 | */ |
| 563 | do { |
| 564 | /* spin until any current reset finishes */ |
| 565 | for (;;) { |
| 566 | temp = ohci_readl (ohci, portstat); |
| 567 | if (!(temp & RH_PS_PRS)) |
| 568 | break; |
| 569 | udelay (500); |
| 570 | } |
| 571 | |
| 572 | if (!(temp & RH_PS_CCS)) |
| 573 | break; |
| 574 | if (temp & RH_PS_PRSC) |
| 575 | ohci_writel (ohci, RH_PS_PRSC, portstat); |
| 576 | |
| 577 | /* start the next reset, sleep till it's probably done */ |
| 578 | ohci_writel (ohci, RH_PS_PRS, portstat); |
| 579 | msleep(PORT_RESET_HW_MSEC); |
| 580 | now = ohci_readl(ohci, &ohci->regs->fmnumber); |
| 581 | } while (tick_before(now, reset_done)); |
| 582 | /* caller synchronizes using PRSC */ |
| 583 | } |
| 584 | |
| 585 | static int ohci_hub_control ( |
| 586 | struct usb_hcd *hcd, |
| 587 | u16 typeReq, |
| 588 | u16 wValue, |
| 589 | u16 wIndex, |
| 590 | char *buf, |
| 591 | u16 wLength |
| 592 | ) { |
| 593 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 594 | int ports = hcd_to_bus (hcd)->root_hub->maxchild; |
| 595 | u32 temp; |
| 596 | int retval = 0; |
| 597 | |
Benjamin Herrenschmidt | 8de9840 | 2005-11-25 09:59:46 +1100 | [diff] [blame] | 598 | if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) |
| 599 | return -ESHUTDOWN; |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | switch (typeReq) { |
| 602 | case ClearHubFeature: |
| 603 | switch (wValue) { |
| 604 | case C_HUB_OVER_CURRENT: |
| 605 | ohci_writel (ohci, RH_HS_OCIC, |
| 606 | &ohci->regs->roothub.status); |
| 607 | case C_HUB_LOCAL_POWER: |
| 608 | break; |
| 609 | default: |
| 610 | goto error; |
| 611 | } |
| 612 | break; |
| 613 | case ClearPortFeature: |
| 614 | if (!wIndex || wIndex > ports) |
| 615 | goto error; |
| 616 | wIndex--; |
| 617 | |
| 618 | switch (wValue) { |
| 619 | case USB_PORT_FEAT_ENABLE: |
| 620 | temp = RH_PS_CCS; |
| 621 | break; |
| 622 | case USB_PORT_FEAT_C_ENABLE: |
| 623 | temp = RH_PS_PESC; |
| 624 | break; |
| 625 | case USB_PORT_FEAT_SUSPEND: |
| 626 | temp = RH_PS_POCI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | break; |
| 628 | case USB_PORT_FEAT_C_SUSPEND: |
| 629 | temp = RH_PS_PSSC; |
| 630 | break; |
| 631 | case USB_PORT_FEAT_POWER: |
| 632 | temp = RH_PS_LSDA; |
| 633 | break; |
| 634 | case USB_PORT_FEAT_C_CONNECTION: |
| 635 | temp = RH_PS_CSC; |
| 636 | break; |
| 637 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 638 | temp = RH_PS_OCIC; |
| 639 | break; |
| 640 | case USB_PORT_FEAT_C_RESET: |
| 641 | temp = RH_PS_PRSC; |
| 642 | break; |
| 643 | default: |
| 644 | goto error; |
| 645 | } |
| 646 | ohci_writel (ohci, temp, |
| 647 | &ohci->regs->roothub.portstatus [wIndex]); |
| 648 | // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]); |
| 649 | break; |
| 650 | case GetHubDescriptor: |
| 651 | ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf); |
| 652 | break; |
| 653 | case GetHubStatus: |
| 654 | temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE); |
David Miller | 92164c5d | 2006-06-21 22:26:09 -0700 | [diff] [blame] | 655 | put_unaligned(cpu_to_le32 (temp), (__le32 *) buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | break; |
| 657 | case GetPortStatus: |
| 658 | if (!wIndex || wIndex > ports) |
| 659 | goto error; |
| 660 | wIndex--; |
| 661 | temp = roothub_portstatus (ohci, wIndex); |
David Miller | 92164c5d | 2006-06-21 22:26:09 -0700 | [diff] [blame] | 662 | put_unaligned(cpu_to_le32 (temp), (__le32 *) buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | #ifndef OHCI_VERBOSE_DEBUG |
| 665 | if (*(u16*)(buf+2)) /* only if wPortChange is interesting */ |
| 666 | #endif |
| 667 | dbg_port (ohci, "GetStatus", wIndex, temp); |
| 668 | break; |
| 669 | case SetHubFeature: |
| 670 | switch (wValue) { |
| 671 | case C_HUB_OVER_CURRENT: |
| 672 | // FIXME: this can be cleared, yes? |
| 673 | case C_HUB_LOCAL_POWER: |
| 674 | break; |
| 675 | default: |
| 676 | goto error; |
| 677 | } |
| 678 | break; |
| 679 | case SetPortFeature: |
| 680 | if (!wIndex || wIndex > ports) |
| 681 | goto error; |
| 682 | wIndex--; |
| 683 | switch (wValue) { |
| 684 | case USB_PORT_FEAT_SUSPEND: |
| 685 | #ifdef CONFIG_USB_OTG |
| 686 | if (hcd->self.otg_port == (wIndex + 1) |
| 687 | && hcd->self.b_hnp_enable) |
| 688 | start_hnp(ohci); |
| 689 | else |
| 690 | #endif |
| 691 | ohci_writel (ohci, RH_PS_PSS, |
| 692 | &ohci->regs->roothub.portstatus [wIndex]); |
| 693 | break; |
| 694 | case USB_PORT_FEAT_POWER: |
| 695 | ohci_writel (ohci, RH_PS_PPS, |
| 696 | &ohci->regs->roothub.portstatus [wIndex]); |
| 697 | break; |
| 698 | case USB_PORT_FEAT_RESET: |
| 699 | root_port_reset (ohci, wIndex); |
| 700 | break; |
| 701 | default: |
| 702 | goto error; |
| 703 | } |
| 704 | break; |
| 705 | |
| 706 | default: |
| 707 | error: |
| 708 | /* "protocol stall" on error */ |
| 709 | retval = -EPIPE; |
| 710 | } |
| 711 | return retval; |
| 712 | } |
| 713 | |