ci13xxx_udc.c 73.6 KB
Newer Older
D
David Lopo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * ci13xxx_udc.c - MIPS USB IP core family device controller
 *
 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
 *
 * Author: David Lopo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * Description: MIPS USB IP core family device controller
 *              Currently it only supports IP part number CI13412
 *
 * This driver is composed of several blocks:
 * - HW:     hardware interface
 * - DBG:    debug facilities (optional)
 * - UTIL:   utilities
 * - ISR:    interrupts handling
 * - ENDPT:  endpoint operations (Gadget API)
 * - GADGET: gadget operations (Gadget API)
 * - BUS:    bus glue code, bus abstraction layer
 *
 * Compile Options
 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
 * - STALL_IN:  non-empty bulk-in pipes cannot be halted
 *              if defined mass storage compliance succeeds but with warnings
 *              => case 4: Hi >  Dn
 *              => case 5: Hi >  Di
 *              => case 8: Hi <> Do
 *              if undefined usbtest 13 fails
 * - TRACE:     enable function tracing (depends on DEBUG)
 *
 * Main Features
 * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
 * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
 * - Normal & LPM support
 *
 * USBTEST Report
 * - OK: 0-12, 13 (STALL_IN defined) & 14
 * - Not Supported: 15 & 16 (ISO)
 *
 * TODO List
 * - OTG
 * - Isochronous & Interrupt Traffic
 * - Handle requests which spawns into several TDs
 * - GET_STATUS(device) - always reports 0
 * - Gadget API (majority of optional features)
 * - Suspend & Remote Wakeup
 */
53
#include <linux/delay.h>
D
David Lopo 已提交
54 55 56 57
#include <linux/device.h>
#include <linux/dmapool.h>
#include <linux/dma-mapping.h>
#include <linux/init.h>
58 59
#include <linux/platform_device.h>
#include <linux/module.h>
D
David Lopo 已提交
60 61 62 63
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/kernel.h>
64
#include <linux/slab.h>
65
#include <linux/pm_runtime.h>
D
David Lopo 已提交
66 67
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
68
#include <linux/usb/otg.h>
D
David Lopo 已提交
69 70 71 72 73 74

#include "ci13xxx_udc.h"

/******************************************************************************
 * DEFINE
 *****************************************************************************/
75 76 77

#define DMA_ADDR_INVALID	(~(dma_addr_t)0)

D
David Lopo 已提交
78 79
/* control endpoint description */
static const struct usb_endpoint_descriptor
80
ctrl_endpt_out_desc = {
D
David Lopo 已提交
81 82 83
	.bLength         = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,

84 85 86 87 88 89 90 91 92 93 94
	.bEndpointAddress = USB_DIR_OUT,
	.bmAttributes    = USB_ENDPOINT_XFER_CONTROL,
	.wMaxPacketSize  = cpu_to_le16(CTRL_PAYLOAD_MAX),
};

static const struct usb_endpoint_descriptor
ctrl_endpt_in_desc = {
	.bLength         = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,

	.bEndpointAddress = USB_DIR_IN,
D
David Lopo 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	.bmAttributes    = USB_ENDPOINT_XFER_CONTROL,
	.wMaxPacketSize  = cpu_to_le16(CTRL_PAYLOAD_MAX),
};

/* Interrupt statistics */
#define ISR_MASK   0x1F
static struct {
	u32 test;
	u32 ui;
	u32 uei;
	u32 pci;
	u32 uri;
	u32 sli;
	u32 none;
	struct {
		u32 cnt;
		u32 buf[ISR_MASK+1];
		u32 idx;
	} hndl;
} isr_statistics;

/**
 * ffs_nr: find first (least significant) bit set
 * @x: the word to search
 *
 * This function returns bit number (instead of position)
 */
static int ffs_nr(u32 x)
{
	int n = ffs(x);

	return n ? n-1 : 32;
}

/******************************************************************************
 * HW block
 *****************************************************************************/

133 134 135
/* MSM specific */
#define ABS_AHBBURST        (0x0090UL)
#define ABS_AHBMODE         (0x0098UL)
D
David Lopo 已提交
136
/* UDC register map */
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static uintptr_t ci_regs_nolpm[] = {
	[CAP_CAPLENGTH]		= 0x000UL,
	[CAP_HCCPARAMS]		= 0x008UL,
	[CAP_DCCPARAMS]		= 0x024UL,
	[CAP_TESTMODE]		= 0x038UL,
	[OP_USBCMD]		= 0x000UL,
	[OP_USBSTS]		= 0x004UL,
	[OP_USBINTR]		= 0x008UL,
	[OP_DEVICEADDR]		= 0x014UL,
	[OP_ENDPTLISTADDR]	= 0x018UL,
	[OP_PORTSC]		= 0x044UL,
	[OP_DEVLC]		= 0x084UL,
	[OP_USBMODE]		= 0x068UL,
	[OP_ENDPTSETUPSTAT]	= 0x06CUL,
	[OP_ENDPTPRIME]		= 0x070UL,
	[OP_ENDPTFLUSH]		= 0x074UL,
	[OP_ENDPTSTAT]		= 0x078UL,
	[OP_ENDPTCOMPLETE]	= 0x07CUL,
	[OP_ENDPTCTRL]		= 0x080UL,
};

static uintptr_t ci_regs_lpm[] = {
	[CAP_CAPLENGTH]		= 0x000UL,
	[CAP_HCCPARAMS]		= 0x008UL,
	[CAP_DCCPARAMS]		= 0x024UL,
	[CAP_TESTMODE]		= 0x0FCUL,
	[OP_USBCMD]		= 0x000UL,
	[OP_USBSTS]		= 0x004UL,
	[OP_USBINTR]		= 0x008UL,
	[OP_DEVICEADDR]		= 0x014UL,
	[OP_ENDPTLISTADDR]	= 0x018UL,
	[OP_PORTSC]		= 0x044UL,
	[OP_DEVLC]		= 0x084UL,
	[OP_USBMODE]		= 0x0C8UL,
	[OP_ENDPTSETUPSTAT]	= 0x0D8UL,
	[OP_ENDPTPRIME]		= 0x0DCUL,
	[OP_ENDPTFLUSH]		= 0x0E0UL,
	[OP_ENDPTSTAT]		= 0x0E4UL,
	[OP_ENDPTCOMPLETE]	= 0x0E8UL,
	[OP_ENDPTCTRL]		= 0x0ECUL,
};

static int hw_alloc_regmap(struct ci13xxx *udc, bool is_lpm)
{
	int i;

	kfree(udc->hw_bank.regmap);

	udc->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
				      GFP_KERNEL);
	if (!udc->hw_bank.regmap)
		return -ENOMEM;

	for (i = 0; i < OP_ENDPTCTRL; i++)
		udc->hw_bank.regmap[i] =
			(i <= CAP_LAST ? udc->hw_bank.cap : udc->hw_bank.op) +
			(is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);

	for (; i <= OP_LAST; i++)
		udc->hw_bank.regmap[i] = udc->hw_bank.op +
			4 * (i - OP_ENDPTCTRL) +
			(is_lpm
			 ? ci_regs_lpm[OP_ENDPTCTRL]
			 : ci_regs_nolpm[OP_ENDPTCTRL]);

	return 0;
}
D
David Lopo 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216

/**
 * hw_ep_bit: calculates the bit number
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns bit number
 */
static inline int hw_ep_bit(int num, int dir)
{
	return num + (dir ? 16 : 0);
}

217
static int ep_to_bit(struct ci13xxx *udc, int n)
218
{
219
	int fill = 16 - udc->hw_ep_max / 2;
220

221
	if (n >= udc->hw_ep_max / 2)
222 223 224 225 226
		n += fill;

	return n;
}

D
David Lopo 已提交
227
/**
228 229
 * hw_read: reads from a hw register
 * @reg:  register index
D
David Lopo 已提交
230 231
 * @mask: bitfield mask
 *
232
 * This function returns register contents
D
David Lopo 已提交
233
 */
234
static u32 hw_read(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask)
D
David Lopo 已提交
235
{
236
	return ioread32(udc->hw_bank.regmap[reg]) & mask;
D
David Lopo 已提交
237 238 239
}

/**
240 241
 * hw_write: writes to a hw register
 * @reg:  register index
D
David Lopo 已提交
242
 * @mask: bitfield mask
243
 * @data: new value
D
David Lopo 已提交
244
 */
245 246
static void hw_write(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask,
		     u32 data)
D
David Lopo 已提交
247
{
248 249 250 251 252
	if (~mask)
		data = (ioread32(udc->hw_bank.regmap[reg]) & ~mask)
			| (data & mask);

	iowrite32(data, udc->hw_bank.regmap[reg]);
D
David Lopo 已提交
253 254 255
}

/**
256 257
 * hw_test_and_clear: tests & clears a hw register
 * @reg:  register index
D
David Lopo 已提交
258 259
 * @mask: bitfield mask
 *
260
 * This function returns register contents
D
David Lopo 已提交
261
 */
262 263
static u32 hw_test_and_clear(struct ci13xxx *udc, enum ci13xxx_regs reg,
			     u32 mask)
D
David Lopo 已提交
264
{
265
	u32 val = ioread32(udc->hw_bank.regmap[reg]) & mask;
D
David Lopo 已提交
266

267 268
	iowrite32(val, udc->hw_bank.regmap[reg]);
	return val;
D
David Lopo 已提交
269 270 271
}

/**
272 273
 * hw_test_and_write: tests & writes a hw register
 * @reg:  register index
D
David Lopo 已提交
274
 * @mask: bitfield mask
275
 * @data: new value
D
David Lopo 已提交
276
 *
277
 * This function returns register contents
D
David Lopo 已提交
278
 */
279 280
static u32 hw_test_and_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
			     u32 mask, u32 data)
D
David Lopo 已提交
281
{
282
	u32 val = hw_read(udc, reg, ~0);
D
David Lopo 已提交
283

284 285
	hw_write(udc, reg, mask, data);
	return (val & mask) >> ffs_nr(mask);
D
David Lopo 已提交
286 287
}

288 289
static int hw_device_init(struct ci13xxx *udc, void __iomem *base,
			  uintptr_t cap_offset)
