uhci-hcd.c 24 KB

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