udc.c 44.0 KB
Newer Older
D
David Lopo 已提交
1
/*
2
 * udc.h - ChipIdea UDC driver
D
David Lopo 已提交
3 4 5 6 7 8 9 10 11 12
 *
 * 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.
 */

13
#include <linux/delay.h>
D
David Lopo 已提交
14 15 16 17
#include <linux/device.h>
#include <linux/dmapool.h>
#include <linux/dma-mapping.h>
#include <linux/init.h>
18 19
#include <linux/platform_device.h>
#include <linux/module.h>
D
David Lopo 已提交
20 21 22 23
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/kernel.h>
24
#include <linux/slab.h>
25
#include <linux/pm_runtime.h>
D
David Lopo 已提交
26 27
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
28
#include <linux/usb/otg.h>
29
#include <linux/usb/chipidea.h>
D
David Lopo 已提交
30

31 32 33 34
#include "ci.h"
#include "udc.h"
#include "bits.h"
#include "debug.h"
35

D
David Lopo 已提交
36 37
/* control endpoint description */
static const struct usb_endpoint_descriptor
38
ctrl_endpt_out_desc = {
D
David Lopo 已提交
39 40 41
	.bLength         = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,

42 43 44 45 46 47 48 49 50 51 52
	.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 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	.bmAttributes    = USB_ENDPOINT_XFER_CONTROL,
	.wMaxPacketSize  = cpu_to_le16(CTRL_PAYLOAD_MAX),
};

/**
 * 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);
}

69
static inline int ep_to_bit(struct ci13xxx *udc, int n)
70
{
71
	int fill = 16 - udc->hw_ep_max / 2;
72

73
	if (n >= udc->hw_ep_max / 2)
74 75 76 77 78
		n += fill;

	return n;
}

D
David Lopo 已提交
79 80 81 82 83 84 85
/**
 * 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
 */
86
static int hw_device_state(struct ci13xxx *udc, u32 dma)
D
David Lopo 已提交
87 88
{
	if (dma) {
89
		hw_write(udc, OP_ENDPTLISTADDR, ~0, dma);
D
David Lopo 已提交
90
		/* interrupt, error, port change, reset, sleep/suspend */
91
		hw_write(udc, OP_USBINTR, ~0,
D
David Lopo 已提交
92
			     USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
93
		hw_write(udc, OP_USBCMD, USBCMD_RS, USBCMD_RS);
D
David Lopo 已提交
94
	} else {
95 96
		hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
		hw_write(udc, OP_USBINTR, ~0, 0);
D
David Lopo 已提交
97 98 99 100 101 102 103 104 105 106 107
	}
	return 0;
}

/**
 * hw_ep_flush: flush endpoint fifo (execute without interruption)
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
108
static int hw_ep_flush(struct ci13xxx *udc, int num, int dir)
D
David Lopo 已提交
109 110 111 112 113
{
	int n = hw_ep_bit(num, dir);

	do {
		/* flush any pending transfer */
114 115
		hw_write(udc, OP_ENDPTFLUSH, BIT(n), BIT(n));
		while (hw_read(udc, OP_ENDPTFLUSH, BIT(n)))
D
David Lopo 已提交
116
			cpu_relax();
117
	} while (hw_read(udc, OP_ENDPTSTAT, BIT(n)));
D
David Lopo 已提交
118 119 120 121 122 123 124 125 126 127 128

	return 0;
}

/**
 * hw_ep_disable: disables endpoint (execute without interruption)
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
129
static int hw_ep_disable(struct ci13xxx *udc, int num, int dir)
D
David Lopo 已提交
130
{
131
	hw_ep_flush(udc, num, dir);
132
	hw_write(udc, OP_ENDPTCTRL + num,
133
		 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
D
David Lopo 已提交
134 135 136 137 138 139 140 141 142 143 144
	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
 */
145
static int hw_ep_enable(struct ci13xxx *udc, int num, int dir, int type)
D
David Lopo 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
{
	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;
	}
168
	hw_write(udc, OP_ENDPTCTRL + num, mask, data);
D
David Lopo 已提交
169 170 171 172 173 174 175 176 177 178
	return 0;
}

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

183
	return hw_read(udc, OP_ENDPTCTRL + num, mask) ? 1 : 0;
D
David Lopo 已提交
184 185 186 187 188
}

/**
 * hw_test_and_clear_setup_status: test & clear setup status (execute without
 *                                 interruption)
189
 * @n: endpoint number
D
David Lopo 已提交
190 191 192
 *
 * This function returns setup status
 */
193
static int hw_test_and_clear_setup_status(struct ci13xxx *udc, int n)
D
David Lopo 已提交
194
{
195
	n = ep_to_bit(udc, n);
196
	return hw_test_and_clear(udc, OP_ENDPTSETUPSTAT, BIT(n));
D
David Lopo 已提交
197 198 199 200 201 202 203 204 205 206
}

/**
 * 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
 */
