uhci-hcd.c 25 KB

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