uhci-hcd.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 = (struct usb_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 *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. if (DEBUG_CONFIGURED) {
  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 "
  530. "debugfs entry\n");
  531. retval = -ENOMEM;
  532. goto err_create_debug_entry;
  533. }
  534. uhci->dentry = dentry;
  535. }
  536. uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
  537. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  538. &uhci->frame_dma_handle, 0);
  539. if (!uhci->frame) {
  540. dev_err(uhci_dev(uhci), "unable to allocate "
  541. "consistent memory for frame list\n");
  542. goto err_alloc_frame;
  543. }
  544. memset(uhci->frame, 0, UHCI_NUMFRAMES * sizeof(*uhci->frame));
  545. uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
  546. GFP_KERNEL);
  547. if (!uhci->frame_cpu) {
  548. dev_err(uhci_dev(uhci), "unable to allocate "
  549. "memory for frame pointers\n");
  550. goto err_alloc_frame_cpu;
  551. }
  552. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  553. sizeof(struct uhci_td), 16, 0);
  554. if (!uhci->td_pool) {
  555. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  556. goto err_create_td_pool;
  557. }
  558. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  559. sizeof(struct uhci_qh), 16, 0);
  560. if (!uhci->qh_pool) {
  561. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  562. goto err_create_qh_pool;
  563. }
  564. uhci->term_td = uhci_alloc_td(uhci);
  565. if (!uhci->term_td) {
  566. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  567. goto err_alloc_term_td;
  568. }
  569. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  570. uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
  571. if (!uhci->skelqh[i]) {
  572. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  573. goto err_alloc_skelqh;
  574. }
  575. }
  576. /*
  577. * 8 Interrupt queues; link all higher int queues to int1 = async
  578. */
  579. for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
  580. uhci->skelqh[i]->link = LINK_TO_QH(uhci->skel_async_qh);
  581. uhci->skel_async_qh->link = UHCI_PTR_TERM;
  582. uhci->skel_term_qh->link = LINK_TO_QH(uhci->skel_term_qh);
  583. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  584. uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
  585. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  586. uhci->term_td->link = UHCI_PTR_TERM;
  587. uhci->skel_async_qh->element = uhci->skel_term_qh->element =
  588. LINK_TO_TD(uhci->term_td);
  589. /*
  590. * Fill the frame list: make all entries point to the proper
  591. * interrupt queue.
  592. */
  593. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  594. /* Only place we don't use the frame list routines */
  595. uhci->frame[i] = uhci_frame_skel_link(uhci, i);
  596. }
  597. /*
  598. * Some architectures require a full mb() to enforce completion of
  599. * the memory writes above before the I/O transfers in configure_hc().
  600. */
  601. mb();
  602. configure_hc(uhci);
  603. uhci->is_initialized = 1;
  604. spin_lock_irq(&uhci->lock);
  605. start_rh(uhci);
  606. spin_unlock_irq(&uhci->lock);
  607. return 0;
  608. /*
  609. * error exits:
  610. */
  611. err_alloc_skelqh:
  612. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  613. if (uhci->skelqh[i])
  614. uhci_free_qh(uhci, uhci->skelqh[i]);
  615. }
  616. uhci_free_td(uhci, uhci->term_td);
  617. err_alloc_term_td:
  618. dma_pool_destroy(uhci->qh_pool);
  619. err_create_qh_pool:
  620. dma_pool_destroy(uhci->td_pool);
  621. err_create_td_pool:
  622. kfree(uhci->frame_cpu);
  623. err_alloc_frame_cpu:
  624. dma_free_coherent(uhci_dev(uhci),
  625. UHCI_NUMFRAMES * sizeof(*uhci->frame),
  626. uhci->frame, uhci->frame_dma_handle);
  627. err_alloc_frame:
  628. debugfs_remove(uhci->dentry);
  629. err_create_debug_entry:
  630. return retval;
  631. }
  632. static void uhci_stop(struct usb_hcd *hcd)
  633. {
  634. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  635. spin_lock_irq(&uhci->lock);
  636. if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
  637. uhci_hc_died(uhci);
  638. uhci_scan_schedule(uhci);
  639. spin_unlock_irq(&uhci->lock);
  640. synchronize_irq(hcd->irq);
  641. del_timer_sync(&uhci->fsbr_timer);
  642. release_uhci(uhci);
  643. }
  644. #ifdef CONFIG_PM
  645. static int uhci_rh_suspend(struct usb_hcd *hcd)
  646. {
  647. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  648. int rc = 0;
  649. spin_lock_irq(&uhci->lock);
  650. if (!HCD_HW_ACCESSIBLE(hcd))
  651. rc = -ESHUTDOWN;
  652. else if (uhci->dead)
  653. ; /* Dead controllers tell no tales */
  654. /* Once the controller is stopped, port resumes that are already
  655. * in progress won't complete. Hence if remote wakeup is enabled
  656. * for the root hub and any ports are in the middle of a resume or
  657. * remote wakeup, we must fail the suspend.
  658. */
  659. else if (hcd->self.root_hub->do_remote_wakeup &&
  660. uhci->resuming_ports) {
  661. dev_dbg(uhci_dev(uhci), "suspend failed because a port "
  662. "is resuming\n");
  663. rc = -EBUSY;
  664. } else
  665. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  666. spin_unlock_irq(&uhci->lock);
  667. return rc;
  668. }
  669. static int uhci_rh_resume(struct usb_hcd *hcd)
  670. {
  671. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  672. int rc = 0;
  673. spin_lock_irq(&uhci->lock);
  674. if (!HCD_HW_ACCESSIBLE(hcd))
  675. rc = -ESHUTDOWN;
  676. else if (!uhci->dead)
  677. wakeup_rh(uhci);
  678. spin_unlock_irq(&uhci->lock);
  679. return rc;
  680. }
  681. static int uhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  682. {
  683. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  684. struct pci_dev *pdev = to_pci_dev(uhci_dev(uhci));
  685. int rc = 0;
  686. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  687. spin_lock_irq(&uhci->lock);
  688. if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
  689. goto done_okay; /* Already suspended or dead */
  690. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  691. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  692. rc = -EBUSY;
  693. goto done;
  694. };
  695. /* All PCI host controllers are required to disable IRQ generation
  696. * at the source, so we must turn off PIRQ.
  697. */
  698. pci_write_config_word(pdev, USBLEGSUP, 0);
  699. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  700. /* Enable platform-specific non-PME# wakeup */
  701. if (do_wakeup) {
  702. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  703. pci_write_config_byte(pdev, USBRES_INTEL,
  704. USBPORT1EN | USBPORT2EN);
  705. }
  706. done_okay:
  707. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  708. done:
  709. spin_unlock_irq(&uhci->lock);
  710. return rc;
  711. }
  712. static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
  713. {
  714. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  715. dev_dbg(uhci_dev(uhci), "%s\n", __func__);
  716. /* Since we aren't in D3 any more, it's safe to set this flag
  717. * even if the controller was dead.
  718. */
  719. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  720. spin_lock_irq(&uhci->lock);
  721. /* Make sure resume from hibernation re-enumerates everything */
  722. if (hibernated)
  723. uhci_hc_died(uhci);
  724. /* The firmware or a boot kernel may have changed the controller
  725. * settings during a system wakeup. Check it and reconfigure
  726. * to avoid problems.
  727. */
  728. check_and_reset_hc(uhci);
  729. /* If the controller was dead before, it's back alive now */
  730. configure_hc(uhci);
  731. /* Tell the core if the controller had to be reset */
  732. if (uhci->rh_state == UHCI_RH_RESET)
  733. usb_root_hub_lost_power(hcd->self.root_hub);
  734. spin_unlock_irq(&uhci->lock);
  735. /* If interrupts don't work and remote wakeup is enabled then
  736. * the suspended root hub needs to be polled.
  737. */
  738. if (!uhci->RD_enable && hcd->self.root_hub->do_remote_wakeup)
  739. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  740. /* Does the root hub have a port wakeup pending? */
  741. usb_hcd_poll_rh_status(hcd);
  742. return 0;
  743. }
  744. #endif
  745. /* Wait until a particular device/endpoint's QH is idle, and free it */
  746. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  747. struct usb_host_endpoint *hep)
  748. {
  749. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  750. struct uhci_qh *qh;
  751. spin_lock_irq(&uhci->lock);
  752. qh = (struct uhci_qh *) hep->hcpriv;
  753. if (qh == NULL)
  754. goto done;
  755. while (qh->state != QH_STATE_IDLE) {
  756. ++uhci->num_waiting;
  757. spin_unlock_irq(&uhci->lock);
  758. wait_event_interruptible(uhci->waitqh,
  759. qh->state == QH_STATE_IDLE);
  760. spin_lock_irq(&uhci->lock);
  761. --uhci->num_waiting;
  762. }
  763. uhci_free_qh(uhci, qh);
  764. done:
  765. spin_unlock_irq(&uhci->lock);
  766. }
  767. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  768. {
  769. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  770. unsigned frame_number;
  771. unsigned delta;
  772. /* Minimize latency by avoiding the spinlock */
  773. frame_number = uhci->frame_number;
  774. barrier();
  775. delta = (inw(uhci->io_addr + USBFRNUM) - frame_number) &
  776. (UHCI_NUMFRAMES - 1);
  777. return frame_number + delta;
  778. }
  779. static const char hcd_name[] = "uhci_hcd";
  780. static const struct hc_driver uhci_driver = {
  781. .description = hcd_name,
  782. .product_desc = "UHCI Host Controller",
  783. .hcd_priv_size = sizeof(struct uhci_hcd),
  784. /* Generic hardware linkage */
  785. .irq = uhci_irq,
  786. .flags = HCD_USB11,
  787. /* Basic lifecycle operations */
  788. .reset = uhci_init,
  789. .start = uhci_start,
  790. #ifdef CONFIG_PM
  791. .pci_suspend = uhci_pci_suspend,
  792. .pci_resume = uhci_pci_resume,
  793. .bus_suspend = uhci_rh_suspend,
  794. .bus_resume = uhci_rh_resume,
  795. #endif
  796. .stop = uhci_stop,
  797. .urb_enqueue = uhci_urb_enqueue,
  798. .urb_dequeue = uhci_urb_dequeue,
  799. .endpoint_disable = uhci_hcd_endpoint_disable,
  800. .get_frame_number = uhci_hcd_get_frame_number,
  801. .hub_status_data = uhci_hub_status_data,
  802. .hub_control = uhci_hub_control,
  803. };
  804. static const struct pci_device_id uhci_pci_ids[] = { {
  805. /* handle any USB UHCI controller */
  806. PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_UHCI, ~0),
  807. .driver_data = (unsigned long) &uhci_driver,
  808. }, { /* end: all zeroes */ }
  809. };
  810. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  811. static struct pci_driver uhci_pci_driver = {
  812. .name = (char *)hcd_name,
  813. .id_table = uhci_pci_ids,
  814. .probe = usb_hcd_pci_probe,
  815. .remove = usb_hcd_pci_remove,
  816. .shutdown = uhci_shutdown,
  817. #ifdef CONFIG_PM_SLEEP
  818. .driver = {
  819. .pm = &usb_hcd_pci_pm_ops
  820. },
  821. #endif
  822. };
  823. static int __init uhci_hcd_init(void)
  824. {
  825. int retval = -ENOMEM;
  826. if (usb_disabled())
  827. return -ENODEV;
  828. printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
  829. ignore_oc ? ", overcurrent ignored" : "");
  830. set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  831. if (DEBUG_CONFIGURED) {
  832. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  833. if (!errbuf)
  834. goto errbuf_failed;
  835. uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
  836. if (!uhci_debugfs_root)
  837. goto debug_failed;
  838. }
  839. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  840. sizeof(struct urb_priv), 0, 0, NULL);
  841. if (!uhci_up_cachep)
  842. goto up_failed;
  843. retval = pci_register_driver(&uhci_pci_driver);
  844. if (retval)
  845. goto init_failed;
  846. return 0;
  847. init_failed:
  848. kmem_cache_destroy(uhci_up_cachep);
  849. up_failed:
  850. debugfs_remove(uhci_debugfs_root);
  851. debug_failed:
  852. kfree(errbuf);
  853. errbuf_failed:
  854. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  855. return retval;
  856. }
  857. static void __exit uhci_hcd_cleanup(void)
  858. {
  859. pci_unregister_driver(&uhci_pci_driver);
  860. kmem_cache_destroy(uhci_up_cachep);
  861. debugfs_remove(uhci_debugfs_root);
  862. kfree(errbuf);
  863. clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
  864. }
  865. module_init(uhci_hcd_init);
  866. module_exit(uhci_hcd_cleanup);
  867. MODULE_AUTHOR(DRIVER_AUTHOR);
  868. MODULE_DESCRIPTION(DRIVER_DESC);
  869. MODULE_LICENSE("GPL");