uhci-hcd.c 26 KB

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