static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv, struct usbhsh_ep *uep) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usbhs_pipe *pipe; struct device *dev = usbhs_priv_to_dev(priv); unsigned long flags; /******************** spin lock ********************/ usbhs_lock(priv, flags); pipe = usbhsh_uep_to_pipe(uep); if (unlikely(!pipe)) { dev_err(dev, "uep doens't have pipe\n"); } else { struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep); struct usbhsh_device *udev = usbhsh_uep_to_udev(uep); /* detach pipe from uep */ usbhsh_uep_to_pipe(uep) = NULL; usbhsh_pipe_to_uep(pipe) = NULL; dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__, usbhsh_device_number(hpriv, udev), usb_endpoint_num(&ep->desc), usbhs_pipe_name(pipe)); } usbhs_unlock(priv, flags); /******************** spin unlock ******************/ }
static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv, struct usb_host_endpoint *ep) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); unsigned long flags; if (!uep) return; dev_dbg(dev, "%s [%d-%d]\n", __func__, usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), usb_endpoint_num(&ep->desc)); if (usbhsh_uep_to_pipe(uep)) usbhsh_pipe_detach(hpriv, uep); /******************** spin lock ********************/ usbhs_lock(priv, flags); /* remove this endpoint from udev */ list_del_init(&uep->ep_list); usbhsh_uep_to_udev(uep) = NULL; usbhsh_uep_to_ep(uep) = NULL; usbhsh_ep_to_uep(ep) = NULL; usbhs_unlock(priv, flags); /******************** spin unlock ******************/ kfree(uep); }
static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) { struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); if (ureq) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usbhs_pkt *pkt = &ureq->pkt; usbhs_pkt_pop(pkt->pipe, pkt); usbhsh_queue_done(priv, pkt); } return 0; }
static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); switch (wValue) { case C_HUB_OVER_CURRENT: case C_HUB_LOCAL_POWER: dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); return 0; } return -EPIPE; }
static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, struct urb *urb, struct usbhs_pipe *pipe) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usb_ctrlrequest req; struct device *dev = usbhs_priv_to_dev(priv); /* * wait setup packet ACK * see * usbhsh_irq_setup_ack() * usbhsh_irq_setup_err() */ init_completion(&hpriv->setup_ack_done); /* copy original request */ memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); /* * renesas_usbhs can not use original usb address. * see HARDWARE LIMITATION. * modify usb address here to use attached device. * see usbhsh_device_attach() */ if (usbhsh_is_request_address(urb)) { struct usb_device *usbv = usbhsh_urb_to_usbv(urb); struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); /* udev is a attached device */ req.wValue = usbhsh_device_number(hpriv, udev); dev_dbg(dev, "create new address - %d\n", req.wValue); } /* set request */ usbhs_usbreq_set_val(priv, &req); /* * wait setup packet ACK */ wait_for_completion(&hpriv->setup_ack_done); dev_dbg(dev, "%s done\n", __func__); }
/* * req alloc/free */ static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv, struct urb *urb, gfp_t mem_flags) { struct usbhsh_request *ureq; struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); if (!ureq) { dev_err(dev, "ureq alloc fail\n"); return NULL; } usbhs_pkt_init(&ureq->pkt); ureq->urb = urb; usbhsh_urb_to_ureq(urb) = ureq; return ureq; }
static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) { struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); int roothub_id = 1; /* only 1 root hub */ /* * does port stat was changed ? * check USB_PORT_STAT_C_xxx << 16 */ if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) *buf = (1 << roothub_id); else *buf = 0; dev_dbg(dev, "%s (%02x)\n", __func__, *buf); return !!(*buf); }
/* * endpoint control */ static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv, struct urb *urb, gfp_t mem_flags) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); struct usb_host_endpoint *ep = urb->ep; struct usbhsh_ep *uep; struct device *dev = usbhs_priv_to_dev(priv); struct usb_endpoint_descriptor *desc = &ep->desc; unsigned long flags; uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); if (!uep) { dev_err(dev, "usbhsh_ep alloc fail\n"); return -ENOMEM; } /******************** spin lock ********************/ usbhs_lock(priv, flags); /* * init endpoint */ uep->counter = 0; INIT_LIST_HEAD(&uep->ep_list); list_add_tail(&uep->ep_list, &udev->ep_list_head); usbhsh_uep_to_udev(uep) = udev; usbhsh_uep_to_ep(uep) = ep; usbhsh_ep_to_uep(ep) = uep; usbhs_unlock(priv, flags); /******************** spin unlock ******************/ dev_dbg(dev, "%s [%d-%d]\n", __func__, usbhsh_device_number(hpriv, udev), usb_endpoint_num(desc)); return 0; }
static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv, struct usbhsh_device *udev) { struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhsh_hcd_to_dev(hcd); struct usb_device *usbv = usbhsh_udev_to_usbv(udev); unsigned long flags; dev_dbg(dev, "%s [%d](%p)\n", __func__, usbhsh_device_number(hpriv, udev), udev); if (usbhsh_device_has_endpoint(udev)) { dev_warn(dev, "udev still have endpoint\n"); usbhsh_endpoint_detach_all(hpriv, udev); } /* * There is nothing to do if it is device0. * see * usbhsh_device_attach() * usbhsh_device_get() */ if (0 == usbhsh_device_number(hpriv, udev)) return; /******************** spin lock ********************/ usbhs_lock(priv, flags); /* * usbhsh_usbv_to_udev() * usbhsh_udev_to_usbv() * will be disable */ dev_set_drvdata(&usbv->dev, NULL); udev->usbv = NULL; usbhs_unlock(priv, flags); /******************** spin unlock ******************/ }
static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; struct device *dev = usbhs_priv_to_dev(priv); int roothub_id = 1; /* only 1 root hub */ switch (typeReq) { case GetHubStatus: dev_dbg(dev, "%s :: GetHubStatus\n", __func__); *buf = 0x00; break; case GetPortStatus: if (wIndex != roothub_id) return -EPIPE; dev_dbg(dev, "%s :: GetPortStatus\n", __func__); *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); break; case GetHubDescriptor: desc->bDescriptorType = USB_DT_HUB; desc->bHubContrCurrent = 0; desc->bNbrPorts = roothub_id; desc->bDescLength = 9; desc->bPwrOn2PwrGood = 0; desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM); desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); desc->u.hs.DeviceRemovable[1] = ~0; dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); break; } return 0; }
static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength) { struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); int ret = -EPIPE; switch (typeReq) { /* Hub Feature */ case ClearHubFeature: case SetHubFeature: ret = __usbhsh_hub_hub_feature(hpriv, typeReq, wValue, wIndex, buf, wLength); break; /* Port Feature */ case SetPortFeature: case ClearPortFeature: ret = __usbhsh_hub_port_feature(hpriv, typeReq, wValue, wIndex, buf, wLength); break; /* Get status */ case GetHubStatus: case GetPortStatus: case GetHubDescriptor: ret = __usbhsh_hub_get_status(hpriv, typeReq, wValue, wIndex, buf, wLength); break; } dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", typeReq, ret, usbhsh_port_stat_get(hpriv)); return ret; }
static int usbhsh_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) { struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); struct usb_host_endpoint *ep = urb->ep; struct usbhsh_device *new_udev = NULL; int is_dir_in = usb_pipein(urb->pipe); int ret; dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); if (!usbhsh_is_running(hpriv)) { ret = -EIO; dev_err(dev, "host is not running\n"); goto usbhsh_urb_enqueue_error_not_linked; } ret = usb_hcd_link_urb_to_ep(hcd, urb); if (ret) { dev_err(dev, "urb link failed\n"); goto usbhsh_urb_enqueue_error_not_linked; } /* * attach udev if needed * see [image of mod_host] */ if (!usbhsh_device_get(hpriv, urb)) { new_udev = usbhsh_device_attach(hpriv, urb); if (!new_udev) { ret = -EIO; dev_err(dev, "device attach failed\n"); goto usbhsh_urb_enqueue_error_not_linked; } } /* * attach endpoint if needed * see [image of mod_host] */ if (!usbhsh_ep_to_uep(ep)) { ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags); if (ret < 0) { dev_err(dev, "endpoint attach failed\n"); goto usbhsh_urb_enqueue_error_free_device; } } /* * attach pipe to endpoint * see [image of mod_host] */ ret = usbhsh_pipe_attach(hpriv, urb); if (ret < 0) { dev_err(dev, "pipe attach failed\n"); goto usbhsh_urb_enqueue_error_free_endpoint; } /* * push packet */ if (usb_pipecontrol(urb->pipe)) ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags); else ret = usbhsh_queue_push(hcd, urb, mem_flags); return ret; usbhsh_urb_enqueue_error_free_endpoint: usbhsh_endpoint_detach(hpriv, ep); usbhsh_urb_enqueue_error_free_device: if (new_udev) usbhsh_device_detach(hpriv, new_udev); usbhsh_urb_enqueue_error_not_linked: dev_dbg(dev, "%s error\n", __func__); return ret; }
static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv, struct urb *urb) { struct usbhsh_device *udev = NULL; struct usbhsh_device *udev0 = usbhsh_device0(hpriv); struct usbhsh_device *pos; struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); struct device *dev = usbhsh_hcd_to_dev(hcd); struct usb_device *usbv = usbhsh_urb_to_usbv(urb); struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); unsigned long flags; u16 upphub, hubport; int i; /* * This function should be called only while urb is pointing to device0. * It will attach unused usbhsh_device to urb (usbv), * and initialize device0. * You can use usbhsh_device_get() to get "current" udev, * and usbhsh_usbv_to_udev() is for "attached" udev. */ if (0 != usb_pipedevice(urb->pipe)) { dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__); return NULL; } /******************** spin lock ********************/ usbhs_lock(priv, flags); /* * find unused device */ usbhsh_for_each_udev(pos, hpriv, i) { if (usbhsh_udev_is_used(pos)) continue; udev = pos; break; } if (udev) { /* * usbhsh_usbv_to_udev() * usbhsh_udev_to_usbv() * will be enable */ dev_set_drvdata(&usbv->dev, udev); udev->usbv = usbv; } usbhs_unlock(priv, flags); /******************** spin unlock ******************/ if (!udev) { dev_err(dev, "no free usbhsh_device\n"); return NULL; } if (usbhsh_device_has_endpoint(udev)) { dev_warn(dev, "udev have old endpoint\n"); usbhsh_endpoint_detach_all(hpriv, udev); } if (usbhsh_device_has_endpoint(udev0)) { dev_warn(dev, "udev0 have old endpoint\n"); usbhsh_endpoint_detach_all(hpriv, udev0); } /* uep will be attached */ INIT_LIST_HEAD(&udev0->ep_list_head); INIT_LIST_HEAD(&udev->ep_list_head); /* * set device0 config */ usbhs_set_device_config(priv, 0, 0, 0, usbv->speed); /* * set new device config */ upphub = 0; hubport = 0; if (!usbhsh_connected_to_rhdev(hcd, udev)) { /* if udev is not connected to rhdev, it means parent is Hub */ struct usbhsh_device *parent = usbhsh_device_parent(udev); upphub = usbhsh_device_number(hpriv, parent); hubport = usbhsh_device_hubport(udev); dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__, upphub, hubport, parent); } usbhs_set_device_config(priv, usbhsh_device_number(hpriv, udev), upphub, hubport, usbv->speed); dev_dbg(dev, "%s [%d](%p)\n", __func__, usbhsh_device_number(hpriv, udev), udev); return udev; }
static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv, struct urb *urb) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); struct usbhs_pipe *pipe; struct usb_endpoint_descriptor *desc = &urb->ep->desc; struct device *dev = usbhs_priv_to_dev(priv); unsigned long flags; int dir_in_req = !!usb_pipein(urb->pipe); int is_dcp = usb_endpoint_xfer_control(desc); int i, dir_in; int ret = -EBUSY; /******************** spin lock ********************/ usbhs_lock(priv, flags); /* * if uep has been attached to pipe, * reuse it */ if (usbhsh_uep_to_pipe(uep)) { ret = 0; goto usbhsh_pipe_attach_done; } usbhs_for_each_pipe_with_dcp(pipe, priv, i) { /* check pipe type */ if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc))) continue; /* check pipe direction if normal pipe */ if (!is_dcp) { dir_in = !!usbhs_pipe_is_dir_in(pipe); if (0 != (dir_in - dir_in_req)) continue; } /* check pipe is free */ if (usbhsh_pipe_to_uep(pipe)) continue; /* * attach pipe to uep * * usbhs_pipe_config_update() should be called after * usbhs_set_device_config() * see * DCPMAXP/PIPEMAXP */ usbhsh_uep_to_pipe(uep) = pipe; usbhsh_pipe_to_uep(pipe) = uep; usbhs_pipe_config_update(pipe, usbhsh_device_number(hpriv, udev), usb_endpoint_num(desc), usb_endpoint_maxp(desc)); dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__, usbhsh_device_number(hpriv, udev), usb_endpoint_num(desc), usbhs_pipe_name(pipe), dir_in_req ? "in" : "out"); ret = 0; break; } usbhsh_pipe_attach_done: if (0 == ret) uep->counter++; usbhs_unlock(priv, flags); /******************** spin unlock ******************/ return ret; }
static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength) { struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); struct device *dev = usbhs_priv_to_dev(priv); int enable = (typeReq == SetPortFeature); int speed, i, timeout = 128; int roothub_id = 1; /* only 1 root hub */ /* common error */ if (wIndex > roothub_id || wLength != 0) return -EPIPE; /* check wValue */ switch (wValue) { case USB_PORT_FEAT_POWER: usbhs_vbus_ctrl(priv, enable); dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); break; case USB_PORT_FEAT_ENABLE: case USB_PORT_FEAT_SUSPEND: case USB_PORT_FEAT_C_ENABLE: case USB_PORT_FEAT_C_SUSPEND: case USB_PORT_FEAT_C_CONNECTION: case USB_PORT_FEAT_C_OVER_CURRENT: case USB_PORT_FEAT_C_RESET: dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); break; case USB_PORT_FEAT_RESET: if (!enable) break; usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED); usbhsh_queue_force_pop_all(priv); usbhs_bus_send_reset(priv); msleep(20); usbhs_bus_send_sof_enable(priv); for (i = 0; i < timeout ; i++) { switch (usbhs_bus_get_speed(priv)) { case USB_SPEED_LOW: speed = USB_PORT_STAT_LOW_SPEED; goto got_usb_bus_speed; case USB_SPEED_HIGH: speed = USB_PORT_STAT_HIGH_SPEED; goto got_usb_bus_speed; case USB_SPEED_FULL: speed = 0; goto got_usb_bus_speed; } msleep(20); } return -EPIPE; got_usb_bus_speed: usbhsh_port_stat_set(hpriv, speed); usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", __func__, speed); /* status change is not needed */ return 0; default: return -EPIPE; } /* set/clear status */ if (enable) usbhsh_port_stat_set(hpriv, (1 << wValue)); else usbhsh_port_stat_clear(hpriv, (1 << wValue)); return 0; }