blob: ec218b0202c0c3941451d32841be26df8877a0db [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);
432 for (i = 1; i < 5; i++) {
433 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
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530438 if (!list_empty(&mEp->qh.queue)) {
439 struct ci13xxx_req *mReqPrev;
440 int n = hw_ep_bit(mEp->num, mEp->dir);
441 int tmp_stat;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200442 u32 next = mReq->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530443
444 mReqPrev = list_entry(mEp->qh.queue.prev,
445 struct ci13xxx_req, queue);
446 if (mReqPrev->zptr)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200447 mReqPrev->zptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530448 else
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200449 mReqPrev->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530450 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800451 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530452 goto done;
453 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800454 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
455 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
456 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
457 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530458 if (tmp_stat)
459 goto done;
460 }
461
462 /* QH configuration */
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200463 mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200464 mEp->qh.ptr->td.token &=
465 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800466
467 wmb(); /* synchronize before ep prime */
468
Richard Zhao26c696c2012-07-07 22:56:40 +0800469 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800470 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530471done:
472 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800473}
474
475/**
476 * _hardware_dequeue: handles a request at hardware level
477 * @gadget: gadget
478 * @mEp: endpoint
479 *
480 * This function returns an error code
481 */
482static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
483{
Michael Grzeschik9e506432013-03-30 12:54:07 +0200484 u32 tmptoken = le32_to_cpu(mReq->ptr->token);
485
David Lopoaa69a802008-11-17 14:14:51 -0800486 if (mReq->req.status != -EALREADY)
487 return -EINVAL;
488
Michael Grzeschik9e506432013-03-30 12:54:07 +0200489 if ((TD_STATUS_ACTIVE & tmptoken) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530490 return -EBUSY;
491
492 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200493 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530494 return -EBUSY;
495 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
496 mReq->zptr = NULL;
497 }
David Lopoaa69a802008-11-17 14:14:51 -0800498
499 mReq->req.status = 0;
500
Richard Zhao26c696c2012-07-07 22:56:40 +0800501 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800502
Michael Grzeschik9e506432013-03-30 12:54:07 +0200503 mReq->req.status = tmptoken & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530504 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800505 mReq->req.status = -1;
506 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
507 mReq->req.status = -1;
508 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
509 mReq->req.status = -1;
510
Michael Grzeschik9e506432013-03-30 12:54:07 +0200511 mReq->req.actual = tmptoken & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200512 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800513 mReq->req.actual = mReq->req.length - mReq->req.actual;
514 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
515
516 return mReq->req.actual;
517}
518
519/**
520 * _ep_nuke: dequeues all endpoint requests
521 * @mEp: endpoint
522 *
523 * This function returns an error code
524 * Caller must hold lock
525 */
526static int _ep_nuke(struct ci13xxx_ep *mEp)
527__releases(mEp->lock)
528__acquires(mEp->lock)
529{
David Lopoaa69a802008-11-17 14:14:51 -0800530 if (mEp == NULL)
531 return -EINVAL;
532
Richard Zhao26c696c2012-07-07 22:56:40 +0800533 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800534
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530535 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800536
537 /* pop oldest request */
538 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530539 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800540 struct ci13xxx_req, queue);
541 list_del_init(&mReq->queue);
542 mReq->req.status = -ESHUTDOWN;
543
Artem Leonenko7c25a822010-12-14 23:46:55 -0800544 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800545 spin_unlock(mEp->lock);
546 mReq->req.complete(&mEp->ep, &mReq->req);
547 spin_lock(mEp->lock);
548 }
549 }
550 return 0;
551}
552
553/**
554 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
555 * @gadget: gadget
556 *
557 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800558 */
559static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800560{
561 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800562 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530563 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800564
Richard Zhao26c696c2012-07-07 22:56:40 +0800565 spin_lock_irqsave(&ci->lock, flags);
566 ci->gadget.speed = USB_SPEED_UNKNOWN;
567 ci->remote_wakeup = 0;
568 ci->suspended = 0;
569 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530570
David Lopoaa69a802008-11-17 14:14:51 -0800571 /* flush all endpoints */
572 gadget_for_each_ep(ep, gadget) {
573 usb_ep_fifo_flush(ep);
574 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800575 usb_ep_fifo_flush(&ci->ep0out->ep);
576 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800577
Richard Zhao26c696c2012-07-07 22:56:40 +0800578 if (ci->driver)
579 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800580
581 /* make sure to disable all endpoints */
582 gadget_for_each_ep(ep, gadget) {
583 usb_ep_disable(ep);
584 }
David Lopoaa69a802008-11-17 14:14:51 -0800585
Richard Zhao26c696c2012-07-07 22:56:40 +0800586 if (ci->status != NULL) {
587 usb_ep_free_request(&ci->ep0in->ep, ci->status);
588 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800589 }
590
David Lopoaa69a802008-11-17 14:14:51 -0800591 return 0;
592}
593
594/******************************************************************************
595 * ISR block
596 *****************************************************************************/
597/**
598 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800599 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800600 *
601 * This function resets USB engine after a bus reset occurred
602 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800603static void isr_reset_handler(struct ci13xxx *ci)
604__releases(ci->lock)
605__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800606{
David Lopoaa69a802008-11-17 14:14:51 -0800607 int retval;
608
Richard Zhao26c696c2012-07-07 22:56:40 +0800609 spin_unlock(&ci->lock);
610 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800611 if (retval)
612 goto done;
613
Richard Zhao26c696c2012-07-07 22:56:40 +0800614 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800615 if (retval)
616 goto done;
617
Richard Zhao26c696c2012-07-07 22:56:40 +0800618 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
619 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530620 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530621
Michael Grzeschikb9322252012-05-11 17:25:50 +0300622done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800623 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800624
David Lopoaa69a802008-11-17 14:14:51 -0800625 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800626 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800627}
628
629/**
630 * isr_get_status_complete: get_status request complete function
631 * @ep: endpoint
632 * @req: request handled
633 *
634 * Caller must release lock
635 */
636static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
637{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300638 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800639 return;
David Lopoaa69a802008-11-17 14:14:51 -0800640
641 kfree(req->buf);
642 usb_ep_free_request(ep, req);
643}
644
645/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200646 * _ep_queue: queues (submits) an I/O request to an endpoint
647 *
648 * Caller must hold lock
649 */
650static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
651 gfp_t __maybe_unused gfp_flags)
652{
653 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
654 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
655 struct ci13xxx *ci = mEp->ci;
656 int retval = 0;
657
658 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
659 return -EINVAL;
660
661 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
662 if (req->length)
663 mEp = (ci->ep0_dir == RX) ?
664 ci->ep0out : ci->ep0in;
665 if (!list_empty(&mEp->qh.queue)) {
666 _ep_nuke(mEp);
667 retval = -EOVERFLOW;
668 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
669 _usb_addr(mEp));
670 }
671 }
672
673 /* first nuke then test link, e.g. previous status has not sent */
674 if (!list_empty(&mReq->queue)) {
675 dev_err(mEp->ci->dev, "request already in queue\n");
676 return -EBUSY;
677 }
678
679 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
680 dev_err(mEp->ci->dev, "request bigger than one td\n");
681 return -EMSGSIZE;
682 }
683
684 /* push request */
685 mReq->req.status = -EINPROGRESS;
686 mReq->req.actual = 0;
687
688 retval = _hardware_enqueue(mEp, mReq);
689
690 if (retval == -EALREADY)
691 retval = 0;
692 if (!retval)
693 list_add_tail(&mReq->queue, &mEp->qh.queue);
694
695 return retval;
696}
697
698/**
David Lopoaa69a802008-11-17 14:14:51 -0800699 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800700 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800701 * @setup: setup request packet
702 *
703 * This function returns an error code
704 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800705static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800706 struct usb_ctrlrequest *setup)
707__releases(mEp->lock)
708__acquires(mEp->lock)
709{
Richard Zhao26c696c2012-07-07 22:56:40 +0800710 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800711 struct usb_request *req = NULL;
712 gfp_t gfp_flags = GFP_ATOMIC;
713 int dir, num, retval;
714
David Lopoaa69a802008-11-17 14:14:51 -0800715 if (mEp == NULL || setup == NULL)
716 return -EINVAL;
717
718 spin_unlock(mEp->lock);
719 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
720 spin_lock(mEp->lock);
721 if (req == NULL)
722 return -ENOMEM;
723
724 req->complete = isr_get_status_complete;
725 req->length = 2;
726 req->buf = kzalloc(req->length, gfp_flags);
727 if (req->buf == NULL) {
728 retval = -ENOMEM;
729 goto err_free_req;
730 }
731
732 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530733 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800734 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800735 retval = 0;
736 } else if ((setup->bRequestType & USB_RECIP_MASK) \
737 == USB_RECIP_ENDPOINT) {
738 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
739 TX : RX;
740 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800741 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800742 }
743 /* else do nothing; reserved for future use */
744
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200745 retval = _ep_queue(&mEp->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800746 if (retval)
747 goto err_free_buf;
748
749 return 0;
750
751 err_free_buf:
752 kfree(req->buf);
753 err_free_req:
754 spin_unlock(mEp->lock);
755 usb_ep_free_request(&mEp->ep, req);
756 spin_lock(mEp->lock);
757 return retval;
758}
759
760/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530761 * isr_setup_status_complete: setup_status request complete function
762 * @ep: endpoint
763 * @req: request handled
764 *
765 * Caller must release lock. Put the port in test mode if test mode
766 * feature is selected.
767 */
768static void
769isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
770{
Richard Zhao26c696c2012-07-07 22:56:40 +0800771 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530772 unsigned long flags;
773
Richard Zhao26c696c2012-07-07 22:56:40 +0800774 if (ci->setaddr) {
775 hw_usb_set_address(ci, ci->address);
776 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300777 }
778
Richard Zhao26c696c2012-07-07 22:56:40 +0800779 spin_lock_irqsave(&ci->lock, flags);
780 if (ci->test_mode)
781 hw_port_test_set(ci, ci->test_mode);
782 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530783}
784
785/**
David Lopoaa69a802008-11-17 14:14:51 -0800786 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800787 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800788 *
789 * This function returns an error code
790 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800791static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800792{
793 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530794 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800795
Richard Zhao26c696c2012-07-07 22:56:40 +0800796 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
797 ci->status->context = ci;
798 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800799
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200800 retval = _ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800801
802 return retval;
803}
804
805/**
806 * isr_tr_complete_low: transaction complete low level handler
807 * @mEp: endpoint
808 *
809 * This function returns an error code
810 * Caller must hold lock
811 */
812static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
813__releases(mEp->lock)
814__acquires(mEp->lock)
815{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530816 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530817 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300818 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800819
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530820 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
821 queue) {
822 retval = _hardware_dequeue(mEp, mReq);
823 if (retval < 0)
824 break;
825 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530826 if (mReq->req.complete != NULL) {
827 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530828 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
829 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800830 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530831 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530832 spin_lock(mEp->lock);
833 }
834 }
David Lopoaa69a802008-11-17 14:14:51 -0800835
Pavankumar Kondetief907482011-05-02 11:56:27 +0530836 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530837 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800838
David Lopoaa69a802008-11-17 14:14:51 -0800839 return retval;
840}
841
842/**
843 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800844 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800845 *
846 * This function handles traffic events
847 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800848static void isr_tr_complete_handler(struct ci13xxx *ci)
849__releases(ci->lock)
850__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800851{
852 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530853 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800854
Richard Zhao26c696c2012-07-07 22:56:40 +0800855 for (i = 0; i < ci->hw_ep_max; i++) {
856 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530857 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800858 struct usb_ctrlrequest req;
859
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200860 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800861 continue; /* not configured */
862
Richard Zhao26c696c2012-07-07 22:56:40 +0800863 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800864 err = isr_tr_complete_low(mEp);
865 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
866 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800867 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800868 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800869 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800870 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800871 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700872 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800873 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800874 }
875 }
876 }
877
878 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800879 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800880 continue;
881
882 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800883 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800884 continue;
885 }
886
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530887 /*
888 * Flush data and handshake transactions of previous
889 * setup packet.
890 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800891 _ep_nuke(ci->ep0out);
892 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530893
David Lopoaa69a802008-11-17 14:14:51 -0800894 /* read_setup_packet */
895 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800896 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530897 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800898 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800899
900 type = req.bRequestType;
901
Richard Zhao26c696c2012-07-07 22:56:40 +0800902 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800903
David Lopoaa69a802008-11-17 14:14:51 -0800904 switch (req.bRequest) {
905 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530906 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
907 le16_to_cpu(req.wValue) ==
908 USB_ENDPOINT_HALT) {
909 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800910 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530911 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530912 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530913 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530914 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800915 num += ci->hw_ep_max/2;
916 if (!ci->ci13xxx_ep[num].wedge) {
917 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530918 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800919 &ci->ci13xxx_ep[num].ep);
920 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530921 if (err)
922 break;
923 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800924 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530925 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
926 le16_to_cpu(req.wValue) ==
927 USB_DEVICE_REMOTE_WAKEUP) {
928 if (req.wLength != 0)
929 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800930 ci->remote_wakeup = 0;
931 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530932 } else {
933 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800934 }
David Lopoaa69a802008-11-17 14:14:51 -0800935 break;
936 case USB_REQ_GET_STATUS:
937 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
938 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
939 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
940 goto delegate;
941 if (le16_to_cpu(req.wLength) != 2 ||
942 le16_to_cpu(req.wValue) != 0)
943 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800944 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800945 break;
946 case USB_REQ_SET_ADDRESS:
947 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
948 goto delegate;
949 if (le16_to_cpu(req.wLength) != 0 ||
950 le16_to_cpu(req.wIndex) != 0)
951 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800952 ci->address = (u8)le16_to_cpu(req.wValue);
953 ci->setaddr = true;
954 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800955 break;
956 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530957 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
958 le16_to_cpu(req.wValue) ==
959 USB_ENDPOINT_HALT) {
960 if (req.wLength != 0)
961 break;
962 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530963 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530964 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530965 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800966 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800967
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 spin_unlock(&ci->lock);
969 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
970 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530971 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800972 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530973 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530974 if (req.wLength != 0)
975 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530976 switch (le16_to_cpu(req.wValue)) {
977 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800978 ci->remote_wakeup = 1;
979 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530980 break;
981 case USB_DEVICE_TEST_MODE:
982 tmode = le16_to_cpu(req.wIndex) >> 8;
983 switch (tmode) {
984 case TEST_J:
985 case TEST_K:
986 case TEST_SE0_NAK:
987 case TEST_PACKET:
988 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800989 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530990 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800991 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530992 break;
993 default:
994 break;
995 }
996 default:
997 goto delegate;
998 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530999 } else {
1000 goto delegate;
1001 }
David Lopoaa69a802008-11-17 14:14:51 -08001002 break;
1003 default:
1004delegate:
1005 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001006 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001007
Richard Zhao26c696c2012-07-07 22:56:40 +08001008 spin_unlock(&ci->lock);
1009 err = ci->driver->setup(&ci->gadget, &req);
1010 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001011 break;
1012 }
1013
1014 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001015 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001016 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001017 dev_err(ci->dev, "error: ep_set_halt\n");
1018 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001019 }
1020 }
1021}
1022
1023/******************************************************************************
1024 * ENDPT block
1025 *****************************************************************************/
1026/**
1027 * ep_enable: configure endpoint, making it usable
1028 *
1029 * Check usb_ep_enable() at "usb_gadget.h" for details
1030 */
1031static int ep_enable(struct usb_ep *ep,
1032 const struct usb_endpoint_descriptor *desc)
1033{
1034 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301035 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001036 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001037 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001038
David Lopoaa69a802008-11-17 14:14:51 -08001039 if (ep == NULL || desc == NULL)
1040 return -EINVAL;
1041
1042 spin_lock_irqsave(mEp->lock, flags);
1043
1044 /* only internal SW should enable ctrl endpts */
1045
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001046 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001047
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301048 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001049 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001050
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001051 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1052 mEp->num = usb_endpoint_num(desc);
1053 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001054
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001055 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001056
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301057 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001058 cap |= QH_IOS;
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001059 if (mEp->num)
1060 cap |= QH_ZLT;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001061 cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001062
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001063 mEp->qh.ptr->cap = cpu_to_le32(cap);
1064
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001065 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001066
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301067 /*
1068 * Enable endpoints in the HW other than ep0 as ep0
1069 * is always enabled
1070 */
1071 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001072 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001073
1074 spin_unlock_irqrestore(mEp->lock, flags);
1075 return retval;
1076}
1077
1078/**
1079 * ep_disable: endpoint is no longer usable
1080 *
1081 * Check usb_ep_disable() at "usb_gadget.h" for details
1082 */
1083static int ep_disable(struct usb_ep *ep)
1084{
1085 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1086 int direction, retval = 0;
1087 unsigned long flags;
1088
David Lopoaa69a802008-11-17 14:14:51 -08001089 if (ep == NULL)
1090 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001091 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001092 return -EBUSY;
1093
1094 spin_lock_irqsave(mEp->lock, flags);
1095
1096 /* only internal SW should disable ctrl endpts */
1097
1098 direction = mEp->dir;
1099 do {
David Lopoaa69a802008-11-17 14:14:51 -08001100 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001101 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001102
1103 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1104 mEp->dir = (mEp->dir == TX) ? RX : TX;
1105
1106 } while (mEp->dir != direction);
1107
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001108 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001109
1110 spin_unlock_irqrestore(mEp->lock, flags);
1111 return retval;
1112}
1113
1114/**
1115 * ep_alloc_request: allocate a request object to use with this endpoint
1116 *
1117 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1118 */
1119static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1120{
1121 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1122 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001123
Alexander Shishkin0f089092012-05-08 23:29:02 +03001124 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001125 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001126
David Lopoaa69a802008-11-17 14:14:51 -08001127 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1128 if (mReq != NULL) {
1129 INIT_LIST_HEAD(&mReq->queue);
1130
1131 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1132 &mReq->dma);
1133 if (mReq->ptr == NULL) {
1134 kfree(mReq);
1135 mReq = NULL;
1136 }
1137 }
1138
David Lopoaa69a802008-11-17 14:14:51 -08001139 return (mReq == NULL) ? NULL : &mReq->req;
1140}
1141
1142/**
1143 * ep_free_request: frees a request object
1144 *
1145 * Check usb_ep_free_request() at "usb_gadget.h" for details
1146 */
1147static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1148{
1149 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1150 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1151 unsigned long flags;
1152
David Lopoaa69a802008-11-17 14:14:51 -08001153 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001154 return;
1155 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001156 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001157 return;
1158 }
1159
1160 spin_lock_irqsave(mEp->lock, flags);
1161
1162 if (mReq->ptr)
1163 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1164 kfree(mReq);
1165
David Lopoaa69a802008-11-17 14:14:51 -08001166 spin_unlock_irqrestore(mEp->lock, flags);
1167}
1168
1169/**
1170 * ep_queue: queues (submits) an I/O request to an endpoint
1171 *
1172 * Check usb_ep_queue()* at usb_gadget.h" for details
1173 */
1174static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1175 gfp_t __maybe_unused gfp_flags)
1176{
1177 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001178 int retval = 0;
1179 unsigned long flags;
1180
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001181 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001182 return -EINVAL;
1183
1184 spin_lock_irqsave(mEp->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001185 retval = _ep_queue(ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -08001186 spin_unlock_irqrestore(mEp->lock, flags);
1187 return retval;
1188}
1189
1190/**
1191 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1192 *
1193 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1194 */
1195static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1196{
1197 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1198 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1199 unsigned long flags;
1200
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301201 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001202 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301203 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001204 return -EINVAL;
1205
1206 spin_lock_irqsave(mEp->lock, flags);
1207
Richard Zhao26c696c2012-07-07 22:56:40 +08001208 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001209
1210 /* pop request */
1211 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001212
Richard Zhao26c696c2012-07-07 22:56:40 +08001213 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001214
David Lopoaa69a802008-11-17 14:14:51 -08001215 req->status = -ECONNRESET;
1216
Artem Leonenko7c25a822010-12-14 23:46:55 -08001217 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001218 spin_unlock(mEp->lock);
1219 mReq->req.complete(&mEp->ep, &mReq->req);
1220 spin_lock(mEp->lock);
1221 }
1222
1223 spin_unlock_irqrestore(mEp->lock, flags);
1224 return 0;
1225}
1226
1227/**
1228 * ep_set_halt: sets the endpoint halt feature
1229 *
1230 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1231 */
1232static int ep_set_halt(struct usb_ep *ep, int value)
1233{
1234 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1235 int direction, retval = 0;
1236 unsigned long flags;
1237
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001238 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001239 return -EINVAL;
1240
1241 spin_lock_irqsave(mEp->lock, flags);
1242
1243#ifndef STALL_IN
1244 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1245 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301246 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001247 spin_unlock_irqrestore(mEp->lock, flags);
1248 return -EAGAIN;
1249 }
1250#endif
1251
1252 direction = mEp->dir;
1253 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001254 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001255
1256 if (!value)
1257 mEp->wedge = 0;
1258
1259 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1260 mEp->dir = (mEp->dir == TX) ? RX : TX;
1261
1262 } while (mEp->dir != direction);
1263
1264 spin_unlock_irqrestore(mEp->lock, flags);
1265 return retval;
1266}
1267
1268/**
1269 * ep_set_wedge: sets the halt feature and ignores clear requests
1270 *
1271 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1272 */
1273static int ep_set_wedge(struct usb_ep *ep)
1274{
1275 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1276 unsigned long flags;
1277
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001278 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001279 return -EINVAL;
1280
1281 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001282 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001283 spin_unlock_irqrestore(mEp->lock, flags);
1284
1285 return usb_ep_set_halt(ep);
1286}
1287
1288/**
1289 * ep_fifo_flush: flushes contents of a fifo
1290 *
1291 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1292 */
1293static void ep_fifo_flush(struct usb_ep *ep)
1294{
1295 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1296 unsigned long flags;
1297
David Lopoaa69a802008-11-17 14:14:51 -08001298 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001299 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001300 return;
1301 }
1302
1303 spin_lock_irqsave(mEp->lock, flags);
1304
Richard Zhao26c696c2012-07-07 22:56:40 +08001305 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001306
1307 spin_unlock_irqrestore(mEp->lock, flags);
1308}
1309
1310/**
1311 * Endpoint-specific part of the API to the USB controller hardware
1312 * Check "usb_gadget.h" for details
1313 */
1314static const struct usb_ep_ops usb_ep_ops = {
1315 .enable = ep_enable,
1316 .disable = ep_disable,
1317 .alloc_request = ep_alloc_request,
1318 .free_request = ep_free_request,
1319 .queue = ep_queue,
1320 .dequeue = ep_dequeue,
1321 .set_halt = ep_set_halt,
1322 .set_wedge = ep_set_wedge,
1323 .fifo_flush = ep_fifo_flush,
1324};
1325
1326/******************************************************************************
1327 * GADGET block
1328 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301329static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1330{
Richard Zhao26c696c2012-07-07 22:56:40 +08001331 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301332 unsigned long flags;
1333 int gadget_ready = 0;
1334
Richard Zhao26c696c2012-07-07 22:56:40 +08001335 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301336 return -EOPNOTSUPP;
1337
Richard Zhao26c696c2012-07-07 22:56:40 +08001338 spin_lock_irqsave(&ci->lock, flags);
1339 ci->vbus_active = is_active;
1340 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301341 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001342 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301343
1344 if (gadget_ready) {
1345 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301346 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001347 hw_device_reset(ci, USBMODE_CM_DC);
1348 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301349 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001350 hw_device_state(ci, 0);
1351 if (ci->platdata->notify_event)
1352 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301353 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001354 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301355 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301356 }
1357 }
1358
1359 return 0;
1360}
1361
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301362static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1363{
Richard Zhao26c696c2012-07-07 22:56:40 +08001364 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301365 unsigned long flags;
1366 int ret = 0;
1367
Richard Zhao26c696c2012-07-07 22:56:40 +08001368 spin_lock_irqsave(&ci->lock, flags);
1369 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301370 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301371 goto out;
1372 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001373 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301374 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301375 goto out;
1376 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001377 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301378out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001379 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301380 return ret;
1381}
1382
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301383static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1384{
Richard Zhao26c696c2012-07-07 22:56:40 +08001385 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301386
Richard Zhao26c696c2012-07-07 22:56:40 +08001387 if (ci->transceiver)
1388 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301389 return -ENOTSUPP;
1390}
1391
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001392/* Change Data+ pullup status
1393 * this func is used by usb_gadget_connect/disconnet
1394 */
1395static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1396{
1397 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1398
1399 if (is_on)
1400 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1401 else
1402 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1403
1404 return 0;
1405}
1406
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001407static int ci13xxx_start(struct usb_gadget *gadget,
1408 struct usb_gadget_driver *driver);
1409static int ci13xxx_stop(struct usb_gadget *gadget,
1410 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001411/**
1412 * Device operations part of the API to the USB controller hardware,
1413 * which don't involve endpoints (or i/o)
1414 * Check "usb_gadget.h" for details
1415 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301416static const struct usb_gadget_ops usb_gadget_ops = {
1417 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301418 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001419 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301420 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001421 .udc_start = ci13xxx_start,
1422 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301423};
David Lopoaa69a802008-11-17 14:14:51 -08001424
Richard Zhao26c696c2012-07-07 22:56:40 +08001425static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001426{
1427 int retval = 0, i, j;
1428
Richard Zhao26c696c2012-07-07 22:56:40 +08001429 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001430 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001431 int k = i + j * ci->hw_ep_max/2;
1432 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001433
1434 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1435 (j == TX) ? "in" : "out");
1436
Richard Zhao26c696c2012-07-07 22:56:40 +08001437 mEp->ci = ci;
1438 mEp->lock = &ci->lock;
1439 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001440
1441 mEp->ep.name = mEp->name;
1442 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001443 /*
1444 * for ep0: maxP defined in desc, for other
1445 * eps, maxP is set by epautoconfig() called
1446 * by gadget layer
1447 */
1448 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001449
1450 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001451 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001452 &mEp->qh.dma);
1453 if (mEp->qh.ptr == NULL)
1454 retval = -ENOMEM;
1455 else
1456 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1457
1458 /*
1459 * set up shorthands for ep0 out and in endpoints,
1460 * don't add to gadget's ep_list
1461 */
1462 if (i == 0) {
1463 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001464 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001465 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001466 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001467
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001468 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001469 continue;
1470 }
1471
Richard Zhao26c696c2012-07-07 22:56:40 +08001472 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001473 }
1474
1475 return retval;
1476}
1477
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001478static void destroy_eps(struct ci13xxx *ci)
1479{
1480 int i;
1481
1482 for (i = 0; i < ci->hw_ep_max; i++) {
1483 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1484
1485 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1486 }
1487}
1488
David Lopoaa69a802008-11-17 14:14:51 -08001489/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001490 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001491 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001492 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001493 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001494 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001495 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001496static int ci13xxx_start(struct usb_gadget *gadget,
1497 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001498{
Richard Zhao26c696c2012-07-07 22:56:40 +08001499 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301500 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001501 int retval = -ENOMEM;
1502
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001503 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001504 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001505
David Lopoaa69a802008-11-17 14:14:51 -08001506
Richard Zhao26c696c2012-07-07 22:56:40 +08001507 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1508 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301509 if (retval)
1510 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001511
Richard Zhao26c696c2012-07-07 22:56:40 +08001512 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1513 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301514 if (retval)
1515 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001516 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001517
Richard Zhao26c696c2012-07-07 22:56:40 +08001518 ci->driver = driver;
1519 pm_runtime_get_sync(&ci->gadget.dev);
1520 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1521 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001522 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001523 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301524 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001525 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301526 goto done;
1527 }
1528 }
1529
Richard Zhao26c696c2012-07-07 22:56:40 +08001530 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301531 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001532 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001533
1534 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001535 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001536 return retval;
1537}
David Lopoaa69a802008-11-17 14:14:51 -08001538
1539/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001540 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001541 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001542static int ci13xxx_stop(struct usb_gadget *gadget,
1543 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001544{
Richard Zhao26c696c2012-07-07 22:56:40 +08001545 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001546 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001547
Richard Zhao26c696c2012-07-07 22:56:40 +08001548 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001549
Richard Zhao26c696c2012-07-07 22:56:40 +08001550 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1551 ci->vbus_active) {
1552 hw_device_state(ci, 0);
1553 if (ci->platdata->notify_event)
1554 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301555 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001556 ci->driver = NULL;
1557 spin_unlock_irqrestore(&ci->lock, flags);
1558 _gadget_stop_activity(&ci->gadget);
1559 spin_lock_irqsave(&ci->lock, flags);
1560 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301561 }
David Lopoaa69a802008-11-17 14:14:51 -08001562
Richard Zhao26c696c2012-07-07 22:56:40 +08001563 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001564
David Lopoaa69a802008-11-17 14:14:51 -08001565 return 0;
1566}
David Lopoaa69a802008-11-17 14:14:51 -08001567
1568/******************************************************************************
1569 * BUS block
1570 *****************************************************************************/
1571/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001572 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001573 *
1574 * This function returns IRQ_HANDLED if the IRQ has been handled
1575 * It locks access to registers
1576 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001577static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001578{
David Lopoaa69a802008-11-17 14:14:51 -08001579 irqreturn_t retval;
1580 u32 intr;
1581
Richard Zhao26c696c2012-07-07 22:56:40 +08001582 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001583 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001584
Richard Zhao26c696c2012-07-07 22:56:40 +08001585 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301586
Richard Zhao26c696c2012-07-07 22:56:40 +08001587 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1588 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001589 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001590 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301591 return IRQ_NONE;
1592 }
1593 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001594 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001595
Alexander Shishkine443b332012-05-11 17:25:46 +03001596 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001597 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001598 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001599 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001600
David Lopoaa69a802008-11-17 14:14:51 -08001601 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001602 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001603 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001604 if (ci->suspended && ci->driver->resume) {
1605 spin_unlock(&ci->lock);
1606 ci->driver->resume(&ci->gadget);
1607 spin_lock(&ci->lock);
1608 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301609 }
David Lopoaa69a802008-11-17 14:14:51 -08001610 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001611
1612 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001613 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001614
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301615 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001616 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1617 ci->driver->suspend) {
1618 ci->suspended = 1;
1619 spin_unlock(&ci->lock);
1620 ci->driver->suspend(&ci->gadget);
1621 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301622 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301623 }
David Lopoaa69a802008-11-17 14:14:51 -08001624 retval = IRQ_HANDLED;
1625 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001626 retval = IRQ_NONE;
1627 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001628 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001629
1630 return retval;
1631}
1632
1633/**
1634 * udc_release: driver release function
1635 * @dev: device
1636 *
1637 * Currently does nothing
1638 */
1639static void udc_release(struct device *dev)
1640{
David Lopoaa69a802008-11-17 14:14:51 -08001641}
1642
1643/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001644 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001645 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001646 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001647static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001648{
Richard Zhao26c696c2012-07-07 22:56:40 +08001649 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001650 int retval = 0;
1651
Richard Zhao26c696c2012-07-07 22:56:40 +08001652 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001653
Richard Zhao26c696c2012-07-07 22:56:40 +08001654 ci->gadget.ops = &usb_gadget_ops;
1655 ci->gadget.speed = USB_SPEED_UNKNOWN;
1656 ci->gadget.max_speed = USB_SPEED_HIGH;
1657 ci->gadget.is_otg = 0;
1658 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001659
Richard Zhao26c696c2012-07-07 22:56:40 +08001660 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001661
Richard Zhao26c696c2012-07-07 22:56:40 +08001662 dev_set_name(&ci->gadget.dev, "gadget");
1663 ci->gadget.dev.dma_mask = dev->dma_mask;
1664 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1665 ci->gadget.dev.parent = dev;
1666 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001667
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001668 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001669 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001670 sizeof(struct ci13xxx_qh),
1671 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001672 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001673 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001674
Richard Zhao26c696c2012-07-07 22:56:40 +08001675 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001676 sizeof(struct ci13xxx_td),
1677 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001678 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001679 retval = -ENOMEM;
1680 goto free_qh_pool;
1681 }
1682
Richard Zhao26c696c2012-07-07 22:56:40 +08001683 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001684 if (retval)
1685 goto free_pools;
1686
Richard Zhao26c696c2012-07-07 22:56:40 +08001687 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301688
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001689 if (ci->global_phy)
1690 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301691
Richard Zhao26c696c2012-07-07 22:56:40 +08001692 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1693 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301694 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001695 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301696 }
1697 }
1698
Richard Zhao26c696c2012-07-07 22:56:40 +08001699 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1700 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301701 if (retval)
1702 goto put_transceiver;
1703 }
1704
Richard Zhao26c696c2012-07-07 22:56:40 +08001705 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301706 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001707 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301708 goto put_transceiver;
1709 }
David Lopoaa69a802008-11-17 14:14:51 -08001710
Richard Zhao26c696c2012-07-07 22:56:40 +08001711 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1712 retval = otg_set_peripheral(ci->transceiver->otg,
1713 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301714 if (retval)
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001715 goto unreg_device;
David Lopoaa69a802008-11-17 14:14:51 -08001716 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001717
Richard Zhao26c696c2012-07-07 22:56:40 +08001718 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001719 if (retval)
1720 goto remove_trans;
1721
Richard Zhao26c696c2012-07-07 22:56:40 +08001722 pm_runtime_no_callbacks(&ci->gadget.dev);
1723 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001724
David Lopoaa69a802008-11-17 14:14:51 -08001725 return retval;
1726
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001727remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001728 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001729 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001730 if (ci->global_phy)
1731 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001732 }
1733
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001734 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301735unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001736 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301737put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001738 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001739 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001740destroy_eps:
1741 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001742free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001743 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001744free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001745 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001746 return retval;
1747}
1748
1749/**
1750 * udc_remove: parent remove must call this to remove UDC
1751 *
1752 * No interrupts active, the IRQ has been released
1753 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001754static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001755{
Richard Zhao26c696c2012-07-07 22:56:40 +08001756 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001757 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001758
Richard Zhao26c696c2012-07-07 22:56:40 +08001759 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001760
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001761 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001762
Richard Zhao26c696c2012-07-07 22:56:40 +08001763 dma_pool_destroy(ci->td_pool);
1764 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001765
Richard Zhao26c696c2012-07-07 22:56:40 +08001766 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1767 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001768 if (ci->global_phy)
1769 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301770 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001771 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001772 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001773 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001774}
David Lopoaa69a802008-11-17 14:14:51 -08001775
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001776/**
1777 * ci_hdrc_gadget_init - initialize device related bits
1778 * ci: the controller
1779 *
1780 * This function enables the gadget role, if the device is "device capable".
1781 */
1782int ci_hdrc_gadget_init(struct ci13xxx *ci)
1783{
1784 struct ci_role_driver *rdrv;
1785
1786 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1787 return -ENXIO;
1788
1789 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1790 if (!rdrv)
1791 return -ENOMEM;
1792
1793 rdrv->start = udc_start;
1794 rdrv->stop = udc_stop;
1795 rdrv->irq = udc_irq;
1796 rdrv->name = "gadget";
1797 ci->roles[CI_ROLE_GADGET] = rdrv;
1798
1799 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001800}