207
static int hw_ep_prime(struct ci13xxx *udc, int num, int dir, int is_ctrl)
D
David Lopo 已提交
208 209 210
{
	int n = hw_ep_bit(num, dir);

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

214
	hw_write(udc, OP_ENDPTPRIME, BIT(n), BIT(n));
D
David Lopo 已提交
215

216
	while (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
D
David Lopo 已提交
217
		cpu_relax();
218
	if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
D
David Lopo 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		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
 */
234
static int hw_ep_set_halt(struct ci13xxx *udc, int num, int dir, int value)
D
David Lopo 已提交
235 236 237 238 239
{
	if (value != 0 && value != 1)
		return -EINVAL;

	do {
240
		enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
D
David Lopo 已提交
241 242 243 244
		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 */
245 246
		hw_write(udc, reg, mask_xs|mask_xr,
			  value ? mask_xs : mask_xr);
247
	} while (value != hw_ep_get_halt(udc, num, dir));
D
David Lopo 已提交
248 249 250 251 252 253 254 255 256

	return 0;
}

/**
 * hw_is_port_high_speed: test if port is high speed
 *
 * This function returns true if high speed port
 */
257
static int hw_port_is_high_speed(struct ci13xxx *udc)
D
David Lopo 已提交
258
{
259 260
	return udc->hw_bank.lpm ? hw_read(udc, OP_DEVLC, DEVLC_PSPD) :
		hw_read(udc, OP_PORTSC, PORTSC_HSP);
D
David Lopo 已提交
261 262 263 264 265 266 267
}

/**
 * hw_read_intr_enable: returns interrupt enable register
 *
 * This function returns register data
 */
268
static u32 hw_read_intr_enable(struct ci13xxx *udc)
D
David Lopo 已提交
269
{
270
	return hw_read(udc, OP_USBINTR, ~0);
D
David Lopo 已提交
271 272 273 274 275 276 277
}

/**
 * hw_read_intr_status: returns interrupt status register
 *
 * This function returns register data
 */
278
static u32 hw_read_intr_status(struct ci13xxx *udc)
D
David Lopo 已提交
279
{
280
	return hw_read(udc, OP_USBSTS, ~0);
D
David Lopo 已提交
281 282 283 284 285
}

/**
 * hw_test_and_clear_complete: test & clear complete status (execute without
 *                             interruption)
286
 * @n: endpoint number
D
David Lopo 已提交
287 288 289
 *
 * This function returns complete status
 */
290
static int hw_test_and_clear_complete(struct ci13xxx *udc, int n)
D
David Lopo 已提交
291
{
292
	n = ep_to_bit(udc, n);
293
	return hw_test_and_clear(udc, OP_ENDPTCOMPLETE, BIT(n));
D
David Lopo 已提交
294 295 296 297 298 299 300 301
}

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

306
	hw_write(udc, OP_USBSTS, ~0, reg);
D
David Lopo 已提交
307 308 309 310 311 312 313 314 315
	return reg;
}

/**
 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
 *                                interruption)
 *
 * This function returns guard value
 */
316
static int hw_test_and_clear_setup_guard(struct ci13xxx *udc)
D
David Lopo 已提交
317
{
318
	return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, 0);
D
David Lopo 已提交
319 320 321 322 323 324 325 326
}

/**
 * hw_test_and_set_setup_guard: test & set setup guard (execute without
 *                              interruption)
 *
 * This function returns guard value
 */
327
static int hw_test_and_set_setup_guard(struct ci13xxx *udc)
D
David Lopo 已提交
328
{
329
	return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
D
David Lopo 已提交
330 331 332 333 334 335
}

/**
 * hw_usb_set_address: configures USB address (execute without interruption)
 * @value: new USB address
 *
336 337
 * This function explicitly sets the address, without the "USBADRA" (advance)
 * feature, which is not supported by older versions of the controller.
D
David Lopo 已提交
338
 */
339
static void hw_usb_set_address(struct ci13xxx *udc, u8 value)
D
David Lopo 已提交
340
{
341 342
	hw_write(udc, OP_DEVICEADDR, DEVICEADDR_USBADR,
		 value << ffs_nr(DEVICEADDR_USBADR));
D
David Lopo 已提交
343 344 345 346 347 348 349 350
}

/**
 * hw_usb_reset: restart device after a bus reset (execute without
 *               interruption)
 *
 * This function returns an error code
 */
351
static int hw_usb_reset(struct ci13xxx *udc)
D
David Lopo 已提交
352
{
353
	hw_usb_set_address(udc, 0);
D
David Lopo 已提交
354 355

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

	/* clear setup token semaphores */
359
	hw_write(udc, OP_ENDPTSETUPSTAT, 0,  0);
D
David Lopo 已提交
360 361

	/* clear complete status */
362
	hw_write(udc, OP_ENDPTCOMPLETE,  0,  0);
D
David Lopo 已提交
363 364

	/* wait until all bits cleared */
365
	while (hw_read(udc, OP_ENDPTPRIME, ~0))
D
David Lopo 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		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;
}

/******************************************************************************
 * 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)
{
397
	struct ci13xxx *udc = mEp->udc;
D
David Lopo 已提交
398
	unsigned i;
399 400
	int ret = 0;
	unsigned length = mReq->req.length;
D
David Lopo 已提交
401 402 403 404 405 406

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

	mReq->req.status = -EALREADY;
407
	if (length && mReq->req.dma == DMA_ADDR_INVALID) {
D
David Lopo 已提交
408 409
		mReq->req.dma = \
			dma_map_single(mEp->device, mReq->req.buf,
410 411
				       length, mEp->dir ? DMA_TO_DEVICE :
				       DMA_FROM_DEVICE);
D
David Lopo 已提交
412 413 414 415 416 417
		if (mReq->req.dma == 0)
			return -ENOMEM;

		mReq->map = 1;
	}

418 419 420 421 422 423 424 425
	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);
426
				mReq->req.dma = DMA_ADDR_INVALID;
427 428 429 430 431 432 433 434 435 436
				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 已提交
437 438 439 440 441
	/*
	 * TD configuration
	 * TODO - handle requests which spawns into several TDs
	 */
	memset(mReq->ptr, 0, sizeof(*mReq->ptr));
