langwell_udc.c 89.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Intel Langwell USB Device Controller driver
 * Copyright (C) 2008-2009, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


/* #undef	DEBUG */
22
/* #undef	VERBOSE_DEBUG */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#if defined(CONFIG_USB_LANGWELL_OTG)
#define	OTG_TRANSCEIVER
#endif


#include <linux/module.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <linux/usb/otg.h>
#include <linux/pm.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <asm/system.h>
#include <asm/unaligned.h>

#include "langwell_udc.h"


#define	DRIVER_DESC		"Intel Langwell USB Device Controller driver"
#define	DRIVER_VERSION		"16 May 2009"

static const char driver_name[] = "langwell_udc";
static const char driver_desc[] = DRIVER_DESC;


/* controller device global variable */
static struct langwell_udc	*the_controller;

/* for endpoint 0 operations */
static const struct usb_endpoint_descriptor
langwell_ep0_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bEndpointAddress =	0,
	.bmAttributes =		USB_ENDPOINT_XFER_CONTROL,
	.wMaxPacketSize =	EP0_MAX_PKT_SIZE,
};


/*-------------------------------------------------------------------------*/
/* debugging */

80
#ifdef	VERBOSE_DEBUG
81 82 83 84 85
static inline void print_all_registers(struct langwell_udc *dev)
{
	int	i;

	/* Capability Registers */
86 87 88 89
	dev_dbg(&dev->pdev->dev,
		"Capability Registers (offset: 0x%04x, length: 0x%08x)\n",
		CAP_REG_OFFSET, (u32)sizeof(struct langwell_cap_regs));
	dev_dbg(&dev->pdev->dev, "caplength=0x%02x\n",
90
			readb(&dev->cap_regs->caplength));
91
	dev_dbg(&dev->pdev->dev, "hciversion=0x%04x\n",
92
			readw(&dev->cap_regs->hciversion));
93
	dev_dbg(&dev->pdev->dev, "hcsparams=0x%08x\n",
94
			readl(&dev->cap_regs->hcsparams));
95
	dev_dbg(&dev->pdev->dev, "hccparams=0x%08x\n",
96
			readl(&dev->cap_regs->hccparams));
97
	dev_dbg(&dev->pdev->dev, "dciversion=0x%04x\n",
98
			readw(&dev->cap_regs->dciversion));
99
	dev_dbg(&dev->pdev->dev, "dccparams=0x%08x\n",
100 101 102
			readl(&dev->cap_regs->dccparams));

	/* Operational Registers */
103 104 105 106
	dev_dbg(&dev->pdev->dev,
		"Operational Registers (offset: 0x%04x, length: 0x%08x)\n",
		OP_REG_OFFSET, (u32)sizeof(struct langwell_op_regs));
	dev_dbg(&dev->pdev->dev, "extsts=0x%08x\n",
107
			readl(&dev->op_regs->extsts));
108
	dev_dbg(&dev->pdev->dev, "extintr=0x%08x\n",
109
			readl(&dev->op_regs->extintr));
110
	dev_dbg(&dev->pdev->dev, "usbcmd=0x%08x\n",
111
			readl(&dev->op_regs->usbcmd));
112
	dev_dbg(&dev->pdev->dev, "usbsts=0x%08x\n",
113
			readl(&dev->op_regs->usbsts));
114
	dev_dbg(&dev->pdev->dev, "usbintr=0x%08x\n",
115
			readl(&dev->op_regs->usbintr));
116
	dev_dbg(&dev->pdev->dev, "frindex=0x%08x\n",
117
			readl(&dev->op_regs->frindex));
118
	dev_dbg(&dev->pdev->dev, "ctrldssegment=0x%08x\n",
119
			readl(&dev->op_regs->ctrldssegment));
120
	dev_dbg(&dev->pdev->dev, "deviceaddr=0x%08x\n",
121
			readl(&dev->op_regs->deviceaddr));
122
	dev_dbg(&dev->pdev->dev, "endpointlistaddr=0x%08x\n",
123
			readl(&dev->op_regs->endpointlistaddr));
124
	dev_dbg(&dev->pdev->dev, "ttctrl=0x%08x\n",
125
			readl(&dev->op_regs->ttctrl));
126
	dev_dbg(&dev->pdev->dev, "burstsize=0x%08x\n",
127
			readl(&dev->op_regs->burstsize));
128
	dev_dbg(&dev->pdev->dev, "txfilltuning=0x%08x\n",
129
			readl(&dev->op_regs->txfilltuning));
130
	dev_dbg(&dev->pdev->dev, "txttfilltuning=0x%08x\n",
131
			readl(&dev->op_regs->txttfilltuning));
132
	dev_dbg(&dev->pdev->dev, "ic_usb=0x%08x\n",
133
			readl(&dev->op_regs->ic_usb));
134
	dev_dbg(&dev->pdev->dev, "ulpi_viewport=0x%08x\n",
135
			readl(&dev->op_regs->ulpi_viewport));
136
	dev_dbg(&dev->pdev->dev, "configflag=0x%08x\n",
137
			readl(&dev->op_regs->configflag));
138
	dev_dbg(&dev->pdev->dev, "portsc1=0x%08x\n",
139
			readl(&dev->op_regs->portsc1));
140
	dev_dbg(&dev->pdev->dev, "devlc=0x%08x\n",
141
			readl(&dev->op_regs->devlc));
142
	dev_dbg(&dev->pdev->dev, "otgsc=0x%08x\n",
143
			readl(&dev->op_regs->otgsc));
144
	dev_dbg(&dev->pdev->dev, "usbmode=0x%08x\n",
145
			readl(&dev->op_regs->usbmode));
146
	dev_dbg(&dev->pdev->dev, "endptnak=0x%08x\n",
147
			readl(&dev->op_regs->endptnak));
148
	dev_dbg(&dev->pdev->dev, "endptnaken=0x%08x\n",
149
			readl(&dev->op_regs->endptnaken));
150
	dev_dbg(&dev->pdev->dev, "endptsetupstat=0x%08x\n",
151
			readl(&dev->op_regs->endptsetupstat));
152
	dev_dbg(&dev->pdev->dev, "endptprime=0x%08x\n",
153
			readl(&dev->op_regs->endptprime));
154
	dev_dbg(&dev->pdev->dev, "endptflush=0x%08x\n",
155
			readl(&dev->op_regs->endptflush));
156
	dev_dbg(&dev->pdev->dev, "endptstat=0x%08x\n",
157
			readl(&dev->op_regs->endptstat));
158
	dev_dbg(&dev->pdev->dev, "endptcomplete=0x%08x\n",
159 160 161
			readl(&dev->op_regs->endptcomplete));

	for (i = 0; i < dev->ep_max / 2; i++) {
162
		dev_dbg(&dev->pdev->dev, "endptctrl[%d]=0x%08x\n",
163 164 165
				i, readl(&dev->op_regs->endptctrl[i]));
	}
}
166 167 168 169 170
#else

#define	print_all_registers(dev)	do { } while (0)

#endif /* VERBOSE_DEBUG */
171 172 173 174


/*-------------------------------------------------------------------------*/

175 176
#define	is_in(ep)	(((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir ==	\
			USB_DIR_IN) : (usb_endpoint_dir_in((ep)->desc)))
177

178
#define	DIR_STRING(ep)	(is_in(ep) ? "in" : "out")
179 180


181
static char *type_string(const struct usb_endpoint_descriptor *desc)
182
{
183
	switch (usb_endpoint_type(desc)) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	case USB_ENDPOINT_XFER_BULK:
		return "bulk";
	case USB_ENDPOINT_XFER_ISOC:
		return "iso";
	case USB_ENDPOINT_XFER_INT:
		return "int";
	};

	return "control";
}


/* configure endpoint control registers */
static void ep_reset(struct langwell_ep *ep, unsigned char ep_num,
		unsigned char is_in, unsigned char ep_type)
{
	struct langwell_udc	*dev;
	u32			endptctrl;

	dev = ep->dev;
204
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
	if (is_in) {	/* TX */
		if (ep_num)
			endptctrl |= EPCTRL_TXR;
		endptctrl |= EPCTRL_TXE;
		endptctrl |= ep_type << EPCTRL_TXT_SHIFT;
	} else {	/* RX */
		if (ep_num)
			endptctrl |= EPCTRL_RXR;
		endptctrl |= EPCTRL_RXE;
		endptctrl |= ep_type << EPCTRL_RXT_SHIFT;
	}

	writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);

221
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
222 223 224 225 226 227 228 229 230
}


/* reset ep0 dQH and endptctrl */
static void ep0_reset(struct langwell_udc *dev)
{
	struct langwell_ep	*ep;
	int			i;

231
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
232 233 234 235 236 237 238 239 240 241 242 243 244

	/* ep0 in and out */
	for (i = 0; i < 2; i++) {
		ep = &dev->ep[i];
		ep->dev = dev;

		/* ep0 dQH */
		ep->dqh = &dev->ep_dqh[i];

		/* configure ep0 endpoint capabilities in dQH */
		ep->dqh->dqh_ios = 1;
		ep->dqh->dqh_mpl = EP0_MAX_PKT_SIZE;

245
		/* enable ep0-in HW zero length termination select */
246 247 248 249
		if (is_in(ep))
			ep->dqh->dqh_zlt = 0;
		ep->dqh->dqh_mult = 0;

250 251
		ep->dqh->dtd_next = DTD_TERM;

252 253 254 255
		/* configure ep0 control registers */
		ep_reset(&dev->ep[0], 0, i, USB_ENDPOINT_XFER_CONTROL);
	}

256
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}


/*-------------------------------------------------------------------------*/

/* endpoints operations */

/* configure endpoint, making it usable */
static int langwell_ep_enable(struct usb_ep *_ep,
		const struct usb_endpoint_descriptor *desc)
{
	struct langwell_udc	*dev;
	struct langwell_ep	*ep;
	u16			max = 0;
	unsigned long		flags;
272
	int			i, retval = 0;
273 274 275 276
	unsigned char		zlt, ios = 0, mult = 0;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;
277
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

	if (!_ep || !desc || ep->desc
			|| desc->bDescriptorType != USB_DT_ENDPOINT)
		return -EINVAL;

	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

	max = le16_to_cpu(desc->wMaxPacketSize);

	/*
	 * disable HW zero length termination select
	 * driver handles zero length packet through req->req.zero
	 */
	zlt = 1;

	/*
	 * sanity check type, direction, address, and then
	 * initialize the endpoint capabilities fields in dQH
	 */
298
	switch (usb_endpoint_type(desc)) {
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	case USB_ENDPOINT_XFER_CONTROL:
		ios = 1;
		break;
	case USB_ENDPOINT_XFER_BULK:
		if ((dev->gadget.speed == USB_SPEED_HIGH
					&& max != 512)
				|| (dev->gadget.speed == USB_SPEED_FULL
					&& max > 64)) {
			goto done;
		}
		break;
	case USB_ENDPOINT_XFER_INT:
		if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
			goto done;

		switch (dev->gadget.speed) {
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
		case USB_SPEED_FULL:
			if (max <= 64)
				break;
		default:
			if (max <= 8)
				break;
			goto done;
		}
		break;
	case USB_ENDPOINT_XFER_ISOC:
		if (strstr(ep->ep.name, "-bulk")
				|| strstr(ep->ep.name, "-int"))
			goto done;

		switch (dev->gadget.speed) {
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
		case USB_SPEED_FULL:
			if (max <= 1023)
				break;
		default:
			goto done;
		}
		/*
		 * FIXME:
		 * calculate transactions needed for high bandwidth iso
		 */
		mult = (unsigned char)(1 + ((max >> 11) & 0x03));
		max = max & 0x8ff;	/* bit 0~10 */
		/* 3 transactions at most */
		if (mult > 3)
			goto done;
		break;
	default:
		goto done;
	}

	spin_lock_irqsave(&dev->lock, flags);

	ep->ep.maxpacket = max;
	ep->desc = desc;
	ep->stopped = 0;
361
	ep->ep_num = usb_endpoint_num(desc);
362 363

	/* ep_type */
364
	ep->ep_type = usb_endpoint_type(desc);
365 366 367 368

	/* configure endpoint control registers */
	ep_reset(ep, ep->ep_num, is_in(ep), ep->ep_type);

369 370 371 372 373 374 375 376 377
	/* configure endpoint capabilities in dQH */
	i = ep->ep_num * 2 + is_in(ep);
	ep->dqh = &dev->ep_dqh[i];
	ep->dqh->dqh_ios = ios;
	ep->dqh->dqh_mpl = cpu_to_le16(max);
	ep->dqh->dqh_zlt = zlt;
	ep->dqh->dqh_mult = mult;
	ep->dqh->dtd_next = DTD_TERM;

378
	dev_dbg(&dev->pdev->dev, "enabled %s (ep%d%s-%s), max %04x\n",
379 380
			_ep->name,
			ep->ep_num,
381 382
			DIR_STRING(ep),
			type_string(desc),
383 384 385 386
			max);

	spin_unlock_irqrestore(&dev->lock, flags);
done:
387
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	return retval;
}


/*-------------------------------------------------------------------------*/

