blob: 7739c64ef2590a1dbdd0daf8e631b90cf99af628 [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 */
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800108 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
Richard Zhao26c696c2012-07-07 22:56:40 +0800109 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800110 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800111 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800112
113 return 0;
114}
115
116/**
117 * hw_ep_disable: disables endpoint (execute without interruption)
118 * @num: endpoint number
119 * @dir: endpoint direction
120 *
121 * This function returns an error code
122 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300123static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800124{
Richard Zhao26c696c2012-07-07 22:56:40 +0800125 hw_ep_flush(ci, num, dir);
126 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300127 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800128 return 0;
129}
130
131/**
132 * hw_ep_enable: enables endpoint (execute without interruption)
133 * @num: endpoint number
134 * @dir: endpoint direction
135 * @type: endpoint type
136 *
137 * This function returns an error code
138 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300139static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800140{
141 u32 mask, data;
142
143 if (dir) {
144 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200145 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800146
147 mask |= ENDPTCTRL_TXS; /* unstall */
148 mask |= ENDPTCTRL_TXR; /* reset data toggle */
149 data |= ENDPTCTRL_TXR;
150 mask |= ENDPTCTRL_TXE; /* enable */
151 data |= ENDPTCTRL_TXE;
152 } else {
153 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200154 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800155
156 mask |= ENDPTCTRL_RXS; /* unstall */
157 mask |= ENDPTCTRL_RXR; /* reset data toggle */
158 data |= ENDPTCTRL_RXR;
159 mask |= ENDPTCTRL_RXE; /* enable */
160 data |= ENDPTCTRL_RXE;
161 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800162 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800163 return 0;
164}
165
166/**
167 * hw_ep_get_halt: return endpoint halt status
168 * @num: endpoint number
169 * @dir: endpoint direction
170 *
171 * This function returns 1 if endpoint halted
172 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300173static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800174{
175 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
176
Richard Zhao26c696c2012-07-07 22:56:40 +0800177 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800178}
179
180/**
David Lopoaa69a802008-11-17 14:14:51 -0800181 * hw_ep_prime: primes endpoint (execute without interruption)
182 * @num: endpoint number
183 * @dir: endpoint direction
184 * @is_ctrl: true if control endpoint
185 *
186 * This function returns an error code
187 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300188static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800189{
190 int n = hw_ep_bit(num, dir);
191
Richard Zhao26c696c2012-07-07 22:56:40 +0800192 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800193 return -EAGAIN;
194
Matthieu CASTET5bf5dbe2014-02-19 13:46:31 +0800195 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800196
Richard Zhao26c696c2012-07-07 22:56:40 +0800197 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800198 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800199 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800200 return -EAGAIN;
201
202 /* status shoult be tested according with manual but it doesn't work */
203 return 0;
204}
205
206/**
207 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
208 * without interruption)
209 * @num: endpoint number
210 * @dir: endpoint direction
211 * @value: true => stall, false => unstall
212 *
213 * This function returns an error code
214 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300215static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800216{
217 if (value != 0 && value != 1)
218 return -EINVAL;
219
220 do {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300221 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800222 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
223 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
224
225 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800226 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300227 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800228 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800229
230 return 0;
231}
232
233/**
David Lopoaa69a802008-11-17 14:14:51 -0800234 * hw_is_port_high_speed: test if port is high speed
235 *
236 * This function returns true if high speed port
237 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300238static int hw_port_is_high_speed(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800239{
Richard Zhao26c696c2012-07-07 22:56:40 +0800240 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
241 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800242}
243
244/**
David Lopoaa69a802008-11-17 14:14:51 -0800245 * hw_read_intr_enable: returns interrupt enable register
246 *
247 * This function returns register data
248 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300249static u32 hw_read_intr_enable(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800250{
Richard Zhao26c696c2012-07-07 22:56:40 +0800251 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800252}
253
254/**
255 * hw_read_intr_status: returns interrupt status register
256 *
257 * This function returns register data
258 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300259static u32 hw_read_intr_status(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800260{
Richard Zhao26c696c2012-07-07 22:56:40 +0800261 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800262}
263
264/**
David Lopoaa69a802008-11-17 14:14:51 -0800265 * hw_test_and_clear_complete: test & clear complete status (execute without
266 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200267 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800268 *
269 * This function returns complete status
270 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300271static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800272{
Richard Zhao26c696c2012-07-07 22:56:40 +0800273 n = ep_to_bit(ci, n);
274 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800275}
276
277/**
278 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
279 * without interruption)
280 *
281 * This function returns active interrutps
282 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300283static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800284{
Richard Zhao26c696c2012-07-07 22:56:40 +0800285 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800286
Richard Zhao26c696c2012-07-07 22:56:40 +0800287 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800288 return reg;
289}
290
291/**
292 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
293 * interruption)
294 *
295 * This function returns guard value
296 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300297static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800298{
Richard Zhao26c696c2012-07-07 22:56:40 +0800299 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800300}
301
302/**
303 * hw_test_and_set_setup_guard: test & set setup guard (execute without
304 * interruption)
305 *
306 * This function returns guard value
307 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300308static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800309{
Richard Zhao26c696c2012-07-07 22:56:40 +0800310 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800311}
312
313/**
314 * hw_usb_set_address: configures USB address (execute without interruption)
315 * @value: new USB address
316 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300317 * This function explicitly sets the address, without the "USBADRA" (advance)
318 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800319 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300320static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800321{
Richard Zhao26c696c2012-07-07 22:56:40 +0800322 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200323 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800324}
325
326/**
327 * hw_usb_reset: restart device after a bus reset (execute without
328 * interruption)
329 *
330 * This function returns an error code
331 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300332static int hw_usb_reset(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800333{
Richard Zhao26c696c2012-07-07 22:56:40 +0800334 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800335
336 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800337 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800338
339 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800340 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800341
342 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800343 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800344
345 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800346 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800347 udelay(10); /* not RTOS friendly */
348
349 /* reset all endpoints ? */
350
351 /* reset internal status and wait for further instructions
352 no need to verify the port reset status (ESS does it) */
353
354 return 0;
355}
356
357/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800358 * UTIL block
359 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300360
Alexander Shishkin8e229782013-06-24 14:46:36 +0300361static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300362 unsigned length)
363{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300364 int i;
365 u32 temp;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300366 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
367 GFP_ATOMIC);
368
369 if (node == NULL)
370 return -ENOMEM;
371
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300372 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300373 &node->dma);
374 if (node->ptr == NULL) {
375 kfree(node);
376 return -ENOMEM;
377 }
378
Alexander Shishkin8e229782013-06-24 14:46:36 +0300379 memset(node->ptr, 0, sizeof(struct ci_hw_td));
Michael Grzeschik2e270412013-06-13 17:59:54 +0300380 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
381 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
382 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800383 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
384 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
385
386 if (hwreq->req.length == 0
387 || hwreq->req.length % hwep->ep.maxpacket)
388 mul++;
389 node->ptr->token |= mul << __ffs(TD_MULTO);
390 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300391
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300392 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300393 if (length) {
394 node->ptr->page[0] = cpu_to_le32(temp);
395 for (i = 1; i < TD_PAGE_COUNT; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300396 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300397 page &= ~TD_RESERVED_MASK;
398 node->ptr->page[i] = cpu_to_le32(page);
399 }
400 }
401
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300402 hwreq->req.actual += length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300403
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300404 if (!list_empty(&hwreq->tds)) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300405 /* get the last entry */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300406 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300407 struct td_node, td);
408 lastnode->ptr->next = cpu_to_le32(node->dma);
409 }
410
411 INIT_LIST_HEAD(&node->td);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300412 list_add_tail(&node->td, &hwreq->tds);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300413
414 return 0;
415}
416
David Lopoaa69a802008-11-17 14:14:51 -0800417/**
418 * _usb_addr: calculates endpoint address from direction & number
419 * @ep: endpoint
420 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300421static inline u8 _usb_addr(struct ci_hw_ep *ep)
David Lopoaa69a802008-11-17 14:14:51 -0800422{
423 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
424}
425
426/**
427 * _hardware_queue: configures a request at hardware level
428 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300429 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800430 *
431 * This function returns an error code
432 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300433static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800434{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300435 struct ci_hdrc *ci = hwep->ci;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530436 int ret = 0;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300437 unsigned rest = hwreq->req.length;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300438 int pages = TD_PAGE_COUNT;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300439 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800440
David Lopoaa69a802008-11-17 14:14:51 -0800441 /* don't queue twice */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300442 if (hwreq->req.status == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800443 return -EALREADY;
444
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300445 hwreq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800446
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300447 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300448 if (ret)
449 return ret;
450
Michael Grzeschik2e270412013-06-13 17:59:54 +0300451 /*
452 * The first buffer could be not page aligned.
453 * In that case we have to span into one extra td.
454 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300455 if (hwreq->req.dma % PAGE_SIZE)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300456 pages--;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300457
Michael Grzeschik2e270412013-06-13 17:59:54 +0300458 if (rest == 0)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300459 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300460
Michael Grzeschik2e270412013-06-13 17:59:54 +0300461 while (rest > 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300462 unsigned count = min(hwreq->req.length - hwreq->req.actual,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300463 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300464 add_td_to_list(hwep, hwreq, count);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300465 rest -= count;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200466 }
David Lopoaa69a802008-11-17 14:14:51 -0800467
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300468 if (hwreq->req.zero && hwreq->req.length
469 && (hwreq->req.length % hwep->ep.maxpacket == 0))
470 add_td_to_list(hwep, hwreq, 0);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300471
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300472 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300473
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300474 lastnode = list_entry(hwreq->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300475 struct td_node, td);
476
477 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300478 if (!hwreq->req.no_interrupt)
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300479 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300480 wmb();
481
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300482 hwreq->req.actual = 0;
483 if (!list_empty(&hwep->qh.queue)) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300484 struct ci_hw_req *hwreqprev;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300485 int n = hw_ep_bit(hwep->num, hwep->dir);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530486 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300487 struct td_node *prevlastnode;
488 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530489
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300490 hwreqprev = list_entry(hwep->qh.queue.prev,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300491 struct ci_hw_req, queue);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300492 prevlastnode = list_entry(hwreqprev->tds.prev,
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300493 struct td_node, td);
494
495 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530496 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800497 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530498 goto done;
499 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800500 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
501 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
502 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
503 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530504 if (tmp_stat)
505 goto done;
506 }
507
508 /* QH configuration */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300509 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
510 hwep->qh.ptr->td.token &=
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200511 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800512
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800513 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300514 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300515
Peter Chen2fc5a7d2014-01-10 13:51:32 +0800516 if (hwreq->req.length == 0
517 || hwreq->req.length % hwep->ep.maxpacket)
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300518 mul++;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300519 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300520 }
521
David Lopoaa69a802008-11-17 14:14:51 -0800522 wmb(); /* synchronize before ep prime */
523
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300524 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
525 hwep->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530526done:
527 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800528}
529
Michael Grzeschik2e270412013-06-13 17:59:54 +0300530/*
531 * free_pending_td: remove a pending request for the endpoint
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300532 * @hwep: endpoint
Michael Grzeschik2e270412013-06-13 17:59:54 +0300533 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300534static void free_pending_td(struct ci_hw_ep *hwep)
Michael Grzeschik2e270412013-06-13 17:59:54 +0300535{
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300536 struct td_node *pending = hwep->pending_td;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300537
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300538 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
539 hwep->pending_td = NULL;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300540 kfree(pending);
541}
542
David Lopoaa69a802008-11-17 14:14:51 -0800543/**
544 * _hardware_dequeue: handles a request at hardware level
545 * @gadget: gadget
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300546 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800547 *
548 * This function returns an error code
549 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300550static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
David Lopoaa69a802008-11-17 14:14:51 -0800551{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300552 u32 tmptoken;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300553 struct td_node *node, *tmpnode;
554 unsigned remaining_length;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300555 unsigned actual = hwreq->req.length;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200556
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300557 if (hwreq->req.status != -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -0800558 return -EINVAL;
559
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300560 hwreq->req.status = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530561
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300562 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300563 tmptoken = le32_to_cpu(node->ptr->token);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300564 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300565 hwreq->req.status = -EALREADY;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530566 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300567 }
David Lopoaa69a802008-11-17 14:14:51 -0800568
Michael Grzeschik2e270412013-06-13 17:59:54 +0300569 remaining_length = (tmptoken & TD_TOTAL_BYTES);
570 remaining_length >>= __ffs(TD_TOTAL_BYTES);
571 actual -= remaining_length;
572
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300573 hwreq->req.status = tmptoken & TD_STATUS;
574 if ((TD_STATUS_HALTED & hwreq->req.status)) {
575 hwreq->req.status = -EPIPE;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300576 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300577 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
578 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300579 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300580 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
581 hwreq->req.status = -EILSEQ;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300582 break;
583 }
584
585 if (remaining_length) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300586 if (hwep->dir) {
587 hwreq->req.status = -EPROTO;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300588 break;
589 }
590 }
591 /*
592 * As the hardware could still address the freed td
593 * which will run the udc unusable, the cleanup of the
594 * td has to be delayed by one.
595 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300596 if (hwep->pending_td)
597 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300598
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300599 hwep->pending_td = node;
Michael Grzeschik2e270412013-06-13 17:59:54 +0300600 list_del_init(&node->td);
601 }
David Lopoaa69a802008-11-17 14:14:51 -0800602
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300603 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800604
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300605 hwreq->req.actual += actual;
David Lopoaa69a802008-11-17 14:14:51 -0800606
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300607 if (hwreq->req.status)
608 return hwreq->req.status;
David Lopoaa69a802008-11-17 14:14:51 -0800609
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300610 return hwreq->req.actual;
David Lopoaa69a802008-11-17 14:14:51 -0800611}
612
613/**
614 * _ep_nuke: dequeues all endpoint requests
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300615 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800616 *
617 * This function returns an error code
618 * Caller must hold lock
619 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300620static int _ep_nuke(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300621__releases(hwep->lock)
622__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800623{
Michael Grzeschik2e270412013-06-13 17:59:54 +0300624 struct td_node *node, *tmpnode;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300625 if (hwep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800626 return -EINVAL;
627
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300628 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800629
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300630 while (!list_empty(&hwep->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800631
632 /* pop oldest request */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300633 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
634 struct ci_hw_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300635
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300636 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
637 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300638 list_del_init(&node->td);
639 node->ptr = NULL;
640 kfree(node);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300641 }
642
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300643 list_del_init(&hwreq->queue);
644 hwreq->req.status = -ESHUTDOWN;
David Lopoaa69a802008-11-17 14:14:51 -0800645
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300646 if (hwreq->req.complete != NULL) {
647 spin_unlock(hwep->lock);
648 hwreq->req.complete(&hwep->ep, &hwreq->req);
649 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800650 }
651 }
Michael Grzeschik2e270412013-06-13 17:59:54 +0300652
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300653 if (hwep->pending_td)
654 free_pending_td(hwep);
Michael Grzeschik2e270412013-06-13 17:59:54 +0300655
David Lopoaa69a802008-11-17 14:14:51 -0800656 return 0;
657}
658
659/**
660 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
661 * @gadget: gadget
662 *
663 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800664 */
665static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800666{
667 struct usb_ep *ep;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300668 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530669 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800670
Richard Zhao26c696c2012-07-07 22:56:40 +0800671 spin_lock_irqsave(&ci->lock, flags);
672 ci->gadget.speed = USB_SPEED_UNKNOWN;
673 ci->remote_wakeup = 0;
674 ci->suspended = 0;
675 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530676
David Lopoaa69a802008-11-17 14:14:51 -0800677 /* flush all endpoints */
678 gadget_for_each_ep(ep, gadget) {
679 usb_ep_fifo_flush(ep);
680 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800681 usb_ep_fifo_flush(&ci->ep0out->ep);
682 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800683
David Lopoaa69a802008-11-17 14:14:51 -0800684 /* make sure to disable all endpoints */
685 gadget_for_each_ep(ep, gadget) {
686 usb_ep_disable(ep);
687 }
David Lopoaa69a802008-11-17 14:14:51 -0800688
Richard Zhao26c696c2012-07-07 22:56:40 +0800689 if (ci->status != NULL) {
690 usb_ep_free_request(&ci->ep0in->ep, ci->status);
691 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800692 }
693
David Lopoaa69a802008-11-17 14:14:51 -0800694 return 0;
695}
696
697/******************************************************************************
698 * ISR block
699 *****************************************************************************/
700/**
701 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800702 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800703 *
704 * This function resets USB engine after a bus reset occurred
705 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300706static void isr_reset_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +0800707__releases(ci->lock)
708__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800709{
David Lopoaa69a802008-11-17 14:14:51 -0800710 int retval;
711
Peter Chena3aee362013-10-09 14:39:52 +0800712 spin_unlock(&ci->lock);
Peter Chen92b336d2013-09-17 12:37:19 +0800713 if (ci->gadget.speed != USB_SPEED_UNKNOWN) {
714 if (ci->driver)
715 ci->driver->disconnect(&ci->gadget);
716 }
717
Richard Zhao26c696c2012-07-07 22:56:40 +0800718 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800719 if (retval)
720 goto done;
721
Richard Zhao26c696c2012-07-07 22:56:40 +0800722 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800723 if (retval)
724 goto done;
725
Richard Zhao26c696c2012-07-07 22:56:40 +0800726 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
727 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530728 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530729
Michael Grzeschikb9322252012-05-11 17:25:50 +0300730done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800731 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800732
David Lopoaa69a802008-11-17 14:14:51 -0800733 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800734 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800735}
736
737/**
738 * isr_get_status_complete: get_status request complete function
739 * @ep: endpoint
740 * @req: request handled
741 *
742 * Caller must release lock
743 */
744static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
745{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300746 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800747 return;
David Lopoaa69a802008-11-17 14:14:51 -0800748
749 kfree(req->buf);
750 usb_ep_free_request(ep, req);
751}
752
753/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200754 * _ep_queue: queues (submits) an I/O request to an endpoint
755 *
756 * Caller must hold lock
757 */
758static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
759 gfp_t __maybe_unused gfp_flags)
760{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300761 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
762 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
763 struct ci_hdrc *ci = hwep->ci;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200764 int retval = 0;
765
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300766 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200767 return -EINVAL;
768
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300769 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200770 if (req->length)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300771 hwep = (ci->ep0_dir == RX) ?
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200772 ci->ep0out : ci->ep0in;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300773 if (!list_empty(&hwep->qh.queue)) {
774 _ep_nuke(hwep);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200775 retval = -EOVERFLOW;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300776 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
777 _usb_addr(hwep));
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200778 }
779 }
780
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300781 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
782 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
783 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300784 return -EMSGSIZE;
785 }
786
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200787 /* first nuke then test link, e.g. previous status has not sent */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300788 if (!list_empty(&hwreq->queue)) {
789 dev_err(hwep->ci->dev, "request already in queue\n");
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200790 return -EBUSY;
791 }
792
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200793 /* push request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300794 hwreq->req.status = -EINPROGRESS;
795 hwreq->req.actual = 0;
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200796
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300797 retval = _hardware_enqueue(hwep, hwreq);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200798
799 if (retval == -EALREADY)
800 retval = 0;
801 if (!retval)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300802 list_add_tail(&hwreq->queue, &hwep->qh.queue);
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200803
804 return retval;
805}
806
807/**
David Lopoaa69a802008-11-17 14:14:51 -0800808 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800809 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800810 * @setup: setup request packet
811 *
812 * This function returns an error code
813 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300814static int isr_get_status_response(struct ci_hdrc *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800815 struct usb_ctrlrequest *setup)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300816__releases(hwep->lock)
817__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800818{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300819 struct ci_hw_ep *hwep = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800820 struct usb_request *req = NULL;
821 gfp_t gfp_flags = GFP_ATOMIC;
822 int dir, num, retval;
823
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300824 if (hwep == NULL || setup == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800825 return -EINVAL;
826
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300827 spin_unlock(hwep->lock);
828 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
829 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800830 if (req == NULL)
831 return -ENOMEM;
832
833 req->complete = isr_get_status_complete;
834 req->length = 2;
835 req->buf = kzalloc(req->length, gfp_flags);
836 if (req->buf == NULL) {
837 retval = -ENOMEM;
838 goto err_free_req;
839 }
840
841 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530842 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800843 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800844 retval = 0;
845 } else if ((setup->bRequestType & USB_RECIP_MASK) \
846 == USB_RECIP_ENDPOINT) {
847 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
848 TX : RX;
849 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800850 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800851 }
852 /* else do nothing; reserved for future use */
853
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300854 retval = _ep_queue(&hwep->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800855 if (retval)
856 goto err_free_buf;
857
858 return 0;
859
860 err_free_buf:
861 kfree(req->buf);
862 err_free_req:
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300863 spin_unlock(hwep->lock);
864 usb_ep_free_request(&hwep->ep, req);
865 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800866 return retval;
867}
868
869/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530870 * isr_setup_status_complete: setup_status request complete function
871 * @ep: endpoint
872 * @req: request handled
873 *
874 * Caller must release lock. Put the port in test mode if test mode
875 * feature is selected.
876 */
877static void
878isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
879{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300880 struct ci_hdrc *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530881 unsigned long flags;
882
Richard Zhao26c696c2012-07-07 22:56:40 +0800883 if (ci->setaddr) {
884 hw_usb_set_address(ci, ci->address);
885 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300886 }
887
Richard Zhao26c696c2012-07-07 22:56:40 +0800888 spin_lock_irqsave(&ci->lock, flags);
889 if (ci->test_mode)
890 hw_port_test_set(ci, ci->test_mode);
891 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530892}
893
894/**
David Lopoaa69a802008-11-17 14:14:51 -0800895 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800896 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800897 *
898 * This function returns an error code
899 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300900static int isr_setup_status_phase(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800901{
902 int retval;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300903 struct ci_hw_ep *hwep;
David Lopoaa69a802008-11-17 14:14:51 -0800904
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300905 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
Richard Zhao26c696c2012-07-07 22:56:40 +0800906 ci->status->context = ci;
907 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800908
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300909 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800910
911 return retval;
912}
913
914/**
915 * isr_tr_complete_low: transaction complete low level handler
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300916 * @hwep: endpoint
David Lopoaa69a802008-11-17 14:14:51 -0800917 *
918 * This function returns an error code
919 * Caller must hold lock
920 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300921static int isr_tr_complete_low(struct ci_hw_ep *hwep)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300922__releases(hwep->lock)
923__acquires(hwep->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800924{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300925 struct ci_hw_req *hwreq, *hwreqtemp;
926 struct ci_hw_ep *hweptemp = hwep;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300927 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800928
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300929 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530930 queue) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300931 retval = _hardware_dequeue(hwep, hwreq);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530932 if (retval < 0)
933 break;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +0300934 list_del_init(&hwreq->queue);
935 if (hwreq->req.complete != NULL) {
936 spin_unlock(hwep->lock);
937 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
938 hwreq->req.length)
939 hweptemp = hwep->ci->ep0in;
940 hwreq->req.complete(&hweptemp->ep, &hwreq->req);
941 spin_lock(hwep->lock);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530942 }
943 }
David Lopoaa69a802008-11-17 14:14:51 -0800944
Pavankumar Kondetief907482011-05-02 11:56:27 +0530945 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530946 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800947
David Lopoaa69a802008-11-17 14:14:51 -0800948 return retval;
949}
950
951/**
Peter Chend7b00e32014-03-11 13:47:37 +0800952 * isr_setup_packet_handler: setup packet handler
953 * @ci: UDC descriptor
954 *
955 * This function handles setup packet
956 */
957static void isr_setup_packet_handler(struct ci_hdrc *ci)
958__releases(ci->lock)
959__acquires(ci->lock)
960{
961 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
962 struct usb_ctrlrequest req;
963 int type, num, dir, err = -EINVAL;
964 u8 tmode = 0;
965
966 /*
967 * Flush data and handshake transactions of previous
968 * setup packet.
969 */
970 _ep_nuke(ci->ep0out);
971 _ep_nuke(ci->ep0in);
972
973 /* read_setup_packet */
974 do {
975 hw_test_and_set_setup_guard(ci);
976 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
977 } while (!hw_test_and_clear_setup_guard(ci));
978
979 type = req.bRequestType;
980
981 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
982
983 switch (req.bRequest) {
984 case USB_REQ_CLEAR_FEATURE:
985 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
986 le16_to_cpu(req.wValue) ==
987 USB_ENDPOINT_HALT) {
988 if (req.wLength != 0)
989 break;
990 num = le16_to_cpu(req.wIndex);
991 dir = num & USB_ENDPOINT_DIR_MASK;
992 num &= USB_ENDPOINT_NUMBER_MASK;
993 if (dir) /* TX */
994 num += ci->hw_ep_max / 2;
995 if (!ci->ci_hw_ep[num].wedge) {
996 spin_unlock(&ci->lock);
997 err = usb_ep_clear_halt(
998 &ci->ci_hw_ep[num].ep);
999 spin_lock(&ci->lock);
1000 if (err)
1001 break;
1002 }
1003 err = isr_setup_status_phase(ci);
1004 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1005 le16_to_cpu(req.wValue) ==
1006 USB_DEVICE_REMOTE_WAKEUP) {
1007 if (req.wLength != 0)
1008 break;
1009 ci->remote_wakeup = 0;
1010 err = isr_setup_status_phase(ci);
1011 } else {
1012 goto delegate;
1013 }
1014 break;
1015 case USB_REQ_GET_STATUS:
1016 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1017 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1018 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1019 goto delegate;
1020 if (le16_to_cpu(req.wLength) != 2 ||
1021 le16_to_cpu(req.wValue) != 0)
1022 break;
1023 err = isr_get_status_response(ci, &req);
1024 break;
1025 case USB_REQ_SET_ADDRESS:
1026 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1027 goto delegate;
1028 if (le16_to_cpu(req.wLength) != 0 ||
1029 le16_to_cpu(req.wIndex) != 0)
1030 break;
1031 ci->address = (u8)le16_to_cpu(req.wValue);
1032 ci->setaddr = true;
1033 err = isr_setup_status_phase(ci);
1034 break;
1035 case USB_REQ_SET_FEATURE:
1036 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1037 le16_to_cpu(req.wValue) ==
1038 USB_ENDPOINT_HALT) {
1039 if (req.wLength != 0)
1040 break;
1041 num = le16_to_cpu(req.wIndex);
1042 dir = num & USB_ENDPOINT_DIR_MASK;
1043 num &= USB_ENDPOINT_NUMBER_MASK;
1044 if (dir) /* TX */
1045 num += ci->hw_ep_max / 2;
1046
1047 spin_unlock(&ci->lock);
1048 err = usb_ep_set_halt(&ci->ci_hw_ep[num].ep);
1049 spin_lock(&ci->lock);
1050 if (!err)
1051 isr_setup_status_phase(ci);
1052 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1053 if (req.wLength != 0)
1054 break;
1055 switch (le16_to_cpu(req.wValue)) {
1056 case USB_DEVICE_REMOTE_WAKEUP:
1057 ci->remote_wakeup = 1;
1058 err = isr_setup_status_phase(ci);
1059 break;
1060 case USB_DEVICE_TEST_MODE:
1061 tmode = le16_to_cpu(req.wIndex) >> 8;
1062 switch (tmode) {
1063 case TEST_J:
1064 case TEST_K:
1065 case TEST_SE0_NAK:
1066 case TEST_PACKET:
1067 case TEST_FORCE_EN:
1068 ci->test_mode = tmode;
1069 err = isr_setup_status_phase(
1070 ci);
1071 break;
1072 default:
1073 break;
1074 }
1075 default:
1076 goto delegate;
1077 }
1078 } else {
1079 goto delegate;
1080 }
1081 break;
1082 default:
1083delegate:
1084 if (req.wLength == 0) /* no data phase */
1085 ci->ep0_dir = TX;
1086
1087 spin_unlock(&ci->lock);
1088 err = ci->driver->setup(&ci->gadget, &req);
1089 spin_lock(&ci->lock);
1090 break;
1091 }
1092
1093 if (err < 0) {
1094 spin_unlock(&ci->lock);
1095 if (usb_ep_set_halt(&hwep->ep))
1096 dev_err(ci->dev, "error: ep_set_halt\n");
1097 spin_lock(&ci->lock);
1098 }
1099}
1100
1101/**
David Lopoaa69a802008-11-17 14:14:51 -08001102 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +08001103 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -08001104 *
1105 * This function handles traffic events
1106 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001107static void isr_tr_complete_handler(struct ci_hdrc *ci)
Richard Zhao26c696c2012-07-07 22:56:40 +08001108__releases(ci->lock)
1109__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -08001110{
1111 unsigned i;
Peter Chend7b00e32014-03-11 13:47:37 +08001112 int err;
David Lopoaa69a802008-11-17 14:14:51 -08001113
Richard Zhao26c696c2012-07-07 22:56:40 +08001114 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001115 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
David Lopoaa69a802008-11-17 14:14:51 -08001116
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001117 if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001118 continue; /* not configured */
1119
Richard Zhao26c696c2012-07-07 22:56:40 +08001120 if (hw_test_and_clear_complete(ci, i)) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001121 err = isr_tr_complete_low(hwep);
1122 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
David Lopoaa69a802008-11-17 14:14:51 -08001123 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001124 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001125 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001126 spin_unlock(&ci->lock);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001127 if (usb_ep_set_halt(&hwep->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001128 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001129 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +08001130 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001131 }
1132 }
1133 }
1134
Peter Chen64fc06c2014-02-19 13:41:41 +08001135 /* Only handle setup packet below */
Peter Chend7b00e32014-03-11 13:47:37 +08001136 if (i == 0 &&
1137 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1138 isr_setup_packet_handler(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001139 }
1140}
1141
1142/******************************************************************************
1143 * ENDPT block
1144 *****************************************************************************/
1145/**
1146 * ep_enable: configure endpoint, making it usable
1147 *
1148 * Check usb_ep_enable() at "usb_gadget.h" for details
1149 */
1150static int ep_enable(struct usb_ep *ep,
1151 const struct usb_endpoint_descriptor *desc)
1152{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001153 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301154 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001155 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001156 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001157
David Lopoaa69a802008-11-17 14:14:51 -08001158 if (ep == NULL || desc == NULL)
1159 return -EINVAL;
1160
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001161 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001162
1163 /* only internal SW should enable ctrl endpts */
1164
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001165 hwep->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001166
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001167 if (!list_empty(&hwep->qh.queue))
1168 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001169
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001170 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1171 hwep->num = usb_endpoint_num(desc);
1172 hwep->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001173
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001174 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1175 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001176
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001177 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001178 cap |= QH_IOS;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001179 if (hwep->num)
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001180 cap |= QH_ZLT;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001181 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
Peter Chen2fc5a7d2014-01-10 13:51:32 +08001182 /*
1183 * For ISO-TX, we set mult at QH as the largest value, and use
1184 * MultO at TD as real mult value.
1185 */
1186 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1187 cap |= 3 << __ffs(QH_MULT);
David Lopoaa69a802008-11-17 14:14:51 -08001188
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001189 hwep->qh.ptr->cap = cpu_to_le32(cap);
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001190
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001191 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001192
Peter Chen64fc06c2014-02-19 13:41:41 +08001193 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1194 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1195 retval = -EINVAL;
1196 }
1197
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301198 /*
1199 * Enable endpoints in the HW other than ep0 as ep0
1200 * is always enabled
1201 */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001202 if (hwep->num)
1203 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1204 hwep->type);
David Lopoaa69a802008-11-17 14:14:51 -08001205
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001206 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001207 return retval;
1208}
1209
1210/**
1211 * ep_disable: endpoint is no longer usable
1212 *
1213 * Check usb_ep_disable() at "usb_gadget.h" for details
1214 */
1215static int ep_disable(struct usb_ep *ep)
1216{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001217 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001218 int direction, retval = 0;
1219 unsigned long flags;
1220
David Lopoaa69a802008-11-17 14:14:51 -08001221 if (ep == NULL)
1222 return -EINVAL;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001223 else if (hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001224 return -EBUSY;
1225
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001226 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001227
1228 /* only internal SW should disable ctrl endpts */
1229
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001230 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001231 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001232 retval |= _ep_nuke(hwep);
1233 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001234
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001235 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1236 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001237
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001238 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001239
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001240 hwep->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001241
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001242 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001243 return retval;
1244}
1245
1246/**
1247 * ep_alloc_request: allocate a request object to use with this endpoint
1248 *
1249 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1250 */
1251static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1252{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001253 struct ci_hw_req *hwreq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001254
Alexander Shishkin0f089092012-05-08 23:29:02 +03001255 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001256 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001257
Alexander Shishkin8e229782013-06-24 14:46:36 +03001258 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001259 if (hwreq != NULL) {
1260 INIT_LIST_HEAD(&hwreq->queue);
1261 INIT_LIST_HEAD(&hwreq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001262 }
1263
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001264 return (hwreq == NULL) ? NULL : &hwreq->req;
David Lopoaa69a802008-11-17 14:14:51 -08001265}
1266
1267/**
1268 * ep_free_request: frees a request object
1269 *
1270 * Check usb_ep_free_request() at "usb_gadget.h" for details
1271 */
1272static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1273{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001274 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1275 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001276 struct td_node *node, *tmpnode;
David Lopoaa69a802008-11-17 14:14:51 -08001277 unsigned long flags;
1278
David Lopoaa69a802008-11-17 14:14:51 -08001279 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001280 return;
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001281 } else if (!list_empty(&hwreq->queue)) {
1282 dev_err(hwep->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001283 return;
1284 }
1285
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001286 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001287
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001288 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1289 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
Michael Grzeschik2e270412013-06-13 17:59:54 +03001290 list_del_init(&node->td);
1291 node->ptr = NULL;
1292 kfree(node);
1293 }
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001294
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001295 kfree(hwreq);
David Lopoaa69a802008-11-17 14:14:51 -08001296
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001297 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001298}
1299
1300/**
1301 * ep_queue: queues (submits) an I/O request to an endpoint
1302 *
1303 * Check usb_ep_queue()* at usb_gadget.h" for details
1304 */
1305static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1306 gfp_t __maybe_unused gfp_flags)
1307{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001308 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001309 int retval = 0;
1310 unsigned long flags;
1311
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001312 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001313 return -EINVAL;
1314
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001315 spin_lock_irqsave(hwep->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001316 retval = _ep_queue(ep, req, gfp_flags);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001317 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001318 return retval;
1319}
1320
1321/**
1322 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1323 *
1324 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1325 */
1326static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1327{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001328 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1329 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
David Lopoaa69a802008-11-17 14:14:51 -08001330 unsigned long flags;
1331
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001332 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1333 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1334 list_empty(&hwep->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001335 return -EINVAL;
1336
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001337 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001338
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001339 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001340
1341 /* pop request */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001342 list_del_init(&hwreq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001343
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001344 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001345
David Lopoaa69a802008-11-17 14:14:51 -08001346 req->status = -ECONNRESET;
1347
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001348 if (hwreq->req.complete != NULL) {
1349 spin_unlock(hwep->lock);
1350 hwreq->req.complete(&hwep->ep, &hwreq->req);
1351 spin_lock(hwep->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001352 }
1353
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001354 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001355 return 0;
1356}
1357
1358/**
1359 * ep_set_halt: sets the endpoint halt feature
1360 *
1361 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1362 */
1363static int ep_set_halt(struct usb_ep *ep, int value)
1364{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001365 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001366 int direction, retval = 0;
1367 unsigned long flags;
1368
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001369 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001370 return -EINVAL;
1371
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001372 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001373 return -EOPNOTSUPP;
1374
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001375 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001376
1377#ifndef STALL_IN
1378 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001379 if (value && hwep->type == USB_ENDPOINT_XFER_BULK && hwep->dir == TX &&
1380 !list_empty(&hwep->qh.queue)) {
1381 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001382 return -EAGAIN;
1383 }
1384#endif
1385
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001386 direction = hwep->dir;
David Lopoaa69a802008-11-17 14:14:51 -08001387 do {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001388 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001389
1390 if (!value)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001391 hwep->wedge = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001392
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001393 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1394 hwep->dir = (hwep->dir == TX) ? RX : TX;
David Lopoaa69a802008-11-17 14:14:51 -08001395
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001396 } while (hwep->dir != direction);
David Lopoaa69a802008-11-17 14:14:51 -08001397
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001398 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001399 return retval;
1400}
1401
1402/**
1403 * ep_set_wedge: sets the halt feature and ignores clear requests
1404 *
1405 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1406 */
1407static int ep_set_wedge(struct usb_ep *ep)
1408{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001409 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001410 unsigned long flags;
1411
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001412 if (ep == NULL || hwep->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001413 return -EINVAL;
1414
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001415 spin_lock_irqsave(hwep->lock, flags);
1416 hwep->wedge = 1;
1417 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001418
1419 return usb_ep_set_halt(ep);
1420}
1421
1422/**
1423 * ep_fifo_flush: flushes contents of a fifo
1424 *
1425 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1426 */
1427static void ep_fifo_flush(struct usb_ep *ep)
1428{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001429 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001430 unsigned long flags;
1431
David Lopoaa69a802008-11-17 14:14:51 -08001432 if (ep == NULL) {
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001433 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
David Lopoaa69a802008-11-17 14:14:51 -08001434 return;
1435 }
1436
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001437 spin_lock_irqsave(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001438
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001439 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001440
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001441 spin_unlock_irqrestore(hwep->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001442}
1443
1444/**
1445 * Endpoint-specific part of the API to the USB controller hardware
1446 * Check "usb_gadget.h" for details
1447 */
1448static const struct usb_ep_ops usb_ep_ops = {
1449 .enable = ep_enable,
1450 .disable = ep_disable,
1451 .alloc_request = ep_alloc_request,
1452 .free_request = ep_free_request,
1453 .queue = ep_queue,
1454 .dequeue = ep_dequeue,
1455 .set_halt = ep_set_halt,
1456 .set_wedge = ep_set_wedge,
1457 .fifo_flush = ep_fifo_flush,
1458};
1459
1460/******************************************************************************
1461 * GADGET block
1462 *****************************************************************************/
Alexander Shishkin8e229782013-06-24 14:46:36 +03001463static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301464{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001465 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301466 unsigned long flags;
1467 int gadget_ready = 0;
1468
Richard Zhao26c696c2012-07-07 22:56:40 +08001469 spin_lock_irqsave(&ci->lock, flags);
1470 ci->vbus_active = is_active;
1471 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301472 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001473 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301474
1475 if (gadget_ready) {
1476 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301477 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001478 hw_device_reset(ci, USBMODE_CM_DC);
1479 hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chena107f8c2013-08-14 12:44:11 +03001480 dev_dbg(ci->dev, "Connected to host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301481 } else {
Peter Chen92b336d2013-09-17 12:37:19 +08001482 if (ci->driver)
1483 ci->driver->disconnect(&ci->gadget);
Richard Zhao26c696c2012-07-07 22:56:40 +08001484 hw_device_state(ci, 0);
1485 if (ci->platdata->notify_event)
1486 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001487 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001488 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301489 pm_runtime_put_sync(&_gadget->dev);
Peter Chena107f8c2013-08-14 12:44:11 +03001490 dev_dbg(ci->dev, "Disconnected from host\n");
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301491 }
1492 }
1493
1494 return 0;
1495}
1496
Alexander Shishkin8e229782013-06-24 14:46:36 +03001497static int ci_udc_wakeup(struct usb_gadget *_gadget)
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301498{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001499 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301500 unsigned long flags;
1501 int ret = 0;
1502
Richard Zhao26c696c2012-07-07 22:56:40 +08001503 spin_lock_irqsave(&ci->lock, flags);
1504 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301505 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301506 goto out;
1507 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001508 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301509 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301510 goto out;
1511 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001512 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301513out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001514 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301515 return ret;
1516}
1517
Alexander Shishkin8e229782013-06-24 14:46:36 +03001518static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301519{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001520 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301521
Richard Zhao26c696c2012-07-07 22:56:40 +08001522 if (ci->transceiver)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001523 return usb_phy_set_power(ci->transceiver, ma);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301524 return -ENOTSUPP;
1525}
1526
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001527/* Change Data+ pullup status
1528 * this func is used by usb_gadget_connect/disconnet
1529 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001530static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001531{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001532 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001533
Peter Chen4a647832013-08-14 12:44:15 +03001534 if (!ci->vbus_active)
1535 return -EOPNOTSUPP;
1536
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001537 if (is_on)
1538 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1539 else
1540 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1541
1542 return 0;
1543}
1544
Alexander Shishkin8e229782013-06-24 14:46:36 +03001545static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001546 struct usb_gadget_driver *driver);
Alexander Shishkin8e229782013-06-24 14:46:36 +03001547static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001548 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001549/**
1550 * Device operations part of the API to the USB controller hardware,
1551 * which don't involve endpoints (or i/o)
1552 * Check "usb_gadget.h" for details
1553 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301554static const struct usb_gadget_ops usb_gadget_ops = {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001555 .vbus_session = ci_udc_vbus_session,
1556 .wakeup = ci_udc_wakeup,
1557 .pullup = ci_udc_pullup,
1558 .vbus_draw = ci_udc_vbus_draw,
1559 .udc_start = ci_udc_start,
1560 .udc_stop = ci_udc_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301561};
David Lopoaa69a802008-11-17 14:14:51 -08001562
Alexander Shishkin8e229782013-06-24 14:46:36 +03001563static int init_eps(struct ci_hdrc *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001564{
1565 int retval = 0, i, j;
1566
Richard Zhao26c696c2012-07-07 22:56:40 +08001567 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001568 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001569 int k = i + j * ci->hw_ep_max/2;
Alexander Shishkin8e229782013-06-24 14:46:36 +03001570 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001571
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001572 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001573 (j == TX) ? "in" : "out");
1574
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001575 hwep->ci = ci;
1576 hwep->lock = &ci->lock;
1577 hwep->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001578
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001579 hwep->ep.name = hwep->name;
1580 hwep->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001581 /*
1582 * for ep0: maxP defined in desc, for other
1583 * eps, maxP is set by epautoconfig() called
1584 * by gadget layer
1585 */
Robert Baldygae117e742013-12-13 12:23:38 +01001586 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001587
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001588 INIT_LIST_HEAD(&hwep->qh.queue);
1589 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1590 &hwep->qh.dma);
1591 if (hwep->qh.ptr == NULL)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001592 retval = -ENOMEM;
1593 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001594 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001595
1596 /*
1597 * set up shorthands for ep0 out and in endpoints,
1598 * don't add to gadget's ep_list
1599 */
1600 if (i == 0) {
1601 if (j == RX)
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001602 ci->ep0out = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001603 else
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001604 ci->ep0in = hwep;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001605
Robert Baldygae117e742013-12-13 12:23:38 +01001606 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001607 continue;
1608 }
1609
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001610 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001611 }
1612
1613 return retval;
1614}
1615
Alexander Shishkin8e229782013-06-24 14:46:36 +03001616static void destroy_eps(struct ci_hdrc *ci)
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001617{
1618 int i;
1619
1620 for (i = 0; i < ci->hw_ep_max; i++) {
Alexander Shishkin8e229782013-06-24 14:46:36 +03001621 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001622
Peter Chen4a295672013-09-10 15:34:39 +08001623 if (hwep->pending_td)
1624 free_pending_td(hwep);
Alexander Shishkin2dbc5c42013-06-13 18:00:03 +03001625 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001626 }
1627}
1628
David Lopoaa69a802008-11-17 14:14:51 -08001629/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001630 * ci_udc_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001631 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001632 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001633 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001634 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001635 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001636static int ci_udc_start(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001637 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001638{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001639 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301640 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001641 int retval = -ENOMEM;
1642
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001643 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001644 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001645
David Lopoaa69a802008-11-17 14:14:51 -08001646
Richard Zhao26c696c2012-07-07 22:56:40 +08001647 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1648 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301649 if (retval)
1650 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001651
Richard Zhao26c696c2012-07-07 22:56:40 +08001652 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1653 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301654 if (retval)
1655 return retval;
David Lopoaa69a802008-11-17 14:14:51 -08001656
Richard Zhao26c696c2012-07-07 22:56:40 +08001657 ci->driver = driver;
1658 pm_runtime_get_sync(&ci->gadget.dev);
Peter Chend268e9b2013-08-14 12:44:14 +03001659 if (ci->vbus_active) {
Peter Chen65b2fb32013-10-08 10:30:17 +08001660 spin_lock_irqsave(&ci->lock, flags);
Peter Chend268e9b2013-08-14 12:44:14 +03001661 hw_device_reset(ci, USBMODE_CM_DC);
1662 } else {
1663 pm_runtime_put_sync(&ci->gadget.dev);
Peter Chen65b2fb32013-10-08 10:30:17 +08001664 return retval;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301665 }
1666
Richard Zhao26c696c2012-07-07 22:56:40 +08001667 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Peter Chen65b2fb32013-10-08 10:30:17 +08001668 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301669 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001670 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001671
David Lopoaa69a802008-11-17 14:14:51 -08001672 return retval;
1673}
David Lopoaa69a802008-11-17 14:14:51 -08001674
1675/**
Alexander Shishkin8e229782013-06-24 14:46:36 +03001676 * ci_udc_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001677 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001678static int ci_udc_stop(struct usb_gadget *gadget,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001679 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001680{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001681 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001682 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001683
Richard Zhao26c696c2012-07-07 22:56:40 +08001684 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001685
Peter Chend268e9b2013-08-14 12:44:14 +03001686 if (ci->vbus_active) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001687 hw_device_state(ci, 0);
1688 if (ci->platdata->notify_event)
1689 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +03001690 CI_HDRC_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001691 spin_unlock_irqrestore(&ci->lock, flags);
1692 _gadget_stop_activity(&ci->gadget);
1693 spin_lock_irqsave(&ci->lock, flags);
1694 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301695 }
David Lopoaa69a802008-11-17 14:14:51 -08001696
Peter Chenf84839d2013-09-17 12:37:20 +08001697 ci->driver = NULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001698 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001699
David Lopoaa69a802008-11-17 14:14:51 -08001700 return 0;
1701}
David Lopoaa69a802008-11-17 14:14:51 -08001702
1703/******************************************************************************
1704 * BUS block
1705 *****************************************************************************/
1706/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001707 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001708 *
1709 * This function returns IRQ_HANDLED if the IRQ has been handled
1710 * It locks access to registers
1711 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001712static irqreturn_t udc_irq(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001713{
David Lopoaa69a802008-11-17 14:14:51 -08001714 irqreturn_t retval;
1715 u32 intr;
1716
Richard Zhao26c696c2012-07-07 22:56:40 +08001717 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001718 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001719
Richard Zhao26c696c2012-07-07 22:56:40 +08001720 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301721
Alexander Shishkin8e229782013-06-24 14:46:36 +03001722 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001723 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001724 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001725 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301726 return IRQ_NONE;
1727 }
1728 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001729 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001730
Alexander Shishkine443b332012-05-11 17:25:46 +03001731 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001732 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001733 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001734 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001735
David Lopoaa69a802008-11-17 14:14:51 -08001736 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001737 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001738 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001739 if (ci->suspended && ci->driver->resume) {
1740 spin_unlock(&ci->lock);
1741 ci->driver->resume(&ci->gadget);
1742 spin_lock(&ci->lock);
1743 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301744 }
David Lopoaa69a802008-11-17 14:14:51 -08001745 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001746
1747 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001748 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001749
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301750 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001751 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1752 ci->driver->suspend) {
1753 ci->suspended = 1;
1754 spin_unlock(&ci->lock);
1755 ci->driver->suspend(&ci->gadget);
1756 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301757 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301758 }
David Lopoaa69a802008-11-17 14:14:51 -08001759 retval = IRQ_HANDLED;
1760 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001761 retval = IRQ_NONE;
1762 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001763 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001764
1765 return retval;
1766}
1767
1768/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001769 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001770 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001771 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001772static int udc_start(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001773{
Richard Zhao26c696c2012-07-07 22:56:40 +08001774 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001775 int retval = 0;
1776
Richard Zhao26c696c2012-07-07 22:56:40 +08001777 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001778
Richard Zhao26c696c2012-07-07 22:56:40 +08001779 ci->gadget.ops = &usb_gadget_ops;
1780 ci->gadget.speed = USB_SPEED_UNKNOWN;
1781 ci->gadget.max_speed = USB_SPEED_HIGH;
1782 ci->gadget.is_otg = 0;
1783 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001784
Richard Zhao26c696c2012-07-07 22:56:40 +08001785 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001786
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001787 /* alloc resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001788 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1789 sizeof(struct ci_hw_qh),
1790 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001791 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001792 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001793
Alexander Shishkin8e229782013-06-24 14:46:36 +03001794 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1795 sizeof(struct ci_hw_td),
1796 64, CI_HDRC_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001797 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001798 retval = -ENOMEM;
1799 goto free_qh_pool;
1800 }
1801
Richard Zhao26c696c2012-07-07 22:56:40 +08001802 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001803 if (retval)
1804 goto free_pools;
1805
Richard Zhao26c696c2012-07-07 22:56:40 +08001806 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301807
Richard Zhao26c696c2012-07-07 22:56:40 +08001808 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001809 if (retval)
Peter Chen74475ed2013-09-24 12:47:53 +08001810 goto destroy_eps;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001811
Richard Zhao26c696c2012-07-07 22:56:40 +08001812 pm_runtime_no_callbacks(&ci->gadget.dev);
1813 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001814
David Lopoaa69a802008-11-17 14:14:51 -08001815 return retval;
1816
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001817destroy_eps:
1818 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001819free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001820 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001821free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001822 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001823 return retval;
1824}
1825
1826/**
Peter Chen3f124d22013-08-14 12:44:07 +03001827 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
David Lopoaa69a802008-11-17 14:14:51 -08001828 *
1829 * No interrupts active, the IRQ has been released
1830 */
Peter Chen3f124d22013-08-14 12:44:07 +03001831void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001832{
Peter Chen3f124d22013-08-14 12:44:07 +03001833 if (!ci->roles[CI_ROLE_GADGET])
David Lopoaa69a802008-11-17 14:14:51 -08001834 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001835
Richard Zhao26c696c2012-07-07 22:56:40 +08001836 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001837
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001838 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001839
Richard Zhao26c696c2012-07-07 22:56:40 +08001840 dma_pool_destroy(ci->td_pool);
1841 dma_pool_destroy(ci->qh_pool);
Peter Chen3f124d22013-08-14 12:44:07 +03001842}
1843
1844static int udc_id_switch_for_device(struct ci_hdrc *ci)
1845{
1846 if (ci->is_otg) {
1847 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1848 ci_enable_otg_interrupt(ci, OTGSC_BSVIE);
1849 }
1850
1851 return 0;
1852}
1853
1854static void udc_id_switch_for_host(struct ci_hdrc *ci)
1855{
1856 if (ci->is_otg) {
1857 /* host doesn't care B_SESSION_VALID event */
1858 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1859 ci_disable_otg_interrupt(ci, OTGSC_BSVIE);
1860 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001861}
David Lopoaa69a802008-11-17 14:14:51 -08001862
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001863/**
1864 * ci_hdrc_gadget_init - initialize device related bits
1865 * ci: the controller
1866 *
Peter Chen3f124d22013-08-14 12:44:07 +03001867 * This function initializes the gadget, if the device is "device capable".
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001868 */
Alexander Shishkin8e229782013-06-24 14:46:36 +03001869int ci_hdrc_gadget_init(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001870{
1871 struct ci_role_driver *rdrv;
1872
1873 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1874 return -ENXIO;
1875
1876 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1877 if (!rdrv)
1878 return -ENOMEM;
1879
Peter Chen3f124d22013-08-14 12:44:07 +03001880 rdrv->start = udc_id_switch_for_device;
1881 rdrv->stop = udc_id_switch_for_host;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001882 rdrv->irq = udc_irq;
1883 rdrv->name = "gadget";
1884 ci->roles[CI_ROLE_GADGET] = rdrv;
1885
Peter Chen3f124d22013-08-14 12:44:07 +03001886 return udc_start(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001887}