Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * SL811HS HCD (Host Controller Driver) for USB.
0004  *
0005  * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
0006  * Copyright (C) 2004-2005 David Brownell
0007  *
0008  * Periodic scheduling is based on Roman's OHCI code
0009  *  Copyright (C) 1999 Roman Weissgaerber
0010  *
0011  * The SL811HS controller handles host side USB (like the SL11H, but with
0012  * another register set and SOF generation) as well as peripheral side USB
0013  * (like the SL811S).  This driver version doesn't implement the Gadget API
0014  * for the peripheral role; or OTG (that'd need much external circuitry).
0015  *
0016  * For documentation, see the SL811HS spec and the "SL811HS Embedded Host"
0017  * document (providing significant pieces missing from that spec); plus
0018  * the SL811S spec if you want peripheral side info.
0019  */
0020 
0021 /*
0022  * Status:  Passed basic stress testing, works with hubs, mice, keyboards,
0023  * and usb-storage.
0024  *
0025  * TODO:
0026  * - usb suspend/resume triggered by sl811
0027  * - various issues noted in the code
0028  * - performance work; use both register banks; ...
0029  * - use urb->iso_frame_desc[] with ISO transfers
0030  */
0031 
0032 #undef  VERBOSE
0033 #undef  PACKET_TRACE
0034 
0035 #include <linux/module.h>
0036 #include <linux/moduleparam.h>
0037 #include <linux/kernel.h>
0038 #include <linux/delay.h>
0039 #include <linux/ioport.h>
0040 #include <linux/sched.h>
0041 #include <linux/slab.h>
0042 #include <linux/errno.h>
0043 #include <linux/timer.h>
0044 #include <linux/list.h>
0045 #include <linux/interrupt.h>
0046 #include <linux/usb.h>
0047 #include <linux/usb/sl811.h>
0048 #include <linux/usb/hcd.h>
0049 #include <linux/platform_device.h>
0050 #include <linux/prefetch.h>
0051 #include <linux/debugfs.h>
0052 #include <linux/seq_file.h>
0053 
0054 #include <asm/io.h>
0055 #include <asm/irq.h>
0056 #include <asm/byteorder.h>
0057 #include <asm/unaligned.h>
0058 
0059 #include "sl811.h"
0060 
0061 
0062 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
0063 MODULE_LICENSE("GPL");
0064 MODULE_ALIAS("platform:sl811-hcd");
0065 
0066 #define DRIVER_VERSION  "19 May 2005"
0067 
0068 /* for now, use only one transfer register bank */
0069 #undef  USE_B
0070 
0071 // #define  QUIRK2
0072 #define QUIRK3
0073 
0074 static const char hcd_name[] = "sl811-hcd";
0075 
0076 /*-------------------------------------------------------------------------*/
0077 
0078 static void port_power(struct sl811 *sl811, int is_on)
0079 {
0080     struct usb_hcd  *hcd = sl811_to_hcd(sl811);
0081 
0082     /* hub is inactive unless the port is powered */
0083     if (is_on) {
0084         if (sl811->port1 & USB_PORT_STAT_POWER)
0085             return;
0086 
0087         sl811->port1 = USB_PORT_STAT_POWER;
0088         sl811->irq_enable = SL11H_INTMASK_INSRMV;
0089     } else {
0090         sl811->port1 = 0;
0091         sl811->irq_enable = 0;
0092         hcd->state = HC_STATE_HALT;
0093     }
0094     sl811->ctrl1 = 0;
0095     sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
0096     sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
0097 
0098     if (sl811->board && sl811->board->port_power) {
0099         /* switch VBUS, at 500mA unless hub power budget gets set */
0100         dev_dbg(hcd->self.controller, "power %s\n",
0101             is_on ? "on" : "off");
0102         sl811->board->port_power(hcd->self.controller, is_on);
0103     }
0104 
0105     /* reset as thoroughly as we can */
0106     if (sl811->board && sl811->board->reset)
0107         sl811->board->reset(hcd->self.controller);
0108     else {
0109         sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
0110         mdelay(20);
0111     }
0112 
0113     sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
0114     sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
0115     sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
0116     sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
0117 
0118     // if !is_on, put into lowpower mode now
0119 }
0120 
0121 /*-------------------------------------------------------------------------*/
0122 
0123 /* This is a PIO-only HCD.  Queueing appends URBs to the endpoint's queue,
0124  * and may start I/O.  Endpoint queues are scanned during completion irq
0125  * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
0126  *
0127  * Using an external DMA engine to copy a packet at a time could work,
0128  * though setup/teardown costs may be too big to make it worthwhile.
0129  */
0130 
0131 /* SETUP starts a new control request.  Devices are not allowed to
0132  * STALL or NAK these; they must cancel any pending control requests.
0133  */
0134 static void setup_packet(
0135     struct sl811        *sl811,
0136     struct sl811h_ep    *ep,
0137     struct urb      *urb,
0138     u8          bank,
0139     u8          control
0140 )
0141 {
0142     u8          addr;
0143     u8          len;
0144     void __iomem        *data_reg;
0145 
0146     addr = SL811HS_PACKET_BUF(bank == 0);
0147     len = sizeof(struct usb_ctrlrequest);
0148     data_reg = sl811->data_reg;
0149     sl811_write_buf(sl811, addr, urb->setup_packet, len);
0150 
0151     /* autoincrementing */
0152     sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
0153     writeb(len, data_reg);
0154     writeb(SL_SETUP /* | ep->epnum */, data_reg);
0155     writeb(usb_pipedevice(urb->pipe), data_reg);
0156 
0157     /* always OUT/data0 */
0158     sl811_write(sl811, bank + SL11H_HOSTCTLREG,
0159             control | SL11H_HCTLMASK_OUT);
0160     ep->length = 0;
0161     PACKET("SETUP qh%p\n", ep);
0162 }
0163 
0164 /* STATUS finishes control requests, often after IN or OUT data packets */
0165 static void status_packet(
0166     struct sl811        *sl811,
0167     struct sl811h_ep    *ep,
0168     struct urb      *urb,
0169     u8          bank,
0170     u8          control
0171 )
0172 {
0173     int         do_out;
0174     void __iomem        *data_reg;
0175 
0176     do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
0177     data_reg = sl811->data_reg;
0178 
0179     /* autoincrementing */
0180     sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
0181     writeb(0, data_reg);
0182     writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
0183     writeb(usb_pipedevice(urb->pipe), data_reg);
0184 
0185     /* always data1; sometimes IN */
0186     control |= SL11H_HCTLMASK_TOGGLE;
0187     if (do_out)
0188         control |= SL11H_HCTLMASK_OUT;
0189     sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
0190     ep->length = 0;
0191     PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
0192             do_out ? "out" : "in", ep);
0193 }
0194 
0195 /* IN packets can be used with any type of endpoint. here we just
0196  * start the transfer, data from the peripheral may arrive later.
0197  * urb->iso_frame_desc is currently ignored here...
0198  */
0199 static void in_packet(
0200     struct sl811        *sl811,
0201     struct sl811h_ep    *ep,
0202     struct urb      *urb,
0203     u8          bank,
0204     u8          control
0205 )
0206 {
0207     u8          addr;
0208     u8          len;
0209     void __iomem        *data_reg;
0210 
0211     /* avoid losing data on overflow */
0212     len = ep->maxpacket;
0213     addr = SL811HS_PACKET_BUF(bank == 0);
0214     if (!(control & SL11H_HCTLMASK_ISOCH)
0215             && usb_gettoggle(urb->dev, ep->epnum, 0))
0216         control |= SL11H_HCTLMASK_TOGGLE;
0217     data_reg = sl811->data_reg;
0218 
0219     /* autoincrementing */
0220     sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
0221     writeb(len, data_reg);
0222     writeb(SL_IN | ep->epnum, data_reg);
0223     writeb(usb_pipedevice(urb->pipe), data_reg);
0224 
0225     sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
0226     ep->length = min_t(u32, len,
0227             urb->transfer_buffer_length - urb->actual_length);
0228     PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
0229             !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
0230 }
0231 
0232 /* OUT packets can be used with any type of endpoint.
0233  * urb->iso_frame_desc is currently ignored here...
0234  */
0235 static void out_packet(
0236     struct sl811        *sl811,
0237     struct sl811h_ep    *ep,
0238     struct urb      *urb,
0239     u8          bank,
0240     u8          control
0241 )
0242 {
0243     void            *buf;
0244     u8          addr;
0245     u8          len;
0246     void __iomem        *data_reg;
0247 
0248     buf = urb->transfer_buffer + urb->actual_length;
0249     prefetch(buf);
0250 
0251     len = min_t(u32, ep->maxpacket,
0252             urb->transfer_buffer_length - urb->actual_length);
0253 
0254     if (!(control & SL11H_HCTLMASK_ISOCH)
0255             && usb_gettoggle(urb->dev, ep->epnum, 1))
0256         control |= SL11H_HCTLMASK_TOGGLE;
0257     addr = SL811HS_PACKET_BUF(bank == 0);
0258     data_reg = sl811->data_reg;
0259 
0260     sl811_write_buf(sl811, addr, buf, len);
0261 
0262     /* autoincrementing */
0263     sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
0264     writeb(len, data_reg);
0265     writeb(SL_OUT | ep->epnum, data_reg);
0266     writeb(usb_pipedevice(urb->pipe), data_reg);
0267 
0268     sl811_write(sl811, bank + SL11H_HOSTCTLREG,
0269             control | SL11H_HCTLMASK_OUT);
0270     ep->length = len;
0271     PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
0272             !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
0273 }
0274 
0275 /*-------------------------------------------------------------------------*/
0276 
0277 /* caller updates on-chip enables later */
0278 
0279 static inline void sofirq_on(struct sl811 *sl811)
0280 {
0281     if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
0282         return;
0283     dev_dbg(sl811_to_hcd(sl811)->self.controller, "sof irq on\n");
0284     sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
0285 }
0286 
0287 static inline void sofirq_off(struct sl811 *sl811)
0288 {
0289     if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
0290         return;
0291     dev_dbg(sl811_to_hcd(sl811)->self.controller, "sof irq off\n");
0292     sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
0293 }
0294 
0295 /*-------------------------------------------------------------------------*/
0296 
0297 /* pick the next endpoint for a transaction, and issue it.
0298  * frames start with periodic transfers (after whatever is pending
0299  * from the previous frame), and the rest of the time is async
0300  * transfers, scheduled round-robin.
0301  */
0302 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
0303 {
0304     struct sl811h_ep    *ep;
0305     struct urb      *urb;
0306     int         fclock;
0307     u8          control;
0308 
0309     /* use endpoint at schedule head */
0310     if (sl811->next_periodic) {
0311         ep = sl811->next_periodic;
0312         sl811->next_periodic = ep->next;
0313     } else {
0314         if (sl811->next_async)
0315             ep = sl811->next_async;
0316         else if (!list_empty(&sl811->async))
0317             ep = container_of(sl811->async.next,
0318                     struct sl811h_ep, schedule);
0319         else {
0320             /* could set up the first fullspeed periodic
0321              * transfer for the next frame ...
0322              */
0323             return NULL;
0324         }
0325 
0326 #ifdef USE_B
0327         if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
0328             return NULL;
0329 #endif
0330 
0331         if (ep->schedule.next == &sl811->async)
0332             sl811->next_async = NULL;
0333         else
0334             sl811->next_async = container_of(ep->schedule.next,
0335                     struct sl811h_ep, schedule);
0336     }
0337 
0338     if (unlikely(list_empty(&ep->hep->urb_list))) {
0339         dev_dbg(sl811_to_hcd(sl811)->self.controller,
0340             "empty %p queue?\n", ep);
0341         return NULL;
0342     }
0343 
0344     urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
0345     control = ep->defctrl;
0346 
0347     /* if this frame doesn't have enough time left to transfer this
0348      * packet, wait till the next frame.  too-simple algorithm...
0349      */
0350     fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
0351     fclock -= 100;      /* setup takes not much time */
0352     if (urb->dev->speed == USB_SPEED_LOW) {
0353         if (control & SL11H_HCTLMASK_PREAMBLE) {
0354             /* also note erratum 1: some hubs won't work */
0355             fclock -= 800;
0356         }
0357         fclock -= ep->maxpacket << 8;
0358 
0359         /* erratum 2: AFTERSOF only works for fullspeed */
0360         if (fclock < 0) {
0361             if (ep->period)
0362                 sl811->stat_overrun++;
0363             sofirq_on(sl811);
0364             return NULL;
0365         }
0366     } else {
0367         fclock -= 12000 / 19;   /* 19 64byte packets/msec */
0368         if (fclock < 0) {
0369             if (ep->period)
0370                 sl811->stat_overrun++;
0371             control |= SL11H_HCTLMASK_AFTERSOF;
0372 
0373         /* throttle bulk/control irq noise */
0374         } else if (ep->nak_count)
0375             control |= SL11H_HCTLMASK_AFTERSOF;
0376     }
0377 
0378 
0379     switch (ep->nextpid) {
0380     case USB_PID_IN:
0381         in_packet(sl811, ep, urb, bank, control);
0382         break;
0383     case USB_PID_OUT:
0384         out_packet(sl811, ep, urb, bank, control);
0385         break;
0386     case USB_PID_SETUP:
0387         setup_packet(sl811, ep, urb, bank, control);
0388         break;
0389     case USB_PID_ACK:       /* for control status */
0390         status_packet(sl811, ep, urb, bank, control);
0391         break;
0392     default:
0393         dev_dbg(sl811_to_hcd(sl811)->self.controller,
0394             "bad ep%p pid %02x\n", ep, ep->nextpid);
0395         ep = NULL;
0396     }
0397     return ep;
0398 }
0399 
0400 #define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
0401 
0402 static inline void start_transfer(struct sl811 *sl811)
0403 {
0404     if (sl811->port1 & USB_PORT_STAT_SUSPEND)
0405         return;
0406     if (sl811->active_a == NULL) {
0407         sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
0408         if (sl811->active_a != NULL)
0409             sl811->jiffies_a = jiffies + MIN_JIFFIES;
0410     }
0411 #ifdef USE_B
0412     if (sl811->active_b == NULL) {
0413         sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
0414         if (sl811->active_b != NULL)
0415             sl811->jiffies_b = jiffies + MIN_JIFFIES;
0416     }
0417 #endif
0418 }
0419 
0420 static void finish_request(
0421     struct sl811        *sl811,
0422     struct sl811h_ep    *ep,
0423     struct urb      *urb,
0424     int         status
0425 ) __releases(sl811->lock) __acquires(sl811->lock)
0426 {
0427     unsigned        i;
0428 
0429     if (usb_pipecontrol(urb->pipe))
0430         ep->nextpid = USB_PID_SETUP;
0431 
0432     usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
0433     spin_unlock(&sl811->lock);
0434     usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, status);
0435     spin_lock(&sl811->lock);
0436 
0437     /* leave active endpoints in the schedule */
0438     if (!list_empty(&ep->hep->urb_list))
0439         return;
0440 
0441     /* async deschedule? */
0442     if (!list_empty(&ep->schedule)) {
0443         list_del_init(&ep->schedule);
0444         if (ep == sl811->next_async)
0445             sl811->next_async = NULL;
0446         return;
0447     }
0448 
0449     /* periodic deschedule */
0450     dev_dbg(sl811_to_hcd(sl811)->self.controller,
0451         "deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
0452     for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
0453         struct sl811h_ep    *temp;
0454         struct sl811h_ep    **prev = &sl811->periodic[i];
0455 
0456         while (*prev && ((temp = *prev) != ep))
0457             prev = &temp->next;
0458         if (*prev)
0459             *prev = ep->next;
0460         sl811->load[i] -= ep->load;
0461     }
0462     ep->branch = PERIODIC_SIZE;
0463     sl811->periodic_count--;
0464     sl811_to_hcd(sl811)->self.bandwidth_allocated
0465         -= ep->load / ep->period;
0466     if (ep == sl811->next_periodic)
0467         sl811->next_periodic = ep->next;
0468 
0469     /* we might turn SOFs back on again for the async schedule */
0470     if (sl811->periodic_count == 0)
0471         sofirq_off(sl811);
0472 }
0473 
0474 static void
0475 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
0476 {
0477     u8          status;
0478     struct urb      *urb;
0479     int         urbstat = -EINPROGRESS;
0480 
0481     if (unlikely(!ep))
0482         return;
0483 
0484     status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
0485 
0486     urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
0487 
0488     /* we can safely ignore NAKs */
0489     if (status & SL11H_STATMASK_NAK) {
0490         // PACKET("...NAK_%02x qh%p\n", bank, ep);
0491         if (!ep->period)
0492             ep->nak_count++;
0493         ep->error_count = 0;
0494 
0495     /* ACK advances transfer, toggle, and maybe queue */
0496     } else if (status & SL11H_STATMASK_ACK) {
0497         struct usb_device   *udev = urb->dev;
0498         int         len;
0499         unsigned char       *buf;
0500 
0501         /* urb->iso_frame_desc is currently ignored here... */
0502 
0503         ep->nak_count = ep->error_count = 0;
0504         switch (ep->nextpid) {
0505         case USB_PID_OUT:
0506             // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
0507             urb->actual_length += ep->length;
0508             usb_dotoggle(udev, ep->epnum, 1);
0509             if (urb->actual_length
0510                     == urb->transfer_buffer_length) {
0511                 if (usb_pipecontrol(urb->pipe))
0512                     ep->nextpid = USB_PID_ACK;
0513 
0514                 /* some bulk protocols terminate OUT transfers
0515                  * by a short packet, using ZLPs not padding.
0516                  */
0517                 else if (ep->length < ep->maxpacket
0518                         || !(urb->transfer_flags
0519                             & URB_ZERO_PACKET))
0520                     urbstat = 0;
0521             }
0522             break;
0523         case USB_PID_IN:
0524             // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
0525             buf = urb->transfer_buffer + urb->actual_length;
0526             prefetchw(buf);
0527             len = ep->maxpacket - sl811_read(sl811,
0528                         bank + SL11H_XFERCNTREG);
0529             if (len > ep->length) {
0530                 len = ep->length;
0531                 urbstat = -EOVERFLOW;
0532             }
0533             urb->actual_length += len;
0534             sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
0535                     buf, len);
0536             usb_dotoggle(udev, ep->epnum, 0);
0537             if (urbstat == -EINPROGRESS &&
0538                     (len < ep->maxpacket ||
0539                         urb->actual_length ==
0540                         urb->transfer_buffer_length)) {
0541                 if (usb_pipecontrol(urb->pipe))
0542                     ep->nextpid = USB_PID_ACK;
0543                 else
0544                     urbstat = 0;
0545             }
0546             break;
0547         case USB_PID_SETUP:
0548             // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
0549             if (urb->transfer_buffer_length == urb->actual_length)
0550                 ep->nextpid = USB_PID_ACK;
0551             else if (usb_pipeout(urb->pipe)) {
0552                 usb_settoggle(udev, 0, 1, 1);
0553                 ep->nextpid = USB_PID_OUT;
0554             } else {
0555                 usb_settoggle(udev, 0, 0, 1);
0556                 ep->nextpid = USB_PID_IN;
0557             }
0558             break;
0559         case USB_PID_ACK:
0560             // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
0561             urbstat = 0;
0562             break;
0563         }
0564 
0565     /* STALL stops all transfers */
0566     } else if (status & SL11H_STATMASK_STALL) {
0567         PACKET("...STALL_%02x qh%p\n", bank, ep);
0568         ep->nak_count = ep->error_count = 0;
0569         urbstat = -EPIPE;
0570 
0571     /* error? retry, until "3 strikes" */
0572     } else if (++ep->error_count >= 3) {
0573         if (status & SL11H_STATMASK_TMOUT)
0574             urbstat = -ETIME;
0575         else if (status & SL11H_STATMASK_OVF)
0576             urbstat = -EOVERFLOW;
0577         else
0578             urbstat = -EPROTO;
0579         ep->error_count = 0;
0580         PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
0581                 bank, status, ep, urbstat);
0582     }
0583 
0584     if (urbstat != -EINPROGRESS || urb->unlinked)
0585         finish_request(sl811, ep, urb, urbstat);
0586 }
0587 
0588 static inline u8 checkdone(struct sl811 *sl811)
0589 {
0590     u8  ctl;
0591     u8  irqstat = 0;
0592 
0593     if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
0594         ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
0595         if (ctl & SL11H_HCTLMASK_ARM)
0596             sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
0597         dev_dbg(sl811_to_hcd(sl811)->self.controller,
0598             "%s DONE_A: ctrl %02x sts %02x\n",
0599             (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
0600             ctl,
0601             sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
0602         irqstat |= SL11H_INTMASK_DONE_A;
0603     }
0604 #ifdef  USE_B
0605     if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
0606         ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
0607         if (ctl & SL11H_HCTLMASK_ARM)
0608             sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
0609         dev_dbg(sl811_to_hcd(sl811)->self.controller,
0610             "%s DONE_B: ctrl %02x sts %02x\n",
0611             (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
0612             ctl,
0613             sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
0614         irqstat |= SL11H_INTMASK_DONE_A;
0615     }
0616 #endif
0617     return irqstat;
0618 }
0619 
0620 static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
0621 {
0622     struct sl811    *sl811 = hcd_to_sl811(hcd);
0623     u8      irqstat;
0624     irqreturn_t ret = IRQ_NONE;
0625     unsigned    retries = 5;
0626 
0627     spin_lock(&sl811->lock);
0628 
0629 retry:
0630     irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
0631     if (irqstat) {
0632         sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
0633         irqstat &= sl811->irq_enable;
0634     }
0635 
0636 #ifdef  QUIRK2
0637     /* this may no longer be necessary ... */
0638     if (irqstat == 0) {
0639         irqstat = checkdone(sl811);
0640         if (irqstat)
0641             sl811->stat_lost++;
0642     }
0643 #endif
0644 
0645     /* USB packets, not necessarily handled in the order they're
0646      * issued ... that's fine if they're different endpoints.
0647      */
0648     if (irqstat & SL11H_INTMASK_DONE_A) {
0649         done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
0650         sl811->active_a = NULL;
0651         sl811->stat_a++;
0652     }
0653 #ifdef USE_B
0654     if (irqstat & SL11H_INTMASK_DONE_B) {
0655         done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
0656         sl811->active_b = NULL;
0657         sl811->stat_b++;
0658     }
0659 #endif
0660     if (irqstat & SL11H_INTMASK_SOFINTR) {
0661         unsigned index;
0662 
0663         index = sl811->frame++ % (PERIODIC_SIZE - 1);
0664         sl811->stat_sof++;
0665 
0666         /* be graceful about almost-inevitable periodic schedule
0667          * overruns:  continue the previous frame's transfers iff
0668          * this one has nothing scheduled.
0669          */
0670         if (sl811->next_periodic) {
0671             // dev_err(hcd->self.controller, "overrun to slot %d\n", index);
0672             sl811->stat_overrun++;
0673         }
0674         if (sl811->periodic[index])
0675             sl811->next_periodic = sl811->periodic[index];
0676     }
0677 
0678     /* hub_wq manages debouncing and wakeup */
0679     if (irqstat & SL11H_INTMASK_INSRMV) {
0680         sl811->stat_insrmv++;
0681 
0682         /* most stats are reset for each VBUS session */
0683         sl811->stat_wake = 0;
0684         sl811->stat_sof = 0;
0685         sl811->stat_a = 0;
0686         sl811->stat_b = 0;
0687         sl811->stat_lost = 0;
0688 
0689         sl811->ctrl1 = 0;
0690         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
0691 
0692         sl811->irq_enable = SL11H_INTMASK_INSRMV;
0693         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
0694 
0695         /* usbcore nukes other pending transactions on disconnect */
0696         if (sl811->active_a) {
0697             sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
0698             finish_request(sl811, sl811->active_a,
0699                 container_of(sl811->active_a
0700                         ->hep->urb_list.next,
0701                     struct urb, urb_list),
0702                 -ESHUTDOWN);
0703             sl811->active_a = NULL;
0704         }
0705 #ifdef  USE_B
0706         if (sl811->active_b) {
0707             sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
0708             finish_request(sl811, sl811->active_b,
0709                 container_of(sl811->active_b
0710                         ->hep->urb_list.next,
0711                     struct urb, urb_list),
0712                 NULL, -ESHUTDOWN);
0713             sl811->active_b = NULL;
0714         }
0715 #endif
0716 
0717         /* port status seems weird until after reset, so
0718          * force the reset and make hub_wq clean up later.
0719          */
0720         if (irqstat & SL11H_INTMASK_RD)
0721             sl811->port1 &= ~USB_PORT_STAT_CONNECTION;
0722         else
0723             sl811->port1 |= USB_PORT_STAT_CONNECTION;
0724 
0725         sl811->port1 |= USB_PORT_STAT_C_CONNECTION << 16;
0726 
0727     } else if (irqstat & SL11H_INTMASK_RD) {
0728         if (sl811->port1 & USB_PORT_STAT_SUSPEND) {
0729             dev_dbg(hcd->self.controller, "wakeup\n");
0730             sl811->port1 |= USB_PORT_STAT_C_SUSPEND << 16;
0731             sl811->stat_wake++;
0732         } else
0733             irqstat &= ~SL11H_INTMASK_RD;
0734     }
0735 
0736     if (irqstat) {
0737         if (sl811->port1 & USB_PORT_STAT_ENABLE)
0738             start_transfer(sl811);
0739         ret = IRQ_HANDLED;
0740         if (retries--)
0741             goto retry;
0742     }
0743 
0744     if (sl811->periodic_count == 0 && list_empty(&sl811->async))
0745         sofirq_off(sl811);
0746     sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
0747 
0748     spin_unlock(&sl811->lock);
0749 
0750     return ret;
0751 }
0752 
0753 /*-------------------------------------------------------------------------*/
0754 
0755 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
0756  * this driver doesn't promise that much since it's got to handle an
0757  * IRQ per packet; irq handling latencies also use up that time.
0758  *
0759  * NOTE:  the periodic schedule is a sparse tree, with the load for
0760  * each branch minimized.  see fig 3.5 in the OHCI spec for example.
0761  */
0762 #define MAX_PERIODIC_LOAD   500 /* out of 1000 usec */
0763 
0764 static int balance(struct sl811 *sl811, u16 period, u16 load)
0765 {
0766     int i, branch = -ENOSPC;
0767 
0768     /* search for the least loaded schedule branch of that period
0769      * which has enough bandwidth left unreserved.
0770      */
0771     for (i = 0; i < period ; i++) {
0772         if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
0773             int j;
0774 
0775             for (j = i; j < PERIODIC_SIZE; j += period) {
0776                 if ((sl811->load[j] + load)
0777                         > MAX_PERIODIC_LOAD)
0778                     break;
0779             }
0780             if (j < PERIODIC_SIZE)
0781                 continue;
0782             branch = i;
0783         }
0784     }
0785     return branch;
0786 }
0787 
0788 /*-------------------------------------------------------------------------*/
0789 
0790 static int sl811h_urb_enqueue(
0791     struct usb_hcd      *hcd,
0792     struct urb      *urb,
0793     gfp_t           mem_flags
0794 ) {
0795     struct sl811        *sl811 = hcd_to_sl811(hcd);
0796     struct usb_device   *udev = urb->dev;
0797     unsigned int        pipe = urb->pipe;
0798     int         is_out = !usb_pipein(pipe);
0799     int         type = usb_pipetype(pipe);
0800     int         epnum = usb_pipeendpoint(pipe);
0801     struct sl811h_ep    *ep = NULL;
0802     unsigned long       flags;
0803     int         i;
0804     int         retval;
0805     struct usb_host_endpoint    *hep = urb->ep;
0806 
0807 #ifndef CONFIG_USB_SL811_HCD_ISO
0808     if (type == PIPE_ISOCHRONOUS)
0809         return -ENOSPC;
0810 #endif
0811 
0812     /* avoid all allocations within spinlocks */
0813     if (!hep->hcpriv) {
0814         ep = kzalloc(sizeof *ep, mem_flags);
0815         if (ep == NULL)
0816             return -ENOMEM;
0817     }
0818 
0819     spin_lock_irqsave(&sl811->lock, flags);
0820 
0821     /* don't submit to a dead or disabled port */
0822     if (!(sl811->port1 & USB_PORT_STAT_ENABLE)
0823             || !HC_IS_RUNNING(hcd->state)) {
0824         retval = -ENODEV;
0825         kfree(ep);
0826         goto fail_not_linked;
0827     }
0828     retval = usb_hcd_link_urb_to_ep(hcd, urb);
0829     if (retval) {
0830         kfree(ep);
0831         goto fail_not_linked;
0832     }
0833 
0834     if (hep->hcpriv) {
0835         kfree(ep);
0836         ep = hep->hcpriv;
0837     } else if (!ep) {
0838         retval = -ENOMEM;
0839         goto fail;
0840 
0841     } else {
0842         INIT_LIST_HEAD(&ep->schedule);
0843         ep->udev = udev;
0844         ep->epnum = epnum;
0845         ep->maxpacket = usb_maxpacket(udev, urb->pipe);
0846         ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
0847         usb_settoggle(udev, epnum, is_out, 0);
0848 
0849         if (type == PIPE_CONTROL)
0850             ep->nextpid = USB_PID_SETUP;
0851         else if (is_out)
0852             ep->nextpid = USB_PID_OUT;
0853         else
0854             ep->nextpid = USB_PID_IN;
0855 
0856         if (ep->maxpacket > H_MAXPACKET) {
0857             /* iso packets up to 240 bytes could work... */
0858             dev_dbg(hcd->self.controller,
0859                 "dev %d ep%d maxpacket %d\n", udev->devnum,
0860                 epnum, ep->maxpacket);
0861             retval = -EINVAL;
0862             kfree(ep);
0863             goto fail;
0864         }
0865 
0866         if (udev->speed == USB_SPEED_LOW) {
0867             /* send preamble for external hub? */
0868             if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
0869                 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
0870         }
0871         switch (type) {
0872         case PIPE_ISOCHRONOUS:
0873         case PIPE_INTERRUPT:
0874             if (urb->interval > PERIODIC_SIZE)
0875                 urb->interval = PERIODIC_SIZE;
0876             ep->period = urb->interval;
0877             ep->branch = PERIODIC_SIZE;
0878             if (type == PIPE_ISOCHRONOUS)
0879                 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
0880             ep->load = usb_calc_bus_time(udev->speed, !is_out,
0881                              type == PIPE_ISOCHRONOUS,
0882                              usb_maxpacket(udev, pipe))
0883                     / 1000;
0884             break;
0885         }
0886 
0887         ep->hep = hep;
0888         hep->hcpriv = ep;
0889     }
0890 
0891     /* maybe put endpoint into schedule */
0892     switch (type) {
0893     case PIPE_CONTROL:
0894     case PIPE_BULK:
0895         if (list_empty(&ep->schedule))
0896             list_add_tail(&ep->schedule, &sl811->async);
0897         break;
0898     case PIPE_ISOCHRONOUS:
0899     case PIPE_INTERRUPT:
0900         urb->interval = ep->period;
0901         if (ep->branch < PERIODIC_SIZE) {
0902             /* NOTE:  the phase is correct here, but the value
0903              * needs offsetting by the transfer queue depth.
0904              * All current drivers ignore start_frame, so this
0905              * is unlikely to ever matter...
0906              */
0907             urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
0908                         + ep->branch;
0909             break;
0910         }
0911 
0912         retval = balance(sl811, ep->period, ep->load);
0913         if (retval < 0)
0914             goto fail;
0915         ep->branch = retval;
0916         retval = 0;
0917         urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
0918                     + ep->branch;
0919 
0920         /* sort each schedule branch by period (slow before fast)
0921          * to share the faster parts of the tree without needing
0922          * dummy/placeholder nodes
0923          */
0924         dev_dbg(hcd->self.controller, "schedule qh%d/%p branch %d\n",
0925             ep->period, ep, ep->branch);
0926         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
0927             struct sl811h_ep    **prev = &sl811->periodic[i];
0928             struct sl811h_ep    *here = *prev;
0929 
0930             while (here && ep != here) {
0931                 if (ep->period > here->period)
0932                     break;
0933                 prev = &here->next;
0934                 here = *prev;
0935             }
0936             if (ep != here) {
0937                 ep->next = here;
0938                 *prev = ep;
0939             }
0940             sl811->load[i] += ep->load;
0941         }
0942         sl811->periodic_count++;
0943         hcd->self.bandwidth_allocated += ep->load / ep->period;
0944         sofirq_on(sl811);
0945     }
0946 
0947     urb->hcpriv = hep;
0948     start_transfer(sl811);
0949     sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
0950 fail:
0951     if (retval)
0952         usb_hcd_unlink_urb_from_ep(hcd, urb);
0953 fail_not_linked:
0954     spin_unlock_irqrestore(&sl811->lock, flags);
0955     return retval;
0956 }
0957 
0958 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
0959 {
0960     struct sl811        *sl811 = hcd_to_sl811(hcd);
0961     struct usb_host_endpoint *hep;
0962     unsigned long       flags;
0963     struct sl811h_ep    *ep;
0964     int         retval;
0965 
0966     spin_lock_irqsave(&sl811->lock, flags);
0967     retval = usb_hcd_check_unlink_urb(hcd, urb, status);
0968     if (retval)
0969         goto fail;
0970 
0971     hep = urb->hcpriv;
0972     ep = hep->hcpriv;
0973     if (ep) {
0974         /* finish right away if this urb can't be active ...
0975          * note that some drivers wrongly expect delays
0976          */
0977         if (ep->hep->urb_list.next != &urb->urb_list) {
0978             /* not front of queue?  never active */
0979 
0980         /* for active transfers, we expect an IRQ */
0981         } else if (sl811->active_a == ep) {
0982             if (time_before_eq(sl811->jiffies_a, jiffies)) {
0983                 /* happens a lot with lowspeed?? */
0984                 dev_dbg(hcd->self.controller,
0985                     "giveup on DONE_A: ctrl %02x sts %02x\n",
0986                     sl811_read(sl811,
0987                         SL811_EP_A(SL11H_HOSTCTLREG)),
0988                     sl811_read(sl811,
0989                         SL811_EP_A(SL11H_PKTSTATREG)));
0990                 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
0991                         0);
0992                 sl811->active_a = NULL;
0993             } else
0994                 urb = NULL;
0995 #ifdef  USE_B
0996         } else if (sl811->active_b == ep) {
0997             if (time_before_eq(sl811->jiffies_a, jiffies)) {
0998                 /* happens a lot with lowspeed?? */
0999                 dev_dbg(hcd->self.controller,
1000                     "giveup on DONE_B: ctrl %02x sts %02x\n",
1001                     sl811_read(sl811,
1002                         SL811_EP_B(SL11H_HOSTCTLREG)),
1003                     sl811_read(sl811,
1004                         SL811_EP_B(SL11H_PKTSTATREG)));
1005                 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1006                         0);
1007                 sl811->active_b = NULL;
1008             } else
1009                 urb = NULL;
1010 #endif
1011         } else {
1012             /* front of queue for inactive endpoint */
1013         }
1014 
1015         if (urb)
1016             finish_request(sl811, ep, urb, 0);
1017         else
1018             dev_dbg(sl811_to_hcd(sl811)->self.controller,
1019                 "dequeue, urb %p active %s; wait4irq\n", urb,
1020                 (sl811->active_a == ep) ? "A" : "B");
1021     } else
1022         retval = -EINVAL;
1023  fail:
1024     spin_unlock_irqrestore(&sl811->lock, flags);
1025     return retval;
1026 }
1027 
1028 static void
1029 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1030 {
1031     struct sl811h_ep    *ep = hep->hcpriv;
1032 
1033     if (!ep)
1034         return;
1035 
1036     /* assume we'd just wait for the irq */
1037     if (!list_empty(&hep->urb_list))
1038         msleep(3);
1039     if (!list_empty(&hep->urb_list))
1040         dev_warn(hcd->self.controller, "ep %p not empty?\n", ep);
1041 
1042     kfree(ep);
1043     hep->hcpriv = NULL;
1044 }
1045 
1046 static int
1047 sl811h_get_frame(struct usb_hcd *hcd)
1048 {
1049     struct sl811 *sl811 = hcd_to_sl811(hcd);
1050 
1051     /* wrong except while periodic transfers are scheduled;
1052      * never matches the on-the-wire frame;
1053      * subject to overruns.
1054      */
1055     return sl811->frame;
1056 }
1057 
1058 
1059 /*-------------------------------------------------------------------------*/
1060 
1061 /* the virtual root hub timer IRQ checks for hub status */
1062 static int
1063 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1064 {
1065     struct sl811 *sl811 = hcd_to_sl811(hcd);
1066 #ifdef  QUIRK3
1067     unsigned long flags;
1068 
1069     /* non-SMP HACK: use root hub timer as i/o watchdog
1070      * this seems essential when SOF IRQs aren't in use...
1071      */
1072     local_irq_save(flags);
1073     if (!timer_pending(&sl811->timer)) {
1074         if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
1075             sl811->stat_lost++;
1076     }
1077     local_irq_restore(flags);
1078 #endif
1079 
1080     if (!(sl811->port1 & (0xffff << 16)))
1081         return 0;
1082 
1083     /* tell hub_wq port 1 changed */
1084     *buf = (1 << 1);
1085     return 1;
1086 }
1087 
1088 static void
1089 sl811h_hub_descriptor (
1090     struct sl811            *sl811,
1091     struct usb_hub_descriptor   *desc
1092 ) {
1093     u16     temp = 0;
1094 
1095     desc->bDescriptorType = USB_DT_HUB;
1096     desc->bHubContrCurrent = 0;
1097 
1098     desc->bNbrPorts = 1;
1099     desc->bDescLength = 9;
1100 
1101     /* per-port power switching (gang of one!), or none */
1102     desc->bPwrOn2PwrGood = 0;
1103     if (sl811->board && sl811->board->port_power) {
1104         desc->bPwrOn2PwrGood = sl811->board->potpg;
1105         if (!desc->bPwrOn2PwrGood)
1106             desc->bPwrOn2PwrGood = 10;
1107         temp = HUB_CHAR_INDV_PORT_LPSM;
1108     } else
1109         temp = HUB_CHAR_NO_LPSM;
1110 
1111     /* no overcurrent errors detection/handling */
1112     temp |= HUB_CHAR_NO_OCPM;
1113 
1114     desc->wHubCharacteristics = cpu_to_le16(temp);
1115 
1116     /* ports removable, and legacy PortPwrCtrlMask */
1117     desc->u.hs.DeviceRemovable[0] = 0 << 1;
1118     desc->u.hs.DeviceRemovable[1] = ~0;
1119 }
1120 
1121 static void
1122 sl811h_timer(struct timer_list *t)
1123 {
1124     struct sl811    *sl811 = from_timer(sl811, t, timer);
1125     unsigned long   flags;
1126     u8      irqstat;
1127     u8      signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
1128     const u32   mask = USB_PORT_STAT_CONNECTION
1129                 | USB_PORT_STAT_ENABLE
1130                 | USB_PORT_STAT_LOW_SPEED;
1131 
1132     spin_lock_irqsave(&sl811->lock, flags);
1133 
1134     /* stop special signaling */
1135     sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1136     sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1137     udelay(3);
1138 
1139     irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1140 
1141     switch (signaling) {
1142     case SL11H_CTL1MASK_SE0:
1143         dev_dbg(sl811_to_hcd(sl811)->self.controller, "end reset\n");
1144         sl811->port1 = (USB_PORT_STAT_C_RESET << 16)
1145                  | USB_PORT_STAT_POWER;
1146         sl811->ctrl1 = 0;
1147         /* don't wrongly ack RD */
1148         if (irqstat & SL11H_INTMASK_INSRMV)
1149             irqstat &= ~SL11H_INTMASK_RD;
1150         break;
1151     case SL11H_CTL1MASK_K:
1152         dev_dbg(sl811_to_hcd(sl811)->self.controller, "end resume\n");
1153         sl811->port1 &= ~USB_PORT_STAT_SUSPEND;
1154         break;
1155     default:
1156         dev_dbg(sl811_to_hcd(sl811)->self.controller,
1157             "odd timer signaling: %02x\n", signaling);
1158         break;
1159     }
1160     sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1161 
1162     if (irqstat & SL11H_INTMASK_RD) {
1163         /* usbcore nukes all pending transactions on disconnect */
1164         if (sl811->port1 & USB_PORT_STAT_CONNECTION)
1165             sl811->port1 |= (USB_PORT_STAT_C_CONNECTION << 16)
1166                     | (USB_PORT_STAT_C_ENABLE << 16);
1167         sl811->port1 &= ~mask;
1168         sl811->irq_enable = SL11H_INTMASK_INSRMV;
1169     } else {
1170         sl811->port1 |= mask;
1171         if (irqstat & SL11H_INTMASK_DP)
1172             sl811->port1 &= ~USB_PORT_STAT_LOW_SPEED;
1173         sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1174     }
1175 
1176     if (sl811->port1 & USB_PORT_STAT_CONNECTION) {
1177         u8  ctrl2 = SL811HS_CTL2_INIT;
1178 
1179         sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1180 #ifdef USE_B
1181         sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1182 #endif
1183         if (sl811->port1 & USB_PORT_STAT_LOW_SPEED) {
1184             sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1185             ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1186         }
1187 
1188         /* start SOFs flowing, kickstarting with A registers */
1189         sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1190         sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1191         sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1192 
1193         /* autoincrementing */
1194         sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1195         writeb(SL_SOF, sl811->data_reg);
1196         writeb(0, sl811->data_reg);
1197         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1198                 SL11H_HCTLMASK_ARM);
1199 
1200         /* hub_wq provides debounce delay */
1201     } else {
1202         sl811->ctrl1 = 0;
1203     }
1204     sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1205 
1206     /* reenable irqs */
1207     sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1208     spin_unlock_irqrestore(&sl811->lock, flags);
1209 }
1210 
1211 static int
1212 sl811h_hub_control(
1213     struct usb_hcd  *hcd,
1214     u16     typeReq,
1215     u16     wValue,
1216     u16     wIndex,
1217     char        *buf,
1218     u16     wLength
1219 ) {
1220     struct sl811    *sl811 = hcd_to_sl811(hcd);
1221     int     retval = 0;
1222     unsigned long   flags;
1223 
1224     spin_lock_irqsave(&sl811->lock, flags);
1225 
1226     switch (typeReq) {
1227     case ClearHubFeature:
1228     case SetHubFeature:
1229         switch (wValue) {
1230         case C_HUB_OVER_CURRENT:
1231         case C_HUB_LOCAL_POWER:
1232             break;
1233         default:
1234             goto error;
1235         }
1236         break;
1237     case ClearPortFeature:
1238         if (wIndex != 1 || wLength != 0)
1239             goto error;
1240 
1241         switch (wValue) {
1242         case USB_PORT_FEAT_ENABLE:
1243             sl811->port1 &= USB_PORT_STAT_POWER;
1244             sl811->ctrl1 = 0;
1245             sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1246             sl811->irq_enable = SL11H_INTMASK_INSRMV;
1247             sl811_write(sl811, SL11H_IRQ_ENABLE,
1248                         sl811->irq_enable);
1249             break;
1250         case USB_PORT_FEAT_SUSPEND:
1251             if (!(sl811->port1 & USB_PORT_STAT_SUSPEND))
1252                 break;
1253 
1254             /* 20 msec of resume/K signaling, other irqs blocked */
1255             dev_dbg(hcd->self.controller, "start resume...\n");
1256             sl811->irq_enable = 0;
1257             sl811_write(sl811, SL11H_IRQ_ENABLE,
1258                         sl811->irq_enable);
1259             sl811->ctrl1 |= SL11H_CTL1MASK_K;
1260             sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1261 
1262             mod_timer(&sl811->timer, jiffies
1263                     + msecs_to_jiffies(USB_RESUME_TIMEOUT));
1264             break;
1265         case USB_PORT_FEAT_POWER:
1266             port_power(sl811, 0);
1267             break;
1268         case USB_PORT_FEAT_C_ENABLE:
1269         case USB_PORT_FEAT_C_SUSPEND:
1270         case USB_PORT_FEAT_C_CONNECTION:
1271         case USB_PORT_FEAT_C_OVER_CURRENT:
1272         case USB_PORT_FEAT_C_RESET:
1273             break;
1274         default:
1275             goto error;
1276         }
1277         sl811->port1 &= ~(1 << wValue);
1278         break;
1279     case GetHubDescriptor:
1280         sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1281         break;
1282     case GetHubStatus:
1283         put_unaligned_le32(0, buf);
1284         break;
1285     case GetPortStatus:
1286         if (wIndex != 1)
1287             goto error;
1288         put_unaligned_le32(sl811->port1, buf);
1289 
1290         if (__is_defined(VERBOSE) ||
1291             *(u16*)(buf+2)) /* only if wPortChange is interesting */
1292             dev_dbg(hcd->self.controller, "GetPortStatus %08x\n",
1293                 sl811->port1);
1294         break;
1295     case SetPortFeature:
1296         if (wIndex != 1 || wLength != 0)
1297             goto error;
1298         switch (wValue) {
1299         case USB_PORT_FEAT_SUSPEND:
1300             if (sl811->port1 & USB_PORT_STAT_RESET)
1301                 goto error;
1302             if (!(sl811->port1 & USB_PORT_STAT_ENABLE))
1303                 goto error;
1304 
1305             dev_dbg(hcd->self.controller,"suspend...\n");
1306             sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1307             sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1308             break;
1309         case USB_PORT_FEAT_POWER:
1310             port_power(sl811, 1);
1311             break;
1312         case USB_PORT_FEAT_RESET:
1313             if (sl811->port1 & USB_PORT_STAT_SUSPEND)
1314                 goto error;
1315             if (!(sl811->port1 & USB_PORT_STAT_POWER))
1316                 break;
1317 
1318             /* 50 msec of reset/SE0 signaling, irqs blocked */
1319             sl811->irq_enable = 0;
1320             sl811_write(sl811, SL11H_IRQ_ENABLE,
1321                         sl811->irq_enable);
1322             sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1323             sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1324             sl811->port1 |= USB_PORT_STAT_RESET;
1325             mod_timer(&sl811->timer, jiffies
1326                     + msecs_to_jiffies(50));
1327             break;
1328         default:
1329             goto error;
1330         }
1331         sl811->port1 |= 1 << wValue;
1332         break;
1333 
1334     default:
1335 error:
1336         /* "protocol stall" on error */
1337         retval = -EPIPE;
1338     }
1339 
1340     spin_unlock_irqrestore(&sl811->lock, flags);
1341     return retval;
1342 }
1343 
1344 #ifdef  CONFIG_PM
1345 
1346 static int
1347 sl811h_bus_suspend(struct usb_hcd *hcd)
1348 {
1349     // SOFs off
1350     dev_dbg(hcd->self.controller, "%s\n", __func__);
1351     return 0;
1352 }
1353 
1354 static int
1355 sl811h_bus_resume(struct usb_hcd *hcd)
1356 {
1357     // SOFs on
1358     dev_dbg(hcd->self.controller, "%s\n", __func__);
1359     return 0;
1360 }
1361 
1362 #else
1363 
1364 #define sl811h_bus_suspend  NULL
1365 #define sl811h_bus_resume   NULL
1366 
1367 #endif
1368 
1369 
1370 /*-------------------------------------------------------------------------*/
1371 
1372 static void dump_irq(struct seq_file *s, char *label, u8 mask)
1373 {
1374     seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1375         (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1376         (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1377         (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1378         (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1379         (mask & SL11H_INTMASK_RD) ? " rd" : "",
1380         (mask & SL11H_INTMASK_DP) ? " dp" : "");
1381 }
1382 
1383 static int sl811h_debug_show(struct seq_file *s, void *unused)
1384 {
1385     struct sl811        *sl811 = s->private;
1386     struct sl811h_ep    *ep;
1387     unsigned        i;
1388 
1389     seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1390         sl811_to_hcd(sl811)->product_desc,
1391         hcd_name, DRIVER_VERSION,
1392         sl811->port1);
1393 
1394     seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1395     seq_printf(s, "current session:  done_a %ld done_b %ld "
1396             "wake %ld sof %ld overrun %ld lost %ld\n\n",
1397         sl811->stat_a, sl811->stat_b,
1398         sl811->stat_wake, sl811->stat_sof,
1399         sl811->stat_overrun, sl811->stat_lost);
1400 
1401     spin_lock_irq(&sl811->lock);
1402 
1403     if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1404         seq_printf(s, "(suspended)\n\n");
1405     else {
1406         u8  t = sl811_read(sl811, SL11H_CTLREG1);
1407 
1408         seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1409             (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1410             ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1411             case SL11H_CTL1MASK_NORMAL: s = ""; break;
1412             case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1413             case SL11H_CTL1MASK_K: s = " k/resume"; break;
1414             default: s = "j"; break;
1415             } s; }),
1416             (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1417             (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1418 
1419         dump_irq(s, "irq_enable",
1420                 sl811_read(sl811, SL11H_IRQ_ENABLE));
1421         dump_irq(s, "irq_status",
1422                 sl811_read(sl811, SL11H_IRQ_STATUS));
1423         seq_printf(s, "frame clocks remaining:  %d\n",
1424                 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1425     }
1426 
1427     seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1428         sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1429         sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1430     seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1431         sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1432         sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1433     seq_printf(s, "\n");
1434     list_for_each_entry (ep, &sl811->async, schedule) {
1435         struct urb      *urb;
1436 
1437         seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1438                     " nak %d err %d\n",
1439             (ep == sl811->active_a) ? "(A) " : "",
1440             (ep == sl811->active_b) ? "(B) " : "",
1441             ep, ep->epnum,
1442             ({ char *s; switch (ep->nextpid) {
1443             case USB_PID_IN: s = "in"; break;
1444             case USB_PID_OUT: s = "out"; break;
1445             case USB_PID_SETUP: s = "setup"; break;
1446             case USB_PID_ACK: s = "status"; break;
1447             default: s = "?"; break;
1448             } s;}),
1449             ep->maxpacket,
1450             ep->nak_count, ep->error_count);
1451         list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1452             seq_printf(s, "  urb%p, %d/%d\n", urb,
1453                 urb->actual_length,
1454                 urb->transfer_buffer_length);
1455         }
1456     }
1457     if (!list_empty(&sl811->async))
1458         seq_printf(s, "\n");
1459 
1460     seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1461 
1462     for (i = 0; i < PERIODIC_SIZE; i++) {
1463         ep = sl811->periodic[i];
1464         if (!ep)
1465             continue;
1466         seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1467 
1468         /* DUMB: prints shared entries multiple times */
1469         do {
1470             seq_printf(s,
1471                 "   %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1472                     "err %d\n",
1473                 (ep == sl811->active_a) ? "(A) " : "",
1474                 (ep == sl811->active_b) ? "(B) " : "",
1475                 ep->period, ep,
1476                 (ep->udev->speed == USB_SPEED_FULL)
1477                     ? "" : "ls ",
1478                 ep->udev->devnum, ep->epnum,
1479                 (ep->epnum == 0) ? ""
1480                     : ((ep->nextpid == USB_PID_IN)
1481                         ? "in"
1482                         : "out"),
1483                 ep->maxpacket, ep->error_count);
1484             ep = ep->next;
1485         } while (ep);
1486     }
1487 
1488     spin_unlock_irq(&sl811->lock);
1489     seq_printf(s, "\n");
1490 
1491     return 0;
1492 }
1493 DEFINE_SHOW_ATTRIBUTE(sl811h_debug);
1494 
1495 /* expect just one sl811 per system */
1496 static void create_debug_file(struct sl811 *sl811)
1497 {
1498     debugfs_create_file("sl811h", S_IRUGO, usb_debug_root, sl811,
1499                 &sl811h_debug_fops);
1500 }
1501 
1502 static void remove_debug_file(struct sl811 *sl811)
1503 {
1504     debugfs_remove(debugfs_lookup("sl811h", usb_debug_root));
1505 }
1506 
1507 /*-------------------------------------------------------------------------*/
1508 
1509 static void
1510 sl811h_stop(struct usb_hcd *hcd)
1511 {
1512     struct sl811    *sl811 = hcd_to_sl811(hcd);
1513     unsigned long   flags;
1514 
1515     del_timer_sync(&hcd->rh_timer);
1516 
1517     spin_lock_irqsave(&sl811->lock, flags);
1518     port_power(sl811, 0);
1519     spin_unlock_irqrestore(&sl811->lock, flags);
1520 }
1521 
1522 static int
1523 sl811h_start(struct usb_hcd *hcd)
1524 {
1525     struct sl811        *sl811 = hcd_to_sl811(hcd);
1526 
1527     /* chip has been reset, VBUS power is off */
1528     hcd->state = HC_STATE_RUNNING;
1529 
1530     if (sl811->board) {
1531         if (!device_can_wakeup(hcd->self.controller))
1532             device_init_wakeup(hcd->self.controller,
1533                 sl811->board->can_wakeup);
1534         hcd->power_budget = sl811->board->power * 2;
1535     }
1536 
1537     /* enable power and interrupts */
1538     port_power(sl811, 1);
1539 
1540     return 0;
1541 }
1542 
1543 /*-------------------------------------------------------------------------*/
1544 
1545 static const struct hc_driver sl811h_hc_driver = {
1546     .description =      hcd_name,
1547     .hcd_priv_size =    sizeof(struct sl811),
1548 
1549     /*
1550      * generic hardware linkage
1551      */
1552     .irq =          sl811h_irq,
1553     .flags =        HCD_USB11 | HCD_MEMORY,
1554 
1555     /* Basic lifecycle operations */
1556     .start =        sl811h_start,
1557     .stop =         sl811h_stop,
1558 
1559     /*
1560      * managing i/o requests and associated device resources
1561      */
1562     .urb_enqueue =      sl811h_urb_enqueue,
1563     .urb_dequeue =      sl811h_urb_dequeue,
1564     .endpoint_disable = sl811h_endpoint_disable,
1565 
1566     /*
1567      * periodic schedule support
1568      */
1569     .get_frame_number = sl811h_get_frame,
1570 
1571     /*
1572      * root hub support
1573      */
1574     .hub_status_data =  sl811h_hub_status_data,
1575     .hub_control =      sl811h_hub_control,
1576     .bus_suspend =      sl811h_bus_suspend,
1577     .bus_resume =       sl811h_bus_resume,
1578 };
1579 
1580 /*-------------------------------------------------------------------------*/
1581 
1582 static int
1583 sl811h_remove(struct platform_device *dev)
1584 {
1585     struct usb_hcd      *hcd = platform_get_drvdata(dev);
1586     struct sl811        *sl811 = hcd_to_sl811(hcd);
1587     struct resource     *res;
1588 
1589     remove_debug_file(sl811);
1590     usb_remove_hcd(hcd);
1591 
1592     /* some platforms may use IORESOURCE_IO */
1593     res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1594     if (res)
1595         iounmap(sl811->data_reg);
1596 
1597     res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1598     if (res)
1599         iounmap(sl811->addr_reg);
1600 
1601     usb_put_hcd(hcd);
1602     return 0;
1603 }
1604 
1605 static int
1606 sl811h_probe(struct platform_device *dev)
1607 {
1608     struct usb_hcd      *hcd;
1609     struct sl811        *sl811;
1610     struct resource     *addr, *data, *ires;
1611     int         irq;
1612     void __iomem        *addr_reg;
1613     void __iomem        *data_reg;
1614     int         retval;
1615     u8          tmp, ioaddr;
1616     unsigned long       irqflags;
1617 
1618     if (usb_disabled())
1619         return -ENODEV;
1620 
1621     /* the chip may be wired for either kind of addressing */
1622     addr = platform_get_mem_or_io(dev, 0);
1623     data = platform_get_mem_or_io(dev, 1);
1624     if (!addr || !data || resource_type(addr) != resource_type(data))
1625         return -ENODEV;
1626 
1627     /* basic sanity checks first.  board-specific init logic should
1628      * have initialized these three resources and probably board
1629      * specific platform_data.  we don't probe for IRQs, and do only
1630      * minimal sanity checking.
1631      */
1632     ires = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1633     if (dev->num_resources < 3 || !ires)
1634         return -ENODEV;
1635 
1636     irq = ires->start;
1637     irqflags = ires->flags & IRQF_TRIGGER_MASK;
1638 
1639     ioaddr = resource_type(addr) == IORESOURCE_IO;
1640     if (ioaddr) {
1641         /*
1642          * NOTE: 64-bit resource->start is getting truncated
1643          * to avoid compiler warning, assuming that ->start
1644          * is always 32-bit for this case
1645          */
1646         addr_reg = (void __iomem *) (unsigned long) addr->start;
1647         data_reg = (void __iomem *) (unsigned long) data->start;
1648     } else {
1649         addr_reg = ioremap(addr->start, 1);
1650         if (addr_reg == NULL) {
1651             retval = -ENOMEM;
1652             goto err2;
1653         }
1654 
1655         data_reg = ioremap(data->start, 1);
1656         if (data_reg == NULL) {
1657             retval = -ENOMEM;
1658             goto err4;
1659         }
1660     }
1661 
1662     /* allocate and initialize hcd */
1663     hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
1664     if (!hcd) {
1665         retval = -ENOMEM;
1666         goto err5;
1667     }
1668     hcd->rsrc_start = addr->start;
1669     sl811 = hcd_to_sl811(hcd);
1670 
1671     spin_lock_init(&sl811->lock);
1672     INIT_LIST_HEAD(&sl811->async);
1673     sl811->board = dev_get_platdata(&dev->dev);
1674     timer_setup(&sl811->timer, sl811h_timer, 0);
1675     sl811->addr_reg = addr_reg;
1676     sl811->data_reg = data_reg;
1677 
1678     spin_lock_irq(&sl811->lock);
1679     port_power(sl811, 0);
1680     spin_unlock_irq(&sl811->lock);
1681     msleep(200);
1682 
1683     tmp = sl811_read(sl811, SL11H_HWREVREG);
1684     switch (tmp >> 4) {
1685     case 1:
1686         hcd->product_desc = "SL811HS v1.2";
1687         break;
1688     case 2:
1689         hcd->product_desc = "SL811HS v1.5";
1690         break;
1691     default:
1692         /* reject case 0, SL11S is less functional */
1693         dev_dbg(&dev->dev, "chiprev %02x\n", tmp);
1694         retval = -ENXIO;
1695         goto err6;
1696     }
1697 
1698     /* The chip's IRQ is level triggered, active high.  A requirement
1699      * for platform device setup is to cope with things like signal
1700      * inverters (e.g. CF is active low) or working only with edge
1701      * triggers (e.g. most ARM CPUs).  Initial driver stress testing
1702      * was on a system with single edge triggering, so most sorts of
1703      * triggering arrangement should work.
1704      *
1705      * Use resource IRQ flags if set by platform device setup.
1706      */
1707     irqflags |= IRQF_SHARED;
1708     retval = usb_add_hcd(hcd, irq, irqflags);
1709     if (retval != 0)
1710         goto err6;
1711 
1712     device_wakeup_enable(hcd->self.controller);
1713 
1714     create_debug_file(sl811);
1715     return retval;
1716 
1717  err6:
1718     usb_put_hcd(hcd);
1719  err5:
1720     if (!ioaddr)
1721         iounmap(data_reg);
1722  err4:
1723     if (!ioaddr)
1724         iounmap(addr_reg);
1725  err2:
1726     dev_dbg(&dev->dev, "init error, %d\n", retval);
1727     return retval;
1728 }
1729 
1730 #ifdef  CONFIG_PM
1731 
1732 /* for this device there's no useful distinction between the controller
1733  * and its root hub.
1734  */
1735 
1736 static int
1737 sl811h_suspend(struct platform_device *dev, pm_message_t state)
1738 {
1739     struct usb_hcd  *hcd = platform_get_drvdata(dev);
1740     struct sl811    *sl811 = hcd_to_sl811(hcd);
1741     int     retval = 0;
1742 
1743     switch (state.event) {
1744     case PM_EVENT_FREEZE:
1745         retval = sl811h_bus_suspend(hcd);
1746         break;
1747     case PM_EVENT_SUSPEND:
1748     case PM_EVENT_HIBERNATE:
1749     case PM_EVENT_PRETHAW:      /* explicitly discard hw state */
1750         port_power(sl811, 0);
1751         break;
1752     }
1753     return retval;
1754 }
1755 
1756 static int
1757 sl811h_resume(struct platform_device *dev)
1758 {
1759     struct usb_hcd  *hcd = platform_get_drvdata(dev);
1760     struct sl811    *sl811 = hcd_to_sl811(hcd);
1761 
1762     /* with no "check to see if VBUS is still powered" board hook,
1763      * let's assume it'd only be powered to enable remote wakeup.
1764      */
1765     if (!sl811->port1 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
1766         sl811->port1 = 0;
1767         port_power(sl811, 1);
1768         usb_root_hub_lost_power(hcd->self.root_hub);
1769         return 0;
1770     }
1771 
1772     return sl811h_bus_resume(hcd);
1773 }
1774 
1775 #else
1776 
1777 #define sl811h_suspend  NULL
1778 #define sl811h_resume   NULL
1779 
1780 #endif
1781 
1782 
1783 /* this driver is exported so sl811_cs can depend on it */
1784 struct platform_driver sl811h_driver = {
1785     .probe =    sl811h_probe,
1786     .remove =   sl811h_remove,
1787 
1788     .suspend =  sl811h_suspend,
1789     .resume =   sl811h_resume,
1790     .driver = {
1791         .name = hcd_name,
1792     },
1793 };
1794 EXPORT_SYMBOL(sl811h_driver);
1795 
1796 module_platform_driver(sl811h_driver);