442
	mReq->ptr->token    = length << ffs_nr(TD_TOTAL_BYTES);
D
David Lopo 已提交
443 444
	mReq->ptr->token   &= TD_TOTAL_BYTES;
	mReq->ptr->token   |= TD_STATUS_ACTIVE;
445 446 447 448 449 450 451
	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 已提交
452 453 454
	mReq->ptr->page[0]  = mReq->req.dma;
	for (i = 1; i < 5; i++)
		mReq->ptr->page[i] =
455
			(mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
D
David Lopo 已提交
456

457 458 459 460 461 462 463 464 465 466 467 468
	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();
469
		if (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
470 471
			goto done;
		do {
472 473 474 475
			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);
476 477 478 479 480
		if (tmp_stat)
			goto done;
	}

	/*  QH configuration */
481 482
	mEp->qh.ptr->td.next   = mReq->dma;    /* TERMINATE = 0 */
	mEp->qh.ptr->td.token &= ~TD_STATUS;   /* clear status */
483
	mEp->qh.ptr->cap |=  QH_ZLT;
D
David Lopo 已提交
484 485 486

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

487
	ret = hw_ep_prime(udc, mEp->num, mEp->dir,
D
David Lopo 已提交
488
			   mEp->type == USB_ENDPOINT_XFER_CONTROL);
489 490
done:
	return ret;
D
David Lopo 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504
}

/**
 * _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)
{
	if (mReq->req.status != -EALREADY)
		return -EINVAL;

505 506 507 508 509 510 511 512 513
	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 已提交
514 515 516 517 518 519

	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);
520
		mReq->req.dma = DMA_ADDR_INVALID;
D
David Lopo 已提交
521 522 523 524
		mReq->map     = 0;
	}

	mReq->req.status = mReq->ptr->token & TD_STATUS;
525
	if ((TD_STATUS_HALTED & mReq->req.status) != 0)
D
David Lopo 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
		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)
{
	if (mEp == NULL)
		return -EINVAL;

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

556
	while (!list_empty(&mEp->qh.queue)) {
D
David Lopo 已提交
557 558 559

		/* pop oldest request */
		struct ci13xxx_req *mReq = \
560
			list_entry(mEp->qh.queue.next,
D
David Lopo 已提交
561 562 563 564
				   struct ci13xxx_req, queue);
		list_del_init(&mReq->queue);
		mReq->req.status = -ESHUTDOWN;

565
		if (mReq->req.complete != NULL) {
D
David Lopo 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
			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);
584
	unsigned long flags;
D
David Lopo 已提交
585 586 587 588

	if (gadget == NULL)
		return -EINVAL;

589
	spin_lock_irqsave(&udc->lock, flags);
590 591 592
	udc->gadget.speed = USB_SPEED_UNKNOWN;
	udc->remote_wakeup = 0;
	udc->suspended = 0;
593
	spin_unlock_irqrestore(&udc->lock, flags);
594

D
David Lopo 已提交
595 596 597 598
	/* flush all endpoints */
	gadget_for_each_ep(ep, gadget) {
		usb_ep_fifo_flush(ep);
	}
599 600
	usb_ep_fifo_flush(&udc->ep0out->ep);
	usb_ep_fifo_flush(&udc->ep0in->ep);
D
David Lopo 已提交
601

602 603
	if (udc->driver)
		udc->driver->disconnect(gadget);
D
David Lopo 已提交
604 605 606 607 608 609

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

610
	if (udc->status != NULL) {
611
		usb_ep_free_request(&udc->ep0in->ep, udc->status);
612
		udc->status = NULL;
D
David Lopo 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	}

	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;

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

635
	spin_unlock(&udc->lock);
D
David Lopo 已提交
636 637 638 639
	retval = _gadget_stop_activity(&udc->gadget);
	if (retval)
		goto done;

640
	retval = hw_usb_reset(udc);
D
David Lopo 已提交
641 642 643
	if (retval)
		goto done;

644
	udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC);
645 646
	if (udc->status == NULL)
		retval = -ENOMEM;
647

648
	spin_lock(&udc->lock);
D
David Lopo 已提交
649 650 651

 done:
	if (retval)
652
		dev_err(udc->dev, "error: %i\n", retval);
D
David Lopo 已提交
653 654 655 656 657 658 659 660 661 662 663
}

/**
 * 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)
{
664
	if (ep == NULL || req == NULL)
D
David Lopo 已提交
665 666 667 668 669 670 671 672
		return;

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

/**
 * isr_get_status_response: get_status request response
673
 * @udc: udc struct
D
David Lopo 已提交
674 675 676 677
 * @setup: setup request packet
 *
 * This function returns an error code
 */
678
static int isr_get_status_response(struct ci13xxx *udc,
D
David Lopo 已提交
679 680 681 682
				   struct usb_ctrlrequest *setup)
