udc.c 51.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
D
David Lopo 已提交
2
/*
3
 * udc.c - ChipIdea UDC driver
D
David Lopo 已提交
4 5 6 7 8 9
 *
 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
 *
 * Author: David Lopo
 */

10
#include <linux/delay.h>
D
David Lopo 已提交
11 12
#include <linux/device.h>
#include <linux/dmapool.h>
13
#include <linux/err.h>
14
#include <linux/irqreturn.h>
D
David Lopo 已提交
15
#include <linux/kernel.h>
16
#include <linux/slab.h>
17
#include <linux/pm_runtime.h>
18
#include <linux/pinctrl/consumer.h>
D
David Lopo 已提交
19 20
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
21
#include <linux/usb/otg-fsm.h>
22
#include <linux/usb/chipidea.h>
D
David Lopo 已提交
23

24 25 26
#include "ci.h"
#include "udc.h"
#include "bits.h"
27
#include "otg.h"
28
#include "otg_fsm.h"
29

D
David Lopo 已提交
30 31
/* control endpoint description */
static const struct usb_endpoint_descriptor
32
ctrl_endpt_out_desc = {
D
David Lopo 已提交
33 34 35
	.bLength         = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,

36 37 38 39 40 41 42 43 44 45 46
	.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 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
	.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)
{
60
	return num + ((dir == TX) ? 16 : 0);
D
David Lopo 已提交
61 62
}

63
static inline int ep_to_bit(struct ci_hdrc *ci, int n)
64
{
65
	int fill = 16 - ci->hw_ep_max / 2;
66

67
	if (n >= ci->hw_ep_max / 2)
68 69 70 71 72
		n += fill;

	return n;
}

D
David Lopo 已提交
73
/**
74
 * hw_device_state: enables/disables interrupts (execute without interruption)
75
 * @ci: the controller
D
David Lopo 已提交
76 77 78 79
 * @dma: 0 => disable, !0 => enable and set dma engine
 *
 * This function returns an error code
 */
80
static int hw_device_state(struct ci_hdrc *ci, u32 dma)
D
David Lopo 已提交
81 82
{
	if (dma) {
83
		hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
D
David Lopo 已提交
84
		/* interrupt, error, port change, reset, sleep/suspend */
85
		hw_write(ci, OP_USBINTR, ~0,
D
David Lopo 已提交
86 87
			     USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
	} else {
88
		hw_write(ci, OP_USBINTR, ~0, 0);
D
David Lopo 已提交
89 90 91 92 93 94
	}
	return 0;
}

/**
 * hw_ep_flush: flush endpoint fifo (execute without interruption)
95
 * @ci: the controller
D
David Lopo 已提交
96 97 98 99 100
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
101
static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
D
David Lopo 已提交
102 103 104 105 106
{
	int n = hw_ep_bit(num, dir);

	do {
		/* flush any pending transfer */
107
		hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
108
		while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
D
David Lopo 已提交
109
			cpu_relax();
110
	} while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
D
David Lopo 已提交
111 112 113 114 115 116

	return 0;
}

/**
 * hw_ep_disable: disables endpoint (execute without interruption)
117
 * @ci: the controller
D
David Lopo 已提交
118 119 120 121 122
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns an error code
 */
123
static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
D
David Lopo 已提交
124
{
125
	hw_write(ci, OP_ENDPTCTRL + num,
126
		 (dir == TX) ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
D
David Lopo 已提交
127 128 129 130 131
	return 0;
}

/**
 * hw_ep_enable: enables endpoint (execute without interruption)
132
 * @ci: the controller
D
David Lopo 已提交
133 134 135 136 137 138
 * @num:  endpoint number
 * @dir:  endpoint direction
 * @type: endpoint type
 *
 * This function returns an error code
 */
139
static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
D
David Lopo 已提交
140 141 142
{
	u32 mask, data;

143
	if (dir == TX) {
D
David Lopo 已提交
144
		mask  = ENDPTCTRL_TXT;  /* type    */
145
		data  = type << __ffs(mask);
D
David Lopo 已提交
146 147 148 149 150 151 152 153

		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    */
154
		data  = type << __ffs(mask);
D
David Lopo 已提交
155 156 157 158 159 160 161

		mask |= ENDPTCTRL_RXS;  /* unstall */
		mask |= ENDPTCTRL_RXR;  /* reset data toggle */
		data |= ENDPTCTRL_RXR;
		mask |= ENDPTCTRL_RXE;  /* enable  */
		data |= ENDPTCTRL_RXE;
	}
162
	hw_write(ci, OP_ENDPTCTRL + num, mask, data);
D
David Lopo 已提交
163 164 165 166 167
	return 0;
}

/**
 * hw_ep_get_halt: return endpoint halt status
168
 * @ci: the controller
D
David Lopo 已提交
169 170 171 172 173
 * @num: endpoint number
 * @dir: endpoint direction
 *
 * This function returns 1 if endpoint halted
 */
174
static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
D
David Lopo 已提交
175
{
176
	u32 mask = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
D
David Lopo 已提交
177

178
	return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
D
David Lopo 已提交
179 180 181 182
}

/**
 * hw_ep_prime: primes endpoint (execute without interruption)
183
 * @ci: the controller
D
David Lopo 已提交
184 185 186 187 188 189
 * @num:     endpoint number
 * @dir:     endpoint direction
 * @is_ctrl: true if control endpoint
 *
 * This function returns an error code
 */
190
static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
D
David Lopo 已提交
191 192 193
{
	int n = hw_ep_bit(num, dir);

194 195 196
	/* Synchronize before ep prime */
	wmb();

197
	if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
D
David Lopo 已提交
198 199
		return -EAGAIN;

200
	hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
D
David Lopo 已提交
201

202
	while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
D
David Lopo 已提交
203
		cpu_relax();
204
	if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
D
David Lopo 已提交
205 206 207 208 209 210 211 212 213
		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)
214
 * @ci: the controller
D
David Lopo 已提交
215 216 217 218 219 220
 * @num:   endpoint number
 * @dir:   endpoint direction
 * @value: true => stall, false => unstall
 *
 * This function returns an error code
 */
221
static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
D
David Lopo 已提交
222 223 224 225 226
{
	if (value != 0 && value != 1)
		return -EINVAL;

	do {
227
		enum ci_hw_regs reg = OP_ENDPTCTRL + num;
228 229
		u32 mask_xs = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
		u32 mask_xr = (dir == TX) ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
D
David Lopo 已提交
230 231

		/* data toggle - reserved for EP0 but it's in ESS */
232
		hw_write(ci, reg, mask_xs|mask_xr,
233
			  value ? mask_xs : mask_xr);
234
	} while (value != hw_ep_get_halt(ci, num, dir));
D
David Lopo 已提交
235 236 237 238 239 240

	return 0;
}

/**
 * hw_is_port_high_speed: test if port is high speed
241
 * @ci: the controller
D
David Lopo 已提交
242 243 244
 *
 * This function returns true if high speed port
 */
245
static int hw_port_is_high_speed(struct ci_hdrc *ci)
D
David Lopo 已提交
246
{
247 248
	return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
		hw_read(ci, OP_PORTSC, PORTSC_HSP);
D
David Lopo 已提交
249 250 251 252 253
}

/**
 * hw_test_and_clear_complete: test & clear complete status (execute without
 *                             interruption)
254
 * @ci: the controller
255
 * @n: endpoint number
D
David Lopo 已提交
256 257 258
 *
 * This function returns complete status
 */
259
static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
D
David Lopo 已提交
260
{
261 262
	n = ep_to_bit(ci, n);
	return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
D
David Lopo 已提交
263 264 265 266 267
}

/**
 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
 *                                without interruption)
268
 * @ci: the controller
D
David Lopo 已提交
269 270 271
 *
 * This function returns active interrutps
 */
272
static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
D
David Lopo 已提交
273
{
274
	u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
D
David Lopo 已提交
275

276
	hw_write(ci, OP_USBSTS, ~0, reg);
D
David Lopo 已提交
277 278 279 280 281 282
	return reg;
}

/**
 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
 *                                interruption)
283
 * @ci: the controller
D
David Lopo 已提交
284 285 286
 *
 * This function returns guard value
 */
287
static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
D
David Lopo 已提交
288
{
289
	return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
D
David Lopo 已提交
290 291 292 293 294
}

/**
 * hw_test_and_set_setup_guard: test & set setup guard (execute without
 *                              interruption)
295
 * @ci: the controller
D
David Lopo 已提交
296 297 298
 *
 * This function returns guard value
 */
299
static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
D
David Lopo 已提交
300
{
301
	return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
D
David Lopo 已提交
302 303 304 305
}

/**
 * hw_usb_set_address: configures USB address (execute without interruption)
306
 * @ci: the controller
D
David Lopo 已提交
307 308
 * @value: new USB address
 *
309 310
 * This function explicitly sets the address, without the "USBADRA" (advance)
 * feature, which is not supported by older versions of the controller.
D
David Lopo 已提交
311
 */
312
static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
D
David Lopo 已提交
313
{
314
	hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
315
		 value << __ffs(DEVICEADDR_USBADR));
D
David Lopo 已提交
316 317 318 319 320
}