D
David Lopo 已提交
290 291 292 293
{
	u32 reg;

	/* bank is a module variable */
294
	udc->hw_bank.abs = base;
D
David Lopo 已提交
295

296 297 298
	udc->hw_bank.cap = udc->hw_bank.abs;
	udc->hw_bank.cap += cap_offset;
	udc->hw_bank.op = udc->hw_bank.cap + ioread8(udc->hw_bank.cap);
D
David Lopo 已提交
299

300 301
	hw_alloc_regmap(udc, false);
	reg = hw_read(udc, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
302
		ffs_nr(HCCPARAMS_LEN);
303
	udc->hw_bank.lpm  = reg;
304
	hw_alloc_regmap(udc, !!reg);
305 306 307
	udc->hw_bank.size = udc->hw_bank.op - udc->hw_bank.abs;
	udc->hw_bank.size += OP_LAST;
	udc->hw_bank.size /= sizeof(u32);
D
David Lopo 已提交
308

309
	reg = hw_read(udc, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
310
		ffs_nr(DCCPARAMS_DEN);
311
	udc->hw_ep_max = reg * 2;   /* cache hw ENDPT_MAX */
312

313
	if (udc->hw_ep_max == 0 || udc->hw_ep_max > ENDPT_MAX)
314
		return -ENODEV;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

	/* setup lock mode ? */

	/* ENDPTSETUPSTAT is '0' by default */

	/* HCSPARAMS.bf.ppc SHOULD BE zero for device */

	return 0;
}
/**
 * hw_device_reset: resets chip (execute without interruption)
 * @base: register base address
 *
 * This function returns an error code
 */
static int hw_device_reset(struct ci13xxx *udc)
{
D
David Lopo 已提交
332
	/* should flush & stop before reset */
333 334
	hw_write(udc, OP_ENDPTFLUSH, ~0, ~0);
	hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
D
David Lopo 已提交
335

336 337
	hw_write(udc, OP_USBCMD, USBCMD_RST, USBCMD_RST);
	while (hw_read(udc, OP_USBCMD, USBCMD_RST))
D
David Lopo 已提交
338 339
		udelay(10);             /* not RTOS friendly */

340 341 342 343 344

	if (udc->udc_driver->notify_event)
		udc->udc_driver->notify_event(udc,
			CI13XXX_CONTROLLER_RESET_EVENT);

345
	if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
346
		hw_write(udc, OP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
347

D
David Lopo 已提交
348
	/* USBMODE should be configured step by step */
349 350
	hw_write(udc, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
	hw_write(udc, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
351
	/* HW >= 2.3 */
352
	hw_write(udc, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
D
David Lopo 已提交
353

354
	if (hw_read(udc, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
D
David Lopo 已提交
355
		pr_err("cannot enter in device mode");
356
		pr_err("lpm = %i", udc->hw_bank.lpm);
D
David Lopo 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369
		return -ENODEV;
	}

	return 0;
}

/**
 * hw_device_state: enables/disables interrupts & starts/stops device (execute
 *                  without interruption)
 * @dma: 0 => disable, !0 => enable and set dma engine
 *
 * This function returns an error code
 */
370
static int hw_device_state(struct ci13xxx *udc, u32 dma)
D
David Lopo 已提交
371 372
{
	if (dma) {
373
		hw_write(udc, OP_ENDPTLISTADDR, ~0, dma);
D
David Lopo 已提交
374
		/* interrupt, error, port change, reset, sleep/suspend */
375
		hw_write(udc, OP_USBINTR, ~0,
D
David Lopo 已提交
376
			     USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
377
		hw_write(udc, OP_USBCMD, USBCMD_RS, USBCMD_RS);
D
David Lopo 已提交
378
	} else {
379 380
		hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
		hw_write(udc, OP_USBINTR, ~0, 0);
D
David Lopo 已提交
381 382 383 384 385 386 387 388 389 390 391
	}
	return 0;
}

/**
 * hw_ep_flush: flush endpoint fifo (execute without interruption)
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
392
static int hw_ep_flush(struct ci13xxx *udc, int num, int dir)
D
David Lopo 已提交
393 394 395 396 397
{
	int n = hw_ep_bit(num, dir);

	do {
		/* flush any pending transfer */
398 399
		hw_write(udc, OP_ENDPTFLUSH, BIT(n), BIT(n));
		while (hw_read(udc, OP_ENDPTFLUSH, BIT(n)))
D
David Lopo 已提交
400
			cpu_relax();
401
	} while (hw_read(udc, OP_ENDPTSTAT, BIT(n)));
D
David Lopo 已提交
402 403 404 405 406 407 408 409 410 411 412

	return 0;
}

/**
 * hw_ep_disable: disables endpoint (execute without interruption)
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
413
static int hw_ep_disable(struct ci13xxx *udc, int num, int dir)
D
David Lopo 已提交
414
{
415
	hw_ep_flush(udc, num, dir);
416
	hw_write(udc, OP_ENDPTCTRL + num,
417
		 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
D
David Lopo 已提交
418 419 420 421 422 423 424 425 426 427 428
	return 0;
}

/**
 * hw_ep_enable: enables endpoint (execute without interruption)
 * @num:  endpoint number
 * @dir:  endpoint direction
 * @type: endpoint type
 *
 * This function returns an error code
 */
429
static int hw_ep_enable(struct ci13xxx *udc, int num, int dir, int type)
D
David Lopo 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{
	u32 mask, data;

	if (dir) {
		mask  = ENDPTCTRL_TXT;  /* type    */
		data  = type << ffs_nr(mask);

		mask |= ENDPTCTRL_TXS;  /* unstall */
		mask |= ENDPTCTRL_TXR;  /* reset data toggle */
		data |= ENDPTCTRL_TXR;
		mask |= ENDPTCTRL_TXE;  /* enable  */
		data |= ENDPTCTRL_TXE;
	} else {
		mask  = ENDPTCTRL_RXT;  /* type    */
		data  = type << ffs_nr(mask);

		mask |= ENDPTCTRL_RXS;  /* unstall */
		mask |= ENDPTCTRL_RXR;  /* reset data toggle */
		data |= ENDPTCTRL_RXR;
		mask |= ENDPTCTRL_RXE;  /* enable  */
		data |= ENDPTCTRL_RXE;
	}
452
	hw_write(udc, OP_ENDPTCTRL + num, mask, data);
D
David Lopo 已提交
453 454 455 456 457 458 459 460 461 462
	return 0;
}

/**
 * hw_ep_get_halt: return endpoint halt status
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns 1 if endpoint halted
 */
463
static int hw_ep_get_halt(struct ci13xxx *udc, int num, int dir)
D
David Lopo 已提交
464 465 466
{
	u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;

467
	return hw_read(udc, OP_ENDPTCTRL + num, mask) ? 1 : 0;
D
David Lopo 已提交
468 469 470 471 472
}

/**
 * hw_test_and_clear_setup_status: test & clear setup status (execute without
 *                                 interruption)
473
 * @n: endpoint number
D
David Lopo 已提交
474 475 476
 *
 * This function returns setup status
 */
477
static int hw_test_and_clear_setup_status(struct ci13xxx *udc, int n)
D
David Lopo 已提交
478
{
479
	n = ep_to_bit(udc, n);
480
	return hw_test_and_clear(udc, OP_ENDPTSETUPSTAT, BIT(n));
D
David Lopo 已提交
481 482 483 484 485 486 487 488 489 490
}

/**
 * hw_ep_prime: primes endpoint (execute without interruption)
 * @num:     endpoint number
 * @dir:     endpoint direction
 * @is_ctrl: true if control endpoint
 *
 * This function returns an error code
 */
491
static int hw_ep_prime(struct ci13xxx *udc, int num, int dir, int is_ctrl)
D
David Lopo 已提交
492 493 494
{
	int n = hw_ep_bit(num, dir);

495
	if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
D
David Lopo 已提交
496 497
		return -EAGAIN;

498
	hw_write(udc, OP_ENDPTPRIME, BIT(n), BIT(n));
D
David Lopo 已提交
499

500
	while (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
D
David Lopo 已提交
501
		cpu_relax();
502
	if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
D
David Lopo 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
		return -EAGAIN;

	/* status shoult be tested according with manual but it doesn't work */
	return 0;
}

/**
 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
 *                 without interruption)
 * @num:   endpoint number
 * @dir:   endpoint direction
 * @value: true => stall, false => unstall
 *
 * This function returns an error code
 */
518
static int hw_ep_set_halt(struct ci13xxx *udc, int num, int dir, int value)
D
David Lopo 已提交
519 520 521 522 523
{
	if (value != 0 && value != 1)
		return -EINVAL;

	do {
524
		enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
D
David Lopo 已提交
525 526 527 528
		u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
		u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;

		/* data toggle - reserved for EP0 but it's in ESS */
529 530
		hw_write(udc, reg, mask_xs|mask_xr,
			  value ? mask_xs : mask_xr);
531
	} while (value != hw_ep_get_halt(udc, num, dir));
D
David Lopo 已提交
532 533 534 535 536 537 538 539 540 541 542

	return 0;
}

/**
 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
 *                interruption)
 * @n: interrupt bit
 *
 * This function returns an error code
 */
543
static int hw_intr_clear(struct ci13xxx *udc, int n)
D
David Lopo 已提交
544 545 546 547
{
	if (n >= REG_BITS)
		return -EINVAL;

548 549
	hw_write(udc, OP_USBINTR, BIT(n), 0);
	hw_write(udc, OP_USBSTS,  BIT(n), BIT(n));
D
David Lopo 已提交
550 551 552 553 554 555 556 557 558 559
	return 0;
}

/**
 * hw_intr_force: enables interrupt & forces interrupt status (execute without
 *                interruption)
 * @n: interrupt bit
 *
 * This function returns an error code
 */
560
static int hw_intr_force(struct ci13xxx *udc, int n)
D
David Lopo 已提交
561 562 563 564
{
	if (n >= REG_BITS)
		return -EINVAL;

565 566 567 568
	hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
	hw_write(udc, OP_USBINTR,  BIT(n), BIT(n));
	hw_write(udc, OP_USBSTS,   BIT(n), BIT(n));
	hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, 0);
D
David Lopo 已提交
569 570 571 572 573 574 575 576
	return 0;
}

/**
 * hw_is_port_high_speed: test if port is high speed
 *
 * This function returns true if high speed port
 */
577
static int hw_port_is_high_speed(struct ci13xxx *udc)
D
David Lopo 已提交
578
{
579 580
	return udc->hw_bank.lpm ? hw_read(udc, OP_DEVLC, DEVLC_PSPD) :
		hw_read(udc, OP_PORTSC, PORTSC_HSP);
D
David Lopo 已提交
581 582 583 584 585 586 587
}

/**
 * hw_port_test_get: reads port test mode value
 *
 * This function returns port test mode value
 */
588
static u8 hw_port_test_get(struct ci13xxx *udc)
D
David Lopo 已提交
589
{
590
	return hw_read(udc, OP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
D
David Lopo 已提交
591 592 593 594 595 596 597 598
}

/**
 * hw_port_test_set: writes port test mode (execute without interruption)
 * @mode: new value
 *
 * This function returns an error code
 */
599
static int hw_port_test_set(struct ci13xxx *udc, u8 mode)
D
David Lopo 已提交
600 601 602 603 604 605
{
	const u8 TEST_MODE_MAX = 7;

	if (mode > TEST_MODE_MAX)
		return -EINVAL;

606
	hw_write(udc, OP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
D
David Lopo 已提交
607 608 609 610 611 612 613 614
	return 0;
}

/**
 * hw_read_intr_enable: returns interrupt enable register
 *
 * This function returns register data
 */
615
static u32 hw_read_intr_enable(struct ci13xxx *udc)
D
David Lopo 已提交
616
{
617
	return hw_read(udc, OP_USBINTR, ~0);
D
David Lopo 已提交
618 619 620 621 622 623 624
}

/**
 * hw_read_intr_status: returns interrupt status register
 *
 * This function returns register data
 */
625
static u32 hw_read_intr_status(struct ci13xxx *udc)
D
David Lopo 已提交
626
{
627
	return hw_read(udc, OP_USBSTS, ~0);
D
David Lopo 已提交
628 629 630 631 632 633 634 635 636
}

/**
 * hw_register_read: reads all device registers (execute without interruption)
 * @buf:  destination buffer
 * @size: buffer size
 *
 * This function returns number of registers read
 */
637
static size_t hw_register_read(struct ci13xxx *udc, u32 *buf, size_t size)
D
David Lopo 已提交
638 639 640
{
	unsigned i;

641 642
	if (size > udc->hw_bank.size)
		size = udc->hw_bank.size;
D
David Lopo 已提交
643 644

	for (i = 0; i < size; i++)
645
		buf[i] = hw_read(udc, i * sizeof(u32), ~0);
D
David Lopo 已提交
646 647 648 649 650 651 652 653 654 655 656

	return size;
}

/**
 * hw_register_write: writes to register
 * @addr: register address
 * @data: register value
 *
 * This function returns an error code
 */
657
static int hw_register_write(struct ci13xxx *udc, u16 addr, u32 data)
D
David Lopo 已提交
658 659 660 661
{
	/* align */
	addr /= sizeof(u32);

662
	if (addr >= udc->hw_bank.size)
D
David Lopo 已提交
663 664 665 666 667
		return -EINVAL;

	/* align */
	addr *= sizeof(u32);

668
	hw_write(udc, addr, ~0, data);
D
David Lopo 已提交
669 670 671 672 673 674
	return 0;
}

/**
 * hw_test_and_clear_complete: test & clear complete status (execute without
 *                             interruption)
675
 * @n: endpoint number
D
David Lopo 已提交
676 677 678
 *
 * This function returns complete status
 */
679
static int hw_test_and_clear_complete(struct ci13xxx *udc, int n)
D
David Lopo 已提交
680
{
681
	n = ep_to_bit(udc, n);
682
	return hw_test_and_clear(udc, OP_ENDPTCOMPLETE, BIT(n));
D
David Lopo 已提交
683 684 685 686 687 688 689 690
}

/**
 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
 *                                without interruption)
 *
 * This function returns active interrutps
 */
691
static u32 hw_test_and_clear_intr_active(struct ci13xxx *udc)
D
David Lopo 已提交
692
{
693
	u32 reg = hw_read_intr_status(udc) & hw_read_intr_enable(udc);
D
David Lopo 已提交
694

695
	hw_write(udc, OP_USBSTS, ~0, reg);
D
David Lopo 已提交
696 697 698 699 700 701 702 703 704
	return reg;
}

/**
 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
 *                                interruption)
 *
 * This function returns guard value
 */
705
static int hw_test_and_clear_setup_guard(struct ci13xxx *udc)
D
David Lopo 已提交
706
{
707
	return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, 0);
D
David Lopo 已提交
708 709 710 711 712 713 714 715
}

/**
 * hw_test_and_set_setup_guard: test & set setup guard (execute without
 *                              interruption)
 *
 * This function returns guard value
 */
716
static int hw_test_and_set_setup_guard(struct ci13xxx *udc)
D
David Lopo 已提交
717
{
718
	return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
D
David Lopo 已提交
719 720 721 722 723 724 725 726
}

/**
 * hw_usb_set_address: configures USB address (execute without interruption)
 * @value: new USB address
 *
 * This function returns an error code
 */
727
static int hw_usb_set_address(struct ci13xxx *udc, u8 value)
D
David Lopo 已提交
728 729
{
	/* advance */
730 731
	hw_write(udc, OP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
		  value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
D
David Lopo 已提交
732 733 734 735 736 737 738 739 740
	return 0;
}

/**
 * hw_usb_reset: restart device after a bus reset (execute without
 *               interruption)
 *
 * This function returns an error code
 */
741
static int hw_usb_reset(struct ci13xxx *udc)
D
David Lopo 已提交
742
{
743
	hw_usb_set_address(udc, 0);
D
David Lopo 已提交
744 745

	/* ESS flushes only at end?!? */
746
	hw_write(udc, OP_ENDPTFLUSH,    ~0, ~0);
D
David Lopo 已提交
747 748

	/* clear setup token semaphores */
749
	hw_write(udc, OP_ENDPTSETUPSTAT, 0,  0);
D
David Lopo 已提交
750 751

	/* clear complete status */
752
	hw_write(udc, OP_ENDPTCOMPLETE,  0,  0);
D
David Lopo 已提交
753 754

	/* wait until all bits cleared */
755
	while (hw_read(udc, OP_ENDPTPRIME, ~0))
D
David Lopo 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
		udelay(10);             /* not RTOS friendly */

	/* reset all endpoints ? */

	/* reset internal status and wait for further instructions
	   no need to verify the port reset status (ESS does it) */

	return 0;
}

/******************************************************************************
 * DBG block
 *****************************************************************************/
/**
 * show_device: prints information about device capabilities and status
 *
 * Check "device.h" for details
 */
static ssize_t show_device(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	struct usb_gadget *gadget = &udc->gadget;
	int n = 0;

781
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
782
	if (attr == NULL || buf == NULL) {
783
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
784 785 786 787 788
		return 0;
	}

	n += scnprintf(buf + n, PAGE_SIZE - n, "speed             = %d\n",
		       gadget->speed);
789 790 791
	n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed         = %d\n",
		       gadget->max_speed);
	/* TODO: Scheduled for removal in 3.8. */
D
David Lopo 已提交
792
	n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed      = %d\n",
793
		       gadget_is_dualspeed(gadget));
D
David Lopo 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg            = %d\n",
		       gadget->is_otg);
	n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral   = %d\n",
		       gadget->is_a_peripheral);
	n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable      = %d\n",
		       gadget->b_hnp_enable);
	n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support     = %d\n",
		       gadget->a_hnp_support);
	n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
		       gadget->a_alt_hnp_support);
	n += scnprintf(buf + n, PAGE_SIZE - n, "name              = %s\n",
		       (gadget->name ? gadget->name : ""));

	return n;
}
static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);