__releases(mEp->lock)
__acquires(mEp->lock)
{
683
	struct ci13xxx_ep *mEp = udc->ep0in;
D
David Lopo 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	struct usb_request *req = NULL;
	gfp_t gfp_flags = GFP_ATOMIC;
	int dir, num, retval;

	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) {
706
		/* Assume that device is bus powered for now. */
707
		*(u16 *)req->buf = udc->remote_wakeup << 1;
D
David Lopo 已提交
708 709 710 711 712 713
		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;
714
		*(u16 *)req->buf = hw_ep_get_halt(udc, num, dir);
D
David Lopo 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
	}
	/* 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;
}

735 736 737 738 739 740 741 742 743 744 745 746 747 748
/**
 * 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;

749 750 751 752 753
	if (udc->setaddr) {
		hw_usb_set_address(udc, udc->address);
		udc->setaddr = false;
	}

754
	spin_lock_irqsave(&udc->lock, flags);
755
	if (udc->test_mode)
756 757
		hw_port_test_set(udc, udc->test_mode);
	spin_unlock_irqrestore(&udc->lock, flags);
758 759
}

D
David Lopo 已提交
760 761
/**
 * isr_setup_status_phase: queues the status phase of a setup transation
762
 * @udc: udc struct
D
David Lopo 已提交
763 764 765
 *
 * This function returns an error code
 */
766
static int isr_setup_status_phase(struct ci13xxx *udc)
D
David Lopo 已提交
767 768 769 770
__releases(mEp->lock)
__acquires(mEp->lock)
{
	int retval;
771
	struct ci13xxx_ep *mEp;
D
David Lopo 已提交
772

773
	mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in;
774 775
	udc->status->context = udc;
	udc->status->complete = isr_setup_status_complete;
D
David Lopo 已提交
776 777

	spin_unlock(mEp->lock);
778
	retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
D
David Lopo 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
	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)
{
795
	struct ci13xxx_req *mReq, *mReqTemp;
796
	struct ci13xxx_ep *mEpTemp = mEp;
797
	int uninitialized_var(retval);
D
David Lopo 已提交
798

799
	if (list_empty(&mEp->qh.queue))
D
David Lopo 已提交
800 801
		return -EINVAL;

802 803 804 805 806 807 808 809 810
	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);
811 812
			if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
					mReq->req.length)
813
				mEpTemp = mEp->udc->ep0in;
814
			mReq->req.complete(&mEpTemp->ep, &mReq->req);
815 816
			spin_lock(mEp->lock);
		}
817 818
	}

819
	if (retval == -EBUSY)
820 821 822
		retval = 0;
	if (retval < 0)
		dbg_event(_usb_addr(mEp), "DONE", retval);
D
David Lopo 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

	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;
838
	u8 tmode = 0;
D
David Lopo 已提交
839

840
	for (i = 0; i < udc->hw_ep_max; i++) {
D
David Lopo 已提交
841
		struct ci13xxx_ep *mEp  = &udc->ci13xxx_ep[i];
842
		int type, num, dir, err = -EINVAL;
D
David Lopo 已提交
843 844
		struct usb_ctrlrequest req;

845
		if (mEp->ep.desc == NULL)
D
David Lopo 已提交
846 847
			continue;   /* not configured */

848
		if (hw_test_and_clear_complete(udc, i)) {
D
David Lopo 已提交
849 850 851
			err = isr_tr_complete_low(mEp);
			if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
				if (err > 0)   /* needs status phase */
852
					err = isr_setup_status_phase(udc);
D
David Lopo 已提交
853 854 855
				if (err < 0) {
					dbg_event(_usb_addr(mEp),
						  "ERROR", err);
856
					spin_unlock(&udc->lock);
D
David Lopo 已提交
857
					if (usb_ep_set_halt(&mEp->ep))
858
						dev_err(udc->dev,
859
							"error: ep_set_halt\n");
860
					spin_lock(&udc->lock);
D
David Lopo 已提交
861 862 863 864 865
				}
			}
		}

		if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
866
		    !hw_test_and_clear_setup_status(udc, i))
D
David Lopo 已提交
867 868 869
			continue;

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

874 875 876 877
		/*
		 * Flush data and handshake transactions of previous
		 * setup packet.
		 */
878 879
		_ep_nuke(udc->ep0out);
		_ep_nuke(udc->ep0in);
880

D
David Lopo 已提交
881 882
		/* read_setup_packet */
		do {
883
			hw_test_and_set_setup_guard(udc);
884
			memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
885
		} while (!hw_test_and_clear_setup_guard(udc));
D
David Lopo 已提交
886 887 888

		type = req.bRequestType;

889
		udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
D
David Lopo 已提交
890 891 892 893 894

		dbg_setup(_usb_addr(mEp), &req);

		switch (req.bRequest) {
		case USB_REQ_CLEAR_FEATURE:
895 896 897 898 899 900
			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);
901
				dir = num & USB_ENDPOINT_DIR_MASK;
902
				num &= USB_ENDPOINT_NUMBER_MASK;
903
				if (dir) /* TX */
904
					num += udc->hw_ep_max/2;
905
				if (!udc->ci13xxx_ep[num].wedge) {
906
					spin_unlock(&udc->lock);
907 908
					err = usb_ep_clear_halt(
						&udc->ci13xxx_ep[num].ep);
909
					spin_lock(&udc->lock);
910 911 912 913 914 915 916 917
					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 已提交
918
					break;
919 920 921 922
				udc->remote_wakeup = 0;
				err = isr_setup_status_phase(udc);
			} else {
				goto delegate;
D
David Lopo 已提交
923 924 925 926 927 928 929 930 931 932
			}
			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;
