blob: b34c81969cba672a7e880f405f2445adf36b7e6f [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>
Alexander Shishkine443b332012-05-11 17:25:46 +030023#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080024
Alexander Shishkine443b332012-05-11 17:25:46 +030025#include "ci.h"
26#include "udc.h"
27#include "bits.h"
28#include "debug.h"
Peter Chen3f124d22013-08-14 12:44:07 +030029#include "otg.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020030
David Lopoaa69a802008-11-17 14:14:51 -080031/* control endpoint description */
32static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053033ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080034 .bLength = USB_DT_ENDPOINT_SIZE,
35 .bDescriptorType = USB_DT_ENDPOINT,
36
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053037 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
39 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
40};
41
42static const struct usb_endpoint_descriptor
43ctrl_endpt_in_desc = {
44 .bLength = USB_DT_ENDPOINT_SIZE,
45 .bDescriptorType = USB_DT_ENDPOINT,
46
47 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080048 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
49 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
50};
51
David Lopoaa69a802008-11-17 14:14:51 -080052/**
53 * hw_ep_bit: calculates the bit number
54 * @num: endpoint number
55 * @dir: endpoint direction
56 *
57 * This function returns bit number
58 */
59static inline int hw_ep_bit(int num, int dir)
60{
61 return num + (dir ? 16 : 0);
62}
63
Alexander Shishkin8e229782013-06-24 14:46:36 +030064static inline int ep_to_bit(struct ci_hdrc *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020065{
Richard Zhao26c696c2012-07-07 22:56:40 +080066 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020067
Richard Zhao26c696c2012-07-07 22:56:40 +080068 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020069 n += fill;
70
71 return n;
72}
73
David Lopoaa69a802008-11-17 14:14:51 -080074/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030075 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080076 * @dma: 0 => disable, !0 => enable and set dma engine
77 *
78 * This function returns an error code
79 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030080static int hw_device_state(struct ci_hdrc *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080081{
82 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080083 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080084 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080085 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080086 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
Peter Chena107f8c2013-08-14 12:44:11 +030087 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
David Lopoaa69a802008-11-17 14:14:51 -080088 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080089 hw_write(ci, OP_USBINTR, ~0, 0);
Peter Chena107f8c2013-08-14 12:44:11 +030090 hw_write(ci, OP_USBCMD, USBCMD_RS, 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 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800108 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
109 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_test_and_clear_setup_status: test & clear setup status (execute without
182 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200183 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800184 *
185 * This function returns setup status
186 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300187static int hw_test_and_clear_setup_status(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800188{
Richard Zhao26c696c2012-07-07 22:56:40 +0800189 n = ep_to_bit(ci, n);
190 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800191}
192
193/**
194 * hw_ep_prime: primes endpoint (execute without interruption)
195 * @num: endpoint number
196 * @dir: endpoint direction
197 * @is_ctrl: true if control endpoint
198 *
199 * This function returns an error code
200 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300201static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800202{
203 int n = hw_ep_bit(num, dir);
204
Richard Zhao26c696c2012-07-07 22:56:40 +0800205 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800206 return -EAGAIN;
207
Richard Zhao26c696c2012-07-07 22:56:40 +0800208 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800209
Richard Zhao26c696c2012-07-07 22:56:40 +0800210 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800211 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800212 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800213 return -EAGAIN;
214
215 /* status shoult be tested according with manual but it doesn't work */
216 return 0;
217}
218
219/**
220 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
221 * without interruption)
222 * @num: endpoint number
223 * @dir: endpoint direction
224 * @value: true => stall, false => unstall
225 *
226 * This function returns an error code
227 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300228static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800229{
230 if (value != 0 && value != 1)
231 return -EINVAL;
232
233 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300234 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800235 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
236 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
237
238 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800239 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300240 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800241 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800242
243 return 0;
244}
245
246/**
David Lopoaa69a802008-11-17 14:14:51 -0800247 * hw_is_port_high_speed: test if port is high speed
248 *
249 * This function returns true if high speed port
250 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800252{
Richard Zhao26c696c2012-07-07 22:56:40 +0800253 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
254 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800255}
256
257/**
David Lopoaa69a802008-11-17 14:14:51 -0800258 * hw_read_intr_enable: returns interrupt enable register
259 *
260 * This function returns register data
261 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300262static u32 hw_read_intr_enable(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800263{
Richard Zhao26c696c2012-07-07 22:56:40 +0800264 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800265}
266
267/**
268 * hw_read_intr_status: returns interrupt status register
269 *
270 * This function returns register data
271 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300272static u32 hw_read_intr_status(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800273{
Richard Zhao26c696c2012-07-07 22:56:40 +0800274 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800275}
276
277/**
David Lopoaa69a802008-11-17 14:14:51 -0800278 * hw_test_and_clear_complete: test & clear complete status (execute without
279 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200280 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800281 *
282 * This function returns complete status
283 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300284static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800285{
Richard Zhao26c696c2012-07-07 22:56:40 +0800286 n = ep_to_bit(ci, n);
287 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800288}
289
290/**
291 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
292 * without interruption)
293 *
294 * This function returns active interrutps
295 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300296static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800297{
Richard Zhao26c696c2012-07-07 22:56:40 +0800298 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800299
Richard Zhao26c696c2012-07-07 22:56:40 +0800300 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800301 return reg;
302}
303
304/**
305 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
306 * interruption)
307 *
308 * This function returns guard value
309 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300310static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800311{
Richard Zhao26c696c2012-07-07 22:56:40 +0800312 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800313}
314
315/**
316 * hw_test_and_set_setup_guard: test & set setup guard (execute without
317 * interruption)
318 *
319 * This function returns guard value
320 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300321static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800322{
Richard Zhao26c696c2012-07-07 22:56:40 +0800323 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800324}
325
326/**
327 * hw_usb_set_address: configures USB address (execute without interruption)
328 * @value: new USB address
329 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300330 * This function explicitly sets the address, without the "USBADRA" (advance)
331 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800332 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300333static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800334{
Richard Zhao26c696c2012-07-07 22:56:40 +0800335 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200336 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800337}
338
339/**
340 * hw_usb_reset: restart device after a bus reset (execute without
341 * interruption)
342 *
343 * This function returns an error code
344 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300345static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800346{
Richard Zhao26c696c2012-07-07 22:56:40 +0800347 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800348
349 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800350 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800351
352 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800353 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800354
355 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800356 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800357
358 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800359 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800360 udelay(10); /* not RTOS friendly */
361
362 /* reset all endpoints ? */
363
364 /* reset internal status and wait for further instructions
365 no need to verify the port reset status (ESS does it) */
366
367 return 0;
368}
369
370/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800371 * UTIL block
372 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300373
Alexander Shishkin8e229782013-06-24 14:46:36 +0300374static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300375 unsigned length)
376{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300377 int i;
378 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300379 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
380 GFP_ATOMIC);
381
382 if (node == NULL)
383 return -ENOMEM;
384
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300385 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300386 &node->dma);
387 if (node->ptr == NULL) {
388 kfree(node);
389 return -ENOMEM;
390 }
391
Alexander Shishkin8e229782013-06-24 14:46:36 +0300392 memset(node->ptr, 0, sizeof(struct ci_hw_td));
Michael Grzeschik2e270412013-06-13 17:59:54 +0300393 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
394 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
395 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
396
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300397 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300398 if (length) {
399 node->ptr->page[0] = cpu_to_le32(temp);
400 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300401 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300402 page &= ~TD_RESERVED_MASK;
403 node->ptr->page[i] = cpu_to_le32(page);
404 }
405 }
406
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300407 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300408
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300409 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300410 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300411 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300412 struct td_node, td);
413 lastnode->ptr->next = cpu_to_le32(node->dma);
414 }
415
416 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300417 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300418
419 return 0;
420}
421
David Lopoaa69a802008-11-17 14:14:51 -0800422/**
423 * _usb_addr: calculates endpoint address from direction & number
424 * @ep: endpoint
425 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300426static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800427{
428 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
429}
430
431/**
432 * _hardware_queue: configures a request at hardware level
433 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300434 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800435 *
436 * This function returns an error code
437 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300438static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800439{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300440 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530441 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300442 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300443 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300444 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800445
David Lopoaa69a802008-11-17 14:14:51 -0800446 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300447 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800448 return -EALREADY;
449
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300450 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800451
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300452 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300453 if (ret)
454 return ret;
455
Michael Grzeschik2e270412013-06-13 17:59:54 +0300456 /*
457 * The first buffer could be not page aligned.
458 * In that case we have to span into one extra td.
459 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300460 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300461 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300462
Michael Grzeschik2e270412013-06-13 17:59:54 +0300463 if (rest == 0)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300464 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300465
Michael Grzeschik2e270412013-06-13 17:59:54 +0300466 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300467 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300468 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300469 add_td_to_list(hwep, hwreq, count);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300470 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200471 }
David Lopoaa69a802008-11-17 14:14:51 -0800472
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300473 if (hwreq->req.zero && hwreq->req.length
474 && (hwreq->req.length % hwep->ep.maxpacket == 0))
475 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300476
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300477 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300478
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300479 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300480 struct td_node, td);
481
482 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300483 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300484 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300485 wmb();
486
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300487 hwreq->req.actual = 0;
488 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300489 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300490 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530491 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300492 struct td_node *prevlastnode;
493 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530494
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300495 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300496 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300497 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300498 struct td_node, td);
499
500 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530501 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800502 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530503 goto done;
504 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800505 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
506 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
507 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
508 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530509 if (tmp_stat)
510 goto done;
511 }
512
513 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300514 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
515 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200516 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800517
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300518 if (hwep->type == USB_ENDPOINT_XFER_ISOC) {
519 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300520
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300521 if (hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300522 mul++;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300523 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300524 }
525
David Lopoaa69a802008-11-17 14:14:51 -0800526 wmb(); /* synchronize before ep prime */
527
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300528 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
529 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530530done:
531 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800532}
533
Michael Grzeschik2e270412013-06-13 17:59:54 +0300534/*
535 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300536 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300537 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300538static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300539{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300540 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300541
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300542 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
543 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300544 kfree(pending);
545}
546
David Lopoaa69a802008-11-17 14:14:51 -0800547/**
548 * _hardware_dequeue: handles a request at hardware level
549 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300550 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800551 *
552 * This function returns an error code
553 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300554static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800555{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300556 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300557 struct td_node *node, *tmpnode;
558 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300559 unsigned actual = hwreq->req.length;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200560
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300561 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800562 return -EINVAL;
563
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300564 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530565
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300566 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300567 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300568 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300569 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530570 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300571 }
David Lopoaa69a802008-11-17 14:14:51 -0800572
Michael Grzeschik2e270412013-06-13 17:59:54 +0300573 remaining_length = (tmptoken & TD_TOTAL_BYTES);
574 remaining_length >>= __ffs(TD_TOTAL_BYTES);
575 actual -= remaining_length;
576
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300577 hwreq->req.status = tmptoken & TD_STATUS;
578 if ((TD_STATUS_HALTED & hwreq->req.status)) {
579 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300580 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300581 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
582 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300583 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300584 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
585 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300586 break;
587 }
588
589 if (remaining_length) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300590 if (hwep->dir) {
591 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300592 break;
593 }
594 }
595 /*
596 * As the hardware could still address the freed td
597 * which will run the udc unusable, the cleanup of the
598 * td has to be delayed by one.
599 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300600 if (hwep->pending_td)
601 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300602
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300603 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300604 list_del_init(&node->td);
605 }
David Lopoaa69a802008-11-17 14:14:51 -0800606
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300607 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800608
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300609 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800610
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300611 if (hwreq->req.status)
612 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800613
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300614 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800615}
616
617/**
618 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300619 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800620 *
621 * This function returns an error code
622 * Caller must hold lock
623 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300624static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300625__releases(hwep->lock)
626__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800627{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300628 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300629 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800630 return -EINVAL;
631
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300632 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800633
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300634 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800635
636 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300637 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
638 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300639
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300640 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
641 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300642 list_del_init(&node->td);
643 node->ptr = NULL;
644 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300645 }
646
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300647 list_del_init(&hwreq->queue);
648 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800649
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300650 if (hwreq->req.complete != NULL) {
651 spin_unlock(hwep->lock);
652 hwreq->req.complete(&hwep->ep, &hwreq->req);
653 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800654 }
655 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300656
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300657 if (hwep->pending_td)
658 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300659
David Lopoaa69a802008-11-17 14:14:51 -0800660 return 0;
661}
662
663/**
664 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
665 * @gadget: gadget
666 *
667 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800668 */
669static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800670{
671 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300672 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530673 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800674
Richard Zhao26c696c2012-07-07 22:56:40 +0800675 spin_lock_irqsave(&ci->lock, flags);
676 ci->gadget.speed = USB_SPEED_UNKNOWN;
677 ci->remote_wakeup = 0;
678 ci->suspended = 0;
679 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530680
David Lopoaa69a802008-11-17 14:14:51 -0800681 /* flush all endpoints */
682 gadget_for_each_ep(ep, gadget) {
683 usb_ep_fifo_flush(ep);
684 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800685 usb_ep_fifo_flush(&ci->ep0out->ep);
686 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800687
David Lopoaa69a802008-11-17 14:14:51 -0800688 /* make sure to disable all endpoints */
689 gadget_for_each_ep(ep, gadget) {
690 usb_ep_disable(ep);
691 }
David Lopoaa69a802008-11-17 14:14:51 -0800692
Richard Zhao26c696c2012-07-07 22:56:40 +0800693 if (ci->status != NULL) {
694 usb_ep_free_request(&ci->ep0in->ep, ci->status);
695 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800696 }
697
David Lopoaa69a802008-11-17 14:14:51 -0800698 return 0;
699}
700
701/******************************************************************************
702 * ISR block
703 *****************************************************************************/
704/**
705 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800706 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800707 *
708 * This function resets USB engine after a bus reset occurred
709 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300710static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800711__releases(ci->lock)
712__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800713{
David Lopoaa69a802008-11-17 14:14:51 -0800714 int retval;
715
Peter Chena3aee362013-10-09 14:39:52 +0800716 spin_unlock(&ci->lock);
Peter Chen92b336d2013-09-17 12:37:19 +0800717 if (ci->gadget.speed != USB_SPEED_UNKNOWN) {
718 if (ci->driver)
719 ci->driver->disconnect(&ci->gadget);
720 }
721
Richard Zhao26c696c2012-07-07 22:56:40 +0800722 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800723 if (retval)
724 goto done;
725
Richard Zhao26c696c2012-07-07 22:56:40 +0800726 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800727 if (retval)
728 goto done;
729
Richard Zhao26c696c2012-07-07 22:56:40 +0800730 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
731 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530732 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530733
Michael Grzeschikb9322252012-05-11 17:25:50 +0300734done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800735 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800736
David Lopoaa69a802008-11-17 14:14:51 -0800737 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800738 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800739}
740
741/**
742 * isr_get_status_complete: get_status request complete function
743 * @ep: endpoint
744 * @req: request handled
745 *
746 * Caller must release lock
747 */
748static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
749{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300750 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800751 return;
David Lopoaa69a802008-11-17 14:14:51 -0800752
753 kfree(req->buf);
754 usb_ep_free_request(ep, req);
755}
756
757/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200758 * _ep_queue: queues (submits) an I/O request to an endpoint
759 *
760 * Caller must hold lock
761 */
762static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
763 gfp_t __maybe_unused gfp_flags)
764{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300765 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
766 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
767 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200768 int retval = 0;
769
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300770 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200771 return -EINVAL;
772
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300773 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200774 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300775 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200776 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300777 if (!list_empty(&hwep->qh.queue)) {
778 _ep_nuke(hwep);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200779 retval = -EOVERFLOW;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300780 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
781 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200782 }
783 }
784
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300785 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
786 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
787 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300788 return -EMSGSIZE;
789 }
790
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200791 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300792 if (!list_empty(&hwreq->queue)) {
793 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200794 return -EBUSY;
795 }
796
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200797 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300798 hwreq->req.status = -EINPROGRESS;
799 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200800
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300801 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200802
803 if (retval == -EALREADY)
804 retval = 0;
805 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300806 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200807
808 return retval;
809}
810
811/**
David Lopoaa69a802008-11-17 14:14:51 -0800812 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800813 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800814 * @setup: setup request packet
815 *
816 * This function returns an error code
817 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300818static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800819 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300820__releases(hwep->lock)
821__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800822{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300823 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800824 struct usb_request *req = NULL;
825 gfp_t gfp_flags = GFP_ATOMIC;
826 int dir, num, retval;
827
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300828 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800829 return -EINVAL;
830
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300831 spin_unlock(hwep->lock);
832 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
833 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800834 if (req == NULL)
835 return -ENOMEM;
836
837 req->complete = isr_get_status_complete;
838 req->length = 2;
839 req->buf = kzalloc(req->length, gfp_flags);
840 if (req->buf == NULL) {
841 retval = -ENOMEM;
842 goto err_free_req;
843 }
844
845 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530846 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800847 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800848 retval = 0;
849 } else if ((setup->bRequestType & USB_RECIP_MASK) \
850 == USB_RECIP_ENDPOINT) {
851 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
852 TX : RX;
853 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800854 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800855 }
856 /* else do nothing; reserved for future use */
857
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300858 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800859 if (retval)
860 goto err_free_buf;
861
862 return 0;
863
864 err_free_buf:
865 kfree(req->buf);
866 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300867 spin_unlock(hwep->lock);
868 usb_ep_free_request(&hwep->ep, req);
869 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800870 return retval;
871}
872
873/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530874 * isr_setup_status_complete: setup_status request complete function
875 * @ep: endpoint
876 * @req: request handled
877 *
878 * Caller must release lock. Put the port in test mode if test mode
879 * feature is selected.
880 */
881static void
882isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
883{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300884 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530885 unsigned long flags;
886
Richard Zhao26c696c2012-07-07 22:56:40 +0800887 if (ci->setaddr) {
888 hw_usb_set_address(ci, ci->address);
889 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300890 }
891
Richard Zhao26c696c2012-07-07 22:56:40 +0800892 spin_lock_irqsave(&ci->lock, flags);
893 if (ci->test_mode)
894 hw_port_test_set(ci, ci->test_mode);
895 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530896}
897
898/**
David Lopoaa69a802008-11-17 14:14:51 -0800899 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800900 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800901 *
902 * This function returns an error code
903 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300904static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800905{
906 int retval;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300907 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800908
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300909 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800910 ci->status->context = ci;
911 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800912
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300913 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800914
915 return retval;
916}
917
918/**
919 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300920 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800921 *
922 * This function returns an error code
923 * Caller must hold lock
924 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300925static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300926__releases(hwep->lock)
927__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800928{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300929 struct ci_hw_req *hwreq, *hwreqtemp;
930 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300931 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800932
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300933 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530934 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300935 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530936 if (retval < 0)
937 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300938 list_del_init(&hwreq->queue);
939 if (hwreq->req.complete != NULL) {
940 spin_unlock(hwep->lock);
941 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
942 hwreq->req.length)
943 hweptemp = hwep->ci->ep0in;
944 hwreq->req.complete(&hweptemp->ep, &hwreq->req);
945 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530946 }
947 }
David Lopoaa69a802008-11-17 14:14:51 -0800948
Pavankumar Kondetief907482011-05-02 11:56:27 +0530949 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530950 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800951
David Lopoaa69a802008-11-17 14:14:51 -0800952 return retval;
953}
954
955/**
956 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800957 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800958 *
959 * This function handles traffic events
960 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300961static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800962__releases(ci->lock)
963__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800964{
965 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530966 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800967
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300969 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530970 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800971 struct usb_ctrlrequest req;
972
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300973 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800974 continue; /* not configured */
975
Richard Zhao26c696c2012-07-07 22:56:40 +0800976 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300977 err = isr_tr_complete_low(hwep);
978 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -0800979 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800980 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800981 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800982 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300983 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800984 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700985 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800986 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800987 }
988 }
989 }
990
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300991 if (hwep->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800992 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800993 continue;
994
995 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800996 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800997 continue;
998 }
999
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301000 /*
1001 * Flush data and handshake transactions of previous
1002 * setup packet.
1003 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001004 _ep_nuke(ci->ep0out);
1005 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301006
David Lopoaa69a802008-11-17 14:14:51 -08001007 /* read_setup_packet */
1008 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001009 hw_test_and_set_setup_guard(ci);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001010 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +08001011 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -08001012
1013 type = req.bRequestType;
1014
Richard Zhao26c696c2012-07-07 22:56:40 +08001015 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -08001016
David Lopoaa69a802008-11-17 14:14:51 -08001017 switch (req.bRequest) {
1018 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301019 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1020 le16_to_cpu(req.wValue) ==
1021 USB_ENDPOINT_HALT) {
1022 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -08001023 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301024 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301025 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301026 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301027 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001028 num += ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001029 if (!ci->ci_hw_ep[num].wedge) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001030 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301031 err = usb_ep_clear_halt(
Alexander Shishkin8e229782013-06-24 14:46:36 +03001032 &ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001033 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301034 if (err)
1035 break;
1036 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001037 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301038 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1039 le16_to_cpu(req.wValue) ==
1040 USB_DEVICE_REMOTE_WAKEUP) {
1041 if (req.wLength != 0)
1042 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001043 ci->remote_wakeup = 0;
1044 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301045 } else {
1046 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -08001047 }
David Lopoaa69a802008-11-17 14:14:51 -08001048 break;
1049 case USB_REQ_GET_STATUS:
1050 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1051 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1052 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1053 goto delegate;
1054 if (le16_to_cpu(req.wLength) != 2 ||
1055 le16_to_cpu(req.wValue) != 0)
1056 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001057 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -08001058 break;
1059 case USB_REQ_SET_ADDRESS:
1060 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1061 goto delegate;
1062 if (le16_to_cpu(req.wLength) != 0 ||
1063 le16_to_cpu(req.wIndex) != 0)
1064 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001065 ci->address = (u8)le16_to_cpu(req.wValue);
1066 ci->setaddr = true;
1067 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001068 break;
1069 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301070 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1071 le16_to_cpu(req.wValue) ==
1072 USB_ENDPOINT_HALT) {
1073 if (req.wLength != 0)
1074 break;
1075 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301076 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301077 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301078 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001079 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -08001080
Richard Zhao26c696c2012-07-07 22:56:40 +08001081 spin_unlock(&ci->lock);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001082 err = usb_ep_set_halt(&ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001083 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301084 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +08001085 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301086 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301087 if (req.wLength != 0)
1088 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301089 switch (le16_to_cpu(req.wValue)) {
1090 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +08001091 ci->remote_wakeup = 1;
1092 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301093 break;
1094 case USB_DEVICE_TEST_MODE:
1095 tmode = le16_to_cpu(req.wIndex) >> 8;
1096 switch (tmode) {
1097 case TEST_J:
1098 case TEST_K:
1099 case TEST_SE0_NAK:
1100 case TEST_PACKET:
1101 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +08001102 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301103 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +08001104 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301105 break;
1106 default:
1107 break;
1108 }
1109 default:
1110 goto delegate;
1111 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301112 } else {
1113 goto delegate;
1114 }
David Lopoaa69a802008-11-17 14:14:51 -08001115 break;
1116 default:
1117delegate:
1118 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001119 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001120
Richard Zhao26c696c2012-07-07 22:56:40 +08001121 spin_unlock(&ci->lock);
1122 err = ci->driver->setup(&ci->gadget, &req);
1123 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001124 break;
1125 }
1126
1127 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001128 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001129 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001130 dev_err(ci->dev, "error: ep_set_halt\n");
1131 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001132 }
1133 }
1134}
1135
1136/******************************************************************************
1137 * ENDPT block
1138 *****************************************************************************/
1139/**
1140 * ep_enable: configure endpoint, making it usable
1141 *
1142 * Check usb_ep_enable() at "usb_gadget.h" for details
1143 */
1144static int ep_enable(struct usb_ep *ep,
1145 const struct usb_endpoint_descriptor *desc)
1146{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001147 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301148 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001149 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001150 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001151
David Lopoaa69a802008-11-17 14:14:51 -08001152 if (ep == NULL || desc == NULL)
1153 return -EINVAL;
1154
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001155 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001156
1157 /* only internal SW should enable ctrl endpts */
1158
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001159 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001160
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001161 if (!list_empty(&hwep->qh.queue))
1162 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001163
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001164 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1165 hwep->num = usb_endpoint_num(desc);
1166 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001167
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001168 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1169 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001170
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001171 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001172 cap |= QH_IOS;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001173 if (hwep->num)
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001174 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001175 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001176
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001177 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001178
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001179 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001180
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301181 /*
1182 * Enable endpoints in the HW other than ep0 as ep0
1183 * is always enabled
1184 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001185 if (hwep->num)
1186 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1187 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001188
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001189 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001190 return retval;
1191}
1192
1193/**
1194 * ep_disable: endpoint is no longer usable
1195 *
1196 * Check usb_ep_disable() at "usb_gadget.h" for details
1197 */
1198static int ep_disable(struct usb_ep *ep)
1199{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001200 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001201 int direction, retval = 0;
1202 unsigned long flags;
1203
David Lopoaa69a802008-11-17 14:14:51 -08001204 if (ep == NULL)
1205 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001206 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001207 return -EBUSY;
1208
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001209 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001210
1211 /* only internal SW should disable ctrl endpts */
1212
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001213 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001214 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001215 retval |= _ep_nuke(hwep);
1216 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001217
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001218 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1219 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001220
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001221 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001222
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001223 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001224
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001225 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001226 return retval;
1227}
1228
1229/**
1230 * ep_alloc_request: allocate a request object to use with this endpoint
1231 *
1232 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1233 */
1234static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1235{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001236 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001237
Alexander Shishkin0f089092012-05-08 23:29:02 +03001238 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001239 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001240
Alexander Shishkin8e229782013-06-24 14:46:36 +03001241 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001242 if (hwreq != NULL) {
1243 INIT_LIST_HEAD(&hwreq->queue);
1244 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001245 }
1246
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001247 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001248}
1249
1250/**
1251 * ep_free_request: frees a request object
1252 *
1253 * Check usb_ep_free_request() at "usb_gadget.h" for details
1254 */
1255static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1256{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001257 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1258 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001259 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001260 unsigned long flags;
1261
David Lopoaa69a802008-11-17 14:14:51 -08001262 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001263 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001264 } else if (!list_empty(&hwreq->queue)) {
1265 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001266 return;
1267 }
1268
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001269 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001270
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001271 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1272 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001273 list_del_init(&node->td);
1274 node->ptr = NULL;
1275 kfree(node);
1276 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001277
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001278 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001279
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001280 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001281}
1282
1283/**
1284 * ep_queue: queues (submits) an I/O request to an endpoint
1285 *
1286 * Check usb_ep_queue()* at usb_gadget.h" for details
1287 */
1288static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1289 gfp_t __maybe_unused gfp_flags)
1290{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001291 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001292 int retval = 0;
1293 unsigned long flags;
1294
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001295 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001296 return -EINVAL;
1297
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001298 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001299 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001300 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001301 return retval;
1302}
1303
1304/**
1305 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1306 *
1307 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1308 */
1309static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1310{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001311 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1312 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001313 unsigned long flags;
1314
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001315 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1316 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1317 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001318 return -EINVAL;
1319
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001320 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001321
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001322 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001323
1324 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001325 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001326
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001327 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001328
David Lopoaa69a802008-11-17 14:14:51 -08001329 req->status = -ECONNRESET;
1330
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001331 if (hwreq->req.complete != NULL) {
1332 spin_unlock(hwep->lock);
1333 hwreq->req.complete(&hwep->ep, &hwreq->req);
1334 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001335 }
1336
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001337 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001338 return 0;
1339}
1340
1341/**
1342 * ep_set_halt: sets the endpoint halt feature
1343 *
1344 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1345 */
1346static int ep_set_halt(struct usb_ep *ep, int value)
1347{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001348 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001349 int direction, retval = 0;
1350 unsigned long flags;
1351
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001352 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001353 return -EINVAL;
1354
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001355 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001356 return -EOPNOTSUPP;
1357
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001358 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001359
1360#ifndef STALL_IN
1361 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001362 if (value && hwep->type == USB_ENDPOINT_XFER_BULK && hwep->dir == TX &&
1363 !list_empty(&hwep->qh.queue)) {
1364 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001365 return -EAGAIN;
1366 }
1367#endif
1368
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001369 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001370 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001371 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001372
1373 if (!value)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001374 hwep->wedge = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001375
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001376 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1377 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001378
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001379 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001380
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001381 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001382 return retval;
1383}
1384
1385/**
1386 * ep_set_wedge: sets the halt feature and ignores clear requests
1387 *
1388 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1389 */
1390static int ep_set_wedge(struct usb_ep *ep)
1391{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001392 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001393 unsigned long flags;
1394
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001395 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001396 return -EINVAL;
1397
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001398 spin_lock_irqsave(hwep->lock, flags);
1399 hwep->wedge = 1;
1400 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001401
1402 return usb_ep_set_halt(ep);
1403}
1404
1405/**
1406 * ep_fifo_flush: flushes contents of a fifo
1407 *
1408 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1409 */
1410static void ep_fifo_flush(struct usb_ep *ep)
1411{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001412 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001413 unsigned long flags;
1414
David Lopoaa69a802008-11-17 14:14:51 -08001415 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001416 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001417 return;
1418 }
1419
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001420 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001421
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001422 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001423
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001424 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001425}
1426
1427/**
1428 * Endpoint-specific part of the API to the USB controller hardware
1429 * Check "usb_gadget.h" for details
1430 */
1431static const struct usb_ep_ops usb_ep_ops = {
1432 .enable = ep_enable,
1433 .disable = ep_disable,
1434 .alloc_request = ep_alloc_request,
1435 .free_request = ep_free_request,
1436 .queue = ep_queue,
1437 .dequeue = ep_dequeue,
1438 .set_halt = ep_set_halt,
1439 .set_wedge = ep_set_wedge,
1440 .fifo_flush = ep_fifo_flush,
1441};
1442
1443/******************************************************************************
1444 * GADGET block
1445 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001446static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301447{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001448 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301449 unsigned long flags;
1450 int gadget_ready = 0;
1451
Richard Zhao26c696c2012-07-07 22:56:40 +08001452 spin_lock_irqsave(&ci->lock, flags);
1453 ci->vbus_active = is_active;
1454 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301455 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001456 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301457
1458 if (gadget_ready) {
1459 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301460 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001461 hw_device_reset(ci, USBMODE_CM_DC);
1462 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chena107f8c2013-08-14 12:44:11 +03001463 dev_dbg(ci->dev, "Connected to host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301464 } else {
Peter Chen92b336d2013-09-17 12:37:19 +08001465 if (ci->driver)
1466 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001467 hw_device_state(ci, 0);
1468 if (ci->platdata->notify_event)
1469 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001470 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001471 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301472 pm_runtime_put_sync(&_gadget->dev);
Peter Chena107f8c2013-08-14 12:44:11 +03001473 dev_dbg(ci->dev, "Disconnected from host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301474 }
1475 }
1476
1477 return 0;
1478}
1479
Alexander Shishkin8e229782013-06-24 14:46:36 +03001480static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301481{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001482 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301483 unsigned long flags;
1484 int ret = 0;
1485
Richard Zhao26c696c2012-07-07 22:56:40 +08001486 spin_lock_irqsave(&ci->lock, flags);
1487 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301488 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301489 goto out;
1490 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001491 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301492 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301493 goto out;
1494 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001495 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301496out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001497 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301498 return ret;
1499}
1500
Alexander Shishkin8e229782013-06-24 14:46:36 +03001501static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301502{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001503 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301504
Richard Zhao26c696c2012-07-07 22:56:40 +08001505 if (ci->transceiver)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001506 return usb_phy_set_power(ci->transceiver, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301507 return -ENOTSUPP;
1508}
1509
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001510/* Change Data+ pullup status
1511 * this func is used by usb_gadget_connect/disconnet
1512 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001513static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001514{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001515 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001516
Peter Chen4a647832013-08-14 12:44:15 +03001517 if (!ci->vbus_active)
1518 return -EOPNOTSUPP;
1519
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001520 if (is_on)
1521 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1522 else
1523 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1524
1525 return 0;
1526}
1527
Alexander Shishkin8e229782013-06-24 14:46:36 +03001528static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001529 struct usb_gadget_driver *driver);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001530static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001531 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001532/**
1533 * Device operations part of the API to the USB controller hardware,
1534 * which don't involve endpoints (or i/o)
1535 * Check "usb_gadget.h" for details
1536 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301537static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001538 .vbus_session = ci_udc_vbus_session,
1539 .wakeup = ci_udc_wakeup,
1540 .pullup = ci_udc_pullup,
1541 .vbus_draw = ci_udc_vbus_draw,
1542 .udc_start = ci_udc_start,
1543 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301544};
David Lopoaa69a802008-11-17 14:14:51 -08001545
Alexander Shishkin8e229782013-06-24 14:46:36 +03001546static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001547{
1548 int retval = 0, i, j;
1549
Richard Zhao26c696c2012-07-07 22:56:40 +08001550 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001551 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001552 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001553 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001554
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001555 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001556 (j == TX) ? "in" : "out");
1557
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001558 hwep->ci = ci;
1559 hwep->lock = &ci->lock;
1560 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001561
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001562 hwep->ep.name = hwep->name;
1563 hwep->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001564 /*
1565 * for ep0: maxP defined in desc, for other
1566 * eps, maxP is set by epautoconfig() called
1567 * by gadget layer
1568 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001569 hwep->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001570
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001571 INIT_LIST_HEAD(&hwep->qh.queue);
1572 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1573 &hwep->qh.dma);
1574 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001575 retval = -ENOMEM;
1576 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001577 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001578
1579 /*
1580 * set up shorthands for ep0 out and in endpoints,
1581 * don't add to gadget's ep_list
1582 */
1583 if (i == 0) {
1584 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001585 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001586 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001587 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001588
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001589 hwep->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001590 continue;
1591 }
1592
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001593 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001594 }
1595
1596 return retval;
1597}
1598
Alexander Shishkin8e229782013-06-24 14:46:36 +03001599static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001600{
1601 int i;
1602
1603 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001604 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001605
Peter Chen4a295672013-09-10 15:34:39 +08001606 if (hwep->pending_td)
1607 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001608 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001609 }
1610}
1611
David Lopoaa69a802008-11-17 14:14:51 -08001612/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001613 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001614 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001615 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001616 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001617 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001618 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001619static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001620 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001621{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001622 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301623 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001624 int retval = -ENOMEM;
1625
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001626 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001627 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001628
David Lopoaa69a802008-11-17 14:14:51 -08001629
Richard Zhao26c696c2012-07-07 22:56:40 +08001630 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1631 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301632 if (retval)
1633 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001634
Richard Zhao26c696c2012-07-07 22:56:40 +08001635 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1636 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301637 if (retval)
1638 return retval;
David Lopoaa69a802008-11-17 14:14:51 -08001639
Richard Zhao26c696c2012-07-07 22:56:40 +08001640 ci->driver = driver;
1641 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001642 if (ci->vbus_active) {
Peter Chen65b2fb32013-10-08 10:30:17 +08001643 spin_lock_irqsave(&ci->lock, flags);
Peter Chend268e9b2013-08-14 12:44:14 +03001644 hw_device_reset(ci, USBMODE_CM_DC);
1645 } else {
1646 pm_runtime_put_sync(&ci->gadget.dev);
Peter Chen65b2fb32013-10-08 10:30:17 +08001647 return retval;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301648 }
1649
Richard Zhao26c696c2012-07-07 22:56:40 +08001650 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen65b2fb32013-10-08 10:30:17 +08001651 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301652 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001653 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001654
David Lopoaa69a802008-11-17 14:14:51 -08001655 return retval;
1656}
David Lopoaa69a802008-11-17 14:14:51 -08001657
1658/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001659 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001660 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001661static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001662 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001663{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001664 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001665 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001666
Richard Zhao26c696c2012-07-07 22:56:40 +08001667 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001668
Peter Chend268e9b2013-08-14 12:44:14 +03001669 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001670 hw_device_state(ci, 0);
1671 if (ci->platdata->notify_event)
1672 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001673 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001674 spin_unlock_irqrestore(&ci->lock, flags);
1675 _gadget_stop_activity(&ci->gadget);
1676 spin_lock_irqsave(&ci->lock, flags);
1677 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301678 }
David Lopoaa69a802008-11-17 14:14:51 -08001679
Peter Chenf84839d2013-09-17 12:37:20 +08001680 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001681 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001682
David Lopoaa69a802008-11-17 14:14:51 -08001683 return 0;
1684}
David Lopoaa69a802008-11-17 14:14:51 -08001685
1686/******************************************************************************
1687 * BUS block
1688 *****************************************************************************/
1689/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001690 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001691 *
1692 * This function returns IRQ_HANDLED if the IRQ has been handled
1693 * It locks access to registers
1694 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001695static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001696{
David Lopoaa69a802008-11-17 14:14:51 -08001697 irqreturn_t retval;
1698 u32 intr;
1699
Richard Zhao26c696c2012-07-07 22:56:40 +08001700 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001701 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001702
Richard Zhao26c696c2012-07-07 22:56:40 +08001703 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301704
Alexander Shishkin8e229782013-06-24 14:46:36 +03001705 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001706 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001707 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001708 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301709 return IRQ_NONE;
1710 }
1711 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001712 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001713
Alexander Shishkine443b332012-05-11 17:25:46 +03001714 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001715 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001716 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001717 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001718
David Lopoaa69a802008-11-17 14:14:51 -08001719 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001720 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001721 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001722 if (ci->suspended && ci->driver->resume) {
1723 spin_unlock(&ci->lock);
1724 ci->driver->resume(&ci->gadget);
1725 spin_lock(&ci->lock);
1726 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301727 }
David Lopoaa69a802008-11-17 14:14:51 -08001728 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001729
1730 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001731 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001732
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301733 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001734 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1735 ci->driver->suspend) {
1736 ci->suspended = 1;
1737 spin_unlock(&ci->lock);
1738 ci->driver->suspend(&ci->gadget);
1739 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301740 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301741 }
David Lopoaa69a802008-11-17 14:14:51 -08001742 retval = IRQ_HANDLED;
1743 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001744 retval = IRQ_NONE;
1745 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001747
1748 return retval;
1749}
1750
1751/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001752 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001753 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001754 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001755static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001756{
Richard Zhao26c696c2012-07-07 22:56:40 +08001757 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001758 int retval = 0;
1759
Richard Zhao26c696c2012-07-07 22:56:40 +08001760 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001761
Richard Zhao26c696c2012-07-07 22:56:40 +08001762 ci->gadget.ops = &usb_gadget_ops;
1763 ci->gadget.speed = USB_SPEED_UNKNOWN;
1764 ci->gadget.max_speed = USB_SPEED_HIGH;
1765 ci->gadget.is_otg = 0;
1766 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001767
Richard Zhao26c696c2012-07-07 22:56:40 +08001768 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001769
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001770 /* alloc resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001771 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1772 sizeof(struct ci_hw_qh),
1773 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001774 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001775 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001776
Alexander Shishkin8e229782013-06-24 14:46:36 +03001777 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1778 sizeof(struct ci_hw_td),
1779 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001780 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001781 retval = -ENOMEM;
1782 goto free_qh_pool;
1783 }
1784
Richard Zhao26c696c2012-07-07 22:56:40 +08001785 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001786 if (retval)
1787 goto free_pools;
1788
Richard Zhao26c696c2012-07-07 22:56:40 +08001789 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301790
Richard Zhao26c696c2012-07-07 22:56:40 +08001791 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001792 if (retval)
Peter Chen74475ed2013-09-24 12:47:53 +08001793 goto destroy_eps;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001794
Richard Zhao26c696c2012-07-07 22:56:40 +08001795 pm_runtime_no_callbacks(&ci->gadget.dev);
1796 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001797
Peter Chena107f8c2013-08-14 12:44:11 +03001798 /* Update ci->vbus_active */
1799 ci_handle_vbus_change(ci);
1800
David Lopoaa69a802008-11-17 14:14:51 -08001801 return retval;
1802
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001803destroy_eps:
1804 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001805free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001806 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001807free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001808 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001809 return retval;
1810}
1811
1812/**
Peter Chen3f124d22013-08-14 12:44:07 +03001813 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001814 *
1815 * No interrupts active, the IRQ has been released
1816 */
Peter Chen3f124d22013-08-14 12:44:07 +03001817void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001818{
Peter Chen3f124d22013-08-14 12:44:07 +03001819 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001820 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001821
Richard Zhao26c696c2012-07-07 22:56:40 +08001822 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001823
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001824 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001825
Richard Zhao26c696c2012-07-07 22:56:40 +08001826 dma_pool_destroy(ci->td_pool);
1827 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001828
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001829 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001830 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001831 if (ci->global_phy)
1832 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301833 }
Peter Chen3f124d22013-08-14 12:44:07 +03001834}
1835
1836static int udc_id_switch_for_device(struct ci_hdrc *ci)
1837{
1838 if (ci->is_otg) {
1839 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1840 ci_enable_otg_interrupt(ci, OTGSC_BSVIE);
1841 }
1842
1843 return 0;
1844}
1845
1846static void udc_id_switch_for_host(struct ci_hdrc *ci)
1847{
1848 if (ci->is_otg) {
1849 /* host doesn't care B_SESSION_VALID event */
1850 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1851 ci_disable_otg_interrupt(ci, OTGSC_BSVIE);
1852 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001853}
David Lopoaa69a802008-11-17 14:14:51 -08001854
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001855/**
1856 * ci_hdrc_gadget_init - initialize device related bits
1857 * ci: the controller
1858 *
Peter Chen3f124d22013-08-14 12:44:07 +03001859 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001860 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001861int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001862{
1863 struct ci_role_driver *rdrv;
1864
1865 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1866 return -ENXIO;
1867
1868 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1869 if (!rdrv)
1870 return -ENOMEM;
1871
Peter Chen3f124d22013-08-14 12:44:07 +03001872 rdrv->start = udc_id_switch_for_device;
1873 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001874 rdrv->irq = udc_irq;
1875 rdrv->name = "gadget";
1876 ci->roles[CI_ROLE_GADGET] = rdrv;
1877
Peter Chen3f124d22013-08-14 12:44:07 +03001878 return udc_start(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001879}