39 #include <linux/kernel.h>
41 #include <linux/slab.h>
47 #include <linux/list.h>
50 #include <linux/usb/ch9.h>
145 dev_vdbg(dwc->
dev,
"link state change request timed out\n");
173 int last_fifo_depth = 0;
196 int fifo_number = dep->
number >> 1;
206 if (usb_endpoint_xfer_bulk(dep->
endpoint.desc)
207 || usb_endpoint_xfer_isoc(dep->
endpoint.desc))
221 tmp = mult * (dep->
endpoint.maxpacket + mdwidth);
226 fifo_size |= (last_fifo_depth << 16);
229 dep->
name, last_fifo_depth, fifo_size & 0xffff);
234 last_fifo_depth += (fifo_size & 0xffff);
246 if (req->
request.num_mapped_sgs)
257 usb_endpoint_xfer_isoc(dep->
endpoint.desc))
272 dev_dbg(dwc->
dev,
"request %p from %s completed %d/%d ===> %d\n",
276 spin_unlock(&dwc->
lock);
278 spin_lock(&dwc->
lock);
281 static const char *dwc3_gadget_ep_cmd_string(
u8 cmd)
285 return "Start New Configuration";
287 return "End Transfer";
289 return "Update Transfer";
291 return "Start Transfer";
293 return "Clear Stall";
297 return "Get Endpoint State";
299 return "Set Endpoint Transfer Resource";
301 return "Set Endpoint Configuration";
303 return "UNKNOWN command";
341 dev_vdbg(dwc->
dev,
"%s: cmd '%s' params %08x %08x %08x\n",
343 dwc3_gadget_ep_cmd_string(cmd), params->
param0,
379 static int dwc3_alloc_trb_pool(
struct dwc3_ep *dep)
393 dev_err(dep->
dwc->dev,
"failed to allocate trb pool for %s\n",
401 static void dwc3_free_trb_pool(
struct dwc3_ep *dep)
412 static int dwc3_gadget_start_config(
struct dwc3 *dwc,
struct dwc3_ep *dep)
435 static int dwc3_gadget_set_ep_config(
struct dwc3 *dwc,
struct dwc3_ep *dep,
460 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
466 if (usb_endpoint_xfer_isoc(desc))
493 static int dwc3_gadget_set_xfer_resource(
struct dwc3 *dwc,
struct dwc3_ep *dep)
512 static int __dwc3_gadget_ep_enable(
struct dwc3_ep *dep,
522 ret = dwc3_gadget_start_config(dwc, dep);
527 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
535 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
541 dep->
type = usb_endpoint_type(desc);
548 if (!usb_endpoint_xfer_isoc(desc))
551 memset(&trb_link, 0,
sizeof(trb_link));
567 static void dwc3_stop_active_transfer(
struct dwc3 *dwc,
u32 epnum);
568 static void dwc3_remove_requests(
struct dwc3 *dwc,
struct dwc3_ep *dep)
573 dwc3_stop_active_transfer(dwc, dep->
number);
598 static int __dwc3_gadget_ep_disable(
struct dwc3_ep *dep)
603 dwc3_remove_requests(dwc, dep);
620 static int dwc3_gadget_ep0_enable(
struct usb_ep *ep,
626 static int dwc3_gadget_ep0_disable(
struct usb_ep *ep)
633 static int dwc3_gadget_ep_enable(
struct usb_ep *ep,
642 pr_debug(
"dwc3: invalid parameters\n");
647 pr_debug(
"dwc3: missing wMaxPacketSize\n");
660 switch (usb_endpoint_type(desc)) {
674 dev_err(dwc->
dev,
"invalid endpoint transfer type\n");
680 ret = __dwc3_gadget_ep_enable(dep, desc, ep->
comp_desc,
false);
681 spin_unlock_irqrestore(&dwc->
lock, flags);
686 static int dwc3_gadget_ep_disable(
struct usb_ep *ep)
694 pr_debug(
"dwc3: invalid parameters\n");
709 (dep->
number & 1) ?
"in" :
"out");
712 ret = __dwc3_gadget_ep_disable(dep);
713 spin_unlock_irqrestore(&dwc->
lock, flags);
725 req = kzalloc(
sizeof(*req), gfp_flags);
737 static void dwc3_gadget_ep_free_request(
struct usb_ep *ep,
750 static void dwc3_prepare_one_trb(
struct dwc3_ep *dep,
757 unsigned int cur_slot;
759 dev_vdbg(dwc->
dev,
"%s: req %p dma %08llx length %d%s%s\n",
760 dep->
name, req, (
unsigned long long) dma,
761 length, last ?
" last" :
"",
762 chain ?
" chain" :
"");
770 usb_endpoint_xfer_isoc(dep->
endpoint.desc))
774 dwc3_gadget_move_request_queued(req);
776 req->
trb_dma = dwc3_trb_dma_offset(dep, trb);
783 switch (usb_endpoint_type(dep->
endpoint.desc)) {
791 if (!req->
request.no_interrupt)
807 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
833 static void dwc3_prepare_trbs(
struct dwc3_ep *dep,
bool starting)
838 unsigned int last_one = 0;
846 if (!usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
872 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
882 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->
endpoint.desc))
889 if (req->
request.num_mapped_sgs > 0) {
896 unsigned chain =
true;
914 dwc3_prepare_one_trb(dep, req, dma, length,
932 dwc3_prepare_one_trb(dep, req, dma, length,
941 static int __dwc3_gadget_kick_transfer(
struct dwc3_ep *dep,
u16 cmd_param,
962 dwc3_prepare_trbs(dep, start_new);
967 dwc3_prepare_trbs(dep, start_new);
991 dev_dbg(dwc->
dev,
"failed to send STARTTRANSFER command\n");
1015 static void __dwc3_gadget_start_isoc(
struct dwc3 *dwc,
1021 dev_vdbg(dwc->
dev,
"ISOC ep %s run out for requests.\n",
1030 __dwc3_gadget_kick_transfer(dep, uf, 1);
1033 static void dwc3_gadget_start_isoc(
struct dwc3 *dwc,
1039 cur_uf =
event->parameters &
mask;
1041 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1093 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
1094 dwc3_stop_active_transfer(dwc, dep->
number);
1098 ret = __dwc3_gadget_kick_transfer(dep, 0,
true);
1099 if (ret && ret != -
EBUSY)
1100 dev_dbg(dwc->
dev,
"%s: failed to kick transfers\n",
1109 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc) &&
1115 if (ret && ret != -
EBUSY)
1116 dev_dbg(dwc->
dev,
"%s: failed to kick transfers\n",
1124 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc) &&
1126 __dwc3_gadget_start_isoc(dwc, dep, dep->
current_uf);
1140 unsigned long flags;
1145 dev_dbg(dwc->
dev,
"trying to queue request %p to disabled %s\n",
1150 dev_vdbg(dwc->
dev,
"queing request %p to %s length %d\n",
1154 ret = __dwc3_gadget_ep_queue(dep, req);
1155 spin_unlock_irqrestore(&dwc->
lock, flags);
1160 static int dwc3_gadget_ep_dequeue(
struct usb_ep *ep,
1169 unsigned long flags;
1186 dwc3_stop_active_transfer(dwc, dep->
number);
1189 dev_err(dwc->
dev,
"request %p was not queued to %s\n",
1200 spin_unlock_irqrestore(&dwc->
lock, flags);
1211 memset(¶ms, 0x00,
sizeof(params));
1217 dev_err(dwc->
dev,
"failed to %s STALL on %s\n",
1218 value ?
"set" :
"clear",
1229 dev_err(dwc->
dev,
"failed to %s STALL on %s\n",
1230 value ?
"set" :
"clear",
1239 static int dwc3_gadget_ep_set_halt(
struct usb_ep *ep,
int value)
1244 unsigned long flags;
1250 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
1258 spin_unlock_irqrestore(&dwc->
lock, flags);
1263 static int dwc3_gadget_ep_set_wedge(
struct usb_ep *ep)
1267 unsigned long flags;
1271 spin_unlock_irqrestore(&dwc->
lock, flags);
1276 return dwc3_gadget_ep_set_halt(ep, 1);
1287 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1288 .enable = dwc3_gadget_ep0_enable,
1289 .disable = dwc3_gadget_ep0_disable,
1290 .alloc_request = dwc3_gadget_ep_alloc_request,
1291 .free_request = dwc3_gadget_ep_free_request,
1293 .dequeue = dwc3_gadget_ep_dequeue,
1295 .set_wedge = dwc3_gadget_ep_set_wedge,
1298 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1299 .enable = dwc3_gadget_ep_enable,
1300 .disable = dwc3_gadget_ep_disable,
1301 .alloc_request = dwc3_gadget_ep_alloc_request,
1302 .free_request = dwc3_gadget_ep_free_request,
1303 .queue = dwc3_gadget_ep_queue,
1304 .dequeue = dwc3_gadget_ep_dequeue,
1305 .set_halt = dwc3_gadget_ep_set_halt,
1306 .set_wedge = dwc3_gadget_ep_set_wedge,
1311 static int dwc3_gadget_get_frame(
struct usb_gadget *g)
1320 static int dwc3_gadget_wakeup(
struct usb_gadget *g)
1325 unsigned long flags;
1346 dev_dbg(dwc->
dev,
"no wakeup on SuperSpeed\n");
1353 switch (link_state) {
1358 dev_dbg(dwc->
dev,
"can't wakeup from link state %d\n",
1366 dev_err(dwc->
dev,
"failed to put link in Recovery\n");
1390 dev_err(dwc->
dev,
"failed to send remote wakeup\n");
1395 spin_unlock_irqrestore(&dwc->
lock, flags);
1400 static int dwc3_gadget_set_selfpowered(
struct usb_gadget *g,
1404 unsigned long flags;
1408 spin_unlock_irqrestore(&dwc->
lock, flags);
1413 static int dwc3_gadget_run_stop(
struct dwc3 *dwc,
int is_on)
1452 is_on ?
"connect" :
"disconnect");
1457 static int dwc3_gadget_pullup(
struct usb_gadget *g,
int is_on)
1460 unsigned long flags;
1466 ret = dwc3_gadget_run_stop(dwc, is_on);
1467 spin_unlock_irqrestore(&dwc->
lock, flags);
1472 static int dwc3_gadget_start(
struct usb_gadget *g,
1477 unsigned long flags;
1484 dev_err(dwc->
dev,
"%s is already bound to %s\n",
1522 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc,
NULL,
false);
1529 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc,
NULL,
false);
1539 spin_unlock_irqrestore(&dwc->
lock, flags);
1544 __dwc3_gadget_ep_disable(dwc->
eps[0]);
1547 spin_unlock_irqrestore(&dwc->
lock, flags);
1552 static int dwc3_gadget_stop(
struct usb_gadget *g,
1556 unsigned long flags;
1560 __dwc3_gadget_ep_disable(dwc->
eps[0]);
1561 __dwc3_gadget_ep_disable(dwc->
eps[1]);
1566 spin_unlock_irqrestore(&dwc->
lock, flags);
1572 .get_frame = dwc3_gadget_get_frame,
1573 .wakeup = dwc3_gadget_wakeup,
1574 .set_selfpowered = dwc3_gadget_set_selfpowered,
1575 .pullup = dwc3_gadget_pullup,
1576 .udc_start = dwc3_gadget_start,
1577 .udc_stop = dwc3_gadget_stop,
1582 static int __devinit dwc3_gadget_init_endpoints(
struct dwc3 *dwc)
1587 INIT_LIST_HEAD(&dwc->
gadget.ep_list);
1592 dev_err(dwc->
dev,
"can't allocate endpoint %d\n",
1599 dwc->
eps[epnum] = dep;
1602 (epnum & 1) ?
"in" :
"out");
1606 if (epnum == 0 || epnum == 1) {
1608 dep->
endpoint.ops = &dwc3_gadget_ep0_ops;
1616 dep->
endpoint.ops = &dwc3_gadget_ep_ops;
1620 ret = dwc3_alloc_trb_pool(dep);
1632 static void dwc3_gadget_free_endpoints(
struct dwc3 *dwc)
1638 dep = dwc->
eps[epnum];
1639 dwc3_free_trb_pool(dep);
1641 if (epnum != 0 && epnum != 1)
1648 static void dwc3_gadget_release(
struct device *
dev)
1650 dev_dbg(dev,
"%s\n", __func__);
1654 static int dwc3_cleanup_done_reqs(
struct dwc3 *dwc,
struct dwc3_ep *dep,
1660 unsigned int s_pkt = 0;
1661 unsigned int trb_status;
1681 dev_err(dwc->
dev,
"%s's TRB (%p) still owned by HW\n",
1689 dev_dbg(dwc->
dev,
"incomplete IN transfer %s\n",
1695 dev_err(dwc->
dev,
"incomplete IN transfer %s\n",
1731 static void dwc3_endpoint_transfer_complete(
struct dwc3 *dwc,
1735 unsigned status = 0;
1741 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1771 static void dwc3_endpoint_interrupt(
struct dwc3 *dwc,
1775 u8 epnum =
event->endpoint_number;
1777 dep = dwc->
eps[epnum];
1785 if (epnum == 0 || epnum == 1) {
1794 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
1795 dev_dbg(dwc->
dev,
"%s is an Isochronous endpoint\n",
1800 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1803 if (!usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
1804 dev_dbg(dwc->
dev,
"%s is not an Isochronous endpoint\n",
1809 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1812 if (usb_endpoint_xfer_isoc(dep->
endpoint.desc)) {
1813 dwc3_gadget_start_isoc(dwc, dep, event);
1821 :
"Transfer Not Active");
1823 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1824 if (!ret || ret == -
EBUSY)
1827 dev_dbg(dwc->
dev,
"%s: failed to kick transfers\n",
1833 if (!usb_endpoint_xfer_bulk(dep->
endpoint.desc)) {
1834 dev_err(dwc->
dev,
"Stream event for non-Bulk %s\n",
1841 dev_vdbg(dwc->
dev,
"Stream %d found and started\n",
1848 dev_dbg(dwc->
dev,
"Couldn't find suitable stream\n");
1855 dev_vdbg(dwc->
dev,
"Endpoint Command Complete\n");
1860 static void dwc3_disconnect_gadget(
struct dwc3 *dwc)
1863 spin_unlock(&dwc->
lock);
1865 spin_lock(&dwc->
lock);
1869 static void dwc3_stop_active_transfer(
struct dwc3 *dwc,
u32 epnum)
1876 dep = dwc->
eps[epnum];
1911 static void dwc3_stop_active_transfers(
struct dwc3 *dwc)
1918 dep = dwc->
eps[epnum];
1922 dwc3_remove_requests(dwc, dep);
1926 static void dwc3_clear_stall_all_ep(
struct dwc3 *dwc)
1935 dep = dwc->
eps[epnum];
1949 static void dwc3_gadget_disconnect_interrupt(
struct dwc3 *dwc)
1962 dwc3_disconnect_gadget(dwc);
1969 static void dwc3_gadget_usb3_phy_suspend(
struct dwc3 *dwc,
int suspend)
1983 static void dwc3_gadget_usb2_phy_suspend(
struct dwc3 *dwc,
int suspend)
1997 static void dwc3_gadget_reset_interrupt(
struct dwc3 *dwc)
2031 dwc3_gadget_disconnect_interrupt(dwc);
2040 dwc3_gadget_usb2_phy_suspend(dwc,
false);
2041 dwc3_gadget_usb3_phy_suspend(dwc,
false);
2045 dwc3_disconnect_gadget(dwc);
2052 dwc3_stop_active_transfers(dwc);
2053 dwc3_clear_stall_all_ep(dwc);
2062 static void dwc3_update_ram_clk_sel(
struct dwc3 *dwc,
u32 speed)
2087 static void dwc3_gadget_phy_suspend(
struct dwc3 *dwc,
u8 speed)
2091 dwc3_gadget_usb2_phy_suspend(dwc,
true);
2096 dwc3_gadget_usb3_phy_suspend(dwc,
true);
2101 static void dwc3_gadget_conndone_interrupt(
struct dwc3 *dwc)
2117 dwc3_update_ram_clk_sel(dwc, speed);
2135 dwc3_gadget_reset_interrupt(dwc);
2138 dwc->
gadget.ep0->maxpacket = 512;
2143 dwc->
gadget.ep0->maxpacket = 64;
2149 dwc->
gadget.ep0->maxpacket = 64;
2154 dwc->
gadget.ep0->maxpacket = 8;
2162 dwc3_gadget_phy_suspend(dwc, dwc->
gadget.speed);
2166 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc,
NULL,
true);
2173 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc,
NULL,
true);
2188 static void dwc3_gadget_wakeup_interrupt(
struct dwc3 *dwc)
2200 static void dwc3_gadget_linksts_change_interrupt(
struct dwc3 *dwc,
2201 unsigned int evtinfo)
2238 dwc->
u1u2 = reg & u1u2;
2256 static void dwc3_gadget_interrupt(
struct dwc3 *dwc,
2259 switch (event->
type) {
2261 dwc3_gadget_disconnect_interrupt(dwc);
2264 dwc3_gadget_reset_interrupt(dwc);
2267 dwc3_gadget_conndone_interrupt(dwc);
2270 dwc3_gadget_wakeup_interrupt(dwc);
2273 dwc3_gadget_linksts_change_interrupt(dwc, event->
event_info);
2295 static void dwc3_process_event_entry(
struct dwc3 *dwc,
2299 if (event->
type.is_devspec == 0) {
2301 return dwc3_endpoint_interrupt(dwc, &event->
depevt);
2304 switch (event->
type.type) {
2306 dwc3_gadget_interrupt(dwc, &event->
devt);
2333 dwc3_process_event_entry(dwc, &event);
2350 static irqreturn_t dwc3_interrupt(
int irq,
void *_dwc)
2352 struct dwc3 *dwc = _dwc;
2356 spin_lock(&dwc->
lock);
2361 status = dwc3_process_event_buf(dwc, i);
2366 spin_unlock(&dwc->
lock);
2386 dev_err(dwc->
dev,
"failed to allocate ctrl request\n");
2394 dev_err(dwc->
dev,
"failed to allocate ep0 trb\n");
2401 dev_err(dwc->
dev,
"failed to allocate setup buffer\n");
2410 dev_err(dwc->
dev,
"failed to allocate ep0 bounce buffer\n");
2417 dwc->
gadget.ops = &dwc3_gadget_ops;
2421 dwc->
gadget.sg_supported =
true;
2425 dwc->
gadget.dev.dma_parms = dwc->
dev->dma_parms;
2426 dwc->
gadget.dev.dma_mask = dwc->
dev->dma_mask;
2427 dwc->
gadget.dev.release = dwc3_gadget_release;
2428 dwc->
gadget.name =
"dwc3-gadget";
2435 ret = dwc3_gadget_init_endpoints(dwc);
2444 dev_err(dwc->
dev,
"failed to request irq #%d --> %d\n",
2479 dwc3_gadget_usb2_phy_suspend(dwc,
false);
2480 dwc3_gadget_usb3_phy_suspend(dwc,
false);
2485 dev_err(dwc->
dev,
"failed to register gadget device\n");
2492 dev_err(dwc->
dev,
"failed to register udc\n");
2506 dwc3_gadget_free_endpoints(dwc);
2537 dwc3_gadget_free_endpoints(dwc);