blob: ba8284e2a2371c355983c5d89dc8129e42f049f8 [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>
16#include <linux/dma-mapping.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053017#include <linux/err.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/init.h>
Alexander Shishkin62bb84e2012-05-08 23:29:01 +030019#include <linux/platform_device.h>
20#include <linux/module.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/interrupt.h>
David Lopoaa69a802008-11-17 14:14:51 -080022#include <linux/io.h>
23#include <linux/irq.h>
24#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053026#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080027#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Pavankumar Kondetif01ef572010-12-07 17:54:02 +053029#include <linux/usb/otg.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030030#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080031
Alexander Shishkine443b332012-05-11 17:25:46 +030032#include "ci.h"
33#include "udc.h"
34#include "bits.h"
35#include "debug.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020036
David Lopoaa69a802008-11-17 14:14:51 -080037/* control endpoint description */
38static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053039ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080040 .bLength = USB_DT_ENDPOINT_SIZE,
41 .bDescriptorType = USB_DT_ENDPOINT,
42
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053043 .bEndpointAddress = USB_DIR_OUT,
44 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
45 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
46};
47
48static const struct usb_endpoint_descriptor
49ctrl_endpt_in_desc = {
50 .bLength = USB_DT_ENDPOINT_SIZE,
51 .bDescriptorType = USB_DT_ENDPOINT,
52
53 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080054 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
55 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
56};
57
David Lopoaa69a802008-11-17 14:14:51 -080058/**
59 * hw_ep_bit: calculates the bit number
60 * @num: endpoint number
61 * @dir: endpoint direction
62 *
63 * This function returns bit number
64 */
65static inline int hw_ep_bit(int num, int dir)
66{
67 return num + (dir ? 16 : 0);
68}
69
Richard Zhao26c696c2012-07-07 22:56:40 +080070static inline int ep_to_bit(struct ci13xxx *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020071{
Richard Zhao26c696c2012-07-07 22:56:40 +080072 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020073
Richard Zhao26c696c2012-07-07 22:56:40 +080074 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020075 n += fill;
76
77 return n;
78}
79
David Lopoaa69a802008-11-17 14:14:51 -080080/**
David Lopoaa69a802008-11-17 14:14:51 -080081 * hw_device_state: enables/disables interrupts & starts/stops device (execute
82 * without interruption)
83 * @dma: 0 => disable, !0 => enable and set dma engine
84 *
85 * This function returns an error code
86 */
Richard Zhao26c696c2012-07-07 22:56:40 +080087static int hw_device_state(struct ci13xxx *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080088{
89 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080090 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080091 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080092 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080093 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
Richard Zhao26c696c2012-07-07 22:56:40 +080094 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
David Lopoaa69a802008-11-17 14:14:51 -080095 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080096 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
97 hw_write(ci, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080098 }
99 return 0;
100}
101
102/**
103 * hw_ep_flush: flush endpoint fifo (execute without interruption)
104 * @num: endpoint number
105 * @dir: endpoint direction
106 *
107 * This function returns an error code
108 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800109static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800110{
111 int n = hw_ep_bit(num, dir);
112
113 do {
114 /* flush any pending transfer */
Richard Zhao26c696c2012-07-07 22:56:40 +0800115 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
116 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800117 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800118 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800119
120 return 0;
121}
122
123/**
124 * hw_ep_disable: disables endpoint (execute without interruption)
125 * @num: endpoint number
126 * @dir: endpoint direction
127 *
128 * This function returns an error code
129 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800130static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800131{
Richard Zhao26c696c2012-07-07 22:56:40 +0800132 hw_ep_flush(ci, num, dir);
133 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300134 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800135 return 0;
136}
137
138/**
139 * hw_ep_enable: enables endpoint (execute without interruption)
140 * @num: endpoint number
141 * @dir: endpoint direction
142 * @type: endpoint type
143 *
144 * This function returns an error code
145 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800146static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800147{
148 u32 mask, data;
149
150 if (dir) {
151 mask = ENDPTCTRL_TXT; /* type */
152 data = type << ffs_nr(mask);
153
154 mask |= ENDPTCTRL_TXS; /* unstall */
155 mask |= ENDPTCTRL_TXR; /* reset data toggle */
156 data |= ENDPTCTRL_TXR;
157 mask |= ENDPTCTRL_TXE; /* enable */
158 data |= ENDPTCTRL_TXE;
159 } else {
160 mask = ENDPTCTRL_RXT; /* type */
161 data = type << ffs_nr(mask);
162
163 mask |= ENDPTCTRL_RXS; /* unstall */
164 mask |= ENDPTCTRL_RXR; /* reset data toggle */
165 data |= ENDPTCTRL_RXR;
166 mask |= ENDPTCTRL_RXE; /* enable */
167 data |= ENDPTCTRL_RXE;
168 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800169 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800170 return 0;
171}
172
173/**
174 * hw_ep_get_halt: return endpoint halt status
175 * @num: endpoint number
176 * @dir: endpoint direction
177 *
178 * This function returns 1 if endpoint halted
179 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800180static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800181{
182 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
183
Richard Zhao26c696c2012-07-07 22:56:40 +0800184 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800185}
186
187/**
David Lopoaa69a802008-11-17 14:14:51 -0800188 * hw_test_and_clear_setup_status: test & clear setup status (execute without
189 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200190 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800191 *
192 * This function returns setup status
193 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800194static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800195{
Richard Zhao26c696c2012-07-07 22:56:40 +0800196 n = ep_to_bit(ci, n);
197 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800198}
199
200/**
201 * hw_ep_prime: primes endpoint (execute without interruption)
202 * @num: endpoint number
203 * @dir: endpoint direction
204 * @is_ctrl: true if control endpoint
205 *
206 * This function returns an error code
207 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800208static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800209{
210 int n = hw_ep_bit(num, dir);
211
Richard Zhao26c696c2012-07-07 22:56:40 +0800212 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800213 return -EAGAIN;
214
Richard Zhao26c696c2012-07-07 22:56:40 +0800215 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800216
Richard Zhao26c696c2012-07-07 22:56:40 +0800217 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800218 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800219 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800220 return -EAGAIN;
221
222 /* status shoult be tested according with manual but it doesn't work */
223 return 0;
224}
225
226/**
227 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
228 * without interruption)
229 * @num: endpoint number
230 * @dir: endpoint direction
231 * @value: true => stall, false => unstall
232 *
233 * This function returns an error code
234 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800235static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800236{
237 if (value != 0 && value != 1)
238 return -EINVAL;
239
240 do {
Alexander Shishkin262c1632012-05-08 23:28:59 +0300241 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800242 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
243 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
244
245 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800246 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300247 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800248 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800249
250 return 0;
251}
252
253/**
David Lopoaa69a802008-11-17 14:14:51 -0800254 * hw_is_port_high_speed: test if port is high speed
255 *
256 * This function returns true if high speed port
257 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800258static int hw_port_is_high_speed(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800259{
Richard Zhao26c696c2012-07-07 22:56:40 +0800260 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
261 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800262}
263
264/**
David Lopoaa69a802008-11-17 14:14:51 -0800265 * hw_read_intr_enable: returns interrupt enable register
266 *
267 * This function returns register data
268 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800269static u32 hw_read_intr_enable(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800270{
Richard Zhao26c696c2012-07-07 22:56:40 +0800271 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800272}
273
274/**
275 * hw_read_intr_status: returns interrupt status register
276 *
277 * This function returns register data
278 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800279static u32 hw_read_intr_status(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800280{
Richard Zhao26c696c2012-07-07 22:56:40 +0800281 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800282}
283
284/**
David Lopoaa69a802008-11-17 14:14:51 -0800285 * hw_test_and_clear_complete: test & clear complete status (execute without
286 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200287 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800288 *
289 * This function returns complete status
290 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800291static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800292{
Richard Zhao26c696c2012-07-07 22:56:40 +0800293 n = ep_to_bit(ci, n);
294 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800295}
296
297/**
298 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
299 * without interruption)
300 *
301 * This function returns active interrutps
302 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800303static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800304{
Richard Zhao26c696c2012-07-07 22:56:40 +0800305 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800306
Richard Zhao26c696c2012-07-07 22:56:40 +0800307 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800308 return reg;
309}
310
311/**
312 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
313 * interruption)
314 *
315 * This function returns guard value
316 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800317static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800318{
Richard Zhao26c696c2012-07-07 22:56:40 +0800319 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800320}
321
322/**
323 * hw_test_and_set_setup_guard: test & set setup guard (execute without
324 * interruption)
325 *
326 * This function returns guard value
327 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800328static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800329{
Richard Zhao26c696c2012-07-07 22:56:40 +0800330 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800331}
332
333/**
334 * hw_usb_set_address: configures USB address (execute without interruption)
335 * @value: new USB address
336 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300337 * This function explicitly sets the address, without the "USBADRA" (advance)
338 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800339 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800340static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800341{
Richard Zhao26c696c2012-07-07 22:56:40 +0800342 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Alexander Shishkinef15e542012-05-11 17:25:43 +0300343 value << ffs_nr(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800344}
345
346/**
347 * hw_usb_reset: restart device after a bus reset (execute without
348 * interruption)
349 *
350 * This function returns an error code
351 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800352static int hw_usb_reset(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800353{
Richard Zhao26c696c2012-07-07 22:56:40 +0800354 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800355
356 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800357 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800358
359 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800360 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800361
362 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800363 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800364
365 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800366 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800367 udelay(10); /* not RTOS friendly */
368
369 /* reset all endpoints ? */
370
371 /* reset internal status and wait for further instructions
372 no need to verify the port reset status (ESS does it) */
373
374 return 0;
375}
376
377/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800378 * UTIL block
379 *****************************************************************************/
380/**
381 * _usb_addr: calculates endpoint address from direction & number
382 * @ep: endpoint
383 */
384static inline u8 _usb_addr(struct ci13xxx_ep *ep)
385{
386 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
387}
388
389/**
390 * _hardware_queue: configures a request at hardware level
391 * @gadget: gadget
392 * @mEp: endpoint
393 *
394 * This function returns an error code
395 */
396static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
397{
Richard Zhao26c696c2012-07-07 22:56:40 +0800398 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -0800399 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530400 int ret = 0;
401 unsigned length = mReq->req.length;
David Lopoaa69a802008-11-17 14:14:51 -0800402
David Lopoaa69a802008-11-17 14:14:51 -0800403 /* don't queue twice */
404 if (mReq->req.status == -EALREADY)
405 return -EALREADY;
406
David Lopoaa69a802008-11-17 14:14:51 -0800407 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800408
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530409 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
410 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
411 &mReq->zdma);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300412 if (mReq->zptr == NULL)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530413 return -ENOMEM;
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300414
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530415 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
416 mReq->zptr->next = TD_TERMINATE;
417 mReq->zptr->token = TD_STATUS_ACTIVE;
418 if (!mReq->req.no_interrupt)
419 mReq->zptr->token |= TD_IOC;
420 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800421 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300422 if (ret)
423 return ret;
424
David Lopoaa69a802008-11-17 14:14:51 -0800425 /*
426 * TD configuration
427 * TODO - handle requests which spawns into several TDs
428 */
429 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530430 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800431 mReq->ptr->token &= TD_TOTAL_BYTES;
David Lopoaa69a802008-11-17 14:14:51 -0800432 mReq->ptr->token |= TD_STATUS_ACTIVE;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530433 if (mReq->zptr) {
434 mReq->ptr->next = mReq->zdma;
435 } else {
436 mReq->ptr->next = TD_TERMINATE;
437 if (!mReq->req.no_interrupt)
438 mReq->ptr->token |= TD_IOC;
439 }
David Lopoaa69a802008-11-17 14:14:51 -0800440 mReq->ptr->page[0] = mReq->req.dma;
441 for (i = 1; i < 5; i++)
442 mReq->ptr->page[i] =
Artem Leonenko0a313c42010-12-14 23:47:06 -0800443 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
David Lopoaa69a802008-11-17 14:14:51 -0800444
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530445 if (!list_empty(&mEp->qh.queue)) {
446 struct ci13xxx_req *mReqPrev;
447 int n = hw_ep_bit(mEp->num, mEp->dir);
448 int tmp_stat;
449
450 mReqPrev = list_entry(mEp->qh.queue.prev,
451 struct ci13xxx_req, queue);
452 if (mReqPrev->zptr)
453 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
454 else
455 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
456 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800457 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530458 goto done;
459 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800460 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
461 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
462 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
463 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530464 if (tmp_stat)
465 goto done;
466 }
467
468 /* QH configuration */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530469 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
470 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530471 mEp->qh.ptr->cap |= QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -0800472
473 wmb(); /* synchronize before ep prime */
474
Richard Zhao26c696c2012-07-07 22:56:40 +0800475 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800476 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530477done:
478 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800479}
480
481/**
482 * _hardware_dequeue: handles a request at hardware level
483 * @gadget: gadget
484 * @mEp: endpoint
485 *
486 * This function returns an error code
487 */
488static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
489{
David Lopoaa69a802008-11-17 14:14:51 -0800490 if (mReq->req.status != -EALREADY)
491 return -EINVAL;
492
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530493 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
494 return -EBUSY;
495
496 if (mReq->zptr) {
497 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
498 return -EBUSY;
499 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
500 mReq->zptr = NULL;
501 }
David Lopoaa69a802008-11-17 14:14:51 -0800502
503 mReq->req.status = 0;
504
Richard Zhao26c696c2012-07-07 22:56:40 +0800505 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800506
507 mReq->req.status = mReq->ptr->token & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530508 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800509 mReq->req.status = -1;
510 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
511 mReq->req.status = -1;
512 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
513 mReq->req.status = -1;
514
515 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
516 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
517 mReq->req.actual = mReq->req.length - mReq->req.actual;
518 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
519
520 return mReq->req.actual;
521}
522
523/**
524 * _ep_nuke: dequeues all endpoint requests
525 * @mEp: endpoint
526 *
527 * This function returns an error code
528 * Caller must hold lock
529 */
530static int _ep_nuke(struct ci13xxx_ep *mEp)
531__releases(mEp->lock)
532__acquires(mEp->lock)
533{
David Lopoaa69a802008-11-17 14:14:51 -0800534 if (mEp == NULL)
535 return -EINVAL;
536
Richard Zhao26c696c2012-07-07 22:56:40 +0800537 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800538
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530539 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800540
541 /* pop oldest request */
542 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530543 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800544 struct ci13xxx_req, queue);
545 list_del_init(&mReq->queue);
546 mReq->req.status = -ESHUTDOWN;
547
Artem Leonenko7c25a822010-12-14 23:46:55 -0800548 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800549 spin_unlock(mEp->lock);
550 mReq->req.complete(&mEp->ep, &mReq->req);
551 spin_lock(mEp->lock);
552 }
553 }
554 return 0;
555}
556
557/**
558 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
559 * @gadget: gadget
560 *
561 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800562 */
563static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800564{
565 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800566 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530567 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800568
Richard Zhao26c696c2012-07-07 22:56:40 +0800569 spin_lock_irqsave(&ci->lock, flags);
570 ci->gadget.speed = USB_SPEED_UNKNOWN;
571 ci->remote_wakeup = 0;
572 ci->suspended = 0;
573 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530574
David Lopoaa69a802008-11-17 14:14:51 -0800575 /* flush all endpoints */
576 gadget_for_each_ep(ep, gadget) {
577 usb_ep_fifo_flush(ep);
578 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800579 usb_ep_fifo_flush(&ci->ep0out->ep);
580 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800581
Richard Zhao26c696c2012-07-07 22:56:40 +0800582 if (ci->driver)
583 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800584
585 /* make sure to disable all endpoints */
586 gadget_for_each_ep(ep, gadget) {
587 usb_ep_disable(ep);
588 }
David Lopoaa69a802008-11-17 14:14:51 -0800589
Richard Zhao26c696c2012-07-07 22:56:40 +0800590 if (ci->status != NULL) {
591 usb_ep_free_request(&ci->ep0in->ep, ci->status);
592 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800593 }
594
David Lopoaa69a802008-11-17 14:14:51 -0800595 return 0;
596}
597
598/******************************************************************************
599 * ISR block
600 *****************************************************************************/
601/**
602 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800603 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800604 *
605 * This function resets USB engine after a bus reset occurred
606 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800607static void isr_reset_handler(struct ci13xxx *ci)
608__releases(ci->lock)
609__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800610{
David Lopoaa69a802008-11-17 14:14:51 -0800611 int retval;
612
David Lopoaa69a802008-11-17 14:14:51 -0800613 dbg_event(0xFF, "BUS RST", 0);
614
Richard Zhao26c696c2012-07-07 22:56:40 +0800615 spin_unlock(&ci->lock);
616 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800617 if (retval)
618 goto done;
619
Richard Zhao26c696c2012-07-07 22:56:40 +0800620 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800621 if (retval)
622 goto done;
623
Richard Zhao26c696c2012-07-07 22:56:40 +0800624 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
625 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530626 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530627
Michael Grzeschikb9322252012-05-11 17:25:50 +0300628done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800629 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800630
David Lopoaa69a802008-11-17 14:14:51 -0800631 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800632 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800633}
634
635/**
636 * isr_get_status_complete: get_status request complete function
637 * @ep: endpoint
638 * @req: request handled
639 *
640 * Caller must release lock
641 */
642static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
643{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300644 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800645 return;
David Lopoaa69a802008-11-17 14:14:51 -0800646
647 kfree(req->buf);
648 usb_ep_free_request(ep, req);
649}
650
651/**
652 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800653 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800654 * @setup: setup request packet
655 *
656 * This function returns an error code
657 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800658static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800659 struct usb_ctrlrequest *setup)
660__releases(mEp->lock)
661__acquires(mEp->lock)
662{
Richard Zhao26c696c2012-07-07 22:56:40 +0800663 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800664 struct usb_request *req = NULL;
665 gfp_t gfp_flags = GFP_ATOMIC;
666 int dir, num, retval;
667
David Lopoaa69a802008-11-17 14:14:51 -0800668 if (mEp == NULL || setup == NULL)
669 return -EINVAL;
670
671 spin_unlock(mEp->lock);
672 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
673 spin_lock(mEp->lock);
674 if (req == NULL)
675 return -ENOMEM;
676
677 req->complete = isr_get_status_complete;
678 req->length = 2;
679 req->buf = kzalloc(req->length, gfp_flags);
680 if (req->buf == NULL) {
681 retval = -ENOMEM;
682 goto err_free_req;
683 }
684
685 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530686 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800687 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800688 retval = 0;
689 } else if ((setup->bRequestType & USB_RECIP_MASK) \
690 == USB_RECIP_ENDPOINT) {
691 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
692 TX : RX;
693 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800694 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800695 }
696 /* else do nothing; reserved for future use */
697
698 spin_unlock(mEp->lock);
699 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
700 spin_lock(mEp->lock);
701 if (retval)
702 goto err_free_buf;
703
704 return 0;
705
706 err_free_buf:
707 kfree(req->buf);
708 err_free_req:
709 spin_unlock(mEp->lock);
710 usb_ep_free_request(&mEp->ep, req);
711 spin_lock(mEp->lock);
712 return retval;
713}
714
715/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530716 * isr_setup_status_complete: setup_status request complete function
717 * @ep: endpoint
718 * @req: request handled
719 *
720 * Caller must release lock. Put the port in test mode if test mode
721 * feature is selected.
722 */
723static void
724isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
725{
Richard Zhao26c696c2012-07-07 22:56:40 +0800726 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530727 unsigned long flags;
728
Richard Zhao26c696c2012-07-07 22:56:40 +0800729 if (ci->setaddr) {
730 hw_usb_set_address(ci, ci->address);
731 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300732 }
733
Richard Zhao26c696c2012-07-07 22:56:40 +0800734 spin_lock_irqsave(&ci->lock, flags);
735 if (ci->test_mode)
736 hw_port_test_set(ci, ci->test_mode);
737 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530738}
739
740/**
David Lopoaa69a802008-11-17 14:14:51 -0800741 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800742 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800743 *
744 * This function returns an error code
745 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800746static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800747__releases(mEp->lock)
748__acquires(mEp->lock)
749{
750 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530751 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800752
Richard Zhao26c696c2012-07-07 22:56:40 +0800753 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
754 ci->status->context = ci;
755 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800756
757 spin_unlock(mEp->lock);
Richard Zhao26c696c2012-07-07 22:56:40 +0800758 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800759 spin_lock(mEp->lock);
760
761 return retval;
762}
763
764/**
765 * isr_tr_complete_low: transaction complete low level handler
766 * @mEp: endpoint
767 *
768 * This function returns an error code
769 * Caller must hold lock
770 */
771static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
772__releases(mEp->lock)
773__acquires(mEp->lock)
774{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530775 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530776 struct ci13xxx_ep *mEpTemp = mEp;
Pavankumar Kondeti986b11b2011-05-02 11:56:29 +0530777 int uninitialized_var(retval);
David Lopoaa69a802008-11-17 14:14:51 -0800778
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530779 if (list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -0800780 return -EINVAL;
781
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530782 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
783 queue) {
784 retval = _hardware_dequeue(mEp, mReq);
785 if (retval < 0)
786 break;
787 list_del_init(&mReq->queue);
788 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
789 if (mReq->req.complete != NULL) {
790 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530791 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
792 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800793 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530794 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530795 spin_lock(mEp->lock);
796 }
797 }
David Lopoaa69a802008-11-17 14:14:51 -0800798
Pavankumar Kondetief907482011-05-02 11:56:27 +0530799 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530800 retval = 0;
801 if (retval < 0)
David Lopoaa69a802008-11-17 14:14:51 -0800802 dbg_event(_usb_addr(mEp), "DONE", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800803
David Lopoaa69a802008-11-17 14:14:51 -0800804 return retval;
805}
806
807/**
808 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800809 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800810 *
811 * This function handles traffic events
812 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800813static void isr_tr_complete_handler(struct ci13xxx *ci)
814__releases(ci->lock)
815__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800816{
817 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530818 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800819
Richard Zhao26c696c2012-07-07 22:56:40 +0800820 for (i = 0; i < ci->hw_ep_max; i++) {
821 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530822 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800823 struct usb_ctrlrequest req;
824
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200825 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800826 continue; /* not configured */
827
Richard Zhao26c696c2012-07-07 22:56:40 +0800828 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800829 err = isr_tr_complete_low(mEp);
830 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
831 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800832 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800833 if (err < 0) {
834 dbg_event(_usb_addr(mEp),
835 "ERROR", err);
Richard Zhao26c696c2012-07-07 22:56:40 +0800836 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800837 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800838 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700839 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800840 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800841 }
842 }
843 }
844
845 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800846 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800847 continue;
848
849 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800850 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800851 continue;
852 }
853
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530854 /*
855 * Flush data and handshake transactions of previous
856 * setup packet.
857 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800858 _ep_nuke(ci->ep0out);
859 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530860
David Lopoaa69a802008-11-17 14:14:51 -0800861 /* read_setup_packet */
862 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800863 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530864 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800865 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800866
867 type = req.bRequestType;
868
Richard Zhao26c696c2012-07-07 22:56:40 +0800869 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800870
871 dbg_setup(_usb_addr(mEp), &req);
872
873 switch (req.bRequest) {
874 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530875 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
876 le16_to_cpu(req.wValue) ==
877 USB_ENDPOINT_HALT) {
878 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800879 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530880 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530881 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530882 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530883 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800884 num += ci->hw_ep_max/2;
885 if (!ci->ci13xxx_ep[num].wedge) {
886 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530887 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800888 &ci->ci13xxx_ep[num].ep);
889 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530890 if (err)
891 break;
892 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800893 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530894 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
895 le16_to_cpu(req.wValue) ==
896 USB_DEVICE_REMOTE_WAKEUP) {
897 if (req.wLength != 0)
898 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800899 ci->remote_wakeup = 0;
900 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530901 } else {
902 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800903 }
David Lopoaa69a802008-11-17 14:14:51 -0800904 break;
905 case USB_REQ_GET_STATUS:
906 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
907 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
908 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
909 goto delegate;
910 if (le16_to_cpu(req.wLength) != 2 ||
911 le16_to_cpu(req.wValue) != 0)
912 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800913 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800914 break;
915 case USB_REQ_SET_ADDRESS:
916 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
917 goto delegate;
918 if (le16_to_cpu(req.wLength) != 0 ||
919 le16_to_cpu(req.wIndex) != 0)
920 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800921 ci->address = (u8)le16_to_cpu(req.wValue);
922 ci->setaddr = true;
923 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800924 break;
925 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530926 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
927 le16_to_cpu(req.wValue) ==
928 USB_ENDPOINT_HALT) {
929 if (req.wLength != 0)
930 break;
931 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530932 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530933 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530934 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800935 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800936
Richard Zhao26c696c2012-07-07 22:56:40 +0800937 spin_unlock(&ci->lock);
938 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
939 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530940 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800941 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530942 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530943 if (req.wLength != 0)
944 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530945 switch (le16_to_cpu(req.wValue)) {
946 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800947 ci->remote_wakeup = 1;
948 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530949 break;
950 case USB_DEVICE_TEST_MODE:
951 tmode = le16_to_cpu(req.wIndex) >> 8;
952 switch (tmode) {
953 case TEST_J:
954 case TEST_K:
955 case TEST_SE0_NAK:
956 case TEST_PACKET:
957 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800958 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530959 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800960 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530961 break;
962 default:
963 break;
964 }
965 default:
966 goto delegate;
967 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530968 } else {
969 goto delegate;
970 }
David Lopoaa69a802008-11-17 14:14:51 -0800971 break;
972 default:
973delegate:
974 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800975 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800976
Richard Zhao26c696c2012-07-07 22:56:40 +0800977 spin_unlock(&ci->lock);
978 err = ci->driver->setup(&ci->gadget, &req);
979 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800980 break;
981 }
982
983 if (err < 0) {
984 dbg_event(_usb_addr(mEp), "ERROR", err);
985
Richard Zhao26c696c2012-07-07 22:56:40 +0800986 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800987 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800988 dev_err(ci->dev, "error: ep_set_halt\n");
989 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800990 }
991 }
992}
993
994/******************************************************************************
995 * ENDPT block
996 *****************************************************************************/
997/**
998 * ep_enable: configure endpoint, making it usable
999 *
1000 * Check usb_ep_enable() at "usb_gadget.h" for details
1001 */
1002static int ep_enable(struct usb_ep *ep,
1003 const struct usb_endpoint_descriptor *desc)
1004{
1005 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301006 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001007 unsigned long flags;
1008
David Lopoaa69a802008-11-17 14:14:51 -08001009 if (ep == NULL || desc == NULL)
1010 return -EINVAL;
1011
1012 spin_lock_irqsave(mEp->lock, flags);
1013
1014 /* only internal SW should enable ctrl endpts */
1015
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001016 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001017
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301018 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001019 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001020
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001021 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1022 mEp->num = usb_endpoint_num(desc);
1023 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001024
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001025 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001026
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301027 dbg_event(_usb_addr(mEp), "ENABLE", 0);
David Lopoaa69a802008-11-17 14:14:51 -08001028
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301029 mEp->qh.ptr->cap = 0;
David Lopof23e6492009-04-16 14:35:24 -07001030
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301031 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1032 mEp->qh.ptr->cap |= QH_IOS;
1033 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1034 mEp->qh.ptr->cap &= ~QH_MULT;
1035 else
1036 mEp->qh.ptr->cap &= ~QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -08001037
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301038 mEp->qh.ptr->cap |=
1039 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1040 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001041
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301042 /*
1043 * Enable endpoints in the HW other than ep0 as ep0
1044 * is always enabled
1045 */
1046 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001047 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001048
1049 spin_unlock_irqrestore(mEp->lock, flags);
1050 return retval;
1051}
1052
1053/**
1054 * ep_disable: endpoint is no longer usable
1055 *
1056 * Check usb_ep_disable() at "usb_gadget.h" for details
1057 */
1058static int ep_disable(struct usb_ep *ep)
1059{
1060 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1061 int direction, retval = 0;
1062 unsigned long flags;
1063
David Lopoaa69a802008-11-17 14:14:51 -08001064 if (ep == NULL)
1065 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001066 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001067 return -EBUSY;
1068
1069 spin_lock_irqsave(mEp->lock, flags);
1070
1071 /* only internal SW should disable ctrl endpts */
1072
1073 direction = mEp->dir;
1074 do {
1075 dbg_event(_usb_addr(mEp), "DISABLE", 0);
1076
1077 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001078 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001079
1080 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1081 mEp->dir = (mEp->dir == TX) ? RX : TX;
1082
1083 } while (mEp->dir != direction);
1084
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001085 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001086
1087 spin_unlock_irqrestore(mEp->lock, flags);
1088 return retval;
1089}
1090
1091/**
1092 * ep_alloc_request: allocate a request object to use with this endpoint
1093 *
1094 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1095 */
1096static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1097{
1098 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1099 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001100
Alexander Shishkin0f089092012-05-08 23:29:02 +03001101 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001102 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001103
David Lopoaa69a802008-11-17 14:14:51 -08001104 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1105 if (mReq != NULL) {
1106 INIT_LIST_HEAD(&mReq->queue);
1107
1108 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1109 &mReq->dma);
1110 if (mReq->ptr == NULL) {
1111 kfree(mReq);
1112 mReq = NULL;
1113 }
1114 }
1115
1116 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
1117
David Lopoaa69a802008-11-17 14:14:51 -08001118 return (mReq == NULL) ? NULL : &mReq->req;
1119}
1120
1121/**
1122 * ep_free_request: frees a request object
1123 *
1124 * Check usb_ep_free_request() at "usb_gadget.h" for details
1125 */
1126static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1127{
1128 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1129 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1130 unsigned long flags;
1131
David Lopoaa69a802008-11-17 14:14:51 -08001132 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001133 return;
1134 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001135 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001136 return;
1137 }
1138
1139 spin_lock_irqsave(mEp->lock, flags);
1140
1141 if (mReq->ptr)
1142 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1143 kfree(mReq);
1144
1145 dbg_event(_usb_addr(mEp), "FREE", 0);
1146
1147 spin_unlock_irqrestore(mEp->lock, flags);
1148}
1149
1150/**
1151 * ep_queue: queues (submits) an I/O request to an endpoint
1152 *
1153 * Check usb_ep_queue()* at usb_gadget.h" for details
1154 */
1155static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1156 gfp_t __maybe_unused gfp_flags)
1157{
1158 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1159 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Richard Zhao26c696c2012-07-07 22:56:40 +08001160 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -08001161 int retval = 0;
1162 unsigned long flags;
1163
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001164 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001165 return -EINVAL;
1166
1167 spin_lock_irqsave(mEp->lock, flags);
1168
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301169 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1170 if (req->length)
Richard Zhao26c696c2012-07-07 22:56:40 +08001171 mEp = (ci->ep0_dir == RX) ?
1172 ci->ep0out : ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301173 if (!list_empty(&mEp->qh.queue)) {
1174 _ep_nuke(mEp);
1175 retval = -EOVERFLOW;
Richard Zhao26c696c2012-07-07 22:56:40 +08001176 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
Alexander Shishkin0f089092012-05-08 23:29:02 +03001177 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301178 }
David Lopoaa69a802008-11-17 14:14:51 -08001179 }
1180
1181 /* first nuke then test link, e.g. previous status has not sent */
1182 if (!list_empty(&mReq->queue)) {
1183 retval = -EBUSY;
Richard Zhao26c696c2012-07-07 22:56:40 +08001184 dev_err(mEp->ci->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001185 goto done;
1186 }
1187
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001188 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1189 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001190 retval = -EMSGSIZE;
Richard Zhao26c696c2012-07-07 22:56:40 +08001191 dev_warn(mEp->ci->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001192 }
1193
1194 dbg_queue(_usb_addr(mEp), req, retval);
1195
1196 /* push request */
1197 mReq->req.status = -EINPROGRESS;
1198 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001199
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301200 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001201
1202 if (retval == -EALREADY) {
David Lopoaa69a802008-11-17 14:14:51 -08001203 dbg_event(_usb_addr(mEp), "QUEUE", retval);
1204 retval = 0;
1205 }
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301206 if (!retval)
1207 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001208
1209 done:
1210 spin_unlock_irqrestore(mEp->lock, flags);
1211 return retval;
1212}
1213
1214/**
1215 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1216 *
1217 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1218 */
1219static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1220{
1221 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1222 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1223 unsigned long flags;
1224
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301225 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001226 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301227 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001228 return -EINVAL;
1229
1230 spin_lock_irqsave(mEp->lock, flags);
1231
1232 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
1233
Richard Zhao26c696c2012-07-07 22:56:40 +08001234 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001235
1236 /* pop request */
1237 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001238
Richard Zhao26c696c2012-07-07 22:56:40 +08001239 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001240
David Lopoaa69a802008-11-17 14:14:51 -08001241 req->status = -ECONNRESET;
1242
Artem Leonenko7c25a822010-12-14 23:46:55 -08001243 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001244 spin_unlock(mEp->lock);
1245 mReq->req.complete(&mEp->ep, &mReq->req);
1246 spin_lock(mEp->lock);
1247 }
1248
1249 spin_unlock_irqrestore(mEp->lock, flags);
1250 return 0;
1251}
1252
1253/**
1254 * ep_set_halt: sets the endpoint halt feature
1255 *
1256 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1257 */
1258static int ep_set_halt(struct usb_ep *ep, int value)
1259{
1260 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1261 int direction, retval = 0;
1262 unsigned long flags;
1263
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001264 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001265 return -EINVAL;
1266
1267 spin_lock_irqsave(mEp->lock, flags);
1268
1269#ifndef STALL_IN
1270 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1271 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301272 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001273 spin_unlock_irqrestore(mEp->lock, flags);
1274 return -EAGAIN;
1275 }
1276#endif
1277
1278 direction = mEp->dir;
1279 do {
1280 dbg_event(_usb_addr(mEp), "HALT", value);
Richard Zhao26c696c2012-07-07 22:56:40 +08001281 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001282
1283 if (!value)
1284 mEp->wedge = 0;
1285
1286 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1287 mEp->dir = (mEp->dir == TX) ? RX : TX;
1288
1289 } while (mEp->dir != direction);
1290
1291 spin_unlock_irqrestore(mEp->lock, flags);
1292 return retval;
1293}
1294
1295/**
1296 * ep_set_wedge: sets the halt feature and ignores clear requests
1297 *
1298 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1299 */
1300static int ep_set_wedge(struct usb_ep *ep)
1301{
1302 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1303 unsigned long flags;
1304
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001305 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001306 return -EINVAL;
1307
1308 spin_lock_irqsave(mEp->lock, flags);
1309
1310 dbg_event(_usb_addr(mEp), "WEDGE", 0);
1311 mEp->wedge = 1;
1312
1313 spin_unlock_irqrestore(mEp->lock, flags);
1314
1315 return usb_ep_set_halt(ep);
1316}
1317
1318/**
1319 * ep_fifo_flush: flushes contents of a fifo
1320 *
1321 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1322 */
1323static void ep_fifo_flush(struct usb_ep *ep)
1324{
1325 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1326 unsigned long flags;
1327
David Lopoaa69a802008-11-17 14:14:51 -08001328 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001329 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001330 return;
1331 }
1332
1333 spin_lock_irqsave(mEp->lock, flags);
1334
1335 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
Richard Zhao26c696c2012-07-07 22:56:40 +08001336 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001337
1338 spin_unlock_irqrestore(mEp->lock, flags);
1339}
1340
1341/**
1342 * Endpoint-specific part of the API to the USB controller hardware
1343 * Check "usb_gadget.h" for details
1344 */
1345static const struct usb_ep_ops usb_ep_ops = {
1346 .enable = ep_enable,
1347 .disable = ep_disable,
1348 .alloc_request = ep_alloc_request,
1349 .free_request = ep_free_request,
1350 .queue = ep_queue,
1351 .dequeue = ep_dequeue,
1352 .set_halt = ep_set_halt,
1353 .set_wedge = ep_set_wedge,
1354 .fifo_flush = ep_fifo_flush,
1355};
1356
1357/******************************************************************************
1358 * GADGET block
1359 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301360static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1361{
Richard Zhao26c696c2012-07-07 22:56:40 +08001362 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301363 unsigned long flags;
1364 int gadget_ready = 0;
1365
Richard Zhao26c696c2012-07-07 22:56:40 +08001366 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301367 return -EOPNOTSUPP;
1368
Richard Zhao26c696c2012-07-07 22:56:40 +08001369 spin_lock_irqsave(&ci->lock, flags);
1370 ci->vbus_active = is_active;
1371 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301372 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001373 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301374
1375 if (gadget_ready) {
1376 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301377 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001378 hw_device_reset(ci, USBMODE_CM_DC);
1379 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301380 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001381 hw_device_state(ci, 0);
1382 if (ci->platdata->notify_event)
1383 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301384 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001385 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301386 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301387 }
1388 }
1389
1390 return 0;
1391}
1392
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301393static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1394{
Richard Zhao26c696c2012-07-07 22:56:40 +08001395 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301396 unsigned long flags;
1397 int ret = 0;
1398
Richard Zhao26c696c2012-07-07 22:56:40 +08001399 spin_lock_irqsave(&ci->lock, flags);
1400 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301401 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301402 goto out;
1403 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001404 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301405 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301406 goto out;
1407 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001408 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301409out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001410 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301411 return ret;
1412}
1413
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301414static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1415{
Richard Zhao26c696c2012-07-07 22:56:40 +08001416 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301417
Richard Zhao26c696c2012-07-07 22:56:40 +08001418 if (ci->transceiver)
1419 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301420 return -ENOTSUPP;
1421}
1422
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001423static int ci13xxx_start(struct usb_gadget *gadget,
1424 struct usb_gadget_driver *driver);
1425static int ci13xxx_stop(struct usb_gadget *gadget,
1426 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001427/**
1428 * Device operations part of the API to the USB controller hardware,
1429 * which don't involve endpoints (or i/o)
1430 * Check "usb_gadget.h" for details
1431 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301432static const struct usb_gadget_ops usb_gadget_ops = {
1433 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301434 .wakeup = ci13xxx_wakeup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301435 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001436 .udc_start = ci13xxx_start,
1437 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301438};
David Lopoaa69a802008-11-17 14:14:51 -08001439
Richard Zhao26c696c2012-07-07 22:56:40 +08001440static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001441{
1442 int retval = 0, i, j;
1443
Richard Zhao26c696c2012-07-07 22:56:40 +08001444 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001445 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001446 int k = i + j * ci->hw_ep_max/2;
1447 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001448
1449 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1450 (j == TX) ? "in" : "out");
1451
Richard Zhao26c696c2012-07-07 22:56:40 +08001452 mEp->ci = ci;
1453 mEp->lock = &ci->lock;
1454 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001455
1456 mEp->ep.name = mEp->name;
1457 mEp->ep.ops = &usb_ep_ops;
1458 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
1459
1460 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001461 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001462 &mEp->qh.dma);
1463 if (mEp->qh.ptr == NULL)
1464 retval = -ENOMEM;
1465 else
1466 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1467
1468 /*
1469 * set up shorthands for ep0 out and in endpoints,
1470 * don't add to gadget's ep_list
1471 */
1472 if (i == 0) {
1473 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001474 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001475 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001476 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001477
1478 continue;
1479 }
1480
Richard Zhao26c696c2012-07-07 22:56:40 +08001481 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001482 }
1483
1484 return retval;
1485}
1486
David Lopoaa69a802008-11-17 14:14:51 -08001487/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001488 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001489 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001490 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001491 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001492 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001493 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001494static int ci13xxx_start(struct usb_gadget *gadget,
1495 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001496{
Richard Zhao26c696c2012-07-07 22:56:40 +08001497 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301498 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001499 int retval = -ENOMEM;
1500
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001501 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001502 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001503
David Lopoaa69a802008-11-17 14:14:51 -08001504
Richard Zhao26c696c2012-07-07 22:56:40 +08001505 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1506 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301507 if (retval)
1508 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001509
Richard Zhao26c696c2012-07-07 22:56:40 +08001510 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1511 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301512 if (retval)
1513 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001514 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001515
Richard Zhao26c696c2012-07-07 22:56:40 +08001516 ci->driver = driver;
1517 pm_runtime_get_sync(&ci->gadget.dev);
1518 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1519 if (ci->vbus_active) {
1520 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
1521 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301522 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001523 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301524 goto done;
1525 }
1526 }
1527
Richard Zhao26c696c2012-07-07 22:56:40 +08001528 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301529 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001530 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001531
1532 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001533 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001534 return retval;
1535}
David Lopoaa69a802008-11-17 14:14:51 -08001536
1537/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001538 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001539 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001540static int ci13xxx_stop(struct usb_gadget *gadget,
1541 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001542{
Richard Zhao26c696c2012-07-07 22:56:40 +08001543 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001544 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001545
Richard Zhao26c696c2012-07-07 22:56:40 +08001546 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001547
Richard Zhao26c696c2012-07-07 22:56:40 +08001548 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1549 ci->vbus_active) {
1550 hw_device_state(ci, 0);
1551 if (ci->platdata->notify_event)
1552 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301553 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001554 ci->driver = NULL;
1555 spin_unlock_irqrestore(&ci->lock, flags);
1556 _gadget_stop_activity(&ci->gadget);
1557 spin_lock_irqsave(&ci->lock, flags);
1558 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301559 }
David Lopoaa69a802008-11-17 14:14:51 -08001560
Richard Zhao26c696c2012-07-07 22:56:40 +08001561 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001562
David Lopoaa69a802008-11-17 14:14:51 -08001563 return 0;
1564}
David Lopoaa69a802008-11-17 14:14:51 -08001565
1566/******************************************************************************
1567 * BUS block
1568 *****************************************************************************/
1569/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001570 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001571 *
1572 * This function returns IRQ_HANDLED if the IRQ has been handled
1573 * It locks access to registers
1574 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001575static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001576{
David Lopoaa69a802008-11-17 14:14:51 -08001577 irqreturn_t retval;
1578 u32 intr;
1579
Richard Zhao26c696c2012-07-07 22:56:40 +08001580 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001581 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001582
Richard Zhao26c696c2012-07-07 22:56:40 +08001583 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301584
Richard Zhao26c696c2012-07-07 22:56:40 +08001585 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1586 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001587 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001588 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301589 return IRQ_NONE;
1590 }
1591 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 intr = hw_test_and_clear_intr_active(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001593 dbg_interrupt(intr);
David Lopoaa69a802008-11-17 14:14:51 -08001594
Alexander Shishkine443b332012-05-11 17:25:46 +03001595 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001596 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001597 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001598 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001599
David Lopoaa69a802008-11-17 14:14:51 -08001600 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001601 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001602 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001603 if (ci->suspended && ci->driver->resume) {
1604 spin_unlock(&ci->lock);
1605 ci->driver->resume(&ci->gadget);
1606 spin_lock(&ci->lock);
1607 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301608 }
David Lopoaa69a802008-11-17 14:14:51 -08001609 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001610
1611 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001612 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001613
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301614 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001615 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1616 ci->driver->suspend) {
1617 ci->suspended = 1;
1618 spin_unlock(&ci->lock);
1619 ci->driver->suspend(&ci->gadget);
1620 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301621 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301622 }
David Lopoaa69a802008-11-17 14:14:51 -08001623 retval = IRQ_HANDLED;
1624 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001625 retval = IRQ_NONE;
1626 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001627 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001628
1629 return retval;
1630}
1631
1632/**
1633 * udc_release: driver release function
1634 * @dev: device
1635 *
1636 * Currently does nothing
1637 */
1638static void udc_release(struct device *dev)
1639{
David Lopoaa69a802008-11-17 14:14:51 -08001640}
1641
1642/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001643 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001644 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001645 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001646static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001647{
Richard Zhao26c696c2012-07-07 22:56:40 +08001648 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001649 int retval = 0;
1650
Richard Zhao26c696c2012-07-07 22:56:40 +08001651 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001652
Richard Zhao26c696c2012-07-07 22:56:40 +08001653 ci->gadget.ops = &usb_gadget_ops;
1654 ci->gadget.speed = USB_SPEED_UNKNOWN;
1655 ci->gadget.max_speed = USB_SPEED_HIGH;
1656 ci->gadget.is_otg = 0;
1657 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001658
Richard Zhao26c696c2012-07-07 22:56:40 +08001659 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001660
Richard Zhao26c696c2012-07-07 22:56:40 +08001661 dev_set_name(&ci->gadget.dev, "gadget");
1662 ci->gadget.dev.dma_mask = dev->dma_mask;
1663 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1664 ci->gadget.dev.parent = dev;
1665 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001666
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001667 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001668 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001669 sizeof(struct ci13xxx_qh),
1670 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001671 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001672 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001673
Richard Zhao26c696c2012-07-07 22:56:40 +08001674 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001675 sizeof(struct ci13xxx_td),
1676 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001677 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001678 retval = -ENOMEM;
1679 goto free_qh_pool;
1680 }
1681
Richard Zhao26c696c2012-07-07 22:56:40 +08001682 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001683 if (retval)
1684 goto free_pools;
1685
Richard Zhao26c696c2012-07-07 22:56:40 +08001686 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301687
Richard Zhao26c696c2012-07-07 22:56:40 +08001688 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301689
Richard Zhao26c696c2012-07-07 22:56:40 +08001690 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1691 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301692 retval = -ENODEV;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001693 goto free_pools;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301694 }
1695 }
1696
Richard Zhao26c696c2012-07-07 22:56:40 +08001697 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1698 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301699 if (retval)
1700 goto put_transceiver;
1701 }
1702
Richard Zhao26c696c2012-07-07 22:56:40 +08001703 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301704 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001705 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301706 goto put_transceiver;
1707 }
David Lopoaa69a802008-11-17 14:14:51 -08001708
Richard Zhao26c696c2012-07-07 22:56:40 +08001709 retval = dbg_create_files(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301710 if (retval)
1711 goto unreg_device;
1712
Richard Zhao26c696c2012-07-07 22:56:40 +08001713 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1714 retval = otg_set_peripheral(ci->transceiver->otg,
1715 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301716 if (retval)
1717 goto remove_dbg;
David Lopoaa69a802008-11-17 14:14:51 -08001718 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001719
Richard Zhao26c696c2012-07-07 22:56:40 +08001720 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001721 if (retval)
1722 goto remove_trans;
1723
Richard Zhao26c696c2012-07-07 22:56:40 +08001724 pm_runtime_no_callbacks(&ci->gadget.dev);
1725 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001726
David Lopoaa69a802008-11-17 14:14:51 -08001727 return retval;
1728
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001729remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001730 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1731 otg_set_peripheral(ci->transceiver->otg, &ci->gadget);
1732 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001733 }
1734
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001735 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301736remove_dbg:
Richard Zhao26c696c2012-07-07 22:56:40 +08001737 dbg_remove_files(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301738unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001739 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301740put_transceiver:
Richard Zhao26c696c2012-07-07 22:56:40 +08001741 if (!IS_ERR_OR_NULL(ci->transceiver))
1742 usb_put_phy(ci->transceiver);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001743free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001744 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001745free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001747 return retval;
1748}
1749
1750/**
1751 * udc_remove: parent remove must call this to remove UDC
1752 *
1753 * No interrupts active, the IRQ has been released
1754 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001755static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001756{
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001757 int i;
David Lopoaa69a802008-11-17 14:14:51 -08001758
Richard Zhao26c696c2012-07-07 22:56:40 +08001759 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001760 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001761
Richard Zhao26c696c2012-07-07 22:56:40 +08001762 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001763
Richard Zhao26c696c2012-07-07 22:56:40 +08001764 for (i = 0; i < ci->hw_ep_max; i++) {
1765 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001766
Richard Zhao26c696c2012-07-07 22:56:40 +08001767 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001768 }
1769
Richard Zhao26c696c2012-07-07 22:56:40 +08001770 dma_pool_destroy(ci->td_pool);
1771 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001772
Richard Zhao26c696c2012-07-07 22:56:40 +08001773 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1774 otg_set_peripheral(ci->transceiver->otg, NULL);
1775 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301776 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001777 dbg_remove_files(&ci->gadget.dev);
1778 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001779 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001780 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001781}
David Lopoaa69a802008-11-17 14:14:51 -08001782
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001783/**
1784 * ci_hdrc_gadget_init - initialize device related bits
1785 * ci: the controller
1786 *
1787 * This function enables the gadget role, if the device is "device capable".
1788 */
1789int ci_hdrc_gadget_init(struct ci13xxx *ci)
1790{
1791 struct ci_role_driver *rdrv;
1792
1793 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1794 return -ENXIO;
1795
1796 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1797 if (!rdrv)
1798 return -ENOMEM;
1799
1800 rdrv->start = udc_start;
1801 rdrv->stop = udc_stop;
1802 rdrv->irq = udc_irq;
1803 rdrv->name = "gadget";
1804 ci->roles[CI_ROLE_GADGET] = rdrv;
1805
1806 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001807}