blob: b501346484aeb16ab5e92e91e9aaf2458bf04784 [file] [log] [blame]
David Lopoaa69a802008-11-17 14:14:51 -08001/*
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03002 * udc.c - ChipIdea UDC driver
David Lopoaa69a802008-11-17 14:14:51 -08003 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020013#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080014#include <linux/device.h>
15#include <linux/dmapool.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053016#include <linux/err.h>
Alexander Shishkin5b083192013-03-30 02:46:18 +020017#include <linux/irqreturn.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053020#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Pavankumar Kondetif01ef572010-12-07 17:54:02 +053023#include <linux/usb/otg.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030024#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080025
Alexander Shishkine443b332012-05-11 17:25:46 +030026#include "ci.h"
27#include "udc.h"
28#include "bits.h"
29#include "debug.h"
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
Richard Zhao26c696c2012-07-07 22:56:40 +080064static inline int ep_to_bit(struct ci13xxx *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 */
Richard Zhao26c696c2012-07-07 22:56:40 +080080static int hw_device_state(struct ci13xxx *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);
David Lopoaa69a802008-11-17 14:14:51 -080087 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080088 hw_write(ci, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080089 }
90 return 0;
91}
92
93/**
94 * hw_ep_flush: flush endpoint fifo (execute without interruption)
95 * @num: endpoint number
96 * @dir: endpoint direction
97 *
98 * This function returns an error code
99 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800100static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800101{
102 int n = hw_ep_bit(num, dir);
103
104 do {
105 /* flush any pending transfer */
Richard Zhao26c696c2012-07-07 22:56:40 +0800106 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
107 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800108 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800109 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800110
111 return 0;
112}
113
114/**
115 * hw_ep_disable: disables endpoint (execute without interruption)
116 * @num: endpoint number
117 * @dir: endpoint direction
118 *
119 * This function returns an error code
120 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800121static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800122{
Richard Zhao26c696c2012-07-07 22:56:40 +0800123 hw_ep_flush(ci, num, dir);
124 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300125 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800126 return 0;
127}
128
129/**
130 * hw_ep_enable: enables endpoint (execute without interruption)
131 * @num: endpoint number
132 * @dir: endpoint direction
133 * @type: endpoint type
134 *
135 * This function returns an error code
136 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800137static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800138{
139 u32 mask, data;
140
141 if (dir) {
142 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200143 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800144
145 mask |= ENDPTCTRL_TXS; /* unstall */
146 mask |= ENDPTCTRL_TXR; /* reset data toggle */
147 data |= ENDPTCTRL_TXR;
148 mask |= ENDPTCTRL_TXE; /* enable */
149 data |= ENDPTCTRL_TXE;
150 } else {
151 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200152 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800153
154 mask |= ENDPTCTRL_RXS; /* unstall */
155 mask |= ENDPTCTRL_RXR; /* reset data toggle */
156 data |= ENDPTCTRL_RXR;
157 mask |= ENDPTCTRL_RXE; /* enable */
158 data |= ENDPTCTRL_RXE;
159 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800160 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800161 return 0;
162}
163
164/**
165 * hw_ep_get_halt: return endpoint halt status
166 * @num: endpoint number
167 * @dir: endpoint direction
168 *
169 * This function returns 1 if endpoint halted
170 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800171static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800172{
173 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
174
Richard Zhao26c696c2012-07-07 22:56:40 +0800175 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800176}
177
178/**
David Lopoaa69a802008-11-17 14:14:51 -0800179 * hw_test_and_clear_setup_status: test & clear setup status (execute without
180 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200181 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800182 *
183 * This function returns setup status
184 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800185static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800186{
Richard Zhao26c696c2012-07-07 22:56:40 +0800187 n = ep_to_bit(ci, n);
188 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800189}
190
191/**
192 * hw_ep_prime: primes endpoint (execute without interruption)
193 * @num: endpoint number
194 * @dir: endpoint direction
195 * @is_ctrl: true if control endpoint
196 *
197 * This function returns an error code
198 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800199static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800200{
201 int n = hw_ep_bit(num, dir);
202
Richard Zhao26c696c2012-07-07 22:56:40 +0800203 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800204 return -EAGAIN;
205
Richard Zhao26c696c2012-07-07 22:56:40 +0800206 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800207
Richard Zhao26c696c2012-07-07 22:56:40 +0800208 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800209 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800210 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800211 return -EAGAIN;
212
213 /* status shoult be tested according with manual but it doesn't work */
214 return 0;
215}
216
217/**
218 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
219 * without interruption)
220 * @num: endpoint number
221 * @dir: endpoint direction
222 * @value: true => stall, false => unstall
223 *
224 * This function returns an error code
225 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800226static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800227{
228 if (value != 0 && value != 1)
229 return -EINVAL;
230
231 do {
Alexander Shishkin262c1632012-05-08 23:28:59 +0300232 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800233 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
234 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
235
236 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800237 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300238 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800239 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800240
241 return 0;
242}
243
244/**
David Lopoaa69a802008-11-17 14:14:51 -0800245 * hw_is_port_high_speed: test if port is high speed
246 *
247 * This function returns true if high speed port
248 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800249static int hw_port_is_high_speed(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800250{
Richard Zhao26c696c2012-07-07 22:56:40 +0800251 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
252 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800253}
254
255/**
David Lopoaa69a802008-11-17 14:14:51 -0800256 * hw_read_intr_enable: returns interrupt enable register
257 *
258 * This function returns register data
259 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800260static u32 hw_read_intr_enable(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800261{
Richard Zhao26c696c2012-07-07 22:56:40 +0800262 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800263}
264
265/**
266 * hw_read_intr_status: returns interrupt status register
267 *
268 * This function returns register data
269 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800270static u32 hw_read_intr_status(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800271{
Richard Zhao26c696c2012-07-07 22:56:40 +0800272 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800273}
274
275/**
David Lopoaa69a802008-11-17 14:14:51 -0800276 * hw_test_and_clear_complete: test & clear complete status (execute without
277 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200278 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800279 *
280 * This function returns complete status
281 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800282static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800283{
Richard Zhao26c696c2012-07-07 22:56:40 +0800284 n = ep_to_bit(ci, n);
285 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800286}
287
288/**
289 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
290 * without interruption)
291 *
292 * This function returns active interrutps
293 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800294static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800295{
Richard Zhao26c696c2012-07-07 22:56:40 +0800296 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800297
Richard Zhao26c696c2012-07-07 22:56:40 +0800298 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800299 return reg;
300}
301
302/**
303 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
304 * interruption)
305 *
306 * This function returns guard value
307 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800308static int hw_test_and_clear_setup_guard(struct ci13xxx *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, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800311}
312
313/**
314 * hw_test_and_set_setup_guard: test & set setup guard (execute without
315 * interruption)
316 *
317 * This function returns guard value
318 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800319static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800320{
Richard Zhao26c696c2012-07-07 22:56:40 +0800321 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800322}
323
324/**
325 * hw_usb_set_address: configures USB address (execute without interruption)
326 * @value: new USB address
327 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300328 * This function explicitly sets the address, without the "USBADRA" (advance)
329 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800330 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800331static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800332{
Richard Zhao26c696c2012-07-07 22:56:40 +0800333 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200334 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800335}
336
337/**
338 * hw_usb_reset: restart device after a bus reset (execute without
339 * interruption)
340 *
341 * This function returns an error code
342 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800343static int hw_usb_reset(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800344{
Richard Zhao26c696c2012-07-07 22:56:40 +0800345 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800346
347 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800348 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800349
350 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800351 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800352
353 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800354 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800355
356 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800357 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800358 udelay(10); /* not RTOS friendly */
359
360 /* reset all endpoints ? */
361
362 /* reset internal status and wait for further instructions
363 no need to verify the port reset status (ESS does it) */
364
365 return 0;
366}
367
368/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800369 * UTIL block
370 *****************************************************************************/
371/**
372 * _usb_addr: calculates endpoint address from direction & number
373 * @ep: endpoint
374 */
375static inline u8 _usb_addr(struct ci13xxx_ep *ep)
376{
377 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
378}
379
380/**
381 * _hardware_queue: configures a request at hardware level
382 * @gadget: gadget
383 * @mEp: endpoint
384 *
385 * This function returns an error code
386 */
387static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
388{
Richard Zhao26c696c2012-07-07 22:56:40 +0800389 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -0800390 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530391 int ret = 0;
392 unsigned length = mReq->req.length;
David Lopoaa69a802008-11-17 14:14:51 -0800393
David Lopoaa69a802008-11-17 14:14:51 -0800394 /* don't queue twice */
395 if (mReq->req.status == -EALREADY)
396 return -EALREADY;
397
David Lopoaa69a802008-11-17 14:14:51 -0800398 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800399
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530400 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
401 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
402 &mReq->zdma);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300403 if (mReq->zptr == NULL)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530404 return -ENOMEM;
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300405
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530406 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200407 mReq->zptr->next = cpu_to_le32(TD_TERMINATE);
408 mReq->zptr->token = cpu_to_le32(TD_STATUS_ACTIVE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530409 if (!mReq->req.no_interrupt)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200410 mReq->zptr->token |= cpu_to_le32(TD_IOC);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530411 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800412 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300413 if (ret)
414 return ret;
415
David Lopoaa69a802008-11-17 14:14:51 -0800416 /*
417 * TD configuration
418 * TODO - handle requests which spawns into several TDs
419 */
420 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200421 mReq->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
422 mReq->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
423 mReq->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530424 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200425 mReq->ptr->next = cpu_to_le32(mReq->zdma);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530426 } else {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200427 mReq->ptr->next = cpu_to_le32(TD_TERMINATE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530428 if (!mReq->req.no_interrupt)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200429 mReq->ptr->token |= cpu_to_le32(TD_IOC);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530430 }
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200431 mReq->ptr->page[0] = cpu_to_le32(mReq->req.dma);
Michael Grzeschikb983e512013-03-30 12:54:10 +0200432 for (i = 1; i < TD_PAGE_COUNT; i++) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200433 u32 page = mReq->req.dma + i * CI13XXX_PAGE_SIZE;
434 page &= ~TD_RESERVED_MASK;
435 mReq->ptr->page[i] = cpu_to_le32(page);
436 }
David Lopoaa69a802008-11-17 14:14:51 -0800437
Michael Grzeschika9c17432013-04-04 13:13:46 +0300438 wmb();
439
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530440 if (!list_empty(&mEp->qh.queue)) {
441 struct ci13xxx_req *mReqPrev;
442 int n = hw_ep_bit(mEp->num, mEp->dir);
443 int tmp_stat;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200444 u32 next = mReq->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530445
446 mReqPrev = list_entry(mEp->qh.queue.prev,
447 struct ci13xxx_req, queue);
448 if (mReqPrev->zptr)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200449 mReqPrev->zptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530450 else
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200451 mReqPrev->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530452 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800453 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530454 goto done;
455 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800456 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
457 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
458 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
459 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530460 if (tmp_stat)
461 goto done;
462 }
463
464 /* QH configuration */
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200465 mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200466 mEp->qh.ptr->td.token &=
467 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800468
469 wmb(); /* synchronize before ep prime */
470
Richard Zhao26c696c2012-07-07 22:56:40 +0800471 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800472 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530473done:
474 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800475}
476
477/**
478 * _hardware_dequeue: handles a request at hardware level
479 * @gadget: gadget
480 * @mEp: endpoint
481 *
482 * This function returns an error code
483 */
484static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
485{
Michael Grzeschik9e506432013-03-30 12:54:07 +0200486 u32 tmptoken = le32_to_cpu(mReq->ptr->token);
487
David Lopoaa69a802008-11-17 14:14:51 -0800488 if (mReq->req.status != -EALREADY)
489 return -EINVAL;
490
Michael Grzeschik9e506432013-03-30 12:54:07 +0200491 if ((TD_STATUS_ACTIVE & tmptoken) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530492 return -EBUSY;
493
494 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200495 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530496 return -EBUSY;
497 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
498 mReq->zptr = NULL;
499 }
David Lopoaa69a802008-11-17 14:14:51 -0800500
501 mReq->req.status = 0;
502
Richard Zhao26c696c2012-07-07 22:56:40 +0800503 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800504
Michael Grzeschik9e506432013-03-30 12:54:07 +0200505 mReq->req.status = tmptoken & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530506 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800507 mReq->req.status = -1;
508 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
509 mReq->req.status = -1;
510 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
511 mReq->req.status = -1;
512
Michael Grzeschik9e506432013-03-30 12:54:07 +0200513 mReq->req.actual = tmptoken & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200514 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800515 mReq->req.actual = mReq->req.length - mReq->req.actual;
516 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
517
518 return mReq->req.actual;
519}
520
521/**
522 * _ep_nuke: dequeues all endpoint requests
523 * @mEp: endpoint
524 *
525 * This function returns an error code
526 * Caller must hold lock
527 */
528static int _ep_nuke(struct ci13xxx_ep *mEp)
529__releases(mEp->lock)
530__acquires(mEp->lock)
531{
David Lopoaa69a802008-11-17 14:14:51 -0800532 if (mEp == NULL)
533 return -EINVAL;
534
Richard Zhao26c696c2012-07-07 22:56:40 +0800535 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800536
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530537 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800538
539 /* pop oldest request */
540 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530541 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800542 struct ci13xxx_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300543
544 if (mReq->zptr) {
545 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
546 mReq->zptr = NULL;
547 }
548
David Lopoaa69a802008-11-17 14:14:51 -0800549 list_del_init(&mReq->queue);
550 mReq->req.status = -ESHUTDOWN;
551
Artem Leonenko7c25a822010-12-14 23:46:55 -0800552 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800553 spin_unlock(mEp->lock);
554 mReq->req.complete(&mEp->ep, &mReq->req);
555 spin_lock(mEp->lock);
556 }
557 }
558 return 0;
559}
560
561/**
562 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
563 * @gadget: gadget
564 *
565 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800566 */
567static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800568{
569 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800570 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530571 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800572
Richard Zhao26c696c2012-07-07 22:56:40 +0800573 spin_lock_irqsave(&ci->lock, flags);
574 ci->gadget.speed = USB_SPEED_UNKNOWN;
575 ci->remote_wakeup = 0;
576 ci->suspended = 0;
577 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530578
David Lopoaa69a802008-11-17 14:14:51 -0800579 /* flush all endpoints */
580 gadget_for_each_ep(ep, gadget) {
581 usb_ep_fifo_flush(ep);
582 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800583 usb_ep_fifo_flush(&ci->ep0out->ep);
584 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800585
Richard Zhao26c696c2012-07-07 22:56:40 +0800586 if (ci->driver)
587 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800588
589 /* make sure to disable all endpoints */
590 gadget_for_each_ep(ep, gadget) {
591 usb_ep_disable(ep);
592 }
David Lopoaa69a802008-11-17 14:14:51 -0800593
Richard Zhao26c696c2012-07-07 22:56:40 +0800594 if (ci->status != NULL) {
595 usb_ep_free_request(&ci->ep0in->ep, ci->status);
596 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800597 }
598
David Lopoaa69a802008-11-17 14:14:51 -0800599 return 0;
600}
601
602/******************************************************************************
603 * ISR block
604 *****************************************************************************/
605/**
606 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800607 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800608 *
609 * This function resets USB engine after a bus reset occurred
610 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800611static void isr_reset_handler(struct ci13xxx *ci)
612__releases(ci->lock)
613__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800614{
David Lopoaa69a802008-11-17 14:14:51 -0800615 int retval;
616
Richard Zhao26c696c2012-07-07 22:56:40 +0800617 spin_unlock(&ci->lock);
618 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800619 if (retval)
620 goto done;
621
Richard Zhao26c696c2012-07-07 22:56:40 +0800622 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800623 if (retval)
624 goto done;
625
Richard Zhao26c696c2012-07-07 22:56:40 +0800626 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
627 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530628 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530629
Michael Grzeschikb9322252012-05-11 17:25:50 +0300630done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800631 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800632
David Lopoaa69a802008-11-17 14:14:51 -0800633 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800634 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800635}
636
637/**
638 * isr_get_status_complete: get_status request complete function
639 * @ep: endpoint
640 * @req: request handled
641 *
642 * Caller must release lock
643 */
644static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
645{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300646 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800647 return;
David Lopoaa69a802008-11-17 14:14:51 -0800648
649 kfree(req->buf);
650 usb_ep_free_request(ep, req);
651}
652
653/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200654 * _ep_queue: queues (submits) an I/O request to an endpoint
655 *
656 * Caller must hold lock
657 */
658static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
659 gfp_t __maybe_unused gfp_flags)
660{
661 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
662 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
663 struct ci13xxx *ci = mEp->ci;
664 int retval = 0;
665
666 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
667 return -EINVAL;
668
669 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
670 if (req->length)
671 mEp = (ci->ep0_dir == RX) ?
672 ci->ep0out : ci->ep0in;
673 if (!list_empty(&mEp->qh.queue)) {
674 _ep_nuke(mEp);
675 retval = -EOVERFLOW;
676 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
677 _usb_addr(mEp));
678 }
679 }
680
681 /* first nuke then test link, e.g. previous status has not sent */
682 if (!list_empty(&mReq->queue)) {
683 dev_err(mEp->ci->dev, "request already in queue\n");
684 return -EBUSY;
685 }
686
Michael Grzeschikb983e512013-03-30 12:54:10 +0200687 if (req->length > (TD_PAGE_COUNT - 1) * CI13XXX_PAGE_SIZE) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200688 dev_err(mEp->ci->dev, "request bigger than one td\n");
689 return -EMSGSIZE;
690 }
691
692 /* push request */
693 mReq->req.status = -EINPROGRESS;
694 mReq->req.actual = 0;
695
696 retval = _hardware_enqueue(mEp, mReq);
697
698 if (retval == -EALREADY)
699 retval = 0;
700 if (!retval)
701 list_add_tail(&mReq->queue, &mEp->qh.queue);
702
703 return retval;
704}
705
706/**
David Lopoaa69a802008-11-17 14:14:51 -0800707 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800708 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800709 * @setup: setup request packet
710 *
711 * This function returns an error code
712 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800713static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800714 struct usb_ctrlrequest *setup)
715__releases(mEp->lock)
716__acquires(mEp->lock)
717{
Richard Zhao26c696c2012-07-07 22:56:40 +0800718 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800719 struct usb_request *req = NULL;
720 gfp_t gfp_flags = GFP_ATOMIC;
721 int dir, num, retval;
722
David Lopoaa69a802008-11-17 14:14:51 -0800723 if (mEp == NULL || setup == NULL)
724 return -EINVAL;
725
726 spin_unlock(mEp->lock);
727 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
728 spin_lock(mEp->lock);
729 if (req == NULL)
730 return -ENOMEM;
731
732 req->complete = isr_get_status_complete;
733 req->length = 2;
734 req->buf = kzalloc(req->length, gfp_flags);
735 if (req->buf == NULL) {
736 retval = -ENOMEM;
737 goto err_free_req;
738 }
739
740 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530741 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800742 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800743 retval = 0;
744 } else if ((setup->bRequestType & USB_RECIP_MASK) \
745 == USB_RECIP_ENDPOINT) {
746 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
747 TX : RX;
748 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800749 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800750 }
751 /* else do nothing; reserved for future use */
752
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200753 retval = _ep_queue(&mEp->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800754 if (retval)
755 goto err_free_buf;
756
757 return 0;
758
759 err_free_buf:
760 kfree(req->buf);
761 err_free_req:
762 spin_unlock(mEp->lock);
763 usb_ep_free_request(&mEp->ep, req);
764 spin_lock(mEp->lock);
765 return retval;
766}
767
768/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530769 * isr_setup_status_complete: setup_status request complete function
770 * @ep: endpoint
771 * @req: request handled
772 *
773 * Caller must release lock. Put the port in test mode if test mode
774 * feature is selected.
775 */
776static void
777isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
778{
Richard Zhao26c696c2012-07-07 22:56:40 +0800779 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530780 unsigned long flags;
781
Richard Zhao26c696c2012-07-07 22:56:40 +0800782 if (ci->setaddr) {
783 hw_usb_set_address(ci, ci->address);
784 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300785 }
786
Richard Zhao26c696c2012-07-07 22:56:40 +0800787 spin_lock_irqsave(&ci->lock, flags);
788 if (ci->test_mode)
789 hw_port_test_set(ci, ci->test_mode);
790 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530791}
792
793/**
David Lopoaa69a802008-11-17 14:14:51 -0800794 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800795 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800796 *
797 * This function returns an error code
798 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800799static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800800{
801 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530802 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800803
Richard Zhao26c696c2012-07-07 22:56:40 +0800804 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
805 ci->status->context = ci;
806 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800807
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200808 retval = _ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800809
810 return retval;
811}
812
813/**
814 * isr_tr_complete_low: transaction complete low level handler
815 * @mEp: endpoint
816 *
817 * This function returns an error code
818 * Caller must hold lock
819 */
820static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
821__releases(mEp->lock)
822__acquires(mEp->lock)
823{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530824 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530825 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300826 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800827
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530828 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
829 queue) {
830 retval = _hardware_dequeue(mEp, mReq);
831 if (retval < 0)
832 break;
833 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530834 if (mReq->req.complete != NULL) {
835 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530836 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
837 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800838 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530839 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530840 spin_lock(mEp->lock);
841 }
842 }
David Lopoaa69a802008-11-17 14:14:51 -0800843
Pavankumar Kondetief907482011-05-02 11:56:27 +0530844 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530845 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800846
David Lopoaa69a802008-11-17 14:14:51 -0800847 return retval;
848}
849
850/**
851 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800852 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800853 *
854 * This function handles traffic events
855 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800856static void isr_tr_complete_handler(struct ci13xxx *ci)
857__releases(ci->lock)
858__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800859{
860 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530861 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800862
Richard Zhao26c696c2012-07-07 22:56:40 +0800863 for (i = 0; i < ci->hw_ep_max; i++) {
864 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530865 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800866 struct usb_ctrlrequest req;
867
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200868 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800869 continue; /* not configured */
870
Richard Zhao26c696c2012-07-07 22:56:40 +0800871 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800872 err = isr_tr_complete_low(mEp);
873 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
874 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800875 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800876 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800877 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800878 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800879 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700880 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800881 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800882 }
883 }
884 }
885
886 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800887 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800888 continue;
889
890 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800891 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800892 continue;
893 }
894
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530895 /*
896 * Flush data and handshake transactions of previous
897 * setup packet.
898 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800899 _ep_nuke(ci->ep0out);
900 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530901
David Lopoaa69a802008-11-17 14:14:51 -0800902 /* read_setup_packet */
903 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800904 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530905 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800906 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800907
908 type = req.bRequestType;
909
Richard Zhao26c696c2012-07-07 22:56:40 +0800910 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800911
David Lopoaa69a802008-11-17 14:14:51 -0800912 switch (req.bRequest) {
913 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530914 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
915 le16_to_cpu(req.wValue) ==
916 USB_ENDPOINT_HALT) {
917 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800918 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530919 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530920 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530921 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530922 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800923 num += ci->hw_ep_max/2;
924 if (!ci->ci13xxx_ep[num].wedge) {
925 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530926 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800927 &ci->ci13xxx_ep[num].ep);
928 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530929 if (err)
930 break;
931 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800932 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530933 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
934 le16_to_cpu(req.wValue) ==
935 USB_DEVICE_REMOTE_WAKEUP) {
936 if (req.wLength != 0)
937 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800938 ci->remote_wakeup = 0;
939 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530940 } else {
941 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800942 }
David Lopoaa69a802008-11-17 14:14:51 -0800943 break;
944 case USB_REQ_GET_STATUS:
945 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
946 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
947 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
948 goto delegate;
949 if (le16_to_cpu(req.wLength) != 2 ||
950 le16_to_cpu(req.wValue) != 0)
951 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800952 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800953 break;
954 case USB_REQ_SET_ADDRESS:
955 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
956 goto delegate;
957 if (le16_to_cpu(req.wLength) != 0 ||
958 le16_to_cpu(req.wIndex) != 0)
959 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800960 ci->address = (u8)le16_to_cpu(req.wValue);
961 ci->setaddr = true;
962 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800963 break;
964 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530965 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
966 le16_to_cpu(req.wValue) ==
967 USB_ENDPOINT_HALT) {
968 if (req.wLength != 0)
969 break;
970 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530971 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530972 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530973 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800974 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800975
Richard Zhao26c696c2012-07-07 22:56:40 +0800976 spin_unlock(&ci->lock);
977 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
978 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530979 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800980 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530981 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530982 if (req.wLength != 0)
983 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530984 switch (le16_to_cpu(req.wValue)) {
985 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800986 ci->remote_wakeup = 1;
987 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530988 break;
989 case USB_DEVICE_TEST_MODE:
990 tmode = le16_to_cpu(req.wIndex) >> 8;
991 switch (tmode) {
992 case TEST_J:
993 case TEST_K:
994 case TEST_SE0_NAK:
995 case TEST_PACKET:
996 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800997 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530998 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800999 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301000 break;
1001 default:
1002 break;
1003 }
1004 default:
1005 goto delegate;
1006 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301007 } else {
1008 goto delegate;
1009 }
David Lopoaa69a802008-11-17 14:14:51 -08001010 break;
1011 default:
1012delegate:
1013 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001014 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001015
Richard Zhao26c696c2012-07-07 22:56:40 +08001016 spin_unlock(&ci->lock);
1017 err = ci->driver->setup(&ci->gadget, &req);
1018 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001019 break;
1020 }
1021
1022 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001023 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001024 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001025 dev_err(ci->dev, "error: ep_set_halt\n");
1026 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001027 }
1028 }
1029}
1030
1031/******************************************************************************
1032 * ENDPT block
1033 *****************************************************************************/
1034/**
1035 * ep_enable: configure endpoint, making it usable
1036 *
1037 * Check usb_ep_enable() at "usb_gadget.h" for details
1038 */
1039static int ep_enable(struct usb_ep *ep,
1040 const struct usb_endpoint_descriptor *desc)
1041{
1042 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301043 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001044 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001045 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001046
David Lopoaa69a802008-11-17 14:14:51 -08001047 if (ep == NULL || desc == NULL)
1048 return -EINVAL;
1049
1050 spin_lock_irqsave(mEp->lock, flags);
1051
1052 /* only internal SW should enable ctrl endpts */
1053
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001054 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001055
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301056 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001057 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001058
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001059 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1060 mEp->num = usb_endpoint_num(desc);
1061 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001062
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001063 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001064
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301065 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001066 cap |= QH_IOS;
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001067 if (mEp->num)
1068 cap |= QH_ZLT;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001069 cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001070
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001071 mEp->qh.ptr->cap = cpu_to_le32(cap);
1072
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001073 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001074
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301075 /*
1076 * Enable endpoints in the HW other than ep0 as ep0
1077 * is always enabled
1078 */
1079 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001080 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001081
1082 spin_unlock_irqrestore(mEp->lock, flags);
1083 return retval;
1084}
1085
1086/**
1087 * ep_disable: endpoint is no longer usable
1088 *
1089 * Check usb_ep_disable() at "usb_gadget.h" for details
1090 */
1091static int ep_disable(struct usb_ep *ep)
1092{
1093 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1094 int direction, retval = 0;
1095 unsigned long flags;
1096
David Lopoaa69a802008-11-17 14:14:51 -08001097 if (ep == NULL)
1098 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001099 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001100 return -EBUSY;
1101
1102 spin_lock_irqsave(mEp->lock, flags);
1103
1104 /* only internal SW should disable ctrl endpts */
1105
1106 direction = mEp->dir;
1107 do {
David Lopoaa69a802008-11-17 14:14:51 -08001108 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001109 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001110
1111 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1112 mEp->dir = (mEp->dir == TX) ? RX : TX;
1113
1114 } while (mEp->dir != direction);
1115
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001116 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001117
1118 spin_unlock_irqrestore(mEp->lock, flags);
1119 return retval;
1120}
1121
1122/**
1123 * ep_alloc_request: allocate a request object to use with this endpoint
1124 *
1125 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1126 */
1127static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1128{
1129 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1130 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001131
Alexander Shishkin0f089092012-05-08 23:29:02 +03001132 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001133 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001134
David Lopoaa69a802008-11-17 14:14:51 -08001135 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1136 if (mReq != NULL) {
1137 INIT_LIST_HEAD(&mReq->queue);
1138
1139 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1140 &mReq->dma);
1141 if (mReq->ptr == NULL) {
1142 kfree(mReq);
1143 mReq = NULL;
1144 }
1145 }
1146
David Lopoaa69a802008-11-17 14:14:51 -08001147 return (mReq == NULL) ? NULL : &mReq->req;
1148}
1149
1150/**
1151 * ep_free_request: frees a request object
1152 *
1153 * Check usb_ep_free_request() at "usb_gadget.h" for details
1154 */
1155static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1156{
1157 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1158 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1159 unsigned long flags;
1160
David Lopoaa69a802008-11-17 14:14:51 -08001161 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001162 return;
1163 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001164 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001165 return;
1166 }
1167
1168 spin_lock_irqsave(mEp->lock, flags);
1169
1170 if (mReq->ptr)
1171 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1172 kfree(mReq);
1173
David Lopoaa69a802008-11-17 14:14:51 -08001174 spin_unlock_irqrestore(mEp->lock, flags);
1175}
1176
1177/**
1178 * ep_queue: queues (submits) an I/O request to an endpoint
1179 *
1180 * Check usb_ep_queue()* at usb_gadget.h" for details
1181 */
1182static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1183 gfp_t __maybe_unused gfp_flags)
1184{
1185 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001186 int retval = 0;
1187 unsigned long flags;
1188
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001189 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001190 return -EINVAL;
1191
1192 spin_lock_irqsave(mEp->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001193 retval = _ep_queue(ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -08001194 spin_unlock_irqrestore(mEp->lock, flags);
1195 return retval;
1196}
1197
1198/**
1199 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1200 *
1201 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1202 */
1203static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1204{
1205 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1206 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1207 unsigned long flags;
1208
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301209 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001210 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301211 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001212 return -EINVAL;
1213
1214 spin_lock_irqsave(mEp->lock, flags);
1215
Richard Zhao26c696c2012-07-07 22:56:40 +08001216 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001217
1218 /* pop request */
1219 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001220
Richard Zhao26c696c2012-07-07 22:56:40 +08001221 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001222
David Lopoaa69a802008-11-17 14:14:51 -08001223 req->status = -ECONNRESET;
1224
Artem Leonenko7c25a822010-12-14 23:46:55 -08001225 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001226 spin_unlock(mEp->lock);
1227 mReq->req.complete(&mEp->ep, &mReq->req);
1228 spin_lock(mEp->lock);
1229 }
1230
1231 spin_unlock_irqrestore(mEp->lock, flags);
1232 return 0;
1233}
1234
1235/**
1236 * ep_set_halt: sets the endpoint halt feature
1237 *
1238 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1239 */
1240static int ep_set_halt(struct usb_ep *ep, int value)
1241{
1242 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1243 int direction, retval = 0;
1244 unsigned long flags;
1245
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001246 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001247 return -EINVAL;
1248
1249 spin_lock_irqsave(mEp->lock, flags);
1250
1251#ifndef STALL_IN
1252 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1253 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301254 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001255 spin_unlock_irqrestore(mEp->lock, flags);
1256 return -EAGAIN;
1257 }
1258#endif
1259
1260 direction = mEp->dir;
1261 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001262 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001263
1264 if (!value)
1265 mEp->wedge = 0;
1266
1267 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1268 mEp->dir = (mEp->dir == TX) ? RX : TX;
1269
1270 } while (mEp->dir != direction);
1271
1272 spin_unlock_irqrestore(mEp->lock, flags);
1273 return retval;
1274}
1275
1276/**
1277 * ep_set_wedge: sets the halt feature and ignores clear requests
1278 *
1279 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1280 */
1281static int ep_set_wedge(struct usb_ep *ep)
1282{
1283 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1284 unsigned long flags;
1285
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001286 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001287 return -EINVAL;
1288
1289 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001290 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001291 spin_unlock_irqrestore(mEp->lock, flags);
1292
1293 return usb_ep_set_halt(ep);
1294}
1295
1296/**
1297 * ep_fifo_flush: flushes contents of a fifo
1298 *
1299 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1300 */
1301static void ep_fifo_flush(struct usb_ep *ep)
1302{
1303 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1304 unsigned long flags;
1305
David Lopoaa69a802008-11-17 14:14:51 -08001306 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001307 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001308 return;
1309 }
1310
1311 spin_lock_irqsave(mEp->lock, flags);
1312
Richard Zhao26c696c2012-07-07 22:56:40 +08001313 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001314
1315 spin_unlock_irqrestore(mEp->lock, flags);
1316}
1317
1318/**
1319 * Endpoint-specific part of the API to the USB controller hardware
1320 * Check "usb_gadget.h" for details
1321 */
1322static const struct usb_ep_ops usb_ep_ops = {
1323 .enable = ep_enable,
1324 .disable = ep_disable,
1325 .alloc_request = ep_alloc_request,
1326 .free_request = ep_free_request,
1327 .queue = ep_queue,
1328 .dequeue = ep_dequeue,
1329 .set_halt = ep_set_halt,
1330 .set_wedge = ep_set_wedge,
1331 .fifo_flush = ep_fifo_flush,
1332};
1333
1334/******************************************************************************
1335 * GADGET block
1336 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301337static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1338{
Richard Zhao26c696c2012-07-07 22:56:40 +08001339 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301340 unsigned long flags;
1341 int gadget_ready = 0;
1342
Richard Zhao26c696c2012-07-07 22:56:40 +08001343 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301344 return -EOPNOTSUPP;
1345
Richard Zhao26c696c2012-07-07 22:56:40 +08001346 spin_lock_irqsave(&ci->lock, flags);
1347 ci->vbus_active = is_active;
1348 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301349 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001350 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301351
1352 if (gadget_ready) {
1353 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301354 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001355 hw_device_reset(ci, USBMODE_CM_DC);
1356 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301357 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001358 hw_device_state(ci, 0);
1359 if (ci->platdata->notify_event)
1360 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301361 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001362 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301363 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301364 }
1365 }
1366
1367 return 0;
1368}
1369
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301370static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1371{
Richard Zhao26c696c2012-07-07 22:56:40 +08001372 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301373 unsigned long flags;
1374 int ret = 0;
1375
Richard Zhao26c696c2012-07-07 22:56:40 +08001376 spin_lock_irqsave(&ci->lock, flags);
1377 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301378 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301379 goto out;
1380 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001381 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301382 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301383 goto out;
1384 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001385 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301386out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001387 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301388 return ret;
1389}
1390
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301391static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1392{
Richard Zhao26c696c2012-07-07 22:56:40 +08001393 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301394
Richard Zhao26c696c2012-07-07 22:56:40 +08001395 if (ci->transceiver)
1396 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301397 return -ENOTSUPP;
1398}
1399
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001400/* Change Data+ pullup status
1401 * this func is used by usb_gadget_connect/disconnet
1402 */
1403static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1404{
1405 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1406
1407 if (is_on)
1408 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1409 else
1410 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1411
1412 return 0;
1413}
1414
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001415static int ci13xxx_start(struct usb_gadget *gadget,
1416 struct usb_gadget_driver *driver);
1417static int ci13xxx_stop(struct usb_gadget *gadget,
1418 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001419/**
1420 * Device operations part of the API to the USB controller hardware,
1421 * which don't involve endpoints (or i/o)
1422 * Check "usb_gadget.h" for details
1423 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301424static const struct usb_gadget_ops usb_gadget_ops = {
1425 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301426 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001427 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301428 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001429 .udc_start = ci13xxx_start,
1430 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301431};
David Lopoaa69a802008-11-17 14:14:51 -08001432
Richard Zhao26c696c2012-07-07 22:56:40 +08001433static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001434{
1435 int retval = 0, i, j;
1436
Richard Zhao26c696c2012-07-07 22:56:40 +08001437 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001438 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001439 int k = i + j * ci->hw_ep_max/2;
1440 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001441
1442 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1443 (j == TX) ? "in" : "out");
1444
Richard Zhao26c696c2012-07-07 22:56:40 +08001445 mEp->ci = ci;
1446 mEp->lock = &ci->lock;
1447 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001448
1449 mEp->ep.name = mEp->name;
1450 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001451 /*
1452 * for ep0: maxP defined in desc, for other
1453 * eps, maxP is set by epautoconfig() called
1454 * by gadget layer
1455 */
1456 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001457
1458 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001459 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001460 &mEp->qh.dma);
1461 if (mEp->qh.ptr == NULL)
1462 retval = -ENOMEM;
1463 else
1464 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1465
1466 /*
1467 * set up shorthands for ep0 out and in endpoints,
1468 * don't add to gadget's ep_list
1469 */
1470 if (i == 0) {
1471 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001472 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001473 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001474 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001475
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001476 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001477 continue;
1478 }
1479
Richard Zhao26c696c2012-07-07 22:56:40 +08001480 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001481 }
1482
1483 return retval;
1484}
1485
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001486static void destroy_eps(struct ci13xxx *ci)
1487{
1488 int i;
1489
1490 for (i = 0; i < ci->hw_ep_max; i++) {
1491 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1492
1493 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1494 }
1495}
1496
David Lopoaa69a802008-11-17 14:14:51 -08001497/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001498 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001499 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001500 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001501 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001502 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001503 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001504static int ci13xxx_start(struct usb_gadget *gadget,
1505 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001506{
Richard Zhao26c696c2012-07-07 22:56:40 +08001507 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301508 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001509 int retval = -ENOMEM;
1510
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001511 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001512 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001513
David Lopoaa69a802008-11-17 14:14:51 -08001514
Richard Zhao26c696c2012-07-07 22:56:40 +08001515 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1516 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301517 if (retval)
1518 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001519
Richard Zhao26c696c2012-07-07 22:56:40 +08001520 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1521 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301522 if (retval)
1523 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001524 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001525
Richard Zhao26c696c2012-07-07 22:56:40 +08001526 ci->driver = driver;
1527 pm_runtime_get_sync(&ci->gadget.dev);
1528 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1529 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001530 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001531 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301532 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001533 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301534 goto done;
1535 }
1536 }
1537
Richard Zhao26c696c2012-07-07 22:56:40 +08001538 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301539 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001540 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001541
1542 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001543 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001544 return retval;
1545}
David Lopoaa69a802008-11-17 14:14:51 -08001546
1547/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001548 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001549 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001550static int ci13xxx_stop(struct usb_gadget *gadget,
1551 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001552{
Richard Zhao26c696c2012-07-07 22:56:40 +08001553 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001554 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001555
Richard Zhao26c696c2012-07-07 22:56:40 +08001556 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001557
Richard Zhao26c696c2012-07-07 22:56:40 +08001558 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1559 ci->vbus_active) {
1560 hw_device_state(ci, 0);
1561 if (ci->platdata->notify_event)
1562 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301563 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001564 ci->driver = NULL;
1565 spin_unlock_irqrestore(&ci->lock, flags);
1566 _gadget_stop_activity(&ci->gadget);
1567 spin_lock_irqsave(&ci->lock, flags);
1568 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301569 }
David Lopoaa69a802008-11-17 14:14:51 -08001570
Richard Zhao26c696c2012-07-07 22:56:40 +08001571 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001572
David Lopoaa69a802008-11-17 14:14:51 -08001573 return 0;
1574}
David Lopoaa69a802008-11-17 14:14:51 -08001575
1576/******************************************************************************
1577 * BUS block
1578 *****************************************************************************/
1579/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001580 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001581 *
1582 * This function returns IRQ_HANDLED if the IRQ has been handled
1583 * It locks access to registers
1584 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001585static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001586{
David Lopoaa69a802008-11-17 14:14:51 -08001587 irqreturn_t retval;
1588 u32 intr;
1589
Richard Zhao26c696c2012-07-07 22:56:40 +08001590 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001591 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001592
Richard Zhao26c696c2012-07-07 22:56:40 +08001593 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301594
Richard Zhao26c696c2012-07-07 22:56:40 +08001595 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1596 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001597 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001598 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301599 return IRQ_NONE;
1600 }
1601 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001602 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001603
Alexander Shishkine443b332012-05-11 17:25:46 +03001604 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001605 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001606 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001607 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001608
David Lopoaa69a802008-11-17 14:14:51 -08001609 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001610 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001611 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001612 if (ci->suspended && ci->driver->resume) {
1613 spin_unlock(&ci->lock);
1614 ci->driver->resume(&ci->gadget);
1615 spin_lock(&ci->lock);
1616 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301617 }
David Lopoaa69a802008-11-17 14:14:51 -08001618 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001619
1620 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001621 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001622
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301623 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001624 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1625 ci->driver->suspend) {
1626 ci->suspended = 1;
1627 spin_unlock(&ci->lock);
1628 ci->driver->suspend(&ci->gadget);
1629 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301630 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301631 }
David Lopoaa69a802008-11-17 14:14:51 -08001632 retval = IRQ_HANDLED;
1633 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001634 retval = IRQ_NONE;
1635 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001636 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001637
1638 return retval;
1639}
1640
1641/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001642 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001643 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001644 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001645static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001646{
Richard Zhao26c696c2012-07-07 22:56:40 +08001647 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001648 int retval = 0;
1649
Richard Zhao26c696c2012-07-07 22:56:40 +08001650 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001651
Richard Zhao26c696c2012-07-07 22:56:40 +08001652 ci->gadget.ops = &usb_gadget_ops;
1653 ci->gadget.speed = USB_SPEED_UNKNOWN;
1654 ci->gadget.max_speed = USB_SPEED_HIGH;
1655 ci->gadget.is_otg = 0;
1656 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001657
Richard Zhao26c696c2012-07-07 22:56:40 +08001658 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001659
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001660 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001661 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001662 sizeof(struct ci13xxx_qh),
1663 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001664 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001665 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001666
Richard Zhao26c696c2012-07-07 22:56:40 +08001667 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001668 sizeof(struct ci13xxx_td),
1669 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001670 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001671 retval = -ENOMEM;
1672 goto free_qh_pool;
1673 }
1674
Richard Zhao26c696c2012-07-07 22:56:40 +08001675 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001676 if (retval)
1677 goto free_pools;
1678
Richard Zhao26c696c2012-07-07 22:56:40 +08001679 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301680
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001681 if (ci->global_phy) {
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001682 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001683 if (IS_ERR(ci->transceiver))
1684 ci->transceiver = NULL;
1685 }
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301686
Richard Zhao26c696c2012-07-07 22:56:40 +08001687 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1688 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301689 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001690 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301691 }
1692 }
1693
Richard Zhao26c696c2012-07-07 22:56:40 +08001694 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1695 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301696 if (retval)
1697 goto put_transceiver;
1698 }
1699
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001700 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001701 retval = otg_set_peripheral(ci->transceiver->otg,
1702 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301703 if (retval)
Greg Kroah-Hartman64dc9e22013-04-05 15:18:00 -07001704 goto put_transceiver;
David Lopoaa69a802008-11-17 14:14:51 -08001705 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001706
Richard Zhao26c696c2012-07-07 22:56:40 +08001707 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001708 if (retval)
1709 goto remove_trans;
1710
Richard Zhao26c696c2012-07-07 22:56:40 +08001711 pm_runtime_no_callbacks(&ci->gadget.dev);
1712 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001713
David Lopoaa69a802008-11-17 14:14:51 -08001714 return retval;
1715
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001716remove_trans:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001717 if (ci->transceiver) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001718 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001719 if (ci->global_phy)
1720 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001721 }
1722
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001723 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301724put_transceiver:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001725 if (ci->transceiver && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001726 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001727destroy_eps:
1728 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001729free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001730 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001731free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001732 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001733 return retval;
1734}
1735
1736/**
1737 * udc_remove: parent remove must call this to remove UDC
1738 *
1739 * No interrupts active, the IRQ has been released
1740 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001741static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001742{
Richard Zhao26c696c2012-07-07 22:56:40 +08001743 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001744 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001745
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001747
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001748 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001749
Richard Zhao26c696c2012-07-07 22:56:40 +08001750 dma_pool_destroy(ci->td_pool);
1751 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001752
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001753 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001754 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001755 if (ci->global_phy)
1756 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301757 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001758 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001759 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001760}
David Lopoaa69a802008-11-17 14:14:51 -08001761
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001762/**
1763 * ci_hdrc_gadget_init - initialize device related bits
1764 * ci: the controller
1765 *
1766 * This function enables the gadget role, if the device is "device capable".
1767 */
1768int ci_hdrc_gadget_init(struct ci13xxx *ci)
1769{
1770 struct ci_role_driver *rdrv;
1771
1772 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1773 return -ENXIO;
1774
1775 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1776 if (!rdrv)
1777 return -ENOMEM;
1778
1779 rdrv->start = udc_start;
1780 rdrv->stop = udc_stop;
1781 rdrv->irq = udc_irq;
1782 rdrv->name = "gadget";
1783 ci->roles[CI_ROLE_GADGET] = rdrv;
1784
1785 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001786}