18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-1.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Renesas USB driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2011 Renesas Solutions Corp. 68c2ecf20Sopenharmony_ci * Copyright (C) 2019 Renesas Electronics Corporation 78c2ecf20Sopenharmony_ci * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 118c2ecf20Sopenharmony_ci#include <linux/io.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h> 158c2ecf20Sopenharmony_ci#include <linux/usb/gadget.h> 168c2ecf20Sopenharmony_ci#include <linux/usb/otg.h> 178c2ecf20Sopenharmony_ci#include "common.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* 208c2ecf20Sopenharmony_ci * struct 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_cistruct usbhsg_request { 238c2ecf20Sopenharmony_ci struct usb_request req; 248c2ecf20Sopenharmony_ci struct usbhs_pkt pkt; 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define EP_NAME_SIZE 8 288c2ecf20Sopenharmony_cistruct usbhsg_gpriv; 298c2ecf20Sopenharmony_cistruct usbhsg_uep { 308c2ecf20Sopenharmony_ci struct usb_ep ep; 318c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe; 328c2ecf20Sopenharmony_ci spinlock_t lock; /* protect the pipe */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci char ep_name[EP_NAME_SIZE]; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv; 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistruct usbhsg_gpriv { 408c2ecf20Sopenharmony_ci struct usb_gadget gadget; 418c2ecf20Sopenharmony_ci struct usbhs_mod mod; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci struct usbhsg_uep *uep; 448c2ecf20Sopenharmony_ci int uep_size; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci struct usb_gadget_driver *driver; 478c2ecf20Sopenharmony_ci struct usb_phy *transceiver; 488c2ecf20Sopenharmony_ci bool vbus_active; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci u32 status; 518c2ecf20Sopenharmony_ci#define USBHSG_STATUS_STARTED (1 << 0) 528c2ecf20Sopenharmony_ci#define USBHSG_STATUS_REGISTERD (1 << 1) 538c2ecf20Sopenharmony_ci#define USBHSG_STATUS_WEDGE (1 << 2) 548c2ecf20Sopenharmony_ci#define USBHSG_STATUS_SELF_POWERED (1 << 3) 558c2ecf20Sopenharmony_ci#define USBHSG_STATUS_SOFT_CONNECT (1 << 4) 568c2ecf20Sopenharmony_ci}; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistruct usbhsg_recip_handle { 598c2ecf20Sopenharmony_ci char *name; 608c2ecf20Sopenharmony_ci int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 618c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl); 628c2ecf20Sopenharmony_ci int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 638c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl); 648c2ecf20Sopenharmony_ci int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 658c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl); 668c2ecf20Sopenharmony_ci}; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/* 698c2ecf20Sopenharmony_ci * macro 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ci#define usbhsg_priv_to_gpriv(priv) \ 728c2ecf20Sopenharmony_ci container_of( \ 738c2ecf20Sopenharmony_ci usbhs_mod_get(priv, USBHS_GADGET), \ 748c2ecf20Sopenharmony_ci struct usbhsg_gpriv, mod) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define __usbhsg_for_each_uep(start, pos, g, i) \ 778c2ecf20Sopenharmony_ci for ((i) = start; \ 788c2ecf20Sopenharmony_ci ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \ 798c2ecf20Sopenharmony_ci (i)++) 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci#define usbhsg_for_each_uep(pos, gpriv, i) \ 828c2ecf20Sopenharmony_ci __usbhsg_for_each_uep(1, pos, gpriv, i) 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \ 858c2ecf20Sopenharmony_ci __usbhsg_for_each_uep(0, pos, gpriv, i) 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci#define usbhsg_gadget_to_gpriv(g)\ 888c2ecf20Sopenharmony_ci container_of(g, struct usbhsg_gpriv, gadget) 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define usbhsg_req_to_ureq(r)\ 918c2ecf20Sopenharmony_ci container_of(r, struct usbhsg_request, req) 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep) 948c2ecf20Sopenharmony_ci#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv) 958c2ecf20Sopenharmony_ci#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv) 968c2ecf20Sopenharmony_ci#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep) 978c2ecf20Sopenharmony_ci#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i) 988c2ecf20Sopenharmony_ci#define usbhsg_uep_to_gpriv(u) ((u)->gpriv) 998c2ecf20Sopenharmony_ci#define usbhsg_uep_to_pipe(u) ((u)->pipe) 1008c2ecf20Sopenharmony_ci#define usbhsg_pipe_to_uep(p) ((p)->mod_private) 1018c2ecf20Sopenharmony_ci#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv)) 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#define usbhsg_ureq_to_pkt(u) (&(u)->pkt) 1048c2ecf20Sopenharmony_ci#define usbhsg_pkt_to_ureq(i) \ 1058c2ecf20Sopenharmony_ci container_of(i, struct usbhsg_request, pkt) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN) 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* status */ 1108c2ecf20Sopenharmony_ci#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0) 1118c2ecf20Sopenharmony_ci#define usbhsg_status_set(gp, b) (gp->status |= b) 1128c2ecf20Sopenharmony_ci#define usbhsg_status_clr(gp, b) (gp->status &= ~b) 1138c2ecf20Sopenharmony_ci#define usbhsg_status_has(gp, b) (gp->status & b) 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci/* 1168c2ecf20Sopenharmony_ci * queue push/pop 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_cistatic void __usbhsg_queue_pop(struct usbhsg_uep *uep, 1198c2ecf20Sopenharmony_ci struct usbhsg_request *ureq, 1208c2ecf20Sopenharmony_ci int status) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 1238c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 1248c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 1258c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (pipe) 1288c2ecf20Sopenharmony_ci dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe)); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci ureq->req.status = status; 1318c2ecf20Sopenharmony_ci spin_unlock(usbhs_priv_to_lock(priv)); 1328c2ecf20Sopenharmony_ci usb_gadget_giveback_request(&uep->ep, &ureq->req); 1338c2ecf20Sopenharmony_ci spin_lock(usbhs_priv_to_lock(priv)); 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic void usbhsg_queue_pop(struct usbhsg_uep *uep, 1378c2ecf20Sopenharmony_ci struct usbhsg_request *ureq, 1388c2ecf20Sopenharmony_ci int status) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 1418c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 1428c2ecf20Sopenharmony_ci unsigned long flags; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 1458c2ecf20Sopenharmony_ci __usbhsg_queue_pop(uep, ureq, status); 1468c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = pkt->pipe; 1528c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); 1538c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); 1548c2ecf20Sopenharmony_ci unsigned long flags; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci ureq->req.actual = pkt->actual; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 1598c2ecf20Sopenharmony_ci if (uep) 1608c2ecf20Sopenharmony_ci __usbhsg_queue_pop(uep, ureq, 0); 1618c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic void usbhsg_queue_push(struct usbhsg_uep *uep, 1658c2ecf20Sopenharmony_ci struct usbhsg_request *ureq) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 1688c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 1698c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 1708c2ecf20Sopenharmony_ci struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq); 1718c2ecf20Sopenharmony_ci struct usb_request *req = &ureq->req; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci req->actual = 0; 1748c2ecf20Sopenharmony_ci req->status = -EINPROGRESS; 1758c2ecf20Sopenharmony_ci usbhs_pkt_push(pipe, pkt, usbhsg_queue_done, 1768c2ecf20Sopenharmony_ci req->buf, req->length, req->zero, -1); 1778c2ecf20Sopenharmony_ci usbhs_pkt_start(pipe); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci dev_dbg(dev, "pipe %d : queue push (%d)\n", 1808c2ecf20Sopenharmony_ci usbhs_pipe_number(pipe), 1818c2ecf20Sopenharmony_ci req->length); 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci/* 1858c2ecf20Sopenharmony_ci * dma map/unmap 1868c2ecf20Sopenharmony_ci */ 1878c2ecf20Sopenharmony_cistatic int usbhsg_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt, 1888c2ecf20Sopenharmony_ci int map) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); 1918c2ecf20Sopenharmony_ci struct usb_request *req = &ureq->req; 1928c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = pkt->pipe; 1938c2ecf20Sopenharmony_ci enum dma_data_direction dir; 1948c2ecf20Sopenharmony_ci int ret = 0; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci dir = usbhs_pipe_is_dir_host(pipe); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if (map) { 1998c2ecf20Sopenharmony_ci /* it can not use scatter/gather */ 2008c2ecf20Sopenharmony_ci WARN_ON(req->num_sgs); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci ret = usb_gadget_map_request_by_dev(dma_dev, req, dir); 2038c2ecf20Sopenharmony_ci if (ret < 0) 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci pkt->dma = req->dma; 2078c2ecf20Sopenharmony_ci } else { 2088c2ecf20Sopenharmony_ci usb_gadget_unmap_request_by_dev(dma_dev, req, dir); 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci return ret; 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci/* 2158c2ecf20Sopenharmony_ci * USB_TYPE_STANDARD / clear feature functions 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, 2188c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 2198c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 2228c2ecf20Sopenharmony_ci struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 2238c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci usbhs_dcp_control_transfer_done(pipe); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return 0; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, 2318c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 2328c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 2358c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { 2388c2ecf20Sopenharmony_ci usbhs_pipe_disable(pipe); 2398c2ecf20Sopenharmony_ci usbhs_pipe_sequence_data0(pipe); 2408c2ecf20Sopenharmony_ci usbhs_pipe_enable(pipe); 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci usbhs_pkt_start(pipe); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci return 0; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic struct usbhsg_recip_handle req_clear_feature = { 2518c2ecf20Sopenharmony_ci .name = "clear feature", 2528c2ecf20Sopenharmony_ci .device = usbhsg_recip_handler_std_control_done, 2538c2ecf20Sopenharmony_ci .interface = usbhsg_recip_handler_std_control_done, 2548c2ecf20Sopenharmony_ci .endpoint = usbhsg_recip_handler_std_clear_endpoint, 2558c2ecf20Sopenharmony_ci}; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci/* 2588c2ecf20Sopenharmony_ci * USB_TYPE_STANDARD / set feature functions 2598c2ecf20Sopenharmony_ci */ 2608c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv, 2618c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 2628c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci switch (le16_to_cpu(ctrl->wValue)) { 2658c2ecf20Sopenharmony_ci case USB_DEVICE_TEST_MODE: 2668c2ecf20Sopenharmony_ci usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 2678c2ecf20Sopenharmony_ci udelay(100); 2688c2ecf20Sopenharmony_ci usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex) >> 8); 2698c2ecf20Sopenharmony_ci break; 2708c2ecf20Sopenharmony_ci default: 2718c2ecf20Sopenharmony_ci usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 2728c2ecf20Sopenharmony_ci break; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci return 0; 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv, 2798c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 2808c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci usbhs_pipe_stall(pipe); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci return 0; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic struct usbhsg_recip_handle req_set_feature = { 2928c2ecf20Sopenharmony_ci .name = "set feature", 2938c2ecf20Sopenharmony_ci .device = usbhsg_recip_handler_std_set_device, 2948c2ecf20Sopenharmony_ci .interface = usbhsg_recip_handler_std_control_done, 2958c2ecf20Sopenharmony_ci .endpoint = usbhsg_recip_handler_std_set_endpoint, 2968c2ecf20Sopenharmony_ci}; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci/* 2998c2ecf20Sopenharmony_ci * USB_TYPE_STANDARD / get status functions 3008c2ecf20Sopenharmony_ci */ 3018c2ecf20Sopenharmony_cistatic void __usbhsg_recip_send_complete(struct usb_ep *ep, 3028c2ecf20Sopenharmony_ci struct usb_request *req) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci /* free allocated recip-buffer/usb_request */ 3078c2ecf20Sopenharmony_ci kfree(ureq->pkt.buf); 3088c2ecf20Sopenharmony_ci usb_ep_free_request(ep, req); 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv, 3128c2ecf20Sopenharmony_ci unsigned short status) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 3158c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 3168c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 3178c2ecf20Sopenharmony_ci struct usb_request *req; 3188c2ecf20Sopenharmony_ci __le16 *buf; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* alloc new usb_request for recip */ 3218c2ecf20Sopenharmony_ci req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC); 3228c2ecf20Sopenharmony_ci if (!req) { 3238c2ecf20Sopenharmony_ci dev_err(dev, "recip request allocation fail\n"); 3248c2ecf20Sopenharmony_ci return; 3258c2ecf20Sopenharmony_ci } 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci /* alloc recip data buffer */ 3288c2ecf20Sopenharmony_ci buf = kmalloc(sizeof(*buf), GFP_ATOMIC); 3298c2ecf20Sopenharmony_ci if (!buf) { 3308c2ecf20Sopenharmony_ci usb_ep_free_request(&dcp->ep, req); 3318c2ecf20Sopenharmony_ci return; 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci /* recip data is status */ 3358c2ecf20Sopenharmony_ci *buf = cpu_to_le16(status); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci /* allocated usb_request/buffer will be freed */ 3388c2ecf20Sopenharmony_ci req->complete = __usbhsg_recip_send_complete; 3398c2ecf20Sopenharmony_ci req->buf = buf; 3408c2ecf20Sopenharmony_ci req->length = sizeof(*buf); 3418c2ecf20Sopenharmony_ci req->zero = 0; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci /* push packet */ 3448c2ecf20Sopenharmony_ci pipe->handler = &usbhs_fifo_pio_push_handler; 3458c2ecf20Sopenharmony_ci usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req)); 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv, 3498c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 3508c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 3518c2ecf20Sopenharmony_ci{ 3528c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 3538c2ecf20Sopenharmony_ci unsigned short status = 0; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED)) 3568c2ecf20Sopenharmony_ci status = 1 << USB_DEVICE_SELF_POWERED; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci __usbhsg_recip_send_status(gpriv, status); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci return 0; 3618c2ecf20Sopenharmony_ci} 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv, 3648c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 3658c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 3688c2ecf20Sopenharmony_ci unsigned short status = 0; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci __usbhsg_recip_send_status(gpriv, status); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci return 0; 3738c2ecf20Sopenharmony_ci} 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_cistatic int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv, 3768c2ecf20Sopenharmony_ci struct usbhsg_uep *uep, 3778c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 3788c2ecf20Sopenharmony_ci{ 3798c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 3808c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 3818c2ecf20Sopenharmony_ci unsigned short status = 0; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci if (usbhs_pipe_is_stall(pipe)) 3848c2ecf20Sopenharmony_ci status = 1 << USB_ENDPOINT_HALT; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci __usbhsg_recip_send_status(gpriv, status); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci return 0; 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic struct usbhsg_recip_handle req_get_status = { 3928c2ecf20Sopenharmony_ci .name = "get status", 3938c2ecf20Sopenharmony_ci .device = usbhsg_recip_handler_std_get_device, 3948c2ecf20Sopenharmony_ci .interface = usbhsg_recip_handler_std_get_interface, 3958c2ecf20Sopenharmony_ci .endpoint = usbhsg_recip_handler_std_get_endpoint, 3968c2ecf20Sopenharmony_ci}; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci/* 3998c2ecf20Sopenharmony_ci * USB_TYPE handler 4008c2ecf20Sopenharmony_ci */ 4018c2ecf20Sopenharmony_cistatic int usbhsg_recip_run_handle(struct usbhs_priv *priv, 4028c2ecf20Sopenharmony_ci struct usbhsg_recip_handle *handler, 4038c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 4068c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 4078c2ecf20Sopenharmony_ci struct usbhsg_uep *uep; 4088c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe; 4098c2ecf20Sopenharmony_ci int recip = ctrl->bRequestType & USB_RECIP_MASK; 4108c2ecf20Sopenharmony_ci int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; 4118c2ecf20Sopenharmony_ci int ret = 0; 4128c2ecf20Sopenharmony_ci int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 4138c2ecf20Sopenharmony_ci struct usb_ctrlrequest *ctrl); 4148c2ecf20Sopenharmony_ci char *msg; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); 4178c2ecf20Sopenharmony_ci pipe = usbhsg_uep_to_pipe(uep); 4188c2ecf20Sopenharmony_ci if (!pipe) { 4198c2ecf20Sopenharmony_ci dev_err(dev, "wrong recip request\n"); 4208c2ecf20Sopenharmony_ci return -EINVAL; 4218c2ecf20Sopenharmony_ci } 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci switch (recip) { 4248c2ecf20Sopenharmony_ci case USB_RECIP_DEVICE: 4258c2ecf20Sopenharmony_ci msg = "DEVICE"; 4268c2ecf20Sopenharmony_ci func = handler->device; 4278c2ecf20Sopenharmony_ci break; 4288c2ecf20Sopenharmony_ci case USB_RECIP_INTERFACE: 4298c2ecf20Sopenharmony_ci msg = "INTERFACE"; 4308c2ecf20Sopenharmony_ci func = handler->interface; 4318c2ecf20Sopenharmony_ci break; 4328c2ecf20Sopenharmony_ci case USB_RECIP_ENDPOINT: 4338c2ecf20Sopenharmony_ci msg = "ENDPOINT"; 4348c2ecf20Sopenharmony_ci func = handler->endpoint; 4358c2ecf20Sopenharmony_ci break; 4368c2ecf20Sopenharmony_ci default: 4378c2ecf20Sopenharmony_ci dev_warn(dev, "unsupported RECIP(%d)\n", recip); 4388c2ecf20Sopenharmony_ci func = NULL; 4398c2ecf20Sopenharmony_ci ret = -EINVAL; 4408c2ecf20Sopenharmony_ci } 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci if (func) { 4438c2ecf20Sopenharmony_ci dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); 4448c2ecf20Sopenharmony_ci ret = func(priv, uep, ctrl); 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci return ret; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci/* 4518c2ecf20Sopenharmony_ci * irq functions 4528c2ecf20Sopenharmony_ci * 4538c2ecf20Sopenharmony_ci * it will be called from usbhs_interrupt 4548c2ecf20Sopenharmony_ci */ 4558c2ecf20Sopenharmony_cistatic int usbhsg_irq_dev_state(struct usbhs_priv *priv, 4568c2ecf20Sopenharmony_ci struct usbhs_irq_state *irq_state) 4578c2ecf20Sopenharmony_ci{ 4588c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 4598c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 4608c2ecf20Sopenharmony_ci int state = usbhs_status_get_device_state(irq_state); 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci gpriv->gadget.speed = usbhs_bus_get_speed(priv); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci dev_dbg(dev, "state = %x : speed : %d\n", state, gpriv->gadget.speed); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (gpriv->gadget.speed != USB_SPEED_UNKNOWN && 4678c2ecf20Sopenharmony_ci (state & SUSPENDED_STATE)) { 4688c2ecf20Sopenharmony_ci if (gpriv->driver && gpriv->driver->suspend) 4698c2ecf20Sopenharmony_ci gpriv->driver->suspend(&gpriv->gadget); 4708c2ecf20Sopenharmony_ci usb_gadget_set_state(&gpriv->gadget, USB_STATE_SUSPENDED); 4718c2ecf20Sopenharmony_ci } 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci return 0; 4748c2ecf20Sopenharmony_ci} 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_cistatic int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, 4778c2ecf20Sopenharmony_ci struct usbhs_irq_state *irq_state) 4788c2ecf20Sopenharmony_ci{ 4798c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 4808c2ecf20Sopenharmony_ci struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 4818c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 4828c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 4838c2ecf20Sopenharmony_ci struct usb_ctrlrequest ctrl; 4848c2ecf20Sopenharmony_ci struct usbhsg_recip_handle *recip_handler = NULL; 4858c2ecf20Sopenharmony_ci int stage = usbhs_status_get_ctrl_stage(irq_state); 4868c2ecf20Sopenharmony_ci int ret = 0; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci dev_dbg(dev, "stage = %d\n", stage); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci /* 4918c2ecf20Sopenharmony_ci * see Manual 4928c2ecf20Sopenharmony_ci * 4938c2ecf20Sopenharmony_ci * "Operation" 4948c2ecf20Sopenharmony_ci * - "Interrupt Function" 4958c2ecf20Sopenharmony_ci * - "Control Transfer Stage Transition Interrupt" 4968c2ecf20Sopenharmony_ci * - Fig. "Control Transfer Stage Transitions" 4978c2ecf20Sopenharmony_ci */ 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci switch (stage) { 5008c2ecf20Sopenharmony_ci case READ_DATA_STAGE: 5018c2ecf20Sopenharmony_ci pipe->handler = &usbhs_fifo_pio_push_handler; 5028c2ecf20Sopenharmony_ci break; 5038c2ecf20Sopenharmony_ci case WRITE_DATA_STAGE: 5048c2ecf20Sopenharmony_ci pipe->handler = &usbhs_fifo_pio_pop_handler; 5058c2ecf20Sopenharmony_ci break; 5068c2ecf20Sopenharmony_ci case NODATA_STATUS_STAGE: 5078c2ecf20Sopenharmony_ci pipe->handler = &usbhs_ctrl_stage_end_handler; 5088c2ecf20Sopenharmony_ci break; 5098c2ecf20Sopenharmony_ci case READ_STATUS_STAGE: 5108c2ecf20Sopenharmony_ci case WRITE_STATUS_STAGE: 5118c2ecf20Sopenharmony_ci usbhs_dcp_control_transfer_done(pipe); 5128c2ecf20Sopenharmony_ci fallthrough; 5138c2ecf20Sopenharmony_ci default: 5148c2ecf20Sopenharmony_ci return ret; 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci /* 5188c2ecf20Sopenharmony_ci * get usb request 5198c2ecf20Sopenharmony_ci */ 5208c2ecf20Sopenharmony_ci usbhs_usbreq_get_val(priv, &ctrl); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci switch (ctrl.bRequestType & USB_TYPE_MASK) { 5238c2ecf20Sopenharmony_ci case USB_TYPE_STANDARD: 5248c2ecf20Sopenharmony_ci switch (ctrl.bRequest) { 5258c2ecf20Sopenharmony_ci case USB_REQ_CLEAR_FEATURE: 5268c2ecf20Sopenharmony_ci recip_handler = &req_clear_feature; 5278c2ecf20Sopenharmony_ci break; 5288c2ecf20Sopenharmony_ci case USB_REQ_SET_FEATURE: 5298c2ecf20Sopenharmony_ci recip_handler = &req_set_feature; 5308c2ecf20Sopenharmony_ci break; 5318c2ecf20Sopenharmony_ci case USB_REQ_GET_STATUS: 5328c2ecf20Sopenharmony_ci recip_handler = &req_get_status; 5338c2ecf20Sopenharmony_ci break; 5348c2ecf20Sopenharmony_ci } 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci /* 5388c2ecf20Sopenharmony_ci * setup stage / run recip 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ci if (recip_handler) 5418c2ecf20Sopenharmony_ci ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); 5428c2ecf20Sopenharmony_ci else 5438c2ecf20Sopenharmony_ci ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci if (ret < 0) 5468c2ecf20Sopenharmony_ci usbhs_pipe_stall(pipe); 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci return ret; 5498c2ecf20Sopenharmony_ci} 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci/* 5528c2ecf20Sopenharmony_ci * 5538c2ecf20Sopenharmony_ci * usb_dcp_ops 5548c2ecf20Sopenharmony_ci * 5558c2ecf20Sopenharmony_ci */ 5568c2ecf20Sopenharmony_cistatic int usbhsg_pipe_disable(struct usbhsg_uep *uep) 5578c2ecf20Sopenharmony_ci{ 5588c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 5598c2ecf20Sopenharmony_ci struct usbhs_pkt *pkt; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci while (1) { 5628c2ecf20Sopenharmony_ci pkt = usbhs_pkt_pop(pipe, NULL); 5638c2ecf20Sopenharmony_ci if (!pkt) 5648c2ecf20Sopenharmony_ci break; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ESHUTDOWN); 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci usbhs_pipe_disable(pipe); 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci return 0; 5728c2ecf20Sopenharmony_ci} 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci/* 5758c2ecf20Sopenharmony_ci * 5768c2ecf20Sopenharmony_ci * usb_ep_ops 5778c2ecf20Sopenharmony_ci * 5788c2ecf20Sopenharmony_ci */ 5798c2ecf20Sopenharmony_cistatic int usbhsg_ep_enable(struct usb_ep *ep, 5808c2ecf20Sopenharmony_ci const struct usb_endpoint_descriptor *desc) 5818c2ecf20Sopenharmony_ci{ 5828c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 5838c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 5848c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 5858c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe; 5868c2ecf20Sopenharmony_ci int ret = -EIO; 5878c2ecf20Sopenharmony_ci unsigned long flags; 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci /* 5928c2ecf20Sopenharmony_ci * if it already have pipe, 5938c2ecf20Sopenharmony_ci * nothing to do 5948c2ecf20Sopenharmony_ci */ 5958c2ecf20Sopenharmony_ci if (uep->pipe) { 5968c2ecf20Sopenharmony_ci usbhs_pipe_clear(uep->pipe); 5978c2ecf20Sopenharmony_ci usbhs_pipe_sequence_data0(uep->pipe); 5988c2ecf20Sopenharmony_ci ret = 0; 5998c2ecf20Sopenharmony_ci goto usbhsg_ep_enable_end; 6008c2ecf20Sopenharmony_ci } 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci pipe = usbhs_pipe_malloc(priv, 6038c2ecf20Sopenharmony_ci usb_endpoint_type(desc), 6048c2ecf20Sopenharmony_ci usb_endpoint_dir_in(desc)); 6058c2ecf20Sopenharmony_ci if (pipe) { 6068c2ecf20Sopenharmony_ci uep->pipe = pipe; 6078c2ecf20Sopenharmony_ci pipe->mod_private = uep; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* set epnum / maxp */ 6108c2ecf20Sopenharmony_ci usbhs_pipe_config_update(pipe, 0, 6118c2ecf20Sopenharmony_ci usb_endpoint_num(desc), 6128c2ecf20Sopenharmony_ci usb_endpoint_maxp(desc)); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci /* 6158c2ecf20Sopenharmony_ci * usbhs_fifo_dma_push/pop_handler try to 6168c2ecf20Sopenharmony_ci * use dmaengine if possible. 6178c2ecf20Sopenharmony_ci * It will use pio handler if impossible. 6188c2ecf20Sopenharmony_ci */ 6198c2ecf20Sopenharmony_ci if (usb_endpoint_dir_in(desc)) { 6208c2ecf20Sopenharmony_ci pipe->handler = &usbhs_fifo_dma_push_handler; 6218c2ecf20Sopenharmony_ci } else { 6228c2ecf20Sopenharmony_ci pipe->handler = &usbhs_fifo_dma_pop_handler; 6238c2ecf20Sopenharmony_ci usbhs_xxxsts_clear(priv, BRDYSTS, 6248c2ecf20Sopenharmony_ci usbhs_pipe_number(pipe)); 6258c2ecf20Sopenharmony_ci } 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci ret = 0; 6288c2ecf20Sopenharmony_ci } 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ciusbhsg_ep_enable_end: 6318c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci return ret; 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cistatic int usbhsg_ep_disable(struct usb_ep *ep) 6378c2ecf20Sopenharmony_ci{ 6388c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 6398c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe; 6408c2ecf20Sopenharmony_ci unsigned long flags; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci spin_lock_irqsave(&uep->lock, flags); 6438c2ecf20Sopenharmony_ci pipe = usbhsg_uep_to_pipe(uep); 6448c2ecf20Sopenharmony_ci if (!pipe) 6458c2ecf20Sopenharmony_ci goto out; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci usbhsg_pipe_disable(uep); 6488c2ecf20Sopenharmony_ci usbhs_pipe_free(pipe); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci uep->pipe->mod_private = NULL; 6518c2ecf20Sopenharmony_ci uep->pipe = NULL; 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ciout: 6548c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uep->lock, flags); 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci return 0; 6578c2ecf20Sopenharmony_ci} 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_cistatic struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, 6608c2ecf20Sopenharmony_ci gfp_t gfp_flags) 6618c2ecf20Sopenharmony_ci{ 6628c2ecf20Sopenharmony_ci struct usbhsg_request *ureq; 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci ureq = kzalloc(sizeof *ureq, gfp_flags); 6658c2ecf20Sopenharmony_ci if (!ureq) 6668c2ecf20Sopenharmony_ci return NULL; 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci return &ureq->req; 6718c2ecf20Sopenharmony_ci} 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_cistatic void usbhsg_ep_free_request(struct usb_ep *ep, 6748c2ecf20Sopenharmony_ci struct usb_request *req) 6758c2ecf20Sopenharmony_ci{ 6768c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci WARN_ON(!list_empty(&ureq->pkt.node)); 6798c2ecf20Sopenharmony_ci kfree(ureq); 6808c2ecf20Sopenharmony_ci} 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_cistatic int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, 6838c2ecf20Sopenharmony_ci gfp_t gfp_flags) 6848c2ecf20Sopenharmony_ci{ 6858c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 6868c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 6878c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 6888c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci /* param check */ 6918c2ecf20Sopenharmony_ci if (usbhsg_is_not_connected(gpriv) || 6928c2ecf20Sopenharmony_ci unlikely(!gpriv->driver) || 6938c2ecf20Sopenharmony_ci unlikely(!pipe)) 6948c2ecf20Sopenharmony_ci return -ESHUTDOWN; 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci usbhsg_queue_push(uep, ureq); 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci return 0; 6998c2ecf20Sopenharmony_ci} 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_cistatic int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 7028c2ecf20Sopenharmony_ci{ 7038c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 7048c2ecf20Sopenharmony_ci struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 7058c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe; 7068c2ecf20Sopenharmony_ci unsigned long flags; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci spin_lock_irqsave(&uep->lock, flags); 7098c2ecf20Sopenharmony_ci pipe = usbhsg_uep_to_pipe(uep); 7108c2ecf20Sopenharmony_ci if (pipe) 7118c2ecf20Sopenharmony_ci usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci /* 7148c2ecf20Sopenharmony_ci * To dequeue a request, this driver should call the usbhsg_queue_pop() 7158c2ecf20Sopenharmony_ci * even if the pipe is NULL. 7168c2ecf20Sopenharmony_ci */ 7178c2ecf20Sopenharmony_ci usbhsg_queue_pop(uep, ureq, -ECONNRESET); 7188c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uep->lock, flags); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci return 0; 7218c2ecf20Sopenharmony_ci} 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_cistatic int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) 7248c2ecf20Sopenharmony_ci{ 7258c2ecf20Sopenharmony_ci struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 7268c2ecf20Sopenharmony_ci struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 7278c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 7288c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 7298c2ecf20Sopenharmony_ci struct device *dev = usbhsg_gpriv_to_dev(gpriv); 7308c2ecf20Sopenharmony_ci unsigned long flags; 7318c2ecf20Sopenharmony_ci int ret = 0; 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci dev_dbg(dev, "set halt %d (pipe %d)\n", 7348c2ecf20Sopenharmony_ci halt, usbhs_pipe_number(pipe)); 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci /******************** spin lock ********************/ 7378c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci /* 7408c2ecf20Sopenharmony_ci * According to usb_ep_set_halt()'s description, this function should 7418c2ecf20Sopenharmony_ci * return -EAGAIN if the IN endpoint has any queue or data. Note 7428c2ecf20Sopenharmony_ci * that the usbhs_pipe_is_dir_in() returns false if the pipe is an 7438c2ecf20Sopenharmony_ci * IN endpoint in the gadget mode. 7448c2ecf20Sopenharmony_ci */ 7458c2ecf20Sopenharmony_ci if (!usbhs_pipe_is_dir_in(pipe) && (__usbhsf_pkt_get(pipe) || 7468c2ecf20Sopenharmony_ci usbhs_pipe_contains_transmittable_data(pipe))) { 7478c2ecf20Sopenharmony_ci ret = -EAGAIN; 7488c2ecf20Sopenharmony_ci goto out; 7498c2ecf20Sopenharmony_ci } 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci if (halt) 7528c2ecf20Sopenharmony_ci usbhs_pipe_stall(pipe); 7538c2ecf20Sopenharmony_ci else 7548c2ecf20Sopenharmony_ci usbhs_pipe_disable(pipe); 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci if (halt && wedge) 7578c2ecf20Sopenharmony_ci usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); 7588c2ecf20Sopenharmony_ci else 7598c2ecf20Sopenharmony_ci usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ciout: 7628c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 7638c2ecf20Sopenharmony_ci /******************** spin unlock ******************/ 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci return ret; 7668c2ecf20Sopenharmony_ci} 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_cistatic int usbhsg_ep_set_halt(struct usb_ep *ep, int value) 7698c2ecf20Sopenharmony_ci{ 7708c2ecf20Sopenharmony_ci return __usbhsg_ep_set_halt_wedge(ep, value, 0); 7718c2ecf20Sopenharmony_ci} 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_cistatic int usbhsg_ep_set_wedge(struct usb_ep *ep) 7748c2ecf20Sopenharmony_ci{ 7758c2ecf20Sopenharmony_ci return __usbhsg_ep_set_halt_wedge(ep, 1, 1); 7768c2ecf20Sopenharmony_ci} 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_cistatic const struct usb_ep_ops usbhsg_ep_ops = { 7798c2ecf20Sopenharmony_ci .enable = usbhsg_ep_enable, 7808c2ecf20Sopenharmony_ci .disable = usbhsg_ep_disable, 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci .alloc_request = usbhsg_ep_alloc_request, 7838c2ecf20Sopenharmony_ci .free_request = usbhsg_ep_free_request, 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci .queue = usbhsg_ep_queue, 7868c2ecf20Sopenharmony_ci .dequeue = usbhsg_ep_dequeue, 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci .set_halt = usbhsg_ep_set_halt, 7898c2ecf20Sopenharmony_ci .set_wedge = usbhsg_ep_set_wedge, 7908c2ecf20Sopenharmony_ci}; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci/* 7938c2ecf20Sopenharmony_ci * pullup control 7948c2ecf20Sopenharmony_ci */ 7958c2ecf20Sopenharmony_cistatic int usbhsg_can_pullup(struct usbhs_priv *priv) 7968c2ecf20Sopenharmony_ci{ 7978c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci return gpriv->driver && 8008c2ecf20Sopenharmony_ci usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT); 8018c2ecf20Sopenharmony_ci} 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_cistatic void usbhsg_update_pullup(struct usbhs_priv *priv) 8048c2ecf20Sopenharmony_ci{ 8058c2ecf20Sopenharmony_ci if (usbhsg_can_pullup(priv)) 8068c2ecf20Sopenharmony_ci usbhs_sys_function_pullup(priv, 1); 8078c2ecf20Sopenharmony_ci else 8088c2ecf20Sopenharmony_ci usbhs_sys_function_pullup(priv, 0); 8098c2ecf20Sopenharmony_ci} 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci/* 8128c2ecf20Sopenharmony_ci * usb module start/end 8138c2ecf20Sopenharmony_ci */ 8148c2ecf20Sopenharmony_cistatic int usbhsg_try_start(struct usbhs_priv *priv, u32 status) 8158c2ecf20Sopenharmony_ci{ 8168c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 8178c2ecf20Sopenharmony_ci struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 8188c2ecf20Sopenharmony_ci struct usbhs_mod *mod = usbhs_mod_get_current(priv); 8198c2ecf20Sopenharmony_ci struct device *dev = usbhs_priv_to_dev(priv); 8208c2ecf20Sopenharmony_ci unsigned long flags; 8218c2ecf20Sopenharmony_ci int ret = 0; 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci /******************** spin lock ********************/ 8248c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_ci usbhsg_status_set(gpriv, status); 8278c2ecf20Sopenharmony_ci if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && 8288c2ecf20Sopenharmony_ci usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) 8298c2ecf20Sopenharmony_ci ret = -1; /* not ready */ 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 8328c2ecf20Sopenharmony_ci /******************** spin unlock ********************/ 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci if (ret < 0) 8358c2ecf20Sopenharmony_ci return 0; /* not ready is not error */ 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci /* 8388c2ecf20Sopenharmony_ci * enable interrupt and systems if ready 8398c2ecf20Sopenharmony_ci */ 8408c2ecf20Sopenharmony_ci dev_dbg(dev, "start gadget\n"); 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci /* 8438c2ecf20Sopenharmony_ci * pipe initialize and enable DCP 8448c2ecf20Sopenharmony_ci */ 8458c2ecf20Sopenharmony_ci usbhs_fifo_init(priv); 8468c2ecf20Sopenharmony_ci usbhs_pipe_init(priv, 8478c2ecf20Sopenharmony_ci usbhsg_dma_map_ctrl); 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci /* dcp init instead of usbhsg_ep_enable() */ 8508c2ecf20Sopenharmony_ci dcp->pipe = usbhs_dcp_malloc(priv); 8518c2ecf20Sopenharmony_ci dcp->pipe->mod_private = dcp; 8528c2ecf20Sopenharmony_ci usbhs_pipe_config_update(dcp->pipe, 0, 0, 64); 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci /* 8558c2ecf20Sopenharmony_ci * system config enble 8568c2ecf20Sopenharmony_ci * - HI speed 8578c2ecf20Sopenharmony_ci * - function 8588c2ecf20Sopenharmony_ci * - usb module 8598c2ecf20Sopenharmony_ci */ 8608c2ecf20Sopenharmony_ci usbhs_sys_function_ctrl(priv, 1); 8618c2ecf20Sopenharmony_ci usbhsg_update_pullup(priv); 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci /* 8648c2ecf20Sopenharmony_ci * enable irq callback 8658c2ecf20Sopenharmony_ci */ 8668c2ecf20Sopenharmony_ci mod->irq_dev_state = usbhsg_irq_dev_state; 8678c2ecf20Sopenharmony_ci mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; 8688c2ecf20Sopenharmony_ci usbhs_irq_callback_update(priv, mod); 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci return 0; 8718c2ecf20Sopenharmony_ci} 8728c2ecf20Sopenharmony_ci 8738c2ecf20Sopenharmony_cistatic int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) 8748c2ecf20Sopenharmony_ci{ 8758c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 8768c2ecf20Sopenharmony_ci struct usbhs_mod *mod = usbhs_mod_get_current(priv); 8778c2ecf20Sopenharmony_ci struct usbhsg_uep *uep; 8788c2ecf20Sopenharmony_ci struct device *dev = usbhs_priv_to_dev(priv); 8798c2ecf20Sopenharmony_ci unsigned long flags; 8808c2ecf20Sopenharmony_ci int ret = 0, i; 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci /******************** spin lock ********************/ 8838c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci usbhsg_status_clr(gpriv, status); 8868c2ecf20Sopenharmony_ci if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && 8878c2ecf20Sopenharmony_ci !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) 8888c2ecf20Sopenharmony_ci ret = -1; /* already done */ 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 8918c2ecf20Sopenharmony_ci /******************** spin unlock ********************/ 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci if (ret < 0) 8948c2ecf20Sopenharmony_ci return 0; /* already done is not error */ 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci /* 8978c2ecf20Sopenharmony_ci * disable interrupt and systems if 1st try 8988c2ecf20Sopenharmony_ci */ 8998c2ecf20Sopenharmony_ci usbhs_fifo_quit(priv); 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci /* disable all irq */ 9028c2ecf20Sopenharmony_ci mod->irq_dev_state = NULL; 9038c2ecf20Sopenharmony_ci mod->irq_ctrl_stage = NULL; 9048c2ecf20Sopenharmony_ci usbhs_irq_callback_update(priv, mod); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci gpriv->gadget.speed = USB_SPEED_UNKNOWN; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci /* disable sys */ 9098c2ecf20Sopenharmony_ci usbhs_sys_set_test_mode(priv, 0); 9108c2ecf20Sopenharmony_ci usbhs_sys_function_ctrl(priv, 0); 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci /* disable all eps */ 9138c2ecf20Sopenharmony_ci usbhsg_for_each_uep_with_dcp(uep, gpriv, i) 9148c2ecf20Sopenharmony_ci usbhsg_ep_disable(&uep->ep); 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ci dev_dbg(dev, "stop gadget\n"); 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci return 0; 9198c2ecf20Sopenharmony_ci} 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci/* 9228c2ecf20Sopenharmony_ci * VBUS provided by the PHY 9238c2ecf20Sopenharmony_ci */ 9248c2ecf20Sopenharmony_cistatic int usbhsm_phy_get_vbus(struct platform_device *pdev) 9258c2ecf20Sopenharmony_ci{ 9268c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 9278c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci return gpriv->vbus_active; 9308c2ecf20Sopenharmony_ci} 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_cistatic void usbhs_mod_phy_mode(struct usbhs_priv *priv) 9338c2ecf20Sopenharmony_ci{ 9348c2ecf20Sopenharmony_ci struct usbhs_mod_info *info = &priv->mod_info; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci info->irq_vbus = NULL; 9378c2ecf20Sopenharmony_ci info->get_vbus = usbhsm_phy_get_vbus; 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci usbhs_irq_callback_update(priv, NULL); 9408c2ecf20Sopenharmony_ci} 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci/* 9438c2ecf20Sopenharmony_ci * 9448c2ecf20Sopenharmony_ci * linux usb function 9458c2ecf20Sopenharmony_ci * 9468c2ecf20Sopenharmony_ci */ 9478c2ecf20Sopenharmony_cistatic int usbhsg_gadget_start(struct usb_gadget *gadget, 9488c2ecf20Sopenharmony_ci struct usb_gadget_driver *driver) 9498c2ecf20Sopenharmony_ci{ 9508c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 9518c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 9528c2ecf20Sopenharmony_ci struct device *dev = usbhs_priv_to_dev(priv); 9538c2ecf20Sopenharmony_ci int ret; 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci if (!driver || 9568c2ecf20Sopenharmony_ci !driver->setup || 9578c2ecf20Sopenharmony_ci driver->max_speed < USB_SPEED_FULL) 9588c2ecf20Sopenharmony_ci return -EINVAL; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci /* connect to bus through transceiver */ 9618c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(gpriv->transceiver)) { 9628c2ecf20Sopenharmony_ci ret = otg_set_peripheral(gpriv->transceiver->otg, 9638c2ecf20Sopenharmony_ci &gpriv->gadget); 9648c2ecf20Sopenharmony_ci if (ret) { 9658c2ecf20Sopenharmony_ci dev_err(dev, "%s: can't bind to transceiver\n", 9668c2ecf20Sopenharmony_ci gpriv->gadget.name); 9678c2ecf20Sopenharmony_ci return ret; 9688c2ecf20Sopenharmony_ci } 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci /* get vbus using phy versions */ 9718c2ecf20Sopenharmony_ci usbhs_mod_phy_mode(priv); 9728c2ecf20Sopenharmony_ci } 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci /* first hook up the driver ... */ 9758c2ecf20Sopenharmony_ci gpriv->driver = driver; 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); 9788c2ecf20Sopenharmony_ci} 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_cistatic int usbhsg_gadget_stop(struct usb_gadget *gadget) 9818c2ecf20Sopenharmony_ci{ 9828c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 9838c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(gpriv->transceiver)) 9888c2ecf20Sopenharmony_ci otg_set_peripheral(gpriv->transceiver->otg, NULL); 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci gpriv->driver = NULL; 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci return 0; 9938c2ecf20Sopenharmony_ci} 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci/* 9968c2ecf20Sopenharmony_ci * usb gadget ops 9978c2ecf20Sopenharmony_ci */ 9988c2ecf20Sopenharmony_cistatic int usbhsg_get_frame(struct usb_gadget *gadget) 9998c2ecf20Sopenharmony_ci{ 10008c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 10018c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci return usbhs_frame_get_num(priv); 10048c2ecf20Sopenharmony_ci} 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_cistatic int usbhsg_pullup(struct usb_gadget *gadget, int is_on) 10078c2ecf20Sopenharmony_ci{ 10088c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 10098c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 10108c2ecf20Sopenharmony_ci unsigned long flags; 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci usbhs_lock(priv, flags); 10138c2ecf20Sopenharmony_ci if (is_on) 10148c2ecf20Sopenharmony_ci usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT); 10158c2ecf20Sopenharmony_ci else 10168c2ecf20Sopenharmony_ci usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT); 10178c2ecf20Sopenharmony_ci usbhsg_update_pullup(priv); 10188c2ecf20Sopenharmony_ci usbhs_unlock(priv, flags); 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci return 0; 10218c2ecf20Sopenharmony_ci} 10228c2ecf20Sopenharmony_ci 10238c2ecf20Sopenharmony_cistatic int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self) 10248c2ecf20Sopenharmony_ci{ 10258c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci if (is_self) 10288c2ecf20Sopenharmony_ci usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED); 10298c2ecf20Sopenharmony_ci else 10308c2ecf20Sopenharmony_ci usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED); 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci gadget->is_selfpowered = (is_self != 0); 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ci return 0; 10358c2ecf20Sopenharmony_ci} 10368c2ecf20Sopenharmony_ci 10378c2ecf20Sopenharmony_cistatic int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active) 10388c2ecf20Sopenharmony_ci{ 10398c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 10408c2ecf20Sopenharmony_ci struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 10418c2ecf20Sopenharmony_ci struct platform_device *pdev = usbhs_priv_to_pdev(priv); 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci gpriv->vbus_active = !!is_active; 10448c2ecf20Sopenharmony_ci 10458c2ecf20Sopenharmony_ci usbhsc_schedule_notify_hotplug(pdev); 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci return 0; 10488c2ecf20Sopenharmony_ci} 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_cistatic const struct usb_gadget_ops usbhsg_gadget_ops = { 10518c2ecf20Sopenharmony_ci .get_frame = usbhsg_get_frame, 10528c2ecf20Sopenharmony_ci .set_selfpowered = usbhsg_set_selfpowered, 10538c2ecf20Sopenharmony_ci .udc_start = usbhsg_gadget_start, 10548c2ecf20Sopenharmony_ci .udc_stop = usbhsg_gadget_stop, 10558c2ecf20Sopenharmony_ci .pullup = usbhsg_pullup, 10568c2ecf20Sopenharmony_ci .vbus_session = usbhsg_vbus_session, 10578c2ecf20Sopenharmony_ci}; 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_cistatic int usbhsg_start(struct usbhs_priv *priv) 10608c2ecf20Sopenharmony_ci{ 10618c2ecf20Sopenharmony_ci return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); 10628c2ecf20Sopenharmony_ci} 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_cistatic int usbhsg_stop(struct usbhs_priv *priv) 10658c2ecf20Sopenharmony_ci{ 10668c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci /* cable disconnect */ 10698c2ecf20Sopenharmony_ci if (gpriv->driver && 10708c2ecf20Sopenharmony_ci gpriv->driver->disconnect) 10718c2ecf20Sopenharmony_ci gpriv->driver->disconnect(&gpriv->gadget); 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); 10748c2ecf20Sopenharmony_ci} 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ciint usbhs_mod_gadget_probe(struct usbhs_priv *priv) 10778c2ecf20Sopenharmony_ci{ 10788c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv; 10798c2ecf20Sopenharmony_ci struct usbhsg_uep *uep; 10808c2ecf20Sopenharmony_ci struct device *dev = usbhs_priv_to_dev(priv); 10818c2ecf20Sopenharmony_ci struct renesas_usbhs_driver_pipe_config *pipe_configs = 10828c2ecf20Sopenharmony_ci usbhs_get_dparam(priv, pipe_configs); 10838c2ecf20Sopenharmony_ci int pipe_size = usbhs_get_dparam(priv, pipe_size); 10848c2ecf20Sopenharmony_ci int i; 10858c2ecf20Sopenharmony_ci int ret; 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); 10888c2ecf20Sopenharmony_ci if (!gpriv) 10898c2ecf20Sopenharmony_ci return -ENOMEM; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci uep = kcalloc(pipe_size, sizeof(struct usbhsg_uep), GFP_KERNEL); 10928c2ecf20Sopenharmony_ci if (!uep) { 10938c2ecf20Sopenharmony_ci ret = -ENOMEM; 10948c2ecf20Sopenharmony_ci goto usbhs_mod_gadget_probe_err_gpriv; 10958c2ecf20Sopenharmony_ci } 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED); 10988c2ecf20Sopenharmony_ci dev_info(dev, "%stransceiver found\n", 10998c2ecf20Sopenharmony_ci !IS_ERR(gpriv->transceiver) ? "" : "no "); 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci /* 11028c2ecf20Sopenharmony_ci * CAUTION 11038c2ecf20Sopenharmony_ci * 11048c2ecf20Sopenharmony_ci * There is no guarantee that it is possible to access usb module here. 11058c2ecf20Sopenharmony_ci * Don't accesses to it. 11068c2ecf20Sopenharmony_ci * The accesse will be enable after "usbhsg_start" 11078c2ecf20Sopenharmony_ci */ 11088c2ecf20Sopenharmony_ci 11098c2ecf20Sopenharmony_ci /* 11108c2ecf20Sopenharmony_ci * register itself 11118c2ecf20Sopenharmony_ci */ 11128c2ecf20Sopenharmony_ci usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); 11138c2ecf20Sopenharmony_ci 11148c2ecf20Sopenharmony_ci /* init gpriv */ 11158c2ecf20Sopenharmony_ci gpriv->mod.name = "gadget"; 11168c2ecf20Sopenharmony_ci gpriv->mod.start = usbhsg_start; 11178c2ecf20Sopenharmony_ci gpriv->mod.stop = usbhsg_stop; 11188c2ecf20Sopenharmony_ci gpriv->uep = uep; 11198c2ecf20Sopenharmony_ci gpriv->uep_size = pipe_size; 11208c2ecf20Sopenharmony_ci usbhsg_status_init(gpriv); 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci /* 11238c2ecf20Sopenharmony_ci * init gadget 11248c2ecf20Sopenharmony_ci */ 11258c2ecf20Sopenharmony_ci gpriv->gadget.dev.parent = dev; 11268c2ecf20Sopenharmony_ci gpriv->gadget.name = "renesas_usbhs_udc"; 11278c2ecf20Sopenharmony_ci gpriv->gadget.ops = &usbhsg_gadget_ops; 11288c2ecf20Sopenharmony_ci gpriv->gadget.max_speed = USB_SPEED_HIGH; 11298c2ecf20Sopenharmony_ci gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv, 11308c2ecf20Sopenharmony_ci has_usb_dmac); 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&gpriv->gadget.ep_list); 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci /* 11358c2ecf20Sopenharmony_ci * init usb_ep 11368c2ecf20Sopenharmony_ci */ 11378c2ecf20Sopenharmony_ci usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { 11388c2ecf20Sopenharmony_ci uep->gpriv = gpriv; 11398c2ecf20Sopenharmony_ci uep->pipe = NULL; 11408c2ecf20Sopenharmony_ci snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_ci uep->ep.name = uep->ep_name; 11438c2ecf20Sopenharmony_ci uep->ep.ops = &usbhsg_ep_ops; 11448c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&uep->ep.ep_list); 11458c2ecf20Sopenharmony_ci spin_lock_init(&uep->lock); 11468c2ecf20Sopenharmony_ci 11478c2ecf20Sopenharmony_ci /* init DCP */ 11488c2ecf20Sopenharmony_ci if (usbhsg_is_dcp(uep)) { 11498c2ecf20Sopenharmony_ci gpriv->gadget.ep0 = &uep->ep; 11508c2ecf20Sopenharmony_ci usb_ep_set_maxpacket_limit(&uep->ep, 64); 11518c2ecf20Sopenharmony_ci uep->ep.caps.type_control = true; 11528c2ecf20Sopenharmony_ci } else { 11538c2ecf20Sopenharmony_ci /* init normal pipe */ 11548c2ecf20Sopenharmony_ci if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC) 11558c2ecf20Sopenharmony_ci uep->ep.caps.type_iso = true; 11568c2ecf20Sopenharmony_ci if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK) 11578c2ecf20Sopenharmony_ci uep->ep.caps.type_bulk = true; 11588c2ecf20Sopenharmony_ci if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT) 11598c2ecf20Sopenharmony_ci uep->ep.caps.type_int = true; 11608c2ecf20Sopenharmony_ci usb_ep_set_maxpacket_limit(&uep->ep, 11618c2ecf20Sopenharmony_ci pipe_configs[i].bufsize); 11628c2ecf20Sopenharmony_ci list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); 11638c2ecf20Sopenharmony_ci } 11648c2ecf20Sopenharmony_ci uep->ep.caps.dir_in = true; 11658c2ecf20Sopenharmony_ci uep->ep.caps.dir_out = true; 11668c2ecf20Sopenharmony_ci } 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci ret = usb_add_gadget_udc(dev, &gpriv->gadget); 11698c2ecf20Sopenharmony_ci if (ret) 11708c2ecf20Sopenharmony_ci goto err_add_udc; 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci dev_info(dev, "gadget probed\n"); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci return 0; 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_cierr_add_udc: 11788c2ecf20Sopenharmony_ci kfree(gpriv->uep); 11798c2ecf20Sopenharmony_ci 11808c2ecf20Sopenharmony_ciusbhs_mod_gadget_probe_err_gpriv: 11818c2ecf20Sopenharmony_ci kfree(gpriv); 11828c2ecf20Sopenharmony_ci 11838c2ecf20Sopenharmony_ci return ret; 11848c2ecf20Sopenharmony_ci} 11858c2ecf20Sopenharmony_ci 11868c2ecf20Sopenharmony_civoid usbhs_mod_gadget_remove(struct usbhs_priv *priv) 11878c2ecf20Sopenharmony_ci{ 11888c2ecf20Sopenharmony_ci struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci usb_del_gadget_udc(&gpriv->gadget); 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_ci kfree(gpriv->uep); 11938c2ecf20Sopenharmony_ci kfree(gpriv); 11948c2ecf20Sopenharmony_ci} 1195