uhci-hcd.c 28 KB

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