uhci-hcd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * Universal Host Controller Interface driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * (C) Copyright 1999 Linus Torvalds
  7. * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
  8. * (C) Copyright 1999 Randy Dunlap
  9. * (C) Copyright 1999 Georg Acher, acher@in.tum.de
  10. * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
  11. * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
  12. * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
  13. * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
  14. * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  15. * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
  16. * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
  17. *
  18. * Intel documents this fairly well, and as far as I know there
  19. * are no royalties or anything like that, but even so there are
  20. * people who decided that they want to do the same thing in a
  21. * completely different way.
  22. *
  23. */
  24. #include <linux/config.h>
  25. #ifdef CONFIG_USB_DEBUG
  26. #define DEBUG
  27. #else
  28. #undef DEBUG
  29. #endif
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/delay.h>
  35. #include <linux/ioport.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/errno.h>
  40. #include <linux/unistd.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/pm.h>
  45. #include <linux/dmapool.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/usb.h>
  48. #include <linux/bitops.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. #include <asm/irq.h>
  52. #include <asm/system.h>
  53. #include "../core/hcd.h"
  54. #include "uhci-hcd.h"
  55. /*
  56. * Version Information
  57. */
  58. #define DRIVER_VERSION "v2.3"
  59. #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
  60. Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
  61. Alan Stern"
  62. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  63. /*
  64. * debug = 0, no debugging messages
  65. * debug = 1, dump failed URB's except for stalls
  66. * debug = 2, dump all failed URB's (including stalls)
  67. * show all queues in /debug/uhci/[pci_addr]
  68. * debug = 3, show all TD's in URB's when dumping
  69. */
  70. #ifdef DEBUG
  71. static int debug = 1;
  72. #else
  73. static int debug = 0;
  74. #endif
  75. module_param(debug, int, S_IRUGO | S_IWUSR);
  76. MODULE_PARM_DESC(debug, "Debug level");
  77. static char *errbuf;
  78. #define ERRBUF_LEN (32 * 1024)
  79. static kmem_cache_t *uhci_up_cachep; /* urb_priv */
  80. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  81. static void wakeup_rh(struct uhci_hcd *uhci);
  82. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  83. /* If a transfer is still active after this much time, turn off FSBR */
  84. #define IDLE_TIMEOUT msecs_to_jiffies(50)
  85. #define FSBR_DELAY msecs_to_jiffies(50)
  86. /* When we timeout an idle transfer for FSBR, we'll switch it over to */
  87. /* depth first traversal. We'll do it in groups of this number of TD's */
  88. /* to make sure it doesn't hog all of the bandwidth */
  89. #define DEPTH_INTERVAL 5
  90. #include "uhci-debug.c"
  91. #include "uhci-q.c"
  92. #include "uhci-hub.c"
  93. extern void uhci_reset_hc(struct pci_dev *pdev, unsigned long base);
  94. extern int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base);
  95. /*
  96. * Finish up a host controller reset and update the recorded state.
  97. */
  98. static void finish_reset(struct uhci_hcd *uhci)
  99. {
  100. int port;
  101. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  102. * bits in the port status and control registers.
  103. * We have to clear them by hand.
  104. */
  105. for (port = 0; port < uhci->rh_numports; ++port)
  106. outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
  107. uhci->port_c_suspend = uhci->suspended_ports =
  108. uhci->resuming_ports = 0;
  109. uhci->rh_state = UHCI_RH_RESET;
  110. uhci->is_stopped = UHCI_IS_STOPPED;
  111. uhci_to_hcd(uhci)->state = HC_STATE_HALT;
  112. uhci_to_hcd(uhci)->poll_rh = 0;
  113. }
  114. /*
  115. * Last rites for a defunct/nonfunctional controller
  116. * or one we don't want to use any more.
  117. */
  118. static void hc_died(struct uhci_hcd *uhci)
  119. {
  120. uhci_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr);
  121. finish_reset(uhci);
  122. uhci->hc_inaccessible = 1;
  123. }
  124. /*
  125. * Initialize a controller that was newly discovered or has just been
  126. * resumed. In either case we can't be sure of its previous state.
  127. */
  128. static void check_and_reset_hc(struct uhci_hcd *uhci)
  129. {
  130. if (uhci_check_and_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr))
  131. finish_reset(uhci);
  132. }
  133. /*
  134. * Store the basic register settings needed by the controller.
  135. */
  136. static void configure_hc(struct uhci_hcd *uhci)
  137. {
  138. /* Set the frame length to the default: 1 ms exactly */
  139. outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
  140. /* Store the frame list base address */
  141. outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
  142. /* Set the current frame number */
  143. outw(uhci->frame_number, uhci->io_addr + USBFRNUM);
  144. /* Mark controller as not halted before we enable interrupts */
  145. uhci_to_hcd(uhci)->state = HC_STATE_SUSPENDED;
  146. mb();
  147. /* Enable PIRQ */
  148. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  149. USBLEGSUP_DEFAULT);
  150. }
  151. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  152. {
  153. int port;
  154. switch (to_pci_dev(uhci_dev(uhci))->vendor) {
  155. default:
  156. break;
  157. case PCI_VENDOR_ID_GENESYS:
  158. /* Genesys Logic's GL880S controllers don't generate
  159. * resume-detect interrupts.
  160. */
  161. return 1;
  162. case PCI_VENDOR_ID_INTEL:
  163. /* Some of Intel's USB controllers have a bug that causes
  164. * resume-detect interrupts if any port has an over-current
  165. * condition. To make matters worse, some motherboards
  166. * hardwire unused USB ports' over-current inputs active!
  167. * To prevent problems, we will not enable resume-detect
  168. * interrupts if any ports are OC.
  169. */
  170. for (port = 0; port < uhci->rh_numports; ++port) {
  171. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  172. USBPORTSC_OC)
  173. return 1;
  174. }
  175. break;
  176. }
  177. return 0;
  178. }
  179. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  180. __releases(uhci->lock)
  181. __acquires(uhci->lock)
  182. {
  183. int auto_stop;
  184. int int_enable;
  185. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  186. dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
  187. (auto_stop ? " (auto-stop)" : ""));
  188. /* If we get a suspend request when we're already auto-stopped
  189. * then there's nothing to do.
  190. */
  191. if (uhci->rh_state == UHCI_RH_AUTO_STOPPED) {
  192. uhci->rh_state = new_state;
  193. return;
  194. }
  195. /* Enable resume-detect interrupts if they work.
  196. * Then enter Global Suspend mode, still configured.
  197. */
  198. uhci->working_RD = 1;
  199. int_enable = USBINTR_RESUME;
  200. if (resume_detect_interrupts_are_broken(uhci)) {
  201. uhci->working_RD = int_enable = 0;
  202. }
  203. outw(int_enable, uhci->io_addr + USBINTR);
  204. outw(USBCMD_EGSM | USBCMD_CF, uhci->io_addr + USBCMD);
  205. mb();
  206. udelay(5);
  207. /* If we're auto-stopping then no devices have been attached
  208. * for a while, so there shouldn't be any active URBs and the
  209. * controller should stop after a few microseconds. Otherwise
  210. * we will give the controller one frame to stop.
  211. */
  212. if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
  213. uhci->rh_state = UHCI_RH_SUSPENDING;
  214. spin_unlock_irq(&uhci->lock);
  215. msleep(1);
  216. spin_lock_irq(&uhci->lock);
  217. if (uhci->hc_inaccessible) /* Died */
  218. return;
  219. }
  220. if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
  221. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  222. uhci_get_current_frame_number(uhci);
  223. smp_wmb();
  224. uhci->rh_state = new_state;
  225. uhci->is_stopped = UHCI_IS_STOPPED;
  226. uhci_to_hcd(uhci)->poll_rh = !int_enable;
  227. uhci_scan_schedule(uhci, NULL);
  228. }
  229. static void start_rh(struct uhci_hcd *uhci)
  230. {
  231. uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
  232. uhci->is_stopped = 0;
  233. smp_wmb();
  234. /* Mark it configured and running with a 64-byte max packet.
  235. * All interrupts are enabled, even though RESUME won't do anything.
  236. */
  237. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
  238. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  239. uhci->io_addr + USBINTR);
  240. mb();
  241. uhci->rh_state = UHCI_RH_RUNNING;
  242. uhci_to_hcd(uhci)->poll_rh = 1;
  243. }
  244. static void wakeup_rh(struct uhci_hcd *uhci)
  245. __releases(uhci->lock)
  246. __acquires(uhci->lock)
  247. {
  248. dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
  249. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  250. " (auto-start)" : "");
  251. /* If we are auto-stopped then no devices are attached so there's
  252. * no need for wakeup signals. Otherwise we send Global Resume
  253. * for 20 ms.
  254. */
  255. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  256. uhci->rh_state = UHCI_RH_RESUMING;
  257. outw(USBCMD_FGR | USBCMD_EGSM | USBCMD_CF,
  258. uhci->io_addr + USBCMD);
  259. spin_unlock_irq(&uhci->lock);
  260. msleep(20);
  261. spin_lock_irq(&uhci->lock);
  262. if (uhci->hc_inaccessible) /* Died */
  263. return;
  264. /* End Global Resume and wait for EOP to be sent */
  265. outw(USBCMD_CF, uhci->io_addr + USBCMD);
  266. mb();
  267. udelay(4);
  268. if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
  269. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  270. }
  271. start_rh(uhci);
  272. /* Restart root hub polling */
  273. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  274. }
  275. static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
  276. {
  277. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  278. unsigned short status;
  279. unsigned long flags;
  280. /*
  281. * Read the interrupt status, and write it back to clear the
  282. * interrupt cause. Contrary to the UHCI specification, the
  283. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  284. */
  285. status = inw(uhci->io_addr + USBSTS);
  286. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  287. return IRQ_NONE;
  288. outw(status, uhci->io_addr + USBSTS); /* Clear it */
  289. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  290. if (status & USBSTS_HSE)
  291. dev_err(uhci_dev(uhci), "host system error, "
  292. "PCI problems?\n");
  293. if (status & USBSTS_HCPE)
  294. dev_err(uhci_dev(uhci), "host controller process "
  295. "error, something bad happened!\n");
  296. if (status & USBSTS_HCH) {
  297. spin_lock_irqsave(&uhci->lock, flags);
  298. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  299. dev_err(uhci_dev(uhci),
  300. "host controller halted, "
  301. "very bad!\n");
  302. hc_died(uhci);
  303. /* Force a callback in case there are
  304. * pending unlinks */
  305. mod_timer(&hcd->rh_timer, jiffies);
  306. }
  307. spin_unlock_irqrestore(&uhci->lock, flags);
  308. }
  309. }
  310. if (status & USBSTS_RD)
  311. usb_hcd_poll_rh_status(hcd);
  312. else {
  313. spin_lock_irqsave(&uhci->lock, flags);
  314. uhci_scan_schedule(uhci, regs);
  315. spin_unlock_irqrestore(&uhci->lock, flags);
  316. }
  317. return IRQ_HANDLED;
  318. }
  319. /*
  320. * Store the current frame number in uhci->frame_number if the controller
  321. * is runnning
  322. */
  323. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  324. {
  325. if (!uhci->is_stopped)
  326. uhci->frame_number = inw(uhci->io_addr + USBFRNUM);
  327. }
  328. /*
  329. * De-allocate all resources
  330. */
  331. static void release_uhci(struct uhci_hcd *uhci)
  332. {
  333. int i;
  334. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  335. uhci_free_qh(uhci, uhci->skelqh[i]);
  336. uhci_free_td(uhci, uhci->term_td);
  337. dma_pool_destroy(uhci->qh_pool);
  338. dma_pool_destroy(uhci->td_pool);
  339. kfree(uhci->frame_cpu);
  340. dma_free_coherent(uhci_dev(uhci),
  341. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  342. uhci->frame, uhci->frame_dma_handle);
  343. debugfs_remove(uhci->dentry);
  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. if (pci_find_capability(to_pci_dev(uhci_dev(uhci)), PCI_CAP_ID_PM))
  416. hcd->can_wakeup = 1; /* Assume it supports PME# */
  417. dentry = debugfs_create_file(hcd->self.bus_name,
  418. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci,
  419. &uhci_debug_operations);
  420. if (!dentry) {
  421. dev_err(uhci_dev(uhci),
  422. "couldn't create uhci debugfs entry\n");
  423. retval = -ENOMEM;
  424. goto err_create_debug_entry;
  425. }
  426. uhci->dentry = dentry;
  427. uhci->fsbr = 0;
  428. uhci->fsbrtimeout = 0;
  429. spin_lock_init(&uhci->lock);
  430. INIT_LIST_HEAD(&uhci->qh_remove_list);
  431. INIT_LIST_HEAD(&uhci->td_remove_list);
  432. INIT_LIST_HEAD(&uhci->urb_remove_list);
  433. INIT_LIST_HEAD(&uhci->urb_list);
  434. INIT_LIST_HEAD(&uhci->complete_list);
  435. init_waitqueue_head(&uhci->waitqh);
  436. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  437. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  438. &uhci->frame_dma_handle, 0);
  439. if (!uhci->frame) {
  440. dev_err(uhci_dev(uhci), "unable to allocate "
  441. "consistent memory for frame list\n");
  442. goto err_alloc_frame;
  443. }
  444. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  445. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  446. GFP_KERNEL);
  447. if (!uhci->frame_cpu) {
  448. dev_err(uhci_dev(uhci), "unable to allocate "
  449. "memory for frame pointers\n");
  450. goto err_alloc_frame_cpu;
  451. }
  452. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  453. sizeof(struct uhci_td), 16, 0);
  454. if (!uhci->td_pool) {
  455. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  456. goto err_create_td_pool;
  457. }
  458. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  459. sizeof(struct uhci_qh), 16, 0);
  460. if (!uhci->qh_pool) {
  461. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  462. goto err_create_qh_pool;
  463. }
  464. uhci->term_td = uhci_alloc_td(uhci);
  465. if (!uhci->term_td) {
  466. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  467. goto err_alloc_term_td;
  468. }
  469. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  470. uhci->skelqh[i] = uhci_alloc_qh(uhci);
  471. if (!uhci->skelqh[i]) {
  472. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  473. goto err_alloc_skelqh;
  474. }
  475. }
  476. /*
  477. * 8 Interrupt queues; link all higher int queues to int1,
  478. * then link int1 to control and control to bulk
  479. */
  480. uhci->skel_int128_qh->link =
  481. uhci->skel_int64_qh->link =
  482. uhci->skel_int32_qh->link =
  483. uhci->skel_int16_qh->link =
  484. uhci->skel_int8_qh->link =
  485. uhci->skel_int4_qh->link =
  486. uhci->skel_int2_qh->link =
  487. cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH;
  488. uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH;
  489. uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  490. uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH;
  491. uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH;
  492. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  493. uhci_fill_td(uhci->term_td, 0, (UHCI_NULL_DATA_SIZE << 21) |
  494. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  495. uhci->term_td->link = cpu_to_le32(uhci->term_td->dma_handle);
  496. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  497. uhci->skel_term_qh->element = cpu_to_le32(uhci->term_td->dma_handle);
  498. /*
  499. * Fill the frame list: make all entries point to the proper
  500. * interrupt queue.
  501. *
  502. * The interrupt queues will be interleaved as evenly as possible.
  503. * There's not much to be done about period-1 interrupts; they have
  504. * to occur in every frame. But we can schedule period-2 interrupts
  505. * in odd-numbered frames, period-4 interrupts in frames congruent
  506. * to 2 (mod 4), and so on. This way each frame only has two
  507. * interrupt QHs, which will help spread out bandwidth utilization.
  508. */
  509. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  510. int irq;
  511. /*
  512. * ffs (Find First bit Set) does exactly what we need:
  513. * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6],
  514. * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc.
  515. * ffs > 6 => not on any high-period queue, so use
  516. * skel_int1_qh = skelqh[7].
  517. * Add UHCI_NUMFRAMES to insure at least one bit is set.
  518. */
  519. irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES);
  520. if (irq < 0)
  521. irq = 7;
  522. /* Only place we don't use the frame list routines */
  523. uhci->frame[i] = UHCI_PTR_QH |
  524. cpu_to_le32(uhci->skelqh[irq]->dma_handle);
  525. }
  526. /*
  527. * Some architectures require a full mb() to enforce completion of
  528. * the memory writes above before the I/O transfers in configure_hc().
  529. */
  530. mb();
  531. configure_hc(uhci);
  532. start_rh(uhci);
  533. return 0;
  534. /*
  535. * error exits:
  536. */
  537. err_alloc_skelqh:
  538. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  539. if (uhci->skelqh[i])
  540. uhci_free_qh(uhci, uhci->skelqh[i]);
  541. }
  542. uhci_free_td(uhci, uhci->term_td);
  543. err_alloc_term_td:
  544. dma_pool_destroy(uhci->qh_pool);
  545. err_create_qh_pool:
  546. dma_pool_destroy(uhci->td_pool);
  547. err_create_td_pool:
  548. kfree(uhci->frame_cpu);
  549. err_alloc_frame_cpu:
  550. dma_free_coherent(uhci_dev(uhci),
  551. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  552. uhci->frame, uhci->frame_dma_handle);
  553. err_alloc_frame:
  554. debugfs_remove(uhci->dentry);
  555. err_create_debug_entry:
  556. return retval;
  557. }
  558. static void uhci_stop(struct usb_hcd *hcd)
  559. {
  560. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  561. spin_lock_irq(&uhci->lock);
  562. if (!uhci->hc_inaccessible)
  563. hc_died(uhci);
  564. uhci_scan_schedule(uhci, NULL);
  565. spin_unlock_irq(&uhci->lock);
  566. release_uhci(uhci);
  567. }
  568. #ifdef CONFIG_PM
  569. static int uhci_rh_suspend(struct usb_hcd *hcd)
  570. {
  571. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  572. spin_lock_irq(&uhci->lock);
  573. if (!uhci->hc_inaccessible) /* Not dead */
  574. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  575. spin_unlock_irq(&uhci->lock);
  576. return 0;
  577. }
  578. static int uhci_rh_resume(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 (uhci->hc_inaccessible) {
  584. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  585. dev_warn(uhci_dev(uhci), "HC isn't running!\n");
  586. rc = -ENODEV;
  587. }
  588. /* Otherwise the HC is dead */
  589. } else
  590. wakeup_rh(uhci);
  591. spin_unlock_irq(&uhci->lock);
  592. return rc;
  593. }
  594. static int uhci_suspend(struct usb_hcd *hcd, pm_message_t message)
  595. {
  596. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  597. int rc = 0;
  598. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  599. spin_lock_irq(&uhci->lock);
  600. if (uhci->hc_inaccessible) /* Dead or already suspended */
  601. goto done;
  602. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  603. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  604. rc = -EBUSY;
  605. goto done;
  606. };
  607. /* All PCI host controllers are required to disable IRQ generation
  608. * at the source, so we must turn off PIRQ.
  609. */
  610. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
  611. 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. /* We aren't in D3 state anymore, we do that even if dead as I
  624. * really don't want to keep a stale HCD_FLAG_HW_ACCESSIBLE=0
  625. */
  626. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  627. if (uhci->rh_state == UHCI_RH_RESET) /* Dead */
  628. return 0;
  629. spin_lock_irq(&uhci->lock);
  630. /* FIXME: Disable non-PME# remote wakeup? */
  631. uhci->hc_inaccessible = 0;
  632. /* The BIOS may have changed the controller settings during a
  633. * system wakeup. Check it and reconfigure to avoid problems.
  634. */
  635. check_and_reset_hc(uhci);
  636. configure_hc(uhci);
  637. if (uhci->rh_state == UHCI_RH_RESET)
  638. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  639. spin_unlock_irq(&uhci->lock);
  640. if (!uhci->working_RD) {
  641. /* Suspended root hub needs to be polled */
  642. hcd->poll_rh = 1;
  643. usb_hcd_poll_rh_status(hcd);
  644. }
  645. return 0;
  646. }
  647. #endif
  648. /* Wait until all the URBs for a particular device/endpoint are gone */
  649. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  650. struct usb_host_endpoint *ep)
  651. {
  652. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  653. wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list));
  654. }
  655. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  656. {
  657. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  658. unsigned long flags;
  659. int is_stopped;
  660. int frame_number;
  661. /* Minimize latency by avoiding the spinlock */
  662. local_irq_save(flags);
  663. is_stopped = uhci->is_stopped;
  664. smp_rmb();
  665. frame_number = (is_stopped ? uhci->frame_number :
  666. inw(uhci->io_addr + USBFRNUM));
  667. local_irq_restore(flags);
  668. return frame_number;
  669. }
  670. static const char hcd_name[] = "uhci_hcd";
  671. static const struct hc_driver uhci_driver = {
  672. .description = hcd_name,
  673. .product_desc = "UHCI Host Controller",
  674. .hcd_priv_size = sizeof(struct uhci_hcd),
  675. /* Generic hardware linkage */
  676. .irq = uhci_irq,
  677. .flags = HCD_USB11,
  678. /* Basic lifecycle operations */
  679. .reset = uhci_reset,
  680. .start = uhci_start,
  681. #ifdef CONFIG_PM
  682. .suspend = uhci_suspend,
  683. .resume = uhci_resume,
  684. .bus_suspend = uhci_rh_suspend,
  685. .bus_resume = uhci_rh_resume,
  686. #endif
  687. .stop = uhci_stop,
  688. .urb_enqueue = uhci_urb_enqueue,
  689. .urb_dequeue = uhci_urb_dequeue,
  690. .endpoint_disable = uhci_hcd_endpoint_disable,
  691. .get_frame_number = uhci_hcd_get_frame_number,
  692. .hub_status_data = uhci_hub_status_data,
  693. .hub_control = uhci_hub_control,
  694. };
  695. static const struct pci_device_id uhci_pci_ids[] = { {
  696. /* handle any USB UHCI controller */
  697. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x00), ~0),
  698. .driver_data = (unsigned long) &uhci_driver,
  699. }, { /* end: all zeroes */ }
  700. };
  701. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  702. static struct pci_driver uhci_pci_driver = {
  703. .name = (char *)hcd_name,
  704. .id_table = uhci_pci_ids,
  705. .probe = usb_hcd_pci_probe,
  706. .remove = usb_hcd_pci_remove,
  707. .shutdown = uhci_shutdown,
  708. #ifdef CONFIG_PM
  709. .suspend = usb_hcd_pci_suspend,
  710. .resume = usb_hcd_pci_resume,
  711. #endif /* PM */
  712. };
  713. static int __init uhci_hcd_init(void)
  714. {
  715. int retval = -ENOMEM;
  716. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "\n");
  717. if (usb_disabled())
  718. return -ENODEV;
  719. if (debug) {
  720. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  721. if (!errbuf)
  722. goto errbuf_failed;
  723. }
  724. uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
  725. if (!uhci_debugfs_root)
  726. goto debug_failed;
  727. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  728. sizeof(struct urb_priv), 0, 0, NULL, NULL);
  729. if (!uhci_up_cachep)
  730. goto up_failed;
  731. retval = pci_register_driver(&uhci_pci_driver);
  732. if (retval)
  733. goto init_failed;
  734. return 0;
  735. init_failed:
  736. if (kmem_cache_destroy(uhci_up_cachep))
  737. warn("not all urb_priv's were freed!");
  738. up_failed:
  739. debugfs_remove(uhci_debugfs_root);
  740. debug_failed:
  741. kfree(errbuf);
  742. errbuf_failed:
  743. return retval;
  744. }
  745. static void __exit uhci_hcd_cleanup(void)
  746. {
  747. pci_unregister_driver(&uhci_pci_driver);
  748. if (kmem_cache_destroy(uhci_up_cachep))
  749. warn("not all urb_priv's were freed!");
  750. debugfs_remove(uhci_debugfs_root);
  751. kfree(errbuf);
  752. }
  753. module_init(uhci_hcd_init);
  754. module_exit(uhci_hcd_cleanup);
  755. MODULE_AUTHOR(DRIVER_AUTHOR);
  756. MODULE_DESCRIPTION(DRIVER_DESC);
  757. MODULE_LICENSE("GPL");