/* retire a request */
static void done(struct langwell_ep *ep, struct langwell_request *req,
		int status)
{
	struct langwell_udc	*dev = ep->dev;
	unsigned		stopped = ep->stopped;
	struct langwell_dtd	*curr_dtd, *next_dtd;
	int			i;

403
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

	/* remove the req from ep->queue */
	list_del_init(&req->queue);

	if (req->req.status == -EINPROGRESS)
		req->req.status = status;
	else
		status = req->req.status;

	/* free dTD for the request */
	next_dtd = req->head;
	for (i = 0; i < req->dtd_count; i++) {
		curr_dtd = next_dtd;
		if (i != req->dtd_count - 1)
			next_dtd = curr_dtd->next_dtd_virt;
		dma_pool_free(dev->dtd_pool, curr_dtd, curr_dtd->dtd_dma);
	}

	if (req->mapped) {
423 424
		dma_unmap_single(&dev->pdev->dev,
			req->req.dma, req->req.length,
425 426 427 428 429 430 431 432 433
			is_in(ep) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
		req->req.dma = DMA_ADDR_INVALID;
		req->mapped = 0;
	} else
		dma_sync_single_for_cpu(&dev->pdev->dev, req->req.dma,
				req->req.length,
				is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);

	if (status != -ESHUTDOWN)
434 435 436 437
		dev_dbg(&dev->pdev->dev,
				"complete %s, req %p, stat %d, len %u/%u\n",
				ep->ep.name, &req->req, status,
				req->req.actual, req->req.length);
438 439 440 441 442 443 444 445 446 447 448 449

	/* don't modify queue heads during completion callback */
	ep->stopped = 1;

	spin_unlock(&dev->lock);
	/* complete routine from gadget driver */
	if (req->req.complete)
		req->req.complete(&ep->ep, &req->req);

	spin_lock(&dev->lock);
	ep->stopped = stopped;

450
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
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
}


static void langwell_ep_fifo_flush(struct usb_ep *_ep);

/* delete all endpoint requests, called with spinlock held */
static void nuke(struct langwell_ep *ep, int status)
{
	/* called with spinlock held */
	ep->stopped = 1;

	/* endpoint fifo flush */
	if (&ep->ep && ep->desc)
		langwell_ep_fifo_flush(&ep->ep);

	while (!list_empty(&ep->queue)) {
		struct langwell_request	*req = NULL;
		req = list_entry(ep->queue.next, struct langwell_request,
				queue);
		done(ep, req, status);
	}
}


/*-------------------------------------------------------------------------*/

/* endpoint is no longer usable */
static int langwell_ep_disable(struct usb_ep *_ep)
{
	struct langwell_ep	*ep;
	unsigned long		flags;
	struct langwell_udc	*dev;
	int			ep_num;
	u32			endptctrl;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;
488
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

	if (!_ep || !ep->desc)
		return -EINVAL;

	spin_lock_irqsave(&dev->lock, flags);

	/* disable endpoint control register */
	ep_num = ep->ep_num;
	endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
	if (is_in(ep))
		endptctrl &= ~EPCTRL_TXE;
	else
		endptctrl &= ~EPCTRL_RXE;
	writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);

	/* nuke all pending requests (does flush) */
	nuke(ep, -ESHUTDOWN);

	ep->desc = NULL;
	ep->stopped = 1;

	spin_unlock_irqrestore(&dev->lock, flags);

512 513
	dev_dbg(&dev->pdev->dev, "disabled %s\n", _ep->name);
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

	return 0;
}


/* allocate a request object to use with this endpoint */
static struct usb_request *langwell_alloc_request(struct usb_ep *_ep,
		gfp_t gfp_flags)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	struct langwell_request	*req = NULL;

	if (!_ep)
		return NULL;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;
532
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
533 534 535 536 537 538 539 540

	req = kzalloc(sizeof(*req), gfp_flags);
	if (!req)
		return NULL;

	req->req.dma = DMA_ADDR_INVALID;
	INIT_LIST_HEAD(&req->queue);

541 542
	dev_vdbg(&dev->pdev->dev, "alloc request for %s\n", _ep->name);
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
543 544 545 546 547 548 549 550 551 552 553 554 555 556
	return &req->req;
}


/* free a request object */
static void langwell_free_request(struct usb_ep *_ep,
		struct usb_request *_req)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	struct langwell_request	*req = NULL;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;
557
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
558 559 560 561 562 563 564 565 566 567

	if (!_ep || !_req)
		return;

	req = container_of(_req, struct langwell_request, req);
	WARN_ON(!list_empty(&req->queue));

	if (_req)
		kfree(req);

568 569
	dev_vdbg(&dev->pdev->dev, "free request for %s\n", _ep->name);
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
}


/*-------------------------------------------------------------------------*/

/* queue dTD and PRIME endpoint */
static int queue_dtd(struct langwell_ep *ep, struct langwell_request *req)
{
	u32			bit_mask, usbcmd, endptstat, dtd_dma;
	u8			dtd_status;
	int			i;
	struct langwell_dqh	*dqh;
	struct langwell_udc	*dev;

	dev = ep->dev;
585
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
586 587 588 589 590

	i = ep->ep_num * 2 + is_in(ep);
	dqh = &dev->ep_dqh[i];

	if (ep->ep_num)
591
		dev_vdbg(&dev->pdev->dev, "%s\n", ep->name);
592 593
	else
		/* ep0 */
594
		dev_vdbg(&dev->pdev->dev, "%s-%s\n", ep->name, DIR_STRING(ep));
595

596 597
	dev_vdbg(&dev->pdev->dev, "ep_dqh[%d] addr: 0x%08x\n",
			i, (u32)&(dev->ep_dqh[i]));
598 599 600 601

	bit_mask = is_in(ep) ?
		(1 << (ep->ep_num + 16)) : (1 << (ep->ep_num));

602
	dev_vdbg(&dev->pdev->dev, "bit_mask = 0x%08x\n", bit_mask);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

	/* check if the pipe is empty */
	if (!(list_empty(&ep->queue))) {
		/* add dTD to the end of linked list */
		struct langwell_request	*lastreq;
		lastreq = list_entry(ep->queue.prev,
				struct langwell_request, queue);

		lastreq->tail->dtd_next =
			cpu_to_le32(req->head->dtd_dma & DTD_NEXT_MASK);

		/* read prime bit, if 1 goto out */
		if (readl(&dev->op_regs->endptprime) & bit_mask)
			goto out;

		do {
			/* set ATDTW bit in USBCMD */
			usbcmd = readl(&dev->op_regs->usbcmd);
			writel(usbcmd | CMD_ATDTW, &dev->op_regs->usbcmd);

			/* read correct status bit */
			endptstat = readl(&dev->op_regs->endptstat) & bit_mask;

		} while (!(readl(&dev->op_regs->usbcmd) & CMD_ATDTW));

		/* write ATDTW bit to 0 */
		usbcmd = readl(&dev->op_regs->usbcmd);
		writel(usbcmd & ~CMD_ATDTW, &dev->op_regs->usbcmd);

		if (endptstat)
			goto out;
	}

	/* write dQH next pointer and terminate bit to 0 */
	dtd_dma = req->head->dtd_dma & DTD_NEXT_MASK;
	dqh->dtd_next = cpu_to_le32(dtd_dma);

	/* clear active and halt bit */
	dtd_status = (u8) ~(DTD_STS_ACTIVE | DTD_STS_HALTED);
	dqh->dtd_status &= dtd_status;
643 644 645 646
	dev_vdbg(&dev->pdev->dev, "dqh->dtd_status = 0x%x\n", dqh->dtd_status);

	/* ensure that updates to the dQH will occure before priming */
	wmb();
647 648 649

	/* write 1 to endptprime register to PRIME endpoint */
	bit_mask = is_in(ep) ? (1 << (ep->ep_num + 16)) : (1 << ep->ep_num);
650
	dev_vdbg(&dev->pdev->dev, "endprime bit_mask = 0x%08x\n", bit_mask);
651 652
	writel(bit_mask, &dev->op_regs->endptprime);
out:
653
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
654 655 656 657 658 659 660 661 662 663 664 665 666 667
	return 0;
}


/* fill in the dTD structure to build a transfer descriptor */
static struct langwell_dtd *build_dtd(struct langwell_request *req,
		unsigned *length, dma_addr_t *dma, int *is_last)
{
	u32			 buf_ptr;
	struct langwell_dtd	*dtd;
	struct langwell_udc	*dev;
	int			i;

	dev = req->ep->dev;
668
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

	/* the maximum transfer length, up to 16k bytes */
	*length = min(req->req.length - req->req.actual,
			(unsigned)DTD_MAX_TRANSFER_LENGTH);

	/* create dTD dma_pool resource */
	dtd = dma_pool_alloc(dev->dtd_pool, GFP_KERNEL, dma);
	if (dtd == NULL)
		return dtd;
	dtd->dtd_dma = *dma;

	/* initialize buffer page pointers */
	buf_ptr = (u32)(req->req.dma + req->req.actual);
	for (i = 0; i < 5; i++)
		dtd->dtd_buf[i] = cpu_to_le32(buf_ptr + i * PAGE_SIZE);

	req->req.actual += *length;

	/* fill in total bytes with transfer size */
	dtd->dtd_total = cpu_to_le16(*length);
689
	dev_vdbg(&dev->pdev->dev, "dtd->dtd_total = %d\n", dtd->dtd_total);
690 691 692 693 694 695 696 697 698 699 700 701 702

	/* set is_last flag if req->req.zero is set or not */
	if (req->req.zero) {
		if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
			*is_last = 1;
		else
			*is_last = 0;
	} else if (req->req.length == req->req.actual) {
		*is_last = 1;
	} else
		*is_last = 0;

	if (*is_last == 0)
703
		dev_vdbg(&dev->pdev->dev, "multi-dtd request!\n");
704 705 706 707 708 709 710 711 712 713

	/* set interrupt on complete bit for the last dTD */
	if (*is_last && !req->req.no_interrupt)
		dtd->dtd_ioc = 1;

	/* set multiplier override 0 for non-ISO and non-TX endpoint */
	dtd->dtd_multo = 0;

	/* set the active bit of status field to 1 */
	dtd->dtd_status = DTD_STS_ACTIVE;
714 715
	dev_vdbg(&dev->pdev->dev, "dtd->dtd_status = 0x%02x\n",
			dtd->dtd_status);
716

717 718 719
	dev_vdbg(&dev->pdev->dev, "length = %d, dma addr= 0x%08x\n",
			*length, (int)*dma);
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
720 721 722 723 724 725 726 727 728 729 730 731 732 733
	return dtd;
}


/* generate dTD linked list for a request */
static int req_to_dtd(struct langwell_request *req)
{
	unsigned		count;
	int			is_last, is_first = 1;
	struct langwell_dtd	*dtd, *last_dtd = NULL;
	struct langwell_udc	*dev;
	dma_addr_t		dma;

	dev = req->ep->dev;
734
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
	do {
		dtd = build_dtd(req, &count, &dma, &is_last);
		if (dtd == NULL)
			return -ENOMEM;

		if (is_first) {
			is_first = 0;
			req->head = dtd;
		} else {
			last_dtd->dtd_next = cpu_to_le32(dma);
			last_dtd->next_dtd_virt = dtd;
		}
		last_dtd = dtd;
		req->dtd_count++;
	} while (!is_last);

	/* set terminate bit to 1 for the last dTD */
	dtd->dtd_next = DTD_TERM;

	req->tail = dtd;

756
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	return 0;
}

/*-------------------------------------------------------------------------*/

/* queue (submits) an I/O requests to an endpoint */
static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
		gfp_t gfp_flags)
{
	struct langwell_request	*req;
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	unsigned long		flags;
	int			is_iso = 0, zlflag = 0;

	/* always require a cpu-view buffer */
	req = container_of(_req, struct langwell_request, req);
	ep = container_of(_ep, struct langwell_ep, ep);

	if (!_req || !_req->complete || !_req->buf
			|| !list_empty(&req->queue)) {
		return -EINVAL;
	}

	if (unlikely(!_ep || !ep->desc))
		return -EINVAL;

	dev = ep->dev;
	req->ep = ep;
786
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
787

788
	if (usb_endpoint_xfer_isoc(ep->desc)) {
789 790 791 792 793 794 795 796 797 798 799 800
		if (req->req.length > ep->ep.maxpacket)
			return -EMSGSIZE;
		is_iso = 1;
	}

	if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
		return -ESHUTDOWN;

	/* set up dma mapping in case the caller didn't */
	if (_req->dma == DMA_ADDR_INVALID) {
		/* WORKAROUND: WARN_ON(size == 0) */
		if (_req->length == 0) {
801
			dev_vdbg(&dev->pdev->dev, "req->length: 0->1\n");
802 803 804 805 806 807 808 809
			zlflag = 1;
			_req->length++;
		}

		_req->dma = dma_map_single(&dev->pdev->dev,
				_req->buf, _req->length,
				is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
		if (zlflag && (_req->length == 1)) {
810
			dev_vdbg(&dev->pdev->dev, "req->length: 1->0\n");
811 812 813 814 815
			zlflag = 0;
			_req->length = 0;
		}

		req->mapped = 1;
816
		dev_vdbg(&dev->pdev->dev, "req->mapped = 1\n");
817 818 819 820 821
	} else {
		dma_sync_single_for_device(&dev->pdev->dev,
				_req->dma, _req->length,
				is_in(ep) ?  DMA_TO_DEVICE : DMA_FROM_DEVICE);
		req->mapped = 0;
822
		dev_vdbg(&dev->pdev->dev, "req->mapped = 0\n");
823 824
	}

825 826 827 828
	dev_dbg(&dev->pdev->dev,
			"%s queue req %p, len %u, buf %p, dma 0x%08x\n",
			_ep->name,
			_req, _req->length, _req->buf, (int)_req->dma);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

	_req->status = -EINPROGRESS;
	_req->actual = 0;
	req->dtd_count = 0;

	spin_lock_irqsave(&dev->lock, flags);

	/* build and put dTDs to endpoint queue */
	if (!req_to_dtd(req)) {
		queue_dtd(ep, req);
	} else {
		spin_unlock_irqrestore(&dev->lock, flags);
		return -ENOMEM;
	}

	/* update ep0 state */
	if (ep->ep_num == 0)
		dev->ep0_state = DATA_STATE_XMIT;

	if (likely(req != NULL)) {
		list_add_tail(&req->queue, &ep->queue);
850
		dev_vdbg(&dev->pdev->dev, "list_add_tail()\n");
851 852 853 854
	}

	spin_unlock_irqrestore(&dev->lock, flags);

855
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
	return 0;
}


/* dequeue (cancels, unlinks) an I/O request from an endpoint */
static int langwell_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	struct langwell_request	*req;
	unsigned long		flags;
	int			stopped, ep_num, retval = 0;
	u32			endptctrl;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;
872
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

	if (!_ep || !ep->desc || !_req)
		return -EINVAL;

	if (!dev->driver)
		return -ESHUTDOWN;

	spin_lock_irqsave(&dev->lock, flags);
	stopped = ep->stopped;

	/* quiesce dma while we patch the queue */
	ep->stopped = 1;
	ep_num = ep->ep_num;

	/* disable endpoint control register */
	endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
	if (is_in(ep))
		endptctrl &= ~EPCTRL_TXE;
	else
		endptctrl &= ~EPCTRL_RXE;
	writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);

	/* make sure it's still queued on this endpoint */
	list_for_each_entry(req, &ep->queue, queue) {
		if (&req->req == _req)
			break;
	}

	if (&req->req != _req) {
		retval = -EINVAL;
		goto done;
	}

	/* queue head may be partially complete. */
	if (ep->queue.next == &req->queue) {
908
		dev_dbg(&dev->pdev->dev, "unlink (%s) dma\n", _ep->name);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		_req->status = -ECONNRESET;
		langwell_ep_fifo_flush(&ep->ep);

		/* not the last request in endpoint queue */
		if (likely(ep->queue.next == &req->queue)) {
			struct langwell_dqh	*dqh;
			struct langwell_request	*next_req;

			dqh = ep->dqh;
			next_req = list_entry(req->queue.next,
					struct langwell_request, queue);

			/* point the dQH to the first dTD of next request */
			writel((u32) next_req->head, &dqh->dqh_current);
		}
	} else {
		struct langwell_request	*prev_req;

		prev_req = list_entry(req->queue.prev,
				struct langwell_request, queue);
		writel(readl(&req->tail->dtd_next),
				&prev_req->tail->dtd_next);
	}

	done(ep, req, -ECONNRESET);

