uhci-hcd.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. #include "pci-quirks.h"
  49. /*
  50. * Version Information
  51. */
  52. #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
  53. Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
  54. Alan Stern"
  55. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  56. /* for flakey hardware, ignore overcurrent indicators */
  57. static int ignore_oc;
  58. module_param(ignore_oc, bool, S_IRUGO);
  59. MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
  60. /*
  61. * debug = 0, no debugging messages
  62. * debug = 1, dump failed URBs except for stalls
  63. * debug = 2, dump all failed URBs (including stalls)
  64. * show all queues in /sys/kernel/debug/uhci/[pci_addr]
  65. * debug = 3, show all TDs in URBs when dumping
  66. */
  67. #ifdef DEBUG
  68. #define DEBUG_CONFIGURED 1
  69. static int debug = 1;
  70. module_param(debug, int, S_IRUGO | S_IWUSR);
  71. MODULE_PARM_DESC(debug, "Debug level");
  72. #else
  73. #define DEBUG_CONFIGURED 0
  74. #define debug 0
  75. #endif
  76. static char *errbuf;
  77. #define ERRBUF_LEN (32 * 1024)
  78. static struct kmem_cache *uhci_up_cachep; /* urb_priv */
  79. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  80. static void wakeup_rh(struct uhci_hcd *uhci);
  81. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  82. /*
  83. * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
  84. */
  85. static __le32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
  86. {
  87. int skelnum;
  88. /*
  89. * The interrupt queues will be interleaved as evenly as possible.
  90. * There's not much to be done about period-1 interrupts; they have
  91. * to occur in every frame. But we can schedule period-2 interrupts
  92. * in odd-numbered frames, period-4 interrupts in frames congruent
  93. * to 2 (mod 4), and so on. This way each frame only has two
  94. * interrupt QHs, which will help spread out bandwidth utilization.
  95. *
  96. * ffs (Find First bit Set) does exactly what we need:
  97. * 1,3,5,... => ffs = 0 => use period-2 QH = skelqh[8],
  98. * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
  99. * ffs >= 7 => not on any high-period queue, so use
  100. * period-1 QH = skelqh[9].
  101. * Add in UHCI_NUMFRAMES to insure at least one bit is set.
  102. */
  103. skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
  104. if (skelnum <= 1)
  105. skelnum = 9;
  106. return LINK_TO_QH(uhci->skelqh[skelnum]);
  107. }
  108. #include "uhci-debug.c"
  109. #include "uhci-q.c"
  110. #include "uhci-hub.c"
  111. /*
  112. * Finish up a host controller reset and update the recorded state.
  113. */
  114. static void finish_reset(struct uhci_hcd *uhci)
  115. {
  116. int port;
  117. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  118. * bits in the port status and control registers.
  119. * We have to clear them by hand.
  120. */
  121. for (port = 0; port < uhci->rh_numports; ++port)
  122. outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
  123. uhci->port_c_suspend = uhci->resuming_ports = 0;
  124. uhci->rh_state = UHCI_RH_RESET;
  125. uhci->is_stopped = UHCI_IS_STOPPED;
  126. uhci_to_hcd(uhci)->state = HC_STATE_HALT;
  127. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  128. uhci->dead = 0; /* Full reset resurrects the controller */
  129. }
  130. /*
  131. * Last rites for a defunct/nonfunctional controller
  132. * or one we don't want to use any more.
  133. */
  134. static void uhci_hc_died(struct uhci_hcd *uhci)
  135. {
  136. uhci_get_current_frame_number(uhci);
  137. uhci_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr);
  138. finish_reset(uhci);
  139. uhci->dead = 1;
  140. /* The current frame may already be partway finished */
  141. ++uhci->frame_number;
  142. }
  143. /*
  144. * Initialize a controller that was newly discovered or has lost power
  145. * or otherwise been reset while it was suspended. In none of these cases
  146. * can we be sure of its previous state.
  147. */
  148. static void check_and_reset_hc(struct uhci_hcd *uhci)
  149. {
  150. if (uhci_check_and_reset_hc(to_pci_dev(uhci_dev(uhci)), uhci->io_addr))
  151. finish_reset(uhci);
  152. }
  153. /*
  154. * Store the basic register settings needed by the controller.
  155. */
  156. static void configure_hc(struct uhci_hcd *uhci)
  157. {
  158. struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
  159. /* Set the frame length to the default: 1 ms exactly */
  160. outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
  161. /* Store the frame list base address */
  162. outl(uhci->frame_dma_handle, uhci->io_addr + USBFLBASEADD);
  163. /* Set the current frame number */
  164. outw(uhci->frame_number & UHCI_MAX_SOF_NUMBER,
  165. uhci->io_addr + USBFRNUM);
  166. /* Mark controller as not halted before we enable interrupts */
  167. uhci_to_hcd(uhci)->state = HC_STATE_SUSPENDED;
  168. mb();
  169. /* Enable PIRQ */
  170. pci_write_config_word(pdev, USBLEGSUP, USBLEGSUP_DEFAULT);
  171. /* Disable platform-specific non-PME# wakeup */
  172. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  173. pci_write_config_byte(pdev, USBRES_INTEL, 0);
  174. }
  175. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  176. {
  177. int port;
  178. /* If we have to ignore overcurrent events then almost by definition
  179. * we can't depend on resume-detect interrupts. */
  180. if (ignore_oc)
  181. return 1;
  182. switch (to_pci_dev(uhci_dev(uhci))->vendor) {
  183. default:
  184. break;
  185. case PCI_VENDOR_ID_GENESYS:
  186. /* Genesys Logic's GL880S controllers don't generate
  187. * resume-detect interrupts.
  188. */
  189. return 1;
  190. case PCI_VENDOR_ID_INTEL:
  191. /* Some of Intel's USB controllers have a bug that causes
  192. * resume-detect interrupts if any port has an over-current
  193. * condition. To make matters worse, some motherboards
  194. * hardwire unused USB ports' over-current inputs active!
  195. * To prevent problems, we will not enable resume-detect
  196. * interrupts if any ports are OC.
  197. */
  198. for (port = 0; port < uhci->rh_numports; ++port) {
  199. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  200. USBPORTSC_OC)
  201. return 1;
  202. }
  203. break;
  204. }
  205. return 0;
  206. }
  207. static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
  208. {
  209. int port;
  210. const char *sys_info;
  211. static char bad_Asus_board[] = "A7V8X";
  212. /* One of Asus's motherboards has a bug which causes it to
  213. * wake up immediately from suspend-to-RAM if any of the ports
  214. * are connected. In such cases we will not set EGSM.
  215. */
  216. sys_info = dmi_get_system_info(DMI_BOARD_NAME);
  217. if (sys_info && !strcmp(sys_info, bad_Asus_board)) {
  218. for (port = 0; port < uhci->rh_numports; ++port) {
  219. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  220. USBPORTSC_CCS)
  221. return 1;
  222. }
  223. }
  224. return 0;
  225. }
  226. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  227. __releases(uhci->lock)
  228. __acquires(uhci->lock)
  229. {
  230. int auto_stop;
  231. int int_enable, egsm_enable, wakeup_enable;
  232. struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
  233. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  234. dev_dbg(&rhdev->dev, "%s%s\n", __func__,
  235. (auto_stop ? " (auto-stop)" : ""));
  236. /* Start off by assuming Resume-Detect interrupts and EGSM work
  237. * and that remote wakeups should be enabled.
  238. */
  239. egsm_enable = USBCMD_EGSM;
  240. uhci->RD_enable = 1;
  241. int_enable = USBINTR_RESUME;
  242. wakeup_enable = 1;
  243. /* In auto-stop mode wakeups must always be detected, but
  244. * Resume-Detect interrupts may be prohibited. (In the absence
  245. * of CONFIG_PM, they are always disallowed.)
  246. */
  247. if (auto_stop) {
  248. if (!device_may_wakeup(&rhdev->dev))
  249. int_enable = 0;
  250. /* In bus-suspend mode wakeups may be disabled, but if they are
  251. * allowed then so are Resume-Detect interrupts.
  252. */
  253. } else {
  254. #ifdef CONFIG_PM
  255. if (!rhdev->do_remote_wakeup)
  256. wakeup_enable = 0;
  257. #endif
  258. }
  259. /* EGSM causes the root hub to echo a 'K' signal (resume) out any
  260. * port which requests a remote wakeup. According to the USB spec,
  261. * every hub is supposed to do this. But if we are ignoring
  262. * remote-wakeup requests anyway then there's no point to it.
  263. * We also shouldn't enable EGSM if it's broken.
  264. */
  265. if (!wakeup_enable || global_suspend_mode_is_broken(uhci))
  266. egsm_enable = 0;
  267. /* If we're ignoring wakeup events then there's no reason to
  268. * enable Resume-Detect interrupts. We also shouldn't enable
  269. * them if they are broken or disallowed.
  270. *
  271. * This logic may lead us to enabling RD but not EGSM. The UHCI
  272. * spec foolishly says that RD works only when EGSM is on, but
  273. * there's no harm in enabling it anyway -- perhaps some chips
  274. * will implement it!
  275. */
  276. if (!wakeup_enable || resume_detect_interrupts_are_broken(uhci) ||
  277. !int_enable)
  278. uhci->RD_enable = int_enable = 0;
  279. outw(int_enable, uhci->io_addr + USBINTR);
  280. outw(egsm_enable | USBCMD_CF, uhci->io_addr + USBCMD);
  281. mb();
  282. udelay(5);
  283. /* If we're auto-stopping then no devices have been attached
  284. * for a while, so there shouldn't be any active URBs and the
  285. * controller should stop after a few microseconds. Otherwise
  286. * we will give the controller one frame to stop.
  287. */
  288. if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
  289. uhci->rh_state = UHCI_RH_SUSPENDING;
  290. spin_unlock_irq(&uhci->lock);
  291. msleep(1);
  292. spin_lock_irq(&uhci->lock);
  293. if (uhci->dead)
  294. return;
  295. }
  296. if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
  297. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  298. uhci_get_current_frame_number(uhci);
  299. uhci->rh_state = new_state;
  300. uhci->is_stopped = UHCI_IS_STOPPED;
  301. /* If interrupts don't work and remote wakeup is enabled then
  302. * the suspended root hub needs to be polled.
  303. */
  304. if (!int_enable && wakeup_enable)
  305. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  306. else
  307. clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  308. uhci_scan_schedule(uhci);
  309. uhci_fsbr_off(uhci);
  310. }
  311. static void start_rh(struct uhci_hcd *uhci)
  312. {
  313. uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
  314. uhci->is_stopped = 0;
  315. /* Mark it configured and running with a 64-byte max packet.
  316. * All interrupts are enabled, even though RESUME won't do anything.
  317. */
  318. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
  319. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  320. uhci->io_addr + USBINTR);
  321. mb();
  322. uhci->rh_state = UHCI_RH_RUNNING;
  323. set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
  324. }
  325. static void wakeup_rh(struct uhci_hcd *uhci)
  326. __releases(uhci->lock)
  327. __acquires(uhci->lock)
  328. {
  329. dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
  330. "%s%s\n", __func__,
  331. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  332. " (auto-start)" : "");
  333. /* If we are auto-stopped then no devices are attached so there's
  334. * no need for wakeup signals. Otherwise we send Global Resume
  335. * for 20 ms.
  336. */
  337. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  338. unsigned egsm;
  339. /* Keep EGSM on if it was set before */
  340. egsm = inw(uhci->io_addr + USBCMD) & USBCMD_EGSM;
  341. uhci->rh_state = UHCI_RH_RESUMING;
  342. outw(USBCMD_FGR | USBCMD_CF | egsm, uhci->io_addr + USBCMD);
  343. spin_unlock_irq(&uhci->lock);
  344. msleep(20);
  345. spin_lock_irq(&uhci->lock);
  346. if (uhci->dead)
  347. return;
  348. /* End Global Resume and wait for EOP to be sent */
  349. outw(USBCMD_CF, uhci->io_addr + USBCMD);
  350. mb();
  351. udelay(4);
  352. if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
  353. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  354. }
  355. start_rh(uhci);
  356. /* Restart root hub polling */
  357. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  358. }
  359. static irqreturn_t uhci_irq(struct usb_hcd *hcd)
  360. {
  361. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  362. unsigned short status;
  363. /*
  364. * Read the interrupt status, and write it back to clear the
  365. * interrupt cause. Contrary to the UHCI specification, the
  366. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  367. */
  368. status = inw(uhci->io_addr + USBSTS);
  369. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  370. return IRQ_NONE;
  371. outw(status, uhci->io_addr + USBSTS); /* Clear it */
  372. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  373. if (status & USBSTS_HSE)
  374. dev_err(uhci_dev(uhci), "host system error, "
  375. "PCI problems?\n");
  376. if (status & USBSTS_HCPE)
  377. dev_err(uhci_dev(uhci), "host controller process "
  378. "error, something bad happened!\n");
  379. if (status & USBSTS_HCH) {
  380. spin_lock(&uhci->lock);
  381. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  382. dev_err(uhci_dev(uhci),
  383. "host controller halted, "
  384. "very bad!\n");
  385. if (debug > 1 && errbuf) {
  386. /* Print the schedule for debugging */
  387. uhci_sprint_schedule(uhci,
  388. errbuf, ERRBUF_LEN);
  389. lprintk(errbuf);
  390. }
  391. uhci_hc_died(uhci);
  392. /* Force a callback in case there are
  393. * pending unlinks */
  394. mod_timer(&hcd->rh_timer, jiffies);
  395. }
  396. spin_unlock(&uhci->lock);
  397. }
  398. }
  399. if (status & USBSTS_RD)
  400. usb_hcd_poll_rh_status(hcd);
  401. else {
  402. spin_lock(&uhci->lock);
  403. uhci_scan_schedule(uhci);
  404. spin_unlock(&uhci->lock);
  405. }
  406. return IRQ_HANDLED;
  407. }
  408. /*
  409. * Store the current frame number in uhci->frame_number if the controller
  410. * is runnning. Expand from 11 bits (of which we use only 10) to a
  411. * full-sized integer.
  412. *
  413. * Like many other parts of the driver, this code relies on being polled
  414. * more than once per second as long as the controller is running.
  415. */
  416. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  417. {
  418. if (!uhci->is_stopped) {
  419. unsigned delta;
  420. delta = (inw(uhci->io_addr + USBFRNUM) - uhci->frame_number) &
  421. (UHCI_NUMFRAMES - 1);
  422. uhci->frame_number += delta;
  423. }
  424. }
  425. /*
  426. * De-allocate all resources
  427. */
  428. static void release_uhci(struct uhci_hcd *uhci)
  429. {
  430. int i;
  431. if (DEBUG_CONFIGURED) {
  432. spin_lock_irq(&uhci->lock);
  433. uhci->is_initialized = 0;
  434. spin_unlock_irq(&uhci->lock);
  435. debugfs_remove(uhci->dentry);
  436. }
  437. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  438. uhci_free_qh(uhci, uhci->skelqh[i]);
  439. uhci_free_td(uhci, uhci->term_td);
  440. dma_pool_destroy(uhci->qh_pool);
  441. dma_pool_destroy(uhci->td_pool);
  442. kfree(uhci->frame_cpu);
  443. dma_free_coherent(uhci_dev(uhci),
  444. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  445. uhci->frame, uhci->frame_dma_handle);
  446. }
  447. static int uhci_init(struct usb_hcd *hcd)
  448. {
  449. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  450. unsigned io_size = (unsigned) hcd->rsrc_len;
  451. int port;
  452. uhci->io_addr = (unsigned long) hcd->rsrc_start;
  453. /* The UHCI spec says devices must have 2 ports, and goes on to say
  454. * they may have more but gives no way to determine how many there
  455. * are. However according to the UHCI spec, Bit 7 of the port
  456. * status and control register is always set to 1. So we try to
  457. * use this to our advantage. Another common failure mode when
  458. * a nonexistent register is addressed is to return all ones, so
  459. * we test for that also.
  460. */
  461. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  462. unsigned int portstatus;
  463. portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
  464. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  465. break;
  466. }
  467. if (debug)
  468. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  469. /* Anything greater than 7 is weird so we'll ignore it. */
  470. if (port > UHCI_RH_MAXCHILD) {
  471. dev_info(uhci_dev(uhci), "port count misdetected? "
  472. "forcing to 2 ports\n");
  473. port = 2;
  474. }
  475. uhci->rh_numports = port;
  476. /* Kick BIOS off this hardware and reset if the controller
  477. * isn't already safely quiescent.
  478. */
  479. check_and_reset_hc(uhci);
  480. return 0;
  481. }
  482. /* Make sure the controller is quiescent and that we're not using it
  483. * any more. This is mainly for the benefit of programs which, like kexec,
  484. * expect the hardware to be idle: not doing DMA or generating IRQs.
  485. *
  486. * This routine may be called in a damaged or failing kernel. Hence we
  487. * do not acquire the spinlock before shutting down the controller.
  488. */
  489. static void uhci_shutdown(struct pci_dev *pdev)
  490. {
  491. struct usb_hcd *hcd = pci_get_drvdata(pdev);
  492. uhci_hc_died(hcd_to_uhci(hcd));
  493. }
  494. /*
  495. * Allocate a frame list, and then setup the skeleton
  496. *
  497. * The hardware doesn't really know any difference
  498. * in the queues, but the order does matter for the
  499. * protocols higher up. The order in which the queues
  500. * are encountered by the hardware is:
  501. *
  502. * - All isochronous events are handled before any
  503. * of the queues. We don't do that here, because
  504. * we'll create the actual TD entries on demand.
  505. * - The first queue is the high-period interrupt queue.
  506. * - The second queue is the period-1 interrupt and async
  507. * (low-speed control, full-speed control, then bulk) queue.
  508. * - The third queue is the terminating bandwidth reclamation queue,
  509. * which contains no members, loops back to itself, and is present
  510. * only when FSBR is on and there are no full-speed control or bulk QHs.
  511. */
  512. static int uhci_start(struct usb_hcd *hcd)
  513. {
  514. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  515. int retval = -EBUSY;
  516. int i;
  517. struct dentry __maybe_unused *dentry;
  518. hcd->uses_new_polling = 1;
  519. spin_lock_init(&uhci->lock);
  520. setup_timer(&uhci->fsbr_timer, uhci_fsbr_timeout,
  521. (unsigned long) uhci);
  522. INIT_LIST_HEAD(&uhci->idle_qh_list);
  523. init_waitqueue_head(&uhci->waitqh);
  524. #ifdef UHCI_DEBUG_OPS
  525. dentry = debugfs_create_file(hcd->self.bus_name,
  526. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
  527. uhci, &uhci_debug_operations);
  528. if (!dentry) {
  529. dev_err(uhci_dev(uhci), "couldn't create uhci debugfs entry\n");
  530. return -ENOMEM;
  531. }
  532. uhci->dentry = dentry;
  533. #endif
  534. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  535. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  536. &uhci->frame_dma_handle, 0);
  537. if (!uhci->frame) {
  538. dev_err(uhci_dev(uhci), "unable to allocate "
  539. "consistent memory for frame list\n");
  540. goto err_alloc_frame;
  541. }
  542. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  543. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  544. GFP_KERNEL);
  545. if (!uhci->frame_cpu) {
  546. dev_err(uhci_dev(uhci), "unable to allocate "
  547. "memory for frame pointers\n");
  548. goto err_alloc_frame_cpu;
  549. }
  550. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  551. sizeof(struct uhci_td), 16, 0);
  552. if (!uhci->td_pool) {
  553. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  554. goto err_create_td_pool;
  555. }
  556. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  557. sizeof(struct uhci_qh), 16, 0);
  558. if (!uhci->qh_pool) {
  559. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  560. goto err_create_qh_pool;
  561. }
  562. uhci->term_td = uhci_alloc_td(uhci);
  563. if (!uhci->term_td) {
  564. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  565. goto err_alloc_term_td;
  566. }
  567. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  568. uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
  569. if (!uhci->skelqh[i]) {
  570. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  571. goto err_alloc_skelqh;
  572. }
  573. }
  574. /*
  575. * 8 Interrupt queues; link all higher int queues to int1 = async
  576. */
  577. for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
  578. uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
  579. uhci->skel_async_qh->link = UHCI_PTR_TERM;
  580. uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
  581. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  582. uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
  583. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  584. uhci->term_td->link = UHCI_PTR_TERM;
  585. uhci->skel_async_qh->element = uhci->skel_term_qh->element =
  586. LINK_TO_TD(uhci->term_td);
  587. /*
  588. * Fill the frame list: make all entries point to the proper
  589. * interrupt queue.
  590. */
  591. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  592. /* Only place we don't use the frame list routines */
  593. uhci->frame[i] = uhci_frame_skel_link(uhci, i);
  594. }
  595. /*
  596. * Some architectures require a full mb() to enforce completion of
  597. * the memory writes above before the I/O transfers in configure_hc().
  598. */
  599. mb();
  600. configure_hc(uhci);
  601. uhci->is_initialized = 1;
  602. spin_lock_irq(&uhci->lock);
  603. start_rh(uhci);
  604. spin_unlock_irq(&uhci->lock);
  605. return 0;
  606. /*
  607. * error exits:
  608. */
  609. err_alloc_skelqh:
  610. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  611. if (uhci->skelqh[i])
  612. uhci_free_qh(uhci, uhci->skelqh[i]);
  613. }
  614. uhci_free_td(uhci, uhci->term_td);
  615. err_alloc_term_td:
  616. dma_pool_destroy(uhci->qh_pool);
  617. err_create_qh_pool:
  618. dma_pool_destroy(uhci->td_pool);
  619. err_create_td_pool:
  620. kfree(uhci->frame_cpu);
  621. err_alloc_frame_cpu:
  622. dma_free_coherent(uhci_dev(uhci),
  623. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  624. uhci->frame, uhci->frame_dma_handle);
  625. err_alloc_frame:
  626. debugfs_remove(uhci->dentry);
  627. return retval;
  628. }
  629. static void uhci_stop(struct usb_hcd *hcd)
  630. {
  631. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  632. spin_lock_irq(&uhci->lock);
  633. if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
  634. uhci_hc_died(uhci);
  635. uhci_scan_schedule(uhci);
  636. spin_unlock_irq(&uhci->lock);
  637. synchronize_irq(hcd->irq);
  638. del_timer_sync(&uhci->fsbr_timer);
  639. release_uhci(uhci);
  640. }
  641. #ifdef CONFIG_PM
  642. static int uhci_rh_suspend(struct usb_hcd *hcd)
  643. {
  644. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  645. int rc = 0;
  646. spin_lock_irq(&uhci->lock);
  647. if (!HCD_HW_ACCESSIBLE(hcd))
  648. rc = -ESHUTDOWN;
  649. else if (uhci->dead)
  650. ; /* Dead controllers tell no tales */
  651. /* Once the controller is stopped, port resumes that are already
  652. * in progress won't complete. Hence if remote wakeup is enabled
  653. * for the root hub and any ports are in the middle of a resume or
  654. * remote wakeup, we must fail the suspend.
  655. */
  656. else if (hcd->self.root_hub->do_remote_wakeup &&
  657. uhci->resuming_ports) {
  658. dev_dbg(uhci_dev(uhci), "suspend failed because a port "
  659. "is resuming\n");
  660. rc = -EBUSY;
  661. } else
  662. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  663. spin_unlock_irq(&uhci->lock);
  664. return rc;
  665. }
  666. static int uhci_rh_resume(struct usb_hcd *hcd)
  667. {
  668. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  669. int rc = 0;
  670. spin_lock_irq(&uhci->lock);
  671. if (!HCD_HW_ACCESSIBLE(hcd))
  672. rc = -ESHUTDOWN;
  673. else if (!uhci->dead)
  674. wakeup_rh(uhci);
  675. spin_unlock_irq(&uhci->lock);
  676. return rc;
  677. }
  678. static int uhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  679. {
  680. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  681. struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
  682. int rc = 0;
  683. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  684. spin_lock_irq(&uhci->lock);
  685. if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
  686. goto done_okay; /* Already suspended or dead */
  687. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  688. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  689. rc = -EBUSY;
  690. goto done;
  691. };
  692. /* All PCI host controllers are required to disable IRQ generation
  693. * at the source, so we must turn off PIRQ.
  694. */
  695. pci_write_config_word(pdev, USBLEGSUP, 0);
  696. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  697. /* Enable platform-specific non-PME# wakeup */
  698. if (do_wakeup) {
  699. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  700. pci_write_config_byte(pdev, USBRES_INTEL,
  701. USBPORT1EN | USBPORT2EN);
  702. }
  703. done_okay:
  704. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  705. done:
  706. spin_unlock_irq(&uhci->lock);
  707. return rc;
  708. }
  709. static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
  710. {
  711. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  712. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  713. /* Since we aren't in D3 any more, it's safe to set this flag
  714. * even if the controller was dead.
  715. */
  716. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  717. spin_lock_irq(&uhci->lock);
  718. /* Make sure resume from hibernation re-enumerates everything */
  719. if (hibernated)
  720. uhci_hc_died(uhci);
  721. /* The firmware or a boot kernel may have changed the controller
  722. * settings during a system wakeup. Check it and reconfigure
  723. * to avoid problems.
  724. */
  725. check_and_reset_hc(uhci);
  726. /* If the controller was dead before, it's back alive now */
  727. configure_hc(uhci);
  728. /* Tell the core if the controller had to be reset */
  729. if (uhci->rh_state == UHCI_RH_RESET)
  730. usb_root_hub_lost_power(hcd->self.root_hub);
  731. spin_unlock_irq(&uhci->lock);
  732. /* If interrupts don't work and remote wakeup is enabled then
  733. * the suspended root hub needs to be polled.
  734. */
  735. if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup)
  736. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  737. /* Does the root hub have a port wakeup pending? */
  738. usb_hcd_poll_rh_status(hcd);
  739. return 0;
  740. }
  741. #endif
  742. /* Wait until a particular device/endpoint's QH is idle, and free it */
  743. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  744. struct usb_host_endpoint *hep)
  745. {
  746. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  747. struct uhci_qh *qh;
  748. spin_lock_irq(&uhci->lock);
  749. qh = (struct uhci_qh *) hep->hcpriv;
  750. if (qh == NULL)
  751. goto done;
  752. while (qh->state != QH_STATE_IDLE) {
  753. ++uhci->num_waiting;
  754. spin_unlock_irq(&uhci->lock);
  755. wait_event_interruptible(uhci->waitqh,
  756. qh->state == QH_STATE_IDLE);
  757. spin_lock_irq(&uhci->lock);
  758. --uhci->num_waiting;
  759. }
  760. uhci_free_qh(uhci, qh);
  761. done:
  762. spin_unlock_irq(&uhci->lock);
  763. }
  764. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  765. {
  766. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  767. unsigned frame_number;
  768. unsigned delta;
  769. /* Minimize latency by avoiding the spinlock */
  770. frame_number = uhci->frame_number;
  771. barrier();
  772. delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
  773. (UHCI_NUMFRAMES - 1);
  774. return frame_number + delta;
  775. }
  776. static const char hcd_name[] = "uhci_hcd";
  777. static const struct hc_driver uhci_driver = {
  778. .description = hcd_name,
  779. .product_desc = "UHCI Host Controller",
  780. .hcd_priv_size = sizeof(struct uhci_hcd),
  781. /* Generic hardware linkage */
  782. .irq = uhci_irq,
  783. .flags = HCD_USB11,
  784. /* Basic lifecycle operations */
  785. .reset = uhci_init,
  786. .start = uhci_start,
  787. #ifdef CONFIG_PM
  788. .pci_suspend = uhci_pci_suspend,
  789. .pci_resume = uhci_pci_resume,
  790. .bus_suspend = uhci_rh_suspend,
  791. .bus_resume = uhci_rh_resume,
  792. #endif
  793. .stop = uhci_stop,
  794. .urb_enqueue = uhci_urb_enqueue,
  795. .urb_dequeue = uhci_urb_dequeue,
  796. .endpoint_disable = uhci_hcd_endpoint_disable,
  797. .get_frame_number = uhci_hcd_get_frame_number,
  798. .hub_status_data = uhci_hub_status_data,
  799. .hub_control = uhci_hub_control,
  800. };
  801. static const struct pci_device_id uhci_pci_ids[] = { {
  802. /* handle any USB UHCI controller */
  803. PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_UHCI, ~0),
  804. .driver_data = (unsigned long) &uhci_driver,
  805. }, { /* end: all zeroes */ }
  806. };
  807. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  808. static struct pci_driver uhci_pci_driver = {
  809. .name = (char *)hcd_name,
  810. .id_table = uhci_pci_ids,
  811. .probe = usb_hcd_pci_probe,
  812. .remove = usb_hcd_pci_remove,
  813. .shutdown = uhci_shutdown,
  814. #ifdef CONFIG_PM_SLEEP
  815. .driver = {
  816. .pm = &usb_hcd_pci_pm_ops
  817. },
  818. #endif
  819. };
  820. static int __init uhci_hcd_init(void)
  821. {
  822. int retval = -ENOMEM;
  823. if (usb_disabled())
  824. return -ENODEV;
  825. printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
  826. ignore_oc ? ", overcurrent ignored" : "");
  827. set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  828. if (DEBUG_CONFIGURED) {
  829. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  830. if (!errbuf)
  831. goto errbuf_failed;
  832. uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
  833. if (!uhci_debugfs_root)
  834. goto debug_failed;
  835. }
  836. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  837. sizeof(struct urb_priv), 0, 0, NULL);
  838. if (!uhci_up_cachep)
  839. goto up_failed;
  840. retval = pci_register_driver(&uhci_pci_driver);
  841. if (retval)
  842. goto init_failed;
  843. return 0;
  844. init_failed:
  845. kmem_cache_destroy(uhci_up_cachep);
  846. up_failed:
  847. debugfs_remove(uhci_debugfs_root);
  848. debug_failed:
  849. kfree(errbuf);
  850. errbuf_failed:
  851. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  852. return retval;
  853. }
  854. static void __exit uhci_hcd_cleanup(void)
  855. {
  856. pci_unregister_driver(&uhci_pci_driver);
  857. kmem_cache_destroy(uhci_up_cachep);
  858. debugfs_remove(uhci_debugfs_root);
  859. kfree(errbuf);
  860. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  861. }
  862. module_init(uhci_hcd_init);
  863. module_exit(uhci_hcd_cleanup);
  864. MODULE_AUTHOR(DRIVER_AUTHOR);
  865. MODULE_DESCRIPTION(DRIVER_DESC);
  866. MODULE_LICENSE("GPL");