ehci-hub.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI Root Hub ... the nonsharable stuff
  22. *
  23. * Registers don't need cpu_to_le32, that happens transparently
  24. */
  25. /*-------------------------------------------------------------------------*/
  26. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  27. #ifdef CONFIG_PM
  28. static int ehci_hub_control(
  29. struct usb_hcd *hcd,
  30. u16 typeReq,
  31. u16 wValue,
  32. u16 wIndex,
  33. char *buf,
  34. u16 wLength
  35. );
  36. /* After a power loss, ports that were owned by the companion must be
  37. * reset so that the companion can still own them.
  38. */
  39. static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  40. {
  41. u32 __iomem *reg;
  42. u32 status;
  43. int port;
  44. __le32 buf;
  45. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  46. if (!ehci->owned_ports)
  47. return;
  48. /* Give the connections some time to appear */
  49. msleep(20);
  50. port = HCS_N_PORTS(ehci->hcs_params);
  51. while (port--) {
  52. if (test_bit(port, &ehci->owned_ports)) {
  53. reg = &ehci->regs->port_status[port];
  54. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  55. /* Port already owned by companion? */
  56. if (status & PORT_OWNER)
  57. clear_bit(port, &ehci->owned_ports);
  58. else if (test_bit(port, &ehci->companion_ports))
  59. ehci_writel(ehci, status & ~PORT_PE, reg);
  60. else
  61. ehci_hub_control(hcd, SetPortFeature,
  62. USB_PORT_FEAT_RESET, port + 1,
  63. NULL, 0);
  64. }
  65. }
  66. if (!ehci->owned_ports)
  67. return;
  68. msleep(90); /* Wait for resets to complete */
  69. port = HCS_N_PORTS(ehci->hcs_params);
  70. while (port--) {
  71. if (test_bit(port, &ehci->owned_ports)) {
  72. ehci_hub_control(hcd, GetPortStatus,
  73. 0, port + 1,
  74. (char *) &buf, sizeof(buf));
  75. /* The companion should now own the port,
  76. * but if something went wrong the port must not
  77. * remain enabled.
  78. */
  79. reg = &ehci->regs->port_status[port];
  80. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  81. if (status & PORT_OWNER)
  82. ehci_writel(ehci, status | PORT_CSC, reg);
  83. else {
  84. ehci_dbg(ehci, "failed handover port %d: %x\n",
  85. port + 1, status);
  86. ehci_writel(ehci, status & ~PORT_PE, reg);
  87. }
  88. }
  89. }
  90. ehci->owned_ports = 0;
  91. }
  92. static int ehci_bus_suspend (struct usb_hcd *hcd)
  93. {
  94. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  95. int port;
  96. int mask;
  97. ehci_dbg(ehci, "suspend root hub\n");
  98. if (time_before (jiffies, ehci->next_statechange))
  99. msleep(5);
  100. del_timer_sync(&ehci->watchdog);
  101. del_timer_sync(&ehci->iaa_watchdog);
  102. port = HCS_N_PORTS (ehci->hcs_params);
  103. spin_lock_irq (&ehci->lock);
  104. /* stop schedules, clean any completed work */
  105. if (HC_IS_RUNNING(hcd->state)) {
  106. ehci_quiesce (ehci);
  107. hcd->state = HC_STATE_QUIESCING;
  108. }
  109. ehci->command = ehci_readl(ehci, &ehci->regs->command);
  110. ehci_work(ehci);
  111. /* Unlike other USB host controller types, EHCI doesn't have
  112. * any notion of "global" or bus-wide suspend. The driver has
  113. * to manually suspend all the active unsuspended ports, and
  114. * then manually resume them in the bus_resume() routine.
  115. */
  116. ehci->bus_suspended = 0;
  117. ehci->owned_ports = 0;
  118. while (port--) {
  119. u32 __iomem *reg = &ehci->regs->port_status [port];
  120. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  121. u32 t2 = t1;
  122. /* keep track of which ports we suspend */
  123. if (t1 & PORT_OWNER)
  124. set_bit(port, &ehci->owned_ports);
  125. else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
  126. t2 |= PORT_SUSPEND;
  127. set_bit(port, &ehci->bus_suspended);
  128. }
  129. /* enable remote wakeup on all ports */
  130. if (hcd->self.root_hub->do_remote_wakeup)
  131. t2 |= PORT_WAKE_BITS;
  132. else
  133. t2 &= ~PORT_WAKE_BITS;
  134. if (t1 != t2) {
  135. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  136. port + 1, t1, t2);
  137. ehci_writel(ehci, t2, reg);
  138. }
  139. }
  140. /* Apparently some devices need a >= 1-uframe delay here */
  141. if (ehci->bus_suspended)
  142. udelay(150);
  143. /* turn off now-idle HC */
  144. ehci_halt (ehci);
  145. hcd->state = HC_STATE_SUSPENDED;
  146. if (ehci->reclaim)
  147. end_unlink_async(ehci);
  148. /* allow remote wakeup */
  149. mask = INTR_MASK;
  150. if (!hcd->self.root_hub->do_remote_wakeup)
  151. mask &= ~STS_PCD;
  152. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  153. ehci_readl(ehci, &ehci->regs->intr_enable);
  154. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  155. spin_unlock_irq (&ehci->lock);
  156. return 0;
  157. }
  158. /* caller has locked the root hub, and should reset/reinit on error */
  159. static int ehci_bus_resume (struct usb_hcd *hcd)
  160. {
  161. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  162. u32 temp;
  163. u32 power_okay;
  164. int i;
  165. u8 resume_needed = 0;
  166. if (time_before (jiffies, ehci->next_statechange))
  167. msleep(5);
  168. spin_lock_irq (&ehci->lock);
  169. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  170. spin_unlock_irq(&ehci->lock);
  171. return -ESHUTDOWN;
  172. }
  173. /* Ideally and we've got a real resume here, and no port's power
  174. * was lost. (For PCI, that means Vaux was maintained.) But we
  175. * could instead be restoring a swsusp snapshot -- so that BIOS was
  176. * the last user of the controller, not reset/pm hardware keeping
  177. * state we gave to it.
  178. */
  179. power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
  180. ehci_dbg(ehci, "resume root hub%s\n",
  181. power_okay ? "" : " after power loss");
  182. /* at least some APM implementations will try to deliver
  183. * IRQs right away, so delay them until we're ready.
  184. */
  185. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  186. /* re-init operational registers */
  187. ehci_writel(ehci, 0, &ehci->regs->segment);
  188. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  189. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  190. /* restore CMD_RUN, framelist size, and irq threshold */
  191. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  192. /* Some controller/firmware combinations need a delay during which
  193. * they set up the port statuses. See Bugzilla #8190. */
  194. spin_unlock_irq(&ehci->lock);
  195. msleep(8);
  196. spin_lock_irq(&ehci->lock);
  197. /* manually resume the ports we suspended during bus_suspend() */
  198. i = HCS_N_PORTS (ehci->hcs_params);
  199. while (i--) {
  200. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  201. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  202. if (test_bit(i, &ehci->bus_suspended) &&
  203. (temp & PORT_SUSPEND)) {
  204. temp |= PORT_RESUME;
  205. resume_needed = 1;
  206. }
  207. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  208. }
  209. /* msleep for 20ms only if code is trying to resume port */
  210. if (resume_needed) {
  211. spin_unlock_irq(&ehci->lock);
  212. msleep(20);
  213. spin_lock_irq(&ehci->lock);
  214. }
  215. i = HCS_N_PORTS (ehci->hcs_params);
  216. while (i--) {
  217. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  218. if (test_bit(i, &ehci->bus_suspended) &&
  219. (temp & PORT_SUSPEND)) {
  220. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  221. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  222. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  223. }
  224. }
  225. (void) ehci_readl(ehci, &ehci->regs->command);
  226. /* maybe re-activate the schedule(s) */
  227. temp = 0;
  228. if (ehci->async->qh_next.qh)
  229. temp |= CMD_ASE;
  230. if (ehci->periodic_sched)
  231. temp |= CMD_PSE;
  232. if (temp) {
  233. ehci->command |= temp;
  234. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  235. }
  236. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  237. hcd->state = HC_STATE_RUNNING;
  238. /* Now we can safely re-enable irqs */
  239. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  240. spin_unlock_irq (&ehci->lock);
  241. ehci_handover_companion_ports(ehci);
  242. return 0;
  243. }
  244. #else
  245. #define ehci_bus_suspend NULL
  246. #define ehci_bus_resume NULL
  247. #endif /* CONFIG_PM */
  248. /*-------------------------------------------------------------------------*/
  249. /* Display the ports dedicated to the companion controller */
  250. static ssize_t show_companion(struct device *dev,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct ehci_hcd *ehci;
  255. int nports, index, n;
  256. int count = PAGE_SIZE;
  257. char *ptr = buf;
  258. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  259. nports = HCS_N_PORTS(ehci->hcs_params);
  260. for (index = 0; index < nports; ++index) {
  261. if (test_bit(index, &ehci->companion_ports)) {
  262. n = scnprintf(ptr, count, "%d\n", index + 1);
  263. ptr += n;
  264. count -= n;
  265. }
  266. }
  267. return ptr - buf;
  268. }
  269. /*
  270. * Sets the owner of a port
  271. */
  272. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  273. {
  274. u32 __iomem *status_reg;
  275. u32 port_status;
  276. int try;
  277. status_reg = &ehci->regs->port_status[portnum];
  278. /*
  279. * The controller won't set the OWNER bit if the port is
  280. * enabled, so this loop will sometimes require at least two
  281. * iterations: one to disable the port and one to set OWNER.
  282. */
  283. for (try = 4; try > 0; --try) {
  284. spin_lock_irq(&ehci->lock);
  285. port_status = ehci_readl(ehci, status_reg);
  286. if ((port_status & PORT_OWNER) == new_owner
  287. || (port_status & (PORT_OWNER | PORT_CONNECT))
  288. == 0)
  289. try = 0;
  290. else {
  291. port_status ^= PORT_OWNER;
  292. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  293. ehci_writel(ehci, port_status, status_reg);
  294. }
  295. spin_unlock_irq(&ehci->lock);
  296. if (try > 1)
  297. msleep(5);
  298. }
  299. }
  300. /*
  301. * Dedicate or undedicate a port to the companion controller.
  302. * Syntax is "[-]portnum", where a leading '-' sign means
  303. * return control of the port to the EHCI controller.
  304. */
  305. static ssize_t store_companion(struct device *dev,
  306. struct device_attribute *attr,
  307. const char *buf, size_t count)
  308. {
  309. struct ehci_hcd *ehci;
  310. int portnum, new_owner;
  311. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  312. new_owner = PORT_OWNER; /* Owned by companion */
  313. if (sscanf(buf, "%d", &portnum) != 1)
  314. return -EINVAL;
  315. if (portnum < 0) {
  316. portnum = - portnum;
  317. new_owner = 0; /* Owned by EHCI */
  318. }
  319. if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
  320. return -ENOENT;
  321. portnum--;
  322. if (new_owner)
  323. set_bit(portnum, &ehci->companion_ports);
  324. else
  325. clear_bit(portnum, &ehci->companion_ports);
  326. set_owner(ehci, portnum, new_owner);
  327. return count;
  328. }
  329. static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
  330. static inline void create_companion_file(struct ehci_hcd *ehci)
  331. {
  332. int i;
  333. /* with integrated TT there is no companion! */
  334. if (!ehci_is_TDI(ehci))
  335. i = device_create_file(ehci_to_hcd(ehci)->self.controller,
  336. &dev_attr_companion);
  337. }
  338. static inline void remove_companion_file(struct ehci_hcd *ehci)
  339. {
  340. /* with integrated TT there is no companion! */
  341. if (!ehci_is_TDI(ehci))
  342. device_remove_file(ehci_to_hcd(ehci)->self.controller,
  343. &dev_attr_companion);
  344. }
  345. /*-------------------------------------------------------------------------*/
  346. static int check_reset_complete (
  347. struct ehci_hcd *ehci,
  348. int index,
  349. u32 __iomem *status_reg,
  350. int port_status
  351. ) {
  352. if (!(port_status & PORT_CONNECT))
  353. return port_status;
  354. /* if reset finished and it's still not enabled -- handoff */
  355. if (!(port_status & PORT_PE)) {
  356. /* with integrated TT, there's nobody to hand it to! */
  357. if (ehci_is_TDI(ehci)) {
  358. ehci_dbg (ehci,
  359. "Failed to enable port %d on root hub TT\n",
  360. index+1);
  361. return port_status;
  362. }
  363. ehci_dbg (ehci, "port %d full speed --> companion\n",
  364. index + 1);
  365. // what happens if HCS_N_CC(params) == 0 ?
  366. port_status |= PORT_OWNER;
  367. port_status &= ~PORT_RWC_BITS;
  368. ehci_writel(ehci, port_status, status_reg);
  369. /* ensure 440EPX ohci controller state is operational */
  370. if (ehci->has_amcc_usb23)
  371. set_ohci_hcfs(ehci, 1);
  372. } else {
  373. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  374. /* ensure 440EPx ohci controller state is suspended */
  375. if (ehci->has_amcc_usb23)
  376. set_ohci_hcfs(ehci, 0);
  377. }
  378. return port_status;
  379. }
  380. /*-------------------------------------------------------------------------*/
  381. /* build "status change" packet (one or two bytes) from HC registers */
  382. static int
  383. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  384. {
  385. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  386. u32 temp, status = 0;
  387. u32 mask;
  388. int ports, i, retval = 1;
  389. unsigned long flags;
  390. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  391. if (!HC_IS_RUNNING(hcd->state))
  392. return 0;
  393. /* init status to no-changes */
  394. buf [0] = 0;
  395. ports = HCS_N_PORTS (ehci->hcs_params);
  396. if (ports > 7) {
  397. buf [1] = 0;
  398. retval++;
  399. }
  400. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  401. * causing massive log spam unless we completely ignore them. It
  402. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  403. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  404. * PORT_POWER; that's surprising, but maybe within-spec.
  405. */
  406. if (!ignore_oc)
  407. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  408. else
  409. mask = PORT_CSC | PORT_PEC;
  410. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  411. /* no hub change reports (bit 0) for now (power, ...) */
  412. /* port N changes (bit N)? */
  413. spin_lock_irqsave (&ehci->lock, flags);
  414. for (i = 0; i < ports; i++) {
  415. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  416. /*
  417. * Return status information even for ports with OWNER set.
  418. * Otherwise khubd wouldn't see the disconnect event when a
  419. * high-speed device is switched over to the companion
  420. * controller by the user.
  421. */
  422. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  423. || (ehci->reset_done[i] && time_after_eq(
  424. jiffies, ehci->reset_done[i]))) {
  425. if (i < 7)
  426. buf [0] |= 1 << (i + 1);
  427. else
  428. buf [1] |= 1 << (i - 7);
  429. status = STS_PCD;
  430. }
  431. }
  432. /* FIXME autosuspend idle root hubs */
  433. spin_unlock_irqrestore (&ehci->lock, flags);
  434. return status ? retval : 0;
  435. }
  436. /*-------------------------------------------------------------------------*/
  437. static void
  438. ehci_hub_descriptor (
  439. struct ehci_hcd *ehci,
  440. struct usb_hub_descriptor *desc
  441. ) {
  442. int ports = HCS_N_PORTS (ehci->hcs_params);
  443. u16 temp;
  444. desc->bDescriptorType = 0x29;
  445. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  446. desc->bHubContrCurrent = 0;
  447. desc->bNbrPorts = ports;
  448. temp = 1 + (ports / 8);
  449. desc->bDescLength = 7 + 2 * temp;
  450. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  451. memset (&desc->bitmap [0], 0, temp);
  452. memset (&desc->bitmap [temp], 0xff, temp);
  453. temp = 0x0008; /* per-port overcurrent reporting */
  454. if (HCS_PPC (ehci->hcs_params))
  455. temp |= 0x0001; /* per-port power control */
  456. else
  457. temp |= 0x0002; /* no power switching */
  458. #if 0
  459. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  460. if (HCS_INDICATOR (ehci->hcs_params))
  461. temp |= 0x0080; /* per-port indicators (LEDs) */
  462. #endif
  463. desc->wHubCharacteristics = cpu_to_le16(temp);
  464. }
  465. /*-------------------------------------------------------------------------*/
  466. static int ehci_hub_control (
  467. struct usb_hcd *hcd,
  468. u16 typeReq,
  469. u16 wValue,
  470. u16 wIndex,
  471. char *buf,
  472. u16 wLength
  473. ) {
  474. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  475. int ports = HCS_N_PORTS (ehci->hcs_params);
  476. u32 __iomem *status_reg = &ehci->regs->port_status[
  477. (wIndex & 0xff) - 1];
  478. u32 temp, status;
  479. unsigned long flags;
  480. int retval = 0;
  481. unsigned selector;
  482. /*
  483. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  484. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  485. * (track current state ourselves) ... blink for diagnostics,
  486. * power, "this is the one", etc. EHCI spec supports this.
  487. */
  488. spin_lock_irqsave (&ehci->lock, flags);
  489. switch (typeReq) {
  490. case ClearHubFeature:
  491. switch (wValue) {
  492. case C_HUB_LOCAL_POWER:
  493. case C_HUB_OVER_CURRENT:
  494. /* no hub-wide feature/status flags */
  495. break;
  496. default:
  497. goto error;
  498. }
  499. break;
  500. case ClearPortFeature:
  501. if (!wIndex || wIndex > ports)
  502. goto error;
  503. wIndex--;
  504. temp = ehci_readl(ehci, status_reg);
  505. /*
  506. * Even if OWNER is set, so the port is owned by the
  507. * companion controller, khubd needs to be able to clear
  508. * the port-change status bits (especially
  509. * USB_PORT_FEAT_C_CONNECTION).
  510. */
  511. switch (wValue) {
  512. case USB_PORT_FEAT_ENABLE:
  513. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  514. break;
  515. case USB_PORT_FEAT_C_ENABLE:
  516. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
  517. status_reg);
  518. break;
  519. case USB_PORT_FEAT_SUSPEND:
  520. if (temp & PORT_RESET)
  521. goto error;
  522. if (ehci->no_selective_suspend)
  523. break;
  524. if (temp & PORT_SUSPEND) {
  525. if ((temp & PORT_PE) == 0)
  526. goto error;
  527. /* resume signaling for 20 msec */
  528. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  529. ehci_writel(ehci, temp | PORT_RESUME,
  530. status_reg);
  531. ehci->reset_done [wIndex] = jiffies
  532. + msecs_to_jiffies (20);
  533. }
  534. break;
  535. case USB_PORT_FEAT_C_SUSPEND:
  536. clear_bit(wIndex, &ehci->port_c_suspend);
  537. break;
  538. case USB_PORT_FEAT_POWER:
  539. if (HCS_PPC (ehci->hcs_params))
  540. ehci_writel(ehci,
  541. temp & ~(PORT_RWC_BITS | PORT_POWER),
  542. status_reg);
  543. break;
  544. case USB_PORT_FEAT_C_CONNECTION:
  545. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
  546. status_reg);
  547. break;
  548. case USB_PORT_FEAT_C_OVER_CURRENT:
  549. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
  550. status_reg);
  551. break;
  552. case USB_PORT_FEAT_C_RESET:
  553. /* GetPortStatus clears reset */
  554. break;
  555. default:
  556. goto error;
  557. }
  558. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  559. break;
  560. case GetHubDescriptor:
  561. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  562. buf);
  563. break;
  564. case GetHubStatus:
  565. /* no hub-wide feature/status flags */
  566. memset (buf, 0, 4);
  567. //cpu_to_le32s ((u32 *) buf);
  568. break;
  569. case GetPortStatus:
  570. if (!wIndex || wIndex > ports)
  571. goto error;
  572. wIndex--;
  573. status = 0;
  574. temp = ehci_readl(ehci, status_reg);
  575. // wPortChange bits
  576. if (temp & PORT_CSC)
  577. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  578. if (temp & PORT_PEC)
  579. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  580. if ((temp & PORT_OCC) && !ignore_oc){
  581. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  582. /*
  583. * Hubs should disable port power on over-current.
  584. * However, not all EHCI implementations do this
  585. * automatically, even if they _do_ support per-port
  586. * power switching; they're allowed to just limit the
  587. * current. khubd will turn the power back on.
  588. */
  589. if (HCS_PPC (ehci->hcs_params)){
  590. ehci_writel(ehci,
  591. temp & ~(PORT_RWC_BITS | PORT_POWER),
  592. status_reg);
  593. }
  594. }
  595. /* whoever resumes must GetPortStatus to complete it!! */
  596. if (temp & PORT_RESUME) {
  597. /* Remote Wakeup received? */
  598. if (!ehci->reset_done[wIndex]) {
  599. /* resume signaling for 20 msec */
  600. ehci->reset_done[wIndex] = jiffies
  601. + msecs_to_jiffies(20);
  602. /* check the port again */
  603. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  604. ehci->reset_done[wIndex]);
  605. }
  606. /* resume completed? */
  607. else if (time_after_eq(jiffies,
  608. ehci->reset_done[wIndex])) {
  609. clear_bit(wIndex, &ehci->suspended_ports);
  610. set_bit(wIndex, &ehci->port_c_suspend);
  611. ehci->reset_done[wIndex] = 0;
  612. /* stop resume signaling */
  613. temp = ehci_readl(ehci, status_reg);
  614. ehci_writel(ehci,
  615. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  616. status_reg);
  617. retval = handshake(ehci, status_reg,
  618. PORT_RESUME, 0, 2000 /* 2msec */);
  619. if (retval != 0) {
  620. ehci_err(ehci,
  621. "port %d resume error %d\n",
  622. wIndex + 1, retval);
  623. goto error;
  624. }
  625. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  626. }
  627. }
  628. /* whoever resets must GetPortStatus to complete it!! */
  629. if ((temp & PORT_RESET)
  630. && time_after_eq(jiffies,
  631. ehci->reset_done[wIndex])) {
  632. status |= 1 << USB_PORT_FEAT_C_RESET;
  633. ehci->reset_done [wIndex] = 0;
  634. /* force reset to complete */
  635. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  636. status_reg);
  637. /* REVISIT: some hardware needs 550+ usec to clear
  638. * this bit; seems too long to spin routinely...
  639. */
  640. retval = handshake(ehci, status_reg,
  641. PORT_RESET, 0, 750);
  642. if (retval != 0) {
  643. ehci_err (ehci, "port %d reset error %d\n",
  644. wIndex + 1, retval);
  645. goto error;
  646. }
  647. /* see what we found out */
  648. temp = check_reset_complete (ehci, wIndex, status_reg,
  649. ehci_readl(ehci, status_reg));
  650. }
  651. if (!(temp & (PORT_RESUME|PORT_RESET)))
  652. ehci->reset_done[wIndex] = 0;
  653. /* transfer dedicated ports to the companion hc */
  654. if ((temp & PORT_CONNECT) &&
  655. test_bit(wIndex, &ehci->companion_ports)) {
  656. temp &= ~PORT_RWC_BITS;
  657. temp |= PORT_OWNER;
  658. ehci_writel(ehci, temp, status_reg);
  659. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  660. temp = ehci_readl(ehci, status_reg);
  661. }
  662. /*
  663. * Even if OWNER is set, there's no harm letting khubd
  664. * see the wPortStatus values (they should all be 0 except
  665. * for PORT_POWER anyway).
  666. */
  667. if (temp & PORT_CONNECT) {
  668. status |= 1 << USB_PORT_FEAT_CONNECTION;
  669. // status may be from integrated TT
  670. status |= ehci_port_speed(ehci, temp);
  671. }
  672. if (temp & PORT_PE)
  673. status |= 1 << USB_PORT_FEAT_ENABLE;
  674. /* maybe the port was unsuspended without our knowledge */
  675. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  676. status |= 1 << USB_PORT_FEAT_SUSPEND;
  677. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  678. clear_bit(wIndex, &ehci->suspended_ports);
  679. ehci->reset_done[wIndex] = 0;
  680. if (temp & PORT_PE)
  681. set_bit(wIndex, &ehci->port_c_suspend);
  682. }
  683. if (temp & PORT_OC)
  684. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  685. if (temp & PORT_RESET)
  686. status |= 1 << USB_PORT_FEAT_RESET;
  687. if (temp & PORT_POWER)
  688. status |= 1 << USB_PORT_FEAT_POWER;
  689. if (test_bit(wIndex, &ehci->port_c_suspend))
  690. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  691. #ifndef VERBOSE_DEBUG
  692. if (status & ~0xffff) /* only if wPortChange is interesting */
  693. #endif
  694. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  695. put_unaligned_le32(status, buf);
  696. break;
  697. case SetHubFeature:
  698. switch (wValue) {
  699. case C_HUB_LOCAL_POWER:
  700. case C_HUB_OVER_CURRENT:
  701. /* no hub-wide feature/status flags */
  702. break;
  703. default:
  704. goto error;
  705. }
  706. break;
  707. case SetPortFeature:
  708. selector = wIndex >> 8;
  709. wIndex &= 0xff;
  710. if (!wIndex || wIndex > ports)
  711. goto error;
  712. wIndex--;
  713. temp = ehci_readl(ehci, status_reg);
  714. if (temp & PORT_OWNER)
  715. break;
  716. temp &= ~PORT_RWC_BITS;
  717. switch (wValue) {
  718. case USB_PORT_FEAT_SUSPEND:
  719. if (ehci->no_selective_suspend)
  720. break;
  721. if ((temp & PORT_PE) == 0
  722. || (temp & PORT_RESET) != 0)
  723. goto error;
  724. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  725. set_bit(wIndex, &ehci->suspended_ports);
  726. break;
  727. case USB_PORT_FEAT_POWER:
  728. if (HCS_PPC (ehci->hcs_params))
  729. ehci_writel(ehci, temp | PORT_POWER,
  730. status_reg);
  731. break;
  732. case USB_PORT_FEAT_RESET:
  733. if (temp & PORT_RESUME)
  734. goto error;
  735. /* line status bits may report this as low speed,
  736. * which can be fine if this root hub has a
  737. * transaction translator built in.
  738. */
  739. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  740. && !ehci_is_TDI(ehci)
  741. && PORT_USB11 (temp)) {
  742. ehci_dbg (ehci,
  743. "port %d low speed --> companion\n",
  744. wIndex + 1);
  745. temp |= PORT_OWNER;
  746. } else {
  747. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  748. temp |= PORT_RESET;
  749. temp &= ~PORT_PE;
  750. /*
  751. * caller must wait, then call GetPortStatus
  752. * usb 2.0 spec says 50 ms resets on root
  753. */
  754. ehci->reset_done [wIndex] = jiffies
  755. + msecs_to_jiffies (50);
  756. }
  757. ehci_writel(ehci, temp, status_reg);
  758. break;
  759. /* For downstream facing ports (these): one hub port is put
  760. * into test mode according to USB2 11.24.2.13, then the hub
  761. * must be reset (which for root hub now means rmmod+modprobe,
  762. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  763. * about the EHCI-specific stuff.
  764. */
  765. case USB_PORT_FEAT_TEST:
  766. if (!selector || selector > 5)
  767. goto error;
  768. ehci_quiesce(ehci);
  769. ehci_halt(ehci);
  770. temp |= selector << 16;
  771. ehci_writel(ehci, temp, status_reg);
  772. break;
  773. default:
  774. goto error;
  775. }
  776. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  777. break;
  778. default:
  779. error:
  780. /* "stall" on error */
  781. retval = -EPIPE;
  782. }
  783. spin_unlock_irqrestore (&ehci->lock, flags);
  784. return retval;
  785. }
  786. static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
  787. {
  788. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  789. if (ehci_is_TDI(ehci))
  790. return;
  791. set_owner(ehci, --portnum, PORT_OWNER);
  792. }
  793. static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
  794. {
  795. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  796. u32 __iomem *reg;
  797. if (ehci_is_TDI(ehci))
  798. return 0;
  799. reg = &ehci->regs->port_status[portnum - 1];
  800. return ehci_readl(ehci, reg) & PORT_OWNER;
  801. }