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