/**
 * hw_usb_reset: restart device after a bus reset (execute without
 *               interruption)
321
 * @ci: the controller
D
David Lopo 已提交
322 323 324
 *
 * This function returns an error code
 */
325
static int hw_usb_reset(struct ci_hdrc *ci)
D
David Lopo 已提交
326
{
327
	hw_usb_set_address(ci, 0);
D
David Lopo 已提交
328 329

	/* ESS flushes only at end?!? */
330
	hw_write(ci, OP_ENDPTFLUSH,    ~0, ~0);
D
David Lopo 已提交
331 332

	/* clear setup token semaphores */
333
	hw_write(ci, OP_ENDPTSETUPSTAT, 0,  0);
D
David Lopo 已提交
334 335

	/* clear complete status */
336
	hw_write(ci, OP_ENDPTCOMPLETE,  0,  0);
D
David Lopo 已提交
337 338

	/* wait until all bits cleared */
339
	while (hw_read(ci, OP_ENDPTPRIME, ~0))
D
David Lopo 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352
		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
 *****************************************************************************/
353

354
static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
355
			unsigned int length, struct scatterlist *s)
356
{
357 358
	int i;
	u32 temp;
359 360 361 362 363 364
	struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
						  GFP_ATOMIC);

	if (node == NULL)
		return -ENOMEM;

365
	node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma);
366 367 368 369 370
	if (node->ptr == NULL) {
		kfree(node);
		return -ENOMEM;
	}

371 372 373
	node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
	node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
	node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
374 375 376 377 378 379
	if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
		u32 mul = hwreq->req.length / hwep->ep.maxpacket;

		if (hwreq->req.length == 0
				|| hwreq->req.length % hwep->ep.maxpacket)
			mul++;
380
		node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO));
381
	}
382

383 384 385 386 387 388 389
	if (s) {
		temp = (u32) (sg_dma_address(s) + hwreq->req.actual);
		node->td_remaining_size = CI_MAX_BUF_SIZE - length;
	} else {
		temp = (u32) (hwreq->req.dma + hwreq->req.actual);
	}

390 391 392
	if (length) {
		node->ptr->page[0] = cpu_to_le32(temp);
		for (i = 1; i < TD_PAGE_COUNT; i++) {
393
			u32 page = temp + i * CI_HDRC_PAGE_SIZE;
394 395 396 397 398
			page &= ~TD_RESERVED_MASK;
			node->ptr->page[i] = cpu_to_le32(page);
		}
	}

399
	hwreq->req.actual += length;
400

401
	if (!list_empty(&hwreq->tds)) {
402
		/* get the last entry */
403
		lastnode = list_entry(hwreq->tds.prev,
404 405 406 407 408
				struct td_node, td);
		lastnode->ptr->next = cpu_to_le32(node->dma);
	}

	INIT_LIST_HEAD(&node->td);
409
	list_add_tail(&node->td, &hwreq->tds);
410 411 412 413

	return 0;
}

D
David Lopo 已提交
414 415 416 417
/**
 * _usb_addr: calculates endpoint address from direction & number
 * @ep:  endpoint
 */
418
static inline u8 _usb_addr(struct ci_hw_ep *ep)
D
David Lopo 已提交
419 420 421 422
{
	return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static int prepare_td_for_non_sg(struct ci_hw_ep *hwep,
		struct ci_hw_req *hwreq)
{
	unsigned int rest = hwreq->req.length;
	int pages = TD_PAGE_COUNT;
	int ret = 0;

	if (rest == 0) {
		ret = add_td_to_list(hwep, hwreq, 0, NULL);
		if (ret < 0)
			return ret;
	}

	/*
	 * The first buffer could be not page aligned.
	 * In that case we have to span into one extra td.
	 */
	if (hwreq->req.dma % PAGE_SIZE)
		pages--;

	while (rest > 0) {
		unsigned int count = min(hwreq->req.length - hwreq->req.actual,
			(unsigned int)(pages * CI_HDRC_PAGE_SIZE));

		ret = add_td_to_list(hwep, hwreq, count, NULL);
		if (ret < 0)
			return ret;

		rest -= count;
	}

	if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
	    && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
		ret = add_td_to_list(hwep, hwreq, 0, NULL);
		if (ret < 0)
			return ret;
	}

	return ret;
}

static int prepare_td_per_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
		struct scatterlist *s)
{
	unsigned int rest = sg_dma_len(s);
	int ret = 0;

	hwreq->req.actual = 0;
	while (rest > 0) {
		unsigned int count = min_t(unsigned int, rest,
				CI_MAX_BUF_SIZE);

		ret = add_td_to_list(hwep, hwreq, count, s);
		if (ret < 0)
			return ret;

		rest -= count;
	}

	return ret;
}

static void ci_add_buffer_entry(struct td_node *node, struct scatterlist *s)
{
	int empty_td_slot_index = (CI_MAX_BUF_SIZE - node->td_remaining_size)
			/ CI_HDRC_PAGE_SIZE;
	int i;
490
	u32 token;
491

492 493
	token = le32_to_cpu(node->ptr->token) + (sg_dma_len(s) << __ffs(TD_TOTAL_BYTES));
	node->ptr->token = cpu_to_le32(token);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

	for (i = empty_td_slot_index; i < TD_PAGE_COUNT; i++) {
		u32 page = (u32) sg_dma_address(s) +
			(i - empty_td_slot_index) * CI_HDRC_PAGE_SIZE;

		page &= ~TD_RESERVED_MASK;
		node->ptr->page[i] = cpu_to_le32(page);
	}
}

static int prepare_td_for_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
{
	struct usb_request *req = &hwreq->req;
	struct scatterlist *s = req->sg;
	int ret = 0, i = 0;
	struct td_node *node = NULL;

	if (!s || req->zero || req->length == 0) {
		dev_err(hwep->ci->dev, "not supported operation for sg\n");
		return -EINVAL;
	}

	while (i++ < req->num_mapped_sgs) {
		if (sg_dma_address(s) % PAGE_SIZE) {
			dev_err(hwep->ci->dev, "not page aligned sg buffer\n");
			return -EINVAL;
		}

		if (node && (node->td_remaining_size >= sg_dma_len(s))) {
			ci_add_buffer_entry(node, s);
			node->td_remaining_size -= sg_dma_len(s);
		} else {
			ret = prepare_td_per_sg(hwep, hwreq, s);
			if (ret)
				return ret;

			node = list_entry(hwreq->tds.prev,
				struct td_node, td);
		}

		s = sg_next(s);
	}

	return ret;
}

D
David Lopo 已提交
540
/**
541
 * _hardware_enqueue: configures a request at hardware level
542
 * @hwep:   endpoint
543
 * @hwreq:  request
D
David Lopo 已提交
544 545 546
 *
 * This function returns an error code
 */
