blob: 3dc5093823894857f152ef37d917e8651b97cdde [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
Richard Zhao8c4fc032012-09-12 14:58:09 +0300302static void hw_enable_vbus_intr(struct ci13xxx *ci)
303{
304 hw_write(ci, OP_OTGSC, OTGSC_AVVIS, OTGSC_AVVIS);
305 hw_write(ci, OP_OTGSC, OTGSC_AVVIE, OTGSC_AVVIE);
306 queue_work(ci->wq, &ci->vbus_work);
307}
308
309static void hw_disable_vbus_intr(struct ci13xxx *ci)
310{
311 hw_write(ci, OP_OTGSC, OTGSC_AVVIE, 0);
312}
313
David Lopoaa69a802008-11-17 14:14:51 -0800314/**
315 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
316 * interruption)
317 *
318 * This function returns guard value
319 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800320static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800321{
Richard Zhao26c696c2012-07-07 22:56:40 +0800322 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800323}
324
325/**
326 * hw_test_and_set_setup_guard: test & set setup guard (execute without
327 * interruption)
328 *
329 * This function returns guard value
330 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800331static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800332{
Richard Zhao26c696c2012-07-07 22:56:40 +0800333 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
David Lopoaa69a802008-11-17 14:14:51 -0800334}
335
336/**
337 * hw_usb_set_address: configures USB address (execute without interruption)
338 * @value: new USB address
339 *
Alexander Shishkinef15e542012-05-11 17:25:43 +0300340 * This function explicitly sets the address, without the "USBADRA" (advance)
341 * feature, which is not supported by older versions of the controller.
David Lopoaa69a802008-11-17 14:14:51 -0800342 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800343static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
David Lopoaa69a802008-11-17 14:14:51 -0800344{
Richard Zhao26c696c2012-07-07 22:56:40 +0800345 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
Alexander Shishkinef15e542012-05-11 17:25:43 +0300346 value << ffs_nr(DEVICEADDR_USBADR));
David Lopoaa69a802008-11-17 14:14:51 -0800347}
348
349/**
350 * hw_usb_reset: restart device after a bus reset (execute without
351 * interruption)
352 *
353 * This function returns an error code
354 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800355static int hw_usb_reset(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800356{
Richard Zhao26c696c2012-07-07 22:56:40 +0800357 hw_usb_set_address(ci, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800358
359 /* ESS flushes only at end?!? */
Richard Zhao26c696c2012-07-07 22:56:40 +0800360 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
David Lopoaa69a802008-11-17 14:14:51 -0800361
362 /* clear setup token semaphores */
Richard Zhao26c696c2012-07-07 22:56:40 +0800363 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800364
365 /* clear complete status */
Richard Zhao26c696c2012-07-07 22:56:40 +0800366 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
David Lopoaa69a802008-11-17 14:14:51 -0800367
368 /* wait until all bits cleared */
Richard Zhao26c696c2012-07-07 22:56:40 +0800369 while (hw_read(ci, OP_ENDPTPRIME, ~0))
David Lopoaa69a802008-11-17 14:14:51 -0800370 udelay(10); /* not RTOS friendly */
371
372 /* reset all endpoints ? */
373
374 /* reset internal status and wait for further instructions
375 no need to verify the port reset status (ESS does it) */
376
377 return 0;
378}
379
Richard Zhao8c4fc032012-09-12 14:58:09 +0300380static void vbus_work(struct work_struct *work)
381{
382 struct ci13xxx *ci = container_of(work, struct ci13xxx, vbus_work);
383
384 if (hw_read(ci, OP_OTGSC, OTGSC_AVV))
385 usb_gadget_vbus_connect(&ci->gadget);
386 else
387 usb_gadget_vbus_disconnect(&ci->gadget);
388}
389
David Lopoaa69a802008-11-17 14:14:51 -0800390/******************************************************************************
David Lopoaa69a802008-11-17 14:14:51 -0800391 * UTIL block
392 *****************************************************************************/
393/**
394 * _usb_addr: calculates endpoint address from direction & number
395 * @ep: endpoint
396 */
397static inline u8 _usb_addr(struct ci13xxx_ep *ep)
398{
399 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
400}
401
402/**
403 * _hardware_queue: configures a request at hardware level
404 * @gadget: gadget
405 * @mEp: endpoint
406 *
407 * This function returns an error code
408 */
409static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
410{
Richard Zhao26c696c2012-07-07 22:56:40 +0800411 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -0800412 unsigned i;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530413 int ret = 0;
414 unsigned length = mReq->req.length;
David Lopoaa69a802008-11-17 14:14:51 -0800415
David Lopoaa69a802008-11-17 14:14:51 -0800416 /* don't queue twice */
417 if (mReq->req.status == -EALREADY)
418 return -EALREADY;
419
David Lopoaa69a802008-11-17 14:14:51 -0800420 mReq->req.status = -EALREADY;
David Lopoaa69a802008-11-17 14:14:51 -0800421
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530422 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
423 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
424 &mReq->zdma);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300425 if (mReq->zptr == NULL)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530426 return -ENOMEM;
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300427
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530428 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
429 mReq->zptr->next = TD_TERMINATE;
430 mReq->zptr->token = TD_STATUS_ACTIVE;
431 if (!mReq->req.no_interrupt)
432 mReq->zptr->token |= TD_IOC;
433 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800434 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +0300435 if (ret)
436 return ret;
437
David Lopoaa69a802008-11-17 14:14:51 -0800438 /*
439 * TD configuration
440 * TODO - handle requests which spawns into several TDs
441 */
442 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530443 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
David Lopoaa69a802008-11-17 14:14:51 -0800444 mReq->ptr->token &= TD_TOTAL_BYTES;
David Lopoaa69a802008-11-17 14:14:51 -0800445 mReq->ptr->token |= TD_STATUS_ACTIVE;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530446 if (mReq->zptr) {
447 mReq->ptr->next = mReq->zdma;
448 } else {
449 mReq->ptr->next = TD_TERMINATE;
450 if (!mReq->req.no_interrupt)
451 mReq->ptr->token |= TD_IOC;
452 }
David Lopoaa69a802008-11-17 14:14:51 -0800453 mReq->ptr->page[0] = mReq->req.dma;
454 for (i = 1; i < 5; i++)
455 mReq->ptr->page[i] =
Artem Leonenko0a313c42010-12-14 23:47:06 -0800456 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
David Lopoaa69a802008-11-17 14:14:51 -0800457
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530458 if (!list_empty(&mEp->qh.queue)) {
459 struct ci13xxx_req *mReqPrev;
460 int n = hw_ep_bit(mEp->num, mEp->dir);
461 int tmp_stat;
462
463 mReqPrev = list_entry(mEp->qh.queue.prev,
464 struct ci13xxx_req, queue);
465 if (mReqPrev->zptr)
466 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
467 else
468 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
469 wmb();
Richard Zhao26c696c2012-07-07 22:56:40 +0800470 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530471 goto done;
472 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800473 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
474 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
475 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
476 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530477 if (tmp_stat)
478 goto done;
479 }
480
481 /* QH configuration */
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530482 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
483 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530484 mEp->qh.ptr->cap |= QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -0800485
486 wmb(); /* synchronize before ep prime */
487
Richard Zhao26c696c2012-07-07 22:56:40 +0800488 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
David Lopoaa69a802008-11-17 14:14:51 -0800489 mEp->type == USB_ENDPOINT_XFER_CONTROL);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530490done:
491 return ret;
David Lopoaa69a802008-11-17 14:14:51 -0800492}
493
494/**
495 * _hardware_dequeue: handles a request at hardware level
496 * @gadget: gadget
497 * @mEp: endpoint
498 *
499 * This function returns an error code
500 */
501static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
502{
David Lopoaa69a802008-11-17 14:14:51 -0800503 if (mReq->req.status != -EALREADY)
504 return -EINVAL;
505
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530506 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
507 return -EBUSY;
508
509 if (mReq->zptr) {
510 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
511 return -EBUSY;
512 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
513 mReq->zptr = NULL;
514 }
David Lopoaa69a802008-11-17 14:14:51 -0800515
516 mReq->req.status = 0;
517
Richard Zhao26c696c2012-07-07 22:56:40 +0800518 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800519
520 mReq->req.status = mReq->ptr->token & TD_STATUS;
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530521 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800522 mReq->req.status = -1;
523 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
524 mReq->req.status = -1;
525 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
526 mReq->req.status = -1;
527
528 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
529 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
530 mReq->req.actual = mReq->req.length - mReq->req.actual;
531 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
532
533 return mReq->req.actual;
534}
535
536/**
537 * _ep_nuke: dequeues all endpoint requests
538 * @mEp: endpoint
539 *
540 * This function returns an error code
541 * Caller must hold lock
542 */
543static int _ep_nuke(struct ci13xxx_ep *mEp)
544__releases(mEp->lock)
545__acquires(mEp->lock)
546{
David Lopoaa69a802008-11-17 14:14:51 -0800547 if (mEp == NULL)
548 return -EINVAL;
549
Richard Zhao26c696c2012-07-07 22:56:40 +0800550 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -0800551
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530552 while (!list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -0800553
554 /* pop oldest request */
555 struct ci13xxx_req *mReq = \
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530556 list_entry(mEp->qh.queue.next,
David Lopoaa69a802008-11-17 14:14:51 -0800557 struct ci13xxx_req, queue);
558 list_del_init(&mReq->queue);
559 mReq->req.status = -ESHUTDOWN;
560
Artem Leonenko7c25a822010-12-14 23:46:55 -0800561 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -0800562 spin_unlock(mEp->lock);
563 mReq->req.complete(&mEp->ep, &mReq->req);
564 spin_lock(mEp->lock);
565 }
566 }
567 return 0;
568}
569
570/**
571 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
572 * @gadget: gadget
573 *
574 * This function returns an error code
David Lopoaa69a802008-11-17 14:14:51 -0800575 */
576static int _gadget_stop_activity(struct usb_gadget *gadget)
David Lopoaa69a802008-11-17 14:14:51 -0800577{
578 struct usb_ep *ep;
Richard Zhao26c696c2012-07-07 22:56:40 +0800579 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530580 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -0800581
Richard Zhao26c696c2012-07-07 22:56:40 +0800582 spin_lock_irqsave(&ci->lock, flags);
583 ci->gadget.speed = USB_SPEED_UNKNOWN;
584 ci->remote_wakeup = 0;
585 ci->suspended = 0;
586 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530587
David Lopoaa69a802008-11-17 14:14:51 -0800588 /* flush all endpoints */
589 gadget_for_each_ep(ep, gadget) {
590 usb_ep_fifo_flush(ep);
591 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800592 usb_ep_fifo_flush(&ci->ep0out->ep);
593 usb_ep_fifo_flush(&ci->ep0in->ep);
David Lopoaa69a802008-11-17 14:14:51 -0800594
Richard Zhao26c696c2012-07-07 22:56:40 +0800595 if (ci->driver)
596 ci->driver->disconnect(gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800597
598 /* make sure to disable all endpoints */
599 gadget_for_each_ep(ep, gadget) {
600 usb_ep_disable(ep);
601 }
David Lopoaa69a802008-11-17 14:14:51 -0800602
Richard Zhao26c696c2012-07-07 22:56:40 +0800603 if (ci->status != NULL) {
604 usb_ep_free_request(&ci->ep0in->ep, ci->status);
605 ci->status = NULL;
David Lopoaa69a802008-11-17 14:14:51 -0800606 }
607
David Lopoaa69a802008-11-17 14:14:51 -0800608 return 0;
609}
610
611/******************************************************************************
612 * ISR block
613 *****************************************************************************/
614/**
615 * isr_reset_handler: USB reset interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800616 * @ci: UDC device
David Lopoaa69a802008-11-17 14:14:51 -0800617 *
618 * This function resets USB engine after a bus reset occurred
619 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800620static void isr_reset_handler(struct ci13xxx *ci)
621__releases(ci->lock)
622__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800623{
David Lopoaa69a802008-11-17 14:14:51 -0800624 int retval;
625
David Lopoaa69a802008-11-17 14:14:51 -0800626 dbg_event(0xFF, "BUS RST", 0);
627
Richard Zhao26c696c2012-07-07 22:56:40 +0800628 spin_unlock(&ci->lock);
629 retval = _gadget_stop_activity(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -0800630 if (retval)
631 goto done;
632
Richard Zhao26c696c2012-07-07 22:56:40 +0800633 retval = hw_usb_reset(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800634 if (retval)
635 goto done;
636
Richard Zhao26c696c2012-07-07 22:56:40 +0800637 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
638 if (ci->status == NULL)
Anji jonnalaac1aa6a2011-05-02 11:56:32 +0530639 retval = -ENOMEM;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530640
Michael Grzeschikb9322252012-05-11 17:25:50 +0300641done:
Richard Zhao26c696c2012-07-07 22:56:40 +0800642 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800643
David Lopoaa69a802008-11-17 14:14:51 -0800644 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +0800645 dev_err(ci->dev, "error: %i\n", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800646}
647
648/**
649 * isr_get_status_complete: get_status request complete function
650 * @ep: endpoint
651 * @req: request handled
652 *
653 * Caller must release lock
654 */
655static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
656{
Alexander Shishkin0f089092012-05-08 23:29:02 +0300657 if (ep == NULL || req == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800658 return;
David Lopoaa69a802008-11-17 14:14:51 -0800659
660 kfree(req->buf);
661 usb_ep_free_request(ep, req);
662}
663
664/**
665 * isr_get_status_response: get_status request response
Richard Zhao26c696c2012-07-07 22:56:40 +0800666 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800667 * @setup: setup request packet
668 *
669 * This function returns an error code
670 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800671static int isr_get_status_response(struct ci13xxx *ci,
David Lopoaa69a802008-11-17 14:14:51 -0800672 struct usb_ctrlrequest *setup)
673__releases(mEp->lock)
674__acquires(mEp->lock)
675{
Richard Zhao26c696c2012-07-07 22:56:40 +0800676 struct ci13xxx_ep *mEp = ci->ep0in;
David Lopoaa69a802008-11-17 14:14:51 -0800677 struct usb_request *req = NULL;
678 gfp_t gfp_flags = GFP_ATOMIC;
679 int dir, num, retval;
680
David Lopoaa69a802008-11-17 14:14:51 -0800681 if (mEp == NULL || setup == NULL)
682 return -EINVAL;
683
684 spin_unlock(mEp->lock);
685 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
686 spin_lock(mEp->lock);
687 if (req == NULL)
688 return -ENOMEM;
689
690 req->complete = isr_get_status_complete;
691 req->length = 2;
692 req->buf = kzalloc(req->length, gfp_flags);
693 if (req->buf == NULL) {
694 retval = -ENOMEM;
695 goto err_free_req;
696 }
697
698 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530699 /* Assume that device is bus powered for now. */
Richard Zhao26c696c2012-07-07 22:56:40 +0800700 *(u16 *)req->buf = ci->remote_wakeup << 1;
David Lopoaa69a802008-11-17 14:14:51 -0800701 retval = 0;
702 } else if ((setup->bRequestType & USB_RECIP_MASK) \
703 == USB_RECIP_ENDPOINT) {
704 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
705 TX : RX;
706 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Richard Zhao26c696c2012-07-07 22:56:40 +0800707 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
David Lopoaa69a802008-11-17 14:14:51 -0800708 }
709 /* else do nothing; reserved for future use */
710
711 spin_unlock(mEp->lock);
712 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
713 spin_lock(mEp->lock);
714 if (retval)
715 goto err_free_buf;
716
717 return 0;
718
719 err_free_buf:
720 kfree(req->buf);
721 err_free_req:
722 spin_unlock(mEp->lock);
723 usb_ep_free_request(&mEp->ep, req);
724 spin_lock(mEp->lock);
725 return retval;
726}
727
728/**
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530729 * isr_setup_status_complete: setup_status request complete function
730 * @ep: endpoint
731 * @req: request handled
732 *
733 * Caller must release lock. Put the port in test mode if test mode
734 * feature is selected.
735 */
736static void
737isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
738{
Richard Zhao26c696c2012-07-07 22:56:40 +0800739 struct ci13xxx *ci = req->context;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530740 unsigned long flags;
741
Richard Zhao26c696c2012-07-07 22:56:40 +0800742 if (ci->setaddr) {
743 hw_usb_set_address(ci, ci->address);
744 ci->setaddr = false;
Alexander Shishkinef15e542012-05-11 17:25:43 +0300745 }
746
Richard Zhao26c696c2012-07-07 22:56:40 +0800747 spin_lock_irqsave(&ci->lock, flags);
748 if (ci->test_mode)
749 hw_port_test_set(ci, ci->test_mode);
750 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530751}
752
753/**
David Lopoaa69a802008-11-17 14:14:51 -0800754 * isr_setup_status_phase: queues the status phase of a setup transation
Richard Zhao26c696c2012-07-07 22:56:40 +0800755 * @ci: ci struct
David Lopoaa69a802008-11-17 14:14:51 -0800756 *
757 * This function returns an error code
758 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800759static int isr_setup_status_phase(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -0800760__releases(mEp->lock)
761__acquires(mEp->lock)
762{
763 int retval;
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530764 struct ci13xxx_ep *mEp;
David Lopoaa69a802008-11-17 14:14:51 -0800765
Richard Zhao26c696c2012-07-07 22:56:40 +0800766 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
767 ci->status->context = ci;
768 ci->status->complete = isr_setup_status_complete;
David Lopoaa69a802008-11-17 14:14:51 -0800769
770 spin_unlock(mEp->lock);
Richard Zhao26c696c2012-07-07 22:56:40 +0800771 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -0800772 spin_lock(mEp->lock);
773
774 return retval;
775}
776
777/**
778 * isr_tr_complete_low: transaction complete low level handler
779 * @mEp: endpoint
780 *
781 * This function returns an error code
782 * Caller must hold lock
783 */
784static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
785__releases(mEp->lock)
786__acquires(mEp->lock)
787{
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530788 struct ci13xxx_req *mReq, *mReqTemp;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530789 struct ci13xxx_ep *mEpTemp = mEp;
Michael Grzeschikdb899602012-09-12 14:58:04 +0300790 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800791
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530792 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
793 queue) {
794 retval = _hardware_dequeue(mEp, mReq);
795 if (retval < 0)
796 break;
797 list_del_init(&mReq->queue);
798 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
799 if (mReq->req.complete != NULL) {
800 spin_unlock(mEp->lock);
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530801 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
802 mReq->req.length)
Richard Zhao26c696c2012-07-07 22:56:40 +0800803 mEpTemp = mEp->ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +0530804 mReq->req.complete(&mEpTemp->ep, &mReq->req);
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530805 spin_lock(mEp->lock);
806 }
807 }
David Lopoaa69a802008-11-17 14:14:51 -0800808
Pavankumar Kondetief907482011-05-02 11:56:27 +0530809 if (retval == -EBUSY)
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +0530810 retval = 0;
811 if (retval < 0)
David Lopoaa69a802008-11-17 14:14:51 -0800812 dbg_event(_usb_addr(mEp), "DONE", retval);
David Lopoaa69a802008-11-17 14:14:51 -0800813
David Lopoaa69a802008-11-17 14:14:51 -0800814 return retval;
815}
816
817/**
818 * isr_tr_complete_handler: transaction complete interrupt handler
Richard Zhao26c696c2012-07-07 22:56:40 +0800819 * @ci: UDC descriptor
David Lopoaa69a802008-11-17 14:14:51 -0800820 *
821 * This function handles traffic events
822 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800823static void isr_tr_complete_handler(struct ci13xxx *ci)
824__releases(ci->lock)
825__acquires(ci->lock)
David Lopoaa69a802008-11-17 14:14:51 -0800826{
827 unsigned i;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530828 u8 tmode = 0;
David Lopoaa69a802008-11-17 14:14:51 -0800829
Richard Zhao26c696c2012-07-07 22:56:40 +0800830 for (i = 0; i < ci->hw_ep_max; i++) {
831 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530832 int type, num, dir, err = -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -0800833 struct usb_ctrlrequest req;
834
Ido Shayevitz31fb6012012-03-12 20:25:23 +0200835 if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -0800836 continue; /* not configured */
837
Richard Zhao26c696c2012-07-07 22:56:40 +0800838 if (hw_test_and_clear_complete(ci, i)) {
David Lopoaa69a802008-11-17 14:14:51 -0800839 err = isr_tr_complete_low(mEp);
840 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
841 if (err > 0) /* needs status phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800842 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800843 if (err < 0) {
844 dbg_event(_usb_addr(mEp),
845 "ERROR", err);
Richard Zhao26c696c2012-07-07 22:56:40 +0800846 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800847 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800848 dev_err(ci->dev,
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -0700849 "error: ep_set_halt\n");
Richard Zhao26c696c2012-07-07 22:56:40 +0800850 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800851 }
852 }
853 }
854
855 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
Richard Zhao26c696c2012-07-07 22:56:40 +0800856 !hw_test_and_clear_setup_status(ci, i))
David Lopoaa69a802008-11-17 14:14:51 -0800857 continue;
858
859 if (i != 0) {
Richard Zhao26c696c2012-07-07 22:56:40 +0800860 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
David Lopoaa69a802008-11-17 14:14:51 -0800861 continue;
862 }
863
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530864 /*
865 * Flush data and handshake transactions of previous
866 * setup packet.
867 */
Richard Zhao26c696c2012-07-07 22:56:40 +0800868 _ep_nuke(ci->ep0out);
869 _ep_nuke(ci->ep0in);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530870
David Lopoaa69a802008-11-17 14:14:51 -0800871 /* read_setup_packet */
872 do {
Richard Zhao26c696c2012-07-07 22:56:40 +0800873 hw_test_and_set_setup_guard(ci);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +0530874 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
Richard Zhao26c696c2012-07-07 22:56:40 +0800875 } while (!hw_test_and_clear_setup_guard(ci));
David Lopoaa69a802008-11-17 14:14:51 -0800876
877 type = req.bRequestType;
878
Richard Zhao26c696c2012-07-07 22:56:40 +0800879 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
David Lopoaa69a802008-11-17 14:14:51 -0800880
881 dbg_setup(_usb_addr(mEp), &req);
882
883 switch (req.bRequest) {
884 case USB_REQ_CLEAR_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530885 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
886 le16_to_cpu(req.wValue) ==
887 USB_ENDPOINT_HALT) {
888 if (req.wLength != 0)
David Lopoaa69a802008-11-17 14:14:51 -0800889 break;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530890 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530891 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530892 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530893 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800894 num += ci->hw_ep_max/2;
895 if (!ci->ci13xxx_ep[num].wedge) {
896 spin_unlock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530897 err = usb_ep_clear_halt(
Richard Zhao26c696c2012-07-07 22:56:40 +0800898 &ci->ci13xxx_ep[num].ep);
899 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530900 if (err)
901 break;
902 }
Richard Zhao26c696c2012-07-07 22:56:40 +0800903 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530904 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
905 le16_to_cpu(req.wValue) ==
906 USB_DEVICE_REMOTE_WAKEUP) {
907 if (req.wLength != 0)
908 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800909 ci->remote_wakeup = 0;
910 err = isr_setup_status_phase(ci);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530911 } else {
912 goto delegate;
David Lopoaa69a802008-11-17 14:14:51 -0800913 }
David Lopoaa69a802008-11-17 14:14:51 -0800914 break;
915 case USB_REQ_GET_STATUS:
916 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
917 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
918 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
919 goto delegate;
920 if (le16_to_cpu(req.wLength) != 2 ||
921 le16_to_cpu(req.wValue) != 0)
922 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800923 err = isr_get_status_response(ci, &req);
David Lopoaa69a802008-11-17 14:14:51 -0800924 break;
925 case USB_REQ_SET_ADDRESS:
926 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
927 goto delegate;
928 if (le16_to_cpu(req.wLength) != 0 ||
929 le16_to_cpu(req.wIndex) != 0)
930 break;
Richard Zhao26c696c2012-07-07 22:56:40 +0800931 ci->address = (u8)le16_to_cpu(req.wValue);
932 ci->setaddr = true;
933 err = isr_setup_status_phase(ci);
David Lopoaa69a802008-11-17 14:14:51 -0800934 break;
935 case USB_REQ_SET_FEATURE:
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530936 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
937 le16_to_cpu(req.wValue) ==
938 USB_ENDPOINT_HALT) {
939 if (req.wLength != 0)
940 break;
941 num = le16_to_cpu(req.wIndex);
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530942 dir = num & USB_ENDPOINT_DIR_MASK;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530943 num &= USB_ENDPOINT_NUMBER_MASK;
Pavankumar Kondeti4c5212b2011-05-02 11:56:30 +0530944 if (dir) /* TX */
Richard Zhao26c696c2012-07-07 22:56:40 +0800945 num += ci->hw_ep_max/2;
David Lopoaa69a802008-11-17 14:14:51 -0800946
Richard Zhao26c696c2012-07-07 22:56:40 +0800947 spin_unlock(&ci->lock);
948 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
949 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530950 if (!err)
Richard Zhao26c696c2012-07-07 22:56:40 +0800951 isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530952 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530953 if (req.wLength != 0)
954 break;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530955 switch (le16_to_cpu(req.wValue)) {
956 case USB_DEVICE_REMOTE_WAKEUP:
Richard Zhao26c696c2012-07-07 22:56:40 +0800957 ci->remote_wakeup = 1;
958 err = isr_setup_status_phase(ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530959 break;
960 case USB_DEVICE_TEST_MODE:
961 tmode = le16_to_cpu(req.wIndex) >> 8;
962 switch (tmode) {
963 case TEST_J:
964 case TEST_K:
965 case TEST_SE0_NAK:
966 case TEST_PACKET:
967 case TEST_FORCE_EN:
Richard Zhao26c696c2012-07-07 22:56:40 +0800968 ci->test_mode = tmode;
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530969 err = isr_setup_status_phase(
Richard Zhao26c696c2012-07-07 22:56:40 +0800970 ci);
Pavankumar Kondeti541cace2011-02-18 17:43:18 +0530971 break;
972 default:
973 break;
974 }
975 default:
976 goto delegate;
977 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +0530978 } else {
979 goto delegate;
980 }
David Lopoaa69a802008-11-17 14:14:51 -0800981 break;
982 default:
983delegate:
984 if (req.wLength == 0) /* no data phase */
Richard Zhao26c696c2012-07-07 22:56:40 +0800985 ci->ep0_dir = TX;
David Lopoaa69a802008-11-17 14:14:51 -0800986
Richard Zhao26c696c2012-07-07 22:56:40 +0800987 spin_unlock(&ci->lock);
988 err = ci->driver->setup(&ci->gadget, &req);
989 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800990 break;
991 }
992
993 if (err < 0) {
994 dbg_event(_usb_addr(mEp), "ERROR", err);
995
Richard Zhao26c696c2012-07-07 22:56:40 +0800996 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -0800997 if (usb_ep_set_halt(&mEp->ep))
Richard Zhao26c696c2012-07-07 22:56:40 +0800998 dev_err(ci->dev, "error: ep_set_halt\n");
999 spin_lock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001000 }
1001 }
1002}
1003
1004/******************************************************************************
1005 * ENDPT block
1006 *****************************************************************************/
1007/**
1008 * ep_enable: configure endpoint, making it usable
1009 *
1010 * Check usb_ep_enable() at "usb_gadget.h" for details
1011 */
1012static int ep_enable(struct usb_ep *ep,
1013 const struct usb_endpoint_descriptor *desc)
1014{
1015 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301016 int retval = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001017 unsigned long flags;
1018
David Lopoaa69a802008-11-17 14:14:51 -08001019 if (ep == NULL || desc == NULL)
1020 return -EINVAL;
1021
1022 spin_lock_irqsave(mEp->lock, flags);
1023
1024 /* only internal SW should enable ctrl endpts */
1025
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001026 mEp->ep.desc = desc;
David Lopoaa69a802008-11-17 14:14:51 -08001027
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301028 if (!list_empty(&mEp->qh.queue))
Richard Zhao26c696c2012-07-07 22:56:40 +08001029 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
David Lopoaa69a802008-11-17 14:14:51 -08001030
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001031 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1032 mEp->num = usb_endpoint_num(desc);
1033 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001034
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001035 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001036
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301037 dbg_event(_usb_addr(mEp), "ENABLE", 0);
David Lopoaa69a802008-11-17 14:14:51 -08001038
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301039 mEp->qh.ptr->cap = 0;
David Lopof23e6492009-04-16 14:35:24 -07001040
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301041 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1042 mEp->qh.ptr->cap |= QH_IOS;
1043 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1044 mEp->qh.ptr->cap &= ~QH_MULT;
1045 else
1046 mEp->qh.ptr->cap &= ~QH_ZLT;
David Lopoaa69a802008-11-17 14:14:51 -08001047
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301048 mEp->qh.ptr->cap |=
1049 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1050 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
David Lopoaa69a802008-11-17 14:14:51 -08001051
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301052 /*
1053 * Enable endpoints in the HW other than ep0 as ep0
1054 * is always enabled
1055 */
1056 if (mEp->num)
Richard Zhao26c696c2012-07-07 22:56:40 +08001057 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
David Lopoaa69a802008-11-17 14:14:51 -08001058
1059 spin_unlock_irqrestore(mEp->lock, flags);
1060 return retval;
1061}
1062
1063/**
1064 * ep_disable: endpoint is no longer usable
1065 *
1066 * Check usb_ep_disable() at "usb_gadget.h" for details
1067 */
1068static int ep_disable(struct usb_ep *ep)
1069{
1070 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1071 int direction, retval = 0;
1072 unsigned long flags;
1073
David Lopoaa69a802008-11-17 14:14:51 -08001074 if (ep == NULL)
1075 return -EINVAL;
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001076 else if (mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001077 return -EBUSY;
1078
1079 spin_lock_irqsave(mEp->lock, flags);
1080
1081 /* only internal SW should disable ctrl endpts */
1082
1083 direction = mEp->dir;
1084 do {
1085 dbg_event(_usb_addr(mEp), "DISABLE", 0);
1086
1087 retval |= _ep_nuke(mEp);
Richard Zhao26c696c2012-07-07 22:56:40 +08001088 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001089
1090 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1091 mEp->dir = (mEp->dir == TX) ? RX : TX;
1092
1093 } while (mEp->dir != direction);
1094
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001095 mEp->ep.desc = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001096
1097 spin_unlock_irqrestore(mEp->lock, flags);
1098 return retval;
1099}
1100
1101/**
1102 * ep_alloc_request: allocate a request object to use with this endpoint
1103 *
1104 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1105 */
1106static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1107{
1108 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1109 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001110
Alexander Shishkin0f089092012-05-08 23:29:02 +03001111 if (ep == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001112 return NULL;
David Lopoaa69a802008-11-17 14:14:51 -08001113
David Lopoaa69a802008-11-17 14:14:51 -08001114 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1115 if (mReq != NULL) {
1116 INIT_LIST_HEAD(&mReq->queue);
1117
1118 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1119 &mReq->dma);
1120 if (mReq->ptr == NULL) {
1121 kfree(mReq);
1122 mReq = NULL;
1123 }
1124 }
1125
1126 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
1127
David Lopoaa69a802008-11-17 14:14:51 -08001128 return (mReq == NULL) ? NULL : &mReq->req;
1129}
1130
1131/**
1132 * ep_free_request: frees a request object
1133 *
1134 * Check usb_ep_free_request() at "usb_gadget.h" for details
1135 */
1136static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1137{
1138 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1139 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1140 unsigned long flags;
1141
David Lopoaa69a802008-11-17 14:14:51 -08001142 if (ep == NULL || req == NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001143 return;
1144 } else if (!list_empty(&mReq->queue)) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001145 dev_err(mEp->ci->dev, "freeing queued request\n");
David Lopoaa69a802008-11-17 14:14:51 -08001146 return;
1147 }
1148
1149 spin_lock_irqsave(mEp->lock, flags);
1150
1151 if (mReq->ptr)
1152 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1153 kfree(mReq);
1154
1155 dbg_event(_usb_addr(mEp), "FREE", 0);
1156
1157 spin_unlock_irqrestore(mEp->lock, flags);
1158}
1159
1160/**
1161 * ep_queue: queues (submits) an I/O request to an endpoint
1162 *
1163 * Check usb_ep_queue()* at usb_gadget.h" for details
1164 */
1165static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1166 gfp_t __maybe_unused gfp_flags)
1167{
1168 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1169 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
Richard Zhao26c696c2012-07-07 22:56:40 +08001170 struct ci13xxx *ci = mEp->ci;
David Lopoaa69a802008-11-17 14:14:51 -08001171 int retval = 0;
1172 unsigned long flags;
1173
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001174 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001175 return -EINVAL;
1176
1177 spin_lock_irqsave(mEp->lock, flags);
1178
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301179 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1180 if (req->length)
Richard Zhao26c696c2012-07-07 22:56:40 +08001181 mEp = (ci->ep0_dir == RX) ?
1182 ci->ep0out : ci->ep0in;
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301183 if (!list_empty(&mEp->qh.queue)) {
1184 _ep_nuke(mEp);
1185 retval = -EOVERFLOW;
Richard Zhao26c696c2012-07-07 22:56:40 +08001186 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
Alexander Shishkin0f089092012-05-08 23:29:02 +03001187 _usb_addr(mEp));
Pavankumar Kondeti76cd9cf2011-05-02 11:56:31 +05301188 }
David Lopoaa69a802008-11-17 14:14:51 -08001189 }
1190
1191 /* first nuke then test link, e.g. previous status has not sent */
1192 if (!list_empty(&mReq->queue)) {
1193 retval = -EBUSY;
Richard Zhao26c696c2012-07-07 22:56:40 +08001194 dev_err(mEp->ci->dev, "request already in queue\n");
David Lopoaa69a802008-11-17 14:14:51 -08001195 goto done;
1196 }
1197
Alexander Shishkin1155a7b2012-05-08 23:28:57 +03001198 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1199 req->length = 4 * CI13XXX_PAGE_SIZE;
David Lopoaa69a802008-11-17 14:14:51 -08001200 retval = -EMSGSIZE;
Richard Zhao26c696c2012-07-07 22:56:40 +08001201 dev_warn(mEp->ci->dev, "request length truncated\n");
David Lopoaa69a802008-11-17 14:14:51 -08001202 }
1203
1204 dbg_queue(_usb_addr(mEp), req, retval);
1205
1206 /* push request */
1207 mReq->req.status = -EINPROGRESS;
1208 mReq->req.actual = 0;
David Lopoaa69a802008-11-17 14:14:51 -08001209
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301210 retval = _hardware_enqueue(mEp, mReq);
Artem Leonenkod9bb9c12010-12-14 23:45:50 -08001211
1212 if (retval == -EALREADY) {
David Lopoaa69a802008-11-17 14:14:51 -08001213 dbg_event(_usb_addr(mEp), "QUEUE", retval);
1214 retval = 0;
1215 }
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301216 if (!retval)
1217 list_add_tail(&mReq->queue, &mEp->qh.queue);
David Lopoaa69a802008-11-17 14:14:51 -08001218
1219 done:
1220 spin_unlock_irqrestore(mEp->lock, flags);
1221 return retval;
1222}
1223
1224/**
1225 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1226 *
1227 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1228 */
1229static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1230{
1231 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1232 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1233 unsigned long flags;
1234
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301235 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001236 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
Pavankumar Kondeti0e6ca192011-02-18 17:43:16 +05301237 list_empty(&mEp->qh.queue))
David Lopoaa69a802008-11-17 14:14:51 -08001238 return -EINVAL;
1239
1240 spin_lock_irqsave(mEp->lock, flags);
1241
1242 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
1243
Richard Zhao26c696c2012-07-07 22:56:40 +08001244 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001245
1246 /* pop request */
1247 list_del_init(&mReq->queue);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001248
Richard Zhao26c696c2012-07-07 22:56:40 +08001249 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
Alexander Shishkin5e0aa492012-05-11 17:25:56 +03001250
David Lopoaa69a802008-11-17 14:14:51 -08001251 req->status = -ECONNRESET;
1252
Artem Leonenko7c25a822010-12-14 23:46:55 -08001253 if (mReq->req.complete != NULL) {
David Lopoaa69a802008-11-17 14:14:51 -08001254 spin_unlock(mEp->lock);
1255 mReq->req.complete(&mEp->ep, &mReq->req);
1256 spin_lock(mEp->lock);
1257 }
1258
1259 spin_unlock_irqrestore(mEp->lock, flags);
1260 return 0;
1261}
1262
1263/**
1264 * ep_set_halt: sets the endpoint halt feature
1265 *
1266 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1267 */
1268static int ep_set_halt(struct usb_ep *ep, int value)
1269{
1270 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1271 int direction, retval = 0;
1272 unsigned long flags;
1273
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001274 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001275 return -EINVAL;
1276
1277 spin_lock_irqsave(mEp->lock, flags);
1278
1279#ifndef STALL_IN
1280 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1281 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301282 !list_empty(&mEp->qh.queue)) {
David Lopoaa69a802008-11-17 14:14:51 -08001283 spin_unlock_irqrestore(mEp->lock, flags);
1284 return -EAGAIN;
1285 }
1286#endif
1287
1288 direction = mEp->dir;
1289 do {
1290 dbg_event(_usb_addr(mEp), "HALT", value);
Richard Zhao26c696c2012-07-07 22:56:40 +08001291 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
David Lopoaa69a802008-11-17 14:14:51 -08001292
1293 if (!value)
1294 mEp->wedge = 0;
1295
1296 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1297 mEp->dir = (mEp->dir == TX) ? RX : TX;
1298
1299 } while (mEp->dir != direction);
1300
1301 spin_unlock_irqrestore(mEp->lock, flags);
1302 return retval;
1303}
1304
1305/**
1306 * ep_set_wedge: sets the halt feature and ignores clear requests
1307 *
1308 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1309 */
1310static int ep_set_wedge(struct usb_ep *ep)
1311{
1312 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1313 unsigned long flags;
1314
Ido Shayevitz31fb6012012-03-12 20:25:23 +02001315 if (ep == NULL || mEp->ep.desc == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001316 return -EINVAL;
1317
1318 spin_lock_irqsave(mEp->lock, flags);
1319
1320 dbg_event(_usb_addr(mEp), "WEDGE", 0);
1321 mEp->wedge = 1;
1322
1323 spin_unlock_irqrestore(mEp->lock, flags);
1324
1325 return usb_ep_set_halt(ep);
1326}
1327
1328/**
1329 * ep_fifo_flush: flushes contents of a fifo
1330 *
1331 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1332 */
1333static void ep_fifo_flush(struct usb_ep *ep)
1334{
1335 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1336 unsigned long flags;
1337
David Lopoaa69a802008-11-17 14:14:51 -08001338 if (ep == NULL) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001339 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
David Lopoaa69a802008-11-17 14:14:51 -08001340 return;
1341 }
1342
1343 spin_lock_irqsave(mEp->lock, flags);
1344
1345 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
Richard Zhao26c696c2012-07-07 22:56:40 +08001346 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
David Lopoaa69a802008-11-17 14:14:51 -08001347
1348 spin_unlock_irqrestore(mEp->lock, flags);
1349}
1350
1351/**
1352 * Endpoint-specific part of the API to the USB controller hardware
1353 * Check "usb_gadget.h" for details
1354 */
1355static const struct usb_ep_ops usb_ep_ops = {
1356 .enable = ep_enable,
1357 .disable = ep_disable,
1358 .alloc_request = ep_alloc_request,
1359 .free_request = ep_free_request,
1360 .queue = ep_queue,
1361 .dequeue = ep_dequeue,
1362 .set_halt = ep_set_halt,
1363 .set_wedge = ep_set_wedge,
1364 .fifo_flush = ep_fifo_flush,
1365};
1366
1367/******************************************************************************
1368 * GADGET block
1369 *****************************************************************************/
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301370static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1371{
Richard Zhao26c696c2012-07-07 22:56:40 +08001372 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301373 unsigned long flags;
1374 int gadget_ready = 0;
1375
Richard Zhao26c696c2012-07-07 22:56:40 +08001376 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301377 return -EOPNOTSUPP;
1378
Richard Zhao26c696c2012-07-07 22:56:40 +08001379 spin_lock_irqsave(&ci->lock, flags);
1380 ci->vbus_active = is_active;
1381 if (ci->driver)
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301382 gadget_ready = 1;
Richard Zhao26c696c2012-07-07 22:56:40 +08001383 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301384
1385 if (gadget_ready) {
1386 if (is_active) {
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301387 pm_runtime_get_sync(&_gadget->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001388 hw_device_reset(ci, USBMODE_CM_DC);
Richard Zhao8c4fc032012-09-12 14:58:09 +03001389 hw_enable_vbus_intr(ci);
Richard Zhao26c696c2012-07-07 22:56:40 +08001390 hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301391 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001392 hw_device_state(ci, 0);
1393 if (ci->platdata->notify_event)
1394 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301395 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001396 _gadget_stop_activity(&ci->gadget);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301397 pm_runtime_put_sync(&_gadget->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301398 }
1399 }
1400
1401 return 0;
1402}
1403
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301404static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1405{
Richard Zhao26c696c2012-07-07 22:56:40 +08001406 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301407 unsigned long flags;
1408 int ret = 0;
1409
Richard Zhao26c696c2012-07-07 22:56:40 +08001410 spin_lock_irqsave(&ci->lock, flags);
1411 if (!ci->remote_wakeup) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301412 ret = -EOPNOTSUPP;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301413 goto out;
1414 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001415 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301416 ret = -EINVAL;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301417 goto out;
1418 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001419 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301420out:
Richard Zhao26c696c2012-07-07 22:56:40 +08001421 spin_unlock_irqrestore(&ci->lock, flags);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301422 return ret;
1423}
1424
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301425static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1426{
Richard Zhao26c696c2012-07-07 22:56:40 +08001427 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301428
Richard Zhao26c696c2012-07-07 22:56:40 +08001429 if (ci->transceiver)
1430 return usb_phy_set_power(ci->transceiver, mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301431 return -ENOTSUPP;
1432}
1433
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001434/* Change Data+ pullup status
1435 * this func is used by usb_gadget_connect/disconnet
1436 */
1437static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1438{
1439 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1440
1441 if (is_on)
1442 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1443 else
1444 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1445
1446 return 0;
1447}
1448
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001449static int ci13xxx_start(struct usb_gadget *gadget,
1450 struct usb_gadget_driver *driver);
1451static int ci13xxx_stop(struct usb_gadget *gadget,
1452 struct usb_gadget_driver *driver);
David Lopoaa69a802008-11-17 14:14:51 -08001453/**
1454 * Device operations part of the API to the USB controller hardware,
1455 * which don't involve endpoints (or i/o)
1456 * Check "usb_gadget.h" for details
1457 */
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301458static const struct usb_gadget_ops usb_gadget_ops = {
1459 .vbus_session = ci13xxx_vbus_session,
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301460 .wakeup = ci13xxx_wakeup,
Michael Grzeschikc0a48e62012-09-12 14:58:01 +03001461 .pullup = ci13xxx_pullup,
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301462 .vbus_draw = ci13xxx_vbus_draw,
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001463 .udc_start = ci13xxx_start,
1464 .udc_stop = ci13xxx_stop,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301465};
David Lopoaa69a802008-11-17 14:14:51 -08001466
Richard Zhao26c696c2012-07-07 22:56:40 +08001467static int init_eps(struct ci13xxx *ci)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001468{
1469 int retval = 0, i, j;
1470
Richard Zhao26c696c2012-07-07 22:56:40 +08001471 for (i = 0; i < ci->hw_ep_max/2; i++)
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001472 for (j = RX; j <= TX; j++) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001473 int k = i + j * ci->hw_ep_max/2;
1474 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001475
1476 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1477 (j == TX) ? "in" : "out");
1478
Richard Zhao26c696c2012-07-07 22:56:40 +08001479 mEp->ci = ci;
1480 mEp->lock = &ci->lock;
1481 mEp->td_pool = ci->td_pool;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001482
1483 mEp->ep.name = mEp->name;
1484 mEp->ep.ops = &usb_ep_ops;
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001485 /*
1486 * for ep0: maxP defined in desc, for other
1487 * eps, maxP is set by epautoconfig() called
1488 * by gadget layer
1489 */
1490 mEp->ep.maxpacket = (unsigned short)~0;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001491
1492 INIT_LIST_HEAD(&mEp->qh.queue);
Richard Zhao26c696c2012-07-07 22:56:40 +08001493 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001494 &mEp->qh.dma);
1495 if (mEp->qh.ptr == NULL)
1496 retval = -ENOMEM;
1497 else
1498 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1499
1500 /*
1501 * set up shorthands for ep0 out and in endpoints,
1502 * don't add to gadget's ep_list
1503 */
1504 if (i == 0) {
1505 if (j == RX)
Richard Zhao26c696c2012-07-07 22:56:40 +08001506 ci->ep0out = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001507 else
Richard Zhao26c696c2012-07-07 22:56:40 +08001508 ci->ep0in = mEp;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001509
Michael Grzeschik7f67c382012-09-12 14:58:00 +03001510 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001511 continue;
1512 }
1513
Richard Zhao26c696c2012-07-07 22:56:40 +08001514 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001515 }
1516
1517 return retval;
1518}
1519
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001520static void destroy_eps(struct ci13xxx *ci)
1521{
1522 int i;
1523
1524 for (i = 0; i < ci->hw_ep_max; i++) {
1525 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1526
1527 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1528 }
1529}
1530
David Lopoaa69a802008-11-17 14:14:51 -08001531/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001532 * ci13xxx_start: register a gadget driver
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001533 * @gadget: our gadget
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001534 * @driver: the driver being registered
David Lopoaa69a802008-11-17 14:14:51 -08001535 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001536 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08001537 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001538static int ci13xxx_start(struct usb_gadget *gadget,
1539 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001540{
Richard Zhao26c696c2012-07-07 22:56:40 +08001541 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Pavankumar Kondetica9cfea2011-01-11 09:19:22 +05301542 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001543 int retval = -ENOMEM;
1544
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001545 if (driver->disconnect == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001546 return -EINVAL;
David Lopoaa69a802008-11-17 14:14:51 -08001547
David Lopoaa69a802008-11-17 14:14:51 -08001548
Richard Zhao26c696c2012-07-07 22:56:40 +08001549 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1550 retval = usb_ep_enable(&ci->ep0out->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301551 if (retval)
1552 return retval;
Felipe Balbi877c1f52011-06-29 16:41:57 +03001553
Richard Zhao26c696c2012-07-07 22:56:40 +08001554 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1555 retval = usb_ep_enable(&ci->ep0in->ep);
Anji jonnalaac1aa6a2011-05-02 11:56:32 +05301556 if (retval)
1557 return retval;
Richard Zhao26c696c2012-07-07 22:56:40 +08001558 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001559
Richard Zhao26c696c2012-07-07 22:56:40 +08001560 ci->driver = driver;
1561 pm_runtime_get_sync(&ci->gadget.dev);
1562 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1563 if (ci->vbus_active) {
Richard Zhao8c4fc032012-09-12 14:58:09 +03001564 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001565 hw_device_reset(ci, USBMODE_CM_DC);
Richard Zhao8c4fc032012-09-12 14:58:09 +03001566 hw_enable_vbus_intr(ci);
1567 }
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301568 } else {
Richard Zhao26c696c2012-07-07 22:56:40 +08001569 pm_runtime_put_sync(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301570 goto done;
1571 }
1572 }
1573
Richard Zhao26c696c2012-07-07 22:56:40 +08001574 retval = hw_device_state(ci, ci->ep0out->qh.dma);
Pavankumar Kondetic0360192010-12-07 17:54:04 +05301575 if (retval)
Richard Zhao26c696c2012-07-07 22:56:40 +08001576 pm_runtime_put_sync(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001577
1578 done:
Richard Zhao26c696c2012-07-07 22:56:40 +08001579 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001580 return retval;
1581}
David Lopoaa69a802008-11-17 14:14:51 -08001582
1583/**
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001584 * ci13xxx_stop: unregister a gadget driver
David Lopoaa69a802008-11-17 14:14:51 -08001585 */
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001586static int ci13xxx_stop(struct usb_gadget *gadget,
1587 struct usb_gadget_driver *driver)
David Lopoaa69a802008-11-17 14:14:51 -08001588{
Richard Zhao26c696c2012-07-07 22:56:40 +08001589 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
Alexander Shishkin1f339d82012-05-08 23:29:04 +03001590 unsigned long flags;
David Lopoaa69a802008-11-17 14:14:51 -08001591
Richard Zhao26c696c2012-07-07 22:56:40 +08001592 spin_lock_irqsave(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001593
Richard Zhao26c696c2012-07-07 22:56:40 +08001594 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1595 ci->vbus_active) {
1596 hw_device_state(ci, 0);
1597 if (ci->platdata->notify_event)
1598 ci->platdata->notify_event(ci,
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301599 CI13XXX_CONTROLLER_STOPPED_EVENT);
Richard Zhao26c696c2012-07-07 22:56:40 +08001600 ci->driver = NULL;
1601 spin_unlock_irqrestore(&ci->lock, flags);
1602 _gadget_stop_activity(&ci->gadget);
1603 spin_lock_irqsave(&ci->lock, flags);
1604 pm_runtime_put(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301605 }
David Lopoaa69a802008-11-17 14:14:51 -08001606
Richard Zhao26c696c2012-07-07 22:56:40 +08001607 spin_unlock_irqrestore(&ci->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08001608
David Lopoaa69a802008-11-17 14:14:51 -08001609 return 0;
1610}
David Lopoaa69a802008-11-17 14:14:51 -08001611
1612/******************************************************************************
1613 * BUS block
1614 *****************************************************************************/
1615/**
Richard Zhao26c696c2012-07-07 22:56:40 +08001616 * udc_irq: ci interrupt handler
David Lopoaa69a802008-11-17 14:14:51 -08001617 *
1618 * This function returns IRQ_HANDLED if the IRQ has been handled
1619 * It locks access to registers
1620 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001621static irqreturn_t udc_irq(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001622{
David Lopoaa69a802008-11-17 14:14:51 -08001623 irqreturn_t retval;
1624 u32 intr;
1625
Richard Zhao26c696c2012-07-07 22:56:40 +08001626 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001627 return IRQ_HANDLED;
David Lopoaa69a802008-11-17 14:14:51 -08001628
Richard Zhao26c696c2012-07-07 22:56:40 +08001629 spin_lock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301630
Richard Zhao26c696c2012-07-07 22:56:40 +08001631 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1632 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
Alexander Shishkin758fc982012-05-11 17:25:53 +03001633 USBMODE_CM_DC) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001634 spin_unlock(&ci->lock);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301635 return IRQ_NONE;
1636 }
1637 }
Richard Zhao26c696c2012-07-07 22:56:40 +08001638 intr = hw_test_and_clear_intr_active(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001639 dbg_interrupt(intr);
David Lopoaa69a802008-11-17 14:14:51 -08001640
Alexander Shishkine443b332012-05-11 17:25:46 +03001641 if (intr) {
David Lopoaa69a802008-11-17 14:14:51 -08001642 /* order defines priority - do NOT change it */
Alexander Shishkine443b332012-05-11 17:25:46 +03001643 if (USBi_URI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001644 isr_reset_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001645
David Lopoaa69a802008-11-17 14:14:51 -08001646 if (USBi_PCI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001647 ci->gadget.speed = hw_port_is_high_speed(ci) ?
David Lopoaa69a802008-11-17 14:14:51 -08001648 USB_SPEED_HIGH : USB_SPEED_FULL;
Richard Zhao26c696c2012-07-07 22:56:40 +08001649 if (ci->suspended && ci->driver->resume) {
1650 spin_unlock(&ci->lock);
1651 ci->driver->resume(&ci->gadget);
1652 spin_lock(&ci->lock);
1653 ci->suspended = 0;
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301654 }
David Lopoaa69a802008-11-17 14:14:51 -08001655 }
Alexander Shishkine443b332012-05-11 17:25:46 +03001656
1657 if (USBi_UI & intr)
Richard Zhao26c696c2012-07-07 22:56:40 +08001658 isr_tr_complete_handler(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001659
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301660 if (USBi_SLI & intr) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001661 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1662 ci->driver->suspend) {
1663 ci->suspended = 1;
1664 spin_unlock(&ci->lock);
1665 ci->driver->suspend(&ci->gadget);
1666 spin_lock(&ci->lock);
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301667 }
Pavankumar Kondetie2b61c12011-02-18 17:43:17 +05301668 }
David Lopoaa69a802008-11-17 14:14:51 -08001669 retval = IRQ_HANDLED;
1670 } else {
David Lopoaa69a802008-11-17 14:14:51 -08001671 retval = IRQ_NONE;
1672 }
Richard Zhao8c4fc032012-09-12 14:58:09 +03001673
1674 intr = hw_read(ci, OP_OTGSC, ~0);
1675 hw_write(ci, OP_OTGSC, ~0, intr);
1676
1677 if (intr & (OTGSC_AVVIE & OTGSC_AVVIS))
1678 queue_work(ci->wq, &ci->vbus_work);
1679
Richard Zhao26c696c2012-07-07 22:56:40 +08001680 spin_unlock(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001681
1682 return retval;
1683}
1684
1685/**
1686 * udc_release: driver release function
1687 * @dev: device
1688 *
1689 * Currently does nothing
1690 */
1691static void udc_release(struct device *dev)
1692{
David Lopoaa69a802008-11-17 14:14:51 -08001693}
1694
1695/**
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001696 * udc_start: initialize gadget role
Richard Zhao26c696c2012-07-07 22:56:40 +08001697 * @ci: chipidea controller
David Lopoaa69a802008-11-17 14:14:51 -08001698 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001699static int udc_start(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001700{
Richard Zhao26c696c2012-07-07 22:56:40 +08001701 struct device *dev = ci->dev;
David Lopoaa69a802008-11-17 14:14:51 -08001702 int retval = 0;
1703
Richard Zhao26c696c2012-07-07 22:56:40 +08001704 spin_lock_init(&ci->lock);
David Lopoaa69a802008-11-17 14:14:51 -08001705
Richard Zhao26c696c2012-07-07 22:56:40 +08001706 ci->gadget.ops = &usb_gadget_ops;
1707 ci->gadget.speed = USB_SPEED_UNKNOWN;
1708 ci->gadget.max_speed = USB_SPEED_HIGH;
1709 ci->gadget.is_otg = 0;
1710 ci->gadget.name = ci->platdata->name;
David Lopoaa69a802008-11-17 14:14:51 -08001711
Richard Zhao26c696c2012-07-07 22:56:40 +08001712 INIT_LIST_HEAD(&ci->gadget.ep_list);
David Lopoaa69a802008-11-17 14:14:51 -08001713
Richard Zhao26c696c2012-07-07 22:56:40 +08001714 dev_set_name(&ci->gadget.dev, "gadget");
1715 ci->gadget.dev.dma_mask = dev->dma_mask;
1716 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1717 ci->gadget.dev.parent = dev;
1718 ci->gadget.dev.release = udc_release;
David Lopoaa69a802008-11-17 14:14:51 -08001719
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001720 /* alloc resources */
Richard Zhao26c696c2012-07-07 22:56:40 +08001721 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001722 sizeof(struct ci13xxx_qh),
1723 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001724 if (ci->qh_pool == NULL)
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001725 return -ENOMEM;
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001726
Richard Zhao26c696c2012-07-07 22:56:40 +08001727 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001728 sizeof(struct ci13xxx_td),
1729 64, CI13XXX_PAGE_SIZE);
Richard Zhao26c696c2012-07-07 22:56:40 +08001730 if (ci->td_pool == NULL) {
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001731 retval = -ENOMEM;
1732 goto free_qh_pool;
1733 }
1734
Richard Zhao26c696c2012-07-07 22:56:40 +08001735 retval = init_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001736 if (retval)
1737 goto free_pools;
1738
Richard Zhao26c696c2012-07-07 22:56:40 +08001739 ci->gadget.ep0 = &ci->ep0in->ep;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301740
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001741 if (ci->global_phy)
1742 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301743
Richard Zhao26c696c2012-07-07 22:56:40 +08001744 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1745 if (ci->transceiver == NULL) {
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301746 retval = -ENODEV;
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001747 goto destroy_eps;
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301748 }
1749 }
1750
Richard Zhao26c696c2012-07-07 22:56:40 +08001751 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1752 retval = hw_device_reset(ci, USBMODE_CM_DC);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301753 if (retval)
1754 goto put_transceiver;
Richard Zhao8c4fc032012-09-12 14:58:09 +03001755 hw_enable_vbus_intr(ci);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301756 }
1757
Richard Zhao26c696c2012-07-07 22:56:40 +08001758 retval = device_register(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301759 if (retval) {
Richard Zhao26c696c2012-07-07 22:56:40 +08001760 put_device(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301761 goto put_transceiver;
1762 }
David Lopoaa69a802008-11-17 14:14:51 -08001763
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001764 retval = dbg_create_files(ci->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301765 if (retval)
1766 goto unreg_device;
1767
Richard Zhao26c696c2012-07-07 22:56:40 +08001768 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1769 retval = otg_set_peripheral(ci->transceiver->otg,
1770 &ci->gadget);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301771 if (retval)
1772 goto remove_dbg;
David Lopoaa69a802008-11-17 14:14:51 -08001773 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001774
Richard Zhao26c696c2012-07-07 22:56:40 +08001775 retval = usb_add_gadget_udc(dev, &ci->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001776 if (retval)
1777 goto remove_trans;
1778
Richard Zhao26c696c2012-07-07 22:56:40 +08001779 pm_runtime_no_callbacks(&ci->gadget.dev);
1780 pm_runtime_enable(&ci->gadget.dev);
David Lopoaa69a802008-11-17 14:14:51 -08001781
David Lopoaa69a802008-11-17 14:14:51 -08001782 return retval;
1783
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001784remove_trans:
Richard Zhao26c696c2012-07-07 22:56:40 +08001785 if (!IS_ERR_OR_NULL(ci->transceiver)) {
Marc Kleine-Buddec9d1f942012-09-12 14:58:02 +03001786 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001787 if (ci->global_phy)
1788 usb_put_phy(ci->transceiver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001789 }
1790
Greg Kroah-Hartman0917ba82012-04-27 11:24:39 -07001791 dev_err(dev, "error = %i\n", retval);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301792remove_dbg:
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001793 dbg_remove_files(ci->dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301794unreg_device:
Richard Zhao26c696c2012-07-07 22:56:40 +08001795 device_unregister(&ci->gadget.dev);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301796put_transceiver:
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001797 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
Richard Zhao26c696c2012-07-07 22:56:40 +08001798 usb_put_phy(ci->transceiver);
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001799destroy_eps:
1800 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001801free_pools:
Richard Zhao26c696c2012-07-07 22:56:40 +08001802 dma_pool_destroy(ci->td_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001803free_qh_pool:
Richard Zhao26c696c2012-07-07 22:56:40 +08001804 dma_pool_destroy(ci->qh_pool);
David Lopoaa69a802008-11-17 14:14:51 -08001805 return retval;
1806}
1807
1808/**
1809 * udc_remove: parent remove must call this to remove UDC
1810 *
1811 * No interrupts active, the IRQ has been released
1812 */
Richard Zhao26c696c2012-07-07 22:56:40 +08001813static void udc_stop(struct ci13xxx *ci)
David Lopoaa69a802008-11-17 14:14:51 -08001814{
Richard Zhao26c696c2012-07-07 22:56:40 +08001815 if (ci == NULL)
David Lopoaa69a802008-11-17 14:14:51 -08001816 return;
Alexander Shishkin0f089092012-05-08 23:29:02 +03001817
Richard Zhao8c4fc032012-09-12 14:58:09 +03001818 hw_disable_vbus_intr(ci);
1819 cancel_work_sync(&ci->vbus_work);
1820
Richard Zhao26c696c2012-07-07 22:56:40 +08001821 usb_del_gadget_udc(&ci->gadget);
David Lopoaa69a802008-11-17 14:14:51 -08001822
Marc Kleine-Buddead6b1b92012-09-12 14:58:03 +03001823 destroy_eps(ci);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001824
Richard Zhao26c696c2012-07-07 22:56:40 +08001825 dma_pool_destroy(ci->td_pool);
1826 dma_pool_destroy(ci->qh_pool);
Alexander Shishkin790c2d52012-05-08 23:29:03 +03001827
Richard Zhao26c696c2012-07-07 22:56:40 +08001828 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1829 otg_set_peripheral(ci->transceiver->otg, NULL);
Richard Zhaoa2c3d692012-07-07 22:56:46 +08001830 if (ci->global_phy)
1831 usb_put_phy(ci->transceiver);
Pavankumar Kondetif01ef572010-12-07 17:54:02 +05301832 }
Felipe Balbi2b7dc3b2013-01-24 17:34:32 +02001833 dbg_remove_files(ci->dev);
Richard Zhao26c696c2012-07-07 22:56:40 +08001834 device_unregister(&ci->gadget.dev);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001835 /* my kobject is dynamic, I swear! */
Richard Zhao26c696c2012-07-07 22:56:40 +08001836 memset(&ci->gadget, 0, sizeof(ci->gadget));
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001837}
David Lopoaa69a802008-11-17 14:14:51 -08001838
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001839/**
1840 * ci_hdrc_gadget_init - initialize device related bits
1841 * ci: the controller
1842 *
1843 * This function enables the gadget role, if the device is "device capable".
1844 */
1845int ci_hdrc_gadget_init(struct ci13xxx *ci)
1846{
1847 struct ci_role_driver *rdrv;
1848
1849 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1850 return -ENXIO;
1851
1852 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1853 if (!rdrv)
1854 return -ENOMEM;
1855
1856 rdrv->start = udc_start;
1857 rdrv->stop = udc_stop;
1858 rdrv->irq = udc_irq;
1859 rdrv->name = "gadget";
1860 ci->roles[CI_ROLE_GADGET] = rdrv;
Richard Zhao8c4fc032012-09-12 14:58:09 +03001861 INIT_WORK(&ci->vbus_work, vbus_work);
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001862
1863 return 0;
David Lopoaa69a802008-11-17 14:14:51 -08001864}