Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for the NXP ISP1761 device controller
0004  *
0005  * Copyright 2021 Linaro, Rui Miguel Silva
0006  * Copyright 2014 Ideas on Board Oy
0007  *
0008  * Contacts:
0009  *  Laurent Pinchart <laurent.pinchart@ideasonboard.com>
0010  *  Rui Miguel Silva <rui.silva@linaro.org>
0011  */
0012 
0013 #include <linux/interrupt.h>
0014 #include <linux/io.h>
0015 #include <linux/kernel.h>
0016 #include <linux/list.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/timer.h>
0020 #include <linux/usb.h>
0021 
0022 #include "isp1760-core.h"
0023 #include "isp1760-regs.h"
0024 #include "isp1760-udc.h"
0025 
0026 #define ISP1760_VBUS_POLL_INTERVAL  msecs_to_jiffies(500)
0027 
0028 struct isp1760_request {
0029     struct usb_request req;
0030     struct list_head queue;
0031     struct isp1760_ep *ep;
0032     unsigned int packet_size;
0033 };
0034 
0035 static inline struct isp1760_udc *gadget_to_udc(struct usb_gadget *gadget)
0036 {
0037     return container_of(gadget, struct isp1760_udc, gadget);
0038 }
0039 
0040 static inline struct isp1760_ep *ep_to_udc_ep(struct usb_ep *ep)
0041 {
0042     return container_of(ep, struct isp1760_ep, ep);
0043 }
0044 
0045 static inline struct isp1760_request *req_to_udc_req(struct usb_request *req)
0046 {
0047     return container_of(req, struct isp1760_request, req);
0048 }
0049 
0050 static u32 isp1760_udc_read(struct isp1760_udc *udc, u16 field)
0051 {
0052     return isp1760_field_read(udc->fields, field);
0053 }
0054 
0055 static void isp1760_udc_write(struct isp1760_udc *udc, u16 field, u32 val)
0056 {
0057     isp1760_field_write(udc->fields, field, val);
0058 }
0059 
0060 static u32 isp1760_udc_read_raw(struct isp1760_udc *udc, u16 reg)
0061 {
0062     __le32 val;
0063 
0064     regmap_raw_read(udc->regs, reg, &val, 4);
0065 
0066     return le32_to_cpu(val);
0067 }
0068 
0069 static u16 isp1760_udc_read_raw16(struct isp1760_udc *udc, u16 reg)
0070 {
0071     __le16 val;
0072 
0073     regmap_raw_read(udc->regs, reg, &val, 2);
0074 
0075     return le16_to_cpu(val);
0076 }
0077 
0078 static void isp1760_udc_write_raw(struct isp1760_udc *udc, u16 reg, u32 val)
0079 {
0080     __le32 val_le = cpu_to_le32(val);
0081 
0082     regmap_raw_write(udc->regs, reg, &val_le, 4);
0083 }
0084 
0085 static void isp1760_udc_write_raw16(struct isp1760_udc *udc, u16 reg, u16 val)
0086 {
0087     __le16 val_le = cpu_to_le16(val);
0088 
0089     regmap_raw_write(udc->regs, reg, &val_le, 2);
0090 }
0091 
0092 static void isp1760_udc_set(struct isp1760_udc *udc, u32 field)
0093 {
0094     isp1760_udc_write(udc, field, 0xFFFFFFFF);
0095 }
0096 
0097 static void isp1760_udc_clear(struct isp1760_udc *udc, u32 field)
0098 {
0099     isp1760_udc_write(udc, field, 0);
0100 }
0101 
0102 static bool isp1760_udc_is_set(struct isp1760_udc *udc, u32 field)
0103 {
0104     return !!isp1760_udc_read(udc, field);
0105 }
0106 /* -----------------------------------------------------------------------------
0107  * Endpoint Management
0108  */
0109 
0110 static struct isp1760_ep *isp1760_udc_find_ep(struct isp1760_udc *udc,
0111                           u16 index)
0112 {
0113     unsigned int i;
0114 
0115     if (index == 0)
0116         return &udc->ep[0];
0117 
0118     for (i = 1; i < ARRAY_SIZE(udc->ep); ++i) {
0119         if (udc->ep[i].addr == index)
0120             return udc->ep[i].desc ? &udc->ep[i] : NULL;
0121     }
0122 
0123     return NULL;
0124 }
0125 
0126 static void __isp1760_udc_select_ep(struct isp1760_udc *udc,
0127                     struct isp1760_ep *ep, int dir)
0128 {
0129     isp1760_udc_write(udc, DC_ENDPIDX, ep->addr & USB_ENDPOINT_NUMBER_MASK);
0130 
0131     if (dir == USB_DIR_IN)
0132         isp1760_udc_set(udc, DC_EPDIR);
0133     else
0134         isp1760_udc_clear(udc, DC_EPDIR);
0135 }
0136 
0137 /**
0138  * isp1760_udc_select_ep - Select an endpoint for register access
0139  * @ep: The endpoint
0140  * @udc: Reference to the device controller
0141  *
0142  * The ISP1761 endpoint registers are banked. This function selects the target
0143  * endpoint for banked register access. The selection remains valid until the
0144  * next call to this function, the next direct access to the EPINDEX register
0145  * or the next reset, whichever comes first.
0146  *
0147  * Called with the UDC spinlock held.
0148  */
0149 static void isp1760_udc_select_ep(struct isp1760_udc *udc,
0150                   struct isp1760_ep *ep)
0151 {
0152     __isp1760_udc_select_ep(udc, ep, ep->addr & USB_ENDPOINT_DIR_MASK);
0153 }
0154 
0155 /* Called with the UDC spinlock held. */
0156 static void isp1760_udc_ctrl_send_status(struct isp1760_ep *ep, int dir)
0157 {
0158     struct isp1760_udc *udc = ep->udc;
0159 
0160     /*
0161      * Proceed to the status stage. The status stage data packet flows in
0162      * the direction opposite to the data stage data packets, we thus need
0163      * to select the OUT/IN endpoint for IN/OUT transfers.
0164      */
0165     if (dir == USB_DIR_IN)
0166         isp1760_udc_clear(udc, DC_EPDIR);
0167     else
0168         isp1760_udc_set(udc, DC_EPDIR);
0169 
0170     isp1760_udc_write(udc, DC_ENDPIDX, 1);
0171     isp1760_udc_set(udc, DC_STATUS);
0172 
0173     /*
0174      * The hardware will terminate the request automatically and go back to
0175      * the setup stage without notifying us.
0176      */
0177     udc->ep0_state = ISP1760_CTRL_SETUP;
0178 }
0179 
0180 /* Called without the UDC spinlock held. */
0181 static void isp1760_udc_request_complete(struct isp1760_ep *ep,
0182                      struct isp1760_request *req,
0183                      int status)
0184 {
0185     struct isp1760_udc *udc = ep->udc;
0186     unsigned long flags;
0187 
0188     dev_dbg(ep->udc->isp->dev, "completing request %p with status %d\n",
0189         req, status);
0190 
0191     req->ep = NULL;
0192     req->req.status = status;
0193     req->req.complete(&ep->ep, &req->req);
0194 
0195     spin_lock_irqsave(&udc->lock, flags);
0196 
0197     /*
0198      * When completing control OUT requests, move to the status stage after
0199      * calling the request complete callback. This gives the gadget an
0200      * opportunity to stall the control transfer if needed.
0201      */
0202     if (status == 0 && ep->addr == 0 && udc->ep0_dir == USB_DIR_OUT)
0203         isp1760_udc_ctrl_send_status(ep, USB_DIR_OUT);
0204 
0205     spin_unlock_irqrestore(&udc->lock, flags);
0206 }
0207 
0208 static void isp1760_udc_ctrl_send_stall(struct isp1760_ep *ep)
0209 {
0210     struct isp1760_udc *udc = ep->udc;
0211     unsigned long flags;
0212 
0213     dev_dbg(ep->udc->isp->dev, "%s(ep%02x)\n", __func__, ep->addr);
0214 
0215     spin_lock_irqsave(&udc->lock, flags);
0216 
0217     /* Stall both the IN and OUT endpoints. */
0218     __isp1760_udc_select_ep(udc, ep, USB_DIR_OUT);
0219     isp1760_udc_set(udc, DC_STALL);
0220     __isp1760_udc_select_ep(udc, ep, USB_DIR_IN);
0221     isp1760_udc_set(udc, DC_STALL);
0222 
0223     /* A protocol stall completes the control transaction. */
0224     udc->ep0_state = ISP1760_CTRL_SETUP;
0225 
0226     spin_unlock_irqrestore(&udc->lock, flags);
0227 }
0228 
0229 /* -----------------------------------------------------------------------------
0230  * Data Endpoints
0231  */
0232 
0233 /* Called with the UDC spinlock held. */
0234 static bool isp1760_udc_receive(struct isp1760_ep *ep,
0235                 struct isp1760_request *req)
0236 {
0237     struct isp1760_udc *udc = ep->udc;
0238     unsigned int len;
0239     u32 *buf;
0240     int i;
0241 
0242     isp1760_udc_select_ep(udc, ep);
0243     len = isp1760_udc_read(udc, DC_BUFLEN);
0244 
0245     dev_dbg(udc->isp->dev, "%s: received %u bytes (%u/%u done)\n",
0246         __func__, len, req->req.actual, req->req.length);
0247 
0248     len = min(len, req->req.length - req->req.actual);
0249 
0250     if (!len) {
0251         /*
0252          * There's no data to be read from the FIFO, acknowledge the RX
0253          * interrupt by clearing the buffer.
0254          *
0255          * TODO: What if another packet arrives in the meantime ? The
0256          * datasheet doesn't clearly document how this should be
0257          * handled.
0258          */
0259         isp1760_udc_set(udc, DC_CLBUF);
0260         return false;
0261     }
0262 
0263     buf = req->req.buf + req->req.actual;
0264 
0265     /*
0266      * Make sure not to read more than one extra byte, otherwise data from
0267      * the next packet might be removed from the FIFO.
0268      */
0269     for (i = len; i > 2; i -= 4, ++buf)
0270         *buf = isp1760_udc_read_raw(udc, ISP176x_DC_DATAPORT);
0271     if (i > 0)
0272         *(u16 *)buf = isp1760_udc_read_raw16(udc, ISP176x_DC_DATAPORT);
0273 
0274     req->req.actual += len;
0275 
0276     /*
0277      * TODO: The short_not_ok flag isn't supported yet, but isn't used by
0278      * any gadget driver either.
0279      */
0280 
0281     dev_dbg(udc->isp->dev,
0282         "%s: req %p actual/length %u/%u maxpacket %u packet size %u\n",
0283         __func__, req, req->req.actual, req->req.length, ep->maxpacket,
0284         len);
0285 
0286     ep->rx_pending = false;
0287 
0288     /*
0289      * Complete the request if all data has been received or if a short
0290      * packet has been received.
0291      */
0292     if (req->req.actual == req->req.length || len < ep->maxpacket) {
0293         list_del(&req->queue);
0294         return true;
0295     }
0296 
0297     return false;
0298 }
0299 
0300 static void isp1760_udc_transmit(struct isp1760_ep *ep,
0301                  struct isp1760_request *req)
0302 {
0303     struct isp1760_udc *udc = ep->udc;
0304     u32 *buf = req->req.buf + req->req.actual;
0305     int i;
0306 
0307     req->packet_size = min(req->req.length - req->req.actual,
0308                    ep->maxpacket);
0309 
0310     dev_dbg(udc->isp->dev, "%s: transferring %u bytes (%u/%u done)\n",
0311         __func__, req->packet_size, req->req.actual,
0312         req->req.length);
0313 
0314     __isp1760_udc_select_ep(udc, ep, USB_DIR_IN);
0315 
0316     if (req->packet_size)
0317         isp1760_udc_write(udc, DC_BUFLEN, req->packet_size);
0318 
0319     /*
0320      * Make sure not to write more than one extra byte, otherwise extra data
0321      * will stay in the FIFO and will be transmitted during the next control
0322      * request. The endpoint control CLBUF bit is supposed to allow flushing
0323      * the FIFO for this kind of conditions, but doesn't seem to work.
0324      */
0325     for (i = req->packet_size; i > 2; i -= 4, ++buf)
0326         isp1760_udc_write_raw(udc, ISP176x_DC_DATAPORT, *buf);
0327     if (i > 0)
0328         isp1760_udc_write_raw16(udc, ISP176x_DC_DATAPORT, *(u16 *)buf);
0329 
0330     if (ep->addr == 0)
0331         isp1760_udc_set(udc, DC_DSEN);
0332     if (!req->packet_size)
0333         isp1760_udc_set(udc, DC_VENDP);
0334 }
0335 
0336 static void isp1760_ep_rx_ready(struct isp1760_ep *ep)
0337 {
0338     struct isp1760_udc *udc = ep->udc;
0339     struct isp1760_request *req;
0340     bool complete;
0341 
0342     spin_lock(&udc->lock);
0343 
0344     if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_OUT) {
0345         spin_unlock(&udc->lock);
0346         dev_dbg(udc->isp->dev, "%s: invalid ep0 state %u\n", __func__,
0347             udc->ep0_state);
0348         return;
0349     }
0350 
0351     if (ep->addr != 0 && !ep->desc) {
0352         spin_unlock(&udc->lock);
0353         dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
0354             ep->addr);
0355         return;
0356     }
0357 
0358     if (list_empty(&ep->queue)) {
0359         ep->rx_pending = true;
0360         spin_unlock(&udc->lock);
0361         dev_dbg(udc->isp->dev, "%s: ep%02x (%p) has no request queued\n",
0362             __func__, ep->addr, ep);
0363         return;
0364     }
0365 
0366     req = list_first_entry(&ep->queue, struct isp1760_request,
0367                    queue);
0368     complete = isp1760_udc_receive(ep, req);
0369 
0370     spin_unlock(&udc->lock);
0371 
0372     if (complete)
0373         isp1760_udc_request_complete(ep, req, 0);
0374 }
0375 
0376 static void isp1760_ep_tx_complete(struct isp1760_ep *ep)
0377 {
0378     struct isp1760_udc *udc = ep->udc;
0379     struct isp1760_request *complete = NULL;
0380     struct isp1760_request *req;
0381     bool need_zlp;
0382 
0383     spin_lock(&udc->lock);
0384 
0385     if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_IN) {
0386         spin_unlock(&udc->lock);
0387         dev_dbg(udc->isp->dev, "TX IRQ: invalid endpoint state %u\n",
0388             udc->ep0_state);
0389         return;
0390     }
0391 
0392     if (list_empty(&ep->queue)) {
0393         /*
0394          * This can happen for the control endpoint when the reply to
0395          * the GET_STATUS IN control request is sent directly by the
0396          * setup IRQ handler. Just proceed to the status stage.
0397          */
0398         if (ep->addr == 0) {
0399             isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
0400             spin_unlock(&udc->lock);
0401             return;
0402         }
0403 
0404         spin_unlock(&udc->lock);
0405         dev_dbg(udc->isp->dev, "%s: ep%02x has no request queued\n",
0406             __func__, ep->addr);
0407         return;
0408     }
0409 
0410     req = list_first_entry(&ep->queue, struct isp1760_request,
0411                    queue);
0412     req->req.actual += req->packet_size;
0413 
0414     need_zlp = req->req.actual == req->req.length &&
0415            !(req->req.length % ep->maxpacket) &&
0416            req->packet_size && req->req.zero;
0417 
0418     dev_dbg(udc->isp->dev,
0419         "TX IRQ: req %p actual/length %u/%u maxpacket %u packet size %u zero %u need zlp %u\n",
0420          req, req->req.actual, req->req.length, ep->maxpacket,
0421          req->packet_size, req->req.zero, need_zlp);
0422 
0423     /*
0424      * Complete the request if all data has been sent and we don't need to
0425      * transmit a zero length packet.
0426      */
0427     if (req->req.actual == req->req.length && !need_zlp) {
0428         complete = req;
0429         list_del(&req->queue);
0430 
0431         if (ep->addr == 0)
0432             isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
0433 
0434         if (!list_empty(&ep->queue))
0435             req = list_first_entry(&ep->queue,
0436                            struct isp1760_request, queue);
0437         else
0438             req = NULL;
0439     }
0440 
0441     /*
0442      * Transmit the next packet or start the next request, if any.
0443      *
0444      * TODO: If the endpoint is stalled the next request shouldn't be
0445      * started, but what about the next packet ?
0446      */
0447     if (req)
0448         isp1760_udc_transmit(ep, req);
0449 
0450     spin_unlock(&udc->lock);
0451 
0452     if (complete)
0453         isp1760_udc_request_complete(ep, complete, 0);
0454 }
0455 
0456 static int __isp1760_udc_set_halt(struct isp1760_ep *ep, bool halt)
0457 {
0458     struct isp1760_udc *udc = ep->udc;
0459 
0460     dev_dbg(udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
0461         halt ? "set" : "clear", ep->addr);
0462 
0463     if (ep->desc && usb_endpoint_xfer_isoc(ep->desc)) {
0464         dev_dbg(udc->isp->dev, "%s: ep%02x is isochronous\n", __func__,
0465             ep->addr);
0466         return -EINVAL;
0467     }
0468 
0469     isp1760_udc_select_ep(udc, ep);
0470 
0471     if (halt)
0472         isp1760_udc_set(udc, DC_STALL);
0473     else
0474         isp1760_udc_clear(udc, DC_STALL);
0475 
0476     if (ep->addr == 0) {
0477         /* When halting the control endpoint, stall both IN and OUT. */
0478         __isp1760_udc_select_ep(udc, ep, USB_DIR_IN);
0479         if (halt)
0480             isp1760_udc_set(udc, DC_STALL);
0481         else
0482             isp1760_udc_clear(udc, DC_STALL);
0483     } else if (!halt) {
0484         /* Reset the data PID by cycling the endpoint enable bit. */
0485         isp1760_udc_clear(udc, DC_EPENABLE);
0486         isp1760_udc_set(udc, DC_EPENABLE);
0487 
0488         /*
0489          * Disabling the endpoint emptied the transmit FIFO, fill it
0490          * again if a request is pending.
0491          *
0492          * TODO: Does the gadget framework require synchronizatino with
0493          * the TX IRQ handler ?
0494          */
0495         if ((ep->addr & USB_DIR_IN) && !list_empty(&ep->queue)) {
0496             struct isp1760_request *req;
0497 
0498             req = list_first_entry(&ep->queue,
0499                            struct isp1760_request, queue);
0500             isp1760_udc_transmit(ep, req);
0501         }
0502     }
0503 
0504     ep->halted = halt;
0505 
0506     return 0;
0507 }
0508 
0509 /* -----------------------------------------------------------------------------
0510  * Control Endpoint
0511  */
0512 
0513 static int isp1760_udc_get_status(struct isp1760_udc *udc,
0514                   const struct usb_ctrlrequest *req)
0515 {
0516     struct isp1760_ep *ep;
0517     u16 status;
0518 
0519     if (req->wLength != cpu_to_le16(2) || req->wValue != cpu_to_le16(0))
0520         return -EINVAL;
0521 
0522     switch (req->bRequestType) {
0523     case USB_DIR_IN | USB_RECIP_DEVICE:
0524         status = udc->devstatus;
0525         break;
0526 
0527     case USB_DIR_IN | USB_RECIP_INTERFACE:
0528         status = 0;
0529         break;
0530 
0531     case USB_DIR_IN | USB_RECIP_ENDPOINT:
0532         ep = isp1760_udc_find_ep(udc, le16_to_cpu(req->wIndex));
0533         if (!ep)
0534             return -EINVAL;
0535 
0536         status = 0;
0537         if (ep->halted)
0538             status |= 1 << USB_ENDPOINT_HALT;
0539         break;
0540 
0541     default:
0542         return -EINVAL;
0543     }
0544 
0545     isp1760_udc_set(udc, DC_EPDIR);
0546     isp1760_udc_write(udc, DC_ENDPIDX, 1);
0547 
0548     isp1760_udc_write(udc, DC_BUFLEN, 2);
0549 
0550     isp1760_udc_write_raw16(udc, ISP176x_DC_DATAPORT, status);
0551 
0552     isp1760_udc_set(udc, DC_DSEN);
0553 
0554     dev_dbg(udc->isp->dev, "%s: status 0x%04x\n", __func__, status);
0555 
0556     return 0;
0557 }
0558 
0559 static int isp1760_udc_set_address(struct isp1760_udc *udc, u16 addr)
0560 {
0561     if (addr > 127) {
0562         dev_dbg(udc->isp->dev, "invalid device address %u\n", addr);
0563         return -EINVAL;
0564     }
0565 
0566     if (udc->gadget.state != USB_STATE_DEFAULT &&
0567         udc->gadget.state != USB_STATE_ADDRESS) {
0568         dev_dbg(udc->isp->dev, "can't set address in state %u\n",
0569             udc->gadget.state);
0570         return -EINVAL;
0571     }
0572 
0573     usb_gadget_set_state(&udc->gadget, addr ? USB_STATE_ADDRESS :
0574                  USB_STATE_DEFAULT);
0575 
0576     isp1760_udc_write(udc, DC_DEVADDR, addr);
0577     isp1760_udc_set(udc, DC_DEVEN);
0578 
0579     spin_lock(&udc->lock);
0580     isp1760_udc_ctrl_send_status(&udc->ep[0], USB_DIR_OUT);
0581     spin_unlock(&udc->lock);
0582 
0583     return 0;
0584 }
0585 
0586 static bool isp1760_ep0_setup_standard(struct isp1760_udc *udc,
0587                        struct usb_ctrlrequest *req)
0588 {
0589     bool stall;
0590 
0591     switch (req->bRequest) {
0592     case USB_REQ_GET_STATUS:
0593         return isp1760_udc_get_status(udc, req);
0594 
0595     case USB_REQ_CLEAR_FEATURE:
0596         switch (req->bRequestType) {
0597         case USB_DIR_OUT | USB_RECIP_DEVICE: {
0598             /* TODO: Handle remote wakeup feature. */
0599             return true;
0600         }
0601 
0602         case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
0603             u16 index = le16_to_cpu(req->wIndex);
0604             struct isp1760_ep *ep;
0605 
0606             if (req->wLength != cpu_to_le16(0) ||
0607                 req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
0608                 return true;
0609 
0610             ep = isp1760_udc_find_ep(udc, index);
0611             if (!ep)
0612                 return true;
0613 
0614             spin_lock(&udc->lock);
0615 
0616             /*
0617              * If the endpoint is wedged only the gadget can clear
0618              * the halt feature. Pretend success in that case, but
0619              * keep the endpoint halted.
0620              */
0621             if (!ep->wedged)
0622                 stall = __isp1760_udc_set_halt(ep, false);
0623             else
0624                 stall = false;
0625 
0626             if (!stall)
0627                 isp1760_udc_ctrl_send_status(&udc->ep[0],
0628                                  USB_DIR_OUT);
0629 
0630             spin_unlock(&udc->lock);
0631             return stall;
0632         }
0633 
0634         default:
0635             return true;
0636         }
0637         break;
0638 
0639     case USB_REQ_SET_FEATURE:
0640         switch (req->bRequestType) {
0641         case USB_DIR_OUT | USB_RECIP_DEVICE: {
0642             /* TODO: Handle remote wakeup and test mode features */
0643             return true;
0644         }
0645 
0646         case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
0647             u16 index = le16_to_cpu(req->wIndex);
0648             struct isp1760_ep *ep;
0649 
0650             if (req->wLength != cpu_to_le16(0) ||
0651                 req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
0652                 return true;
0653 
0654             ep = isp1760_udc_find_ep(udc, index);
0655             if (!ep)
0656                 return true;
0657 
0658             spin_lock(&udc->lock);
0659 
0660             stall = __isp1760_udc_set_halt(ep, true);
0661             if (!stall)
0662                 isp1760_udc_ctrl_send_status(&udc->ep[0],
0663                                  USB_DIR_OUT);
0664 
0665             spin_unlock(&udc->lock);
0666             return stall;
0667         }
0668 
0669         default:
0670             return true;
0671         }
0672         break;
0673 
0674     case USB_REQ_SET_ADDRESS:
0675         if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
0676             return true;
0677 
0678         return isp1760_udc_set_address(udc, le16_to_cpu(req->wValue));
0679 
0680     case USB_REQ_SET_CONFIGURATION:
0681         if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
0682             return true;
0683 
0684         if (udc->gadget.state != USB_STATE_ADDRESS &&
0685             udc->gadget.state != USB_STATE_CONFIGURED)
0686             return true;
0687 
0688         stall = udc->driver->setup(&udc->gadget, req) < 0;
0689         if (stall)
0690             return true;
0691 
0692         usb_gadget_set_state(&udc->gadget, req->wValue ?
0693                      USB_STATE_CONFIGURED : USB_STATE_ADDRESS);
0694 
0695         /*
0696          * SET_CONFIGURATION (and SET_INTERFACE) must reset the halt
0697          * feature on all endpoints. There is however no need to do so
0698          * explicitly here as the gadget driver will disable and
0699          * reenable endpoints, clearing the halt feature.
0700          */
0701         return false;
0702 
0703     default:
0704         return udc->driver->setup(&udc->gadget, req) < 0;
0705     }
0706 }
0707 
0708 static void isp1760_ep0_setup(struct isp1760_udc *udc)
0709 {
0710     union {
0711         struct usb_ctrlrequest r;
0712         u32 data[2];
0713     } req;
0714     unsigned int count;
0715     bool stall = false;
0716 
0717     spin_lock(&udc->lock);
0718 
0719     isp1760_udc_set(udc, DC_EP0SETUP);
0720 
0721     count = isp1760_udc_read(udc, DC_BUFLEN);
0722     if (count != sizeof(req)) {
0723         spin_unlock(&udc->lock);
0724 
0725         dev_err(udc->isp->dev, "invalid length %u for setup packet\n",
0726             count);
0727 
0728         isp1760_udc_ctrl_send_stall(&udc->ep[0]);
0729         return;
0730     }
0731 
0732     req.data[0] = isp1760_udc_read_raw(udc, ISP176x_DC_DATAPORT);
0733     req.data[1] = isp1760_udc_read_raw(udc, ISP176x_DC_DATAPORT);
0734 
0735     if (udc->ep0_state != ISP1760_CTRL_SETUP) {
0736         spin_unlock(&udc->lock);
0737         dev_dbg(udc->isp->dev, "unexpected SETUP packet\n");
0738         return;
0739     }
0740 
0741     /* Move to the data stage. */
0742     if (!req.r.wLength)
0743         udc->ep0_state = ISP1760_CTRL_STATUS;
0744     else if (req.r.bRequestType & USB_DIR_IN)
0745         udc->ep0_state = ISP1760_CTRL_DATA_IN;
0746     else
0747         udc->ep0_state = ISP1760_CTRL_DATA_OUT;
0748 
0749     udc->ep0_dir = req.r.bRequestType & USB_DIR_IN;
0750     udc->ep0_length = le16_to_cpu(req.r.wLength);
0751 
0752     spin_unlock(&udc->lock);
0753 
0754     dev_dbg(udc->isp->dev,
0755         "%s: bRequestType 0x%02x bRequest 0x%02x wValue 0x%04x wIndex 0x%04x wLength 0x%04x\n",
0756         __func__, req.r.bRequestType, req.r.bRequest,
0757         le16_to_cpu(req.r.wValue), le16_to_cpu(req.r.wIndex),
0758         le16_to_cpu(req.r.wLength));
0759 
0760     if ((req.r.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
0761         stall = isp1760_ep0_setup_standard(udc, &req.r);
0762     else
0763         stall = udc->driver->setup(&udc->gadget, &req.r) < 0;
0764 
0765     if (stall)
0766         isp1760_udc_ctrl_send_stall(&udc->ep[0]);
0767 }
0768 
0769 /* -----------------------------------------------------------------------------
0770  * Gadget Endpoint Operations
0771  */
0772 
0773 static int isp1760_ep_enable(struct usb_ep *ep,
0774                  const struct usb_endpoint_descriptor *desc)
0775 {
0776     struct isp1760_ep *uep = ep_to_udc_ep(ep);
0777     struct isp1760_udc *udc = uep->udc;
0778     unsigned long flags;
0779     unsigned int type;
0780 
0781     dev_dbg(uep->udc->isp->dev, "%s\n", __func__);
0782 
0783     /*
0784      * Validate the descriptor. The control endpoint can't be enabled
0785      * manually.
0786      */
0787     if (desc->bDescriptorType != USB_DT_ENDPOINT ||
0788         desc->bEndpointAddress == 0 ||
0789         desc->bEndpointAddress != uep->addr ||
0790         le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket) {
0791         dev_dbg(udc->isp->dev,
0792             "%s: invalid descriptor type %u addr %02x ep addr %02x max packet size %u/%u\n",
0793             __func__, desc->bDescriptorType,
0794             desc->bEndpointAddress, uep->addr,
0795             le16_to_cpu(desc->wMaxPacketSize), ep->maxpacket);
0796         return -EINVAL;
0797     }
0798 
0799     switch (usb_endpoint_type(desc)) {
0800     case USB_ENDPOINT_XFER_ISOC:
0801         type = ISP176x_DC_ENDPTYP_ISOC;
0802         break;
0803     case USB_ENDPOINT_XFER_BULK:
0804         type = ISP176x_DC_ENDPTYP_BULK;
0805         break;
0806     case USB_ENDPOINT_XFER_INT:
0807         type = ISP176x_DC_ENDPTYP_INTERRUPT;
0808         break;
0809     case USB_ENDPOINT_XFER_CONTROL:
0810     default:
0811         dev_dbg(udc->isp->dev, "%s: control endpoints unsupported\n",
0812             __func__);
0813         return -EINVAL;
0814     }
0815 
0816     spin_lock_irqsave(&udc->lock, flags);
0817 
0818     uep->desc = desc;
0819     uep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
0820     uep->rx_pending = false;
0821     uep->halted = false;
0822     uep->wedged = false;
0823 
0824     isp1760_udc_select_ep(udc, uep);
0825 
0826     isp1760_udc_write(udc, DC_FFOSZ, uep->maxpacket);
0827     isp1760_udc_write(udc, DC_BUFLEN, uep->maxpacket);
0828 
0829     isp1760_udc_write(udc, DC_ENDPTYP, type);
0830     isp1760_udc_set(udc, DC_EPENABLE);
0831 
0832     spin_unlock_irqrestore(&udc->lock, flags);
0833 
0834     return 0;
0835 }
0836 
0837 static int isp1760_ep_disable(struct usb_ep *ep)
0838 {
0839     struct isp1760_ep *uep = ep_to_udc_ep(ep);
0840     struct isp1760_udc *udc = uep->udc;
0841     struct isp1760_request *req, *nreq;
0842     LIST_HEAD(req_list);
0843     unsigned long flags;
0844 
0845     dev_dbg(udc->isp->dev, "%s\n", __func__);
0846 
0847     spin_lock_irqsave(&udc->lock, flags);
0848 
0849     if (!uep->desc) {
0850         dev_dbg(udc->isp->dev, "%s: endpoint not enabled\n", __func__);
0851         spin_unlock_irqrestore(&udc->lock, flags);
0852         return -EINVAL;
0853     }
0854 
0855     uep->desc = NULL;
0856     uep->maxpacket = 0;
0857 
0858     isp1760_udc_select_ep(udc, uep);
0859     isp1760_udc_clear(udc, DC_EPENABLE);
0860     isp1760_udc_clear(udc, DC_ENDPTYP);
0861 
0862     /* TODO Synchronize with the IRQ handler */
0863 
0864     list_splice_init(&uep->queue, &req_list);
0865 
0866     spin_unlock_irqrestore(&udc->lock, flags);
0867 
0868     list_for_each_entry_safe(req, nreq, &req_list, queue) {
0869         list_del(&req->queue);
0870         isp1760_udc_request_complete(uep, req, -ESHUTDOWN);
0871     }
0872 
0873     return 0;
0874 }
0875 
0876 static struct usb_request *isp1760_ep_alloc_request(struct usb_ep *ep,
0877                             gfp_t gfp_flags)
0878 {
0879     struct isp1760_request *req;
0880 
0881     req = kzalloc(sizeof(*req), gfp_flags);
0882     if (!req)
0883         return NULL;
0884 
0885     return &req->req;
0886 }
0887 
0888 static void isp1760_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
0889 {
0890     struct isp1760_request *req = req_to_udc_req(_req);
0891 
0892     kfree(req);
0893 }
0894 
0895 static int isp1760_ep_queue(struct usb_ep *ep, struct usb_request *_req,
0896                 gfp_t gfp_flags)
0897 {
0898     struct isp1760_request *req = req_to_udc_req(_req);
0899     struct isp1760_ep *uep = ep_to_udc_ep(ep);
0900     struct isp1760_udc *udc = uep->udc;
0901     bool complete = false;
0902     unsigned long flags;
0903     int ret = 0;
0904 
0905     _req->status = -EINPROGRESS;
0906     _req->actual = 0;
0907 
0908     spin_lock_irqsave(&udc->lock, flags);
0909 
0910     dev_dbg(udc->isp->dev,
0911         "%s: req %p (%u bytes%s) ep %p(0x%02x)\n", __func__, _req,
0912         _req->length, _req->zero ? " (zlp)" : "", uep, uep->addr);
0913 
0914     req->ep = uep;
0915 
0916     if (uep->addr == 0) {
0917         if (_req->length != udc->ep0_length &&
0918             udc->ep0_state != ISP1760_CTRL_DATA_IN) {
0919             dev_dbg(udc->isp->dev,
0920                 "%s: invalid length %u for req %p\n",
0921                 __func__, _req->length, req);
0922             ret = -EINVAL;
0923             goto done;
0924         }
0925 
0926         switch (udc->ep0_state) {
0927         case ISP1760_CTRL_DATA_IN:
0928             dev_dbg(udc->isp->dev, "%s: transmitting req %p\n",
0929                 __func__, req);
0930 
0931             list_add_tail(&req->queue, &uep->queue);
0932             isp1760_udc_transmit(uep, req);
0933             break;
0934 
0935         case ISP1760_CTRL_DATA_OUT:
0936             list_add_tail(&req->queue, &uep->queue);
0937             __isp1760_udc_select_ep(udc, uep, USB_DIR_OUT);
0938             isp1760_udc_set(udc, DC_DSEN);
0939             break;
0940 
0941         case ISP1760_CTRL_STATUS:
0942             complete = true;
0943             break;
0944 
0945         default:
0946             dev_dbg(udc->isp->dev, "%s: invalid ep0 state\n",
0947                 __func__);
0948             ret = -EINVAL;
0949             break;
0950         }
0951     } else if (uep->desc) {
0952         bool empty = list_empty(&uep->queue);
0953 
0954         list_add_tail(&req->queue, &uep->queue);
0955         if ((uep->addr & USB_DIR_IN) && !uep->halted && empty)
0956             isp1760_udc_transmit(uep, req);
0957         else if (!(uep->addr & USB_DIR_IN) && uep->rx_pending)
0958             complete = isp1760_udc_receive(uep, req);
0959     } else {
0960         dev_dbg(udc->isp->dev,
0961             "%s: can't queue request to disabled ep%02x\n",
0962             __func__, uep->addr);
0963         ret = -ESHUTDOWN;
0964     }
0965 
0966 done:
0967     if (ret < 0)
0968         req->ep = NULL;
0969 
0970     spin_unlock_irqrestore(&udc->lock, flags);
0971 
0972     if (complete)
0973         isp1760_udc_request_complete(uep, req, 0);
0974 
0975     return ret;
0976 }
0977 
0978 static int isp1760_ep_dequeue(struct usb_ep *ep, struct usb_request *_req)
0979 {
0980     struct isp1760_request *req = req_to_udc_req(_req);
0981     struct isp1760_ep *uep = ep_to_udc_ep(ep);
0982     struct isp1760_udc *udc = uep->udc;
0983     unsigned long flags;
0984 
0985     dev_dbg(uep->udc->isp->dev, "%s(ep%02x)\n", __func__, uep->addr);
0986 
0987     spin_lock_irqsave(&udc->lock, flags);
0988 
0989     if (req->ep != uep)
0990         req = NULL;
0991     else
0992         list_del(&req->queue);
0993 
0994     spin_unlock_irqrestore(&udc->lock, flags);
0995 
0996     if (!req)
0997         return -EINVAL;
0998 
0999     isp1760_udc_request_complete(uep, req, -ECONNRESET);
1000     return 0;
1001 }
1002 
1003 static int __isp1760_ep_set_halt(struct isp1760_ep *uep, bool stall, bool wedge)
1004 {
1005     struct isp1760_udc *udc = uep->udc;
1006     int ret;
1007 
1008     if (!uep->addr) {
1009         /*
1010          * Halting the control endpoint is only valid as a delayed error
1011          * response to a SETUP packet. Make sure EP0 is in the right
1012          * stage and that the gadget isn't trying to clear the halt
1013          * condition.
1014          */
1015         if (WARN_ON(udc->ep0_state == ISP1760_CTRL_SETUP || !stall ||
1016                  wedge)) {
1017             return -EINVAL;
1018         }
1019     }
1020 
1021     if (uep->addr && !uep->desc) {
1022         dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
1023             uep->addr);
1024         return -EINVAL;
1025     }
1026 
1027     if (uep->addr & USB_DIR_IN) {
1028         /* Refuse to halt IN endpoints with active transfers. */
1029         if (!list_empty(&uep->queue)) {
1030             dev_dbg(udc->isp->dev,
1031                 "%s: ep%02x has request pending\n", __func__,
1032                 uep->addr);
1033             return -EAGAIN;
1034         }
1035     }
1036 
1037     ret = __isp1760_udc_set_halt(uep, stall);
1038     if (ret < 0)
1039         return ret;
1040 
1041     if (!uep->addr) {
1042         /*
1043          * Stalling EP0 completes the control transaction, move back to
1044          * the SETUP state.
1045          */
1046         udc->ep0_state = ISP1760_CTRL_SETUP;
1047         return 0;
1048     }
1049 
1050     if (wedge)
1051         uep->wedged = true;
1052     else if (!stall)
1053         uep->wedged = false;
1054 
1055     return 0;
1056 }
1057 
1058 static int isp1760_ep_set_halt(struct usb_ep *ep, int value)
1059 {
1060     struct isp1760_ep *uep = ep_to_udc_ep(ep);
1061     unsigned long flags;
1062     int ret;
1063 
1064     dev_dbg(uep->udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
1065         value ? "set" : "clear", uep->addr);
1066 
1067     spin_lock_irqsave(&uep->udc->lock, flags);
1068     ret = __isp1760_ep_set_halt(uep, value, false);
1069     spin_unlock_irqrestore(&uep->udc->lock, flags);
1070 
1071     return ret;
1072 }
1073 
1074 static int isp1760_ep_set_wedge(struct usb_ep *ep)
1075 {
1076     struct isp1760_ep *uep = ep_to_udc_ep(ep);
1077     unsigned long flags;
1078     int ret;
1079 
1080     dev_dbg(uep->udc->isp->dev, "%s: set wedge on ep%02x)\n", __func__,
1081         uep->addr);
1082 
1083     spin_lock_irqsave(&uep->udc->lock, flags);
1084     ret = __isp1760_ep_set_halt(uep, true, true);
1085     spin_unlock_irqrestore(&uep->udc->lock, flags);
1086 
1087     return ret;
1088 }
1089 
1090 static void isp1760_ep_fifo_flush(struct usb_ep *ep)
1091 {
1092     struct isp1760_ep *uep = ep_to_udc_ep(ep);
1093     struct isp1760_udc *udc = uep->udc;
1094     unsigned long flags;
1095 
1096     spin_lock_irqsave(&udc->lock, flags);
1097 
1098     isp1760_udc_select_ep(udc, uep);
1099 
1100     /*
1101      * Set the CLBUF bit twice to flush both buffers in case double
1102      * buffering is enabled.
1103      */
1104     isp1760_udc_set(udc, DC_CLBUF);
1105     isp1760_udc_set(udc, DC_CLBUF);
1106 
1107     spin_unlock_irqrestore(&udc->lock, flags);
1108 }
1109 
1110 static const struct usb_ep_ops isp1760_ep_ops = {
1111     .enable = isp1760_ep_enable,
1112     .disable = isp1760_ep_disable,
1113     .alloc_request = isp1760_ep_alloc_request,
1114     .free_request = isp1760_ep_free_request,
1115     .queue = isp1760_ep_queue,
1116     .dequeue = isp1760_ep_dequeue,
1117     .set_halt = isp1760_ep_set_halt,
1118     .set_wedge = isp1760_ep_set_wedge,
1119     .fifo_flush = isp1760_ep_fifo_flush,
1120 };
1121 
1122 /* -----------------------------------------------------------------------------
1123  * Device States
1124  */
1125 
1126 /* Called with the UDC spinlock held. */
1127 static void isp1760_udc_connect(struct isp1760_udc *udc)
1128 {
1129     usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
1130     mod_timer(&udc->vbus_timer, jiffies + ISP1760_VBUS_POLL_INTERVAL);
1131 }
1132 
1133 /* Called with the UDC spinlock held. */
1134 static void isp1760_udc_disconnect(struct isp1760_udc *udc)
1135 {
1136     if (udc->gadget.state < USB_STATE_POWERED)
1137         return;
1138 
1139     dev_dbg(udc->isp->dev, "Device disconnected in state %u\n",
1140          udc->gadget.state);
1141 
1142     udc->gadget.speed = USB_SPEED_UNKNOWN;
1143     usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
1144 
1145     if (udc->driver->disconnect)
1146         udc->driver->disconnect(&udc->gadget);
1147 
1148     del_timer(&udc->vbus_timer);
1149 
1150     /* TODO Reset all endpoints ? */
1151 }
1152 
1153 static void isp1760_udc_init_hw(struct isp1760_udc *udc)
1154 {
1155     u32 intconf = udc->is_isp1763 ? ISP1763_DC_INTCONF : ISP176x_DC_INTCONF;
1156     u32 intena = udc->is_isp1763 ? ISP1763_DC_INTENABLE :
1157                         ISP176x_DC_INTENABLE;
1158 
1159     /*
1160      * The device controller currently shares its interrupt with the host
1161      * controller, the DC_IRQ polarity and signaling mode are ignored. Set
1162      * the to active-low level-triggered.
1163      *
1164      * Configure the control, in and out pipes to generate interrupts on
1165      * ACK tokens only (and NYET for the out pipe). The default
1166      * configuration also generates an interrupt on the first NACK token.
1167      */
1168     isp1760_reg_write(udc->regs, intconf,
1169               ISP176x_DC_CDBGMOD_ACK | ISP176x_DC_DDBGMODIN_ACK |
1170               ISP176x_DC_DDBGMODOUT_ACK);
1171 
1172     isp1760_reg_write(udc->regs, intena, DC_IEPRXTX(7) |
1173               DC_IEPRXTX(6) | DC_IEPRXTX(5) | DC_IEPRXTX(4) |
1174               DC_IEPRXTX(3) | DC_IEPRXTX(2) | DC_IEPRXTX(1) |
1175               DC_IEPRXTX(0) | ISP176x_DC_IEP0SETUP |
1176               ISP176x_DC_IEVBUS | ISP176x_DC_IERESM |
1177               ISP176x_DC_IESUSP | ISP176x_DC_IEHS_STA |
1178               ISP176x_DC_IEBRST);
1179 
1180     if (udc->connected)
1181         isp1760_set_pullup(udc->isp, true);
1182 
1183     isp1760_udc_set(udc, DC_DEVEN);
1184 }
1185 
1186 static void isp1760_udc_reset(struct isp1760_udc *udc)
1187 {
1188     unsigned long flags;
1189 
1190     spin_lock_irqsave(&udc->lock, flags);
1191 
1192     /*
1193      * The bus reset has reset most registers to their default value,
1194      * reinitialize the UDC hardware.
1195      */
1196     isp1760_udc_init_hw(udc);
1197 
1198     udc->ep0_state = ISP1760_CTRL_SETUP;
1199     udc->gadget.speed = USB_SPEED_FULL;
1200 
1201     usb_gadget_udc_reset(&udc->gadget, udc->driver);
1202 
1203     spin_unlock_irqrestore(&udc->lock, flags);
1204 }
1205 
1206 static void isp1760_udc_suspend(struct isp1760_udc *udc)
1207 {
1208     if (udc->gadget.state < USB_STATE_DEFAULT)
1209         return;
1210 
1211     if (udc->driver->suspend)
1212         udc->driver->suspend(&udc->gadget);
1213 }
1214 
1215 static void isp1760_udc_resume(struct isp1760_udc *udc)
1216 {
1217     if (udc->gadget.state < USB_STATE_DEFAULT)
1218         return;
1219 
1220     if (udc->driver->resume)
1221         udc->driver->resume(&udc->gadget);
1222 }
1223 
1224 /* -----------------------------------------------------------------------------
1225  * Gadget Operations
1226  */
1227 
1228 static int isp1760_udc_get_frame(struct usb_gadget *gadget)
1229 {
1230     struct isp1760_udc *udc = gadget_to_udc(gadget);
1231 
1232     return isp1760_udc_read(udc, DC_FRAMENUM);
1233 }
1234 
1235 static int isp1760_udc_wakeup(struct usb_gadget *gadget)
1236 {
1237     struct isp1760_udc *udc = gadget_to_udc(gadget);
1238 
1239     dev_dbg(udc->isp->dev, "%s\n", __func__);
1240     return -ENOTSUPP;
1241 }
1242 
1243 static int isp1760_udc_set_selfpowered(struct usb_gadget *gadget,
1244                        int is_selfpowered)
1245 {
1246     struct isp1760_udc *udc = gadget_to_udc(gadget);
1247 
1248     if (is_selfpowered)
1249         udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
1250     else
1251         udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1252 
1253     return 0;
1254 }
1255 
1256 static int isp1760_udc_pullup(struct usb_gadget *gadget, int is_on)
1257 {
1258     struct isp1760_udc *udc = gadget_to_udc(gadget);
1259 
1260     isp1760_set_pullup(udc->isp, is_on);
1261     udc->connected = is_on;
1262 
1263     return 0;
1264 }
1265 
1266 static int isp1760_udc_start(struct usb_gadget *gadget,
1267                  struct usb_gadget_driver *driver)
1268 {
1269     struct isp1760_udc *udc = gadget_to_udc(gadget);
1270     unsigned long flags;
1271 
1272     /* The hardware doesn't support low speed. */
1273     if (driver->max_speed < USB_SPEED_FULL) {
1274         dev_err(udc->isp->dev, "Invalid gadget driver\n");
1275         return -EINVAL;
1276     }
1277 
1278     spin_lock_irqsave(&udc->lock, flags);
1279 
1280     if (udc->driver) {
1281         dev_err(udc->isp->dev, "UDC already has a gadget driver\n");
1282         spin_unlock_irqrestore(&udc->lock, flags);
1283         return -EBUSY;
1284     }
1285 
1286     udc->driver = driver;
1287 
1288     spin_unlock_irqrestore(&udc->lock, flags);
1289 
1290     dev_dbg(udc->isp->dev, "starting UDC with driver %s\n",
1291         driver->function);
1292 
1293     udc->devstatus = 0;
1294     udc->connected = true;
1295 
1296     usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
1297 
1298     /* DMA isn't supported yet, don't enable the DMA clock. */
1299     isp1760_udc_set(udc, DC_GLINTENA);
1300 
1301     isp1760_udc_init_hw(udc);
1302 
1303     dev_dbg(udc->isp->dev, "UDC started with driver %s\n",
1304         driver->function);
1305 
1306     return 0;
1307 }
1308 
1309 static int isp1760_udc_stop(struct usb_gadget *gadget)
1310 {
1311     struct isp1760_udc *udc = gadget_to_udc(gadget);
1312     u32 mode_reg = udc->is_isp1763 ? ISP1763_DC_MODE : ISP176x_DC_MODE;
1313     unsigned long flags;
1314 
1315     dev_dbg(udc->isp->dev, "%s\n", __func__);
1316 
1317     del_timer_sync(&udc->vbus_timer);
1318 
1319     isp1760_reg_write(udc->regs, mode_reg, 0);
1320 
1321     spin_lock_irqsave(&udc->lock, flags);
1322     udc->driver = NULL;
1323     spin_unlock_irqrestore(&udc->lock, flags);
1324 
1325     return 0;
1326 }
1327 
1328 static const struct usb_gadget_ops isp1760_udc_ops = {
1329     .get_frame = isp1760_udc_get_frame,
1330     .wakeup = isp1760_udc_wakeup,
1331     .set_selfpowered = isp1760_udc_set_selfpowered,
1332     .pullup = isp1760_udc_pullup,
1333     .udc_start = isp1760_udc_start,
1334     .udc_stop = isp1760_udc_stop,
1335 };
1336 
1337 /* -----------------------------------------------------------------------------
1338  * Interrupt Handling
1339  */
1340 
1341 static u32 isp1760_udc_irq_get_status(struct isp1760_udc *udc)
1342 {
1343     u32 status;
1344 
1345     if (udc->is_isp1763) {
1346         status = isp1760_reg_read(udc->regs, ISP1763_DC_INTERRUPT)
1347             & isp1760_reg_read(udc->regs, ISP1763_DC_INTENABLE);
1348         isp1760_reg_write(udc->regs, ISP1763_DC_INTERRUPT, status);
1349     } else {
1350         status = isp1760_reg_read(udc->regs, ISP176x_DC_INTERRUPT)
1351             & isp1760_reg_read(udc->regs, ISP176x_DC_INTENABLE);
1352         isp1760_reg_write(udc->regs, ISP176x_DC_INTERRUPT, status);
1353     }
1354 
1355     return status;
1356 }
1357 
1358 static irqreturn_t isp1760_udc_irq(int irq, void *dev)
1359 {
1360     struct isp1760_udc *udc = dev;
1361     unsigned int i;
1362     u32 status;
1363 
1364     status = isp1760_udc_irq_get_status(udc);
1365 
1366     if (status & ISP176x_DC_IEVBUS) {
1367         dev_dbg(udc->isp->dev, "%s(VBUS)\n", __func__);
1368         /* The VBUS interrupt is only triggered when VBUS appears. */
1369         spin_lock(&udc->lock);
1370         isp1760_udc_connect(udc);
1371         spin_unlock(&udc->lock);
1372     }
1373 
1374     if (status & ISP176x_DC_IEBRST) {
1375         dev_dbg(udc->isp->dev, "%s(BRST)\n", __func__);
1376 
1377         isp1760_udc_reset(udc);
1378     }
1379 
1380     for (i = 0; i <= 7; ++i) {
1381         struct isp1760_ep *ep = &udc->ep[i*2];
1382 
1383         if (status & DC_IEPTX(i)) {
1384             dev_dbg(udc->isp->dev, "%s(EPTX%u)\n", __func__, i);
1385             isp1760_ep_tx_complete(ep);
1386         }
1387 
1388         if (status & DC_IEPRX(i)) {
1389             dev_dbg(udc->isp->dev, "%s(EPRX%u)\n", __func__, i);
1390             isp1760_ep_rx_ready(i ? ep - 1 : ep);
1391         }
1392     }
1393 
1394     if (status & ISP176x_DC_IEP0SETUP) {
1395         dev_dbg(udc->isp->dev, "%s(EP0SETUP)\n", __func__);
1396 
1397         isp1760_ep0_setup(udc);
1398     }
1399 
1400     if (status & ISP176x_DC_IERESM) {
1401         dev_dbg(udc->isp->dev, "%s(RESM)\n", __func__);
1402         isp1760_udc_resume(udc);
1403     }
1404 
1405     if (status & ISP176x_DC_IESUSP) {
1406         dev_dbg(udc->isp->dev, "%s(SUSP)\n", __func__);
1407 
1408         spin_lock(&udc->lock);
1409         if (!isp1760_udc_is_set(udc, DC_VBUSSTAT))
1410             isp1760_udc_disconnect(udc);
1411         else
1412             isp1760_udc_suspend(udc);
1413         spin_unlock(&udc->lock);
1414     }
1415 
1416     if (status & ISP176x_DC_IEHS_STA) {
1417         dev_dbg(udc->isp->dev, "%s(HS_STA)\n", __func__);
1418         udc->gadget.speed = USB_SPEED_HIGH;
1419     }
1420 
1421     return status ? IRQ_HANDLED : IRQ_NONE;
1422 }
1423 
1424 static void isp1760_udc_vbus_poll(struct timer_list *t)
1425 {
1426     struct isp1760_udc *udc = from_timer(udc, t, vbus_timer);
1427     unsigned long flags;
1428 
1429     spin_lock_irqsave(&udc->lock, flags);
1430 
1431     if (!(isp1760_udc_is_set(udc, DC_VBUSSTAT)))
1432         isp1760_udc_disconnect(udc);
1433     else if (udc->gadget.state >= USB_STATE_POWERED)
1434         mod_timer(&udc->vbus_timer,
1435               jiffies + ISP1760_VBUS_POLL_INTERVAL);
1436 
1437     spin_unlock_irqrestore(&udc->lock, flags);
1438 }
1439 
1440 /* -----------------------------------------------------------------------------
1441  * Registration
1442  */
1443 
1444 static void isp1760_udc_init_eps(struct isp1760_udc *udc)
1445 {
1446     unsigned int i;
1447 
1448     INIT_LIST_HEAD(&udc->gadget.ep_list);
1449 
1450     for (i = 0; i < ARRAY_SIZE(udc->ep); ++i) {
1451         struct isp1760_ep *ep = &udc->ep[i];
1452         unsigned int ep_num = (i + 1) / 2;
1453         bool is_in = !(i & 1);
1454 
1455         ep->udc = udc;
1456 
1457         INIT_LIST_HEAD(&ep->queue);
1458 
1459         ep->addr = (ep_num && is_in ? USB_DIR_IN : USB_DIR_OUT)
1460              | ep_num;
1461         ep->desc = NULL;
1462 
1463         sprintf(ep->name, "ep%u%s", ep_num,
1464             ep_num ? (is_in ? "in" : "out") : "");
1465 
1466         ep->ep.ops = &isp1760_ep_ops;
1467         ep->ep.name = ep->name;
1468 
1469         /*
1470          * Hardcode the maximum packet sizes for now, to 64 bytes for
1471          * the control endpoint and 512 bytes for all other endpoints.
1472          * This fits in the 8kB FIFO without double-buffering.
1473          */
1474         if (ep_num == 0) {
1475             usb_ep_set_maxpacket_limit(&ep->ep, 64);
1476             ep->ep.caps.type_control = true;
1477             ep->ep.caps.dir_in = true;
1478             ep->ep.caps.dir_out = true;
1479             ep->maxpacket = 64;
1480             udc->gadget.ep0 = &ep->ep;
1481         } else {
1482             usb_ep_set_maxpacket_limit(&ep->ep, 512);
1483             ep->ep.caps.type_iso = true;
1484             ep->ep.caps.type_bulk = true;
1485             ep->ep.caps.type_int = true;
1486             ep->maxpacket = 0;
1487             list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1488         }
1489 
1490         if (is_in)
1491             ep->ep.caps.dir_in = true;
1492         else
1493             ep->ep.caps.dir_out = true;
1494     }
1495 }
1496 
1497 static int isp1760_udc_init(struct isp1760_udc *udc)
1498 {
1499     u32 mode_reg = udc->is_isp1763 ? ISP1763_DC_MODE : ISP176x_DC_MODE;
1500     u16 scratch;
1501     u32 chipid;
1502 
1503     /*
1504      * Check that the controller is present by writing to the scratch
1505      * register, modifying the bus pattern by reading from the chip ID
1506      * register, and reading the scratch register value back. The chip ID
1507      * and scratch register contents must match the expected values.
1508      */
1509     isp1760_udc_write(udc, DC_SCRATCH, 0xbabe);
1510     chipid = isp1760_udc_read(udc, DC_CHIP_ID_HIGH) << 16;
1511     chipid |= isp1760_udc_read(udc, DC_CHIP_ID_LOW);
1512     scratch = isp1760_udc_read(udc, DC_SCRATCH);
1513 
1514     if (scratch != 0xbabe) {
1515         dev_err(udc->isp->dev,
1516             "udc: scratch test failed (0x%04x/0x%08x)\n",
1517             scratch, chipid);
1518         return -ENODEV;
1519     }
1520 
1521     if (chipid != 0x00011582 && chipid != 0x00158210 &&
1522         chipid != 0x00176320) {
1523         dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid);
1524         return -ENODEV;
1525     }
1526 
1527     /* Reset the device controller. */
1528     isp1760_udc_set(udc, DC_SFRESET);
1529     usleep_range(10000, 11000);
1530     isp1760_reg_write(udc->regs, mode_reg, 0);
1531     usleep_range(10000, 11000);
1532 
1533     return 0;
1534 }
1535 
1536 int isp1760_udc_register(struct isp1760_device *isp, int irq,
1537              unsigned long irqflags)
1538 {
1539     struct isp1760_udc *udc = &isp->udc;
1540     int ret;
1541 
1542     udc->irq = -1;
1543     udc->isp = isp;
1544 
1545     spin_lock_init(&udc->lock);
1546     timer_setup(&udc->vbus_timer, isp1760_udc_vbus_poll, 0);
1547 
1548     ret = isp1760_udc_init(udc);
1549     if (ret < 0)
1550         return ret;
1551 
1552     udc->irqname = kasprintf(GFP_KERNEL, "%s (udc)", dev_name(isp->dev));
1553     if (!udc->irqname)
1554         return -ENOMEM;
1555 
1556     ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | irqflags,
1557               udc->irqname, udc);
1558     if (ret < 0)
1559         goto error;
1560 
1561     udc->irq = irq;
1562 
1563     /*
1564      * Initialize the gadget static fields and register its device. Gadget
1565      * fields that vary during the life time of the gadget are initialized
1566      * by the UDC core.
1567      */
1568     udc->gadget.ops = &isp1760_udc_ops;
1569     udc->gadget.speed = USB_SPEED_UNKNOWN;
1570     udc->gadget.max_speed = USB_SPEED_HIGH;
1571     udc->gadget.name = "isp1761_udc";
1572 
1573     isp1760_udc_init_eps(udc);
1574 
1575     ret = usb_add_gadget_udc(isp->dev, &udc->gadget);
1576     if (ret < 0)
1577         goto error;
1578 
1579     return 0;
1580 
1581 error:
1582     if (udc->irq >= 0)
1583         free_irq(udc->irq, udc);
1584     kfree(udc->irqname);
1585 
1586     return ret;
1587 }
1588 
1589 void isp1760_udc_unregister(struct isp1760_device *isp)
1590 {
1591     struct isp1760_udc *udc = &isp->udc;
1592 
1593     if (!udc->isp)
1594         return;
1595 
1596     usb_del_gadget_udc(&udc->gadget);
1597 
1598     free_irq(udc->irq, udc);
1599     kfree(udc->irqname);
1600 }