blob: 960814f4179d941e07a23b9d5b005801bcab0cee [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 *****************************************************************************/
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300371
372static void setup_td_bits(struct td_node *tdnode, unsigned length)
373{
374 memset(tdnode->ptr, 0, sizeof(*tdnode->ptr));
375 tdnode->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
376 tdnode->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
377 tdnode->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
378}
379
380static int add_td_to_list(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq,
381 unsigned length)
382{
383 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
384 GFP_ATOMIC);
385
386 if (node == NULL)
387 return -ENOMEM;
388
389 node->ptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
390 &node->dma);
391 if (node->ptr == NULL) {
392 kfree(node);
393 return -ENOMEM;
394 }
395
396 setup_td_bits(node, length);
397
398 if (!list_empty(&mReq->tds)) {
399 /* get the last entry */
400 lastnode = list_entry(mReq->tds.prev,
401 struct td_node, td);
402 lastnode->ptr->next = cpu_to_le32(node->dma);
403 }
404
405 INIT_LIST_HEAD(&node->td);
406 list_add_tail(&node->td, &mReq->tds);
407
408 return 0;
409}
410
David Lopoaa69a802008-11-17 14:14:51 -0800411/**
412 * _usb_addr: calculates endpoint address from direction & number
413 * @ep: endpoint
414 */
415static inline u8 _usb_addr(struct ci13xxx_ep *ep)
416{
417 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
418}
419
420/**
421 * _hardware_queue: configures a request at hardware level
422 * @gadget: gadget
423 * @mEp: endpoint
424 *
425 * This function returns an error code
426 */
427static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
428{
Richard Zhao26c696c2012-07-07 22:56:40 +0800429 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -0800430 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530431 int ret = 0;
432 unsigned length = mReq->req.length;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300433 struct td_node *firstnode, *lastnode;
David Lopoaa69a802008-11-17 14:14:51 -0800434
David Lopoaa69a802008-11-17 14:14:51 -0800435 /* don't queue twice */
436 if (mReq->req.status == -EALREADY)
437 return -EALREADY;
438
David Lopoaa69a802008-11-17 14:14:51 -0800439 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800440
Richard Zhao26c696c2012-07-07 22:56:40 +0800441 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300442 if (ret)
443 return ret;
444
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300445 firstnode = list_first_entry(&mReq->tds,
446 struct td_node, td);
447
448 setup_td_bits(firstnode, length);
449
450 firstnode->ptr->page[0] = cpu_to_le32(mReq->req.dma);
Michael Grzeschikb983e512013-03-30 12:54:10 +0200451 for (i = 1; i < TD_PAGE_COUNT; i++) {
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200452 u32 page = mReq->req.dma + i * CI13XXX_PAGE_SIZE;
453 page &= ~TD_RESERVED_MASK;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300454 firstnode->ptr->page[i] = cpu_to_le32(page);
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200455 }
David Lopoaa69a802008-11-17 14:14:51 -0800456
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300457 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0))
458 add_td_to_list(mEp, mReq, 0);
459
460 lastnode = list_entry(mReq->tds.prev,
461 struct td_node, td);
462
463 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
464 if (!mReq->req.no_interrupt)
465 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
Michael Grzeschika9c17432013-04-04 13:13:46 +0300466 wmb();
467
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530468 if (!list_empty(&mEp->qh.queue)) {
469 struct ci13xxx_req *mReqPrev;
470 int n = hw_ep_bit(mEp->num, mEp->dir);
471 int tmp_stat;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300472 struct td_node *prevlastnode;
473 u32 next = firstnode->dma & TD_ADDR_MASK;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530474
475 mReqPrev = list_entry(mEp->qh.queue.prev,
476 struct ci13xxx_req, queue);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300477 prevlastnode = list_entry(mReqPrev->tds.prev,
478 struct td_node, td);
479
480 prevlastnode->ptr->next = cpu_to_le32(next);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530481 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800482 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530483 goto done;
484 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800485 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
486 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
487 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
488 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530489 if (tmp_stat)
490 goto done;
491 }
492
493 /* QH configuration */
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300494 mEp->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
Michael Grzeschik080ff5f2013-03-30 12:54:04 +0200495 mEp->qh.ptr->td.token &=
496 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
David Lopoaa69a802008-11-17 14:14:51 -0800497
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300498 if (mEp->type == USB_ENDPOINT_XFER_ISOC) {
499 u32 mul = mReq->req.length / mEp->ep.maxpacket;
500
501 if (mReq->req.length % mEp->ep.maxpacket)
502 mul++;
503 mEp->qh.ptr->cap |= mul << __ffs(QH_MULT);
504 }
505
David Lopoaa69a802008-11-17 14:14:51 -0800506 wmb(); /* synchronize before ep prime */
507
Richard Zhao26c696c2012-07-07 22:56:40 +0800508 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800509 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530510done:
511 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800512}
513
514/**
515 * _hardware_dequeue: handles a request at hardware level
516 * @gadget: gadget
517 * @mEp: endpoint
518 *
519 * This function returns an error code
520 */
521static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
522{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300523 u32 tmptoken;
524 struct td_node *node, *tmpnode, *firstnode;
Michael Grzeschik9e506432013-03-30 12:54:07 +0200525
David Lopoaa69a802008-11-17 14:14:51 -0800526 if (mReq->req.status != -EALREADY)
527 return -EINVAL;
528
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300529 firstnode = list_first_entry(&mReq->tds,
530 struct td_node, td);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530531
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300532 list_for_each_entry_safe(node, tmpnode, &mReq->tds, td) {
533 tmptoken = le32_to_cpu(node->ptr->token);
534 if ((TD_STATUS_ACTIVE & tmptoken) != 0)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530535 return -EBUSY;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300536 if (node != firstnode) {
537 dma_pool_free(mEp->td_pool, node->ptr, node->dma);
538 list_del_init(&node->td);
539 node->ptr = NULL;
540 kfree(node);
541 }
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530542 }
David Lopoaa69a802008-11-17 14:14:51 -0800543
544 mReq->req.status = 0;
545
Richard Zhao26c696c2012-07-07 22:56:40 +0800546 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800547
Michael Grzeschik9e506432013-03-30 12:54:07 +0200548 mReq->req.status = tmptoken & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530549 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800550 mReq->req.status = -1;
551 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
552 mReq->req.status = -1;
553 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
554 mReq->req.status = -1;
555
Michael Grzeschik9e506432013-03-30 12:54:07 +0200556 mReq->req.actual = tmptoken & TD_TOTAL_BYTES;
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200557 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800558 mReq->req.actual = mReq->req.length - mReq->req.actual;
559 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
560
561 return mReq->req.actual;
562}
563
564/**
565 * _ep_nuke: dequeues all endpoint requests
566 * @mEp: endpoint
567 *
568 * This function returns an error code
569 * Caller must hold lock
570 */
571static int _ep_nuke(struct ci13xxx_ep *mEp)
572__releases(mEp->lock)
573__acquires(mEp->lock)
574{
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300575 struct td_node *node, *tmpnode, *firstnode;
David Lopoaa69a802008-11-17 14:14:51 -0800576 if (mEp == NULL)
577 return -EINVAL;
578
Richard Zhao26c696c2012-07-07 22:56:40 +0800579 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800580
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530581 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800582
583 /* pop oldest request */
584 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530585 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800586 struct ci13xxx_req, queue);
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300587
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300588 firstnode = list_first_entry(&mReq->tds,
589 struct td_node, td);
590
591 list_for_each_entry_safe(node, tmpnode, &mReq->tds, td) {
592 if (node != firstnode) {
593 dma_pool_free(mEp->td_pool, node->ptr,
594 node->dma);
595 list_del_init(&node->td);
596 node->ptr = NULL;
597 kfree(node);
598 }
Michael Grzeschik7ca2cd22013-04-04 13:13:47 +0300599 }
600
David Lopoaa69a802008-11-17 14:14:51 -0800601 list_del_init(&mReq->queue);
602 mReq->req.status = -ESHUTDOWN;
603
Artem Leonenko7c25a822010-12-14 23:46:55 -0800604 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800605 spin_unlock(mEp->lock);
606 mReq->req.complete(&mEp->ep, &mReq->req);
607 spin_lock(mEp->lock);
608 }
609 }
610 return 0;
611}
612
613/**
614 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
615 * @gadget: gadget
616 *
617 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800618 */
619static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800620{
621 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800622 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530623 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800624
Richard Zhao26c696c2012-07-07 22:56:40 +0800625 spin_lock_irqsave(&ci->lock, flags);
626 ci->gadget.speed = USB_SPEED_UNKNOWN;
627 ci->remote_wakeup = 0;
628 ci->suspended = 0;
629 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530630
David Lopoaa69a802008-11-17 14:14:51 -0800631 /* flush all endpoints */
632 gadget_for_each_ep(ep, gadget) {
633 usb_ep_fifo_flush(ep);
634 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800635 usb_ep_fifo_flush(&ci->ep0out->ep);
636 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800637
Richard Zhao26c696c2012-07-07 22:56:40 +0800638 if (ci->driver)
639 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800640
641 /* make sure to disable all endpoints */
642 gadget_for_each_ep(ep, gadget) {
643 usb_ep_disable(ep);
644 }
David Lopoaa69a802008-11-17 14:14:51 -0800645
Richard Zhao26c696c2012-07-07 22:56:40 +0800646 if (ci->status != NULL) {
647 usb_ep_free_request(&ci->ep0in->ep, ci->status);
648 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800649 }
650
David Lopoaa69a802008-11-17 14:14:51 -0800651 return 0;
652}
653
654/******************************************************************************
655 * ISR block
656 *****************************************************************************/
657/**
658 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800659 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800660 *
661 * This function resets USB engine after a bus reset occurred
662 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800663static void isr_reset_handler(struct ci13xxx *ci)
664__releases(ci->lock)
665__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800666{
David Lopoaa69a802008-11-17 14:14:51 -0800667 int retval;
668
Richard Zhao26c696c2012-07-07 22:56:40 +0800669 spin_unlock(&ci->lock);
670 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800671 if (retval)
672 goto done;
673
Richard Zhao26c696c2012-07-07 22:56:40 +0800674 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800675 if (retval)
676 goto done;
677
Richard Zhao26c696c2012-07-07 22:56:40 +0800678 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
679 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530680 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530681
Michael Grzeschikb9322252012-05-11 17:25:50 +0300682done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800683 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800684
David Lopoaa69a802008-11-17 14:14:51 -0800685 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800686 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800687}
688
689/**
690 * isr_get_status_complete: get_status request complete function
691 * @ep: endpoint
692 * @req: request handled
693 *
694 * Caller must release lock
695 */
696static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
697{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300698 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800699 return;
David Lopoaa69a802008-11-17 14:14:51 -0800700
701 kfree(req->buf);
702 usb_ep_free_request(ep, req);
703}
704
705/**
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200706 * _ep_queue: queues (submits) an I/O request to an endpoint
707 *
708 * Caller must hold lock
709 */
710static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
711 gfp_t __maybe_unused gfp_flags)
712{
713 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
714 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
715 struct ci13xxx *ci = mEp->ci;
716 int retval = 0;
717
718 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
719 return -EINVAL;
720
721 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
722 if (req->length)
723 mEp = (ci->ep0_dir == RX) ?
724 ci->ep0out : ci->ep0in;
725 if (!list_empty(&mEp->qh.queue)) {
726 _ep_nuke(mEp);
727 retval = -EOVERFLOW;
728 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
729 _usb_addr(mEp));
730 }
731 }
732
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +0300733 if (usb_endpoint_xfer_isoc(mEp->ep.desc) &&
734 mReq->req.length > (1 + mEp->ep.mult) * mEp->ep.maxpacket) {
735 dev_err(mEp->ci->dev, "request length too big for isochronous\n");
736 return -EMSGSIZE;
737 }
738
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200739 /* first nuke then test link, e.g. previous status has not sent */
740 if (!list_empty(&mReq->queue)) {
741 dev_err(mEp->ci->dev, "request already in queue\n");
742 return -EBUSY;
743 }
744
Michael Grzeschikb983e512013-03-30 12:54:10 +0200745 if (req->length > (TD_PAGE_COUNT - 1) * CI13XXX_PAGE_SIZE) {
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200746 dev_err(mEp->ci->dev, "request bigger than one td\n");
747 return -EMSGSIZE;
748 }
749
750 /* push request */
751 mReq->req.status = -EINPROGRESS;
752 mReq->req.actual = 0;
753
754 retval = _hardware_enqueue(mEp, mReq);
755
756 if (retval == -EALREADY)
757 retval = 0;
758 if (!retval)
759 list_add_tail(&mReq->queue, &mEp->qh.queue);
760
761 return retval;
762}
763
764/**
David Lopoaa69a802008-11-17 14:14:51 -0800765 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800766 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800767 * @setup: setup request packet
768 *
769 * This function returns an error code
770 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800771static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800772 struct usb_ctrlrequest *setup)
773__releases(mEp->lock)
774__acquires(mEp->lock)
775{
Richard Zhao26c696c2012-07-07 22:56:40 +0800776 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800777 struct usb_request *req = NULL;
778 gfp_t gfp_flags = GFP_ATOMIC;
779 int dir, num, retval;
780
David Lopoaa69a802008-11-17 14:14:51 -0800781 if (mEp == NULL || setup == NULL)
782 return -EINVAL;
783
784 spin_unlock(mEp->lock);
785 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
786 spin_lock(mEp->lock);
787 if (req == NULL)
788 return -ENOMEM;
789
790 req->complete = isr_get_status_complete;
791 req->length = 2;
792 req->buf = kzalloc(req->length, gfp_flags);
793 if (req->buf == NULL) {
794 retval = -ENOMEM;
795 goto err_free_req;
796 }
797
798 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530799 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800800 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800801 retval = 0;
802 } else if ((setup->bRequestType & USB_RECIP_MASK) \
803 == USB_RECIP_ENDPOINT) {
804 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
805 TX : RX;
806 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800807 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800808 }
809 /* else do nothing; reserved for future use */
810
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200811 retval = _ep_queue(&mEp->ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -0800812 if (retval)
813 goto err_free_buf;
814
815 return 0;
816
817 err_free_buf:
818 kfree(req->buf);
819 err_free_req:
820 spin_unlock(mEp->lock);
821 usb_ep_free_request(&mEp->ep, req);
822 spin_lock(mEp->lock);
823 return retval;
824}
825
826/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530827 * isr_setup_status_complete: setup_status request complete function
828 * @ep: endpoint
829 * @req: request handled
830 *
831 * Caller must release lock. Put the port in test mode if test mode
832 * feature is selected.
833 */
834static void
835isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
836{
Richard Zhao26c696c2012-07-07 22:56:40 +0800837 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530838 unsigned long flags;
839
Richard Zhao26c696c2012-07-07 22:56:40 +0800840 if (ci->setaddr) {
841 hw_usb_set_address(ci, ci->address);
842 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300843 }
844
Richard Zhao26c696c2012-07-07 22:56:40 +0800845 spin_lock_irqsave(&ci->lock, flags);
846 if (ci->test_mode)
847 hw_port_test_set(ci, ci->test_mode);
848 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530849}
850
851/**
David Lopoaa69a802008-11-17 14:14:51 -0800852 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800853 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800854 *
855 * This function returns an error code
856 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800857static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800858{
859 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530860 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800861
Richard Zhao26c696c2012-07-07 22:56:40 +0800862 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
863 ci->status->context = ci;
864 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800865
Michael Grzeschikdd064e92013-03-30 12:54:09 +0200866 retval = _ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800867
868 return retval;
869}
870
871/**
872 * isr_tr_complete_low: transaction complete low level handler
873 * @mEp: endpoint
874 *
875 * This function returns an error code
876 * Caller must hold lock
877 */
878static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
879__releases(mEp->lock)
880__acquires(mEp->lock)
881{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530882 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530883 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300884 int retval = 0;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300885 struct td_node *firstnode;
David Lopoaa69a802008-11-17 14:14:51 -0800886
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530887 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
888 queue) {
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +0300889 firstnode = list_first_entry(&mReq->tds,
890 struct td_node, td);
891
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530892 retval = _hardware_dequeue(mEp, mReq);
893 if (retval < 0)
894 break;
895 list_del_init(&mReq->queue);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530896 if (mReq->req.complete != NULL) {
897 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530898 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
899 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800900 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530901 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530902 spin_lock(mEp->lock);
903 }
904 }
David Lopoaa69a802008-11-17 14:14:51 -0800905
Pavankumar Kondetief907482011-05-02 11:56:27 +0530906 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530907 retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800908
David Lopoaa69a802008-11-17 14:14:51 -0800909 return retval;
910}
911
912/**
913 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800914 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800915 *
916 * This function handles traffic events
917 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800918static void isr_tr_complete_handler(struct ci13xxx *ci)
919__releases(ci->lock)
920__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800921{
922 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530923 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800924
Richard Zhao26c696c2012-07-07 22:56:40 +0800925 for (i = 0; i < ci->hw_ep_max; i++) {
926 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530927 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800928 struct usb_ctrlrequest req;
929
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200930 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800931 continue; /* not configured */
932
Richard Zhao26c696c2012-07-07 22:56:40 +0800933 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800934 err = isr_tr_complete_low(mEp);
935 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
936 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800937 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800938 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800939 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800940 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800941 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700942 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800943 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800944 }
945 }
946 }
947
948 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800949 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800950 continue;
951
952 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800953 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800954 continue;
955 }
956
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530957 /*
958 * Flush data and handshake transactions of previous
959 * setup packet.
960 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800961 _ep_nuke(ci->ep0out);
962 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530963
David Lopoaa69a802008-11-17 14:14:51 -0800964 /* read_setup_packet */
965 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800966 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530967 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800969
970 type = req.bRequestType;
971
Richard Zhao26c696c2012-07-07 22:56:40 +0800972 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800973
David Lopoaa69a802008-11-17 14:14:51 -0800974 switch (req.bRequest) {
975 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530976 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
977 le16_to_cpu(req.wValue) ==
978 USB_ENDPOINT_HALT) {
979 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800980 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530981 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530982 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530983 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530984 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800985 num += ci->hw_ep_max/2;
986 if (!ci->ci13xxx_ep[num].wedge) {
987 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530988 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800989 &ci->ci13xxx_ep[num].ep);
990 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530991 if (err)
992 break;
993 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800994 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530995 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
996 le16_to_cpu(req.wValue) ==
997 USB_DEVICE_REMOTE_WAKEUP) {
998 if (req.wLength != 0)
999 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001000 ci->remote_wakeup = 0;
1001 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301002 } else {
1003 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -08001004 }
David Lopoaa69a802008-11-17 14:14:51 -08001005 break;
1006 case USB_REQ_GET_STATUS:
1007 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1008 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1009 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1010 goto delegate;
1011 if (le16_to_cpu(req.wLength) != 2 ||
1012 le16_to_cpu(req.wValue) != 0)
1013 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001014 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -08001015 break;
1016 case USB_REQ_SET_ADDRESS:
1017 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1018 goto delegate;
1019 if (le16_to_cpu(req.wLength) != 0 ||
1020 le16_to_cpu(req.wIndex) != 0)
1021 break;
Richard Zhao26c696c2012-07-07 22:56:40 +08001022 ci->address = (u8)le16_to_cpu(req.wValue);
1023 ci->setaddr = true;
1024 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001025 break;
1026 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301027 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1028 le16_to_cpu(req.wValue) ==
1029 USB_ENDPOINT_HALT) {
1030 if (req.wLength != 0)
1031 break;
1032 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301033 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301034 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +05301035 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +08001036 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -08001037
Richard Zhao26c696c2012-07-07 22:56:40 +08001038 spin_unlock(&ci->lock);
1039 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
1040 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301041 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +08001042 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301043 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301044 if (req.wLength != 0)
1045 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301046 switch (le16_to_cpu(req.wValue)) {
1047 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +08001048 ci->remote_wakeup = 1;
1049 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301050 break;
1051 case USB_DEVICE_TEST_MODE:
1052 tmode = le16_to_cpu(req.wIndex) >> 8;
1053 switch (tmode) {
1054 case TEST_J:
1055 case TEST_K:
1056 case TEST_SE0_NAK:
1057 case TEST_PACKET:
1058 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +08001059 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301060 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +08001061 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +05301062 break;
1063 default:
1064 break;
1065 }
1066 default:
1067 goto delegate;
1068 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301069 } else {
1070 goto delegate;
1071 }
David Lopoaa69a802008-11-17 14:14:51 -08001072 break;
1073 default:
1074delegate:
1075 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +08001076 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -08001077
Richard Zhao26c696c2012-07-07 22:56:40 +08001078 spin_unlock(&ci->lock);
1079 err = ci->driver->setup(&ci->gadget, &req);
1080 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001081 break;
1082 }
1083
1084 if (err < 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001085 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001086 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +08001087 dev_err(ci->dev, "error: ep_set_halt\n");
1088 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001089 }
1090 }
1091}
1092
1093/******************************************************************************
1094 * ENDPT block
1095 *****************************************************************************/
1096/**
1097 * ep_enable: configure endpoint, making it usable
1098 *
1099 * Check usb_ep_enable() at "usb_gadget.h" for details
1100 */
1101static int ep_enable(struct usb_ep *ep,
1102 const struct usb_endpoint_descriptor *desc)
1103{
1104 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301105 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001106 unsigned long flags;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001107 u32 cap = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001108
David Lopoaa69a802008-11-17 14:14:51 -08001109 if (ep == NULL || desc == NULL)
1110 return -EINVAL;
1111
1112 spin_lock_irqsave(mEp->lock, flags);
1113
1114 /* only internal SW should enable ctrl endpts */
1115
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001116 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001117
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301118 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001119 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001120
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001121 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1122 mEp->num = usb_endpoint_num(desc);
1123 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001124
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001125 mEp->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1126 mEp->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
David Lopoaa69a802008-11-17 14:14:51 -08001127
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301128 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001129 cap |= QH_IOS;
Michael Grzeschik776ffc12013-03-30 12:54:06 +02001130 if (mEp->num)
1131 cap |= QH_ZLT;
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001132 cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
David Lopoaa69a802008-11-17 14:14:51 -08001133
Michael Grzeschik1cd12a92013-03-30 12:54:05 +02001134 mEp->qh.ptr->cap = cpu_to_le32(cap);
1135
Svetoslav Neykov938d3232013-03-30 12:54:03 +02001136 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001137
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301138 /*
1139 * Enable endpoints in the HW other than ep0 as ep0
1140 * is always enabled
1141 */
1142 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001143 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001144
1145 spin_unlock_irqrestore(mEp->lock, flags);
1146 return retval;
1147}
1148
1149/**
1150 * ep_disable: endpoint is no longer usable
1151 *
1152 * Check usb_ep_disable() at "usb_gadget.h" for details
1153 */
1154static int ep_disable(struct usb_ep *ep)
1155{
1156 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1157 int direction, retval = 0;
1158 unsigned long flags;
1159
David Lopoaa69a802008-11-17 14:14:51 -08001160 if (ep == NULL)
1161 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001162 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001163 return -EBUSY;
1164
1165 spin_lock_irqsave(mEp->lock, flags);
1166
1167 /* only internal SW should disable ctrl endpts */
1168
1169 direction = mEp->dir;
1170 do {
David Lopoaa69a802008-11-17 14:14:51 -08001171 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001172 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001173
1174 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1175 mEp->dir = (mEp->dir == TX) ? RX : TX;
1176
1177 } while (mEp->dir != direction);
1178
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001179 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001180
1181 spin_unlock_irqrestore(mEp->lock, flags);
1182 return retval;
1183}
1184
1185/**
1186 * ep_alloc_request: allocate a request object to use with this endpoint
1187 *
1188 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1189 */
1190static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1191{
1192 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1193 struct ci13xxx_req *mReq = NULL;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001194 struct td_node *node;
David Lopoaa69a802008-11-17 14:14:51 -08001195
Alexander Shishkin0f089092012-05-08 23:29:02 +03001196 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001197 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001198
David Lopoaa69a802008-11-17 14:14:51 -08001199 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001200 node = kzalloc(sizeof(struct td_node), gfp_flags);
1201 if (mReq != NULL && node != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001202 INIT_LIST_HEAD(&mReq->queue);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001203 INIT_LIST_HEAD(&mReq->tds);
1204 INIT_LIST_HEAD(&node->td);
David Lopoaa69a802008-11-17 14:14:51 -08001205
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001206 node->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1207 &node->dma);
1208 if (node->ptr == NULL) {
1209 kfree(node);
David Lopoaa69a802008-11-17 14:14:51 -08001210 kfree(mReq);
1211 mReq = NULL;
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001212 } else {
1213 list_add_tail(&node->td, &mReq->tds);
David Lopoaa69a802008-11-17 14:14:51 -08001214 }
1215 }
1216
David Lopoaa69a802008-11-17 14:14:51 -08001217 return (mReq == NULL) ? NULL : &mReq->req;
1218}
1219
1220/**
1221 * ep_free_request: frees a request object
1222 *
1223 * Check usb_ep_free_request() at "usb_gadget.h" for details
1224 */
1225static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1226{
1227 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1228 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001229 struct td_node *firstnode;
David Lopoaa69a802008-11-17 14:14:51 -08001230 unsigned long flags;
1231
David Lopoaa69a802008-11-17 14:14:51 -08001232 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001233 return;
1234 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001235 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001236 return;
1237 }
1238
1239 spin_lock_irqsave(mEp->lock, flags);
1240
Michael Grzeschikcc9e6c42013-06-13 17:59:53 +03001241 firstnode = list_first_entry(&mReq->tds,
1242 struct td_node, td);
1243
1244 if (firstnode->ptr)
1245 dma_pool_free(mEp->td_pool, firstnode->ptr, firstnode->dma);
David Lopoaa69a802008-11-17 14:14:51 -08001246 kfree(mReq);
1247
David Lopoaa69a802008-11-17 14:14:51 -08001248 spin_unlock_irqrestore(mEp->lock, flags);
1249}
1250
1251/**
1252 * ep_queue: queues (submits) an I/O request to an endpoint
1253 *
1254 * Check usb_ep_queue()* at usb_gadget.h" for details
1255 */
1256static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1257 gfp_t __maybe_unused gfp_flags)
1258{
1259 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
David Lopoaa69a802008-11-17 14:14:51 -08001260 int retval = 0;
1261 unsigned long flags;
1262
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001263 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001264 return -EINVAL;
1265
1266 spin_lock_irqsave(mEp->lock, flags);
Michael Grzeschikdd064e92013-03-30 12:54:09 +02001267 retval = _ep_queue(ep, req, gfp_flags);
David Lopoaa69a802008-11-17 14:14:51 -08001268 spin_unlock_irqrestore(mEp->lock, flags);
1269 return retval;
1270}
1271
1272/**
1273 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1274 *
1275 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1276 */
1277static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1278{
1279 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1280 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1281 unsigned long flags;
1282
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301283 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001284 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301285 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001286 return -EINVAL;
1287
1288 spin_lock_irqsave(mEp->lock, flags);
1289
Richard Zhao26c696c2012-07-07 22:56:40 +08001290 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001291
1292 /* pop request */
1293 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001294
Richard Zhao26c696c2012-07-07 22:56:40 +08001295 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001296
David Lopoaa69a802008-11-17 14:14:51 -08001297 req->status = -ECONNRESET;
1298
Artem Leonenko7c25a822010-12-14 23:46:55 -08001299 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001300 spin_unlock(mEp->lock);
1301 mReq->req.complete(&mEp->ep, &mReq->req);
1302 spin_lock(mEp->lock);
1303 }
1304
1305 spin_unlock_irqrestore(mEp->lock, flags);
1306 return 0;
1307}
1308
1309/**
1310 * ep_set_halt: sets the endpoint halt feature
1311 *
1312 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1313 */
1314static int ep_set_halt(struct usb_ep *ep, int value)
1315{
1316 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1317 int direction, retval = 0;
1318 unsigned long flags;
1319
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001320 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001321 return -EINVAL;
1322
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +03001323 if (usb_endpoint_xfer_isoc(mEp->ep.desc))
1324 return -EOPNOTSUPP;
1325
David Lopoaa69a802008-11-17 14:14:51 -08001326 spin_lock_irqsave(mEp->lock, flags);
1327
1328#ifndef STALL_IN
1329 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1330 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301331 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001332 spin_unlock_irqrestore(mEp->lock, flags);
1333 return -EAGAIN;
1334 }
1335#endif
1336
1337 direction = mEp->dir;
1338 do {
Richard Zhao26c696c2012-07-07 22:56:40 +08001339 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001340
1341 if (!value)
1342 mEp->wedge = 0;
1343
1344 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1345 mEp->dir = (mEp->dir == TX) ? RX : TX;
1346
1347 } while (mEp->dir != direction);
1348
1349 spin_unlock_irqrestore(mEp->lock, flags);
1350 return retval;
1351}
1352
1353/**
1354 * ep_set_wedge: sets the halt feature and ignores clear requests
1355 *
1356 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1357 */
1358static int ep_set_wedge(struct usb_ep *ep)
1359{
1360 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1361 unsigned long flags;
1362
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001363 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001364 return -EINVAL;
1365
1366 spin_lock_irqsave(mEp->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001367 mEp->wedge = 1;
David Lopoaa69a802008-11-17 14:14:51 -08001368 spin_unlock_irqrestore(mEp->lock, flags);
1369
1370 return usb_ep_set_halt(ep);
1371}
1372
1373/**
1374 * ep_fifo_flush: flushes contents of a fifo
1375 *
1376 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1377 */
1378static void ep_fifo_flush(struct usb_ep *ep)
1379{
1380 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1381 unsigned long flags;
1382
David Lopoaa69a802008-11-17 14:14:51 -08001383 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001384 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001385 return;
1386 }
1387
1388 spin_lock_irqsave(mEp->lock, flags);
1389
Richard Zhao26c696c2012-07-07 22:56:40 +08001390 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001391
1392 spin_unlock_irqrestore(mEp->lock, flags);
1393}
1394
1395/**
1396 * Endpoint-specific part of the API to the USB controller hardware
1397 * Check "usb_gadget.h" for details
1398 */
1399static const struct usb_ep_ops usb_ep_ops = {
1400 .enable = ep_enable,
1401 .disable = ep_disable,
1402 .alloc_request = ep_alloc_request,
1403 .free_request = ep_free_request,
1404 .queue = ep_queue,
1405 .dequeue = ep_dequeue,
1406 .set_halt = ep_set_halt,
1407 .set_wedge = ep_set_wedge,
1408 .fifo_flush = ep_fifo_flush,
1409};
1410
1411/******************************************************************************
1412 * GADGET block
1413 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301414static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1415{
Richard Zhao26c696c2012-07-07 22:56:40 +08001416 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301417 unsigned long flags;
1418 int gadget_ready = 0;
1419
Richard Zhao26c696c2012-07-07 22:56:40 +08001420 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301421 return -EOPNOTSUPP;
1422
Richard Zhao26c696c2012-07-07 22:56:40 +08001423 spin_lock_irqsave(&ci->lock, flags);
1424 ci->vbus_active = is_active;
1425 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301426 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001427 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301428
1429 if (gadget_ready) {
1430 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301431 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001432 hw_device_reset(ci, USBMODE_CM_DC);
1433 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301434 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001435 hw_device_state(ci, 0);
1436 if (ci->platdata->notify_event)
1437 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301438 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001439 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301440 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301441 }
1442 }
1443
1444 return 0;
1445}
1446
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301447static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1448{
Richard Zhao26c696c2012-07-07 22:56:40 +08001449 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301450 unsigned long flags;
1451 int ret = 0;
1452
Richard Zhao26c696c2012-07-07 22:56:40 +08001453 spin_lock_irqsave(&ci->lock, flags);
1454 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301455 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301456 goto out;
1457 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001458 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301459 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301460 goto out;
1461 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001462 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301463out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001464 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301465 return ret;
1466}
1467
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301468static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1469{
Richard Zhao26c696c2012-07-07 22:56:40 +08001470 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301471
Richard Zhao26c696c2012-07-07 22:56:40 +08001472 if (ci->transceiver)
1473 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301474 return -ENOTSUPP;
1475}
1476
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001477/* Change Data+ pullup status
1478 * this func is used by usb_gadget_connect/disconnet
1479 */
1480static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1481{
1482 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1483
1484 if (is_on)
1485 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1486 else
1487 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1488
1489 return 0;
1490}
1491
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001492static int ci13xxx_start(struct usb_gadget *gadget,
1493 struct usb_gadget_driver *driver);
1494static int ci13xxx_stop(struct usb_gadget *gadget,
1495 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001496/**
1497 * Device operations part of the API to the USB controller hardware,
1498 * which don't involve endpoints (or i/o)
1499 * Check "usb_gadget.h" for details
1500 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301501static const struct usb_gadget_ops usb_gadget_ops = {
1502 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301503 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001504 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301505 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001506 .udc_start = ci13xxx_start,
1507 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301508};
David Lopoaa69a802008-11-17 14:14:51 -08001509
Richard Zhao26c696c2012-07-07 22:56:40 +08001510static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001511{
1512 int retval = 0, i, j;
1513
Richard Zhao26c696c2012-07-07 22:56:40 +08001514 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001515 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001516 int k = i + j * ci->hw_ep_max/2;
1517 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001518
1519 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1520 (j == TX) ? "in" : "out");
1521
Richard Zhao26c696c2012-07-07 22:56:40 +08001522 mEp->ci = ci;
1523 mEp->lock = &ci->lock;
1524 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001525
1526 mEp->ep.name = mEp->name;
1527 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001528 /*
1529 * for ep0: maxP defined in desc, for other
1530 * eps, maxP is set by epautoconfig() called
1531 * by gadget layer
1532 */
1533 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001534
1535 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001536 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001537 &mEp->qh.dma);
1538 if (mEp->qh.ptr == NULL)
1539 retval = -ENOMEM;
1540 else
1541 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1542
1543 /*
1544 * set up shorthands for ep0 out and in endpoints,
1545 * don't add to gadget's ep_list
1546 */
1547 if (i == 0) {
1548 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001549 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001550 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001551 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001552
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001553 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001554 continue;
1555 }
1556
Richard Zhao26c696c2012-07-07 22:56:40 +08001557 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001558 }
1559
1560 return retval;
1561}
1562
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001563static void destroy_eps(struct ci13xxx *ci)
1564{
1565 int i;
1566
1567 for (i = 0; i < ci->hw_ep_max; i++) {
1568 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1569
1570 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1571 }
1572}
1573
David Lopoaa69a802008-11-17 14:14:51 -08001574/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001575 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001576 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001577 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001578 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001579 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001580 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001581static int ci13xxx_start(struct usb_gadget *gadget,
1582 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001583{
Richard Zhao26c696c2012-07-07 22:56:40 +08001584 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301585 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001586 int retval = -ENOMEM;
1587
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001588 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001589 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001590
David Lopoaa69a802008-11-17 14:14:51 -08001591
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1593 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301594 if (retval)
1595 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001596
Richard Zhao26c696c2012-07-07 22:56:40 +08001597 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1598 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301599 if (retval)
1600 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001601 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001602
Richard Zhao26c696c2012-07-07 22:56:40 +08001603 ci->driver = driver;
1604 pm_runtime_get_sync(&ci->gadget.dev);
1605 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1606 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001607 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001608 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301609 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001610 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301611 goto done;
1612 }
1613 }
1614
Richard Zhao26c696c2012-07-07 22:56:40 +08001615 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301616 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001617 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001618
1619 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001620 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001621 return retval;
1622}
David Lopoaa69a802008-11-17 14:14:51 -08001623
1624/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001625 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001626 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001627static int ci13xxx_stop(struct usb_gadget *gadget,
1628 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001629{
Richard Zhao26c696c2012-07-07 22:56:40 +08001630 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001631 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001632
Richard Zhao26c696c2012-07-07 22:56:40 +08001633 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001634
Richard Zhao26c696c2012-07-07 22:56:40 +08001635 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1636 ci->vbus_active) {
1637 hw_device_state(ci, 0);
1638 if (ci->platdata->notify_event)
1639 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301640 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001641 ci->driver = NULL;
1642 spin_unlock_irqrestore(&ci->lock, flags);
1643 _gadget_stop_activity(&ci->gadget);
1644 spin_lock_irqsave(&ci->lock, flags);
1645 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301646 }
David Lopoaa69a802008-11-17 14:14:51 -08001647
Richard Zhao26c696c2012-07-07 22:56:40 +08001648 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001649
David Lopoaa69a802008-11-17 14:14:51 -08001650 return 0;
1651}
David Lopoaa69a802008-11-17 14:14:51 -08001652
1653/******************************************************************************
1654 * BUS block
1655 *****************************************************************************/
1656/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001657 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001658 *
1659 * This function returns IRQ_HANDLED if the IRQ has been handled
1660 * It locks access to registers
1661 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001662static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001663{
David Lopoaa69a802008-11-17 14:14:51 -08001664 irqreturn_t retval;
1665 u32 intr;
1666
Richard Zhao26c696c2012-07-07 22:56:40 +08001667 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001668 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001669
Richard Zhao26c696c2012-07-07 22:56:40 +08001670 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301671
Richard Zhao26c696c2012-07-07 22:56:40 +08001672 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1673 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001674 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001675 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301676 return IRQ_NONE;
1677 }
1678 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001679 intr = hw_test_and_clear_intr_active(ci);
David Lopoaa69a802008-11-17 14:14:51 -08001680
Alexander Shishkine443b332012-05-11 17:25:46 +03001681 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001682 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001683 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001684 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001685
David Lopoaa69a802008-11-17 14:14:51 -08001686 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001687 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001688 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001689 if (ci->suspended && ci->driver->resume) {
1690 spin_unlock(&ci->lock);
1691 ci->driver->resume(&ci->gadget);
1692 spin_lock(&ci->lock);
1693 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301694 }
David Lopoaa69a802008-11-17 14:14:51 -08001695 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001696
1697 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001698 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001699
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301700 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001701 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1702 ci->driver->suspend) {
1703 ci->suspended = 1;
1704 spin_unlock(&ci->lock);
1705 ci->driver->suspend(&ci->gadget);
1706 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301707 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301708 }
David Lopoaa69a802008-11-17 14:14:51 -08001709 retval = IRQ_HANDLED;
1710 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001711 retval = IRQ_NONE;
1712 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001713 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001714
1715 return retval;
1716}
1717
1718/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001719 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001720 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001721 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001722static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001723{
Richard Zhao26c696c2012-07-07 22:56:40 +08001724 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001725 int retval = 0;
1726
Richard Zhao26c696c2012-07-07 22:56:40 +08001727 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001728
Richard Zhao26c696c2012-07-07 22:56:40 +08001729 ci->gadget.ops = &usb_gadget_ops;
1730 ci->gadget.speed = USB_SPEED_UNKNOWN;
1731 ci->gadget.max_speed = USB_SPEED_HIGH;
1732 ci->gadget.is_otg = 0;
1733 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001734
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001736
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001737 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001738 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001739 sizeof(struct ci13xxx_qh),
1740 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001741 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001742 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001743
Richard Zhao26c696c2012-07-07 22:56:40 +08001744 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001745 sizeof(struct ci13xxx_td),
1746 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001747 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001748 retval = -ENOMEM;
1749 goto free_qh_pool;
1750 }
1751
Richard Zhao26c696c2012-07-07 22:56:40 +08001752 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001753 if (retval)
1754 goto free_pools;
1755
Richard Zhao26c696c2012-07-07 22:56:40 +08001756 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301757
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001758 if (ci->global_phy) {
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001759 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001760 if (IS_ERR(ci->transceiver))
1761 ci->transceiver = NULL;
1762 }
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301763
Richard Zhao26c696c2012-07-07 22:56:40 +08001764 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1765 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301766 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001767 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301768 }
1769 }
1770
Richard Zhao26c696c2012-07-07 22:56:40 +08001771 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1772 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301773 if (retval)
1774 goto put_transceiver;
1775 }
1776
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001777 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001778 retval = otg_set_peripheral(ci->transceiver->otg,
1779 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301780 if (retval)
Greg Kroah-Hartman64dc9e22013-04-05 15:18:00 -07001781 goto put_transceiver;
David Lopoaa69a802008-11-17 14:14:51 -08001782 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001783
Richard Zhao26c696c2012-07-07 22:56:40 +08001784 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001785 if (retval)
1786 goto remove_trans;
1787
Richard Zhao26c696c2012-07-07 22:56:40 +08001788 pm_runtime_no_callbacks(&ci->gadget.dev);
1789 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001790
David Lopoaa69a802008-11-17 14:14:51 -08001791 return retval;
1792
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001793remove_trans:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001794 if (ci->transceiver) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001795 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001796 if (ci->global_phy)
1797 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001798 }
1799
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001800 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301801put_transceiver:
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001802 if (ci->transceiver && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001803 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001804destroy_eps:
1805 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001806free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001807 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001808free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001809 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001810 return retval;
1811}
1812
1813/**
1814 * udc_remove: parent remove must call this to remove UDC
1815 *
1816 * No interrupts active, the IRQ has been released
1817 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001818static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001819{
Richard Zhao26c696c2012-07-07 22:56:40 +08001820 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001821 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001822
Richard Zhao26c696c2012-07-07 22:56:40 +08001823 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001824
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001825 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001826
Richard Zhao26c696c2012-07-07 22:56:40 +08001827 dma_pool_destroy(ci->td_pool);
1828 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001829
Alexander Shishkind343f4e2013-06-11 13:41:47 +03001830 if (ci->transceiver) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001831 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001832 if (ci->global_phy)
1833 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301834 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001835 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001836 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001837}
David Lopoaa69a802008-11-17 14:14:51 -08001838
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001839/**
1840 * ci_hdrc_gadget_init - initialize device related bits
1841 * ci: the controller
1842 *
1843 * This function enables the gadget role, if the device is "device capable".
1844 */
1845int ci_hdrc_gadget_init(struct ci13xxx *ci)
1846{
1847 struct ci_role_driver *rdrv;
1848
1849 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1850 return -ENXIO;
1851
1852 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1853 if (!rdrv)
1854 return -ENOMEM;
1855
1856 rdrv->start = udc_start;
1857 rdrv->stop = udc_stop;
1858 rdrv->irq = udc_irq;
1859 rdrv->name = "gadget";
1860 ci->roles[CI_ROLE_GADGET] = rdrv;
1861
1862 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001863}