547
static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
D
David Lopo 已提交
548
{
549
	struct ci_hdrc *ci = hwep->ci;
550
	int ret = 0;
551
	struct td_node *firstnode, *lastnode;
D
David Lopo 已提交
552 553

	/* don't queue twice */
554
	if (hwreq->req.status == -EALREADY)
D
David Lopo 已提交
555 556
		return -EALREADY;

557
	hwreq->req.status = -EALREADY;
D
David Lopo 已提交
558

559 560
	ret = usb_gadget_map_request_by_dev(ci->dev->parent,
					    &hwreq->req, hwep->dir);
561 562 563
	if (ret)
		return ret;

564 565 566 567
	if (hwreq->req.num_mapped_sgs)
		ret = prepare_td_for_sg(hwep, hwreq);
	else
		ret = prepare_td_for_non_sg(hwep, hwreq);
D
David Lopo 已提交
568

569 570
	if (ret)
		return ret;
571

572
	firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
573

574
	lastnode = list_entry(hwreq->tds.prev,
575 576 577
		struct td_node, td);

	lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
578
	if (!hwreq->req.no_interrupt)
579
		lastnode->ptr->token |= cpu_to_le32(TD_IOC);
580 581
	wmb();

582 583
	hwreq->req.actual = 0;
	if (!list_empty(&hwep->qh.queue)) {
584
		struct ci_hw_req *hwreqprev;
585
		int n = hw_ep_bit(hwep->num, hwep->dir);
586
		int tmp_stat;
587 588
		struct td_node *prevlastnode;
		u32 next = firstnode->dma & TD_ADDR_MASK;
589

590
		hwreqprev = list_entry(hwep->qh.queue.prev,
591
				struct ci_hw_req, queue);
592
		prevlastnode = list_entry(hwreqprev->tds.prev,
593 594 595
				struct td_node, td);

		prevlastnode->ptr->next = cpu_to_le32(next);
596
		wmb();
597
		if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
598 599
			goto done;
		do {
600 601 602 603
			hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
			tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
		} while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
		hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
604 605 606 607 608
		if (tmp_stat)
			goto done;
	}

	/*  QH configuration */
609 610
	hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
	hwep->qh.ptr->td.token &=
611
		cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
D
David Lopo 已提交
612

613
	if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
614
		u32 mul = hwreq->req.length / hwep->ep.maxpacket;
615

616 617
		if (hwreq->req.length == 0
				|| hwreq->req.length % hwep->ep.maxpacket)
618
			mul++;
619
		hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT));
620 621
	}

622 623
	ret = hw_ep_prime(ci, hwep->num, hwep->dir,
			   hwep->type == USB_ENDPOINT_XFER_CONTROL);
624 625
done:
	return ret;
D
David Lopo 已提交
626 627
}

628
/**
629
 * free_pending_td: remove a pending request for the endpoint
630
 * @hwep: endpoint
631
 */
632
static void free_pending_td(struct ci_hw_ep *hwep)
633
{
634
	struct td_node *pending = hwep->pending_td;
635

636 637
	dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
	hwep->pending_td = NULL;
638 639 640
	kfree(pending);
}

641 642 643
static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
					   struct td_node *node)
{
644
	hwep->qh.ptr->td.next = cpu_to_le32(node->dma);
645 646 647 648 649 650 651
	hwep->qh.ptr->td.token &=
		cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));

	return hw_ep_prime(ci, hwep->num, hwep->dir,
				hwep->type == USB_ENDPOINT_XFER_CONTROL);
}

D
David Lopo 已提交
652 653
/**
 * _hardware_dequeue: handles a request at hardware level
654
 * @hwep: endpoint
655
 * @hwreq:  request
D
David Lopo 已提交
656 657 658
 *
 * This function returns an error code
 */
659
static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
D
David Lopo 已提交
660
{
661
	u32 tmptoken;
662 663
	struct td_node *node, *tmpnode;
	unsigned remaining_length;
664
	unsigned actual = hwreq->req.length;
665
	struct ci_hdrc *ci = hwep->ci;
666

667
	if (hwreq->req.status != -EALREADY)
D
David Lopo 已提交
668 669
		return -EINVAL;

670
	hwreq->req.status = 0;
671

672
	list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
673
		tmptoken = le32_to_cpu(node->ptr->token);
674
		if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
675 676 677 678 679
			int n = hw_ep_bit(hwep->num, hwep->dir);

			if (ci->rev == CI_REVISION_24)
				if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
					reprime_dtd(ci, hwep, node);
680
			hwreq->req.status = -EALREADY;
681
			return -EBUSY;
682
		}
D
David Lopo 已提交
683

684 685 686 687
		remaining_length = (tmptoken & TD_TOTAL_BYTES);
		remaining_length >>= __ffs(TD_TOTAL_BYTES);
		actual -= remaining_length;

688 689 690
		hwreq->req.status = tmptoken & TD_STATUS;
		if ((TD_STATUS_HALTED & hwreq->req.status)) {
			hwreq->req.status = -EPIPE;
691
			break;
692 693
		} else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
			hwreq->req.status = -EPROTO;
694
			break;
695 696
		} else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
			hwreq->req.status = -EILSEQ;
697 698 699 700
			break;
		}

		if (remaining_length) {
701
			if (hwep->dir == TX) {
702
				hwreq->req.status = -EPROTO;
703 704 705 706 707 708 709 710
				break;
			}
		}
		/*
		 * As the hardware could still address the freed td
		 * which will run the udc unusable, the cleanup of the
		 * td has to be delayed by one.
		 */
711 712
		if (hwep->pending_td)
			free_pending_td(hwep);
713

714
		hwep->pending_td = node;
715 716
		list_del_init(&node->td);
	}
D
David Lopo 已提交
717

718 719
	usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent,
					&hwreq->req, hwep->dir);
D
David Lopo 已提交
720

721
	hwreq->req.actual += actual;
D
David Lopo 已提交
722

723 724
	if (hwreq->req.status)
		return hwreq->req.status;
D
David Lopo 已提交
725

726
	return hwreq->req.actual;
D
David Lopo 已提交
727 728 729 730
}

/**
 * _ep_nuke: dequeues all endpoint requests
731
 * @hwep: endpoint
D
David Lopo 已提交
732 733 734 735
 *
 * This function returns an error code
 * Caller must hold lock
 */
736
static int _ep_nuke(struct ci_hw_ep *hwep)
737 738
__releases(hwep->lock)
__acquires(hwep->lock)
D
David Lopo 已提交
739
{
740
	struct td_node *node, *tmpnode;
741
	if (hwep == NULL)
D
David Lopo 已提交
742 743
		return -EINVAL;

744
	hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
D
David Lopo 已提交
745

746
	while (!list_empty(&hwep->qh.queue)) {
D
David Lopo 已提交
747 748

		/* pop oldest request */
749 750
		struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
						     struct ci_hw_req, queue);
751

752 753
		list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
			dma_pool_free(hwep->td_pool, node->ptr, node->dma);
754 755 756
			list_del_init(&node->td);
			node->ptr = NULL;
			kfree(node);
757 758
		}

759 760
		list_del_init(&hwreq->queue);
		hwreq->req.status = -ESHUTDOWN;
D
David Lopo 已提交
761

762 763
		if (hwreq->req.complete != NULL) {
			spin_unlock(hwep->lock);
764
			usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
765
			spin_lock(hwep->lock);
D
David Lopo 已提交
766 767
		}
	}
768

769 770
	if (hwep->pending_td)
		free_pending_td(hwep);
771

D
David Lopo 已提交
772 773 774
	return 0;
}

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
{
	struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
	int direction, retval = 0;
	unsigned long flags;

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

	if (usb_endpoint_xfer_isoc(hwep->ep.desc))
		return -EOPNOTSUPP;

	spin_lock_irqsave(hwep->lock, flags);

	if (value && hwep->dir == TX && check_transfer &&
		!list_empty(&hwep->qh.queue) &&
			!usb_endpoint_xfer_control(hwep->ep.desc)) {
		spin_unlock_irqrestore(hwep->lock, flags);
		return -EAGAIN;
	}

	direction = hwep->dir;
	do {
		retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);

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

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

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

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


D
David Lopo 已提交
813 814 815 816 817 818 819 820 821
/**
 * _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;
822
	struct ci_hdrc    *ci = container_of(gadget, struct ci_hdrc, gadget);
823
	unsigned long flags;
D
David Lopo 已提交
824 825 826 827 828

	/* flush all endpoints */
	gadget_for_each_ep(ep, gadget) {
		usb_ep_fifo_flush(ep);
	}
829 830
	usb_ep_fifo_flush(&ci->ep0out->ep);
	usb_ep_fifo_flush(&ci->ep0in->ep);
D
David Lopo 已提交
831 832 833 834 835 836

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

837 838 839
	if (ci->status != NULL) {
		usb_ep_free_request(&ci->ep0in->ep, ci->status);
		ci->status = NULL;
D
David Lopo 已提交
840 841
	}

842 843 844 845 846 847
	spin_lock_irqsave(&ci->lock, flags);
	ci->gadget.speed = USB_SPEED_UNKNOWN;
	ci->remote_wakeup = 0;
	ci->suspended = 0;
	spin_unlock_irqrestore(&ci->lock, flags);

D
David Lopo 已提交
848 849 850 851 852 853 854 855
	return 0;
}

/******************************************************************************
 * ISR block
 *****************************************************************************/
