blob: 9852ec5e6e017e91049d1ecd082137c3eb53ce5f [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
David Lopoaa69a802008-11-17 14:14:51 -08002/*
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03003 * udc.c - ChipIdea UDC driver
David Lopoaa69a802008-11-17 14:14:51 -08004 *
5 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
6 *
7 * Author: David Lopo
David Lopoaa69a802008-11-17 14:14:51 -08008 */
9
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020010#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080011#include <linux/device.h>
12#include <linux/dmapool.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053013#include <linux/err.h>
Alexander Shishkin5b083192013-03-30 02:46:18 +020014#include <linux/irqreturn.h>
David Lopoaa69a802008-11-17 14:14:51 -080015#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053017#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/usb/ch9.h>
19#include <linux/usb/gadget.h>
Li Jun95f55552014-04-23 15:56:47 +080020#include <linux/usb/otg-fsm.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030021#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080022
Alexander Shishkine443b332012-05-11 17:25:46 +030023#include "ci.h"
24#include "udc.h"
25#include "bits.h"
Peter Chen3f124d22013-08-14 12:44:07 +030026#include "otg.h"
Li Jun4dcf7202014-04-23 15:56:50 +080027#include "otg_fsm.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020028
David Lopoaa69a802008-11-17 14:14:51 -080029/* control endpoint description */
30static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053031ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080032 .bLength = USB_DT_ENDPOINT_SIZE,
33 .bDescriptorType = USB_DT_ENDPOINT,
34
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053035 .bEndpointAddress = USB_DIR_OUT,
36 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
37 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
38};
39
40static const struct usb_endpoint_descriptor
41ctrl_endpt_in_desc = {
42 .bLength = USB_DT_ENDPOINT_SIZE,
43 .bDescriptorType = USB_DT_ENDPOINT,
44
45 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080046 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
47 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
48};
49
David Lopoaa69a802008-11-17 14:14:51 -080050/**
51 * hw_ep_bit: calculates the bit number
52 * @num: endpoint number
53 * @dir: endpoint direction
54 *
55 * This function returns bit number
56 */
57static inline int hw_ep_bit(int num, int dir)
58{
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +000059 return num + ((dir == TX) ? 16 : 0);
David Lopoaa69a802008-11-17 14:14:51 -080060}
61
Alexander Shishkin8e229782013-06-24 14:46:36 +030062static inline int ep_to_bit(struct ci_hdrc *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020063{
Richard Zhao26c696c2012-07-07 22:56:40 +080064 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020065
Richard Zhao26c696c2012-07-07 22:56:40 +080066 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020067 n += fill;
68
69 return n;
70}
71
David Lopoaa69a802008-11-17 14:14:51 -080072/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030073 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080074 * @dma: 0 => disable, !0 => enable and set dma engine
75 *
76 * This function returns an error code
77 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030078static int hw_device_state(struct ci_hdrc *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080079{
80 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080081 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080082 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080083 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080084 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
David Lopoaa69a802008-11-17 14:14:51 -080085 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080086 hw_write(ci, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080087 }
88 return 0;
89}
90
91/**
92 * hw_ep_flush: flush endpoint fifo (execute without interruption)
93 * @num: endpoint number
94 * @dir: endpoint direction
95 *
96 * This function returns an error code
97 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030098static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -080099{
100 int n = hw_ep_bit(num, dir);
101
102 do {
103 /* flush any pending transfer */
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800104 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
Richard Zhao26c696c2012-07-07 22:56:40 +0800105 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800106 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800107 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800108
109 return 0;
110}
111
112/**
113 * hw_ep_disable: disables endpoint (execute without interruption)
114 * @num: endpoint number
115 * @dir: endpoint direction
116 *
117 * This function returns an error code
118 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300119static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800120{
Richard Zhao26c696c2012-07-07 22:56:40 +0800121 hw_write(ci, OP_ENDPTCTRL + num,
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +0000122 (dir == TX) ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800123 return 0;
124}
125
126/**
127 * hw_ep_enable: enables endpoint (execute without interruption)
128 * @num: endpoint number
129 * @dir: endpoint direction
130 * @type: endpoint type
131 *
132 * This function returns an error code
133 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300134static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800135{
136 u32 mask, data;
137
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +0000138 if (dir == TX) {
David Lopoaa69a802008-11-17 14:14:51 -0800139 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200140 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800141
142 mask |= ENDPTCTRL_TXS; /* unstall */
143 mask |= ENDPTCTRL_TXR; /* reset data toggle */
144 data |= ENDPTCTRL_TXR;
145 mask |= ENDPTCTRL_TXE; /* enable */
146 data |= ENDPTCTRL_TXE;
147 } else {
148 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200149 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800150
151 mask |= ENDPTCTRL_RXS; /* unstall */
152 mask |= ENDPTCTRL_RXR; /* reset data toggle */
153 data |= ENDPTCTRL_RXR;
154 mask |= ENDPTCTRL_RXE; /* enable */
155 data |= ENDPTCTRL_RXE;
156 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800157 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800158 return 0;
159}
160
161/**
162 * hw_ep_get_halt: return endpoint halt status
163 * @num: endpoint number
164 * @dir: endpoint direction
165 *
166 * This function returns 1 if endpoint halted
167 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300168static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800169{
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +0000170 u32 mask = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
David Lopoaa69a802008-11-17 14:14:51 -0800171
Richard Zhao26c696c2012-07-07 22:56:40 +0800172 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800173}
174
175/**
David Lopoaa69a802008-11-17 14:14:51 -0800176 * hw_ep_prime: primes endpoint (execute without interruption)
177 * @num: endpoint number
178 * @dir: endpoint direction
179 * @is_ctrl: true if control endpoint
180 *
181 * This function returns an error code
182 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300183static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800184{
185 int n = hw_ep_bit(num, dir);
186
Stefan Wahren66b76db2016-07-09 14:16:38 +0000187 /* Synchronize before ep prime */
188 wmb();
189
Richard Zhao26c696c2012-07-07 22:56:40 +0800190 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800191 return -EAGAIN;
192
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800193 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800194
Richard Zhao26c696c2012-07-07 22:56:40 +0800195 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800196 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800197 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800198 return -EAGAIN;
199
200 /* status shoult be tested according with manual but it doesn't work */
201 return 0;
202}
203
204/**
205 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
206 * without interruption)
207 * @num: endpoint number
208 * @dir: endpoint direction
209 * @value: true => stall, false => unstall
210 *
211 * This function returns an error code
212 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300213static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800214{
215 if (value != 0 && value != 1)
216 return -EINVAL;
217
218 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300219 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +0000220 u32 mask_xs = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
221 u32 mask_xr = (dir == TX) ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
David Lopoaa69a802008-11-17 14:14:51 -0800222
223 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800224 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300225 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800226 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800227
228 return 0;
229}
230
231/**
David Lopoaa69a802008-11-17 14:14:51 -0800232 * hw_is_port_high_speed: test if port is high speed
233 *
234 * This function returns true if high speed port
235 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300236static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800237{
Richard Zhao26c696c2012-07-07 22:56:40 +0800238 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
239 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800240}
241
242/**
David Lopoaa69a802008-11-17 14:14:51 -0800243 * hw_test_and_clear_complete: test & clear complete status (execute without
244 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200245 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800246 *
247 * This function returns complete status
248 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300249static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800250{
Richard Zhao26c696c2012-07-07 22:56:40 +0800251 n = ep_to_bit(ci, n);
252 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800253}
254
255/**
256 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
257 * without interruption)
258 *
259 * This function returns active interrutps
260 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300261static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800262{
Richard Zhao26c696c2012-07-07 22:56:40 +0800263 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800264
Richard Zhao26c696c2012-07-07 22:56:40 +0800265 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800266 return reg;
267}
268
269/**
270 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
271 * interruption)
272 *
273 * This function returns guard value
274 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300275static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800276{
Richard Zhao26c696c2012-07-07 22:56:40 +0800277 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800278}
279
280/**
281 * hw_test_and_set_setup_guard: test & set setup guard (execute without
282 * interruption)
283 *
284 * This function returns guard value
285 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300286static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800287{
Richard Zhao26c696c2012-07-07 22:56:40 +0800288 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800289}
290
291/**
292 * hw_usb_set_address: configures USB address (execute without interruption)
293 * @value: new USB address
294 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300295 * This function explicitly sets the address, without the "USBADRA" (advance)
296 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800297 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300298static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800299{
Richard Zhao26c696c2012-07-07 22:56:40 +0800300 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200301 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800302}
303
304/**
305 * hw_usb_reset: restart device after a bus reset (execute without
306 * interruption)
307 *
308 * This function returns an error code
309 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300310static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800311{
Richard Zhao26c696c2012-07-07 22:56:40 +0800312 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800313
314 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800315 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800316
317 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800318 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800319
320 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800321 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800322
323 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800324 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800325 udelay(10); /* not RTOS friendly */
326
327 /* reset all endpoints ? */
328
329 /* reset internal status and wait for further instructions
330 no need to verify the port reset status (ESS does it) */
331
332 return 0;
333}
334
335/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800336 * UTIL block
337 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300338
Alexander Shishkin8e229782013-06-24 14:46:36 +0300339static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300340 unsigned length)
341{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300342 int i;
343 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300344 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
345 GFP_ATOMIC);
346
347 if (node == NULL)
348 return -ENOMEM;
349
Fabio Estevam58001ef2016-09-08 09:34:31 -0300350 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300351 if (node->ptr == NULL) {
352 kfree(node);
353 return -ENOMEM;
354 }
355
Michael Grzeschik2e270412013-06-13 17:59:54 +0300356 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
357 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
358 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800359 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
360 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
361
362 if (hwreq->req.length == 0
363 || hwreq->req.length % hwep->ep.maxpacket)
364 mul++;
Stephen Boyd34445fb2016-09-13 22:53:02 -0700365 node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO));
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800366 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300367
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300368 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300369 if (length) {
370 node->ptr->page[0] = cpu_to_le32(temp);
371 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300372 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300373 page &= ~TD_RESERVED_MASK;
374 node->ptr->page[i] = cpu_to_le32(page);
375 }
376 }
377
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300378 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300379
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300380 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300381 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300382 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300383 struct td_node, td);
384 lastnode->ptr->next = cpu_to_le32(node->dma);
385 }
386
387 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300388 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300389
390 return 0;
391}
392
David Lopoaa69a802008-11-17 14:14:51 -0800393/**
394 * _usb_addr: calculates endpoint address from direction & number
395 * @ep: endpoint
396 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300397static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800398{
399 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
400}
401
402/**
Felipe F. Tonelloe46fed92015-09-18 18:30:19 +0100403 * _hardware_enqueue: configures a request at hardware level
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300404 * @hwep: endpoint
Felipe F. Tonelloe46fed92015-09-18 18:30:19 +0100405 * @hwreq: request
David Lopoaa69a802008-11-17 14:14:51 -0800406 *
407 * This function returns an error code
408 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300409static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800410{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300411 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530412 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300413 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300414 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300415 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800416
David Lopoaa69a802008-11-17 14:14:51 -0800417 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300418 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800419 return -EALREADY;
420
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300421 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800422
Arnd Bergmannaeb78cd2017-03-13 10:18:42 +0800423 ret = usb_gadget_map_request_by_dev(ci->dev->parent,
424 &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300425 if (ret)
426 return ret;
427
Michael Grzeschik2e270412013-06-13 17:59:54 +0300428 /*
429 * The first buffer could be not page aligned.
430 * In that case we have to span into one extra td.
431 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300432 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300433 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300434
Felipe F. Tonello779debd2015-10-27 10:36:32 +0000435 if (rest == 0) {
436 ret = add_td_to_list(hwep, hwreq, 0);
437 if (ret < 0)
438 goto done;
439 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300440
Michael Grzeschik2e270412013-06-13 17:59:54 +0300441 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300442 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300443 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Felipe F. Tonello779debd2015-10-27 10:36:32 +0000444 ret = add_td_to_list(hwep, hwreq, count);
445 if (ret < 0)
446 goto done;
447
Michael Grzeschik2e270412013-06-13 17:59:54 +0300448 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200449 }
David Lopoaa69a802008-11-17 14:14:51 -0800450
Peter Chena4da4f12015-07-28 10:08:32 +0800451 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
Felipe F. Tonello779debd2015-10-27 10:36:32 +0000452 && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
453 ret = add_td_to_list(hwep, hwreq, 0);
454 if (ret < 0)
455 goto done;
456 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300457
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300458 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300459
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300460 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300461 struct td_node, td);
462
463 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300464 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300465 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300466 wmb();
467
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300468 hwreq->req.actual = 0;
469 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300470 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300471 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530472 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300473 struct td_node *prevlastnode;
474 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530475
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300476 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300477 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300478 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300479 struct td_node, td);
480
481 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530482 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800483 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530484 goto done;
485 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800486 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
487 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
488 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
489 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530490 if (tmp_stat)
491 goto done;
492 }
493
494 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300495 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
496 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200497 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800498
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800499 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300500 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300501
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800502 if (hwreq->req.length == 0
503 || hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300504 mul++;
Stephen Boyd34445fb2016-09-13 22:53:02 -0700505 hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT));
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300506 }
507
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300508 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
509 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530510done:
511 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800512}
513
Michael Grzeschik2e270412013-06-13 17:59:54 +0300514/*
515 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300516 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300517 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300518static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300519{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300520 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300521
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300522 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
523 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300524 kfree(pending);
525}
526
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800527static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
528 struct td_node *node)
529{
Stephen Boyd34445fb2016-09-13 22:53:02 -0700530 hwep->qh.ptr->td.next = cpu_to_le32(node->dma);
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800531 hwep->qh.ptr->td.token &=
532 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
533
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800534 return hw_ep_prime(ci, hwep->num, hwep->dir,
535 hwep->type == USB_ENDPOINT_XFER_CONTROL);
536}
537
David Lopoaa69a802008-11-17 14:14:51 -0800538/**
539 * _hardware_dequeue: handles a request at hardware level
540 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300541 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800542 *
543 * This function returns an error code
544 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300545static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800546{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300547 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300548 struct td_node *node, *tmpnode;
549 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300550 unsigned actual = hwreq->req.length;
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800551 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200552
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300553 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800554 return -EINVAL;
555
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300556 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530557
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300558 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300559 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300560 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800561 int n = hw_ep_bit(hwep->num, hwep->dir);
562
563 if (ci->rev == CI_REVISION_24)
564 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
565 reprime_dtd(ci, hwep, node);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300566 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530567 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300568 }
David Lopoaa69a802008-11-17 14:14:51 -0800569
Michael Grzeschik2e270412013-06-13 17:59:54 +0300570 remaining_length = (tmptoken & TD_TOTAL_BYTES);
571 remaining_length >>= __ffs(TD_TOTAL_BYTES);
572 actual -= remaining_length;
573
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300574 hwreq->req.status = tmptoken & TD_STATUS;
575 if ((TD_STATUS_HALTED & hwreq->req.status)) {
576 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300577 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300578 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
579 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300580 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300581 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
582 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300583 break;
584 }
585
586 if (remaining_length) {
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +0000587 if (hwep->dir == TX) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300588 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300589 break;
590 }
591 }
592 /*
593 * As the hardware could still address the freed td
594 * which will run the udc unusable, the cleanup of the
595 * td has to be delayed by one.
596 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300597 if (hwep->pending_td)
598 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300599
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300600 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300601 list_del_init(&node->td);
602 }
David Lopoaa69a802008-11-17 14:14:51 -0800603
Arnd Bergmannaeb78cd2017-03-13 10:18:42 +0800604 usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent,
605 &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800606
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300607 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800608
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300609 if (hwreq->req.status)
610 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800611
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300612 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800613}
614
615/**
616 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300617 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800618 *
619 * This function returns an error code
620 * Caller must hold lock
621 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300622static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300623__releases(hwep->lock)
624__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800625{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300626 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300627 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800628 return -EINVAL;
629
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300630 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800631
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300632 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800633
634 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300635 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
636 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300637
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300638 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
639 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300640 list_del_init(&node->td);
641 node->ptr = NULL;
642 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300643 }
644
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300645 list_del_init(&hwreq->queue);
646 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800647
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300648 if (hwreq->req.complete != NULL) {
649 spin_unlock(hwep->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200650 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300651 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800652 }
653 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300654
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300655 if (hwep->pending_td)
656 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300657
David Lopoaa69a802008-11-17 14:14:51 -0800658 return 0;
659}
660
Peter Chen56ffa1d2015-08-24 14:10:07 +0800661static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
662{
663 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
664 int direction, retval = 0;
665 unsigned long flags;
666
667 if (ep == NULL || hwep->ep.desc == NULL)
668 return -EINVAL;
669
670 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
671 return -EOPNOTSUPP;
672
673 spin_lock_irqsave(hwep->lock, flags);
674
675 if (value && hwep->dir == TX && check_transfer &&
676 !list_empty(&hwep->qh.queue) &&
677 !usb_endpoint_xfer_control(hwep->ep.desc)) {
678 spin_unlock_irqrestore(hwep->lock, flags);
679 return -EAGAIN;
680 }
681
682 direction = hwep->dir;
683 do {
684 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
685
686 if (!value)
687 hwep->wedge = 0;
688
689 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
690 hwep->dir = (hwep->dir == TX) ? RX : TX;
691
692 } while (hwep->dir != direction);
693
694 spin_unlock_irqrestore(hwep->lock, flags);
695 return retval;
696}
697
698
David Lopoaa69a802008-11-17 14:14:51 -0800699/**
700 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
701 * @gadget: gadget
702 *
703 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800704 */
705static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800706{
707 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300708 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530709 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800710
Richard Zhao26c696c2012-07-07 22:56:40 +0800711 spin_lock_irqsave(&ci->lock, flags);
712 ci->gadget.speed = USB_SPEED_UNKNOWN;
713 ci->remote_wakeup = 0;
714 ci->suspended = 0;
715 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530716
David Lopoaa69a802008-11-17 14:14:51 -0800717 /* flush all endpoints */
718 gadget_for_each_ep(ep, gadget) {
719 usb_ep_fifo_flush(ep);
720 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800721 usb_ep_fifo_flush(&ci->ep0out->ep);
722 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800723
David Lopoaa69a802008-11-17 14:14:51 -0800724 /* make sure to disable all endpoints */
725 gadget_for_each_ep(ep, gadget) {
726 usb_ep_disable(ep);
727 }
David Lopoaa69a802008-11-17 14:14:51 -0800728
Richard Zhao26c696c2012-07-07 22:56:40 +0800729 if (ci->status != NULL) {
730 usb_ep_free_request(&ci->ep0in->ep, ci->status);
731 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800732 }
733
David Lopoaa69a802008-11-17 14:14:51 -0800734 return 0;
735}
736
737/******************************************************************************
738 * ISR block
739 *****************************************************************************/
740/**
741 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800742 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800743 *
744 * This function resets USB engine after a bus reset occurred
745 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300746static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800747__releases(ci->lock)
748__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800749{
David Lopoaa69a802008-11-17 14:14:51 -0800750 int retval;
751
Peter Chena3aee362013-10-09 14:39:52 +0800752 spin_unlock(&ci->lock);
Peter Chenafbe4772014-11-06 14:27:58 +0800753 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
754 usb_gadget_udc_reset(&ci->gadget, ci->driver);
Peter Chen92b336d2013-09-17 12:37:19 +0800755
Richard Zhao26c696c2012-07-07 22:56:40 +0800756 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800757 if (retval)
758 goto done;
759
Richard Zhao26c696c2012-07-07 22:56:40 +0800760 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800761 if (retval)
762 goto done;
763
Richard Zhao26c696c2012-07-07 22:56:40 +0800764 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
765 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530766 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530767
Michael Grzeschikb9322252012-05-11 17:25:50 +0300768done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800769 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800770
David Lopoaa69a802008-11-17 14:14:51 -0800771 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800772 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800773}
774
775/**
776 * isr_get_status_complete: get_status request complete function
777 * @ep: endpoint
778 * @req: request handled
779 *
780 * Caller must release lock
781 */
782static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
783{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300784 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800785 return;
David Lopoaa69a802008-11-17 14:14:51 -0800786
787 kfree(req->buf);
788 usb_ep_free_request(ep, req);
789}
790
791/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200792 * _ep_queue: queues (submits) an I/O request to an endpoint
Felipe F. Tonelloe46fed92015-09-18 18:30:19 +0100793 * @ep: endpoint
794 * @req: request
795 * @gfp_flags: GFP flags (not used)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200796 *
797 * Caller must hold lock
Felipe F. Tonelloe46fed92015-09-18 18:30:19 +0100798 * This function returns an error code
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200799 */
800static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
801 gfp_t __maybe_unused gfp_flags)
802{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300803 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
804 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
805 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200806 int retval = 0;
807
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300808 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200809 return -EINVAL;
810
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300811 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200812 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300813 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200814 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300815 if (!list_empty(&hwep->qh.queue)) {
816 _ep_nuke(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300817 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
818 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200819 }
820 }
821
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300822 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
Felipe Balbia98e25e72016-09-28 13:26:18 +0300823 hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300824 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300825 return -EMSGSIZE;
826 }
827
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200828 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300829 if (!list_empty(&hwreq->queue)) {
830 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200831 return -EBUSY;
832 }
833
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200834 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300835 hwreq->req.status = -EINPROGRESS;
836 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200837
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300838 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200839
840 if (retval == -EALREADY)
841 retval = 0;
842 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300843 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200844
845 return retval;
846}
847
848/**
David Lopoaa69a802008-11-17 14:14:51 -0800849 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800850 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800851 * @setup: setup request packet
852 *
853 * This function returns an error code
854 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300855static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800856 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300857__releases(hwep->lock)
858__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800859{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300860 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800861 struct usb_request *req = NULL;
862 gfp_t gfp_flags = GFP_ATOMIC;
863 int dir, num, retval;
864
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300865 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800866 return -EINVAL;
867
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300868 spin_unlock(hwep->lock);
869 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
870 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800871 if (req == NULL)
872 return -ENOMEM;
873
874 req->complete = isr_get_status_complete;
875 req->length = 2;
876 req->buf = kzalloc(req->length, gfp_flags);
877 if (req->buf == NULL) {
878 retval = -ENOMEM;
879 goto err_free_req;
880 }
881
882 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Peter Chen1009f9a2015-01-28 16:32:25 +0800883 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
884 ci->gadget.is_selfpowered;
David Lopoaa69a802008-11-17 14:14:51 -0800885 } else if ((setup->bRequestType & USB_RECIP_MASK) \
886 == USB_RECIP_ENDPOINT) {
887 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
888 TX : RX;
889 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800890 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800891 }
892 /* else do nothing; reserved for future use */
893
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300894 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800895 if (retval)
896 goto err_free_buf;
897
898 return 0;
899
900 err_free_buf:
901 kfree(req->buf);
902 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300903 spin_unlock(hwep->lock);
904 usb_ep_free_request(&hwep->ep, req);
905 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800906 return retval;
907}
908
909/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530910 * isr_setup_status_complete: setup_status request complete function
911 * @ep: endpoint
912 * @req: request handled
913 *
914 * Caller must release lock. Put the port in test mode if test mode
915 * feature is selected.
916 */
917static void
918isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
919{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300920 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530921 unsigned long flags;
922
Richard Zhao26c696c2012-07-07 22:56:40 +0800923 if (ci->setaddr) {
924 hw_usb_set_address(ci, ci->address);
925 ci->setaddr = false;
Peter Chen10775eb2014-05-04 09:24:44 +0800926 if (ci->address)
927 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
Alexander Shishkinef15e542012-05-11 17:25:43 +0300928 }
929
Richard Zhao26c696c2012-07-07 22:56:40 +0800930 spin_lock_irqsave(&ci->lock, flags);
931 if (ci->test_mode)
932 hw_port_test_set(ci, ci->test_mode);
933 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530934}
935
936/**
David Lopoaa69a802008-11-17 14:14:51 -0800937 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800938 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800939 *
940 * This function returns an error code
941 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300942static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800943{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300944 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800945
Clemens Gruber6f3c4fb2016-09-05 19:29:58 +0200946 /*
947 * Unexpected USB controller behavior, caused by bad signal integrity
948 * or ground reference problems, can lead to isr_setup_status_phase
949 * being called with ci->status equal to NULL.
950 * If this situation occurs, you should review your USB hardware design.
951 */
952 if (WARN_ON_ONCE(!ci->status))
953 return -EPIPE;
954
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300955 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800956 ci->status->context = ci;
957 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800958
Gustavo A. R. Silva734c58a2017-07-09 21:08:20 -0500959 return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800960}
961
962/**
963 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300964 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800965 *
966 * This function returns an error code
967 * Caller must hold lock
968 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300969static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300970__releases(hwep->lock)
971__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800972{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300973 struct ci_hw_req *hwreq, *hwreqtemp;
974 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300975 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800976
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300977 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530978 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300979 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530980 if (retval < 0)
981 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300982 list_del_init(&hwreq->queue);
983 if (hwreq->req.complete != NULL) {
984 spin_unlock(hwep->lock);
985 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
986 hwreq->req.length)
987 hweptemp = hwep->ci->ep0in;
Michal Sojka304f7e52014-09-24 22:43:19 +0200988 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300989 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530990 }
991 }
David Lopoaa69a802008-11-17 14:14:51 -0800992
Pavankumar Kondetief907482011-05-02 11:56:27 +0530993 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530994 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800995
David Lopoaa69a802008-11-17 14:14:51 -0800996 return retval;
997}
998
Li Jund20f78072015-03-08 16:05:01 +0800999static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
1000{
1001 dev_warn(&ci->gadget.dev,
1002 "connect the device to an alternate port if you want HNP\n");
1003 return isr_setup_status_phase(ci);
1004}
1005
David Lopoaa69a802008-11-17 14:14:51 -08001006/**
Peter Chend7b00e32014-03-11 13:47:37 +08001007 * isr_setup_packet_handler: setup packet handler
1008 * @ci: UDC descriptor
1009 *
1010 * This function handles setup packet
1011 */
1012static void isr_setup_packet_handler(struct ci_hdrc *ci)
1013__releases(ci->lock)
1014__acquires(ci->lock)
1015{
1016 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1017 struct usb_ctrlrequest req;
1018 int type, num, dir, err = -EINVAL;
1019 u8 tmode = 0;
1020
1021 /*
1022 * Flush data and handshake transactions of previous
1023 * setup packet.
1024 */
1025 _ep_nuke(ci->ep0out);
1026 _ep_nuke(ci->ep0in);
1027
1028 /* read_setup_packet */
1029 do {
1030 hw_test_and_set_setup_guard(ci);
1031 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1032 } while (!hw_test_and_clear_setup_guard(ci));
1033
1034 type = req.bRequestType;
1035
1036 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1037
1038 switch (req.bRequest) {
1039 case USB_REQ_CLEAR_FEATURE:
1040 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1041 le16_to_cpu(req.wValue) ==
1042 USB_ENDPOINT_HALT) {
1043 if (req.wLength != 0)
1044 break;
1045 num = le16_to_cpu(req.wIndex);
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +00001046 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
Peter Chend7b00e32014-03-11 13:47:37 +08001047 num &= USB_ENDPOINT_NUMBER_MASK;
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +00001048 if (dir == TX)
Peter Chend7b00e32014-03-11 13:47:37 +08001049 num += ci->hw_ep_max / 2;
1050 if (!ci->ci_hw_ep[num].wedge) {
1051 spin_unlock(&ci->lock);
1052 err = usb_ep_clear_halt(
1053 &ci->ci_hw_ep[num].ep);
1054 spin_lock(&ci->lock);
1055 if (err)
1056 break;
1057 }
1058 err = isr_setup_status_phase(ci);
1059 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1060 le16_to_cpu(req.wValue) ==
1061 USB_DEVICE_REMOTE_WAKEUP) {
1062 if (req.wLength != 0)
1063 break;
1064 ci->remote_wakeup = 0;
1065 err = isr_setup_status_phase(ci);
1066 } else {
1067 goto delegate;
1068 }
1069 break;
1070 case USB_REQ_GET_STATUS:
Li Jund6da40a2016-02-19 10:04:43 +08001071 if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) ||
1072 le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) &&
Peter Chend7b00e32014-03-11 13:47:37 +08001073 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1074 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1075 goto delegate;
1076 if (le16_to_cpu(req.wLength) != 2 ||
1077 le16_to_cpu(req.wValue) != 0)
1078 break;
1079 err = isr_get_status_response(ci, &req);
1080 break;
1081 case USB_REQ_SET_ADDRESS:
1082 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1083 goto delegate;
1084 if (le16_to_cpu(req.wLength) != 0 ||
1085 le16_to_cpu(req.wIndex) != 0)
1086 break;
1087 ci->address = (u8)le16_to_cpu(req.wValue);
1088 ci->setaddr = true;
1089 err = isr_setup_status_phase(ci);
1090 break;
1091 case USB_REQ_SET_FEATURE:
1092 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1093 le16_to_cpu(req.wValue) ==
1094 USB_ENDPOINT_HALT) {
1095 if (req.wLength != 0)
1096 break;
1097 num = le16_to_cpu(req.wIndex);
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +00001098 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
Peter Chend7b00e32014-03-11 13:47:37 +08001099 num &= USB_ENDPOINT_NUMBER_MASK;
Stefan Wahrenc6ee9f22016-08-11 17:19:13 +00001100 if (dir == TX)
Peter Chend7b00e32014-03-11 13:47:37 +08001101 num += ci->hw_ep_max / 2;
1102
1103 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001104 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
Peter Chend7b00e32014-03-11 13:47:37 +08001105 spin_lock(&ci->lock);
1106 if (!err)
1107 isr_setup_status_phase(ci);
1108 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1109 if (req.wLength != 0)
1110 break;
1111 switch (le16_to_cpu(req.wValue)) {
1112 case USB_DEVICE_REMOTE_WAKEUP:
1113 ci->remote_wakeup = 1;
1114 err = isr_setup_status_phase(ci);
1115 break;
1116 case USB_DEVICE_TEST_MODE:
1117 tmode = le16_to_cpu(req.wIndex) >> 8;
1118 switch (tmode) {
1119 case TEST_J:
1120 case TEST_K:
1121 case TEST_SE0_NAK:
1122 case TEST_PACKET:
1123 case TEST_FORCE_EN:
1124 ci->test_mode = tmode;
1125 err = isr_setup_status_phase(
1126 ci);
1127 break;
1128 default:
1129 break;
1130 }
Li Jun95f55552014-04-23 15:56:47 +08001131 break;
1132 case USB_DEVICE_B_HNP_ENABLE:
1133 if (ci_otg_is_fsm_mode(ci)) {
1134 ci->gadget.b_hnp_enable = 1;
1135 err = isr_setup_status_phase(
1136 ci);
1137 }
1138 break;
Li Jund20f78072015-03-08 16:05:01 +08001139 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1140 if (ci_otg_is_fsm_mode(ci))
1141 err = otg_a_alt_hnp_support(ci);
1142 break;
Peter Chen3520d462015-07-17 08:44:24 +08001143 case USB_DEVICE_A_HNP_SUPPORT:
1144 if (ci_otg_is_fsm_mode(ci)) {
1145 ci->gadget.a_hnp_support = 1;
1146 err = isr_setup_status_phase(
1147 ci);
1148 }
1149 break;
Peter Chend7b00e32014-03-11 13:47:37 +08001150 default:
1151 goto delegate;
1152 }
1153 } else {
1154 goto delegate;
1155 }
1156 break;
1157 default:
1158delegate:
1159 if (req.wLength == 0) /* no data phase */
1160 ci->ep0_dir = TX;
1161
1162 spin_unlock(&ci->lock);
1163 err = ci->driver->setup(&ci->gadget, &req);
1164 spin_lock(&ci->lock);
1165 break;
1166 }
1167
1168 if (err < 0) {
1169 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001170 if (_ep_set_halt(&hwep->ep, 1, false))
1171 dev_err(ci->dev, "error: _ep_set_halt\n");
Peter Chend7b00e32014-03-11 13:47:37 +08001172 spin_lock(&ci->lock);
1173 }
1174}
1175
1176/**
David Lopoaa69a802008-11-17 14:14:51 -08001177 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +08001178 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -08001179 *
1180 * This function handles traffic events
1181 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001182static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +08001183__releases(ci->lock)
1184__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -08001185{
1186 unsigned i;
Peter Chend7b00e32014-03-11 13:47:37 +08001187 int err;
David Lopoaa69a802008-11-17 14:14:51 -08001188
Richard Zhao26c696c2012-07-07 22:56:40 +08001189 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001190 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
David Lopoaa69a802008-11-17 14:14:51 -08001191
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001192 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001193 continue; /* not configured */
1194
Richard Zhao26c696c2012-07-07 22:56:40 +08001195 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001196 err = isr_tr_complete_low(hwep);
1197 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -08001198 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001199 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001200 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001201 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001202 if (_ep_set_halt(&hwep->ep, 1, false))
Richard Zhao26c696c2012-07-07 22:56:40 +08001203 dev_err(ci->dev,
Peter Chen56ffa1d2015-08-24 14:10:07 +08001204 "error: _ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +08001205 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001206 }
1207 }
1208 }
1209
Peter Chen64fc06c2014-02-19 13:41:41 +08001210 /* Only handle setup packet below */
Peter Chend7b00e32014-03-11 13:47:37 +08001211 if (i == 0 &&
1212 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1213 isr_setup_packet_handler(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001214 }
1215}
1216
1217/******************************************************************************
1218 * ENDPT block
1219 *****************************************************************************/
1220/**
1221 * ep_enable: configure endpoint, making it usable
1222 *
1223 * Check usb_ep_enable() at "usb_gadget.h" for details
1224 */
1225static int ep_enable(struct usb_ep *ep,
1226 const struct usb_endpoint_descriptor *desc)
1227{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001228 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301229 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001230 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001231 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001232
David Lopoaa69a802008-11-17 14:14:51 -08001233 if (ep == NULL || desc == NULL)
1234 return -EINVAL;
1235
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001236 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001237
1238 /* only internal SW should enable ctrl endpts */
1239
Peter Chend5d1e1b2015-02-11 12:44:41 +08001240 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001241 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
Peter Chend5d1e1b2015-02-11 12:44:41 +08001242 spin_unlock_irqrestore(hwep->lock, flags);
1243 return -EBUSY;
1244 }
1245
1246 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001247
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001248 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1249 hwep->num = usb_endpoint_num(desc);
1250 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001251
Felipe Balbi63b9e902016-09-28 14:17:38 +03001252 hwep->ep.maxpacket = usb_endpoint_maxp(desc);
Felipe Balbia98e25e72016-09-28 13:26:18 +03001253 hwep->ep.mult = usb_endpoint_maxp_mult(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001254
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001255 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001256 cap |= QH_IOS;
Abbas Raza953c6642014-07-17 19:34:31 +08001257
1258 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001259 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
Peter Chen2fc5a7d2014-01-10 13:51:32 +08001260 /*
1261 * For ISO-TX, we set mult at QH as the largest value, and use
1262 * MultO at TD as real mult value.
1263 */
1264 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1265 cap |= 3 << __ffs(QH_MULT);
David Lopoaa69a802008-11-17 14:14:51 -08001266
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001267 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001268
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001269 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001270
Peter Chen64fc06c2014-02-19 13:41:41 +08001271 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1272 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1273 retval = -EINVAL;
1274 }
1275
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301276 /*
1277 * Enable endpoints in the HW other than ep0 as ep0
1278 * is always enabled
1279 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001280 if (hwep->num)
1281 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1282 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001283
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001284 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001285 return retval;
1286}
1287
1288/**
1289 * ep_disable: endpoint is no longer usable
1290 *
1291 * Check usb_ep_disable() at "usb_gadget.h" for details
1292 */
1293static int ep_disable(struct usb_ep *ep)
1294{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001295 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001296 int direction, retval = 0;
1297 unsigned long flags;
1298
David Lopoaa69a802008-11-17 14:14:51 -08001299 if (ep == NULL)
1300 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001301 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001302 return -EBUSY;
1303
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001304 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001305
1306 /* only internal SW should disable ctrl endpts */
1307
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001308 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001309 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001310 retval |= _ep_nuke(hwep);
1311 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001312
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001313 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1314 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001315
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001316 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001317
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001318 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001319
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001320 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001321 return retval;
1322}
1323
1324/**
1325 * ep_alloc_request: allocate a request object to use with this endpoint
1326 *
1327 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1328 */
1329static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1330{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001331 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001332
Alexander Shishkin0f089092012-05-08 23:29:02 +03001333 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001334 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001335
Alexander Shishkin8e229782013-06-24 14:46:36 +03001336 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001337 if (hwreq != NULL) {
1338 INIT_LIST_HEAD(&hwreq->queue);
1339 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001340 }
1341
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001342 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001343}
1344
1345/**
1346 * ep_free_request: frees a request object
1347 *
1348 * Check usb_ep_free_request() at "usb_gadget.h" for details
1349 */
1350static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1351{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001352 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1353 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001354 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001355 unsigned long flags;
1356
David Lopoaa69a802008-11-17 14:14:51 -08001357 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001358 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001359 } else if (!list_empty(&hwreq->queue)) {
1360 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001361 return;
1362 }
1363
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001364 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001365
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001366 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1367 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001368 list_del_init(&node->td);
1369 node->ptr = NULL;
1370 kfree(node);
1371 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001372
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001373 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001374
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001375 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001376}
1377
1378/**
1379 * ep_queue: queues (submits) an I/O request to an endpoint
1380 *
1381 * Check usb_ep_queue()* at usb_gadget.h" for details
1382 */
1383static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1384 gfp_t __maybe_unused gfp_flags)
1385{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001386 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001387 int retval = 0;
1388 unsigned long flags;
1389
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001390 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001391 return -EINVAL;
1392
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001393 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001394 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001395 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001396 return retval;
1397}
1398
1399/**
1400 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1401 *
1402 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1403 */
1404static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1405{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001406 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1407 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001408 unsigned long flags;
Peter Chene4adcff2014-07-02 12:16:31 +08001409 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001410
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001411 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1412 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1413 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001414 return -EINVAL;
1415
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001416 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001417
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001418 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001419
Peter Chene4adcff2014-07-02 12:16:31 +08001420 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1421 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1422 list_del(&node->td);
1423 kfree(node);
1424 }
1425
David Lopoaa69a802008-11-17 14:14:51 -08001426 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001427 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001428
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001429 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001430
David Lopoaa69a802008-11-17 14:14:51 -08001431 req->status = -ECONNRESET;
1432
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001433 if (hwreq->req.complete != NULL) {
1434 spin_unlock(hwep->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001435 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001436 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001437 }
1438
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001439 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001440 return 0;
1441}
1442
1443/**
1444 * ep_set_halt: sets the endpoint halt feature
1445 *
1446 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1447 */
1448static int ep_set_halt(struct usb_ep *ep, int value)
1449{
Peter Chen56ffa1d2015-08-24 14:10:07 +08001450 return _ep_set_halt(ep, value, true);
David Lopoaa69a802008-11-17 14:14:51 -08001451}
1452
1453/**
1454 * ep_set_wedge: sets the halt feature and ignores clear requests
1455 *
1456 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1457 */
1458static int ep_set_wedge(struct usb_ep *ep)
1459{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001460 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001461 unsigned long flags;
1462
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001463 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001464 return -EINVAL;
1465
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001466 spin_lock_irqsave(hwep->lock, flags);
1467 hwep->wedge = 1;
1468 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001469
1470 return usb_ep_set_halt(ep);
1471}
1472
1473/**
1474 * ep_fifo_flush: flushes contents of a fifo
1475 *
1476 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1477 */
1478static void ep_fifo_flush(struct usb_ep *ep)
1479{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001480 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001481 unsigned long flags;
1482
David Lopoaa69a802008-11-17 14:14:51 -08001483 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001484 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001485 return;
1486 }
1487
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001488 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001489
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001490 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001491
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001492 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001493}
1494
1495/**
1496 * Endpoint-specific part of the API to the USB controller hardware
1497 * Check "usb_gadget.h" for details
1498 */
1499static const struct usb_ep_ops usb_ep_ops = {
1500 .enable = ep_enable,
1501 .disable = ep_disable,
1502 .alloc_request = ep_alloc_request,
1503 .free_request = ep_free_request,
1504 .queue = ep_queue,
1505 .dequeue = ep_dequeue,
1506 .set_halt = ep_set_halt,
1507 .set_wedge = ep_set_wedge,
1508 .fifo_flush = ep_fifo_flush,
1509};
1510
1511/******************************************************************************
1512 * GADGET block
1513 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001514static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301515{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001516 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301517 unsigned long flags;
1518 int gadget_ready = 0;
1519
Richard Zhao26c696c2012-07-07 22:56:40 +08001520 spin_lock_irqsave(&ci->lock, flags);
1521 ci->vbus_active = is_active;
1522 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301523 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001524 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301525
Li Junfc5b9202017-09-04 23:14:01 +08001526 if (ci->usb_phy)
1527 usb_phy_set_charger_state(ci->usb_phy, is_active ?
1528 USB_CHARGER_PRESENT : USB_CHARGER_ABSENT);
1529
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301530 if (gadget_ready) {
1531 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301532 pm_runtime_get_sync(&_gadget->dev);
Peter Chen5b157302014-11-26 13:44:33 +08001533 hw_device_reset(ci);
Richard Zhao26c696c2012-07-07 22:56:40 +08001534 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen10775eb2014-05-04 09:24:44 +08001535 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
Peter Chen467a78c2015-03-06 10:36:04 +08001536 usb_udc_vbus_handler(_gadget, true);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301537 } else {
Peter Chen467a78c2015-03-06 10:36:04 +08001538 usb_udc_vbus_handler(_gadget, false);
Peter Chen92b336d2013-09-17 12:37:19 +08001539 if (ci->driver)
1540 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001541 hw_device_state(ci, 0);
1542 if (ci->platdata->notify_event)
1543 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001544 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001545 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301546 pm_runtime_put_sync(&_gadget->dev);
Peter Chen10775eb2014-05-04 09:24:44 +08001547 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301548 }
1549 }
1550
1551 return 0;
1552}
1553
Alexander Shishkin8e229782013-06-24 14:46:36 +03001554static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301555{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001556 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301557 unsigned long flags;
1558 int ret = 0;
1559
Richard Zhao26c696c2012-07-07 22:56:40 +08001560 spin_lock_irqsave(&ci->lock, flags);
1561 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301562 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301563 goto out;
1564 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001565 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301566 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301567 goto out;
1568 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001569 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301570out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001571 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301572 return ret;
1573}
1574
Alexander Shishkin8e229782013-06-24 14:46:36 +03001575static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301576{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001577 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301578
Antoine Tenartef44cb42014-10-30 18:41:16 +01001579 if (ci->usb_phy)
1580 return usb_phy_set_power(ci->usb_phy, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301581 return -ENOTSUPP;
1582}
1583
Peter Chen1009f9a2015-01-28 16:32:25 +08001584static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1585{
1586 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1587 struct ci_hw_ep *hwep = ci->ep0in;
1588 unsigned long flags;
1589
1590 spin_lock_irqsave(hwep->lock, flags);
1591 _gadget->is_selfpowered = (is_on != 0);
1592 spin_unlock_irqrestore(hwep->lock, flags);
1593
1594 return 0;
1595}
1596
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001597/* Change Data+ pullup status
1598 * this func is used by usb_gadget_connect/disconnet
1599 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001600static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001601{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001602 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001603
Li Junc4e94172016-08-16 19:19:11 +08001604 /*
1605 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
1606 * and don't touch Data+ in host mode for dual role config.
1607 */
1608 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
Li Jun9b6567e2015-03-23 16:03:35 +08001609 return 0;
1610
Peter Chen467a78c2015-03-06 10:36:04 +08001611 pm_runtime_get_sync(&ci->gadget.dev);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001612 if (is_on)
1613 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1614 else
1615 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
Peter Chen467a78c2015-03-06 10:36:04 +08001616 pm_runtime_put_sync(&ci->gadget.dev);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001617
1618 return 0;
1619}
1620
Alexander Shishkin8e229782013-06-24 14:46:36 +03001621static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001622 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001623static int ci_udc_stop(struct usb_gadget *gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001624/**
1625 * Device operations part of the API to the USB controller hardware,
1626 * which don't involve endpoints (or i/o)
1627 * Check "usb_gadget.h" for details
1628 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301629static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001630 .vbus_session = ci_udc_vbus_session,
1631 .wakeup = ci_udc_wakeup,
Peter Chen1009f9a2015-01-28 16:32:25 +08001632 .set_selfpowered = ci_udc_selfpowered,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001633 .pullup = ci_udc_pullup,
1634 .vbus_draw = ci_udc_vbus_draw,
1635 .udc_start = ci_udc_start,
1636 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301637};
David Lopoaa69a802008-11-17 14:14:51 -08001638
Alexander Shishkin8e229782013-06-24 14:46:36 +03001639static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001640{
1641 int retval = 0, i, j;
1642
Richard Zhao26c696c2012-07-07 22:56:40 +08001643 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001644 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001645 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001646 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001647
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001648 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001649 (j == TX) ? "in" : "out");
1650
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001651 hwep->ci = ci;
1652 hwep->lock = &ci->lock;
1653 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001654
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001655 hwep->ep.name = hwep->name;
1656 hwep->ep.ops = &usb_ep_ops;
Robert Baldygaa7e3f142015-07-31 16:00:17 +02001657
1658 if (i == 0) {
1659 hwep->ep.caps.type_control = true;
1660 } else {
1661 hwep->ep.caps.type_iso = true;
1662 hwep->ep.caps.type_bulk = true;
1663 hwep->ep.caps.type_int = true;
1664 }
1665
1666 if (j == TX)
1667 hwep->ep.caps.dir_in = true;
1668 else
1669 hwep->ep.caps.dir_out = true;
1670
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001671 /*
1672 * for ep0: maxP defined in desc, for other
1673 * eps, maxP is set by epautoconfig() called
1674 * by gadget layer
1675 */
Robert Baldygae117e742013-12-13 12:23:38 +01001676 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001677
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001678 INIT_LIST_HEAD(&hwep->qh.queue);
Fabio Estevam382c1b32016-09-08 09:34:30 -03001679 hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL,
1680 &hwep->qh.dma);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001681 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001682 retval = -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001683
1684 /*
1685 * set up shorthands for ep0 out and in endpoints,
1686 * don't add to gadget's ep_list
1687 */
1688 if (i == 0) {
1689 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001690 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001691 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001692 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001693
Robert Baldygae117e742013-12-13 12:23:38 +01001694 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001695 continue;
1696 }
1697
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001698 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001699 }
1700
1701 return retval;
1702}
1703
Alexander Shishkin8e229782013-06-24 14:46:36 +03001704static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001705{
1706 int i;
1707
1708 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001709 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001710
Peter Chen4a295672013-09-10 15:34:39 +08001711 if (hwep->pending_td)
1712 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001713 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001714 }
1715}
1716
David Lopoaa69a802008-11-17 14:14:51 -08001717/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001718 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001719 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001720 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001721 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001722 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001723 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001724static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001725 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001726{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001727 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001728 int retval = -ENOMEM;
1729
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001730 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001731 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001732
David Lopoaa69a802008-11-17 14:14:51 -08001733
Richard Zhao26c696c2012-07-07 22:56:40 +08001734 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1735 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301736 if (retval)
1737 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001738
Richard Zhao26c696c2012-07-07 22:56:40 +08001739 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1740 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301741 if (retval)
1742 return retval;
David Lopoaa69a802008-11-17 14:14:51 -08001743
Richard Zhao26c696c2012-07-07 22:56:40 +08001744 ci->driver = driver;
Li Jun4dcf7202014-04-23 15:56:50 +08001745
1746 /* Start otg fsm for B-device */
1747 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1748 ci_hdrc_otg_fsm_start(ci);
1749 return retval;
1750 }
1751
Richard Zhao26c696c2012-07-07 22:56:40 +08001752 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001753 if (ci->vbus_active) {
Peter Chen5b157302014-11-26 13:44:33 +08001754 hw_device_reset(ci);
Peter Chend268e9b2013-08-14 12:44:14 +03001755 } else {
Peter Chen467a78c2015-03-06 10:36:04 +08001756 usb_udc_vbus_handler(&ci->gadget, false);
Peter Chend268e9b2013-08-14 12:44:14 +03001757 pm_runtime_put_sync(&ci->gadget.dev);
Peter Chen65b2fb32013-10-08 10:30:17 +08001758 return retval;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301759 }
1760
Richard Zhao26c696c2012-07-07 22:56:40 +08001761 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301762 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001763 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001764
David Lopoaa69a802008-11-17 14:14:51 -08001765 return retval;
1766}
David Lopoaa69a802008-11-17 14:14:51 -08001767
Li Jun85da8522014-12-12 09:11:42 +08001768static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1769{
1770 if (!ci_otg_is_fsm_mode(ci))
1771 return;
1772
1773 mutex_lock(&ci->fsm.lock);
1774 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1775 ci->fsm.a_bidl_adis_tmout = 1;
1776 ci_hdrc_otg_fsm_start(ci);
1777 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1778 ci->fsm.protocol = PROTO_UNDEF;
1779 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1780 }
1781 mutex_unlock(&ci->fsm.lock);
1782}
1783
David Lopoaa69a802008-11-17 14:14:51 -08001784/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001785 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001786 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001787static int ci_udc_stop(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -08001788{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001789 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001790 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001791
Richard Zhao26c696c2012-07-07 22:56:40 +08001792 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001793
Peter Chend268e9b2013-08-14 12:44:14 +03001794 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001795 hw_device_state(ci, 0);
Stephen Boydafff6062016-12-28 14:57:08 -08001796 spin_unlock_irqrestore(&ci->lock, flags);
Richard Zhao26c696c2012-07-07 22:56:40 +08001797 if (ci->platdata->notify_event)
1798 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001799 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001800 _gadget_stop_activity(&ci->gadget);
1801 spin_lock_irqsave(&ci->lock, flags);
1802 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301803 }
David Lopoaa69a802008-11-17 14:14:51 -08001804
Peter Chenf84839d2013-09-17 12:37:20 +08001805 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001806 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001807
Li Jun85da8522014-12-12 09:11:42 +08001808 ci_udc_stop_for_otg_fsm(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001809 return 0;
1810}
David Lopoaa69a802008-11-17 14:14:51 -08001811
1812/******************************************************************************
1813 * BUS block
1814 *****************************************************************************/
1815/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001816 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001817 *
1818 * This function returns IRQ_HANDLED if the IRQ has been handled
1819 * It locks access to registers
1820 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001821static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001822{
David Lopoaa69a802008-11-17 14:14:51 -08001823 irqreturn_t retval;
1824 u32 intr;
1825
Richard Zhao26c696c2012-07-07 22:56:40 +08001826 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001827 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001828
Richard Zhao26c696c2012-07-07 22:56:40 +08001829 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301830
Alexander Shishkin8e229782013-06-24 14:46:36 +03001831 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001832 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001833 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001834 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301835 return IRQ_NONE;
1836 }
1837 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001838 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001839
Alexander Shishkine443b332012-05-11 17:25:46 +03001840 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001841 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001842 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001843 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001844
David Lopoaa69a802008-11-17 14:14:51 -08001845 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001846 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001847 USB_SPEED_HIGH : USB_SPEED_FULL;
Li Jun4f4555c2017-03-07 10:35:01 +08001848 if (ci->suspended) {
1849 if (ci->driver->resume) {
1850 spin_unlock(&ci->lock);
1851 ci->driver->resume(&ci->gadget);
1852 spin_lock(&ci->lock);
1853 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001854 ci->suspended = 0;
Li Jun4f4555c2017-03-07 10:35:01 +08001855 usb_gadget_set_state(&ci->gadget,
1856 ci->resume_state);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301857 }
David Lopoaa69a802008-11-17 14:14:51 -08001858 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001859
1860 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001861 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001862
Li Jun4f4555c2017-03-07 10:35:01 +08001863 if ((USBi_SLI & intr) && !(ci->suspended)) {
1864 ci->suspended = 1;
1865 ci->resume_state = ci->gadget.state;
Richard Zhao26c696c2012-07-07 22:56:40 +08001866 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1867 ci->driver->suspend) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001868 spin_unlock(&ci->lock);
1869 ci->driver->suspend(&ci->gadget);
1870 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301871 }
Li Jun4f4555c2017-03-07 10:35:01 +08001872 usb_gadget_set_state(&ci->gadget,
1873 USB_STATE_SUSPENDED);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301874 }
David Lopoaa69a802008-11-17 14:14:51 -08001875 retval = IRQ_HANDLED;
1876 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001877 retval = IRQ_NONE;
1878 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001879 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001880
1881 return retval;
1882}
1883
1884/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001885 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001886 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001887 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001888static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001889{
Richard Zhao26c696c2012-07-07 22:56:40 +08001890 struct device *dev = ci->dev;
Li Jun79742352015-07-09 15:18:45 +08001891 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
David Lopoaa69a802008-11-17 14:14:51 -08001892 int retval = 0;
1893
Richard Zhao26c696c2012-07-07 22:56:40 +08001894 ci->gadget.ops = &usb_gadget_ops;
1895 ci->gadget.speed = USB_SPEED_UNKNOWN;
1896 ci->gadget.max_speed = USB_SPEED_HIGH;
Richard Zhao26c696c2012-07-07 22:56:40 +08001897 ci->gadget.name = ci->platdata->name;
Li Jun79742352015-07-09 15:18:45 +08001898 ci->gadget.otg_caps = otg_caps;
1899
Dmitry Osipenko581821a2017-08-16 13:32:39 +03001900 if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA)
1901 ci->gadget.quirk_avoids_skb_reserve = 1;
1902
Li Jun3f217e92015-07-31 10:41:00 +08001903 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
1904 otg_caps->adp_support))
Li Jun79742352015-07-09 15:18:45 +08001905 ci->gadget.is_otg = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001906
Richard Zhao26c696c2012-07-07 22:56:40 +08001907 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001908
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001909 /* alloc resources */
Arnd Bergmannaeb78cd2017-03-13 10:18:42 +08001910 ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001911 sizeof(struct ci_hw_qh),
1912 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001913 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001914 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001915
Arnd Bergmannaeb78cd2017-03-13 10:18:42 +08001916 ci->td_pool = dma_pool_create("ci_hw_td", dev->parent,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001917 sizeof(struct ci_hw_td),
1918 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001919 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001920 retval = -ENOMEM;
1921 goto free_qh_pool;
1922 }
1923
Richard Zhao26c696c2012-07-07 22:56:40 +08001924 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001925 if (retval)
1926 goto free_pools;
1927
Richard Zhao26c696c2012-07-07 22:56:40 +08001928 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301929
Richard Zhao26c696c2012-07-07 22:56:40 +08001930 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001931 if (retval)
Peter Chen74475ed2013-09-24 12:47:53 +08001932 goto destroy_eps;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001933
Richard Zhao26c696c2012-07-07 22:56:40 +08001934 pm_runtime_no_callbacks(&ci->gadget.dev);
1935 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001936
David Lopoaa69a802008-11-17 14:14:51 -08001937 return retval;
1938
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001939destroy_eps:
1940 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001941free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001942 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001943free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001944 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001945 return retval;
1946}
1947
1948/**
Peter Chen3f124d22013-08-14 12:44:07 +03001949 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001950 *
1951 * No interrupts active, the IRQ has been released
1952 */
Peter Chen3f124d22013-08-14 12:44:07 +03001953void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001954{
Peter Chen3f124d22013-08-14 12:44:07 +03001955 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001956 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001957
Richard Zhao26c696c2012-07-07 22:56:40 +08001958 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001959
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001960 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001961
Richard Zhao26c696c2012-07-07 22:56:40 +08001962 dma_pool_destroy(ci->td_pool);
1963 dma_pool_destroy(ci->qh_pool);
Peter Chen3f124d22013-08-14 12:44:07 +03001964}
1965
1966static int udc_id_switch_for_device(struct ci_hdrc *ci)
1967{
Li Jun0c33bf72014-04-23 15:56:38 +08001968 if (ci->is_otg)
1969 /* Clear and enable BSV irq */
1970 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
1971 OTGSC_BSVIS | OTGSC_BSVIE);
Peter Chen3f124d22013-08-14 12:44:07 +03001972
1973 return 0;
1974}
1975
1976static void udc_id_switch_for_host(struct ci_hdrc *ci)
1977{
Li Jun0c33bf72014-04-23 15:56:38 +08001978 /*
1979 * host doesn't care B_SESSION_VALID event
1980 * so clear and disbale BSV irq
1981 */
1982 if (ci->is_otg)
1983 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
Peter Chena932a802017-03-27 10:54:27 +08001984
1985 ci->vbus_active = 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001986}
David Lopoaa69a802008-11-17 14:14:51 -08001987
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001988/**
1989 * ci_hdrc_gadget_init - initialize device related bits
1990 * ci: the controller
1991 *
Peter Chen3f124d22013-08-14 12:44:07 +03001992 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001993 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001994int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001995{
1996 struct ci_role_driver *rdrv;
Jisheng Zhangaa1f0582017-04-24 12:35:51 +00001997 int ret;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001998
1999 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
2000 return -ENXIO;
2001
Fabio Estevame74e8372016-09-08 09:34:32 -03002002 rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03002003 if (!rdrv)
2004 return -ENOMEM;
2005
Peter Chen3f124d22013-08-14 12:44:07 +03002006 rdrv->start = udc_id_switch_for_device;
2007 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03002008 rdrv->irq = udc_irq;
2009 rdrv->name = "gadget";
Alexander Shishkin5f36e232012-05-11 17:25:47 +03002010
Jisheng Zhangaa1f0582017-04-24 12:35:51 +00002011 ret = udc_start(ci);
2012 if (!ret)
2013 ci->roles[CI_ROLE_GADGET] = rdrv;
2014
2015 return ret;
David Lopoaa69a802008-11-17 14:14:51 -08002016}