uhci-hcd.c 28 KB

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