/**
 * isr_reset_handler: USB reset interrupt handler
856
 * @ci: UDC device
D
David Lopo 已提交
857 858 859
 *
 * This function resets USB engine after a bus reset occurred
 */
860
static void isr_reset_handler(struct ci_hdrc *ci)
861 862
__releases(ci->lock)
__acquires(ci->lock)
D
David Lopo 已提交
863 864 865
{
	int retval;

866
	spin_unlock(&ci->lock);
867 868
	if (ci->gadget.speed != USB_SPEED_UNKNOWN)
		usb_gadget_udc_reset(&ci->gadget, ci->driver);
869

870
	retval = _gadget_stop_activity(&ci->gadget);
D
David Lopo 已提交
871 872 873
	if (retval)
		goto done;

874
	retval = hw_usb_reset(ci);
D
David Lopo 已提交
875 876 877
	if (retval)
		goto done;

878 879
	ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
	if (ci->status == NULL)
880
		retval = -ENOMEM;
881

882
done:
883
	spin_lock(&ci->lock);
D
David Lopo 已提交
884 885

	if (retval)
886
		dev_err(ci->dev, "error: %i\n", retval);
D
David Lopo 已提交
887 888 889 890 891 892 893 894 895 896 897
}

/**
 * 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)
{
898
	if (ep == NULL || req == NULL)
D
David Lopo 已提交
899 900 901 902 903 904
		return;

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

905 906
/**
 * _ep_queue: queues (submits) an I/O request to an endpoint
907 908 909
 * @ep:        endpoint
 * @req:       request
 * @gfp_flags: GFP flags (not used)
910 911
 *
 * Caller must hold lock
912
 * This function returns an error code
913 914 915 916
 */
static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
		    gfp_t __maybe_unused gfp_flags)
{
917 918 919
	struct ci_hw_ep  *hwep  = container_of(ep,  struct ci_hw_ep, ep);
	struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
	struct ci_hdrc *ci = hwep->ci;
920 921
	int retval = 0;

922
	if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
923 924
		return -EINVAL;

925
	if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
926
		if (req->length)
927
			hwep = (ci->ep0_dir == RX) ?
928
			       ci->ep0out : ci->ep0in;
929 930 931 932
		if (!list_empty(&hwep->qh.queue)) {
			_ep_nuke(hwep);
			dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
				 _usb_addr(hwep));
933 934 935
		}
	}

936
	if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
937
	    hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) {
938
		dev_err(hwep->ci->dev, "request length too big for isochronous\n");
939 940 941
		return -EMSGSIZE;
	}

942
	/* first nuke then test link, e.g. previous status has not sent */
943 944
	if (!list_empty(&hwreq->queue)) {
		dev_err(hwep->ci->dev, "request already in queue\n");
945 946 947 948
		return -EBUSY;
	}

	/* push request */
949 950
	hwreq->req.status = -EINPROGRESS;
	hwreq->req.actual = 0;
951

952
	retval = _hardware_enqueue(hwep, hwreq);
953 954 955 956

	if (retval == -EALREADY)
		retval = 0;
	if (!retval)
957
		list_add_tail(&hwreq->queue, &hwep->qh.queue);
958 959 960 961

	return retval;
}

D
David Lopo 已提交
962 963
/**
 * isr_get_status_response: get_status request response
964
 * @ci: ci struct
D
David Lopo 已提交
965 966 967 968
 * @setup: setup request packet
 *
 * This function returns an error code
 */
969
static int isr_get_status_response(struct ci_hdrc *ci,
D
David Lopo 已提交
970
				   struct usb_ctrlrequest *setup)
971 972
__releases(hwep->lock)
__acquires(hwep->lock)
D
David Lopo 已提交
973
{
974
	struct ci_hw_ep *hwep = ci->ep0in;
D
David Lopo 已提交
975 976 977 978
	struct usb_request *req = NULL;
	gfp_t gfp_flags = GFP_ATOMIC;
	int dir, num, retval;

979
	if (hwep == NULL || setup == NULL)
D
David Lopo 已提交
980 981
		return -EINVAL;

982 983 984
	spin_unlock(hwep->lock);
	req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
	spin_lock(hwep->lock);
D
David Lopo 已提交
985 986 987 988 989 990 991 992 993 994 995 996
	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) {
997 998
		*(u16 *)req->buf = (ci->remote_wakeup << 1) |
			ci->gadget.is_selfpowered;
D
David Lopo 已提交
999 1000 1001 1002 1003
	} 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;
1004
		*(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
D
David Lopo 已提交
1005 1006 1007
	}
	/* else do nothing; reserved for future use */

1008
	retval = _ep_queue(&hwep->ep, req, gfp_flags);
D
David Lopo 已提交
1009 1010 1011 1012 1013 1014 1015 1016
	if (retval)
		goto err_free_buf;

	return 0;

 err_free_buf:
	kfree(req->buf);
 err_free_req:
1017 1018 1019
	spin_unlock(hwep->lock);
	usb_ep_free_request(&hwep->ep, req);
	spin_lock(hwep->lock);
D
David Lopo 已提交
1020 1021 1022
	return retval;
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/**
 * 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)
{
1034
	struct ci_hdrc *ci = req->context;
1035 1036
	unsigned long flags;

1037 1038 1039
	if (ci->setaddr) {
		hw_usb_set_address(ci, ci->address);
		ci->setaddr = false;
1040 1041
		if (ci->address)
			usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
1042 1043
	}

1044 1045 1046 1047
	spin_lock_irqsave(&ci->lock, flags);
	if (ci->test_mode)
		hw_port_test_set(ci, ci->test_mode);
	spin_unlock_irqrestore(&ci->lock, flags);
1048 1049
}

D
David Lopo 已提交
1050 1051
/**
 * isr_setup_status_phase: queues the status phase of a setup transation
1052
 * @ci: ci struct
D
David Lopo 已提交
1053 1054 1055
 *
 * This function returns an error code
 */
1056
static int isr_setup_status_phase(struct ci_hdrc *ci)
D
David Lopo 已提交
1057
{
1058
	struct ci_hw_ep *hwep;
D
David Lopo 已提交
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068
	/*
	 * Unexpected USB controller behavior, caused by bad signal integrity
	 * or ground reference problems, can lead to isr_setup_status_phase
	 * being called with ci->status equal to NULL.
	 * If this situation occurs, you should review your USB hardware design.
	 */
	if (WARN_ON_ONCE(!ci->status))
		return -EPIPE;

1069
	hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
1070 1071
	ci->status->context = ci;
	ci->status->complete = isr_setup_status_complete;
D
David Lopo 已提交
1072

1073
	return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
D
David Lopo 已提交
1074 1075 1076 1077
}

/**
 * isr_tr_complete_low: transaction complete low level handler
1078
 * @hwep: endpoint
D
David Lopo 已提交
1079 1080 1081 1082
 *
 * This function returns an error code
 * Caller must hold lock
 */
1083
static int isr_tr_complete_low(struct ci_hw_ep *hwep)
1084 1085
__releases(hwep->lock)
__acquires(hwep->lock)
D
David Lopo 已提交
1086
{
1087 1088
	struct ci_hw_req *hwreq, *hwreqtemp;
	struct ci_hw_ep *hweptemp = hwep;
1089
	int retval = 0;
D
David Lopo 已提交
1090

1091
	list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
1092
			queue) {
1093
		retval = _hardware_dequeue(hwep, hwreq);
1094 1095
		if (retval < 0)
			break;
1096 1097 1098 1099 1100 1101
		list_del_init(&hwreq->queue);
		if (hwreq->req.complete != NULL) {
			spin_unlock(hwep->lock);
			if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
					hwreq->req.length)
				hweptemp = hwep->ci->ep0in;
1102
			usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
1103
			spin_lock(hwep->lock);
1104
		}
1105 1106
	}

1107
	if (retval == -EBUSY)
1108
		retval = 0;
D
David Lopo 已提交
1109 1110 1111 1112

	return retval;
}

1113 1114 1115 1116 1117 1118 1119
static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
{
	dev_warn(&ci->gadget.dev,
		"connect the device to an alternate port if you want HNP\n");
	return isr_setup_status_phase(ci);
}

1120 1121 1122 1123 1124 1125 1126 1127 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 1156 1157 1158 1159
/**
 * isr_setup_packet_handler: setup packet handler
 * @ci: UDC descriptor
 *
 * This function handles setup packet 
 */