done:
	/* enable endpoint again */
	endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
	if (is_in(ep))
		endptctrl |= EPCTRL_TXE;
	else
		endptctrl |= EPCTRL_RXE;
	writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);

	ep->stopped = stopped;
	spin_unlock_irqrestore(&dev->lock, flags);

947
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
948 949 950 951 952 953 954 955 956 957 958 959
	return retval;
}


/*-------------------------------------------------------------------------*/

/* endpoint set/clear halt */
static void ep_set_halt(struct langwell_ep *ep, int value)
{
	u32			endptctrl = 0;
	int			ep_num;
	struct langwell_udc	*dev = ep->dev;
960
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

	ep_num = ep->ep_num;
	endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);

	/* value: 1 - set halt, 0 - clear halt */
	if (value) {
		/* set the stall bit */
		if (is_in(ep))
			endptctrl |= EPCTRL_TXS;
		else
			endptctrl |= EPCTRL_RXS;
	} else {
		/* clear the stall bit and reset data toggle */
		if (is_in(ep)) {
			endptctrl &= ~EPCTRL_TXS;
			endptctrl |= EPCTRL_TXR;
		} else {
			endptctrl &= ~EPCTRL_RXS;
			endptctrl |= EPCTRL_RXR;
		}
	}

	writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);

985
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
986 987 988 989 990 991 992 993 994 995 996 997 998 999
}


/* set the endpoint halt feature */
static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	unsigned long		flags;
	int			retval = 0;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;

1000
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1001 1002 1003 1004 1005 1006 1007

	if (!_ep || !ep->desc)
		return -EINVAL;

	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

1008
	if (usb_endpoint_xfer_isoc(ep->desc))
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
		return  -EOPNOTSUPP;

	spin_lock_irqsave(&dev->lock, flags);

	/*
	 * attempt to halt IN ep will fail if any transfer requests
	 * are still queue
	 */
	if (!list_empty(&ep->queue) && is_in(ep) && value) {
		/* IN endpoint FIFO holds bytes */
1019
		dev_dbg(&dev->pdev->dev, "%s FIFO holds bytes\n", _ep->name);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
		retval = -EAGAIN;
		goto done;
	}

	/* endpoint set/clear halt */
	if (ep->ep_num) {
		ep_set_halt(ep, value);
	} else { /* endpoint 0 */
		dev->ep0_state = WAIT_FOR_SETUP;
		dev->ep0_dir = USB_DIR_OUT;
	}
done:
	spin_unlock_irqrestore(&dev->lock, flags);
1033 1034 1035
	dev_dbg(&dev->pdev->dev, "%s %s halt\n",
			_ep->name, value ? "set" : "clear");
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	return retval;
}


/* set the halt feature and ignores clear requests */
static int langwell_ep_set_wedge(struct usb_ep *_ep)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;

1049
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1050 1051 1052 1053

	if (!_ep || !ep->desc)
		return -EINVAL;

1054
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	return usb_ep_set_halt(_ep);
}


/* flush contents of a fifo */
static void langwell_ep_fifo_flush(struct usb_ep *_ep)
{
	struct langwell_ep	*ep;
	struct langwell_udc	*dev;
	u32			flush_bit;
	unsigned long		timeout;

	ep = container_of(_ep, struct langwell_ep, ep);
	dev = ep->dev;

1070
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1071 1072

	if (!_ep || !ep->desc) {
1073 1074
		dev_vdbg(&dev->pdev->dev, "ep or ep->desc is NULL\n");
		dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1075 1076 1077
		return;
	}

1078 1079
	dev_vdbg(&dev->pdev->dev, "%s-%s fifo flush\n",
			_ep->name, DIR_STRING(ep));
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094

	/* flush endpoint buffer */
	if (ep->ep_num == 0)
		flush_bit = (1 << 16) | 1;
	else if (is_in(ep))
		flush_bit = 1 << (ep->ep_num + 16);	/* TX */
	else
		flush_bit = 1 << ep->ep_num;		/* RX */

	/* wait until flush complete */
	timeout = jiffies + FLUSH_TIMEOUT;
	do {
		writel(flush_bit, &dev->op_regs->endptflush);
		while (readl(&dev->op_regs->endptflush)) {
			if (time_after(jiffies, timeout)) {
1095
				dev_err(&dev->pdev->dev, "ep flush timeout\n");
1096 1097 1098 1099 1100 1101
				goto done;
			}
			cpu_relax();
		}
	} while (readl(&dev->op_regs->endptstat) & flush_bit);
done:
1102
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 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
}


/* endpoints operations structure */
static const struct usb_ep_ops langwell_ep_ops = {

	/* configure endpoint, making it usable */
	.enable		= langwell_ep_enable,

	/* endpoint is no longer usable */
	.disable	= langwell_ep_disable,

	/* allocate a request object to use with this endpoint */
	.alloc_request	= langwell_alloc_request,

	/* free a request object */
	.free_request	= langwell_free_request,

	/* queue (submits) an I/O requests to an endpoint */
	.queue		= langwell_ep_queue,

	/* dequeue (cancels, unlinks) an I/O request from an endpoint */
	.dequeue	= langwell_ep_dequeue,

	/* set the endpoint halt feature */
	.set_halt	= langwell_ep_set_halt,

	/* set the halt feature and ignores clear requests */
	.set_wedge	= langwell_ep_set_wedge,

	/* flush contents of a fifo */
	.fifo_flush	= langwell_ep_fifo_flush,
};


/*-------------------------------------------------------------------------*/

/* device controller usb_gadget_ops structure */

/* returns the current frame number */
static int langwell_get_frame(struct usb_gadget *_gadget)
{
	struct langwell_udc	*dev;
	u16			retval;

	if (!_gadget)
		return -ENODEV;

	dev = container_of(_gadget, struct langwell_udc, gadget);
1152
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1153 1154 1155

	retval = readl(&dev->op_regs->frindex) & FRINDEX_MASK;

1156
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1157 1158 1159 1160
	return retval;
}


1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
/* enter or exit PHY low power state */
static void langwell_phy_low_power(struct langwell_udc *dev, bool flag)
{
	u32		devlc;
	u8		devlc_byte2;
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);

	devlc = readl(&dev->op_regs->devlc);
	dev_vdbg(&dev->pdev->dev, "devlc = 0x%08x\n", devlc);

	if (flag)
		devlc |= LPM_PHCD;
	else
		devlc &= ~LPM_PHCD;

	/* FIXME: workaround for Langwell A1/A2/A3 sighting */
	devlc_byte2 = (devlc >> 16) & 0xff;
	writeb(devlc_byte2, (u8 *)&dev->op_regs->devlc + 2);

	devlc = readl(&dev->op_regs->devlc);
	dev_vdbg(&dev->pdev->dev,
			"%s PHY low power suspend, devlc = 0x%08x\n",
			flag ? "enter" : "exit", devlc);
}


1187 1188 1189 1190
/* tries to wake up the host connected to this gadget */
static int langwell_wakeup(struct usb_gadget *_gadget)
{
	struct langwell_udc	*dev;
1191
	u32			portsc1;
1192
	unsigned long		flags;
1193 1194 1195 1196 1197

	if (!_gadget)
		return 0;

	dev = container_of(_gadget, struct langwell_udc, gadget);
1198
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1199

1200 1201 1202
	/* remote wakeup feature not enabled by host */
	if (!dev->remote_wakeup) {
		dev_info(&dev->pdev->dev, "remote wakeup is disabled\n");
1203
		return -ENOTSUPP;
1204
	}
1205 1206 1207 1208 1209 1210 1211 1212 1213

	spin_lock_irqsave(&dev->lock, flags);

	portsc1 = readl(&dev->op_regs->portsc1);
	if (!(portsc1 & PORTS_SUSP)) {
		spin_unlock_irqrestore(&dev->lock, flags);
		return 0;
	}

1214 1215 1216 1217 1218
	/* LPM L1 to L0 or legacy remote wakeup */
	if (dev->lpm && dev->lpm_state == LPM_L1)
		dev_info(&dev->pdev->dev, "LPM L1 to L0 remote wakeup\n");
	else
		dev_info(&dev->pdev->dev, "device remote wakeup\n");
1219 1220

	/* exit PHY low power suspend */
1221 1222 1223 1224 1225 1226
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 0);

	/* force port resume */
	portsc1 |= PORTS_FPR;
	writel(portsc1, &dev->op_regs->portsc1);
1227 1228 1229

	spin_unlock_irqrestore(&dev->lock, flags);

1230
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1231 1232 1233 1234 1235 1236 1237 1238 1239
	return 0;
}


/* notify controller that VBUS is powered or not */
static int langwell_vbus_session(struct usb_gadget *_gadget, int is_active)
{
	struct langwell_udc	*dev;
	unsigned long		flags;
1240
	u32			usbcmd;
1241 1242 1243 1244 1245

	if (!_gadget)
		return -ENODEV;

	dev = container_of(_gadget, struct langwell_udc, gadget);
1246
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1247 1248

	spin_lock_irqsave(&dev->lock, flags);
1249 1250
	dev_vdbg(&dev->pdev->dev, "VBUS status: %s\n",
			is_active ? "on" : "off");
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

	dev->vbus_active = (is_active != 0);
	if (dev->driver && dev->softconnected && dev->vbus_active) {
		usbcmd = readl(&dev->op_regs->usbcmd);
		usbcmd |= CMD_RUNSTOP;
		writel(usbcmd, &dev->op_regs->usbcmd);
	} else {
		usbcmd = readl(&dev->op_regs->usbcmd);
		usbcmd &= ~CMD_RUNSTOP;
		writel(usbcmd, &dev->op_regs->usbcmd);
	}

	spin_unlock_irqrestore(&dev->lock, flags);

1265
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	return 0;
}


/* constrain controller's VBUS power usage */
static int langwell_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
{
	struct langwell_udc	*dev;

	if (!_gadget)
		return -ENODEV;

	dev = container_of(_gadget, struct langwell_udc, gadget);
1279
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1280 1281

	if (dev->transceiver) {
1282 1283
		dev_vdbg(&dev->pdev->dev, "otg_set_power\n");
		dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1284 1285 1286
		return otg_set_power(dev->transceiver, mA);
	}

1287
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1288 1289 1290 1291 1292 1293 1294 1295
	return -ENOTSUPP;
}


/* D+ pullup, software-controlled connect/disconnect to USB host */
static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
{
	struct langwell_udc	*dev;
1296 1297
	u32			usbcmd;
	unsigned long		flags;
1298 1299 1300 1301 1302 1303

	if (!_gadget)
		return -ENODEV;

	dev = container_of(_gadget, struct langwell_udc, gadget);

1304
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

	spin_lock_irqsave(&dev->lock, flags);
	dev->softconnected = (is_on != 0);

	if (dev->driver && dev->softconnected && dev->vbus_active) {
		usbcmd = readl(&dev->op_regs->usbcmd);
		usbcmd |= CMD_RUNSTOP;
		writel(usbcmd, &dev->op_regs->usbcmd);
	} else {
		usbcmd = readl(&dev->op_regs->usbcmd);
		usbcmd &= ~CMD_RUNSTOP;
		writel(usbcmd, &dev->op_regs->usbcmd);
	}
	spin_unlock_irqrestore(&dev->lock, flags);

1320
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	return 0;
}


/* device controller usb_gadget_ops structure */
static const struct usb_gadget_ops langwell_ops = {

	/* returns the current frame number */
	.get_frame	= langwell_get_frame,

	/* tries to wake up the host connected to this gadget */
	.wakeup		= langwell_wakeup,

	/* set the device selfpowered feature, always selfpowered */
	/* .set_selfpowered = langwell_set_selfpowered, */

	/* notify controller that VBUS is powered or not */
	.vbus_session	= langwell_vbus_session,

	/* constrain controller's VBUS power usage */
	.vbus_draw	= langwell_vbus_draw,

	/* D+ pullup, software-controlled connect/disconnect to USB host */
	.pullup		= langwell_pullup,
};


/*-------------------------------------------------------------------------*/

/* device controller operations */

