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