uhci-hcd.c 27 KB

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