blob: b4cac44ce26c5f079700531c437690fa68e477cd [file] [log] [blame]
David Lopoaa69a802008-11-17 14:14:51 -08001/*
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03002 * udc.c - ChipIdea UDC driver
David Lopoaa69a802008-11-17 14:14:51 -08003 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020013#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080014#include <linux/device.h>
15#include <linux/dmapool.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053016#include <linux/err.h>
Alexander Shishkin5b083192013-03-30 02:46:18 +020017#include <linux/irqreturn.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053020#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Pavankumar Kondetif01ef572010-12-07 17:54:02 +053023#include <linux/usb/otg.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030024#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080025
Alexander Shishkine443b332012-05-11 17:25:46 +030026#include "ci.h"
27#include "udc.h"
28#include "bits.h"
29#include "debug.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020030
David Lopoaa69a802008-11-17 14:14:51 -080031/* control endpoint description */
32static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053033ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080034 .bLength = USB_DT_ENDPOINT_SIZE,
35 .bDescriptorType = USB_DT_ENDPOINT,
36
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053037 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
39 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
40};
41
42static const struct usb_endpoint_descriptor
43ctrl_endpt_in_desc = {
44 .bLength = USB_DT_ENDPOINT_SIZE,
45 .bDescriptorType = USB_DT_ENDPOINT,
46
47 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080048 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
49 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
50};
51
David Lopoaa69a802008-11-17 14:14:51 -080052/**
53 * hw_ep_bit: calculates the bit number
54 * @num: endpoint number
55 * @dir: endpoint direction
56 *
57 * This function returns bit number
58 */
59static inline int hw_ep_bit(int num, int dir)
60{
61 return num + (dir ? 16 : 0);
62}
63
Richard Zhao26c696c2012-07-07 22:56:40 +080064static inline int ep_to_bit(struct ci13xxx *ci, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020065{
Richard Zhao26c696c2012-07-07 22:56:40 +080066 int fill = 16 - ci->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020067
Richard Zhao26c696c2012-07-07 22:56:40 +080068 if (n >= ci->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020069 n += fill;
70
71 return n;
72}
73
David Lopoaa69a802008-11-17 14:14:51 -080074/**
Michael Grzeschikc0a48e62012-09-12 14:58:01 +030075 * hw_device_state: enables/disables interrupts (execute without interruption)
David Lopoaa69a802008-11-17 14:14:51 -080076 * @dma: 0 => disable, !0 => enable and set dma engine
77 *
78 * This function returns an error code
79 */
Richard Zhao26c696c2012-07-07 22:56:40 +080080static int hw_device_state(struct ci13xxx *ci, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080081{
82 if (dma) {
Richard Zhao26c696c2012-07-07 22:56:40 +080083 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080084 /* interrupt, error, port change, reset, sleep/suspend */
Richard Zhao26c696c2012-07-07 22:56:40 +080085 hw_write(ci, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080086 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
David Lopoaa69a802008-11-17 14:14:51 -080087 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +080088 hw_write(ci, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080089 }
90 return 0;
91}
92
93/**
94 * hw_ep_flush: flush endpoint fifo (execute without interruption)
95 * @num: endpoint number
96 * @dir: endpoint direction
97 *
98 * This function returns an error code
99 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800100static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800101{
102 int n = hw_ep_bit(num, dir);
103
104 do {
105 /* flush any pending transfer */
Richard Zhao26c696c2012-07-07 22:56:40 +0800106 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
107 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800108 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800109 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800110
111 return 0;
112}
113
114/**
115 * hw_ep_disable: disables endpoint (execute without interruption)
116 * @num: endpoint number
117 * @dir: endpoint direction
118 *
119 * This function returns an error code
120 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800121static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800122{
Richard Zhao26c696c2012-07-07 22:56:40 +0800123 hw_ep_flush(ci, num, dir);
124 hw_write(ci, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300125 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800126 return 0;
127}
128
129/**
130 * hw_ep_enable: enables endpoint (execute without interruption)
131 * @num: endpoint number
132 * @dir: endpoint direction
133 * @type: endpoint type
134 *
135 * This function returns an error code
136 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800137static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800138{
139 u32 mask, data;
140
141 if (dir) {
142 mask = ENDPTCTRL_TXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200143 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800144
145 mask |= ENDPTCTRL_TXS; /* unstall */
146 mask |= ENDPTCTRL_TXR; /* reset data toggle */
147 data |= ENDPTCTRL_TXR;
148 mask |= ENDPTCTRL_TXE; /* enable */
149 data |= ENDPTCTRL_TXE;
150 } else {
151 mask = ENDPTCTRL_RXT; /* type */
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200152 data = type << __ffs(mask);
David Lopoaa69a802008-11-17 14:14:51 -0800153
154 mask |= ENDPTCTRL_RXS; /* unstall */
155 mask |= ENDPTCTRL_RXR; /* reset data toggle */
156 data |= ENDPTCTRL_RXR;
157 mask |= ENDPTCTRL_RXE; /* enable */
158 data |= ENDPTCTRL_RXE;
159 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800160 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800161 return 0;
162}
163
164/**
165 * hw_ep_get_halt: return endpoint halt status
166 * @num: endpoint number
167 * @dir: endpoint direction
168 *
169 * This function returns 1 if endpoint halted
170 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800171static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800172{
173 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
174
Richard Zhao26c696c2012-07-07 22:56:40 +0800175 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800176}
177
178/**
David Lopoaa69a802008-11-17 14:14:51 -0800179 * hw_test_and_clear_setup_status: test & clear setup status (execute without
180 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200181 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800182 *
183 * This function returns setup status
184 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800185static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800186{
Richard Zhao26c696c2012-07-07 22:56:40 +0800187 n = ep_to_bit(ci, n);
188 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800189}
190
191/**
192 * hw_ep_prime: primes endpoint (execute without interruption)
193 * @num: endpoint number
194 * @dir: endpoint direction
195 * @is_ctrl: true if control endpoint
196 *
197 * This function returns an error code
198 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800199static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800200{
201 int n = hw_ep_bit(num, dir);
202
Richard Zhao26c696c2012-07-07 22:56:40 +0800203 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800204 return -EAGAIN;
205
Richard Zhao26c696c2012-07-07 22:56:40 +0800206 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800207
Richard Zhao26c696c2012-07-07 22:56:40 +0800208 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800209 cpu_relax();
Richard Zhao26c696c2012-07-07 22:56:40 +0800210 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800211 return -EAGAIN;
212
213 /* status shoult be tested according with manual but it doesn't work */
214 return 0;
215}
216
217/**
218 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
219 * without interruption)
220 * @num: endpoint number
221 * @dir: endpoint direction
222 * @value: true => stall, false => unstall
223 *
224 * This function returns an error code
225 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800226static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800227{
228 if (value != 0 && value != 1)
229 return -EINVAL;
230
231 do {
Alexander Shishkin262c1632012-05-08 23:28:59 +0300232 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800233 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
234 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
235
236 /* data toggle - reserved for EP0 but it's in ESS */
Richard Zhao26c696c2012-07-07 22:56:40 +0800237 hw_write(ci, reg, mask_xs|mask_xr,
Alexander Shishkin262c1632012-05-08 23:28:59 +0300238 value ? mask_xs : mask_xr);
Richard Zhao26c696c2012-07-07 22:56:40 +0800239 } while (value != hw_ep_get_halt(ci, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800240
241 return 0;
242}
243
244/**
David Lopoaa69a802008-11-17 14:14:51 -0800245 * hw_is_port_high_speed: test if port is high speed
246 *
247 * This function returns true if high speed port
248 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800249static int hw_port_is_high_speed(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800250{
Richard Zhao26c696c2012-07-07 22:56:40 +0800251 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
252 hw_read(ci, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800253}
254
255/**
David Lopoaa69a802008-11-17 14:14:51 -0800256 * hw_read_intr_enable: returns interrupt enable register
257 *
258 * This function returns register data
259 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800260static u32 hw_read_intr_enable(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800261{
Richard Zhao26c696c2012-07-07 22:56:40 +0800262 return hw_read(ci, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800263}
264
265/**
266 * hw_read_intr_status: returns interrupt status register
267 *
268 * This function returns register data
269 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800270static u32 hw_read_intr_status(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800271{
Richard Zhao26c696c2012-07-07 22:56:40 +0800272 return hw_read(ci, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800273}
274
275/**
David Lopoaa69a802008-11-17 14:14:51 -0800276 * hw_test_and_clear_complete: test & clear complete status (execute without
277 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200278 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800279 *
280 * This function returns complete status
281 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800282static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800283{
Richard Zhao26c696c2012-07-07 22:56:40 +0800284 n = ep_to_bit(ci, n);
285 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800286}
287
288/**
289 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
290 * without interruption)
291 *
292 * This function returns active interrutps
293 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800294static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800295{
Richard Zhao26c696c2012-07-07 22:56:40 +0800296 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800297
Richard Zhao26c696c2012-07-07 22:56:40 +0800298 hw_write(ci, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800299 return reg;
300}
301
302/**
303 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
304 * interruption)
305 *
306 * This function returns guard value
307 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800308static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800309{
Richard Zhao26c696c2012-07-07 22:56:40 +0800310 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800311}
312
313/**
314 * hw_test_and_set_setup_guard: test & set setup guard (execute without
315 * interruption)
316 *
317 * This function returns guard value
318 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800319static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800320{
Richard Zhao26c696c2012-07-07 22:56:40 +0800321 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800322}
323
324/**
325 * hw_usb_set_address: configures USB address (execute without interruption)
326 * @value: new USB address
327 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300328 * This function explicitly sets the address, without the "USBADRA" (advance)
329 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800330 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800331static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800332{
Richard Zhao26c696c2012-07-07 22:56:40 +0800333 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200334 value << __ffs(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800335}
336
337/**
338 * hw_usb_reset: restart device after a bus reset (execute without
339 * interruption)
340 *
341 * This function returns an error code
342 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800343static int hw_usb_reset(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800344{
Richard Zhao26c696c2012-07-07 22:56:40 +0800345 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800346
347 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800348 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800349
350 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800351 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800352
353 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800354 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800355
356 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800357 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800358 udelay(10); /* not RTOS friendly */
359
360 /* reset all endpoints ? */
361
362 /* reset internal status and wait for further instructions
363 no need to verify the port reset status (ESS does it) */
364
365 return 0;
366}
367
368/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800369 * UTIL block
370 *****************************************************************************/
371/**
372 * _usb_addr: calculates endpoint address from direction & number
373 * @ep: endpoint
374 */
375static inline u8 _usb_addr(struct ci13xxx_ep *ep)
376{
377 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
378}
379
380/**
381 * _hardware_queue: configures a request at hardware level
382 * @gadget: gadget
383 * @mEp: endpoint
384 *
385 * This function returns an error code
386 */
387static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
388{
Richard Zhao26c696c2012-07-07 22:56:40 +0800389 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -0800390 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530391 int ret = 0;
392 unsigned length = mReq->req.length;
David Lopoaa69a802008-11-17 14:14:51 -0800393
David Lopoaa69a802008-11-17 14:14:51 -0800394 /* don't queue twice */
395 if (mReq->req.status == -EALREADY)
396 return -EALREADY;
397
David Lopoaa69a802008-11-17 14:14:51 -0800398 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800399
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530400 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
401 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
402 &mReq->zdma);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300403 if (mReq->zptr == NULL)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530404 return -ENOMEM;
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300405
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530406 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200407 mReq->zptr->next = cpu_to_le32(TD_TERMINATE);
408 mReq->zptr->token = cpu_to_le32(TD_STATUS_ACTIVE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530409 if (!mReq->req.no_interrupt)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200410 mReq->zptr->token |= cpu_to_le32(TD_IOC);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530411 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800412 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300413 if (ret)
414 return ret;
415
David Lopoaa69a802008-11-17 14:14:51 -0800416 /*
417 * TD configuration
418 * TODO - handle requests which spawns into several TDs
419 */
420 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200421 mReq->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
422 mReq->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
423 mReq->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530424 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200425 mReq->ptr->next = cpu_to_le32(mReq->zdma);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530426 } else {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200427 mReq->ptr->next = cpu_to_le32(TD_TERMINATE);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530428 if (!mReq->req.no_interrupt)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200429 mReq->ptr->token |= cpu_to_le32(TD_IOC);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530430 }
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200431 mReq->ptr->page[0] = cpu_to_le32(mReq->req.dma);
Michael Grzeschikb983e512013-03-30 12:54:10 +0200432 for (i = 1; i < TD_PAGE_COUNT; i++) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200433 u32 page = mReq->req.dma + i * CI13XXX_PAGE_SIZE;
434 page &= ~TD_RESERVED_MASK;
435 mReq->ptr->page[i] = cpu_to_le32(page);
436 }
David Lopoaa69a802008-11-17 14:14:51 -0800437
Michael Grzeschika9c17432013-04-04 13:13:46 +0300438 wmb();
439
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530440 if (!list_empty(&mEp->qh.queue)) {
441 struct ci13xxx_req *mReqPrev;
442 int n = hw_ep_bit(mEp->num, mEp->dir);
443 int tmp_stat;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200444 u32 next = mReq->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530445
446 mReqPrev = list_entry(mEp->qh.queue.prev,
447 struct ci13xxx_req, queue);
448 if (mReqPrev->zptr)
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200449 mReqPrev->zptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530450 else
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200451 mReqPrev->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530452 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800453 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530454 goto done;
455 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800456 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
457 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
458 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
459 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530460 if (tmp_stat)
461 goto done;
462 }
463
464 /* QH configuration */
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200465 mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200466 mEp->qh.ptr->td.token &=
467 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800468
469 wmb(); /* synchronize before ep prime */
470
Richard Zhao26c696c2012-07-07 22:56:40 +0800471 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800472 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530473done:
474 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800475}
476
477/**
478 * _hardware_dequeue: handles a request at hardware level
479 * @gadget: gadget
480 * @mEp: endpoint
481 *
482 * This function returns an error code
483 */
484static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
485{
Michael Grzeschik9e506432013-03-30 12:54:07 +0200486 u32 tmptoken = le32_to_cpu(mReq->ptr->token);
487
David Lopoaa69a802008-11-17 14:14:51 -0800488 if (mReq->req.status != -EALREADY)
489 return -EINVAL;
490
Michael Grzeschik9e506432013-03-30 12:54:07 +0200491 if ((TD_STATUS_ACTIVE & tmptoken) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530492 return -EBUSY;
493
494 if (mReq->zptr) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200495 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530496 return -EBUSY;
497 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
498 mReq->zptr = NULL;
499 }
David Lopoaa69a802008-11-17 14:14:51 -0800500
501 mReq->req.status = 0;
502
Richard Zhao26c696c2012-07-07 22:56:40 +0800503 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800504
Michael Grzeschik9e506432013-03-30 12:54:07 +0200505 mReq->req.status = tmptoken & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530506 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800507 mReq->req.status = -1;
508 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
509 mReq->req.status = -1;
510 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
511 mReq->req.status = -1;
512
Michael Grzeschik9e506432013-03-30 12:54:07 +0200513 mReq->req.actual = tmptoken & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200514 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800515 mReq->req.actual = mReq->req.length - mReq->req.actual;
516 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
517
518 return mReq->req.actual;
519}
520
521/**
522 * _ep_nuke: dequeues all endpoint requests
523 * @mEp: endpoint
524 *
525 * This function returns an error code
526 * Caller must hold lock
527 */
528static int _ep_nuke(struct ci13xxx_ep *mEp)
529__releases(mEp->lock)
530__acquires(mEp->lock)
531{
David Lopoaa69a802008-11-17 14:14:51 -0800532 if (mEp == NULL)
533 return -EINVAL;
534
Richard Zhao26c696c2012-07-07 22:56:40 +0800535 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800536
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530537 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800538
539 /* pop oldest request */
540 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530541 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800542 struct ci13xxx_req, queue);
543 list_del_init(&mReq->queue);
544 mReq->req.status = -ESHUTDOWN;
545
Artem Leonenko7c25a822010-12-14 23:46:55 -0800546 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800547 spin_unlock(mEp->lock);
548 mReq->req.complete(&mEp->ep, &mReq->req);
549 spin_lock(mEp->lock);
550 }
551 }
552 return 0;
553}
554
555/**
556 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
557 * @gadget: gadget
558 *
559 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800560 */
561static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800562{
563 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800564 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530565 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800566
Richard Zhao26c696c2012-07-07 22:56:40 +0800567 spin_lock_irqsave(&ci->lock, flags);
568 ci->gadget.speed = USB_SPEED_UNKNOWN;
569 ci->remote_wakeup = 0;
570 ci->suspended = 0;
571 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530572
David Lopoaa69a802008-11-17 14:14:51 -0800573 /* flush all endpoints */
574 gadget_for_each_ep(ep, gadget) {
575 usb_ep_fifo_flush(ep);
576 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800577 usb_ep_fifo_flush(&ci->ep0out->ep);
578 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800579
Richard Zhao26c696c2012-07-07 22:56:40 +0800580 if (ci->driver)
581 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800582
583 /* make sure to disable all endpoints */
584 gadget_for_each_ep(ep, gadget) {
585 usb_ep_disable(ep);
586 }
David Lopoaa69a802008-11-17 14:14:51 -0800587
Richard Zhao26c696c2012-07-07 22:56:40 +0800588 if (ci->status != NULL) {
589 usb_ep_free_request(&ci->ep0in->ep, ci->status);
590 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800591 }
592
David Lopoaa69a802008-11-17 14:14:51 -0800593 return 0;
594}
595
596/******************************************************************************
597 * ISR block
598 *****************************************************************************/
599/**
600 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800601 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800602 *
603 * This function resets USB engine after a bus reset occurred
604 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800605static void isr_reset_handler(struct ci13xxx *ci)
606__releases(ci->lock)
607__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800608{
David Lopoaa69a802008-11-17 14:14:51 -0800609 int retval;
610
Richard Zhao26c696c2012-07-07 22:56:40 +0800611 spin_unlock(&ci->lock);
612 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800613 if (retval)
614 goto done;
615
Richard Zhao26c696c2012-07-07 22:56:40 +0800616 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800617 if (retval)
618 goto done;
619
Richard Zhao26c696c2012-07-07 22:56:40 +0800620 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
621 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530622 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530623
Michael Grzeschikb9322252012-05-11 17:25:50 +0300624done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800625 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800626
David Lopoaa69a802008-11-17 14:14:51 -0800627 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800628 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800629}
630
631/**
632 * isr_get_status_complete: get_status request complete function
633 * @ep: endpoint
634 * @req: request handled
635 *
636 * Caller must release lock
637 */
638static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
639{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300640 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800641 return;
David Lopoaa69a802008-11-17 14:14:51 -0800642
643 kfree(req->buf);
644 usb_ep_free_request(ep, req);
645}
646
647/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200648 * _ep_queue: queues (submits) an I/O request to an endpoint
649 *
650 * Caller must hold lock
651 */
652static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
653 gfp_t __maybe_unused gfp_flags)
654{
655 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
656 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
657 struct ci13xxx *ci = mEp->ci;
658 int retval = 0;
659
660 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
661 return -EINVAL;
662
663 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
664 if (req->length)
665 mEp = (ci->ep0_dir == RX) ?
666 ci->ep0out : ci->ep0in;
667 if (!list_empty(&mEp->qh.queue)) {
668 _ep_nuke(mEp);
669 retval = -EOVERFLOW;
670 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
671 _usb_addr(mEp));
672 }
673 }
674
675 /* first nuke then test link, e.g. previous status has not sent */
676 if (!list_empty(&mReq->queue)) {
677 dev_err(mEp->ci->dev, "request already in queue\n");
678 return -EBUSY;
679 }
680
Michael Grzeschikb983e512013-03-30 12:54:10 +0200681 if (req->length > (TD_PAGE_COUNT - 1) * CI13XXX_PAGE_SIZE) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200682 dev_err(mEp->ci->dev, "request bigger than one td\n");
683 return -EMSGSIZE;
684 }
685
686 /* push request */
687 mReq->req.status = -EINPROGRESS;
688 mReq->req.actual = 0;
689
690 retval = _hardware_enqueue(mEp, mReq);
691
692 if (retval == -EALREADY)
693 retval = 0;
694 if (!retval)
695 list_add_tail(&mReq->queue, &mEp->qh.queue);
696
697 return retval;
698}
699
700/**
David Lopoaa69a802008-11-17 14:14:51 -0800701 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800702 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800703 * @setup: setup request packet
704 *
705 * This function returns an error code
706 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800707static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800708 struct usb_ctrlrequest *setup)
709__releases(mEp->lock)
710__acquires(mEp->lock)
711{
Richard Zhao26c696c2012-07-07 22:56:40 +0800712 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800713 struct usb_request *req = NULL;
714 gfp_t gfp_flags = GFP_ATOMIC;
715 int dir, num, retval;
716
David Lopoaa69a802008-11-17 14:14:51 -0800717 if (mEp == NULL || setup == NULL)
718 return -EINVAL;
719
720 spin_unlock(mEp->lock);
721 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
722 spin_lock(mEp->lock);
723 if (req == NULL)
724 return -ENOMEM;
725
726 req->complete = isr_get_status_complete;
727 req->length = 2;
728 req->buf = kzalloc(req->length, gfp_flags);
729 if (req->buf == NULL) {
730 retval = -ENOMEM;
731 goto err_free_req;
732 }
733
734 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530735 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800736 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800737 retval = 0;
738 } else if ((setup->bRequestType & USB_RECIP_MASK) \
739 == USB_RECIP_ENDPOINT) {
740 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
741 TX : RX;
742 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800743 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800744 }
745 /* else do nothing; reserved for future use */
746
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200747 retval = _ep_queue(&mEp->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800748 if (retval)
749 goto err_free_buf;
750
751 return 0;
752
753 err_free_buf:
754 kfree(req->buf);
755 err_free_req:
756 spin_unlock(mEp->lock);
757 usb_ep_free_request(&mEp->ep, req);
758 spin_lock(mEp->lock);
759 return retval;
760}
761
762/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530763 * isr_setup_status_complete: setup_status request complete function
764 * @ep: endpoint
765 * @req: request handled
766 *
767 * Caller must release lock. Put the port in test mode if test mode
768 * feature is selected.
769 */
770static void
771isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
772{
Richard Zhao26c696c2012-07-07 22:56:40 +0800773 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530774 unsigned long flags;
775
Richard Zhao26c696c2012-07-07 22:56:40 +0800776 if (ci->setaddr) {
777 hw_usb_set_address(ci, ci->address);
778 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300779 }
780
Richard Zhao26c696c2012-07-07 22:56:40 +0800781 spin_lock_irqsave(&ci->lock, flags);
782 if (ci->test_mode)
783 hw_port_test_set(ci, ci->test_mode);
784 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530785}
786
787/**
David Lopoaa69a802008-11-17 14:14:51 -0800788 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800789 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800790 *
791 * This function returns an error code
792 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800793static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800794{
795 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530796 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800797
Richard Zhao26c696c2012-07-07 22:56:40 +0800798 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
799 ci->status->context = ci;
800 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800801
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200802 retval = _ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800803
804 return retval;
805}
806
807/**
808 * isr_tr_complete_low: transaction complete low level handler
809 * @mEp: endpoint
810 *
811 * This function returns an error code
812 * Caller must hold lock
813 */
814static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
815__releases(mEp->lock)
816__acquires(mEp->lock)
817{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530818 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530819 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300820 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800821
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530822 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
823 queue) {
824 retval = _hardware_dequeue(mEp, mReq);
825 if (retval < 0)
826 break;
827 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530828 if (mReq->req.complete != NULL) {
829 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530830 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
831 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800832 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530833 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530834 spin_lock(mEp->lock);
835 }
836 }
David Lopoaa69a802008-11-17 14:14:51 -0800837
Pavankumar Kondetief907482011-05-02 11:56:27 +0530838 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530839 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800840
David Lopoaa69a802008-11-17 14:14:51 -0800841 return retval;
842}
843
844/**
845 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800846 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800847 *
848 * This function handles traffic events
849 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800850static void isr_tr_complete_handler(struct ci13xxx *ci)
851__releases(ci->lock)
852__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800853{
854 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530855 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800856
Richard Zhao26c696c2012-07-07 22:56:40 +0800857 for (i = 0; i < ci->hw_ep_max; i++) {
858 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530859 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800860 struct usb_ctrlrequest req;
861
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200862 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800863 continue; /* not configured */
864
Richard Zhao26c696c2012-07-07 22:56:40 +0800865 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800866 err = isr_tr_complete_low(mEp);
867 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
868 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800869 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800870 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800871 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800872 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800873 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700874 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800875 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800876 }
877 }
878 }
879
880 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800881 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800882 continue;
883
884 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800885 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800886 continue;
887 }
888
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530889 /*
890 * Flush data and handshake transactions of previous
891 * setup packet.
892 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800893 _ep_nuke(ci->ep0out);
894 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530895
David Lopoaa69a802008-11-17 14:14:51 -0800896 /* read_setup_packet */
897 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800898 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530899 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800900 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800901
902 type = req.bRequestType;
903
Richard Zhao26c696c2012-07-07 22:56:40 +0800904 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800905
David Lopoaa69a802008-11-17 14:14:51 -0800906 switch (req.bRequest) {
907 case USB_REQ_CLEAR_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)
David Lopoaa69a802008-11-17 14:14:51 -0800912 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530913 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;
918 if (!ci->ci13xxx_ep[num].wedge) {
919 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530920 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800921 &ci->ci13xxx_ep[num].ep);
922 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530923 if (err)
924 break;
925 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800926 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530927 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
928 le16_to_cpu(req.wValue) ==
929 USB_DEVICE_REMOTE_WAKEUP) {
930 if (req.wLength != 0)
931 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800932 ci->remote_wakeup = 0;
933 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530934 } else {
935 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800936 }
David Lopoaa69a802008-11-17 14:14:51 -0800937 break;
938 case USB_REQ_GET_STATUS:
939 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
940 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
941 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
942 goto delegate;
943 if (le16_to_cpu(req.wLength) != 2 ||
944 le16_to_cpu(req.wValue) != 0)
945 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800946 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800947 break;
948 case USB_REQ_SET_ADDRESS:
949 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
950 goto delegate;
951 if (le16_to_cpu(req.wLength) != 0 ||
952 le16_to_cpu(req.wIndex) != 0)
953 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800954 ci->address = (u8)le16_to_cpu(req.wValue);
955 ci->setaddr = true;
956 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800957 break;
958 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530959 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
960 le16_to_cpu(req.wValue) ==
961 USB_ENDPOINT_HALT) {
962 if (req.wLength != 0)
963 break;
964 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530965 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530966 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530967 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800969
Richard Zhao26c696c2012-07-07 22:56:40 +0800970 spin_unlock(&ci->lock);
971 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
972 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530973 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800974 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530975 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530976 if (req.wLength != 0)
977 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530978 switch (le16_to_cpu(req.wValue)) {
979 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800980 ci->remote_wakeup = 1;
981 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530982 break;
983 case USB_DEVICE_TEST_MODE:
984 tmode = le16_to_cpu(req.wIndex) >> 8;
985 switch (tmode) {
986 case TEST_J:
987 case TEST_K:
988 case TEST_SE0_NAK:
989 case TEST_PACKET:
990 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800991 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530992 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800993 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530994 break;
995 default:
996 break;
997 }
998 default:
999 goto delegate;
1000 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301001 } else {
1002 goto delegate;
1003 }
David Lopoaa69a802008-11-17 14:14:51 -08001004 break;
1005 default:
1006delegate:
1007 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001008 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001009
Richard Zhao26c696c2012-07-07 22:56:40 +08001010 spin_unlock(&ci->lock);
1011 err = ci->driver->setup(&ci->gadget, &req);
1012 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001013 break;
1014 }
1015
1016 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001017 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001018 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001019 dev_err(ci->dev, "error: ep_set_halt\n");
1020 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001021 }
1022 }
1023}
1024
1025/******************************************************************************
1026 * ENDPT block
1027 *****************************************************************************/
1028/**
1029 * ep_enable: configure endpoint, making it usable
1030 *
1031 * Check usb_ep_enable() at "usb_gadget.h" for details
1032 */
1033static int ep_enable(struct usb_ep *ep,
1034 const struct usb_endpoint_descriptor *desc)
1035{
1036 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301037 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001038 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001039 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001040
David Lopoaa69a802008-11-17 14:14:51 -08001041 if (ep == NULL || desc == NULL)
1042 return -EINVAL;
1043
1044 spin_lock_irqsave(mEp->lock, flags);
1045
1046 /* only internal SW should enable ctrl endpts */
1047
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001048 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001049
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301050 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001051 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001052
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001053 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1054 mEp->num = usb_endpoint_num(desc);
1055 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001056
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001057 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001058
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301059 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001060 cap |= QH_IOS;
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001061 if (mEp->num)
1062 cap |= QH_ZLT;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001063 cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001064
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001065 mEp->qh.ptr->cap = cpu_to_le32(cap);
1066
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001067 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001068
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301069 /*
1070 * Enable endpoints in the HW other than ep0 as ep0
1071 * is always enabled
1072 */
1073 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001074 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001075
1076 spin_unlock_irqrestore(mEp->lock, flags);
1077 return retval;
1078}
1079
1080/**
1081 * ep_disable: endpoint is no longer usable
1082 *
1083 * Check usb_ep_disable() at "usb_gadget.h" for details
1084 */
1085static int ep_disable(struct usb_ep *ep)
1086{
1087 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1088 int direction, retval = 0;
1089 unsigned long flags;
1090
David Lopoaa69a802008-11-17 14:14:51 -08001091 if (ep == NULL)
1092 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001093 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001094 return -EBUSY;
1095
1096 spin_lock_irqsave(mEp->lock, flags);
1097
1098 /* only internal SW should disable ctrl endpts */
1099
1100 direction = mEp->dir;
1101 do {
David Lopoaa69a802008-11-17 14:14:51 -08001102 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001103 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001104
1105 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1106 mEp->dir = (mEp->dir == TX) ? RX : TX;
1107
1108 } while (mEp->dir != direction);
1109
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001110 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001111
1112 spin_unlock_irqrestore(mEp->lock, flags);
1113 return retval;
1114}
1115
1116/**
1117 * ep_alloc_request: allocate a request object to use with this endpoint
1118 *
1119 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1120 */
1121static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1122{
1123 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1124 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001125
Alexander Shishkin0f089092012-05-08 23:29:02 +03001126 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001127 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001128
David Lopoaa69a802008-11-17 14:14:51 -08001129 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1130 if (mReq != NULL) {
1131 INIT_LIST_HEAD(&mReq->queue);
1132
1133 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1134 &mReq->dma);
1135 if (mReq->ptr == NULL) {
1136 kfree(mReq);
1137 mReq = NULL;
1138 }
1139 }
1140
David Lopoaa69a802008-11-17 14:14:51 -08001141 return (mReq == NULL) ? NULL : &mReq->req;
1142}
1143
1144/**
1145 * ep_free_request: frees a request object
1146 *
1147 * Check usb_ep_free_request() at "usb_gadget.h" for details
1148 */
1149static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1150{
1151 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1152 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1153 unsigned long flags;
1154
David Lopoaa69a802008-11-17 14:14:51 -08001155 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001156 return;
1157 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001158 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001159 return;
1160 }
1161
1162 spin_lock_irqsave(mEp->lock, flags);
1163
1164 if (mReq->ptr)
1165 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1166 kfree(mReq);
1167
David Lopoaa69a802008-11-17 14:14:51 -08001168 spin_unlock_irqrestore(mEp->lock, flags);
1169}
1170
1171/**
1172 * ep_queue: queues (submits) an I/O request to an endpoint
1173 *
1174 * Check usb_ep_queue()* at usb_gadget.h" for details
1175 */
1176static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1177 gfp_t __maybe_unused gfp_flags)
1178{
1179 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001180 int retval = 0;
1181 unsigned long flags;
1182
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001183 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001184 return -EINVAL;
1185
1186 spin_lock_irqsave(mEp->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001187 retval = _ep_queue(ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -08001188 spin_unlock_irqrestore(mEp->lock, flags);
1189 return retval;
1190}
1191
1192/**
1193 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1194 *
1195 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1196 */
1197static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1198{
1199 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1200 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1201 unsigned long flags;
1202
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301203 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001204 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301205 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001206 return -EINVAL;
1207
1208 spin_lock_irqsave(mEp->lock, flags);
1209
Richard Zhao26c696c2012-07-07 22:56:40 +08001210 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001211
1212 /* pop request */
1213 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001214
Richard Zhao26c696c2012-07-07 22:56:40 +08001215 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001216
David Lopoaa69a802008-11-17 14:14:51 -08001217 req->status = -ECONNRESET;
1218
Artem Leonenko7c25a822010-12-14 23:46:55 -08001219 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001220 spin_unlock(mEp->lock);
1221 mReq->req.complete(&mEp->ep, &mReq->req);
1222 spin_lock(mEp->lock);
1223 }
1224
1225 spin_unlock_irqrestore(mEp->lock, flags);
1226 return 0;
1227}
1228
1229/**
1230 * ep_set_halt: sets the endpoint halt feature
1231 *
1232 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1233 */
1234static int ep_set_halt(struct usb_ep *ep, int value)
1235{
1236 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1237 int direction, retval = 0;
1238 unsigned long flags;
1239
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001240 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001241 return -EINVAL;
1242
1243 spin_lock_irqsave(mEp->lock, flags);
1244
1245#ifndef STALL_IN
1246 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1247 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301248 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001249 spin_unlock_irqrestore(mEp->lock, flags);
1250 return -EAGAIN;
1251 }
1252#endif
1253
1254 direction = mEp->dir;
1255 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001256 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001257
1258 if (!value)
1259 mEp->wedge = 0;
1260
1261 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1262 mEp->dir = (mEp->dir == TX) ? RX : TX;
1263
1264 } while (mEp->dir != direction);
1265
1266 spin_unlock_irqrestore(mEp->lock, flags);
1267 return retval;
1268}
1269
1270/**
1271 * ep_set_wedge: sets the halt feature and ignores clear requests
1272 *
1273 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1274 */
1275static int ep_set_wedge(struct usb_ep *ep)
1276{
1277 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1278 unsigned long flags;
1279
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001280 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001281 return -EINVAL;
1282
1283 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001284 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001285 spin_unlock_irqrestore(mEp->lock, flags);
1286
1287 return usb_ep_set_halt(ep);
1288}
1289
1290/**
1291 * ep_fifo_flush: flushes contents of a fifo
1292 *
1293 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1294 */
1295static void ep_fifo_flush(struct usb_ep *ep)
1296{
1297 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1298 unsigned long flags;
1299
David Lopoaa69a802008-11-17 14:14:51 -08001300 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001301 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001302 return;
1303 }
1304
1305 spin_lock_irqsave(mEp->lock, flags);
1306
Richard Zhao26c696c2012-07-07 22:56:40 +08001307 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001308
1309 spin_unlock_irqrestore(mEp->lock, flags);
1310}
1311
1312/**
1313 * Endpoint-specific part of the API to the USB controller hardware
1314 * Check "usb_gadget.h" for details
1315 */
1316static const struct usb_ep_ops usb_ep_ops = {
1317 .enable = ep_enable,
1318 .disable = ep_disable,
1319 .alloc_request = ep_alloc_request,
1320 .free_request = ep_free_request,
1321 .queue = ep_queue,
1322 .dequeue = ep_dequeue,
1323 .set_halt = ep_set_halt,
1324 .set_wedge = ep_set_wedge,
1325 .fifo_flush = ep_fifo_flush,
1326};
1327
1328/******************************************************************************
1329 * GADGET block
1330 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301331static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1332{
Richard Zhao26c696c2012-07-07 22:56:40 +08001333 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301334 unsigned long flags;
1335 int gadget_ready = 0;
1336
Richard Zhao26c696c2012-07-07 22:56:40 +08001337 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301338 return -EOPNOTSUPP;
1339
Richard Zhao26c696c2012-07-07 22:56:40 +08001340 spin_lock_irqsave(&ci->lock, flags);
1341 ci->vbus_active = is_active;
1342 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301343 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001344 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301345
1346 if (gadget_ready) {
1347 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301348 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001349 hw_device_reset(ci, USBMODE_CM_DC);
1350 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301351 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001352 hw_device_state(ci, 0);
1353 if (ci->platdata->notify_event)
1354 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301355 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001356 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301357 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301358 }
1359 }
1360
1361 return 0;
1362}
1363
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301364static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1365{
Richard Zhao26c696c2012-07-07 22:56:40 +08001366 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301367 unsigned long flags;
1368 int ret = 0;
1369
Richard Zhao26c696c2012-07-07 22:56:40 +08001370 spin_lock_irqsave(&ci->lock, flags);
1371 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301372 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301373 goto out;
1374 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001375 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301376 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301377 goto out;
1378 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001379 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301380out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001381 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301382 return ret;
1383}
1384
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301385static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1386{
Richard Zhao26c696c2012-07-07 22:56:40 +08001387 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301388
Richard Zhao26c696c2012-07-07 22:56:40 +08001389 if (ci->transceiver)
1390 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301391 return -ENOTSUPP;
1392}
1393
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001394/* Change Data+ pullup status
1395 * this func is used by usb_gadget_connect/disconnet
1396 */
1397static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1398{
1399 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1400
1401 if (is_on)
1402 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1403 else
1404 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1405
1406 return 0;
1407}
1408
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001409static int ci13xxx_start(struct usb_gadget *gadget,
1410 struct usb_gadget_driver *driver);
1411static int ci13xxx_stop(struct usb_gadget *gadget,
1412 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001413/**
1414 * Device operations part of the API to the USB controller hardware,
1415 * which don't involve endpoints (or i/o)
1416 * Check "usb_gadget.h" for details
1417 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301418static const struct usb_gadget_ops usb_gadget_ops = {
1419 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301420 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001421 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301422 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001423 .udc_start = ci13xxx_start,
1424 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301425};
David Lopoaa69a802008-11-17 14:14:51 -08001426
Richard Zhao26c696c2012-07-07 22:56:40 +08001427static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001428{
1429 int retval = 0, i, j;
1430
Richard Zhao26c696c2012-07-07 22:56:40 +08001431 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001432 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001433 int k = i + j * ci->hw_ep_max/2;
1434 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001435
1436 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1437 (j == TX) ? "in" : "out");
1438
Richard Zhao26c696c2012-07-07 22:56:40 +08001439 mEp->ci = ci;
1440 mEp->lock = &ci->lock;
1441 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001442
1443 mEp->ep.name = mEp->name;
1444 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001445 /*
1446 * for ep0: maxP defined in desc, for other
1447 * eps, maxP is set by epautoconfig() called
1448 * by gadget layer
1449 */
1450 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001451
1452 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001453 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001454 &mEp->qh.dma);
1455 if (mEp->qh.ptr == NULL)
1456 retval = -ENOMEM;
1457 else
1458 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1459
1460 /*
1461 * set up shorthands for ep0 out and in endpoints,
1462 * don't add to gadget's ep_list
1463 */
1464 if (i == 0) {
1465 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001466 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001467 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001468 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001469
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001470 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001471 continue;
1472 }
1473
Richard Zhao26c696c2012-07-07 22:56:40 +08001474 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001475 }
1476
1477 return retval;
1478}
1479
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001480static void destroy_eps(struct ci13xxx *ci)
1481{
1482 int i;
1483
1484 for (i = 0; i < ci->hw_ep_max; i++) {
1485 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1486
1487 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1488 }
1489}
1490
David Lopoaa69a802008-11-17 14:14:51 -08001491/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001492 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001493 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001494 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001495 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001496 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001497 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001498static int ci13xxx_start(struct usb_gadget *gadget,
1499 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001500{
Richard Zhao26c696c2012-07-07 22:56:40 +08001501 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301502 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001503 int retval = -ENOMEM;
1504
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001505 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001506 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001507
David Lopoaa69a802008-11-17 14:14:51 -08001508
Richard Zhao26c696c2012-07-07 22:56:40 +08001509 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1510 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301511 if (retval)
1512 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001513
Richard Zhao26c696c2012-07-07 22:56:40 +08001514 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1515 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301516 if (retval)
1517 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001518 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001519
Richard Zhao26c696c2012-07-07 22:56:40 +08001520 ci->driver = driver;
1521 pm_runtime_get_sync(&ci->gadget.dev);
1522 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1523 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001524 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001525 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301526 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001527 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301528 goto done;
1529 }
1530 }
1531
Richard Zhao26c696c2012-07-07 22:56:40 +08001532 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301533 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001534 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001535
1536 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001537 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001538 return retval;
1539}
David Lopoaa69a802008-11-17 14:14:51 -08001540
1541/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001542 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001543 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001544static int ci13xxx_stop(struct usb_gadget *gadget,
1545 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001546{
Richard Zhao26c696c2012-07-07 22:56:40 +08001547 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001548 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001549
Richard Zhao26c696c2012-07-07 22:56:40 +08001550 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001551
Richard Zhao26c696c2012-07-07 22:56:40 +08001552 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1553 ci->vbus_active) {
1554 hw_device_state(ci, 0);
1555 if (ci->platdata->notify_event)
1556 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301557 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001558 ci->driver = NULL;
1559 spin_unlock_irqrestore(&ci->lock, flags);
1560 _gadget_stop_activity(&ci->gadget);
1561 spin_lock_irqsave(&ci->lock, flags);
1562 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301563 }
David Lopoaa69a802008-11-17 14:14:51 -08001564
Richard Zhao26c696c2012-07-07 22:56:40 +08001565 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001566
David Lopoaa69a802008-11-17 14:14:51 -08001567 return 0;
1568}
David Lopoaa69a802008-11-17 14:14:51 -08001569
1570/******************************************************************************
1571 * BUS block
1572 *****************************************************************************/
1573/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001574 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001575 *
1576 * This function returns IRQ_HANDLED if the IRQ has been handled
1577 * It locks access to registers
1578 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001579static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001580{
David Lopoaa69a802008-11-17 14:14:51 -08001581 irqreturn_t retval;
1582 u32 intr;
1583
Richard Zhao26c696c2012-07-07 22:56:40 +08001584 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001585 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001586
Richard Zhao26c696c2012-07-07 22:56:40 +08001587 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301588
Richard Zhao26c696c2012-07-07 22:56:40 +08001589 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1590 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001591 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301593 return IRQ_NONE;
1594 }
1595 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001596 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001597
Alexander Shishkine443b332012-05-11 17:25:46 +03001598 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001599 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001600 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001601 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001602
David Lopoaa69a802008-11-17 14:14:51 -08001603 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001604 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001605 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001606 if (ci->suspended && ci->driver->resume) {
1607 spin_unlock(&ci->lock);
1608 ci->driver->resume(&ci->gadget);
1609 spin_lock(&ci->lock);
1610 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301611 }
David Lopoaa69a802008-11-17 14:14:51 -08001612 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001613
1614 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001615 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001616
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301617 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001618 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1619 ci->driver->suspend) {
1620 ci->suspended = 1;
1621 spin_unlock(&ci->lock);
1622 ci->driver->suspend(&ci->gadget);
1623 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301624 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301625 }
David Lopoaa69a802008-11-17 14:14:51 -08001626 retval = IRQ_HANDLED;
1627 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001628 retval = IRQ_NONE;
1629 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001630 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001631
1632 return retval;
1633}
1634
1635/**
1636 * udc_release: driver release function
1637 * @dev: device
1638 *
1639 * Currently does nothing
1640 */
1641static void udc_release(struct device *dev)
1642{
David Lopoaa69a802008-11-17 14:14:51 -08001643}
1644
1645/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001646 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001647 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001648 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001649static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001650{
Richard Zhao26c696c2012-07-07 22:56:40 +08001651 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001652 int retval = 0;
1653
Richard Zhao26c696c2012-07-07 22:56:40 +08001654 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001655
Richard Zhao26c696c2012-07-07 22:56:40 +08001656 ci->gadget.ops = &usb_gadget_ops;
1657 ci->gadget.speed = USB_SPEED_UNKNOWN;
1658 ci->gadget.max_speed = USB_SPEED_HIGH;
1659 ci->gadget.is_otg = 0;
1660 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001661
Richard Zhao26c696c2012-07-07 22:56:40 +08001662 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001663
Richard Zhao26c696c2012-07-07 22:56:40 +08001664 dev_set_name(&ci->gadget.dev, "gadget");
1665 ci->gadget.dev.dma_mask = dev->dma_mask;
1666 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1667 ci->gadget.dev.parent = dev;
1668 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001669
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001670 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001671 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001672 sizeof(struct ci13xxx_qh),
1673 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001674 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001675 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001676
Richard Zhao26c696c2012-07-07 22:56:40 +08001677 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001678 sizeof(struct ci13xxx_td),
1679 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001680 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001681 retval = -ENOMEM;
1682 goto free_qh_pool;
1683 }
1684
Richard Zhao26c696c2012-07-07 22:56:40 +08001685 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001686 if (retval)
1687 goto free_pools;
1688
Richard Zhao26c696c2012-07-07 22:56:40 +08001689 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301690
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001691 if (ci->global_phy)
1692 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301693
Richard Zhao26c696c2012-07-07 22:56:40 +08001694 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1695 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301696 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001697 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301698 }
1699 }
1700
Richard Zhao26c696c2012-07-07 22:56:40 +08001701 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1702 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301703 if (retval)
1704 goto put_transceiver;
1705 }
1706
Richard Zhao26c696c2012-07-07 22:56:40 +08001707 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301708 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001709 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301710 goto put_transceiver;
1711 }
David Lopoaa69a802008-11-17 14:14:51 -08001712
Richard Zhao26c696c2012-07-07 22:56:40 +08001713 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1714 retval = otg_set_peripheral(ci->transceiver->otg,
1715 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301716 if (retval)
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001717 goto unreg_device;
David Lopoaa69a802008-11-17 14:14:51 -08001718 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001719
Richard Zhao26c696c2012-07-07 22:56:40 +08001720 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001721 if (retval)
1722 goto remove_trans;
1723
Richard Zhao26c696c2012-07-07 22:56:40 +08001724 pm_runtime_no_callbacks(&ci->gadget.dev);
1725 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001726
David Lopoaa69a802008-11-17 14:14:51 -08001727 return retval;
1728
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001729remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001730 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001731 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001732 if (ci->global_phy)
1733 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001734 }
1735
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001736 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301737unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001738 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301739put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001740 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001741 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001742destroy_eps:
1743 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001744free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001745 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001746free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001747 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001748 return retval;
1749}
1750
1751/**
1752 * udc_remove: parent remove must call this to remove UDC
1753 *
1754 * No interrupts active, the IRQ has been released
1755 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001756static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001757{
Richard Zhao26c696c2012-07-07 22:56:40 +08001758 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001759 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001760
Richard Zhao26c696c2012-07-07 22:56:40 +08001761 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001762
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001763 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001764
Richard Zhao26c696c2012-07-07 22:56:40 +08001765 dma_pool_destroy(ci->td_pool);
1766 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001767
Richard Zhao26c696c2012-07-07 22:56:40 +08001768 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1769 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001770 if (ci->global_phy)
1771 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301772 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001773 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001774 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001775 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001776}
David Lopoaa69a802008-11-17 14:14:51 -08001777
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001778/**
1779 * ci_hdrc_gadget_init - initialize device related bits
1780 * ci: the controller
1781 *
1782 * This function enables the gadget role, if the device is "device capable".
1783 */
1784int ci_hdrc_gadget_init(struct ci13xxx *ci)
1785{
1786 struct ci_role_driver *rdrv;
1787
1788 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1789 return -ENXIO;
1790
1791 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1792 if (!rdrv)
1793 return -ENOMEM;
1794
1795 rdrv->start = udc_start;
1796 rdrv->stop = udc_stop;
1797 rdrv->irq = udc_irq;
1798 rdrv->name = "gadget";
1799 ci->roles[CI_ROLE_GADGET] = rdrv;
1800
1801 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001802}