static void isr_setup_packet_handler(struct ci_hdrc *ci)
__releases(ci->lock)
__acquires(ci->lock)
{
	struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
	struct usb_ctrlrequest req;
	int type, num, dir, err = -EINVAL;
	u8 tmode = 0;

	/*
	 * Flush data and handshake transactions of previous
	 * setup packet.
	 */
	_ep_nuke(ci->ep0out);
	_ep_nuke(ci->ep0in);

	/* read_setup_packet */
	do {
		hw_test_and_set_setup_guard(ci);
		memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
	} while (!hw_test_and_clear_setup_guard(ci));

	type = req.bRequestType;

	ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;

	switch (req.bRequest) {
	case USB_REQ_CLEAR_FEATURE:
		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);
1160
			dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1161
			num &= USB_ENDPOINT_NUMBER_MASK;
1162
			if (dir == TX)
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
				num += ci->hw_ep_max / 2;
			if (!ci->ci_hw_ep[num].wedge) {
				spin_unlock(&ci->lock);
				err = usb_ep_clear_halt(
					&ci->ci_hw_ep[num].ep);
				spin_lock(&ci->lock);
				if (err)
					break;
			}
			err = isr_setup_status_phase(ci);
		} else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
				le16_to_cpu(req.wValue) ==
				USB_DEVICE_REMOTE_WAKEUP) {
			if (req.wLength != 0)
				break;
			ci->remote_wakeup = 0;
			err = isr_setup_status_phase(ci);
		} else {
			goto delegate;
		}
		break;
	case USB_REQ_GET_STATUS:
1185 1186
		if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) ||
			le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) &&
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		    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;
		err = isr_get_status_response(ci, &req);
		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;
		ci->address = (u8)le16_to_cpu(req.wValue);
		ci->setaddr = true;
		err = isr_setup_status_phase(ci);
		break;
	case USB_REQ_SET_FEATURE:
		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);
1212
			dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1213
			num &= USB_ENDPOINT_NUMBER_MASK;
1214
			if (dir == TX)
1215 1216 1217
				num += ci->hw_ep_max / 2;

			spin_unlock(&ci->lock);
1218
			err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
			spin_lock(&ci->lock);
			if (!err)
				isr_setup_status_phase(ci);
		} else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
			if (req.wLength != 0)
				break;
			switch (le16_to_cpu(req.wValue)) {
			case USB_DEVICE_REMOTE_WAKEUP:
				ci->remote_wakeup = 1;
				err = isr_setup_status_phase(ci);
				break;
			case USB_DEVICE_TEST_MODE:
				tmode = le16_to_cpu(req.wIndex) >> 8;
				switch (tmode) {
1233 1234 1235 1236 1237
				case USB_TEST_J:
				case USB_TEST_K:
				case USB_TEST_SE0_NAK:
				case USB_TEST_PACKET:
				case USB_TEST_FORCE_ENABLE:
1238 1239 1240 1241 1242 1243 1244
					ci->test_mode = tmode;
					err = isr_setup_status_phase(
							ci);
					break;
				default:
					break;
				}
1245 1246 1247 1248 1249 1250 1251 1252
				break;
			case USB_DEVICE_B_HNP_ENABLE:
				if (ci_otg_is_fsm_mode(ci)) {
					ci->gadget.b_hnp_enable = 1;
					err = isr_setup_status_phase(
							ci);
				}
				break;
1253 1254 1255 1256
			case USB_DEVICE_A_ALT_HNP_SUPPORT:
				if (ci_otg_is_fsm_mode(ci))
					err = otg_a_alt_hnp_support(ci);
				break;
1257 1258 1259 1260 1261 1262 1263
			case USB_DEVICE_A_HNP_SUPPORT:
				if (ci_otg_is_fsm_mode(ci)) {
					ci->gadget.a_hnp_support = 1;
					err = isr_setup_status_phase(
							ci);
				}
				break;
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
			default:
				goto delegate;
			}
		} else {
			goto delegate;
		}
		break;
	default:
delegate:
		if (req.wLength == 0)   /* no data phase */
			ci->ep0_dir = TX;

		spin_unlock(&ci->lock);
		err = ci->driver->setup(&ci->gadget, &req);
		spin_lock(&ci->lock);
		break;
	}

	if (err < 0) {
		spin_unlock(&ci->lock);
1284 1285
		if (_ep_set_halt(&hwep->ep, 1, false))
			dev_err(ci->dev, "error: _ep_set_halt\n");
1286 1287 1288 1289
		spin_lock(&ci->lock);
	}
}

D
David Lopo 已提交
1290 1291
/**
 * isr_tr_complete_handler: transaction complete interrupt handler
1292
 * @ci: UDC descriptor
D
David Lopo 已提交
1293 1294 1295
 *
 * This function handles traffic events
 */
1296
static void isr_tr_complete_handler(struct ci_hdrc *ci)
1297 1298
__releases(ci->lock)
__acquires(ci->lock)
D
David Lopo 已提交
1299 1300
{
	unsigned i;
1301
	int err;
D
David Lopo 已提交
1302

1303
	for (i = 0; i < ci->hw_ep_max; i++) {
1304
		struct ci_hw_ep *hwep  = &ci->ci_hw_ep[i];
D
David Lopo 已提交
1305

1306
		if (hwep->ep.desc == NULL)
D
David Lopo 已提交
1307 1308
			continue;   /* not configured */

1309
		if (hw_test_and_clear_complete(ci, i)) {
1310 1311
			err = isr_tr_complete_low(hwep);
			if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
D
David Lopo 已提交
1312
				if (err > 0)   /* needs status phase */
1313
					err = isr_setup_status_phase(ci);
D
David Lopo 已提交
1314
				if (err < 0) {
1315
					spin_unlock(&ci->lock);
1316
					if (_ep_set_halt(&hwep->ep, 1, false))
1317
						dev_err(ci->dev,
1318
						"error: _ep_set_halt\n");
1319
					spin_lock(&ci->lock);
D
David Lopo 已提交
1320 1321 1322 1323
				}
			}
		}

1324
		/* Only handle setup packet below */
1325 1326 1327
		if (i == 0 &&
			hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
			isr_setup_packet_handler(ci);
D
David Lopo 已提交
1328 1329 1330 1331 1332 1333
	}
}

/******************************************************************************
 * ENDPT block
 *****************************************************************************/
1334
/*
D
David Lopo 已提交
1335 1336 1337 1338 1339 1340 1341
 * 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)
{
1342
	struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1343
	int retval = 0;
D
David Lopo 已提交
1344
	unsigned long flags;
1345
	u32 cap = 0;
D
David Lopo 已提交
1346 1347 1348 1349

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

1350
	spin_lock_irqsave(hwep->lock, flags);
D
David Lopo 已提交
1351 1352 1353

	/* only internal SW should enable ctrl endpts */

1354
	if (!list_empty(&hwep->qh.queue)) {
1355
		dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1356 1357 1358 1359 1360
		spin_unlock_irqrestore(hwep->lock, flags);
		return -EBUSY;
	}

	hwep->ep.desc = desc;
D
David Lopo 已提交
1361

1362 1363 1364
	hwep->dir  = usb_endpoint_dir_in(desc) ? TX : RX;
	hwep->num  = usb_endpoint_num(desc);
	hwep->type = usb_endpoint_type(desc);
D
David Lopo 已提交
1365

1366
	hwep->ep.maxpacket = usb_endpoint_maxp(desc);
1367
	hwep->ep.mult = usb_endpoint_maxp_mult(desc);
D
David Lopo 已提交
1368

1369
	if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1370
		cap |= QH_IOS;
1371 1372

	cap |= QH_ZLT;
1373
	cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1374 1375 1376 1377 1378 1379
	/*
	 * For ISO-TX, we set mult at QH as the largest value, and use
	 * MultO at TD as real mult value.
	 */
	if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
		cap |= 3 << __ffs(QH_MULT);
1380

1381
	hwep->qh.ptr->cap = cpu_to_le32(cap);
1382

1383
	hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE);   /* needed? */
D
David Lopo 已提交
1384

1385 1386 1387 1388 1389
	if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
		dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
		retval = -EINVAL;
	}

1390 1391 1392 1393
	/*
	 * Enable endpoints in the HW other than ep0 as ep0
	 * is always enabled
	 */
1394 1395 1396
	if (hwep->num)
		retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
				       hwep->type);
D
David Lopo 已提交
1397

1398
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1399 1400 1401
	return retval;
}

