uhci-hcd.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. #include "pci-quirks.h"
  49. /*
  50. * Version Information
  51. */
  52. #define DRIVER_AUTHOR \
  53. "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, " \
  54. "Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, " \
  55. "Roman Weissgaerber, Alan Stern"
  56. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  57. /* for flakey hardware, ignore overcurrent indicators */
  58. static int ignore_oc;
  59. module_param(ignore_oc, bool, S_IRUGO);
  60. MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
  61. /*
  62. * debug = 0, no debugging messages
  63. * debug = 1, dump failed URBs except for stalls
  64. * debug = 2, dump all failed URBs (including stalls)
  65. * show all queues in /sys/kernel/debug/uhci/[pci_addr]
  66. * debug = 3, show all TDs in URBs when dumping
  67. */
  68. #ifdef DEBUG
  69. #define DEBUG_CONFIGURED 1
  70. static int debug = 1;
  71. module_param(debug, int, S_IRUGO | S_IWUSR);
  72. MODULE_PARM_DESC(debug, "Debug level");
  73. #else
  74. #define DEBUG_CONFIGURED 0
  75. #define debug 0
  76. #endif
  77. static char *errbuf;
  78. #define ERRBUF_LEN (32 * 1024)
  79. static struct kmem_cache *uhci_up_cachep; /* urb_priv */
  80. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  81. static void wakeup_rh(struct uhci_hcd *uhci);
  82. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  83. /*
  84. * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
  85. */
  86. static __le32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
  87. {
  88. int skelnum;
  89. /*
  90. * The interrupt queues will be interleaved as evenly as possible.
  91. * There's not much to be done about period-1 interrupts; they have
  92. * to occur in every frame. But we can schedule period-2 interrupts
  93. * in odd-numbered frames, period-4 interrupts in frames congruent
  94. * to 2 (mod 4), and so on. This way each frame only has two
  95. * interrupt QHs, which will help spread out bandwidth utilization.
  96. *
  97. * ffs (Find First bit Set) does exactly what we need:
  98. * 1,3,5,... => ffs = 0 => use period-2 QH = skelqh[8],
  99. * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
  100. * ffs >= 7 => not on any high-period queue, so use
  101. * period-1 QH = skelqh[9].
  102. * Add in UHCI_NUMFRAMES to insure at least one bit is set.
  103. */
  104. skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
  105. if (skelnum <= 1)
  106. skelnum = 9;
  107. return LINK_TO_QH(uhci->skelqh[skelnum]);
  108. }
  109. #include "uhci-debug.c"
  110. #include "uhci-q.c"
  111. #include "uhci-hub.c"
  112. /*
  113. * Finish up a host controller reset and update the recorded state.
  114. */
  115. static void finish_reset(struct uhci_hcd *uhci)
  116. {
  117. int port;
  118. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  119. * bits in the port status and control registers.
  120. * We have to clear them by hand.
  121. */
  122. for (port = 0; port < uhci->rh_numports; ++port)
  123. outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
  124. uhci->port_c_suspend = uhci->resuming_ports = 0;
  125. uhci->rh_state = UHCI_RH_RESET;
  126. uhci->is_stopped = UHCI_IS_STOPPED;
  127. uhci_to_hcd(uhci)->state = HC_STATE_HALT;
  128. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  129. uhci->dead = 0; /* Full reset resurrects the controller */
  130. }
  131. /*
  132. * Last rites for a defunct/nonfunctional controller
  133. * or one we don't want to use any more.
  134. */
  135. static void uhci_hc_died(struct uhci_hcd *uhci)
  136. {
  137. uhci_get_current_frame_number(uhci);
  138. uhci_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr);
  139. finish_reset(uhci);
  140. uhci->dead = 1;
  141. /* The current frame may already be partway finished */
  142. ++uhci->frame_number;
  143. }
  144. /*
  145. * Initialize a controller that was newly discovered or has lost power
  146. * or otherwise been reset while it was suspended. In none of these cases
  147. * can we be sure of its previous state.
  148. */
  149. static void check_and_reset_hc(struct uhci_hcd *uhci)
  150. {
  151. if (uhci_check_and_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr))
  152. finish_reset(uhci);
  153. }
  154. /*
  155. * Store the basic register settings needed by the controller.
  156. */
  157. static void configure_hc(struct uhci_hcd *uhci)
  158. {
  159. struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
  160. /* Set the frame length to the default: 1 ms exactly */
  161. outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
  162. /* Store the frame list base address */
  163. outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
  164. /* Set the current frame number */
  165. outw(uhci->frame_number & UHCI_MAX_SOF_NUMBER,
  166. uhci->io_addr + USBFRNUM);
  167. /* Mark controller as not halted before we enable interrupts */
  168. uhci_to_hcd(uhci)->state = HC_STATE_SUSPENDED;
  169. mb();
  170. /* Enable PIRQ */
  171. pci_write_config_word(pdev, USBLEGSUP, USBLEGSUP_DEFAULT);
  172. /* Disable platform-specific non-PME# wakeup */
  173. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  174. pci_write_config_byte(pdev, USBRES_INTEL, 0);
  175. }
  176. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  177. {
  178. int port;
  179. /* If we have to ignore overcurrent events then almost by definition
  180. * we can't depend on resume-detect interrupts. */
  181. if (ignore_oc)
  182. return 1;
  183. switch (to_pci_dev(uhci_dev(uhci))->vendor) {
  184. default:
  185. break;
  186. case PCI_VENDOR_ID_GENESYS:
  187. /* Genesys Logic's GL880S controllers don't generate
  188. * resume-detect interrupts.
  189. */
  190. return 1;
  191. case PCI_VENDOR_ID_INTEL:
  192. /* Some of Intel's USB controllers have a bug that causes
  193. * resume-detect interrupts if any port has an over-current
  194. * condition. To make matters worse, some motherboards
  195. * hardwire unused USB ports' over-current inputs active!
  196. * To prevent problems, we will not enable resume-detect
  197. * interrupts if any ports are OC.
  198. */
  199. for (port = 0; port < uhci->rh_numports; ++port) {
  200. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  201. USBPORTSC_OC)
  202. return 1;
  203. }
  204. break;
  205. }
  206. return 0;
  207. }
  208. static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
  209. {
  210. int port;
  211. const char *sys_info;
  212. static char bad_Asus_board[] = "A7V8X";
  213. /* One of Asus's motherboards has a bug which causes it to
  214. * wake up immediately from suspend-to-RAM if any of the ports
  215. * are connected. In such cases we will not set EGSM.
  216. */
  217. sys_info = dmi_get_system_info(DMI_BOARD_NAME);
  218. if (sys_info && !strcmp(sys_info, bad_Asus_board)) {
  219. for (port = 0; port < uhci->rh_numports; ++port) {
  220. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  221. USBPORTSC_CCS)
  222. return 1;
  223. }
  224. }
  225. return 0;
  226. }
  227. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  228. __releases(uhci->lock)
  229. __acquires(uhci->lock)
  230. {
  231. int auto_stop;
  232. int int_enable, egsm_enable, wakeup_enable;
  233. struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
  234. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  235. dev_dbg(&rhdev->dev, "%s%s\n", __func__,
  236. (auto_stop ? " (auto-stop)" : ""));
  237. /* Start off by assuming Resume-Detect interrupts and EGSM work
  238. * and that remote wakeups should be enabled.
  239. */
  240. egsm_enable = USBCMD_EGSM;
  241. uhci->RD_enable = 1;
  242. int_enable = USBINTR_RESUME;
  243. wakeup_enable = 1;
  244. /* In auto-stop mode wakeups must always be detected, but
  245. * Resume-Detect interrupts may be prohibited. (In the absence
  246. * of CONFIG_PM, they are always disallowed.)
  247. */
  248. if (auto_stop) {
  249. if (!device_may_wakeup(&rhdev->dev))
  250. int_enable = 0;
  251. /* In bus-suspend mode wakeups may be disabled, but if they are
  252. * allowed then so are Resume-Detect interrupts.
  253. */
  254. } else {
  255. #ifdef CONFIG_PM
  256. if (!rhdev->do_remote_wakeup)
  257. wakeup_enable = 0;
  258. #endif
  259. }
  260. /* EGSM causes the root hub to echo a 'K' signal (resume) out any
  261. * port which requests a remote wakeup. According to the USB spec,
  262. * every hub is supposed to do this. But if we are ignoring
  263. * remote-wakeup requests anyway then there's no point to it.
  264. * We also shouldn't enable EGSM if it's broken.
  265. */
  266. if (!wakeup_enable || global_suspend_mode_is_broken(uhci))
  267. egsm_enable = 0;
  268. /* If we're ignoring wakeup events then there's no reason to
  269. * enable Resume-Detect interrupts. We also shouldn't enable
  270. * them if they are broken or disallowed.
  271. *
  272. * This logic may lead us to enabling RD but not EGSM. The UHCI
  273. * spec foolishly says that RD works only when EGSM is on, but
  274. * there's no harm in enabling it anyway -- perhaps some chips
  275. * will implement it!
  276. */
  277. if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) ||
  278. !int_enable)
  279. uhci->RD_enable = int_enable = 0;
  280. outw(int_enable, uhci->io_addr + USBINTR);
  281. outw(egsm_enable | USBCMD_CF, uhci->io_addr + USBCMD);
  282. mb();
  283. udelay(5);
  284. /* If we're auto-stopping then no devices have been attached
  285. * for a while, so there shouldn't be any active URBs and the
  286. * controller should stop after a few microseconds. Otherwise
  287. * we will give the controller one frame to stop.
  288. */
  289. if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
  290. uhci->rh_state = UHCI_RH_SUSPENDING;
  291. spin_unlock_irq(&uhci->lock);
  292. msleep(1);
  293. spin_lock_irq(&uhci->lock);
  294. if (uhci->dead)
  295. return;
  296. }
  297. if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
  298. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  299. uhci_get_current_frame_number(uhci);
  300. uhci->rh_state = new_state;
  301. uhci->is_stopped = UHCI_IS_STOPPED;
  302. /* If interrupts don't work and remote wakeup is enabled then
  303. * the suspended root hub needs to be polled.
  304. */
  305. if (!int_enable && wakeup_enable)
  306. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  307. else
  308. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  309. uhci_scan_schedule(uhci);
  310. uhci_fsbr_off(uhci);
  311. }
  312. static void start_rh(struct uhci_hcd *uhci)
  313. {
  314. uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
  315. uhci->is_stopped = 0;
  316. /* Mark it configured and running with a 64-byte max packet.
  317. * All interrupts are enabled, even though RESUME won't do anything.
  318. */
  319. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
  320. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  321. uhci->io_addr + USBINTR);
  322. mb();
  323. uhci->rh_state = UHCI_RH_RUNNING;
  324. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  325. }
  326. static void wakeup_rh(struct uhci_hcd *uhci)
  327. __releases(uhci->lock)
  328. __acquires(uhci->lock)
  329. {
  330. dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
  331. "%s%s\n", __func__,
  332. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  333. " (auto-start)" : "");
  334. /* If we are auto-stopped then no devices are attached so there's
  335. * no need for wakeup signals. Otherwise we send Global Resume
  336. * for 20 ms.
  337. */
  338. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  339. unsigned egsm;
  340. /* Keep EGSM on if it was set before */
  341. egsm = inw(uhci->io_addr + USBCMD) & USBCMD_EGSM;
  342. uhci->rh_state = UHCI_RH_RESUMING;
  343. outw(USBCMD_FGR | USBCMD_CF | egsm, uhci->io_addr + USBCMD);
  344. spin_unlock_irq(&uhci->lock);
  345. msleep(20);
  346. spin_lock_irq(&uhci->lock);
  347. if (uhci->dead)
  348. return;
  349. /* End Global Resume and wait for EOP to be sent */
  350. outw(USBCMD_CF, uhci->io_addr + USBCMD);
  351. mb();
  352. udelay(4);
  353. if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
  354. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  355. }
  356. start_rh(uhci);
  357. /* Restart root hub polling */
  358. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  359. }
  360. static irqreturn_t uhci_irq(struct usb_hcd *hcd)
  361. {
  362. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  363. unsigned short status;
  364. /*
  365. * Read the interrupt status, and write it back to clear the
  366. * interrupt cause. Contrary to the UHCI specification, the
  367. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  368. */
  369. status = inw(uhci->io_addr + USBSTS);
  370. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  371. return IRQ_NONE;
  372. outw(status, uhci->io_addr + USBSTS); /* Clear it */
  373. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  374. if (status & USBSTS_HSE)
  375. dev_err(uhci_dev(uhci), "host system error, "
  376. "PCI problems?\n");
  377. if (status & USBSTS_HCPE)
  378. dev_err(uhci_dev(uhci), "host controller process "
  379. "error, something bad happened!\n");
  380. if (status & USBSTS_HCH) {
  381. spin_lock(&uhci->lock);
  382. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  383. dev_err(uhci_dev(uhci),
  384. "host controller halted, "
  385. "very bad!\n");
  386. if (debug > 1 && errbuf) {
  387. /* Print the schedule for debugging */
  388. uhci_sprint_schedule(uhci,
  389. errbuf, ERRBUF_LEN);
  390. lprintk(errbuf);
  391. }
  392. uhci_hc_died(uhci);
  393. /* Force a callback in case there are
  394. * pending unlinks */
  395. mod_timer(&hcd->rh_timer, jiffies);
  396. }
  397. spin_unlock(&uhci->lock);
  398. }
  399. }
  400. if (status & USBSTS_RD)
  401. usb_hcd_poll_rh_status(hcd);
  402. else {
  403. spin_lock(&uhci->lock);
  404. uhci_scan_schedule(uhci);
  405. spin_unlock(&uhci->lock);
  406. }
  407. return IRQ_HANDLED;
  408. }
  409. /*
  410. * Store the current frame number in uhci->frame_number if the controller
  411. * is running. Expand from 11 bits (of which we use only 10) to a
  412. * full-sized integer.
  413. *
  414. * Like many other parts of the driver, this code relies on being polled
  415. * more than once per second as long as the controller is running.
  416. */
  417. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  418. {
  419. if (!uhci->is_stopped) {
  420. unsigned delta;
  421. delta = (inw(uhci->io_addr + USBFRNUM) - uhci->frame_number) &
  422. (UHCI_NUMFRAMES - 1);
  423. uhci->frame_number += delta;
  424. }
  425. }
  426. /*
  427. * De-allocate all resources
  428. */
  429. static void release_uhci(struct uhci_hcd *uhci)
  430. {
  431. int i;
  432. if (DEBUG_CONFIGURED) {
  433. spin_lock_irq(&uhci->lock);
  434. uhci->is_initialized = 0;
  435. spin_unlock_irq(&uhci->lock);
  436. debugfs_remove(uhci->dentry);
  437. }
  438. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  439. uhci_free_qh(uhci, uhci->skelqh[i]);
  440. uhci_free_td(uhci, uhci->term_td);
  441. dma_pool_destroy(uhci->qh_pool);
  442. dma_pool_destroy(uhci->td_pool);
  443. kfree(uhci->frame_cpu);
  444. dma_free_coherent(uhci_dev(uhci),
  445. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  446. uhci->frame, uhci->frame_dma_handle);
  447. }
  448. static int uhci_init(struct usb_hcd *hcd)
  449. {
  450. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  451. unsigned io_size = (unsigned) hcd->rsrc_len;
  452. int port;
  453. uhci->io_addr = (unsigned long) hcd->rsrc_start;
  454. /* The UHCI spec says devices must have 2 ports, and goes on to say
  455. * they may have more but gives no way to determine how many there
  456. * are. However according to the UHCI spec, Bit 7 of the port
  457. * status and control register is always set to 1. So we try to
  458. * use this to our advantage. Another common failure mode when
  459. * a nonexistent register is addressed is to return all ones, so
  460. * we test for that also.
  461. */
  462. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  463. unsigned int portstatus;
  464. portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
  465. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  466. break;
  467. }
  468. if (debug)
  469. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  470. /* Anything greater than 7 is weird so we'll ignore it. */
  471. if (port > UHCI_RH_MAXCHILD) {
  472. dev_info(uhci_dev(uhci), "port count misdetected? "
  473. "forcing to 2 ports\n");
  474. port = 2;
  475. }
  476. uhci->rh_numports = port;
  477. /* Kick BIOS off this hardware and reset if the controller
  478. * isn't already safely quiescent.
  479. */
  480. check_and_reset_hc(uhci);
  481. return 0;
  482. }
  483. /* Make sure the controller is quiescent and that we're not using it
  484. * any more. This is mainly for the benefit of programs which, like kexec,
  485. * expect the hardware to be idle: not doing DMA or generating IRQs.
  486. *
  487. * This routine may be called in a damaged or failing kernel. Hence we
  488. * do not acquire the spinlock before shutting down the controller.
  489. */
  490. static void uhci_shutdown(struct pci_dev *pdev)
  491. {
  492. struct usb_hcd *hcd = pci_get_drvdata(pdev);
  493. uhci_hc_died(hcd_to_uhci(hcd));
  494. }
  495. /*
  496. * Allocate a frame list, and then setup the skeleton
  497. *
  498. * The hardware doesn't really know any difference
  499. * in the queues, but the order does matter for the
  500. * protocols higher up. The order in which the queues
  501. * are encountered by the hardware is:
  502. *
  503. * - All isochronous events are handled before any
  504. * of the queues. We don't do that here, because
  505. * we'll create the actual TD entries on demand.
  506. * - The first queue is the high-period interrupt queue.
  507. * - The second queue is the period-1 interrupt and async
  508. * (low-speed control, full-speed control, then bulk) queue.
  509. * - The third queue is the terminating bandwidth reclamation queue,
  510. * which contains no members, loops back to itself, and is present
  511. * only when FSBR is on and there are no full-speed control or bulk QHs.
  512. */
  513. static int uhci_start(struct usb_hcd *hcd)
  514. {
  515. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  516. int retval = -EBUSY;
  517. int i;
  518. struct dentry __maybe_unused *dentry;
  519. hcd->uses_new_polling = 1;
  520. spin_lock_init(&uhci->lock);
  521. setup_timer(&uhci->fsbr_timer, uhci_fsbr_timeout,
  522. (unsigned long) uhci);
  523. INIT_LIST_HEAD(&uhci->idle_qh_list);
  524. init_waitqueue_head(&uhci->waitqh);
  525. #ifdef UHCI_DEBUG_OPS
  526. dentry = debugfs_create_file(hcd->self.bus_name,
  527. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
  528. uhci, &uhci_debug_operations);
  529. if (!dentry) {
  530. dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
  531. return -ENOMEM;
  532. }
  533. uhci->dentry = dentry;
  534. #endif
  535. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  536. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  537. &uhci->frame_dma_handle, 0);
  538. if (!uhci->frame) {
  539. dev_err(uhci_dev(uhci), "unable to allocate "
  540. "consistent memory for frame list\n");
  541. goto err_alloc_frame;
  542. }
  543. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  544. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  545. GFP_KERNEL);
  546. if (!uhci->frame_cpu) {
  547. dev_err(uhci_dev(uhci), "unable to allocate "
  548. "memory for frame pointers\n");
  549. goto err_alloc_frame_cpu;
  550. }
  551. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  552. sizeof(struct uhci_td), 16, 0);
  553. if (!uhci->td_pool) {
  554. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  555. goto err_create_td_pool;
  556. }
  557. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  558. sizeof(struct uhci_qh), 16, 0);
  559. if (!uhci->qh_pool) {
  560. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  561. goto err_create_qh_pool;
  562. }
  563. uhci->term_td = uhci_alloc_td(uhci);
  564. if (!uhci->term_td) {
  565. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  566. goto err_alloc_term_td;
  567. }
  568. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  569. uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
  570. if (!uhci->skelqh[i]) {
  571. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  572. goto err_alloc_skelqh;
  573. }
  574. }
  575. /*
  576. * 8 Interrupt queues; link all higher int queues to int1 = async
  577. */
  578. for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
  579. uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
  580. uhci->skel_async_qh->link = UHCI_PTR_TERM;
  581. uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
  582. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  583. uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
  584. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  585. uhci->term_td->link = UHCI_PTR_TERM;
  586. uhci->skel_async_qh->element = uhci->skel_term_qh->element =
  587. LINK_TO_TD(uhci->term_td);
  588. /*
  589. * Fill the frame list: make all entries point to the proper
  590. * interrupt queue.
  591. */
  592. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  593. /* Only place we don't use the frame list routines */
  594. uhci->frame[i] = uhci_frame_skel_link(uhci, i);
  595. }
  596. /*
  597. * Some architectures require a full mb() to enforce completion of
  598. * the memory writes above before the I/O transfers in configure_hc().
  599. */
  600. mb();
  601. configure_hc(uhci);
  602. uhci->is_initialized = 1;
  603. spin_lock_irq(&uhci->lock);
  604. start_rh(uhci);
  605. spin_unlock_irq(&uhci->lock);
  606. return 0;
  607. /*
  608. * error exits:
  609. */
  610. err_alloc_skelqh:
  611. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  612. if (uhci->skelqh[i])
  613. uhci_free_qh(uhci, uhci->skelqh[i]);
  614. }
  615. uhci_free_td(uhci, uhci->term_td);
  616. err_alloc_term_td:
  617. dma_pool_destroy(uhci->qh_pool);
  618. err_create_qh_pool:
  619. dma_pool_destroy(uhci->td_pool);
  620. err_create_td_pool:
  621. kfree(uhci->frame_cpu);
  622. err_alloc_frame_cpu:
  623. dma_free_coherent(uhci_dev(uhci),
  624. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  625. uhci->frame, uhci->frame_dma_handle);
  626. err_alloc_frame:
  627. debugfs_remove(uhci->dentry);
  628. return retval;
  629. }
  630. static void uhci_stop(struct usb_hcd *hcd)
  631. {
  632. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  633. spin_lock_irq(&uhci->lock);
  634. if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
  635. uhci_hc_died(uhci);
  636. uhci_scan_schedule(uhci);
  637. spin_unlock_irq(&uhci->lock);
  638. synchronize_irq(hcd->irq);
  639. del_timer_sync(&uhci->fsbr_timer);
  640. release_uhci(uhci);
  641. }
  642. #ifdef CONFIG_PM
  643. static int uhci_rh_suspend(struct usb_hcd *hcd)
  644. {
  645. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  646. int rc = 0;
  647. spin_lock_irq(&uhci->lock);
  648. if (!HCD_HW_ACCESSIBLE(hcd))
  649. rc = -ESHUTDOWN;
  650. else if (uhci->dead)
  651. ; /* Dead controllers tell no tales */
  652. /* Once the controller is stopped, port resumes that are already
  653. * in progress won't complete. Hence if remote wakeup is enabled
  654. * for the root hub and any ports are in the middle of a resume or
  655. * remote wakeup, we must fail the suspend.
  656. */
  657. else if (hcd->self.root_hub->do_remote_wakeup &&
  658. uhci->resuming_ports) {
  659. dev_dbg(uhci_dev(uhci), "suspend failed because a port "
  660. "is resuming\n");
  661. rc = -EBUSY;
  662. } else
  663. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  664. spin_unlock_irq(&uhci->lock);
  665. return rc;
  666. }
  667. static int uhci_rh_resume(struct usb_hcd *hcd)
  668. {
  669. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  670. int rc = 0;
  671. spin_lock_irq(&uhci->lock);
  672. if (!HCD_HW_ACCESSIBLE(hcd))
  673. rc = -ESHUTDOWN;
  674. else if (!uhci->dead)
  675. wakeup_rh(uhci);
  676. spin_unlock_irq(&uhci->lock);
  677. return rc;
  678. }
  679. static int uhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  680. {
  681. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  682. struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
  683. int rc = 0;
  684. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  685. spin_lock_irq(&uhci->lock);
  686. if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
  687. goto done_okay; /* Already suspended or dead */
  688. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  689. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  690. rc = -EBUSY;
  691. goto done;
  692. };
  693. /* All PCI host controllers are required to disable IRQ generation
  694. * at the source, so we must turn off PIRQ.
  695. */
  696. pci_write_config_word(pdev, USBLEGSUP, 0);
  697. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  698. /* Enable platform-specific non-PME# wakeup */
  699. if (do_wakeup) {
  700. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  701. pci_write_config_byte(pdev, USBRES_INTEL,
  702. USBPORT1EN | USBPORT2EN);
  703. }
  704. done_okay:
  705. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  706. done:
  707. spin_unlock_irq(&uhci->lock);
  708. return rc;
  709. }
  710. static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
  711. {
  712. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  713. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  714. /* Since we aren't in D3 any more, it's safe to set this flag
  715. * even if the controller was dead.
  716. */
  717. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  718. spin_lock_irq(&uhci->lock);
  719. /* Make sure resume from hibernation re-enumerates everything */
  720. if (hibernated)
  721. uhci_hc_died(uhci);
  722. /* The firmware or a boot kernel may have changed the controller
  723. * settings during a system wakeup. Check it and reconfigure
  724. * to avoid problems.
  725. */
  726. check_and_reset_hc(uhci);
  727. /* If the controller was dead before, it's back alive now */
  728. configure_hc(uhci);
  729. /* Tell the core if the controller had to be reset */
  730. if (uhci->rh_state == UHCI_RH_RESET)
  731. usb_root_hub_lost_power(hcd->self.root_hub);
  732. spin_unlock_irq(&uhci->lock);
  733. /* If interrupts don't work and remote wakeup is enabled then
  734. * the suspended root hub needs to be polled.
  735. */
  736. if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup)
  737. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  738. /* Does the root hub have a port wakeup pending? */
  739. usb_hcd_poll_rh_status(hcd);
  740. return 0;
  741. }
  742. #endif
  743. /* Wait until a particular device/endpoint's QH is idle, and free it */
  744. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  745. struct usb_host_endpoint *hep)
  746. {
  747. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  748. struct uhci_qh *qh;
  749. spin_lock_irq(&uhci->lock);
  750. qh = (struct uhci_qh *) hep->hcpriv;
  751. if (qh == NULL)
  752. goto done;
  753. while (qh->state != QH_STATE_IDLE) {
  754. ++uhci->num_waiting;
  755. spin_unlock_irq(&uhci->lock);
  756. wait_event_interruptible(uhci->waitqh,
  757. qh->state == QH_STATE_IDLE);
  758. spin_lock_irq(&uhci->lock);
  759. --uhci->num_waiting;
  760. }
  761. uhci_free_qh(uhci, qh);
  762. done:
  763. spin_unlock_irq(&uhci->lock);
  764. }
  765. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  766. {
  767. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  768. unsigned frame_number;
  769. unsigned delta;
  770. /* Minimize latency by avoiding the spinlock */
  771. frame_number = uhci->frame_number;
  772. barrier();
  773. delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
  774. (UHCI_NUMFRAMES - 1);
  775. return frame_number + delta;
  776. }
  777. static const char hcd_name[] = "uhci_hcd";
  778. static const struct hc_driver uhci_driver = {
  779. .description = hcd_name,
  780. .product_desc = "UHCI Host Controller",
  781. .hcd_priv_size = sizeof(struct uhci_hcd),
  782. /* Generic hardware linkage */
  783. .irq = uhci_irq,
  784. .flags = HCD_USB11,
  785. /* Basic lifecycle operations */
  786. .reset = uhci_init,
  787. .start = uhci_start,
  788. #ifdef CONFIG_PM
  789. .pci_suspend = uhci_pci_suspend,
  790. .pci_resume = uhci_pci_resume,
  791. .bus_suspend = uhci_rh_suspend,
  792. .bus_resume = uhci_rh_resume,
  793. #endif
  794. .stop = uhci_stop,
  795. .urb_enqueue = uhci_urb_enqueue,
  796. .urb_dequeue = uhci_urb_dequeue,
  797. .endpoint_disable = uhci_hcd_endpoint_disable,
  798. .get_frame_number = uhci_hcd_get_frame_number,
  799. .hub_status_data = uhci_hub_status_data,
  800. .hub_control = uhci_hub_control,
  801. };
  802. static const struct pci_device_id uhci_pci_ids[] = { {
  803. /* handle any USB UHCI controller */
  804. PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_UHCI, ~0),
  805. .driver_data = (unsigned long) &uhci_driver,
  806. }, { /* end: all zeroes */ }
  807. };
  808. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  809. static struct pci_driver uhci_pci_driver = {
  810. .name = (char *)hcd_name,
  811. .id_table = uhci_pci_ids,
  812. .probe = usb_hcd_pci_probe,
  813. .remove = usb_hcd_pci_remove,
  814. .shutdown = uhci_shutdown,
  815. #ifdef CONFIG_PM_SLEEP
  816. .driver = {
  817. .pm = &usb_hcd_pci_pm_ops
  818. },
  819. #endif
  820. };
  821. static int __init uhci_hcd_init(void)
  822. {
  823. int retval = -ENOMEM;
  824. if (usb_disabled())
  825. return -ENODEV;
  826. printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
  827. ignore_oc ? ", overcurrent ignored" : "");
  828. set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  829. if (DEBUG_CONFIGURED) {
  830. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  831. if (!errbuf)
  832. goto errbuf_failed;
  833. uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
  834. if (!uhci_debugfs_root)
  835. goto debug_failed;
  836. }
  837. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  838. sizeof(struct urb_priv), 0, 0, NULL);
  839. if (!uhci_up_cachep)
  840. goto up_failed;
  841. retval = pci_register_driver(&uhci_pci_driver);
  842. if (retval)
  843. goto init_failed;
  844. return 0;
  845. init_failed:
  846. kmem_cache_destroy(uhci_up_cachep);
  847. up_failed:
  848. debugfs_remove(uhci_debugfs_root);
  849. debug_failed:
  850. kfree(errbuf);
  851. errbuf_failed:
  852. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  853. return retval;
  854. }
  855. static void __exit uhci_hcd_cleanup(void)
  856. {
  857. pci_unregister_driver(&uhci_pci_driver);
  858. kmem_cache_destroy(uhci_up_cachep);
  859. debugfs_remove(uhci_debugfs_root);
  860. kfree(errbuf);
  861. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  862. }
  863. module_init(uhci_hcd_init);
  864. module_exit(uhci_hcd_cleanup);
  865. MODULE_AUTHOR(DRIVER_AUTHOR);
  866. MODULE_DESCRIPTION(DRIVER_DESC);
  867. MODULE_LICENSE("GPL");