uhci-hcd.c 25 KB

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