Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-1.0+
0002 /*
0003  * Renesas USB driver
0004  *
0005  * Copyright (C) 2011 Renesas Solutions Corp.
0006  * Copyright (C) 2019 Renesas Electronics Corporation
0007  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
0008  */
0009 #include <linux/delay.h>
0010 #include <linux/dma-mapping.h>
0011 #include <linux/io.h>
0012 #include <linux/module.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/usb/ch9.h>
0015 #include <linux/usb/gadget.h>
0016 #include <linux/usb/otg.h>
0017 #include "common.h"
0018 
0019 /*
0020  *      struct
0021  */
0022 struct usbhsg_request {
0023     struct usb_request  req;
0024     struct usbhs_pkt    pkt;
0025 };
0026 
0027 #define EP_NAME_SIZE 8
0028 struct usbhsg_gpriv;
0029 struct usbhsg_uep {
0030     struct usb_ep        ep;
0031     struct usbhs_pipe   *pipe;
0032     spinlock_t      lock;   /* protect the pipe */
0033 
0034     char ep_name[EP_NAME_SIZE];
0035 
0036     struct usbhsg_gpriv *gpriv;
0037 };
0038 
0039 struct usbhsg_gpriv {
0040     struct usb_gadget    gadget;
0041     struct usbhs_mod     mod;
0042 
0043     struct usbhsg_uep   *uep;
0044     int          uep_size;
0045 
0046     struct usb_gadget_driver    *driver;
0047     struct usb_phy      *transceiver;
0048     bool             vbus_active;
0049 
0050     u32 status;
0051 #define USBHSG_STATUS_STARTED       (1 << 0)
0052 #define USBHSG_STATUS_REGISTERD     (1 << 1)
0053 #define USBHSG_STATUS_WEDGE     (1 << 2)
0054 #define USBHSG_STATUS_SELF_POWERED  (1 << 3)
0055 #define USBHSG_STATUS_SOFT_CONNECT  (1 << 4)
0056 };
0057 
0058 struct usbhsg_recip_handle {
0059     char *name;
0060     int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
0061               struct usb_ctrlrequest *ctrl);
0062     int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
0063              struct usb_ctrlrequest *ctrl);
0064     int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
0065             struct usb_ctrlrequest *ctrl);
0066 };
0067 
0068 /*
0069  *      macro
0070  */
0071 #define usbhsg_priv_to_gpriv(priv)          \
0072     container_of(                   \
0073         usbhs_mod_get(priv, USBHS_GADGET),  \
0074         struct usbhsg_gpriv, mod)
0075 
0076 #define __usbhsg_for_each_uep(start, pos, g, i) \
0077     for ((i) = start;                   \
0078          ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
0079          (i)++)
0080 
0081 #define usbhsg_for_each_uep(pos, gpriv, i)  \
0082     __usbhsg_for_each_uep(1, pos, gpriv, i)
0083 
0084 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
0085     __usbhsg_for_each_uep(0, pos, gpriv, i)
0086 
0087 #define usbhsg_gadget_to_gpriv(g)\
0088     container_of(g, struct usbhsg_gpriv, gadget)
0089 
0090 #define usbhsg_req_to_ureq(r)\
0091     container_of(r, struct usbhsg_request, req)
0092 
0093 #define usbhsg_ep_to_uep(e)     container_of(e, struct usbhsg_uep, ep)
0094 #define usbhsg_gpriv_to_dev(gp)     usbhs_priv_to_dev((gp)->mod.priv)
0095 #define usbhsg_gpriv_to_priv(gp)    ((gp)->mod.priv)
0096 #define usbhsg_gpriv_to_dcp(gp)     ((gp)->uep)
0097 #define usbhsg_gpriv_to_nth_uep(gp, i)  ((gp)->uep + i)
0098 #define usbhsg_uep_to_gpriv(u)      ((u)->gpriv)
0099 #define usbhsg_uep_to_pipe(u)       ((u)->pipe)
0100 #define usbhsg_pipe_to_uep(p)       ((p)->mod_private)
0101 #define usbhsg_is_dcp(u)        ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
0102 
0103 #define usbhsg_ureq_to_pkt(u)       (&(u)->pkt)
0104 #define usbhsg_pkt_to_ureq(i)   \
0105     container_of(i, struct usbhsg_request, pkt)
0106 
0107 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
0108 
0109 /* status */
0110 #define usbhsg_status_init(gp)   do {(gp)->status = 0; } while (0)
0111 #define usbhsg_status_set(gp, b) (gp->status |=  b)
0112 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
0113 #define usbhsg_status_has(gp, b) (gp->status &   b)
0114 
0115 /*
0116  *      queue push/pop
0117  */
0118 static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
0119                    struct usbhsg_request *ureq,
0120                    int status)
0121 {
0122     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0123     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0124     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0125     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0126 
0127     if (pipe)
0128         dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
0129 
0130     ureq->req.status = status;
0131     spin_unlock(usbhs_priv_to_lock(priv));
0132     usb_gadget_giveback_request(&uep->ep, &ureq->req);
0133     spin_lock(usbhs_priv_to_lock(priv));
0134 }
0135 
0136 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
0137                  struct usbhsg_request *ureq,
0138                  int status)
0139 {
0140     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0141     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0142     unsigned long flags;
0143 
0144     usbhs_lock(priv, flags);
0145     __usbhsg_queue_pop(uep, ureq, status);
0146     usbhs_unlock(priv, flags);
0147 }
0148 
0149 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
0150 {
0151     struct usbhs_pipe *pipe = pkt->pipe;
0152     struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
0153     struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
0154     unsigned long flags;
0155 
0156     ureq->req.actual = pkt->actual;
0157 
0158     usbhs_lock(priv, flags);
0159     if (uep)
0160         __usbhsg_queue_pop(uep, ureq, 0);
0161     usbhs_unlock(priv, flags);
0162 }
0163 
0164 static void usbhsg_queue_push(struct usbhsg_uep *uep,
0165                   struct usbhsg_request *ureq)
0166 {
0167     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0168     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0169     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0170     struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
0171     struct usb_request *req = &ureq->req;
0172 
0173     req->actual = 0;
0174     req->status = -EINPROGRESS;
0175     usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
0176                req->buf, req->length, req->zero, -1);
0177     usbhs_pkt_start(pipe);
0178 
0179     dev_dbg(dev, "pipe %d : queue push (%d)\n",
0180         usbhs_pipe_number(pipe),
0181         req->length);
0182 }
0183 
0184 /*
0185  *      dma map/unmap
0186  */
0187 static int usbhsg_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
0188                    int map)
0189 {
0190     struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
0191     struct usb_request *req = &ureq->req;
0192     struct usbhs_pipe *pipe = pkt->pipe;
0193     enum dma_data_direction dir;
0194     int ret = 0;
0195 
0196     dir = usbhs_pipe_is_dir_host(pipe);
0197 
0198     if (map) {
0199         /* it can not use scatter/gather */
0200         WARN_ON(req->num_sgs);
0201 
0202         ret = usb_gadget_map_request_by_dev(dma_dev, req, dir);
0203         if (ret < 0)
0204             return ret;
0205 
0206         pkt->dma = req->dma;
0207     } else {
0208         usb_gadget_unmap_request_by_dev(dma_dev, req, dir);
0209     }
0210 
0211     return ret;
0212 }
0213 
0214 /*
0215  *      USB_TYPE_STANDARD / clear feature functions
0216  */
0217 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
0218                          struct usbhsg_uep *uep,
0219                          struct usb_ctrlrequest *ctrl)
0220 {
0221     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0222     struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
0223     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
0224 
0225     usbhs_dcp_control_transfer_done(pipe);
0226 
0227     return 0;
0228 }
0229 
0230 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
0231                            struct usbhsg_uep *uep,
0232                            struct usb_ctrlrequest *ctrl)
0233 {
0234     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0235     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0236 
0237     if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
0238         usbhs_pipe_disable(pipe);
0239         usbhs_pipe_sequence_data0(pipe);
0240         usbhs_pipe_enable(pipe);
0241     }
0242 
0243     usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
0244 
0245     usbhs_pkt_start(pipe);
0246 
0247     return 0;
0248 }
0249 
0250 static struct usbhsg_recip_handle req_clear_feature = {
0251     .name       = "clear feature",
0252     .device     = usbhsg_recip_handler_std_control_done,
0253     .interface  = usbhsg_recip_handler_std_control_done,
0254     .endpoint   = usbhsg_recip_handler_std_clear_endpoint,
0255 };
0256 
0257 /*
0258  *      USB_TYPE_STANDARD / set feature functions
0259  */
0260 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
0261                          struct usbhsg_uep *uep,
0262                          struct usb_ctrlrequest *ctrl)
0263 {
0264     switch (le16_to_cpu(ctrl->wValue)) {
0265     case USB_DEVICE_TEST_MODE:
0266         usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
0267         udelay(100);
0268         usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex) >> 8);
0269         break;
0270     default:
0271         usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
0272         break;
0273     }
0274 
0275     return 0;
0276 }
0277 
0278 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
0279                          struct usbhsg_uep *uep,
0280                          struct usb_ctrlrequest *ctrl)
0281 {
0282     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0283 
0284     usbhs_pipe_stall(pipe);
0285 
0286     usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
0287 
0288     return 0;
0289 }
0290 
0291 static struct usbhsg_recip_handle req_set_feature = {
0292     .name       = "set feature",
0293     .device     = usbhsg_recip_handler_std_set_device,
0294     .interface  = usbhsg_recip_handler_std_control_done,
0295     .endpoint   = usbhsg_recip_handler_std_set_endpoint,
0296 };
0297 
0298 /*
0299  *      USB_TYPE_STANDARD / get status functions
0300  */
0301 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
0302                      struct usb_request *req)
0303 {
0304     struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
0305 
0306     /* free allocated recip-buffer/usb_request */
0307     kfree(ureq->pkt.buf);
0308     usb_ep_free_request(ep, req);
0309 }
0310 
0311 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
0312                        unsigned short status)
0313 {
0314     struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
0315     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
0316     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0317     struct usb_request *req;
0318     __le16 *buf;
0319 
0320     /* alloc new usb_request for recip */
0321     req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
0322     if (!req) {
0323         dev_err(dev, "recip request allocation fail\n");
0324         return;
0325     }
0326 
0327     /* alloc recip data buffer */
0328     buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
0329     if (!buf) {
0330         usb_ep_free_request(&dcp->ep, req);
0331         return;
0332     }
0333 
0334     /* recip data is status */
0335     *buf = cpu_to_le16(status);
0336 
0337     /* allocated usb_request/buffer will be freed */
0338     req->complete   = __usbhsg_recip_send_complete;
0339     req->buf    = buf;
0340     req->length = sizeof(*buf);
0341     req->zero   = 0;
0342 
0343     /* push packet */
0344     pipe->handler = &usbhs_fifo_pio_push_handler;
0345     usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
0346 }
0347 
0348 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
0349                            struct usbhsg_uep *uep,
0350                            struct usb_ctrlrequest *ctrl)
0351 {
0352     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0353     unsigned short status = 0;
0354 
0355     if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
0356         status = 1 << USB_DEVICE_SELF_POWERED;
0357 
0358     __usbhsg_recip_send_status(gpriv, status);
0359 
0360     return 0;
0361 }
0362 
0363 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
0364                           struct usbhsg_uep *uep,
0365                           struct usb_ctrlrequest *ctrl)
0366 {
0367     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0368     unsigned short status = 0;
0369 
0370     __usbhsg_recip_send_status(gpriv, status);
0371 
0372     return 0;
0373 }
0374 
0375 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
0376                          struct usbhsg_uep *uep,
0377                          struct usb_ctrlrequest *ctrl)
0378 {
0379     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0380     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0381     unsigned short status = 0;
0382 
0383     if (usbhs_pipe_is_stall(pipe))
0384         status = 1 << USB_ENDPOINT_HALT;
0385 
0386     __usbhsg_recip_send_status(gpriv, status);
0387 
0388     return 0;
0389 }
0390 
0391 static struct usbhsg_recip_handle req_get_status = {
0392     .name       = "get status",
0393     .device     = usbhsg_recip_handler_std_get_device,
0394     .interface  = usbhsg_recip_handler_std_get_interface,
0395     .endpoint   = usbhsg_recip_handler_std_get_endpoint,
0396 };
0397 
0398 /*
0399  *      USB_TYPE handler
0400  */
0401 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
0402                    struct usbhsg_recip_handle *handler,
0403                    struct usb_ctrlrequest *ctrl)
0404 {
0405     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0406     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0407     struct usbhsg_uep *uep;
0408     struct usbhs_pipe *pipe;
0409     int recip = ctrl->bRequestType & USB_RECIP_MASK;
0410     int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
0411     int ret = 0;
0412     int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
0413             struct usb_ctrlrequest *ctrl);
0414     char *msg;
0415 
0416     uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
0417     pipe = usbhsg_uep_to_pipe(uep);
0418     if (!pipe) {
0419         dev_err(dev, "wrong recip request\n");
0420         return -EINVAL;
0421     }
0422 
0423     switch (recip) {
0424     case USB_RECIP_DEVICE:
0425         msg = "DEVICE";
0426         func    = handler->device;
0427         break;
0428     case USB_RECIP_INTERFACE:
0429         msg = "INTERFACE";
0430         func    = handler->interface;
0431         break;
0432     case USB_RECIP_ENDPOINT:
0433         msg = "ENDPOINT";
0434         func    = handler->endpoint;
0435         break;
0436     default:
0437         dev_warn(dev, "unsupported RECIP(%d)\n", recip);
0438         func = NULL;
0439         ret = -EINVAL;
0440     }
0441 
0442     if (func) {
0443         dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
0444         ret = func(priv, uep, ctrl);
0445     }
0446 
0447     return ret;
0448 }
0449 
0450 /*
0451  *      irq functions
0452  *
0453  * it will be called from usbhs_interrupt
0454  */
0455 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
0456                 struct usbhs_irq_state *irq_state)
0457 {
0458     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0459     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0460     int state = usbhs_status_get_device_state(irq_state);
0461 
0462     gpriv->gadget.speed = usbhs_bus_get_speed(priv);
0463 
0464     dev_dbg(dev, "state = %x : speed : %d\n", state, gpriv->gadget.speed);
0465 
0466     if (gpriv->gadget.speed != USB_SPEED_UNKNOWN &&
0467         (state & SUSPENDED_STATE)) {
0468         if (gpriv->driver && gpriv->driver->suspend)
0469             gpriv->driver->suspend(&gpriv->gadget);
0470         usb_gadget_set_state(&gpriv->gadget, USB_STATE_SUSPENDED);
0471     }
0472 
0473     return 0;
0474 }
0475 
0476 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
0477                  struct usbhs_irq_state *irq_state)
0478 {
0479     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0480     struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
0481     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
0482     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0483     struct usb_ctrlrequest ctrl;
0484     struct usbhsg_recip_handle *recip_handler = NULL;
0485     int stage = usbhs_status_get_ctrl_stage(irq_state);
0486     int ret = 0;
0487 
0488     dev_dbg(dev, "stage = %d\n", stage);
0489 
0490     /*
0491      * see Manual
0492      *
0493      *  "Operation"
0494      *  - "Interrupt Function"
0495      *    - "Control Transfer Stage Transition Interrupt"
0496      *      - Fig. "Control Transfer Stage Transitions"
0497      */
0498 
0499     switch (stage) {
0500     case READ_DATA_STAGE:
0501         pipe->handler = &usbhs_fifo_pio_push_handler;
0502         break;
0503     case WRITE_DATA_STAGE:
0504         pipe->handler = &usbhs_fifo_pio_pop_handler;
0505         break;
0506     case NODATA_STATUS_STAGE:
0507         pipe->handler = &usbhs_ctrl_stage_end_handler;
0508         break;
0509     case READ_STATUS_STAGE:
0510     case WRITE_STATUS_STAGE:
0511         usbhs_dcp_control_transfer_done(pipe);
0512         fallthrough;
0513     default:
0514         return ret;
0515     }
0516 
0517     /*
0518      * get usb request
0519      */
0520     usbhs_usbreq_get_val(priv, &ctrl);
0521 
0522     switch (ctrl.bRequestType & USB_TYPE_MASK) {
0523     case USB_TYPE_STANDARD:
0524         switch (ctrl.bRequest) {
0525         case USB_REQ_CLEAR_FEATURE:
0526             recip_handler = &req_clear_feature;
0527             break;
0528         case USB_REQ_SET_FEATURE:
0529             recip_handler = &req_set_feature;
0530             break;
0531         case USB_REQ_GET_STATUS:
0532             recip_handler = &req_get_status;
0533             break;
0534         }
0535     }
0536 
0537     /*
0538      * setup stage / run recip
0539      */
0540     if (recip_handler)
0541         ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
0542     else
0543         ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
0544 
0545     if (ret < 0)
0546         usbhs_pipe_stall(pipe);
0547 
0548     return ret;
0549 }
0550 
0551 /*
0552  *
0553  *      usb_dcp_ops
0554  *
0555  */
0556 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
0557 {
0558     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0559     struct usbhs_pkt *pkt;
0560 
0561     while (1) {
0562         pkt = usbhs_pkt_pop(pipe, NULL);
0563         if (!pkt)
0564             break;
0565 
0566         usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ESHUTDOWN);
0567     }
0568 
0569     usbhs_pipe_disable(pipe);
0570 
0571     return 0;
0572 }
0573 
0574 /*
0575  *
0576  *      usb_ep_ops
0577  *
0578  */
0579 static int usbhsg_ep_enable(struct usb_ep *ep,
0580              const struct usb_endpoint_descriptor *desc)
0581 {
0582     struct usbhsg_uep *uep   = usbhsg_ep_to_uep(ep);
0583     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0584     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0585     struct usbhs_pipe *pipe;
0586     int ret = -EIO;
0587     unsigned long flags;
0588 
0589     usbhs_lock(priv, flags);
0590 
0591     /*
0592      * if it already have pipe,
0593      * nothing to do
0594      */
0595     if (uep->pipe) {
0596         usbhs_pipe_clear(uep->pipe);
0597         usbhs_pipe_sequence_data0(uep->pipe);
0598         ret = 0;
0599         goto usbhsg_ep_enable_end;
0600     }
0601 
0602     pipe = usbhs_pipe_malloc(priv,
0603                  usb_endpoint_type(desc),
0604                  usb_endpoint_dir_in(desc));
0605     if (pipe) {
0606         uep->pipe       = pipe;
0607         pipe->mod_private   = uep;
0608 
0609         /* set epnum / maxp */
0610         usbhs_pipe_config_update(pipe, 0,
0611                      usb_endpoint_num(desc),
0612                      usb_endpoint_maxp(desc));
0613 
0614         /*
0615          * usbhs_fifo_dma_push/pop_handler try to
0616          * use dmaengine if possible.
0617          * It will use pio handler if impossible.
0618          */
0619         if (usb_endpoint_dir_in(desc)) {
0620             pipe->handler = &usbhs_fifo_dma_push_handler;
0621         } else {
0622             pipe->handler = &usbhs_fifo_dma_pop_handler;
0623             usbhs_xxxsts_clear(priv, BRDYSTS,
0624                        usbhs_pipe_number(pipe));
0625         }
0626 
0627         ret = 0;
0628     }
0629 
0630 usbhsg_ep_enable_end:
0631     usbhs_unlock(priv, flags);
0632 
0633     return ret;
0634 }
0635 
0636 static int usbhsg_ep_disable(struct usb_ep *ep)
0637 {
0638     struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
0639     struct usbhs_pipe *pipe;
0640     unsigned long flags;
0641 
0642     spin_lock_irqsave(&uep->lock, flags);
0643     pipe = usbhsg_uep_to_pipe(uep);
0644     if (!pipe)
0645         goto out;
0646 
0647     usbhsg_pipe_disable(uep);
0648     usbhs_pipe_free(pipe);
0649 
0650     uep->pipe->mod_private  = NULL;
0651     uep->pipe       = NULL;
0652 
0653 out:
0654     spin_unlock_irqrestore(&uep->lock, flags);
0655 
0656     return 0;
0657 }
0658 
0659 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
0660                            gfp_t gfp_flags)
0661 {
0662     struct usbhsg_request *ureq;
0663 
0664     ureq = kzalloc(sizeof *ureq, gfp_flags);
0665     if (!ureq)
0666         return NULL;
0667 
0668     usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
0669 
0670     return &ureq->req;
0671 }
0672 
0673 static void usbhsg_ep_free_request(struct usb_ep *ep,
0674                    struct usb_request *req)
0675 {
0676     struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
0677 
0678     WARN_ON(!list_empty(&ureq->pkt.node));
0679     kfree(ureq);
0680 }
0681 
0682 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
0683               gfp_t gfp_flags)
0684 {
0685     struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
0686     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0687     struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
0688     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0689 
0690     /* param check */
0691     if (usbhsg_is_not_connected(gpriv)  ||
0692         unlikely(!gpriv->driver)        ||
0693         unlikely(!pipe))
0694         return -ESHUTDOWN;
0695 
0696     usbhsg_queue_push(uep, ureq);
0697 
0698     return 0;
0699 }
0700 
0701 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
0702 {
0703     struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
0704     struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
0705     struct usbhs_pipe *pipe;
0706     unsigned long flags;
0707 
0708     spin_lock_irqsave(&uep->lock, flags);
0709     pipe = usbhsg_uep_to_pipe(uep);
0710     if (pipe)
0711         usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
0712 
0713     /*
0714      * To dequeue a request, this driver should call the usbhsg_queue_pop()
0715      * even if the pipe is NULL.
0716      */
0717     usbhsg_queue_pop(uep, ureq, -ECONNRESET);
0718     spin_unlock_irqrestore(&uep->lock, flags);
0719 
0720     return 0;
0721 }
0722 
0723 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
0724 {
0725     struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
0726     struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
0727     struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
0728     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0729     struct device *dev = usbhsg_gpriv_to_dev(gpriv);
0730     unsigned long flags;
0731     int ret = 0;
0732 
0733     dev_dbg(dev, "set halt %d (pipe %d)\n",
0734         halt, usbhs_pipe_number(pipe));
0735 
0736     /********************  spin lock ********************/
0737     usbhs_lock(priv, flags);
0738 
0739     /*
0740      * According to usb_ep_set_halt()'s description, this function should
0741      * return -EAGAIN if the IN endpoint has any queue or data. Note
0742      * that the usbhs_pipe_is_dir_in() returns false if the pipe is an
0743      * IN endpoint in the gadget mode.
0744      */
0745     if (!usbhs_pipe_is_dir_in(pipe) && (__usbhsf_pkt_get(pipe) ||
0746         usbhs_pipe_contains_transmittable_data(pipe))) {
0747         ret = -EAGAIN;
0748         goto out;
0749     }
0750 
0751     if (halt)
0752         usbhs_pipe_stall(pipe);
0753     else
0754         usbhs_pipe_disable(pipe);
0755 
0756     if (halt && wedge)
0757         usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
0758     else
0759         usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
0760 
0761 out:
0762     usbhs_unlock(priv, flags);
0763     /********************  spin unlock ******************/
0764 
0765     return ret;
0766 }
0767 
0768 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
0769 {
0770     return __usbhsg_ep_set_halt_wedge(ep, value, 0);
0771 }
0772 
0773 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
0774 {
0775     return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
0776 }
0777 
0778 static const struct usb_ep_ops usbhsg_ep_ops = {
0779     .enable     = usbhsg_ep_enable,
0780     .disable    = usbhsg_ep_disable,
0781 
0782     .alloc_request  = usbhsg_ep_alloc_request,
0783     .free_request   = usbhsg_ep_free_request,
0784 
0785     .queue      = usbhsg_ep_queue,
0786     .dequeue    = usbhsg_ep_dequeue,
0787 
0788     .set_halt   = usbhsg_ep_set_halt,
0789     .set_wedge  = usbhsg_ep_set_wedge,
0790 };
0791 
0792 /*
0793  *      pullup control
0794  */
0795 static int usbhsg_can_pullup(struct usbhs_priv *priv)
0796 {
0797     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0798 
0799     return gpriv->driver &&
0800            usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
0801 }
0802 
0803 static void usbhsg_update_pullup(struct usbhs_priv *priv)
0804 {
0805     if (usbhsg_can_pullup(priv))
0806         usbhs_sys_function_pullup(priv, 1);
0807     else
0808         usbhs_sys_function_pullup(priv, 0);
0809 }
0810 
0811 /*
0812  *      usb module start/end
0813  */
0814 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
0815 {
0816     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0817     struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
0818     struct usbhs_mod *mod = usbhs_mod_get_current(priv);
0819     struct device *dev = usbhs_priv_to_dev(priv);
0820     unsigned long flags;
0821     int ret = 0;
0822 
0823     /********************  spin lock ********************/
0824     usbhs_lock(priv, flags);
0825 
0826     usbhsg_status_set(gpriv, status);
0827     if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
0828           usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
0829         ret = -1; /* not ready */
0830 
0831     usbhs_unlock(priv, flags);
0832     /********************  spin unlock ********************/
0833 
0834     if (ret < 0)
0835         return 0; /* not ready is not error */
0836 
0837     /*
0838      * enable interrupt and systems if ready
0839      */
0840     dev_dbg(dev, "start gadget\n");
0841 
0842     /*
0843      * pipe initialize and enable DCP
0844      */
0845     usbhs_fifo_init(priv);
0846     usbhs_pipe_init(priv,
0847             usbhsg_dma_map_ctrl);
0848 
0849     /* dcp init instead of usbhsg_ep_enable() */
0850     dcp->pipe       = usbhs_dcp_malloc(priv);
0851     dcp->pipe->mod_private  = dcp;
0852     usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
0853 
0854     /*
0855      * system config enble
0856      * - HI speed
0857      * - function
0858      * - usb module
0859      */
0860     usbhs_sys_function_ctrl(priv, 1);
0861     usbhsg_update_pullup(priv);
0862 
0863     /*
0864      * enable irq callback
0865      */
0866     mod->irq_dev_state  = usbhsg_irq_dev_state;
0867     mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
0868     usbhs_irq_callback_update(priv, mod);
0869 
0870     return 0;
0871 }
0872 
0873 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
0874 {
0875     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0876     struct usbhs_mod *mod = usbhs_mod_get_current(priv);
0877     struct usbhsg_uep *uep;
0878     struct device *dev = usbhs_priv_to_dev(priv);
0879     unsigned long flags;
0880     int ret = 0, i;
0881 
0882     /********************  spin lock ********************/
0883     usbhs_lock(priv, flags);
0884 
0885     usbhsg_status_clr(gpriv, status);
0886     if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
0887         !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
0888         ret = -1; /* already done */
0889 
0890     usbhs_unlock(priv, flags);
0891     /********************  spin unlock ********************/
0892 
0893     if (ret < 0)
0894         return 0; /* already done is not error */
0895 
0896     /*
0897      * disable interrupt and systems if 1st try
0898      */
0899     usbhs_fifo_quit(priv);
0900 
0901     /* disable all irq */
0902     mod->irq_dev_state  = NULL;
0903     mod->irq_ctrl_stage = NULL;
0904     usbhs_irq_callback_update(priv, mod);
0905 
0906     gpriv->gadget.speed = USB_SPEED_UNKNOWN;
0907 
0908     /* disable sys */
0909     usbhs_sys_set_test_mode(priv, 0);
0910     usbhs_sys_function_ctrl(priv, 0);
0911 
0912     /* disable all eps */
0913     usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
0914         usbhsg_ep_disable(&uep->ep);
0915 
0916     dev_dbg(dev, "stop gadget\n");
0917 
0918     return 0;
0919 }
0920 
0921 /*
0922  * VBUS provided by the PHY
0923  */
0924 static int usbhsm_phy_get_vbus(struct platform_device *pdev)
0925 {
0926     struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
0927     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
0928 
0929     return  gpriv->vbus_active;
0930 }
0931 
0932 static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
0933 {
0934     struct usbhs_mod_info *info = &priv->mod_info;
0935 
0936     info->irq_vbus = NULL;
0937     info->get_vbus = usbhsm_phy_get_vbus;
0938 
0939     usbhs_irq_callback_update(priv, NULL);
0940 }
0941 
0942 /*
0943  *
0944  *      linux usb function
0945  *
0946  */
0947 static int usbhsg_gadget_start(struct usb_gadget *gadget,
0948         struct usb_gadget_driver *driver)
0949 {
0950     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
0951     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0952     struct device *dev = usbhs_priv_to_dev(priv);
0953     int ret;
0954 
0955     if (!driver     ||
0956         !driver->setup  ||
0957         driver->max_speed < USB_SPEED_FULL)
0958         return -EINVAL;
0959 
0960     /* connect to bus through transceiver */
0961     if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
0962         ret = otg_set_peripheral(gpriv->transceiver->otg,
0963                     &gpriv->gadget);
0964         if (ret) {
0965             dev_err(dev, "%s: can't bind to transceiver\n",
0966                 gpriv->gadget.name);
0967             return ret;
0968         }
0969 
0970         /* get vbus using phy versions */
0971         usbhs_mod_phy_mode(priv);
0972     }
0973 
0974     /* first hook up the driver ... */
0975     gpriv->driver = driver;
0976 
0977     return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
0978 }
0979 
0980 static int usbhsg_gadget_stop(struct usb_gadget *gadget)
0981 {
0982     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
0983     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
0984 
0985     usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
0986 
0987     if (!IS_ERR_OR_NULL(gpriv->transceiver))
0988         otg_set_peripheral(gpriv->transceiver->otg, NULL);
0989 
0990     gpriv->driver = NULL;
0991 
0992     return 0;
0993 }
0994 
0995 /*
0996  *      usb gadget ops
0997  */
0998 static int usbhsg_get_frame(struct usb_gadget *gadget)
0999 {
1000     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1001     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1002 
1003     return usbhs_frame_get_num(priv);
1004 }
1005 
1006 static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
1007 {
1008     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1009     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1010     unsigned long flags;
1011 
1012     usbhs_lock(priv, flags);
1013     if (is_on)
1014         usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
1015     else
1016         usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
1017     usbhsg_update_pullup(priv);
1018     usbhs_unlock(priv, flags);
1019 
1020     return 0;
1021 }
1022 
1023 static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
1024 {
1025     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1026 
1027     if (is_self)
1028         usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
1029     else
1030         usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
1031 
1032     gadget->is_selfpowered = (is_self != 0);
1033 
1034     return 0;
1035 }
1036 
1037 static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
1038 {
1039     struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1040     struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1041     struct platform_device *pdev = usbhs_priv_to_pdev(priv);
1042 
1043     gpriv->vbus_active = !!is_active;
1044 
1045     usbhsc_schedule_notify_hotplug(pdev);
1046 
1047     return 0;
1048 }
1049 
1050 static const struct usb_gadget_ops usbhsg_gadget_ops = {
1051     .get_frame      = usbhsg_get_frame,
1052     .set_selfpowered    = usbhsg_set_selfpowered,
1053     .udc_start      = usbhsg_gadget_start,
1054     .udc_stop       = usbhsg_gadget_stop,
1055     .pullup         = usbhsg_pullup,
1056     .vbus_session       = usbhsg_vbus_session,
1057 };
1058 
1059 static int usbhsg_start(struct usbhs_priv *priv)
1060 {
1061     return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1062 }
1063 
1064 static int usbhsg_stop(struct usbhs_priv *priv)
1065 {
1066     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1067 
1068     /* cable disconnect */
1069     if (gpriv->driver &&
1070         gpriv->driver->disconnect)
1071         gpriv->driver->disconnect(&gpriv->gadget);
1072 
1073     return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1074 }
1075 
1076 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1077 {
1078     struct usbhsg_gpriv *gpriv;
1079     struct usbhsg_uep *uep;
1080     struct device *dev = usbhs_priv_to_dev(priv);
1081     struct renesas_usbhs_driver_pipe_config *pipe_configs =
1082                     usbhs_get_dparam(priv, pipe_configs);
1083     int pipe_size = usbhs_get_dparam(priv, pipe_size);
1084     int i;
1085     int ret;
1086 
1087     gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1088     if (!gpriv)
1089         return -ENOMEM;
1090 
1091     uep = kcalloc(pipe_size, sizeof(struct usbhsg_uep), GFP_KERNEL);
1092     if (!uep) {
1093         ret = -ENOMEM;
1094         goto usbhs_mod_gadget_probe_err_gpriv;
1095     }
1096 
1097     gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
1098     dev_info(dev, "%stransceiver found\n",
1099          !IS_ERR(gpriv->transceiver) ? "" : "no ");
1100 
1101     /*
1102      * CAUTION
1103      *
1104      * There is no guarantee that it is possible to access usb module here.
1105      * Don't accesses to it.
1106      * The accesse will be enable after "usbhsg_start"
1107      */
1108 
1109     /*
1110      * register itself
1111      */
1112     usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1113 
1114     /* init gpriv */
1115     gpriv->mod.name     = "gadget";
1116     gpriv->mod.start    = usbhsg_start;
1117     gpriv->mod.stop     = usbhsg_stop;
1118     gpriv->uep      = uep;
1119     gpriv->uep_size     = pipe_size;
1120     usbhsg_status_init(gpriv);
1121 
1122     /*
1123      * init gadget
1124      */
1125     gpriv->gadget.dev.parent    = dev;
1126     gpriv->gadget.name      = "renesas_usbhs_udc";
1127     gpriv->gadget.ops       = &usbhsg_gadget_ops;
1128     gpriv->gadget.max_speed     = USB_SPEED_HIGH;
1129     gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv,
1130                                 has_usb_dmac);
1131 
1132     INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1133 
1134     /*
1135      * init usb_ep
1136      */
1137     usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1138         uep->gpriv  = gpriv;
1139         uep->pipe   = NULL;
1140         snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1141 
1142         uep->ep.name        = uep->ep_name;
1143         uep->ep.ops     = &usbhsg_ep_ops;
1144         INIT_LIST_HEAD(&uep->ep.ep_list);
1145         spin_lock_init(&uep->lock);
1146 
1147         /* init DCP */
1148         if (usbhsg_is_dcp(uep)) {
1149             gpriv->gadget.ep0 = &uep->ep;
1150             usb_ep_set_maxpacket_limit(&uep->ep, 64);
1151             uep->ep.caps.type_control = true;
1152         } else {
1153             /* init normal pipe */
1154             if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
1155                 uep->ep.caps.type_iso = true;
1156             if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
1157                 uep->ep.caps.type_bulk = true;
1158             if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
1159                 uep->ep.caps.type_int = true;
1160             usb_ep_set_maxpacket_limit(&uep->ep,
1161                            pipe_configs[i].bufsize);
1162             list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1163         }
1164         uep->ep.caps.dir_in = true;
1165         uep->ep.caps.dir_out = true;
1166     }
1167 
1168     ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1169     if (ret)
1170         goto err_add_udc;
1171 
1172 
1173     dev_info(dev, "gadget probed\n");
1174 
1175     return 0;
1176 
1177 err_add_udc:
1178     kfree(gpriv->uep);
1179 
1180 usbhs_mod_gadget_probe_err_gpriv:
1181     kfree(gpriv);
1182 
1183     return ret;
1184 }
1185 
1186 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1187 {
1188     struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1189 
1190     usb_del_gadget_udc(&gpriv->gadget);
1191 
1192     kfree(gpriv->uep);
1193     kfree(gpriv);
1194 }