/**
 * show_driver: prints information about attached gadget (if any)
 *
 * Check "device.h" for details
 */
static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	struct usb_gadget_driver *driver = udc->driver;
	int n = 0;

823
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
824 825 826 827 828 829 830 831 832 833 834 835
	if (attr == NULL || buf == NULL) {
		dev_err(dev, "[%s] EINVAL\n", __func__);
		return 0;
	}

	if (driver == NULL)
		return scnprintf(buf, PAGE_SIZE,
				 "There is no gadget attached!\n");

	n += scnprintf(buf + n, PAGE_SIZE - n, "function  = %s\n",
		       (driver->function ? driver->function : ""));
	n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
836
		       driver->max_speed);
D
David Lopo 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

	return n;
}
static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);

/* Maximum event message length */
#define DBG_DATA_MSG   64UL

/* Maximum event messages */
#define DBG_DATA_MAX   128UL

/* Event buffer descriptor */
static struct {
	char     (buf[DBG_DATA_MAX])[DBG_DATA_MSG];   /* buffer */
	unsigned idx;   /* index */
	unsigned tty;   /* print to console? */
	rwlock_t lck;   /* lock */
} dbg_data = {
	.idx = 0,
	.tty = 0,
	.lck = __RW_LOCK_UNLOCKED(lck)
};

/**
 * dbg_dec: decrements debug event index
 * @idx: buffer index
 */
static void dbg_dec(unsigned *idx)
{
	*idx = (*idx - 1) & (DBG_DATA_MAX-1);
}

/**
 * dbg_inc: increments debug event index
 * @idx: buffer index
 */
static void dbg_inc(unsigned *idx)
{
	*idx = (*idx + 1) & (DBG_DATA_MAX-1);
}

/**
 * dbg_print:  prints the common part of the event
 * @addr:   endpoint address
 * @name:   event name
 * @status: status
 * @extra:  extra information
 */
static void dbg_print(u8 addr, const char *name, int status, const char *extra)
{
	struct timeval tval;
	unsigned int stamp;
	unsigned long flags;

	write_lock_irqsave(&dbg_data.lck, flags);

	do_gettimeofday(&tval);
	stamp = tval.tv_sec & 0xFFFF;	/* 2^32 = 4294967296. Limit to 4096s */
	stamp = stamp * 1000000 + tval.tv_usec;

	scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
898
		  "%04X\t? %02X %-7.7s %4i ?\t%s\n",
D
David Lopo 已提交
899 900 901 902 903 904 905
		  stamp, addr, name, status, extra);

	dbg_inc(&dbg_data.idx);

	write_unlock_irqrestore(&dbg_data.lck, flags);

	if (dbg_data.tty != 0)
906
		pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
D
David Lopo 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
			  stamp, addr, name, status, extra);
}

/**
 * dbg_done: prints a DONE event
 * @addr:   endpoint address
 * @td:     transfer descriptor
 * @status: status
 */
static void dbg_done(u8 addr, const u32 token, int status)
{
	char msg[DBG_DATA_MSG];

	scnprintf(msg, sizeof(msg), "%d %02X",
		  (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
		  (int)(token & TD_STATUS)      >> ffs_nr(TD_STATUS));
	dbg_print(addr, "DONE", status, msg);
}

/**
 * dbg_event: prints a generic event
 * @addr:   endpoint address
 * @name:   event name
 * @status: status
 */
static void dbg_event(u8 addr, const char *name, int status)
{
	if (name != NULL)
		dbg_print(addr, name, status, "");
}

/*
 * dbg_queue: prints a QUEUE event
 * @addr:   endpoint address
 * @req:    USB request
 * @status: status
 */
static void dbg_queue(u8 addr, const struct usb_request *req, int status)
{
	char msg[DBG_DATA_MSG];

	if (req != NULL) {
		scnprintf(msg, sizeof(msg),
			  "%d %d", !req->no_interrupt, req->length);
		dbg_print(addr, "QUEUE", status, msg);
	}
}

/**
 * dbg_setup: prints a SETUP event
 * @addr: endpoint address
 * @req:  setup request
 */
static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
{
	char msg[DBG_DATA_MSG];

	if (req != NULL) {
		scnprintf(msg, sizeof(msg),
			  "%02X %02X %04X %04X %d", req->bRequestType,
			  req->bRequest, le16_to_cpu(req->wValue),
			  le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
		dbg_print(addr, "SETUP", 0, msg);
	}
}

/**
 * show_events: displays the event buffer
 *
 * Check "device.h" for details
 */
static ssize_t show_events(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	unsigned long flags;
	unsigned i, j, n = 0;

984
	trace(dev->parent, "%p\n", buf);
D
David Lopo 已提交
985
	if (attr == NULL || buf == NULL) {
986
		dev_err(dev->parent, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
		return 0;
	}

	read_lock_irqsave(&dbg_data.lck, flags);

	i = dbg_data.idx;
	for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
		n += strlen(dbg_data.buf[i]);
		if (n >= PAGE_SIZE) {
			n -= strlen(dbg_data.buf[i]);
			break;
		}
	}
	for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
		j += scnprintf(buf + j, PAGE_SIZE - j,
			       "%s", dbg_data.buf[i]);

	read_unlock_irqrestore(&dbg_data.lck, flags);

	return n;
}

/**
 * store_events: configure if events are going to be also printed to console
 *
 * Check "device.h" for details
 */
static ssize_t store_events(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	unsigned tty;

1019
	trace(dev->parent, "[%s] %p, %d\n", __func__, buf, count);
D
David Lopo 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	if (attr == NULL || buf == NULL) {
		dev_err(dev, "[%s] EINVAL\n", __func__);
		goto done;
	}

	if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
		dev_err(dev, "<1|0>: enable|disable console log\n");
		goto done;
	}

	dbg_data.tty = tty;
	dev_info(dev, "tty = %u", dbg_data.tty);

 done:
	return count;
}
static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);

/**
 * show_inters: interrupt status, enable status and historic
 *
 * Check "device.h" for details
 */
static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	u32 intr;
	unsigned i, j, n = 0;

1051
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
1052
	if (attr == NULL || buf == NULL) {
1053
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1054 1055 1056
		return 0;
	}

1057
	spin_lock_irqsave(&udc->lock, flags);
D
David Lopo 已提交
1058 1059

	n += scnprintf(buf + n, PAGE_SIZE - n,
1060
		       "status = %08x\n", hw_read_intr_status(udc));
D
David Lopo 已提交
1061
	n += scnprintf(buf + n, PAGE_SIZE - n,
1062
		       "enable = %08x\n", hw_read_intr_enable(udc));
D
David Lopo 已提交
1063 1064 1065

	n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
		       isr_statistics.test);
1066
	n += scnprintf(buf + n, PAGE_SIZE - n, "? ui  = %d\n",
D
David Lopo 已提交
1067
		       isr_statistics.ui);
1068
	n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
D
David Lopo 已提交
1069
		       isr_statistics.uei);
1070
	n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
D
David Lopo 已提交
1071
		       isr_statistics.pci);
1072
	n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
D
David Lopo 已提交
1073
		       isr_statistics.uri);
1074
	n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
D
David Lopo 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
		       isr_statistics.sli);
	n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
		       isr_statistics.none);
	n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
		       isr_statistics.hndl.cnt);

	for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
		i   &= ISR_MASK;
		intr = isr_statistics.hndl.buf[i];

		if (USBi_UI  & intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "ui  ");
		intr &= ~USBi_UI;
		if (USBi_UEI & intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
		intr &= ~USBi_UEI;
		if (USBi_PCI & intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
		intr &= ~USBi_PCI;
		if (USBi_URI & intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
		intr &= ~USBi_URI;
		if (USBi_SLI & intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
		intr &= ~USBi_SLI;
		if (intr)
			n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
		if (isr_statistics.hndl.buf[i])
			n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
	}

1106
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	return n;
}

/**
 * store_inters: enable & force or disable an individual interrutps
 *                   (to be used for test purposes only)
 *
 * Check "device.h" for details
 */
static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	unsigned en, bit;

1124
	trace(udc->dev, "%p, %d\n", buf, count);
D
David Lopo 已提交
1125
	if (attr == NULL || buf == NULL) {
1126
		dev_err(udc->dev, "EINVAL\n");
D
David Lopo 已提交
1127 1128 1129 1130
		goto done;
	}

	if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
1131
		dev_err(udc->dev, "<1|0> <bit>: enable|disable interrupt\n");
D
David Lopo 已提交
1132 1133 1134
		goto done;
	}

1135
	spin_lock_irqsave(&udc->lock, flags);
D
David Lopo 已提交
1136
	if (en) {
1137
		if (hw_intr_force(udc, bit))
D
David Lopo 已提交
1138 1139 1140 1141
			dev_err(dev, "invalid bit number\n");
		else
			isr_statistics.test++;
	} else {
1142
		if (hw_intr_clear(udc, bit))
D
David Lopo 已提交
1143 1144
			dev_err(dev, "invalid bit number\n");
	}
1145
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

 done:
	return count;
}
static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);

/**
 * show_port_test: reads port test mode
 *
 * Check "device.h" for details
 */
static ssize_t show_port_test(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	unsigned mode;

1164
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
1165
	if (attr == NULL || buf == NULL) {
1166
		dev_err(udc->dev, "EINVAL\n");
D
David Lopo 已提交
1167 1168 1169
		return 0;
	}

1170 1171 1172
	spin_lock_irqsave(&udc->lock, flags);
	mode = hw_port_test_get(udc);
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

	return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
}

/**
 * store_port_test: writes port test mode
 *
 * Check "device.h" for details
 */
