blob: b292b454c77ba8862b1e23c6472ea0ebf24f2b96 [file] [log] [blame]
David Lopoaa69a802008-11-17 14:14:51 -08001/*
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03002 * udc.c - ChipIdea UDC driver
David Lopoaa69a802008-11-17 14:14:51 -08003 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020013#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080014#include <linux/device.h>
15#include <linux/dmapool.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053016#include <linux/err.h>
Alexander Shishkin5b083192013-03-30 02:46:18 +020017#include <linux/irqreturn.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053020#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Li Jun95f55552014-04-23 15:56:47 +080023#include <linux/usb/otg-fsm.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030024#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080025
Alexander Shishkine443b332012-05-11 17:25:46 +030026#include "ci.h"
27#include "udc.h"
28#include "bits.h"
29#include "debug.h"
Peter Chen3f124d22013-08-14 12:44:07 +030030#include "otg.h"
Li Jun4dcf7202014-04-23 15:56:50 +080031#include "otg_fsm.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020032
David Lopoaa69a802008-11-17 14:14:51 -080033/* control endpoint description */
34static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053035ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080036 .bLength = USB_DT_ENDPOINT_SIZE,
37 .bDescriptorType = USB_DT_ENDPOINT,
38
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053039 .bEndpointAddress = USB_DIR_OUT,
40 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
41 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
42};
43
44static const struct usb_endpoint_descriptor
45ctrl_endpt_in_desc = {
46 .bLength = USB_DT_ENDPOINT_SIZE,
47 .bDescriptorType = USB_DT_ENDPOINT,
48
49 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080050 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
51 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
52};
53
David Lopoaa69a802008-11-17 14:14:51 -080054/**
55 * hw_ep_bit: calculates the bit number
56 * @num: endpoint number
57 * @dir: endpoint direction
58 *
59 * This function returns bit number
60 */
61static inline int hw_ep_bit(int num, int dir)
62{
63 return num + (dir ? 16 : 0);
64}
65
Alexander Shishkin8e229782013-06-24 14:46:36 +030066static inline int ep_to_bit(struct ci_hdrc *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020067{
Richard Zhao26c696c2012-07-07 22:56:40 +080068 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020069
Richard Zhao26c696c2012-07-07 22:56:40 +080070 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020071 n += fill;
72
73 return n;
74}
75
David Lopoaa69a802008-11-17 14:14:51 -080076/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030077 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080078 * @dma: 0 => disable, !0 => enable and set dma engine
79 *
80 * This function returns an error code
81 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030082static int hw_device_state(struct ci_hdrc *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080083{
84 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080085 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080086 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080087 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080088 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
David Lopoaa69a802008-11-17 14:14:51 -080089 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080090 hw_write(ci, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080091 }
92 return 0;
93}
94
95/**
96 * hw_ep_flush: flush endpoint fifo (execute without interruption)
97 * @num: endpoint number
98 * @dir: endpoint direction
99 *
100 * This function returns an error code
101 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300102static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800103{
104 int n = hw_ep_bit(num, dir);
105
106 do {
107 /* flush any pending transfer */
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800108 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
Richard Zhao26c696c2012-07-07 22:56:40 +0800109 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800110 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800111 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800112
113 return 0;
114}
115
116/**
117 * hw_ep_disable: disables endpoint (execute without interruption)
118 * @num: endpoint number
119 * @dir: endpoint direction
120 *
121 * This function returns an error code
122 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300123static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800124{
Richard Zhao26c696c2012-07-07 22:56:40 +0800125 hw_ep_flush(ci, num, dir);
126 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300127 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800128 return 0;
129}
130
131/**
132 * hw_ep_enable: enables endpoint (execute without interruption)
133 * @num: endpoint number
134 * @dir: endpoint direction
135 * @type: endpoint type
136 *
137 * This function returns an error code
138 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300139static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800140{
141 u32 mask, data;
142
143 if (dir) {
144 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200145 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800146
147 mask |= ENDPTCTRL_TXS; /* unstall */
148 mask |= ENDPTCTRL_TXR; /* reset data toggle */
149 data |= ENDPTCTRL_TXR;
150 mask |= ENDPTCTRL_TXE; /* enable */
151 data |= ENDPTCTRL_TXE;
152 } else {
153 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200154 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800155
156 mask |= ENDPTCTRL_RXS; /* unstall */
157 mask |= ENDPTCTRL_RXR; /* reset data toggle */
158 data |= ENDPTCTRL_RXR;
159 mask |= ENDPTCTRL_RXE; /* enable */
160 data |= ENDPTCTRL_RXE;
161 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800162 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800163 return 0;
164}
165
166/**
167 * hw_ep_get_halt: return endpoint halt status
168 * @num: endpoint number
169 * @dir: endpoint direction
170 *
171 * This function returns 1 if endpoint halted
172 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300173static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800174{
175 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
176
Richard Zhao26c696c2012-07-07 22:56:40 +0800177 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800178}
179
180/**
David Lopoaa69a802008-11-17 14:14:51 -0800181 * hw_ep_prime: primes endpoint (execute without interruption)
182 * @num: endpoint number
183 * @dir: endpoint direction
184 * @is_ctrl: true if control endpoint
185 *
186 * This function returns an error code
187 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300188static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800189{
190 int n = hw_ep_bit(num, dir);
191
Richard Zhao26c696c2012-07-07 22:56:40 +0800192 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800193 return -EAGAIN;
194
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800195 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800196
Richard Zhao26c696c2012-07-07 22:56:40 +0800197 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800198 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800199 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800200 return -EAGAIN;
201
202 /* status shoult be tested according with manual but it doesn't work */
203 return 0;
204}
205
206/**
207 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
208 * without interruption)
209 * @num: endpoint number
210 * @dir: endpoint direction
211 * @value: true => stall, false => unstall
212 *
213 * This function returns an error code
214 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300215static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800216{
217 if (value != 0 && value != 1)
218 return -EINVAL;
219
220 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300221 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800222 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
223 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
224
225 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800226 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300227 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800228 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800229
230 return 0;
231}
232
233/**
David Lopoaa69a802008-11-17 14:14:51 -0800234 * hw_is_port_high_speed: test if port is high speed
235 *
236 * This function returns true if high speed port
237 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300238static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800239{
Richard Zhao26c696c2012-07-07 22:56:40 +0800240 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
241 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800242}
243
244/**
David Lopoaa69a802008-11-17 14:14:51 -0800245 * hw_test_and_clear_complete: test & clear complete status (execute without
246 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200247 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800248 *
249 * This function returns complete status
250 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800252{
Richard Zhao26c696c2012-07-07 22:56:40 +0800253 n = ep_to_bit(ci, n);
254 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800255}
256
257/**
258 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
259 * without interruption)
260 *
261 * This function returns active interrutps
262 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300263static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800264{
Richard Zhao26c696c2012-07-07 22:56:40 +0800265 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800266
Richard Zhao26c696c2012-07-07 22:56:40 +0800267 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800268 return reg;
269}
270
271/**
272 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
273 * interruption)
274 *
275 * This function returns guard value
276 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300277static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800278{
Richard Zhao26c696c2012-07-07 22:56:40 +0800279 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800280}
281
282/**
283 * hw_test_and_set_setup_guard: test & set setup guard (execute without
284 * interruption)
285 *
286 * This function returns guard value
287 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300288static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800289{
Richard Zhao26c696c2012-07-07 22:56:40 +0800290 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800291}
292
293/**
294 * hw_usb_set_address: configures USB address (execute without interruption)
295 * @value: new USB address
296 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300297 * This function explicitly sets the address, without the "USBADRA" (advance)
298 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800299 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300300static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800301{
Richard Zhao26c696c2012-07-07 22:56:40 +0800302 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200303 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800304}
305
306/**
307 * hw_usb_reset: restart device after a bus reset (execute without
308 * interruption)
309 *
310 * This function returns an error code
311 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300312static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800313{
Richard Zhao26c696c2012-07-07 22:56:40 +0800314 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800315
316 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800317 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800318
319 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800320 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800321
322 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800323 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800324
325 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800326 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800327 udelay(10); /* not RTOS friendly */
328
329 /* reset all endpoints ? */
330
331 /* reset internal status and wait for further instructions
332 no need to verify the port reset status (ESS does it) */
333
334 return 0;
335}
336
337/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800338 * UTIL block
339 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300340
Alexander Shishkin8e229782013-06-24 14:46:36 +0300341static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300342 unsigned length)
343{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300344 int i;
345 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300346 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
347 GFP_ATOMIC);
348
349 if (node == NULL)
350 return -ENOMEM;
351
Saurabh Sengar84c1eeb2015-10-28 12:44:35 +0530352 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300353 &node->dma);
354 if (node->ptr == NULL) {
355 kfree(node);
356 return -ENOMEM;
357 }
358
Michael Grzeschik2e270412013-06-13 17:59:54 +0300359 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
360 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
361 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800362 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
363 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
364
365 if (hwreq->req.length == 0
366 || hwreq->req.length % hwep->ep.maxpacket)
367 mul++;
368 node->ptr->token |= mul << __ffs(TD_MULTO);
369 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300370
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300371 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300372 if (length) {
373 node->ptr->page[0] = cpu_to_le32(temp);
374 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300375 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300376 page &= ~TD_RESERVED_MASK;
377 node->ptr->page[i] = cpu_to_le32(page);
378 }
379 }
380
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300381 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300382
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300383 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300384 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300385 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300386 struct td_node, td);
387 lastnode->ptr->next = cpu_to_le32(node->dma);
388 }
389
390 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300391 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300392
393 return 0;
394}
395
David Lopoaa69a802008-11-17 14:14:51 -0800396/**
397 * _usb_addr: calculates endpoint address from direction & number
398 * @ep: endpoint
399 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300400static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800401{
402 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
403}
404
405/**
406 * _hardware_queue: configures a request at hardware level
407 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300408 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800409 *
410 * This function returns an error code
411 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300412static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800413{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300414 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530415 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300416 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300417 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300418 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800419
David Lopoaa69a802008-11-17 14:14:51 -0800420 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300421 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800422 return -EALREADY;
423
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300424 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800425
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300426 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300427 if (ret)
428 return ret;
429
Michael Grzeschik2e270412013-06-13 17:59:54 +0300430 /*
431 * The first buffer could be not page aligned.
432 * In that case we have to span into one extra td.
433 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300434 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300435 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300436
Michael Grzeschik2e270412013-06-13 17:59:54 +0300437 if (rest == 0)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300438 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300439
Michael Grzeschik2e270412013-06-13 17:59:54 +0300440 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300441 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300442 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300443 add_td_to_list(hwep, hwreq, count);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300444 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200445 }
David Lopoaa69a802008-11-17 14:14:51 -0800446
Peter Chena4da4f12015-07-28 10:08:32 +0800447 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300448 && (hwreq->req.length % hwep->ep.maxpacket == 0))
449 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300450
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300451 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300452
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300453 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300454 struct td_node, td);
455
456 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300457 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300458 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300459 wmb();
460
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300461 hwreq->req.actual = 0;
462 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300463 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300464 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530465 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300466 struct td_node *prevlastnode;
467 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530468
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300469 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300470 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300471 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300472 struct td_node, td);
473
474 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530475 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800476 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530477 goto done;
478 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800479 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
480 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
481 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
482 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530483 if (tmp_stat)
484 goto done;
485 }
486
487 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300488 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
489 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200490 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800491
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800492 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300493 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300494
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800495 if (hwreq->req.length == 0
496 || hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300497 mul++;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300498 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300499 }
500
David Lopoaa69a802008-11-17 14:14:51 -0800501 wmb(); /* synchronize before ep prime */
502
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300503 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
504 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530505done:
506 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800507}
508
Michael Grzeschik2e270412013-06-13 17:59:54 +0300509/*
510 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300511 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300512 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300513static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300514{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300515 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300516
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300517 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
518 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300519 kfree(pending);
520}
521
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800522static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
523 struct td_node *node)
524{
525 hwep->qh.ptr->td.next = node->dma;
526 hwep->qh.ptr->td.token &=
527 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
528
529 /* Synchronize before ep prime */
530 wmb();
531
532 return hw_ep_prime(ci, hwep->num, hwep->dir,
533 hwep->type == USB_ENDPOINT_XFER_CONTROL);
534}
535
David Lopoaa69a802008-11-17 14:14:51 -0800536/**
537 * _hardware_dequeue: handles a request at hardware level
538 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300539 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800540 *
541 * This function returns an error code
542 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300543static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800544{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300545 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300546 struct td_node *node, *tmpnode;
547 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300548 unsigned actual = hwreq->req.length;
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800549 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200550
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300551 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800552 return -EINVAL;
553
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300554 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530555
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300556 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300557 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300558 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Sanchayan Maity06bdfcdb2015-02-11 12:44:56 +0800559 int n = hw_ep_bit(hwep->num, hwep->dir);
560
561 if (ci->rev == CI_REVISION_24)
562 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
563 reprime_dtd(ci, hwep, node);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300564 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530565 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300566 }
David Lopoaa69a802008-11-17 14:14:51 -0800567
Michael Grzeschik2e270412013-06-13 17:59:54 +0300568 remaining_length = (tmptoken & TD_TOTAL_BYTES);
569 remaining_length >>= __ffs(TD_TOTAL_BYTES);
570 actual -= remaining_length;
571
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300572 hwreq->req.status = tmptoken & TD_STATUS;
573 if ((TD_STATUS_HALTED & hwreq->req.status)) {
574 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300575 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300576 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
577 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300578 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300579 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
580 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300581 break;
582 }
583
584 if (remaining_length) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300585 if (hwep->dir) {
586 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300587 break;
588 }
589 }
590 /*
591 * As the hardware could still address the freed td
592 * which will run the udc unusable, the cleanup of the
593 * td has to be delayed by one.
594 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300595 if (hwep->pending_td)
596 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300597
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300598 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300599 list_del_init(&node->td);
600 }
David Lopoaa69a802008-11-17 14:14:51 -0800601
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300602 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800603
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300604 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800605
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300606 if (hwreq->req.status)
607 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800608
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300609 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800610}
611
612/**
613 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300614 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800615 *
616 * This function returns an error code
617 * Caller must hold lock
618 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300619static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300620__releases(hwep->lock)
621__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800622{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300623 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300624 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800625 return -EINVAL;
626
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300627 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800628
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300629 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800630
631 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300632 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
633 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300634
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300635 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
636 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300637 list_del_init(&node->td);
638 node->ptr = NULL;
639 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300640 }
641
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300642 list_del_init(&hwreq->queue);
643 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800644
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300645 if (hwreq->req.complete != NULL) {
646 spin_unlock(hwep->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200647 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300648 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800649 }
650 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300651
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300652 if (hwep->pending_td)
653 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300654
David Lopoaa69a802008-11-17 14:14:51 -0800655 return 0;
656}
657
Peter Chen56ffa1d2015-08-24 14:10:07 +0800658static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
659{
660 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
661 int direction, retval = 0;
662 unsigned long flags;
663
664 if (ep == NULL || hwep->ep.desc == NULL)
665 return -EINVAL;
666
667 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
668 return -EOPNOTSUPP;
669
670 spin_lock_irqsave(hwep->lock, flags);
671
672 if (value && hwep->dir == TX && check_transfer &&
673 !list_empty(&hwep->qh.queue) &&
674 !usb_endpoint_xfer_control(hwep->ep.desc)) {
675 spin_unlock_irqrestore(hwep->lock, flags);
676 return -EAGAIN;
677 }
678
679 direction = hwep->dir;
680 do {
681 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
682
683 if (!value)
684 hwep->wedge = 0;
685
686 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
687 hwep->dir = (hwep->dir == TX) ? RX : TX;
688
689 } while (hwep->dir != direction);
690
691 spin_unlock_irqrestore(hwep->lock, flags);
692 return retval;
693}
694
695
David Lopoaa69a802008-11-17 14:14:51 -0800696/**
697 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
698 * @gadget: gadget
699 *
700 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800701 */
702static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800703{
704 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300705 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530706 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800707
Richard Zhao26c696c2012-07-07 22:56:40 +0800708 spin_lock_irqsave(&ci->lock, flags);
709 ci->gadget.speed = USB_SPEED_UNKNOWN;
710 ci->remote_wakeup = 0;
711 ci->suspended = 0;
712 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530713
David Lopoaa69a802008-11-17 14:14:51 -0800714 /* flush all endpoints */
715 gadget_for_each_ep(ep, gadget) {
716 usb_ep_fifo_flush(ep);
717 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800718 usb_ep_fifo_flush(&ci->ep0out->ep);
719 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800720
David Lopoaa69a802008-11-17 14:14:51 -0800721 /* make sure to disable all endpoints */
722 gadget_for_each_ep(ep, gadget) {
723 usb_ep_disable(ep);
724 }
David Lopoaa69a802008-11-17 14:14:51 -0800725
Richard Zhao26c696c2012-07-07 22:56:40 +0800726 if (ci->status != NULL) {
727 usb_ep_free_request(&ci->ep0in->ep, ci->status);
728 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800729 }
730
David Lopoaa69a802008-11-17 14:14:51 -0800731 return 0;
732}
733
734/******************************************************************************
735 * ISR block
736 *****************************************************************************/
737/**
738 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800739 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800740 *
741 * This function resets USB engine after a bus reset occurred
742 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300743static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800744__releases(ci->lock)
745__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800746{
David Lopoaa69a802008-11-17 14:14:51 -0800747 int retval;
748
Peter Chena3aee362013-10-09 14:39:52 +0800749 spin_unlock(&ci->lock);
Peter Chenafbe4772014-11-06 14:27:58 +0800750 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
751 usb_gadget_udc_reset(&ci->gadget, ci->driver);
Peter Chen92b336d2013-09-17 12:37:19 +0800752
Richard Zhao26c696c2012-07-07 22:56:40 +0800753 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800754 if (retval)
755 goto done;
756
Richard Zhao26c696c2012-07-07 22:56:40 +0800757 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800758 if (retval)
759 goto done;
760
Richard Zhao26c696c2012-07-07 22:56:40 +0800761 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
762 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530763 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530764
Michael Grzeschikb9322252012-05-11 17:25:50 +0300765done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800766 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800767
David Lopoaa69a802008-11-17 14:14:51 -0800768 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800769 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800770}
771
772/**
773 * isr_get_status_complete: get_status request complete function
774 * @ep: endpoint
775 * @req: request handled
776 *
777 * Caller must release lock
778 */
779static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
780{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300781 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800782 return;
David Lopoaa69a802008-11-17 14:14:51 -0800783
784 kfree(req->buf);
785 usb_ep_free_request(ep, req);
786}
787
788/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200789 * _ep_queue: queues (submits) an I/O request to an endpoint
790 *
791 * Caller must hold lock
792 */
793static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
794 gfp_t __maybe_unused gfp_flags)
795{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300796 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
797 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
798 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200799 int retval = 0;
800
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300801 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200802 return -EINVAL;
803
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300804 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200805 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300806 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200807 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300808 if (!list_empty(&hwep->qh.queue)) {
809 _ep_nuke(hwep);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200810 retval = -EOVERFLOW;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300811 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
812 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200813 }
814 }
815
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300816 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
817 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
818 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300819 return -EMSGSIZE;
820 }
821
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200822 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300823 if (!list_empty(&hwreq->queue)) {
824 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200825 return -EBUSY;
826 }
827
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200828 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300829 hwreq->req.status = -EINPROGRESS;
830 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200831
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300832 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200833
834 if (retval == -EALREADY)
835 retval = 0;
836 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300837 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200838
839 return retval;
840}
841
842/**
David Lopoaa69a802008-11-17 14:14:51 -0800843 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800844 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800845 * @setup: setup request packet
846 *
847 * This function returns an error code
848 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300849static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800850 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300851__releases(hwep->lock)
852__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800853{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300854 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800855 struct usb_request *req = NULL;
856 gfp_t gfp_flags = GFP_ATOMIC;
857 int dir, num, retval;
858
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300859 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800860 return -EINVAL;
861
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300862 spin_unlock(hwep->lock);
863 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
864 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800865 if (req == NULL)
866 return -ENOMEM;
867
868 req->complete = isr_get_status_complete;
869 req->length = 2;
870 req->buf = kzalloc(req->length, gfp_flags);
871 if (req->buf == NULL) {
872 retval = -ENOMEM;
873 goto err_free_req;
874 }
875
876 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Peter Chen1009f9a2015-01-28 16:32:25 +0800877 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
878 ci->gadget.is_selfpowered;
David Lopoaa69a802008-11-17 14:14:51 -0800879 } else if ((setup->bRequestType & USB_RECIP_MASK) \
880 == USB_RECIP_ENDPOINT) {
881 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
882 TX : RX;
883 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800884 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800885 }
886 /* else do nothing; reserved for future use */
887
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300888 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800889 if (retval)
890 goto err_free_buf;
891
892 return 0;
893
894 err_free_buf:
895 kfree(req->buf);
896 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300897 spin_unlock(hwep->lock);
898 usb_ep_free_request(&hwep->ep, req);
899 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800900 return retval;
901}
902
903/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530904 * isr_setup_status_complete: setup_status request complete function
905 * @ep: endpoint
906 * @req: request handled
907 *
908 * Caller must release lock. Put the port in test mode if test mode
909 * feature is selected.
910 */
911static void
912isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
913{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300914 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530915 unsigned long flags;
916
Richard Zhao26c696c2012-07-07 22:56:40 +0800917 if (ci->setaddr) {
918 hw_usb_set_address(ci, ci->address);
919 ci->setaddr = false;
Peter Chen10775eb2014-05-04 09:24:44 +0800920 if (ci->address)
921 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
Alexander Shishkinef15e542012-05-11 17:25:43 +0300922 }
923
Richard Zhao26c696c2012-07-07 22:56:40 +0800924 spin_lock_irqsave(&ci->lock, flags);
925 if (ci->test_mode)
926 hw_port_test_set(ci, ci->test_mode);
927 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530928}
929
930/**
David Lopoaa69a802008-11-17 14:14:51 -0800931 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800932 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800933 *
934 * This function returns an error code
935 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300936static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800937{
938 int retval;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300939 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800940
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300941 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800942 ci->status->context = ci;
943 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800944
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300945 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800946
947 return retval;
948}
949
950/**
951 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300952 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800953 *
954 * This function returns an error code
955 * Caller must hold lock
956 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300957static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300958__releases(hwep->lock)
959__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800960{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300961 struct ci_hw_req *hwreq, *hwreqtemp;
962 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300963 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800964
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300965 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530966 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300967 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530968 if (retval < 0)
969 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300970 list_del_init(&hwreq->queue);
971 if (hwreq->req.complete != NULL) {
972 spin_unlock(hwep->lock);
973 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
974 hwreq->req.length)
975 hweptemp = hwep->ci->ep0in;
Michal Sojka304f7e52014-09-24 22:43:19 +0200976 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300977 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530978 }
979 }
David Lopoaa69a802008-11-17 14:14:51 -0800980
Pavankumar Kondetief907482011-05-02 11:56:27 +0530981 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530982 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800983
David Lopoaa69a802008-11-17 14:14:51 -0800984 return retval;
985}
986
Li Jund20f78072015-03-08 16:05:01 +0800987static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
988{
989 dev_warn(&ci->gadget.dev,
990 "connect the device to an alternate port if you want HNP\n");
991 return isr_setup_status_phase(ci);
992}
993
David Lopoaa69a802008-11-17 14:14:51 -0800994/**
Peter Chend7b00e32014-03-11 13:47:37 +0800995 * isr_setup_packet_handler: setup packet handler
996 * @ci: UDC descriptor
997 *
998 * This function handles setup packet
999 */
1000static void isr_setup_packet_handler(struct ci_hdrc *ci)
1001__releases(ci->lock)
1002__acquires(ci->lock)
1003{
1004 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1005 struct usb_ctrlrequest req;
1006 int type, num, dir, err = -EINVAL;
1007 u8 tmode = 0;
1008
1009 /*
1010 * Flush data and handshake transactions of previous
1011 * setup packet.
1012 */
1013 _ep_nuke(ci->ep0out);
1014 _ep_nuke(ci->ep0in);
1015
1016 /* read_setup_packet */
1017 do {
1018 hw_test_and_set_setup_guard(ci);
1019 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1020 } while (!hw_test_and_clear_setup_guard(ci));
1021
1022 type = req.bRequestType;
1023
1024 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1025
1026 switch (req.bRequest) {
1027 case USB_REQ_CLEAR_FEATURE:
1028 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1029 le16_to_cpu(req.wValue) ==
1030 USB_ENDPOINT_HALT) {
1031 if (req.wLength != 0)
1032 break;
1033 num = le16_to_cpu(req.wIndex);
1034 dir = num & USB_ENDPOINT_DIR_MASK;
1035 num &= USB_ENDPOINT_NUMBER_MASK;
1036 if (dir) /* TX */
1037 num += ci->hw_ep_max / 2;
1038 if (!ci->ci_hw_ep[num].wedge) {
1039 spin_unlock(&ci->lock);
1040 err = usb_ep_clear_halt(
1041 &ci->ci_hw_ep[num].ep);
1042 spin_lock(&ci->lock);
1043 if (err)
1044 break;
1045 }
1046 err = isr_setup_status_phase(ci);
1047 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1048 le16_to_cpu(req.wValue) ==
1049 USB_DEVICE_REMOTE_WAKEUP) {
1050 if (req.wLength != 0)
1051 break;
1052 ci->remote_wakeup = 0;
1053 err = isr_setup_status_phase(ci);
1054 } else {
1055 goto delegate;
1056 }
1057 break;
1058 case USB_REQ_GET_STATUS:
1059 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1060 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1061 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1062 goto delegate;
1063 if (le16_to_cpu(req.wLength) != 2 ||
1064 le16_to_cpu(req.wValue) != 0)
1065 break;
1066 err = isr_get_status_response(ci, &req);
1067 break;
1068 case USB_REQ_SET_ADDRESS:
1069 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1070 goto delegate;
1071 if (le16_to_cpu(req.wLength) != 0 ||
1072 le16_to_cpu(req.wIndex) != 0)
1073 break;
1074 ci->address = (u8)le16_to_cpu(req.wValue);
1075 ci->setaddr = true;
1076 err = isr_setup_status_phase(ci);
1077 break;
1078 case USB_REQ_SET_FEATURE:
1079 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1080 le16_to_cpu(req.wValue) ==
1081 USB_ENDPOINT_HALT) {
1082 if (req.wLength != 0)
1083 break;
1084 num = le16_to_cpu(req.wIndex);
1085 dir = num & USB_ENDPOINT_DIR_MASK;
1086 num &= USB_ENDPOINT_NUMBER_MASK;
1087 if (dir) /* TX */
1088 num += ci->hw_ep_max / 2;
1089
1090 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001091 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
Peter Chend7b00e32014-03-11 13:47:37 +08001092 spin_lock(&ci->lock);
1093 if (!err)
1094 isr_setup_status_phase(ci);
1095 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1096 if (req.wLength != 0)
1097 break;
1098 switch (le16_to_cpu(req.wValue)) {
1099 case USB_DEVICE_REMOTE_WAKEUP:
1100 ci->remote_wakeup = 1;
1101 err = isr_setup_status_phase(ci);
1102 break;
1103 case USB_DEVICE_TEST_MODE:
1104 tmode = le16_to_cpu(req.wIndex) >> 8;
1105 switch (tmode) {
1106 case TEST_J:
1107 case TEST_K:
1108 case TEST_SE0_NAK:
1109 case TEST_PACKET:
1110 case TEST_FORCE_EN:
1111 ci->test_mode = tmode;
1112 err = isr_setup_status_phase(
1113 ci);
1114 break;
1115 default:
1116 break;
1117 }
Li Jun95f55552014-04-23 15:56:47 +08001118 break;
1119 case USB_DEVICE_B_HNP_ENABLE:
1120 if (ci_otg_is_fsm_mode(ci)) {
1121 ci->gadget.b_hnp_enable = 1;
1122 err = isr_setup_status_phase(
1123 ci);
1124 }
1125 break;
Li Jund20f78072015-03-08 16:05:01 +08001126 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1127 if (ci_otg_is_fsm_mode(ci))
1128 err = otg_a_alt_hnp_support(ci);
1129 break;
Peter Chen3520d462015-07-17 08:44:24 +08001130 case USB_DEVICE_A_HNP_SUPPORT:
1131 if (ci_otg_is_fsm_mode(ci)) {
1132 ci->gadget.a_hnp_support = 1;
1133 err = isr_setup_status_phase(
1134 ci);
1135 }
1136 break;
Peter Chend7b00e32014-03-11 13:47:37 +08001137 default:
1138 goto delegate;
1139 }
1140 } else {
1141 goto delegate;
1142 }
1143 break;
1144 default:
1145delegate:
1146 if (req.wLength == 0) /* no data phase */
1147 ci->ep0_dir = TX;
1148
1149 spin_unlock(&ci->lock);
1150 err = ci->driver->setup(&ci->gadget, &req);
1151 spin_lock(&ci->lock);
1152 break;
1153 }
1154
1155 if (err < 0) {
1156 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001157 if (_ep_set_halt(&hwep->ep, 1, false))
1158 dev_err(ci->dev, "error: _ep_set_halt\n");
Peter Chend7b00e32014-03-11 13:47:37 +08001159 spin_lock(&ci->lock);
1160 }
1161}
1162
1163/**
David Lopoaa69a802008-11-17 14:14:51 -08001164 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +08001165 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -08001166 *
1167 * This function handles traffic events
1168 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001169static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +08001170__releases(ci->lock)
1171__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -08001172{
1173 unsigned i;
Peter Chend7b00e32014-03-11 13:47:37 +08001174 int err;
David Lopoaa69a802008-11-17 14:14:51 -08001175
Richard Zhao26c696c2012-07-07 22:56:40 +08001176 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001177 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
David Lopoaa69a802008-11-17 14:14:51 -08001178
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001179 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001180 continue; /* not configured */
1181
Richard Zhao26c696c2012-07-07 22:56:40 +08001182 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001183 err = isr_tr_complete_low(hwep);
1184 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -08001185 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001186 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001187 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001188 spin_unlock(&ci->lock);
Peter Chen56ffa1d2015-08-24 14:10:07 +08001189 if (_ep_set_halt(&hwep->ep, 1, false))
Richard Zhao26c696c2012-07-07 22:56:40 +08001190 dev_err(ci->dev,
Peter Chen56ffa1d2015-08-24 14:10:07 +08001191 "error: _ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +08001192 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001193 }
1194 }
1195 }
1196
Peter Chen64fc06c2014-02-19 13:41:41 +08001197 /* Only handle setup packet below */
Peter Chend7b00e32014-03-11 13:47:37 +08001198 if (i == 0 &&
1199 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1200 isr_setup_packet_handler(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001201 }
1202}
1203
1204/******************************************************************************
1205 * ENDPT block
1206 *****************************************************************************/
1207/**
1208 * ep_enable: configure endpoint, making it usable
1209 *
1210 * Check usb_ep_enable() at "usb_gadget.h" for details
1211 */
1212static int ep_enable(struct usb_ep *ep,
1213 const struct usb_endpoint_descriptor *desc)
1214{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001215 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301216 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001217 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001218 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001219
David Lopoaa69a802008-11-17 14:14:51 -08001220 if (ep == NULL || desc == NULL)
1221 return -EINVAL;
1222
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001223 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001224
1225 /* only internal SW should enable ctrl endpts */
1226
Peter Chend5d1e1b2015-02-11 12:44:41 +08001227 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001228 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
Peter Chend5d1e1b2015-02-11 12:44:41 +08001229 spin_unlock_irqrestore(hwep->lock, flags);
1230 return -EBUSY;
1231 }
1232
1233 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001234
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001235 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1236 hwep->num = usb_endpoint_num(desc);
1237 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001238
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001239 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1240 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001241
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001242 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001243 cap |= QH_IOS;
Abbas Raza953c6642014-07-17 19:34:31 +08001244
1245 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001246 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
Peter Chen2fc5a7d2014-01-10 13:51:32 +08001247 /*
1248 * For ISO-TX, we set mult at QH as the largest value, and use
1249 * MultO at TD as real mult value.
1250 */
1251 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1252 cap |= 3 << __ffs(QH_MULT);
David Lopoaa69a802008-11-17 14:14:51 -08001253
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001254 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001255
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001256 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001257
Peter Chen64fc06c2014-02-19 13:41:41 +08001258 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1259 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1260 retval = -EINVAL;
1261 }
1262
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301263 /*
1264 * Enable endpoints in the HW other than ep0 as ep0
1265 * is always enabled
1266 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001267 if (hwep->num)
1268 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1269 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001270
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001271 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001272 return retval;
1273}
1274
1275/**
1276 * ep_disable: endpoint is no longer usable
1277 *
1278 * Check usb_ep_disable() at "usb_gadget.h" for details
1279 */
1280static int ep_disable(struct usb_ep *ep)
1281{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001282 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001283 int direction, retval = 0;
1284 unsigned long flags;
1285
David Lopoaa69a802008-11-17 14:14:51 -08001286 if (ep == NULL)
1287 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001288 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001289 return -EBUSY;
1290
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001291 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001292
1293 /* only internal SW should disable ctrl endpts */
1294
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001295 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001296 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001297 retval |= _ep_nuke(hwep);
1298 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001299
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001300 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1301 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001302
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001303 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001304
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001305 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001306
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001307 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001308 return retval;
1309}
1310
1311/**
1312 * ep_alloc_request: allocate a request object to use with this endpoint
1313 *
1314 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1315 */
1316static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1317{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001318 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001319
Alexander Shishkin0f089092012-05-08 23:29:02 +03001320 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001321 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001322
Alexander Shishkin8e229782013-06-24 14:46:36 +03001323 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001324 if (hwreq != NULL) {
1325 INIT_LIST_HEAD(&hwreq->queue);
1326 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001327 }
1328
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001329 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001330}
1331
1332/**
1333 * ep_free_request: frees a request object
1334 *
1335 * Check usb_ep_free_request() at "usb_gadget.h" for details
1336 */
1337static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1338{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001339 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1340 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001341 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001342 unsigned long flags;
1343
David Lopoaa69a802008-11-17 14:14:51 -08001344 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001345 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001346 } else if (!list_empty(&hwreq->queue)) {
1347 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001348 return;
1349 }
1350
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001351 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001352
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001353 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1354 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001355 list_del_init(&node->td);
1356 node->ptr = NULL;
1357 kfree(node);
1358 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001359
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001360 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001361
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001362 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001363}
1364
1365/**
1366 * ep_queue: queues (submits) an I/O request to an endpoint
1367 *
1368 * Check usb_ep_queue()* at usb_gadget.h" for details
1369 */
1370static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1371 gfp_t __maybe_unused gfp_flags)
1372{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001373 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001374 int retval = 0;
1375 unsigned long flags;
1376
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001377 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001378 return -EINVAL;
1379
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001380 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001381 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001382 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001383 return retval;
1384}
1385
1386/**
1387 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1388 *
1389 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1390 */
1391static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1392{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001393 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1394 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001395 unsigned long flags;
Peter Chene4adcff2014-07-02 12:16:31 +08001396 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001397
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001398 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1399 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1400 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001401 return -EINVAL;
1402
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001403 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001404
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001405 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001406
Peter Chene4adcff2014-07-02 12:16:31 +08001407 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1408 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1409 list_del(&node->td);
1410 kfree(node);
1411 }
1412
David Lopoaa69a802008-11-17 14:14:51 -08001413 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001414 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001415
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001416 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001417
David Lopoaa69a802008-11-17 14:14:51 -08001418 req->status = -ECONNRESET;
1419
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001420 if (hwreq->req.complete != NULL) {
1421 spin_unlock(hwep->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001422 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001423 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001424 }
1425
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001426 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001427 return 0;
1428}
1429
1430/**
1431 * ep_set_halt: sets the endpoint halt feature
1432 *
1433 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1434 */
1435static int ep_set_halt(struct usb_ep *ep, int value)
1436{
Peter Chen56ffa1d2015-08-24 14:10:07 +08001437 return _ep_set_halt(ep, value, true);
David Lopoaa69a802008-11-17 14:14:51 -08001438}
1439
1440/**
1441 * ep_set_wedge: sets the halt feature and ignores clear requests
1442 *
1443 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1444 */
1445static int ep_set_wedge(struct usb_ep *ep)
1446{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001447 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001448 unsigned long flags;
1449
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001450 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001451 return -EINVAL;
1452
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001453 spin_lock_irqsave(hwep->lock, flags);
1454 hwep->wedge = 1;
1455 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001456
1457 return usb_ep_set_halt(ep);
1458}
1459
1460/**
1461 * ep_fifo_flush: flushes contents of a fifo
1462 *
1463 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1464 */
1465static void ep_fifo_flush(struct usb_ep *ep)
1466{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001467 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001468 unsigned long flags;
1469
David Lopoaa69a802008-11-17 14:14:51 -08001470 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001471 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001472 return;
1473 }
1474
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001475 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001476
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001477 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001478
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001479 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001480}
1481
1482/**
1483 * Endpoint-specific part of the API to the USB controller hardware
1484 * Check "usb_gadget.h" for details
1485 */
1486static const struct usb_ep_ops usb_ep_ops = {
1487 .enable = ep_enable,
1488 .disable = ep_disable,
1489 .alloc_request = ep_alloc_request,
1490 .free_request = ep_free_request,
1491 .queue = ep_queue,
1492 .dequeue = ep_dequeue,
1493 .set_halt = ep_set_halt,
1494 .set_wedge = ep_set_wedge,
1495 .fifo_flush = ep_fifo_flush,
1496};
1497
1498/******************************************************************************
1499 * GADGET block
1500 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001501static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301502{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001503 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301504 unsigned long flags;
1505 int gadget_ready = 0;
1506
Richard Zhao26c696c2012-07-07 22:56:40 +08001507 spin_lock_irqsave(&ci->lock, flags);
1508 ci->vbus_active = is_active;
1509 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301510 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001511 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301512
1513 if (gadget_ready) {
1514 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301515 pm_runtime_get_sync(&_gadget->dev);
Peter Chen5b157302014-11-26 13:44:33 +08001516 hw_device_reset(ci);
Richard Zhao26c696c2012-07-07 22:56:40 +08001517 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen10775eb2014-05-04 09:24:44 +08001518 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
Peter Chen467a78c2015-03-06 10:36:04 +08001519 usb_udc_vbus_handler(_gadget, true);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301520 } else {
Peter Chen467a78c2015-03-06 10:36:04 +08001521 usb_udc_vbus_handler(_gadget, false);
Peter Chen92b336d2013-09-17 12:37:19 +08001522 if (ci->driver)
1523 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001524 hw_device_state(ci, 0);
1525 if (ci->platdata->notify_event)
1526 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001527 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001528 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301529 pm_runtime_put_sync(&_gadget->dev);
Peter Chen10775eb2014-05-04 09:24:44 +08001530 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301531 }
1532 }
1533
1534 return 0;
1535}
1536
Alexander Shishkin8e229782013-06-24 14:46:36 +03001537static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301538{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001539 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301540 unsigned long flags;
1541 int ret = 0;
1542
Richard Zhao26c696c2012-07-07 22:56:40 +08001543 spin_lock_irqsave(&ci->lock, flags);
1544 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301545 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301546 goto out;
1547 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001548 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301549 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301550 goto out;
1551 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001552 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301553out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001554 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301555 return ret;
1556}
1557
Alexander Shishkin8e229782013-06-24 14:46:36 +03001558static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301559{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001560 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301561
Antoine Tenartef44cb42014-10-30 18:41:16 +01001562 if (ci->usb_phy)
1563 return usb_phy_set_power(ci->usb_phy, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301564 return -ENOTSUPP;
1565}
1566
Peter Chen1009f9a2015-01-28 16:32:25 +08001567static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1568{
1569 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1570 struct ci_hw_ep *hwep = ci->ep0in;
1571 unsigned long flags;
1572
1573 spin_lock_irqsave(hwep->lock, flags);
1574 _gadget->is_selfpowered = (is_on != 0);
1575 spin_unlock_irqrestore(hwep->lock, flags);
1576
1577 return 0;
1578}
1579
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001580/* Change Data+ pullup status
1581 * this func is used by usb_gadget_connect/disconnet
1582 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001583static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001584{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001585 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001586
Li Jun9b6567e2015-03-23 16:03:35 +08001587 /* Data+ pullup controlled by OTG state machine in OTG fsm mode */
1588 if (ci_otg_is_fsm_mode(ci))
1589 return 0;
1590
Peter Chen467a78c2015-03-06 10:36:04 +08001591 pm_runtime_get_sync(&ci->gadget.dev);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001592 if (is_on)
1593 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1594 else
1595 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
Peter Chen467a78c2015-03-06 10:36:04 +08001596 pm_runtime_put_sync(&ci->gadget.dev);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001597
1598 return 0;
1599}
1600
Alexander Shishkin8e229782013-06-24 14:46:36 +03001601static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001602 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001603static int ci_udc_stop(struct usb_gadget *gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001604/**
1605 * Device operations part of the API to the USB controller hardware,
1606 * which don't involve endpoints (or i/o)
1607 * Check "usb_gadget.h" for details
1608 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301609static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001610 .vbus_session = ci_udc_vbus_session,
1611 .wakeup = ci_udc_wakeup,
Peter Chen1009f9a2015-01-28 16:32:25 +08001612 .set_selfpowered = ci_udc_selfpowered,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001613 .pullup = ci_udc_pullup,
1614 .vbus_draw = ci_udc_vbus_draw,
1615 .udc_start = ci_udc_start,
1616 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301617};
David Lopoaa69a802008-11-17 14:14:51 -08001618
Alexander Shishkin8e229782013-06-24 14:46:36 +03001619static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001620{
1621 int retval = 0, i, j;
1622
Richard Zhao26c696c2012-07-07 22:56:40 +08001623 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001624 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001625 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001626 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001627
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001628 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001629 (j == TX) ? "in" : "out");
1630
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001631 hwep->ci = ci;
1632 hwep->lock = &ci->lock;
1633 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001634
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001635 hwep->ep.name = hwep->name;
1636 hwep->ep.ops = &usb_ep_ops;
Robert Baldygaa7e3f142015-07-31 16:00:17 +02001637
1638 if (i == 0) {
1639 hwep->ep.caps.type_control = true;
1640 } else {
1641 hwep->ep.caps.type_iso = true;
1642 hwep->ep.caps.type_bulk = true;
1643 hwep->ep.caps.type_int = true;
1644 }
1645
1646 if (j == TX)
1647 hwep->ep.caps.dir_in = true;
1648 else
1649 hwep->ep.caps.dir_out = true;
1650
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001651 /*
1652 * for ep0: maxP defined in desc, for other
1653 * eps, maxP is set by epautoconfig() called
1654 * by gadget layer
1655 */
Robert Baldygae117e742013-12-13 12:23:38 +01001656 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001657
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001658 INIT_LIST_HEAD(&hwep->qh.queue);
1659 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1660 &hwep->qh.dma);
1661 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001662 retval = -ENOMEM;
1663 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001664 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001665
1666 /*
1667 * set up shorthands for ep0 out and in endpoints,
1668 * don't add to gadget's ep_list
1669 */
1670 if (i == 0) {
1671 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001672 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001673 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001674 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001675
Robert Baldygae117e742013-12-13 12:23:38 +01001676 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001677 continue;
1678 }
1679
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001680 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001681 }
1682
1683 return retval;
1684}
1685
Alexander Shishkin8e229782013-06-24 14:46:36 +03001686static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001687{
1688 int i;
1689
1690 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001691 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001692
Peter Chen4a295672013-09-10 15:34:39 +08001693 if (hwep->pending_td)
1694 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001695 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001696 }
1697}
1698
David Lopoaa69a802008-11-17 14:14:51 -08001699/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001700 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001701 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001702 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001703 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001704 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001705 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001706static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001707 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001708{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001709 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301710 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001711 int retval = -ENOMEM;
1712
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001713 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001714 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001715
David Lopoaa69a802008-11-17 14:14:51 -08001716
Richard Zhao26c696c2012-07-07 22:56:40 +08001717 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1718 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301719 if (retval)
1720 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001721
Richard Zhao26c696c2012-07-07 22:56:40 +08001722 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1723 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301724 if (retval)
1725 return retval;
David Lopoaa69a802008-11-17 14:14:51 -08001726
Richard Zhao26c696c2012-07-07 22:56:40 +08001727 ci->driver = driver;
Li Jun4dcf7202014-04-23 15:56:50 +08001728
1729 /* Start otg fsm for B-device */
1730 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1731 ci_hdrc_otg_fsm_start(ci);
1732 return retval;
1733 }
1734
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001736 if (ci->vbus_active) {
Peter Chen65b2fb32013-10-08 10:30:17 +08001737 spin_lock_irqsave(&ci->lock, flags);
Peter Chen5b157302014-11-26 13:44:33 +08001738 hw_device_reset(ci);
Peter Chend268e9b2013-08-14 12:44:14 +03001739 } else {
Peter Chen467a78c2015-03-06 10:36:04 +08001740 usb_udc_vbus_handler(&ci->gadget, false);
Peter Chend268e9b2013-08-14 12:44:14 +03001741 pm_runtime_put_sync(&ci->gadget.dev);
Peter Chen65b2fb32013-10-08 10:30:17 +08001742 return retval;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301743 }
1744
Richard Zhao26c696c2012-07-07 22:56:40 +08001745 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen65b2fb32013-10-08 10:30:17 +08001746 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301747 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001748 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001749
David Lopoaa69a802008-11-17 14:14:51 -08001750 return retval;
1751}
David Lopoaa69a802008-11-17 14:14:51 -08001752
Li Jun85da8522014-12-12 09:11:42 +08001753static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1754{
1755 if (!ci_otg_is_fsm_mode(ci))
1756 return;
1757
1758 mutex_lock(&ci->fsm.lock);
1759 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1760 ci->fsm.a_bidl_adis_tmout = 1;
1761 ci_hdrc_otg_fsm_start(ci);
1762 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1763 ci->fsm.protocol = PROTO_UNDEF;
1764 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1765 }
1766 mutex_unlock(&ci->fsm.lock);
1767}
1768
David Lopoaa69a802008-11-17 14:14:51 -08001769/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001770 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001771 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001772static int ci_udc_stop(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -08001773{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001774 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001775 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001776
Richard Zhao26c696c2012-07-07 22:56:40 +08001777 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001778
Peter Chend268e9b2013-08-14 12:44:14 +03001779 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001780 hw_device_state(ci, 0);
1781 if (ci->platdata->notify_event)
1782 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001783 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001784 spin_unlock_irqrestore(&ci->lock, flags);
1785 _gadget_stop_activity(&ci->gadget);
1786 spin_lock_irqsave(&ci->lock, flags);
1787 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301788 }
David Lopoaa69a802008-11-17 14:14:51 -08001789
Peter Chenf84839d2013-09-17 12:37:20 +08001790 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001791 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001792
Li Jun85da8522014-12-12 09:11:42 +08001793 ci_udc_stop_for_otg_fsm(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001794 return 0;
1795}
David Lopoaa69a802008-11-17 14:14:51 -08001796
1797/******************************************************************************
1798 * BUS block
1799 *****************************************************************************/
1800/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001801 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001802 *
1803 * This function returns IRQ_HANDLED if the IRQ has been handled
1804 * It locks access to registers
1805 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001806static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001807{
David Lopoaa69a802008-11-17 14:14:51 -08001808 irqreturn_t retval;
1809 u32 intr;
1810
Richard Zhao26c696c2012-07-07 22:56:40 +08001811 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001812 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001813
Richard Zhao26c696c2012-07-07 22:56:40 +08001814 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301815
Alexander Shishkin8e229782013-06-24 14:46:36 +03001816 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001817 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001818 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001819 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301820 return IRQ_NONE;
1821 }
1822 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001823 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001824
Alexander Shishkine443b332012-05-11 17:25:46 +03001825 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001826 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001827 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001828 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001829
David Lopoaa69a802008-11-17 14:14:51 -08001830 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001831 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001832 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001833 if (ci->suspended && ci->driver->resume) {
1834 spin_unlock(&ci->lock);
1835 ci->driver->resume(&ci->gadget);
1836 spin_lock(&ci->lock);
1837 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301838 }
David Lopoaa69a802008-11-17 14:14:51 -08001839 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001840
1841 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001842 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001843
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301844 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001845 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1846 ci->driver->suspend) {
1847 ci->suspended = 1;
1848 spin_unlock(&ci->lock);
1849 ci->driver->suspend(&ci->gadget);
Peter Chen10775eb2014-05-04 09:24:44 +08001850 usb_gadget_set_state(&ci->gadget,
1851 USB_STATE_SUSPENDED);
Richard Zhao26c696c2012-07-07 22:56:40 +08001852 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301853 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301854 }
David Lopoaa69a802008-11-17 14:14:51 -08001855 retval = IRQ_HANDLED;
1856 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001857 retval = IRQ_NONE;
1858 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001859 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001860
1861 return retval;
1862}
1863
1864/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001865 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001866 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001867 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001868static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001869{
Richard Zhao26c696c2012-07-07 22:56:40 +08001870 struct device *dev = ci->dev;
Li Jun79742352015-07-09 15:18:45 +08001871 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
David Lopoaa69a802008-11-17 14:14:51 -08001872 int retval = 0;
1873
Richard Zhao26c696c2012-07-07 22:56:40 +08001874 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001875
Richard Zhao26c696c2012-07-07 22:56:40 +08001876 ci->gadget.ops = &usb_gadget_ops;
1877 ci->gadget.speed = USB_SPEED_UNKNOWN;
1878 ci->gadget.max_speed = USB_SPEED_HIGH;
Richard Zhao26c696c2012-07-07 22:56:40 +08001879 ci->gadget.name = ci->platdata->name;
Li Jun79742352015-07-09 15:18:45 +08001880 ci->gadget.otg_caps = otg_caps;
1881
Li Jun3f217e92015-07-31 10:41:00 +08001882 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
1883 otg_caps->adp_support))
Li Jun79742352015-07-09 15:18:45 +08001884 ci->gadget.is_otg = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001885
Richard Zhao26c696c2012-07-07 22:56:40 +08001886 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001887
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001888 /* alloc resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001889 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1890 sizeof(struct ci_hw_qh),
1891 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001892 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001893 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001894
Alexander Shishkin8e229782013-06-24 14:46:36 +03001895 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1896 sizeof(struct ci_hw_td),
1897 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001898 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001899 retval = -ENOMEM;
1900 goto free_qh_pool;
1901 }
1902
Richard Zhao26c696c2012-07-07 22:56:40 +08001903 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001904 if (retval)
1905 goto free_pools;
1906
Richard Zhao26c696c2012-07-07 22:56:40 +08001907 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301908
Richard Zhao26c696c2012-07-07 22:56:40 +08001909 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001910 if (retval)
Peter Chen74475ed2013-09-24 12:47:53 +08001911 goto destroy_eps;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001912
Richard Zhao26c696c2012-07-07 22:56:40 +08001913 pm_runtime_no_callbacks(&ci->gadget.dev);
1914 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001915
David Lopoaa69a802008-11-17 14:14:51 -08001916 return retval;
1917
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001918destroy_eps:
1919 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001920free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001921 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001922free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001923 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001924 return retval;
1925}
1926
1927/**
Peter Chen3f124d22013-08-14 12:44:07 +03001928 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001929 *
1930 * No interrupts active, the IRQ has been released
1931 */
Peter Chen3f124d22013-08-14 12:44:07 +03001932void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001933{
Peter Chen3f124d22013-08-14 12:44:07 +03001934 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001935 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001936
Richard Zhao26c696c2012-07-07 22:56:40 +08001937 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001938
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001939 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001940
Richard Zhao26c696c2012-07-07 22:56:40 +08001941 dma_pool_destroy(ci->td_pool);
1942 dma_pool_destroy(ci->qh_pool);
Peter Chen3f124d22013-08-14 12:44:07 +03001943}
1944
1945static int udc_id_switch_for_device(struct ci_hdrc *ci)
1946{
Li Jun0c33bf72014-04-23 15:56:38 +08001947 if (ci->is_otg)
1948 /* Clear and enable BSV irq */
1949 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
1950 OTGSC_BSVIS | OTGSC_BSVIE);
Peter Chen3f124d22013-08-14 12:44:07 +03001951
1952 return 0;
1953}
1954
1955static void udc_id_switch_for_host(struct ci_hdrc *ci)
1956{
Li Jun0c33bf72014-04-23 15:56:38 +08001957 /*
1958 * host doesn't care B_SESSION_VALID event
1959 * so clear and disbale BSV irq
1960 */
1961 if (ci->is_otg)
1962 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001963}
David Lopoaa69a802008-11-17 14:14:51 -08001964
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001965/**
1966 * ci_hdrc_gadget_init - initialize device related bits
1967 * ci: the controller
1968 *
Peter Chen3f124d22013-08-14 12:44:07 +03001969 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001970 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001971int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001972{
1973 struct ci_role_driver *rdrv;
1974
1975 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1976 return -ENXIO;
1977
1978 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1979 if (!rdrv)
1980 return -ENOMEM;
1981
Peter Chen3f124d22013-08-14 12:44:07 +03001982 rdrv->start = udc_id_switch_for_device;
1983 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001984 rdrv->irq = udc_irq;
1985 rdrv->name = "gadget";
1986 ci->roles[CI_ROLE_GADGET] = rdrv;
1987
Peter Chen3f124d22013-08-14 12:44:07 +03001988 return udc_start(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001989}