uhci-hcd.c 26 KB

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