933
			err = isr_get_status_response(udc, &req);
D
David Lopo 已提交
934 935 936 937 938 939 940
			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;
941 942
			udc->address = (u8)le16_to_cpu(req.wValue);
			udc->setaddr = true;
943
			err = isr_setup_status_phase(udc);
D
David Lopo 已提交
944 945
			break;
		case USB_REQ_SET_FEATURE:
946 947 948 949 950 951
			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);
952
				dir = num & USB_ENDPOINT_DIR_MASK;
953
				num &= USB_ENDPOINT_NUMBER_MASK;
954
				if (dir) /* TX */
955
					num += udc->hw_ep_max/2;
D
David Lopo 已提交
956

957
				spin_unlock(&udc->lock);
958
				err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
959
				spin_lock(&udc->lock);
960
				if (!err)
961 962
					isr_setup_status_phase(udc);
			} else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
963 964
				if (req.wLength != 0)
					break;
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
				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;
				}
988 989 990
			} else {
				goto delegate;
			}
D
David Lopo 已提交
991 992 993 994
			break;
		default:
delegate:
			if (req.wLength == 0)   /* no data phase */
995
				udc->ep0_dir = TX;
D
David Lopo 已提交
996

997
			spin_unlock(&udc->lock);
D
David Lopo 已提交
998
			err = udc->driver->setup(&udc->gadget, &req);
999
			spin_lock(&udc->lock);
D
David Lopo 已提交
1000 1001 1002 1003 1004 1005
			break;
		}

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

1006
			spin_unlock(&udc->lock);
D
David Lopo 已提交
1007
			if (usb_ep_set_halt(&mEp->ep))
1008
				dev_err(udc->dev, "error: ep_set_halt\n");
1009
			spin_lock(&udc->lock);
D
David Lopo 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		}
	}
}

/******************************************************************************
 * 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);
1026
	int retval = 0;
D
David Lopo 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035
	unsigned long flags;

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

	spin_lock_irqsave(mEp->lock, flags);

	/* only internal SW should enable ctrl endpts */

1036
	mEp->ep.desc = desc;
D
David Lopo 已提交
1037

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

1041 1042 1043
	mEp->dir  = usb_endpoint_dir_in(desc) ? TX : RX;
	mEp->num  = usb_endpoint_num(desc);
	mEp->type = usb_endpoint_type(desc);
D
David Lopo 已提交
1044

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

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

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

1051 1052 1053 1054 1055 1056
	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 已提交
1057

1058 1059 1060
	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 已提交
1061

1062 1063 1064 1065 1066
	/*
	 * Enable endpoints in the HW other than ep0 as ep0
	 * is always enabled
	 */
	if (mEp->num)
1067
		retval |= hw_ep_enable(mEp->udc, mEp->num, mEp->dir, mEp->type);
D
David Lopo 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

	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;

	if (ep == NULL)
		return -EINVAL;
1086
	else if (mEp->ep.desc == NULL)
D
David Lopo 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
		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);
1098
		retval |= hw_ep_disable(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
1099 1100 1101 1102 1103 1104

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

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

1105
	mEp->ep.desc = NULL;
D
David Lopo 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

	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;

1121
	if (ep == NULL)
D
David Lopo 已提交
1122 1123 1124 1125 1126
		return NULL;

	mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
	if (mReq != NULL) {
		INIT_LIST_HEAD(&mReq->queue);
1127
		mReq->req.dma = DMA_ADDR_INVALID;
D
David Lopo 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

		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;

	if (ep == NULL || req == NULL) {
		return;
	} else if (!list_empty(&mReq->queue)) {
1156
		dev_err(mEp->udc->dev, "freeing queued request\n");
D
David Lopo 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
		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);
1181
	struct ci13xxx *udc = mEp->udc;
D
David Lopo 已提交
1182 1183 1184
	int retval = 0;
	unsigned long flags;

1185
	if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
1186 1187 1188 1189
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

1190 1191
	if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
		if (req->length)
1192 1193
			mEp = (udc->ep0_dir == RX) ?
			       udc->ep0out : udc->ep0in;
1194 1195 1196
		if (!list_empty(&mEp->qh.queue)) {
			_ep_nuke(mEp);
			retval = -EOVERFLOW;
1197 1198
			dev_warn(mEp->udc->dev, "endpoint ctrl %X nuked\n",
				 _usb_addr(mEp));
1199
		}
D
David Lopo 已提交
1200 1201 1202 1203 1204
	}

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

1209 1210
	if (req->length > 4 * CI13XXX_PAGE_SIZE) {
		req->length = 4 * CI13XXX_PAGE_SIZE;
D
David Lopo 已提交
1211
		retval = -EMSGSIZE;
1212
		dev_warn(mEp->udc->dev, "request length truncated\n");
D
David Lopo 已提交
1213 1214 1215 1216 1217 1218 1219 1220
	}

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

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

1221
	retval = _hardware_enqueue(mEp, mReq);
1222 1223

	if (retval == -EALREADY) {
D
David Lopo 已提交
1224 1225 1226
		dbg_event(_usb_addr(mEp), "QUEUE", retval);
		retval = 0;
	}
1227 1228
	if (!retval)
		list_add_tail(&mReq->queue, &mEp->qh.queue);
D
David Lopo 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

 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;

1246
	if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
1247
		mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
1248
		list_empty(&mEp->qh.queue))