static ssize_t store_port_test(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t count)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	unsigned mode;

1190
	trace(udc->dev, "%p, %d\n", buf, count);
D
David Lopo 已提交
1191
	if (attr == NULL || buf == NULL) {
1192
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1193 1194 1195 1196
		goto done;
	}

	if (sscanf(buf, "%u", &mode) != 1) {
1197
		dev_err(udc->dev, "<mode>: set port test mode");
D
David Lopo 已提交
1198 1199 1200
		goto done;
	}

1201 1202
	spin_lock_irqsave(&udc->lock, flags);
	if (hw_port_test_set(udc, mode))
1203
		dev_err(udc->dev, "invalid mode\n");
1204
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

 done:
	return count;
}
static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
		   show_port_test, store_port_test);

/**
 * show_qheads: DMA contents of all queue heads
 *
 * Check "device.h" for details
 */
static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	unsigned i, j, n = 0;

1224
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
1225
	if (attr == NULL || buf == NULL) {
1226
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1227 1228 1229
		return 0;
	}

1230 1231
	spin_lock_irqsave(&udc->lock, flags);
	for (i = 0; i < udc->hw_ep_max/2; i++) {
1232
		struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
1233 1234
		struct ci13xxx_ep *mEpTx =
			&udc->ci13xxx_ep[i + udc->hw_ep_max/2];
D
David Lopo 已提交
1235 1236
		n += scnprintf(buf + n, PAGE_SIZE - n,
			       "EP=%02i: RX=%08X TX=%08X\n",
1237
			       i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
D
David Lopo 已提交
1238 1239 1240
		for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
			n += scnprintf(buf + n, PAGE_SIZE - n,
				       " %04X:    %08X    %08X\n", j,
1241 1242
				       *((u32 *)mEpRx->qh.ptr + j),
				       *((u32 *)mEpTx->qh.ptr + j));
D
David Lopo 已提交
1243 1244
		}
	}
1245
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

	return n;
}
static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);

/**
 * show_registers: dumps all registers
 *
 * Check "device.h" for details
 */
1256
#define DUMP_ENTRIES	512
D
David Lopo 已提交
1257 1258 1259 1260 1261
static ssize_t show_registers(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
1262
	u32 *dump;
D
David Lopo 已提交
1263 1264
	unsigned i, k, n = 0;

1265
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
1266
	if (attr == NULL || buf == NULL) {
1267
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1268 1269 1270
		return 0;
	}

1271 1272
	dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
	if (!dump) {
1273
		dev_err(udc->dev, "%s: out of memory\n", __func__);
1274 1275 1276
		return 0;
	}

1277 1278 1279
	spin_lock_irqsave(&udc->lock, flags);
	k = hw_register_read(udc, dump, DUMP_ENTRIES);
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1280 1281 1282 1283 1284 1285

	for (i = 0; i < k; i++) {
		n += scnprintf(buf + n, PAGE_SIZE - n,
			       "reg[0x%04X] = 0x%08X\n",
			       i * (unsigned)sizeof(u32), dump[i]);
	}
1286
	kfree(dump);
D
David Lopo 已提交
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

	return n;
}

/**
 * store_registers: writes value to register address
 *
 * Check "device.h" for details
 */
static ssize_t store_registers(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t count)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long addr, data, flags;

1303
	trace(udc->dev, "%p, %d\n", buf, count);
D
David Lopo 已提交
1304
	if (attr == NULL || buf == NULL) {
1305
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1306 1307 1308 1309
		goto done;
	}

	if (sscanf(buf, "%li %li", &addr, &data) != 2) {
1310 1311
		dev_err(udc->dev,
			"<addr> <data>: write data to register address\n");
D
David Lopo 已提交
1312 1313 1314
		goto done;
	}

1315 1316
	spin_lock_irqsave(&udc->lock, flags);
	if (hw_register_write(udc, addr, data))
1317
		dev_err(udc->dev, "invalid address range\n");
1318
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

 done:
	return count;
}
static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
		   show_registers, store_registers);

/**
 * show_requests: DMA contents of all requests currently queued (all endpts)
 *
 * Check "device.h" for details
 */
static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
	struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
	unsigned long flags;
	struct list_head   *ptr = NULL;
	struct ci13xxx_req *req = NULL;
1338
	unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
D
David Lopo 已提交
1339

1340
	trace(udc->dev, "%p\n", buf);
D
David Lopo 已提交
1341
	if (attr == NULL || buf == NULL) {
1342
		dev_err(udc->dev, "[%s] EINVAL\n", __func__);
D
David Lopo 已提交
1343 1344 1345
		return 0;
	}

1346 1347
	spin_lock_irqsave(&udc->lock, flags);
	for (i = 0; i < udc->hw_ep_max; i++)
1348 1349 1350
		list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
		{
			req = list_entry(ptr, struct ci13xxx_req, queue);
D
David Lopo 已提交
1351

1352 1353
			n += scnprintf(buf + n, PAGE_SIZE - n,
					"EP=%02i: TD=%08X %s\n",
1354 1355
					i % udc->hw_ep_max/2, (u32)req->dma,
					((i < udc->hw_ep_max/2) ? "RX" : "TX"));
1356 1357

			for (j = 0; j < qSize; j++)
D
David Lopo 已提交
1358
				n += scnprintf(buf + n, PAGE_SIZE - n,
1359 1360 1361
						" %04X:    %08X\n", j,
						*((u32 *)req->ptr + j));
		}
1362
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

	return n;
}
static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);

/**
 * dbg_create_files: initializes the attribute interface
 * @dev: device
 *
 * This function returns an error code
 */
__maybe_unused static int dbg_create_files(struct device *dev)
{
	int retval = 0;

	if (dev == NULL)
		return -EINVAL;
	retval = device_create_file(dev, &dev_attr_device);
	if (retval)
		goto done;
	retval = device_create_file(dev, &dev_attr_driver);
	if (retval)
		goto rm_device;
	retval = device_create_file(dev, &dev_attr_events);
	if (retval)
		goto rm_driver;
	retval = device_create_file(dev, &dev_attr_inters);
	if (retval)
		goto rm_events;
	retval = device_create_file(dev, &dev_attr_port_test);
	if (retval)
		goto rm_inters;
	retval = device_create_file(dev, &dev_attr_qheads);
	if (retval)
		goto rm_port_test;
	retval = device_create_file(dev, &dev_attr_registers);
	if (retval)
		goto rm_qheads;
	retval = device_create_file(dev, &dev_attr_requests);
	if (retval)
		goto rm_registers;
	return 0;

 rm_registers:
	device_remove_file(dev, &dev_attr_registers);
 rm_qheads:
	device_remove_file(dev, &dev_attr_qheads);
 rm_port_test:
	device_remove_file(dev, &dev_attr_port_test);
 rm_inters:
	device_remove_file(dev, &dev_attr_inters);
 rm_events:
	device_remove_file(dev, &dev_attr_events);
 rm_driver:
	device_remove_file(dev, &dev_attr_driver);
 rm_device:
	device_remove_file(dev, &dev_attr_device);
 done:
	return retval;
}

/**
 * dbg_remove_files: destroys the attribute interface
 * @dev: device
 *
 * This function returns an error code
 */
__maybe_unused static int dbg_remove_files(struct device *dev)
{
	if (dev == NULL)
		return -EINVAL;
	device_remove_file(dev, &dev_attr_requests);
	device_remove_file(dev, &dev_attr_registers);
	device_remove_file(dev, &dev_attr_qheads);
	device_remove_file(dev, &dev_attr_port_test);
	device_remove_file(dev, &dev_attr_inters);
	device_remove_file(dev, &dev_attr_events);
	device_remove_file(dev, &dev_attr_driver);
	device_remove_file(dev, &dev_attr_device);
	return 0;
}

/******************************************************************************
 * UTIL block
 *****************************************************************************/
/**
 * _usb_addr: calculates endpoint address from direction & number
 * @ep:  endpoint
 */
static inline u8 _usb_addr(struct ci13xxx_ep *ep)
{
	return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
}

/**
 * _hardware_queue: configures a request at hardware level
 * @gadget: gadget
 * @mEp:    endpoint
 *
 * This function returns an error code
 */
static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
{
1466
	struct ci13xxx *udc = mEp->udc;
D
David Lopo 已提交
1467
	unsigned i;
1468 1469
	int ret = 0;
	unsigned length = mReq->req.length;
D
David Lopo 已提交
1470

1471
	trace(udc->dev, "%p, %p", mEp, mReq);
D
David Lopo 已提交
1472 1473 1474 1475 1476 1477

	/* don't queue twice */
	if (mReq->req.status == -EALREADY)
		return -EALREADY;

	mReq->req.status = -EALREADY;
1478
	if (length && mReq->req.dma == DMA_ADDR_INVALID) {
D
David Lopo 已提交
1479 1480
		mReq->req.dma = \
			dma_map_single(mEp->device, mReq->req.buf,
1481 1482
				       length, mEp->dir ? DMA_TO_DEVICE :
				       DMA_FROM_DEVICE);
D
David Lopo 已提交
1483 1484 1485 1486 1487 1488
		if (mReq->req.dma == 0)
			return -ENOMEM;

		mReq->map = 1;
	}

1489 1490 1491 1492 1493 1494 1495 1496
	if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
		mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
					   &mReq->zdma);
		if (mReq->zptr == NULL) {
			if (mReq->map) {
				dma_unmap_single(mEp->device, mReq->req.dma,
					length, mEp->dir ? DMA_TO_DEVICE :
					DMA_FROM_DEVICE);
1497
				mReq->req.dma = DMA_ADDR_INVALID;
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
				mReq->map     = 0;
			}
			return -ENOMEM;
		}
		memset(mReq->zptr, 0, sizeof(*mReq->zptr));
		mReq->zptr->next    = TD_TERMINATE;
		mReq->zptr->token   = TD_STATUS_ACTIVE;
		if (!mReq->req.no_interrupt)
			mReq->zptr->token   |= TD_IOC;
	}
D
David Lopo 已提交
1508 1509 1510 1511 1512
	/*
	 * TD configuration
	 * TODO - handle requests which spawns into several TDs
	 */
	memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1513
	mReq->ptr->token    = length << ffs_nr(TD_TOTAL_BYTES);
D
David Lopo 已提交
1514 1515
	mReq->ptr->token   &= TD_TOTAL_BYTES;
	mReq->ptr->token   |= TD_STATUS_ACTIVE;
1516 1517 1518 1519 1520 1521 1522
	if (mReq->zptr) {
		mReq->ptr->next    = mReq->zdma;
	} else {
		mReq->ptr->next    = TD_TERMINATE;
		if (!mReq->req.no_interrupt)
			mReq->ptr->token  |= TD_IOC;
	}
D
David Lopo 已提交
1523 1524 1525
	mReq->ptr->page[0]  = mReq->req.dma;
	for (i = 1; i < 5; i++)
		mReq->ptr->page[i] =
