blob: 4688ab71bd275108b6b787a8c756661f6d83c345 [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>
16#include <linux/dma-mapping.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053017#include <linux/err.h>
David Lopoaa69a802008-11-17 14:14:51 -080018#include <linux/init.h>
Alexander Shishkin62bb84e2012-05-08 23:29:01 +030019#include <linux/platform_device.h>
20#include <linux/module.h>
David Lopoaa69a802008-11-17 14:14:51 -080021#include <linux/interrupt.h>
David Lopoaa69a802008-11-17 14:14:51 -080022#include <linux/io.h>
23#include <linux/irq.h>
24#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Pavankumar Kondetic0360192010-12-07 17:54:04 +053026#include <linux/pm_runtime.h>
David Lopoaa69a802008-11-17 14:14:51 -080027#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Pavankumar Kondetif01ef572010-12-07 17:54:02 +053029#include <linux/usb/otg.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030030#include <linux/usb/chipidea.h>
David Lopoaa69a802008-11-17 14:14:51 -080031
Alexander Shishkine443b332012-05-11 17:25:46 +030032#include "ci.h"
33#include "udc.h"
34#include "bits.h"
35#include "debug.h"
Michael Grzeschik954aad82011-10-10 18:38:06 +020036
David Lopoaa69a802008-11-17 14:14:51 -080037/* control endpoint description */
38static const struct usb_endpoint_descriptor
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053039ctrl_endpt_out_desc = {
David Lopoaa69a802008-11-17 14:14:51 -080040 .bLength = USB_DT_ENDPOINT_SIZE,
41 .bDescriptorType = USB_DT_ENDPOINT,
42
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +053043 .bEndpointAddress = USB_DIR_OUT,
44 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
45 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
46};
47
48static const struct usb_endpoint_descriptor
49ctrl_endpt_in_desc = {
50 .bLength = USB_DT_ENDPOINT_SIZE,
51 .bDescriptorType = USB_DT_ENDPOINT,
52
53 .bEndpointAddress = USB_DIR_IN,
David Lopoaa69a802008-11-17 14:14:51 -080054 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
55 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
56};
57
David Lopoaa69a802008-11-17 14:14:51 -080058/**
59 * hw_ep_bit: calculates the bit number
60 * @num: endpoint number
61 * @dir: endpoint direction
62 *
63 * This function returns bit number
64 */
65static inline int hw_ep_bit(int num, int dir)
66{
67 return num + (dir ? 16 : 0);
68}
69
Alexander Shishkine443b332012-05-11 17:25:46 +030070static inline int ep_to_bit(struct ci13xxx *udc, int n)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020071{
Alexander Shishkind3595d12012-05-08 23:28:58 +030072 int fill = 16 - udc->hw_ep_max / 2;
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020073
Alexander Shishkind3595d12012-05-08 23:28:58 +030074 if (n >= udc->hw_ep_max / 2)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +020075 n += fill;
76
77 return n;
78}
79
David Lopoaa69a802008-11-17 14:14:51 -080080/**
David Lopoaa69a802008-11-17 14:14:51 -080081 * hw_device_state: enables/disables interrupts & starts/stops device (execute
82 * without interruption)
83 * @dma: 0 => disable, !0 => enable and set dma engine
84 *
85 * This function returns an error code
86 */
Alexander Shishkind3595d12012-05-08 23:28:58 +030087static int hw_device_state(struct ci13xxx *udc, u32 dma)
David Lopoaa69a802008-11-17 14:14:51 -080088{
89 if (dma) {
Alexander Shishkin262c1632012-05-08 23:28:59 +030090 hw_write(udc, OP_ENDPTLISTADDR, ~0, dma);
David Lopoaa69a802008-11-17 14:14:51 -080091 /* interrupt, error, port change, reset, sleep/suspend */
Alexander Shishkin262c1632012-05-08 23:28:59 +030092 hw_write(udc, OP_USBINTR, ~0,
David Lopoaa69a802008-11-17 14:14:51 -080093 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
Alexander Shishkin262c1632012-05-08 23:28:59 +030094 hw_write(udc, OP_USBCMD, USBCMD_RS, USBCMD_RS);
David Lopoaa69a802008-11-17 14:14:51 -080095 } else {
Alexander Shishkin262c1632012-05-08 23:28:59 +030096 hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
97 hw_write(udc, OP_USBINTR, ~0, 0);
David Lopoaa69a802008-11-17 14:14:51 -080098 }
99 return 0;
100}
101
102/**
103 * hw_ep_flush: flush endpoint fifo (execute without interruption)
104 * @num: endpoint number
105 * @dir: endpoint direction
106 *
107 * This function returns an error code
108 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300109static int hw_ep_flush(struct ci13xxx *udc, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800110{
111 int n = hw_ep_bit(num, dir);
112
113 do {
114 /* flush any pending transfer */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300115 hw_write(udc, OP_ENDPTFLUSH, BIT(n), BIT(n));
116 while (hw_read(udc, OP_ENDPTFLUSH, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800117 cpu_relax();
Alexander Shishkin262c1632012-05-08 23:28:59 +0300118 } while (hw_read(udc, OP_ENDPTSTAT, BIT(n)));
David Lopoaa69a802008-11-17 14:14:51 -0800119
120 return 0;
121}
122
123/**
124 * hw_ep_disable: disables endpoint (execute without interruption)
125 * @num: endpoint number
126 * @dir: endpoint direction
127 *
128 * This function returns an error code
129 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300130static int hw_ep_disable(struct ci13xxx *udc, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800131{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300132 hw_ep_flush(udc, num, dir);
Alexander Shishkin262c1632012-05-08 23:28:59 +0300133 hw_write(udc, OP_ENDPTCTRL + num,
Alexander Shishkind3595d12012-05-08 23:28:58 +0300134 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800135 return 0;
136}
137
138/**
139 * hw_ep_enable: enables endpoint (execute without interruption)
140 * @num: endpoint number
141 * @dir: endpoint direction
142 * @type: endpoint type
143 *
144 * This function returns an error code
145 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300146static int hw_ep_enable(struct ci13xxx *udc, int num, int dir, int type)
David Lopoaa69a802008-11-17 14:14:51 -0800147{
148 u32 mask, data;
149
150 if (dir) {
151 mask = ENDPTCTRL_TXT; /* type */
152 data = type << ffs_nr(mask);
153
154 mask |= ENDPTCTRL_TXS; /* unstall */
155 mask |= ENDPTCTRL_TXR; /* reset data toggle */
156 data |= ENDPTCTRL_TXR;
157 mask |= ENDPTCTRL_TXE; /* enable */
158 data |= ENDPTCTRL_TXE;
159 } else {
160 mask = ENDPTCTRL_RXT; /* type */
161 data = type << ffs_nr(mask);
162
163 mask |= ENDPTCTRL_RXS; /* unstall */
164 mask |= ENDPTCTRL_RXR; /* reset data toggle */
165 data |= ENDPTCTRL_RXR;
166 mask |= ENDPTCTRL_RXE; /* enable */
167 data |= ENDPTCTRL_RXE;
168 }
Alexander Shishkin262c1632012-05-08 23:28:59 +0300169 hw_write(udc, OP_ENDPTCTRL + num, mask, data);
David Lopoaa69a802008-11-17 14:14:51 -0800170 return 0;
171}
172
173/**
174 * hw_ep_get_halt: return endpoint halt status
175 * @num: endpoint number
176 * @dir: endpoint direction
177 *
178 * This function returns 1 if endpoint halted
179 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300180static int hw_ep_get_halt(struct ci13xxx *udc, int num, int dir)
David Lopoaa69a802008-11-17 14:14:51 -0800181{
182 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
183
Alexander Shishkin262c1632012-05-08 23:28:59 +0300184 return hw_read(udc, OP_ENDPTCTRL + num, mask) ? 1 : 0;
David Lopoaa69a802008-11-17 14:14:51 -0800185}
186
187/**
David Lopoaa69a802008-11-17 14:14:51 -0800188 * hw_test_and_clear_setup_status: test & clear setup status (execute without
189 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200190 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800191 *
192 * This function returns setup status
193 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300194static int hw_test_and_clear_setup_status(struct ci13xxx *udc, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800195{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300196 n = ep_to_bit(udc, n);
Alexander Shishkin262c1632012-05-08 23:28:59 +0300197 return hw_test_and_clear(udc, OP_ENDPTSETUPSTAT, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800198}
199
200/**
201 * hw_ep_prime: primes endpoint (execute without interruption)
202 * @num: endpoint number
203 * @dir: endpoint direction
204 * @is_ctrl: true if control endpoint
205 *
206 * This function returns an error code
207 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300208static int hw_ep_prime(struct ci13xxx *udc, int num, int dir, int is_ctrl)
David Lopoaa69a802008-11-17 14:14:51 -0800209{
210 int n = hw_ep_bit(num, dir);
211
Alexander Shishkin262c1632012-05-08 23:28:59 +0300212 if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800213 return -EAGAIN;
214
Alexander Shishkin262c1632012-05-08 23:28:59 +0300215 hw_write(udc, OP_ENDPTPRIME, BIT(n), BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800216
Alexander Shishkin262c1632012-05-08 23:28:59 +0300217 while (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
David Lopoaa69a802008-11-17 14:14:51 -0800218 cpu_relax();
Alexander Shishkin262c1632012-05-08 23:28:59 +0300219 if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
David Lopoaa69a802008-11-17 14:14:51 -0800220 return -EAGAIN;
221
222 /* status shoult be tested according with manual but it doesn't work */
223 return 0;
224}
225
226/**
227 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
228 * without interruption)
229 * @num: endpoint number
230 * @dir: endpoint direction
231 * @value: true => stall, false => unstall
232 *
233 * This function returns an error code
234 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300235static int hw_ep_set_halt(struct ci13xxx *udc, int num, int dir, int value)
David Lopoaa69a802008-11-17 14:14:51 -0800236{
237 if (value != 0 && value != 1)
238 return -EINVAL;
239
240 do {
Alexander Shishkin262c1632012-05-08 23:28:59 +0300241 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
David Lopoaa69a802008-11-17 14:14:51 -0800242 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
243 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
244
245 /* data toggle - reserved for EP0 but it's in ESS */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300246 hw_write(udc, reg, mask_xs|mask_xr,
247 value ? mask_xs : mask_xr);
Alexander Shishkind3595d12012-05-08 23:28:58 +0300248 } while (value != hw_ep_get_halt(udc, num, dir));
David Lopoaa69a802008-11-17 14:14:51 -0800249
250 return 0;
251}
252
253/**
David Lopoaa69a802008-11-17 14:14:51 -0800254 * hw_is_port_high_speed: test if port is high speed
255 *
256 * This function returns true if high speed port
257 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300258static int hw_port_is_high_speed(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800259{
Alexander Shishkin262c1632012-05-08 23:28:59 +0300260 return udc->hw_bank.lpm ? hw_read(udc, OP_DEVLC, DEVLC_PSPD) :
261 hw_read(udc, OP_PORTSC, PORTSC_HSP);
David Lopoaa69a802008-11-17 14:14:51 -0800262}
263
264/**
David Lopoaa69a802008-11-17 14:14:51 -0800265 * hw_read_intr_enable: returns interrupt enable register
266 *
267 * This function returns register data
268 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300269static u32 hw_read_intr_enable(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800270{
Alexander Shishkin262c1632012-05-08 23:28:59 +0300271 return hw_read(udc, OP_USBINTR, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800272}
273
274/**
275 * hw_read_intr_status: returns interrupt status register
276 *
277 * This function returns register data
278 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300279static u32 hw_read_intr_status(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800280{
Alexander Shishkin262c1632012-05-08 23:28:59 +0300281 return hw_read(udc, OP_USBSTS, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800282}
283
284/**
David Lopoaa69a802008-11-17 14:14:51 -0800285 * hw_test_and_clear_complete: test & clear complete status (execute without
286 * interruption)
Marc Kleine-Buddedd39c352011-10-10 18:38:10 +0200287 * @n: endpoint number
David Lopoaa69a802008-11-17 14:14:51 -0800288 *
289 * This function returns complete status
290 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300291static int hw_test_and_clear_complete(struct ci13xxx *udc, int n)
David Lopoaa69a802008-11-17 14:14:51 -0800292{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300293 n = ep_to_bit(udc, n);
Alexander Shishkin262c1632012-05-08 23:28:59 +0300294 return hw_test_and_clear(udc, OP_ENDPTCOMPLETE, BIT(n));
David Lopoaa69a802008-11-17 14:14:51 -0800295}
296
297/**
298 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
299 * without interruption)
300 *
301 * This function returns active interrutps
302 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300303static u32 hw_test_and_clear_intr_active(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800304{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300305 u32 reg = hw_read_intr_status(udc) & hw_read_intr_enable(udc);
David Lopoaa69a802008-11-17 14:14:51 -0800306
Alexander Shishkin262c1632012-05-08 23:28:59 +0300307 hw_write(udc, OP_USBSTS, ~0, reg);
David Lopoaa69a802008-11-17 14:14:51 -0800308 return reg;
309}
310
311/**
312 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
313 * interruption)
314 *
315 * This function returns guard value
316 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300317static int hw_test_and_clear_setup_guard(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800318{
Alexander Shishkin262c1632012-05-08 23:28:59 +0300319 return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800320}
321
322/**
323 * hw_test_and_set_setup_guard: test & set setup guard (execute without
324 * interruption)
325 *
326 * This function returns guard value
327 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300328static int hw_test_and_set_setup_guard(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800329{
Alexander Shishkin262c1632012-05-08 23:28:59 +0300330 return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800331}
332
333/**
334 * hw_usb_set_address: configures USB address (execute without interruption)
335 * @value: new USB address
336 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300337 * This function explicitly sets the address, without the "USBADRA" (advance)
338 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800339 */
Alexander Shishkinef15e542012-05-11 17:25:43 +0300340static void hw_usb_set_address(struct ci13xxx *udc, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800341{
Alexander Shishkinef15e542012-05-11 17:25:43 +0300342 hw_write(udc, OP_DEVICEADDR, DEVICEADDR_USBADR,
343 value << ffs_nr(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800344}
345
346/**
347 * hw_usb_reset: restart device after a bus reset (execute without
348 * interruption)
349 *
350 * This function returns an error code
351 */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300352static int hw_usb_reset(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800353{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300354 hw_usb_set_address(udc, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800355
356 /* ESS flushes only at end?!? */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300357 hw_write(udc, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800358
359 /* clear setup token semaphores */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300360 hw_write(udc, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800361
362 /* clear complete status */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300363 hw_write(udc, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800364
365 /* wait until all bits cleared */
Alexander Shishkin262c1632012-05-08 23:28:59 +0300366 while (hw_read(udc, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800367 udelay(10); /* not RTOS friendly */
368
369 /* reset all endpoints ? */
370
371 /* reset internal status and wait for further instructions
372 no need to verify the port reset status (ESS does it) */
373
374 return 0;
375}
376
377/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800378 * UTIL block
379 *****************************************************************************/
380/**
381 * _usb_addr: calculates endpoint address from direction & number
382 * @ep: endpoint
383 */
384static inline u8 _usb_addr(struct ci13xxx_ep *ep)
385{
386 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
387}
388
389/**
390 * _hardware_queue: configures a request at hardware level
391 * @gadget: gadget
392 * @mEp: endpoint
393 *
394 * This function returns an error code
395 */
396static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
397{
Alexander Shishkind3595d12012-05-08 23:28:58 +0300398 struct ci13xxx *udc = mEp->udc;
David Lopoaa69a802008-11-17 14:14:51 -0800399 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530400 int ret = 0;
401 unsigned length = mReq->req.length;
David Lopoaa69a802008-11-17 14:14:51 -0800402
David Lopoaa69a802008-11-17 14:14:51 -0800403 /* don't queue twice */
404 if (mReq->req.status == -EALREADY)
405 return -EALREADY;
406
David Lopoaa69a802008-11-17 14:14:51 -0800407 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800408
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530409 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
410 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
411 &mReq->zdma);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300412 if (mReq->zptr == NULL)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530413 return -ENOMEM;
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300414
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530415 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
416 mReq->zptr->next = TD_TERMINATE;
417 mReq->zptr->token = TD_STATUS_ACTIVE;
418 if (!mReq->req.no_interrupt)
419 mReq->zptr->token |= TD_IOC;
420 }
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300421 ret = usb_gadget_map_request(&udc->gadget, &mReq->req, mEp->dir);
422 if (ret)
423 return ret;
424
David Lopoaa69a802008-11-17 14:14:51 -0800425 /*
426 * TD configuration
427 * TODO - handle requests which spawns into several TDs
428 */
429 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530430 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800431 mReq->ptr->token &= TD_TOTAL_BYTES;
David Lopoaa69a802008-11-17 14:14:51 -0800432 mReq->ptr->token |= TD_STATUS_ACTIVE;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530433 if (mReq->zptr) {
434 mReq->ptr->next = mReq->zdma;
435 } else {
436 mReq->ptr->next = TD_TERMINATE;
437 if (!mReq->req.no_interrupt)
438 mReq->ptr->token |= TD_IOC;
439 }
David Lopoaa69a802008-11-17 14:14:51 -0800440 mReq->ptr->page[0] = mReq->req.dma;
441 for (i = 1; i < 5; i++)
442 mReq->ptr->page[i] =
Artem Leonenko0a313c42010-12-14 23:47:06 -0800443 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
David Lopoaa69a802008-11-17 14:14:51 -0800444
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530445 if (!list_empty(&mEp->qh.queue)) {
446 struct ci13xxx_req *mReqPrev;
447 int n = hw_ep_bit(mEp->num, mEp->dir);
448 int tmp_stat;
449
450 mReqPrev = list_entry(mEp->qh.queue.prev,
451 struct ci13xxx_req, queue);
452 if (mReqPrev->zptr)
453 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
454 else
455 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
456 wmb();
Alexander Shishkin262c1632012-05-08 23:28:59 +0300457 if (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530458 goto done;
459 do {
Alexander Shishkin262c1632012-05-08 23:28:59 +0300460 hw_write(udc, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
461 tmp_stat = hw_read(udc, OP_ENDPTSTAT, BIT(n));
462 } while (!hw_read(udc, OP_USBCMD, USBCMD_ATDTW));
463 hw_write(udc, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530464 if (tmp_stat)
465 goto done;
466 }
467
468 /* QH configuration */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530469 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
470 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530471 mEp->qh.ptr->cap |= QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -0800472
473 wmb(); /* synchronize before ep prime */
474
Alexander Shishkind3595d12012-05-08 23:28:58 +0300475 ret = hw_ep_prime(udc, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800476 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530477done:
478 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800479}
480
481/**
482 * _hardware_dequeue: handles a request at hardware level
483 * @gadget: gadget
484 * @mEp: endpoint
485 *
486 * This function returns an error code
487 */
488static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
489{
David Lopoaa69a802008-11-17 14:14:51 -0800490 if (mReq->req.status != -EALREADY)
491 return -EINVAL;
492
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530493 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
494 return -EBUSY;
495
496 if (mReq->zptr) {
497 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
498 return -EBUSY;
499 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
500 mReq->zptr = NULL;
501 }
David Lopoaa69a802008-11-17 14:14:51 -0800502
503 mReq->req.status = 0;
504
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300505 usb_gadget_unmap_request(&mEp->udc->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800506
507 mReq->req.status = mReq->ptr->token & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530508 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800509 mReq->req.status = -1;
510 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
511 mReq->req.status = -1;
512 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
513 mReq->req.status = -1;
514
515 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
516 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
517 mReq->req.actual = mReq->req.length - mReq->req.actual;
518 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
519
520 return mReq->req.actual;
521}
522
523/**
524 * _ep_nuke: dequeues all endpoint requests
525 * @mEp: endpoint
526 *
527 * This function returns an error code
528 * Caller must hold lock
529 */
530static int _ep_nuke(struct ci13xxx_ep *mEp)
531__releases(mEp->lock)
532__acquires(mEp->lock)
533{
David Lopoaa69a802008-11-17 14:14:51 -0800534 if (mEp == NULL)
535 return -EINVAL;
536
Alexander Shishkind3595d12012-05-08 23:28:58 +0300537 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800538
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530539 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800540
541 /* pop oldest request */
542 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530543 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800544 struct ci13xxx_req, queue);
545 list_del_init(&mReq->queue);
546 mReq->req.status = -ESHUTDOWN;
547
Artem Leonenko7c25a822010-12-14 23:46:55 -0800548 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800549 spin_unlock(mEp->lock);
550 mReq->req.complete(&mEp->ep, &mReq->req);
551 spin_lock(mEp->lock);
552 }
553 }
554 return 0;
555}
556
557/**
558 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
559 * @gadget: gadget
560 *
561 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800562 */
563static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800564{
565 struct usb_ep *ep;
566 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530567 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800568
Alexander Shishkind3595d12012-05-08 23:28:58 +0300569 spin_lock_irqsave(&udc->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530570 udc->gadget.speed = USB_SPEED_UNKNOWN;
571 udc->remote_wakeup = 0;
572 udc->suspended = 0;
Alexander Shishkind3595d12012-05-08 23:28:58 +0300573 spin_unlock_irqrestore(&udc->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530574
David Lopoaa69a802008-11-17 14:14:51 -0800575 /* flush all endpoints */
576 gadget_for_each_ep(ep, gadget) {
577 usb_ep_fifo_flush(ep);
578 }
Alexander Shishkind36ade62012-05-04 16:47:15 +0300579 usb_ep_fifo_flush(&udc->ep0out->ep);
580 usb_ep_fifo_flush(&udc->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800581
Alexander Shishkin1f339d82012-05-08 23:29:04 +0300582 if (udc->driver)
583 udc->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800584
585 /* make sure to disable all endpoints */
586 gadget_for_each_ep(ep, gadget) {
587 usb_ep_disable(ep);
588 }
David Lopoaa69a802008-11-17 14:14:51 -0800589
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530590 if (udc->status != NULL) {
Alexander Shishkind36ade62012-05-04 16:47:15 +0300591 usb_ep_free_request(&udc->ep0in->ep, udc->status);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530592 udc->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800593 }
594
David Lopoaa69a802008-11-17 14:14:51 -0800595 return 0;
596}
597
598/******************************************************************************
599 * ISR block
600 *****************************************************************************/
601/**
602 * isr_reset_handler: USB reset interrupt handler
603 * @udc: UDC device
604 *
605 * This function resets USB engine after a bus reset occurred
606 */
607static void isr_reset_handler(struct ci13xxx *udc)
608__releases(udc->lock)
609__acquires(udc->lock)
610{
David Lopoaa69a802008-11-17 14:14:51 -0800611 int retval;
612
David Lopoaa69a802008-11-17 14:14:51 -0800613 dbg_event(0xFF, "BUS RST", 0);
614
Alexander Shishkind3595d12012-05-08 23:28:58 +0300615 spin_unlock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800616 retval = _gadget_stop_activity(&udc->gadget);
617 if (retval)
618 goto done;
619
Alexander Shishkind3595d12012-05-08 23:28:58 +0300620 retval = hw_usb_reset(udc);
David Lopoaa69a802008-11-17 14:14:51 -0800621 if (retval)
622 goto done;
623
Alexander Shishkind36ade62012-05-04 16:47:15 +0300624 udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530625 if (udc->status == NULL)
626 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530627
Michael Grzeschikb9322252012-05-11 17:25:50 +0300628done:
Alexander Shishkind3595d12012-05-08 23:28:58 +0300629 spin_lock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800630
David Lopoaa69a802008-11-17 14:14:51 -0800631 if (retval)
Alexander Shishkin0f089092012-05-08 23:29:02 +0300632 dev_err(udc->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800633}
634
635/**
636 * isr_get_status_complete: get_status request complete function
637 * @ep: endpoint
638 * @req: request handled
639 *
640 * Caller must release lock
641 */
642static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
643{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300644 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800645 return;
David Lopoaa69a802008-11-17 14:14:51 -0800646
647 kfree(req->buf);
648 usb_ep_free_request(ep, req);
649}
650
651/**
652 * isr_get_status_response: get_status request response
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530653 * @udc: udc struct
David Lopoaa69a802008-11-17 14:14:51 -0800654 * @setup: setup request packet
655 *
656 * This function returns an error code
657 */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530658static int isr_get_status_response(struct ci13xxx *udc,
David Lopoaa69a802008-11-17 14:14:51 -0800659 struct usb_ctrlrequest *setup)
660__releases(mEp->lock)
661__acquires(mEp->lock)
662{
Alexander Shishkind36ade62012-05-04 16:47:15 +0300663 struct ci13xxx_ep *mEp = udc->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800664 struct usb_request *req = NULL;
665 gfp_t gfp_flags = GFP_ATOMIC;
666 int dir, num, retval;
667
David Lopoaa69a802008-11-17 14:14:51 -0800668 if (mEp == NULL || setup == NULL)
669 return -EINVAL;
670
671 spin_unlock(mEp->lock);
672 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
673 spin_lock(mEp->lock);
674 if (req == NULL)
675 return -ENOMEM;
676
677 req->complete = isr_get_status_complete;
678 req->length = 2;
679 req->buf = kzalloc(req->length, gfp_flags);
680 if (req->buf == NULL) {
681 retval = -ENOMEM;
682 goto err_free_req;
683 }
684
685 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530686 /* Assume that device is bus powered for now. */
Alexander Shishkin1f339d82012-05-08 23:29:04 +0300687 *(u16 *)req->buf = udc->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800688 retval = 0;
689 } else if ((setup->bRequestType & USB_RECIP_MASK) \
690 == USB_RECIP_ENDPOINT) {
691 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
692 TX : RX;
693 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Alexander Shishkind3595d12012-05-08 23:28:58 +0300694 *(u16 *)req->buf = hw_ep_get_halt(udc, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800695 }
696 /* else do nothing; reserved for future use */
697
698 spin_unlock(mEp->lock);
699 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
700 spin_lock(mEp->lock);
701 if (retval)
702 goto err_free_buf;
703
704 return 0;
705
706 err_free_buf:
707 kfree(req->buf);
708 err_free_req:
709 spin_unlock(mEp->lock);
710 usb_ep_free_request(&mEp->ep, req);
711 spin_lock(mEp->lock);
712 return retval;
713}
714
715/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530716 * isr_setup_status_complete: setup_status request complete function
717 * @ep: endpoint
718 * @req: request handled
719 *
720 * Caller must release lock. Put the port in test mode if test mode
721 * feature is selected.
722 */
723static void
724isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
725{
726 struct ci13xxx *udc = req->context;
727 unsigned long flags;
728
Alexander Shishkinef15e542012-05-11 17:25:43 +0300729 if (udc->setaddr) {
730 hw_usb_set_address(udc, udc->address);
731 udc->setaddr = false;
732 }
733
Alexander Shishkind3595d12012-05-08 23:28:58 +0300734 spin_lock_irqsave(&udc->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530735 if (udc->test_mode)
Alexander Shishkind3595d12012-05-08 23:28:58 +0300736 hw_port_test_set(udc, udc->test_mode);
737 spin_unlock_irqrestore(&udc->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530738}
739
740/**
David Lopoaa69a802008-11-17 14:14:51 -0800741 * isr_setup_status_phase: queues the status phase of a setup transation
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530742 * @udc: udc struct
David Lopoaa69a802008-11-17 14:14:51 -0800743 *
744 * This function returns an error code
745 */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530746static int isr_setup_status_phase(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -0800747__releases(mEp->lock)
748__acquires(mEp->lock)
749{
750 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530751 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800752
Alexander Shishkind36ade62012-05-04 16:47:15 +0300753 mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530754 udc->status->context = udc;
755 udc->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800756
757 spin_unlock(mEp->lock);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530758 retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800759 spin_lock(mEp->lock);
760
761 return retval;
762}
763
764/**
765 * isr_tr_complete_low: transaction complete low level handler
766 * @mEp: endpoint
767 *
768 * This function returns an error code
769 * Caller must hold lock
770 */
771static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
772__releases(mEp->lock)
773__acquires(mEp->lock)
774{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530775 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530776 struct ci13xxx_ep *mEpTemp = mEp;
Pavankumar Kondeti986b11b2011-05-02 11:56:29 +0530777 int uninitialized_var(retval);
David Lopoaa69a802008-11-17 14:14:51 -0800778
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530779 if (list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -0800780 return -EINVAL;
781
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530782 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
783 queue) {
784 retval = _hardware_dequeue(mEp, mReq);
785 if (retval < 0)
786 break;
787 list_del_init(&mReq->queue);
788 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
789 if (mReq->req.complete != NULL) {
790 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530791 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
792 mReq->req.length)
Alexander Shishkin1f339d82012-05-08 23:29:04 +0300793 mEpTemp = mEp->udc->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530794 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530795 spin_lock(mEp->lock);
796 }
797 }
David Lopoaa69a802008-11-17 14:14:51 -0800798
Pavankumar Kondetief907482011-05-02 11:56:27 +0530799 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530800 retval = 0;
801 if (retval < 0)
David Lopoaa69a802008-11-17 14:14:51 -0800802 dbg_event(_usb_addr(mEp), "DONE", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800803
David Lopoaa69a802008-11-17 14:14:51 -0800804 return retval;
805}
806
807/**
808 * isr_tr_complete_handler: transaction complete interrupt handler
809 * @udc: UDC descriptor
810 *
811 * This function handles traffic events
812 */
813static void isr_tr_complete_handler(struct ci13xxx *udc)
814__releases(udc->lock)
815__acquires(udc->lock)
816{
817 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530818 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800819
Alexander Shishkind3595d12012-05-08 23:28:58 +0300820 for (i = 0; i < udc->hw_ep_max; i++) {
David Lopoaa69a802008-11-17 14:14:51 -0800821 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530822 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800823 struct usb_ctrlrequest req;
824
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200825 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800826 continue; /* not configured */
827
Alexander Shishkind3595d12012-05-08 23:28:58 +0300828 if (hw_test_and_clear_complete(udc, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800829 err = isr_tr_complete_low(mEp);
830 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
831 if (err > 0) /* needs status phase */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530832 err = isr_setup_status_phase(udc);
David Lopoaa69a802008-11-17 14:14:51 -0800833 if (err < 0) {
834 dbg_event(_usb_addr(mEp),
835 "ERROR", err);
Alexander Shishkind3595d12012-05-08 23:28:58 +0300836 spin_unlock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800837 if (usb_ep_set_halt(&mEp->ep))
Alexander Shishkin0f089092012-05-08 23:29:02 +0300838 dev_err(udc->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700839 "error: ep_set_halt\n");
Alexander Shishkind3595d12012-05-08 23:28:58 +0300840 spin_lock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800841 }
842 }
843 }
844
845 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Alexander Shishkind3595d12012-05-08 23:28:58 +0300846 !hw_test_and_clear_setup_status(udc, i))
David Lopoaa69a802008-11-17 14:14:51 -0800847 continue;
848
849 if (i != 0) {
Alexander Shishkin0f089092012-05-08 23:29:02 +0300850 dev_warn(udc->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800851 continue;
852 }
853
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530854 /*
855 * Flush data and handshake transactions of previous
856 * setup packet.
857 */
Alexander Shishkind36ade62012-05-04 16:47:15 +0300858 _ep_nuke(udc->ep0out);
859 _ep_nuke(udc->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530860
David Lopoaa69a802008-11-17 14:14:51 -0800861 /* read_setup_packet */
862 do {
Alexander Shishkind3595d12012-05-08 23:28:58 +0300863 hw_test_and_set_setup_guard(udc);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530864 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Alexander Shishkind3595d12012-05-08 23:28:58 +0300865 } while (!hw_test_and_clear_setup_guard(udc));
David Lopoaa69a802008-11-17 14:14:51 -0800866
867 type = req.bRequestType;
868
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530869 udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800870
871 dbg_setup(_usb_addr(mEp), &req);
872
873 switch (req.bRequest) {
874 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530875 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
876 le16_to_cpu(req.wValue) ==
877 USB_ENDPOINT_HALT) {
878 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800879 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530880 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530881 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530882 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530883 if (dir) /* TX */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300884 num += udc->hw_ep_max/2;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530885 if (!udc->ci13xxx_ep[num].wedge) {
Alexander Shishkind3595d12012-05-08 23:28:58 +0300886 spin_unlock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530887 err = usb_ep_clear_halt(
888 &udc->ci13xxx_ep[num].ep);
Alexander Shishkind3595d12012-05-08 23:28:58 +0300889 spin_lock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530890 if (err)
891 break;
892 }
893 err = isr_setup_status_phase(udc);
894 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
895 le16_to_cpu(req.wValue) ==
896 USB_DEVICE_REMOTE_WAKEUP) {
897 if (req.wLength != 0)
898 break;
899 udc->remote_wakeup = 0;
900 err = isr_setup_status_phase(udc);
901 } else {
902 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800903 }
David Lopoaa69a802008-11-17 14:14:51 -0800904 break;
905 case USB_REQ_GET_STATUS:
906 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
907 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
908 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
909 goto delegate;
910 if (le16_to_cpu(req.wLength) != 2 ||
911 le16_to_cpu(req.wValue) != 0)
912 break;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530913 err = isr_get_status_response(udc, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800914 break;
915 case USB_REQ_SET_ADDRESS:
916 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
917 goto delegate;
918 if (le16_to_cpu(req.wLength) != 0 ||
919 le16_to_cpu(req.wIndex) != 0)
920 break;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300921 udc->address = (u8)le16_to_cpu(req.wValue);
922 udc->setaddr = true;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530923 err = isr_setup_status_phase(udc);
David Lopoaa69a802008-11-17 14:14:51 -0800924 break;
925 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530926 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
927 le16_to_cpu(req.wValue) ==
928 USB_ENDPOINT_HALT) {
929 if (req.wLength != 0)
930 break;
931 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530932 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530933 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530934 if (dir) /* TX */
Alexander Shishkind3595d12012-05-08 23:28:58 +0300935 num += udc->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800936
Alexander Shishkind3595d12012-05-08 23:28:58 +0300937 spin_unlock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530938 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
Alexander Shishkind3595d12012-05-08 23:28:58 +0300939 spin_lock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530940 if (!err)
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530941 isr_setup_status_phase(udc);
942 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530943 if (req.wLength != 0)
944 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530945 switch (le16_to_cpu(req.wValue)) {
946 case USB_DEVICE_REMOTE_WAKEUP:
947 udc->remote_wakeup = 1;
948 err = isr_setup_status_phase(udc);
949 break;
950 case USB_DEVICE_TEST_MODE:
951 tmode = le16_to_cpu(req.wIndex) >> 8;
952 switch (tmode) {
953 case TEST_J:
954 case TEST_K:
955 case TEST_SE0_NAK:
956 case TEST_PACKET:
957 case TEST_FORCE_EN:
958 udc->test_mode = tmode;
959 err = isr_setup_status_phase(
960 udc);
961 break;
962 default:
963 break;
964 }
965 default:
966 goto delegate;
967 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530968 } else {
969 goto delegate;
970 }
David Lopoaa69a802008-11-17 14:14:51 -0800971 break;
972 default:
973delegate:
974 if (req.wLength == 0) /* no data phase */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530975 udc->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800976
Alexander Shishkind3595d12012-05-08 23:28:58 +0300977 spin_unlock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800978 err = udc->driver->setup(&udc->gadget, &req);
Alexander Shishkind3595d12012-05-08 23:28:58 +0300979 spin_lock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800980 break;
981 }
982
983 if (err < 0) {
984 dbg_event(_usb_addr(mEp), "ERROR", err);
985
Alexander Shishkind3595d12012-05-08 23:28:58 +0300986 spin_unlock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800987 if (usb_ep_set_halt(&mEp->ep))
Alexander Shishkin0f089092012-05-08 23:29:02 +0300988 dev_err(udc->dev, "error: ep_set_halt\n");
Alexander Shishkind3595d12012-05-08 23:28:58 +0300989 spin_lock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800990 }
991 }
992}
993
994/******************************************************************************
995 * ENDPT block
996 *****************************************************************************/
997/**
998 * ep_enable: configure endpoint, making it usable
999 *
1000 * Check usb_ep_enable() at "usb_gadget.h" for details
1001 */
1002static int ep_enable(struct usb_ep *ep,
1003 const struct usb_endpoint_descriptor *desc)
1004{
1005 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301006 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001007 unsigned long flags;
1008
David Lopoaa69a802008-11-17 14:14:51 -08001009 if (ep == NULL || desc == NULL)
1010 return -EINVAL;
1011
1012 spin_lock_irqsave(mEp->lock, flags);
1013
1014 /* only internal SW should enable ctrl endpts */
1015
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001016 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001017
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301018 if (!list_empty(&mEp->qh.queue))
Alexander Shishkin0f089092012-05-08 23:29:02 +03001019 dev_warn(mEp->udc->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001020
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001021 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1022 mEp->num = usb_endpoint_num(desc);
1023 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001024
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001025 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001026
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301027 dbg_event(_usb_addr(mEp), "ENABLE", 0);
David Lopoaa69a802008-11-17 14:14:51 -08001028
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301029 mEp->qh.ptr->cap = 0;
David Lopof23e6492009-04-16 14:35:24 -07001030
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301031 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1032 mEp->qh.ptr->cap |= QH_IOS;
1033 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1034 mEp->qh.ptr->cap &= ~QH_MULT;
1035 else
1036 mEp->qh.ptr->cap &= ~QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -08001037
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301038 mEp->qh.ptr->cap |=
1039 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1040 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001041
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301042 /*
1043 * Enable endpoints in the HW other than ep0 as ep0
1044 * is always enabled
1045 */
1046 if (mEp->num)
Alexander Shishkind3595d12012-05-08 23:28:58 +03001047 retval |= hw_ep_enable(mEp->udc, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001048
1049 spin_unlock_irqrestore(mEp->lock, flags);
1050 return retval;
1051}
1052
1053/**
1054 * ep_disable: endpoint is no longer usable
1055 *
1056 * Check usb_ep_disable() at "usb_gadget.h" for details
1057 */
1058static int ep_disable(struct usb_ep *ep)
1059{
1060 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1061 int direction, retval = 0;
1062 unsigned long flags;
1063
David Lopoaa69a802008-11-17 14:14:51 -08001064 if (ep == NULL)
1065 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001066 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001067 return -EBUSY;
1068
1069 spin_lock_irqsave(mEp->lock, flags);
1070
1071 /* only internal SW should disable ctrl endpts */
1072
1073 direction = mEp->dir;
1074 do {
1075 dbg_event(_usb_addr(mEp), "DISABLE", 0);
1076
1077 retval |= _ep_nuke(mEp);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001078 retval |= hw_ep_disable(mEp->udc, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001079
1080 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1081 mEp->dir = (mEp->dir == TX) ? RX : TX;
1082
1083 } while (mEp->dir != direction);
1084
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001085 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001086
1087 spin_unlock_irqrestore(mEp->lock, flags);
1088 return retval;
1089}
1090
1091/**
1092 * ep_alloc_request: allocate a request object to use with this endpoint
1093 *
1094 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1095 */
1096static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1097{
1098 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1099 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001100
Alexander Shishkin0f089092012-05-08 23:29:02 +03001101 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001102 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001103
David Lopoaa69a802008-11-17 14:14:51 -08001104 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1105 if (mReq != NULL) {
1106 INIT_LIST_HEAD(&mReq->queue);
1107
1108 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1109 &mReq->dma);
1110 if (mReq->ptr == NULL) {
1111 kfree(mReq);
1112 mReq = NULL;
1113 }
1114 }
1115
1116 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
1117
David Lopoaa69a802008-11-17 14:14:51 -08001118 return (mReq == NULL) ? NULL : &mReq->req;
1119}
1120
1121/**
1122 * ep_free_request: frees a request object
1123 *
1124 * Check usb_ep_free_request() at "usb_gadget.h" for details
1125 */
1126static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1127{
1128 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1129 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1130 unsigned long flags;
1131
David Lopoaa69a802008-11-17 14:14:51 -08001132 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001133 return;
1134 } else if (!list_empty(&mReq->queue)) {
Alexander Shishkin0f089092012-05-08 23:29:02 +03001135 dev_err(mEp->udc->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001136 return;
1137 }
1138
1139 spin_lock_irqsave(mEp->lock, flags);
1140
1141 if (mReq->ptr)
1142 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1143 kfree(mReq);
1144
1145 dbg_event(_usb_addr(mEp), "FREE", 0);
1146
1147 spin_unlock_irqrestore(mEp->lock, flags);
1148}
1149
1150/**
1151 * ep_queue: queues (submits) an I/O request to an endpoint
1152 *
1153 * Check usb_ep_queue()* at usb_gadget.h" for details
1154 */
1155static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1156 gfp_t __maybe_unused gfp_flags)
1157{
1158 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1159 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001160 struct ci13xxx *udc = mEp->udc;
David Lopoaa69a802008-11-17 14:14:51 -08001161 int retval = 0;
1162 unsigned long flags;
1163
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001164 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001165 return -EINVAL;
1166
1167 spin_lock_irqsave(mEp->lock, flags);
1168
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301169 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1170 if (req->length)
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001171 mEp = (udc->ep0_dir == RX) ?
1172 udc->ep0out : udc->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301173 if (!list_empty(&mEp->qh.queue)) {
1174 _ep_nuke(mEp);
1175 retval = -EOVERFLOW;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001176 dev_warn(mEp->udc->dev, "endpoint ctrl %X nuked\n",
1177 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301178 }
David Lopoaa69a802008-11-17 14:14:51 -08001179 }
1180
1181 /* first nuke then test link, e.g. previous status has not sent */
1182 if (!list_empty(&mReq->queue)) {
1183 retval = -EBUSY;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001184 dev_err(mEp->udc->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001185 goto done;
1186 }
1187
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001188 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1189 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001190 retval = -EMSGSIZE;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001191 dev_warn(mEp->udc->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001192 }
1193
1194 dbg_queue(_usb_addr(mEp), req, retval);
1195
1196 /* push request */
1197 mReq->req.status = -EINPROGRESS;
1198 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001199
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301200 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001201
1202 if (retval == -EALREADY) {
David Lopoaa69a802008-11-17 14:14:51 -08001203 dbg_event(_usb_addr(mEp), "QUEUE", retval);
1204 retval = 0;
1205 }
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301206 if (!retval)
1207 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001208
1209 done:
1210 spin_unlock_irqrestore(mEp->lock, flags);
1211 return retval;
1212}
1213
1214/**
1215 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1216 *
1217 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1218 */
1219static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1220{
1221 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1222 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1223 unsigned long flags;
1224
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301225 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001226 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301227 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001228 return -EINVAL;
1229
1230 spin_lock_irqsave(mEp->lock, flags);
1231
1232 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
1233
Alexander Shishkind3595d12012-05-08 23:28:58 +03001234 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001235
1236 /* pop request */
1237 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001238
1239 usb_gadget_unmap_request(&mEp->udc->gadget, req, mEp->dir);
1240
David Lopoaa69a802008-11-17 14:14:51 -08001241 req->status = -ECONNRESET;
1242
Artem Leonenko7c25a822010-12-14 23:46:55 -08001243 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001244 spin_unlock(mEp->lock);
1245 mReq->req.complete(&mEp->ep, &mReq->req);
1246 spin_lock(mEp->lock);
1247 }
1248
1249 spin_unlock_irqrestore(mEp->lock, flags);
1250 return 0;
1251}
1252
1253/**
1254 * ep_set_halt: sets the endpoint halt feature
1255 *
1256 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1257 */
1258static int ep_set_halt(struct usb_ep *ep, int value)
1259{
1260 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1261 int direction, retval = 0;
1262 unsigned long flags;
1263
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001264 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001265 return -EINVAL;
1266
1267 spin_lock_irqsave(mEp->lock, flags);
1268
1269#ifndef STALL_IN
1270 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1271 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301272 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001273 spin_unlock_irqrestore(mEp->lock, flags);
1274 return -EAGAIN;
1275 }
1276#endif
1277
1278 direction = mEp->dir;
1279 do {
1280 dbg_event(_usb_addr(mEp), "HALT", value);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001281 retval |= hw_ep_set_halt(mEp->udc, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001282
1283 if (!value)
1284 mEp->wedge = 0;
1285
1286 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1287 mEp->dir = (mEp->dir == TX) ? RX : TX;
1288
1289 } while (mEp->dir != direction);
1290
1291 spin_unlock_irqrestore(mEp->lock, flags);
1292 return retval;
1293}
1294
1295/**
1296 * ep_set_wedge: sets the halt feature and ignores clear requests
1297 *
1298 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1299 */
1300static int ep_set_wedge(struct usb_ep *ep)
1301{
1302 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1303 unsigned long flags;
1304
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001305 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001306 return -EINVAL;
1307
1308 spin_lock_irqsave(mEp->lock, flags);
1309
1310 dbg_event(_usb_addr(mEp), "WEDGE", 0);
1311 mEp->wedge = 1;
1312
1313 spin_unlock_irqrestore(mEp->lock, flags);
1314
1315 return usb_ep_set_halt(ep);
1316}
1317
1318/**
1319 * ep_fifo_flush: flushes contents of a fifo
1320 *
1321 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1322 */
1323static void ep_fifo_flush(struct usb_ep *ep)
1324{
1325 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1326 unsigned long flags;
1327
David Lopoaa69a802008-11-17 14:14:51 -08001328 if (ep == NULL) {
Alexander Shishkin0f089092012-05-08 23:29:02 +03001329 dev_err(mEp->udc->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001330 return;
1331 }
1332
1333 spin_lock_irqsave(mEp->lock, flags);
1334
1335 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001336 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001337
1338 spin_unlock_irqrestore(mEp->lock, flags);
1339}
1340
1341/**
1342 * Endpoint-specific part of the API to the USB controller hardware
1343 * Check "usb_gadget.h" for details
1344 */
1345static const struct usb_ep_ops usb_ep_ops = {
1346 .enable = ep_enable,
1347 .disable = ep_disable,
1348 .alloc_request = ep_alloc_request,
1349 .free_request = ep_free_request,
1350 .queue = ep_queue,
1351 .dequeue = ep_dequeue,
1352 .set_halt = ep_set_halt,
1353 .set_wedge = ep_set_wedge,
1354 .fifo_flush = ep_fifo_flush,
1355};
1356
1357/******************************************************************************
1358 * GADGET block
1359 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301360static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1361{
1362 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
1363 unsigned long flags;
1364 int gadget_ready = 0;
1365
1366 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
1367 return -EOPNOTSUPP;
1368
Alexander Shishkind3595d12012-05-08 23:28:58 +03001369 spin_lock_irqsave(&udc->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301370 udc->vbus_active = is_active;
1371 if (udc->driver)
1372 gadget_ready = 1;
Alexander Shishkind3595d12012-05-08 23:28:58 +03001373 spin_unlock_irqrestore(&udc->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301374
1375 if (gadget_ready) {
1376 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301377 pm_runtime_get_sync(&_gadget->dev);
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03001378 hw_device_reset(udc, USBMODE_CM_DC);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001379 hw_device_state(udc, udc->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301380 } else {
Alexander Shishkind3595d12012-05-08 23:28:58 +03001381 hw_device_state(udc, 0);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301382 if (udc->udc_driver->notify_event)
1383 udc->udc_driver->notify_event(udc,
1384 CI13XXX_CONTROLLER_STOPPED_EVENT);
1385 _gadget_stop_activity(&udc->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301386 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301387 }
1388 }
1389
1390 return 0;
1391}
1392
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301393static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1394{
1395 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
1396 unsigned long flags;
1397 int ret = 0;
1398
Alexander Shishkind3595d12012-05-08 23:28:58 +03001399 spin_lock_irqsave(&udc->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301400 if (!udc->remote_wakeup) {
1401 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301402 goto out;
1403 }
Alexander Shishkin262c1632012-05-08 23:28:59 +03001404 if (!hw_read(udc, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301405 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301406 goto out;
1407 }
Alexander Shishkin262c1632012-05-08 23:28:59 +03001408 hw_write(udc, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301409out:
Alexander Shishkind3595d12012-05-08 23:28:58 +03001410 spin_unlock_irqrestore(&udc->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301411 return ret;
1412}
1413
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301414static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1415{
1416 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
1417
1418 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001419 return usb_phy_set_power(udc->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301420 return -ENOTSUPP;
1421}
1422
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001423static int ci13xxx_start(struct usb_gadget *gadget,
1424 struct usb_gadget_driver *driver);
1425static int ci13xxx_stop(struct usb_gadget *gadget,
1426 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001427/**
1428 * Device operations part of the API to the USB controller hardware,
1429 * which don't involve endpoints (or i/o)
1430 * Check "usb_gadget.h" for details
1431 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301432static const struct usb_gadget_ops usb_gadget_ops = {
1433 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301434 .wakeup = ci13xxx_wakeup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301435 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001436 .udc_start = ci13xxx_start,
1437 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301438};
David Lopoaa69a802008-11-17 14:14:51 -08001439
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001440static int init_eps(struct ci13xxx *udc)
1441{
1442 int retval = 0, i, j;
1443
1444 for (i = 0; i < udc->hw_ep_max/2; i++)
1445 for (j = RX; j <= TX; j++) {
1446 int k = i + j * udc->hw_ep_max/2;
1447 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
1448
1449 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1450 (j == TX) ? "in" : "out");
1451
1452 mEp->udc = udc;
1453 mEp->lock = &udc->lock;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001454 mEp->td_pool = udc->td_pool;
1455
1456 mEp->ep.name = mEp->name;
1457 mEp->ep.ops = &usb_ep_ops;
1458 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
1459
1460 INIT_LIST_HEAD(&mEp->qh.queue);
1461 mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
1462 &mEp->qh.dma);
1463 if (mEp->qh.ptr == NULL)
1464 retval = -ENOMEM;
1465 else
1466 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1467
1468 /*
1469 * set up shorthands for ep0 out and in endpoints,
1470 * don't add to gadget's ep_list
1471 */
1472 if (i == 0) {
1473 if (j == RX)
1474 udc->ep0out = mEp;
1475 else
1476 udc->ep0in = mEp;
1477
1478 continue;
1479 }
1480
1481 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
1482 }
1483
1484 return retval;
1485}
1486
David Lopoaa69a802008-11-17 14:14:51 -08001487/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001488 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001489 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001490 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001491 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001492 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001493 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001494static int ci13xxx_start(struct usb_gadget *gadget,
1495 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001496{
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001497 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301498 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001499 int retval = -ENOMEM;
1500
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001501 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001502 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001503
David Lopoaa69a802008-11-17 14:14:51 -08001504
Alexander Shishkind36ade62012-05-04 16:47:15 +03001505 udc->ep0out->ep.desc = &ctrl_endpt_out_desc;
1506 retval = usb_ep_enable(&udc->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301507 if (retval)
1508 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001509
Alexander Shishkind36ade62012-05-04 16:47:15 +03001510 udc->ep0in->ep.desc = &ctrl_endpt_in_desc;
1511 retval = usb_ep_enable(&udc->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301512 if (retval)
1513 return retval;
Alexander Shishkind3595d12012-05-08 23:28:58 +03001514 spin_lock_irqsave(&udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001515
Pavankumar Kondeti49d3df52011-01-11 09:19:21 +05301516 udc->driver = driver;
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301517 pm_runtime_get_sync(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301518 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
1519 if (udc->vbus_active) {
1520 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03001521 hw_device_reset(udc, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301522 } else {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301523 pm_runtime_put_sync(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301524 goto done;
1525 }
1526 }
1527
Alexander Shishkind3595d12012-05-08 23:28:58 +03001528 retval = hw_device_state(udc, udc->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301529 if (retval)
1530 pm_runtime_put_sync(&udc->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001531
1532 done:
Alexander Shishkind3595d12012-05-08 23:28:58 +03001533 spin_unlock_irqrestore(&udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001534 return retval;
1535}
David Lopoaa69a802008-11-17 14:14:51 -08001536
1537/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001538 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001539 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001540static int ci13xxx_stop(struct usb_gadget *gadget,
1541 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001542{
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001543 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1544 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001545
Alexander Shishkind3595d12012-05-08 23:28:58 +03001546 spin_lock_irqsave(&udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001547
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301548 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
1549 udc->vbus_active) {
Alexander Shishkind3595d12012-05-08 23:28:58 +03001550 hw_device_state(udc, 0);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301551 if (udc->udc_driver->notify_event)
1552 udc->udc_driver->notify_event(udc,
1553 CI13XXX_CONTROLLER_STOPPED_EVENT);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001554 udc->driver = NULL;
Alexander Shishkind3595d12012-05-08 23:28:58 +03001555 spin_unlock_irqrestore(&udc->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301556 _gadget_stop_activity(&udc->gadget);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001557 spin_lock_irqsave(&udc->lock, flags);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301558 pm_runtime_put(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301559 }
David Lopoaa69a802008-11-17 14:14:51 -08001560
Alexander Shishkind3595d12012-05-08 23:28:58 +03001561 spin_unlock_irqrestore(&udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001562
David Lopoaa69a802008-11-17 14:14:51 -08001563 return 0;
1564}
David Lopoaa69a802008-11-17 14:14:51 -08001565
1566/******************************************************************************
1567 * BUS block
1568 *****************************************************************************/
1569/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001570 * udc_irq: udc interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001571 *
1572 * This function returns IRQ_HANDLED if the IRQ has been handled
1573 * It locks access to registers
1574 */
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001575static irqreturn_t udc_irq(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -08001576{
David Lopoaa69a802008-11-17 14:14:51 -08001577 irqreturn_t retval;
1578 u32 intr;
1579
Greg Kroah-Hartmanf6395542012-05-10 11:37:18 -07001580 if (udc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001581 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001582
Alexander Shishkind3595d12012-05-08 23:28:58 +03001583 spin_lock(&udc->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301584
1585 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
Alexander Shishkin262c1632012-05-08 23:28:59 +03001586 if (hw_read(udc, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001587 USBMODE_CM_DC) {
Alexander Shishkind3595d12012-05-08 23:28:58 +03001588 spin_unlock(&udc->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301589 return IRQ_NONE;
1590 }
1591 }
Alexander Shishkind3595d12012-05-08 23:28:58 +03001592 intr = hw_test_and_clear_intr_active(udc);
Alexander Shishkine443b332012-05-11 17:25:46 +03001593 dbg_interrupt(intr);
David Lopoaa69a802008-11-17 14:14:51 -08001594
Alexander Shishkine443b332012-05-11 17:25:46 +03001595 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001596 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001597 if (USBi_URI & intr)
David Lopoaa69a802008-11-17 14:14:51 -08001598 isr_reset_handler(udc);
Alexander Shishkine443b332012-05-11 17:25:46 +03001599
David Lopoaa69a802008-11-17 14:14:51 -08001600 if (USBi_PCI & intr) {
Alexander Shishkind3595d12012-05-08 23:28:58 +03001601 udc->gadget.speed = hw_port_is_high_speed(udc) ?
David Lopoaa69a802008-11-17 14:14:51 -08001602 USB_SPEED_HIGH : USB_SPEED_FULL;
Marc Kleine-Budde7bb4fdc2011-10-10 18:38:09 +02001603 if (udc->suspended && udc->driver->resume) {
Alexander Shishkind3595d12012-05-08 23:28:58 +03001604 spin_unlock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301605 udc->driver->resume(&udc->gadget);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001606 spin_lock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301607 udc->suspended = 0;
1608 }
David Lopoaa69a802008-11-17 14:14:51 -08001609 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001610
1611 if (USBi_UI & intr)
David Lopoaa69a802008-11-17 14:14:51 -08001612 isr_tr_complete_handler(udc);
Alexander Shishkine443b332012-05-11 17:25:46 +03001613
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301614 if (USBi_SLI & intr) {
Marc Kleine-Budde7bb4fdc2011-10-10 18:38:09 +02001615 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
1616 udc->driver->suspend) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301617 udc->suspended = 1;
Alexander Shishkind3595d12012-05-08 23:28:58 +03001618 spin_unlock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301619 udc->driver->suspend(&udc->gadget);
Alexander Shishkind3595d12012-05-08 23:28:58 +03001620 spin_lock(&udc->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301621 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301622 }
David Lopoaa69a802008-11-17 14:14:51 -08001623 retval = IRQ_HANDLED;
1624 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001625 retval = IRQ_NONE;
1626 }
Alexander Shishkind3595d12012-05-08 23:28:58 +03001627 spin_unlock(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001628
1629 return retval;
1630}
1631
1632/**
1633 * udc_release: driver release function
1634 * @dev: device
1635 *
1636 * Currently does nothing
1637 */
1638static void udc_release(struct device *dev)
1639{
David Lopoaa69a802008-11-17 14:14:51 -08001640}
1641
1642/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001643 * udc_start: initialize gadget role
1644 * @udc: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001645 */
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001646static int udc_start(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -08001647{
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001648 struct device *dev = udc->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001649 int retval = 0;
1650
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001651 if (!udc)
David Lopoaa69a802008-11-17 14:14:51 -08001652 return -EINVAL;
1653
Alexander Shishkind3595d12012-05-08 23:28:58 +03001654 spin_lock_init(&udc->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001655
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301656 udc->gadget.ops = &usb_gadget_ops;
David Lopoaa69a802008-11-17 14:14:51 -08001657 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001658 udc->gadget.max_speed = USB_SPEED_HIGH;
David Lopoaa69a802008-11-17 14:14:51 -08001659 udc->gadget.is_otg = 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001660 udc->gadget.name = udc->udc_driver->name;
David Lopoaa69a802008-11-17 14:14:51 -08001661
1662 INIT_LIST_HEAD(&udc->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001663
Kay Sievers5df585242009-03-24 16:38:23 -07001664 dev_set_name(&udc->gadget.dev, "gadget");
David Lopoaa69a802008-11-17 14:14:51 -08001665 udc->gadget.dev.dma_mask = dev->dma_mask;
Pavankumar Kondeti61948ee2010-12-07 17:54:01 +05301666 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
David Lopoaa69a802008-11-17 14:14:51 -08001667 udc->gadget.dev.parent = dev;
1668 udc->gadget.dev.release = udc_release;
1669
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001670 /* alloc resources */
1671 udc->qh_pool = dma_pool_create("ci13xxx_qh", dev,
1672 sizeof(struct ci13xxx_qh),
1673 64, CI13XXX_PAGE_SIZE);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001674 if (udc->qh_pool == NULL)
1675 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001676
1677 udc->td_pool = dma_pool_create("ci13xxx_td", dev,
1678 sizeof(struct ci13xxx_td),
1679 64, CI13XXX_PAGE_SIZE);
1680 if (udc->td_pool == NULL) {
1681 retval = -ENOMEM;
1682 goto free_qh_pool;
1683 }
1684
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001685 retval = init_eps(udc);
1686 if (retval)
1687 goto free_pools;
1688
1689 udc->gadget.ep0 = &udc->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301690
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301691 udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301692
1693 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1694 if (udc->transceiver == NULL) {
1695 retval = -ENODEV;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001696 goto free_pools;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301697 }
1698 }
1699
1700 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
Alexander Shishkineb70e5a2012-05-11 17:25:54 +03001701 retval = hw_device_reset(udc, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301702 if (retval)
1703 goto put_transceiver;
1704 }
1705
David Lopoaa69a802008-11-17 14:14:51 -08001706 retval = device_register(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301707 if (retval) {
1708 put_device(&udc->gadget.dev);
1709 goto put_transceiver;
1710 }
David Lopoaa69a802008-11-17 14:14:51 -08001711
David Lopoaa69a802008-11-17 14:14:51 -08001712 retval = dbg_create_files(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301713 if (retval)
1714 goto unreg_device;
1715
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301716 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001717 retval = otg_set_peripheral(udc->transceiver->otg,
1718 &udc->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301719 if (retval)
1720 goto remove_dbg;
David Lopoaa69a802008-11-17 14:14:51 -08001721 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001722
1723 retval = usb_add_gadget_udc(dev, &udc->gadget);
1724 if (retval)
1725 goto remove_trans;
1726
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301727 pm_runtime_no_callbacks(&udc->gadget.dev);
1728 pm_runtime_enable(&udc->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001729
David Lopoaa69a802008-11-17 14:14:51 -08001730 return retval;
1731
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001732remove_trans:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301733 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001734 otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301735 usb_put_phy(udc->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001736 }
1737
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001738 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301739remove_dbg:
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301740 dbg_remove_files(&udc->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301741unreg_device:
1742 device_unregister(&udc->gadget.dev);
1743put_transceiver:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301744 if (!IS_ERR_OR_NULL(udc->transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301745 usb_put_phy(udc->transceiver);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001746free_pools:
1747 dma_pool_destroy(udc->td_pool);
1748free_qh_pool:
1749 dma_pool_destroy(udc->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001750 return retval;
1751}
1752
1753/**
1754 * udc_remove: parent remove must call this to remove UDC
1755 *
1756 * No interrupts active, the IRQ has been released
1757 */
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001758static void udc_stop(struct ci13xxx *udc)
David Lopoaa69a802008-11-17 14:14:51 -08001759{
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001760 int i;
David Lopoaa69a802008-11-17 14:14:51 -08001761
Alexander Shishkin0f089092012-05-08 23:29:02 +03001762 if (udc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001763 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001764
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001765 usb_del_gadget_udc(&udc->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001766
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001767 for (i = 0; i < udc->hw_ep_max; i++) {
1768 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1769
1770 dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1771 }
1772
1773 dma_pool_destroy(udc->td_pool);
1774 dma_pool_destroy(udc->qh_pool);
1775
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301776 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001777 otg_set_peripheral(udc->transceiver->otg, NULL);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301778 usb_put_phy(udc->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301779 }
David Lopoaa69a802008-11-17 14:14:51 -08001780 dbg_remove_files(&udc->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001781 device_unregister(&udc->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001782 /* my kobject is dynamic, I swear! */
1783 memset(&udc->gadget, 0, sizeof(udc->gadget));
1784}
David Lopoaa69a802008-11-17 14:14:51 -08001785
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001786/**
1787 * ci_hdrc_gadget_init - initialize device related bits
1788 * ci: the controller
1789 *
1790 * This function enables the gadget role, if the device is "device capable".
1791 */
1792int ci_hdrc_gadget_init(struct ci13xxx *ci)
1793{
1794 struct ci_role_driver *rdrv;
1795
1796 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1797 return -ENXIO;
1798
1799 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1800 if (!rdrv)
1801 return -ENOMEM;
1802
1803 rdrv->start = udc_start;
1804 rdrv->stop = udc_stop;
1805 rdrv->irq = udc_irq;
1806 rdrv->name = "gadget";
1807 ci->roles[CI_ROLE_GADGET] = rdrv;
1808
1809 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001810}