1402
/*
D
David Lopo 已提交
1403 1404 1405 1406 1407 1408
 * 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)
{
1409
	struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
D
David Lopo 已提交
1410 1411 1412 1413 1414
	int direction, retval = 0;
	unsigned long flags;

	if (ep == NULL)
		return -EINVAL;
1415
	else if (hwep->ep.desc == NULL)
D
David Lopo 已提交
1416 1417
		return -EBUSY;

1418
	spin_lock_irqsave(hwep->lock, flags);
1419 1420 1421 1422
	if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
		spin_unlock_irqrestore(hwep->lock, flags);
		return 0;
	}
D
David Lopo 已提交
1423 1424 1425

	/* only internal SW should disable ctrl endpts */

1426
	direction = hwep->dir;
D
David Lopo 已提交
1427
	do {
1428 1429
		retval |= _ep_nuke(hwep);
		retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
D
David Lopo 已提交
1430

1431 1432
		if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
			hwep->dir = (hwep->dir == TX) ? RX : TX;
D
David Lopo 已提交
1433

1434
	} while (hwep->dir != direction);
D
David Lopo 已提交
1435

1436
	hwep->ep.desc = NULL;
D
David Lopo 已提交
1437

1438
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1439 1440 1441
	return retval;
}

1442
/*
D
David Lopo 已提交
1443 1444 1445 1446 1447 1448
 * 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)
{
1449
	struct ci_hw_req *hwreq = NULL;
D
David Lopo 已提交
1450

1451
	if (ep == NULL)
D
David Lopo 已提交
1452 1453
		return NULL;

1454
	hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1455 1456 1457
	if (hwreq != NULL) {
		INIT_LIST_HEAD(&hwreq->queue);
		INIT_LIST_HEAD(&hwreq->tds);
D
David Lopo 已提交
1458 1459
	}

1460
	return (hwreq == NULL) ? NULL : &hwreq->req;
D
David Lopo 已提交
1461 1462
}

1463
/*
D
David Lopo 已提交
1464 1465 1466 1467 1468 1469
 * 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)
{
1470 1471
	struct ci_hw_ep  *hwep  = container_of(ep,  struct ci_hw_ep, ep);
	struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1472
	struct td_node *node, *tmpnode;
D
David Lopo 已提交
1473 1474 1475 1476
	unsigned long flags;

	if (ep == NULL || req == NULL) {
		return;
1477 1478
	} else if (!list_empty(&hwreq->queue)) {
		dev_err(hwep->ci->dev, "freeing queued request\n");
D
David Lopo 已提交
1479 1480 1481
		return;
	}

1482
	spin_lock_irqsave(hwep->lock, flags);
D
David Lopo 已提交
1483

1484 1485
	list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
		dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1486 1487 1488 1489
		list_del_init(&node->td);
		node->ptr = NULL;
		kfree(node);
	}
1490

1491
	kfree(hwreq);
D
David Lopo 已提交
1492

1493
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1494 1495
}

1496
/*
D
David Lopo 已提交
1497 1498 1499 1500 1501 1502 1503
 * 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)
{
1504
	struct ci_hw_ep  *hwep  = container_of(ep,  struct ci_hw_ep, ep);
D
David Lopo 已提交
1505 1506 1507
	int retval = 0;
	unsigned long flags;

1508
	if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
D
David Lopo 已提交
1509 1510
		return -EINVAL;

1511
	spin_lock_irqsave(hwep->lock, flags);
1512 1513 1514 1515
	if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
		spin_unlock_irqrestore(hwep->lock, flags);
		return 0;
	}
1516
	retval = _ep_queue(ep, req, gfp_flags);
1517
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1518 1519 1520
	return retval;
}

1521
/*
D
David Lopo 已提交
1522 1523 1524 1525 1526 1527
 * 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)
{
1528 1529
	struct ci_hw_ep  *hwep  = container_of(ep,  struct ci_hw_ep, ep);
	struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
D
David Lopo 已提交
1530
	unsigned long flags;
1531
	struct td_node *node, *tmpnode;
D
David Lopo 已提交
1532

1533 1534 1535
	if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
		hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
		list_empty(&hwep->qh.queue))
D
David Lopo 已提交
1536 1537
		return -EINVAL;

1538
	spin_lock_irqsave(hwep->lock, flags);
1539 1540
	if (hwep->ci->gadget.speed != USB_SPEED_UNKNOWN)
		hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
D
David Lopo 已提交
1541

1542 1543 1544 1545 1546 1547
	list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
		dma_pool_free(hwep->td_pool, node->ptr, node->dma);
		list_del(&node->td);
		kfree(node);
	}

D
David Lopo 已提交
1548
	/* pop request */
1549
	list_del_init(&hwreq->queue);
1550

1551
	usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1552

D
David Lopo 已提交
1553 1554
	req->status = -ECONNRESET;

1555 1556
	if (hwreq->req.complete != NULL) {
		spin_unlock(hwep->lock);
1557
		usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1558
		spin_lock(hwep->lock);
D
David Lopo 已提交
1559 1560
	}

1561
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1562 1563 1564
	return 0;
}

1565
/*
D
David Lopo 已提交
1566 1567 1568 1569 1570 1571
 * 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)
{
1572
	return _ep_set_halt(ep, value, true);
D
David Lopo 已提交
1573 1574
}

1575
/*
D
David Lopo 已提交
1576 1577 1578 1579 1580 1581
 * 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)
{
1582
	struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
D
David Lopo 已提交
1583 1584
	unsigned long flags;

1585
	if (ep == NULL || hwep->ep.desc == NULL)
D
David Lopo 已提交
1586 1587
		return -EINVAL;

1588 1589 1590
	spin_lock_irqsave(hwep->lock, flags);
	hwep->wedge = 1;
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1591 1592 1593 1594

	return usb_ep_set_halt(ep);
}

1595
/*
D
David Lopo 已提交
1596 1597 1598 1599 1600 1601
 * 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)
{
1602
	struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
D
David Lopo 已提交
1603 1604 1605
	unsigned long flags;

	if (ep == NULL) {
1606
		dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
D
David Lopo 已提交
1607 1608 1609
		return;
	}

1610
	spin_lock_irqsave(hwep->lock, flags);
1611 1612 1613 1614
	if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
		spin_unlock_irqrestore(hwep->lock, flags);
		return;
	}
D
David Lopo 已提交
1615

1616
	hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
D
David Lopo 已提交
1617

1618
	spin_unlock_irqrestore(hwep->lock, flags);
D
David Lopo 已提交
1619 1620
}

1621
/*
D
David Lopo 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
 * 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
 *****************************************************************************/
1640
/*
1641 1642 1643 1644 1645 1646 1647
 * ci_hdrc_gadget_connect: caller makes sure gadget driver is binded
 */
static void ci_hdrc_gadget_connect(struct usb_gadget *_gadget, int is_active)
{
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);

	if (is_active) {
1648
		pm_runtime_get_sync(ci->dev);
1649
		hw_device_reset(ci);
1650
		spin_lock_irq(&ci->lock);
1651 1652 1653
		if (ci->driver) {
			hw_device_state(ci, ci->ep0out->qh.dma);
			usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1654
			spin_unlock_irq(&ci->lock);
1655
			usb_udc_vbus_handler(_gadget, true);
1656 1657
		} else {
			spin_unlock_irq(&ci->lock);
1658
		}
1659 1660 1661 1662 1663 1664 1665 1666 1667
	} else {
		usb_udc_vbus_handler(_gadget, false);
		if (ci->driver)
			ci->driver->disconnect(&ci->gadget);
		hw_device_state(ci, 0);
		if (ci->platdata->notify_event)
			ci->platdata->notify_event(ci,
			CI_HDRC_CONTROLLER_STOPPED_EVENT);
		_gadget_stop_activity(&ci->gadget);
1668
		pm_runtime_put_sync(ci->dev);
1669 1670 1671 1672
		usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
	}
}

1673
static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1674
{
1675
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1676
	unsigned long flags;
1677
	int ret = 0;
1678

1679 1680 1681
	spin_lock_irqsave(&ci->lock, flags);
	ci->vbus_active = is_active;
	spin_unlock_irqrestore(&ci->lock, flags);
1682

1683 1684 1685 1686
	if (ci->usb_phy)
		usb_phy_set_charger_state(ci->usb_phy, is_active ?
			USB_CHARGER_PRESENT : USB_CHARGER_ABSENT);

1687 1688 1689 1690
	if (ci->platdata->notify_event)
		ret = ci->platdata->notify_event(ci,
				CI_HDRC_CONTROLLER_VBUS_EVENT);

1691
	if (ci->driver)
1692
		ci_hdrc_gadget_connect(_gadget, is_active);
1693

1694
	return ret;
1695 1696
}

