uhci-hcd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Universal Host Controller Interface driver for USB.
  4. *
  5. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  6. *
  7. * (C) Copyright 1999 Linus Torvalds
  8. * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
  9. * (C) Copyright 1999 Randy Dunlap
  10. * (C) Copyright 1999 Georg Acher, acher@in.tum.de
  11. * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
  12. * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
  13. * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
  14. * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
  15. * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  16. * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
  17. * (C) Copyright 2004-2007 Alan Stern, stern@rowland.harvard.edu
  18. *
  19. * Intel documents this fairly well, and as far as I know there
  20. * are no royalties or anything like that, but even so there are
  21. * people who decided that they want to do the same thing in a
  22. * completely different way.
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/ioport.h>
  31. #include <linux/slab.h>
  32. #include <linux/errno.h>
  33. #include <linux/unistd.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/debugfs.h>
  37. #include <linux/pm.h>
  38. #include <linux/dmapool.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/usb.h>
  41. #include <linux/usb/hcd.h>
  42. #include <linux/bitops.h>
  43. #include <linux/dmi.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/io.h>
  46. #include <asm/irq.h>
  47. #include "uhci-hcd.h"
  48. /*
  49. * Version Information
  50. */
  51. #define DRIVER_AUTHOR \
  52. "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, " \
  53. "Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, " \
  54. "Roman Weissgaerber, Alan Stern"
  55. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  56. /* for flakey hardware, ignore overcurrent indicators */
  57. static bool ignore_oc;
  58. module_param(ignore_oc, bool, S_IRUGO);
  59. MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
  60. /*
  61. * debug = 0, no debugging messages
  62. * debug = 1, dump failed URBs except for stalls
  63. * debug = 2, dump all failed URBs (including stalls)
  64. * show all queues in /sys/kernel/debug/uhci/[pci_addr]
  65. * debug = 3, show all TDs in URBs when dumping
  66. */
  67. #ifdef CONFIG_DYNAMIC_DEBUG
  68. static int debug = 1;
  69. module_param(debug, int, S_IRUGO | S_IWUSR);
  70. MODULE_PARM_DESC(debug, "Debug level");
  71. static char *errbuf;
  72. #else
  73. #define debug 0
  74. #define errbuf NULL
  75. #endif
  76. #define ERRBUF_LEN (32 * 1024)
  77. static struct kmem_cache *uhci_up_cachep; /* urb_priv */
  78. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  79. static void wakeup_rh(struct uhci_hcd *uhci);
  80. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  81. /*
  82. * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
  83. */
  84. static __hc32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
  85. {
  86. int skelnum;
  87. /*
  88. * The interrupt queues will be interleaved as evenly as possible.
  89. * There's not much to be done about period-1 interrupts; they have
  90. * to occur in every frame. But we can schedule period-2 interrupts
  91. * in odd-numbered frames, period-4 interrupts in frames congruent
  92. * to 2 (mod 4), and so on. This way each frame only has two
  93. * interrupt QHs, which will help spread out bandwidth utilization.
  94. *
  95. * ffs (Find First bit Set) does exactly what we need:
  96. * 1,3,5,... => ffs = 0 => use period-2 QH = skelqh[8],
  97. * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
  98. * ffs >= 7 => not on any high-period queue, so use
  99. * period-1 QH = skelqh[9].
  100. * Add in UHCI_NUMFRAMES to insure at least one bit is set.
  101. */
  102. skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
  103. if (skelnum <= 1)
  104. skelnum = 9;
  105. return LINK_TO_QH(uhci, uhci->skelqh[skelnum]);
  106. }
  107. #include "uhci-debug.c"
  108. #include "uhci-q.c"
  109. #include "uhci-hub.c"
  110. /*
  111. * Finish up a host controller reset and update the recorded state.
  112. */
  113. static void finish_reset(struct uhci_hcd *uhci)
  114. {
  115. int port;
  116. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  117. * bits in the port status and control registers.
  118. * We have to clear them by hand.
  119. */
  120. for (port = 0; port < uhci->rh_numports; ++port)
  121. uhci_writew(uhci, 0, USBPORTSC1 + (port * 2));
  122. uhci->port_c_suspend = uhci->resuming_ports = 0;
  123. uhci->rh_state = UHCI_RH_RESET;
  124. uhci->is_stopped = UHCI_IS_STOPPED;
  125. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  126. }
  127. /*
  128. * Last rites for a defunct/nonfunctional controller
  129. * or one we don't want to use any more.
  130. */
  131. static void uhci_hc_died(struct uhci_hcd *uhci)
  132. {
  133. uhci_get_current_frame_number(uhci);
  134. uhci->reset_hc(uhci);
  135. finish_reset(uhci);
  136. uhci->dead = 1;
  137. /* The current frame may already be partway finished */
  138. ++uhci->frame_number;
  139. }
  140. /*
  141. * Initialize a controller that was newly discovered or has lost power
  142. * or otherwise been reset while it was suspended. In none of these cases
  143. * can we be sure of its previous state.
  144. */
  145. static void check_and_reset_hc(struct uhci_hcd *uhci)
  146. {
  147. if (uhci->check_and_reset_hc(uhci))
  148. finish_reset(uhci);
  149. }
  150. #if defined(CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC)
  151. /*
  152. * The two functions below are generic reset functions that are used on systems
  153. * that do not have keyboard and mouse legacy support. We assume that we are
  154. * running on such a system if CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC is defined.
  155. */
  156. /*
  157. * Make sure the controller is completely inactive, unable to
  158. * generate interrupts or do DMA.
  159. */
  160. static void uhci_generic_reset_hc(struct uhci_hcd *uhci)
  161. {
  162. /* Reset the HC - this will force us to get a
  163. * new notification of any already connected
  164. * ports due to the virtual disconnect that it
  165. * implies.
  166. */
  167. uhci_writew(uhci, USBCMD_HCRESET, USBCMD);
  168. mb();
  169. udelay(5);
  170. if (uhci_readw(uhci, USBCMD) & USBCMD_HCRESET)
  171. dev_warn(uhci_dev(uhci), "HCRESET not completed yet!\n");
  172. /* Just to be safe, disable interrupt requests and
  173. * make sure the controller is stopped.
  174. */
  175. uhci_writew(uhci, 0, USBINTR);
  176. uhci_writew(uhci, 0, USBCMD);
  177. }
  178. /*
  179. * Initialize a controller that was newly discovered or has just been
  180. * resumed. In either case we can't be sure of its previous state.
  181. *
  182. * Returns: 1 if the controller was reset, 0 otherwise.
  183. */
  184. static int uhci_generic_check_and_reset_hc(struct uhci_hcd *uhci)
  185. {
  186. unsigned int cmd, intr;
  187. /*
  188. * When restarting a suspended controller, we expect all the
  189. * settings to be the same as we left them:
  190. *
  191. * Controller is stopped and configured with EGSM set;
  192. * No interrupts enabled except possibly Resume Detect.
  193. *
  194. * If any of these conditions are violated we do a complete reset.
  195. */
  196. cmd = uhci_readw(uhci, USBCMD);
  197. if ((cmd & USBCMD_RS) || !(cmd & USBCMD_CF) || !(cmd & USBCMD_EGSM)) {
  198. dev_dbg(uhci_dev(uhci), "%s: cmd = 0x%04x\n",
  199. __func__, cmd);
  200. goto reset_needed;
  201. }
  202. intr = uhci_readw(uhci, USBINTR);
  203. if (intr & (~USBINTR_RESUME)) {
  204. dev_dbg(uhci_dev(uhci), "%s: intr = 0x%04x\n",
  205. __func__, intr);
  206. goto reset_needed;
  207. }
  208. return 0;
  209. reset_needed:
  210. dev_dbg(uhci_dev(uhci), "Performing full reset\n");
  211. uhci_generic_reset_hc(uhci);
  212. return 1;
  213. }
  214. #endif /* CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC */
  215. /*
  216. * Store the basic register settings needed by the controller.
  217. */
  218. static void configure_hc(struct uhci_hcd *uhci)
  219. {
  220. /* Set the frame length to the default: 1 ms exactly */
  221. uhci_writeb(uhci, USBSOF_DEFAULT, USBSOF);
  222. /* Store the frame list base address */
  223. uhci_writel(uhci, uhci->frame_dma_handle, USBFLBASEADD);
  224. /* Set the current frame number */
  225. uhci_writew(uhci, uhci->frame_number & UHCI_MAX_SOF_NUMBER,
  226. USBFRNUM);
  227. /* perform any arch/bus specific configuration */
  228. if (uhci->configure_hc)
  229. uhci->configure_hc(uhci);
  230. }
  231. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  232. {
  233. /*
  234. * If we have to ignore overcurrent events then almost by definition
  235. * we can't depend on resume-detect interrupts.
  236. *
  237. * Those interrupts also don't seem to work on ASpeed SoCs.
  238. */
  239. if (ignore_oc || uhci_is_aspeed(uhci))
  240. return 1;
  241. return uhci->resume_detect_interrupts_are_broken ?
  242. uhci->resume_detect_interrupts_are_broken(uhci) : 0;
  243. }
  244. static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
  245. {
  246. return uhci->global_suspend_mode_is_broken ?
  247. uhci->global_suspend_mode_is_broken(uhci) : 0;
  248. }
  249. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  250. __releases(uhci->lock)
  251. __acquires(uhci->lock)
  252. {
  253. int auto_stop;
  254. int int_enable, egsm_enable, wakeup_enable;
  255. struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
  256. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  257. dev_dbg(&rhdev->dev, "%s%s\n", __func__,
  258. (auto_stop ? " (auto-stop)" : ""));
  259. /* Start off by assuming Resume-Detect interrupts and EGSM work
  260. * and that remote wakeups should be enabled.
  261. */
  262. egsm_enable = USBCMD_EGSM;
  263. int_enable = USBINTR_RESUME;
  264. wakeup_enable = 1;
  265. /*
  266. * In auto-stop mode, we must be able to detect new connections.
  267. * The user can force us to poll by disabling remote wakeup;
  268. * otherwise we will use the EGSM/RD mechanism.
  269. */
  270. if (auto_stop) {
  271. if (!device_may_wakeup(&rhdev->dev))
  272. egsm_enable = int_enable = 0;
  273. }
  274. #ifdef CONFIG_PM
  275. /*
  276. * In bus-suspend mode, we use the wakeup setting specified
  277. * for the root hub.
  278. */
  279. else {
  280. if (!rhdev->do_remote_wakeup)
  281. wakeup_enable = 0;
  282. }
  283. #endif
  284. /*
  285. * UHCI doesn't distinguish between wakeup requests from downstream
  286. * devices and local connect/disconnect events. There's no way to
  287. * enable one without the other; both are controlled by EGSM. Thus
  288. * if wakeups are disallowed then EGSM must be turned off -- in which
  289. * case remote wakeup requests from downstream during system sleep
  290. * will be lost.
  291. *
  292. * In addition, if EGSM is broken then we can't use it. Likewise,
  293. * if Resume-Detect interrupts are broken then we can't use them.
  294. *
  295. * Finally, neither EGSM nor RD is useful by itself. Without EGSM,
  296. * the RD status bit will never get set. Without RD, the controller
  297. * won't generate interrupts to tell the system about wakeup events.
  298. */
  299. if (!wakeup_enable || global_suspend_mode_is_broken(uhci) ||
  300. resume_detect_interrupts_are_broken(uhci))
  301. egsm_enable = int_enable = 0;
  302. uhci->RD_enable = !!int_enable;
  303. uhci_writew(uhci, int_enable, USBINTR);
  304. uhci_writew(uhci, egsm_enable | USBCMD_CF, USBCMD);
  305. mb();
  306. udelay(5);
  307. /* If we're auto-stopping then no devices have been attached
  308. * for a while, so there shouldn't be any active URBs and the
  309. * controller should stop after a few microseconds. Otherwise
  310. * we will give the controller one frame to stop.
  311. */
  312. if (!auto_stop && !(uhci_readw(uhci, USBSTS) & USBSTS_HCH)) {
  313. uhci->rh_state = UHCI_RH_SUSPENDING;
  314. spin_unlock_irq(&uhci->lock);
  315. msleep(1);
  316. spin_lock_irq(&uhci->lock);
  317. if (uhci->dead)
  318. return;
  319. }
  320. if (!(uhci_readw(uhci, USBSTS) & USBSTS_HCH))
  321. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  322. uhci_get_current_frame_number(uhci);
  323. uhci->rh_state = new_state;
  324. uhci->is_stopped = UHCI_IS_STOPPED;
  325. /*
  326. * If remote wakeup is enabled but either EGSM or RD interrupts
  327. * doesn't work, then we won't get an interrupt when a wakeup event
  328. * occurs. Thus the suspended root hub needs to be polled.
  329. */
  330. if (wakeup_enable && (!int_enable || !egsm_enable))
  331. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  332. else
  333. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  334. uhci_scan_schedule(uhci);
  335. uhci_fsbr_off(uhci);
  336. }
  337. static void start_rh(struct uhci_hcd *uhci)
  338. {
  339. uhci->is_stopped = 0;
  340. /*
  341. * Clear stale status bits on Aspeed as we get a stale HCH
  342. * which causes problems later on
  343. */
  344. if (uhci_is_aspeed(uhci))
  345. uhci_writew(uhci, uhci_readw(uhci, USBSTS), USBSTS);
  346. /* Mark it configured and running with a 64-byte max packet.
  347. * All interrupts are enabled, even though RESUME won't do anything.
  348. */
  349. uhci_writew(uhci, USBCMD_RS | USBCMD_CF | USBCMD_MAXP, USBCMD);
  350. uhci_writew(uhci, USBINTR_TIMEOUT | USBINTR_RESUME |
  351. USBINTR_IOC | USBINTR_SP, USBINTR);
  352. mb();
  353. uhci->rh_state = UHCI_RH_RUNNING;
  354. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  355. }
  356. static void wakeup_rh(struct uhci_hcd *uhci)
  357. __releases(uhci->lock)
  358. __acquires(uhci->lock)
  359. {
  360. dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
  361. "%s%s\n", __func__,
  362. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  363. " (auto-start)" : "");
  364. /* If we are auto-stopped then no devices are attached so there's
  365. * no need for wakeup signals. Otherwise we send Global Resume
  366. * for 20 ms.
  367. */
  368. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  369. unsigned egsm;
  370. /* Keep EGSM on if it was set before */
  371. egsm = uhci_readw(uhci, USBCMD) & USBCMD_EGSM;
  372. uhci->rh_state = UHCI_RH_RESUMING;
  373. uhci_writew(uhci, USBCMD_FGR | USBCMD_CF | egsm, USBCMD);
  374. spin_unlock_irq(&uhci->lock);
  375. msleep(20);
  376. spin_lock_irq(&uhci->lock);
  377. if (uhci->dead)
  378. return;
  379. /* End Global Resume and wait for EOP to be sent */
  380. uhci_writew(uhci, USBCMD_CF, USBCMD);
  381. mb();
  382. udelay(4);
  383. if (uhci_readw(uhci, USBCMD) & USBCMD_FGR)
  384. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  385. }
  386. start_rh(uhci);
  387. /* Restart root hub polling */
  388. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  389. }
  390. static irqreturn_t uhci_irq(struct usb_hcd *hcd)
  391. {
  392. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  393. unsigned short status;
  394. /*
  395. * Read the interrupt status, and write it back to clear the
  396. * interrupt cause. Contrary to the UHCI specification, the
  397. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  398. */
  399. status = uhci_readw(uhci, USBSTS);
  400. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  401. return IRQ_NONE;
  402. uhci_writew(uhci, status, USBSTS); /* Clear it */
  403. spin_lock(&uhci->lock);
  404. if (unlikely(!uhci->is_initialized)) /* not yet configured */
  405. goto done;
  406. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  407. if (status & USBSTS_HSE)
  408. dev_err(uhci_dev(uhci),
  409. "host system error, PCI problems?\n");
  410. if (status & USBSTS_HCPE)
  411. dev_err(uhci_dev(uhci),
  412. "host controller process error, something bad happened!\n");
  413. if (status & USBSTS_HCH) {
  414. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  415. dev_err(uhci_dev(uhci),
  416. "host controller halted, very bad!\n");
  417. if (debug > 1 && errbuf) {
  418. /* Print the schedule for debugging */
  419. uhci_sprint_schedule(uhci, errbuf,
  420. ERRBUF_LEN - EXTRA_SPACE);
  421. lprintk(errbuf);
  422. }
  423. uhci_hc_died(uhci);
  424. usb_hc_died(hcd);
  425. /* Force a callback in case there are
  426. * pending unlinks */
  427. mod_timer(&hcd->rh_timer, jiffies);
  428. }
  429. }
  430. }
  431. if (status & USBSTS_RD) {
  432. spin_unlock(&uhci->lock);
  433. usb_hcd_poll_rh_status(hcd);
  434. } else {
  435. uhci_scan_schedule(uhci);
  436. done:
  437. spin_unlock(&uhci->lock);
  438. }
  439. return IRQ_HANDLED;
  440. }
  441. /*
  442. * Store the current frame number in uhci->frame_number if the controller
  443. * is running. Expand from 11 bits (of which we use only 10) to a
  444. * full-sized integer.
  445. *
  446. * Like many other parts of the driver, this code relies on being polled
  447. * more than once per second as long as the controller is running.
  448. */
  449. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  450. {
  451. if (!uhci->is_stopped) {
  452. unsigned delta;
  453. delta = (uhci_readw(uhci, USBFRNUM) - uhci->frame_number) &
  454. (UHCI_NUMFRAMES - 1);
  455. uhci->frame_number += delta;
  456. }
  457. }
  458. /*
  459. * De-allocate all resources
  460. */
  461. static void release_uhci(struct uhci_hcd *uhci)
  462. {
  463. int i;
  464. spin_lock_irq(&uhci->lock);
  465. uhci->is_initialized = 0;
  466. spin_unlock_irq(&uhci->lock);
  467. debugfs_remove(uhci->dentry);
  468. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  469. uhci_free_qh(uhci, uhci->skelqh[i]);
  470. uhci_free_td(uhci, uhci->term_td);
  471. dma_pool_destroy(uhci->qh_pool);
  472. dma_pool_destroy(uhci->td_pool);
  473. kfree(uhci->frame_cpu);
  474. dma_free_coherent(uhci_dev(uhci),
  475. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  476. uhci->frame, uhci->frame_dma_handle);
  477. }
  478. /*
  479. * Allocate a frame list, and then setup the skeleton
  480. *
  481. * The hardware doesn't really know any difference
  482. * in the queues, but the order does matter for the
  483. * protocols higher up. The order in which the queues
  484. * are encountered by the hardware is:
  485. *
  486. * - All isochronous events are handled before any
  487. * of the queues. We don't do that here, because
  488. * we'll create the actual TD entries on demand.
  489. * - The first queue is the high-period interrupt queue.
  490. * - The second queue is the period-1 interrupt and async
  491. * (low-speed control, full-speed control, then bulk) queue.
  492. * - The third queue is the terminating bandwidth reclamation queue,
  493. * which contains no members, loops back to itself, and is present
  494. * only when FSBR is on and there are no full-speed control or bulk QHs.
  495. */
  496. static int uhci_start(struct usb_hcd *hcd)
  497. {
  498. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  499. int retval = -EBUSY;
  500. int i;
  501. struct dentry __maybe_unused *dentry;
  502. hcd->uses_new_polling = 1;
  503. /* Accept arbitrarily long scatter-gather lists */
  504. if (!hcd->localmem_pool)
  505. hcd->self.sg_tablesize = ~0;
  506. spin_lock_init(&uhci->lock);
  507. timer_setup(&uhci->fsbr_timer, uhci_fsbr_timeout, 0);
  508. INIT_LIST_HEAD(&uhci->idle_qh_list);
  509. init_waitqueue_head(&uhci->waitqh);
  510. #ifdef UHCI_DEBUG_OPS
  511. uhci->dentry = debugfs_create_file(hcd->self.bus_name,
  512. S_IFREG|S_IRUGO|S_IWUSR,
  513. uhci_debugfs_root, uhci,
  514. &uhci_debug_operations);
  515. #endif
  516. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  517. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  518. &uhci->frame_dma_handle, GFP_KERNEL);
  519. if (!uhci->frame) {
  520. dev_err(uhci_dev(uhci),
  521. "unable to allocate consistent memory for frame list\n");
  522. goto err_alloc_frame;
  523. }
  524. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  525. GFP_KERNEL);
  526. if (!uhci->frame_cpu)
  527. goto err_alloc_frame_cpu;
  528. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  529. sizeof(struct uhci_td), 16, 0);
  530. if (!uhci->td_pool) {
  531. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  532. goto err_create_td_pool;
  533. }
  534. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  535. sizeof(struct uhci_qh), 16, 0);
  536. if (!uhci->qh_pool) {
  537. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  538. goto err_create_qh_pool;
  539. }
  540. uhci->term_td = uhci_alloc_td(uhci);
  541. if (!uhci->term_td) {
  542. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  543. goto err_alloc_term_td;
  544. }
  545. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  546. uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
  547. if (!uhci->skelqh[i]) {
  548. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  549. goto err_alloc_skelqh;
  550. }
  551. }
  552. /*
  553. * 8 Interrupt queues; link all higher int queues to int1 = async
  554. */
  555. for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
  556. uhci->skelqh[i]->link = LINK_TO_QH(uhci, uhci->skel_async_qh);
  557. uhci->skel_async_qh->link = UHCI_PTR_TERM(uhci);
  558. uhci->skel_term_qh->link = LINK_TO_QH(uhci, uhci->skel_term_qh);
  559. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  560. uhci_fill_td(uhci, uhci->term_td, 0, uhci_explen(0) |
  561. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  562. uhci->term_td->link = UHCI_PTR_TERM(uhci);
  563. uhci->skel_async_qh->element = uhci->skel_term_qh->element =
  564. LINK_TO_TD(uhci, uhci->term_td);
  565. /*
  566. * Fill the frame list: make all entries point to the proper
  567. * interrupt queue.
  568. */
  569. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  570. /* Only place we don't use the frame list routines */
  571. uhci->frame[i] = uhci_frame_skel_link(uhci, i);
  572. }
  573. /*
  574. * Some architectures require a full mb() to enforce completion of
  575. * the memory writes above before the I/O transfers in configure_hc().
  576. */
  577. mb();
  578. spin_lock_irq(&uhci->lock);
  579. configure_hc(uhci);
  580. uhci->is_initialized = 1;
  581. start_rh(uhci);
  582. spin_unlock_irq(&uhci->lock);
  583. return 0;
  584. /*
  585. * error exits:
  586. */
  587. err_alloc_skelqh:
  588. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  589. if (uhci->skelqh[i])
  590. uhci_free_qh(uhci, uhci->skelqh[i]);
  591. }
  592. uhci_free_td(uhci, uhci->term_td);
  593. err_alloc_term_td:
  594. dma_pool_destroy(uhci->qh_pool);
  595. err_create_qh_pool:
  596. dma_pool_destroy(uhci->td_pool);
  597. err_create_td_pool:
  598. kfree(uhci->frame_cpu);
  599. err_alloc_frame_cpu:
  600. dma_free_coherent(uhci_dev(uhci),
  601. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  602. uhci->frame, uhci->frame_dma_handle);
  603. err_alloc_frame:
  604. debugfs_remove(uhci->dentry);
  605. return retval;
  606. }
  607. static void uhci_stop(struct usb_hcd *hcd)
  608. {
  609. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  610. spin_lock_irq(&uhci->lock);
  611. if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
  612. uhci_hc_died(uhci);
  613. uhci_scan_schedule(uhci);
  614. spin_unlock_irq(&uhci->lock);
  615. synchronize_irq(hcd->irq);
  616. del_timer_sync(&uhci->fsbr_timer);
  617. release_uhci(uhci);
  618. }
  619. #ifdef CONFIG_PM
  620. static int uhci_rh_suspend(struct usb_hcd *hcd)
  621. {
  622. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  623. int rc = 0;
  624. spin_lock_irq(&uhci->lock);
  625. if (!HCD_HW_ACCESSIBLE(hcd))
  626. rc = -ESHUTDOWN;
  627. else if (uhci->dead)
  628. ; /* Dead controllers tell no tales */
  629. /* Once the controller is stopped, port resumes that are already
  630. * in progress won't complete. Hence if remote wakeup is enabled
  631. * for the root hub and any ports are in the middle of a resume or
  632. * remote wakeup, we must fail the suspend.
  633. */
  634. else if (hcd->self.root_hub->do_remote_wakeup &&
  635. uhci->resuming_ports) {
  636. dev_dbg(uhci_dev(uhci),
  637. "suspend failed because a port is resuming\n");
  638. rc = -EBUSY;
  639. } else
  640. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  641. spin_unlock_irq(&uhci->lock);
  642. return rc;
  643. }
  644. static int uhci_rh_resume(struct usb_hcd *hcd)
  645. {
  646. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  647. int rc = 0;
  648. spin_lock_irq(&uhci->lock);
  649. if (!HCD_HW_ACCESSIBLE(hcd))
  650. rc = -ESHUTDOWN;
  651. else if (!uhci->dead)
  652. wakeup_rh(uhci);
  653. spin_unlock_irq(&uhci->lock);
  654. return rc;
  655. }
  656. #endif
  657. /* Wait until a particular device/endpoint's QH is idle, and free it */
  658. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  659. struct usb_host_endpoint *hep)
  660. {
  661. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  662. struct uhci_qh *qh;
  663. spin_lock_irq(&uhci->lock);
  664. qh = (struct uhci_qh *) hep->hcpriv;
  665. if (qh == NULL)
  666. goto done;
  667. while (qh->state != QH_STATE_IDLE) {
  668. ++uhci->num_waiting;
  669. spin_unlock_irq(&uhci->lock);
  670. wait_event_interruptible(uhci->waitqh,
  671. qh->state == QH_STATE_IDLE);
  672. spin_lock_irq(&uhci->lock);
  673. --uhci->num_waiting;
  674. }
  675. uhci_free_qh(uhci, qh);
  676. done:
  677. spin_unlock_irq(&uhci->lock);
  678. }
  679. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  680. {
  681. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  682. unsigned frame_number;
  683. unsigned delta;
  684. /* Minimize latency by avoiding the spinlock */
  685. frame_number = uhci->frame_number;
  686. barrier();
  687. delta = (uhci_readw(uhci, USBFRNUM) - frame_number) &
  688. (UHCI_NUMFRAMES - 1);
  689. return frame_number + delta;
  690. }
  691. /* Determines number of ports on controller */
  692. static int uhci_count_ports(struct usb_hcd *hcd)
  693. {
  694. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  695. unsigned io_size = (unsigned) hcd->rsrc_len;
  696. int port;
  697. /* The UHCI spec says devices must have 2 ports, and goes on to say
  698. * they may have more but gives no way to determine how many there
  699. * are. However according to the UHCI spec, Bit 7 of the port
  700. * status and control register is always set to 1. So we try to
  701. * use this to our advantage. Another common failure mode when
  702. * a nonexistent register is addressed is to return all ones, so
  703. * we test for that also.
  704. */
  705. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  706. unsigned int portstatus;
  707. portstatus = uhci_readw(uhci, USBPORTSC1 + (port * 2));
  708. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  709. break;
  710. }
  711. if (debug)
  712. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  713. /* Anything greater than 7 is weird so we'll ignore it. */
  714. if (port > UHCI_RH_MAXCHILD) {
  715. dev_info(uhci_dev(uhci),
  716. "port count misdetected? forcing to 2 ports\n");
  717. port = 2;
  718. }
  719. return port;
  720. }
  721. static const char hcd_name[] = "uhci_hcd";
  722. #ifdef CONFIG_USB_PCI
  723. #include "uhci-pci.c"
  724. #define PCI_DRIVER uhci_pci_driver
  725. #endif
  726. #ifdef CONFIG_SPARC_LEON
  727. #include "uhci-grlib.c"
  728. #define PLATFORM_DRIVER uhci_grlib_driver
  729. #endif
  730. #ifdef CONFIG_USB_UHCI_PLATFORM
  731. #include "uhci-platform.c"
  732. #define PLATFORM_DRIVER uhci_platform_driver
  733. #endif
  734. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER)
  735. #error "missing bus glue for uhci-hcd"
  736. #endif
  737. static int __init uhci_hcd_init(void)
  738. {
  739. int retval = -ENOMEM;
  740. if (usb_disabled())
  741. return -ENODEV;
  742. printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
  743. ignore_oc ? ", overcurrent ignored" : "");
  744. set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  745. #ifdef CONFIG_DYNAMIC_DEBUG
  746. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  747. if (!errbuf)
  748. goto errbuf_failed;
  749. uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
  750. #endif
  751. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  752. sizeof(struct urb_priv), 0, 0, NULL);
  753. if (!uhci_up_cachep)
  754. goto up_failed;
  755. #ifdef PLATFORM_DRIVER
  756. retval = platform_driver_register(&PLATFORM_DRIVER);
  757. if (retval < 0)
  758. goto clean0;
  759. #endif
  760. #ifdef PCI_DRIVER
  761. retval = pci_register_driver(&PCI_DRIVER);
  762. if (retval < 0)
  763. goto clean1;
  764. #endif
  765. return 0;
  766. #ifdef PCI_DRIVER
  767. clean1:
  768. #endif
  769. #ifdef PLATFORM_DRIVER
  770. platform_driver_unregister(&PLATFORM_DRIVER);
  771. clean0:
  772. #endif
  773. kmem_cache_destroy(uhci_up_cachep);
  774. up_failed:
  775. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  776. debugfs_remove(uhci_debugfs_root);
  777. kfree(errbuf);
  778. errbuf_failed:
  779. #endif
  780. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  781. return retval;
  782. }
  783. static void __exit uhci_hcd_cleanup(void)
  784. {
  785. #ifdef PLATFORM_DRIVER
  786. platform_driver_unregister(&PLATFORM_DRIVER);
  787. #endif
  788. #ifdef PCI_DRIVER
  789. pci_unregister_driver(&PCI_DRIVER);
  790. #endif
  791. kmem_cache_destroy(uhci_up_cachep);
  792. debugfs_remove(uhci_debugfs_root);
  793. #ifdef CONFIG_DYNAMIC_DEBUG
  794. kfree(errbuf);
  795. #endif
  796. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  797. }
  798. module_init(uhci_hcd_init);
  799. module_exit(uhci_hcd_cleanup);
  800. MODULE_AUTHOR(DRIVER_AUTHOR);
  801. MODULE_DESCRIPTION(DRIVER_DESC);
  802. MODULE_LICENSE("GPL");