uhci-hcd.c 25 KB

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