/* reset device controller */
static int langwell_udc_reset(struct langwell_udc *dev)
{
	u32		usbcmd, usbmode, devlc, endpointlistaddr;
1356
	u8		devlc_byte0, devlc_byte2;
1357 1358 1359 1360 1361
	unsigned long	timeout;

	if (!dev)
		return -EINVAL;

1362
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

	/* set controller to stop state */
	usbcmd = readl(&dev->op_regs->usbcmd);
	usbcmd &= ~CMD_RUNSTOP;
	writel(usbcmd, &dev->op_regs->usbcmd);

	/* reset device controller */
	usbcmd = readl(&dev->op_regs->usbcmd);
	usbcmd |= CMD_RST;
	writel(usbcmd, &dev->op_regs->usbcmd);

	/* wait for reset to complete */
	timeout = jiffies + RESET_TIMEOUT;
	while (readl(&dev->op_regs->usbcmd) & CMD_RST) {
		if (time_after(jiffies, timeout)) {
1378
			dev_err(&dev->pdev->dev, "device reset timeout\n");
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
			return -ETIMEDOUT;
		}
		cpu_relax();
	}

	/* set controller to device mode */
	usbmode = readl(&dev->op_regs->usbmode);
	usbmode |= MODE_DEVICE;

	/* turn setup lockout off, require setup tripwire in usbcmd */
	usbmode |= MODE_SLOM;

	writel(usbmode, &dev->op_regs->usbmode);
	usbmode = readl(&dev->op_regs->usbmode);
1393
	dev_vdbg(&dev->pdev->dev, "usbmode=0x%08x\n", usbmode);
1394 1395 1396 1397 1398 1399 1400

	/* Write-Clear setup status */
	writel(0, &dev->op_regs->usbsts);

	/* if support USB LPM, ACK all LPM token */
	if (dev->lpm) {
		devlc = readl(&dev->op_regs->devlc);
1401 1402
		dev_vdbg(&dev->pdev->dev, "devlc = 0x%08x\n", devlc);
		/* FIXME: workaround for Langwell A1/A2/A3 sighting */
1403 1404
		devlc &= ~LPM_STL;	/* don't STALL LPM token */
		devlc &= ~LPM_NYT_ACK;	/* ACK LPM token */
1405 1406 1407 1408 1409 1410 1411
		devlc_byte0 = devlc & 0xff;
		devlc_byte2 = (devlc >> 16) & 0xff;
		writeb(devlc_byte0, (u8 *)&dev->op_regs->devlc);
		writeb(devlc_byte2, (u8 *)&dev->op_regs->devlc + 2);
		devlc = readl(&dev->op_regs->devlc);
		dev_vdbg(&dev->pdev->dev,
				"ACK LPM token, devlc = 0x%08x\n", devlc);
1412 1413 1414 1415 1416 1417 1418
	}

	/* fill endpointlistaddr register */
	endpointlistaddr = dev->ep_dqh_dma;
	endpointlistaddr &= ENDPOINTLISTADDR_MASK;
	writel(endpointlistaddr, &dev->op_regs->endpointlistaddr);

1419 1420 1421 1422 1423
	dev_vdbg(&dev->pdev->dev,
		"dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n",
		dev->ep_dqh, endpointlistaddr,
		readl(&dev->op_regs->endpointlistaddr));
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	return 0;
}


/* reinitialize device controller endpoints */
static int eps_reinit(struct langwell_udc *dev)
{
	struct langwell_ep	*ep;
	char			name[14];
	int			i;

1435
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

	/* initialize ep0 */
	ep = &dev->ep[0];
	ep->dev = dev;
	strncpy(ep->name, "ep0", sizeof(ep->name));
	ep->ep.name = ep->name;
	ep->ep.ops = &langwell_ep_ops;
	ep->stopped = 0;
	ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
	ep->ep_num = 0;
	ep->desc = &langwell_ep0_desc;
	INIT_LIST_HEAD(&ep->queue);

	ep->ep_type = USB_ENDPOINT_XFER_CONTROL;

	/* initialize other endpoints */
	for (i = 2; i < dev->ep_max; i++) {
		ep = &dev->ep[i];
		if (i % 2)
			snprintf(name, sizeof(name), "ep%din", i / 2);
		else
			snprintf(name, sizeof(name), "ep%dout", i / 2);
		ep->dev = dev;
		strncpy(ep->name, name, sizeof(ep->name));
		ep->ep.name = ep->name;

		ep->ep.ops = &langwell_ep_ops;
		ep->stopped = 0;
		ep->ep.maxpacket = (unsigned short) ~0;
		ep->ep_num = i / 2;

		INIT_LIST_HEAD(&ep->queue);
		list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
	}

1471
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1472 1473 1474 1475 1476 1477 1478 1479
	return 0;
}


/* enable interrupt and set controller to run state */
static void langwell_udc_start(struct langwell_udc *dev)
{
	u32	usbintr, usbcmd;
1480
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502

	/* enable interrupts */
	usbintr = INTR_ULPIE	/* ULPI */
		| INTR_SLE	/* suspend */
		/* | INTR_SRE	SOF received */
		| INTR_URE	/* USB reset */
		| INTR_AAE	/* async advance */
		| INTR_SEE	/* system error */
		| INTR_FRE	/* frame list rollover */
		| INTR_PCE	/* port change detect */
		| INTR_UEE	/* USB error interrupt */
		| INTR_UE;	/* USB interrupt */
	writel(usbintr, &dev->op_regs->usbintr);

	/* clear stopped bit */
	dev->stopped = 0;

	/* set controller to run */
	usbcmd = readl(&dev->op_regs->usbcmd);
	usbcmd |= CMD_RUNSTOP;
	writel(usbcmd, &dev->op_regs->usbcmd);

1503
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1504 1505 1506 1507 1508 1509 1510 1511
}


/* disable interrupt and set controller to stop state */
static void langwell_udc_stop(struct langwell_udc *dev)
{
	u32	usbcmd;

1512
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

	/* disable all interrupts */
	writel(0, &dev->op_regs->usbintr);

	/* set stopped bit */
	dev->stopped = 1;

	/* set controller to stop state */
	usbcmd = readl(&dev->op_regs->usbcmd);
	usbcmd &= ~CMD_RUNSTOP;
	writel(usbcmd, &dev->op_regs->usbcmd);

1525
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1526 1527 1528 1529 1530 1531 1532 1533
}


/* stop all USB activities */
static void stop_activity(struct langwell_udc *dev,
		struct usb_gadget_driver *driver)
{
	struct langwell_ep	*ep;
1534
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

	nuke(&dev->ep[0], -ESHUTDOWN);

	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
		nuke(ep, -ESHUTDOWN);
	}

	/* report disconnect; the driver is already quiesced */
	if (driver) {
		spin_unlock(&dev->lock);
		driver->disconnect(&dev->gadget);
		spin_lock(&dev->lock);
	}

1549
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
}


/*-------------------------------------------------------------------------*/

/* device "function" sysfs attribute file */
static ssize_t show_function(struct device *_dev,
		struct device_attribute *attr, char *buf)
{
	struct langwell_udc	*dev = the_controller;

	if (!dev->driver || !dev->driver->function
			|| strlen(dev->driver->function) > PAGE_SIZE)
		return 0;

	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
}
static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);


/* device "langwell_udc" sysfs attribute file */
static ssize_t show_langwell_udc(struct device *_dev,
		struct device_attribute *attr, char *buf)
{
	struct langwell_udc	*dev = the_controller;
	struct langwell_request *req;
	struct langwell_ep	*ep = NULL;
	char			*next;
	unsigned		size;
	unsigned		t;
	unsigned		i;
	unsigned long		flags;
	u32			tmp_reg;

	next = buf;
	size = PAGE_SIZE;
	spin_lock_irqsave(&dev->lock, flags);

	/* driver basic information */
	t = scnprintf(next, size,
			DRIVER_DESC "\n"
			"%s version: %s\n"
			"Gadget driver: %s\n\n",
			driver_name, DRIVER_VERSION,
			dev->driver ? dev->driver->driver.name : "(none)");
	size -= t;
	next += t;

	/* device registers */
	tmp_reg = readl(&dev->op_regs->usbcmd);
	t = scnprintf(next, size,
			"USBCMD reg:\n"
			"SetupTW: %d\n"
			"Run/Stop: %s\n\n",
			(tmp_reg & CMD_SUTW) ? 1 : 0,
			(tmp_reg & CMD_RUNSTOP) ? "Run" : "Stop");
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->usbsts);
	t = scnprintf(next, size,
			"USB Status Reg:\n"
			"Device Suspend: %d\n"
			"Reset Received: %d\n"
			"System Error: %s\n"
			"USB Error Interrupt: %s\n\n",
			(tmp_reg & STS_SLI) ? 1 : 0,
			(tmp_reg & STS_URI) ? 1 : 0,
			(tmp_reg & STS_SEI) ? "Error" : "No error",
			(tmp_reg & STS_UEI) ? "Error detected" : "No error");
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->usbintr);
	t = scnprintf(next, size,
			"USB Intrrupt Enable Reg:\n"
			"Sleep Enable: %d\n"
			"SOF Received Enable: %d\n"
			"Reset Enable: %d\n"
			"System Error Enable: %d\n"
			"Port Change Dectected Enable: %d\n"
			"USB Error Intr Enable: %d\n"
			"USB Intr Enable: %d\n\n",
			(tmp_reg & INTR_SLE) ? 1 : 0,
			(tmp_reg & INTR_SRE) ? 1 : 0,
			(tmp_reg & INTR_URE) ? 1 : 0,
			(tmp_reg & INTR_SEE) ? 1 : 0,
			(tmp_reg & INTR_PCE) ? 1 : 0,
			(tmp_reg & INTR_UEE) ? 1 : 0,
			(tmp_reg & INTR_UE) ? 1 : 0);
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->frindex);
	t = scnprintf(next, size,
			"USB Frame Index Reg:\n"
			"Frame Number is 0x%08x\n\n",
			(tmp_reg & FRINDEX_MASK));
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->deviceaddr);
	t = scnprintf(next, size,
			"USB Device Address Reg:\n"
			"Device Addr is 0x%x\n\n",
			USBADR(tmp_reg));
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->endpointlistaddr);
	t = scnprintf(next, size,
			"USB Endpoint List Address Reg:\n"
			"Endpoint List Pointer is 0x%x\n\n",
			EPBASE(tmp_reg));
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->portsc1);
	t = scnprintf(next, size,
		"USB Port Status & Control Reg:\n"
		"Port Reset: %s\n"
		"Port Suspend Mode: %s\n"
		"Over-current Change: %s\n"
		"Port Enable/Disable Change: %s\n"
		"Port Enabled/Disabled: %s\n"
1675 1676
		"Current Connect Status: %s\n"
		"LPM Suspend Status: %s\n\n",
1677 1678 1679 1680 1681
		(tmp_reg & PORTS_PR) ? "Reset" : "Not Reset",
		(tmp_reg & PORTS_SUSP) ? "Suspend " : "Not Suspend",
		(tmp_reg & PORTS_OCC) ? "Detected" : "No",
		(tmp_reg & PORTS_PEC) ? "Changed" : "Not Changed",
		(tmp_reg & PORTS_PE) ? "Enable" : "Not Correct",
1682 1683
		(tmp_reg & PORTS_CCS) ?  "Attached" : "Not Attached",
		(tmp_reg & PORTS_SLP) ? "LPM L1" : "LPM L0");
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->devlc);
	t = scnprintf(next, size,
		"Device LPM Control Reg:\n"
		"Parallel Transceiver : %d\n"
		"Serial Transceiver : %d\n"
		"Port Speed: %s\n"
		"Port Force Full Speed Connenct: %s\n"
1694
		"PHY Low Power Suspend Clock: %s\n"
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
		"BmAttributes: %d\n\n",
		LPM_PTS(tmp_reg),
		(tmp_reg & LPM_STS) ? 1 : 0,
		({
			char	*s;
			switch (LPM_PSPD(tmp_reg)) {
			case LPM_SPEED_FULL:
				s = "Full Speed"; break;
			case LPM_SPEED_LOW:
				s = "Low Speed"; break;
			case LPM_SPEED_HIGH:
				s = "High Speed"; break;
			default:
				s = "Unknown Speed"; break;
			}
			s;
		}),
		(tmp_reg & LPM_PFSC) ? "Force Full Speed" : "Not Force",
		(tmp_reg & LPM_PHCD) ? "Disabled" : "Enabled",
		LPM_BA(tmp_reg));
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->usbmode);
	t = scnprintf(next, size,
			"USB Mode Reg:\n"
			"Controller Mode is : %s\n\n", ({
				char *s;
				switch (MODE_CM(tmp_reg)) {
				case MODE_IDLE:
					s = "Idle"; break;
				case MODE_DEVICE:
					s = "Device Controller"; break;
				case MODE_HOST:
					s = "Host Controller"; break;
				default:
					s = "None"; break;
				}
				s;
			}));
	size -= t;
	next += t;

	tmp_reg = readl(&dev->op_regs->endptsetupstat);
	t = scnprintf(next, size,
			"Endpoint Setup Status Reg:\n"
			"SETUP on ep 0x%04x\n\n",
			tmp_reg & SETUPSTAT_MASK);
	size -= t;
	next += t;

	for (i = 0; i < dev->ep_max / 2; i++) {
		tmp_reg = readl(&dev->op_regs->endptctrl[i]);
		t = scnprintf(next, size, "EP Ctrl Reg [%d]: 0x%08x\n",
				i, tmp_reg);
		size -= t;
		next += t;
	}
	tmp_reg = readl(&dev->op_regs->endptprime);
	t = scnprintf(next, size, "EP Prime Reg: 0x%08x\n\n", tmp_reg);
	size -= t;
	next += t;

	/* langwell_udc, langwell_ep, langwell_request structure information */
	ep = &dev->ep[0];
	t = scnprintf(next, size, "%s MaxPacketSize: 0x%x, ep_num: %d\n",
			ep->ep.name, ep->ep.maxpacket, ep->ep_num);
	size -= t;
	next += t;

	if (list_empty(&ep->queue)) {
		t = scnprintf(next, size, "its req queue is empty\n\n");
		size -= t;
		next += t;
	} else {
		list_for_each_entry(req, &ep->queue, queue) {
			t = scnprintf(next, size,
				"req %p actual 0x%x length 0x%x  buf %p\n",
				&req->req, req->req.actual,
				req->req.length, req->req.buf);
			size -= t;
			next += t;
		}
	}
	/* other gadget->eplist ep */
	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
		if (ep->desc) {
			t = scnprintf(next, size,
					"\n%s MaxPacketSize: 0x%x, "
					"ep_num: %d\n",
					ep->ep.name, ep->ep.maxpacket,
					ep->ep_num);
			size -= t;
			next += t;

			if (list_empty(&ep->queue)) {
				t = scnprintf(next, size,
						"its req queue is empty\n\n");
				size -= t;
				next += t;
			} else {
				list_for_each_entry(req, &ep->queue, queue) {
					t = scnprintf(next, size,
						"req %p actual 0x%x length "
						"0x%x  buf %p\n",
						&req->req, req->req.actual,
						req->req.length, req->req.buf);
					size -= t;
					next += t;
				}
			}
		}
	}

	spin_unlock_irqrestore(&dev->lock, flags);
	return PAGE_SIZE - size;
}
static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);