1526
			(mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
D
David Lopo 已提交
1527

1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	if (!list_empty(&mEp->qh.queue)) {
		struct ci13xxx_req *mReqPrev;
		int n = hw_ep_bit(mEp->num, mEp->dir);
		int tmp_stat;

		mReqPrev = list_entry(mEp->qh.queue.prev,
				struct ci13xxx_req, queue);
		if (mReqPrev->zptr)
			mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
		else
			mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
		wmb();
1540
		if (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
1541 1542
			goto done;
		do {
1543 1544 1545 1546
			hw_write(udc, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
			tmp_stat = hw_read(udc, OP_ENDPTSTAT, BIT(n));
		} while (!hw_read(udc, OP_USBCMD, USBCMD_ATDTW));
		hw_write(udc, OP_USBCMD, USBCMD_ATDTW, 0);
1547 1548 1549 1550 1551
		if (tmp_stat)
			goto done;
	}

	/*  QH configuration */
1552 1553
	mEp->qh.ptr->td.next   = mReq->dma;    /* TERMINATE = 0 */
	mEp->qh.ptr->td.token &= ~TD_STATUS;   /* clear status */
1554
	mEp->qh.ptr->cap |=  QH_ZLT;
D
David Lopo 已提交
1555 1556 1557

	wmb();   /* synchronize before ep prime */

1558
	ret = hw_ep_prime(udc, mEp->num, mEp->dir,
D
David Lopo 已提交
1559
			   mEp->type == USB_ENDPOINT_XFER_CONTROL);
1560 1561
done:
	return ret;
D
David Lopo 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
}

/**
 * _hardware_dequeue: handles a request at hardware level
 * @gadget: gadget
 * @mEp:    endpoint
 *
 * This function returns an error code
 */
static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
{
1573
	trace(mEp->udc->dev, "%p, %p", mEp, mReq);
D
David Lopo 已提交
1574 1575 1576 1577

	if (mReq->req.status != -EALREADY)
		return -EINVAL;

1578 1579 1580 1581 1582 1583 1584 1585 1586
	if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
		return -EBUSY;

	if (mReq->zptr) {
		if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
			return -EBUSY;
		dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
		mReq->zptr = NULL;
	}
D
David Lopo 已提交
1587 1588 1589 1590 1591 1592

	mReq->req.status = 0;

	if (mReq->map) {
		dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
				 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1593
		mReq->req.dma = DMA_ADDR_INVALID;
D
David Lopo 已提交
1594 1595 1596 1597
		mReq->map     = 0;
	}

	mReq->req.status = mReq->ptr->token & TD_STATUS;
1598
	if ((TD_STATUS_HALTED & mReq->req.status) != 0)
D
David Lopo 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
		mReq->req.status = -1;
	else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
		mReq->req.status = -1;
	else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
		mReq->req.status = -1;

	mReq->req.actual   = mReq->ptr->token & TD_TOTAL_BYTES;
	mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
	mReq->req.actual   = mReq->req.length - mReq->req.actual;
	mReq->req.actual   = mReq->req.status ? 0 : mReq->req.actual;

	return mReq->req.actual;
}

/**
 * _ep_nuke: dequeues all endpoint requests
 * @mEp: endpoint
 *
 * This function returns an error code
 * Caller must hold lock
 */
static int _ep_nuke(struct ci13xxx_ep *mEp)
__releases(mEp->lock)
__acquires(mEp->lock)
{
1624
	trace(mEp->udc->dev, "%p", mEp);
D
David Lopo 已提交
1625 1626 1627 1628

	if (mEp == NULL)
		return -EINVAL;

1629
	hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
1630

1631
	while (!list_empty(&mEp->qh.queue)) {
D
David Lopo 已提交
1632 1633 1634

		/* pop oldest request */
		struct ci13xxx_req *mReq = \
1635
			list_entry(mEp->qh.queue.next,
D
David Lopo 已提交
1636 1637 1638 1639
				   struct ci13xxx_req, queue);
		list_del_init(&mReq->queue);
		mReq->req.status = -ESHUTDOWN;

1640
		if (mReq->req.complete != NULL) {
D
David Lopo 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
			spin_unlock(mEp->lock);
			mReq->req.complete(&mEp->ep, &mReq->req);
			spin_lock(mEp->lock);
		}
	}
	return 0;
}

/**
 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
 * @gadget: gadget
 *
 * This function returns an error code
 */
static int _gadget_stop_activity(struct usb_gadget *gadget)
{
	struct usb_ep *ep;
	struct ci13xxx    *udc = container_of(gadget, struct ci13xxx, gadget);
1659
	unsigned long flags;
D
David Lopo 已提交
1660

1661
	trace(udc->dev, "%p", gadget);
D
David Lopo 已提交
1662 1663 1664 1665

	if (gadget == NULL)
		return -EINVAL;

1666
	spin_lock_irqsave(&udc->lock, flags);
1667 1668 1669
	udc->gadget.speed = USB_SPEED_UNKNOWN;
	udc->remote_wakeup = 0;
	udc->suspended = 0;
1670
	spin_unlock_irqrestore(&udc->lock, flags);
1671

D
David Lopo 已提交
1672 1673 1674 1675
	/* flush all endpoints */
	gadget_for_each_ep(ep, gadget) {
		usb_ep_fifo_flush(ep);
	}
1676 1677
	usb_ep_fifo_flush(&udc->ep0out->ep);
	usb_ep_fifo_flush(&udc->ep0in->ep);
D
David Lopo 已提交
1678

1679 1680
	if (udc->driver)
		udc->driver->disconnect(gadget);
D
David Lopo 已提交
1681 1682 1683 1684 1685 1686

	/* make sure to disable all endpoints */
	gadget_for_each_ep(ep, gadget) {
		usb_ep_disable(ep);
	}

1687
	if (udc->status != NULL) {
1688
		usb_ep_free_request(&udc->ep0in->ep, udc->status);
1689
		udc->status = NULL;
D
David Lopo 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	}

	return 0;
}

/******************************************************************************
 * ISR block
 *****************************************************************************/
/**
 * isr_reset_handler: USB reset interrupt handler
 * @udc: UDC device
 *
 * This function resets USB engine after a bus reset occurred
 */
static void isr_reset_handler(struct ci13xxx *udc)
__releases(udc->lock)
__acquires(udc->lock)
{
	int retval;

1710
	trace(udc->dev, "%p", udc);
D
David Lopo 已提交
1711 1712 1713

	dbg_event(0xFF, "BUS RST", 0);

1714
	spin_unlock(&udc->lock);
D
David Lopo 已提交
1715 1716 1717 1718
	retval = _gadget_stop_activity(&udc->gadget);
	if (retval)
		goto done;

1719
	retval = hw_usb_reset(udc);
D
David Lopo 已提交
1720 1721 1722
	if (retval)
		goto done;

1723
	udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC);
1724 1725
	if (udc->status == NULL)
		retval = -ENOMEM;
1726

1727
	spin_lock(&udc->lock);
D
David Lopo 已提交
1728 1729 1730

 done:
	if (retval)
1731
		dev_err(udc->dev, "error: %i\n", retval);
D
David Lopo 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
}

/**
 * isr_get_status_complete: get_status request complete function
 * @ep:  endpoint
 * @req: request handled
 *
 * Caller must release lock
 */
static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
{
1743
	trace(NULL, "%p, %p", ep, req);
D
David Lopo 已提交
1744

1745
	if (ep == NULL || req == NULL)
D
David Lopo 已提交
1746 1747 1748 1749 1750 1751 1752 1753
		return;

	kfree(req->buf);
	usb_ep_free_request(ep, req);
}

/**
 * isr_get_status_response: get_status request response
1754
 * @udc: udc struct
D
David Lopo 已提交
1755 1756 1757 1758
 * @setup: setup request packet
 *
 * This function returns an error code
 */
1759
static int isr_get_status_response(struct ci13xxx *udc,
D
David Lopo 已提交
1760 1761 1762 1763
				   struct usb_ctrlrequest *setup)
__releases(mEp->lock)
__acquires(mEp->lock)
{
1764
	struct ci13xxx_ep *mEp = udc->ep0in;
D
David Lopo 已提交
1765 1766 1767 1768
	struct usb_request *req = NULL;
	gfp_t gfp_flags = GFP_ATOMIC;
	int dir, num, retval;

1769
	trace(udc->dev, "%p, %p", mEp, setup);
D
David Lopo 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788

	if (mEp == NULL || setup == NULL)
		return -EINVAL;

	spin_unlock(mEp->lock);
	req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
	spin_lock(mEp->lock);
	if (req == NULL)
		return -ENOMEM;

	req->complete = isr_get_status_complete;
	req->length   = 2;
	req->buf      = kzalloc(req->length, gfp_flags);
	if (req->buf == NULL) {
		retval = -ENOMEM;
		goto err_free_req;
	}

	if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1789
		/* Assume that device is bus powered for now. */
1790
		*(u16 *)req->buf = udc->remote_wakeup << 1;
D
David Lopo 已提交
1791 1792 1793 1794 1795 1796
		retval = 0;
	} else if ((setup->bRequestType & USB_RECIP_MASK) \
		   == USB_RECIP_ENDPOINT) {
		dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
			TX : RX;
		num =  le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1797
		*(u16 *)req->buf = hw_ep_get_halt(udc, num, dir);
D
David Lopo 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
	}
	/* else do nothing; reserved for future use */

	spin_unlock(mEp->lock);
	retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
	spin_lock(mEp->lock);
	if (retval)
		goto err_free_buf;

	return 0;

 err_free_buf:
	kfree(req->buf);
 err_free_req:
	spin_unlock(mEp->lock);
	usb_ep_free_request(&mEp->ep, req);
	spin_lock(mEp->lock);
	return retval;
}

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
/**
 * isr_setup_status_complete: setup_status request complete function
 * @ep:  endpoint
 * @req: request handled
 *
 * Caller must release lock. Put the port in test mode if test mode
 * feature is selected.
 */
static void
isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
{
	struct ci13xxx *udc = req->context;
	unsigned long flags;

1832
	trace(udc->dev, "%p, %p", ep, req);
1833

1834
	spin_lock_irqsave(&udc->lock, flags);
1835
	if (udc->test_mode)
1836 1837
		hw_port_test_set(udc, udc->test_mode);
	spin_unlock_irqrestore(&udc->lock, flags);
1838 1839
}

D
David Lopo 已提交
1840 1841
/**
 * isr_setup_status_phase: queues the status phase of a setup transation
1842
 * @udc: udc struct
D
David Lopo 已提交
1843 1844 1845
 *
 * This function returns an error code
 */
1846
static int isr_setup_status_phase(struct ci13xxx *udc)
D
David Lopo 已提交
1847 1848 1849 1850
__releases(mEp->lock)
__acquires(mEp->lock)
{
	int retval;
1851
	struct ci13xxx_ep *mEp;
D
David Lopo 已提交
1852

1853
	trace(udc->dev, "%p", udc);
D
David Lopo 已提交
1854

1855
	mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in;
1856 1857
	udc->status->context = udc;
	udc->status->complete = isr_setup_status_complete;
D
David Lopo 已提交
1858 1859

	spin_unlock(mEp->lock);
1860
	retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
D
David Lopo 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
	spin_lock(mEp->lock);

	return retval;
}

/**
 * isr_tr_complete_low: transaction complete low level handler
 * @mEp: endpoint
 *
 * This function returns an error code
 * Caller must hold lock
 */
static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
__releases(mEp->lock)
__acquires(mEp->lock)
{
1877
	struct ci13xxx_req *mReq, *mReqTemp;
1878
	struct ci13xxx_ep *mEpTemp = mEp;
1879
	int uninitialized_var(retval);
D
David Lopo 已提交
1880

1881
	trace(mEp->udc->dev, "%p", mEp);
D
David Lopo 已提交
1882

1883
	if (list_empty(&mEp->qh.queue))
D
David Lopo 已提交
1884 1885
		return -EINVAL;

1886 1887 1888 1889 1890 1891 1892 1893 1894
	list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
			queue) {
		retval = _hardware_dequeue(mEp, mReq);
		if (retval < 0)
			break;
		list_del_init(&mReq->queue);
		dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
		if (mReq->req.complete != NULL) {
			spin_unlock(mEp->lock);
1895 1896
			if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
					mReq->req.length)
1897
				mEpTemp = mEp->udc->ep0in;
1898
			mReq->req.complete(&mEpTemp->ep, &mReq->req);
1899 1900
			spin_lock(mEp->lock);
		}
1901 1902
	}

1903
	if (retval == -EBUSY)
1904 1905 1906
		retval = 0;
	if (retval < 0)
		dbg_event(_usb_addr(mEp), "DONE", retval);
D
David Lopo 已提交
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921

	return retval;
}

/**
 * isr_tr_complete_handler: transaction complete interrupt handler
 * @udc: UDC descriptor
 *
 * This function handles traffic events
 */
static void isr_tr_complete_handler(struct ci13xxx *udc)
__releases(udc->lock)
__acquires(udc->lock)
{
	unsigned i;
1922
	u8 tmode = 0;
D
David Lopo 已提交
1923

1924
	trace(udc->dev, "%p", udc);
D
David Lopo 已提交
1925

1926
	for (i = 0; i < udc->hw_ep_max; i++) {
D
David Lopo 已提交
1927
		struct ci13xxx_ep *mEp  = &udc->ci13xxx_ep[i];
1928
		int type, num, dir, err = -EINVAL;
D
David Lopo 已提交
1929 1930
		struct usb_ctrlrequest req;

1931
		if (mEp->ep.desc == NULL)
D
David Lopo 已提交
1932 1933
			continue;   /* not configured */

1934
		if (hw_test_and_clear_complete(udc, i)) {
D
David Lopo 已提交
1935 1936 1937
			err = isr_tr_complete_low(mEp);
			if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
				if (err > 0)   /* needs status phase */
1938
					err = isr_setup_status_phase(udc);
D
David Lopo 已提交
1939 1940 1941
				if (err < 0) {
					dbg_event(_usb_addr(mEp),
						  "ERROR", err);
1942
					spin_unlock(&udc->lock);
D
David Lopo 已提交
1943
					if (usb_ep_set_halt(&mEp->ep))
1944
						dev_err(udc->dev,
1945
							"error: ep_set_halt\n");
1946
					spin_lock(&udc->lock);
D
David Lopo 已提交
1947 1948 1949 1950 1951
				}
			}
		}

		if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
1952
		    !hw_test_and_clear_setup_status(udc, i))