D
David Lopo 已提交
1249 1250 1251 1252 1253 1254
		return -EINVAL;

	spin_lock_irqsave(mEp->lock, flags);

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

1255
	hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
1256 1257 1258

	/* pop request */
	list_del_init(&mReq->queue);
1259 1260 1261
	if (mReq->map) {
		dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
				 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1262
		mReq->req.dma = DMA_ADDR_INVALID;
1263 1264
		mReq->map     = 0;
	}
D
David Lopo 已提交
1265 1266
	req->status = -ECONNRESET;

1267
	if (mReq->req.complete != NULL) {
D
David Lopo 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
		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;

1288
	if (ep == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
1289 1290 1291 1292 1293 1294 1295
		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 &&
1296
	    !list_empty(&mEp->qh.queue)) {
D
David Lopo 已提交
1297 1298 1299 1300 1301 1302 1303 1304
		spin_unlock_irqrestore(mEp->lock, flags);
		return -EAGAIN;
	}
#endif

	direction = mEp->dir;
	do {
		dbg_event(_usb_addr(mEp), "HALT", value);
1305
		retval |= hw_ep_set_halt(mEp->udc, mEp->num, mEp->dir, value);
D
David Lopo 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

		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;

1329
	if (ep == NULL || mEp->ep.desc == NULL)
D
David Lopo 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
		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;

	if (ep == NULL) {
1353
		dev_err(mEp->udc->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
D
David Lopo 已提交
1354 1355 1356 1357 1358 1359
		return;
	}

	spin_lock_irqsave(mEp->lock, flags);

	dbg_event(_usb_addr(mEp), "FFLUSH", 0);
1360
	hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
D
David Lopo 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

	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
 *****************************************************************************/
1384 1385 1386 1387 1388 1389 1390 1391 1392
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;

1393
	spin_lock_irqsave(&udc->lock, flags);
1394 1395 1396
	udc->vbus_active = is_active;
	if (udc->driver)
		gadget_ready = 1;
1397
	spin_unlock_irqrestore(&udc->lock, flags);
1398 1399 1400

	if (gadget_ready) {
		if (is_active) {
1401
			pm_runtime_get_sync(&_gadget->dev);
1402
			hw_device_reset(udc);
1403
			hw_device_state(udc, udc->ep0out->qh.dma);
1404
		} else {
1405
			hw_device_state(udc, 0);
1406 1407 1408 1409
			if (udc->udc_driver->notify_event)
				udc->udc_driver->notify_event(udc,
				CI13XXX_CONTROLLER_STOPPED_EVENT);
			_gadget_stop_activity(&udc->gadget);
1410
			pm_runtime_put_sync(&_gadget->dev);
1411 1412 1413 1414 1415 1416
		}
	}

	return 0;
}

1417 1418 1419 1420 1421 1422
static int ci13xxx_wakeup(struct usb_gadget *_gadget)
{
	struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
	unsigned long flags;
	int ret = 0;

1423
	spin_lock_irqsave(&udc->lock, flags);
1424 1425 1426 1427
	if (!udc->remote_wakeup) {
		ret = -EOPNOTSUPP;
		goto out;
	}
1428
	if (!hw_read(udc, OP_PORTSC, PORTSC_SUSP)) {
1429 1430 1431
		ret = -EINVAL;
		goto out;
	}
1432
	hw_write(udc, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1433
out:
1434
	spin_unlock_irqrestore(&udc->lock, flags);
1435 1436 1437
	return ret;
}

1438 1439 1440 1441 1442
static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
{
	struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);

	if (udc->transceiver)
1443
		return usb_phy_set_power(udc->transceiver, mA);
1444 1445 1446
	return -ENOTSUPP;
}

1447 1448 1449 1450
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 已提交
1451 1452 1453 1454 1455
/**
 * 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
 */
1456 1457
static const struct usb_gadget_ops usb_gadget_ops = {
	.vbus_session	= ci13xxx_vbus_session,
1458
	.wakeup		= ci13xxx_wakeup,
1459
	.vbus_draw	= ci13xxx_vbus_draw,
1460 1461
	.udc_start	= ci13xxx_start,
	.udc_stop	= ci13xxx_stop,
1462
};
D
David Lopo 已提交
1463

1464
static int init_eps(struct ci13xxx *udc)
D
David Lopo 已提交
1465
{
1466
	int retval = 0, i, j;
D
David Lopo 已提交
1467

1468
	for (i = 0; i < udc->hw_ep_max/2; i++)
1469
		for (j = RX; j <= TX; j++) {
1470
			int k = i + j * udc->hw_ep_max/2;
1471
			struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
D
David Lopo 已提交
1472

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

1476 1477
			mEp->udc          = udc;
			mEp->lock         = &udc->lock;
1478 1479
			mEp->device       = &udc->gadget.dev;
			mEp->td_pool      = udc->td_pool;
D
David Lopo 已提交
1480

1481 1482 1483
			mEp->ep.name      = mEp->name;
			mEp->ep.ops       = &usb_ep_ops;
			mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
D
David Lopo 已提交
1484

1485 1486
			INIT_LIST_HEAD(&mEp->qh.queue);
			mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
1487
						     &mEp->qh.dma);
1488
			if (mEp->qh.ptr == NULL)
D
David Lopo 已提交
1489 1490
				retval = -ENOMEM;
			else
1491 1492
				memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
			/*
			 * 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;

1503
				continue;
1504
			}
1505

D
David Lopo 已提交
1506
			list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
1507
		}
1508 1509 1510 1511 1512 1513

	return retval;
}

/**
 * ci13xxx_start: register a gadget driver
1514
 * @gadget: our gadget
1515 1516 1517 1518
 * @driver: the driver being registered
 *
 * Interrupts are enabled here.
 */
1519 1520
static int ci13xxx_start(struct usb_gadget *gadget,
			 struct usb_gadget_driver *driver)
1521
{
1522
	struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1523 1524 1525
	unsigned long flags;
	int retval = -ENOMEM;

1526
	if (driver->disconnect == NULL)
1527 1528 1529
		return -EINVAL;


1530 1531
	udc->ep0out->ep.desc = &ctrl_endpt_out_desc;
	retval = usb_ep_enable(&udc->ep0out->ep);
1532 1533
	if (retval)
		return retval;
1534

1535 1536
	udc->ep0in->ep.desc = &ctrl_endpt_in_desc;
	retval = usb_ep_enable(&udc->ep0in->ep);
1537 1538
	if (retval)
		return retval;
1539
	spin_lock_irqsave(&udc->lock, flags);
D
David Lopo 已提交
1540

1541
	udc->driver = driver;
1542
	pm_runtime_get_sync(&udc->gadget.dev);
1543 1544 1545 1546 1547
	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 {
1548
			pm_runtime_put_sync(&udc->gadget.dev);
1549 1550 1551 1552
			goto done;
		}
	}

1553
	retval = hw_device_state(udc, udc->ep0out->qh.dma);
1554 1555
	if (retval)
		pm_runtime_put_sync(&udc->gadget.dev);
D
David Lopo 已提交
1556 1557

 done:
1558
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1559 1560 1561 1562
	return retval;
}

/**
1563
 * ci13xxx_stop: unregister a gadget driver
D
David Lopo 已提交
1564
 */
1565 1566
static int ci13xxx_stop(struct usb_gadget *gadget,
			struct usb_gadget_driver *driver)
D
David Lopo 已提交
1567
{
1568 1569
	struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
	unsigned long flags;
D
David Lopo 已提交
1570

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

1573 1574
	if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
			udc->vbus_active) {
1575
		hw_device_state(udc, 0);
1576 1577 1578
		if (udc->udc_driver->notify_event)
			udc->udc_driver->notify_event(udc,
			CI13XXX_CONTROLLER_STOPPED_EVENT);
1579
		udc->driver = NULL;
1580
		spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1581
		_gadget_stop_activity(&udc->gadget);
1582
		spin_lock_irqsave(&udc->lock, flags);
1583
		pm_runtime_put(&udc->gadget.dev);
1584
	}
D
David Lopo 已提交
1585

1586
	spin_unlock_irqrestore(&udc->lock, flags);
D
David Lopo 已提交
1587 1588 1589 1590 1591 1592 1593 1594

	return 0;
}

/******************************************************************************
 * BUS block
 *****************************************************************************/
/**
1595
 * udc_irq: udc interrupt handler
D
David Lopo 已提交
1596 1597 1598 1599
 *
 * This function returns IRQ_HANDLED if the IRQ has been handled
 * It locks access to registers
 */
1600
static irqreturn_t udc_irq(struct ci13xxx *udc)
D
David Lopo 已提交
1601 1602 1603 1604
{
	irqreturn_t retval;
	u32 intr;

1605
	if (udc == NULL)
D
David Lopo 已提交
1606 1607
		return IRQ_HANDLED;

1608
	spin_lock(&udc->lock);
1609 1610

	if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
1611 1612
		if (hw_read(udc, OP_USBMODE, USBMODE_CM) !=
				USBMODE_CM_DEVICE) {
1613
			spin_unlock(&udc->lock);
1614 1615 1616
			return IRQ_NONE;
		}
	}
1617
	intr = hw_test_and_clear_intr_active(udc);
1618
	dbg_interrupt(intr);
D
David Lopo 已提交
1619

1620
	if (intr) {
D
David Lopo 已提交
1621
		/* order defines priority - do NOT change it */
1622
		if (USBi_URI & intr)
D
David Lopo 已提交
1623
			isr_reset_handler(udc);
1624

D
David Lopo 已提交
1625
		if (USBi_PCI & intr) {
1626
			udc->gadget.speed = hw_port_is_high_speed(udc) ?
D
David Lopo 已提交
1627
				USB_SPEED_HIGH : USB_SPEED_FULL;
1628
			if (udc->suspended && udc->driver->resume) {
1629
				spin_unlock(&udc->lock);
1630
				udc->driver->resume(&udc->gadget);
1631
				spin_lock(&udc->lock);
1632 1633
				udc->suspended = 0;
			}
D
David Lopo 已提交
1634
		}
1635 1636

		if (USBi_UI  & intr)
D
David Lopo 已提交
1637
			isr_tr_complete_handler(udc);
1638

1639
		if (USBi_SLI & intr) {
1640 1641
			if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
			    udc->driver->suspend) {
1642
				udc->suspended = 1;
1643
				spin_unlock(&udc->lock);
1644
				udc->driver->suspend(&udc->gadget);
1645
				spin_lock(&udc->lock);
1646 1647
			}
		}
D
David Lopo 已提交
1648 1649 1650 1651
		retval = IRQ_HANDLED;
	} else {
		retval = IRQ_NONE;
	}