1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
/* device "remote_wakeup" sysfs attribute file */
static ssize_t store_remote_wakeup(struct device *_dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct langwell_udc	*dev = the_controller;
	unsigned long		flags;
	ssize_t			rc = count;

	if (count > 2)
		return -EINVAL;

	if (count > 0 && buf[count-1] == '\n')
		((char *) buf)[count-1] = 0;

	if (buf[0] != '1')
		return -EINVAL;

	/* force remote wakeup enabled in case gadget driver doesn't support */
	spin_lock_irqsave(&dev->lock, flags);
	dev->remote_wakeup = 1;
	dev->dev_status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
	spin_unlock_irqrestore(&dev->lock, flags);

	langwell_wakeup(&dev->gadget);

	return rc;
}
static DEVICE_ATTR(remote_wakeup, S_IWUSR, NULL, store_remote_wakeup);


1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
/*-------------------------------------------------------------------------*/

/*
 * when a driver is successfully registered, it will receive
 * control requests including set_configuration(), which enables
 * non-control requests.  then usb traffic follows until a
 * disconnect is reported.  then a host may connect again, or
 * the driver might get unbound.
 */

1855 1856
int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
		int (*bind)(struct usb_gadget *))
1857 1858 1859 1860 1861 1862 1863 1864
{
	struct langwell_udc	*dev = the_controller;
	unsigned long		flags;
	int			retval;

	if (!dev)
		return -ENODEV;

1865
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878

	if (dev->driver)
		return -EBUSY;

	spin_lock_irqsave(&dev->lock, flags);

	/* hook up the driver ... */
	driver->driver.bus = NULL;
	dev->driver = driver;
	dev->gadget.dev.driver = &driver->driver;

	spin_unlock_irqrestore(&dev->lock, flags);

1879
	retval = bind(&dev->gadget);
1880
	if (retval) {
1881
		dev_dbg(&dev->pdev->dev, "bind to driver %s --> %d\n",
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
				driver->driver.name, retval);
		dev->driver = NULL;
		dev->gadget.dev.driver = NULL;
		return retval;
	}

	retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
	if (retval)
		goto err_unbind;

	dev->usb_state = USB_STATE_ATTACHED;
	dev->ep0_state = WAIT_FOR_SETUP;
	dev->ep0_dir = USB_DIR_OUT;

	/* enable interrupt and set controller to run state */
	if (dev->got_irq)
		langwell_udc_start(dev);

1900 1901
	dev_vdbg(&dev->pdev->dev,
			"After langwell_udc_start(), print all registers:\n");
1902 1903
	print_all_registers(dev);

1904 1905 1906
	dev_info(&dev->pdev->dev, "register driver: %s\n",
			driver->driver.name);
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1907 1908 1909 1910 1911 1912 1913
	return 0;

err_unbind:
	driver->unbind(&dev->gadget);
	dev->gadget.dev.driver = NULL;
	dev->driver = NULL;

1914
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1915 1916
	return retval;
}
1917
EXPORT_SYMBOL(usb_gadget_probe_driver);
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928


/* unregister gadget driver */
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
{
	struct langwell_udc	*dev = the_controller;
	unsigned long		flags;

	if (!dev)
		return -ENODEV;

1929
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
1930

1931
	if (unlikely(!driver || !driver->unbind))
1932 1933
		return -EINVAL;

1934 1935 1936 1937
	/* exit PHY low power suspend */
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 0);

1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	/* unbind OTG transceiver */
	if (dev->transceiver)
		(void)otg_set_peripheral(dev->transceiver, 0);

	/* disable interrupt and set controller to stop state */
	langwell_udc_stop(dev);

	dev->usb_state = USB_STATE_ATTACHED;
	dev->ep0_state = WAIT_FOR_SETUP;
	dev->ep0_dir = USB_DIR_OUT;

	spin_lock_irqsave(&dev->lock, flags);

	/* stop all usb activities */
	dev->gadget.speed = USB_SPEED_UNKNOWN;
	stop_activity(dev, driver);
	spin_unlock_irqrestore(&dev->lock, flags);

	/* unbind gadget driver */
	driver->unbind(&dev->gadget);
	dev->gadget.dev.driver = NULL;
	dev->driver = NULL;

	device_remove_file(&dev->pdev->dev, &dev_attr_function);

1963 1964 1965
	dev_info(&dev->pdev->dev, "unregistered driver '%s'\n",
			driver->driver.name);
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
	return 0;
}
EXPORT_SYMBOL(usb_gadget_unregister_driver);


/*-------------------------------------------------------------------------*/

/*
 * setup tripwire is used as a semaphore to ensure that the setup data
 * payload is extracted from a dQH without being corrupted
 */
static void setup_tripwire(struct langwell_udc *dev)
{
	u32			usbcmd,
				endptsetupstat;
	unsigned long		timeout;
	struct langwell_dqh	*dqh;

1984
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

	/* ep0 OUT dQH */
	dqh = &dev->ep_dqh[EP_DIR_OUT];

	/* Write-Clear endptsetupstat */
	endptsetupstat = readl(&dev->op_regs->endptsetupstat);
	writel(endptsetupstat, &dev->op_regs->endptsetupstat);

	/* wait until endptsetupstat is cleared */
	timeout = jiffies + SETUPSTAT_TIMEOUT;
	while (readl(&dev->op_regs->endptsetupstat)) {
		if (time_after(jiffies, timeout)) {
1997
			dev_err(&dev->pdev->dev, "setup_tripwire timeout\n");
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
			break;
		}
		cpu_relax();
	}

	/* while a hazard exists when setup packet arrives */
	do {
		/* set setup tripwire bit */
		usbcmd = readl(&dev->op_regs->usbcmd);
		writel(usbcmd | CMD_SUTW, &dev->op_regs->usbcmd);

		/* copy the setup packet to local buffer */
		memcpy(&dev->local_setup_buff, &dqh->dqh_setup, 8);
	} while (!(readl(&dev->op_regs->usbcmd) & CMD_SUTW));

	/* Write-Clear setup tripwire bit */
	usbcmd = readl(&dev->op_regs->usbcmd);
	writel(usbcmd & ~CMD_SUTW, &dev->op_regs->usbcmd);

2017
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2018 2019 2020 2021 2022 2023 2024 2025
}


/* protocol ep0 stall, will automatically be cleared on new transaction */
static void ep0_stall(struct langwell_udc *dev)
{
	u32	endptctrl;

2026
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036

	/* set TX and RX to stall */
	endptctrl = readl(&dev->op_regs->endptctrl[0]);
	endptctrl |= EPCTRL_TXS | EPCTRL_RXS;
	writel(endptctrl, &dev->op_regs->endptctrl[0]);

	/* update ep0 state */
	dev->ep0_state = WAIT_FOR_SETUP;
	dev->ep0_dir = USB_DIR_OUT;

2037
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
}


/* PRIME a status phase for ep0 */
static int prime_status_phase(struct langwell_udc *dev, int dir)
{
	struct langwell_request	*req;
	struct langwell_ep	*ep;
	int			status = 0;

2048
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072

	if (dir == EP_DIR_IN)
		dev->ep0_dir = USB_DIR_IN;
	else
		dev->ep0_dir = USB_DIR_OUT;

	ep = &dev->ep[0];
	dev->ep0_state = WAIT_FOR_OUT_STATUS;

	req = dev->status_req;

	req->ep = ep;
	req->req.length = 0;
	req->req.status = -EINPROGRESS;
	req->req.actual = 0;
	req->req.complete = NULL;
	req->dtd_count = 0;

	if (!req_to_dtd(req))
		status = queue_dtd(ep, req);
	else
		return -ENOMEM;

	if (status)
2073
		dev_err(&dev->pdev->dev, "can't queue ep0 status request\n");
2074 2075 2076

	list_add_tail(&req->queue, &ep->queue);

2077
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2078 2079 2080 2081 2082 2083 2084 2085
	return status;
}


/* SET_ADDRESS request routine */
static void set_address(struct langwell_udc *dev, u16 value,
		u16 index, u16 length)
{
2086
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2087 2088 2089

	/* save the new address to device struct */
	dev->dev_addr = (u8) value;
2090
	dev_vdbg(&dev->pdev->dev, "dev->dev_addr = %d\n", dev->dev_addr);
2091 2092 2093 2094 2095 2096 2097 2098

	/* update usb state */
	dev->usb_state = USB_STATE_ADDRESS;

	/* STATUS phase */
	if (prime_status_phase(dev, EP_DIR_IN))
		ep0_stall(dev);

2099
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2100 2101 2102 2103 2104 2105 2106 2107
}


/* return endpoint by windex */
static struct langwell_ep *get_ep_by_windex(struct langwell_udc *dev,
		u16 wIndex)
{
	struct langwell_ep		*ep;
2108
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126

	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
		return &dev->ep[0];

	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
		u8	bEndpointAddress;
		if (!ep->desc)
			continue;

		bEndpointAddress = ep->desc->bEndpointAddress;
		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
			continue;

		if ((wIndex & USB_ENDPOINT_NUMBER_MASK)
			== (bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
			return ep;
	}

2127
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
	return NULL;
}


/* return whether endpoint is stalled, 0: not stalled; 1: stalled */
static int ep_is_stall(struct langwell_ep *ep)
{
	struct langwell_udc	*dev = ep->dev;
	u32			endptctrl;
	int			retval;

2139
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2140 2141 2142 2143 2144 2145 2146

	endptctrl = readl(&dev->op_regs->endptctrl[ep->ep_num]);
	if (is_in(ep))
		retval = endptctrl & EPCTRL_TXS ? 1 : 0;
	else
		retval = endptctrl & EPCTRL_RXS ? 1 : 0;

2147
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
	return retval;
}


/* GET_STATUS request routine */
static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
		u16 index, u16 length)
{
	struct langwell_request	*req;
	struct langwell_ep	*ep;
	u16	status_data = 0;	/* 16 bits cpu view status data */
	int	status = 0;

2161
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2162 2163 2164 2165 2166

	ep = &dev->ep[0];

	if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
		/* get device status */
2167
		status_data = dev->dev_status;
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
	} else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
		/* get interface status */
		status_data = 0;
	} else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
		/* get endpoint status */
		struct langwell_ep	*epn;
		epn = get_ep_by_windex(dev, index);
		/* stall if endpoint doesn't exist */
		if (!epn)
			goto stall;

		status_data = ep_is_stall(epn) << USB_ENDPOINT_HALT;
	}

2182 2183
	dev_dbg(&dev->pdev->dev, "get status data: 0x%04x\n", status_data);

2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	dev->ep0_dir = USB_DIR_IN;

	/* borrow the per device status_req */
	req = dev->status_req;

	/* fill in the reqest structure */
	*((u16 *) req->req.buf) = cpu_to_le16(status_data);
	req->ep = ep;
	req->req.length = 2;
	req->req.status = -EINPROGRESS;
	req->req.actual = 0;
	req->req.complete = NULL;
	req->dtd_count = 0;

	/* prime the data phase */
	if (!req_to_dtd(req))
		status = queue_dtd(ep, req);
	else			/* no mem */
		goto stall;

	if (status) {
2205 2206
		dev_err(&dev->pdev->dev,
				"response error on GET_STATUS request\n");
2207 2208 2209 2210 2211 2212
		goto stall;
	}

	list_add_tail(&req->queue, &ep->queue);
	dev->ep0_state = DATA_STATE_XMIT;

2213
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2214 2215 2216
	return;
stall:
	ep0_stall(dev);
2217
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
}


/* setup packet interrupt handler */
static void handle_setup_packet(struct langwell_udc *dev,
		struct usb_ctrlrequest *setup)
{
	u16	wValue = le16_to_cpu(setup->wValue);
	u16	wIndex = le16_to_cpu(setup->wIndex);
	u16	wLength = le16_to_cpu(setup->wLength);
2228
	u32	portsc1;
2229

2230
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2231 2232 2233 2234

	/* ep0 fifo flush */
	nuke(&dev->ep[0], -ESHUTDOWN);

2235
	dev_dbg(&dev->pdev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
			setup->bRequestType, setup->bRequest,
			wValue, wIndex, wLength);

	/* RNDIS gadget delegate */
	if ((setup->bRequestType == 0x21) && (setup->bRequest == 0x00)) {
		/* USB_CDC_SEND_ENCAPSULATED_COMMAND */
		goto delegate;
	}

	/* USB_CDC_GET_ENCAPSULATED_RESPONSE */
	if ((setup->bRequestType == 0xa1) && (setup->bRequest == 0x01)) {
		/* USB_CDC_GET_ENCAPSULATED_RESPONSE */
		goto delegate;
	}

	/* We process some stardard setup requests here */
	switch (setup->bRequest) {
	case USB_REQ_GET_STATUS:
2254
		dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_GET_STATUS\n");
2255 2256 2257 2258 2259 2260 2261 2262
		/* get status, DATA and STATUS phase */
		if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
					!= (USB_DIR_IN | USB_TYPE_STANDARD))
			break;
		get_status(dev, setup->bRequestType, wValue, wIndex, wLength);
		goto end;

