uhci-hcd.c 24 KB

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