blob: 94af0208dab83adb9c7c038022c004fa9bdc48e2 [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{
David Lopoaa69a802008-11-17 14:14:51 -0800484 if (mReq->req.status != -EALREADY)
485 return -EINVAL;
486
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200487 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->ptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530488 return -EBUSY;
489
490 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200491 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530492 return -EBUSY;
493 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
494 mReq->zptr = NULL;
495 }
David Lopoaa69a802008-11-17 14:14:51 -0800496
497 mReq->req.status = 0;
498
Richard Zhao26c696c2012-07-07 22:56:40 +0800499 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800500
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200501 mReq->req.status = le32_to_cpu(mReq->ptr->token) & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530502 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800503 mReq->req.status = -1;
504 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
505 mReq->req.status = -1;
506 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
507 mReq->req.status = -1;
508
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200509 mReq->req.actual = le32_to_cpu(mReq->ptr->token) & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200510 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800511 mReq->req.actual = mReq->req.length - mReq->req.actual;
512 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
513
514 return mReq->req.actual;
515}
516
517/**
518 * _ep_nuke: dequeues all endpoint requests
519 * @mEp: endpoint
520 *
521 * This function returns an error code
522 * Caller must hold lock
523 */
524static int _ep_nuke(struct ci13xxx_ep *mEp)
525__releases(mEp->lock)
526__acquires(mEp->lock)
527{
David Lopoaa69a802008-11-17 14:14:51 -0800528 if (mEp == NULL)
529 return -EINVAL;
530
Richard Zhao26c696c2012-07-07 22:56:40 +0800531 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800532
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530533 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800534
535 /* pop oldest request */
536 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530537 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800538 struct ci13xxx_req, queue);
539 list_del_init(&mReq->queue);
540 mReq->req.status = -ESHUTDOWN;
541
Artem Leonenko7c25a822010-12-14 23:46:55 -0800542 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800543 spin_unlock(mEp->lock);
544 mReq->req.complete(&mEp->ep, &mReq->req);
545 spin_lock(mEp->lock);
546 }
547 }
548 return 0;
549}
550
551/**
552 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
553 * @gadget: gadget
554 *
555 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800556 */
557static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800558{
559 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800560 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530561 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800562
Richard Zhao26c696c2012-07-07 22:56:40 +0800563 spin_lock_irqsave(&ci->lock, flags);
564 ci->gadget.speed = USB_SPEED_UNKNOWN;
565 ci->remote_wakeup = 0;
566 ci->suspended = 0;
567 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530568
David Lopoaa69a802008-11-17 14:14:51 -0800569 /* flush all endpoints */
570 gadget_for_each_ep(ep, gadget) {
571 usb_ep_fifo_flush(ep);
572 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800573 usb_ep_fifo_flush(&ci->ep0out->ep);
574 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800575
Richard Zhao26c696c2012-07-07 22:56:40 +0800576 if (ci->driver)
577 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800578
579 /* make sure to disable all endpoints */
580 gadget_for_each_ep(ep, gadget) {
581 usb_ep_disable(ep);
582 }
David Lopoaa69a802008-11-17 14:14:51 -0800583
Richard Zhao26c696c2012-07-07 22:56:40 +0800584 if (ci->status != NULL) {
585 usb_ep_free_request(&ci->ep0in->ep, ci->status);
586 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800587 }
588
David Lopoaa69a802008-11-17 14:14:51 -0800589 return 0;
590}
591
592/******************************************************************************
593 * ISR block
594 *****************************************************************************/
595/**
596 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800597 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800598 *
599 * This function resets USB engine after a bus reset occurred
600 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800601static void isr_reset_handler(struct ci13xxx *ci)
602__releases(ci->lock)
603__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800604{
David Lopoaa69a802008-11-17 14:14:51 -0800605 int retval;
606
Richard Zhao26c696c2012-07-07 22:56:40 +0800607 spin_unlock(&ci->lock);
608 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800609 if (retval)
610 goto done;
611
Richard Zhao26c696c2012-07-07 22:56:40 +0800612 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800613 if (retval)
614 goto done;
615
Richard Zhao26c696c2012-07-07 22:56:40 +0800616 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
617 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530618 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530619
Michael Grzeschikb9322252012-05-11 17:25:50 +0300620done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800621 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800622
David Lopoaa69a802008-11-17 14:14:51 -0800623 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800624 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800625}
626
627/**
628 * isr_get_status_complete: get_status request complete function
629 * @ep: endpoint
630 * @req: request handled
631 *
632 * Caller must release lock
633 */
634static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
635{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300636 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800637 return;
David Lopoaa69a802008-11-17 14:14:51 -0800638
639 kfree(req->buf);
640 usb_ep_free_request(ep, req);
641}
642
643/**
644 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800645 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800646 * @setup: setup request packet
647 *
648 * This function returns an error code
649 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800650static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800651 struct usb_ctrlrequest *setup)
652__releases(mEp->lock)
653__acquires(mEp->lock)
654{
Richard Zhao26c696c2012-07-07 22:56:40 +0800655 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800656 struct usb_request *req = NULL;
657 gfp_t gfp_flags = GFP_ATOMIC;
658 int dir, num, retval;
659
David Lopoaa69a802008-11-17 14:14:51 -0800660 if (mEp == NULL || setup == NULL)
661 return -EINVAL;
662
663 spin_unlock(mEp->lock);
664 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
665 spin_lock(mEp->lock);
666 if (req == NULL)
667 return -ENOMEM;
668
669 req->complete = isr_get_status_complete;
670 req->length = 2;
671 req->buf = kzalloc(req->length, gfp_flags);
672 if (req->buf == NULL) {
673 retval = -ENOMEM;
674 goto err_free_req;
675 }
676
677 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530678 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800679 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800680 retval = 0;
681 } else if ((setup->bRequestType & USB_RECIP_MASK) \
682 == USB_RECIP_ENDPOINT) {
683 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
684 TX : RX;
685 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800686 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800687 }
688 /* else do nothing; reserved for future use */
689
690 spin_unlock(mEp->lock);
691 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
692 spin_lock(mEp->lock);
693 if (retval)
694 goto err_free_buf;
695
696 return 0;
697
698 err_free_buf:
699 kfree(req->buf);
700 err_free_req:
701 spin_unlock(mEp->lock);
702 usb_ep_free_request(&mEp->ep, req);
703 spin_lock(mEp->lock);
704 return retval;
705}
706
707/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530708 * isr_setup_status_complete: setup_status request complete function
709 * @ep: endpoint
710 * @req: request handled
711 *
712 * Caller must release lock. Put the port in test mode if test mode
713 * feature is selected.
714 */
715static void
716isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
717{
Richard Zhao26c696c2012-07-07 22:56:40 +0800718 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530719 unsigned long flags;
720
Richard Zhao26c696c2012-07-07 22:56:40 +0800721 if (ci->setaddr) {
722 hw_usb_set_address(ci, ci->address);
723 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300724 }
725
Richard Zhao26c696c2012-07-07 22:56:40 +0800726 spin_lock_irqsave(&ci->lock, flags);
727 if (ci->test_mode)
728 hw_port_test_set(ci, ci->test_mode);
729 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530730}
731
732/**
David Lopoaa69a802008-11-17 14:14:51 -0800733 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800734 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800735 *
736 * This function returns an error code
737 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800738static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800739__releases(mEp->lock)
740__acquires(mEp->lock)
741{
742 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530743 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800744
Richard Zhao26c696c2012-07-07 22:56:40 +0800745 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
746 ci->status->context = ci;
747 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800748
749 spin_unlock(mEp->lock);
Richard Zhao26c696c2012-07-07 22:56:40 +0800750 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800751 spin_lock(mEp->lock);
752
753 return retval;
754}
755
756/**
757 * isr_tr_complete_low: transaction complete low level handler
758 * @mEp: endpoint
759 *
760 * This function returns an error code
761 * Caller must hold lock
762 */
763static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
764__releases(mEp->lock)
765__acquires(mEp->lock)
766{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530767 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530768 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300769 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800770
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530771 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
772 queue) {
773 retval = _hardware_dequeue(mEp, mReq);
774 if (retval < 0)
775 break;
776 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530777 if (mReq->req.complete != NULL) {
778 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530779 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
780 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800781 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530782 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530783 spin_lock(mEp->lock);
784 }
785 }
David Lopoaa69a802008-11-17 14:14:51 -0800786
Pavankumar Kondetief907482011-05-02 11:56:27 +0530787 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530788 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800789
David Lopoaa69a802008-11-17 14:14:51 -0800790 return retval;
791}
792
793/**
794 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800795 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800796 *
797 * This function handles traffic events
798 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800799static void isr_tr_complete_handler(struct ci13xxx *ci)
800__releases(ci->lock)
801__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800802{
803 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530804 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800805
Richard Zhao26c696c2012-07-07 22:56:40 +0800806 for (i = 0; i < ci->hw_ep_max; i++) {
807 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530808 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800809 struct usb_ctrlrequest req;
810
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200811 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800812 continue; /* not configured */
813
Richard Zhao26c696c2012-07-07 22:56:40 +0800814 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800815 err = isr_tr_complete_low(mEp);
816 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
817 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800818 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800819 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800820 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800821 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800822 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700823 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800824 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800825 }
826 }
827 }
828
829 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800830 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800831 continue;
832
833 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800834 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800835 continue;
836 }
837
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530838 /*
839 * Flush data and handshake transactions of previous
840 * setup packet.
841 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800842 _ep_nuke(ci->ep0out);
843 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530844
David Lopoaa69a802008-11-17 14:14:51 -0800845 /* read_setup_packet */
846 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800847 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530848 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800849 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800850
851 type = req.bRequestType;
852
Richard Zhao26c696c2012-07-07 22:56:40 +0800853 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800854
David Lopoaa69a802008-11-17 14:14:51 -0800855 switch (req.bRequest) {
856 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530857 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
858 le16_to_cpu(req.wValue) ==
859 USB_ENDPOINT_HALT) {
860 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800861 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530862 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530863 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530864 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530865 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800866 num += ci->hw_ep_max/2;
867 if (!ci->ci13xxx_ep[num].wedge) {
868 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530869 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800870 &ci->ci13xxx_ep[num].ep);
871 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530872 if (err)
873 break;
874 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800875 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530876 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
877 le16_to_cpu(req.wValue) ==
878 USB_DEVICE_REMOTE_WAKEUP) {
879 if (req.wLength != 0)
880 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800881 ci->remote_wakeup = 0;
882 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530883 } else {
884 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800885 }
David Lopoaa69a802008-11-17 14:14:51 -0800886 break;
887 case USB_REQ_GET_STATUS:
888 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
889 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
890 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
891 goto delegate;
892 if (le16_to_cpu(req.wLength) != 2 ||
893 le16_to_cpu(req.wValue) != 0)
894 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800895 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800896 break;
897 case USB_REQ_SET_ADDRESS:
898 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
899 goto delegate;
900 if (le16_to_cpu(req.wLength) != 0 ||
901 le16_to_cpu(req.wIndex) != 0)
902 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800903 ci->address = (u8)le16_to_cpu(req.wValue);
904 ci->setaddr = true;
905 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800906 break;
907 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530908 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
909 le16_to_cpu(req.wValue) ==
910 USB_ENDPOINT_HALT) {
911 if (req.wLength != 0)
912 break;
913 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530914 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530915 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530916 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800917 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800918
Richard Zhao26c696c2012-07-07 22:56:40 +0800919 spin_unlock(&ci->lock);
920 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
921 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530922 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800923 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530924 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530925 if (req.wLength != 0)
926 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530927 switch (le16_to_cpu(req.wValue)) {
928 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800929 ci->remote_wakeup = 1;
930 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530931 break;
932 case USB_DEVICE_TEST_MODE:
933 tmode = le16_to_cpu(req.wIndex) >> 8;
934 switch (tmode) {
935 case TEST_J:
936 case TEST_K:
937 case TEST_SE0_NAK:
938 case TEST_PACKET:
939 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800940 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530941 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800942 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530943 break;
944 default:
945 break;
946 }
947 default:
948 goto delegate;
949 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530950 } else {
951 goto delegate;
952 }
David Lopoaa69a802008-11-17 14:14:51 -0800953 break;
954 default:
955delegate:
956 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800957 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800958
Richard Zhao26c696c2012-07-07 22:56:40 +0800959 spin_unlock(&ci->lock);
960 err = ci->driver->setup(&ci->gadget, &req);
961 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800962 break;
963 }
964
965 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800966 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800967 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 dev_err(ci->dev, "error: ep_set_halt\n");
969 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800970 }
971 }
972}
973
974/******************************************************************************
975 * ENDPT block
976 *****************************************************************************/
977/**
978 * ep_enable: configure endpoint, making it usable
979 *
980 * Check usb_ep_enable() at "usb_gadget.h" for details
981 */
982static int ep_enable(struct usb_ep *ep,
983 const struct usb_endpoint_descriptor *desc)
984{
985 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530986 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800987 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +0200988 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800989
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 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001009 cap |= QH_IOS;
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001010 if (mEp->num)
1011 cap |= QH_ZLT;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001012 cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001013
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001014 mEp->qh.ptr->cap = cpu_to_le32(cap);
1015
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001016 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001017
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301018 /*
1019 * Enable endpoints in the HW other than ep0 as ep0
1020 * is always enabled
1021 */
1022 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001023 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001024
1025 spin_unlock_irqrestore(mEp->lock, flags);
1026 return retval;
1027}
1028
1029/**
1030 * ep_disable: endpoint is no longer usable
1031 *
1032 * Check usb_ep_disable() at "usb_gadget.h" for details
1033 */
1034static int ep_disable(struct usb_ep *ep)
1035{
1036 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1037 int direction, retval = 0;
1038 unsigned long flags;
1039
David Lopoaa69a802008-11-17 14:14:51 -08001040 if (ep == NULL)
1041 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001042 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001043 return -EBUSY;
1044
1045 spin_lock_irqsave(mEp->lock, flags);
1046
1047 /* only internal SW should disable ctrl endpts */
1048
1049 direction = mEp->dir;
1050 do {
David Lopoaa69a802008-11-17 14:14:51 -08001051 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001052 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001053
1054 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1055 mEp->dir = (mEp->dir == TX) ? RX : TX;
1056
1057 } while (mEp->dir != direction);
1058
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001059 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001060
1061 spin_unlock_irqrestore(mEp->lock, flags);
1062 return retval;
1063}
1064
1065/**
1066 * ep_alloc_request: allocate a request object to use with this endpoint
1067 *
1068 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1069 */
1070static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1071{
1072 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1073 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001074
Alexander Shishkin0f089092012-05-08 23:29:02 +03001075 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001076 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001077
David Lopoaa69a802008-11-17 14:14:51 -08001078 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1079 if (mReq != NULL) {
1080 INIT_LIST_HEAD(&mReq->queue);
1081
1082 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1083 &mReq->dma);
1084 if (mReq->ptr == NULL) {
1085 kfree(mReq);
1086 mReq = NULL;
1087 }
1088 }
1089
David Lopoaa69a802008-11-17 14:14:51 -08001090 return (mReq == NULL) ? NULL : &mReq->req;
1091}
1092
1093/**
1094 * ep_free_request: frees a request object
1095 *
1096 * Check usb_ep_free_request() at "usb_gadget.h" for details
1097 */
1098static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1099{
1100 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1101 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1102 unsigned long flags;
1103
David Lopoaa69a802008-11-17 14:14:51 -08001104 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001105 return;
1106 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001107 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001108 return;
1109 }
1110
1111 spin_lock_irqsave(mEp->lock, flags);
1112
1113 if (mReq->ptr)
1114 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1115 kfree(mReq);
1116
David Lopoaa69a802008-11-17 14:14:51 -08001117 spin_unlock_irqrestore(mEp->lock, flags);
1118}
1119
1120/**
1121 * ep_queue: queues (submits) an I/O request to an endpoint
1122 *
1123 * Check usb_ep_queue()* at usb_gadget.h" for details
1124 */
1125static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1126 gfp_t __maybe_unused gfp_flags)
1127{
1128 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1129 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Richard Zhao26c696c2012-07-07 22:56:40 +08001130 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -08001131 int retval = 0;
1132 unsigned long flags;
1133
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001134 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001135 return -EINVAL;
1136
1137 spin_lock_irqsave(mEp->lock, flags);
1138
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301139 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1140 if (req->length)
Richard Zhao26c696c2012-07-07 22:56:40 +08001141 mEp = (ci->ep0_dir == RX) ?
1142 ci->ep0out : ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301143 if (!list_empty(&mEp->qh.queue)) {
1144 _ep_nuke(mEp);
1145 retval = -EOVERFLOW;
Richard Zhao26c696c2012-07-07 22:56:40 +08001146 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
Alexander Shishkin0f089092012-05-08 23:29:02 +03001147 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301148 }
David Lopoaa69a802008-11-17 14:14:51 -08001149 }
1150
1151 /* first nuke then test link, e.g. previous status has not sent */
1152 if (!list_empty(&mReq->queue)) {
1153 retval = -EBUSY;
Richard Zhao26c696c2012-07-07 22:56:40 +08001154 dev_err(mEp->ci->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001155 goto done;
1156 }
1157
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001158 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1159 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001160 retval = -EMSGSIZE;
Richard Zhao26c696c2012-07-07 22:56:40 +08001161 dev_warn(mEp->ci->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001162 }
1163
David Lopoaa69a802008-11-17 14:14:51 -08001164 /* push request */
1165 mReq->req.status = -EINPROGRESS;
1166 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001167
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301168 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001169
Alexander Shishkin69b7e8d2013-03-30 12:53:50 +02001170 if (retval == -EALREADY)
David Lopoaa69a802008-11-17 14:14:51 -08001171 retval = 0;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301172 if (!retval)
1173 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001174
1175 done:
1176 spin_unlock_irqrestore(mEp->lock, flags);
1177 return retval;
1178}
1179
1180/**
1181 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1182 *
1183 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1184 */
1185static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1186{
1187 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1188 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1189 unsigned long flags;
1190
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301191 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001192 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301193 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001194 return -EINVAL;
1195
1196 spin_lock_irqsave(mEp->lock, flags);
1197
Richard Zhao26c696c2012-07-07 22:56:40 +08001198 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001199
1200 /* pop request */
1201 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001202
Richard Zhao26c696c2012-07-07 22:56:40 +08001203 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001204
David Lopoaa69a802008-11-17 14:14:51 -08001205 req->status = -ECONNRESET;
1206
Artem Leonenko7c25a822010-12-14 23:46:55 -08001207 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001208 spin_unlock(mEp->lock);
1209 mReq->req.complete(&mEp->ep, &mReq->req);
1210 spin_lock(mEp->lock);
1211 }
1212
1213 spin_unlock_irqrestore(mEp->lock, flags);
1214 return 0;
1215}
1216
1217/**
1218 * ep_set_halt: sets the endpoint halt feature
1219 *
1220 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1221 */
1222static int ep_set_halt(struct usb_ep *ep, int value)
1223{
1224 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1225 int direction, retval = 0;
1226 unsigned long flags;
1227
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001228 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001229 return -EINVAL;
1230
1231 spin_lock_irqsave(mEp->lock, flags);
1232
1233#ifndef STALL_IN
1234 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1235 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301236 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001237 spin_unlock_irqrestore(mEp->lock, flags);
1238 return -EAGAIN;
1239 }
1240#endif
1241
1242 direction = mEp->dir;
1243 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001244 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001245
1246 if (!value)
1247 mEp->wedge = 0;
1248
1249 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1250 mEp->dir = (mEp->dir == TX) ? RX : TX;
1251
1252 } while (mEp->dir != direction);
1253
1254 spin_unlock_irqrestore(mEp->lock, flags);
1255 return retval;
1256}
1257
1258/**
1259 * ep_set_wedge: sets the halt feature and ignores clear requests
1260 *
1261 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1262 */
1263static int ep_set_wedge(struct usb_ep *ep)
1264{
1265 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1266 unsigned long flags;
1267
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001268 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001269 return -EINVAL;
1270
1271 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001272 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001273 spin_unlock_irqrestore(mEp->lock, flags);
1274
1275 return usb_ep_set_halt(ep);
1276}
1277
1278/**
1279 * ep_fifo_flush: flushes contents of a fifo
1280 *
1281 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1282 */
1283static void ep_fifo_flush(struct usb_ep *ep)
1284{
1285 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1286 unsigned long flags;
1287
David Lopoaa69a802008-11-17 14:14:51 -08001288 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001289 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001290 return;
1291 }
1292
1293 spin_lock_irqsave(mEp->lock, flags);
1294
Richard Zhao26c696c2012-07-07 22:56:40 +08001295 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001296
1297 spin_unlock_irqrestore(mEp->lock, flags);
1298}
1299
1300/**
1301 * Endpoint-specific part of the API to the USB controller hardware
1302 * Check "usb_gadget.h" for details
1303 */
1304static const struct usb_ep_ops usb_ep_ops = {
1305 .enable = ep_enable,
1306 .disable = ep_disable,
1307 .alloc_request = ep_alloc_request,
1308 .free_request = ep_free_request,
1309 .queue = ep_queue,
1310 .dequeue = ep_dequeue,
1311 .set_halt = ep_set_halt,
1312 .set_wedge = ep_set_wedge,
1313 .fifo_flush = ep_fifo_flush,
1314};
1315
1316/******************************************************************************
1317 * GADGET block
1318 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301319static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1320{
Richard Zhao26c696c2012-07-07 22:56:40 +08001321 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301322 unsigned long flags;
1323 int gadget_ready = 0;
1324
Richard Zhao26c696c2012-07-07 22:56:40 +08001325 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301326 return -EOPNOTSUPP;
1327
Richard Zhao26c696c2012-07-07 22:56:40 +08001328 spin_lock_irqsave(&ci->lock, flags);
1329 ci->vbus_active = is_active;
1330 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301331 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001332 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301333
1334 if (gadget_ready) {
1335 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301336 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001337 hw_device_reset(ci, USBMODE_CM_DC);
1338 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301339 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001340 hw_device_state(ci, 0);
1341 if (ci->platdata->notify_event)
1342 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301343 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001344 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301345 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301346 }
1347 }
1348
1349 return 0;
1350}
1351
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301352static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1353{
Richard Zhao26c696c2012-07-07 22:56:40 +08001354 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301355 unsigned long flags;
1356 int ret = 0;
1357
Richard Zhao26c696c2012-07-07 22:56:40 +08001358 spin_lock_irqsave(&ci->lock, flags);
1359 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301360 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301361 goto out;
1362 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001363 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301364 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301365 goto out;
1366 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001367 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301368out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001369 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301370 return ret;
1371}
1372
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301373static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1374{
Richard Zhao26c696c2012-07-07 22:56:40 +08001375 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301376
Richard Zhao26c696c2012-07-07 22:56:40 +08001377 if (ci->transceiver)
1378 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301379 return -ENOTSUPP;
1380}
1381
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001382/* Change Data+ pullup status
1383 * this func is used by usb_gadget_connect/disconnet
1384 */
1385static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1386{
1387 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1388
1389 if (is_on)
1390 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1391 else
1392 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1393
1394 return 0;
1395}
1396
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001397static int ci13xxx_start(struct usb_gadget *gadget,
1398 struct usb_gadget_driver *driver);
1399static int ci13xxx_stop(struct usb_gadget *gadget,
1400 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001401/**
1402 * Device operations part of the API to the USB controller hardware,
1403 * which don't involve endpoints (or i/o)
1404 * Check "usb_gadget.h" for details
1405 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301406static const struct usb_gadget_ops usb_gadget_ops = {
1407 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301408 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001409 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301410 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001411 .udc_start = ci13xxx_start,
1412 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301413};
David Lopoaa69a802008-11-17 14:14:51 -08001414
Richard Zhao26c696c2012-07-07 22:56:40 +08001415static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001416{
1417 int retval = 0, i, j;
1418
Richard Zhao26c696c2012-07-07 22:56:40 +08001419 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001420 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001421 int k = i + j * ci->hw_ep_max/2;
1422 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001423
1424 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1425 (j == TX) ? "in" : "out");
1426
Richard Zhao26c696c2012-07-07 22:56:40 +08001427 mEp->ci = ci;
1428 mEp->lock = &ci->lock;
1429 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001430
1431 mEp->ep.name = mEp->name;
1432 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001433 /*
1434 * for ep0: maxP defined in desc, for other
1435 * eps, maxP is set by epautoconfig() called
1436 * by gadget layer
1437 */
1438 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001439
1440 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001441 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001442 &mEp->qh.dma);
1443 if (mEp->qh.ptr == NULL)
1444 retval = -ENOMEM;
1445 else
1446 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1447
1448 /*
1449 * set up shorthands for ep0 out and in endpoints,
1450 * don't add to gadget's ep_list
1451 */
1452 if (i == 0) {
1453 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001454 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001455 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001456 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001457
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001458 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001459 continue;
1460 }
1461
Richard Zhao26c696c2012-07-07 22:56:40 +08001462 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001463 }
1464
1465 return retval;
1466}
1467
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001468static void destroy_eps(struct ci13xxx *ci)
1469{
1470 int i;
1471
1472 for (i = 0; i < ci->hw_ep_max; i++) {
1473 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1474
1475 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1476 }
1477}
1478
David Lopoaa69a802008-11-17 14:14:51 -08001479/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001480 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001481 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001482 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001483 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001484 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001485 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001486static int ci13xxx_start(struct usb_gadget *gadget,
1487 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001488{
Richard Zhao26c696c2012-07-07 22:56:40 +08001489 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301490 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001491 int retval = -ENOMEM;
1492
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001493 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001494 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001495
David Lopoaa69a802008-11-17 14:14:51 -08001496
Richard Zhao26c696c2012-07-07 22:56:40 +08001497 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1498 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301499 if (retval)
1500 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001501
Richard Zhao26c696c2012-07-07 22:56:40 +08001502 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1503 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301504 if (retval)
1505 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001506 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001507
Richard Zhao26c696c2012-07-07 22:56:40 +08001508 ci->driver = driver;
1509 pm_runtime_get_sync(&ci->gadget.dev);
1510 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1511 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001512 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001513 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301514 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001515 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301516 goto done;
1517 }
1518 }
1519
Richard Zhao26c696c2012-07-07 22:56:40 +08001520 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301521 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001522 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001523
1524 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001525 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001526 return retval;
1527}
David Lopoaa69a802008-11-17 14:14:51 -08001528
1529/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001530 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001531 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001532static int ci13xxx_stop(struct usb_gadget *gadget,
1533 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001534{
Richard Zhao26c696c2012-07-07 22:56:40 +08001535 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001536 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001537
Richard Zhao26c696c2012-07-07 22:56:40 +08001538 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001539
Richard Zhao26c696c2012-07-07 22:56:40 +08001540 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1541 ci->vbus_active) {
1542 hw_device_state(ci, 0);
1543 if (ci->platdata->notify_event)
1544 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301545 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001546 ci->driver = NULL;
1547 spin_unlock_irqrestore(&ci->lock, flags);
1548 _gadget_stop_activity(&ci->gadget);
1549 spin_lock_irqsave(&ci->lock, flags);
1550 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301551 }
David Lopoaa69a802008-11-17 14:14:51 -08001552
Richard Zhao26c696c2012-07-07 22:56:40 +08001553 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001554
David Lopoaa69a802008-11-17 14:14:51 -08001555 return 0;
1556}
David Lopoaa69a802008-11-17 14:14:51 -08001557
1558/******************************************************************************
1559 * BUS block
1560 *****************************************************************************/
1561/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001562 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001563 *
1564 * This function returns IRQ_HANDLED if the IRQ has been handled
1565 * It locks access to registers
1566 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001567static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001568{
David Lopoaa69a802008-11-17 14:14:51 -08001569 irqreturn_t retval;
1570 u32 intr;
1571
Richard Zhao26c696c2012-07-07 22:56:40 +08001572 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001573 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001574
Richard Zhao26c696c2012-07-07 22:56:40 +08001575 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301576
Richard Zhao26c696c2012-07-07 22:56:40 +08001577 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1578 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001579 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001580 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301581 return IRQ_NONE;
1582 }
1583 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001584 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001585
Alexander Shishkine443b332012-05-11 17:25:46 +03001586 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001587 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001588 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001589 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001590
David Lopoaa69a802008-11-17 14:14:51 -08001591 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001593 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001594 if (ci->suspended && ci->driver->resume) {
1595 spin_unlock(&ci->lock);
1596 ci->driver->resume(&ci->gadget);
1597 spin_lock(&ci->lock);
1598 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301599 }
David Lopoaa69a802008-11-17 14:14:51 -08001600 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001601
1602 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001603 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001604
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301605 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001606 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1607 ci->driver->suspend) {
1608 ci->suspended = 1;
1609 spin_unlock(&ci->lock);
1610 ci->driver->suspend(&ci->gadget);
1611 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301612 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301613 }
David Lopoaa69a802008-11-17 14:14:51 -08001614 retval = IRQ_HANDLED;
1615 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001616 retval = IRQ_NONE;
1617 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001618 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001619
1620 return retval;
1621}
1622
1623/**
1624 * udc_release: driver release function
1625 * @dev: device
1626 *
1627 * Currently does nothing
1628 */
1629static void udc_release(struct device *dev)
1630{
David Lopoaa69a802008-11-17 14:14:51 -08001631}
1632
1633/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001634 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001635 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001636 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001637static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001638{
Richard Zhao26c696c2012-07-07 22:56:40 +08001639 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001640 int retval = 0;
1641
Richard Zhao26c696c2012-07-07 22:56:40 +08001642 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001643
Richard Zhao26c696c2012-07-07 22:56:40 +08001644 ci->gadget.ops = &usb_gadget_ops;
1645 ci->gadget.speed = USB_SPEED_UNKNOWN;
1646 ci->gadget.max_speed = USB_SPEED_HIGH;
1647 ci->gadget.is_otg = 0;
1648 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001649
Richard Zhao26c696c2012-07-07 22:56:40 +08001650 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001651
Richard Zhao26c696c2012-07-07 22:56:40 +08001652 dev_set_name(&ci->gadget.dev, "gadget");
1653 ci->gadget.dev.dma_mask = dev->dma_mask;
1654 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1655 ci->gadget.dev.parent = dev;
1656 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001657
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001658 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001659 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001660 sizeof(struct ci13xxx_qh),
1661 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001662 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001663 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001664
Richard Zhao26c696c2012-07-07 22:56:40 +08001665 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001666 sizeof(struct ci13xxx_td),
1667 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001668 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001669 retval = -ENOMEM;
1670 goto free_qh_pool;
1671 }
1672
Richard Zhao26c696c2012-07-07 22:56:40 +08001673 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001674 if (retval)
1675 goto free_pools;
1676
Richard Zhao26c696c2012-07-07 22:56:40 +08001677 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301678
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001679 if (ci->global_phy)
1680 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301681
Richard Zhao26c696c2012-07-07 22:56:40 +08001682 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1683 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301684 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001685 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301686 }
1687 }
1688
Richard Zhao26c696c2012-07-07 22:56:40 +08001689 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1690 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301691 if (retval)
1692 goto put_transceiver;
1693 }
1694
Richard Zhao26c696c2012-07-07 22:56:40 +08001695 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301696 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001697 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301698 goto put_transceiver;
1699 }
David Lopoaa69a802008-11-17 14:14:51 -08001700
Richard Zhao26c696c2012-07-07 22:56:40 +08001701 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1702 retval = otg_set_peripheral(ci->transceiver->otg,
1703 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301704 if (retval)
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001705 goto unreg_device;
David Lopoaa69a802008-11-17 14:14:51 -08001706 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001707
Richard Zhao26c696c2012-07-07 22:56:40 +08001708 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001709 if (retval)
1710 goto remove_trans;
1711
Richard Zhao26c696c2012-07-07 22:56:40 +08001712 pm_runtime_no_callbacks(&ci->gadget.dev);
1713 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001714
David Lopoaa69a802008-11-17 14:14:51 -08001715 return retval;
1716
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001717remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001718 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001719 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001720 if (ci->global_phy)
1721 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001722 }
1723
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001724 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301725unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001726 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301727put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001728 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001729 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001730destroy_eps:
1731 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001732free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001733 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001734free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001736 return retval;
1737}
1738
1739/**
1740 * udc_remove: parent remove must call this to remove UDC
1741 *
1742 * No interrupts active, the IRQ has been released
1743 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001744static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001745{
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001747 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001748
Richard Zhao26c696c2012-07-07 22:56:40 +08001749 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001750
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001751 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001752
Richard Zhao26c696c2012-07-07 22:56:40 +08001753 dma_pool_destroy(ci->td_pool);
1754 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001755
Richard Zhao26c696c2012-07-07 22:56:40 +08001756 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1757 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001758 if (ci->global_phy)
1759 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301760 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001761 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001762 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001763 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001764}
David Lopoaa69a802008-11-17 14:14:51 -08001765
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001766/**
1767 * ci_hdrc_gadget_init - initialize device related bits
1768 * ci: the controller
1769 *
1770 * This function enables the gadget role, if the device is "device capable".
1771 */
1772int ci_hdrc_gadget_init(struct ci13xxx *ci)
1773{
1774 struct ci_role_driver *rdrv;
1775
1776 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1777 return -ENXIO;
1778
1779 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1780 if (!rdrv)
1781 return -ENOMEM;
1782
1783 rdrv->start = udc_start;
1784 rdrv->stop = udc_stop;
1785 rdrv->irq = udc_irq;
1786 rdrv->name = "gadget";
1787 ci->roles[CI_ROLE_GADGET] = rdrv;
1788
1789 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001790}