	case USB_REQ_SET_ADDRESS:
2263
		dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_SET_ADDRESS\n");
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
		/* STATUS phase */
		if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
						| USB_RECIP_DEVICE))
			break;
		set_address(dev, wValue, wIndex, wLength);
		goto end;

	case USB_REQ_CLEAR_FEATURE:
	case USB_REQ_SET_FEATURE:
		/* STATUS phase */
	{
		int rc = -EOPNOTSUPP;
		if (setup->bRequest == USB_REQ_SET_FEATURE)
2277 2278
			dev_dbg(&dev->pdev->dev,
					"SETUP: USB_REQ_SET_FEATURE\n");
2279
		else if (setup->bRequest == USB_REQ_CLEAR_FEATURE)
2280 2281
			dev_dbg(&dev->pdev->dev,
					"SETUP: USB_REQ_CLEAR_FEATURE\n");
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298

		if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
				== (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
			struct langwell_ep	*epn;
			epn = get_ep_by_windex(dev, wIndex);
			/* stall if endpoint doesn't exist */
			if (!epn) {
				ep0_stall(dev);
				goto end;
			}

			if (wValue != 0 || wLength != 0
					|| epn->ep_num > dev->ep_max)
				break;

			spin_unlock(&dev->lock);
			rc = langwell_ep_set_halt(&epn->ep,
2299 2300
				(setup->bRequest == USB_REQ_SET_FEATURE)
				? 1 : 0);
2301 2302 2303 2304 2305
			spin_lock(&dev->lock);

		} else if ((setup->bRequestType & (USB_RECIP_MASK
				| USB_TYPE_MASK)) == (USB_RECIP_DEVICE
				| USB_TYPE_STANDARD)) {
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
			rc = 0;
			switch (wValue) {
			case USB_DEVICE_REMOTE_WAKEUP:
				if (setup->bRequest == USB_REQ_SET_FEATURE) {
					dev->remote_wakeup = 1;
					dev->dev_status |= (1 << wValue);
				} else {
					dev->remote_wakeup = 0;
					dev->dev_status &= ~(1 << wValue);
				}
				break;
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
			case USB_DEVICE_TEST_MODE:
				dev_dbg(&dev->pdev->dev, "SETUP: TEST MODE\n");
				if ((wIndex & 0xff) ||
					(dev->gadget.speed != USB_SPEED_HIGH))
					ep0_stall(dev);

				switch (wIndex >> 8) {
				case TEST_J:
				case TEST_K:
				case TEST_SE0_NAK:
				case TEST_PACKET:
				case TEST_FORCE_EN:
					if (prime_status_phase(dev, EP_DIR_IN))
						ep0_stall(dev);
					portsc1 = readl(&dev->op_regs->portsc1);
					portsc1 |= (wIndex & 0xf00) << 8;
					writel(portsc1, &dev->op_regs->portsc1);
					goto end;
				default:
					rc = -EOPNOTSUPP;
				}
				break;
2339 2340 2341 2342 2343
			default:
				rc = -EOPNOTSUPP;
				break;
			}

2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
			if (!gadget_is_otg(&dev->gadget))
				break;
			else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) {
				dev->gadget.b_hnp_enable = 1;
#ifdef	OTG_TRANSCEIVER
				if (!dev->lotg->otg.default_a)
					dev->lotg->hsm.b_hnp_enable = 1;
#endif
			} else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
				dev->gadget.a_hnp_support = 1;
			else if (setup->bRequest ==
					USB_DEVICE_A_ALT_HNP_SUPPORT)
				dev->gadget.a_alt_hnp_support = 1;
			else
				break;
		} else
			break;

		if (rc == 0) {
			if (prime_status_phase(dev, EP_DIR_IN))
				ep0_stall(dev);
		}
		goto end;
	}

	case USB_REQ_GET_DESCRIPTOR:
2370 2371
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_GET_DESCRIPTOR\n");
2372 2373 2374
		goto delegate;

	case USB_REQ_SET_DESCRIPTOR:
2375 2376
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n");
2377 2378 2379
		goto delegate;

	case USB_REQ_GET_CONFIGURATION:
2380 2381
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_GET_CONFIGURATION\n");
2382 2383 2384
		goto delegate;

	case USB_REQ_SET_CONFIGURATION:
2385 2386
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_SET_CONFIGURATION\n");
2387 2388 2389
		goto delegate;

	case USB_REQ_GET_INTERFACE:
2390 2391
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_GET_INTERFACE\n");
2392 2393 2394
		goto delegate;

	case USB_REQ_SET_INTERFACE:
2395 2396
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_SET_INTERFACE\n");
2397 2398 2399
		goto delegate;

	case USB_REQ_SYNCH_FRAME:
2400 2401
		dev_dbg(&dev->pdev->dev,
				"SETUP: USB_REQ_SYNCH_FRAME unsupported\n");
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
		goto delegate;

	default:
		/* delegate USB standard requests to the gadget driver */
		goto delegate;
delegate:
		/* USB requests handled by gadget */
		if (wLength) {
			/* DATA phase from gadget, STATUS phase from udc */
			dev->ep0_dir = (setup->bRequestType & USB_DIR_IN)
					?  USB_DIR_IN : USB_DIR_OUT;
2413 2414
			dev_vdbg(&dev->pdev->dev,
					"dev->ep0_dir = 0x%x, wLength = %d\n",
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
					dev->ep0_dir, wLength);
			spin_unlock(&dev->lock);
			if (dev->driver->setup(&dev->gadget,
					&dev->local_setup_buff) < 0)
				ep0_stall(dev);
			spin_lock(&dev->lock);
			dev->ep0_state = (setup->bRequestType & USB_DIR_IN)
					?  DATA_STATE_XMIT : DATA_STATE_RECV;
		} else {
			/* no DATA phase, IN STATUS phase from gadget */
			dev->ep0_dir = USB_DIR_IN;
2426 2427
			dev_vdbg(&dev->pdev->dev,
					"dev->ep0_dir = 0x%x, wLength = %d\n",
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
					dev->ep0_dir, wLength);
			spin_unlock(&dev->lock);
			if (dev->driver->setup(&dev->gadget,
					&dev->local_setup_buff) < 0)
				ep0_stall(dev);
			spin_lock(&dev->lock);
			dev->ep0_state = WAIT_FOR_OUT_STATUS;
		}
		break;
	}
end:
2439
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
}


/* transfer completion, process endpoint request and free the completed dTDs
 * for this request
 */
static int process_ep_req(struct langwell_udc *dev, int index,
		struct langwell_request *curr_req)
{
	struct langwell_dtd	*curr_dtd;
	struct langwell_dqh	*curr_dqh;
	int			td_complete, actual, remaining_length;
	int			i, dir;
	u8			dtd_status = 0;
	int			retval = 0;

	curr_dqh = &dev->ep_dqh[index];
	dir = index % 2;

	curr_dtd = curr_req->head;
	td_complete = 0;
	actual = curr_req->req.length;

2463
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2464 2465 2466 2467 2468 2469

	for (i = 0; i < curr_req->dtd_count; i++) {

		/* command execution states by dTD */
		dtd_status = curr_dtd->dtd_status;

2470 2471 2472 2473
		barrier();
		remaining_length = le16_to_cpu(curr_dtd->dtd_total);
		actual -= remaining_length;

2474 2475 2476 2477
		if (!dtd_status) {
			/* transfers completed successfully */
			if (!remaining_length) {
				td_complete++;
2478 2479
				dev_vdbg(&dev->pdev->dev,
					"dTD transmitted successfully\n");
2480 2481
			} else {
				if (dir) {
2482 2483
					dev_vdbg(&dev->pdev->dev,
						"TX dTD remains data\n");
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
					retval = -EPROTO;
					break;

				} else {
					td_complete++;
					break;
				}
			}
		} else {
			/* transfers completed with errors */
			if (dtd_status & DTD_STS_ACTIVE) {
2495 2496
				dev_dbg(&dev->pdev->dev,
					"dTD status ACTIVE dQH[%d]\n", index);
2497 2498 2499
				retval = 1;
				return retval;
			} else if (dtd_status & DTD_STS_HALTED) {
2500 2501 2502
				dev_err(&dev->pdev->dev,
					"dTD error %08x dQH[%d]\n",
					dtd_status, index);
2503 2504 2505 2506 2507
				/* clear the errors and halt condition */
				curr_dqh->dtd_status = 0;
				retval = -EPIPE;
				break;
			} else if (dtd_status & DTD_STS_DBE) {
2508 2509
				dev_dbg(&dev->pdev->dev,
					"data buffer (overflow) error\n");
2510 2511 2512
				retval = -EPROTO;
				break;
			} else if (dtd_status & DTD_STS_TRE) {
2513 2514
				dev_dbg(&dev->pdev->dev,
					"transaction(ISO) error\n");
2515 2516 2517
				retval = -EILSEQ;
				break;
			} else
2518 2519 2520
				dev_err(&dev->pdev->dev,
					"unknown error (0x%x)!\n",
					dtd_status);
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
		}

		if (i != curr_req->dtd_count - 1)
			curr_dtd = (struct langwell_dtd *)
				curr_dtd->next_dtd_virt;
	}

	if (retval)
		return retval;

	curr_req->req.actual = actual;

2533
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2534 2535 2536 2537 2538 2539 2540 2541 2542
	return 0;
}


/* complete DATA or STATUS phase of ep0 prime status phase if needed */
static void ep0_req_complete(struct langwell_udc *dev,
		struct langwell_ep *ep0, struct langwell_request *req)
{
	u32	new_addr;
2543
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2544 2545 2546 2547 2548 2549 2550

	if (dev->usb_state == USB_STATE_ADDRESS) {
		/* set the new address */
		new_addr = (u32)dev->dev_addr;
		writel(new_addr << USBADR_SHIFT, &dev->op_regs->deviceaddr);

		new_addr = USBADR(readl(&dev->op_regs->deviceaddr));
2551
		dev_vdbg(&dev->pdev->dev, "new_addr = %d\n", new_addr);
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
	}

	done(ep0, req, 0);

	switch (dev->ep0_state) {
	case DATA_STATE_XMIT:
		/* receive status phase */
		if (prime_status_phase(dev, EP_DIR_OUT))
			ep0_stall(dev);
		break;
	case DATA_STATE_RECV:
		/* send status phase */
		if (prime_status_phase(dev, EP_DIR_IN))
			ep0_stall(dev);
		break;
	case WAIT_FOR_OUT_STATUS:
		dev->ep0_state = WAIT_FOR_SETUP;
		break;
	case WAIT_FOR_SETUP:
2571
		dev_err(&dev->pdev->dev, "unexpect ep0 packets\n");
2572 2573 2574 2575 2576 2577
		break;
	default:
		ep0_stall(dev);
		break;
	}

2578
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
}


/* USB transfer completion interrupt */
static void handle_trans_complete(struct langwell_udc *dev)
{
	u32			complete_bits;
	int			i, ep_num, dir, bit_mask, status;
	struct langwell_ep	*epn;
	struct langwell_request	*curr_req, *temp_req;

2590
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2591 2592

	complete_bits = readl(&dev->op_regs->endptcomplete);
2593 2594
	dev_vdbg(&dev->pdev->dev, "endptcomplete register: 0x%08x\n",
			complete_bits);
2595 2596 2597 2598 2599

	/* Write-Clear the bits in endptcomplete register */
	writel(complete_bits, &dev->op_regs->endptcomplete);

	if (!complete_bits) {
2600
		dev_dbg(&dev->pdev->dev, "complete_bits = 0\n");
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
		goto done;
	}

	for (i = 0; i < dev->ep_max; i++) {
		ep_num = i / 2;
		dir = i % 2;

		bit_mask = 1 << (ep_num + 16 * dir);

		if (!(complete_bits & bit_mask))
			continue;

		/* ep0 */
		if (i == 1)
			epn = &dev->ep[0];
		else
			epn = &dev->ep[i];

		if (epn->name == NULL) {
2620
			dev_warn(&dev->pdev->dev, "invalid endpoint\n");
2621 2622 2623 2624 2625
			continue;
		}

		if (i < 2)
			/* ep0 in and out */
2626
			dev_dbg(&dev->pdev->dev, "%s-%s transfer completed\n",
2627 2628 2629
					epn->name,
					is_in(epn) ? "in" : "out");
		else
2630 2631
			dev_dbg(&dev->pdev->dev, "%s transfer completed\n",
					epn->name);
2632 2633 2634 2635 2636

		/* process the req queue until an uncomplete request */
		list_for_each_entry_safe(curr_req, temp_req,
				&epn->queue, queue) {
			status = process_ep_req(dev, i, curr_req);
2637 2638
			dev_vdbg(&dev->pdev->dev, "%s req status: %d\n",
					epn->name, status);
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655

			if (status)
				break;

			/* write back status to req */
			curr_req->req.status = status;

			/* ep0 request completion */
			if (ep_num == 0) {
				ep0_req_complete(dev, epn, curr_req);
				break;
			} else {
				done(epn, curr_req, status);
			}
		}
	}
done:
2656
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2657 2658 2659 2660 2661 2662 2663 2664 2665
}


/* port change detect interrupt handler */
static void handle_port_change(struct langwell_udc *dev)
{
	u32	portsc1, devlc;
	u32	speed;

2666
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2667 2668 2669 2670 2671 2672

	if (dev->bus_reset)
		dev->bus_reset = 0;

	portsc1 = readl(&dev->op_regs->portsc1);
	devlc = readl(&dev->op_regs->devlc);
2673
	dev_vdbg(&dev->pdev->dev, "portsc1 = 0x%08x, devlc = 0x%08x\n",
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
			portsc1, devlc);

	/* bus reset is finished */
	if (!(portsc1 & PORTS_PR)) {
		/* get the speed */
		speed = LPM_PSPD(devlc);
		switch (speed) {
		case LPM_SPEED_HIGH:
			dev->gadget.speed = USB_SPEED_HIGH;
			break;
		case LPM_SPEED_FULL:
			dev->gadget.speed = USB_SPEED_FULL;
			break;
		case LPM_SPEED_LOW:
			dev->gadget.speed = USB_SPEED_LOW;
			break;
		default:
			dev->gadget.speed = USB_SPEED_UNKNOWN;
			break;
		}
2694 2695
		dev_vdbg(&dev->pdev->dev,
				"speed = %d, dev->gadget.speed = %d\n",
2696 2697 2698 2699 2700 2701
				speed, dev->gadget.speed);
	}

	/* LPM L0 to L1 */
	if (dev->lpm && dev->lpm_state == LPM_L0)
		if (portsc1 & PORTS_SUSP && portsc1 & PORTS_SLP) {
2702 2703
			dev_info(&dev->pdev->dev, "LPM L0 to L1\n");
			dev->lpm_state = LPM_L1;
2704 2705 2706 2707 2708
		}

	/* LPM L1 to L0, force resume or remote wakeup finished */
	if (dev->lpm && dev->lpm_state == LPM_L1)
		if (!(portsc1 & PORTS_SUSP)) {
2709
			dev_info(&dev->pdev->dev, "LPM L1 to L0\n");
2710 2711 2712 2713 2714 2715 2716
			dev->lpm_state = LPM_L0;
		}

	/* update USB state */
	if (!dev->resume_state)
		dev->usb_state = USB_STATE_DEFAULT;

2717
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
}