D
David Lopo 已提交
1953 1954 1955
			continue;

		if (i != 0) {
1956
			dev_warn(udc->dev, "ctrl traffic at endpoint %d\n", i);
D
David Lopo 已提交
1957 1958 1959
			continue;
		}

1960 1961 1962 1963
		/*
		 * Flush data and handshake transactions of previous
		 * setup packet.
		 */
1964 1965
		_ep_nuke(udc->ep0out);
		_ep_nuke(udc->ep0in);
1966

D
David Lopo 已提交
1967 1968
		/* read_setup_packet */
		do {
1969
			hw_test_and_set_setup_guard(udc);
1970
			memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
1971
		} while (!hw_test_and_clear_setup_guard(udc));
D
David Lopo 已提交
1972 1973 1974

		type = req.bRequestType;

1975
		udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
D
David Lopo 已提交
1976 1977 1978 1979 1980

		dbg_setup(_usb_addr(mEp), &req);

		switch (req.bRequest) {
		case USB_REQ_CLEAR_FEATURE:
1981 1982 1983 1984 1985 1986
			if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
					le16_to_cpu(req.wValue) ==
					USB_ENDPOINT_HALT) {
				if (req.wLength != 0)
					break;
				num  = le16_to_cpu(req.wIndex);
1987
				dir = num & USB_ENDPOINT_DIR_MASK;
1988
				num &= USB_ENDPOINT_NUMBER_MASK;
1989
				if (dir) /* TX */
1990
					num += udc->hw_ep_max/2;
1991
				if (!udc->ci13xxx_ep[num].wedge) {
1992
					spin_unlock(&udc->lock);
1993 1994
					err = usb_ep_clear_halt(
						&udc->ci13xxx_ep[num].ep);
1995
					spin_lock(&udc->lock);
1996 1997 1998 1999 2000 2001 2002 2003
					if (err)
						break;
				}
				err = isr_setup_status_phase(udc);
			} else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
					le16_to_cpu(req.wValue) ==
					USB_DEVICE_REMOTE_WAKEUP) {
				if (req.wLength != 0)
D
David Lopo 已提交
2004
					break;
2005 2006 2007 2008
				udc->remote_wakeup = 0;
				err = isr_setup_status_phase(udc);
			} else {
				goto delegate;
D
David Lopo 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
			}
			break;
		case USB_REQ_GET_STATUS:
			if (type != (USB_DIR_IN|USB_RECIP_DEVICE)   &&
			    type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
			    type != (USB_DIR_IN|USB_RECIP_INTERFACE))
				goto delegate;
			if (le16_to_cpu(req.wLength) != 2 ||
			    le16_to_cpu(req.wValue)  != 0)
				break;
2019
			err = isr_get_status_response(udc, &req);
D
David Lopo 已提交
2020 2021 2022 2023 2024 2025 2026
			break;
		case USB_REQ_SET_ADDRESS:
			if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
				goto delegate;
			if (le16_to_cpu(req.wLength) != 0 ||
			    le16_to_cpu(req.wIndex)  != 0)
				break;
2027 2028
			err = hw_usb_set_address(udc,
						 (u8)le16_to_cpu(req.wValue));
D
David Lopo 已提交
2029 2030
			if (err)
				break;
2031
			err = isr_setup_status_phase(udc);
D
David Lopo 已提交
2032 2033
			break;
		case USB_REQ_SET_FEATURE:
2034 2035 2036 2037 2038 2039
			if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
					le16_to_cpu(req.wValue) ==
					USB_ENDPOINT_HALT) {
				if (req.wLength != 0)
					break;
				num  = le16_to_cpu(req.wIndex);
2040
				dir = num & USB_ENDPOINT_DIR_MASK;
2041
				num &= USB_ENDPOINT_NUMBER_MASK;
2042
				if (dir) /* TX */
2043
					num += udc->hw_ep_max/2;
D
David Lopo 已提交
2044

2045
				spin_unlock(&udc->lock);
2046
				err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
2047
				spin_lock(&udc->lock);
2048
				if (!err)
2049 2050
					isr_setup_status_phase(udc);
			} else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
2051 2052
				if (req.wLength != 0)
					break;
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
				switch (le16_to_cpu(req.wValue)) {
				case USB_DEVICE_REMOTE_WAKEUP:
					udc->remote_wakeup = 1;
					err = isr_setup_status_phase(udc);
					break;
				case USB_DEVICE_TEST_MODE:
					tmode = le16_to_cpu(req.wIndex) >> 8;
					switch (tmode) {
					case TEST_J:
					case TEST_K:
					case TEST_SE0_NAK:
					case TEST_PACKET:
					case TEST_FORCE_EN:
						udc->test_mode = tmode;
						err = isr_setup_status_phase(
								udc);
						break;
					default:
						break;
					}
				default:
					goto delegate;
				}
2076 2077 2078
			} else {
				goto delegate;
			}
D
David Lopo 已提交
2079 2080 2081 2082
			break;
		default:
delegate:
			if (req.wLength == 0)   /* no data phase */
2083
				udc->ep0_dir = TX;
D
David Lopo 已提交
2084

2085
			spin_unlock(&udc->lock);
D
David Lopo 已提交
2086
			err = udc->driver->setup(&udc->gadget, &req);
2087
			spin_lock(&udc->lock);
D
David Lopo 已提交
2088 2089 2090 2091 2092 2093
			break;
		}

		if (err < 0) {
			dbg_event(_usb_addr(mEp), "ERROR", err);

2094
			spin_unlock(&udc->lock);
D
David Lopo 已提交
2095
			if (usb_ep_set_halt(&mEp->ep))
2096
				dev_err(udc->dev, "error: ep_set_halt\n");
2097
			spin_lock(&udc->lock);
D
David Lopo 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
		}
	}
}

/******************************************************************************
 * ENDPT block
 *****************************************************************************/
/**
 * ep_enable: configure endpoint, making it usable
 *
 * Check usb_ep_enable() at "usb_gadget.h" for details
 */
static int ep_enable(struct usb_ep *ep,
		     const struct usb_endpoint_descriptor *desc)
{
	struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2114
	int retval = 0;
D
David Lopo 已提交
2115 2116
	unsigned long flags;

2117
	trace(mEp->udc->dev, "%p, %p", ep, desc);
D
David Lopo 已提交
2118 2119 2120 2121 2122 2123 2124 2125

	if (ep == NULL || desc == NULL)
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

	/* only internal SW should enable ctrl endpts */

2126
	mEp->ep.desc = desc;
D
David Lopo 已提交
2127

2128
	if (!list_empty(&mEp->qh.queue))
2129
		dev_warn(mEp->udc->dev, "enabling a non-empty endpoint!\n");
D
David Lopo 已提交
2130

2131 2132 2133
	mEp->dir  = usb_endpoint_dir_in(desc) ? TX : RX;
	mEp->num  = usb_endpoint_num(desc);
	mEp->type = usb_endpoint_type(desc);
D
David Lopo 已提交
2134

2135
	mEp->ep.maxpacket = usb_endpoint_maxp(desc);
D
David Lopo 已提交
2136

2137
	dbg_event(_usb_addr(mEp), "ENABLE", 0);
D
David Lopo 已提交
2138

2139
	mEp->qh.ptr->cap = 0;
D
David Lopo 已提交
2140

2141 2142 2143 2144 2145 2146
	if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
		mEp->qh.ptr->cap |=  QH_IOS;
	else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
		mEp->qh.ptr->cap &= ~QH_MULT;
	else
		mEp->qh.ptr->cap &= ~QH_ZLT;
D
David Lopo 已提交
2147

2148 2149 2150
	mEp->qh.ptr->cap |=
		(mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
	mEp->qh.ptr->td.next |= TD_TERMINATE;   /* needed? */
D
David Lopo 已提交
2151

2152 2153 2154 2155 2156
	/*
	 * Enable endpoints in the HW other than ep0 as ep0
	 * is always enabled
	 */
	if (mEp->num)
2157
		retval |= hw_ep_enable(mEp->udc, mEp->num, mEp->dir, mEp->type);
D
David Lopo 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173

	spin_unlock_irqrestore(mEp->lock, flags);
	return retval;
}

/**
 * ep_disable: endpoint is no longer usable
 *
 * Check usb_ep_disable() at "usb_gadget.h" for details
 */
static int ep_disable(struct usb_ep *ep)
{
	struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
	int direction, retval = 0;
	unsigned long flags;

2174
	trace(mEp->udc->dev, "%p", ep);
D
David Lopo 已提交
2175 2176 2177

	if (ep == NULL)
		return -EINVAL;
2178
	else if (mEp->ep.desc == NULL)
D
David Lopo 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
		return -EBUSY;

	spin_lock_irqsave(mEp->lock, flags);

	/* only internal SW should disable ctrl endpts */

	direction = mEp->dir;
	do {
		dbg_event(_usb_addr(mEp), "DISABLE", 0);

		retval |= _ep_nuke(mEp);
2190
		retval |= hw_ep_disable(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
2191 2192 2193 2194 2195 2196

		if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
			mEp->dir = (mEp->dir == TX) ? RX : TX;

	} while (mEp->dir != direction);

2197
	mEp->ep.desc = NULL;
D
David Lopo 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212

	spin_unlock_irqrestore(mEp->lock, flags);
	return retval;
}

/**
 * ep_alloc_request: allocate a request object to use with this endpoint
 *
 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
 */
static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
{
	struct ci13xxx_ep  *mEp  = container_of(ep, struct ci13xxx_ep, ep);
	struct ci13xxx_req *mReq = NULL;

2213
	trace(mEp->udc->dev, "%p, %i", ep, gfp_flags);
D
David Lopo 已提交
2214

2215
	if (ep == NULL)
D
David Lopo 已提交
2216 2217 2218 2219 2220
		return NULL;

	mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
	if (mReq != NULL) {
		INIT_LIST_HEAD(&mReq->queue);
2221
		mReq->req.dma = DMA_ADDR_INVALID;
D
David Lopo 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246

		mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
					   &mReq->dma);
		if (mReq->ptr == NULL) {
			kfree(mReq);
			mReq = NULL;
		}
	}

	dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);

	return (mReq == NULL) ? NULL : &mReq->req;
}

/**
 * ep_free_request: frees a request object
 *
 * Check usb_ep_free_request() at "usb_gadget.h" for details
 */
static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
{
	struct ci13xxx_ep  *mEp  = container_of(ep,  struct ci13xxx_ep, ep);
	struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
	unsigned long flags;

2247
	trace(mEp->udc->dev, "%p, %p", ep, req);
D
David Lopo 已提交
2248 2249 2250 2251

	if (ep == NULL || req == NULL) {
		return;
	} else if (!list_empty(&mReq->queue)) {
2252
		dev_err(mEp->udc->dev, "freeing queued request\n");
D
David Lopo 已提交
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
		return;
	}

	spin_lock_irqsave(mEp->lock, flags);

	if (mReq->ptr)
		dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
	kfree(mReq);

	dbg_event(_usb_addr(mEp), "FREE", 0);

	spin_unlock_irqrestore(mEp->lock, flags);
}

/**
 * ep_queue: queues (submits) an I/O request to an endpoint
 *
 * Check usb_ep_queue()* at usb_gadget.h" for details
 */
static int ep_queue(struct usb_ep *ep, struct usb_request *req,
		    gfp_t __maybe_unused gfp_flags)
{
	struct ci13xxx_ep  *mEp  = container_of(ep,  struct ci13xxx_ep, ep);
	struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2277
	struct ci13xxx *udc = mEp->udc;
D
David Lopo 已提交
2278 2279 2280
	int retval = 0;
	unsigned long flags;

2281
	trace(mEp->udc->dev, "%p, %p, %X", ep, req, gfp_flags);
D
David Lopo 已提交
2282

2283
	if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
2284 2285 2286 2287
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

2288 2289
	if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
		if (req->length)
2290 2291
			mEp = (udc->ep0_dir == RX) ?
			       udc->ep0out : udc->ep0in;
2292 2293 2294
		if (!list_empty(&mEp->qh.queue)) {
			_ep_nuke(mEp);
			retval = -EOVERFLOW;
2295 2296
			dev_warn(mEp->udc->dev, "endpoint ctrl %X nuked\n",
				 _usb_addr(mEp));
2297
		}
D
David Lopo 已提交
2298 2299 2300 2301 2302
	}

	/* first nuke then test link, e.g. previous status has not sent */
	if (!list_empty(&mReq->queue)) {
		retval = -EBUSY;
2303
		dev_err(mEp->udc->dev, "request already in queue\n");
D
David Lopo 已提交
2304 2305 2306
		goto done;
	}

