blob: b157c95f7a36d0bf8f8e5b2f72ae66c3deadc794 [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>
Pavankumar Kondetif01ef572010-12-07 17:54:02 +053023#include <linux/usb/otg.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030024#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080025
Alexander Shishkine443b332012-05-11 17:25:46 +030026#include "ci.h"
27#include "udc.h"
28#include "bits.h"
29#include "debug.h"
Peter Chen3f124d22013-08-14 12:44:07 +030030#include "otg.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020031
David Lopoaa69a802008-11-17 14:14:51 -080032/* control endpoint description */
33static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053034ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080035 .bLength = USB_DT_ENDPOINT_SIZE,
36 .bDescriptorType = USB_DT_ENDPOINT,
37
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053038 .bEndpointAddress = USB_DIR_OUT,
39 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
40 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
41};
42
43static const struct usb_endpoint_descriptor
44ctrl_endpt_in_desc = {
45 .bLength = USB_DT_ENDPOINT_SIZE,
46 .bDescriptorType = USB_DT_ENDPOINT,
47
48 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080049 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
50 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
51};
52
David Lopoaa69a802008-11-17 14:14:51 -080053/**
54 * hw_ep_bit: calculates the bit number
55 * @num: endpoint number
56 * @dir: endpoint direction
57 *
58 * This function returns bit number
59 */
60static inline int hw_ep_bit(int num, int dir)
61{
62 return num + (dir ? 16 : 0);
63}
64
Alexander Shishkin8e229782013-06-24 14:46:36 +030065static inline int ep_to_bit(struct ci_hdrc *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020066{
Richard Zhao26c696c2012-07-07 22:56:40 +080067 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020068
Richard Zhao26c696c2012-07-07 22:56:40 +080069 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020070 n += fill;
71
72 return n;
73}
74
David Lopoaa69a802008-11-17 14:14:51 -080075/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030076 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080077 * @dma: 0 => disable, !0 => enable and set dma engine
78 *
79 * This function returns an error code
80 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030081static int hw_device_state(struct ci_hdrc *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080082{
83 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080084 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080085 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080086 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080087 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
Peter Chena107f8c2013-08-14 12:44:11 +030088 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
David Lopoaa69a802008-11-17 14:14:51 -080089 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080090 hw_write(ci, OP_USBINTR, ~0, 0);
Peter Chena107f8c2013-08-14 12:44:11 +030091 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
David Lopoaa69a802008-11-17 14:14:51 -080092 }
93 return 0;
94}
95
96/**
97 * hw_ep_flush: flush endpoint fifo (execute without interruption)
98 * @num: endpoint number
99 * @dir: endpoint direction
100 *
101 * This function returns an error code
102 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300103static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800104{
105 int n = hw_ep_bit(num, dir);
106
107 do {
108 /* flush any pending transfer */
Richard Zhao26c696c2012-07-07 22:56:40 +0800109 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
110 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800111 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800112 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800113
114 return 0;
115}
116
117/**
118 * hw_ep_disable: disables endpoint (execute without interruption)
119 * @num: endpoint number
120 * @dir: endpoint direction
121 *
122 * This function returns an error code
123 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300124static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800125{
Richard Zhao26c696c2012-07-07 22:56:40 +0800126 hw_ep_flush(ci, num, dir);
127 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300128 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800129 return 0;
130}
131
132/**
133 * hw_ep_enable: enables endpoint (execute without interruption)
134 * @num: endpoint number
135 * @dir: endpoint direction
136 * @type: endpoint type
137 *
138 * This function returns an error code
139 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300140static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800141{
142 u32 mask, data;
143
144 if (dir) {
145 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200146 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800147
148 mask |= ENDPTCTRL_TXS; /* unstall */
149 mask |= ENDPTCTRL_TXR; /* reset data toggle */
150 data |= ENDPTCTRL_TXR;
151 mask |= ENDPTCTRL_TXE; /* enable */
152 data |= ENDPTCTRL_TXE;
153 } else {
154 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200155 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800156
157 mask |= ENDPTCTRL_RXS; /* unstall */
158 mask |= ENDPTCTRL_RXR; /* reset data toggle */
159 data |= ENDPTCTRL_RXR;
160 mask |= ENDPTCTRL_RXE; /* enable */
161 data |= ENDPTCTRL_RXE;
162 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800163 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800164 return 0;
165}
166
167/**
168 * hw_ep_get_halt: return endpoint halt status
169 * @num: endpoint number
170 * @dir: endpoint direction
171 *
172 * This function returns 1 if endpoint halted
173 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300174static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800175{
176 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
177
Richard Zhao26c696c2012-07-07 22:56:40 +0800178 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800179}
180
181/**
David Lopoaa69a802008-11-17 14:14:51 -0800182 * hw_test_and_clear_setup_status: test & clear setup status (execute without
183 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200184 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800185 *
186 * This function returns setup status
187 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300188static int hw_test_and_clear_setup_status(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800189{
Richard Zhao26c696c2012-07-07 22:56:40 +0800190 n = ep_to_bit(ci, n);
191 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800192}
193
194/**
195 * hw_ep_prime: primes endpoint (execute without interruption)
196 * @num: endpoint number
197 * @dir: endpoint direction
198 * @is_ctrl: true if control endpoint
199 *
200 * This function returns an error code
201 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300202static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800203{
204 int n = hw_ep_bit(num, dir);
205
Richard Zhao26c696c2012-07-07 22:56:40 +0800206 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800207 return -EAGAIN;
208
Richard Zhao26c696c2012-07-07 22:56:40 +0800209 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800210
Richard Zhao26c696c2012-07-07 22:56:40 +0800211 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800212 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800213 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800214 return -EAGAIN;
215
216 /* status shoult be tested according with manual but it doesn't work */
217 return 0;
218}
219
220/**
221 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
222 * without interruption)
223 * @num: endpoint number
224 * @dir: endpoint direction
225 * @value: true => stall, false => unstall
226 *
227 * This function returns an error code
228 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300229static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800230{
231 if (value != 0 && value != 1)
232 return -EINVAL;
233
234 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300235 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800236 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
237 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
238
239 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800240 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300241 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800242 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800243
244 return 0;
245}
246
247/**
David Lopoaa69a802008-11-17 14:14:51 -0800248 * hw_is_port_high_speed: test if port is high speed
249 *
250 * This function returns true if high speed port
251 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300252static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800253{
Richard Zhao26c696c2012-07-07 22:56:40 +0800254 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
255 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800256}
257
258/**
David Lopoaa69a802008-11-17 14:14:51 -0800259 * hw_read_intr_enable: returns interrupt enable register
260 *
261 * This function returns register data
262 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300263static u32 hw_read_intr_enable(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800264{
Richard Zhao26c696c2012-07-07 22:56:40 +0800265 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800266}
267
268/**
269 * hw_read_intr_status: returns interrupt status register
270 *
271 * This function returns register data
272 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300273static u32 hw_read_intr_status(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800274{
Richard Zhao26c696c2012-07-07 22:56:40 +0800275 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800276}
277
278/**
David Lopoaa69a802008-11-17 14:14:51 -0800279 * hw_test_and_clear_complete: test & clear complete status (execute without
280 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200281 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800282 *
283 * This function returns complete status
284 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300285static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800286{
Richard Zhao26c696c2012-07-07 22:56:40 +0800287 n = ep_to_bit(ci, n);
288 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800289}
290
291/**
292 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
293 * without interruption)
294 *
295 * This function returns active interrutps
296 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300297static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800298{
Richard Zhao26c696c2012-07-07 22:56:40 +0800299 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800300
Richard Zhao26c696c2012-07-07 22:56:40 +0800301 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800302 return reg;
303}
304
305/**
306 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
307 * interruption)
308 *
309 * This function returns guard value
310 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300311static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800312{
Richard Zhao26c696c2012-07-07 22:56:40 +0800313 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800314}
315
316/**
317 * hw_test_and_set_setup_guard: test & set setup guard (execute without
318 * interruption)
319 *
320 * This function returns guard value
321 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300322static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800323{
Richard Zhao26c696c2012-07-07 22:56:40 +0800324 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800325}
326
327/**
328 * hw_usb_set_address: configures USB address (execute without interruption)
329 * @value: new USB address
330 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300331 * This function explicitly sets the address, without the "USBADRA" (advance)
332 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800333 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300334static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800335{
Richard Zhao26c696c2012-07-07 22:56:40 +0800336 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200337 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800338}
339
340/**
341 * hw_usb_reset: restart device after a bus reset (execute without
342 * interruption)
343 *
344 * This function returns an error code
345 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300346static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800347{
Richard Zhao26c696c2012-07-07 22:56:40 +0800348 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800349
350 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800351 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800352
353 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800354 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800355
356 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800357 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800358
359 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800360 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800361 udelay(10); /* not RTOS friendly */
362
363 /* reset all endpoints ? */
364
365 /* reset internal status and wait for further instructions
366 no need to verify the port reset status (ESS does it) */
367
368 return 0;
369}
370
371/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800372 * UTIL block
373 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300374
Alexander Shishkin8e229782013-06-24 14:46:36 +0300375static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300376 unsigned length)
377{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300378 int i;
379 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300380 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
381 GFP_ATOMIC);
382
383 if (node == NULL)
384 return -ENOMEM;
385
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300386 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300387 &node->dma);
388 if (node->ptr == NULL) {
389 kfree(node);
390 return -ENOMEM;
391 }
392
Alexander Shishkin8e229782013-06-24 14:46:36 +0300393 memset(node->ptr, 0, sizeof(struct ci_hw_td));
Michael Grzeschik2e270412013-06-13 17:59:54 +0300394 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
395 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
396 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
397
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300398 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300399 if (length) {
400 node->ptr->page[0] = cpu_to_le32(temp);
401 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300402 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300403 page &= ~TD_RESERVED_MASK;
404 node->ptr->page[i] = cpu_to_le32(page);
405 }
406 }
407
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300408 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300409
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300410 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300411 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300412 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300413 struct td_node, td);
414 lastnode->ptr->next = cpu_to_le32(node->dma);
415 }
416
417 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300418 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300419
420 return 0;
421}
422
David Lopoaa69a802008-11-17 14:14:51 -0800423/**
424 * _usb_addr: calculates endpoint address from direction & number
425 * @ep: endpoint
426 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300427static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800428{
429 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
430}
431
432/**
433 * _hardware_queue: configures a request at hardware level
434 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300435 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800436 *
437 * This function returns an error code
438 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300439static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800440{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300441 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530442 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300443 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300444 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300445 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800446
David Lopoaa69a802008-11-17 14:14:51 -0800447 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300448 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800449 return -EALREADY;
450
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300451 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800452
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300453 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300454 if (ret)
455 return ret;
456
Michael Grzeschik2e270412013-06-13 17:59:54 +0300457 /*
458 * The first buffer could be not page aligned.
459 * In that case we have to span into one extra td.
460 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300461 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300462 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300463
Michael Grzeschik2e270412013-06-13 17:59:54 +0300464 if (rest == 0)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300465 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300466
Michael Grzeschik2e270412013-06-13 17:59:54 +0300467 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300468 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300469 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300470 add_td_to_list(hwep, hwreq, count);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300471 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200472 }
David Lopoaa69a802008-11-17 14:14:51 -0800473
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300474 if (hwreq->req.zero && hwreq->req.length
475 && (hwreq->req.length % hwep->ep.maxpacket == 0))
476 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300477
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300478 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300479
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300480 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300481 struct td_node, td);
482
483 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300484 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300485 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300486 wmb();
487
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300488 hwreq->req.actual = 0;
489 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300490 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300491 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530492 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300493 struct td_node *prevlastnode;
494 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530495
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300496 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300497 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300498 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300499 struct td_node, td);
500
501 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530502 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800503 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530504 goto done;
505 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800506 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
507 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
508 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
509 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530510 if (tmp_stat)
511 goto done;
512 }
513
514 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300515 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
516 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200517 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800518
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300519 if (hwep->type == USB_ENDPOINT_XFER_ISOC) {
520 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300521
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300522 if (hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300523 mul++;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300524 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300525 }
526
David Lopoaa69a802008-11-17 14:14:51 -0800527 wmb(); /* synchronize before ep prime */
528
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300529 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
530 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530531done:
532 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800533}
534
Michael Grzeschik2e270412013-06-13 17:59:54 +0300535/*
536 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300537 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300538 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300539static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300540{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300541 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300542
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300543 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
544 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300545 kfree(pending);
546}
547
David Lopoaa69a802008-11-17 14:14:51 -0800548/**
549 * _hardware_dequeue: handles a request at hardware level
550 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300551 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800552 *
553 * This function returns an error code
554 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300555static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800556{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300557 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300558 struct td_node *node, *tmpnode;
559 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300560 unsigned actual = hwreq->req.length;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200561
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300562 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800563 return -EINVAL;
564
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300565 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530566
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300567 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300568 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300569 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300570 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530571 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300572 }
David Lopoaa69a802008-11-17 14:14:51 -0800573
Michael Grzeschik2e270412013-06-13 17:59:54 +0300574 remaining_length = (tmptoken & TD_TOTAL_BYTES);
575 remaining_length >>= __ffs(TD_TOTAL_BYTES);
576 actual -= remaining_length;
577
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300578 hwreq->req.status = tmptoken & TD_STATUS;
579 if ((TD_STATUS_HALTED & hwreq->req.status)) {
580 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300581 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300582 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
583 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300584 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300585 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
586 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300587 break;
588 }
589
590 if (remaining_length) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300591 if (hwep->dir) {
592 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300593 break;
594 }
595 }
596 /*
597 * As the hardware could still address the freed td
598 * which will run the udc unusable, the cleanup of the
599 * td has to be delayed by one.
600 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300601 if (hwep->pending_td)
602 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300603
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300604 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300605 list_del_init(&node->td);
606 }
David Lopoaa69a802008-11-17 14:14:51 -0800607
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300608 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800609
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300610 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800611
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300612 if (hwreq->req.status)
613 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800614
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300615 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800616}
617
618/**
619 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300620 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800621 *
622 * This function returns an error code
623 * Caller must hold lock
624 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300625static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300626__releases(hwep->lock)
627__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800628{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300629 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300630 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800631 return -EINVAL;
632
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300633 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800634
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300635 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800636
637 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300638 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
639 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300640
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300641 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
642 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300643 list_del_init(&node->td);
644 node->ptr = NULL;
645 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300646 }
647
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300648 list_del_init(&hwreq->queue);
649 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800650
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300651 if (hwreq->req.complete != NULL) {
652 spin_unlock(hwep->lock);
653 hwreq->req.complete(&hwep->ep, &hwreq->req);
654 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800655 }
656 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300657
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300658 if (hwep->pending_td)
659 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300660
David Lopoaa69a802008-11-17 14:14:51 -0800661 return 0;
662}
663
664/**
665 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
666 * @gadget: gadget
667 *
668 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800669 */
670static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800671{
672 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300673 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530674 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800675
Richard Zhao26c696c2012-07-07 22:56:40 +0800676 spin_lock_irqsave(&ci->lock, flags);
677 ci->gadget.speed = USB_SPEED_UNKNOWN;
678 ci->remote_wakeup = 0;
679 ci->suspended = 0;
680 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530681
David Lopoaa69a802008-11-17 14:14:51 -0800682 /* flush all endpoints */
683 gadget_for_each_ep(ep, gadget) {
684 usb_ep_fifo_flush(ep);
685 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800686 usb_ep_fifo_flush(&ci->ep0out->ep);
687 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800688
David Lopoaa69a802008-11-17 14:14:51 -0800689 /* make sure to disable all endpoints */
690 gadget_for_each_ep(ep, gadget) {
691 usb_ep_disable(ep);
692 }
David Lopoaa69a802008-11-17 14:14:51 -0800693
Richard Zhao26c696c2012-07-07 22:56:40 +0800694 if (ci->status != NULL) {
695 usb_ep_free_request(&ci->ep0in->ep, ci->status);
696 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800697 }
698
David Lopoaa69a802008-11-17 14:14:51 -0800699 return 0;
700}
701
702/******************************************************************************
703 * ISR block
704 *****************************************************************************/
705/**
706 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800707 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800708 *
709 * This function resets USB engine after a bus reset occurred
710 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300711static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800712__releases(ci->lock)
713__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800714{
David Lopoaa69a802008-11-17 14:14:51 -0800715 int retval;
716
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 spin_unlock(&ci->lock);
723 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800724 if (retval)
725 goto done;
726
Richard Zhao26c696c2012-07-07 22:56:40 +0800727 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800728 if (retval)
729 goto done;
730
Richard Zhao26c696c2012-07-07 22:56:40 +0800731 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
732 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530733 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530734
Michael Grzeschikb9322252012-05-11 17:25:50 +0300735done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800736 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800737
David Lopoaa69a802008-11-17 14:14:51 -0800738 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800739 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800740}
741
742/**
743 * isr_get_status_complete: get_status request complete function
744 * @ep: endpoint
745 * @req: request handled
746 *
747 * Caller must release lock
748 */
749static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
750{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300751 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800752 return;
David Lopoaa69a802008-11-17 14:14:51 -0800753
754 kfree(req->buf);
755 usb_ep_free_request(ep, req);
756}
757
758/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200759 * _ep_queue: queues (submits) an I/O request to an endpoint
760 *
761 * Caller must hold lock
762 */
763static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
764 gfp_t __maybe_unused gfp_flags)
765{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300766 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
767 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
768 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200769 int retval = 0;
770
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300771 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200772 return -EINVAL;
773
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300774 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200775 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300776 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200777 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300778 if (!list_empty(&hwep->qh.queue)) {
779 _ep_nuke(hwep);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200780 retval = -EOVERFLOW;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300781 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
782 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200783 }
784 }
785
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300786 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
787 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
788 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300789 return -EMSGSIZE;
790 }
791
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200792 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300793 if (!list_empty(&hwreq->queue)) {
794 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200795 return -EBUSY;
796 }
797
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200798 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300799 hwreq->req.status = -EINPROGRESS;
800 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200801
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300802 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200803
804 if (retval == -EALREADY)
805 retval = 0;
806 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300807 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200808
809 return retval;
810}
811
812/**
David Lopoaa69a802008-11-17 14:14:51 -0800813 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800814 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800815 * @setup: setup request packet
816 *
817 * This function returns an error code
818 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300819static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800820 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300821__releases(hwep->lock)
822__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800823{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300824 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800825 struct usb_request *req = NULL;
826 gfp_t gfp_flags = GFP_ATOMIC;
827 int dir, num, retval;
828
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300829 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800830 return -EINVAL;
831
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300832 spin_unlock(hwep->lock);
833 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
834 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800835 if (req == NULL)
836 return -ENOMEM;
837
838 req->complete = isr_get_status_complete;
839 req->length = 2;
840 req->buf = kzalloc(req->length, gfp_flags);
841 if (req->buf == NULL) {
842 retval = -ENOMEM;
843 goto err_free_req;
844 }
845
846 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530847 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800848 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800849 retval = 0;
850 } else if ((setup->bRequestType & USB_RECIP_MASK) \
851 == USB_RECIP_ENDPOINT) {
852 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
853 TX : RX;
854 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800855 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800856 }
857 /* else do nothing; reserved for future use */
858
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300859 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800860 if (retval)
861 goto err_free_buf;
862
863 return 0;
864
865 err_free_buf:
866 kfree(req->buf);
867 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300868 spin_unlock(hwep->lock);
869 usb_ep_free_request(&hwep->ep, req);
870 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800871 return retval;
872}
873
874/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530875 * isr_setup_status_complete: setup_status request complete function
876 * @ep: endpoint
877 * @req: request handled
878 *
879 * Caller must release lock. Put the port in test mode if test mode
880 * feature is selected.
881 */
882static void
883isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
884{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300885 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530886 unsigned long flags;
887
Richard Zhao26c696c2012-07-07 22:56:40 +0800888 if (ci->setaddr) {
889 hw_usb_set_address(ci, ci->address);
890 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300891 }
892
Richard Zhao26c696c2012-07-07 22:56:40 +0800893 spin_lock_irqsave(&ci->lock, flags);
894 if (ci->test_mode)
895 hw_port_test_set(ci, ci->test_mode);
896 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530897}
898
899/**
David Lopoaa69a802008-11-17 14:14:51 -0800900 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800901 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800902 *
903 * This function returns an error code
904 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300905static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800906{
907 int retval;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300908 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800909
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300910 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800911 ci->status->context = ci;
912 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800913
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300914 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800915
916 return retval;
917}
918
919/**
920 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300921 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800922 *
923 * This function returns an error code
924 * Caller must hold lock
925 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300926static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300927__releases(hwep->lock)
928__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800929{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300930 struct ci_hw_req *hwreq, *hwreqtemp;
931 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300932 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800933
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300934 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530935 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300936 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530937 if (retval < 0)
938 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300939 list_del_init(&hwreq->queue);
940 if (hwreq->req.complete != NULL) {
941 spin_unlock(hwep->lock);
942 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
943 hwreq->req.length)
944 hweptemp = hwep->ci->ep0in;
945 hwreq->req.complete(&hweptemp->ep, &hwreq->req);
946 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530947 }
948 }
David Lopoaa69a802008-11-17 14:14:51 -0800949
Pavankumar Kondetief907482011-05-02 11:56:27 +0530950 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530951 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800952
David Lopoaa69a802008-11-17 14:14:51 -0800953 return retval;
954}
955
956/**
957 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800958 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800959 *
960 * This function handles traffic events
961 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300962static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800963__releases(ci->lock)
964__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800965{
966 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530967 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800968
Richard Zhao26c696c2012-07-07 22:56:40 +0800969 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300970 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530971 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800972 struct usb_ctrlrequest req;
973
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300974 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800975 continue; /* not configured */
976
Richard Zhao26c696c2012-07-07 22:56:40 +0800977 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300978 err = isr_tr_complete_low(hwep);
979 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -0800980 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800981 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800982 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800983 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300984 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800985 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700986 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800987 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800988 }
989 }
990 }
991
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300992 if (hwep->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800993 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800994 continue;
995
996 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800997 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800998 continue;
999 }
1000
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301001 /*
1002 * Flush data and handshake transactions of previous
1003 * setup packet.
1004 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001005 _ep_nuke(ci->ep0out);
1006 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301007
David Lopoaa69a802008-11-17 14:14:51 -08001008 /* read_setup_packet */
1009 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001010 hw_test_and_set_setup_guard(ci);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001011 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +08001012 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -08001013
1014 type = req.bRequestType;
1015
Richard Zhao26c696c2012-07-07 22:56:40 +08001016 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -08001017
David Lopoaa69a802008-11-17 14:14:51 -08001018 switch (req.bRequest) {
1019 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301020 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1021 le16_to_cpu(req.wValue) ==
1022 USB_ENDPOINT_HALT) {
1023 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -08001024 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301025 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301026 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301027 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301028 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001029 num += ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001030 if (!ci->ci_hw_ep[num].wedge) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001031 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301032 err = usb_ep_clear_halt(
Alexander Shishkin8e229782013-06-24 14:46:36 +03001033 &ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001034 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301035 if (err)
1036 break;
1037 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001038 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301039 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1040 le16_to_cpu(req.wValue) ==
1041 USB_DEVICE_REMOTE_WAKEUP) {
1042 if (req.wLength != 0)
1043 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001044 ci->remote_wakeup = 0;
1045 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301046 } else {
1047 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -08001048 }
David Lopoaa69a802008-11-17 14:14:51 -08001049 break;
1050 case USB_REQ_GET_STATUS:
1051 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1052 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1053 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1054 goto delegate;
1055 if (le16_to_cpu(req.wLength) != 2 ||
1056 le16_to_cpu(req.wValue) != 0)
1057 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001058 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -08001059 break;
1060 case USB_REQ_SET_ADDRESS:
1061 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1062 goto delegate;
1063 if (le16_to_cpu(req.wLength) != 0 ||
1064 le16_to_cpu(req.wIndex) != 0)
1065 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001066 ci->address = (u8)le16_to_cpu(req.wValue);
1067 ci->setaddr = true;
1068 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001069 break;
1070 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301071 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1072 le16_to_cpu(req.wValue) ==
1073 USB_ENDPOINT_HALT) {
1074 if (req.wLength != 0)
1075 break;
1076 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301077 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301078 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301079 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001080 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -08001081
Richard Zhao26c696c2012-07-07 22:56:40 +08001082 spin_unlock(&ci->lock);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001083 err = usb_ep_set_halt(&ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001084 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301085 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +08001086 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301087 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301088 if (req.wLength != 0)
1089 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301090 switch (le16_to_cpu(req.wValue)) {
1091 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +08001092 ci->remote_wakeup = 1;
1093 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301094 break;
1095 case USB_DEVICE_TEST_MODE:
1096 tmode = le16_to_cpu(req.wIndex) >> 8;
1097 switch (tmode) {
1098 case TEST_J:
1099 case TEST_K:
1100 case TEST_SE0_NAK:
1101 case TEST_PACKET:
1102 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +08001103 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301104 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +08001105 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301106 break;
1107 default:
1108 break;
1109 }
1110 default:
1111 goto delegate;
1112 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301113 } else {
1114 goto delegate;
1115 }
David Lopoaa69a802008-11-17 14:14:51 -08001116 break;
1117 default:
1118delegate:
1119 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001120 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001121
Richard Zhao26c696c2012-07-07 22:56:40 +08001122 spin_unlock(&ci->lock);
1123 err = ci->driver->setup(&ci->gadget, &req);
1124 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001125 break;
1126 }
1127
1128 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001129 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001130 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001131 dev_err(ci->dev, "error: ep_set_halt\n");
1132 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001133 }
1134 }
1135}
1136
1137/******************************************************************************
1138 * ENDPT block
1139 *****************************************************************************/
1140/**
1141 * ep_enable: configure endpoint, making it usable
1142 *
1143 * Check usb_ep_enable() at "usb_gadget.h" for details
1144 */
1145static int ep_enable(struct usb_ep *ep,
1146 const struct usb_endpoint_descriptor *desc)
1147{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001148 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301149 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001150 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001151 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001152
David Lopoaa69a802008-11-17 14:14:51 -08001153 if (ep == NULL || desc == NULL)
1154 return -EINVAL;
1155
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001156 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001157
1158 /* only internal SW should enable ctrl endpts */
1159
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001160 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001161
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001162 if (!list_empty(&hwep->qh.queue))
1163 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001164
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001165 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1166 hwep->num = usb_endpoint_num(desc);
1167 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001168
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001169 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1170 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001171
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001172 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001173 cap |= QH_IOS;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001174 if (hwep->num)
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001175 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001176 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001177
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001178 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001179
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001180 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001181
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301182 /*
1183 * Enable endpoints in the HW other than ep0 as ep0
1184 * is always enabled
1185 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001186 if (hwep->num)
1187 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1188 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001189
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001190 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001191 return retval;
1192}
1193
1194/**
1195 * ep_disable: endpoint is no longer usable
1196 *
1197 * Check usb_ep_disable() at "usb_gadget.h" for details
1198 */
1199static int ep_disable(struct usb_ep *ep)
1200{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001201 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001202 int direction, retval = 0;
1203 unsigned long flags;
1204
David Lopoaa69a802008-11-17 14:14:51 -08001205 if (ep == NULL)
1206 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001207 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001208 return -EBUSY;
1209
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001210 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001211
1212 /* only internal SW should disable ctrl endpts */
1213
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001214 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001215 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001216 retval |= _ep_nuke(hwep);
1217 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001218
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001219 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1220 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001221
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001222 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001223
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001224 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001225
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001226 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001227 return retval;
1228}
1229
1230/**
1231 * ep_alloc_request: allocate a request object to use with this endpoint
1232 *
1233 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1234 */
1235static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1236{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001237 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001238
Alexander Shishkin0f089092012-05-08 23:29:02 +03001239 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001240 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001241
Alexander Shishkin8e229782013-06-24 14:46:36 +03001242 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001243 if (hwreq != NULL) {
1244 INIT_LIST_HEAD(&hwreq->queue);
1245 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001246 }
1247
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001248 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001249}
1250
1251/**
1252 * ep_free_request: frees a request object
1253 *
1254 * Check usb_ep_free_request() at "usb_gadget.h" for details
1255 */
1256static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1257{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001258 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1259 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001260 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001261 unsigned long flags;
1262
David Lopoaa69a802008-11-17 14:14:51 -08001263 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001264 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001265 } else if (!list_empty(&hwreq->queue)) {
1266 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001267 return;
1268 }
1269
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001270 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001271
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001272 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1273 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001274 list_del_init(&node->td);
1275 node->ptr = NULL;
1276 kfree(node);
1277 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001278
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001279 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001280
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001281 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001282}
1283
1284/**
1285 * ep_queue: queues (submits) an I/O request to an endpoint
1286 *
1287 * Check usb_ep_queue()* at usb_gadget.h" for details
1288 */
1289static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1290 gfp_t __maybe_unused gfp_flags)
1291{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001292 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001293 int retval = 0;
1294 unsigned long flags;
1295
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001296 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001297 return -EINVAL;
1298
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001299 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001300 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001301 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001302 return retval;
1303}
1304
1305/**
1306 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1307 *
1308 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1309 */
1310static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1311{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001312 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1313 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001314 unsigned long flags;
1315
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001316 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1317 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1318 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001319 return -EINVAL;
1320
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001321 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001322
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001323 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001324
1325 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001326 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001327
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001328 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001329
David Lopoaa69a802008-11-17 14:14:51 -08001330 req->status = -ECONNRESET;
1331
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001332 if (hwreq->req.complete != NULL) {
1333 spin_unlock(hwep->lock);
1334 hwreq->req.complete(&hwep->ep, &hwreq->req);
1335 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001336 }
1337
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001338 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001339 return 0;
1340}
1341
1342/**
1343 * ep_set_halt: sets the endpoint halt feature
1344 *
1345 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1346 */
1347static int ep_set_halt(struct usb_ep *ep, int value)
1348{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001349 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001350 int direction, retval = 0;
1351 unsigned long flags;
1352
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001353 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001354 return -EINVAL;
1355
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001356 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001357 return -EOPNOTSUPP;
1358
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001359 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001360
1361#ifndef STALL_IN
1362 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001363 if (value && hwep->type == USB_ENDPOINT_XFER_BULK && hwep->dir == TX &&
1364 !list_empty(&hwep->qh.queue)) {
1365 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001366 return -EAGAIN;
1367 }
1368#endif
1369
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001370 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001371 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001372 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001373
1374 if (!value)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001375 hwep->wedge = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001376
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001377 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1378 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001379
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001380 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001381
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001382 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001383 return retval;
1384}
1385
1386/**
1387 * ep_set_wedge: sets the halt feature and ignores clear requests
1388 *
1389 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1390 */
1391static int ep_set_wedge(struct usb_ep *ep)
1392{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001393 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001394 unsigned long flags;
1395
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001396 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001397 return -EINVAL;
1398
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001399 spin_lock_irqsave(hwep->lock, flags);
1400 hwep->wedge = 1;
1401 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001402
1403 return usb_ep_set_halt(ep);
1404}
1405
1406/**
1407 * ep_fifo_flush: flushes contents of a fifo
1408 *
1409 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1410 */
1411static void ep_fifo_flush(struct usb_ep *ep)
1412{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001413 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001414 unsigned long flags;
1415
David Lopoaa69a802008-11-17 14:14:51 -08001416 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001417 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001418 return;
1419 }
1420
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001421 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001422
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001423 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001424
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001425 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001426}
1427
1428/**
1429 * Endpoint-specific part of the API to the USB controller hardware
1430 * Check "usb_gadget.h" for details
1431 */
1432static const struct usb_ep_ops usb_ep_ops = {
1433 .enable = ep_enable,
1434 .disable = ep_disable,
1435 .alloc_request = ep_alloc_request,
1436 .free_request = ep_free_request,
1437 .queue = ep_queue,
1438 .dequeue = ep_dequeue,
1439 .set_halt = ep_set_halt,
1440 .set_wedge = ep_set_wedge,
1441 .fifo_flush = ep_fifo_flush,
1442};
1443
1444/******************************************************************************
1445 * GADGET block
1446 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001447static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301448{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001449 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301450 unsigned long flags;
1451 int gadget_ready = 0;
1452
Richard Zhao26c696c2012-07-07 22:56:40 +08001453 spin_lock_irqsave(&ci->lock, flags);
1454 ci->vbus_active = is_active;
1455 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301456 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001457 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301458
1459 if (gadget_ready) {
1460 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301461 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001462 hw_device_reset(ci, USBMODE_CM_DC);
1463 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chena107f8c2013-08-14 12:44:11 +03001464 dev_dbg(ci->dev, "Connected to host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301465 } else {
Peter Chen92b336d2013-09-17 12:37:19 +08001466 if (ci->driver)
1467 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001468 hw_device_state(ci, 0);
1469 if (ci->platdata->notify_event)
1470 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001471 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001472 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301473 pm_runtime_put_sync(&_gadget->dev);
Peter Chena107f8c2013-08-14 12:44:11 +03001474 dev_dbg(ci->dev, "Disconnected from host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301475 }
1476 }
1477
1478 return 0;
1479}
1480
Alexander Shishkin8e229782013-06-24 14:46:36 +03001481static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301482{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001483 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301484 unsigned long flags;
1485 int ret = 0;
1486
Richard Zhao26c696c2012-07-07 22:56:40 +08001487 spin_lock_irqsave(&ci->lock, flags);
1488 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301489 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301490 goto out;
1491 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001492 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301493 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301494 goto out;
1495 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001496 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301497out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001498 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301499 return ret;
1500}
1501
Alexander Shishkin8e229782013-06-24 14:46:36 +03001502static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301503{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001504 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301505
Richard Zhao26c696c2012-07-07 22:56:40 +08001506 if (ci->transceiver)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001507 return usb_phy_set_power(ci->transceiver, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301508 return -ENOTSUPP;
1509}
1510
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001511/* Change Data+ pullup status
1512 * this func is used by usb_gadget_connect/disconnet
1513 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001514static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001515{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001516 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001517
Peter Chen4a647832013-08-14 12:44:15 +03001518 if (!ci->vbus_active)
1519 return -EOPNOTSUPP;
1520
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001521 if (is_on)
1522 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1523 else
1524 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1525
1526 return 0;
1527}
1528
Alexander Shishkin8e229782013-06-24 14:46:36 +03001529static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001530 struct usb_gadget_driver *driver);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001531static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001532 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001533/**
1534 * Device operations part of the API to the USB controller hardware,
1535 * which don't involve endpoints (or i/o)
1536 * Check "usb_gadget.h" for details
1537 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301538static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001539 .vbus_session = ci_udc_vbus_session,
1540 .wakeup = ci_udc_wakeup,
1541 .pullup = ci_udc_pullup,
1542 .vbus_draw = ci_udc_vbus_draw,
1543 .udc_start = ci_udc_start,
1544 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301545};
David Lopoaa69a802008-11-17 14:14:51 -08001546
Alexander Shishkin8e229782013-06-24 14:46:36 +03001547static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001548{
1549 int retval = 0, i, j;
1550
Richard Zhao26c696c2012-07-07 22:56:40 +08001551 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001552 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001553 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001554 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001555
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001556 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001557 (j == TX) ? "in" : "out");
1558
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001559 hwep->ci = ci;
1560 hwep->lock = &ci->lock;
1561 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001562
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001563 hwep->ep.name = hwep->name;
1564 hwep->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001565 /*
1566 * for ep0: maxP defined in desc, for other
1567 * eps, maxP is set by epautoconfig() called
1568 * by gadget layer
1569 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001570 hwep->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001571
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001572 INIT_LIST_HEAD(&hwep->qh.queue);
1573 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1574 &hwep->qh.dma);
1575 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001576 retval = -ENOMEM;
1577 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001578 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001579
1580 /*
1581 * set up shorthands for ep0 out and in endpoints,
1582 * don't add to gadget's ep_list
1583 */
1584 if (i == 0) {
1585 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001586 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001587 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001588 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001589
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001590 hwep->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001591 continue;
1592 }
1593
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001594 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001595 }
1596
1597 return retval;
1598}
1599
Alexander Shishkin8e229782013-06-24 14:46:36 +03001600static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001601{
1602 int i;
1603
1604 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001605 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001606
Peter Chen4a295672013-09-10 15:34:39 +08001607 if (hwep->pending_td)
1608 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001609 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001610 }
1611}
1612
David Lopoaa69a802008-11-17 14:14:51 -08001613/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001614 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001615 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001616 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001617 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001618 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001619 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001620static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001621 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001622{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001623 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301624 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001625 int retval = -ENOMEM;
1626
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001627 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001628 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001629
David Lopoaa69a802008-11-17 14:14:51 -08001630
Richard Zhao26c696c2012-07-07 22:56:40 +08001631 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1632 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301633 if (retval)
1634 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001635
Richard Zhao26c696c2012-07-07 22:56:40 +08001636 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1637 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301638 if (retval)
1639 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001640 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001641
Richard Zhao26c696c2012-07-07 22:56:40 +08001642 ci->driver = driver;
1643 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001644 if (ci->vbus_active) {
1645 hw_device_reset(ci, USBMODE_CM_DC);
1646 } else {
1647 pm_runtime_put_sync(&ci->gadget.dev);
1648 goto done;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301649 }
1650
Richard Zhao26c696c2012-07-07 22:56:40 +08001651 retval = hw_device_state(ci, ci->ep0out->qh.dma);
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
1655 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001656 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001657 return retval;
1658}
David Lopoaa69a802008-11-17 14:14:51 -08001659
1660/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001661 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001662 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001663static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001664 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001665{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001666 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001667 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001668
Richard Zhao26c696c2012-07-07 22:56:40 +08001669 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001670
Peter Chend268e9b2013-08-14 12:44:14 +03001671 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001672 hw_device_state(ci, 0);
1673 if (ci->platdata->notify_event)
1674 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001675 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001676 spin_unlock_irqrestore(&ci->lock, flags);
1677 _gadget_stop_activity(&ci->gadget);
1678 spin_lock_irqsave(&ci->lock, flags);
1679 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301680 }
David Lopoaa69a802008-11-17 14:14:51 -08001681
Peter Chenf84839d2013-09-17 12:37:20 +08001682 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001683 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001684
David Lopoaa69a802008-11-17 14:14:51 -08001685 return 0;
1686}
David Lopoaa69a802008-11-17 14:14:51 -08001687
1688/******************************************************************************
1689 * BUS block
1690 *****************************************************************************/
1691/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001692 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001693 *
1694 * This function returns IRQ_HANDLED if the IRQ has been handled
1695 * It locks access to registers
1696 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001697static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001698{
David Lopoaa69a802008-11-17 14:14:51 -08001699 irqreturn_t retval;
1700 u32 intr;
1701
Richard Zhao26c696c2012-07-07 22:56:40 +08001702 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001703 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001704
Richard Zhao26c696c2012-07-07 22:56:40 +08001705 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301706
Alexander Shishkin8e229782013-06-24 14:46:36 +03001707 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001708 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001709 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001710 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301711 return IRQ_NONE;
1712 }
1713 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001714 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001715
Alexander Shishkine443b332012-05-11 17:25:46 +03001716 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001717 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001718 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001719 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001720
David Lopoaa69a802008-11-17 14:14:51 -08001721 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001722 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001723 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001724 if (ci->suspended && ci->driver->resume) {
1725 spin_unlock(&ci->lock);
1726 ci->driver->resume(&ci->gadget);
1727 spin_lock(&ci->lock);
1728 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301729 }
David Lopoaa69a802008-11-17 14:14:51 -08001730 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001731
1732 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001733 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001734
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301735 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001736 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1737 ci->driver->suspend) {
1738 ci->suspended = 1;
1739 spin_unlock(&ci->lock);
1740 ci->driver->suspend(&ci->gadget);
1741 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301742 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301743 }
David Lopoaa69a802008-11-17 14:14:51 -08001744 retval = IRQ_HANDLED;
1745 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001746 retval = IRQ_NONE;
1747 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001748 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001749
1750 return retval;
1751}
1752
1753/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001754 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001755 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001756 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001757static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001758{
Richard Zhao26c696c2012-07-07 22:56:40 +08001759 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001760 int retval = 0;
1761
Richard Zhao26c696c2012-07-07 22:56:40 +08001762 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001763
Richard Zhao26c696c2012-07-07 22:56:40 +08001764 ci->gadget.ops = &usb_gadget_ops;
1765 ci->gadget.speed = USB_SPEED_UNKNOWN;
1766 ci->gadget.max_speed = USB_SPEED_HIGH;
1767 ci->gadget.is_otg = 0;
1768 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001769
Richard Zhao26c696c2012-07-07 22:56:40 +08001770 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001771
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001772 /* alloc resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001773 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1774 sizeof(struct ci_hw_qh),
1775 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001776 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001777 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001778
Alexander Shishkin8e229782013-06-24 14:46:36 +03001779 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1780 sizeof(struct ci_hw_td),
1781 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001782 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001783 retval = -ENOMEM;
1784 goto free_qh_pool;
1785 }
1786
Richard Zhao26c696c2012-07-07 22:56:40 +08001787 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001788 if (retval)
1789 goto free_pools;
1790
Richard Zhao26c696c2012-07-07 22:56:40 +08001791 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301792
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001793 if (ci->global_phy) {
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001794 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001795 if (IS_ERR(ci->transceiver))
1796 ci->transceiver = NULL;
1797 }
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301798
Alexander Shishkin8e229782013-06-24 14:46:36 +03001799 if (ci->platdata->flags & CI_HDRC_REQUIRE_TRANSCEIVER) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001800 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301801 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001802 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301803 }
1804 }
1805
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001806 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001807 retval = otg_set_peripheral(ci->transceiver->otg,
1808 &ci->gadget);
Peter Chend66895f2013-08-14 12:44:05 +03001809 /*
1810 * If we implement all USB functions using chipidea drivers,
1811 * it doesn't need to call above API, meanwhile, if we only
1812 * use gadget function, calling above API is useless.
1813 */
1814 if (retval && retval != -ENOTSUPP)
Greg Kroah-Hartman64dc9e22013-04-05 15:18:00 -07001815 goto put_transceiver;
David Lopoaa69a802008-11-17 14:14:51 -08001816 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001817
Richard Zhao26c696c2012-07-07 22:56:40 +08001818 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001819 if (retval)
1820 goto remove_trans;
1821
Richard Zhao26c696c2012-07-07 22:56:40 +08001822 pm_runtime_no_callbacks(&ci->gadget.dev);
1823 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001824
Peter Chena107f8c2013-08-14 12:44:11 +03001825 /* Update ci->vbus_active */
1826 ci_handle_vbus_change(ci);
1827
David Lopoaa69a802008-11-17 14:14:51 -08001828 return retval;
1829
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001830remove_trans:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001831 if (ci->transceiver) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001832 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001833 if (ci->global_phy)
1834 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001835 }
1836
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001837 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301838put_transceiver:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001839 if (ci->transceiver && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001840 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001841destroy_eps:
1842 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001843free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001844 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001845free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001846 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001847 return retval;
1848}
1849
1850/**
Peter Chen3f124d22013-08-14 12:44:07 +03001851 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001852 *
1853 * No interrupts active, the IRQ has been released
1854 */
Peter Chen3f124d22013-08-14 12:44:07 +03001855void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001856{
Peter Chen3f124d22013-08-14 12:44:07 +03001857 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001858 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001859
Richard Zhao26c696c2012-07-07 22:56:40 +08001860 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001861
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001862 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001863
Richard Zhao26c696c2012-07-07 22:56:40 +08001864 dma_pool_destroy(ci->td_pool);
1865 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001866
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001867 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001868 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001869 if (ci->global_phy)
1870 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301871 }
Peter Chen3f124d22013-08-14 12:44:07 +03001872}
1873
1874static int udc_id_switch_for_device(struct ci_hdrc *ci)
1875{
1876 if (ci->is_otg) {
1877 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1878 ci_enable_otg_interrupt(ci, OTGSC_BSVIE);
1879 }
1880
1881 return 0;
1882}
1883
1884static void udc_id_switch_for_host(struct ci_hdrc *ci)
1885{
1886 if (ci->is_otg) {
1887 /* host doesn't care B_SESSION_VALID event */
1888 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1889 ci_disable_otg_interrupt(ci, OTGSC_BSVIE);
1890 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001891}
David Lopoaa69a802008-11-17 14:14:51 -08001892
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001893/**
1894 * ci_hdrc_gadget_init - initialize device related bits
1895 * ci: the controller
1896 *
Peter Chen3f124d22013-08-14 12:44:07 +03001897 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001898 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001899int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001900{
1901 struct ci_role_driver *rdrv;
1902
1903 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1904 return -ENXIO;
1905
1906 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1907 if (!rdrv)
1908 return -ENOMEM;
1909
Peter Chen3f124d22013-08-14 12:44:07 +03001910 rdrv->start = udc_id_switch_for_device;
1911 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001912 rdrv->irq = udc_irq;
1913 rdrv->name = "gadget";
1914 ci->roles[CI_ROLE_GADGET] = rdrv;
1915
Peter Chen3f124d22013-08-14 12:44:07 +03001916 return udc_start(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001917}