uhci-hcd.c 28 KB

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