uhci-hcd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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.2"
  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. static void hc_state_transitions(struct uhci_hcd *uhci);
  87. /* If a transfer is still active after this much time, turn off FSBR */
  88. #define IDLE_TIMEOUT msecs_to_jiffies(50)
  89. #define FSBR_DELAY msecs_to_jiffies(50)
  90. /* When we timeout an idle transfer for FSBR, we'll switch it over to */
  91. /* depth first traversal. We'll do it in groups of this number of TD's */
  92. /* to make sure it doesn't hog all of the bandwidth */
  93. #define DEPTH_INTERVAL 5
  94. #include "uhci-hub.c"
  95. #include "uhci-debug.c"
  96. #include "uhci-q.c"
  97. static int init_stall_timer(struct usb_hcd *hcd);
  98. static void stall_callback(unsigned long ptr)
  99. {
  100. struct usb_hcd *hcd = (struct usb_hcd *)ptr;
  101. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  102. struct urb_priv *up;
  103. unsigned long flags;
  104. spin_lock_irqsave(&uhci->lock, flags);
  105. uhci_scan_schedule(uhci, NULL);
  106. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  107. struct urb *u = up->urb;
  108. spin_lock(&u->lock);
  109. /* Check if the FSBR timed out */
  110. if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
  111. uhci_fsbr_timeout(uhci, u);
  112. spin_unlock(&u->lock);
  113. }
  114. /* Really disable FSBR */
  115. if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
  116. uhci->fsbrtimeout = 0;
  117. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  118. }
  119. /* Poll for and perform state transitions */
  120. hc_state_transitions(uhci);
  121. if (unlikely(uhci->suspended_ports && uhci->state != UHCI_SUSPENDED))
  122. uhci_check_ports(uhci);
  123. init_stall_timer(hcd);
  124. spin_unlock_irqrestore(&uhci->lock, flags);
  125. }
  126. static int init_stall_timer(struct usb_hcd *hcd)
  127. {
  128. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  129. init_timer(&uhci->stall_timer);
  130. uhci->stall_timer.function = stall_callback;
  131. uhci->stall_timer.data = (unsigned long)hcd;
  132. uhci->stall_timer.expires = jiffies + msecs_to_jiffies(100);
  133. add_timer(&uhci->stall_timer);
  134. return 0;
  135. }
  136. static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
  137. {
  138. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  139. unsigned long io_addr = uhci->io_addr;
  140. unsigned short status;
  141. /*
  142. * Read the interrupt status, and write it back to clear the
  143. * interrupt cause. Contrary to the UHCI specification, the
  144. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  145. */
  146. status = inw(io_addr + USBSTS);
  147. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  148. return IRQ_NONE;
  149. outw(status, io_addr + USBSTS); /* Clear it */
  150. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  151. if (status & USBSTS_HSE)
  152. dev_err(uhci_dev(uhci), "host system error, "
  153. "PCI problems?\n");
  154. if (status & USBSTS_HCPE)
  155. dev_err(uhci_dev(uhci), "host controller process "
  156. "error, something bad happened!\n");
  157. if ((status & USBSTS_HCH) && uhci->state > 0) {
  158. dev_err(uhci_dev(uhci), "host controller halted, "
  159. "very bad!\n");
  160. /* FIXME: Reset the controller, fix the offending TD */
  161. }
  162. }
  163. if (status & USBSTS_RD)
  164. uhci->resume_detect = 1;
  165. spin_lock(&uhci->lock);
  166. uhci_scan_schedule(uhci, regs);
  167. spin_unlock(&uhci->lock);
  168. return IRQ_HANDLED;
  169. }
  170. static void reset_hc(struct uhci_hcd *uhci)
  171. {
  172. unsigned long io_addr = uhci->io_addr;
  173. /* Turn off PIRQ, SMI, and all interrupts. This also turns off
  174. * the BIOS's USB Legacy Support.
  175. */
  176. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
  177. outw(0, uhci->io_addr + USBINTR);
  178. /* Global reset for 50ms */
  179. uhci->state = UHCI_RESET;
  180. outw(USBCMD_GRESET, io_addr + USBCMD);
  181. msleep(50);
  182. outw(0, io_addr + USBCMD);
  183. /* Another 10ms delay */
  184. msleep(10);
  185. uhci->resume_detect = 0;
  186. uhci->is_stopped = UHCI_IS_STOPPED;
  187. }
  188. static void suspend_hc(struct uhci_hcd *uhci)
  189. {
  190. unsigned long io_addr = uhci->io_addr;
  191. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  192. uhci->state = UHCI_SUSPENDED;
  193. uhci->resume_detect = 0;
  194. outw(USBCMD_EGSM, io_addr + USBCMD);
  195. /* FIXME: Wait for the controller to actually stop */
  196. uhci_get_current_frame_number(uhci);
  197. uhci->is_stopped = UHCI_IS_STOPPED;
  198. uhci_scan_schedule(uhci, NULL);
  199. }
  200. static void wakeup_hc(struct uhci_hcd *uhci)
  201. {
  202. unsigned long io_addr = uhci->io_addr;
  203. switch (uhci->state) {
  204. case UHCI_SUSPENDED: /* Start the resume */
  205. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  206. /* Global resume for >= 20ms */
  207. outw(USBCMD_FGR | USBCMD_EGSM, io_addr + USBCMD);
  208. uhci->state = UHCI_RESUMING_1;
  209. uhci->state_end = jiffies + msecs_to_jiffies(20);
  210. uhci->is_stopped = 0;
  211. break;
  212. case UHCI_RESUMING_1: /* End global resume */
  213. uhci->state = UHCI_RESUMING_2;
  214. outw(0, io_addr + USBCMD);
  215. /* Falls through */
  216. case UHCI_RESUMING_2: /* Wait for EOP to be sent */
  217. if (inw(io_addr + USBCMD) & USBCMD_FGR)
  218. break;
  219. /* Run for at least 1 second, and
  220. * mark it configured with a 64-byte max packet */
  221. uhci->state = UHCI_RUNNING_GRACE;
  222. uhci->state_end = jiffies + HZ;
  223. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP,
  224. io_addr + USBCMD);
  225. break;
  226. case UHCI_RUNNING_GRACE: /* Now allowed to suspend */
  227. uhci->state = UHCI_RUNNING;
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. static int ports_active(struct uhci_hcd *uhci)
  234. {
  235. unsigned long io_addr = uhci->io_addr;
  236. int connection = 0;
  237. int i;
  238. for (i = 0; i < uhci->rh_numports; i++)
  239. connection |= (inw(io_addr + USBPORTSC1 + i * 2) & USBPORTSC_CCS);
  240. return connection;
  241. }
  242. static int suspend_allowed(struct uhci_hcd *uhci)
  243. {
  244. unsigned long io_addr = uhci->io_addr;
  245. int i;
  246. if (to_pci_dev(uhci_dev(uhci))->vendor != PCI_VENDOR_ID_INTEL)
  247. return 1;
  248. /* Some of Intel's USB controllers have a bug that causes false
  249. * resume indications if any port has an over current condition.
  250. * To prevent problems, we will not allow a global suspend if
  251. * any ports are OC.
  252. *
  253. * Some motherboards using Intel's chipsets (but not using all
  254. * the USB ports) appear to hardwire the over current inputs active
  255. * to disable the USB ports.
  256. */
  257. /* check for over current condition on any port */
  258. for (i = 0; i < uhci->rh_numports; i++) {
  259. if (inw(io_addr + USBPORTSC1 + i * 2) & USBPORTSC_OC)
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. static void hc_state_transitions(struct uhci_hcd *uhci)
  265. {
  266. switch (uhci->state) {
  267. case UHCI_RUNNING:
  268. /* global suspend if nothing connected for 1 second */
  269. if (!ports_active(uhci) && suspend_allowed(uhci)) {
  270. uhci->state = UHCI_SUSPENDING_GRACE;
  271. uhci->state_end = jiffies + HZ;
  272. }
  273. break;
  274. case UHCI_SUSPENDING_GRACE:
  275. if (ports_active(uhci))
  276. uhci->state = UHCI_RUNNING;
  277. else if (time_after_eq(jiffies, uhci->state_end))
  278. suspend_hc(uhci);
  279. break;
  280. case UHCI_SUSPENDED:
  281. /* wakeup if requested by a device */
  282. if (uhci->resume_detect)
  283. wakeup_hc(uhci);
  284. break;
  285. case UHCI_RESUMING_1:
  286. case UHCI_RESUMING_2:
  287. case UHCI_RUNNING_GRACE:
  288. if (time_after_eq(jiffies, uhci->state_end))
  289. wakeup_hc(uhci);
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. /*
  296. * Store the current frame number in uhci->frame_number if the controller
  297. * is runnning
  298. */
  299. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  300. {
  301. if (!uhci->is_stopped)
  302. uhci->frame_number = inw(uhci->io_addr + USBFRNUM);
  303. }
  304. static int start_hc(struct uhci_hcd *uhci)
  305. {
  306. unsigned long io_addr = uhci->io_addr;
  307. int timeout = 10;
  308. /*
  309. * Reset the HC - this will force us to get a
  310. * new notification of any already connected
  311. * ports due to the virtual disconnect that it
  312. * implies.
  313. */
  314. outw(USBCMD_HCRESET, io_addr + USBCMD);
  315. while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
  316. if (--timeout < 0) {
  317. dev_err(uhci_dev(uhci), "USBCMD_HCRESET timed out!\n");
  318. return -ETIMEDOUT;
  319. }
  320. msleep(1);
  321. }
  322. /* Mark controller as running before we enable interrupts */
  323. uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
  324. /* Turn on PIRQ and all interrupts */
  325. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  326. USBLEGSUP_DEFAULT);
  327. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  328. io_addr + USBINTR);
  329. /* Start at frame 0 */
  330. outw(0, io_addr + USBFRNUM);
  331. outl(uhci->fl->dma_handle, io_addr + USBFLBASEADD);
  332. /* Run and mark it configured with a 64-byte max packet */
  333. uhci->state = UHCI_RUNNING_GRACE;
  334. uhci->state_end = jiffies + HZ;
  335. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
  336. uhci->is_stopped = 0;
  337. return 0;
  338. }
  339. /*
  340. * De-allocate all resources
  341. */
  342. static void release_uhci(struct uhci_hcd *uhci)
  343. {
  344. int i;
  345. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  346. if (uhci->skelqh[i]) {
  347. uhci_free_qh(uhci, uhci->skelqh[i]);
  348. uhci->skelqh[i] = NULL;
  349. }
  350. if (uhci->term_td) {
  351. uhci_free_td(uhci, uhci->term_td);
  352. uhci->term_td = NULL;
  353. }
  354. if (uhci->qh_pool) {
  355. dma_pool_destroy(uhci->qh_pool);
  356. uhci->qh_pool = NULL;
  357. }
  358. if (uhci->td_pool) {
  359. dma_pool_destroy(uhci->td_pool);
  360. uhci->td_pool = NULL;
  361. }
  362. if (uhci->fl) {
  363. dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  364. uhci->fl, uhci->fl->dma_handle);
  365. uhci->fl = NULL;
  366. }
  367. if (uhci->dentry) {
  368. debugfs_remove(uhci->dentry);
  369. uhci->dentry = NULL;
  370. }
  371. }
  372. static int uhci_reset(struct usb_hcd *hcd)
  373. {
  374. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  375. uhci->io_addr = (unsigned long) hcd->rsrc_start;
  376. /* Kick BIOS off this hardware and reset, so we won't get
  377. * interrupts from any previous setup.
  378. */
  379. reset_hc(uhci);
  380. return 0;
  381. }
  382. /*
  383. * Allocate a frame list, and then setup the skeleton
  384. *
  385. * The hardware doesn't really know any difference
  386. * in the queues, but the order does matter for the
  387. * protocols higher up. The order is:
  388. *
  389. * - any isochronous events handled before any
  390. * of the queues. We don't do that here, because
  391. * we'll create the actual TD entries on demand.
  392. * - The first queue is the interrupt queue.
  393. * - The second queue is the control queue, split into low- and full-speed
  394. * - The third queue is bulk queue.
  395. * - The fourth queue is the bandwidth reclamation queue, which loops back
  396. * to the full-speed control queue.
  397. */
  398. static int uhci_start(struct usb_hcd *hcd)
  399. {
  400. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  401. int retval = -EBUSY;
  402. int i, port;
  403. unsigned io_size;
  404. dma_addr_t dma_handle;
  405. struct usb_device *udev;
  406. struct dentry *dentry;
  407. io_size = (unsigned) hcd->rsrc_len;
  408. dentry = debugfs_create_file(hcd->self.bus_name, S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci, &uhci_debug_operations);
  409. if (!dentry) {
  410. dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
  411. retval = -ENOMEM;
  412. goto err_create_debug_entry;
  413. }
  414. uhci->dentry = dentry;
  415. uhci->fsbr = 0;
  416. uhci->fsbrtimeout = 0;
  417. spin_lock_init(&uhci->lock);
  418. INIT_LIST_HEAD(&uhci->qh_remove_list);
  419. INIT_LIST_HEAD(&uhci->td_remove_list);
  420. INIT_LIST_HEAD(&uhci->urb_remove_list);
  421. INIT_LIST_HEAD(&uhci->urb_list);
  422. INIT_LIST_HEAD(&uhci->complete_list);
  423. init_waitqueue_head(&uhci->waitqh);
  424. uhci->fl = dma_alloc_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  425. &dma_handle, 0);
  426. if (!uhci->fl) {
  427. dev_err(uhci_dev(uhci), "unable to allocate "
  428. "consistent memory for frame list\n");
  429. goto err_alloc_fl;
  430. }
  431. memset((void *)uhci->fl, 0, sizeof(*uhci->fl));
  432. uhci->fl->dma_handle = dma_handle;
  433. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  434. sizeof(struct uhci_td), 16, 0);
  435. if (!uhci->td_pool) {
  436. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  437. goto err_create_td_pool;
  438. }
  439. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  440. sizeof(struct uhci_qh), 16, 0);
  441. if (!uhci->qh_pool) {
  442. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  443. goto err_create_qh_pool;
  444. }
  445. /* Initialize the root hub */
  446. /* UHCI specs says devices must have 2 ports, but goes on to say */
  447. /* they may have more but give no way to determine how many they */
  448. /* have. However, according to the UHCI spec, Bit 7 is always set */
  449. /* to 1. So we try to use this to our advantage */
  450. for (port = 0; port < (io_size - 0x10) / 2; port++) {
  451. unsigned int portstatus;
  452. portstatus = inw(uhci->io_addr + 0x10 + (port * 2));
  453. if (!(portstatus & 0x0080))
  454. break;
  455. }
  456. if (debug)
  457. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  458. /* This is experimental so anything less than 2 or greater than 8 is */
  459. /* something weird and we'll ignore it */
  460. if (port < 2 || port > UHCI_RH_MAXCHILD) {
  461. dev_info(uhci_dev(uhci), "port count misdetected? "
  462. "forcing to 2 ports\n");
  463. port = 2;
  464. }
  465. uhci->rh_numports = port;
  466. udev = usb_alloc_dev(NULL, &hcd->self, 0);
  467. if (!udev) {
  468. dev_err(uhci_dev(uhci), "unable to allocate root hub\n");
  469. goto err_alloc_root_hub;
  470. }
  471. uhci->term_td = uhci_alloc_td(uhci, udev);
  472. if (!uhci->term_td) {
  473. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  474. goto err_alloc_term_td;
  475. }
  476. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  477. uhci->skelqh[i] = uhci_alloc_qh(uhci, udev);
  478. if (!uhci->skelqh[i]) {
  479. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  480. goto err_alloc_skelqh;
  481. }
  482. }
  483. /*
  484. * 8 Interrupt queues; link all higher int queues to int1,
  485. * then link int1 to control and control to bulk
  486. */
  487. uhci->skel_int128_qh->link =
  488. uhci->skel_int64_qh->link =
  489. uhci->skel_int32_qh->link =
  490. uhci->skel_int16_qh->link =
  491. uhci->skel_int8_qh->link =
  492. uhci->skel_int4_qh->link =
  493. uhci->skel_int2_qh->link =
  494. cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH;
  495. uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH;
  496. uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  497. uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH;
  498. uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH;
  499. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  500. uhci_fill_td(uhci->term_td, 0, (UHCI_NULL_DATA_SIZE << 21) |
  501. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  502. uhci->term_td->link = cpu_to_le32(uhci->term_td->dma_handle);
  503. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  504. uhci->skel_term_qh->element = cpu_to_le32(uhci->term_td->dma_handle);
  505. /*
  506. * Fill the frame list: make all entries point to the proper
  507. * interrupt queue.
  508. *
  509. * The interrupt queues will be interleaved as evenly as possible.
  510. * There's not much to be done about period-1 interrupts; they have
  511. * to occur in every frame. But we can schedule period-2 interrupts
  512. * in odd-numbered frames, period-4 interrupts in frames congruent
  513. * to 2 (mod 4), and so on. This way each frame only has two
  514. * interrupt QHs, which will help spread out bandwidth utilization.
  515. */
  516. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  517. int irq;
  518. /*
  519. * ffs (Find First bit Set) does exactly what we need:
  520. * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6],
  521. * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc.
  522. * ffs > 6 => not on any high-period queue, so use
  523. * skel_int1_qh = skelqh[7].
  524. * Add UHCI_NUMFRAMES to insure at least one bit is set.
  525. */
  526. irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES);
  527. if (irq < 0)
  528. irq = 7;
  529. /* Only place we don't use the frame list routines */
  530. uhci->fl->frame[i] = UHCI_PTR_QH |
  531. cpu_to_le32(uhci->skelqh[irq]->dma_handle);
  532. }
  533. /*
  534. * Some architectures require a full mb() to enforce completion of
  535. * the memory writes above before the I/O transfers in start_hc().
  536. */
  537. mb();
  538. if ((retval = start_hc(uhci)) != 0)
  539. goto err_alloc_skelqh;
  540. init_stall_timer(hcd);
  541. udev->speed = USB_SPEED_FULL;
  542. if (usb_hcd_register_root_hub(udev, hcd) != 0) {
  543. dev_err(uhci_dev(uhci), "unable to start root hub\n");
  544. retval = -ENOMEM;
  545. goto err_start_root_hub;
  546. }
  547. return 0;
  548. /*
  549. * error exits:
  550. */
  551. err_start_root_hub:
  552. reset_hc(uhci);
  553. del_timer_sync(&uhci->stall_timer);
  554. err_alloc_skelqh:
  555. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  556. if (uhci->skelqh[i]) {
  557. uhci_free_qh(uhci, uhci->skelqh[i]);
  558. uhci->skelqh[i] = NULL;
  559. }
  560. uhci_free_td(uhci, uhci->term_td);
  561. uhci->term_td = NULL;
  562. err_alloc_term_td:
  563. usb_put_dev(udev);
  564. err_alloc_root_hub:
  565. dma_pool_destroy(uhci->qh_pool);
  566. uhci->qh_pool = NULL;
  567. err_create_qh_pool:
  568. dma_pool_destroy(uhci->td_pool);
  569. uhci->td_pool = NULL;
  570. err_create_td_pool:
  571. dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  572. uhci->fl, uhci->fl->dma_handle);
  573. uhci->fl = NULL;
  574. err_alloc_fl:
  575. debugfs_remove(uhci->dentry);
  576. uhci->dentry = NULL;
  577. err_create_debug_entry:
  578. return retval;
  579. }
  580. static void uhci_stop(struct usb_hcd *hcd)
  581. {
  582. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  583. del_timer_sync(&uhci->stall_timer);
  584. reset_hc(uhci);
  585. spin_lock_irq(&uhci->lock);
  586. uhci_scan_schedule(uhci, NULL);
  587. spin_unlock_irq(&uhci->lock);
  588. release_uhci(uhci);
  589. }
  590. #ifdef CONFIG_PM
  591. static int uhci_suspend(struct usb_hcd *hcd, pm_message_t message)
  592. {
  593. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  594. spin_lock_irq(&uhci->lock);
  595. /* Don't try to suspend broken motherboards, reset instead */
  596. if (suspend_allowed(uhci))
  597. suspend_hc(uhci);
  598. else {
  599. spin_unlock_irq(&uhci->lock);
  600. reset_hc(uhci);
  601. spin_lock_irq(&uhci->lock);
  602. uhci_scan_schedule(uhci, NULL);
  603. }
  604. spin_unlock_irq(&uhci->lock);
  605. return 0;
  606. }
  607. static int uhci_resume(struct usb_hcd *hcd)
  608. {
  609. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  610. int rc;
  611. pci_set_master(to_pci_dev(uhci_dev(uhci)));
  612. spin_lock_irq(&uhci->lock);
  613. if (uhci->state == UHCI_SUSPENDED) {
  614. /*
  615. * Some systems don't maintain the UHCI register values
  616. * during a PM suspend/resume cycle, so reinitialize
  617. * the Frame Number, Framelist Base Address, Interrupt
  618. * Enable, and Legacy Support registers.
  619. */
  620. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  621. 0);
  622. outw(uhci->frame_number, uhci->io_addr + USBFRNUM);
  623. outl(uhci->fl->dma_handle, uhci->io_addr + USBFLBASEADD);
  624. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC |
  625. USBINTR_SP, uhci->io_addr + USBINTR);
  626. uhci->resume_detect = 1;
  627. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  628. USBLEGSUP_DEFAULT);
  629. } else {
  630. spin_unlock_irq(&uhci->lock);
  631. reset_hc(uhci);
  632. if ((rc = start_hc(uhci)) != 0)
  633. return rc;
  634. spin_lock_irq(&uhci->lock);
  635. }
  636. hcd->state = HC_STATE_RUNNING;
  637. spin_unlock_irq(&uhci->lock);
  638. return 0;
  639. }
  640. #endif
  641. /* Wait until all the URBs for a particular device/endpoint are gone */
  642. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  643. struct usb_host_endpoint *ep)
  644. {
  645. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  646. wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list));
  647. }
  648. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  649. {
  650. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  651. int frame_number;
  652. unsigned long flags;
  653. /* Minimize latency by avoiding the spinlock */
  654. local_irq_save(flags);
  655. rmb();
  656. frame_number = (uhci->is_stopped ? uhci->frame_number :
  657. inw(uhci->io_addr + USBFRNUM));
  658. local_irq_restore(flags);
  659. return frame_number;
  660. }
  661. static const char hcd_name[] = "uhci_hcd";
  662. static const struct hc_driver uhci_driver = {
  663. .description = hcd_name,
  664. .product_desc = "UHCI Host Controller",
  665. .hcd_priv_size = sizeof(struct uhci_hcd),
  666. /* Generic hardware linkage */
  667. .irq = uhci_irq,
  668. .flags = HCD_USB11,
  669. /* Basic lifecycle operations */
  670. .reset = uhci_reset,
  671. .start = uhci_start,
  672. #ifdef CONFIG_PM
  673. .suspend = uhci_suspend,
  674. .resume = uhci_resume,
  675. #endif
  676. .stop = uhci_stop,
  677. .urb_enqueue = uhci_urb_enqueue,
  678. .urb_dequeue = uhci_urb_dequeue,
  679. .endpoint_disable = uhci_hcd_endpoint_disable,
  680. .get_frame_number = uhci_hcd_get_frame_number,
  681. .hub_status_data = uhci_hub_status_data,
  682. .hub_control = uhci_hub_control,
  683. };
  684. static const struct pci_device_id uhci_pci_ids[] = { {
  685. /* handle any USB UHCI controller */
  686. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x00), ~0),
  687. .driver_data = (unsigned long) &uhci_driver,
  688. }, { /* end: all zeroes */ }
  689. };
  690. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  691. static struct pci_driver uhci_pci_driver = {
  692. .name = (char *)hcd_name,
  693. .id_table = uhci_pci_ids,
  694. .probe = usb_hcd_pci_probe,
  695. .remove = usb_hcd_pci_remove,
  696. #ifdef CONFIG_PM
  697. .suspend = usb_hcd_pci_suspend,
  698. .resume = usb_hcd_pci_resume,
  699. #endif /* PM */
  700. };
  701. static int __init uhci_hcd_init(void)
  702. {
  703. int retval = -ENOMEM;
  704. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "\n");
  705. if (usb_disabled())
  706. return -ENODEV;
  707. if (debug) {
  708. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  709. if (!errbuf)
  710. goto errbuf_failed;
  711. }
  712. uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
  713. if (!uhci_debugfs_root)
  714. goto debug_failed;
  715. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  716. sizeof(struct urb_priv), 0, 0, NULL, NULL);
  717. if (!uhci_up_cachep)
  718. goto up_failed;
  719. retval = pci_register_driver(&uhci_pci_driver);
  720. if (retval)
  721. goto init_failed;
  722. return 0;
  723. init_failed:
  724. if (kmem_cache_destroy(uhci_up_cachep))
  725. warn("not all urb_priv's were freed!");
  726. up_failed:
  727. debugfs_remove(uhci_debugfs_root);
  728. debug_failed:
  729. if (errbuf)
  730. kfree(errbuf);
  731. errbuf_failed:
  732. return retval;
  733. }
  734. static void __exit uhci_hcd_cleanup(void)
  735. {
  736. pci_unregister_driver(&uhci_pci_driver);
  737. if (kmem_cache_destroy(uhci_up_cachep))
  738. warn("not all urb_priv's were freed!");
  739. debugfs_remove(uhci_debugfs_root);
  740. if (errbuf)
  741. kfree(errbuf);
  742. }
  743. module_init(uhci_hcd_init);
  744. module_exit(uhci_hcd_cleanup);
  745. MODULE_AUTHOR(DRIVER_AUTHOR);
  746. MODULE_DESCRIPTION(DRIVER_DESC);
  747. MODULE_LICENSE("GPL");