1652
	spin_unlock(&udc->lock);
D
David Lopo 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

	return retval;
}

/**
 * udc_release: driver release function
 * @dev: device
 *
 * Currently does nothing
 */
static void udc_release(struct device *dev)
{
}

/**
1668 1669
 * udc_start: initialize gadget role
 * @udc: chipidea controller
D
David Lopo 已提交
1670
 */
1671
static int udc_start(struct ci13xxx *udc)
D
David Lopo 已提交
1672
{
1673
	struct device *dev = udc->dev;
D
David Lopo 已提交
1674 1675
	int retval = 0;

1676
	if (!udc)
D
David Lopo 已提交
1677 1678
		return -EINVAL;

1679
	spin_lock_init(&udc->lock);
D
David Lopo 已提交
1680

1681
	udc->gadget.ops          = &usb_gadget_ops;
D
David Lopo 已提交
1682
	udc->gadget.speed        = USB_SPEED_UNKNOWN;
1683
	udc->gadget.max_speed    = USB_SPEED_HIGH;
D
David Lopo 已提交
1684
	udc->gadget.is_otg       = 0;
1685
	udc->gadget.name         = udc->udc_driver->name;
D
David Lopo 已提交
1686 1687 1688

	INIT_LIST_HEAD(&udc->gadget.ep_list);

1689
	dev_set_name(&udc->gadget.dev, "gadget");
D
David Lopo 已提交
1690
	udc->gadget.dev.dma_mask = dev->dma_mask;
1691
	udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
D
David Lopo 已提交
1692 1693 1694
	udc->gadget.dev.parent   = dev;
	udc->gadget.dev.release  = udc_release;

1695 1696 1697 1698
	/* alloc resources */
	udc->qh_pool = dma_pool_create("ci13xxx_qh", dev,
				       sizeof(struct ci13xxx_qh),
				       64, CI13XXX_PAGE_SIZE);
1699 1700
	if (udc->qh_pool == NULL)
		return -ENOMEM;
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

	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;
	}

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

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

