uhci-hcd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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->frame_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. uhci_free_qh(uhci, uhci->skelqh[i]);
  385. uhci_free_td(uhci, uhci->term_td);
  386. dma_pool_destroy(uhci->qh_pool);
  387. dma_pool_destroy(uhci->td_pool);
  388. kfree(uhci->frame_cpu);
  389. dma_free_coherent(uhci_dev(uhci),
  390. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  391. uhci->frame, uhci->frame_dma_handle);
  392. debugfs_remove(uhci->dentry);
  393. }
  394. static int uhci_reset(struct usb_hcd *hcd)
  395. {
  396. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  397. unsigned io_size = (unsigned) hcd->rsrc_len;
  398. int port;
  399. uhci->io_addr = (unsigned long) hcd->rsrc_start;
  400. /* The UHCI spec says devices must have 2 ports, and goes on to say
  401. * they may have more but gives no way to determine how many there
  402. * are. However according to the UHCI spec, Bit 7 of the port
  403. * status and control register is always set to 1. So we try to
  404. * use this to our advantage. Another common failure mode when
  405. * a nonexistent register is addressed is to return all ones, so
  406. * we test for that also.
  407. */
  408. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  409. unsigned int portstatus;
  410. portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
  411. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  412. break;
  413. }
  414. if (debug)
  415. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  416. /* Anything greater than 7 is weird so we'll ignore it. */
  417. if (port > UHCI_RH_MAXCHILD) {
  418. dev_info(uhci_dev(uhci), "port count misdetected? "
  419. "forcing to 2 ports\n");
  420. port = 2;
  421. }
  422. uhci->rh_numports = port;
  423. /* Kick BIOS off this hardware and reset if the controller
  424. * isn't already safely quiescent.
  425. */
  426. check_and_reset_hc(uhci);
  427. return 0;
  428. }
  429. /* Make sure the controller is quiescent and that we're not using it
  430. * any more. This is mainly for the benefit of programs which, like kexec,
  431. * expect the hardware to be idle: not doing DMA or generating IRQs.
  432. *
  433. * This routine may be called in a damaged or failing kernel. Hence we
  434. * do not acquire the spinlock before shutting down the controller.
  435. */
  436. static void uhci_shutdown(struct pci_dev *pdev)
  437. {
  438. struct usb_hcd *hcd = (struct usb_hcd *) pci_get_drvdata(pdev);
  439. hc_died(hcd_to_uhci(hcd));
  440. }
  441. /*
  442. * Allocate a frame list, and then setup the skeleton
  443. *
  444. * The hardware doesn't really know any difference
  445. * in the queues, but the order does matter for the
  446. * protocols higher up. The order is:
  447. *
  448. * - any isochronous events handled before any
  449. * of the queues. We don't do that here, because
  450. * we'll create the actual TD entries on demand.
  451. * - The first queue is the interrupt queue.
  452. * - The second queue is the control queue, split into low- and full-speed
  453. * - The third queue is bulk queue.
  454. * - The fourth queue is the bandwidth reclamation queue, which loops back
  455. * to the full-speed control queue.
  456. */
  457. static int uhci_start(struct usb_hcd *hcd)
  458. {
  459. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  460. int retval = -EBUSY;
  461. int i;
  462. struct dentry *dentry;
  463. hcd->uses_new_polling = 1;
  464. if (pci_find_capability(to_pci_dev(uhci_dev(uhci)), PCI_CAP_ID_PM))
  465. hcd->can_wakeup = 1; /* Assume it supports PME# */
  466. dentry = debugfs_create_file(hcd->self.bus_name,
  467. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci,
  468. &uhci_debug_operations);
  469. if (!dentry) {
  470. dev_err(uhci_dev(uhci),
  471. "couldn't create uhci debugfs entry\n");
  472. retval = -ENOMEM;
  473. goto err_create_debug_entry;
  474. }
  475. uhci->dentry = dentry;
  476. uhci->fsbr = 0;
  477. uhci->fsbrtimeout = 0;
  478. spin_lock_init(&uhci->lock);
  479. INIT_LIST_HEAD(&uhci->qh_remove_list);
  480. INIT_LIST_HEAD(&uhci->td_remove_list);
  481. INIT_LIST_HEAD(&uhci->urb_remove_list);
  482. INIT_LIST_HEAD(&uhci->urb_list);
  483. INIT_LIST_HEAD(&uhci->complete_list);
  484. init_waitqueue_head(&uhci->waitqh);
  485. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  486. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  487. &uhci->frame_dma_handle, 0);
  488. if (!uhci->frame) {
  489. dev_err(uhci_dev(uhci), "unable to allocate "
  490. "consistent memory for frame list\n");
  491. goto err_alloc_frame;
  492. }
  493. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  494. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  495. GFP_KERNEL);
  496. if (!uhci->frame_cpu) {
  497. dev_err(uhci_dev(uhci), "unable to allocate "
  498. "memory for frame pointers\n");
  499. goto err_alloc_frame_cpu;
  500. }
  501. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  502. sizeof(struct uhci_td), 16, 0);
  503. if (!uhci->td_pool) {
  504. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  505. goto err_create_td_pool;
  506. }
  507. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  508. sizeof(struct uhci_qh), 16, 0);
  509. if (!uhci->qh_pool) {
  510. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  511. goto err_create_qh_pool;
  512. }
  513. uhci->term_td = uhci_alloc_td(uhci);
  514. if (!uhci->term_td) {
  515. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  516. goto err_alloc_term_td;
  517. }
  518. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  519. uhci->skelqh[i] = uhci_alloc_qh(uhci);
  520. if (!uhci->skelqh[i]) {
  521. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  522. goto err_alloc_skelqh;
  523. }
  524. }
  525. /*
  526. * 8 Interrupt queues; link all higher int queues to int1,
  527. * then link int1 to control and control to bulk
  528. */
  529. uhci->skel_int128_qh->link =
  530. uhci->skel_int64_qh->link =
  531. uhci->skel_int32_qh->link =
  532. uhci->skel_int16_qh->link =
  533. uhci->skel_int8_qh->link =
  534. uhci->skel_int4_qh->link =
  535. uhci->skel_int2_qh->link =
  536. cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH;
  537. uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH;
  538. uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  539. uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH;
  540. uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH;
  541. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  542. uhci_fill_td(uhci->term_td, 0, (UHCI_NULL_DATA_SIZE << 21) |
  543. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  544. uhci->term_td->link = cpu_to_le32(uhci->term_td->dma_handle);
  545. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  546. uhci->skel_term_qh->element = cpu_to_le32(uhci->term_td->dma_handle);
  547. /*
  548. * Fill the frame list: make all entries point to the proper
  549. * interrupt queue.
  550. *
  551. * The interrupt queues will be interleaved as evenly as possible.
  552. * There's not much to be done about period-1 interrupts; they have
  553. * to occur in every frame. But we can schedule period-2 interrupts
  554. * in odd-numbered frames, period-4 interrupts in frames congruent
  555. * to 2 (mod 4), and so on. This way each frame only has two
  556. * interrupt QHs, which will help spread out bandwidth utilization.
  557. */
  558. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  559. int irq;
  560. /*
  561. * ffs (Find First bit Set) does exactly what we need:
  562. * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6],
  563. * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc.
  564. * ffs > 6 => not on any high-period queue, so use
  565. * skel_int1_qh = skelqh[7].
  566. * Add UHCI_NUMFRAMES to insure at least one bit is set.
  567. */
  568. irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES);
  569. if (irq < 0)
  570. irq = 7;
  571. /* Only place we don't use the frame list routines */
  572. uhci->frame[i] = UHCI_PTR_QH |
  573. cpu_to_le32(uhci->skelqh[irq]->dma_handle);
  574. }
  575. /*
  576. * Some architectures require a full mb() to enforce completion of
  577. * the memory writes above before the I/O transfers in configure_hc().
  578. */
  579. mb();
  580. configure_hc(uhci);
  581. start_rh(uhci);
  582. return 0;
  583. /*
  584. * error exits:
  585. */
  586. err_alloc_skelqh:
  587. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  588. if (uhci->skelqh[i])
  589. uhci_free_qh(uhci, uhci->skelqh[i]);
  590. }
  591. uhci_free_td(uhci, uhci->term_td);
  592. err_alloc_term_td:
  593. dma_pool_destroy(uhci->qh_pool);
  594. err_create_qh_pool:
  595. dma_pool_destroy(uhci->td_pool);
  596. err_create_td_pool:
  597. kfree(uhci->frame_cpu);
  598. err_alloc_frame_cpu:
  599. dma_free_coherent(uhci_dev(uhci),
  600. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  601. uhci->frame, uhci->frame_dma_handle);
  602. err_alloc_frame:
  603. debugfs_remove(uhci->dentry);
  604. err_create_debug_entry:
  605. return retval;
  606. }
  607. static void uhci_stop(struct usb_hcd *hcd)
  608. {
  609. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  610. spin_lock_irq(&uhci->lock);
  611. if (!uhci->hc_inaccessible)
  612. reset_hc(uhci);
  613. uhci_scan_schedule(uhci, NULL);
  614. spin_unlock_irq(&uhci->lock);
  615. release_uhci(uhci);
  616. }
  617. #ifdef CONFIG_PM
  618. static int uhci_rh_suspend(struct usb_hcd *hcd)
  619. {
  620. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  621. spin_lock_irq(&uhci->lock);
  622. if (!uhci->hc_inaccessible) /* Not dead */
  623. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  624. spin_unlock_irq(&uhci->lock);
  625. return 0;
  626. }
  627. static int uhci_rh_resume(struct usb_hcd *hcd)
  628. {
  629. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  630. int rc = 0;
  631. spin_lock_irq(&uhci->lock);
  632. if (uhci->hc_inaccessible) {
  633. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  634. dev_warn(uhci_dev(uhci), "HC isn't running!\n");
  635. rc = -ENODEV;
  636. }
  637. /* Otherwise the HC is dead */
  638. } else
  639. wakeup_rh(uhci);
  640. spin_unlock_irq(&uhci->lock);
  641. return rc;
  642. }
  643. static int uhci_suspend(struct usb_hcd *hcd, pm_message_t message)
  644. {
  645. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  646. int rc = 0;
  647. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  648. spin_lock_irq(&uhci->lock);
  649. if (uhci->hc_inaccessible) /* Dead or already suspended */
  650. goto done;
  651. #ifndef CONFIG_USB_SUSPEND
  652. /* Otherwise this would never happen */
  653. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  654. #endif
  655. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  656. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  657. hcd->state = HC_STATE_RUNNING;
  658. rc = -EBUSY;
  659. goto done;
  660. };
  661. /* All PCI host controllers are required to disable IRQ generation
  662. * at the source, so we must turn off PIRQ.
  663. */
  664. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
  665. uhci->hc_inaccessible = 1;
  666. hcd->poll_rh = 0;
  667. /* FIXME: Enable non-PME# remote wakeup? */
  668. done:
  669. spin_unlock_irq(&uhci->lock);
  670. return rc;
  671. }
  672. static int uhci_resume(struct usb_hcd *hcd)
  673. {
  674. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  675. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  676. if (uhci->rh_state == UHCI_RH_RESET) /* Dead */
  677. return 0;
  678. spin_lock_irq(&uhci->lock);
  679. /* FIXME: Disable non-PME# remote wakeup? */
  680. uhci->hc_inaccessible = 0;
  681. /* The BIOS may have changed the controller settings during a
  682. * system wakeup. Check it and reconfigure to avoid problems.
  683. */
  684. check_and_reset_hc(uhci);
  685. configure_hc(uhci);
  686. #ifndef CONFIG_USB_SUSPEND
  687. /* Otherwise this would never happen */
  688. wakeup_rh(uhci);
  689. #endif
  690. if (uhci->rh_state == UHCI_RH_RESET)
  691. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  692. spin_unlock_irq(&uhci->lock);
  693. if (!uhci->working_RD) {
  694. /* Suspended root hub needs to be polled */
  695. hcd->poll_rh = 1;
  696. usb_hcd_poll_rh_status(hcd);
  697. }
  698. return 0;
  699. }
  700. #endif
  701. /* Wait until all the URBs for a particular device/endpoint are gone */
  702. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  703. struct usb_host_endpoint *ep)
  704. {
  705. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  706. wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list));
  707. }
  708. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  709. {
  710. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  711. unsigned long flags;
  712. int is_stopped;
  713. int frame_number;
  714. /* Minimize latency by avoiding the spinlock */
  715. local_irq_save(flags);
  716. is_stopped = uhci->is_stopped;
  717. smp_rmb();
  718. frame_number = (is_stopped ? uhci->frame_number :
  719. inw(uhci->io_addr + USBFRNUM));
  720. local_irq_restore(flags);
  721. return frame_number;
  722. }
  723. static const char hcd_name[] = "uhci_hcd";
  724. static const struct hc_driver uhci_driver = {
  725. .description = hcd_name,
  726. .product_desc = "UHCI Host Controller",
  727. .hcd_priv_size = sizeof(struct uhci_hcd),
  728. /* Generic hardware linkage */
  729. .irq = uhci_irq,
  730. .flags = HCD_USB11,
  731. /* Basic lifecycle operations */
  732. .reset = uhci_reset,
  733. .start = uhci_start,
  734. #ifdef CONFIG_PM
  735. .suspend = uhci_suspend,
  736. .resume = uhci_resume,
  737. .hub_suspend = uhci_rh_suspend,
  738. .hub_resume = uhci_rh_resume,
  739. #endif
  740. .stop = uhci_stop,
  741. .urb_enqueue = uhci_urb_enqueue,
  742. .urb_dequeue = uhci_urb_dequeue,
  743. .endpoint_disable = uhci_hcd_endpoint_disable,
  744. .get_frame_number = uhci_hcd_get_frame_number,
  745. .hub_status_data = uhci_hub_status_data,
  746. .hub_control = uhci_hub_control,
  747. };
  748. static const struct pci_device_id uhci_pci_ids[] = { {
  749. /* handle any USB UHCI controller */
  750. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x00), ~0),
  751. .driver_data = (unsigned long) &uhci_driver,
  752. }, { /* end: all zeroes */ }
  753. };
  754. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  755. static struct pci_driver uhci_pci_driver = {
  756. .name = (char *)hcd_name,
  757. .id_table = uhci_pci_ids,
  758. .probe = usb_hcd_pci_probe,
  759. .remove = usb_hcd_pci_remove,
  760. .shutdown = uhci_shutdown,
  761. #ifdef CONFIG_PM
  762. .suspend = usb_hcd_pci_suspend,
  763. .resume = usb_hcd_pci_resume,
  764. #endif /* PM */
  765. };
  766. static int __init uhci_hcd_init(void)
  767. {
  768. int retval = -ENOMEM;
  769. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "\n");
  770. if (usb_disabled())
  771. return -ENODEV;
  772. if (debug) {
  773. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  774. if (!errbuf)
  775. goto errbuf_failed;
  776. }
  777. uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
  778. if (!uhci_debugfs_root)
  779. goto debug_failed;
  780. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  781. sizeof(struct urb_priv), 0, 0, NULL, NULL);
  782. if (!uhci_up_cachep)
  783. goto up_failed;
  784. retval = pci_register_driver(&uhci_pci_driver);
  785. if (retval)
  786. goto init_failed;
  787. return 0;
  788. init_failed:
  789. if (kmem_cache_destroy(uhci_up_cachep))
  790. warn("not all urb_priv's were freed!");
  791. up_failed:
  792. debugfs_remove(uhci_debugfs_root);
  793. debug_failed:
  794. kfree(errbuf);
  795. errbuf_failed:
  796. return retval;
  797. }
  798. static void __exit uhci_hcd_cleanup(void)
  799. {
  800. pci_unregister_driver(&uhci_pci_driver);
  801. if (kmem_cache_destroy(uhci_up_cachep))
  802. warn("not all urb_priv's were freed!");
  803. debugfs_remove(uhci_debugfs_root);
  804. kfree(errbuf);
  805. }
  806. module_init(uhci_hcd_init);
  807. module_exit(uhci_hcd_cleanup);
  808. MODULE_AUTHOR(DRIVER_AUTHOR);
  809. MODULE_DESCRIPTION(DRIVER_DESC);
  810. MODULE_LICENSE("GPL");