2307 2308
	if (req->length > 4 * CI13XXX_PAGE_SIZE) {
		req->length = 4 * CI13XXX_PAGE_SIZE;
D
David Lopo 已提交
2309
		retval = -EMSGSIZE;
2310
		dev_warn(mEp->udc->dev, "request length truncated\n");
D
David Lopo 已提交
2311 2312 2313 2314 2315 2316 2317 2318
	}

	dbg_queue(_usb_addr(mEp), req, retval);

	/* push request */
	mReq->req.status = -EINPROGRESS;
	mReq->req.actual = 0;

2319
	retval = _hardware_enqueue(mEp, mReq);
2320 2321

	if (retval == -EALREADY) {
D
David Lopo 已提交
2322 2323 2324
		dbg_event(_usb_addr(mEp), "QUEUE", retval);
		retval = 0;
	}
2325 2326
	if (!retval)
		list_add_tail(&mReq->queue, &mEp->qh.queue);
D
David Lopo 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343

 done:
	spin_unlock_irqrestore(mEp->lock, flags);
	return retval;
}

/**
 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
 *
 * Check usb_ep_dequeue() at "usb_gadget.h" for details
 */
static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
{
	struct ci13xxx_ep  *mEp  = container_of(ep,  struct ci13xxx_ep, ep);
	struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
	unsigned long flags;

2344
	trace(mEp->udc->dev, "%p, %p", ep, req);
D
David Lopo 已提交
2345

2346
	if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
2347
		mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
2348
		list_empty(&mEp->qh.queue))
D
David Lopo 已提交
2349 2350 2351 2352 2353 2354
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

	dbg_event(_usb_addr(mEp), "DEQUEUE", 0);

2355
	hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
2356 2357 2358

	/* pop request */
	list_del_init(&mReq->queue);
2359 2360 2361
	if (mReq->map) {
		dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
				 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2362
		mReq->req.dma = DMA_ADDR_INVALID;
2363 2364
		mReq->map     = 0;
	}
D
David Lopo 已提交
2365 2366
	req->status = -ECONNRESET;

2367
	if (mReq->req.complete != NULL) {
D
David Lopo 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
		spin_unlock(mEp->lock);
		mReq->req.complete(&mEp->ep, &mReq->req);
		spin_lock(mEp->lock);
	}

	spin_unlock_irqrestore(mEp->lock, flags);
	return 0;
}

/**
 * ep_set_halt: sets the endpoint halt feature
 *
 * Check usb_ep_set_halt() at "usb_gadget.h" for details
 */
static int ep_set_halt(struct usb_ep *ep, int value)
{
	struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
	int direction, retval = 0;
	unsigned long flags;

2388
	trace(mEp->udc->dev, "%p, %i", ep, value);
D
David Lopo 已提交
2389

2390
	if (ep == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
2391 2392 2393 2394 2395 2396 2397
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

#ifndef STALL_IN
	/* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
	if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2398
	    !list_empty(&mEp->qh.queue)) {
D
David Lopo 已提交
2399 2400 2401 2402 2403 2404 2405 2406
		spin_unlock_irqrestore(mEp->lock, flags);
		return -EAGAIN;
	}
#endif

	direction = mEp->dir;
	do {
		dbg_event(_usb_addr(mEp), "HALT", value);
2407
		retval |= hw_ep_set_halt(mEp->udc, mEp->num, mEp->dir, value);
D
David Lopo 已提交
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430

		if (!value)
			mEp->wedge = 0;

		if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
			mEp->dir = (mEp->dir == TX) ? RX : TX;

	} while (mEp->dir != direction);

	spin_unlock_irqrestore(mEp->lock, flags);
	return retval;
}

/**
 * ep_set_wedge: sets the halt feature and ignores clear requests
 *
 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
 */
static int ep_set_wedge(struct usb_ep *ep)
{
	struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
	unsigned long flags;

2431
	trace(mEp->udc->dev, "%p", ep);
D
David Lopo 已提交
2432

2433
	if (ep == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

	dbg_event(_usb_addr(mEp), "WEDGE", 0);
	mEp->wedge = 1;

	spin_unlock_irqrestore(mEp->lock, flags);

	return usb_ep_set_halt(ep);
}

/**
 * ep_fifo_flush: flushes contents of a fifo
 *
 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
 */
static void ep_fifo_flush(struct usb_ep *ep)
{
	struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
	unsigned long flags;

2456
	trace(mEp->udc->dev, "%p", ep);
D
David Lopo 已提交
2457 2458

	if (ep == NULL) {
2459
		dev_err(mEp->udc->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
D
David Lopo 已提交
2460 2461 2462 2463 2464 2465
		return;
	}

	spin_lock_irqsave(mEp->lock, flags);

	dbg_event(_usb_addr(mEp), "FFLUSH", 0);
2466
	hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489

	spin_unlock_irqrestore(mEp->lock, flags);
}

/**
 * Endpoint-specific part of the API to the USB controller hardware
 * Check "usb_gadget.h" for details
 */
static const struct usb_ep_ops usb_ep_ops = {
	.enable	       = ep_enable,
	.disable       = ep_disable,
	.alloc_request = ep_alloc_request,
	.free_request  = ep_free_request,
	.queue	       = ep_queue,
	.dequeue       = ep_dequeue,
	.set_halt      = ep_set_halt,
	.set_wedge     = ep_set_wedge,
	.fifo_flush    = ep_fifo_flush,
};

/******************************************************************************
 * GADGET block
 *****************************************************************************/
2490 2491 2492 2493 2494 2495 2496 2497 2498
static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
{
	struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
	unsigned long flags;
	int gadget_ready = 0;

	if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
		return -EOPNOTSUPP;

2499
	spin_lock_irqsave(&udc->lock, flags);
2500 2501 2502
	udc->vbus_active = is_active;
	if (udc->driver)
		gadget_ready = 1;
2503
	spin_unlock_irqrestore(&udc->lock, flags);
2504 2505 2506

	if (gadget_ready) {
		if (is_active) {
2507
			pm_runtime_get_sync(&_gadget->dev);
2508
			hw_device_reset(udc);
2509
			hw_device_state(udc, udc->ep0out->qh.dma);
2510
		} else {
2511
			hw_device_state(udc, 0);
2512 2513 2514 2515
			if (udc->udc_driver->notify_event)
				udc->udc_driver->notify_event(udc,
				CI13XXX_CONTROLLER_STOPPED_EVENT);
			_gadget_stop_activity(&udc->gadget);
2516
			pm_runtime_put_sync(&_gadget->dev);
2517 2518 2519 2520 2521 2522
		}
	}

	return 0;
}

2523 2524 2525 2526 2527 2528
static int ci13xxx_wakeup(struct usb_gadget *_gadget)
{
	struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
	unsigned long flags;
	int ret = 0;

2529
	trace(udc->dev, "");
2530

2531
	spin_lock_irqsave(&udc->lock, flags);
2532 2533
	if (!udc->remote_wakeup) {
		ret = -EOPNOTSUPP;
2534
		trace(udc->dev, "remote wakeup feature is not enabled\n");
2535 2536
		goto out;
	}
2537
	if (!hw_read(udc, OP_PORTSC, PORTSC_SUSP)) {
2538
		ret = -EINVAL;
2539
		trace(udc->dev, "port is not suspended\n");
2540 2541
		goto out;
	}
2542
	hw_write(udc, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
2543
out:
2544
	spin_unlock_irqrestore(&udc->lock, flags);
2545 2546 2547
	return ret;
}

2548 2549 2550 2551 2552
static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
{
	struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);

	if (udc->transceiver)
2553
		return usb_phy_set_power(udc->transceiver, mA);
2554 2555 2556
	return -ENOTSUPP;
}

2557 2558 2559 2560
static int ci13xxx_start(struct usb_gadget *gadget,
			 struct usb_gadget_driver *driver);
static int ci13xxx_stop(struct usb_gadget *gadget,
			struct usb_gadget_driver *driver);
D
David Lopo 已提交
2561 2562 2563 2564 2565
/**
 * Device operations part of the API to the USB controller hardware,
 * which don't involve endpoints (or i/o)
 * Check  "usb_gadget.h" for details
 */
2566 2567
static const struct usb_gadget_ops usb_gadget_ops = {
	.vbus_session	= ci13xxx_vbus_session,
2568
	.wakeup		= ci13xxx_wakeup,
2569
	.vbus_draw	= ci13xxx_vbus_draw,
2570 2571
	.udc_start	= ci13xxx_start,
	.udc_stop	= ci13xxx_stop,
2572
};
D
David Lopo 已提交
2573

2574
static int init_eps(struct ci13xxx *udc)
D
David Lopo 已提交
2575
{
2576
	int retval = 0, i, j;
D
David Lopo 已提交
2577

2578
	for (i = 0; i < udc->hw_ep_max/2; i++)
2579
		for (j = RX; j <= TX; j++) {
2580
			int k = i + j * udc->hw_ep_max/2;
2581
			struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
D
David Lopo 已提交
2582

2583 2584
			scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
					(j == TX)  ? "in" : "out");
D
David Lopo 已提交
2585

2586 2587
			mEp->udc          = udc;
			mEp->lock         = &udc->lock;
2588 2589
			mEp->device       = &udc->gadget.dev;
			mEp->td_pool      = udc->td_pool;
D
David Lopo 已提交
2590

2591 2592 2593
			mEp->ep.name      = mEp->name;
			mEp->ep.ops       = &usb_ep_ops;
			mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
D
David Lopo 已提交
2594

2595 2596
			INIT_LIST_HEAD(&mEp->qh.queue);
			mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
2597
						     &mEp->qh.dma);
2598
			if (mEp->qh.ptr == NULL)
D
David Lopo 已提交
2599 2600
				retval = -ENOMEM;
			else
2601 2602
				memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));

2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
			/*
			 * set up shorthands for ep0 out and in endpoints,
			 * don't add to gadget's ep_list
			 */
			if (i == 0) {
				if (j == RX)
					udc->ep0out = mEp;
				else
					udc->ep0in = mEp;

2613
				continue;
2614
			}
2615

D
David Lopo 已提交
2616
			list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2617
		}
2618 2619 2620 2621 2622 2623

	return retval;
}

/**
 * ci13xxx_start: register a gadget driver
2624
 * @gadget: our gadget
2625 2626 2627 2628
 * @driver: the driver being registered
 *
 * Interrupts are enabled here.
 */
2629 2630
static int ci13xxx_start(struct usb_gadget *gadget,
			 struct usb_gadget_driver *driver)
2631
{
2632
	struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
2633 2634 2635 2636 2637
	unsigned long flags;
	int retval = -ENOMEM;

	trace(udc->dev, "%p", driver);

2638
	if (driver->disconnect == NULL)
2639 2640 2641
		return -EINVAL;


2642 2643
	udc->ep0out->ep.desc = &ctrl_endpt_out_desc;
	retval = usb_ep_enable(&udc->ep0out->ep);
2644 2645
	if (retval)
		return retval;
2646

2647 2648
	udc->ep0in->ep.desc = &ctrl_endpt_in_desc;
	retval = usb_ep_enable(&udc->ep0in->ep);
2649 2650
	if (retval)
		return retval;
2651
	spin_lock_irqsave(&udc->lock, flags);
D
David Lopo 已提交
2652

2653
	udc->driver = driver;
2654
	pm_runtime_get_sync(&udc->gadget.dev);
2655 2656 2657 2658 2659
	if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
		if (udc->vbus_active) {
			if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
				hw_device_reset(udc);
		} else {
2660
			pm_runtime_put_sync(&udc->gadget.dev);
2661 2662 2663 2664
			goto done;
		}
	}

2665
	retval = hw_device_state(udc, udc->ep0out->qh.dma);
2666 2667
	if (retval)
		pm_runtime_put_sync(&udc->gadget.dev);
D
David Lopo 已提交
2668 2669

 done:
2670
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
2671 2672 2673 2674
	return retval;
}

/**
2675
 * ci13xxx_stop: unregister a gadget driver
D
David Lopo 已提交
2676
 */
2677 2678
static int ci13xxx_stop(struct usb_gadget *gadget,
			struct usb_gadget_driver *driver)