/* USB reset interrupt handler */
static void handle_usb_reset(struct langwell_udc *dev)
{
	u32		deviceaddr,
			endptsetupstat,
			endptcomplete;
	unsigned long	timeout;

2729
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745

	/* Write-Clear the device address */
	deviceaddr = readl(&dev->op_regs->deviceaddr);
	writel(deviceaddr & ~USBADR_MASK, &dev->op_regs->deviceaddr);

	dev->dev_addr = 0;

	/* clear usb state */
	dev->resume_state = 0;

	/* LPM L1 to L0, reset */
	if (dev->lpm)
		dev->lpm_state = LPM_L0;

	dev->ep0_dir = USB_DIR_OUT;
	dev->ep0_state = WAIT_FOR_SETUP;
2746 2747 2748 2749

	/* remote wakeup reset to 0 when the device is reset */
	dev->remote_wakeup = 0;
	dev->dev_status = 1 << USB_DEVICE_SELF_POWERED;
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
	dev->gadget.b_hnp_enable = 0;
	dev->gadget.a_hnp_support = 0;
	dev->gadget.a_alt_hnp_support = 0;

	/* Write-Clear all the setup token semaphores */
	endptsetupstat = readl(&dev->op_regs->endptsetupstat);
	writel(endptsetupstat, &dev->op_regs->endptsetupstat);

	/* Write-Clear all the endpoint complete status bits */
	endptcomplete = readl(&dev->op_regs->endptcomplete);
	writel(endptcomplete, &dev->op_regs->endptcomplete);

	/* wait until all endptprime bits cleared */
	timeout = jiffies + PRIME_TIMEOUT;
	while (readl(&dev->op_regs->endptprime)) {
		if (time_after(jiffies, timeout)) {
2766
			dev_err(&dev->pdev->dev, "USB reset timeout\n");
2767 2768 2769 2770 2771 2772 2773 2774 2775
			break;
		}
		cpu_relax();
	}

	/* write 1s to endptflush register to clear any primed buffers */
	writel((u32) ~0, &dev->op_regs->endptflush);

	if (readl(&dev->op_regs->portsc1) & PORTS_PR) {
2776
		dev_vdbg(&dev->pdev->dev, "USB bus reset\n");
2777 2778 2779 2780 2781 2782 2783
		/* bus is reseting */
		dev->bus_reset = 1;

		/* reset all the queues, stop all USB activities */
		stop_activity(dev, dev->driver);
		dev->usb_state = USB_STATE_DEFAULT;
	} else {
2784
		dev_vdbg(&dev->pdev->dev, "device controller reset\n");
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
		/* controller reset */
		langwell_udc_reset(dev);

		/* reset all the queues, stop all USB activities */
		stop_activity(dev, dev->driver);

		/* reset ep0 dQH and endptctrl */
		ep0_reset(dev);

		/* enable interrupt and set controller to run state */
		langwell_udc_start(dev);

		dev->usb_state = USB_STATE_ATTACHED;
	}

#ifdef	OTG_TRANSCEIVER
	/* refer to USB OTG 6.6.2.3 b_hnp_en is cleared */
	if (!dev->lotg->otg.default_a)
		dev->lotg->hsm.b_hnp_enable = 0;
#endif

2806
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2807 2808 2809 2810 2811 2812
}


/* USB bus suspend/resume interrupt */
static void handle_bus_suspend(struct langwell_udc *dev)
{
2813
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846

	dev->resume_state = dev->usb_state;
	dev->usb_state = USB_STATE_SUSPENDED;

#ifdef	OTG_TRANSCEIVER
	if (dev->lotg->otg.default_a) {
		if (dev->lotg->hsm.b_bus_suspend_vld == 1) {
			dev->lotg->hsm.b_bus_suspend = 1;
			/* notify transceiver the state changes */
			if (spin_trylock(&dev->lotg->wq_lock)) {
				langwell_update_transceiver();
				spin_unlock(&dev->lotg->wq_lock);
			}
		}
		dev->lotg->hsm.b_bus_suspend_vld++;
	} else {
		if (!dev->lotg->hsm.a_bus_suspend) {
			dev->lotg->hsm.a_bus_suspend = 1;
			/* notify transceiver the state changes */
			if (spin_trylock(&dev->lotg->wq_lock)) {
				langwell_update_transceiver();
				spin_unlock(&dev->lotg->wq_lock);
			}
		}
	}
#endif

	/* report suspend to the driver */
	if (dev->driver) {
		if (dev->driver->suspend) {
			spin_unlock(&dev->lock);
			dev->driver->suspend(&dev->gadget);
			spin_lock(&dev->lock);
2847 2848
			dev_dbg(&dev->pdev->dev, "suspend %s\n",
					dev->driver->driver.name);
2849 2850 2851 2852
		}
	}

	/* enter PHY low power suspend */
2853 2854
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 0);
2855

2856
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2857 2858 2859 2860 2861
}


static void handle_bus_resume(struct langwell_udc *dev)
{
2862
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
2863 2864 2865 2866 2867

	dev->usb_state = dev->resume_state;
	dev->resume_state = 0;

	/* exit PHY low power suspend */
2868 2869
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 0);
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881

#ifdef	OTG_TRANSCEIVER
	if (dev->lotg->otg.default_a == 0)
		dev->lotg->hsm.a_bus_suspend = 0;
#endif

	/* report resume to the driver */
	if (dev->driver) {
		if (dev->driver->resume) {
			spin_unlock(&dev->lock);
			dev->driver->resume(&dev->gadget);
			spin_lock(&dev->lock);
2882 2883
			dev_dbg(&dev->pdev->dev, "resume %s\n",
					dev->driver->driver.name);
2884 2885 2886
		}
	}

2887
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
}


/* USB device controller interrupt handler */
static irqreturn_t langwell_irq(int irq, void *_dev)
{
	struct langwell_udc	*dev = _dev;
	u32			usbsts,
				usbintr,
				irq_sts,
				portsc1;

2900
	dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
2901 2902

	if (dev->stopped) {
2903 2904
		dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
		dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
		return IRQ_NONE;
	}

	spin_lock(&dev->lock);

	/* USB status */
	usbsts = readl(&dev->op_regs->usbsts);

	/* USB interrupt enable */
	usbintr = readl(&dev->op_regs->usbintr);

	irq_sts = usbsts & usbintr;
2917 2918
	dev_vdbg(&dev->pdev->dev,
			"usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n",
2919 2920 2921
			usbsts, usbintr, irq_sts);

	if (!irq_sts) {
2922 2923
		dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
		dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
		spin_unlock(&dev->lock);
		return IRQ_NONE;
	}

	/* Write-Clear interrupt status bits */
	writel(irq_sts, &dev->op_regs->usbsts);

	/* resume from suspend */
	portsc1 = readl(&dev->op_regs->portsc1);
	if (dev->usb_state == USB_STATE_SUSPENDED)
		if (!(portsc1 & PORTS_SUSP))
			handle_bus_resume(dev);

	/* USB interrupt */
	if (irq_sts & STS_UI) {
2939
		dev_vdbg(&dev->pdev->dev, "USB interrupt\n");
2940 2941 2942 2943

		/* setup packet received from ep0 */
		if (readl(&dev->op_regs->endptsetupstat)
				& EP0SETUPSTAT_MASK) {
2944 2945
			dev_vdbg(&dev->pdev->dev,
				"USB SETUP packet received interrupt\n");
2946 2947 2948 2949 2950 2951 2952
			/* setup tripwire semaphone */
			setup_tripwire(dev);
			handle_setup_packet(dev, &dev->local_setup_buff);
		}

		/* USB transfer completion */
		if (readl(&dev->op_regs->endptcomplete)) {
2953 2954
			dev_vdbg(&dev->pdev->dev,
				"USB transfer completion interrupt\n");
2955 2956 2957 2958 2959 2960 2961
			handle_trans_complete(dev);
		}
	}

	/* SOF received interrupt (for ISO transfer) */
	if (irq_sts & STS_SRI) {
		/* FIXME */
2962
		/* dev_vdbg(&dev->pdev->dev, "SOF received interrupt\n"); */
2963 2964 2965 2966
	}

	/* port change detect interrupt */
	if (irq_sts & STS_PCI) {
2967
		dev_vdbg(&dev->pdev->dev, "port change detect interrupt\n");
2968 2969 2970 2971 2972
		handle_port_change(dev);
	}

	/* suspend interrrupt */
	if (irq_sts & STS_SLI) {
2973
		dev_vdbg(&dev->pdev->dev, "suspend interrupt\n");
2974 2975 2976 2977 2978
		handle_bus_suspend(dev);
	}

	/* USB reset interrupt */
	if (irq_sts & STS_URI) {
2979
		dev_vdbg(&dev->pdev->dev, "USB reset interrupt\n");
2980 2981 2982 2983 2984 2985
		handle_usb_reset(dev);
	}

	/* USB error or system error interrupt */
	if (irq_sts & (STS_UEI | STS_SEI)) {
		/* FIXME */
2986
		dev_warn(&dev->pdev->dev, "error IRQ, irq_sts: %x\n", irq_sts);
2987 2988 2989 2990
	}

	spin_unlock(&dev->lock);

2991
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
	return IRQ_HANDLED;
}


/*-------------------------------------------------------------------------*/

/* release device structure */
static void gadget_release(struct device *_dev)
{
	struct langwell_udc	*dev = the_controller;

3003
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3004 3005 3006

	complete(dev->done);

3007
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3008 3009 3010 3011
	kfree(dev);
}


3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
/* enable SRAM caching if SRAM detected */
static void sram_init(struct langwell_udc *dev)
{
	struct pci_dev		*pdev = dev->pdev;

	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);

	dev->sram_addr = pci_resource_start(pdev, 1);
	dev->sram_size = pci_resource_len(pdev, 1);
	dev_info(&dev->pdev->dev, "Found private SRAM at %x size:%x\n",
			dev->sram_addr, dev->sram_size);
	dev->got_sram = 1;

	if (pci_request_region(pdev, 1, kobject_name(&pdev->dev.kobj))) {
		dev_warn(&dev->pdev->dev, "SRAM request failed\n");
		dev->got_sram = 0;
	} else if (!dma_declare_coherent_memory(&pdev->dev, dev->sram_addr,
			dev->sram_addr, dev->sram_size, DMA_MEMORY_MAP)) {
		dev_warn(&dev->pdev->dev, "SRAM DMA declare failed\n");
		pci_release_region(pdev, 1);
		dev->got_sram = 0;
	}

	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
}


/* release SRAM caching */
static void sram_deinit(struct langwell_udc *dev)
{
	struct pci_dev *pdev = dev->pdev;

	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);

	dma_release_declared_memory(&pdev->dev);
	pci_release_region(pdev, 1);

	dev->got_sram = 0;

	dev_info(&dev->pdev->dev, "release SRAM caching\n");
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
}


3056 3057 3058 3059 3060 3061 3062 3063
/* tear down the binding between this driver and the pci device */
static void langwell_udc_remove(struct pci_dev *pdev)
{
	struct langwell_udc	*dev = the_controller;

	DECLARE_COMPLETION(done);

	BUG_ON(dev->driver);
3064
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3065 3066 3067

	dev->done = &done;

3068 3069
#ifndef	OTG_TRANSCEIVER
	/* free dTD dma_pool and dQH */
3070 3071 3072
	if (dev->dtd_pool)
		dma_pool_destroy(dev->dtd_pool);

3073 3074 3075 3076 3077 3078 3079 3080 3081
	if (dev->ep_dqh)
		dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
			dev->ep_dqh, dev->ep_dqh_dma);

	/* release SRAM caching */
	if (dev->has_sram && dev->got_sram)
		sram_deinit(dev);
#endif

3082 3083 3084 3085 3086 3087 3088
	if (dev->status_req) {
		kfree(dev->status_req->req.buf);
		kfree(dev->status_req);
	}

	kfree(dev->ep);

3089
	/* disable IRQ handler */
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
	if (dev->got_irq)
		free_irq(pdev->irq, dev);

#ifndef	OTG_TRANSCEIVER
	if (dev->cap_regs)
		iounmap(dev->cap_regs);

	if (dev->region)
		release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));

	if (dev->enabled)
		pci_disable_device(pdev);
#else
	if (dev->transceiver) {
		otg_put_transceiver(dev->transceiver);
		dev->transceiver = NULL;
		dev->lotg = NULL;
	}
#endif

	dev->cap_regs = NULL;

3113 3114
	dev_info(&dev->pdev->dev, "unbind\n");
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3115 3116 3117

	device_unregister(&dev->gadget.dev);
	device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
3118
	device_remove_file(&pdev->dev, &dev_attr_remote_wakeup);
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161

#ifndef	OTG_TRANSCEIVER
	pci_set_drvdata(pdev, NULL);
#endif

	/* free dev, wait for the release() finished */
	wait_for_completion(&done);

	the_controller = NULL;
}


/*
 * wrap this driver around the specified device, but
 * don't respond over USB until a gadget driver binds to us.
 */
