blob: 80de2f88ed2c7852fdd38bec785e6d5681cbe310 [file] [log] [blame]
David Lopoaa69a802008-11-17 14:14:51 -08001/*
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03002 * udc.c - ChipIdea UDC driver
David Lopoaa69a802008-11-17 14:14:51 -08003 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020013#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080014#include <linux/device.h>
15#include <linux/dmapool.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053016#include <linux/err.h>
Alexander Shishkin5b083192013-03-30 02:46:18 +020017#include <linux/irqreturn.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053020#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030023#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080024
Alexander Shishkine443b332012-05-11 17:25:46 +030025#include "ci.h"
26#include "udc.h"
27#include "bits.h"
28#include "debug.h"
Peter Chen3f124d22013-08-14 12:44:07 +030029#include "otg.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020030
David Lopoaa69a802008-11-17 14:14:51 -080031/* control endpoint description */
32static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053033ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080034 .bLength = USB_DT_ENDPOINT_SIZE,
35 .bDescriptorType = USB_DT_ENDPOINT,
36
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053037 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
39 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
40};
41
42static const struct usb_endpoint_descriptor
43ctrl_endpt_in_desc = {
44 .bLength = USB_DT_ENDPOINT_SIZE,
45 .bDescriptorType = USB_DT_ENDPOINT,
46
47 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080048 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
49 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
50};
51
David Lopoaa69a802008-11-17 14:14:51 -080052/**
53 * hw_ep_bit: calculates the bit number
54 * @num: endpoint number
55 * @dir: endpoint direction
56 *
57 * This function returns bit number
58 */
59static inline int hw_ep_bit(int num, int dir)
60{
61 return num + (dir ? 16 : 0);
62}
63
Alexander Shishkin8e229782013-06-24 14:46:36 +030064static inline int ep_to_bit(struct ci_hdrc *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020065{
Richard Zhao26c696c2012-07-07 22:56:40 +080066 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020067
Richard Zhao26c696c2012-07-07 22:56:40 +080068 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020069 n += fill;
70
71 return n;
72}
73
David Lopoaa69a802008-11-17 14:14:51 -080074/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030075 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080076 * @dma: 0 => disable, !0 => enable and set dma engine
77 *
78 * This function returns an error code
79 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030080static int hw_device_state(struct ci_hdrc *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080081{
82 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080083 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080084 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080085 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080086 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
Peter Chena107f8c2013-08-14 12:44:11 +030087 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
David Lopoaa69a802008-11-17 14:14:51 -080088 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080089 hw_write(ci, OP_USBINTR, ~0, 0);
Peter Chena107f8c2013-08-14 12:44:11 +030090 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
David Lopoaa69a802008-11-17 14:14:51 -080091 }
92 return 0;
93}
94
95/**
96 * hw_ep_flush: flush endpoint fifo (execute without interruption)
97 * @num: endpoint number
98 * @dir: endpoint direction
99 *
100 * This function returns an error code
101 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300102static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800103{
104 int n = hw_ep_bit(num, dir);
105
106 do {
107 /* flush any pending transfer */
Richard Zhao26c696c2012-07-07 22:56:40 +0800108 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
109 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800110 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800111 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800112
113 return 0;
114}
115
116/**
117 * hw_ep_disable: disables endpoint (execute without interruption)
118 * @num: endpoint number
119 * @dir: endpoint direction
120 *
121 * This function returns an error code
122 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300123static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800124{
Richard Zhao26c696c2012-07-07 22:56:40 +0800125 hw_ep_flush(ci, num, dir);
126 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300127 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800128 return 0;
129}
130
131/**
132 * hw_ep_enable: enables endpoint (execute without interruption)
133 * @num: endpoint number
134 * @dir: endpoint direction
135 * @type: endpoint type
136 *
137 * This function returns an error code
138 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300139static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800140{
141 u32 mask, data;
142
143 if (dir) {
144 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200145 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800146
147 mask |= ENDPTCTRL_TXS; /* unstall */
148 mask |= ENDPTCTRL_TXR; /* reset data toggle */
149 data |= ENDPTCTRL_TXR;
150 mask |= ENDPTCTRL_TXE; /* enable */
151 data |= ENDPTCTRL_TXE;
152 } else {
153 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200154 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800155
156 mask |= ENDPTCTRL_RXS; /* unstall */
157 mask |= ENDPTCTRL_RXR; /* reset data toggle */
158 data |= ENDPTCTRL_RXR;
159 mask |= ENDPTCTRL_RXE; /* enable */
160 data |= ENDPTCTRL_RXE;
161 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800162 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800163 return 0;
164}
165
166/**
167 * hw_ep_get_halt: return endpoint halt status
168 * @num: endpoint number
169 * @dir: endpoint direction
170 *
171 * This function returns 1 if endpoint halted
172 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300173static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800174{
175 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
176
Richard Zhao26c696c2012-07-07 22:56:40 +0800177 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800178}
179
180/**
David Lopoaa69a802008-11-17 14:14:51 -0800181 * hw_test_and_clear_setup_status: test & clear setup status (execute without
182 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200183 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800184 *
185 * This function returns setup status
186 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300187static int hw_test_and_clear_setup_status(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800188{
Richard Zhao26c696c2012-07-07 22:56:40 +0800189 n = ep_to_bit(ci, n);
190 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800191}
192
193/**
194 * hw_ep_prime: primes endpoint (execute without interruption)
195 * @num: endpoint number
196 * @dir: endpoint direction
197 * @is_ctrl: true if control endpoint
198 *
199 * This function returns an error code
200 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300201static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800202{
203 int n = hw_ep_bit(num, dir);
204
Richard Zhao26c696c2012-07-07 22:56:40 +0800205 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800206 return -EAGAIN;
207
Richard Zhao26c696c2012-07-07 22:56:40 +0800208 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800209
Richard Zhao26c696c2012-07-07 22:56:40 +0800210 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800211 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800212 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800213 return -EAGAIN;
214
215 /* status shoult be tested according with manual but it doesn't work */
216 return 0;
217}
218
219/**
220 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
221 * without interruption)
222 * @num: endpoint number
223 * @dir: endpoint direction
224 * @value: true => stall, false => unstall
225 *
226 * This function returns an error code
227 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300228static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800229{
230 if (value != 0 && value != 1)
231 return -EINVAL;
232
233 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300234 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800235 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
236 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
237
238 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800239 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300240 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800241 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800242
243 return 0;
244}
245
246/**
David Lopoaa69a802008-11-17 14:14:51 -0800247 * hw_is_port_high_speed: test if port is high speed
248 *
249 * This function returns true if high speed port
250 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800252{
Richard Zhao26c696c2012-07-07 22:56:40 +0800253 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
254 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800255}
256
257/**
David Lopoaa69a802008-11-17 14:14:51 -0800258 * hw_read_intr_enable: returns interrupt enable register
259 *
260 * This function returns register data
261 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300262static u32 hw_read_intr_enable(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800263{
Richard Zhao26c696c2012-07-07 22:56:40 +0800264 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800265}
266
267/**
268 * hw_read_intr_status: returns interrupt status register
269 *
270 * This function returns register data
271 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300272static u32 hw_read_intr_status(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800273{
Richard Zhao26c696c2012-07-07 22:56:40 +0800274 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800275}
276
277/**
David Lopoaa69a802008-11-17 14:14:51 -0800278 * hw_test_and_clear_complete: test & clear complete status (execute without
279 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200280 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800281 *
282 * This function returns complete status
283 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300284static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800285{
Richard Zhao26c696c2012-07-07 22:56:40 +0800286 n = ep_to_bit(ci, n);
287 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800288}
289
290/**
291 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
292 * without interruption)
293 *
294 * This function returns active interrutps
295 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300296static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800297{
Richard Zhao26c696c2012-07-07 22:56:40 +0800298 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800299
Richard Zhao26c696c2012-07-07 22:56:40 +0800300 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800301 return reg;
302}
303
304/**
305 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
306 * interruption)
307 *
308 * This function returns guard value
309 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300310static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800311{
Richard Zhao26c696c2012-07-07 22:56:40 +0800312 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800313}
314
315/**
316 * hw_test_and_set_setup_guard: test & set setup guard (execute without
317 * interruption)
318 *
319 * This function returns guard value
320 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300321static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800322{
Richard Zhao26c696c2012-07-07 22:56:40 +0800323 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800324}
325
326/**
327 * hw_usb_set_address: configures USB address (execute without interruption)
328 * @value: new USB address
329 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300330 * This function explicitly sets the address, without the "USBADRA" (advance)
331 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800332 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300333static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800334{
Richard Zhao26c696c2012-07-07 22:56:40 +0800335 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200336 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800337}
338
339/**
340 * hw_usb_reset: restart device after a bus reset (execute without
341 * interruption)
342 *
343 * This function returns an error code
344 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300345static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800346{
Richard Zhao26c696c2012-07-07 22:56:40 +0800347 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800348
349 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800350 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800351
352 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800353 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800354
355 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800356 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800357
358 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800359 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800360 udelay(10); /* not RTOS friendly */
361
362 /* reset all endpoints ? */
363
364 /* reset internal status and wait for further instructions
365 no need to verify the port reset status (ESS does it) */
366
367 return 0;
368}
369
370/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800371 * UTIL block
372 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300373
Alexander Shishkin8e229782013-06-24 14:46:36 +0300374static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300375 unsigned length)
376{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300377 int i;
378 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300379 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
380 GFP_ATOMIC);
381
382 if (node == NULL)
383 return -ENOMEM;
384
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300385 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300386 &node->dma);
387 if (node->ptr == NULL) {
388 kfree(node);
389 return -ENOMEM;
390 }
391
Alexander Shishkin8e229782013-06-24 14:46:36 +0300392 memset(node->ptr, 0, sizeof(struct ci_hw_td));
Michael Grzeschik2e270412013-06-13 17:59:54 +0300393 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
394 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
395 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800396 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
397 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
398
399 if (hwreq->req.length == 0
400 || hwreq->req.length % hwep->ep.maxpacket)
401 mul++;
402 node->ptr->token |= mul << __ffs(TD_MULTO);
403 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300404
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300405 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300406 if (length) {
407 node->ptr->page[0] = cpu_to_le32(temp);
408 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300409 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300410 page &= ~TD_RESERVED_MASK;
411 node->ptr->page[i] = cpu_to_le32(page);
412 }
413 }
414
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300415 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300416
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300417 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300418 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300419 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300420 struct td_node, td);
421 lastnode->ptr->next = cpu_to_le32(node->dma);
422 }
423
424 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300425 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300426
427 return 0;
428}
429
David Lopoaa69a802008-11-17 14:14:51 -0800430/**
431 * _usb_addr: calculates endpoint address from direction & number
432 * @ep: endpoint
433 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300434static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800435{
436 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
437}
438
439/**
440 * _hardware_queue: configures a request at hardware level
441 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300442 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800443 *
444 * This function returns an error code
445 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300446static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800447{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300448 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530449 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300450 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300451 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300452 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800453
David Lopoaa69a802008-11-17 14:14:51 -0800454 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300455 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800456 return -EALREADY;
457
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300458 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800459
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300460 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300461 if (ret)
462 return ret;
463
Michael Grzeschik2e270412013-06-13 17:59:54 +0300464 /*
465 * The first buffer could be not page aligned.
466 * In that case we have to span into one extra td.
467 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300468 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300469 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300470
Michael Grzeschik2e270412013-06-13 17:59:54 +0300471 if (rest == 0)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300472 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300473
Michael Grzeschik2e270412013-06-13 17:59:54 +0300474 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300475 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300476 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300477 add_td_to_list(hwep, hwreq, count);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300478 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200479 }
David Lopoaa69a802008-11-17 14:14:51 -0800480
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300481 if (hwreq->req.zero && hwreq->req.length
482 && (hwreq->req.length % hwep->ep.maxpacket == 0))
483 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300484
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300485 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300486
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300487 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300488 struct td_node, td);
489
490 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300491 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300492 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300493 wmb();
494
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300495 hwreq->req.actual = 0;
496 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300497 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300498 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530499 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300500 struct td_node *prevlastnode;
501 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530502
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300503 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300504 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300505 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300506 struct td_node, td);
507
508 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530509 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800510 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530511 goto done;
512 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800513 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
514 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
515 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
516 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530517 if (tmp_stat)
518 goto done;
519 }
520
521 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300522 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
523 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200524 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800525
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800526 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300527 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300528
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800529 if (hwreq->req.length == 0
530 || hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300531 mul++;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300532 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300533 }
534
David Lopoaa69a802008-11-17 14:14:51 -0800535 wmb(); /* synchronize before ep prime */
536
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300537 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
538 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530539done:
540 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800541}
542
Michael Grzeschik2e270412013-06-13 17:59:54 +0300543/*
544 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300545 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300546 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300547static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300548{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300549 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300550
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300551 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
552 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300553 kfree(pending);
554}
555
David Lopoaa69a802008-11-17 14:14:51 -0800556/**
557 * _hardware_dequeue: handles a request at hardware level
558 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300559 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800560 *
561 * This function returns an error code
562 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300563static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800564{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300565 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300566 struct td_node *node, *tmpnode;
567 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300568 unsigned actual = hwreq->req.length;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200569
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300570 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800571 return -EINVAL;
572
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300573 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530574
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300575 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300576 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300577 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300578 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530579 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300580 }
David Lopoaa69a802008-11-17 14:14:51 -0800581
Michael Grzeschik2e270412013-06-13 17:59:54 +0300582 remaining_length = (tmptoken & TD_TOTAL_BYTES);
583 remaining_length >>= __ffs(TD_TOTAL_BYTES);
584 actual -= remaining_length;
585
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300586 hwreq->req.status = tmptoken & TD_STATUS;
587 if ((TD_STATUS_HALTED & hwreq->req.status)) {
588 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300589 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300590 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
591 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300592 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300593 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
594 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300595 break;
596 }
597
598 if (remaining_length) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300599 if (hwep->dir) {
600 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300601 break;
602 }
603 }
604 /*
605 * As the hardware could still address the freed td
606 * which will run the udc unusable, the cleanup of the
607 * td has to be delayed by one.
608 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300609 if (hwep->pending_td)
610 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300611
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300612 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300613 list_del_init(&node->td);
614 }
David Lopoaa69a802008-11-17 14:14:51 -0800615
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300616 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800617
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300618 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800619
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300620 if (hwreq->req.status)
621 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800622
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300623 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800624}
625
626/**
627 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300628 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800629 *
630 * This function returns an error code
631 * Caller must hold lock
632 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300633static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300634__releases(hwep->lock)
635__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800636{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300637 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300638 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800639 return -EINVAL;
640
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300641 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800642
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300643 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800644
645 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300646 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
647 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300648
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300649 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
650 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300651 list_del_init(&node->td);
652 node->ptr = NULL;
653 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300654 }
655
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300656 list_del_init(&hwreq->queue);
657 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800658
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300659 if (hwreq->req.complete != NULL) {
660 spin_unlock(hwep->lock);
661 hwreq->req.complete(&hwep->ep, &hwreq->req);
662 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800663 }
664 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300665
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300666 if (hwep->pending_td)
667 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300668
David Lopoaa69a802008-11-17 14:14:51 -0800669 return 0;
670}
671
672/**
673 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
674 * @gadget: gadget
675 *
676 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800677 */
678static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800679{
680 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300681 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530682 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800683
Richard Zhao26c696c2012-07-07 22:56:40 +0800684 spin_lock_irqsave(&ci->lock, flags);
685 ci->gadget.speed = USB_SPEED_UNKNOWN;
686 ci->remote_wakeup = 0;
687 ci->suspended = 0;
688 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530689
David Lopoaa69a802008-11-17 14:14:51 -0800690 /* flush all endpoints */
691 gadget_for_each_ep(ep, gadget) {
692 usb_ep_fifo_flush(ep);
693 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800694 usb_ep_fifo_flush(&ci->ep0out->ep);
695 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800696
David Lopoaa69a802008-11-17 14:14:51 -0800697 /* make sure to disable all endpoints */
698 gadget_for_each_ep(ep, gadget) {
699 usb_ep_disable(ep);
700 }
David Lopoaa69a802008-11-17 14:14:51 -0800701
Richard Zhao26c696c2012-07-07 22:56:40 +0800702 if (ci->status != NULL) {
703 usb_ep_free_request(&ci->ep0in->ep, ci->status);
704 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800705 }
706
David Lopoaa69a802008-11-17 14:14:51 -0800707 return 0;
708}
709
710/******************************************************************************
711 * ISR block
712 *****************************************************************************/
713/**
714 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800715 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800716 *
717 * This function resets USB engine after a bus reset occurred
718 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300719static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800720__releases(ci->lock)
721__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800722{
David Lopoaa69a802008-11-17 14:14:51 -0800723 int retval;
724
Peter Chena3aee362013-10-09 14:39:52 +0800725 spin_unlock(&ci->lock);
Peter Chen92b336d2013-09-17 12:37:19 +0800726 if (ci->gadget.speed != USB_SPEED_UNKNOWN) {
727 if (ci->driver)
728 ci->driver->disconnect(&ci->gadget);
729 }
730
Richard Zhao26c696c2012-07-07 22:56:40 +0800731 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800732 if (retval)
733 goto done;
734
Richard Zhao26c696c2012-07-07 22:56:40 +0800735 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800736 if (retval)
737 goto done;
738
Richard Zhao26c696c2012-07-07 22:56:40 +0800739 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
740 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530741 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530742
Michael Grzeschikb9322252012-05-11 17:25:50 +0300743done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800744 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800745
David Lopoaa69a802008-11-17 14:14:51 -0800746 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800747 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800748}
749
750/**
751 * isr_get_status_complete: get_status request complete function
752 * @ep: endpoint
753 * @req: request handled
754 *
755 * Caller must release lock
756 */
757static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
758{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300759 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800760 return;
David Lopoaa69a802008-11-17 14:14:51 -0800761
762 kfree(req->buf);
763 usb_ep_free_request(ep, req);
764}
765
766/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200767 * _ep_queue: queues (submits) an I/O request to an endpoint
768 *
769 * Caller must hold lock
770 */
771static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
772 gfp_t __maybe_unused gfp_flags)
773{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300774 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
775 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
776 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200777 int retval = 0;
778
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300779 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200780 return -EINVAL;
781
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300782 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200783 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300784 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200785 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300786 if (!list_empty(&hwep->qh.queue)) {
787 _ep_nuke(hwep);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200788 retval = -EOVERFLOW;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300789 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
790 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200791 }
792 }
793
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300794 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
795 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
796 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300797 return -EMSGSIZE;
798 }
799
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200800 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300801 if (!list_empty(&hwreq->queue)) {
802 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200803 return -EBUSY;
804 }
805
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200806 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300807 hwreq->req.status = -EINPROGRESS;
808 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200809
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300810 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200811
812 if (retval == -EALREADY)
813 retval = 0;
814 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300815 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200816
817 return retval;
818}
819
820/**
David Lopoaa69a802008-11-17 14:14:51 -0800821 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800822 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800823 * @setup: setup request packet
824 *
825 * This function returns an error code
826 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300827static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800828 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300829__releases(hwep->lock)
830__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800831{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300832 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800833 struct usb_request *req = NULL;
834 gfp_t gfp_flags = GFP_ATOMIC;
835 int dir, num, retval;
836
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300837 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800838 return -EINVAL;
839
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300840 spin_unlock(hwep->lock);
841 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
842 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800843 if (req == NULL)
844 return -ENOMEM;
845
846 req->complete = isr_get_status_complete;
847 req->length = 2;
848 req->buf = kzalloc(req->length, gfp_flags);
849 if (req->buf == NULL) {
850 retval = -ENOMEM;
851 goto err_free_req;
852 }
853
854 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530855 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800856 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800857 retval = 0;
858 } else if ((setup->bRequestType & USB_RECIP_MASK) \
859 == USB_RECIP_ENDPOINT) {
860 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
861 TX : RX;
862 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800863 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800864 }
865 /* else do nothing; reserved for future use */
866
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300867 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800868 if (retval)
869 goto err_free_buf;
870
871 return 0;
872
873 err_free_buf:
874 kfree(req->buf);
875 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300876 spin_unlock(hwep->lock);
877 usb_ep_free_request(&hwep->ep, req);
878 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800879 return retval;
880}
881
882/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530883 * isr_setup_status_complete: setup_status request complete function
884 * @ep: endpoint
885 * @req: request handled
886 *
887 * Caller must release lock. Put the port in test mode if test mode
888 * feature is selected.
889 */
890static void
891isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
892{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300893 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530894 unsigned long flags;
895
Richard Zhao26c696c2012-07-07 22:56:40 +0800896 if (ci->setaddr) {
897 hw_usb_set_address(ci, ci->address);
898 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300899 }
900
Richard Zhao26c696c2012-07-07 22:56:40 +0800901 spin_lock_irqsave(&ci->lock, flags);
902 if (ci->test_mode)
903 hw_port_test_set(ci, ci->test_mode);
904 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530905}
906
907/**
David Lopoaa69a802008-11-17 14:14:51 -0800908 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800909 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800910 *
911 * This function returns an error code
912 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300913static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800914{
915 int retval;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300916 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800917
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300918 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800919 ci->status->context = ci;
920 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800921
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300922 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800923
924 return retval;
925}
926
927/**
928 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300929 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800930 *
931 * This function returns an error code
932 * Caller must hold lock
933 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300934static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300935__releases(hwep->lock)
936__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800937{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300938 struct ci_hw_req *hwreq, *hwreqtemp;
939 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300940 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800941
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300942 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530943 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300944 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530945 if (retval < 0)
946 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300947 list_del_init(&hwreq->queue);
948 if (hwreq->req.complete != NULL) {
949 spin_unlock(hwep->lock);
950 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
951 hwreq->req.length)
952 hweptemp = hwep->ci->ep0in;
953 hwreq->req.complete(&hweptemp->ep, &hwreq->req);
954 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530955 }
956 }
David Lopoaa69a802008-11-17 14:14:51 -0800957
Pavankumar Kondetief907482011-05-02 11:56:27 +0530958 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530959 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800960
David Lopoaa69a802008-11-17 14:14:51 -0800961 return retval;
962}
963
964/**
965 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800966 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800967 *
968 * This function handles traffic events
969 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300970static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800971__releases(ci->lock)
972__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800973{
974 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530975 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800976
Richard Zhao26c696c2012-07-07 22:56:40 +0800977 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300978 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530979 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800980 struct usb_ctrlrequest req;
981
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300982 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800983 continue; /* not configured */
984
Richard Zhao26c696c2012-07-07 22:56:40 +0800985 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300986 err = isr_tr_complete_low(hwep);
987 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -0800988 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800989 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800990 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800991 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300992 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800993 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700994 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800995 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800996 }
997 }
998 }
999
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001000 if (hwep->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +08001001 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -08001002 continue;
1003
1004 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001005 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -08001006 continue;
1007 }
1008
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301009 /*
1010 * Flush data and handshake transactions of previous
1011 * setup packet.
1012 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001013 _ep_nuke(ci->ep0out);
1014 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301015
David Lopoaa69a802008-11-17 14:14:51 -08001016 /* read_setup_packet */
1017 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001018 hw_test_and_set_setup_guard(ci);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001019 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +08001020 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -08001021
1022 type = req.bRequestType;
1023
Richard Zhao26c696c2012-07-07 22:56:40 +08001024 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -08001025
David Lopoaa69a802008-11-17 14:14:51 -08001026 switch (req.bRequest) {
1027 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301028 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1029 le16_to_cpu(req.wValue) ==
1030 USB_ENDPOINT_HALT) {
1031 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -08001032 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301033 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301034 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301035 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301036 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001037 num += ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001038 if (!ci->ci_hw_ep[num].wedge) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001039 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301040 err = usb_ep_clear_halt(
Alexander Shishkin8e229782013-06-24 14:46:36 +03001041 &ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001042 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301043 if (err)
1044 break;
1045 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001046 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301047 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1048 le16_to_cpu(req.wValue) ==
1049 USB_DEVICE_REMOTE_WAKEUP) {
1050 if (req.wLength != 0)
1051 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001052 ci->remote_wakeup = 0;
1053 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301054 } else {
1055 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -08001056 }
David Lopoaa69a802008-11-17 14:14:51 -08001057 break;
1058 case USB_REQ_GET_STATUS:
1059 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1060 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1061 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1062 goto delegate;
1063 if (le16_to_cpu(req.wLength) != 2 ||
1064 le16_to_cpu(req.wValue) != 0)
1065 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001066 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -08001067 break;
1068 case USB_REQ_SET_ADDRESS:
1069 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1070 goto delegate;
1071 if (le16_to_cpu(req.wLength) != 0 ||
1072 le16_to_cpu(req.wIndex) != 0)
1073 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001074 ci->address = (u8)le16_to_cpu(req.wValue);
1075 ci->setaddr = true;
1076 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001077 break;
1078 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301079 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1080 le16_to_cpu(req.wValue) ==
1081 USB_ENDPOINT_HALT) {
1082 if (req.wLength != 0)
1083 break;
1084 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301085 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301086 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301087 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001088 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -08001089
Richard Zhao26c696c2012-07-07 22:56:40 +08001090 spin_unlock(&ci->lock);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001091 err = usb_ep_set_halt(&ci->ci_hw_ep[num].ep);
Richard Zhao26c696c2012-07-07 22:56:40 +08001092 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301093 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +08001094 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301095 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301096 if (req.wLength != 0)
1097 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301098 switch (le16_to_cpu(req.wValue)) {
1099 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +08001100 ci->remote_wakeup = 1;
1101 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301102 break;
1103 case USB_DEVICE_TEST_MODE:
1104 tmode = le16_to_cpu(req.wIndex) >> 8;
1105 switch (tmode) {
1106 case TEST_J:
1107 case TEST_K:
1108 case TEST_SE0_NAK:
1109 case TEST_PACKET:
1110 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +08001111 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301112 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +08001113 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301114 break;
1115 default:
1116 break;
1117 }
1118 default:
1119 goto delegate;
1120 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301121 } else {
1122 goto delegate;
1123 }
David Lopoaa69a802008-11-17 14:14:51 -08001124 break;
1125 default:
1126delegate:
1127 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001128 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001129
Richard Zhao26c696c2012-07-07 22:56:40 +08001130 spin_unlock(&ci->lock);
1131 err = ci->driver->setup(&ci->gadget, &req);
1132 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001133 break;
1134 }
1135
1136 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001137 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001138 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001139 dev_err(ci->dev, "error: ep_set_halt\n");
1140 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001141 }
1142 }
1143}
1144
1145/******************************************************************************
1146 * ENDPT block
1147 *****************************************************************************/
1148/**
1149 * ep_enable: configure endpoint, making it usable
1150 *
1151 * Check usb_ep_enable() at "usb_gadget.h" for details
1152 */
1153static int ep_enable(struct usb_ep *ep,
1154 const struct usb_endpoint_descriptor *desc)
1155{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001156 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301157 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001158 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001159 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001160
David Lopoaa69a802008-11-17 14:14:51 -08001161 if (ep == NULL || desc == NULL)
1162 return -EINVAL;
1163
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001164 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001165
1166 /* only internal SW should enable ctrl endpts */
1167
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001168 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001169
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001170 if (!list_empty(&hwep->qh.queue))
1171 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001172
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001173 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1174 hwep->num = usb_endpoint_num(desc);
1175 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001176
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001177 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1178 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001179
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001180 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001181 cap |= QH_IOS;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001182 if (hwep->num)
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001183 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001184 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
Peter Chen2fc5a7d2014-01-10 13:51:32 +08001185 /*
1186 * For ISO-TX, we set mult at QH as the largest value, and use
1187 * MultO at TD as real mult value.
1188 */
1189 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1190 cap |= 3 << __ffs(QH_MULT);
David Lopoaa69a802008-11-17 14:14:51 -08001191
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001192 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001193
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001194 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001195
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301196 /*
1197 * Enable endpoints in the HW other than ep0 as ep0
1198 * is always enabled
1199 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001200 if (hwep->num)
1201 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1202 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001203
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001204 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001205 return retval;
1206}
1207
1208/**
1209 * ep_disable: endpoint is no longer usable
1210 *
1211 * Check usb_ep_disable() at "usb_gadget.h" for details
1212 */
1213static int ep_disable(struct usb_ep *ep)
1214{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001215 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001216 int direction, retval = 0;
1217 unsigned long flags;
1218
David Lopoaa69a802008-11-17 14:14:51 -08001219 if (ep == NULL)
1220 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001221 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001222 return -EBUSY;
1223
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001224 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001225
1226 /* only internal SW should disable ctrl endpts */
1227
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001228 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001229 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001230 retval |= _ep_nuke(hwep);
1231 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001232
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001233 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1234 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001235
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001236 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001237
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001238 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001239
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001240 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001241 return retval;
1242}
1243
1244/**
1245 * ep_alloc_request: allocate a request object to use with this endpoint
1246 *
1247 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1248 */
1249static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1250{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001251 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001252
Alexander Shishkin0f089092012-05-08 23:29:02 +03001253 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001254 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001255
Alexander Shishkin8e229782013-06-24 14:46:36 +03001256 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001257 if (hwreq != NULL) {
1258 INIT_LIST_HEAD(&hwreq->queue);
1259 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001260 }
1261
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001262 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001263}
1264
1265/**
1266 * ep_free_request: frees a request object
1267 *
1268 * Check usb_ep_free_request() at "usb_gadget.h" for details
1269 */
1270static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1271{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001272 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1273 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001274 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001275 unsigned long flags;
1276
David Lopoaa69a802008-11-17 14:14:51 -08001277 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001278 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001279 } else if (!list_empty(&hwreq->queue)) {
1280 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001281 return;
1282 }
1283
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001284 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001285
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001286 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1287 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001288 list_del_init(&node->td);
1289 node->ptr = NULL;
1290 kfree(node);
1291 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001292
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001293 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001294
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001295 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001296}
1297
1298/**
1299 * ep_queue: queues (submits) an I/O request to an endpoint
1300 *
1301 * Check usb_ep_queue()* at usb_gadget.h" for details
1302 */
1303static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1304 gfp_t __maybe_unused gfp_flags)
1305{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001306 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001307 int retval = 0;
1308 unsigned long flags;
1309
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001310 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001311 return -EINVAL;
1312
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001313 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001314 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001315 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001316 return retval;
1317}
1318
1319/**
1320 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1321 *
1322 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1323 */
1324static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1325{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001326 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1327 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001328 unsigned long flags;
1329
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001330 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1331 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1332 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001333 return -EINVAL;
1334
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001335 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001336
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001337 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001338
1339 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001340 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001341
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001342 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001343
David Lopoaa69a802008-11-17 14:14:51 -08001344 req->status = -ECONNRESET;
1345
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001346 if (hwreq->req.complete != NULL) {
1347 spin_unlock(hwep->lock);
1348 hwreq->req.complete(&hwep->ep, &hwreq->req);
1349 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001350 }
1351
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001352 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001353 return 0;
1354}
1355
1356/**
1357 * ep_set_halt: sets the endpoint halt feature
1358 *
1359 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1360 */
1361static int ep_set_halt(struct usb_ep *ep, int value)
1362{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001363 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001364 int direction, retval = 0;
1365 unsigned long flags;
1366
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001367 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001368 return -EINVAL;
1369
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001370 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001371 return -EOPNOTSUPP;
1372
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001373 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001374
1375#ifndef STALL_IN
1376 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001377 if (value && hwep->type == USB_ENDPOINT_XFER_BULK && hwep->dir == TX &&
1378 !list_empty(&hwep->qh.queue)) {
1379 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001380 return -EAGAIN;
1381 }
1382#endif
1383
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001384 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001385 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001386 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001387
1388 if (!value)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001389 hwep->wedge = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001390
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001391 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1392 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001393
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001394 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001395
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001396 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001397 return retval;
1398}
1399
1400/**
1401 * ep_set_wedge: sets the halt feature and ignores clear requests
1402 *
1403 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1404 */
1405static int ep_set_wedge(struct usb_ep *ep)
1406{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001407 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001408 unsigned long flags;
1409
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001410 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001411 return -EINVAL;
1412
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001413 spin_lock_irqsave(hwep->lock, flags);
1414 hwep->wedge = 1;
1415 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001416
1417 return usb_ep_set_halt(ep);
1418}
1419
1420/**
1421 * ep_fifo_flush: flushes contents of a fifo
1422 *
1423 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1424 */
1425static void ep_fifo_flush(struct usb_ep *ep)
1426{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001427 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001428 unsigned long flags;
1429
David Lopoaa69a802008-11-17 14:14:51 -08001430 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001431 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001432 return;
1433 }
1434
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001435 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001436
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001437 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001438
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001439 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001440}
1441
1442/**
1443 * Endpoint-specific part of the API to the USB controller hardware
1444 * Check "usb_gadget.h" for details
1445 */
1446static const struct usb_ep_ops usb_ep_ops = {
1447 .enable = ep_enable,
1448 .disable = ep_disable,
1449 .alloc_request = ep_alloc_request,
1450 .free_request = ep_free_request,
1451 .queue = ep_queue,
1452 .dequeue = ep_dequeue,
1453 .set_halt = ep_set_halt,
1454 .set_wedge = ep_set_wedge,
1455 .fifo_flush = ep_fifo_flush,
1456};
1457
1458/******************************************************************************
1459 * GADGET block
1460 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001461static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301462{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001463 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301464 unsigned long flags;
1465 int gadget_ready = 0;
1466
Richard Zhao26c696c2012-07-07 22:56:40 +08001467 spin_lock_irqsave(&ci->lock, flags);
1468 ci->vbus_active = is_active;
1469 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301470 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001471 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301472
1473 if (gadget_ready) {
1474 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301475 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001476 hw_device_reset(ci, USBMODE_CM_DC);
1477 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chena107f8c2013-08-14 12:44:11 +03001478 dev_dbg(ci->dev, "Connected to host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301479 } else {
Peter Chen92b336d2013-09-17 12:37:19 +08001480 if (ci->driver)
1481 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001482 hw_device_state(ci, 0);
1483 if (ci->platdata->notify_event)
1484 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001485 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001486 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301487 pm_runtime_put_sync(&_gadget->dev);
Peter Chena107f8c2013-08-14 12:44:11 +03001488 dev_dbg(ci->dev, "Disconnected from host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301489 }
1490 }
1491
1492 return 0;
1493}
1494
Alexander Shishkin8e229782013-06-24 14:46:36 +03001495static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301496{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001497 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301498 unsigned long flags;
1499 int ret = 0;
1500
Richard Zhao26c696c2012-07-07 22:56:40 +08001501 spin_lock_irqsave(&ci->lock, flags);
1502 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301503 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301504 goto out;
1505 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001506 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301507 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301508 goto out;
1509 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001510 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301511out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001512 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301513 return ret;
1514}
1515
Alexander Shishkin8e229782013-06-24 14:46:36 +03001516static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301517{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001518 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301519
Richard Zhao26c696c2012-07-07 22:56:40 +08001520 if (ci->transceiver)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001521 return usb_phy_set_power(ci->transceiver, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301522 return -ENOTSUPP;
1523}
1524
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001525/* Change Data+ pullup status
1526 * this func is used by usb_gadget_connect/disconnet
1527 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001528static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001529{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001530 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001531
Peter Chen4a647832013-08-14 12:44:15 +03001532 if (!ci->vbus_active)
1533 return -EOPNOTSUPP;
1534
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001535 if (is_on)
1536 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1537 else
1538 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1539
1540 return 0;
1541}
1542
Alexander Shishkin8e229782013-06-24 14:46:36 +03001543static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001544 struct usb_gadget_driver *driver);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001545static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001546 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001547/**
1548 * Device operations part of the API to the USB controller hardware,
1549 * which don't involve endpoints (or i/o)
1550 * Check "usb_gadget.h" for details
1551 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301552static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001553 .vbus_session = ci_udc_vbus_session,
1554 .wakeup = ci_udc_wakeup,
1555 .pullup = ci_udc_pullup,
1556 .vbus_draw = ci_udc_vbus_draw,
1557 .udc_start = ci_udc_start,
1558 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301559};
David Lopoaa69a802008-11-17 14:14:51 -08001560
Alexander Shishkin8e229782013-06-24 14:46:36 +03001561static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001562{
1563 int retval = 0, i, j;
1564
Richard Zhao26c696c2012-07-07 22:56:40 +08001565 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001566 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001567 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001568 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001569
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001570 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001571 (j == TX) ? "in" : "out");
1572
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001573 hwep->ci = ci;
1574 hwep->lock = &ci->lock;
1575 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001576
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001577 hwep->ep.name = hwep->name;
1578 hwep->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001579 /*
1580 * for ep0: maxP defined in desc, for other
1581 * eps, maxP is set by epautoconfig() called
1582 * by gadget layer
1583 */
Robert Baldygae117e742013-12-13 12:23:38 +01001584 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001585
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001586 INIT_LIST_HEAD(&hwep->qh.queue);
1587 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1588 &hwep->qh.dma);
1589 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001590 retval = -ENOMEM;
1591 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001592 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001593
1594 /*
1595 * set up shorthands for ep0 out and in endpoints,
1596 * don't add to gadget's ep_list
1597 */
1598 if (i == 0) {
1599 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001600 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001601 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001602 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001603
Robert Baldygae117e742013-12-13 12:23:38 +01001604 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001605 continue;
1606 }
1607
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001608 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001609 }
1610
1611 return retval;
1612}
1613
Alexander Shishkin8e229782013-06-24 14:46:36 +03001614static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001615{
1616 int i;
1617
1618 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001619 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001620
Peter Chen4a295672013-09-10 15:34:39 +08001621 if (hwep->pending_td)
1622 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001623 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001624 }
1625}
1626
David Lopoaa69a802008-11-17 14:14:51 -08001627/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001628 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001629 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001630 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001631 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001632 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001633 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001634static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001635 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001636{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001637 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301638 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001639 int retval = -ENOMEM;
1640
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001641 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001642 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001643
David Lopoaa69a802008-11-17 14:14:51 -08001644
Richard Zhao26c696c2012-07-07 22:56:40 +08001645 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1646 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301647 if (retval)
1648 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001649
Richard Zhao26c696c2012-07-07 22:56:40 +08001650 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1651 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301652 if (retval)
1653 return retval;
David Lopoaa69a802008-11-17 14:14:51 -08001654
Richard Zhao26c696c2012-07-07 22:56:40 +08001655 ci->driver = driver;
1656 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001657 if (ci->vbus_active) {
Peter Chen65b2fb32013-10-08 10:30:17 +08001658 spin_lock_irqsave(&ci->lock, flags);
Peter Chend268e9b2013-08-14 12:44:14 +03001659 hw_device_reset(ci, USBMODE_CM_DC);
1660 } else {
1661 pm_runtime_put_sync(&ci->gadget.dev);
Peter Chen65b2fb32013-10-08 10:30:17 +08001662 return retval;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301663 }
1664
Richard Zhao26c696c2012-07-07 22:56:40 +08001665 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen65b2fb32013-10-08 10:30:17 +08001666 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301667 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001668 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001669
David Lopoaa69a802008-11-17 14:14:51 -08001670 return retval;
1671}
David Lopoaa69a802008-11-17 14:14:51 -08001672
1673/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001674 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001675 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001676static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001677 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001678{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001679 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001680 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001681
Richard Zhao26c696c2012-07-07 22:56:40 +08001682 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001683
Peter Chend268e9b2013-08-14 12:44:14 +03001684 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001685 hw_device_state(ci, 0);
1686 if (ci->platdata->notify_event)
1687 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001688 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001689 spin_unlock_irqrestore(&ci->lock, flags);
1690 _gadget_stop_activity(&ci->gadget);
1691 spin_lock_irqsave(&ci->lock, flags);
1692 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301693 }
David Lopoaa69a802008-11-17 14:14:51 -08001694
Peter Chenf84839d2013-09-17 12:37:20 +08001695 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001696 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001697
David Lopoaa69a802008-11-17 14:14:51 -08001698 return 0;
1699}
David Lopoaa69a802008-11-17 14:14:51 -08001700
1701/******************************************************************************
1702 * BUS block
1703 *****************************************************************************/
1704/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001705 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001706 *
1707 * This function returns IRQ_HANDLED if the IRQ has been handled
1708 * It locks access to registers
1709 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001710static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001711{
David Lopoaa69a802008-11-17 14:14:51 -08001712 irqreturn_t retval;
1713 u32 intr;
1714
Richard Zhao26c696c2012-07-07 22:56:40 +08001715 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001716 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001717
Richard Zhao26c696c2012-07-07 22:56:40 +08001718 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301719
Alexander Shishkin8e229782013-06-24 14:46:36 +03001720 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001721 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001722 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001723 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301724 return IRQ_NONE;
1725 }
1726 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001727 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001728
Alexander Shishkine443b332012-05-11 17:25:46 +03001729 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001730 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001731 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001732 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001733
David Lopoaa69a802008-11-17 14:14:51 -08001734 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001736 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001737 if (ci->suspended && ci->driver->resume) {
1738 spin_unlock(&ci->lock);
1739 ci->driver->resume(&ci->gadget);
1740 spin_lock(&ci->lock);
1741 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301742 }
David Lopoaa69a802008-11-17 14:14:51 -08001743 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001744
1745 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001747
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301748 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001749 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1750 ci->driver->suspend) {
1751 ci->suspended = 1;
1752 spin_unlock(&ci->lock);
1753 ci->driver->suspend(&ci->gadget);
1754 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301755 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301756 }
David Lopoaa69a802008-11-17 14:14:51 -08001757 retval = IRQ_HANDLED;
1758 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001759 retval = IRQ_NONE;
1760 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001761 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001762
1763 return retval;
1764}
1765
1766/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001767 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001768 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001769 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001770static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001771{
Richard Zhao26c696c2012-07-07 22:56:40 +08001772 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001773 int retval = 0;
1774
Richard Zhao26c696c2012-07-07 22:56:40 +08001775 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001776
Richard Zhao26c696c2012-07-07 22:56:40 +08001777 ci->gadget.ops = &usb_gadget_ops;
1778 ci->gadget.speed = USB_SPEED_UNKNOWN;
1779 ci->gadget.max_speed = USB_SPEED_HIGH;
1780 ci->gadget.is_otg = 0;
1781 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001782
Richard Zhao26c696c2012-07-07 22:56:40 +08001783 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001784
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001785 /* alloc resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001786 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1787 sizeof(struct ci_hw_qh),
1788 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001789 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001790 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001791
Alexander Shishkin8e229782013-06-24 14:46:36 +03001792 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1793 sizeof(struct ci_hw_td),
1794 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001795 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001796 retval = -ENOMEM;
1797 goto free_qh_pool;
1798 }
1799
Richard Zhao26c696c2012-07-07 22:56:40 +08001800 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001801 if (retval)
1802 goto free_pools;
1803
Richard Zhao26c696c2012-07-07 22:56:40 +08001804 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301805
Richard Zhao26c696c2012-07-07 22:56:40 +08001806 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001807 if (retval)
Peter Chen74475ed2013-09-24 12:47:53 +08001808 goto destroy_eps;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001809
Richard Zhao26c696c2012-07-07 22:56:40 +08001810 pm_runtime_no_callbacks(&ci->gadget.dev);
1811 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001812
David Lopoaa69a802008-11-17 14:14:51 -08001813 return retval;
1814
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001815destroy_eps:
1816 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001817free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001818 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001819free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001820 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001821 return retval;
1822}
1823
1824/**
Peter Chen3f124d22013-08-14 12:44:07 +03001825 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001826 *
1827 * No interrupts active, the IRQ has been released
1828 */
Peter Chen3f124d22013-08-14 12:44:07 +03001829void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001830{
Peter Chen3f124d22013-08-14 12:44:07 +03001831 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001832 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001833
Richard Zhao26c696c2012-07-07 22:56:40 +08001834 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001835
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001836 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001837
Richard Zhao26c696c2012-07-07 22:56:40 +08001838 dma_pool_destroy(ci->td_pool);
1839 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001840
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001841 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001842 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001843 if (ci->global_phy)
1844 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301845 }
Peter Chen3f124d22013-08-14 12:44:07 +03001846}
1847
1848static int udc_id_switch_for_device(struct ci_hdrc *ci)
1849{
1850 if (ci->is_otg) {
1851 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1852 ci_enable_otg_interrupt(ci, OTGSC_BSVIE);
1853 }
1854
1855 return 0;
1856}
1857
1858static void udc_id_switch_for_host(struct ci_hdrc *ci)
1859{
1860 if (ci->is_otg) {
1861 /* host doesn't care B_SESSION_VALID event */
1862 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1863 ci_disable_otg_interrupt(ci, OTGSC_BSVIE);
1864 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001865}
David Lopoaa69a802008-11-17 14:14:51 -08001866
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001867/**
1868 * ci_hdrc_gadget_init - initialize device related bits
1869 * ci: the controller
1870 *
Peter Chen3f124d22013-08-14 12:44:07 +03001871 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001872 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001873int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001874{
1875 struct ci_role_driver *rdrv;
1876
1877 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1878 return -ENXIO;
1879
1880 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1881 if (!rdrv)
1882 return -ENOMEM;
1883
Peter Chen3f124d22013-08-14 12:44:07 +03001884 rdrv->start = udc_id_switch_for_device;
1885 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001886 rdrv->irq = udc_irq;
1887 rdrv->name = "gadget";
1888 ci->roles[CI_ROLE_GADGET] = rdrv;
1889
Peter Chen3f124d22013-08-14 12:44:07 +03001890 return udc_start(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001891}