Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Universal Host Controller Interface driver for USB.
0004  *
0005  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
0006  *
0007  * (C) Copyright 1999 Linus Torvalds
0008  * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
0009  * (C) Copyright 1999 Randy Dunlap
0010  * (C) Copyright 1999 Georg Acher, acher@in.tum.de
0011  * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
0012  * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
0013  * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
0014  * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
0015  *               support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
0016  * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
0017  * (C) Copyright 2004-2007 Alan Stern, stern@rowland.harvard.edu
0018  *
0019  * Intel documents this fairly well, and as far as I know there
0020  * are no royalties or anything like that, but even so there are
0021  * people who decided that they want to do the same thing in a
0022  * completely different way.
0023  *
0024  */
0025 
0026 #include <linux/module.h>
0027 #include <linux/pci.h>
0028 #include <linux/kernel.h>
0029 #include <linux/init.h>
0030 #include <linux/delay.h>
0031 #include <linux/ioport.h>
0032 #include <linux/slab.h>
0033 #include <linux/errno.h>
0034 #include <linux/unistd.h>
0035 #include <linux/interrupt.h>
0036 #include <linux/spinlock.h>
0037 #include <linux/debugfs.h>
0038 #include <linux/pm.h>
0039 #include <linux/dmapool.h>
0040 #include <linux/dma-mapping.h>
0041 #include <linux/usb.h>
0042 #include <linux/usb/hcd.h>
0043 #include <linux/bitops.h>
0044 #include <linux/dmi.h>
0045 
0046 #include <linux/uaccess.h>
0047 #include <asm/io.h>
0048 #include <asm/irq.h>
0049 
0050 #include "uhci-hcd.h"
0051 
0052 /*
0053  * Version Information
0054  */
0055 #define DRIVER_AUTHOR                           \
0056     "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, "     \
0057     "Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, "   \
0058     "Roman Weissgaerber, Alan Stern"
0059 #define DRIVER_DESC "USB Universal Host Controller Interface driver"
0060 
0061 /* for flakey hardware, ignore overcurrent indicators */
0062 static bool ignore_oc;
0063 module_param(ignore_oc, bool, S_IRUGO);
0064 MODULE_PARM_DESC(ignore_oc, "ignore hardware overcurrent indications");
0065 
0066 /*
0067  * debug = 0, no debugging messages
0068  * debug = 1, dump failed URBs except for stalls
0069  * debug = 2, dump all failed URBs (including stalls)
0070  *            show all queues in /sys/kernel/debug/uhci/[pci_addr]
0071  * debug = 3, show all TDs in URBs when dumping
0072  */
0073 #ifdef CONFIG_DYNAMIC_DEBUG
0074 
0075 static int debug = 1;
0076 module_param(debug, int, S_IRUGO | S_IWUSR);
0077 MODULE_PARM_DESC(debug, "Debug level");
0078 static char *errbuf;
0079 
0080 #else
0081 
0082 #define debug 0
0083 #define errbuf NULL
0084 
0085 #endif
0086 
0087 
0088 #define ERRBUF_LEN    (32 * 1024)
0089 
0090 static struct kmem_cache *uhci_up_cachep;   /* urb_priv */
0091 
0092 static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state);
0093 static void wakeup_rh(struct uhci_hcd *uhci);
0094 static void uhci_get_current_frame_number(struct uhci_hcd *uhci);
0095 
0096 /*
0097  * Calculate the link pointer DMA value for the first Skeleton QH in a frame.
0098  */
0099 static __hc32 uhci_frame_skel_link(struct uhci_hcd *uhci, int frame)
0100 {
0101     int skelnum;
0102 
0103     /*
0104      * The interrupt queues will be interleaved as evenly as possible.
0105      * There's not much to be done about period-1 interrupts; they have
0106      * to occur in every frame.  But we can schedule period-2 interrupts
0107      * in odd-numbered frames, period-4 interrupts in frames congruent
0108      * to 2 (mod 4), and so on.  This way each frame only has two
0109      * interrupt QHs, which will help spread out bandwidth utilization.
0110      *
0111      * ffs (Find First bit Set) does exactly what we need:
0112      * 1,3,5,...  => ffs = 0 => use period-2 QH = skelqh[8],
0113      * 2,6,10,... => ffs = 1 => use period-4 QH = skelqh[7], etc.
0114      * ffs >= 7 => not on any high-period queue, so use
0115      *  period-1 QH = skelqh[9].
0116      * Add in UHCI_NUMFRAMES to insure at least one bit is set.
0117      */
0118     skelnum = 8 - (int) __ffs(frame | UHCI_NUMFRAMES);
0119     if (skelnum <= 1)
0120         skelnum = 9;
0121     return LINK_TO_QH(uhci, uhci->skelqh[skelnum]);
0122 }
0123 
0124 #include "uhci-debug.c"
0125 #include "uhci-q.c"
0126 #include "uhci-hub.c"
0127 
0128 /*
0129  * Finish up a host controller reset and update the recorded state.
0130  */
0131 static void finish_reset(struct uhci_hcd *uhci)
0132 {
0133     int port;
0134 
0135     /* HCRESET doesn't affect the Suspend, Reset, and Resume Detect
0136      * bits in the port status and control registers.
0137      * We have to clear them by hand.
0138      */
0139     for (port = 0; port < uhci->rh_numports; ++port)
0140         uhci_writew(uhci, 0, USBPORTSC1 + (port * 2));
0141 
0142     uhci->port_c_suspend = uhci->resuming_ports = 0;
0143     uhci->rh_state = UHCI_RH_RESET;
0144     uhci->is_stopped = UHCI_IS_STOPPED;
0145     clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
0146 }
0147 
0148 /*
0149  * Last rites for a defunct/nonfunctional controller
0150  * or one we don't want to use any more.
0151  */
0152 static void uhci_hc_died(struct uhci_hcd *uhci)
0153 {
0154     uhci_get_current_frame_number(uhci);
0155     uhci->reset_hc(uhci);
0156     finish_reset(uhci);
0157     uhci->dead = 1;
0158 
0159     /* The current frame may already be partway finished */
0160     ++uhci->frame_number;
0161 }
0162 
0163 /*
0164  * Initialize a controller that was newly discovered or has lost power
0165  * or otherwise been reset while it was suspended.  In none of these cases
0166  * can we be sure of its previous state.
0167  */
0168 static void check_and_reset_hc(struct uhci_hcd *uhci)
0169 {
0170     if (uhci->check_and_reset_hc(uhci))
0171         finish_reset(uhci);
0172 }
0173 
0174 #if defined(CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC)
0175 /*
0176  * The two functions below are generic reset functions that are used on systems
0177  * that do not have keyboard and mouse legacy support. We assume that we are
0178  * running on such a system if CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC is defined.
0179  */
0180 
0181 /*
0182  * Make sure the controller is completely inactive, unable to
0183  * generate interrupts or do DMA.
0184  */
0185 static void uhci_generic_reset_hc(struct uhci_hcd *uhci)
0186 {
0187     /* Reset the HC - this will force us to get a
0188      * new notification of any already connected
0189      * ports due to the virtual disconnect that it
0190      * implies.
0191      */
0192     uhci_writew(uhci, USBCMD_HCRESET, USBCMD);
0193     mb();
0194     udelay(5);
0195     if (uhci_readw(uhci, USBCMD) & USBCMD_HCRESET)
0196         dev_warn(uhci_dev(uhci), "HCRESET not completed yet!\n");
0197 
0198     /* Just to be safe, disable interrupt requests and
0199      * make sure the controller is stopped.
0200      */
0201     uhci_writew(uhci, 0, USBINTR);
0202     uhci_writew(uhci, 0, USBCMD);
0203 }
0204 
0205 /*
0206  * Initialize a controller that was newly discovered or has just been
0207  * resumed.  In either case we can't be sure of its previous state.
0208  *
0209  * Returns: 1 if the controller was reset, 0 otherwise.
0210  */
0211 static int uhci_generic_check_and_reset_hc(struct uhci_hcd *uhci)
0212 {
0213     unsigned int cmd, intr;
0214 
0215     /*
0216      * When restarting a suspended controller, we expect all the
0217      * settings to be the same as we left them:
0218      *
0219      *  Controller is stopped and configured with EGSM set;
0220      *  No interrupts enabled except possibly Resume Detect.
0221      *
0222      * If any of these conditions are violated we do a complete reset.
0223      */
0224 
0225     cmd = uhci_readw(uhci, USBCMD);
0226     if ((cmd & USBCMD_RS) || !(cmd & USBCMD_CF) || !(cmd & USBCMD_EGSM)) {
0227         dev_dbg(uhci_dev(uhci), "%s: cmd = 0x%04x\n",
0228                 __func__, cmd);
0229         goto reset_needed;
0230     }
0231 
0232     intr = uhci_readw(uhci, USBINTR);
0233     if (intr & (~USBINTR_RESUME)) {
0234         dev_dbg(uhci_dev(uhci), "%s: intr = 0x%04x\n",
0235                 __func__, intr);
0236         goto reset_needed;
0237     }
0238     return 0;
0239 
0240 reset_needed:
0241     dev_dbg(uhci_dev(uhci), "Performing full reset\n");
0242     uhci_generic_reset_hc(uhci);
0243     return 1;
0244 }
0245 #endif /* CONFIG_USB_UHCI_SUPPORT_NON_PCI_HC */
0246 
0247 /*
0248  * Store the basic register settings needed by the controller.
0249  */
0250 static void configure_hc(struct uhci_hcd *uhci)
0251 {
0252     /* Set the frame length to the default: 1 ms exactly */
0253     uhci_writeb(uhci, USBSOF_DEFAULT, USBSOF);
0254 
0255     /* Store the frame list base address */
0256     uhci_writel(uhci, uhci->frame_dma_handle, USBFLBASEADD);
0257 
0258     /* Set the current frame number */
0259     uhci_writew(uhci, uhci->frame_number & UHCI_MAX_SOF_NUMBER,
0260             USBFRNUM);
0261 
0262     /* perform any arch/bus specific configuration */
0263     if (uhci->configure_hc)
0264         uhci->configure_hc(uhci);
0265 }
0266 
0267 static int resume_detect_interrupts_are_broken(struct uhci_hcd *uhci)
0268 {
0269     /*
0270      * If we have to ignore overcurrent events then almost by definition
0271      * we can't depend on resume-detect interrupts.
0272      *
0273      * Those interrupts also don't seem to work on ASpeed SoCs.
0274      */
0275     if (ignore_oc || uhci_is_aspeed(uhci))
0276         return 1;
0277 
0278     return uhci->resume_detect_interrupts_are_broken ?
0279         uhci->resume_detect_interrupts_are_broken(uhci) : 0;
0280 }
0281 
0282 static int global_suspend_mode_is_broken(struct uhci_hcd *uhci)
0283 {
0284     return uhci->global_suspend_mode_is_broken ?
0285         uhci->global_suspend_mode_is_broken(uhci) : 0;
0286 }
0287 
0288 static void suspend_rh(struct uhci_hcd *uhci, enum uhci_rh_state new_state)
0289 __releases(uhci->lock)
0290 __acquires(uhci->lock)
0291 {
0292     int auto_stop;
0293     int int_enable, egsm_enable, wakeup_enable;
0294     struct usb_device *rhdev = uhci_to_hcd(uhci)->self.root_hub;
0295 
0296     auto_stop = (new_state == UHCI_RH_AUTO_STOPPED);
0297     dev_dbg(&rhdev->dev, "%s%s\n", __func__,
0298             (auto_stop ? " (auto-stop)" : ""));
0299 
0300     /* Start off by assuming Resume-Detect interrupts and EGSM work
0301      * and that remote wakeups should be enabled.
0302      */
0303     egsm_enable = USBCMD_EGSM;
0304     int_enable = USBINTR_RESUME;
0305     wakeup_enable = 1;
0306 
0307     /*
0308      * In auto-stop mode, we must be able to detect new connections.
0309      * The user can force us to poll by disabling remote wakeup;
0310      * otherwise we will use the EGSM/RD mechanism.
0311      */
0312     if (auto_stop) {
0313         if (!device_may_wakeup(&rhdev->dev))
0314             egsm_enable = int_enable = 0;
0315     }
0316 
0317 #ifdef CONFIG_PM
0318     /*
0319      * In bus-suspend mode, we use the wakeup setting specified
0320      * for the root hub.
0321      */
0322     else {
0323         if (!rhdev->do_remote_wakeup)
0324             wakeup_enable = 0;
0325     }
0326 #endif
0327 
0328     /*
0329      * UHCI doesn't distinguish between wakeup requests from downstream
0330      * devices and local connect/disconnect events.  There's no way to
0331      * enable one without the other; both are controlled by EGSM.  Thus
0332      * if wakeups are disallowed then EGSM must be turned off -- in which
0333      * case remote wakeup requests from downstream during system sleep
0334      * will be lost.
0335      *
0336      * In addition, if EGSM is broken then we can't use it.  Likewise,
0337      * if Resume-Detect interrupts are broken then we can't use them.
0338      *
0339      * Finally, neither EGSM nor RD is useful by itself.  Without EGSM,
0340      * the RD status bit will never get set.  Without RD, the controller
0341      * won't generate interrupts to tell the system about wakeup events.
0342      */
0343     if (!wakeup_enable || global_suspend_mode_is_broken(uhci) ||
0344             resume_detect_interrupts_are_broken(uhci))
0345         egsm_enable = int_enable = 0;
0346 
0347     uhci->RD_enable = !!int_enable;
0348     uhci_writew(uhci, int_enable, USBINTR);
0349     uhci_writew(uhci, egsm_enable | USBCMD_CF, USBCMD);
0350     mb();
0351     udelay(5);
0352 
0353     /* If we're auto-stopping then no devices have been attached
0354      * for a while, so there shouldn't be any active URBs and the
0355      * controller should stop after a few microseconds.  Otherwise
0356      * we will give the controller one frame to stop.
0357      */
0358     if (!auto_stop && !(uhci_readw(uhci, USBSTS) & USBSTS_HCH)) {
0359         uhci->rh_state = UHCI_RH_SUSPENDING;
0360         spin_unlock_irq(&uhci->lock);
0361         msleep(1);
0362         spin_lock_irq(&uhci->lock);
0363         if (uhci->dead)
0364             return;
0365     }
0366     if (!(uhci_readw(uhci, USBSTS) & USBSTS_HCH))
0367         dev_warn(uhci_dev(uhci), "Controller not stopped yet!\n");
0368 
0369     uhci_get_current_frame_number(uhci);
0370 
0371     uhci->rh_state = new_state;
0372     uhci->is_stopped = UHCI_IS_STOPPED;
0373 
0374     /*
0375      * If remote wakeup is enabled but either EGSM or RD interrupts
0376      * doesn't work, then we won't get an interrupt when a wakeup event
0377      * occurs.  Thus the suspended root hub needs to be polled.
0378      */
0379     if (wakeup_enable && (!int_enable || !egsm_enable))
0380         set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
0381     else
0382         clear_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
0383 
0384     uhci_scan_schedule(uhci);
0385     uhci_fsbr_off(uhci);
0386 }
0387 
0388 static void start_rh(struct uhci_hcd *uhci)
0389 {
0390     uhci->is_stopped = 0;
0391 
0392     /*
0393      * Clear stale status bits on Aspeed as we get a stale HCH
0394      * which causes problems later on
0395      */
0396     if (uhci_is_aspeed(uhci))
0397         uhci_writew(uhci, uhci_readw(uhci, USBSTS), USBSTS);
0398 
0399     /* Mark it configured and running with a 64-byte max packet.
0400      * All interrupts are enabled, even though RESUME won't do anything.
0401      */
0402     uhci_writew(uhci, USBCMD_RS | USBCMD_CF | USBCMD_MAXP, USBCMD);
0403     uhci_writew(uhci, USBINTR_TIMEOUT | USBINTR_RESUME |
0404         USBINTR_IOC | USBINTR_SP, USBINTR);
0405     mb();
0406     uhci->rh_state = UHCI_RH_RUNNING;
0407     set_bit(HCD_FLAG_POLL_RH, &uhci_to_hcd(uhci)->flags);
0408 }
0409 
0410 static void wakeup_rh(struct uhci_hcd *uhci)
0411 __releases(uhci->lock)
0412 __acquires(uhci->lock)
0413 {
0414     dev_dbg(&uhci_to_hcd(uhci)->self.root_hub->dev,
0415             "%s%s\n", __func__,
0416             uhci->rh_state == UHCI_RH_AUTO_STOPPED ?
0417                 " (auto-start)" : "");
0418 
0419     /* If we are auto-stopped then no devices are attached so there's
0420      * no need for wakeup signals.  Otherwise we send Global Resume
0421      * for 20 ms.
0422      */
0423     if (uhci->rh_state == UHCI_RH_SUSPENDED) {
0424         unsigned egsm;
0425 
0426         /* Keep EGSM on if it was set before */
0427         egsm = uhci_readw(uhci, USBCMD) & USBCMD_EGSM;
0428         uhci->rh_state = UHCI_RH_RESUMING;
0429         uhci_writew(uhci, USBCMD_FGR | USBCMD_CF | egsm, USBCMD);
0430         spin_unlock_irq(&uhci->lock);
0431         msleep(20);
0432         spin_lock_irq(&uhci->lock);
0433         if (uhci->dead)
0434             return;
0435 
0436         /* End Global Resume and wait for EOP to be sent */
0437         uhci_writew(uhci, USBCMD_CF, USBCMD);
0438         mb();
0439         udelay(4);
0440         if (uhci_readw(uhci, USBCMD) & USBCMD_FGR)
0441             dev_warn(uhci_dev(uhci), "FGR not stopped yet!\n");
0442     }
0443 
0444     start_rh(uhci);
0445 
0446     /* Restart root hub polling */
0447     mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
0448 }
0449 
0450 static irqreturn_t uhci_irq(struct usb_hcd *hcd)
0451 {
0452     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0453     unsigned short status;
0454 
0455     /*
0456      * Read the interrupt status, and write it back to clear the
0457      * interrupt cause.  Contrary to the UHCI specification, the
0458      * "HC Halted" status bit is persistent: it is RO, not R/WC.
0459      */
0460     status = uhci_readw(uhci, USBSTS);
0461     if (!(status & ~USBSTS_HCH))    /* shared interrupt, not mine */
0462         return IRQ_NONE;
0463     uhci_writew(uhci, status, USBSTS);      /* Clear it */
0464 
0465     spin_lock(&uhci->lock);
0466     if (unlikely(!uhci->is_initialized))    /* not yet configured */
0467         goto done;
0468 
0469     if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
0470         if (status & USBSTS_HSE)
0471             dev_err(uhci_dev(uhci),
0472                 "host system error, PCI problems?\n");
0473         if (status & USBSTS_HCPE)
0474             dev_err(uhci_dev(uhci),
0475                 "host controller process error, something bad happened!\n");
0476         if (status & USBSTS_HCH) {
0477             if (uhci->rh_state >= UHCI_RH_RUNNING) {
0478                 dev_err(uhci_dev(uhci),
0479                     "host controller halted, very bad!\n");
0480                 if (debug > 1 && errbuf) {
0481                     /* Print the schedule for debugging */
0482                     uhci_sprint_schedule(uhci, errbuf,
0483                         ERRBUF_LEN - EXTRA_SPACE);
0484                     lprintk(errbuf);
0485                 }
0486                 uhci_hc_died(uhci);
0487                 usb_hc_died(hcd);
0488 
0489                 /* Force a callback in case there are
0490                  * pending unlinks */
0491                 mod_timer(&hcd->rh_timer, jiffies);
0492             }
0493         }
0494     }
0495 
0496     if (status & USBSTS_RD) {
0497         spin_unlock(&uhci->lock);
0498         usb_hcd_poll_rh_status(hcd);
0499     } else {
0500         uhci_scan_schedule(uhci);
0501  done:
0502         spin_unlock(&uhci->lock);
0503     }
0504 
0505     return IRQ_HANDLED;
0506 }
0507 
0508 /*
0509  * Store the current frame number in uhci->frame_number if the controller
0510  * is running.  Expand from 11 bits (of which we use only 10) to a
0511  * full-sized integer.
0512  *
0513  * Like many other parts of the driver, this code relies on being polled
0514  * more than once per second as long as the controller is running.
0515  */
0516 static void uhci_get_current_frame_number(struct uhci_hcd *uhci)
0517 {
0518     if (!uhci->is_stopped) {
0519         unsigned delta;
0520 
0521         delta = (uhci_readw(uhci, USBFRNUM) - uhci->frame_number) &
0522                 (UHCI_NUMFRAMES - 1);
0523         uhci->frame_number += delta;
0524     }
0525 }
0526 
0527 /*
0528  * De-allocate all resources
0529  */
0530 static void release_uhci(struct uhci_hcd *uhci)
0531 {
0532     int i;
0533 
0534 
0535     spin_lock_irq(&uhci->lock);
0536     uhci->is_initialized = 0;
0537     spin_unlock_irq(&uhci->lock);
0538 
0539     debugfs_remove(debugfs_lookup(uhci_to_hcd(uhci)->self.bus_name,
0540                       uhci_debugfs_root));
0541 
0542     for (i = 0; i < UHCI_NUM_SKELQH; i++)
0543         uhci_free_qh(uhci, uhci->skelqh[i]);
0544 
0545     uhci_free_td(uhci, uhci->term_td);
0546 
0547     dma_pool_destroy(uhci->qh_pool);
0548 
0549     dma_pool_destroy(uhci->td_pool);
0550 
0551     kfree(uhci->frame_cpu);
0552 
0553     dma_free_coherent(uhci_dev(uhci),
0554             UHCI_NUMFRAMES * sizeof(*uhci->frame),
0555             uhci->frame, uhci->frame_dma_handle);
0556 }
0557 
0558 /*
0559  * Allocate a frame list, and then setup the skeleton
0560  *
0561  * The hardware doesn't really know any difference
0562  * in the queues, but the order does matter for the
0563  * protocols higher up.  The order in which the queues
0564  * are encountered by the hardware is:
0565  *
0566  *  - All isochronous events are handled before any
0567  *    of the queues. We don't do that here, because
0568  *    we'll create the actual TD entries on demand.
0569  *  - The first queue is the high-period interrupt queue.
0570  *  - The second queue is the period-1 interrupt and async
0571  *    (low-speed control, full-speed control, then bulk) queue.
0572  *  - The third queue is the terminating bandwidth reclamation queue,
0573  *    which contains no members, loops back to itself, and is present
0574  *    only when FSBR is on and there are no full-speed control or bulk QHs.
0575  */
0576 static int uhci_start(struct usb_hcd *hcd)
0577 {
0578     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0579     int retval = -EBUSY;
0580     int i;
0581 
0582     hcd->uses_new_polling = 1;
0583     /* Accept arbitrarily long scatter-gather lists */
0584     if (!hcd->localmem_pool)
0585         hcd->self.sg_tablesize = ~0;
0586 
0587     spin_lock_init(&uhci->lock);
0588     timer_setup(&uhci->fsbr_timer, uhci_fsbr_timeout, 0);
0589     INIT_LIST_HEAD(&uhci->idle_qh_list);
0590     init_waitqueue_head(&uhci->waitqh);
0591 
0592 #ifdef UHCI_DEBUG_OPS
0593     debugfs_create_file(hcd->self.bus_name, S_IFREG|S_IRUGO|S_IWUSR,
0594                 uhci_debugfs_root, uhci, &uhci_debug_operations);
0595 #endif
0596 
0597     uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
0598                      UHCI_NUMFRAMES * sizeof(*uhci->frame),
0599                      &uhci->frame_dma_handle, GFP_KERNEL);
0600     if (!uhci->frame) {
0601         dev_err(uhci_dev(uhci),
0602             "unable to allocate consistent memory for frame list\n");
0603         goto err_alloc_frame;
0604     }
0605 
0606     uhci->frame_cpu = kcalloc(UHCI_NUMFRAMES, sizeof(*uhci->frame_cpu),
0607             GFP_KERNEL);
0608     if (!uhci->frame_cpu)
0609         goto err_alloc_frame_cpu;
0610 
0611     uhci->td_pool = dma_pool_create("uhci_td", uhci_dev(uhci),
0612             sizeof(struct uhci_td), 16, 0);
0613     if (!uhci->td_pool) {
0614         dev_err(uhci_dev(uhci), "unable to create td dma_pool\n");
0615         goto err_create_td_pool;
0616     }
0617 
0618     uhci->qh_pool = dma_pool_create("uhci_qh", uhci_dev(uhci),
0619             sizeof(struct uhci_qh), 16, 0);
0620     if (!uhci->qh_pool) {
0621         dev_err(uhci_dev(uhci), "unable to create qh dma_pool\n");
0622         goto err_create_qh_pool;
0623     }
0624 
0625     uhci->term_td = uhci_alloc_td(uhci);
0626     if (!uhci->term_td) {
0627         dev_err(uhci_dev(uhci), "unable to allocate terminating TD\n");
0628         goto err_alloc_term_td;
0629     }
0630 
0631     for (i = 0; i < UHCI_NUM_SKELQH; i++) {
0632         uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
0633         if (!uhci->skelqh[i]) {
0634             dev_err(uhci_dev(uhci), "unable to allocate QH\n");
0635             goto err_alloc_skelqh;
0636         }
0637     }
0638 
0639     /*
0640      * 8 Interrupt queues; link all higher int queues to int1 = async
0641      */
0642     for (i = SKEL_ISO + 1; i < SKEL_ASYNC; ++i)
0643         uhci->skelqh[i]->link = LINK_TO_QH(uhci, uhci->skel_async_qh);
0644     uhci->skel_async_qh->link = UHCI_PTR_TERM(uhci);
0645     uhci->skel_term_qh->link = LINK_TO_QH(uhci, uhci->skel_term_qh);
0646 
0647     /* This dummy TD is to work around a bug in Intel PIIX controllers */
0648     uhci_fill_td(uhci, uhci->term_td, 0, uhci_explen(0) |
0649             (0x7f << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_IN, 0);
0650     uhci->term_td->link = UHCI_PTR_TERM(uhci);
0651     uhci->skel_async_qh->element = uhci->skel_term_qh->element =
0652         LINK_TO_TD(uhci, uhci->term_td);
0653 
0654     /*
0655      * Fill the frame list: make all entries point to the proper
0656      * interrupt queue.
0657      */
0658     for (i = 0; i < UHCI_NUMFRAMES; i++) {
0659 
0660         /* Only place we don't use the frame list routines */
0661         uhci->frame[i] = uhci_frame_skel_link(uhci, i);
0662     }
0663 
0664     /*
0665      * Some architectures require a full mb() to enforce completion of
0666      * the memory writes above before the I/O transfers in configure_hc().
0667      */
0668     mb();
0669 
0670     spin_lock_irq(&uhci->lock);
0671     configure_hc(uhci);
0672     uhci->is_initialized = 1;
0673     start_rh(uhci);
0674     spin_unlock_irq(&uhci->lock);
0675     return 0;
0676 
0677 /*
0678  * error exits:
0679  */
0680 err_alloc_skelqh:
0681     for (i = 0; i < UHCI_NUM_SKELQH; i++) {
0682         if (uhci->skelqh[i])
0683             uhci_free_qh(uhci, uhci->skelqh[i]);
0684     }
0685 
0686     uhci_free_td(uhci, uhci->term_td);
0687 
0688 err_alloc_term_td:
0689     dma_pool_destroy(uhci->qh_pool);
0690 
0691 err_create_qh_pool:
0692     dma_pool_destroy(uhci->td_pool);
0693 
0694 err_create_td_pool:
0695     kfree(uhci->frame_cpu);
0696 
0697 err_alloc_frame_cpu:
0698     dma_free_coherent(uhci_dev(uhci),
0699             UHCI_NUMFRAMES * sizeof(*uhci->frame),
0700             uhci->frame, uhci->frame_dma_handle);
0701 
0702 err_alloc_frame:
0703     debugfs_remove(debugfs_lookup(hcd->self.bus_name, uhci_debugfs_root));
0704 
0705     return retval;
0706 }
0707 
0708 static void uhci_stop(struct usb_hcd *hcd)
0709 {
0710     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0711 
0712     spin_lock_irq(&uhci->lock);
0713     if (HCD_HW_ACCESSIBLE(hcd) && !uhci->dead)
0714         uhci_hc_died(uhci);
0715     uhci_scan_schedule(uhci);
0716     spin_unlock_irq(&uhci->lock);
0717     synchronize_irq(hcd->irq);
0718 
0719     del_timer_sync(&uhci->fsbr_timer);
0720     release_uhci(uhci);
0721 }
0722 
0723 #ifdef CONFIG_PM
0724 static int uhci_rh_suspend(struct usb_hcd *hcd)
0725 {
0726     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0727     int rc = 0;
0728 
0729     spin_lock_irq(&uhci->lock);
0730     if (!HCD_HW_ACCESSIBLE(hcd))
0731         rc = -ESHUTDOWN;
0732     else if (uhci->dead)
0733         ;       /* Dead controllers tell no tales */
0734 
0735     /* Once the controller is stopped, port resumes that are already
0736      * in progress won't complete.  Hence if remote wakeup is enabled
0737      * for the root hub and any ports are in the middle of a resume or
0738      * remote wakeup, we must fail the suspend.
0739      */
0740     else if (hcd->self.root_hub->do_remote_wakeup &&
0741             uhci->resuming_ports) {
0742         dev_dbg(uhci_dev(uhci),
0743             "suspend failed because a port is resuming\n");
0744         rc = -EBUSY;
0745     } else
0746         suspend_rh(uhci, UHCI_RH_SUSPENDED);
0747     spin_unlock_irq(&uhci->lock);
0748     return rc;
0749 }
0750 
0751 static int uhci_rh_resume(struct usb_hcd *hcd)
0752 {
0753     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0754     int rc = 0;
0755 
0756     spin_lock_irq(&uhci->lock);
0757     if (!HCD_HW_ACCESSIBLE(hcd))
0758         rc = -ESHUTDOWN;
0759     else if (!uhci->dead)
0760         wakeup_rh(uhci);
0761     spin_unlock_irq(&uhci->lock);
0762     return rc;
0763 }
0764 
0765 #endif
0766 
0767 /* Wait until a particular device/endpoint's QH is idle, and free it */
0768 static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
0769         struct usb_host_endpoint *hep)
0770 {
0771     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0772     struct uhci_qh *qh;
0773 
0774     spin_lock_irq(&uhci->lock);
0775     qh = (struct uhci_qh *) hep->hcpriv;
0776     if (qh == NULL)
0777         goto done;
0778 
0779     while (qh->state != QH_STATE_IDLE) {
0780         ++uhci->num_waiting;
0781         spin_unlock_irq(&uhci->lock);
0782         wait_event_interruptible(uhci->waitqh,
0783                 qh->state == QH_STATE_IDLE);
0784         spin_lock_irq(&uhci->lock);
0785         --uhci->num_waiting;
0786     }
0787 
0788     uhci_free_qh(uhci, qh);
0789 done:
0790     spin_unlock_irq(&uhci->lock);
0791 }
0792 
0793 static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
0794 {
0795     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0796     unsigned frame_number;
0797     unsigned delta;
0798 
0799     /* Minimize latency by avoiding the spinlock */
0800     frame_number = uhci->frame_number;
0801     barrier();
0802     delta = (uhci_readw(uhci, USBFRNUM) - frame_number) &
0803             (UHCI_NUMFRAMES - 1);
0804     return frame_number + delta;
0805 }
0806 
0807 /* Determines number of ports on controller */
0808 static int uhci_count_ports(struct usb_hcd *hcd)
0809 {
0810     struct uhci_hcd *uhci = hcd_to_uhci(hcd);
0811     unsigned io_size = (unsigned) hcd->rsrc_len;
0812     int port;
0813 
0814     /* The UHCI spec says devices must have 2 ports, and goes on to say
0815      * they may have more but gives no way to determine how many there
0816      * are.  However according to the UHCI spec, Bit 7 of the port
0817      * status and control register is always set to 1.  So we try to
0818      * use this to our advantage.  Another common failure mode when
0819      * a nonexistent register is addressed is to return all ones, so
0820      * we test for that also.
0821      */
0822     for (port = 0; port < (io_size - USBPORTSC1) / 2; port++) {
0823         unsigned int portstatus;
0824 
0825         portstatus = uhci_readw(uhci, USBPORTSC1 + (port * 2));
0826         if (!(portstatus & 0x0080) || portstatus == 0xffff)
0827             break;
0828     }
0829     if (debug)
0830         dev_info(uhci_dev(uhci), "detected %d ports\n", port);
0831 
0832     /* Anything greater than 7 is weird so we'll ignore it. */
0833     if (port > UHCI_RH_MAXCHILD) {
0834         dev_info(uhci_dev(uhci),
0835             "port count misdetected? forcing to 2 ports\n");
0836         port = 2;
0837     }
0838 
0839     return port;
0840 }
0841 
0842 static const char hcd_name[] = "uhci_hcd";
0843 
0844 #ifdef CONFIG_USB_PCI
0845 #include "uhci-pci.c"
0846 #define PCI_DRIVER      uhci_pci_driver
0847 #endif
0848 
0849 #ifdef CONFIG_SPARC_LEON
0850 #include "uhci-grlib.c"
0851 #define PLATFORM_DRIVER     uhci_grlib_driver
0852 #endif
0853 
0854 #ifdef CONFIG_USB_UHCI_PLATFORM
0855 #include "uhci-platform.c"
0856 #define PLATFORM_DRIVER     uhci_platform_driver
0857 #endif
0858 
0859 #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER)
0860 #error "missing bus glue for uhci-hcd"
0861 #endif
0862 
0863 static int __init uhci_hcd_init(void)
0864 {
0865     int retval = -ENOMEM;
0866 
0867     if (usb_disabled())
0868         return -ENODEV;
0869 
0870     printk(KERN_INFO "uhci_hcd: " DRIVER_DESC "%s\n",
0871             ignore_oc ? ", overcurrent ignored" : "");
0872     set_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
0873 
0874 #ifdef CONFIG_DYNAMIC_DEBUG
0875     errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
0876     if (!errbuf)
0877         goto errbuf_failed;
0878     uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
0879 #endif
0880 
0881     uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
0882         sizeof(struct urb_priv), 0, 0, NULL);
0883     if (!uhci_up_cachep)
0884         goto up_failed;
0885 
0886 #ifdef PLATFORM_DRIVER
0887     retval = platform_driver_register(&PLATFORM_DRIVER);
0888     if (retval < 0)
0889         goto clean0;
0890 #endif
0891 
0892 #ifdef PCI_DRIVER
0893     retval = pci_register_driver(&PCI_DRIVER);
0894     if (retval < 0)
0895         goto clean1;
0896 #endif
0897 
0898     return 0;
0899 
0900 #ifdef PCI_DRIVER
0901 clean1:
0902 #endif
0903 #ifdef PLATFORM_DRIVER
0904     platform_driver_unregister(&PLATFORM_DRIVER);
0905 clean0:
0906 #endif
0907     kmem_cache_destroy(uhci_up_cachep);
0908 
0909 up_failed:
0910 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
0911     debugfs_remove(uhci_debugfs_root);
0912 
0913     kfree(errbuf);
0914 
0915 errbuf_failed:
0916 #endif
0917 
0918     clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
0919     return retval;
0920 }
0921 
0922 static void __exit uhci_hcd_cleanup(void) 
0923 {
0924 #ifdef PLATFORM_DRIVER
0925     platform_driver_unregister(&PLATFORM_DRIVER);
0926 #endif
0927 #ifdef PCI_DRIVER
0928     pci_unregister_driver(&PCI_DRIVER);
0929 #endif
0930     kmem_cache_destroy(uhci_up_cachep);
0931     debugfs_remove(uhci_debugfs_root);
0932 #ifdef CONFIG_DYNAMIC_DEBUG
0933     kfree(errbuf);
0934 #endif
0935     clear_bit(USB_UHCI_LOADED, &usb_hcds_loaded);
0936 }
0937 
0938 module_init(uhci_hcd_init);
0939 module_exit(uhci_hcd_cleanup);
0940 
0941 MODULE_AUTHOR(DRIVER_AUTHOR);
0942 MODULE_DESCRIPTION(DRIVER_DESC);
0943 MODULE_LICENSE("GPL");