static int langwell_udc_probe(struct pci_dev *pdev,
		const struct pci_device_id *id)
{
	struct langwell_udc	*dev;
#ifndef	OTG_TRANSCEIVER
	unsigned long		resource, len;
#endif
	void			__iomem *base = NULL;
	size_t			size;
	int			retval;

	if (the_controller) {
		dev_warn(&pdev->dev, "ignoring\n");
		return -EBUSY;
	}

	/* alloc, and start init */
	dev = kzalloc(sizeof *dev, GFP_KERNEL);
	if (dev == NULL) {
		retval = -ENOMEM;
		goto error;
	}

	/* initialize device spinlock */
	spin_lock_init(&dev->lock);

	dev->pdev = pdev;
3162
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186

#ifdef	OTG_TRANSCEIVER
	/* PCI device is already enabled by otg_transceiver driver */
	dev->enabled = 1;

	/* mem region and register base */
	dev->region = 1;
	dev->transceiver = otg_get_transceiver();
	dev->lotg = otg_to_langwell(dev->transceiver);
	base = dev->lotg->regs;
#else
	pci_set_drvdata(pdev, dev);

	/* now all the pci goodies ... */
	if (pci_enable_device(pdev) < 0) {
		retval = -ENODEV;
		goto error;
	}
	dev->enabled = 1;

	/* control register: BAR 0 */
	resource = pci_resource_start(pdev, 0);
	len = pci_resource_len(pdev, 0);
	if (!request_mem_region(resource, len, driver_name)) {
3187
		dev_err(&dev->pdev->dev, "controller already in use\n");
3188 3189 3190 3191 3192 3193 3194 3195
		retval = -EBUSY;
		goto error;
	}
	dev->region = 1;

	base = ioremap_nocache(resource, len);
#endif
	if (base == NULL) {
3196
		dev_err(&dev->pdev->dev, "can't map memory\n");
3197 3198 3199 3200 3201
		retval = -EFAULT;
		goto error;
	}

	dev->cap_regs = (struct langwell_cap_regs __iomem *) base;
3202
	dev_vdbg(&dev->pdev->dev, "dev->cap_regs: %p\n", dev->cap_regs);
3203 3204
	dev->op_regs = (struct langwell_op_regs __iomem *)
		(base + OP_REG_OFFSET);
3205
	dev_vdbg(&dev->pdev->dev, "dev->op_regs: %p\n", dev->op_regs);
3206 3207 3208

	/* irq setup after old hardware is cleaned up */
	if (!pdev->irq) {
3209
		dev_err(&dev->pdev->dev, "No IRQ. Check PCI setup!\n");
3210 3211 3212 3213
		retval = -ENODEV;
		goto error;
	}

3214 3215 3216 3217
	dev->has_sram = 1;
	dev->got_sram = 0;
	dev_vdbg(&dev->pdev->dev, "dev->has_sram: %d\n", dev->has_sram);

3218
#ifndef	OTG_TRANSCEIVER
3219 3220 3221 3222
	/* enable SRAM caching if detected */
	if (dev->has_sram && !dev->got_sram)
		sram_init(dev);

3223 3224
	dev_info(&dev->pdev->dev,
			"irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n",
3225 3226 3227 3228 3229 3230
			pdev->irq, resource, len, base);
	/* enables bus-mastering for device dev */
	pci_set_master(pdev);

	if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
				driver_name, dev) != 0) {
3231 3232
		dev_err(&dev->pdev->dev,
				"request interrupt %d failed\n", pdev->irq);
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
		retval = -EBUSY;
		goto error;
	}
	dev->got_irq = 1;
#endif

	/* set stopped bit */
	dev->stopped = 1;

	/* capabilities and endpoint number */
	dev->lpm = (readl(&dev->cap_regs->hccparams) & HCC_LEN) ? 1 : 0;
	dev->dciversion = readw(&dev->cap_regs->dciversion);
	dev->devcap = (readl(&dev->cap_regs->dccparams) & DEVCAP) ? 1 : 0;
3246 3247 3248 3249 3250 3251
	dev_vdbg(&dev->pdev->dev, "dev->lpm: %d\n", dev->lpm);
	dev_vdbg(&dev->pdev->dev, "dev->dciversion: 0x%04x\n",
			dev->dciversion);
	dev_vdbg(&dev->pdev->dev, "dccparams: 0x%08x\n",
			readl(&dev->cap_regs->dccparams));
	dev_vdbg(&dev->pdev->dev, "dev->devcap: %d\n", dev->devcap);
3252
	if (!dev->devcap) {
3253
		dev_err(&dev->pdev->dev, "can't support device mode\n");
3254 3255 3256 3257 3258 3259
		retval = -ENODEV;
		goto error;
	}

	/* a pair of endpoints (out/in) for each address */
	dev->ep_max = DEN(readl(&dev->cap_regs->dccparams)) * 2;
3260
	dev_vdbg(&dev->pdev->dev, "dev->ep_max: %d\n", dev->ep_max);
3261 3262 3263 3264 3265

	/* allocate endpoints memory */
	dev->ep = kzalloc(sizeof(struct langwell_ep) * dev->ep_max,
			GFP_KERNEL);
	if (!dev->ep) {
3266
		dev_err(&dev->pdev->dev, "allocate endpoints memory failed\n");
3267 3268 3269 3270 3271 3272
		retval = -ENOMEM;
		goto error;
	}

	/* allocate device dQH memory */
	size = dev->ep_max * sizeof(struct langwell_dqh);
3273
	dev_vdbg(&dev->pdev->dev, "orig size = %d\n", size);
3274 3275 3276 3277 3278 3279 3280 3281 3282
	if (size < DQH_ALIGNMENT)
		size = DQH_ALIGNMENT;
	else if ((size % DQH_ALIGNMENT) != 0) {
		size += DQH_ALIGNMENT + 1;
		size &= ~(DQH_ALIGNMENT - 1);
	}
	dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
					&dev->ep_dqh_dma, GFP_KERNEL);
	if (!dev->ep_dqh) {
3283
		dev_err(&dev->pdev->dev, "allocate dQH memory failed\n");
3284 3285 3286 3287
		retval = -ENOMEM;
		goto error;
	}
	dev->ep_dqh_size = size;
3288
	dev_vdbg(&dev->pdev->dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);
3289 3290 3291 3292

	/* initialize ep0 status request structure */
	dev->status_req = kzalloc(sizeof(struct langwell_request), GFP_KERNEL);
	if (!dev->status_req) {
3293 3294
		dev_err(&dev->pdev->dev,
				"allocate status_req memory failed\n");
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306
		retval = -ENOMEM;
		goto error;
	}
	INIT_LIST_HEAD(&dev->status_req->queue);

	/* allocate a small amount of memory to get valid address */
	dev->status_req->req.buf = kmalloc(8, GFP_KERNEL);
	dev->status_req->req.dma = virt_to_phys(dev->status_req->req.buf);

	dev->resume_state = USB_STATE_NOTATTACHED;
	dev->usb_state = USB_STATE_POWERED;
	dev->ep0_dir = USB_DIR_OUT;
3307 3308 3309 3310

	/* remote wakeup reset to 0 when the device is reset */
	dev->remote_wakeup = 0;
	dev->dev_status = 1 << USB_DEVICE_SELF_POWERED;
3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354

#ifndef	OTG_TRANSCEIVER
	/* reset device controller */
	langwell_udc_reset(dev);
#endif

	/* initialize gadget structure */
	dev->gadget.ops = &langwell_ops;	/* usb_gadget_ops */
	dev->gadget.ep0 = &dev->ep[0].ep;	/* gadget ep0 */
	INIT_LIST_HEAD(&dev->gadget.ep_list);	/* ep_list */
	dev->gadget.speed = USB_SPEED_UNKNOWN;	/* speed */
	dev->gadget.is_dualspeed = 1;		/* support dual speed */
#ifdef	OTG_TRANSCEIVER
	dev->gadget.is_otg = 1;			/* support otg mode */
#endif

	/* the "gadget" abstracts/virtualizes the controller */
	dev_set_name(&dev->gadget.dev, "gadget");
	dev->gadget.dev.parent = &pdev->dev;
	dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
	dev->gadget.dev.release = gadget_release;
	dev->gadget.name = driver_name;		/* gadget name */

	/* controller endpoints reinit */
	eps_reinit(dev);

#ifndef	OTG_TRANSCEIVER
	/* reset ep0 dQH and endptctrl */
	ep0_reset(dev);
#endif

	/* create dTD dma_pool resource */
	dev->dtd_pool = dma_pool_create("langwell_dtd",
			&dev->pdev->dev,
			sizeof(struct langwell_dtd),
			DTD_ALIGNMENT,
			DMA_BOUNDARY);

	if (!dev->dtd_pool) {
		retval = -ENOMEM;
		goto error;
	}

	/* done */
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
	dev_info(&dev->pdev->dev, "%s\n", driver_desc);
	dev_info(&dev->pdev->dev, "irq %d, pci mem %p\n", pdev->irq, base);
	dev_info(&dev->pdev->dev, "Driver version: " DRIVER_VERSION "\n");
	dev_info(&dev->pdev->dev, "Support (max) %d endpoints\n", dev->ep_max);
	dev_info(&dev->pdev->dev, "Device interface version: 0x%04x\n",
			dev->dciversion);
	dev_info(&dev->pdev->dev, "Controller mode: %s\n",
			dev->devcap ? "Device" : "Host");
	dev_info(&dev->pdev->dev, "Support USB LPM: %s\n",
			dev->lpm ? "Yes" : "No");

	dev_vdbg(&dev->pdev->dev,
			"After langwell_udc_probe(), print all registers:\n");
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
	print_all_registers(dev);

	the_controller = dev;

	retval = device_register(&dev->gadget.dev);
	if (retval)
		goto error;

	retval = device_create_file(&pdev->dev, &dev_attr_langwell_udc);
	if (retval)
		goto error;

3380 3381 3382 3383
	retval = device_create_file(&pdev->dev, &dev_attr_remote_wakeup);
	if (retval)
		goto error_attr1;

3384
	dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3385 3386
	return 0;

3387 3388
error_attr1:
	device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
3389 3390
error:
	if (dev) {
3391
		dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403
		langwell_udc_remove(pdev);
	}

	return retval;
}


/* device controller suspend */
static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct langwell_udc	*dev = the_controller;

3404
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3405 3406 3407 3408

	/* disable interrupt and set controller to stop state */
	langwell_udc_stop(dev);

3409
	/* disable IRQ handler */
3410 3411 3412 3413 3414 3415 3416
	if (dev->got_irq)
		free_irq(pdev->irq, dev);
	dev->got_irq = 0;

	/* save PCI state */
	pci_save_state(pdev);

3417 3418 3419 3420 3421
	spin_lock_irq(&dev->lock);
	/* stop all usb activities */
	stop_activity(dev, dev->driver);
	spin_unlock_irq(&dev->lock);

3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
	/* free dTD dma_pool and dQH */
	if (dev->dtd_pool)
		dma_pool_destroy(dev->dtd_pool);

	if (dev->ep_dqh)
		dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
			dev->ep_dqh, dev->ep_dqh_dma);

	/* release SRAM caching */
	if (dev->has_sram && dev->got_sram)
		sram_deinit(dev);

3434 3435 3436 3437
	/* set device power state */
	pci_set_power_state(pdev, PCI_D3hot);

	/* enter PHY low power suspend */
3438 3439
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 1);
3440

3441
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3442 3443 3444 3445 3446 3447 3448 3449
	return 0;
}


/* device controller resume */
static int langwell_udc_resume(struct pci_dev *pdev)
{
	struct langwell_udc	*dev = the_controller;
3450
	size_t			size;
3451

3452
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3453 3454

	/* exit PHY low power suspend */
3455 3456
	if (dev->pdev->device != 0x0829)
		langwell_phy_low_power(dev, 0);
3457 3458 3459 3460

	/* set device D0 power state */
	pci_set_power_state(pdev, PCI_D0);

3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
	/* enable SRAM caching if detected */
	if (dev->has_sram && !dev->got_sram)
		sram_init(dev);

	/* allocate device dQH memory */
	size = dev->ep_max * sizeof(struct langwell_dqh);
	dev_vdbg(&dev->pdev->dev, "orig size = %d\n", size);
	if (size < DQH_ALIGNMENT)
		size = DQH_ALIGNMENT;
	else if ((size % DQH_ALIGNMENT) != 0) {
		size += DQH_ALIGNMENT + 1;
		size &= ~(DQH_ALIGNMENT - 1);
	}
	dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
					&dev->ep_dqh_dma, GFP_KERNEL);
	if (!dev->ep_dqh) {
		dev_err(&dev->pdev->dev, "allocate dQH memory failed\n");
		return -ENOMEM;
	}
	dev->ep_dqh_size = size;
	dev_vdbg(&dev->pdev->dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);

	/* create dTD dma_pool resource */
	dev->dtd_pool = dma_pool_create("langwell_dtd",
			&dev->pdev->dev,
			sizeof(struct langwell_dtd),
			DTD_ALIGNMENT,
			DMA_BOUNDARY);

	if (!dev->dtd_pool)
		return -ENOMEM;

3493 3494 3495 3496
	/* restore PCI state */
	pci_restore_state(pdev);

	/* enable IRQ handler */
3497 3498 3499 3500 3501
	if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
				driver_name, dev) != 0) {
		dev_err(&dev->pdev->dev, "request interrupt %d failed\n",
				pdev->irq);
		return -EBUSY;
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
	}
	dev->got_irq = 1;

	/* reset and start controller to run state */
	if (dev->stopped) {
		/* reset device controller */
		langwell_udc_reset(dev);

		/* reset ep0 dQH and endptctrl */
		ep0_reset(dev);

		/* start device if gadget is loaded */
		if (dev->driver)
			langwell_udc_start(dev);
	}

	/* reset USB status */
	dev->usb_state = USB_STATE_ATTACHED;
	dev->ep0_state = WAIT_FOR_SETUP;
	dev->ep0_dir = USB_DIR_OUT;

3523
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
	return 0;
}


/* pci driver shutdown */
static void langwell_udc_shutdown(struct pci_dev *pdev)
{
	struct langwell_udc	*dev = the_controller;
	u32			usbmode;

3534
	dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
3535 3536 3537

	/* reset controller mode to IDLE */
	usbmode = readl(&dev->op_regs->usbmode);
3538
	dev_dbg(&dev->pdev->dev, "usbmode = 0x%08x\n", usbmode);
3539 3540 3541
	usbmode &= (~3 | MODE_IDLE);
	writel(usbmode, &dev->op_regs->usbmode);

3542
	dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
}

/*-------------------------------------------------------------------------*/

static const struct pci_device_id pci_ids[] = { {
	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
	.class_mask =	~0,
	.vendor =	0x8086,
	.device =	0x0811,
	.subvendor =	PCI_ANY_ID,
	.subdevice =	PCI_ANY_ID,
}, { /* end: all zeroes */ }
};

MODULE_DEVICE_TABLE(pci, pci_ids);


static struct pci_driver langwell_pci_driver = {
	.name =		(char *) driver_name,
	.id_table =	pci_ids,

	.probe =	langwell_udc_probe,
	.remove =	langwell_udc_remove,

	/* device controller suspend/resume */
	.suspend =	langwell_udc_suspend,
	.resume =	langwell_udc_resume,

	.shutdown =	langwell_udc_shutdown,
};


static int __init init(void)
{
#ifdef	OTG_TRANSCEIVER
	return langwell_register_peripheral(&langwell_pci_driver);
#else
	return pci_register_driver(&langwell_pci_driver);
#endif
}
module_init(init);


static void __exit cleanup(void)
{
#ifdef	OTG_TRANSCEIVER
	return langwell_unregister_peripheral(&langwell_pci_driver);
#else
	pci_unregister_driver(&langwell_pci_driver);
#endif
}
module_exit(cleanup);

3596 3597 3598 3599 3600 3601

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");