D
David Lopo 已提交
2679
{
2680 2681
	struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
	unsigned long flags;
D
David Lopo 已提交
2682

2683
	trace(udc->dev, "%p", driver);
D
David Lopo 已提交
2684

2685
	spin_lock_irqsave(&udc->lock, flags);
D
David Lopo 已提交
2686

2687 2688
	if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
			udc->vbus_active) {
2689
		hw_device_state(udc, 0);
2690 2691 2692
		if (udc->udc_driver->notify_event)
			udc->udc_driver->notify_event(udc,
			CI13XXX_CONTROLLER_STOPPED_EVENT);
2693
		udc->driver = NULL;
2694
		spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
2695
		_gadget_stop_activity(&udc->gadget);
2696
		spin_lock_irqsave(&udc->lock, flags);
2697
		pm_runtime_put(&udc->gadget.dev);
2698
	}
D
David Lopo 已提交
2699

2700
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713

	return 0;
}

/******************************************************************************
 * BUS block
 *****************************************************************************/
/**
 * udc_irq: global interrupt handler
 *
 * This function returns IRQ_HANDLED if the IRQ has been handled
 * It locks access to registers
 */
2714
static irqreturn_t udc_irq(int irq, void *data)
D
David Lopo 已提交
2715
{
2716
	struct ci13xxx *udc = data;
D
David Lopo 已提交
2717 2718 2719
	irqreturn_t retval;
	u32 intr;

2720
	trace(udc ? udc->dev : NULL, "");
D
David Lopo 已提交
2721 2722

	if (udc == NULL) {
2723
		dev_err(udc->dev, "ENODEV");
D
David Lopo 已提交
2724 2725 2726
		return IRQ_HANDLED;
	}

2727
	spin_lock(&udc->lock);
2728 2729

	if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2730 2731
		if (hw_read(udc, OP_USBMODE, USBMODE_CM) !=
				USBMODE_CM_DEVICE) {
2732
			spin_unlock(&udc->lock);
2733 2734 2735
			return IRQ_NONE;
		}
	}
2736
	intr = hw_test_and_clear_intr_active(udc);
D
David Lopo 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
	if (intr) {
		isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
		isr_statistics.hndl.idx &= ISR_MASK;
		isr_statistics.hndl.cnt++;

		/* order defines priority - do NOT change it */
		if (USBi_URI & intr) {
			isr_statistics.uri++;
			isr_reset_handler(udc);
		}
		if (USBi_PCI & intr) {
			isr_statistics.pci++;
2749
			udc->gadget.speed = hw_port_is_high_speed(udc) ?
D
David Lopo 已提交
2750
				USB_SPEED_HIGH : USB_SPEED_FULL;
2751
			if (udc->suspended && udc->driver->resume) {
2752
				spin_unlock(&udc->lock);
2753
				udc->driver->resume(&udc->gadget);
2754
				spin_lock(&udc->lock);
2755 2756
				udc->suspended = 0;
			}
D
David Lopo 已提交
2757 2758 2759 2760 2761 2762 2763
		}
		if (USBi_UEI & intr)
			isr_statistics.uei++;
		if (USBi_UI  & intr) {
			isr_statistics.ui++;
			isr_tr_complete_handler(udc);
		}
2764
		if (USBi_SLI & intr) {
2765 2766
			if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
			    udc->driver->suspend) {
2767
				udc->suspended = 1;
2768
				spin_unlock(&udc->lock);
2769
				udc->driver->suspend(&udc->gadget);
2770
				spin_lock(&udc->lock);
2771
			}
D
David Lopo 已提交
2772
			isr_statistics.sli++;
2773
		}
D
David Lopo 已提交
2774 2775 2776 2777 2778
		retval = IRQ_HANDLED;
	} else {
		isr_statistics.none++;
		retval = IRQ_NONE;
	}
2779
	spin_unlock(&udc->lock);
D
David Lopo 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791

	return retval;
}

/**
 * udc_release: driver release function
 * @dev: device
 *
 * Currently does nothing
 */
static void udc_release(struct device *dev)
{
2792
	trace(dev->parent, "%p", dev);
D
David Lopo 已提交
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
}

/**
 * udc_probe: parent probe must call this to initialize UDC
 * @dev:  parent device
 * @regs: registers base address
 * @name: driver name
 *
 * This function returns an error code
 * No interrupts active, the IRQ has not been requested yet
 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
 */
2805
static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2806
		     void __iomem *regs, struct ci13xxx **_udc)
D
David Lopo 已提交
2807 2808 2809 2810
{
	struct ci13xxx *udc;
	int retval = 0;

2811
	trace(dev, "%p, %p, %p", dev, regs, driver->name);
D
David Lopo 已提交
2812

2813 2814
	if (dev == NULL || regs == NULL || driver == NULL ||
			driver->name == NULL)
D
David Lopo 已提交
2815 2816 2817 2818 2819 2820
		return -EINVAL;

	udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
	if (udc == NULL)
		return -ENOMEM;

2821
	spin_lock_init(&udc->lock);
2822 2823
	udc->regs = regs;
	udc->udc_driver = driver;
D
David Lopo 已提交
2824

2825
	udc->gadget.ops          = &usb_gadget_ops;
D
David Lopo 已提交
2826
	udc->gadget.speed        = USB_SPEED_UNKNOWN;
2827
	udc->gadget.max_speed    = USB_SPEED_HIGH;
D
David Lopo 已提交
2828
	udc->gadget.is_otg       = 0;
2829
	udc->gadget.name         = driver->name;
D
David Lopo 已提交
2830 2831 2832

	INIT_LIST_HEAD(&udc->gadget.ep_list);

2833
	dev_set_name(&udc->gadget.dev, "gadget");
D
David Lopo 已提交
2834
	udc->gadget.dev.dma_mask = dev->dma_mask;
2835
	udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
D
David Lopo 已提交
2836 2837 2838
	udc->gadget.dev.parent   = dev;
	udc->gadget.dev.release  = udc_release;

2839 2840
	udc->dev = dev;

2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
	/* alloc resources */
	udc->qh_pool = dma_pool_create("ci13xxx_qh", dev,
				       sizeof(struct ci13xxx_qh),
				       64, CI13XXX_PAGE_SIZE);
	if (udc->qh_pool == NULL) {
		retval = -ENOMEM;
		goto free_udc;
	}

	udc->td_pool = dma_pool_create("ci13xxx_td", dev,
				       sizeof(struct ci13xxx_td),
				       64, CI13XXX_PAGE_SIZE);
	if (udc->td_pool == NULL) {
		retval = -ENOMEM;
		goto free_qh_pool;
	}

2858
	retval = hw_device_init(udc, regs, driver->capoffset);
2859
	if (retval < 0)
2860 2861 2862 2863 2864 2865 2866
		goto free_pools;

	retval = init_eps(udc);
	if (retval)
		goto free_pools;

	udc->gadget.ep0 = &udc->ep0in->ep;
2867

2868
	udc->transceiver = usb_get_transceiver();
2869 2870 2871 2872

	if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
		if (udc->transceiver == NULL) {
			retval = -ENODEV;
2873
			goto free_pools;
2874 2875 2876 2877 2878 2879 2880 2881 2882
		}
	}

	if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
		retval = hw_device_reset(udc);
		if (retval)
			goto put_transceiver;
	}

D
David Lopo 已提交
2883
	retval = device_register(&udc->gadget.dev);
2884 2885 2886 2887
	if (retval) {
		put_device(&udc->gadget.dev);
		goto put_transceiver;
	}
D
David Lopo 已提交
2888 2889 2890 2891

#ifdef CONFIG_USB_GADGET_DEBUG_FILES
	retval = dbg_create_files(&udc->gadget.dev);
#endif
2892 2893 2894 2895
	if (retval)
		goto unreg_device;

	if (udc->transceiver) {
2896 2897
		retval = otg_set_peripheral(udc->transceiver->otg,
						&udc->gadget);
2898 2899
		if (retval)
			goto remove_dbg;
D
David Lopo 已提交
2900
	}
2901 2902 2903 2904 2905

	retval = usb_add_gadget_udc(dev, &udc->gadget);
	if (retval)
		goto remove_trans;

2906 2907
	pm_runtime_no_callbacks(&udc->gadget.dev);
	pm_runtime_enable(&udc->gadget.dev);
D
David Lopo 已提交
2908

2909
	*_udc = udc;
D
David Lopo 已提交
2910 2911
	return retval;

2912 2913
remove_trans:
	if (udc->transceiver) {
2914
		otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
2915
		usb_put_transceiver(udc->transceiver);
2916 2917
	}

2918
	dev_err(dev, "error = %i\n", retval);
2919 2920 2921 2922 2923 2924 2925 2926
remove_dbg:
#ifdef CONFIG_USB_GADGET_DEBUG_FILES
	dbg_remove_files(&udc->gadget.dev);
#endif
unreg_device:
	device_unregister(&udc->gadget.dev);
put_transceiver:
	if (udc->transceiver)
2927
		usb_put_transceiver(udc->transceiver);
2928 2929 2930 2931
free_pools:
	dma_pool_destroy(udc->td_pool);
free_qh_pool:
	dma_pool_destroy(udc->qh_pool);
2932
free_udc:
D
David Lopo 已提交
2933
	kfree(udc);
2934
	*_udc = NULL;
D
David Lopo 已提交
2935 2936 2937 2938 2939 2940 2941 2942
	return retval;
}

/**
 * udc_remove: parent remove must call this to remove UDC
 *
 * No interrupts active, the IRQ has been released
 */
2943
static void udc_remove(struct ci13xxx *udc)
D
David Lopo 已提交
2944
{
2945
	int i;
D
David Lopo 已提交
2946

2947
	if (udc == NULL)
D
David Lopo 已提交
2948
		return;
2949

2950
	usb_del_gadget_udc(&udc->gadget);
D
David Lopo 已提交
2951

2952 2953 2954 2955 2956 2957 2958 2959 2960
	for (i = 0; i < udc->hw_ep_max; i++) {
		struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];

		dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
	}

	dma_pool_destroy(udc->td_pool);
	dma_pool_destroy(udc->qh_pool);

2961
	if (udc->transceiver) {
2962
		otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
2963
		usb_put_transceiver(udc->transceiver);
2964
	}
D
David Lopo 已提交
2965 2966 2967 2968 2969
#ifdef CONFIG_USB_GADGET_DEBUG_FILES
	dbg_remove_files(&udc->gadget.dev);
#endif
	device_unregister(&udc->gadget.dev);

2970
	kfree(udc->hw_bank.regmap);
D
David Lopo 已提交
2971 2972
	kfree(udc);
}
2973 2974 2975 2976 2977

static int __devinit ci_udc_probe(struct platform_device *pdev)
{
	struct device	*dev = &pdev->dev;
	struct ci13xxx_udc_driver *driver = dev->platform_data;
2978
	struct ci13xxx	*udc;
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
	struct resource	*res;
	void __iomem	*base;
	int		ret;

	if (!driver) {
		dev_err(dev, "platform data missing\n");
		return -ENODEV;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(dev, "missing resource\n");
		return -ENODEV;
	}

	base = devm_request_and_ioremap(dev, res);
	if (!res) {
		dev_err(dev, "can't request and ioremap resource\n");
		return -ENOMEM;
	}

3000
	ret = udc_probe(driver, dev, base, &udc);
3001 3002 3003
	if (ret)
		return ret;

3004 3005
	udc->irq = platform_get_irq(pdev, 0);
	if (udc->irq < 0) {
3006 3007 3008 3009 3010
		dev_err(dev, "missing IRQ\n");
		ret = -ENODEV;
		goto out;
	}

3011 3012
	platform_set_drvdata(pdev, udc);
	ret = request_irq(udc->irq, udc_irq, IRQF_SHARED, driver->name, udc);
3013 3014 3015

out:
	if (ret)
3016
		udc_remove(udc);
3017 3018 3019 3020 3021 3022

	return ret;
}

static int __devexit ci_udc_remove(struct platform_device *pdev)
{
3023 3024 3025 3026
	struct ci13xxx *udc = platform_get_drvdata(pdev);

	free_irq(udc->irq, udc);
	udc_remove(udc);
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045

	return 0;
}

static struct platform_driver ci_udc_driver = {
	.probe	= ci_udc_probe,
	.remove	= __devexit_p(ci_udc_remove),
	.driver	= {
		.name	= "ci_udc",
	},
};

module_platform_driver(ci_udc_driver);

MODULE_ALIAS("platform:ci_udc");
MODULE_ALIAS("platform:ci13xxx");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
MODULE_DESCRIPTION("ChipIdea UDC Driver");