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  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
0007  */
0008 #include <linux/io.h>
0009 #include <linux/list.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 #include <linux/usb.h>
0014 #include <linux/usb/hcd.h>
0015 #include "common.h"
0016 
0017 /*
0018  *** HARDWARE LIMITATION ***
0019  *
0020  * 1) renesas_usbhs has a limited number of controllable devices.
0021  *    it can control only 9 devices in generally.
0022  *  see DEVADDn / DCPMAXP / PIPEMAXP.
0023  *
0024  * 2) renesas_usbhs pipe number is limited.
0025  *    the pipe will be re-used for each devices.
0026  *    so, software should control DATA0/1 sequence of each devices.
0027  */
0028 
0029 
0030 /*
0031  *      image of mod_host
0032  *
0033  * +--------+
0034  * | udev 0 | --> it is used when set address
0035  * +--------+
0036  *
0037  * +--------+                   pipes are reused for each uep.
0038  * | udev 1 |-+- [uep 0 (dcp) ] --+     pipe will be switched when
0039  * +--------+ |           |     other device requested
0040  *        +- [uep 1 (bulk)] --|---+        +--------------+
0041  *        |           +--------------> | pipe0 (dcp)  |
0042  *        +- [uep 2 (bulk)] -@    |        +--------------+
0043  *                    |        | pipe1 (isoc) |
0044  * +--------+                 |        +--------------+
0045  * | udev 2 |-+- [uep 0 (dcp) ] -@    +----------> | pipe2 (bulk) |
0046  * +--------+ |                    +--------------+
0047  *        +- [uep 1 (int) ] ----+     +------> | pipe3 (bulk) |
0048  *                  |     |    +--------------+
0049  * +--------+               +-----|------> | pipe4 (int)  |
0050  * | udev 3 |-+- [uep 0 (dcp) ] -@    |    +--------------+
0051  * +--------+ |               |    | ....     |
0052  *        +- [uep 1 (bulk)] -@    |    | ....     |
0053  *        |               |
0054  *        +- [uep 2 (bulk)]-----------+
0055  *
0056  * @ :  uep requested free pipe, but all have been used.
0057  *  now it is waiting for free pipe
0058  */
0059 
0060 
0061 /*
0062  *      struct
0063  */
0064 struct usbhsh_request {
0065     struct urb      *urb;
0066     struct usbhs_pkt    pkt;
0067 };
0068 
0069 struct usbhsh_device {
0070     struct usb_device   *usbv;
0071     struct list_head    ep_list_head; /* list of usbhsh_ep */
0072 };
0073 
0074 struct usbhsh_ep {
0075     struct usbhs_pipe   *pipe;   /* attached pipe */
0076     struct usbhsh_device    *udev;   /* attached udev */
0077     struct usb_host_endpoint *ep;
0078     struct list_head    ep_list; /* list to usbhsh_device */
0079     unsigned int        counter; /* pipe attach counter */
0080 };
0081 
0082 #define USBHSH_DEVICE_MAX   10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
0083 #define USBHSH_PORT_MAX      7 /* see DEVADDn :: HUBPORT */
0084 struct usbhsh_hpriv {
0085     struct usbhs_mod    mod;
0086     struct usbhs_pipe   *dcp;
0087 
0088     struct usbhsh_device    udev[USBHSH_DEVICE_MAX];
0089 
0090     u32 port_stat;  /* USB_PORT_STAT_xxx */
0091 
0092     struct completion   setup_ack_done;
0093 };
0094 
0095 
0096 static const char usbhsh_hcd_name[] = "renesas_usbhs host";
0097 
0098 /*
0099  *      macro
0100  */
0101 #define usbhsh_priv_to_hpriv(priv) \
0102     container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
0103 
0104 #define __usbhsh_for_each_udev(start, pos, h, i)    \
0105     for ((i) = start;                       \
0106          ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i));    \
0107          (i)++)
0108 
0109 #define usbhsh_for_each_udev(pos, hpriv, i) \
0110     __usbhsh_for_each_udev(1, pos, hpriv, i)
0111 
0112 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i)   \
0113     __usbhsh_for_each_udev(0, pos, hpriv, i)
0114 
0115 #define usbhsh_hcd_to_hpriv(h)  (struct usbhsh_hpriv *)((h)->hcd_priv)
0116 #define usbhsh_hcd_to_dev(h)    ((h)->self.controller)
0117 
0118 #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
0119 #define usbhsh_hpriv_to_dcp(h)  ((h)->dcp)
0120 #define usbhsh_hpriv_to_hcd(h)  \
0121     container_of((void *)h, struct usb_hcd, hcd_priv)
0122 
0123 #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
0124 #define usbhsh_uep_to_pipe(u)   ((u)->pipe)
0125 #define usbhsh_uep_to_udev(u)   ((u)->udev)
0126 #define usbhsh_uep_to_ep(u) ((u)->ep)
0127 
0128 #define usbhsh_urb_to_ureq(u)   ((u)->hcpriv)
0129 #define usbhsh_urb_to_usbv(u)   ((u)->dev)
0130 
0131 #define usbhsh_usbv_to_udev(d)  dev_get_drvdata(&(d)->dev)
0132 
0133 #define usbhsh_udev_to_usbv(h)  ((h)->usbv)
0134 #define usbhsh_udev_is_used(h)  usbhsh_udev_to_usbv(h)
0135 
0136 #define usbhsh_pipe_to_uep(p)   ((p)->mod_private)
0137 
0138 #define usbhsh_device_parent(d)     (usbhsh_usbv_to_udev((d)->usbv->parent))
0139 #define usbhsh_device_hubport(d)    ((d)->usbv->portnum)
0140 #define usbhsh_device_number(h, d)  ((int)((d) - (h)->udev))
0141 #define usbhsh_device_nth(h, d)     ((h)->udev + d)
0142 #define usbhsh_device0(h)       usbhsh_device_nth(h, 0)
0143 
0144 #define usbhsh_port_stat_init(h)    ((h)->port_stat = 0)
0145 #define usbhsh_port_stat_set(h, s)  ((h)->port_stat |= (s))
0146 #define usbhsh_port_stat_clear(h, s)    ((h)->port_stat &= ~(s))
0147 #define usbhsh_port_stat_get(h)     ((h)->port_stat)
0148 
0149 #define usbhsh_pkt_to_ureq(p)   \
0150     container_of((void *)p, struct usbhsh_request, pkt)
0151 
0152 /*
0153  *      req alloc/free
0154  */
0155 static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
0156                            struct urb *urb,
0157                            gfp_t mem_flags)
0158 {
0159     struct usbhsh_request *ureq;
0160 
0161     ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
0162     if (!ureq)
0163         return NULL;
0164 
0165     usbhs_pkt_init(&ureq->pkt);
0166     ureq->urb = urb;
0167     usbhsh_urb_to_ureq(urb) = ureq;
0168 
0169     return ureq;
0170 }
0171 
0172 static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
0173                 struct usbhsh_request *ureq)
0174 {
0175     usbhsh_urb_to_ureq(ureq->urb) = NULL;
0176     ureq->urb = NULL;
0177 
0178     kfree(ureq);
0179 }
0180 
0181 /*
0182  *      status
0183  */
0184 static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
0185 {
0186     /*
0187      * we can decide some device is attached or not
0188      * by checking mod.irq_attch
0189      * see
0190      *  usbhsh_irq_attch()
0191      *  usbhsh_irq_dtch()
0192      */
0193     return (hpriv->mod.irq_attch == NULL);
0194 }
0195 
0196 /*
0197  *      pipe control
0198  */
0199 static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
0200                       struct urb *urb,
0201                       struct usbhs_pkt *pkt)
0202 {
0203     int len = urb->actual_length;
0204     int maxp = usb_endpoint_maxp(&urb->ep->desc);
0205     int t = 0;
0206 
0207     /* DCP is out of sequence control */
0208     if (usb_pipecontrol(urb->pipe))
0209         return;
0210 
0211     /*
0212      * renesas_usbhs pipe has a limitation in a number.
0213      * So, driver should re-use the limited pipe for each device/endpoint.
0214      * DATA0/1 sequence should be saved for it.
0215      * see [image of mod_host]
0216      *     [HARDWARE LIMITATION]
0217      */
0218 
0219     /*
0220      * next sequence depends on actual_length
0221      *
0222      * ex) actual_length = 1147, maxp = 512
0223      * data0 : 512
0224      * data1 : 512
0225      * data0 : 123
0226      * data1 is the next sequence
0227      */
0228     t = len / maxp;
0229     if (len % maxp)
0230         t++;
0231     if (pkt->zero)
0232         t++;
0233     t %= 2;
0234 
0235     if (t)
0236         usb_dotoggle(urb->dev,
0237                  usb_pipeendpoint(urb->pipe),
0238                  usb_pipeout(urb->pipe));
0239 }
0240 
0241 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
0242                            struct urb *urb);
0243 
0244 static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
0245                   struct urb *urb)
0246 {
0247     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0248     struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
0249     struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
0250     struct usbhs_pipe *pipe;
0251     struct usb_endpoint_descriptor *desc = &urb->ep->desc;
0252     struct device *dev = usbhs_priv_to_dev(priv);
0253     unsigned long flags;
0254     int dir_in_req = !!usb_pipein(urb->pipe);
0255     int is_dcp = usb_endpoint_xfer_control(desc);
0256     int i, dir_in;
0257     int ret = -EBUSY;
0258 
0259     /********************  spin lock ********************/
0260     usbhs_lock(priv, flags);
0261 
0262     /*
0263      * if uep has been attached to pipe,
0264      * reuse it
0265      */
0266     if (usbhsh_uep_to_pipe(uep)) {
0267         ret = 0;
0268         goto usbhsh_pipe_attach_done;
0269     }
0270 
0271     usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
0272 
0273         /* check pipe type */
0274         if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
0275             continue;
0276 
0277         /* check pipe direction if normal pipe */
0278         if (!is_dcp) {
0279             dir_in = !!usbhs_pipe_is_dir_in(pipe);
0280             if (0 != (dir_in - dir_in_req))
0281                 continue;
0282         }
0283 
0284         /* check pipe is free */
0285         if (usbhsh_pipe_to_uep(pipe))
0286             continue;
0287 
0288         /*
0289          * attach pipe to uep
0290          *
0291          * usbhs_pipe_config_update() should be called after
0292          * usbhs_set_device_config()
0293          * see
0294          *  DCPMAXP/PIPEMAXP
0295          */
0296         usbhsh_uep_to_pipe(uep)     = pipe;
0297         usbhsh_pipe_to_uep(pipe)    = uep;
0298 
0299         usbhs_pipe_config_update(pipe,
0300                      usbhsh_device_number(hpriv, udev),
0301                      usb_endpoint_num(desc),
0302                      usb_endpoint_maxp(desc));
0303 
0304         dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
0305             usbhsh_device_number(hpriv, udev),
0306             usb_endpoint_num(desc),
0307             usbhs_pipe_name(pipe),
0308             dir_in_req ? "in" : "out");
0309 
0310         ret = 0;
0311         break;
0312     }
0313 
0314 usbhsh_pipe_attach_done:
0315     if (0 == ret)
0316         uep->counter++;
0317 
0318     usbhs_unlock(priv, flags);
0319     /********************  spin unlock ******************/
0320 
0321     return ret;
0322 }
0323 
0324 static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
0325                    struct usbhsh_ep *uep)
0326 {
0327     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0328     struct usbhs_pipe *pipe;
0329     struct device *dev = usbhs_priv_to_dev(priv);
0330     unsigned long flags;
0331 
0332     if (unlikely(!uep)) {
0333         dev_err(dev, "no uep\n");
0334         return;
0335     }
0336 
0337     /********************  spin lock ********************/
0338     usbhs_lock(priv, flags);
0339 
0340     pipe = usbhsh_uep_to_pipe(uep);
0341 
0342     if (unlikely(!pipe)) {
0343         dev_err(dev, "uep doesn't have pipe\n");
0344     } else if (1 == uep->counter--) { /* last user */
0345         struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
0346         struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
0347 
0348         /* detach pipe from uep */
0349         usbhsh_uep_to_pipe(uep)     = NULL;
0350         usbhsh_pipe_to_uep(pipe)    = NULL;
0351 
0352         dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
0353             usbhsh_device_number(hpriv, udev),
0354             usb_endpoint_num(&ep->desc),
0355             usbhs_pipe_name(pipe));
0356     }
0357 
0358     usbhs_unlock(priv, flags);
0359     /********************  spin unlock ******************/
0360 }
0361 
0362 /*
0363  *      endpoint control
0364  */
0365 static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
0366                   struct urb *urb,
0367                   gfp_t mem_flags)
0368 {
0369     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0370     struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
0371     struct usb_host_endpoint *ep = urb->ep;
0372     struct usbhsh_ep *uep;
0373     struct device *dev = usbhs_priv_to_dev(priv);
0374     struct usb_endpoint_descriptor *desc = &ep->desc;
0375     unsigned long flags;
0376 
0377     uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
0378     if (!uep)
0379         return -ENOMEM;
0380 
0381     /********************  spin lock ********************/
0382     usbhs_lock(priv, flags);
0383 
0384     /*
0385      * init endpoint
0386      */
0387     uep->counter = 0;
0388     INIT_LIST_HEAD(&uep->ep_list);
0389     list_add_tail(&uep->ep_list, &udev->ep_list_head);
0390 
0391     usbhsh_uep_to_udev(uep) = udev;
0392     usbhsh_uep_to_ep(uep)   = ep;
0393     usbhsh_ep_to_uep(ep)    = uep;
0394 
0395     usbhs_unlock(priv, flags);
0396     /********************  spin unlock ******************/
0397 
0398     dev_dbg(dev, "%s [%d-%d]\n", __func__,
0399         usbhsh_device_number(hpriv, udev),
0400         usb_endpoint_num(desc));
0401 
0402     return 0;
0403 }
0404 
0405 static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
0406                    struct usb_host_endpoint *ep)
0407 {
0408     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0409     struct device *dev = usbhs_priv_to_dev(priv);
0410     struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
0411     unsigned long flags;
0412 
0413     if (!uep)
0414         return;
0415 
0416     dev_dbg(dev, "%s [%d-%d]\n", __func__,
0417         usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
0418         usb_endpoint_num(&ep->desc));
0419 
0420     if (usbhsh_uep_to_pipe(uep))
0421         usbhsh_pipe_detach(hpriv, uep);
0422 
0423     /********************  spin lock ********************/
0424     usbhs_lock(priv, flags);
0425 
0426     /* remove this endpoint from udev */
0427     list_del_init(&uep->ep_list);
0428 
0429     usbhsh_uep_to_udev(uep) = NULL;
0430     usbhsh_uep_to_ep(uep)   = NULL;
0431     usbhsh_ep_to_uep(ep)    = NULL;
0432 
0433     usbhs_unlock(priv, flags);
0434     /********************  spin unlock ******************/
0435 
0436     kfree(uep);
0437 }
0438 
0439 static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
0440                        struct usbhsh_device *udev)
0441 {
0442     struct usbhsh_ep *uep, *next;
0443 
0444     list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
0445         usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
0446 }
0447 
0448 /*
0449  *      device control
0450  */
0451 static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
0452                      struct usbhsh_device *udev)
0453 {
0454     struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
0455 
0456     return hcd->self.root_hub == usbv->parent;
0457 }
0458 
0459 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
0460 {
0461     return !list_empty(&udev->ep_list_head);
0462 }
0463 
0464 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
0465                            struct urb *urb)
0466 {
0467     struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
0468     struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
0469 
0470     /* usbhsh_device_attach() is still not called */
0471     if (!udev)
0472         return NULL;
0473 
0474     /* if it is device0, return it */
0475     if (0 == usb_pipedevice(urb->pipe))
0476         return usbhsh_device0(hpriv);
0477 
0478     /* return attached device */
0479     return udev;
0480 }
0481 
0482 static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
0483                          struct urb *urb)
0484 {
0485     struct usbhsh_device *udev = NULL;
0486     struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
0487     struct usbhsh_device *pos;
0488     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
0489     struct device *dev = usbhsh_hcd_to_dev(hcd);
0490     struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
0491     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0492     unsigned long flags;
0493     u16 upphub, hubport;
0494     int i;
0495 
0496     /*
0497      * This function should be called only while urb is pointing to device0.
0498      * It will attach unused usbhsh_device to urb (usbv),
0499      * and initialize device0.
0500      * You can use usbhsh_device_get() to get "current" udev,
0501      * and usbhsh_usbv_to_udev() is for "attached" udev.
0502      */
0503     if (0 != usb_pipedevice(urb->pipe)) {
0504         dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
0505         return NULL;
0506     }
0507 
0508     /********************  spin lock ********************/
0509     usbhs_lock(priv, flags);
0510 
0511     /*
0512      * find unused device
0513      */
0514     usbhsh_for_each_udev(pos, hpriv, i) {
0515         if (usbhsh_udev_is_used(pos))
0516             continue;
0517         udev = pos;
0518         break;
0519     }
0520 
0521     if (udev) {
0522         /*
0523          * usbhsh_usbv_to_udev()
0524          * usbhsh_udev_to_usbv()
0525          * will be enable
0526          */
0527         dev_set_drvdata(&usbv->dev, udev);
0528         udev->usbv = usbv;
0529     }
0530 
0531     usbhs_unlock(priv, flags);
0532     /********************  spin unlock ******************/
0533 
0534     if (!udev) {
0535         dev_err(dev, "no free usbhsh_device\n");
0536         return NULL;
0537     }
0538 
0539     if (usbhsh_device_has_endpoint(udev)) {
0540         dev_warn(dev, "udev have old endpoint\n");
0541         usbhsh_endpoint_detach_all(hpriv, udev);
0542     }
0543 
0544     if (usbhsh_device_has_endpoint(udev0)) {
0545         dev_warn(dev, "udev0 have old endpoint\n");
0546         usbhsh_endpoint_detach_all(hpriv, udev0);
0547     }
0548 
0549     /* uep will be attached */
0550     INIT_LIST_HEAD(&udev0->ep_list_head);
0551     INIT_LIST_HEAD(&udev->ep_list_head);
0552 
0553     /*
0554      * set device0 config
0555      */
0556     usbhs_set_device_config(priv,
0557                 0, 0, 0, usbv->speed);
0558 
0559     /*
0560      * set new device config
0561      */
0562     upphub  = 0;
0563     hubport = 0;
0564     if (!usbhsh_connected_to_rhdev(hcd, udev)) {
0565         /* if udev is not connected to rhdev, it means parent is Hub */
0566         struct usbhsh_device *parent = usbhsh_device_parent(udev);
0567 
0568         upphub  = usbhsh_device_number(hpriv, parent);
0569         hubport = usbhsh_device_hubport(udev);
0570 
0571         dev_dbg(dev, "%s connected to Hub [%d:%d](%p)\n", __func__,
0572             upphub, hubport, parent);
0573     }
0574 
0575     usbhs_set_device_config(priv,
0576                    usbhsh_device_number(hpriv, udev),
0577                    upphub, hubport, usbv->speed);
0578 
0579     dev_dbg(dev, "%s [%d](%p)\n", __func__,
0580         usbhsh_device_number(hpriv, udev), udev);
0581 
0582     return udev;
0583 }
0584 
0585 static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
0586                    struct usbhsh_device *udev)
0587 {
0588     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
0589     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0590     struct device *dev = usbhsh_hcd_to_dev(hcd);
0591     struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
0592     unsigned long flags;
0593 
0594     dev_dbg(dev, "%s [%d](%p)\n", __func__,
0595         usbhsh_device_number(hpriv, udev), udev);
0596 
0597     if (usbhsh_device_has_endpoint(udev)) {
0598         dev_warn(dev, "udev still have endpoint\n");
0599         usbhsh_endpoint_detach_all(hpriv, udev);
0600     }
0601 
0602     /*
0603      * There is nothing to do if it is device0.
0604      * see
0605      *  usbhsh_device_attach()
0606      *  usbhsh_device_get()
0607      */
0608     if (0 == usbhsh_device_number(hpriv, udev))
0609         return;
0610 
0611     /********************  spin lock ********************/
0612     usbhs_lock(priv, flags);
0613 
0614     /*
0615      * usbhsh_usbv_to_udev()
0616      * usbhsh_udev_to_usbv()
0617      * will be disable
0618      */
0619     dev_set_drvdata(&usbv->dev, NULL);
0620     udev->usbv = NULL;
0621 
0622     usbhs_unlock(priv, flags);
0623     /********************  spin unlock ******************/
0624 }
0625 
0626 /*
0627  *      queue push/pop
0628  */
0629 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
0630 {
0631     struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
0632     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
0633     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
0634     struct urb *urb = ureq->urb;
0635     struct device *dev = usbhs_priv_to_dev(priv);
0636     int status = 0;
0637 
0638     dev_dbg(dev, "%s\n", __func__);
0639 
0640     if (!urb) {
0641         dev_warn(dev, "pkt doesn't have urb\n");
0642         return;
0643     }
0644 
0645     if (!usbhsh_is_running(hpriv))
0646         status = -ESHUTDOWN;
0647 
0648     urb->actual_length = pkt->actual;
0649 
0650     usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
0651     usbhsh_ureq_free(hpriv, ureq);
0652 
0653     usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
0654 
0655     usb_hcd_unlink_urb_from_ep(hcd, urb);
0656     usb_hcd_giveback_urb(hcd, urb, status);
0657 }
0658 
0659 static int usbhsh_queue_push(struct usb_hcd *hcd,
0660                  struct urb *urb,
0661                  gfp_t mem_flags)
0662 {
0663     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
0664     struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
0665     struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
0666     struct device *dev = usbhsh_hcd_to_dev(hcd);
0667     struct usbhsh_request *ureq;
0668     void *buf;
0669     int len, sequence;
0670 
0671     if (usb_pipeisoc(urb->pipe)) {
0672         dev_err(dev, "pipe iso is not supported now\n");
0673         return -EIO;
0674     }
0675 
0676     /* this ureq will be freed on usbhsh_queue_done() */
0677     ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
0678     if (unlikely(!ureq)) {
0679         dev_err(dev, "ureq alloc fail\n");
0680         return -ENOMEM;
0681     }
0682 
0683     if (usb_pipein(urb->pipe))
0684         pipe->handler = &usbhs_fifo_dma_pop_handler;
0685     else
0686         pipe->handler = &usbhs_fifo_dma_push_handler;
0687 
0688     buf = (void *)(urb->transfer_buffer + urb->actual_length);
0689     len = urb->transfer_buffer_length - urb->actual_length;
0690 
0691     sequence = usb_gettoggle(urb->dev,
0692                  usb_pipeendpoint(urb->pipe),
0693                  usb_pipeout(urb->pipe));
0694 
0695     dev_dbg(dev, "%s\n", __func__);
0696     usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
0697                buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
0698                sequence);
0699 
0700     usbhs_pkt_start(pipe);
0701 
0702     return 0;
0703 }
0704 
0705 static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
0706                    struct usbhs_pipe *pipe)
0707 {
0708     struct usbhs_pkt *pkt;
0709 
0710     while (1) {
0711         pkt = usbhs_pkt_pop(pipe, NULL);
0712         if (!pkt)
0713             break;
0714 
0715         /*
0716          * if all packet are gone, usbhsh_endpoint_disable()
0717          * will be called.
0718          * then, attached device/endpoint/pipe will be detached
0719          */
0720         usbhsh_queue_done(priv, pkt);
0721     }
0722 }
0723 
0724 static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
0725 {
0726     struct usbhs_pipe *pos;
0727     int i;
0728 
0729     usbhs_for_each_pipe_with_dcp(pos, priv, i)
0730         usbhsh_queue_force_pop(priv, pos);
0731 }
0732 
0733 /*
0734  *      DCP setup stage
0735  */
0736 static int usbhsh_is_request_address(struct urb *urb)
0737 {
0738     struct usb_ctrlrequest *req;
0739 
0740     req = (struct usb_ctrlrequest *)urb->setup_packet;
0741 
0742     if ((DeviceOutRequest    == req->bRequestType << 8) &&
0743         (USB_REQ_SET_ADDRESS == req->bRequest))
0744         return 1;
0745     else
0746         return 0;
0747 }
0748 
0749 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
0750                        struct urb *urb,
0751                        struct usbhs_pipe *pipe)
0752 {
0753     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0754     struct usb_ctrlrequest req;
0755     struct device *dev = usbhs_priv_to_dev(priv);
0756 
0757     /*
0758      * wait setup packet ACK
0759      * see
0760      *  usbhsh_irq_setup_ack()
0761      *  usbhsh_irq_setup_err()
0762      */
0763     init_completion(&hpriv->setup_ack_done);
0764 
0765     /* copy original request */
0766     memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
0767 
0768     /*
0769      * renesas_usbhs can not use original usb address.
0770      * see HARDWARE LIMITATION.
0771      * modify usb address here to use attached device.
0772      * see usbhsh_device_attach()
0773      */
0774     if (usbhsh_is_request_address(urb)) {
0775         struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
0776         struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
0777 
0778         /* udev is a attached device */
0779         req.wValue = usbhsh_device_number(hpriv, udev);
0780         dev_dbg(dev, "create new address - %d\n", req.wValue);
0781     }
0782 
0783     /* set request */
0784     usbhs_usbreq_set_val(priv, &req);
0785 
0786     /*
0787      * wait setup packet ACK
0788      */
0789     wait_for_completion(&hpriv->setup_ack_done);
0790 
0791     dev_dbg(dev, "%s done\n", __func__);
0792 }
0793 
0794 /*
0795  *      DCP data stage
0796  */
0797 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
0798                       struct usbhs_pkt *pkt)
0799 {
0800     struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
0801     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
0802 
0803     /* this ureq was connected to urb when usbhsh_urb_enqueue()  */
0804 
0805     usbhsh_ureq_free(hpriv, ureq);
0806 }
0807 
0808 static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
0809                      struct urb *urb,
0810                      struct usbhs_pipe *pipe,
0811                      gfp_t mem_flags)
0812 
0813 {
0814     struct usbhsh_request *ureq;
0815 
0816     /* this ureq will be freed on usbhsh_data_stage_packet_done() */
0817     ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
0818     if (unlikely(!ureq))
0819         return -ENOMEM;
0820 
0821     if (usb_pipein(urb->pipe))
0822         pipe->handler = &usbhs_dcp_data_stage_in_handler;
0823     else
0824         pipe->handler = &usbhs_dcp_data_stage_out_handler;
0825 
0826     usbhs_pkt_push(pipe, &ureq->pkt,
0827                usbhsh_data_stage_packet_done,
0828                urb->transfer_buffer,
0829                urb->transfer_buffer_length,
0830                (urb->transfer_flags & URB_ZERO_PACKET),
0831                -1);
0832 
0833     return 0;
0834 }
0835 
0836 /*
0837  *      DCP status stage
0838  */
0839 static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
0840                         struct urb *urb,
0841                         struct usbhs_pipe *pipe,
0842                         gfp_t mem_flags)
0843 {
0844     struct usbhsh_request *ureq;
0845 
0846     /* This ureq will be freed on usbhsh_queue_done() */
0847     ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
0848     if (unlikely(!ureq))
0849         return -ENOMEM;
0850 
0851     if (usb_pipein(urb->pipe))
0852         pipe->handler = &usbhs_dcp_status_stage_in_handler;
0853     else
0854         pipe->handler = &usbhs_dcp_status_stage_out_handler;
0855 
0856     usbhs_pkt_push(pipe, &ureq->pkt,
0857                usbhsh_queue_done,
0858                NULL,
0859                urb->transfer_buffer_length,
0860                0, -1);
0861 
0862     return 0;
0863 }
0864 
0865 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
0866                  struct urb *urb,
0867                  gfp_t mflags)
0868 {
0869     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
0870     struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
0871     struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
0872     struct device *dev = usbhsh_hcd_to_dev(hcd);
0873     int ret;
0874 
0875     dev_dbg(dev, "%s\n", __func__);
0876 
0877     /*
0878      * setup stage
0879      *
0880      * usbhsh_send_setup_stage_packet() wait SACK/SIGN
0881      */
0882     usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
0883 
0884     /*
0885      * data stage
0886      *
0887      * It is pushed only when urb has buffer.
0888      */
0889     if (urb->transfer_buffer_length) {
0890         ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
0891         if (ret < 0) {
0892             dev_err(dev, "data stage failed\n");
0893             return ret;
0894         }
0895     }
0896 
0897     /*
0898      * status stage
0899      */
0900     ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
0901     if (ret < 0) {
0902         dev_err(dev, "status stage failed\n");
0903         return ret;
0904     }
0905 
0906     /*
0907      * start pushed packets
0908      */
0909     usbhs_pkt_start(pipe);
0910 
0911     return 0;
0912 }
0913 
0914 /*
0915  *      dma map functions
0916  */
0917 static int usbhsh_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
0918                    int map)
0919 {
0920     if (map) {
0921         struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
0922         struct urb *urb = ureq->urb;
0923 
0924         /* it can not use scatter/gather */
0925         if (urb->num_sgs)
0926             return -EINVAL;
0927 
0928         pkt->dma = urb->transfer_dma;
0929         if (!pkt->dma)
0930             return -EINVAL;
0931     }
0932 
0933     return 0;
0934 }
0935 
0936 /*
0937  *      for hc_driver
0938  */
0939 static int usbhsh_host_start(struct usb_hcd *hcd)
0940 {
0941     return 0;
0942 }
0943 
0944 static void usbhsh_host_stop(struct usb_hcd *hcd)
0945 {
0946 }
0947 
0948 static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
0949                   struct urb *urb,
0950                   gfp_t mem_flags)
0951 {
0952     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
0953     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
0954     struct device *dev = usbhs_priv_to_dev(priv);
0955     struct usb_host_endpoint *ep = urb->ep;
0956     struct usbhsh_device *new_udev = NULL;
0957     int is_dir_in = usb_pipein(urb->pipe);
0958     int ret;
0959 
0960     dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
0961 
0962     if (!usbhsh_is_running(hpriv)) {
0963         ret = -EIO;
0964         dev_err(dev, "host is not running\n");
0965         goto usbhsh_urb_enqueue_error_not_linked;
0966     }
0967 
0968     ret = usb_hcd_link_urb_to_ep(hcd, urb);
0969     if (ret) {
0970         dev_err(dev, "urb link failed\n");
0971         goto usbhsh_urb_enqueue_error_not_linked;
0972     }
0973 
0974     /*
0975      * attach udev if needed
0976      * see [image of mod_host]
0977      */
0978     if (!usbhsh_device_get(hpriv, urb)) {
0979         new_udev = usbhsh_device_attach(hpriv, urb);
0980         if (!new_udev) {
0981             ret = -EIO;
0982             dev_err(dev, "device attach failed\n");
0983             goto usbhsh_urb_enqueue_error_not_linked;
0984         }
0985     }
0986 
0987     /*
0988      * attach endpoint if needed
0989      * see [image of mod_host]
0990      */
0991     if (!usbhsh_ep_to_uep(ep)) {
0992         ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
0993         if (ret < 0) {
0994             dev_err(dev, "endpoint attach failed\n");
0995             goto usbhsh_urb_enqueue_error_free_device;
0996         }
0997     }
0998 
0999     /*
1000      * attach pipe to endpoint
1001      * see [image of mod_host]
1002      */
1003     ret = usbhsh_pipe_attach(hpriv, urb);
1004     if (ret < 0) {
1005         dev_err(dev, "pipe attach failed\n");
1006         goto usbhsh_urb_enqueue_error_free_endpoint;
1007     }
1008 
1009     /*
1010      * push packet
1011      */
1012     if (usb_pipecontrol(urb->pipe))
1013         ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
1014     else
1015         ret = usbhsh_queue_push(hcd, urb, mem_flags);
1016 
1017     return ret;
1018 
1019 usbhsh_urb_enqueue_error_free_endpoint:
1020     usbhsh_endpoint_detach(hpriv, ep);
1021 usbhsh_urb_enqueue_error_free_device:
1022     if (new_udev)
1023         usbhsh_device_detach(hpriv, new_udev);
1024 usbhsh_urb_enqueue_error_not_linked:
1025 
1026     dev_dbg(dev, "%s error\n", __func__);
1027 
1028     return ret;
1029 }
1030 
1031 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1032 {
1033     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1034     struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1035 
1036     if (ureq) {
1037         struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1038         struct usbhs_pkt *pkt = &ureq->pkt;
1039 
1040         usbhs_pkt_pop(pkt->pipe, pkt);
1041         usbhsh_queue_done(priv, pkt);
1042     }
1043 
1044     return 0;
1045 }
1046 
1047 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1048                     struct usb_host_endpoint *ep)
1049 {
1050     struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1051     struct usbhsh_device *udev;
1052     struct usbhsh_hpriv *hpriv;
1053 
1054     /*
1055      * this function might be called manytimes by same hcd/ep
1056      * in-endpoint == out-endpoint if ep == dcp.
1057      */
1058     if (!uep)
1059         return;
1060 
1061     udev    = usbhsh_uep_to_udev(uep);
1062     hpriv   = usbhsh_hcd_to_hpriv(hcd);
1063 
1064     usbhsh_endpoint_detach(hpriv, ep);
1065 
1066     /*
1067      * if there is no endpoint,
1068      * free device
1069      */
1070     if (!usbhsh_device_has_endpoint(udev))
1071         usbhsh_device_detach(hpriv, udev);
1072 }
1073 
1074 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1075 {
1076     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1077     int roothub_id = 1; /* only 1 root hub */
1078 
1079     /*
1080      * does port stat was changed ?
1081      * check USB_PORT_STAT_C_xxx << 16
1082      */
1083     if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1084         *buf = (1 << roothub_id);
1085     else
1086         *buf = 0;
1087 
1088     return !!(*buf);
1089 }
1090 
1091 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1092                     u16 typeReq, u16 wValue,
1093                     u16 wIndex, char *buf, u16 wLength)
1094 {
1095     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1096     struct device *dev = usbhs_priv_to_dev(priv);
1097 
1098     switch (wValue) {
1099     case C_HUB_OVER_CURRENT:
1100     case C_HUB_LOCAL_POWER:
1101         dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1102         return 0;
1103     }
1104 
1105     return -EPIPE;
1106 }
1107 
1108 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1109                      u16 typeReq, u16 wValue,
1110                      u16 wIndex, char *buf, u16 wLength)
1111 {
1112     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1113     struct device *dev = usbhs_priv_to_dev(priv);
1114     int enable = (typeReq == SetPortFeature);
1115     int speed, i, timeout = 128;
1116     int roothub_id = 1; /* only 1 root hub */
1117 
1118     /* common error */
1119     if (wIndex > roothub_id || wLength != 0)
1120         return -EPIPE;
1121 
1122     /* check wValue */
1123     switch (wValue) {
1124     case USB_PORT_FEAT_POWER:
1125         usbhs_vbus_ctrl(priv, enable);
1126         dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1127         break;
1128 
1129     case USB_PORT_FEAT_ENABLE:
1130     case USB_PORT_FEAT_SUSPEND:
1131     case USB_PORT_FEAT_C_ENABLE:
1132     case USB_PORT_FEAT_C_SUSPEND:
1133     case USB_PORT_FEAT_C_CONNECTION:
1134     case USB_PORT_FEAT_C_OVER_CURRENT:
1135     case USB_PORT_FEAT_C_RESET:
1136         dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1137         break;
1138 
1139     case USB_PORT_FEAT_RESET:
1140         if (!enable)
1141             break;
1142 
1143         usbhsh_port_stat_clear(hpriv,
1144                        USB_PORT_STAT_HIGH_SPEED |
1145                        USB_PORT_STAT_LOW_SPEED);
1146 
1147         usbhsh_queue_force_pop_all(priv);
1148 
1149         usbhs_bus_send_reset(priv);
1150         msleep(20);
1151         usbhs_bus_send_sof_enable(priv);
1152 
1153         for (i = 0; i < timeout ; i++) {
1154             switch (usbhs_bus_get_speed(priv)) {
1155             case USB_SPEED_LOW:
1156                 speed = USB_PORT_STAT_LOW_SPEED;
1157                 goto got_usb_bus_speed;
1158             case USB_SPEED_HIGH:
1159                 speed = USB_PORT_STAT_HIGH_SPEED;
1160                 goto got_usb_bus_speed;
1161             case USB_SPEED_FULL:
1162                 speed = 0;
1163                 goto got_usb_bus_speed;
1164             }
1165 
1166             msleep(20);
1167         }
1168         return -EPIPE;
1169 
1170 got_usb_bus_speed:
1171         usbhsh_port_stat_set(hpriv, speed);
1172         usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1173 
1174         dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1175             __func__, speed);
1176 
1177         /* status change is not needed */
1178         return 0;
1179 
1180     default:
1181         return -EPIPE;
1182     }
1183 
1184     /* set/clear status */
1185     if (enable)
1186         usbhsh_port_stat_set(hpriv, (1 << wValue));
1187     else
1188         usbhsh_port_stat_clear(hpriv, (1 << wValue));
1189 
1190     return 0;
1191 }
1192 
1193 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1194                    u16 typeReq, u16 wValue,
1195                    u16 wIndex, char *buf, u16 wLength)
1196 {
1197     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1198     struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1199     struct device *dev = usbhs_priv_to_dev(priv);
1200     int roothub_id = 1; /* only 1 root hub */
1201 
1202     switch (typeReq) {
1203     case GetHubStatus:
1204         dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1205 
1206         *buf = 0x00;
1207         break;
1208 
1209     case GetPortStatus:
1210         if (wIndex != roothub_id)
1211             return -EPIPE;
1212 
1213         dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1214         *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1215         break;
1216 
1217     case GetHubDescriptor:
1218         desc->bDescriptorType       = USB_DT_HUB;
1219         desc->bHubContrCurrent      = 0;
1220         desc->bNbrPorts         = roothub_id;
1221         desc->bDescLength       = 9;
1222         desc->bPwrOn2PwrGood        = 0;
1223         desc->wHubCharacteristics   =
1224             cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
1225         desc->u.hs.DeviceRemovable[0]   = (roothub_id << 1);
1226         desc->u.hs.DeviceRemovable[1]   = ~0;
1227         dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1228         break;
1229     }
1230 
1231     return 0;
1232 }
1233 
1234 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1235                   u16 wIndex, char *buf, u16 wLength)
1236 {
1237     struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1238     struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1239     struct device *dev = usbhs_priv_to_dev(priv);
1240     int ret = -EPIPE;
1241 
1242     switch (typeReq) {
1243 
1244     /* Hub Feature */
1245     case ClearHubFeature:
1246     case SetHubFeature:
1247         ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1248                            wValue, wIndex, buf, wLength);
1249         break;
1250 
1251     /* Port Feature */
1252     case SetPortFeature:
1253     case ClearPortFeature:
1254         ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1255                         wValue, wIndex, buf, wLength);
1256         break;
1257 
1258     /* Get status */
1259     case GetHubStatus:
1260     case GetPortStatus:
1261     case GetHubDescriptor:
1262         ret = __usbhsh_hub_get_status(hpriv, typeReq,
1263                           wValue, wIndex, buf, wLength);
1264         break;
1265     }
1266 
1267     dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1268         typeReq, ret, usbhsh_port_stat_get(hpriv));
1269 
1270     return ret;
1271 }
1272 
1273 static int usbhsh_bus_nop(struct usb_hcd *hcd)
1274 {
1275     /* nothing to do */
1276     return 0;
1277 }
1278 
1279 static const struct hc_driver usbhsh_driver = {
1280     .description =      usbhsh_hcd_name,
1281     .hcd_priv_size =    sizeof(struct usbhsh_hpriv),
1282 
1283     /*
1284      * generic hardware linkage
1285      */
1286     .flags =        HCD_DMA | HCD_USB2,
1287 
1288     .start =        usbhsh_host_start,
1289     .stop =         usbhsh_host_stop,
1290 
1291     /*
1292      * managing i/o requests and associated device resources
1293      */
1294     .urb_enqueue =      usbhsh_urb_enqueue,
1295     .urb_dequeue =      usbhsh_urb_dequeue,
1296     .endpoint_disable = usbhsh_endpoint_disable,
1297 
1298     /*
1299      * root hub
1300      */
1301     .hub_status_data =  usbhsh_hub_status_data,
1302     .hub_control =      usbhsh_hub_control,
1303     .bus_suspend =      usbhsh_bus_nop,
1304     .bus_resume =       usbhsh_bus_nop,
1305 };
1306 
1307 /*
1308  *      interrupt functions
1309  */
1310 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1311                 struct usbhs_irq_state *irq_state)
1312 {
1313     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1314     struct device *dev = usbhs_priv_to_dev(priv);
1315 
1316     dev_dbg(dev, "device attached\n");
1317 
1318     usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1319     usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1320 
1321     /*
1322      * attch interrupt might happen infinitely on some device
1323      * (on self power USB hub ?)
1324      * disable it here.
1325      *
1326      * usbhsh_is_running() becomes effective
1327      * according to this process.
1328      * see
1329      *  usbhsh_is_running()
1330      *  usbhsh_urb_enqueue()
1331      */
1332     hpriv->mod.irq_attch = NULL;
1333     usbhs_irq_callback_update(priv, &hpriv->mod);
1334 
1335     return 0;
1336 }
1337 
1338 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1339                struct usbhs_irq_state *irq_state)
1340 {
1341     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1342     struct device *dev = usbhs_priv_to_dev(priv);
1343 
1344     dev_dbg(dev, "device detached\n");
1345 
1346     usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1347     usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1348 
1349     /*
1350      * enable attch interrupt again
1351      *
1352      * usbhsh_is_running() becomes invalid
1353      * according to this process.
1354      * see
1355      *  usbhsh_is_running()
1356      *  usbhsh_urb_enqueue()
1357      */
1358     hpriv->mod.irq_attch = usbhsh_irq_attch;
1359     usbhs_irq_callback_update(priv, &hpriv->mod);
1360 
1361     /*
1362      * usbhsh_queue_force_pop_all() should be called
1363      * after usbhsh_is_running() becomes invalid.
1364      */
1365     usbhsh_queue_force_pop_all(priv);
1366 
1367     return 0;
1368 }
1369 
1370 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1371                 struct usbhs_irq_state *irq_state)
1372 {
1373     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1374     struct device *dev = usbhs_priv_to_dev(priv);
1375 
1376     dev_dbg(dev, "setup packet OK\n");
1377 
1378     complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1379 
1380     return 0;
1381 }
1382 
1383 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1384                 struct usbhs_irq_state *irq_state)
1385 {
1386     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1387     struct device *dev = usbhs_priv_to_dev(priv);
1388 
1389     dev_dbg(dev, "setup packet Err\n");
1390 
1391     complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1392 
1393     return 0;
1394 }
1395 
1396 /*
1397  *      module start/stop
1398  */
1399 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1400 {
1401     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1402     struct usbhs_pipe *pipe;
1403     struct renesas_usbhs_driver_pipe_config *pipe_configs =
1404                     usbhs_get_dparam(priv, pipe_configs);
1405     int pipe_size = usbhs_get_dparam(priv, pipe_size);
1406     int old_type, dir_in, i;
1407 
1408     /* init all pipe */
1409     old_type = USB_ENDPOINT_XFER_CONTROL;
1410     for (i = 0; i < pipe_size; i++) {
1411 
1412         /*
1413          * data "output" will be finished as soon as possible,
1414          * but there is no guaranty at data "input" case.
1415          *
1416          * "input" needs "standby" pipe.
1417          * So, "input" direction pipe > "output" direction pipe
1418          * is good idea.
1419          *
1420          * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1421          * and the other will be input direction here.
1422          *
1423          * ex)
1424          * ...
1425          * USB_ENDPOINT_XFER_ISOC -> dir out
1426          * USB_ENDPOINT_XFER_ISOC -> dir in
1427          * USB_ENDPOINT_XFER_BULK -> dir out
1428          * USB_ENDPOINT_XFER_BULK -> dir in
1429          * USB_ENDPOINT_XFER_BULK -> dir in
1430          * ...
1431          */
1432         dir_in = (pipe_configs[i].type == old_type);
1433         old_type = pipe_configs[i].type;
1434 
1435         if (USB_ENDPOINT_XFER_CONTROL == pipe_configs[i].type) {
1436             pipe = usbhs_dcp_malloc(priv);
1437             usbhsh_hpriv_to_dcp(hpriv) = pipe;
1438         } else {
1439             pipe = usbhs_pipe_malloc(priv,
1440                          pipe_configs[i].type,
1441                          dir_in);
1442         }
1443 
1444         pipe->mod_private = NULL;
1445     }
1446 }
1447 
1448 static int usbhsh_start(struct usbhs_priv *priv)
1449 {
1450     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1451     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1452     struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1453     struct device *dev = usbhs_priv_to_dev(priv);
1454     int ret;
1455 
1456     /* add hcd */
1457     ret = usb_add_hcd(hcd, 0, 0);
1458     if (ret < 0)
1459         return 0;
1460     device_wakeup_enable(hcd->self.controller);
1461 
1462     /*
1463      * pipe initialize and enable DCP
1464      */
1465     usbhs_fifo_init(priv);
1466     usbhs_pipe_init(priv,
1467             usbhsh_dma_map_ctrl);
1468     usbhsh_pipe_init_for_host(priv);
1469 
1470     /*
1471      * system config enble
1472      * - HI speed
1473      * - host
1474      * - usb module
1475      */
1476     usbhs_sys_host_ctrl(priv, 1);
1477 
1478     /*
1479      * enable irq callback
1480      */
1481     mod->irq_attch      = usbhsh_irq_attch;
1482     mod->irq_dtch       = usbhsh_irq_dtch;
1483     mod->irq_sack       = usbhsh_irq_setup_ack;
1484     mod->irq_sign       = usbhsh_irq_setup_err;
1485     usbhs_irq_callback_update(priv, mod);
1486 
1487     dev_dbg(dev, "start host\n");
1488 
1489     return ret;
1490 }
1491 
1492 static int usbhsh_stop(struct usbhs_priv *priv)
1493 {
1494     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1495     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1496     struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1497     struct device *dev = usbhs_priv_to_dev(priv);
1498 
1499     /*
1500      * disable irq callback
1501      */
1502     mod->irq_attch  = NULL;
1503     mod->irq_dtch   = NULL;
1504     mod->irq_sack   = NULL;
1505     mod->irq_sign   = NULL;
1506     usbhs_irq_callback_update(priv, mod);
1507 
1508     usb_remove_hcd(hcd);
1509 
1510     /* disable sys */
1511     usbhs_sys_host_ctrl(priv, 0);
1512 
1513     dev_dbg(dev, "quit host\n");
1514 
1515     return 0;
1516 }
1517 
1518 int usbhs_mod_host_probe(struct usbhs_priv *priv)
1519 {
1520     struct usbhsh_hpriv *hpriv;
1521     struct usb_hcd *hcd;
1522     struct usbhsh_device *udev;
1523     struct device *dev = usbhs_priv_to_dev(priv);
1524     int i;
1525 
1526     /* initialize hcd */
1527     hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1528     if (!hcd) {
1529         dev_err(dev, "Failed to create hcd\n");
1530         return -ENOMEM;
1531     }
1532     hcd->has_tt = 1; /* for low/full speed */
1533 
1534     /*
1535      * CAUTION
1536      *
1537      * There is no guarantee that it is possible to access usb module here.
1538      * Don't accesses to it.
1539      * The accesse will be enable after "usbhsh_start"
1540      */
1541 
1542     hpriv = usbhsh_hcd_to_hpriv(hcd);
1543 
1544     /*
1545      * register itself
1546      */
1547     usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1548 
1549     /* init hpriv */
1550     hpriv->mod.name     = "host";
1551     hpriv->mod.start    = usbhsh_start;
1552     hpriv->mod.stop     = usbhsh_stop;
1553     usbhsh_port_stat_init(hpriv);
1554 
1555     /* init all device */
1556     usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1557         udev->usbv  = NULL;
1558         INIT_LIST_HEAD(&udev->ep_list_head);
1559     }
1560 
1561     dev_info(dev, "host probed\n");
1562 
1563     return 0;
1564 }
1565 
1566 int usbhs_mod_host_remove(struct usbhs_priv *priv)
1567 {
1568     struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1569     struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1570 
1571     usb_put_hcd(hcd);
1572 
1573     return 0;
1574 }