blob: 0e11172a4c84aca892a1d60c7abdb2facd964593 [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));
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200466 mEp->qh.ptr->cap |= cpu_to_le32(QH_ZLT);
David Lopoaa69a802008-11-17 14:14:51 -0800467
468 wmb(); /* synchronize before ep prime */
469
Richard Zhao26c696c2012-07-07 22:56:40 +0800470 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800471 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530472done:
473 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800474}
475
476/**
477 * _hardware_dequeue: handles a request at hardware level
478 * @gadget: gadget
479 * @mEp: endpoint
480 *
481 * This function returns an error code
482 */
483static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
484{
David Lopoaa69a802008-11-17 14:14:51 -0800485 if (mReq->req.status != -EALREADY)
486 return -EINVAL;
487
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200488 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->ptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530489 return -EBUSY;
490
491 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200492 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530493 return -EBUSY;
494 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
495 mReq->zptr = NULL;
496 }
David Lopoaa69a802008-11-17 14:14:51 -0800497
498 mReq->req.status = 0;
499
Richard Zhao26c696c2012-07-07 22:56:40 +0800500 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800501
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200502 mReq->req.status = le32_to_cpu(mReq->ptr->token) & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530503 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800504 mReq->req.status = -1;
505 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
506 mReq->req.status = -1;
507 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
508 mReq->req.status = -1;
509
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200510 mReq->req.actual = le32_to_cpu(mReq->ptr->token) & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200511 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800512 mReq->req.actual = mReq->req.length - mReq->req.actual;
513 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
514
515 return mReq->req.actual;
516}
517
518/**
519 * _ep_nuke: dequeues all endpoint requests
520 * @mEp: endpoint
521 *
522 * This function returns an error code
523 * Caller must hold lock
524 */
525static int _ep_nuke(struct ci13xxx_ep *mEp)
526__releases(mEp->lock)
527__acquires(mEp->lock)
528{
David Lopoaa69a802008-11-17 14:14:51 -0800529 if (mEp == NULL)
530 return -EINVAL;
531
Richard Zhao26c696c2012-07-07 22:56:40 +0800532 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800533
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530534 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800535
536 /* pop oldest request */
537 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530538 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800539 struct ci13xxx_req, queue);
540 list_del_init(&mReq->queue);
541 mReq->req.status = -ESHUTDOWN;
542
Artem Leonenko7c25a822010-12-14 23:46:55 -0800543 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800544 spin_unlock(mEp->lock);
545 mReq->req.complete(&mEp->ep, &mReq->req);
546 spin_lock(mEp->lock);
547 }
548 }
549 return 0;
550}
551
552/**
553 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
554 * @gadget: gadget
555 *
556 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800557 */
558static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800559{
560 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800561 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530562 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800563
Richard Zhao26c696c2012-07-07 22:56:40 +0800564 spin_lock_irqsave(&ci->lock, flags);
565 ci->gadget.speed = USB_SPEED_UNKNOWN;
566 ci->remote_wakeup = 0;
567 ci->suspended = 0;
568 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530569
David Lopoaa69a802008-11-17 14:14:51 -0800570 /* flush all endpoints */
571 gadget_for_each_ep(ep, gadget) {
572 usb_ep_fifo_flush(ep);
573 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800574 usb_ep_fifo_flush(&ci->ep0out->ep);
575 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800576
Richard Zhao26c696c2012-07-07 22:56:40 +0800577 if (ci->driver)
578 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800579
580 /* make sure to disable all endpoints */
581 gadget_for_each_ep(ep, gadget) {
582 usb_ep_disable(ep);
583 }
David Lopoaa69a802008-11-17 14:14:51 -0800584
Richard Zhao26c696c2012-07-07 22:56:40 +0800585 if (ci->status != NULL) {
586 usb_ep_free_request(&ci->ep0in->ep, ci->status);
587 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800588 }
589
David Lopoaa69a802008-11-17 14:14:51 -0800590 return 0;
591}
592
593/******************************************************************************
594 * ISR block
595 *****************************************************************************/
596/**
597 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800598 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800599 *
600 * This function resets USB engine after a bus reset occurred
601 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800602static void isr_reset_handler(struct ci13xxx *ci)
603__releases(ci->lock)
604__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800605{
David Lopoaa69a802008-11-17 14:14:51 -0800606 int retval;
607
Richard Zhao26c696c2012-07-07 22:56:40 +0800608 spin_unlock(&ci->lock);
609 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800610 if (retval)
611 goto done;
612
Richard Zhao26c696c2012-07-07 22:56:40 +0800613 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800614 if (retval)
615 goto done;
616
Richard Zhao26c696c2012-07-07 22:56:40 +0800617 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
618 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530619 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530620
Michael Grzeschikb9322252012-05-11 17:25:50 +0300621done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800622 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800623
David Lopoaa69a802008-11-17 14:14:51 -0800624 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800625 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800626}
627
628/**
629 * isr_get_status_complete: get_status request complete function
630 * @ep: endpoint
631 * @req: request handled
632 *
633 * Caller must release lock
634 */
635static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
636{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300637 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800638 return;
David Lopoaa69a802008-11-17 14:14:51 -0800639
640 kfree(req->buf);
641 usb_ep_free_request(ep, req);
642}
643
644/**
645 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800646 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800647 * @setup: setup request packet
648 *
649 * This function returns an error code
650 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800651static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800652 struct usb_ctrlrequest *setup)
653__releases(mEp->lock)
654__acquires(mEp->lock)
655{
Richard Zhao26c696c2012-07-07 22:56:40 +0800656 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800657 struct usb_request *req = NULL;
658 gfp_t gfp_flags = GFP_ATOMIC;
659 int dir, num, retval;
660
David Lopoaa69a802008-11-17 14:14:51 -0800661 if (mEp == NULL || setup == NULL)
662 return -EINVAL;
663
664 spin_unlock(mEp->lock);
665 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
666 spin_lock(mEp->lock);
667 if (req == NULL)
668 return -ENOMEM;
669
670 req->complete = isr_get_status_complete;
671 req->length = 2;
672 req->buf = kzalloc(req->length, gfp_flags);
673 if (req->buf == NULL) {
674 retval = -ENOMEM;
675 goto err_free_req;
676 }
677
678 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530679 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800680 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800681 retval = 0;
682 } else if ((setup->bRequestType & USB_RECIP_MASK) \
683 == USB_RECIP_ENDPOINT) {
684 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
685 TX : RX;
686 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800687 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800688 }
689 /* else do nothing; reserved for future use */
690
691 spin_unlock(mEp->lock);
692 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
693 spin_lock(mEp->lock);
694 if (retval)
695 goto err_free_buf;
696
697 return 0;
698
699 err_free_buf:
700 kfree(req->buf);
701 err_free_req:
702 spin_unlock(mEp->lock);
703 usb_ep_free_request(&mEp->ep, req);
704 spin_lock(mEp->lock);
705 return retval;
706}
707
708/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530709 * isr_setup_status_complete: setup_status request complete function
710 * @ep: endpoint
711 * @req: request handled
712 *
713 * Caller must release lock. Put the port in test mode if test mode
714 * feature is selected.
715 */
716static void
717isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
718{
Richard Zhao26c696c2012-07-07 22:56:40 +0800719 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530720 unsigned long flags;
721
Richard Zhao26c696c2012-07-07 22:56:40 +0800722 if (ci->setaddr) {
723 hw_usb_set_address(ci, ci->address);
724 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300725 }
726
Richard Zhao26c696c2012-07-07 22:56:40 +0800727 spin_lock_irqsave(&ci->lock, flags);
728 if (ci->test_mode)
729 hw_port_test_set(ci, ci->test_mode);
730 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530731}
732
733/**
David Lopoaa69a802008-11-17 14:14:51 -0800734 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800735 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800736 *
737 * This function returns an error code
738 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800739static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800740__releases(mEp->lock)
741__acquires(mEp->lock)
742{
743 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530744 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800745
Richard Zhao26c696c2012-07-07 22:56:40 +0800746 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
747 ci->status->context = ci;
748 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800749
750 spin_unlock(mEp->lock);
Richard Zhao26c696c2012-07-07 22:56:40 +0800751 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800752 spin_lock(mEp->lock);
753
754 return retval;
755}
756
757/**
758 * isr_tr_complete_low: transaction complete low level handler
759 * @mEp: endpoint
760 *
761 * This function returns an error code
762 * Caller must hold lock
763 */
764static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
765__releases(mEp->lock)
766__acquires(mEp->lock)
767{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530768 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530769 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300770 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800771
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530772 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
773 queue) {
774 retval = _hardware_dequeue(mEp, mReq);
775 if (retval < 0)
776 break;
777 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530778 if (mReq->req.complete != NULL) {
779 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530780 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
781 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800782 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530783 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530784 spin_lock(mEp->lock);
785 }
786 }
David Lopoaa69a802008-11-17 14:14:51 -0800787
Pavankumar Kondetief907482011-05-02 11:56:27 +0530788 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530789 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800790
David Lopoaa69a802008-11-17 14:14:51 -0800791 return retval;
792}
793
794/**
795 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800796 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800797 *
798 * This function handles traffic events
799 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800800static void isr_tr_complete_handler(struct ci13xxx *ci)
801__releases(ci->lock)
802__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800803{
804 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530805 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800806
Richard Zhao26c696c2012-07-07 22:56:40 +0800807 for (i = 0; i < ci->hw_ep_max; i++) {
808 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530809 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800810 struct usb_ctrlrequest req;
811
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200812 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800813 continue; /* not configured */
814
Richard Zhao26c696c2012-07-07 22:56:40 +0800815 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800816 err = isr_tr_complete_low(mEp);
817 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
818 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800819 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800820 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800821 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800822 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800823 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700824 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800825 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800826 }
827 }
828 }
829
830 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800831 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800832 continue;
833
834 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800835 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800836 continue;
837 }
838
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530839 /*
840 * Flush data and handshake transactions of previous
841 * setup packet.
842 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800843 _ep_nuke(ci->ep0out);
844 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530845
David Lopoaa69a802008-11-17 14:14:51 -0800846 /* read_setup_packet */
847 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800848 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530849 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800850 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800851
852 type = req.bRequestType;
853
Richard Zhao26c696c2012-07-07 22:56:40 +0800854 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800855
David Lopoaa69a802008-11-17 14:14:51 -0800856 switch (req.bRequest) {
857 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530858 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
859 le16_to_cpu(req.wValue) ==
860 USB_ENDPOINT_HALT) {
861 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800862 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530863 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530864 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530865 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530866 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800867 num += ci->hw_ep_max/2;
868 if (!ci->ci13xxx_ep[num].wedge) {
869 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530870 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800871 &ci->ci13xxx_ep[num].ep);
872 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530873 if (err)
874 break;
875 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800876 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530877 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
878 le16_to_cpu(req.wValue) ==
879 USB_DEVICE_REMOTE_WAKEUP) {
880 if (req.wLength != 0)
881 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800882 ci->remote_wakeup = 0;
883 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530884 } else {
885 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800886 }
David Lopoaa69a802008-11-17 14:14:51 -0800887 break;
888 case USB_REQ_GET_STATUS:
889 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
890 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
891 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
892 goto delegate;
893 if (le16_to_cpu(req.wLength) != 2 ||
894 le16_to_cpu(req.wValue) != 0)
895 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800896 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800897 break;
898 case USB_REQ_SET_ADDRESS:
899 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
900 goto delegate;
901 if (le16_to_cpu(req.wLength) != 0 ||
902 le16_to_cpu(req.wIndex) != 0)
903 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800904 ci->address = (u8)le16_to_cpu(req.wValue);
905 ci->setaddr = true;
906 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800907 break;
908 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530909 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
910 le16_to_cpu(req.wValue) ==
911 USB_ENDPOINT_HALT) {
912 if (req.wLength != 0)
913 break;
914 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530915 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530916 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530917 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800918 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800919
Richard Zhao26c696c2012-07-07 22:56:40 +0800920 spin_unlock(&ci->lock);
921 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
922 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530923 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800924 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530925 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530926 if (req.wLength != 0)
927 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530928 switch (le16_to_cpu(req.wValue)) {
929 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800930 ci->remote_wakeup = 1;
931 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530932 break;
933 case USB_DEVICE_TEST_MODE:
934 tmode = le16_to_cpu(req.wIndex) >> 8;
935 switch (tmode) {
936 case TEST_J:
937 case TEST_K:
938 case TEST_SE0_NAK:
939 case TEST_PACKET:
940 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800941 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530942 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800943 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530944 break;
945 default:
946 break;
947 }
948 default:
949 goto delegate;
950 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530951 } else {
952 goto delegate;
953 }
David Lopoaa69a802008-11-17 14:14:51 -0800954 break;
955 default:
956delegate:
957 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800958 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800959
Richard Zhao26c696c2012-07-07 22:56:40 +0800960 spin_unlock(&ci->lock);
961 err = ci->driver->setup(&ci->gadget, &req);
962 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800963 break;
964 }
965
966 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800967 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800968 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800969 dev_err(ci->dev, "error: ep_set_halt\n");
970 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800971 }
972 }
973}
974
975/******************************************************************************
976 * ENDPT block
977 *****************************************************************************/
978/**
979 * ep_enable: configure endpoint, making it usable
980 *
981 * Check usb_ep_enable() at "usb_gadget.h" for details
982 */
983static int ep_enable(struct usb_ep *ep,
984 const struct usb_endpoint_descriptor *desc)
985{
986 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530987 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800988 unsigned long flags;
989
David Lopoaa69a802008-11-17 14:14:51 -0800990 if (ep == NULL || desc == NULL)
991 return -EINVAL;
992
993 spin_lock_irqsave(mEp->lock, flags);
994
995 /* only internal SW should enable ctrl endpts */
996
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200997 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -0800998
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530999 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001000 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001001
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001002 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1003 mEp->num = usb_endpoint_num(desc);
1004 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001005
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001006 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001007
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301008 mEp->qh.ptr->cap = 0;
David Lopof23e6492009-04-16 14:35:24 -07001009
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301010 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001011 mEp->qh.ptr->cap |= cpu_to_le32(QH_IOS);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301012 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001013 mEp->qh.ptr->cap &= cpu_to_le32(~QH_MULT);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301014 else
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001015 mEp->qh.ptr->cap &= cpu_to_le32(~QH_ZLT);
David Lopoaa69a802008-11-17 14:14:51 -08001016
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001017 mEp->qh.ptr->cap |= cpu_to_le32((mEp->ep.maxpacket << __ffs(QH_MAX_PKT))
1018 & QH_MAX_PKT);
1019 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001020
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301021 /*
1022 * Enable endpoints in the HW other than ep0 as ep0
1023 * is always enabled
1024 */
1025 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001026 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001027
1028 spin_unlock_irqrestore(mEp->lock, flags);
1029 return retval;
1030}
1031
1032/**
1033 * ep_disable: endpoint is no longer usable
1034 *
1035 * Check usb_ep_disable() at "usb_gadget.h" for details
1036 */
1037static int ep_disable(struct usb_ep *ep)
1038{
1039 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1040 int direction, retval = 0;
1041 unsigned long flags;
1042
David Lopoaa69a802008-11-17 14:14:51 -08001043 if (ep == NULL)
1044 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001045 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001046 return -EBUSY;
1047
1048 spin_lock_irqsave(mEp->lock, flags);
1049
1050 /* only internal SW should disable ctrl endpts */
1051
1052 direction = mEp->dir;
1053 do {
David Lopoaa69a802008-11-17 14:14:51 -08001054 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001055 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001056
1057 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1058 mEp->dir = (mEp->dir == TX) ? RX : TX;
1059
1060 } while (mEp->dir != direction);
1061
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001062 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001063
1064 spin_unlock_irqrestore(mEp->lock, flags);
1065 return retval;
1066}
1067
1068/**
1069 * ep_alloc_request: allocate a request object to use with this endpoint
1070 *
1071 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1072 */
1073static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1074{
1075 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1076 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001077
Alexander Shishkin0f089092012-05-08 23:29:02 +03001078 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001079 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001080
David Lopoaa69a802008-11-17 14:14:51 -08001081 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1082 if (mReq != NULL) {
1083 INIT_LIST_HEAD(&mReq->queue);
1084
1085 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1086 &mReq->dma);
1087 if (mReq->ptr == NULL) {
1088 kfree(mReq);
1089 mReq = NULL;
1090 }
1091 }
1092
David Lopoaa69a802008-11-17 14:14:51 -08001093 return (mReq == NULL) ? NULL : &mReq->req;
1094}
1095
1096/**
1097 * ep_free_request: frees a request object
1098 *
1099 * Check usb_ep_free_request() at "usb_gadget.h" for details
1100 */
1101static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1102{
1103 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1104 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1105 unsigned long flags;
1106
David Lopoaa69a802008-11-17 14:14:51 -08001107 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001108 return;
1109 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001110 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001111 return;
1112 }
1113
1114 spin_lock_irqsave(mEp->lock, flags);
1115
1116 if (mReq->ptr)
1117 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1118 kfree(mReq);
1119
David Lopoaa69a802008-11-17 14:14:51 -08001120 spin_unlock_irqrestore(mEp->lock, flags);
1121}
1122
1123/**
1124 * ep_queue: queues (submits) an I/O request to an endpoint
1125 *
1126 * Check usb_ep_queue()* at usb_gadget.h" for details
1127 */
1128static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1129 gfp_t __maybe_unused gfp_flags)
1130{
1131 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1132 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Richard Zhao26c696c2012-07-07 22:56:40 +08001133 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -08001134 int retval = 0;
1135 unsigned long flags;
1136
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001137 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001138 return -EINVAL;
1139
1140 spin_lock_irqsave(mEp->lock, flags);
1141
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301142 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1143 if (req->length)
Richard Zhao26c696c2012-07-07 22:56:40 +08001144 mEp = (ci->ep0_dir == RX) ?
1145 ci->ep0out : ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301146 if (!list_empty(&mEp->qh.queue)) {
1147 _ep_nuke(mEp);
1148 retval = -EOVERFLOW;
Richard Zhao26c696c2012-07-07 22:56:40 +08001149 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
Alexander Shishkin0f089092012-05-08 23:29:02 +03001150 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301151 }
David Lopoaa69a802008-11-17 14:14:51 -08001152 }
1153
1154 /* first nuke then test link, e.g. previous status has not sent */
1155 if (!list_empty(&mReq->queue)) {
1156 retval = -EBUSY;
Richard Zhao26c696c2012-07-07 22:56:40 +08001157 dev_err(mEp->ci->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001158 goto done;
1159 }
1160
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001161 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1162 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001163 retval = -EMSGSIZE;
Richard Zhao26c696c2012-07-07 22:56:40 +08001164 dev_warn(mEp->ci->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001165 }
1166
David Lopoaa69a802008-11-17 14:14:51 -08001167 /* push request */
1168 mReq->req.status = -EINPROGRESS;
1169 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001170
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301171 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001172
Alexander Shishkin69b7e8d2013-03-30 12:53:50 +02001173 if (retval == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -08001174 retval = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301175 if (!retval)
1176 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001177
1178 done:
1179 spin_unlock_irqrestore(mEp->lock, flags);
1180 return retval;
1181}
1182
1183/**
1184 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1185 *
1186 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1187 */
1188static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1189{
1190 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1191 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1192 unsigned long flags;
1193
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301194 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001195 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301196 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001197 return -EINVAL;
1198
1199 spin_lock_irqsave(mEp->lock, flags);
1200
Richard Zhao26c696c2012-07-07 22:56:40 +08001201 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001202
1203 /* pop request */
1204 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001205
Richard Zhao26c696c2012-07-07 22:56:40 +08001206 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001207
David Lopoaa69a802008-11-17 14:14:51 -08001208 req->status = -ECONNRESET;
1209
Artem Leonenko7c25a822010-12-14 23:46:55 -08001210 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001211 spin_unlock(mEp->lock);
1212 mReq->req.complete(&mEp->ep, &mReq->req);
1213 spin_lock(mEp->lock);
1214 }
1215
1216 spin_unlock_irqrestore(mEp->lock, flags);
1217 return 0;
1218}
1219
1220/**
1221 * ep_set_halt: sets the endpoint halt feature
1222 *
1223 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1224 */
1225static int ep_set_halt(struct usb_ep *ep, int value)
1226{
1227 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1228 int direction, retval = 0;
1229 unsigned long flags;
1230
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001231 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001232 return -EINVAL;
1233
1234 spin_lock_irqsave(mEp->lock, flags);
1235
1236#ifndef STALL_IN
1237 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1238 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301239 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001240 spin_unlock_irqrestore(mEp->lock, flags);
1241 return -EAGAIN;
1242 }
1243#endif
1244
1245 direction = mEp->dir;
1246 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001247 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001248
1249 if (!value)
1250 mEp->wedge = 0;
1251
1252 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1253 mEp->dir = (mEp->dir == TX) ? RX : TX;
1254
1255 } while (mEp->dir != direction);
1256
1257 spin_unlock_irqrestore(mEp->lock, flags);
1258 return retval;
1259}
1260
1261/**
1262 * ep_set_wedge: sets the halt feature and ignores clear requests
1263 *
1264 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1265 */
1266static int ep_set_wedge(struct usb_ep *ep)
1267{
1268 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1269 unsigned long flags;
1270
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001271 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001272 return -EINVAL;
1273
1274 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001275 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001276 spin_unlock_irqrestore(mEp->lock, flags);
1277
1278 return usb_ep_set_halt(ep);
1279}
1280
1281/**
1282 * ep_fifo_flush: flushes contents of a fifo
1283 *
1284 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1285 */
1286static void ep_fifo_flush(struct usb_ep *ep)
1287{
1288 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1289 unsigned long flags;
1290
David Lopoaa69a802008-11-17 14:14:51 -08001291 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001292 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001293 return;
1294 }
1295
1296 spin_lock_irqsave(mEp->lock, flags);
1297
Richard Zhao26c696c2012-07-07 22:56:40 +08001298 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001299
1300 spin_unlock_irqrestore(mEp->lock, flags);
1301}
1302
1303/**
1304 * Endpoint-specific part of the API to the USB controller hardware
1305 * Check "usb_gadget.h" for details
1306 */
1307static const struct usb_ep_ops usb_ep_ops = {
1308 .enable = ep_enable,
1309 .disable = ep_disable,
1310 .alloc_request = ep_alloc_request,
1311 .free_request = ep_free_request,
1312 .queue = ep_queue,
1313 .dequeue = ep_dequeue,
1314 .set_halt = ep_set_halt,
1315 .set_wedge = ep_set_wedge,
1316 .fifo_flush = ep_fifo_flush,
1317};
1318
1319/******************************************************************************
1320 * GADGET block
1321 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301322static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1323{
Richard Zhao26c696c2012-07-07 22:56:40 +08001324 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301325 unsigned long flags;
1326 int gadget_ready = 0;
1327
Richard Zhao26c696c2012-07-07 22:56:40 +08001328 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301329 return -EOPNOTSUPP;
1330
Richard Zhao26c696c2012-07-07 22:56:40 +08001331 spin_lock_irqsave(&ci->lock, flags);
1332 ci->vbus_active = is_active;
1333 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301334 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001335 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301336
1337 if (gadget_ready) {
1338 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301339 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001340 hw_device_reset(ci, USBMODE_CM_DC);
1341 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301342 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001343 hw_device_state(ci, 0);
1344 if (ci->platdata->notify_event)
1345 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301346 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001347 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301348 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301349 }
1350 }
1351
1352 return 0;
1353}
1354
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301355static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1356{
Richard Zhao26c696c2012-07-07 22:56:40 +08001357 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301358 unsigned long flags;
1359 int ret = 0;
1360
Richard Zhao26c696c2012-07-07 22:56:40 +08001361 spin_lock_irqsave(&ci->lock, flags);
1362 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301363 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301364 goto out;
1365 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001366 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301367 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301368 goto out;
1369 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001370 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301371out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001372 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301373 return ret;
1374}
1375
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301376static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1377{
Richard Zhao26c696c2012-07-07 22:56:40 +08001378 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301379
Richard Zhao26c696c2012-07-07 22:56:40 +08001380 if (ci->transceiver)
1381 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301382 return -ENOTSUPP;
1383}
1384
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001385/* Change Data+ pullup status
1386 * this func is used by usb_gadget_connect/disconnet
1387 */
1388static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1389{
1390 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1391
1392 if (is_on)
1393 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1394 else
1395 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1396
1397 return 0;
1398}
1399
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001400static int ci13xxx_start(struct usb_gadget *gadget,
1401 struct usb_gadget_driver *driver);
1402static int ci13xxx_stop(struct usb_gadget *gadget,
1403 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001404/**
1405 * Device operations part of the API to the USB controller hardware,
1406 * which don't involve endpoints (or i/o)
1407 * Check "usb_gadget.h" for details
1408 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301409static const struct usb_gadget_ops usb_gadget_ops = {
1410 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301411 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001412 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301413 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001414 .udc_start = ci13xxx_start,
1415 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301416};
David Lopoaa69a802008-11-17 14:14:51 -08001417
Richard Zhao26c696c2012-07-07 22:56:40 +08001418static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001419{
1420 int retval = 0, i, j;
1421
Richard Zhao26c696c2012-07-07 22:56:40 +08001422 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001423 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001424 int k = i + j * ci->hw_ep_max/2;
1425 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001426
1427 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1428 (j == TX) ? "in" : "out");
1429
Richard Zhao26c696c2012-07-07 22:56:40 +08001430 mEp->ci = ci;
1431 mEp->lock = &ci->lock;
1432 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001433
1434 mEp->ep.name = mEp->name;
1435 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001436 /*
1437 * for ep0: maxP defined in desc, for other
1438 * eps, maxP is set by epautoconfig() called
1439 * by gadget layer
1440 */
1441 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001442
1443 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001444 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001445 &mEp->qh.dma);
1446 if (mEp->qh.ptr == NULL)
1447 retval = -ENOMEM;
1448 else
1449 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1450
1451 /*
1452 * set up shorthands for ep0 out and in endpoints,
1453 * don't add to gadget's ep_list
1454 */
1455 if (i == 0) {
1456 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001457 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001458 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001459 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001460
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001461 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001462 continue;
1463 }
1464
Richard Zhao26c696c2012-07-07 22:56:40 +08001465 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001466 }
1467
1468 return retval;
1469}
1470
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001471static void destroy_eps(struct ci13xxx *ci)
1472{
1473 int i;
1474
1475 for (i = 0; i < ci->hw_ep_max; i++) {
1476 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1477
1478 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1479 }
1480}
1481
David Lopoaa69a802008-11-17 14:14:51 -08001482/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001483 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001484 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001485 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001486 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001487 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001488 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001489static int ci13xxx_start(struct usb_gadget *gadget,
1490 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001491{
Richard Zhao26c696c2012-07-07 22:56:40 +08001492 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301493 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001494 int retval = -ENOMEM;
1495
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001496 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001497 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001498
David Lopoaa69a802008-11-17 14:14:51 -08001499
Richard Zhao26c696c2012-07-07 22:56:40 +08001500 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1501 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301502 if (retval)
1503 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001504
Richard Zhao26c696c2012-07-07 22:56:40 +08001505 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1506 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301507 if (retval)
1508 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001509 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001510
Richard Zhao26c696c2012-07-07 22:56:40 +08001511 ci->driver = driver;
1512 pm_runtime_get_sync(&ci->gadget.dev);
1513 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1514 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001515 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001516 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301517 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001518 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301519 goto done;
1520 }
1521 }
1522
Richard Zhao26c696c2012-07-07 22:56:40 +08001523 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301524 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001525 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001526
1527 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001528 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001529 return retval;
1530}
David Lopoaa69a802008-11-17 14:14:51 -08001531
1532/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001533 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001534 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001535static int ci13xxx_stop(struct usb_gadget *gadget,
1536 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001537{
Richard Zhao26c696c2012-07-07 22:56:40 +08001538 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001539 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001540
Richard Zhao26c696c2012-07-07 22:56:40 +08001541 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001542
Richard Zhao26c696c2012-07-07 22:56:40 +08001543 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1544 ci->vbus_active) {
1545 hw_device_state(ci, 0);
1546 if (ci->platdata->notify_event)
1547 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301548 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001549 ci->driver = NULL;
1550 spin_unlock_irqrestore(&ci->lock, flags);
1551 _gadget_stop_activity(&ci->gadget);
1552 spin_lock_irqsave(&ci->lock, flags);
1553 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301554 }
David Lopoaa69a802008-11-17 14:14:51 -08001555
Richard Zhao26c696c2012-07-07 22:56:40 +08001556 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001557
David Lopoaa69a802008-11-17 14:14:51 -08001558 return 0;
1559}
David Lopoaa69a802008-11-17 14:14:51 -08001560
1561/******************************************************************************
1562 * BUS block
1563 *****************************************************************************/
1564/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001565 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001566 *
1567 * This function returns IRQ_HANDLED if the IRQ has been handled
1568 * It locks access to registers
1569 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001570static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001571{
David Lopoaa69a802008-11-17 14:14:51 -08001572 irqreturn_t retval;
1573 u32 intr;
1574
Richard Zhao26c696c2012-07-07 22:56:40 +08001575 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001576 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001577
Richard Zhao26c696c2012-07-07 22:56:40 +08001578 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301579
Richard Zhao26c696c2012-07-07 22:56:40 +08001580 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1581 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001582 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001583 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301584 return IRQ_NONE;
1585 }
1586 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001587 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001588
Alexander Shishkine443b332012-05-11 17:25:46 +03001589 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001590 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001591 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001593
David Lopoaa69a802008-11-17 14:14:51 -08001594 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001595 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001596 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001597 if (ci->suspended && ci->driver->resume) {
1598 spin_unlock(&ci->lock);
1599 ci->driver->resume(&ci->gadget);
1600 spin_lock(&ci->lock);
1601 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301602 }
David Lopoaa69a802008-11-17 14:14:51 -08001603 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001604
1605 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001606 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001607
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301608 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001609 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1610 ci->driver->suspend) {
1611 ci->suspended = 1;
1612 spin_unlock(&ci->lock);
1613 ci->driver->suspend(&ci->gadget);
1614 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301615 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301616 }
David Lopoaa69a802008-11-17 14:14:51 -08001617 retval = IRQ_HANDLED;
1618 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001619 retval = IRQ_NONE;
1620 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001621 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001622
1623 return retval;
1624}
1625
1626/**
1627 * udc_release: driver release function
1628 * @dev: device
1629 *
1630 * Currently does nothing
1631 */
1632static void udc_release(struct device *dev)
1633{
David Lopoaa69a802008-11-17 14:14:51 -08001634}
1635
1636/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001637 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001638 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001639 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001640static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001641{
Richard Zhao26c696c2012-07-07 22:56:40 +08001642 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001643 int retval = 0;
1644
Richard Zhao26c696c2012-07-07 22:56:40 +08001645 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001646
Richard Zhao26c696c2012-07-07 22:56:40 +08001647 ci->gadget.ops = &usb_gadget_ops;
1648 ci->gadget.speed = USB_SPEED_UNKNOWN;
1649 ci->gadget.max_speed = USB_SPEED_HIGH;
1650 ci->gadget.is_otg = 0;
1651 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001652
Richard Zhao26c696c2012-07-07 22:56:40 +08001653 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001654
Richard Zhao26c696c2012-07-07 22:56:40 +08001655 dev_set_name(&ci->gadget.dev, "gadget");
1656 ci->gadget.dev.dma_mask = dev->dma_mask;
1657 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1658 ci->gadget.dev.parent = dev;
1659 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001660
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001661 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001662 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001663 sizeof(struct ci13xxx_qh),
1664 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001665 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001666 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001667
Richard Zhao26c696c2012-07-07 22:56:40 +08001668 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001669 sizeof(struct ci13xxx_td),
1670 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001671 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001672 retval = -ENOMEM;
1673 goto free_qh_pool;
1674 }
1675
Richard Zhao26c696c2012-07-07 22:56:40 +08001676 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001677 if (retval)
1678 goto free_pools;
1679
Richard Zhao26c696c2012-07-07 22:56:40 +08001680 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301681
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001682 if (ci->global_phy)
1683 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301684
Richard Zhao26c696c2012-07-07 22:56:40 +08001685 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1686 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301687 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001688 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301689 }
1690 }
1691
Richard Zhao26c696c2012-07-07 22:56:40 +08001692 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1693 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301694 if (retval)
1695 goto put_transceiver;
1696 }
1697
Richard Zhao26c696c2012-07-07 22:56:40 +08001698 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301699 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001700 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301701 goto put_transceiver;
1702 }
David Lopoaa69a802008-11-17 14:14:51 -08001703
Richard Zhao26c696c2012-07-07 22:56:40 +08001704 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1705 retval = otg_set_peripheral(ci->transceiver->otg,
1706 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301707 if (retval)
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001708 goto unreg_device;
David Lopoaa69a802008-11-17 14:14:51 -08001709 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001710
Richard Zhao26c696c2012-07-07 22:56:40 +08001711 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001712 if (retval)
1713 goto remove_trans;
1714
Richard Zhao26c696c2012-07-07 22:56:40 +08001715 pm_runtime_no_callbacks(&ci->gadget.dev);
1716 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001717
David Lopoaa69a802008-11-17 14:14:51 -08001718 return retval;
1719
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001720remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001721 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001722 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001723 if (ci->global_phy)
1724 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001725 }
1726
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001727 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301728unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001729 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301730put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001731 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001732 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001733destroy_eps:
1734 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001735free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001736 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001737free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001738 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001739 return retval;
1740}
1741
1742/**
1743 * udc_remove: parent remove must call this to remove UDC
1744 *
1745 * No interrupts active, the IRQ has been released
1746 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001747static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001748{
Richard Zhao26c696c2012-07-07 22:56:40 +08001749 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001750 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001751
Richard Zhao26c696c2012-07-07 22:56:40 +08001752 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001753
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001754 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001755
Richard Zhao26c696c2012-07-07 22:56:40 +08001756 dma_pool_destroy(ci->td_pool);
1757 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001758
Richard Zhao26c696c2012-07-07 22:56:40 +08001759 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1760 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001761 if (ci->global_phy)
1762 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301763 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001764 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001765 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001766 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001767}
David Lopoaa69a802008-11-17 14:14:51 -08001768
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001769/**
1770 * ci_hdrc_gadget_init - initialize device related bits
1771 * ci: the controller
1772 *
1773 * This function enables the gadget role, if the device is "device capable".
1774 */
1775int ci_hdrc_gadget_init(struct ci13xxx *ci)
1776{
1777 struct ci_role_driver *rdrv;
1778
1779 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1780 return -ENXIO;
1781
1782 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1783 if (!rdrv)
1784 return -ENOMEM;
1785
1786 rdrv->start = udc_start;
1787 rdrv->stop = udc_stop;
1788 rdrv->irq = udc_irq;
1789 rdrv->name = "gadget";
1790 ci->roles[CI_ROLE_GADGET] = rdrv;
1791
1792 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001793}