1697
static int ci_udc_wakeup(struct usb_gadget *_gadget)
1698
{
1699
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1700 1701 1702
	unsigned long flags;
	int ret = 0;

1703
	spin_lock_irqsave(&ci->lock, flags);
1704 1705 1706 1707
	if (ci->gadget.speed == USB_SPEED_UNKNOWN) {
		spin_unlock_irqrestore(&ci->lock, flags);
		return 0;
	}
1708
	if (!ci->remote_wakeup) {
1709 1710 1711
		ret = -EOPNOTSUPP;
		goto out;
	}
1712
	if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1713 1714 1715
		ret = -EINVAL;
		goto out;
	}
1716
	hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1717
out:
1718
	spin_unlock_irqrestore(&ci->lock, flags);
1719 1720 1721
	return ret;
}

1722
static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1723
{
1724
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1725

1726 1727
	if (ci->usb_phy)
		return usb_phy_set_power(ci->usb_phy, ma);
1728 1729 1730
	return -ENOTSUPP;
}

1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
{
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
	struct ci_hw_ep *hwep = ci->ep0in;
	unsigned long flags;

	spin_lock_irqsave(hwep->lock, flags);
	_gadget->is_selfpowered = (is_on != 0);
	spin_unlock_irqrestore(hwep->lock, flags);

	return 0;
}

1744
/* Change Data+ pullup status
1745
 * this func is used by usb_gadget_connect/disconnect
1746
 */
1747
static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1748
{
1749
	struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1750

1751 1752 1753 1754 1755
	/*
	 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
	 * and don't touch Data+ in host mode for dual role config.
	 */
	if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
1756 1757
		return 0;

1758
	pm_runtime_get_sync(ci->dev);
1759 1760 1761 1762
	if (is_on)
		hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
	else
		hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1763
	pm_runtime_put_sync(ci->dev);
1764 1765 1766 1767

	return 0;
}

1768
static int ci_udc_start(struct usb_gadget *gadget,
1769
			 struct usb_gadget_driver *driver);
1770
static int ci_udc_stop(struct usb_gadget *gadget);
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

/* Match ISOC IN from the highest endpoint */
static struct usb_ep *ci_udc_match_ep(struct usb_gadget *gadget,
			      struct usb_endpoint_descriptor *desc,
			      struct usb_ss_ep_comp_descriptor *comp_desc)
{
	struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
	struct usb_ep *ep;

	if (usb_endpoint_xfer_isoc(desc) && usb_endpoint_dir_in(desc)) {
		list_for_each_entry_reverse(ep, &ci->gadget.ep_list, ep_list) {
			if (ep->caps.dir_in && !ep->claimed)
				return ep;
		}
	}

	return NULL;
}

1790
/*
D
David Lopo 已提交
1791 1792 1793 1794
 * 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
 */
1795
static const struct usb_gadget_ops usb_gadget_ops = {
1796 1797
	.vbus_session	= ci_udc_vbus_session,
	.wakeup		= ci_udc_wakeup,
1798
	.set_selfpowered	= ci_udc_selfpowered,
1799 1800 1801 1802
	.pullup		= ci_udc_pullup,
	.vbus_draw	= ci_udc_vbus_draw,
	.udc_start	= ci_udc_start,
	.udc_stop	= ci_udc_stop,
1803
	.match_ep 	= ci_udc_match_ep,
1804
};
D
David Lopo 已提交
1805

1806
static int init_eps(struct ci_hdrc *ci)
D
David Lopo 已提交
1807
{
1808
	int retval = 0, i, j;
D
David Lopo 已提交
1809

1810
	for (i = 0; i < ci->hw_ep_max/2; i++)
1811
		for (j = RX; j <= TX; j++) {
1812
			int k = i + j * ci->hw_ep_max/2;
1813
			struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
D
David Lopo 已提交
1814

1815
			scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1816
					(j == TX)  ? "in" : "out");
D
David Lopo 已提交
1817

1818 1819 1820
			hwep->ci          = ci;
			hwep->lock         = &ci->lock;
			hwep->td_pool      = ci->td_pool;
D
David Lopo 已提交
1821

1822 1823
			hwep->ep.name      = hwep->name;
			hwep->ep.ops       = &usb_ep_ops;
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837

			if (i == 0) {
				hwep->ep.caps.type_control = true;
			} else {
				hwep->ep.caps.type_iso = true;
				hwep->ep.caps.type_bulk = true;
				hwep->ep.caps.type_int = true;
			}

			if (j == TX)
				hwep->ep.caps.dir_in = true;
			else
				hwep->ep.caps.dir_out = true;

1838 1839 1840 1841 1842
			/*
			 * for ep0: maxP defined in desc, for other
			 * eps, maxP is set by epautoconfig() called
			 * by gadget layer
			 */
1843
			usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
D
David Lopo 已提交
1844

1845
			INIT_LIST_HEAD(&hwep->qh.queue);
1846 1847
			hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL,
						       &hwep->qh.dma);
1848
			if (hwep->qh.ptr == NULL)
D
David Lopo 已提交
1849
				retval = -ENOMEM;
1850

1851 1852 1853 1854 1855 1856
			/*
			 * set up shorthands for ep0 out and in endpoints,
			 * don't add to gadget's ep_list
			 */
			if (i == 0) {
				if (j == RX)
1857
					ci->ep0out = hwep;
1858
				else
1859
					ci->ep0in = hwep;
1860

1861
				usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
1862
				continue;
1863
			}
1864

1865
			list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1866
		}
1867 1868 1869 1870

	return retval;
}

1871
static void destroy_eps(struct ci_hdrc *ci)
1872 1873 1874 1875
{
	int i;

	for (i = 0; i < ci->hw_ep_max; i++) {
1876
		struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1877

1878 1879
		if (hwep->pending_td)
			free_pending_td(hwep);
1880
		dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1881 1882 1883
	}
}

1884
/**
1885
 * ci_udc_start: register a gadget driver
1886
 * @gadget: our gadget
1887 1888 1889 1890
 * @driver: the driver being registered
 *
 * Interrupts are enabled here.
 */
1891
static int ci_udc_start(struct usb_gadget *gadget,
1892
			 struct usb_gadget_driver *driver)
1893
{
1894
	struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1895
	int retval;
1896

1897
	if (driver->disconnect == NULL)
1898 1899
		return -EINVAL;

1900 1901
	ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
	retval = usb_ep_enable(&ci->ep0out->ep);
1902 1903
	if (retval)
		return retval;
1904

1905 1906
	ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
	retval = usb_ep_enable(&ci->ep0in->ep);
1907 1908
	if (retval)
		return retval;
1909 1910

	ci->driver = driver;
1911 1912 1913 1914 1915 1916 1917

	/* Start otg fsm for B-device */
	if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
		ci_hdrc_otg_fsm_start(ci);
		return retval;
	}

1918 1919 1920
	if (ci->vbus_active)
		ci_hdrc_gadget_connect(gadget, 1);
	else
1921
		usb_udc_vbus_handler(&ci->gadget, false);
D
David Lopo 已提交
1922 1923 1924 1925

	return retval;
}

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
{
	if (!ci_otg_is_fsm_mode(ci))
		return;

	mutex_lock(&ci->fsm.lock);
	if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
		ci->fsm.a_bidl_adis_tmout = 1;
		ci_hdrc_otg_fsm_start(ci);
	} else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
		ci->fsm.protocol = PROTO_UNDEF;
		ci->fsm.otg->state = OTG_STATE_UNDEFINED;
	}
	mutex_unlock(&ci->fsm.lock);
}

1942
/*
1943
 * ci_udc_stop: unregister a gadget driver
D
David Lopo 已提交
1944
 */
1945
static int ci_udc_stop(struct usb_gadget *gadget)
D
David Lopo 已提交
1946
{
1947
	struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1948
	unsigned long flags;
D
David Lopo 已提交
1949

1950
	spin_lock_irqsave(&ci->lock, flags);
1951
	ci->driver = NULL;
D
David Lopo 已提交
1952

1953
	if (ci->vbus_active) {
1954
		hw_device_state(ci, 0);
1955
		spin_unlock_irqrestore(&ci->lock, flags);
1956 1957
		if (ci->platdata->notify_event)
			ci->platdata->notify_event(ci,
1958
			CI_HDRC_CONTROLLER_STOPPED_EVENT);
1959 1960
		_gadget_stop_activity(&ci->gadget);
		spin_lock_irqsave(&ci->lock, flags);
1961
		pm_runtime_put(ci->dev);
1962
	}
D
David Lopo 已提交
1963

1964
	spin_unlock_irqrestore(&ci->lock, flags);
D
David Lopo 已提交
1965

1966
	ci_udc_stop_for_otg_fsm(ci);
D
David Lopo 已提交
1967 1968 1969 1970 1971 1972
	return 0;
}

