uhci-hcd.c 26 KB

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