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