/******************************************************************************
 * BUS block
 *****************************************************************************/
1973
/*
1974
 * udc_irq: ci interrupt handler
D
David Lopo 已提交
1975 1976 1977 1978
 *
 * This function returns IRQ_HANDLED if the IRQ has been handled
 * It locks access to registers
 */
1979
static irqreturn_t udc_irq(struct ci_hdrc *ci)
D
David Lopo 已提交
1980 1981 1982 1983
{
	irqreturn_t retval;
	u32 intr;

1984
	if (ci == NULL)
D
David Lopo 已提交
1985 1986
		return IRQ_HANDLED;

1987
	spin_lock(&ci->lock);
1988

1989
	if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1990
		if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1991
				USBMODE_CM_DC) {
1992
			spin_unlock(&ci->lock);
1993 1994 1995
			return IRQ_NONE;
		}
	}
1996
	intr = hw_test_and_clear_intr_active(ci);
D
David Lopo 已提交
1997

1998
	if (intr) {
D
David Lopo 已提交
1999
		/* order defines priority - do NOT change it */
2000
		if (USBi_URI & intr)
2001
			isr_reset_handler(ci);
2002

D
David Lopo 已提交
2003
		if (USBi_PCI & intr) {
2004
			ci->gadget.speed = hw_port_is_high_speed(ci) ?
D
David Lopo 已提交
2005
				USB_SPEED_HIGH : USB_SPEED_FULL;
2006 2007 2008 2009 2010 2011
			if (ci->suspended) {
				if (ci->driver->resume) {
					spin_unlock(&ci->lock);
					ci->driver->resume(&ci->gadget);
					spin_lock(&ci->lock);
				}
2012
				ci->suspended = 0;
2013 2014
				usb_gadget_set_state(&ci->gadget,
						ci->resume_state);
2015
			}
D
David Lopo 已提交
2016
		}
2017 2018

		if (USBi_UI  & intr)
2019
			isr_tr_complete_handler(ci);
2020

2021 2022 2023
		if ((USBi_SLI & intr) && !(ci->suspended)) {
			ci->suspended = 1;
			ci->resume_state = ci->gadget.state;
2024 2025 2026 2027 2028
			if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
			    ci->driver->suspend) {
				spin_unlock(&ci->lock);
				ci->driver->suspend(&ci->gadget);
				spin_lock(&ci->lock);
2029
			}
2030 2031
			usb_gadget_set_state(&ci->gadget,
					USB_STATE_SUSPENDED);
2032
		}
D
David Lopo 已提交
2033 2034 2035 2036
		retval = IRQ_HANDLED;
	} else {
		retval = IRQ_NONE;
	}
2037
	spin_unlock(&ci->lock);
D
David Lopo 已提交
2038 2039 2040 2041 2042

	return retval;
}

/**
2043
 * udc_start: initialize gadget role
2044
 * @ci: chipidea controller
D
David Lopo 已提交
2045
 */
2046
static int udc_start(struct ci_hdrc *ci)
D
David Lopo 已提交
2047
{
2048
	struct device *dev = ci->dev;
L
Li Jun 已提交
2049
	struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
D
David Lopo 已提交
2050 2051
	int retval = 0;

2052 2053 2054 2055
	ci->gadget.ops          = &usb_gadget_ops;
	ci->gadget.speed        = USB_SPEED_UNKNOWN;
	ci->gadget.max_speed    = USB_SPEED_HIGH;
	ci->gadget.name         = ci->platdata->name;
L
Li Jun 已提交
2056
	ci->gadget.otg_caps	= otg_caps;
2057
	ci->gadget.sg_supported = 1;
L
Li Jun 已提交
2058

2059 2060 2061
	if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA)
		ci->gadget.quirk_avoids_skb_reserve = 1;

2062 2063
	if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
						otg_caps->adp_support))
L
Li Jun 已提交
2064
		ci->gadget.is_otg = 1;
D
David Lopo 已提交
2065

2066
	INIT_LIST_HEAD(&ci->gadget.ep_list);
D
David Lopo 已提交
2067

2068
	/* alloc resources */
2069
	ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent,
2070 2071
				       sizeof(struct ci_hw_qh),
				       64, CI_HDRC_PAGE_SIZE);
2072
	if (ci->qh_pool == NULL)
2073
		return -ENOMEM;
2074

2075
	ci->td_pool = dma_pool_create("ci_hw_td", dev->parent,
2076 2077
				       sizeof(struct ci_hw_td),
				       64, CI_HDRC_PAGE_SIZE);
2078
	if (ci->td_pool == NULL) {
2079 2080 2081 2082
		retval = -ENOMEM;
		goto free_qh_pool;
	}

2083
	retval = init_eps(ci);
2084 2085 2086
	if (retval)
		goto free_pools;

2087
	ci->gadget.ep0 = &ci->ep0in->ep;
2088

2089
	retval = usb_add_gadget_udc(dev, &ci->gadget);
2090
	if (retval)
2091
		goto destroy_eps;
2092

D
David Lopo 已提交
2093 2094
	return retval;

2095 2096
destroy_eps:
	destroy_eps(ci);
2097
free_pools:
2098
	dma_pool_destroy(ci->td_pool);
2099
free_qh_pool:
2100
	dma_pool_destroy(ci->qh_pool);
D
David Lopo 已提交
2101 2102 2103
	return retval;
}

2104
/*
2105
 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
D
David Lopo 已提交
2106 2107 2108
 *
 * No interrupts active, the IRQ has been released
 */
2109
void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
D
David Lopo 已提交
2110
{
2111
	if (!ci->roles[CI_ROLE_GADGET])
D
David Lopo 已提交
2112
		return;
2113

2114
	usb_del_gadget_udc(&ci->gadget);
D
David Lopo 已提交
2115

2116
	destroy_eps(ci);
2117

2118 2119
	dma_pool_destroy(ci->td_pool);
	dma_pool_destroy(ci->qh_pool);
2120 2121 2122 2123
}

static int udc_id_switch_for_device(struct ci_hdrc *ci)
{
2124 2125 2126 2127
	if (ci->platdata->pins_device)
		pinctrl_select_state(ci->platdata->pctl,
				     ci->platdata->pins_device);

2128 2129 2130 2131
	if (ci->is_otg)
		/* Clear and enable BSV irq */
		hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
					OTGSC_BSVIS | OTGSC_BSVIE);
2132 2133 2134 2135 2136 2137

	return 0;
}

static void udc_id_switch_for_host(struct ci_hdrc *ci)
{
2138 2139 2140 2141 2142 2143
	/*
	 * host doesn't care B_SESSION_VALID event
	 * so clear and disbale BSV irq
	 */
	if (ci->is_otg)
		hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
2144 2145

	ci->vbus_active = 0;
2146 2147 2148 2149

	if (ci->platdata->pins_device && ci->platdata->pins_default)
		pinctrl_select_state(ci->platdata->pctl,
				     ci->platdata->pins_default);
2150 2151 2152 2153
}

/**
 * ci_hdrc_gadget_init - initialize device related bits
2154
 * @ci: the controller
2155
 *
2156
 * This function initializes the gadget, if the device is "device capable".
2157
 */
2158
int ci_hdrc_gadget_init(struct ci_hdrc *ci)
2159 2160
{
	struct ci_role_driver *rdrv;
2161
	int ret;
2162 2163 2164 2165

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

2166
	rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL);
2167 2168 2169
	if (!rdrv)
		return -ENOMEM;

2170 2171
	rdrv->start	= udc_id_switch_for_device;
	rdrv->stop	= udc_id_switch_for_host;
2172 2173
	rdrv->irq	= udc_irq;
	rdrv->name	= "gadget";
D
David Lopo 已提交
2174

2175 2176 2177 2178 2179
	ret = udc_start(ci);
	if (!ret)
		ci->roles[CI_ROLE_GADGET] = rdrv;

	return ret;
D
David Lopo 已提交
2180
}