uhci-hcd.c 28 KB

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