uhci-hcd.c 25 KB

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