blob: 86db1def9e28aea91a60601656fcbc816c132878 [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 */
143 data = type << ffs_nr(mask);
144
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 */
152 data = type << ffs_nr(mask);
153
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,
Alexander Shishkinef15e542012-05-11 17:25:43 +0300334 value << ffs_nr(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));
407 mReq->zptr->next = TD_TERMINATE;
408 mReq->zptr->token = TD_STATUS_ACTIVE;
409 if (!mReq->req.no_interrupt)
410 mReq->zptr->token |= TD_IOC;
411 }
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));
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530421 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800422 mReq->ptr->token &= TD_TOTAL_BYTES;
David Lopoaa69a802008-11-17 14:14:51 -0800423 mReq->ptr->token |= TD_STATUS_ACTIVE;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530424 if (mReq->zptr) {
425 mReq->ptr->next = mReq->zdma;
426 } else {
427 mReq->ptr->next = TD_TERMINATE;
428 if (!mReq->req.no_interrupt)
429 mReq->ptr->token |= TD_IOC;
430 }
David Lopoaa69a802008-11-17 14:14:51 -0800431 mReq->ptr->page[0] = mReq->req.dma;
432 for (i = 1; i < 5; i++)
433 mReq->ptr->page[i] =
Artem Leonenko0a313c42010-12-14 23:47:06 -0800434 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
David Lopoaa69a802008-11-17 14:14:51 -0800435
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530436 if (!list_empty(&mEp->qh.queue)) {
437 struct ci13xxx_req *mReqPrev;
438 int n = hw_ep_bit(mEp->num, mEp->dir);
439 int tmp_stat;
440
441 mReqPrev = list_entry(mEp->qh.queue.prev,
442 struct ci13xxx_req, queue);
443 if (mReqPrev->zptr)
444 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
445 else
446 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
447 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800448 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530449 goto done;
450 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800451 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
452 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
453 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
454 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530455 if (tmp_stat)
456 goto done;
457 }
458
459 /* QH configuration */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530460 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
461 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530462 mEp->qh.ptr->cap |= QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -0800463
464 wmb(); /* synchronize before ep prime */
465
Richard Zhao26c696c2012-07-07 22:56:40 +0800466 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800467 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530468done:
469 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800470}
471
472/**
473 * _hardware_dequeue: handles a request at hardware level
474 * @gadget: gadget
475 * @mEp: endpoint
476 *
477 * This function returns an error code
478 */
479static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
480{
David Lopoaa69a802008-11-17 14:14:51 -0800481 if (mReq->req.status != -EALREADY)
482 return -EINVAL;
483
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530484 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
485 return -EBUSY;
486
487 if (mReq->zptr) {
488 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
489 return -EBUSY;
490 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
491 mReq->zptr = NULL;
492 }
David Lopoaa69a802008-11-17 14:14:51 -0800493
494 mReq->req.status = 0;
495
Richard Zhao26c696c2012-07-07 22:56:40 +0800496 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800497
498 mReq->req.status = mReq->ptr->token & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530499 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800500 mReq->req.status = -1;
501 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
502 mReq->req.status = -1;
503 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
504 mReq->req.status = -1;
505
506 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
507 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
508 mReq->req.actual = mReq->req.length - mReq->req.actual;
509 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
510
511 return mReq->req.actual;
512}
513
514/**
515 * _ep_nuke: dequeues all endpoint requests
516 * @mEp: endpoint
517 *
518 * This function returns an error code
519 * Caller must hold lock
520 */
521static int _ep_nuke(struct ci13xxx_ep *mEp)
522__releases(mEp->lock)
523__acquires(mEp->lock)
524{
David Lopoaa69a802008-11-17 14:14:51 -0800525 if (mEp == NULL)
526 return -EINVAL;
527
Richard Zhao26c696c2012-07-07 22:56:40 +0800528 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800529
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530530 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800531
532 /* pop oldest request */
533 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530534 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800535 struct ci13xxx_req, queue);
536 list_del_init(&mReq->queue);
537 mReq->req.status = -ESHUTDOWN;
538
Artem Leonenko7c25a822010-12-14 23:46:55 -0800539 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800540 spin_unlock(mEp->lock);
541 mReq->req.complete(&mEp->ep, &mReq->req);
542 spin_lock(mEp->lock);
543 }
544 }
545 return 0;
546}
547
548/**
549 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
550 * @gadget: gadget
551 *
552 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800553 */
554static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800555{
556 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800557 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530558 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800559
Richard Zhao26c696c2012-07-07 22:56:40 +0800560 spin_lock_irqsave(&ci->lock, flags);
561 ci->gadget.speed = USB_SPEED_UNKNOWN;
562 ci->remote_wakeup = 0;
563 ci->suspended = 0;
564 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530565
David Lopoaa69a802008-11-17 14:14:51 -0800566 /* flush all endpoints */
567 gadget_for_each_ep(ep, gadget) {
568 usb_ep_fifo_flush(ep);
569 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800570 usb_ep_fifo_flush(&ci->ep0out->ep);
571 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800572
Richard Zhao26c696c2012-07-07 22:56:40 +0800573 if (ci->driver)
574 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800575
576 /* make sure to disable all endpoints */
577 gadget_for_each_ep(ep, gadget) {
578 usb_ep_disable(ep);
579 }
David Lopoaa69a802008-11-17 14:14:51 -0800580
Richard Zhao26c696c2012-07-07 22:56:40 +0800581 if (ci->status != NULL) {
582 usb_ep_free_request(&ci->ep0in->ep, ci->status);
583 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800584 }
585
David Lopoaa69a802008-11-17 14:14:51 -0800586 return 0;
587}
588
589/******************************************************************************
590 * ISR block
591 *****************************************************************************/
592/**
593 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800594 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800595 *
596 * This function resets USB engine after a bus reset occurred
597 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800598static void isr_reset_handler(struct ci13xxx *ci)
599__releases(ci->lock)
600__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800601{
David Lopoaa69a802008-11-17 14:14:51 -0800602 int retval;
603
David Lopoaa69a802008-11-17 14:14:51 -0800604 dbg_event(0xFF, "BUS RST", 0);
605
Richard Zhao26c696c2012-07-07 22:56:40 +0800606 spin_unlock(&ci->lock);
607 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800608 if (retval)
609 goto done;
610
Richard Zhao26c696c2012-07-07 22:56:40 +0800611 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800612 if (retval)
613 goto done;
614
Richard Zhao26c696c2012-07-07 22:56:40 +0800615 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
616 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530617 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530618
Michael Grzeschikb9322252012-05-11 17:25:50 +0300619done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800620 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800621
David Lopoaa69a802008-11-17 14:14:51 -0800622 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800623 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800624}
625
626/**
627 * isr_get_status_complete: get_status request complete function
628 * @ep: endpoint
629 * @req: request handled
630 *
631 * Caller must release lock
632 */
633static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
634{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300635 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800636 return;
David Lopoaa69a802008-11-17 14:14:51 -0800637
638 kfree(req->buf);
639 usb_ep_free_request(ep, req);
640}
641
642/**
643 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800644 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800645 * @setup: setup request packet
646 *
647 * This function returns an error code
648 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800649static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800650 struct usb_ctrlrequest *setup)
651__releases(mEp->lock)
652__acquires(mEp->lock)
653{
Richard Zhao26c696c2012-07-07 22:56:40 +0800654 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800655 struct usb_request *req = NULL;
656 gfp_t gfp_flags = GFP_ATOMIC;
657 int dir, num, retval;
658
David Lopoaa69a802008-11-17 14:14:51 -0800659 if (mEp == NULL || setup == NULL)
660 return -EINVAL;
661
662 spin_unlock(mEp->lock);
663 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
664 spin_lock(mEp->lock);
665 if (req == NULL)
666 return -ENOMEM;
667
668 req->complete = isr_get_status_complete;
669 req->length = 2;
670 req->buf = kzalloc(req->length, gfp_flags);
671 if (req->buf == NULL) {
672 retval = -ENOMEM;
673 goto err_free_req;
674 }
675
676 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530677 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800678 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800679 retval = 0;
680 } else if ((setup->bRequestType & USB_RECIP_MASK) \
681 == USB_RECIP_ENDPOINT) {
682 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
683 TX : RX;
684 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800685 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800686 }
687 /* else do nothing; reserved for future use */
688
689 spin_unlock(mEp->lock);
690 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
691 spin_lock(mEp->lock);
692 if (retval)
693 goto err_free_buf;
694
695 return 0;
696
697 err_free_buf:
698 kfree(req->buf);
699 err_free_req:
700 spin_unlock(mEp->lock);
701 usb_ep_free_request(&mEp->ep, req);
702 spin_lock(mEp->lock);
703 return retval;
704}
705
706/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530707 * isr_setup_status_complete: setup_status request complete function
708 * @ep: endpoint
709 * @req: request handled
710 *
711 * Caller must release lock. Put the port in test mode if test mode
712 * feature is selected.
713 */
714static void
715isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
716{
Richard Zhao26c696c2012-07-07 22:56:40 +0800717 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530718 unsigned long flags;
719
Richard Zhao26c696c2012-07-07 22:56:40 +0800720 if (ci->setaddr) {
721 hw_usb_set_address(ci, ci->address);
722 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300723 }
724
Richard Zhao26c696c2012-07-07 22:56:40 +0800725 spin_lock_irqsave(&ci->lock, flags);
726 if (ci->test_mode)
727 hw_port_test_set(ci, ci->test_mode);
728 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530729}
730
731/**
David Lopoaa69a802008-11-17 14:14:51 -0800732 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800733 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800734 *
735 * This function returns an error code
736 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800737static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800738__releases(mEp->lock)
739__acquires(mEp->lock)
740{
741 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530742 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800743
Richard Zhao26c696c2012-07-07 22:56:40 +0800744 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
745 ci->status->context = ci;
746 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800747
748 spin_unlock(mEp->lock);
Richard Zhao26c696c2012-07-07 22:56:40 +0800749 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800750 spin_lock(mEp->lock);
751
752 return retval;
753}
754
755/**
756 * isr_tr_complete_low: transaction complete low level handler
757 * @mEp: endpoint
758 *
759 * This function returns an error code
760 * Caller must hold lock
761 */
762static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
763__releases(mEp->lock)
764__acquires(mEp->lock)
765{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530766 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530767 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300768 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800769
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530770 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
771 queue) {
772 retval = _hardware_dequeue(mEp, mReq);
773 if (retval < 0)
774 break;
775 list_del_init(&mReq->queue);
776 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
777 if (mReq->req.complete != NULL) {
778 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530779 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
780 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800781 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530782 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530783 spin_lock(mEp->lock);
784 }
785 }
David Lopoaa69a802008-11-17 14:14:51 -0800786
Pavankumar Kondetief907482011-05-02 11:56:27 +0530787 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530788 retval = 0;
789 if (retval < 0)
David Lopoaa69a802008-11-17 14:14:51 -0800790 dbg_event(_usb_addr(mEp), "DONE", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800791
David Lopoaa69a802008-11-17 14:14:51 -0800792 return retval;
793}
794
795/**
796 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800797 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800798 *
799 * This function handles traffic events
800 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800801static void isr_tr_complete_handler(struct ci13xxx *ci)
802__releases(ci->lock)
803__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800804{
805 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530806 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800807
Richard Zhao26c696c2012-07-07 22:56:40 +0800808 for (i = 0; i < ci->hw_ep_max; i++) {
809 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530810 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800811 struct usb_ctrlrequest req;
812
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200813 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800814 continue; /* not configured */
815
Richard Zhao26c696c2012-07-07 22:56:40 +0800816 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800817 err = isr_tr_complete_low(mEp);
818 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
819 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800820 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800821 if (err < 0) {
822 dbg_event(_usb_addr(mEp),
823 "ERROR", err);
Richard Zhao26c696c2012-07-07 22:56:40 +0800824 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800825 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800826 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700827 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800828 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800829 }
830 }
831 }
832
833 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800834 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800835 continue;
836
837 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800838 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800839 continue;
840 }
841
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530842 /*
843 * Flush data and handshake transactions of previous
844 * setup packet.
845 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800846 _ep_nuke(ci->ep0out);
847 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530848
David Lopoaa69a802008-11-17 14:14:51 -0800849 /* read_setup_packet */
850 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800851 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530852 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800853 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800854
855 type = req.bRequestType;
856
Richard Zhao26c696c2012-07-07 22:56:40 +0800857 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800858
859 dbg_setup(_usb_addr(mEp), &req);
860
861 switch (req.bRequest) {
862 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530863 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
864 le16_to_cpu(req.wValue) ==
865 USB_ENDPOINT_HALT) {
866 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800867 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530868 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530869 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530870 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530871 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800872 num += ci->hw_ep_max/2;
873 if (!ci->ci13xxx_ep[num].wedge) {
874 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530875 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800876 &ci->ci13xxx_ep[num].ep);
877 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530878 if (err)
879 break;
880 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800881 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530882 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
883 le16_to_cpu(req.wValue) ==
884 USB_DEVICE_REMOTE_WAKEUP) {
885 if (req.wLength != 0)
886 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800887 ci->remote_wakeup = 0;
888 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530889 } else {
890 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800891 }
David Lopoaa69a802008-11-17 14:14:51 -0800892 break;
893 case USB_REQ_GET_STATUS:
894 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
895 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
896 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
897 goto delegate;
898 if (le16_to_cpu(req.wLength) != 2 ||
899 le16_to_cpu(req.wValue) != 0)
900 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800901 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800902 break;
903 case USB_REQ_SET_ADDRESS:
904 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
905 goto delegate;
906 if (le16_to_cpu(req.wLength) != 0 ||
907 le16_to_cpu(req.wIndex) != 0)
908 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800909 ci->address = (u8)le16_to_cpu(req.wValue);
910 ci->setaddr = true;
911 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800912 break;
913 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530914 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
915 le16_to_cpu(req.wValue) ==
916 USB_ENDPOINT_HALT) {
917 if (req.wLength != 0)
918 break;
919 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530920 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530921 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530922 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800923 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800924
Richard Zhao26c696c2012-07-07 22:56:40 +0800925 spin_unlock(&ci->lock);
926 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
927 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530928 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800929 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530930 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530931 if (req.wLength != 0)
932 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530933 switch (le16_to_cpu(req.wValue)) {
934 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800935 ci->remote_wakeup = 1;
936 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530937 break;
938 case USB_DEVICE_TEST_MODE:
939 tmode = le16_to_cpu(req.wIndex) >> 8;
940 switch (tmode) {
941 case TEST_J:
942 case TEST_K:
943 case TEST_SE0_NAK:
944 case TEST_PACKET:
945 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800946 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530947 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800948 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530949 break;
950 default:
951 break;
952 }
953 default:
954 goto delegate;
955 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530956 } else {
957 goto delegate;
958 }
David Lopoaa69a802008-11-17 14:14:51 -0800959 break;
960 default:
961delegate:
962 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800963 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800964
Richard Zhao26c696c2012-07-07 22:56:40 +0800965 spin_unlock(&ci->lock);
966 err = ci->driver->setup(&ci->gadget, &req);
967 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800968 break;
969 }
970
971 if (err < 0) {
972 dbg_event(_usb_addr(mEp), "ERROR", err);
973
Richard Zhao26c696c2012-07-07 22:56:40 +0800974 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800975 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800976 dev_err(ci->dev, "error: ep_set_halt\n");
977 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800978 }
979 }
980}
981
982/******************************************************************************
983 * ENDPT block
984 *****************************************************************************/
985/**
986 * ep_enable: configure endpoint, making it usable
987 *
988 * Check usb_ep_enable() at "usb_gadget.h" for details
989 */
990static int ep_enable(struct usb_ep *ep,
991 const struct usb_endpoint_descriptor *desc)
992{
993 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530994 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800995 unsigned long flags;
996
David Lopoaa69a802008-11-17 14:14:51 -0800997 if (ep == NULL || desc == NULL)
998 return -EINVAL;
999
1000 spin_lock_irqsave(mEp->lock, flags);
1001
1002 /* only internal SW should enable ctrl endpts */
1003
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001004 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001005
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301006 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001007 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001008
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001009 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1010 mEp->num = usb_endpoint_num(desc);
1011 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001012
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001013 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001014
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301015 dbg_event(_usb_addr(mEp), "ENABLE", 0);
David Lopoaa69a802008-11-17 14:14:51 -08001016
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301017 mEp->qh.ptr->cap = 0;
David Lopof23e6492009-04-16 14:35:24 -07001018
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301019 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1020 mEp->qh.ptr->cap |= QH_IOS;
1021 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1022 mEp->qh.ptr->cap &= ~QH_MULT;
1023 else
1024 mEp->qh.ptr->cap &= ~QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -08001025
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301026 mEp->qh.ptr->cap |=
1027 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1028 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001029
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301030 /*
1031 * Enable endpoints in the HW other than ep0 as ep0
1032 * is always enabled
1033 */
1034 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001035 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001036
1037 spin_unlock_irqrestore(mEp->lock, flags);
1038 return retval;
1039}
1040
1041/**
1042 * ep_disable: endpoint is no longer usable
1043 *
1044 * Check usb_ep_disable() at "usb_gadget.h" for details
1045 */
1046static int ep_disable(struct usb_ep *ep)
1047{
1048 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1049 int direction, retval = 0;
1050 unsigned long flags;
1051
David Lopoaa69a802008-11-17 14:14:51 -08001052 if (ep == NULL)
1053 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001054 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001055 return -EBUSY;
1056
1057 spin_lock_irqsave(mEp->lock, flags);
1058
1059 /* only internal SW should disable ctrl endpts */
1060
1061 direction = mEp->dir;
1062 do {
1063 dbg_event(_usb_addr(mEp), "DISABLE", 0);
1064
1065 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001066 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001067
1068 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1069 mEp->dir = (mEp->dir == TX) ? RX : TX;
1070
1071 } while (mEp->dir != direction);
1072
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001073 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001074
1075 spin_unlock_irqrestore(mEp->lock, flags);
1076 return retval;
1077}
1078
1079/**
1080 * ep_alloc_request: allocate a request object to use with this endpoint
1081 *
1082 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1083 */
1084static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1085{
1086 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1087 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001088
Alexander Shishkin0f089092012-05-08 23:29:02 +03001089 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001090 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001091
David Lopoaa69a802008-11-17 14:14:51 -08001092 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1093 if (mReq != NULL) {
1094 INIT_LIST_HEAD(&mReq->queue);
1095
1096 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1097 &mReq->dma);
1098 if (mReq->ptr == NULL) {
1099 kfree(mReq);
1100 mReq = NULL;
1101 }
1102 }
1103
1104 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
1105
David Lopoaa69a802008-11-17 14:14:51 -08001106 return (mReq == NULL) ? NULL : &mReq->req;
1107}
1108
1109/**
1110 * ep_free_request: frees a request object
1111 *
1112 * Check usb_ep_free_request() at "usb_gadget.h" for details
1113 */
1114static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1115{
1116 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1117 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1118 unsigned long flags;
1119
David Lopoaa69a802008-11-17 14:14:51 -08001120 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001121 return;
1122 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001123 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001124 return;
1125 }
1126
1127 spin_lock_irqsave(mEp->lock, flags);
1128
1129 if (mReq->ptr)
1130 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1131 kfree(mReq);
1132
1133 dbg_event(_usb_addr(mEp), "FREE", 0);
1134
1135 spin_unlock_irqrestore(mEp->lock, flags);
1136}
1137
1138/**
1139 * ep_queue: queues (submits) an I/O request to an endpoint
1140 *
1141 * Check usb_ep_queue()* at usb_gadget.h" for details
1142 */
1143static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1144 gfp_t __maybe_unused gfp_flags)
1145{
1146 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1147 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Richard Zhao26c696c2012-07-07 22:56:40 +08001148 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -08001149 int retval = 0;
1150 unsigned long flags;
1151
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001152 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001153 return -EINVAL;
1154
1155 spin_lock_irqsave(mEp->lock, flags);
1156
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301157 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1158 if (req->length)
Richard Zhao26c696c2012-07-07 22:56:40 +08001159 mEp = (ci->ep0_dir == RX) ?
1160 ci->ep0out : ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301161 if (!list_empty(&mEp->qh.queue)) {
1162 _ep_nuke(mEp);
1163 retval = -EOVERFLOW;
Richard Zhao26c696c2012-07-07 22:56:40 +08001164 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
Alexander Shishkin0f089092012-05-08 23:29:02 +03001165 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301166 }
David Lopoaa69a802008-11-17 14:14:51 -08001167 }
1168
1169 /* first nuke then test link, e.g. previous status has not sent */
1170 if (!list_empty(&mReq->queue)) {
1171 retval = -EBUSY;
Richard Zhao26c696c2012-07-07 22:56:40 +08001172 dev_err(mEp->ci->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001173 goto done;
1174 }
1175
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001176 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1177 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001178 retval = -EMSGSIZE;
Richard Zhao26c696c2012-07-07 22:56:40 +08001179 dev_warn(mEp->ci->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001180 }
1181
1182 dbg_queue(_usb_addr(mEp), req, retval);
1183
1184 /* push request */
1185 mReq->req.status = -EINPROGRESS;
1186 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001187
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301188 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001189
1190 if (retval == -EALREADY) {
David Lopoaa69a802008-11-17 14:14:51 -08001191 dbg_event(_usb_addr(mEp), "QUEUE", retval);
1192 retval = 0;
1193 }
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301194 if (!retval)
1195 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001196
1197 done:
1198 spin_unlock_irqrestore(mEp->lock, flags);
1199 return retval;
1200}
1201
1202/**
1203 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1204 *
1205 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1206 */
1207static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1208{
1209 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1210 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1211 unsigned long flags;
1212
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301213 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001214 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301215 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001216 return -EINVAL;
1217
1218 spin_lock_irqsave(mEp->lock, flags);
1219
1220 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
1221
Richard Zhao26c696c2012-07-07 22:56:40 +08001222 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001223
1224 /* pop request */
1225 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001226
Richard Zhao26c696c2012-07-07 22:56:40 +08001227 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001228
David Lopoaa69a802008-11-17 14:14:51 -08001229 req->status = -ECONNRESET;
1230
Artem Leonenko7c25a822010-12-14 23:46:55 -08001231 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001232 spin_unlock(mEp->lock);
1233 mReq->req.complete(&mEp->ep, &mReq->req);
1234 spin_lock(mEp->lock);
1235 }
1236
1237 spin_unlock_irqrestore(mEp->lock, flags);
1238 return 0;
1239}
1240
1241/**
1242 * ep_set_halt: sets the endpoint halt feature
1243 *
1244 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1245 */
1246static int ep_set_halt(struct usb_ep *ep, int value)
1247{
1248 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1249 int direction, retval = 0;
1250 unsigned long flags;
1251
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001252 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001253 return -EINVAL;
1254
1255 spin_lock_irqsave(mEp->lock, flags);
1256
1257#ifndef STALL_IN
1258 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1259 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301260 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001261 spin_unlock_irqrestore(mEp->lock, flags);
1262 return -EAGAIN;
1263 }
1264#endif
1265
1266 direction = mEp->dir;
1267 do {
1268 dbg_event(_usb_addr(mEp), "HALT", value);
Richard Zhao26c696c2012-07-07 22:56:40 +08001269 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001270
1271 if (!value)
1272 mEp->wedge = 0;
1273
1274 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1275 mEp->dir = (mEp->dir == TX) ? RX : TX;
1276
1277 } while (mEp->dir != direction);
1278
1279 spin_unlock_irqrestore(mEp->lock, flags);
1280 return retval;
1281}
1282
1283/**
1284 * ep_set_wedge: sets the halt feature and ignores clear requests
1285 *
1286 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1287 */
1288static int ep_set_wedge(struct usb_ep *ep)
1289{
1290 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1291 unsigned long flags;
1292
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001293 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001294 return -EINVAL;
1295
1296 spin_lock_irqsave(mEp->lock, flags);
1297
1298 dbg_event(_usb_addr(mEp), "WEDGE", 0);
1299 mEp->wedge = 1;
1300
1301 spin_unlock_irqrestore(mEp->lock, flags);
1302
1303 return usb_ep_set_halt(ep);
1304}
1305
1306/**
1307 * ep_fifo_flush: flushes contents of a fifo
1308 *
1309 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1310 */
1311static void ep_fifo_flush(struct usb_ep *ep)
1312{
1313 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1314 unsigned long flags;
1315
David Lopoaa69a802008-11-17 14:14:51 -08001316 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001317 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001318 return;
1319 }
1320
1321 spin_lock_irqsave(mEp->lock, flags);
1322
1323 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
Richard Zhao26c696c2012-07-07 22:56:40 +08001324 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001325
1326 spin_unlock_irqrestore(mEp->lock, flags);
1327}
1328
1329/**
1330 * Endpoint-specific part of the API to the USB controller hardware
1331 * Check "usb_gadget.h" for details
1332 */
1333static const struct usb_ep_ops usb_ep_ops = {
1334 .enable = ep_enable,
1335 .disable = ep_disable,
1336 .alloc_request = ep_alloc_request,
1337 .free_request = ep_free_request,
1338 .queue = ep_queue,
1339 .dequeue = ep_dequeue,
1340 .set_halt = ep_set_halt,
1341 .set_wedge = ep_set_wedge,
1342 .fifo_flush = ep_fifo_flush,
1343};
1344
1345/******************************************************************************
1346 * GADGET block
1347 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301348static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1349{
Richard Zhao26c696c2012-07-07 22:56:40 +08001350 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301351 unsigned long flags;
1352 int gadget_ready = 0;
1353
Richard Zhao26c696c2012-07-07 22:56:40 +08001354 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301355 return -EOPNOTSUPP;
1356
Richard Zhao26c696c2012-07-07 22:56:40 +08001357 spin_lock_irqsave(&ci->lock, flags);
1358 ci->vbus_active = is_active;
1359 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301360 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001361 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301362
1363 if (gadget_ready) {
1364 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301365 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001366 hw_device_reset(ci, USBMODE_CM_DC);
1367 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301368 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001369 hw_device_state(ci, 0);
1370 if (ci->platdata->notify_event)
1371 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301372 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001373 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301374 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301375 }
1376 }
1377
1378 return 0;
1379}
1380
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301381static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1382{
Richard Zhao26c696c2012-07-07 22:56:40 +08001383 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301384 unsigned long flags;
1385 int ret = 0;
1386
Richard Zhao26c696c2012-07-07 22:56:40 +08001387 spin_lock_irqsave(&ci->lock, flags);
1388 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301389 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301390 goto out;
1391 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001392 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301393 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301394 goto out;
1395 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001396 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301397out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001398 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301399 return ret;
1400}
1401
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301402static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1403{
Richard Zhao26c696c2012-07-07 22:56:40 +08001404 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301405
Richard Zhao26c696c2012-07-07 22:56:40 +08001406 if (ci->transceiver)
1407 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301408 return -ENOTSUPP;
1409}
1410
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001411/* Change Data+ pullup status
1412 * this func is used by usb_gadget_connect/disconnet
1413 */
1414static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1415{
1416 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1417
1418 if (is_on)
1419 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1420 else
1421 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1422
1423 return 0;
1424}
1425
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001426static int ci13xxx_start(struct usb_gadget *gadget,
1427 struct usb_gadget_driver *driver);
1428static int ci13xxx_stop(struct usb_gadget *gadget,
1429 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001430/**
1431 * Device operations part of the API to the USB controller hardware,
1432 * which don't involve endpoints (or i/o)
1433 * Check "usb_gadget.h" for details
1434 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301435static const struct usb_gadget_ops usb_gadget_ops = {
1436 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301437 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001438 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301439 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001440 .udc_start = ci13xxx_start,
1441 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301442};
David Lopoaa69a802008-11-17 14:14:51 -08001443
Richard Zhao26c696c2012-07-07 22:56:40 +08001444static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001445{
1446 int retval = 0, i, j;
1447
Richard Zhao26c696c2012-07-07 22:56:40 +08001448 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001449 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001450 int k = i + j * ci->hw_ep_max/2;
1451 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001452
1453 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1454 (j == TX) ? "in" : "out");
1455
Richard Zhao26c696c2012-07-07 22:56:40 +08001456 mEp->ci = ci;
1457 mEp->lock = &ci->lock;
1458 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001459
1460 mEp->ep.name = mEp->name;
1461 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001462 /*
1463 * for ep0: maxP defined in desc, for other
1464 * eps, maxP is set by epautoconfig() called
1465 * by gadget layer
1466 */
1467 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001468
1469 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001470 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001471 &mEp->qh.dma);
1472 if (mEp->qh.ptr == NULL)
1473 retval = -ENOMEM;
1474 else
1475 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1476
1477 /*
1478 * set up shorthands for ep0 out and in endpoints,
1479 * don't add to gadget's ep_list
1480 */
1481 if (i == 0) {
1482 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001483 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001484 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001485 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001486
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001487 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001488 continue;
1489 }
1490
Richard Zhao26c696c2012-07-07 22:56:40 +08001491 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001492 }
1493
1494 return retval;
1495}
1496
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001497static void destroy_eps(struct ci13xxx *ci)
1498{
1499 int i;
1500
1501 for (i = 0; i < ci->hw_ep_max; i++) {
1502 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1503
1504 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1505 }
1506}
1507
David Lopoaa69a802008-11-17 14:14:51 -08001508/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001509 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001510 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001511 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001512 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001513 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001514 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001515static int ci13xxx_start(struct usb_gadget *gadget,
1516 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001517{
Richard Zhao26c696c2012-07-07 22:56:40 +08001518 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301519 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001520 int retval = -ENOMEM;
1521
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001522 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001523 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001524
David Lopoaa69a802008-11-17 14:14:51 -08001525
Richard Zhao26c696c2012-07-07 22:56:40 +08001526 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1527 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301528 if (retval)
1529 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001530
Richard Zhao26c696c2012-07-07 22:56:40 +08001531 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1532 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301533 if (retval)
1534 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001535 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001536
Richard Zhao26c696c2012-07-07 22:56:40 +08001537 ci->driver = driver;
1538 pm_runtime_get_sync(&ci->gadget.dev);
1539 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1540 if (ci->vbus_active) {
Peter Chen571bb7a2013-03-30 02:46:43 +02001541 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
Richard Zhao26c696c2012-07-07 22:56:40 +08001542 hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301543 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001544 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301545 goto done;
1546 }
1547 }
1548
Richard Zhao26c696c2012-07-07 22:56:40 +08001549 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301550 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001551 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001552
1553 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001554 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001555 return retval;
1556}
David Lopoaa69a802008-11-17 14:14:51 -08001557
1558/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001559 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001560 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001561static int ci13xxx_stop(struct usb_gadget *gadget,
1562 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001563{
Richard Zhao26c696c2012-07-07 22:56:40 +08001564 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001565 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001566
Richard Zhao26c696c2012-07-07 22:56:40 +08001567 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001568
Richard Zhao26c696c2012-07-07 22:56:40 +08001569 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1570 ci->vbus_active) {
1571 hw_device_state(ci, 0);
1572 if (ci->platdata->notify_event)
1573 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301574 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001575 ci->driver = NULL;
1576 spin_unlock_irqrestore(&ci->lock, flags);
1577 _gadget_stop_activity(&ci->gadget);
1578 spin_lock_irqsave(&ci->lock, flags);
1579 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301580 }
David Lopoaa69a802008-11-17 14:14:51 -08001581
Richard Zhao26c696c2012-07-07 22:56:40 +08001582 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001583
David Lopoaa69a802008-11-17 14:14:51 -08001584 return 0;
1585}
David Lopoaa69a802008-11-17 14:14:51 -08001586
1587/******************************************************************************
1588 * BUS block
1589 *****************************************************************************/
1590/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001591 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001592 *
1593 * This function returns IRQ_HANDLED if the IRQ has been handled
1594 * It locks access to registers
1595 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001596static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001597{
David Lopoaa69a802008-11-17 14:14:51 -08001598 irqreturn_t retval;
1599 u32 intr;
1600
Richard Zhao26c696c2012-07-07 22:56:40 +08001601 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001602 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001603
Richard Zhao26c696c2012-07-07 22:56:40 +08001604 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301605
Richard Zhao26c696c2012-07-07 22:56:40 +08001606 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1607 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001608 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001609 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301610 return IRQ_NONE;
1611 }
1612 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001613 intr = hw_test_and_clear_intr_active(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001614 dbg_interrupt(intr);
David Lopoaa69a802008-11-17 14:14:51 -08001615
Alexander Shishkine443b332012-05-11 17:25:46 +03001616 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001617 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001618 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001619 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001620
David Lopoaa69a802008-11-17 14:14:51 -08001621 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001622 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001623 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001624 if (ci->suspended && ci->driver->resume) {
1625 spin_unlock(&ci->lock);
1626 ci->driver->resume(&ci->gadget);
1627 spin_lock(&ci->lock);
1628 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301629 }
David Lopoaa69a802008-11-17 14:14:51 -08001630 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001631
1632 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001633 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001634
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301635 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001636 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1637 ci->driver->suspend) {
1638 ci->suspended = 1;
1639 spin_unlock(&ci->lock);
1640 ci->driver->suspend(&ci->gadget);
1641 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301642 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301643 }
David Lopoaa69a802008-11-17 14:14:51 -08001644 retval = IRQ_HANDLED;
1645 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001646 retval = IRQ_NONE;
1647 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001648 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001649
1650 return retval;
1651}
1652
1653/**
1654 * udc_release: driver release function
1655 * @dev: device
1656 *
1657 * Currently does nothing
1658 */
1659static void udc_release(struct device *dev)
1660{
David Lopoaa69a802008-11-17 14:14:51 -08001661}
1662
1663/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001664 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001665 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001666 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001667static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001668{
Richard Zhao26c696c2012-07-07 22:56:40 +08001669 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001670 int retval = 0;
1671
Richard Zhao26c696c2012-07-07 22:56:40 +08001672 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001673
Richard Zhao26c696c2012-07-07 22:56:40 +08001674 ci->gadget.ops = &usb_gadget_ops;
1675 ci->gadget.speed = USB_SPEED_UNKNOWN;
1676 ci->gadget.max_speed = USB_SPEED_HIGH;
1677 ci->gadget.is_otg = 0;
1678 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001679
Richard Zhao26c696c2012-07-07 22:56:40 +08001680 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001681
Richard Zhao26c696c2012-07-07 22:56:40 +08001682 dev_set_name(&ci->gadget.dev, "gadget");
1683 ci->gadget.dev.dma_mask = dev->dma_mask;
1684 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1685 ci->gadget.dev.parent = dev;
1686 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001687
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001688 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001689 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001690 sizeof(struct ci13xxx_qh),
1691 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001692 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001693 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001694
Richard Zhao26c696c2012-07-07 22:56:40 +08001695 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001696 sizeof(struct ci13xxx_td),
1697 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001698 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001699 retval = -ENOMEM;
1700 goto free_qh_pool;
1701 }
1702
Richard Zhao26c696c2012-07-07 22:56:40 +08001703 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001704 if (retval)
1705 goto free_pools;
1706
Richard Zhao26c696c2012-07-07 22:56:40 +08001707 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301708
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001709 if (ci->global_phy)
1710 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301711
Richard Zhao26c696c2012-07-07 22:56:40 +08001712 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1713 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301714 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001715 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301716 }
1717 }
1718
Richard Zhao26c696c2012-07-07 22:56:40 +08001719 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1720 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301721 if (retval)
1722 goto put_transceiver;
1723 }
1724
Richard Zhao26c696c2012-07-07 22:56:40 +08001725 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301726 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001727 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301728 goto put_transceiver;
1729 }
David Lopoaa69a802008-11-17 14:14:51 -08001730
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001731 retval = dbg_create_files(ci->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301732 if (retval)
1733 goto unreg_device;
1734
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1736 retval = otg_set_peripheral(ci->transceiver->otg,
1737 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301738 if (retval)
1739 goto remove_dbg;
David Lopoaa69a802008-11-17 14:14:51 -08001740 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001741
Richard Zhao26c696c2012-07-07 22:56:40 +08001742 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001743 if (retval)
1744 goto remove_trans;
1745
Richard Zhao26c696c2012-07-07 22:56:40 +08001746 pm_runtime_no_callbacks(&ci->gadget.dev);
1747 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001748
David Lopoaa69a802008-11-17 14:14:51 -08001749 return retval;
1750
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001751remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001752 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001753 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001754 if (ci->global_phy)
1755 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001756 }
1757
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001758 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301759remove_dbg:
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001760 dbg_remove_files(ci->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301761unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001762 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301763put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001764 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001765 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001766destroy_eps:
1767 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001768free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001769 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001770free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001771 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001772 return retval;
1773}
1774
1775/**
1776 * udc_remove: parent remove must call this to remove UDC
1777 *
1778 * No interrupts active, the IRQ has been released
1779 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001780static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001781{
Richard Zhao26c696c2012-07-07 22:56:40 +08001782 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001783 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001784
Richard Zhao26c696c2012-07-07 22:56:40 +08001785 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001786
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001787 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001788
Richard Zhao26c696c2012-07-07 22:56:40 +08001789 dma_pool_destroy(ci->td_pool);
1790 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001791
Richard Zhao26c696c2012-07-07 22:56:40 +08001792 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1793 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001794 if (ci->global_phy)
1795 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301796 }
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001797 dbg_remove_files(ci->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001798 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001799 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001800 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001801}
David Lopoaa69a802008-11-17 14:14:51 -08001802
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001803/**
1804 * ci_hdrc_gadget_init - initialize device related bits
1805 * ci: the controller
1806 *
1807 * This function enables the gadget role, if the device is "device capable".
1808 */
1809int ci_hdrc_gadget_init(struct ci13xxx *ci)
1810{
1811 struct ci_role_driver *rdrv;
1812
1813 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1814 return -ENXIO;
1815
1816 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1817 if (!rdrv)
1818 return -ENOMEM;
1819
1820 rdrv->start = udc_start;
1821 rdrv->stop = udc_stop;
1822 rdrv->irq = udc_irq;
1823 rdrv->name = "gadget";
1824 ci->roles[CI_ROLE_GADGET] = rdrv;
1825
1826 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001827}