1716
	udc->transceiver = usb_get_transceiver();
1717 1718 1719 1720

	if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
		if (udc->transceiver == NULL) {
			retval = -ENODEV;
1721
			goto free_pools;
1722 1723 1724 1725 1726 1727 1728 1729 1730
		}
	}

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

D
David Lopo 已提交
1731
	retval = device_register(&udc->gadget.dev);
1732 1733 1734 1735
	if (retval) {
		put_device(&udc->gadget.dev);
		goto put_transceiver;
	}
D
David Lopo 已提交
1736 1737

	retval = dbg_create_files(&udc->gadget.dev);
1738 1739 1740 1741
	if (retval)
		goto unreg_device;

	if (udc->transceiver) {
1742 1743
		retval = otg_set_peripheral(udc->transceiver->otg,
						&udc->gadget);
1744 1745
		if (retval)
			goto remove_dbg;
D
David Lopo 已提交
1746
	}
1747 1748 1749 1750 1751

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

1752 1753
	pm_runtime_no_callbacks(&udc->gadget.dev);
	pm_runtime_enable(&udc->gadget.dev);
D
David Lopo 已提交
1754 1755 1756

	return retval;

1757 1758
remove_trans:
	if (udc->transceiver) {
1759
		otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
1760
		usb_put_transceiver(udc->transceiver);
1761 1762
	}

1763
	dev_err(dev, "error = %i\n", retval);
1764 1765 1766 1767 1768 1769
remove_dbg:
	dbg_remove_files(&udc->gadget.dev);
unreg_device:
	device_unregister(&udc->gadget.dev);
put_transceiver:
	if (udc->transceiver)
1770
		usb_put_transceiver(udc->transceiver);
1771 1772 1773 1774
free_pools:
	dma_pool_destroy(udc->td_pool);
free_qh_pool:
	dma_pool_destroy(udc->qh_pool);
D
David Lopo 已提交
1775 1776 1777 1778 1779 1780 1781 1782
	return retval;
}

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

1787
	if (udc == NULL)
D
David Lopo 已提交
1788
		return;
1789

1790
	usb_del_gadget_udc(&udc->gadget);
D
David Lopo 已提交
1791

1792 1793 1794 1795 1796 1797 1798 1799 1800
	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);

1801
	if (udc->transceiver) {
1802
		otg_set_peripheral(udc->transceiver->otg, NULL);
1803
		usb_put_transceiver(udc->transceiver);
1804
	}
D
David Lopo 已提交
1805 1806
	dbg_remove_files(&udc->gadget.dev);
	device_unregister(&udc->gadget.dev);
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	/* my kobject is dynamic, I swear! */
	memset(&udc->gadget, 0, sizeof(udc->gadget));
}

/**
 * ci_hdrc_gadget_init - initialize device related bits
 * ci: the controller
 *
 * This function enables the gadget role, if the device is "device capable".
 */
int ci_hdrc_gadget_init(struct ci13xxx *ci)
{
	struct ci_role_driver *rdrv;

	if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
		return -ENXIO;

	rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
	if (!rdrv)
		return -ENOMEM;

	rdrv->start	= udc_start;
	rdrv->stop	= udc_stop;
	rdrv->irq	= udc_irq;
	rdrv->name	= "gadget";
	ci->roles[CI_ROLE_GADGET] = rdrv;
D
David Lopo 已提交
1833

1834
	return 0;
D
David Lopo 已提交
1835
}