uhci-hcd.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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-2005 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/config.h>
  25. #ifdef CONFIG_USB_DEBUG
  26. #define DEBUG
  27. #else
  28. #undef DEBUG
  29. #endif
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/delay.h>
  35. #include <linux/ioport.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/errno.h>
  40. #include <linux/unistd.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/pm.h>
  45. #include <linux/dmapool.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/usb.h>
  48. #include <linux/bitops.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. #include <asm/irq.h>
  52. #include <asm/system.h>
  53. #include "../core/hcd.h"
  54. #include "uhci-hcd.h"
  55. /*
  56. * Version Information
  57. */
  58. #define DRIVER_VERSION "v2.3"
  59. #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
  60. Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
  61. Alan Stern"
  62. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  63. /*
  64. * debug = 0, no debugging messages
  65. * debug = 1, dump failed URB's except for stalls
  66. * debug = 2, dump all failed URB's (including stalls)
  67. * show all queues in /debug/uhci/[pci_addr]
  68. * debug = 3, show all TD's in URB's when dumping
  69. */
  70. #ifdef DEBUG
  71. static int debug = 1;
  72. #else
  73. static int debug = 0;
  74. #endif
  75. module_param(debug, int, S_IRUGO | S_IWUSR);
  76. MODULE_PARM_DESC(debug, "Debug level");
  77. static char *errbuf;
  78. #define ERRBUF_LEN (32 * 1024)
  79. static kmem_cache_t *uhci_up_cachep; /* urb_priv */
  80. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
  81. static void wakeup_rh(struct uhci_hcd *uhci);
  82. static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
  83. /* If a transfer is still active after this much time, turn off FSBR */
  84. #define IDLE_TIMEOUT msecs_to_jiffies(50)
  85. #define FSBR_DELAY msecs_to_jiffies(50)
  86. /* When we timeout an idle transfer for FSBR, we'll switch it over to */
  87. /* depth first traversal. We'll do it in groups of this number of TD's */
  88. /* to make sure it doesn't hog all of the bandwidth */
  89. #define DEPTH_INTERVAL 5
  90. static inline void restart_timer(struct uhci_hcd *uhci)
  91. {
  92. mod_timer(&uhci->stall_timer, jiffies + msecs_to_jiffies(100));
  93. }
  94. #include "uhci-hub.c"
  95. #include "uhci-debug.c"
  96. #include "uhci-q.c"
  97. /*
  98. * Make sure the controller is completely inactive, unable to
  99. * generate interrupts or do DMA.
  100. */
  101. static void reset_hc(struct uhci_hcd *uhci)
  102. {
  103. int port;
  104. /* Turn off PIRQ enable and SMI enable. (This also turns off the
  105. * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
  106. */
  107. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  108. USBLEGSUP_RWC);
  109. /* Reset the HC - this will force us to get a
  110. * new notification of any already connected
  111. * ports due to the virtual disconnect that it
  112. * implies.
  113. */
  114. outw(USBCMD_HCRESET, uhci->io_addr + USBCMD);
  115. mb();
  116. udelay(5);
  117. if (inw(uhci->io_addr + USBCMD) & USBCMD_HCRESET)
  118. dev_warn(uhci_dev(uhci), "HCRESET not completed yet!\n");
  119. /* Just to be safe, disable interrupt requests and
  120. * make sure the controller is stopped.
  121. */
  122. outw(0, uhci->io_addr + USBINTR);
  123. outw(0, uhci->io_addr + USBCMD);
  124. /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
  125. * bits in the port status and control registers.
  126. * We have to clear them by hand.
  127. */
  128. for (port = 0; port < uhci->rh_numports; ++port)
  129. outw(0, uhci->io_addr + USBPORTSC1 + (port * 2));
  130. uhci->port_c_suspend = uhci->suspended_ports =
  131. uhci->resuming_ports = 0;
  132. uhci->rh_state = UHCI_RH_RESET;
  133. uhci->is_stopped = UHCI_IS_STOPPED;
  134. uhci_to_hcd(uhci)->state = HC_STATE_HALT;
  135. uhci_to_hcd(uhci)->poll_rh = 0;
  136. }
  137. /*
  138. * Last rites for a defunct/nonfunctional controller
  139. * or one we don't want to use any more.
  140. */
  141. static void hc_died(struct uhci_hcd *uhci)
  142. {
  143. reset_hc(uhci);
  144. uhci->hc_inaccessible = 1;
  145. del_timer(&uhci->stall_timer);
  146. }
  147. /*
  148. * Initialize a controller that was newly discovered or has just been
  149. * resumed. In either case we can't be sure of its previous state.
  150. */
  151. static void check_and_reset_hc(struct uhci_hcd *uhci)
  152. {
  153. u16 legsup;
  154. unsigned int cmd, intr;
  155. /*
  156. * When restarting a suspended controller, we expect all the
  157. * settings to be the same as we left them:
  158. *
  159. * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
  160. * Controller is stopped and configured with EGSM set;
  161. * No interrupts enabled except possibly Resume Detect.
  162. *
  163. * If any of these conditions are violated we do a complete reset.
  164. */
  165. pci_read_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, &legsup);
  166. if (legsup & ~(USBLEGSUP_RO | USBLEGSUP_RWC)) {
  167. dev_dbg(uhci_dev(uhci), "%s: legsup = 0x%04x\n",
  168. __FUNCTION__, legsup);
  169. goto reset_needed;
  170. }
  171. cmd = inw(uhci->io_addr + USBCMD);
  172. if ((cmd & USBCMD_RS) || !(cmd & USBCMD_CF) || !(cmd & USBCMD_EGSM)) {
  173. dev_dbg(uhci_dev(uhci), "%s: cmd = 0x%04x\n",
  174. __FUNCTION__, cmd);
  175. goto reset_needed;
  176. }
  177. intr = inw(uhci->io_addr + USBINTR);
  178. if (intr & (~USBINTR_RESUME)) {
  179. dev_dbg(uhci_dev(uhci), "%s: intr = 0x%04x\n",
  180. __FUNCTION__, intr);
  181. goto reset_needed;
  182. }
  183. return;
  184. reset_needed:
  185. dev_dbg(uhci_dev(uhci), "Performing full reset\n");
  186. reset_hc(uhci);
  187. }
  188. /*
  189. * Store the basic register settings needed by the controller.
  190. */
  191. static void configure_hc(struct uhci_hcd *uhci)
  192. {
  193. /* Set the frame length to the default: 1 ms exactly */
  194. outb(USBSOF_DEFAULT, uhci->io_addr + USBSOF);
  195. /* Store the frame list base address */
  196. outl(uhci->fl->dma_handle, uhci->io_addr + USBFLBASEADD);
  197. /* Set the current frame number */
  198. outw(uhci->frame_number, uhci->io_addr + USBFRNUM);
  199. /* Mark controller as running before we enable interrupts */
  200. uhci_to_hcd(uhci)->state = HC_STATE_RUNNING;
  201. mb();
  202. /* Enable PIRQ */
  203. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP,
  204. USBLEGSUP_DEFAULT);
  205. }
  206. static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
  207. {
  208. int port;
  209. switch (to_pci_dev(uhci_dev(uhci))->vendor) {
  210. default:
  211. break;
  212. case PCI_VENDOR_ID_GENESYS:
  213. /* Genesys Logic's GL880S controllers don't generate
  214. * resume-detect interrupts.
  215. */
  216. return 1;
  217. case PCI_VENDOR_ID_INTEL:
  218. /* Some of Intel's USB controllers have a bug that causes
  219. * resume-detect interrupts if any port has an over-current
  220. * condition. To make matters worse, some motherboards
  221. * hardwire unused USB ports' over-current inputs active!
  222. * To prevent problems, we will not enable resume-detect
  223. * interrupts if any ports are OC.
  224. */
  225. for (port = 0; port < uhci->rh_numports; ++port) {
  226. if (inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  227. USBPORTSC_OC)
  228. return 1;
  229. }
  230. break;
  231. }
  232. return 0;
  233. }
  234. static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
  235. __releases(uhci->lock)
  236. __acquires(uhci->lock)
  237. {
  238. int auto_stop;
  239. int int_enable;
  240. auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
  241. dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
  242. (auto_stop ? " (auto-stop)" : ""));
  243. /* If we get a suspend request when we're already auto-stopped
  244. * then there's nothing to do.
  245. */
  246. if (uhci->rh_state == UHCI_RH_AUTO_STOPPED) {
  247. uhci->rh_state = new_state;
  248. return;
  249. }
  250. /* Enable resume-detect interrupts if they work.
  251. * Then enter Global Suspend mode, still configured.
  252. */
  253. int_enable = (resume_detect_interrupts_are_broken(uhci) ?
  254. 0 : USBINTR_RESUME);
  255. outw(int_enable, uhci->io_addr + USBINTR);
  256. outw(USBCMD_EGSM | USBCMD_CF, uhci->io_addr + USBCMD);
  257. mb();
  258. udelay(5);
  259. /* If we're auto-stopping then no devices have been attached
  260. * for a while, so there shouldn't be any active URBs and the
  261. * controller should stop after a few microseconds. Otherwise
  262. * we will give the controller one frame to stop.
  263. */
  264. if (!auto_stop && !(inw(uhci->io_addr + USBSTS) & USBSTS_HCH)) {
  265. uhci->rh_state = UHCI_RH_SUSPENDING;
  266. spin_unlock_irq(&uhci->lock);
  267. msleep(1);
  268. spin_lock_irq(&uhci->lock);
  269. if (uhci->hc_inaccessible) /* Died */
  270. return;
  271. }
  272. if (!(inw(uhci->io_addr + USBSTS) & USBSTS_HCH))
  273. dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
  274. uhci_get_current_frame_number(uhci);
  275. smp_wmb();
  276. uhci->rh_state = new_state;
  277. uhci->is_stopped = UHCI_IS_STOPPED;
  278. del_timer(&uhci->stall_timer);
  279. uhci_to_hcd(uhci)->poll_rh = !int_enable;
  280. uhci_scan_schedule(uhci, NULL);
  281. }
  282. static void start_rh(struct uhci_hcd *uhci)
  283. {
  284. uhci->is_stopped = 0;
  285. smp_wmb();
  286. /* Mark it configured and running with a 64-byte max packet.
  287. * All interrupts are enabled, even though RESUME won't do anything.
  288. */
  289. outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, uhci->io_addr + USBCMD);
  290. outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
  291. uhci->io_addr + USBINTR);
  292. mb();
  293. uhci->rh_state = UHCI_RH_RUNNING;
  294. uhci_to_hcd(uhci)->poll_rh = 1;
  295. restart_timer(uhci);
  296. }
  297. static void wakeup_rh(struct uhci_hcd *uhci)
  298. __releases(uhci->lock)
  299. __acquires(uhci->lock)
  300. {
  301. dev_dbg(uhci_dev(uhci), "%s%s\n", __FUNCTION__,
  302. uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
  303. " (auto-start)" : "");
  304. /* If we are auto-stopped then no devices are attached so there's
  305. * no need for wakeup signals. Otherwise we send Global Resume
  306. * for 20 ms.
  307. */
  308. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  309. uhci->rh_state = UHCI_RH_RESUMING;
  310. outw(USBCMD_FGR | USBCMD_EGSM | USBCMD_CF,
  311. uhci->io_addr + USBCMD);
  312. spin_unlock_irq(&uhci->lock);
  313. msleep(20);
  314. spin_lock_irq(&uhci->lock);
  315. if (uhci->hc_inaccessible) /* Died */
  316. return;
  317. /* End Global Resume and wait for EOP to be sent */
  318. outw(USBCMD_CF, uhci->io_addr + USBCMD);
  319. mb();
  320. udelay(4);
  321. if (inw(uhci->io_addr + USBCMD) & USBCMD_FGR)
  322. dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
  323. }
  324. start_rh(uhci);
  325. /* Restart root hub polling */
  326. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  327. }
  328. static void stall_callback(unsigned long _uhci)
  329. {
  330. struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
  331. unsigned long flags;
  332. spin_lock_irqsave(&uhci->lock, flags);
  333. uhci_scan_schedule(uhci, NULL);
  334. check_fsbr(uhci);
  335. if (!uhci->is_stopped)
  336. restart_timer(uhci);
  337. spin_unlock_irqrestore(&uhci->lock, flags);
  338. }
  339. static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
  340. {
  341. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  342. unsigned short status;
  343. unsigned long flags;
  344. /*
  345. * Read the interrupt status, and write it back to clear the
  346. * interrupt cause. Contrary to the UHCI specification, the
  347. * "HC Halted" status bit is persistent: it is RO, not R/WC.
  348. */
  349. status = inw(uhci->io_addr + USBSTS);
  350. if (!(status & ~USBSTS_HCH)) /* shared interrupt, not mine */
  351. return IRQ_NONE;
  352. outw(status, uhci->io_addr + USBSTS); /* Clear it */
  353. if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
  354. if (status & USBSTS_HSE)
  355. dev_err(uhci_dev(uhci), "host system error, "
  356. "PCI problems?\n");
  357. if (status & USBSTS_HCPE)
  358. dev_err(uhci_dev(uhci), "host controller process "
  359. "error, something bad happened!\n");
  360. if (status & USBSTS_HCH) {
  361. spin_lock_irqsave(&uhci->lock, flags);
  362. if (uhci->rh_state >= UHCI_RH_RUNNING) {
  363. dev_err(uhci_dev(uhci),
  364. "host controller halted, "
  365. "very bad!\n");
  366. hc_died(uhci);
  367. spin_unlock_irqrestore(&uhci->lock, flags);
  368. return IRQ_HANDLED;
  369. }
  370. spin_unlock_irqrestore(&uhci->lock, flags);
  371. }
  372. }
  373. if (status & USBSTS_RD)
  374. usb_hcd_poll_rh_status(hcd);
  375. spin_lock_irqsave(&uhci->lock, flags);
  376. uhci_scan_schedule(uhci, regs);
  377. spin_unlock_irqrestore(&uhci->lock, flags);
  378. return IRQ_HANDLED;
  379. }
  380. /*
  381. * Store the current frame number in uhci->frame_number if the controller
  382. * is runnning
  383. */
  384. static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
  385. {
  386. if (!uhci->is_stopped)
  387. uhci->frame_number = inw(uhci->io_addr + USBFRNUM);
  388. }
  389. /*
  390. * De-allocate all resources
  391. */
  392. static void release_uhci(struct uhci_hcd *uhci)
  393. {
  394. int i;
  395. for (i = 0; i < UHCI_NUM_SKELQH; i++)
  396. if (uhci->skelqh[i]) {
  397. uhci_free_qh(uhci, uhci->skelqh[i]);
  398. uhci->skelqh[i] = NULL;
  399. }
  400. if (uhci->term_td) {
  401. uhci_free_td(uhci, uhci->term_td);
  402. uhci->term_td = NULL;
  403. }
  404. if (uhci->qh_pool) {
  405. dma_pool_destroy(uhci->qh_pool);
  406. uhci->qh_pool = NULL;
  407. }
  408. if (uhci->td_pool) {
  409. dma_pool_destroy(uhci->td_pool);
  410. uhci->td_pool = NULL;
  411. }
  412. if (uhci->fl) {
  413. dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  414. uhci->fl, uhci->fl->dma_handle);
  415. uhci->fl = NULL;
  416. }
  417. if (uhci->dentry) {
  418. debugfs_remove(uhci->dentry);
  419. uhci->dentry = NULL;
  420. }
  421. }
  422. static int uhci_reset(struct usb_hcd *hcd)
  423. {
  424. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  425. unsigned io_size = (unsigned) hcd->rsrc_len;
  426. int port;
  427. uhci->io_addr = (unsigned long) hcd->rsrc_start;
  428. /* The UHCI spec says devices must have 2 ports, and goes on to say
  429. * they may have more but gives no way to determine how many there
  430. * are. However according to the UHCI spec, Bit 7 of the port
  431. * status and control register is always set to 1. So we try to
  432. * use this to our advantage. Another common failure mode when
  433. * a nonexistent register is addressed is to return all ones, so
  434. * we test for that also.
  435. */
  436. for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
  437. unsigned int portstatus;
  438. portstatus = inw(uhci->io_addr + USBPORTSC1 + (port * 2));
  439. if (!(portstatus & 0x0080) || portstatus == 0xffff)
  440. break;
  441. }
  442. if (debug)
  443. dev_info(uhci_dev(uhci), "detected %d ports\n", port);
  444. /* Anything greater than 7 is weird so we'll ignore it. */
  445. if (port > UHCI_RH_MAXCHILD) {
  446. dev_info(uhci_dev(uhci), "port count misdetected? "
  447. "forcing to 2 ports\n");
  448. port = 2;
  449. }
  450. uhci->rh_numports = port;
  451. /* Kick BIOS off this hardware and reset if the controller
  452. * isn't already safely quiescent.
  453. */
  454. check_and_reset_hc(uhci);
  455. return 0;
  456. }
  457. /* Make sure the controller is quiescent and that we're not using it
  458. * any more. This is mainly for the benefit of programs which, like kexec,
  459. * expect the hardware to be idle: not doing DMA or generating IRQs.
  460. *
  461. * This routine may be called in a damaged or failing kernel. Hence we
  462. * do not acquire the spinlock before shutting down the controller.
  463. */
  464. static void uhci_shutdown(struct pci_dev *pdev)
  465. {
  466. struct usb_hcd *hcd = (struct usb_hcd *) pci_get_drvdata(pdev);
  467. hc_died(hcd_to_uhci(hcd));
  468. }
  469. /*
  470. * Allocate a frame list, and then setup the skeleton
  471. *
  472. * The hardware doesn't really know any difference
  473. * in the queues, but the order does matter for the
  474. * protocols higher up. The order is:
  475. *
  476. * - any isochronous events handled before any
  477. * of the queues. We don't do that here, because
  478. * we'll create the actual TD entries on demand.
  479. * - The first queue is the interrupt queue.
  480. * - The second queue is the control queue, split into low- and full-speed
  481. * - The third queue is bulk queue.
  482. * - The fourth queue is the bandwidth reclamation queue, which loops back
  483. * to the full-speed control queue.
  484. */
  485. static int uhci_start(struct usb_hcd *hcd)
  486. {
  487. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  488. int retval = -EBUSY;
  489. int i;
  490. dma_addr_t dma_handle;
  491. struct dentry *dentry;
  492. hcd->uses_new_polling = 1;
  493. if (pci_find_capability(to_pci_dev(uhci_dev(uhci)), PCI_CAP_ID_PM))
  494. hcd->can_wakeup = 1; /* Assume it supports PME# */
  495. dentry = debugfs_create_file(hcd->self.bus_name,
  496. S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci,
  497. &uhci_debug_operations);
  498. if (!dentry) {
  499. dev_err(uhci_dev(uhci),
  500. "couldn't create uhci debugfs entry\n");
  501. retval = -ENOMEM;
  502. goto err_create_debug_entry;
  503. }
  504. uhci->dentry = dentry;
  505. uhci->fsbr = 0;
  506. uhci->fsbrtimeout = 0;
  507. spin_lock_init(&uhci->lock);
  508. INIT_LIST_HEAD(&uhci->qh_remove_list);
  509. INIT_LIST_HEAD(&uhci->td_remove_list);
  510. INIT_LIST_HEAD(&uhci->urb_remove_list);
  511. INIT_LIST_HEAD(&uhci->urb_list);
  512. INIT_LIST_HEAD(&uhci->complete_list);
  513. init_waitqueue_head(&uhci->waitqh);
  514. init_timer(&uhci->stall_timer);
  515. uhci->stall_timer.function = stall_callback;
  516. uhci->stall_timer.data = (unsigned long) uhci;
  517. uhci->fl = dma_alloc_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  518. &dma_handle, 0);
  519. if (!uhci->fl) {
  520. dev_err(uhci_dev(uhci), "unable to allocate "
  521. "consistent memory for frame list\n");
  522. goto err_alloc_fl;
  523. }
  524. memset((void *)uhci->fl, 0, sizeof(*uhci->fl));
  525. uhci->fl->dma_handle = dma_handle;
  526. uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
  527. sizeof(struct uhci_td), 16, 0);
  528. if (!uhci->td_pool) {
  529. dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
  530. goto err_create_td_pool;
  531. }
  532. uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
  533. sizeof(struct uhci_qh), 16, 0);
  534. if (!uhci->qh_pool) {
  535. dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
  536. goto err_create_qh_pool;
  537. }
  538. uhci->term_td = uhci_alloc_td(uhci);
  539. if (!uhci->term_td) {
  540. dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
  541. goto err_alloc_term_td;
  542. }
  543. for (i = 0; i < UHCI_NUM_SKELQH; i++) {
  544. uhci->skelqh[i] = uhci_alloc_qh(uhci);
  545. if (!uhci->skelqh[i]) {
  546. dev_err(uhci_dev(uhci), "unable to allocate QH\n");
  547. goto err_alloc_skelqh;
  548. }
  549. }
  550. /*
  551. * 8 Interrupt queues; link all higher int queues to int1,
  552. * then link int1 to control and control to bulk
  553. */
  554. uhci->skel_int128_qh->link =
  555. uhci->skel_int64_qh->link =
  556. uhci->skel_int32_qh->link =
  557. uhci->skel_int16_qh->link =
  558. uhci->skel_int8_qh->link =
  559. uhci->skel_int4_qh->link =
  560. uhci->skel_int2_qh->link =
  561. cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH;
  562. uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH;
  563. uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  564. uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH;
  565. uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH;
  566. /* This dummy TD is to work around a bug in Intel PIIX controllers */
  567. uhci_fill_td(uhci->term_td, 0, (UHCI_NULL_DATA_SIZE << 21) |
  568. (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
  569. uhci->term_td->link = cpu_to_le32(uhci->term_td->dma_handle);
  570. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  571. uhci->skel_term_qh->element = cpu_to_le32(uhci->term_td->dma_handle);
  572. /*
  573. * Fill the frame list: make all entries point to the proper
  574. * interrupt queue.
  575. *
  576. * The interrupt queues will be interleaved as evenly as possible.
  577. * There's not much to be done about period-1 interrupts; they have
  578. * to occur in every frame. But we can schedule period-2 interrupts
  579. * in odd-numbered frames, period-4 interrupts in frames congruent
  580. * to 2 (mod 4), and so on. This way each frame only has two
  581. * interrupt QHs, which will help spread out bandwidth utilization.
  582. */
  583. for (i = 0; i < UHCI_NUMFRAMES; i++) {
  584. int irq;
  585. /*
  586. * ffs (Find First bit Set) does exactly what we need:
  587. * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6],
  588. * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc.
  589. * ffs > 6 => not on any high-period queue, so use
  590. * skel_int1_qh = skelqh[7].
  591. * Add UHCI_NUMFRAMES to insure at least one bit is set.
  592. */
  593. irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES);
  594. if (irq < 0)
  595. irq = 7;
  596. /* Only place we don't use the frame list routines */
  597. uhci->fl->frame[i] = UHCI_PTR_QH |
  598. cpu_to_le32(uhci->skelqh[irq]->dma_handle);
  599. }
  600. /*
  601. * Some architectures require a full mb() to enforce completion of
  602. * the memory writes above before the I/O transfers in configure_hc().
  603. */
  604. mb();
  605. configure_hc(uhci);
  606. start_rh(uhci);
  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. uhci->skelqh[i] = NULL;
  616. }
  617. uhci_free_td(uhci, uhci->term_td);
  618. uhci->term_td = NULL;
  619. err_alloc_term_td:
  620. dma_pool_destroy(uhci->qh_pool);
  621. uhci->qh_pool = NULL;
  622. err_create_qh_pool:
  623. dma_pool_destroy(uhci->td_pool);
  624. uhci->td_pool = NULL;
  625. err_create_td_pool:
  626. dma_free_coherent(uhci_dev(uhci), sizeof(*uhci->fl),
  627. uhci->fl, uhci->fl->dma_handle);
  628. uhci->fl = NULL;
  629. err_alloc_fl:
  630. debugfs_remove(uhci->dentry);
  631. uhci->dentry = NULL;
  632. err_create_debug_entry:
  633. return retval;
  634. }
  635. static void uhci_stop(struct usb_hcd *hcd)
  636. {
  637. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  638. spin_lock_irq(&uhci->lock);
  639. reset_hc(uhci);
  640. uhci_scan_schedule(uhci, NULL);
  641. spin_unlock_irq(&uhci->lock);
  642. del_timer_sync(&uhci->stall_timer);
  643. release_uhci(uhci);
  644. }
  645. #ifdef CONFIG_PM
  646. static int uhci_rh_suspend(struct usb_hcd *hcd)
  647. {
  648. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  649. spin_lock_irq(&uhci->lock);
  650. if (!uhci->hc_inaccessible) /* Not dead */
  651. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  652. spin_unlock_irq(&uhci->lock);
  653. return 0;
  654. }
  655. static int uhci_rh_resume(struct usb_hcd *hcd)
  656. {
  657. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  658. int rc = 0;
  659. spin_lock_irq(&uhci->lock);
  660. if (uhci->hc_inaccessible) {
  661. if (uhci->rh_state == UHCI_RH_SUSPENDED) {
  662. dev_warn(uhci_dev(uhci), "HC isn't running!\n");
  663. rc = -ENODEV;
  664. }
  665. /* Otherwise the HC is dead */
  666. } else
  667. wakeup_rh(uhci);
  668. spin_unlock_irq(&uhci->lock);
  669. return rc;
  670. }
  671. static int uhci_suspend(struct usb_hcd *hcd, pm_message_t message)
  672. {
  673. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  674. int rc = 0;
  675. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  676. spin_lock_irq(&uhci->lock);
  677. if (uhci->hc_inaccessible) /* Dead or already suspended */
  678. goto done;
  679. #ifndef CONFIG_USB_SUSPEND
  680. /* Otherwise this would never happen */
  681. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  682. #endif
  683. if (uhci->rh_state > UHCI_RH_SUSPENDED) {
  684. dev_warn(uhci_dev(uhci), "Root hub isn't suspended!\n");
  685. hcd->state = HC_STATE_RUNNING;
  686. rc = -EBUSY;
  687. goto done;
  688. };
  689. /* All PCI host controllers are required to disable IRQ generation
  690. * at the source, so we must turn off PIRQ.
  691. */
  692. pci_write_config_word(to_pci_dev(uhci_dev(uhci)), USBLEGSUP, 0);
  693. uhci->hc_inaccessible = 1;
  694. /* FIXME: Enable non-PME# remote wakeup? */
  695. done:
  696. spin_unlock_irq(&uhci->lock);
  697. if (rc == 0)
  698. del_timer_sync(&hcd->rh_timer);
  699. return rc;
  700. }
  701. static int uhci_resume(struct usb_hcd *hcd)
  702. {
  703. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  704. dev_dbg(uhci_dev(uhci), "%s\n", __FUNCTION__);
  705. if (uhci->rh_state == UHCI_RH_RESET) /* Dead */
  706. return 0;
  707. spin_lock_irq(&uhci->lock);
  708. /* FIXME: Disable non-PME# remote wakeup? */
  709. uhci->hc_inaccessible = 0;
  710. /* The BIOS may have changed the controller settings during a
  711. * system wakeup. Check it and reconfigure to avoid problems.
  712. */
  713. check_and_reset_hc(uhci);
  714. configure_hc(uhci);
  715. #ifndef CONFIG_USB_SUSPEND
  716. /* Otherwise this would never happen */
  717. wakeup_rh(uhci);
  718. #endif
  719. if (uhci->rh_state == UHCI_RH_RESET)
  720. suspend_rh(uhci, UHCI_RH_SUSPENDED);
  721. spin_unlock_irq(&uhci->lock);
  722. if (hcd->poll_rh)
  723. usb_hcd_poll_rh_status(hcd);
  724. return 0;
  725. }
  726. #endif
  727. /* Wait until all the URBs for a particular device/endpoint are gone */
  728. static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
  729. struct usb_host_endpoint *ep)
  730. {
  731. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  732. wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list));
  733. }
  734. static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
  735. {
  736. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  737. unsigned long flags;
  738. int is_stopped;
  739. int frame_number;
  740. /* Minimize latency by avoiding the spinlock */
  741. local_irq_save(flags);
  742. is_stopped = uhci->is_stopped;
  743. smp_rmb();
  744. frame_number = (is_stopped ? uhci->frame_number :
  745. inw(uhci->io_addr + USBFRNUM));
  746. local_irq_restore(flags);
  747. return frame_number;
  748. }
  749. static const char hcd_name[] = "uhci_hcd";
  750. static const struct hc_driver uhci_driver = {
  751. .description = hcd_name,
  752. .product_desc = "UHCI Host Controller",
  753. .hcd_priv_size = sizeof(struct uhci_hcd),
  754. /* Generic hardware linkage */
  755. .irq = uhci_irq,
  756. .flags = HCD_USB11,
  757. /* Basic lifecycle operations */
  758. .reset = uhci_reset,
  759. .start = uhci_start,
  760. #ifdef CONFIG_PM
  761. .suspend = uhci_suspend,
  762. .resume = uhci_resume,
  763. .hub_suspend = uhci_rh_suspend,
  764. .hub_resume = uhci_rh_resume,
  765. #endif
  766. .stop = uhci_stop,
  767. .urb_enqueue = uhci_urb_enqueue,
  768. .urb_dequeue = uhci_urb_dequeue,
  769. .endpoint_disable = uhci_hcd_endpoint_disable,
  770. .get_frame_number = uhci_hcd_get_frame_number,
  771. .hub_status_data = uhci_hub_status_data,
  772. .hub_control = uhci_hub_control,
  773. };
  774. static const struct pci_device_id uhci_pci_ids[] = { {
  775. /* handle any USB UHCI controller */
  776. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x00), ~0),
  777. .driver_data = (unsigned long) &uhci_driver,
  778. }, { /* end: all zeroes */ }
  779. };
  780. MODULE_DEVICE_TABLE(pci, uhci_pci_ids);
  781. static struct pci_driver uhci_pci_driver = {
  782. .name = (char *)hcd_name,
  783. .id_table = uhci_pci_ids,
  784. .probe = usb_hcd_pci_probe,
  785. .remove = usb_hcd_pci_remove,
  786. .shutdown = uhci_shutdown,
  787. #ifdef CONFIG_PM
  788. .suspend = usb_hcd_pci_suspend,
  789. .resume = usb_hcd_pci_resume,
  790. #endif /* PM */
  791. };
  792. static int __init uhci_hcd_init(void)
  793. {
  794. int retval = -ENOMEM;
  795. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION "\n");
  796. if (usb_disabled())
  797. return -ENODEV;
  798. if (debug) {
  799. errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
  800. if (!errbuf)
  801. goto errbuf_failed;
  802. }
  803. uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
  804. if (!uhci_debugfs_root)
  805. goto debug_failed;
  806. uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
  807. sizeof(struct urb_priv), 0, 0, NULL, NULL);
  808. if (!uhci_up_cachep)
  809. goto up_failed;
  810. retval = pci_register_driver(&uhci_pci_driver);
  811. if (retval)
  812. goto init_failed;
  813. return 0;
  814. init_failed:
  815. if (kmem_cache_destroy(uhci_up_cachep))
  816. warn("not all urb_priv's were freed!");
  817. up_failed:
  818. debugfs_remove(uhci_debugfs_root);
  819. debug_failed:
  820. kfree(errbuf);
  821. errbuf_failed:
  822. return retval;
  823. }
  824. static void __exit uhci_hcd_cleanup(void)
  825. {
  826. pci_unregister_driver(&uhci_pci_driver);
  827. if (kmem_cache_destroy(uhci_up_cachep))
  828. warn("not all urb_priv's were freed!");
  829. debugfs_remove(uhci_debugfs_root);
  830. kfree(errbuf);
  831. }
  832. module_init(uhci_hcd_init);
  833. module_exit(uhci_hcd_cleanup);
  834. MODULE_AUTHOR(DRIVER_AUTHOR);
  835. MODULE_DESCRIPTION(DRIVER_DESC);
  836. MODULE_LICENSE("GPL");