uhci-hcd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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-2007 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/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/unistd.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/pm.h>
  37. #include <linux/dmapool.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/usb.h>
  40. #include <linux/usb/hcd.h>
  41. #include <linux/bitops.h>
  42. #include <linux/dmi.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/io.h>
  45. #include <asm/irq.h>
  46. #include <asm/system.h>
  47. #include "uhci-hcd.h"
  48. /*
  49. * Version Information
  50. */
  51. #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
  52. Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
  53. Alan Stern"
  54. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  55. /* for flakey hardware, ignore overcurrent indicators */
  56. static int ignore_oc;
  57. module_param(ignore_oc, bool, S_IRUGO);
  58. MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
  59. /*
  60. * debug = 0, no debugging messages
  61. * debug = 1, dump failed URBs except for stalls
  62. * debug = 2, dump all failed URBs (including stalls)
  63. * show all queues in /sys/kernel/debug/uhci/[pci_addr]
  64. * debug = 3, show all TDs in URBs when dumping
  65. */
  66. #ifdef DEBUG
  67. #define DEBUG_CONFIGURED 1
  68. static int debug = 1;
  69. module_param(debug, int, S_IRUGO | S_IWUSR);
  70. MODULE_PARM_DESC(debug, "Debug level");
  71. #else
  72. #define DEBUG_CONFIGURED 0
  73. #define debug 0
  74. #endif
  75. static char *errbuf;
  76. #define ERRBUF_LEN (32 * 1024)
  77. static struct kmem_cache *uhci_up_cachep; /* urb_priv */
  78. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  79. static void wakeup_rh(struct uhci_hcd *uhci);
  80. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  81. /*
  82. * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
  83. */
  84. static __le32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
  85. {
  86. int skelnum;
  87. /*
  88. * The interrupt queues will be interleaved as evenly as possible.
  89. * There's not much to be done about period-1 interrupts; they have
  90. * to occur in every frame. But we can schedule period-2 interrupts
  91. * in odd-numbered frames, period-4 interrupts in frames congruent
  92. * to 2 (mod 4), and so on. This way each frame only has two
  93. * interrupt QHs, which will help spread out bandwidth utilization.
  94. *
  95. * ffs (Find First bit Set) does exactly what we need:
  96. * 1,3,5,... => ffs = 0 => use period-2 QH = skelqh[8],
  97. * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
  98. * ffs >= 7 => not on any high-period queue, so use
  99. * period-1 QH = skelqh[9].
  100. * Add in UHCI_NUMFRAMES to insure at least one bit is set.
  101. */
  102. skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
  103. if (skelnum <= 1)
  104. skelnum = 9;
  105. return LINK_TO_QH(uhci->skelqh[skelnum]);
  106. }
  107. #include "uhci-debug.c"
  108. #include "uhci-q.c"
  109. #include "uhci-hub.c"
  110. /*
  111. * Finish up a host controller reset and update the recorded state.
  112. */
  113. static void finish_reset(struct uhci_hcd *uhci)
  114. {
  115. int port;
  116. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  117. * bits in the port status and control registers.
  118. * We have to clear them by hand.
  119. */
  120. for (port = 0; port < uhci->rh_numports; ++port)
  121. outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
  122. uhci->port_c_suspend = uhci->resuming_ports = 0;
  123. uhci->rh_state = UHCI_RH_RESET;
  124. uhci->is_stopped = UHCI_IS_STOPPED;
  125. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  126. }
  127. /*
  128. * Last rites for a defunct/nonfunctional controller
  129. * or one we don't want to use any more.
  130. */
  131. static void uhci_hc_died(struct uhci_hcd *uhci)
  132. {
  133. uhci_get_current_frame_number(uhci);
  134. uhci->reset_hc(uhci);
  135. finish_reset(uhci);
  136. uhci->dead = 1;
  137. /* The current frame may already be partway finished */
  138. ++uhci->frame_number;
  139. }
  140. /*
  141. * Initialize a controller that was newly discovered or has lost power
  142. * or otherwise been reset while it was suspended. In none of these cases
  143. * can we be sure of its previous state.
  144. */
  145. static void check_and_reset_hc(struct uhci_hcd *uhci)
  146. {
  147. if (uhci->check_and_reset_hc(uhci))
  148. finish_reset(uhci);
  149. }
  150. /*
  151. * Store the basic register settings needed by the controller.
  152. */
  153. static void configure_hc(struct uhci_hcd *uhci)
  154. {
  155. /* Set the frame length to the default: 1 ms exactly */
  156. outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
  157. /* Store the frame list base address */
  158. outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
  159. /* Set the current frame number */
  160. outw(uhci->frame_number & UHCI_MAX_SOF_NUMBER,
  161. uhci->io_addr + USBFRNUM);
  162. /* perform any arch/bus specific configuration */
  163. if (uhci->configure_hc)
  164. uhci->configure_hc(uhci);
  165. }
  166. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  167. {
  168. /* If we have to ignore overcurrent events then almost by definition
  169. * we can't depend on resume-detect interrupts. */
  170. if (ignore_oc)
  171. return 1;
  172. return uhci->resume_detect_interrupts_are_broken ?
  173. uhci->resume_detect_interrupts_are_broken(uhci) : 0;
  174. }
  175. static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
  176. {
  177. return uhci->global_suspend_mode_is_broken ?
  178. uhci->global_suspend_mode_is_broken(uhci) : 0;
  179. }
  180. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  181. __releases(uhci->lock)
  182. __acquires(uhci->lock)
  183. {
  184. int auto_stop;
  185. int int_enable, egsm_enable, wakeup_enable;
  186. struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
  187. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  188. dev_dbg(&rhdev->dev, "%s%s\n", __func__,
  189. (auto_stop ? " (auto-stop)" : ""));
  190. /* Start off by assuming Resume-Detect interrupts and EGSM work
  191. * and that remote wakeups should be enabled.
  192. */
  193. egsm_enable = USBCMD_EGSM;
  194. uhci->RD_enable = 1;
  195. int_enable = USBINTR_RESUME;
  196. wakeup_enable = 1;
  197. /* In auto-stop mode wakeups must always be detected, but
  198. * Resume-Detect interrupts may be prohibited. (In the absence
  199. * of CONFIG_PM, they are always disallowed.)
  200. */
  201. if (auto_stop) {
  202. if (!device_may_wakeup(&rhdev->dev))
  203. int_enable = 0;
  204. /* In bus-suspend mode wakeups may be disabled, but if they are
  205. * allowed then so are Resume-Detect interrupts.
  206. */
  207. } else {
  208. #ifdef CONFIG_PM
  209. if (!rhdev->do_remote_wakeup)
  210. wakeup_enable = 0;
  211. #endif
  212. }
  213. /* EGSM causes the root hub to echo a 'K' signal (resume) out any
  214. * port which requests a remote wakeup. According to the USB spec,
  215. * every hub is supposed to do this. But if we are ignoring
  216. * remote-wakeup requests anyway then there's no point to it.
  217. * We also shouldn't enable EGSM if it's broken.
  218. */
  219. if (!wakeup_enable || global_suspend_mode_is_broken(uhci))
  220. egsm_enable = 0;
  221. /* If we're ignoring wakeup events then there's no reason to
  222. * enable Resume-Detect interrupts. We also shouldn't enable
  223. * them if they are broken or disallowed.
  224. *
  225. * This logic may lead us to enabling RD but not EGSM. The UHCI
  226. * spec foolishly says that RD works only when EGSM is on, but
  227. * there's no harm in enabling it anyway -- perhaps some chips
  228. * will implement it!
  229. */
  230. if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) ||
  231. !int_enable)
  232. uhci->RD_enable = int_enable = 0;
  233. outw(int_enable, uhci->io_addr + USBINTR);
  234. outw(egsm_enable | USBCMD_CF, uhci->io_addr + USBCMD);
  235. mb();
  236. udelay(5);
  237. /* If we're auto-stopping then no devices have been attached
  238. * for a while, so there shouldn't be any active URBs and the
  239. * controller should stop after a few microseconds. Otherwise
  240. * we will give the controller one frame to stop.
  241. */
  242. if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
  243. uhci->rh_state = UHCI_RH_SUSPENDING;
  244. spin_unlock_irq(&uhci->lock);
  245. msleep(1);
  246. spin_lock_irq(&uhci->lock);
  247. if (uhci->dead)
  248. return;
  249. }
  250. if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
  251. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  252. uhci_get_current_frame_number(uhci);
  253. uhci->rh_state = new_state;
  254. uhci->is_stopped = UHCI_IS_STOPPED;
  255. /* If interrupts don't work and remote wakeup is enabled then
  256. * the suspended root hub needs to be polled.
  257. */
  258. if (!int_enable && wakeup_enable)
  259. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  260. else
  261. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  262. uhci_scan_schedule(uhci);
  263. uhci_fsbr_off(uhci);
  264. }
  265. static void start_rh(struct uhci_hcd *uhci)
  266. {
  267. uhci->is_stopped = 0;
  268. /* Mark it configured and running with a 64-byte max packet.
  269. * All interrupts are enabled, even though RESUME won't do anything.
  270. */
  271. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
  272. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  273. uhci->io_addr + USBINTR);
  274. mb();
  275. uhci->rh_state = UHCI_RH_RUNNING;
  276. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  277. }
  278. static void wakeup_rh(struct uhci_hcd *uhci)
  279. __releases(uhci->lock)
  280. __acquires(uhci->lock)
  281. {
  282. dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
  283. "%s%s\n", __func__,
  284. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  285. " (auto-start)" : "");
  286. /* If we are auto-stopped then no devices are attached so there's
  287. * no need for wakeup signals. Otherwise we send Global Resume
  288. * for 20 ms.
  289. */
  290. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  291. unsigned egsm;
  292. /* Keep EGSM on if it was set before */
  293. egsm = inw(uhci->io_addr + USBCMD) & USBCMD_EGSM;
  294. uhci->rh_state = UHCI_RH_RESUMING;
  295. outw(USBCMD_FGR | USBCMD_CF | egsm, uhci->io_addr + USBCMD);
  296. spin_unlock_irq(&uhci->lock);
  297. msleep(20);
  298. spin_lock_irq(&uhci->lock);
  299. if (uhci->dead)
  300. return;
  301. /* End Global Resume and wait for EOP to be sent */
  302. outw(USBCMD_CF, uhci->io_addr + USBCMD);
  303. mb();
  304. udelay(4);
  305. if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
  306. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  307. }
  308. start_rh(uhci);
  309. /* Restart root hub polling */
  310. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  311. }
  312. static irqreturn_t uhci_irq(struct usb_hcd *hcd)
  313. {
  314. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  315. unsigned short status;
  316. /*
  317. * Read the interrupt status, and write it back to clear the
  318. * interrupt cause. Contrary to the UHCI specification, the
  319. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  320. */
  321. status = inw(uhci->io_addr + USBSTS);
  322. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  323. return IRQ_NONE;
  324. outw(status, uhci->io_addr + USBSTS); /* Clear it */
  325. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  326. if (status & USBSTS_HSE)
  327. dev_err(uhci_dev(uhci), "host system error, "
  328. "PCI problems?\n");
  329. if (status & USBSTS_HCPE)
  330. dev_err(uhci_dev(uhci), "host controller process "
  331. "error, something bad happened!\n");
  332. if (status & USBSTS_HCH) {
  333. spin_lock(&uhci->lock);
  334. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  335. dev_err(uhci_dev(uhci),
  336. "host controller halted, "
  337. "very bad!\n");
  338. if (debug > 1 && errbuf) {
  339. /* Print the schedule for debugging */
  340. uhci_sprint_schedule(uhci,
  341. errbuf, ERRBUF_LEN);
  342. lprintk(errbuf);
  343. }
  344. uhci_hc_died(uhci);
  345. usb_hc_died(hcd);
  346. /* Force a callback in case there are
  347. * pending unlinks */
  348. mod_timer(&hcd->rh_timer, jiffies);
  349. }
  350. spin_unlock(&uhci->lock);
  351. }
  352. }
  353. if (status & USBSTS_RD)
  354. usb_hcd_poll_rh_status(hcd);
  355. else {
  356. spin_lock(&uhci->lock);
  357. uhci_scan_schedule(uhci);
  358. spin_unlock(&uhci->lock);
  359. }
  360. return IRQ_HANDLED;
  361. }
  362. /*
  363. * Store the current frame number in uhci->frame_number if the controller
  364. * is running. Expand from 11 bits (of which we use only 10) to a
  365. * full-sized integer.
  366. *
  367. * Like many other parts of the driver, this code relies on being polled
  368. * more than once per second as long as the controller is running.
  369. */
  370. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  371. {
  372. if (!uhci->is_stopped) {
  373. unsigned delta;
  374. delta = (inw(uhci->io_addr + USBFRNUM) - uhci->frame_number) &
  375. (UHCI_NUMFRAMES - 1);
  376. uhci->frame_number += delta;
  377. }
  378. }
  379. /*
  380. * De-allocate all resources
  381. */
  382. static void release_uhci(struct uhci_hcd *uhci)
  383. {
  384. int i;
  385. if (DEBUG_CONFIGURED) {
  386. spin_lock_irq(&uhci->lock);
  387. uhci->is_initialized = 0;
  388. spin_unlock_irq(&uhci->lock);
  389. debugfs_remove(uhci->dentry);
  390. }
  391. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  392. uhci_free_qh(uhci, uhci->skelqh[i]);
  393. uhci_free_td(uhci, uhci->term_td);
  394. dma_pool_destroy(uhci->qh_pool);
  395. dma_pool_destroy(uhci->td_pool);
  396. kfree(uhci->frame_cpu);
  397. dma_free_coherent(uhci_dev(uhci),
  398. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  399. uhci->frame, uhci->frame_dma_handle);
  400. }
  401. /*
  402. * Allocate a frame list, and then setup the skeleton
  403. *
  404. * The hardware doesn't really know any difference
  405. * in the queues, but the order does matter for the
  406. * protocols higher up. The order in which the queues
  407. * are encountered by the hardware is:
  408. *
  409. * - All isochronous events are handled before any
  410. * of the queues. We don't do that here, because
  411. * we'll create the actual TD entries on demand.
  412. * - The first queue is the high-period interrupt queue.
  413. * - The second queue is the period-1 interrupt and async
  414. * (low-speed control, full-speed control, then bulk) queue.
  415. * - The third queue is the terminating bandwidth reclamation queue,
  416. * which contains no members, loops back to itself, and is present
  417. * only when FSBR is on and there are no full-speed control or bulk QHs.
  418. */
  419. static int uhci_start(struct usb_hcd *hcd)
  420. {
  421. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  422. int retval = -EBUSY;
  423. int i;
  424. struct dentry __maybe_unused *dentry;
  425. hcd->uses_new_polling = 1;
  426. spin_lock_init(&uhci->lock);
  427. setup_timer(&uhci->fsbr_timer, uhci_fsbr_timeout,
  428. (unsigned long) uhci);
  429. INIT_LIST_HEAD(&uhci->idle_qh_list);
  430. init_waitqueue_head(&uhci->waitqh);
  431. #ifdef UHCI_DEBUG_OPS
  432. dentry = debugfs_create_file(hcd->self.bus_name,
  433. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
  434. uhci, &uhci_debug_operations);
  435. if (!dentry) {
  436. dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
  437. return -ENOMEM;
  438. }
  439. uhci->dentry = dentry;
  440. #endif
  441. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  442. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  443. &uhci->frame_dma_handle, 0);
  444. if (!uhci->frame) {
  445. dev_err(uhci_dev(uhci), "unable to allocate "
  446. "consistent memory for frame list\n");
  447. goto err_alloc_frame;
  448. }
  449. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  450. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  451. GFP_KERNEL);
  452. if (!uhci->frame_cpu) {
  453. dev_err(uhci_dev(uhci), "unable to allocate "
  454. "memory for frame pointers\n");
  455. goto err_alloc_frame_cpu;
  456. }
  457. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  458. sizeof(struct uhci_td), 16, 0);
  459. if (!uhci->td_pool) {
  460. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  461. goto err_create_td_pool;
  462. }
  463. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  464. sizeof(struct uhci_qh), 16, 0);
  465. if (!uhci->qh_pool) {
  466. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  467. goto err_create_qh_pool;
  468. }
  469. uhci->term_td = uhci_alloc_td(uhci);
  470. if (!uhci->term_td) {
  471. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  472. goto err_alloc_term_td;
  473. }
  474. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  475. uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
  476. if (!uhci->skelqh[i]) {
  477. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  478. goto err_alloc_skelqh;
  479. }
  480. }
  481. /*
  482. * 8 Interrupt queues; link all higher int queues to int1 = async
  483. */
  484. for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
  485. uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
  486. uhci->skel_async_qh->link = UHCI_PTR_TERM;
  487. uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
  488. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  489. uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
  490. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  491. uhci->term_td->link = UHCI_PTR_TERM;
  492. uhci->skel_async_qh->element = uhci->skel_term_qh->element =
  493. LINK_TO_TD(uhci->term_td);
  494. /*
  495. * Fill the frame list: make all entries point to the proper
  496. * interrupt queue.
  497. */
  498. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  499. /* Only place we don't use the frame list routines */
  500. uhci->frame[i] = uhci_frame_skel_link(uhci, i);
  501. }
  502. /*
  503. * Some architectures require a full mb() to enforce completion of
  504. * the memory writes above before the I/O transfers in configure_hc().
  505. */
  506. mb();
  507. configure_hc(uhci);
  508. uhci->is_initialized = 1;
  509. spin_lock_irq(&uhci->lock);
  510. start_rh(uhci);
  511. spin_unlock_irq(&uhci->lock);
  512. return 0;
  513. /*
  514. * error exits:
  515. */
  516. err_alloc_skelqh:
  517. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  518. if (uhci->skelqh[i])
  519. uhci_free_qh(uhci, uhci->skelqh[i]);
  520. }
  521. uhci_free_td(uhci, uhci->term_td);
  522. err_alloc_term_td:
  523. dma_pool_destroy(uhci->qh_pool);
  524. err_create_qh_pool:
  525. dma_pool_destroy(uhci->td_pool);
  526. err_create_td_pool:
  527. kfree(uhci->frame_cpu);
  528. err_alloc_frame_cpu:
  529. dma_free_coherent(uhci_dev(uhci),
  530. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  531. uhci->frame, uhci->frame_dma_handle);
  532. err_alloc_frame:
  533. debugfs_remove(uhci->dentry);
  534. return retval;
  535. }
  536. static void uhci_stop(struct usb_hcd *hcd)
  537. {
  538. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  539. spin_lock_irq(&uhci->lock);
  540. if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
  541. uhci_hc_died(uhci);
  542. uhci_scan_schedule(uhci);
  543. spin_unlock_irq(&uhci->lock);
  544. synchronize_irq(hcd->irq);
  545. del_timer_sync(&uhci->fsbr_timer);
  546. release_uhci(uhci);
  547. }
  548. #ifdef CONFIG_PM
  549. static int uhci_rh_suspend(struct usb_hcd *hcd)
  550. {
  551. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  552. int rc = 0;
  553. spin_lock_irq(&uhci->lock);
  554. if (!HCD_HW_ACCESSIBLE(hcd))
  555. rc = -ESHUTDOWN;
  556. else if (uhci->dead)
  557. ; /* Dead controllers tell no tales */
  558. /* Once the controller is stopped, port resumes that are already
  559. * in progress won't complete. Hence if remote wakeup is enabled
  560. * for the root hub and any ports are in the middle of a resume or
  561. * remote wakeup, we must fail the suspend.
  562. */
  563. else if (hcd->self.root_hub->do_remote_wakeup &&
  564. uhci->resuming_ports) {
  565. dev_dbg(uhci_dev(uhci), "suspend failed because a port "
  566. "is resuming\n");
  567. rc = -EBUSY;
  568. } else
  569. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  570. spin_unlock_irq(&uhci->lock);
  571. return rc;
  572. }
  573. static int uhci_rh_resume(struct usb_hcd *hcd)
  574. {
  575. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  576. int rc = 0;
  577. spin_lock_irq(&uhci->lock);
  578. if (!HCD_HW_ACCESSIBLE(hcd))
  579. rc = -ESHUTDOWN;
  580. else if (!uhci->dead)
  581. wakeup_rh(uhci);
  582. spin_unlock_irq(&uhci->lock);
  583. return rc;
  584. }
  585. #endif
  586. /* Wait until a particular device/endpoint's QH is idle, and free it */
  587. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  588. struct usb_host_endpoint *hep)
  589. {
  590. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  591. struct uhci_qh *qh;
  592. spin_lock_irq(&uhci->lock);
  593. qh = (struct uhci_qh *) hep->hcpriv;
  594. if (qh == NULL)
  595. goto done;
  596. while (qh->state != QH_STATE_IDLE) {
  597. ++uhci->num_waiting;
  598. spin_unlock_irq(&uhci->lock);
  599. wait_event_interruptible(uhci->waitqh,
  600. qh->state == QH_STATE_IDLE);
  601. spin_lock_irq(&uhci->lock);
  602. --uhci->num_waiting;
  603. }
  604. uhci_free_qh(uhci, qh);
  605. done:
  606. spin_unlock_irq(&uhci->lock);
  607. }
  608. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  609. {
  610. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  611. unsigned frame_number;
  612. unsigned delta;
  613. /* Minimize latency by avoiding the spinlock */
  614. frame_number = uhci->frame_number;
  615. barrier();
  616. delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
  617. (UHCI_NUMFRAMES - 1);
  618. return frame_number + delta;
  619. }
  620. /* Determines number of ports on controller */
  621. static int uhci_count_ports(struct usb_hcd *hcd)
  622. {
  623. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  624. unsigned io_size = (unsigned) hcd->rsrc_len;
  625. int port;
  626. /* The UHCI spec says devices must have 2 ports, and goes on to say
  627. * they may have more but gives no way to determine how many there
  628. * are. However according to the UHCI spec, Bit 7 of the port
  629. * status and control register is always set to 1. So we try to
  630. * use this to our advantage. Another common failure mode when
  631. * a nonexistent register is addressed is to return all ones, so
  632. * we test for that also.
  633. */
  634. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  635. unsigned int portstatus;
  636. portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
  637. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  638. break;
  639. }
  640. if (debug)
  641. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  642. /* Anything greater than 7 is weird so we'll ignore it. */
  643. if (port > UHCI_RH_MAXCHILD) {
  644. dev_info(uhci_dev(uhci), "port count misdetected? "
  645. "forcing to 2 ports\n");
  646. port = 2;
  647. }
  648. return port;
  649. }
  650. static const char hcd_name[] = "uhci_hcd";
  651. #include "uhci-pci.c"
  652. static int __init uhci_hcd_init(void)
  653. {
  654. int retval = -ENOMEM;
  655. if (usb_disabled())
  656. return -ENODEV;
  657. printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
  658. ignore_oc ? ", overcurrent ignored" : "");
  659. set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  660. if (DEBUG_CONFIGURED) {
  661. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  662. if (!errbuf)
  663. goto errbuf_failed;
  664. uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
  665. if (!uhci_debugfs_root)
  666. goto debug_failed;
  667. }
  668. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  669. sizeof(struct urb_priv), 0, 0, NULL);
  670. if (!uhci_up_cachep)
  671. goto up_failed;
  672. retval = pci_register_driver(&uhci_pci_driver);
  673. if (retval)
  674. goto init_failed;
  675. return 0;
  676. init_failed:
  677. kmem_cache_destroy(uhci_up_cachep);
  678. up_failed:
  679. debugfs_remove(uhci_debugfs_root);
  680. debug_failed:
  681. kfree(errbuf);
  682. errbuf_failed:
  683. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  684. return retval;
  685. }
  686. static void __exit uhci_hcd_cleanup(void)
  687. {
  688. pci_unregister_driver(&uhci_pci_driver);
  689. kmem_cache_destroy(uhci_up_cachep);
  690. debugfs_remove(uhci_debugfs_root);
  691. kfree(errbuf);
  692. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  693. }
  694. module_init(uhci_hcd_init);
  695. module_exit(uhci_hcd_cleanup);
  696. MODULE_AUTHOR(DRIVER_AUTHOR);
  697. MODULE_DESCRIPTION(DRIVER_DESC);
  698. MODULE_LICENSE("GPL");