r8a66597-hcd.c 63.0 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * R8A66597 HCD (Host Controller Driver)
 *
 * Copyright (C) 2006-2007 Renesas Solutions Corp.
 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
 * Portions Copyright (C) 2004-2005 David Brownell
 * Portions Copyright (C) 1999 Roman Weissgaerber
 *
9
 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that 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
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/usb.h>
36
#include <linux/usb/hcd.h>
37
#include <linux/platform_device.h>
38
#include <linux/io.h>
39
#include <linux/mm.h>
40
#include <linux/irq.h>
41
#include <linux/slab.h>
42
#include <asm/cacheflush.h>
43 44 45 46 47 48

#include "r8a66597.h"

MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Yoshihiro Shimoda");
49
MODULE_ALIAS("platform:r8a66597_hcd");
50

51
#define DRIVER_VERSION	"2009-05-26"
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 80 81 82 83 84 85 86 87 88 89 90 91

static const char hcd_name[] = "r8a66597_hcd";

static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
static int r8a66597_get_frame(struct usb_hcd *hcd);

/* this function must be called with interrupt disabled */
static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
			    unsigned long reg)
{
	u16 tmp;

	tmp = r8a66597_read(r8a66597, INTENB0);
	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
	r8a66597_bset(r8a66597, 1 << pipenum, reg);
	r8a66597_write(r8a66597, tmp, INTENB0);
}

/* this function must be called with interrupt disabled */
static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
			     unsigned long reg)
{
	u16 tmp;

	tmp = r8a66597_read(r8a66597, INTENB0);
	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
	r8a66597_bclr(r8a66597, 1 << pipenum, reg);
	r8a66597_write(r8a66597, tmp, INTENB0);
}

static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
			   u16 usbspd, u8 upphub, u8 hubport, int port)
{
	u16 val;
	unsigned long devadd_reg = get_devadd_addr(r8a66597_address);

	val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
	r8a66597_write(r8a66597, val, devadd_reg);
}

92
static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
93 94 95 96
{
	u16 tmp;
	int i = 0;

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	if (r8a66597->pdata->on_chip) {
		clk_enable(r8a66597->clk);
		do {
			r8a66597_write(r8a66597, SCKE, SYSCFG0);
			tmp = r8a66597_read(r8a66597, SYSCFG0);
			if (i++ > 1000) {
				printk(KERN_ERR "r8a66597: reg access fail.\n");
				return -ENXIO;
			}
		} while ((tmp & SCKE) != SCKE);
		r8a66597_write(r8a66597, 0x04, 0x02);
	} else {
		do {
			r8a66597_write(r8a66597, USBE, SYSCFG0);
			tmp = r8a66597_read(r8a66597, SYSCFG0);
			if (i++ > 1000) {
				printk(KERN_ERR "r8a66597: reg access fail.\n");
				return -ENXIO;
			}
		} while ((tmp & USBE) != USBE);
		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
		r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
			      XTAL, SYSCFG0);
120

121 122 123 124 125 126 127 128 129 130 131
		i = 0;
		r8a66597_bset(r8a66597, XCKE, SYSCFG0);
		do {
			msleep(1);
			tmp = r8a66597_read(r8a66597, SYSCFG0);
			if (i++ > 500) {
				printk(KERN_ERR "r8a66597: reg access fail.\n");
				return -ENXIO;
			}
		} while ((tmp & SCKE) != SCKE);
	}
132 133 134 135 136 137 138 139

	return 0;
}

static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
{
	r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
	udelay(1);
140 141 142 143 144 145 146 147

	if (r8a66597->pdata->on_chip) {
		clk_disable(r8a66597->clk);
	} else {
		r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
		r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
	}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
{
	u16 val;

	val = port ? DRPD : DCFM | DRPD;
	r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
	r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));

	r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
	r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
	r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
}

static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
{
	u16 val, tmp;

	r8a66597_write(r8a66597, 0, get_intenb_reg(port));
	r8a66597_write(r8a66597, 0, get_intsts_reg(port));
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	r8a66597_port_power(r8a66597, port, 0);

	do {
		tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
		udelay(640);
	} while (tmp == EDGESTS);

	val = port ? DRPD : DCFM | DRPD;
	r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
	r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
}

static int enable_controller(struct r8a66597 *r8a66597)
{
	int ret, port;
185 186 187
	u16 vif = r8a66597->pdata->vif ? LDRV : 0;
	u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
	u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
188 189 190 191

	ret = r8a66597_clock_enable(r8a66597);
	if (ret < 0)
		return ret;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

	r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
	r8a66597_bset(r8a66597, USBE, SYSCFG0);

	r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
	r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
	r8a66597_bset(r8a66597, BRDY0, BRDYENB);
	r8a66597_bset(r8a66597, BEMP0, BEMPENB);

	r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
	r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
	r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
	r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);

	r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
207

208
	for (port = 0; port < r8a66597->max_root_hub; port++)
209
		r8a66597_enable_port(r8a66597, port);
210 211 212 213 214 215

	return 0;
}

static void disable_controller(struct r8a66597 *r8a66597)
{
216
	int port;
217

218
	/* disable interrupts */
219
	r8a66597_write(r8a66597, 0, INTENB0);
220 221 222 223 224 225 226 227 228
	r8a66597_write(r8a66597, 0, INTENB1);
	r8a66597_write(r8a66597, 0, BRDYENB);
	r8a66597_write(r8a66597, 0, BEMPENB);
	r8a66597_write(r8a66597, 0, NRDYENB);

	/* clear status */
	r8a66597_write(r8a66597, 0, BRDYSTS);
	r8a66597_write(r8a66597, 0, NRDYSTS);
	r8a66597_write(r8a66597, 0, BEMPSTS);
229

230
	for (port = 0; port < r8a66597->max_root_hub; port++)
231
		r8a66597_disable_port(r8a66597, port);
232

233
	r8a66597_clock_disable(r8a66597);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
}

static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
				       struct usb_device *udev)
{
	struct r8a66597_device *dev;

	if (udev->parent && udev->parent->devnum != 1)
		udev = udev->parent;

	dev = dev_get_drvdata(&udev->dev);
	if (dev)
		return dev->address;
	else
		return 0;
}

static int is_child_device(char *devpath)
{
	return (devpath[2] ? 1 : 0);
}

static int is_hub_limit(char *devpath)
{
	return ((strlen(devpath) >= 4) ? 1 : 0);
}

261 262
static void get_port_number(struct r8a66597 *r8a66597,
			    char *devpath, u16 *root_port, u16 *hub_port)
263 264 265
{
	if (root_port) {
		*root_port = (devpath[0] & 0x0F) - 1;
266
		if (*root_port >= r8a66597->max_root_hub)
267
			printk(KERN_ERR "r8a66597: Illegal root port number.\n");
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	}
	if (hub_port)
		*hub_port = devpath[2] & 0x0F;
}

static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
{
	u16 usbspd = 0;

	switch (speed) {
	case USB_SPEED_LOW:
		usbspd = LSMODE;
		break;
	case USB_SPEED_FULL:
		usbspd = FSMODE;
		break;
	case USB_SPEED_HIGH:
		usbspd = HSMODE;
		break;
	default:
288
		printk(KERN_ERR "r8a66597: unknown speed\n");
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		break;
	}

	return usbspd;
}

static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
{
	int idx;

	idx = address / 32;
	r8a66597->child_connect_map[idx] |= 1 << (address % 32);
}

static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
{
	int idx;

	idx = address / 32;
	r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
}

static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
{
	u16 pipenum = pipe->info.pipenum;
314 315 316
	const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
	const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
	const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
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

	if (dma_ch > R8A66597_PIPE_NO_DMA)	/* dma fifo not use? */
		dma_ch = R8A66597_PIPE_NO_DMA;

	pipe->fifoaddr = fifoaddr[dma_ch];
	pipe->fifosel = fifosel[dma_ch];
	pipe->fifoctr = fifoctr[dma_ch];

	if (pipenum == 0)
		pipe->pipectr = DCPCTR;
	else
		pipe->pipectr = get_pipectr_addr(pipenum);

	if (check_bulk_or_isoc(pipenum)) {
		pipe->pipetre = get_pipetre_addr(pipenum);
		pipe->pipetrn = get_pipetrn_addr(pipenum);
	} else {
		pipe->pipetre = 0;
		pipe->pipetrn = 0;
	}
}

static struct r8a66597_device *
get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
{
	if (usb_pipedevice(urb->pipe) == 0)
		return &r8a66597->device0;

	return dev_get_drvdata(&urb->dev->dev);
}

static int make_r8a66597_device(struct r8a66597 *r8a66597,
				struct urb *urb, u8 addr)
{
	struct r8a66597_device *dev;
	int usb_address = urb->setup_packet[2];	/* urb->pipe is address 0 */

354
	dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
355 356 357 358 359 360 361 362 363 364 365 366 367
	if (dev == NULL)
		return -ENOMEM;

	dev_set_drvdata(&urb->dev->dev, dev);
	dev->udev = urb->dev;
	dev->address = addr;
	dev->usb_address = usb_address;
	dev->state = USB_STATE_ADDRESS;
	dev->ep_in_toggle = 0;
	dev->ep_out_toggle = 0;
	INIT_LIST_HEAD(&dev->device_list);
	list_add_tail(&dev->device_list, &r8a66597->child_device);

368 369
	get_port_number(r8a66597, urb->dev->devpath,
			&dev->root_port, &dev->hub_port);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if (!is_child_device(urb->dev->devpath))
		r8a66597->root_hub[dev->root_port].dev = dev;

	set_devadd_reg(r8a66597, dev->address,
		       get_r8a66597_usb_speed(urb->dev->speed),
		       get_parent_r8a66597_address(r8a66597, urb->dev),
		       dev->hub_port, dev->root_port);

	return 0;
}

/* this function must be called with interrupt disabled */
static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
{
	u8 addr;	/* R8A66597's address */
	struct r8a66597_device *dev;

	if (is_hub_limit(urb->dev->devpath)) {
388
		dev_err(&urb->dev->dev, "External hub limit reached.\n");
389 390 391 392 393 394 395 396 397 398 399
		return 0;
	}

	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
	if (dev && dev->state >= USB_STATE_ADDRESS)
		return dev->address;

	for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
		if (r8a66597->address_map & (1 << addr))
			continue;

400
		dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
401 402 403 404 405 406 407 408
		r8a66597->address_map |= 1 << addr;

		if (make_r8a66597_device(r8a66597, urb, addr) < 0)
			return 0;

		return addr;
	}

409 410 411
	dev_err(&urb->dev->dev,
		"cannot communicate with a USB device more than 10.(%x)\n",
		r8a66597->address_map);
412 413 414 415 416 417

	return 0;
}

/* this function must be called with interrupt disabled */
static void free_usb_address(struct r8a66597 *r8a66597,
418
			     struct r8a66597_device *dev, int reset)
419 420 421 422 423 424
{
	int port;

	if (!dev)
		return;

425
	dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
426 427 428 429

	dev->state = USB_STATE_DEFAULT;
	r8a66597->address_map &= ~(1 << dev->address);
	dev->address = 0;
430 431 432 433 434 435 436
	/*
	 * Only when resetting USB, it is necessary to erase drvdata. When
	 * a usb device with usb hub is disconnect, "dev->udev" is already
	 * freed on usb_desconnect(). So we cannot access the data.
	 */
	if (reset)
		dev_set_drvdata(&dev->udev->dev, NULL);
437 438 439
	list_del(&dev->device_list);
	kfree(dev);

440
	for (port = 0; port < r8a66597->max_root_hub; port++) {
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		if (r8a66597->root_hub[port].dev == dev) {
			r8a66597->root_hub[port].dev = NULL;
			break;
		}
	}
}

static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
			      u16 mask, u16 loop)
{
	u16 tmp;
	int i = 0;

	do {
		tmp = r8a66597_read(r8a66597, reg);
		if (i++ > 1000000) {
457 458
			printk(KERN_ERR "r8a66597: register%lx, loop %x "
			       "is timeout\n", reg, loop);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
			break;
		}
		ndelay(1);
	} while ((tmp & mask) != loop);
}

/* this function must be called with interrupt disabled */
static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
{
	u16 tmp;

	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
	if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
		r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
	r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
}

/* this function must be called with interrupt disabled */
static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
{
	u16 tmp;

	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
	if ((tmp & PID_STALL11) != PID_STALL11)	/* force stall? */
		r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
	r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
	r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
}

/* this function must be called with interrupt disabled */
static void clear_all_buffer(struct r8a66597 *r8a66597,
			     struct r8a66597_pipe *pipe)
{
	u16 tmp;

	if (!pipe || pipe->info.pipenum == 0)
		return;

	pipe_stop(r8a66597, pipe);
	r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
	tmp = r8a66597_read(r8a66597, pipe->pipectr);
	tmp = r8a66597_read(r8a66597, pipe->pipectr);
	tmp = r8a66597_read(r8a66597, pipe->pipectr);
	r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
}

/* this function must be called with interrupt disabled */
static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
				 struct r8a66597_pipe *pipe, int toggle)
{
	if (toggle)
		r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
	else
		r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
}

515 516 517 518 519 520 521 522
static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
{
	if (r8a66597->pdata->on_chip)
		return MBW_32;
	else
		return MBW_16;
}

523 524 525
/* this function must be called with interrupt disabled */
static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
{
526 527 528
	unsigned short mbw = mbw_value(r8a66597);

	r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
529 530 531 532 533 534 535
	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
}

/* this function must be called with interrupt disabled */
static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
					 struct r8a66597_pipe *pipe)
{
536 537
	unsigned short mbw = mbw_value(r8a66597);

538
	cfifo_change(r8a66597, 0);
539 540
	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
541

542
	r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
		      pipe->fifosel);
	r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
}

static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
{
	struct r8a66597_pipe *pipe = hep->hcpriv;

	if (usb_pipeendpoint(urb->pipe) == 0)
		return 0;
	else
		return pipe->info.pipenum;
}

static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
{
	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);

	return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
}

static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
					  int urb_pipe)
{
	if (!dev)
		return NULL;

	return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
}

/* this function must be called with interrupt disabled */
static void pipe_toggle_set(struct r8a66597 *r8a66597,
			    struct r8a66597_pipe *pipe,
			    struct urb *urb, int set)
{
	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);

	if (!toggle)
		return;

	if (set)
		*toggle |= 1 << endpoint;
	else
		*toggle &= ~(1 << endpoint);
}

/* this function must be called with interrupt disabled */
static void pipe_toggle_save(struct r8a66597 *r8a66597,
			     struct r8a66597_pipe *pipe,
			     struct urb *urb)
{
	if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
		pipe_toggle_set(r8a66597, pipe, urb, 1);
	else
		pipe_toggle_set(r8a66597, pipe, urb, 0);
}

/* this function must be called with interrupt disabled */
static void pipe_toggle_restore(struct r8a66597 *r8a66597,
				struct r8a66597_pipe *pipe,
				struct urb *urb)
{
	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);

611 612 613
	if (!toggle)
		return;

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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
}

/* this function must be called with interrupt disabled */
static void pipe_buffer_setting(struct r8a66597 *r8a66597,
				struct r8a66597_pipe_info *info)
{
	u16 val = 0;

	if (info->pipenum == 0)
		return;

	r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
	r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
	r8a66597_write(r8a66597, info->pipenum, PIPESEL);
	if (!info->dir_in)
		val |= R8A66597_DIR;
	if (info->type == R8A66597_BULK && info->dir_in)
		val |= R8A66597_DBLB | R8A66597_SHTNAK;
	val |= info->type | info->epnum;
	r8a66597_write(r8a66597, val, PIPECFG);

	r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
		       PIPEBUF);
	r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
		       PIPEMAXP);
	r8a66597_write(r8a66597, info->interval, PIPEPERI);
}

/* this function must be called with interrupt disabled */
static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
{
	struct r8a66597_pipe_info *info;
	struct urb *urb = td->urb;

	if (td->pipenum > 0) {
		info = &td->pipe->info;
		cfifo_change(r8a66597, 0);
		pipe_buffer_setting(r8a66597, info);

		if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
				   usb_pipeout(urb->pipe)) &&
		    !usb_pipecontrol(urb->pipe)) {
			r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
			pipe_toggle_set(r8a66597, td->pipe, urb, 0);
			clear_all_buffer(r8a66597, td->pipe);
			usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
				      usb_pipeout(urb->pipe), 1);
		}
		pipe_toggle_restore(r8a66597, td->pipe, urb);
	}
}

/* this function must be called with interrupt disabled */
static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
			     struct usb_endpoint_descriptor *ep)
{
	u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;

	memset(array, 0, sizeof(array));
674
	switch (usb_endpoint_type(ep)) {
675
	case USB_ENDPOINT_XFER_BULK:
676
		if (usb_endpoint_dir_in(ep))
677 678 679 680 681
			array[i++] = 4;
		else {
			array[i++] = 3;
			array[i++] = 5;
		}
682 683
		break;
	case USB_ENDPOINT_XFER_INT:
684
		if (usb_endpoint_dir_in(ep)) {
685 686 687 688 689
			array[i++] = 6;
			array[i++] = 7;
			array[i++] = 8;
		} else
			array[i++] = 9;
690 691
		break;
	case USB_ENDPOINT_XFER_ISOC:
692
		if (usb_endpoint_dir_in(ep))
693 694 695
			array[i++] = 2;
		else
			array[i++] = 1;
696 697
		break;
	default:
698
		printk(KERN_ERR "r8a66597: Illegal type\n");
699 700
		return 0;
	}
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

	i = 1;
	min = array[0];
	while (array[i] != 0) {
		if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
			min = array[i];
		i++;
	}

	return min;
}

static u16 get_r8a66597_type(__u8 type)
{
	u16 r8a66597_type;

717
	switch (type) {
718 719 720 721 722 723 724 725 726 727
	case USB_ENDPOINT_XFER_BULK:
		r8a66597_type = R8A66597_BULK;
		break;
	case USB_ENDPOINT_XFER_INT:
		r8a66597_type = R8A66597_INT;
		break;
	case USB_ENDPOINT_XFER_ISOC:
		r8a66597_type = R8A66597_ISO;
		break;
	default:
728
		printk(KERN_ERR "r8a66597: Illegal type\n");
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		r8a66597_type = 0x0000;
		break;
	}

	return r8a66597_type;
}

static u16 get_bufnum(u16 pipenum)
{
	u16 bufnum = 0;

	if (pipenum == 0)
		bufnum = 0;
	else if (check_bulk_or_isoc(pipenum))
		bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
	else if (check_interrupt(pipenum))
		bufnum = 4 + (pipenum - 6);
	else
747
		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

	return bufnum;
}

static u16 get_buf_bsize(u16 pipenum)
{
	u16 buf_bsize = 0;

	if (pipenum == 0)
		buf_bsize = 3;
	else if (check_bulk_or_isoc(pipenum))
		buf_bsize = R8A66597_BUF_BSIZE - 1;
	else if (check_interrupt(pipenum))
		buf_bsize = 0;
	else
763
		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
764 765 766 767 768 769 770 771 772 773 774 775

	return buf_bsize;
}

/* this function must be called with interrupt disabled */
static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
				     struct r8a66597_device *dev,
				     struct r8a66597_pipe *pipe,
				     struct urb *urb)
{
	int i;
	struct r8a66597_pipe_info *info = &pipe->info;
776 777 778 779 780
	unsigned short mbw = mbw_value(r8a66597);

	/* pipe dma is only for external controlles */
	if (r8a66597->pdata->on_chip)
		return;
781 782 783 784 785 786

	if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
		for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
			if ((r8a66597->dma_map & (1 << i)) != 0)
				continue;

787 788 789 790 791 792
			dev_info(&dev->udev->dev,
				 "address %d, EndpointAddress 0x%02x use "
				 "DMA FIFO\n", usb_pipedevice(urb->pipe),
				 info->dir_in ?
				 	USB_ENDPOINT_DIR_MASK + info->epnum
					: info->epnum);
793 794 795 796 797 798

			r8a66597->dma_map |= 1 << i;
			dev->dma_map |= 1 << i;
			set_pipe_reg_addr(pipe, i);

			cfifo_change(r8a66597, 0);
799 800
			r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
				      mbw | CURPIPE, pipe->fifosel);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

			r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
					  pipe->info.pipenum);
			r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
			break;
		}
	}
}

/* this function must be called with interrupt disabled */
static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
				 struct usb_host_endpoint *hep,
				 struct r8a66597_pipe_info *info)
{
	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
	struct r8a66597_pipe *pipe = hep->hcpriv;

818
	dev_dbg(&dev->udev->dev, "enable_pipe:\n");
819 820 821 822 823 824 825 826 827

	pipe->info = *info;
	set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
	r8a66597->pipe_cnt[pipe->info.pipenum]++;
	dev->pipe_cnt[pipe->info.pipenum]++;

	enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
}

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
			      int status)
__releases(r8a66597->lock)
__acquires(r8a66597->lock)
{
	if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
		void *ptr;

		for (ptr = urb->transfer_buffer;
		     ptr < urb->transfer_buffer + urb->transfer_buffer_length;
		     ptr += PAGE_SIZE)
			flush_dcache_page(virt_to_page(ptr));
	}

	usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
	spin_unlock(&r8a66597->lock);
	usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
	spin_lock(&r8a66597->lock);
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
/* this function must be called with interrupt disabled */
static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
{
	struct r8a66597_td *td, *next;
	struct urb *urb;
	struct list_head *list = &r8a66597->pipe_queue[pipenum];

	if (list_empty(list))
		return;

	list_for_each_entry_safe(td, next, list, queue) {
		if (td->address != address)
			continue;

		urb = td->urb;
		list_del(&td->queue);
		kfree(td);

866 867
		if (urb)
			r8a66597_urb_done(r8a66597, urb, -ENODEV);
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
		break;
	}
}

/* this function must be called with interrupt disabled */
static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
				      struct r8a66597_device *dev)
{
	int check_ep0 = 0;
	u16 pipenum;

	if (!dev)
		return;

	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		if (!dev->pipe_cnt[pipenum])
			continue;

		if (!check_ep0) {
			check_ep0 = 1;
			force_dequeue(r8a66597, 0, dev->address);
		}

		r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
		dev->pipe_cnt[pipenum] = 0;
		force_dequeue(r8a66597, pipenum, dev->address);
	}

897
	dev_dbg(&dev->udev->dev, "disable_pipe\n");
898 899 900 901 902

	r8a66597->dma_map &= ~(dev->dma_map);
	dev->dma_map = 0;
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
static u16 get_interval(struct urb *urb, __u8 interval)
{
	u16 time = 1;
	int i;

	if (urb->dev->speed == USB_SPEED_HIGH) {
		if (interval > IITV)
			time = IITV;
		else
			time = interval ? interval - 1 : 0;
	} else {
		if (interval > 128) {
			time = IITV;
		} else {
			/* calculate the nearest value for PIPEPERI */
			for (i = 0; i < 7; i++) {
				if ((1 << i) < interval &&
				    (1 << (i + 1) > interval))
					time = 1 << i;
			}
		}
	}

	return time;
}

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
{
	__u8 i;
	unsigned long time = 1;

	if (usb_pipeisoc(urb->pipe))
		return 0;

	if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
		for (i = 0; i < (interval - 1); i++)
			time *= 2;
		time = time * 125 / 1000;	/* uSOF -> msec */
	} else {
		time = interval;
	}

	return time;
}

948 949 950 951 952 953 954 955 956
/* this function must be called with interrupt disabled */
static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
			   struct usb_host_endpoint *hep,
			   struct usb_endpoint_descriptor *ep)
{
	struct r8a66597_pipe_info info;

	info.pipenum = get_empty_pipenum(r8a66597, ep);
	info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
957
	info.epnum = usb_endpoint_num(ep);
958
	info.maxpacket = usb_endpoint_maxp(ep);
959
	info.type = get_r8a66597_type(usb_endpoint_type(ep));
960 961
	info.bufnum = get_bufnum(info.pipenum);
	info.buf_bsize = get_buf_bsize(info.pipenum);
962 963 964 965
	if (info.type == R8A66597_BULK) {
		info.interval = 0;
		info.timer_interval = 0;
	} else {
966
		info.interval = get_interval(urb, ep->bInterval);
967 968
		info.timer_interval = get_timer_interval(urb, ep->bInterval);
	}
969
	if (usb_endpoint_dir_in(ep))
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
		info.dir_in = 1;
	else
		info.dir_in = 0;

	enable_r8a66597_pipe(r8a66597, urb, hep, &info);
}

static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
{
	struct r8a66597_device *dev;

	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
	dev->state = USB_STATE_CONFIGURED;
}

static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
			    u16 pipenum)
{
	if (pipenum == 0 && usb_pipeout(urb->pipe))
		enable_irq_empty(r8a66597, pipenum);
	else
		enable_irq_ready(r8a66597, pipenum);

	if (!usb_pipeisoc(urb->pipe))
		enable_irq_nrdy(r8a66597, pipenum);
}

static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
{
	disable_irq_ready(r8a66597, pipenum);
	disable_irq_nrdy(r8a66597, pipenum);
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
{
	mod_timer(&r8a66597->rh_timer,
			jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
}

static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
					int connect)
{
	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];

	rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
	rh->scount = R8A66597_MAX_SAMPLING;
	if (connect)
1017
		rh->port |= USB_PORT_STAT_CONNECTION;
1018
	else
1019 1020
		rh->port &= ~USB_PORT_STAT_CONNECTION;
	rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
1021 1022 1023 1024

	r8a66597_root_hub_start_polling(r8a66597);
}

1025
/* this function must be called with interrupt disabled */
1026 1027
static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
					u16 syssts)
1028 1029
__releases(r8a66597->lock)
__acquires(r8a66597->lock)
1030
{
1031
	if (syssts == SE0) {
1032
		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1033
		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1034 1035 1036 1037 1038
	} else {
		if (syssts == FS_JSTS)
			r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
		else if (syssts == LS_JSTS)
			r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1039

1040 1041
		r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
		r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1042

1043 1044 1045
		if (r8a66597->bus_suspended)
			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
	}
1046

1047
	spin_unlock(&r8a66597->lock);
1048
	usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1049
	spin_lock(&r8a66597->lock);
1050 1051 1052 1053 1054 1055 1056 1057
}

/* this function must be called with interrupt disabled */
static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
{
	u16 speed = get_rh_usb_speed(r8a66597, port);
	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];

1058
	rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
1059
	if (speed == HSMODE)
1060
		rh->port |= USB_PORT_STAT_HIGH_SPEED;
1061
	else if (speed == LSMODE)
1062
		rh->port |= USB_PORT_STAT_LOW_SPEED;
1063

1064
	rh->port &= ~USB_PORT_STAT_RESET;
1065
	rh->port |= USB_PORT_STAT_ENABLE;
1066 1067 1068 1069 1070 1071 1072 1073
}

/* this function must be called with interrupt disabled */
static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
{
	struct r8a66597_device *dev = r8a66597->root_hub[port].dev;

	disable_r8a66597_pipe_all(r8a66597, dev);
1074
	free_usb_address(r8a66597, dev, 0);
1075

1076
	start_root_hub_sampling(r8a66597, port, 0);
1077 1078 1079 1080 1081 1082 1083
}

/* this function must be called with interrupt disabled */
static void prepare_setup_packet(struct r8a66597 *r8a66597,
				 struct r8a66597_td *td)
{
	int i;
A
Al Viro 已提交
1084
	__le16 *p = (__le16 *)td->urb->setup_packet;
1085 1086 1087 1088
	unsigned long setup_addr = USBREQ;

	r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
		       DCPMAXP);
1089
	r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1090 1091

	for (i = 0; i < 4; i++) {
A
Al Viro 已提交
1092
		r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
		setup_addr += 2;
	}
	r8a66597_write(r8a66597, SUREQ, DCPCTR);
}

/* this function must be called with interrupt disabled */
static void prepare_packet_read(struct r8a66597 *r8a66597,
				struct r8a66597_td *td)
{
	struct urb *urb = td->urb;

	if (usb_pipecontrol(urb->pipe)) {
		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
		if (urb->actual_length == 0) {
			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
		}
		pipe_irq_disable(r8a66597, td->pipenum);
		pipe_start(r8a66597, td->pipe);
		pipe_irq_enable(r8a66597, urb, td->pipenum);
	} else {
		if (urb->actual_length == 0) {
			pipe_irq_disable(r8a66597, td->pipenum);
			pipe_setting(r8a66597, td);
			pipe_stop(r8a66597, td->pipe);
1120
			r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1121 1122 1123

			if (td->pipe->pipetre) {
				r8a66597_write(r8a66597, TRCLR,
1124
						td->pipe->pipetre);
1125
				r8a66597_write(r8a66597,
J
Julia Lawall 已提交
1126 1127 1128
						DIV_ROUND_UP
						  (urb->transfer_buffer_length,
						   td->maxpacket),
1129
						td->pipe->pipetrn);
1130
				r8a66597_bset(r8a66597, TRENB,
1131
						td->pipe->pipetre);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
			}

			pipe_start(r8a66597, td->pipe);
			pipe_irq_enable(r8a66597, urb, td->pipenum);
		}
	}
}

/* this function must be called with interrupt disabled */
static void prepare_packet_write(struct r8a66597 *r8a66597,
				 struct r8a66597_td *td)
{
	u16 tmp;
	struct urb *urb = td->urb;

	if (usb_pipecontrol(urb->pipe)) {
		pipe_stop(r8a66597, td->pipe);
		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
		if (urb->actual_length == 0) {
			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
		}
	} else {
		if (urb->actual_length == 0)
			pipe_setting(r8a66597, td);
		if (td->pipe->pipetre)
			r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
	}
1162
	r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

	fifo_change_from_pipe(r8a66597, td->pipe);
	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
	if (unlikely((tmp & FRDY) == 0))
		pipe_irq_enable(r8a66597, urb, td->pipenum);
	else
		packet_write(r8a66597, td->pipenum);
	pipe_start(r8a66597, td->pipe);
}

/* this function must be called with interrupt disabled */
static void prepare_status_packet(struct r8a66597 *r8a66597,
				  struct r8a66597_td *td)
{
	struct urb *urb = td->urb;

	r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1180
	pipe_stop(r8a66597, td->pipe);
1181 1182 1183 1184 1185

	if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1186
		r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1187
		r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		enable_irq_empty(r8a66597, 0);
	} else {
		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
		r8a66597_write(r8a66597, BCLR, CFIFOCTR);
		enable_irq_ready(r8a66597, 0);
	}
	enable_irq_nrdy(r8a66597, 0);
	pipe_start(r8a66597, td->pipe);
}

1200 1201 1202 1203 1204 1205 1206 1207 1208
static int is_set_address(unsigned char *setup_packet)
{
	if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
			setup_packet[1] == USB_REQ_SET_ADDRESS)
		return 1;
	else
		return 0;
}

1209 1210 1211 1212 1213 1214 1215
/* this function must be called with interrupt disabled */
static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
{
	BUG_ON(!td);

	switch (td->type) {
	case USB_PID_SETUP:
1216
		if (is_set_address(td->urb->setup_packet)) {
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
			td->set_address = 1;
			td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
								     td->urb);
			if (td->urb->setup_packet[2] == 0)
				return -EPIPE;
		}
		prepare_setup_packet(r8a66597, td);
		break;
	case USB_PID_IN:
		prepare_packet_read(r8a66597, td);
		break;
	case USB_PID_OUT:
		prepare_packet_write(r8a66597, td);
		break;
	case USB_PID_ACK:
		prepare_status_packet(r8a66597, td);
		break;
	default:
1235
		printk(KERN_ERR "r8a66597: invalid type.\n");
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
		break;
	}

	return 0;
}

static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
{
	if (usb_pipeisoc(urb->pipe)) {
		if (urb->number_of_packets == td->iso_cnt)
			return 1;
	}

	/* control or bulk or interrupt */
	if ((urb->transfer_buffer_length <= urb->actual_length) ||
	    (td->short_packet) || (td->zero_packet))
		return 1;

	return 0;
}

/* this function must be called with interrupt disabled */
static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
{
	unsigned long time;

	BUG_ON(!td);

	if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
	    !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
		r8a66597->timeout_map |= 1 << td->pipenum;
		switch (usb_pipetype(td->urb->pipe)) {
		case PIPE_INTERRUPT:
		case PIPE_ISOCHRONOUS:
			time = 30;
			break;
		default:
			time = 300;
			break;
		}

		mod_timer(&r8a66597->td_timer[td->pipenum],
			  jiffies + msecs_to_jiffies(time));
	}
}

/* this function must be called with interrupt disabled */
A
Alan Stern 已提交
1283
static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1284
		u16 pipenum, struct urb *urb, int status)
A
Alan Stern 已提交
1285
__releases(r8a66597->lock) __acquires(r8a66597->lock)
1286 1287 1288 1289 1290 1291 1292
{
	int restart = 0;
	struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);

	r8a66597->timeout_map &= ~(1 << pipenum);

	if (likely(td)) {
1293
		if (td->set_address && (status != 0 || urb->unlinked))
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
			r8a66597->address_map &= ~(1 << urb->setup_packet[2]);

		pipe_toggle_save(r8a66597, td->pipe, urb);
		list_del(&td->queue);
		kfree(td);
	}

	if (!list_empty(&r8a66597->pipe_queue[pipenum]))
		restart = 1;

	if (likely(urb)) {
		if (usb_pipeisoc(urb->pipe))
			urb->start_frame = r8a66597_get_frame(hcd);

1308
		r8a66597_urb_done(r8a66597, urb, status);
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	}

	if (restart) {
		td = r8a66597_get_td(r8a66597, pipenum);
		if (unlikely(!td))
			return;

		start_transfer(r8a66597, td);
		set_td_timer(r8a66597, td);
	}
}

static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
{
	u16 tmp;
	int rcv_len, bufsize, urb_len, size;
	u16 *buf;
	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
	struct urb *urb;
	int finish = 0;
A
Alan Stern 已提交
1329
	int status = 0;
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

	if (unlikely(!td))
		return;
	urb = td->urb;

	fifo_change_from_pipe(r8a66597, td->pipe);
	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
	if (unlikely((tmp & FRDY) == 0)) {
		pipe_stop(r8a66597, td->pipe);
		pipe_irq_disable(r8a66597, pipenum);
1340
		printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1341
		finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
		return;
	}

	/* prepare parameters */
	rcv_len = tmp & DTLN;
	if (usb_pipeisoc(urb->pipe)) {
		buf = (u16 *)(urb->transfer_buffer +
				urb->iso_frame_desc[td->iso_cnt].offset);
		urb_len = urb->iso_frame_desc[td->iso_cnt].length;
	} else {
		buf = (void *)urb->transfer_buffer + urb->actual_length;
		urb_len = urb->transfer_buffer_length - urb->actual_length;
	}
A
Alan Stern 已提交
1355 1356 1357 1358 1359 1360 1361 1362
	bufsize = min(urb_len, (int) td->maxpacket);
	if (rcv_len <= bufsize) {
		size = rcv_len;
	} else {
		size = bufsize;
		status = -EOVERFLOW;
		finish = 1;
	}
1363 1364 1365 1366 1367

	/* update parameters */
	urb->actual_length += size;
	if (rcv_len == 0)
		td->zero_packet = 1;
A
Alan Stern 已提交
1368
	if (rcv_len < bufsize) {
1369 1370 1371 1372
		td->short_packet = 1;
	}
	if (usb_pipeisoc(urb->pipe)) {
		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
A
Alan Stern 已提交
1373
		urb->iso_frame_desc[td->iso_cnt].status = status;
1374
		td->iso_cnt++;
A
Alan Stern 已提交
1375
		finish = 0;
1376 1377 1378
	}

	/* check transfer finish */
A
Alan Stern 已提交
1379
	if (finish || check_transfer_finish(td, urb)) {
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
		pipe_stop(r8a66597, td->pipe);
		pipe_irq_disable(r8a66597, pipenum);
		finish = 1;
	}

	/* read fifo */
	if (urb->transfer_buffer) {
		if (size == 0)
			r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
		else
			r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
					   buf, size);
	}

1394 1395
	if (finish && pipenum != 0)
		finish_request(r8a66597, td, pipenum, urb, status);
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
}

static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
{
	u16 tmp;
	int bufsize, size;
	u16 *buf;
	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
	struct urb *urb;

	if (unlikely(!td))
		return;
	urb = td->urb;

	fifo_change_from_pipe(r8a66597, td->pipe);
	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
	if (unlikely((tmp & FRDY) == 0)) {
		pipe_stop(r8a66597, td->pipe);
		pipe_irq_disable(r8a66597, pipenum);
1415
		printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1416
		finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
		return;
	}

	/* prepare parameters */
	bufsize = td->maxpacket;
	if (usb_pipeisoc(urb->pipe)) {
		buf = (u16 *)(urb->transfer_buffer +
				urb->iso_frame_desc[td->iso_cnt].offset);
		size = min(bufsize,
			   (int)urb->iso_frame_desc[td->iso_cnt].length);
	} else {
		buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1429
		size = min_t(u32, bufsize,
1430 1431 1432 1433 1434
			   urb->transfer_buffer_length - urb->actual_length);
	}

	/* write fifo */
	if (pipenum > 0)
1435
		r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1436
	if (urb->transfer_buffer) {
1437
		r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
		if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
			r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
	}

	/* update parameters */
	urb->actual_length += size;
	if (usb_pipeisoc(urb->pipe)) {
		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
		urb->iso_frame_desc[td->iso_cnt].status = 0;
		td->iso_cnt++;
	}

	/* check transfer finish */
	if (check_transfer_finish(td, urb)) {
		disable_irq_ready(r8a66597, pipenum);
		enable_irq_empty(r8a66597, pipenum);
		if (!usb_pipeisoc(urb->pipe))
			enable_irq_nrdy(r8a66597, pipenum);
	} else
		pipe_irq_enable(r8a66597, urb, pipenum);
}


1461
static void check_next_phase(struct r8a66597 *r8a66597, int status)
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
{
	struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
	struct urb *urb;
	u8 finish = 0;

	if (unlikely(!td))
		return;
	urb = td->urb;

	switch (td->type) {
	case USB_PID_IN:
	case USB_PID_OUT:
		if (check_transfer_finish(td, urb))
			td->type = USB_PID_ACK;
		break;
	case USB_PID_SETUP:
A
Alan Stern 已提交
1478
		if (urb->transfer_buffer_length == urb->actual_length)
1479
			td->type = USB_PID_ACK;
A
Alan Stern 已提交
1480
		else if (usb_pipeout(urb->pipe))
1481 1482 1483 1484 1485 1486 1487 1488 1489
			td->type = USB_PID_OUT;
		else
			td->type = USB_PID_IN;
		break;
	case USB_PID_ACK:
		finish = 1;
		break;
	}

1490 1491
	if (finish || status != 0 || urb->unlinked)
		finish_request(r8a66597, td, 0, urb, status);
1492 1493 1494 1495
	else
		start_transfer(r8a66597, td);
}

1496
static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1497 1498 1499
{
	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);

1500
	if (td) {
1501 1502 1503
		u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;

		if (pid == PID_NAK)
1504
			return -ECONNRESET;
1505
		else
1506
			return -EPIPE;
1507
	}
1508
	return 0;
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
}

static void irq_pipe_ready(struct r8a66597 *r8a66597)
{
	u16 check;
	u16 pipenum;
	u16 mask;
	struct r8a66597_td *td;

	mask = r8a66597_read(r8a66597, BRDYSTS)
	       & r8a66597_read(r8a66597, BRDYENB);
1520
	r8a66597_write(r8a66597, ~mask, BRDYSTS);
1521 1522 1523 1524 1525 1526
	if (mask & BRDY0) {
		td = r8a66597_get_td(r8a66597, 0);
		if (td && td->type == USB_PID_IN)
			packet_read(r8a66597, 0);
		else
			pipe_irq_disable(r8a66597, 0);
1527
		check_next_phase(r8a66597, 0);
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	}

	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		check = 1 << pipenum;
		if (mask & check) {
			td = r8a66597_get_td(r8a66597, pipenum);
			if (unlikely(!td))
				continue;

			if (td->type == USB_PID_IN)
				packet_read(r8a66597, pipenum);
			else if (td->type == USB_PID_OUT)
				packet_write(r8a66597, pipenum);
		}
	}
}

static void irq_pipe_empty(struct r8a66597 *r8a66597)
{
	u16 tmp;
	u16 check;
	u16 pipenum;
	u16 mask;
	struct r8a66597_td *td;

	mask = r8a66597_read(r8a66597, BEMPSTS)
	       & r8a66597_read(r8a66597, BEMPENB);
1555
	r8a66597_write(r8a66597, ~mask, BEMPSTS);
1556 1557 1558 1559 1560
	if (mask & BEMP0) {
		cfifo_change(r8a66597, 0);
		td = r8a66597_get_td(r8a66597, 0);
		if (td && td->type != USB_PID_OUT)
			disable_irq_empty(r8a66597, 0);
1561
		check_next_phase(r8a66597, 0);
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	}

	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		check = 1 << pipenum;
		if (mask &  check) {
			struct r8a66597_td *td;
			td = r8a66597_get_td(r8a66597, pipenum);
			if (unlikely(!td))
				continue;

			tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
			if ((tmp & INBUFM) == 0) {
				disable_irq_empty(r8a66597, pipenum);
				pipe_irq_disable(r8a66597, pipenum);
1576 1577
				finish_request(r8a66597, td, pipenum, td->urb,
						0);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
			}
		}
	}
}

static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
{
	u16 check;
	u16 pipenum;
	u16 mask;
1588
	int status;
1589 1590 1591

	mask = r8a66597_read(r8a66597, NRDYSTS)
	       & r8a66597_read(r8a66597, NRDYENB);
1592
	r8a66597_write(r8a66597, ~mask, NRDYSTS);
1593 1594
	if (mask & NRDY0) {
		cfifo_change(r8a66597, 0);
1595
		status = get_urb_error(r8a66597, 0);
1596
		pipe_irq_disable(r8a66597, 0);
1597
		check_next_phase(r8a66597, status);
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	}

	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		check = 1 << pipenum;
		if (mask & check) {
			struct r8a66597_td *td;
			td = r8a66597_get_td(r8a66597, pipenum);
			if (unlikely(!td))
				continue;

1608
			status = get_urb_error(r8a66597, pipenum);
1609 1610
			pipe_irq_disable(r8a66597, pipenum);
			pipe_stop(r8a66597, td->pipe);
1611
			finish_request(r8a66597, td, pipenum, td->urb, status);
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
		}
	}
}

static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	u16 intsts0, intsts1, intsts2;
	u16 intenb0, intenb1, intenb2;
	u16 mask0, mask1, mask2;
1622
	int status;
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

	spin_lock(&r8a66597->lock);

	intsts0 = r8a66597_read(r8a66597, INTSTS0);
	intsts1 = r8a66597_read(r8a66597, INTSTS1);
	intsts2 = r8a66597_read(r8a66597, INTSTS2);
	intenb0 = r8a66597_read(r8a66597, INTENB0);
	intenb1 = r8a66597_read(r8a66597, INTENB1);
	intenb2 = r8a66597_read(r8a66597, INTENB2);

	mask2 = intsts2 & intenb2;
	mask1 = intsts1 & intenb1;
	mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
	if (mask2) {
		if (mask2 & ATTCH) {
1638
			r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1639 1640 1641
			r8a66597_bclr(r8a66597, ATTCHE, INTENB2);

			/* start usb bus sampling */
1642
			start_root_hub_sampling(r8a66597, 1, 1);
1643 1644
		}
		if (mask2 & DTCH) {
1645
			r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1646 1647 1648
			r8a66597_bclr(r8a66597, DTCHE, INTENB2);
			r8a66597_usb_disconnect(r8a66597, 1);
		}
1649 1650 1651 1652 1653
		if (mask2 & BCHG) {
			r8a66597_write(r8a66597, ~BCHG, INTSTS2);
			r8a66597_bclr(r8a66597, BCHGE, INTENB2);
			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
		}
1654 1655 1656 1657
	}

	if (mask1) {
		if (mask1 & ATTCH) {
1658
			r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1659 1660 1661
			r8a66597_bclr(r8a66597, ATTCHE, INTENB1);

			/* start usb bus sampling */
1662
			start_root_hub_sampling(r8a66597, 0, 1);
1663 1664
		}
		if (mask1 & DTCH) {
1665
			r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1666 1667 1668
			r8a66597_bclr(r8a66597, DTCHE, INTENB1);
			r8a66597_usb_disconnect(r8a66597, 0);
		}
1669 1670 1671 1672 1673 1674
		if (mask1 & BCHG) {
			r8a66597_write(r8a66597, ~BCHG, INTSTS1);
			r8a66597_bclr(r8a66597, BCHGE, INTENB1);
			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
		}

1675
		if (mask1 & SIGN) {
1676
			r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1677 1678
			status = get_urb_error(r8a66597, 0);
			check_next_phase(r8a66597, status);
1679 1680
		}
		if (mask1 & SACK) {
1681
			r8a66597_write(r8a66597, ~SACK, INTSTS1);
1682
			check_next_phase(r8a66597, 0);
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
		}
	}
	if (mask0) {
		if (mask0 & BRDY)
			irq_pipe_ready(r8a66597);
		if (mask0 & BEMP)
			irq_pipe_empty(r8a66597);
		if (mask0 & NRDY)
			irq_pipe_nrdy(r8a66597);
	}

	spin_unlock(&r8a66597->lock);
	return IRQ_HANDLED;
}

/* this function must be called with interrupt disabled */
static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
{
	u16 tmp;
	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];

1704
	if (rh->port & USB_PORT_STAT_RESET) {
1705 1706 1707 1708 1709 1710
		unsigned long dvstctr_reg = get_dvstctr_reg(port);

		tmp = r8a66597_read(r8a66597, dvstctr_reg);
		if ((tmp & USBRST) == USBRST) {
			r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
				      dvstctr_reg);
1711
			r8a66597_root_hub_start_polling(r8a66597);
1712 1713 1714 1715
		} else
			r8a66597_usb_connect(r8a66597, port);
	}

1716
	if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
1717 1718 1719 1720
		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
	}

1721 1722 1723 1724
	if (rh->scount > 0) {
		tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
		if (tmp == rh->old_syssts) {
			rh->scount--;
1725 1726 1727 1728
			if (rh->scount == 0)
				r8a66597_check_syssts(r8a66597, port, tmp);
			else
				r8a66597_root_hub_start_polling(r8a66597);
1729 1730 1731
		} else {
			rh->scount = R8A66597_MAX_SAMPLING;
			rh->old_syssts = tmp;
1732
			r8a66597_root_hub_start_polling(r8a66597);
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
static void r8a66597_interval_timer(unsigned long _r8a66597)
{
	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
	unsigned long flags;
	u16 pipenum;
	struct r8a66597_td *td;

	spin_lock_irqsave(&r8a66597->lock, flags);

	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		if (!(r8a66597->interval_map & (1 << pipenum)))
			continue;
		if (timer_pending(&r8a66597->interval_timer[pipenum]))
			continue;

		td = r8a66597_get_td(r8a66597, pipenum);
		if (td)
			start_transfer(r8a66597, td);
	}

	spin_unlock_irqrestore(&r8a66597->lock, flags);
}

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
static void r8a66597_td_timer(unsigned long _r8a66597)
{
	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
	unsigned long flags;
	u16 pipenum;
	struct r8a66597_td *td, *new_td = NULL;
	struct r8a66597_pipe *pipe;

	spin_lock_irqsave(&r8a66597->lock, flags);
	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
		if (!(r8a66597->timeout_map & (1 << pipenum)))
			continue;
		if (timer_pending(&r8a66597->td_timer[pipenum]))
			continue;

		td = r8a66597_get_td(r8a66597, pipenum);
		if (!td) {
			r8a66597->timeout_map &= ~(1 << pipenum);
			continue;
		}

		if (td->urb->actual_length) {
			set_td_timer(r8a66597, td);
			break;
		}

		pipe = td->pipe;
		pipe_stop(r8a66597, pipe);

		new_td = td;
		do {
			list_move_tail(&new_td->queue,
				       &r8a66597->pipe_queue[pipenum]);
			new_td = r8a66597_get_td(r8a66597, pipenum);
			if (!new_td) {
				new_td = td;
				break;
			}
		} while (td != new_td && td->address == new_td->address);

		start_transfer(r8a66597, new_td);

		if (td == new_td)
			r8a66597->timeout_map &= ~(1 << pipenum);
		else
			set_td_timer(r8a66597, new_td);
		break;
	}
	spin_unlock_irqrestore(&r8a66597->lock, flags);
}

static void r8a66597_timer(unsigned long _r8a66597)
{
	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
	unsigned long flags;
1815
	int port;
1816 1817 1818

	spin_lock_irqsave(&r8a66597->lock, flags);

1819
	for (port = 0; port < r8a66597->max_root_hub; port++)
1820
		r8a66597_root_hub_control(r8a66597, port);
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840

	spin_unlock_irqrestore(&r8a66597->lock, flags);
}

static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
{
	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);

	if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
	    (urb->dev->state == USB_STATE_CONFIGURED))
		return 1;
	else
		return 0;
}

static int r8a66597_start(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);

	hcd->state = HC_STATE_RUNNING;
1841
	return enable_controller(r8a66597);
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
}

static void r8a66597_stop(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);

	disable_controller(r8a66597);
}

static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
{
	unsigned int usb_address = usb_pipedevice(urb->pipe);
	u16 root_port, hub_port;

	if (usb_address == 0) {
1857
		get_port_number(r8a66597, urb->dev->devpath,
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
				&root_port, &hub_port);
		set_devadd_reg(r8a66597, 0,
			       get_r8a66597_usb_speed(urb->dev->speed),
			       get_parent_r8a66597_address(r8a66597, urb->dev),
			       hub_port, root_port);
	}
}

static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
					    struct urb *urb,
1868
					    struct usb_host_endpoint *hep)
1869 1870 1871 1872
{
	struct r8a66597_td *td;
	u16 pipenum;

1873
	td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
	if (td == NULL)
		return NULL;

	pipenum = r8a66597_get_pipenum(urb, hep);
	td->pipenum = pipenum;
	td->pipe = hep->hcpriv;
	td->urb = urb;
	td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
	td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
				      !usb_pipein(urb->pipe));
	if (usb_pipecontrol(urb->pipe))
		td->type = USB_PID_SETUP;
	else if (usb_pipein(urb->pipe))
		td->type = USB_PID_IN;
	else
		td->type = USB_PID_OUT;
	INIT_LIST_HEAD(&td->queue);

	return td;
}

static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
				struct urb *urb,
				gfp_t mem_flags)
{
1899
	struct usb_host_endpoint *hep = urb->ep;
1900 1901
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	struct r8a66597_td *td = NULL;
1902
	int ret, request = 0;
1903 1904 1905 1906 1907
	unsigned long flags;

	spin_lock_irqsave(&r8a66597->lock, flags);
	if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
		ret = -ENODEV;
1908
		goto error_not_linked;
1909 1910
	}

1911 1912 1913 1914
	ret = usb_hcd_link_urb_to_ep(hcd, urb);
	if (ret)
		goto error_not_linked;

1915
	if (!hep->hcpriv) {
1916 1917
		hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
				GFP_ATOMIC);
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
		if (!hep->hcpriv) {
			ret = -ENOMEM;
			goto error;
		}
		set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
		if (usb_pipeendpoint(urb->pipe))
			init_pipe_info(r8a66597, urb, hep, &hep->desc);
	}

	if (unlikely(check_pipe_config(r8a66597, urb)))
		init_pipe_config(r8a66597, urb);

	set_address_zero(r8a66597, urb);
1931
	td = r8a66597_make_td(r8a66597, urb, hep);
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
	if (td == NULL) {
		ret = -ENOMEM;
		goto error;
	}
	if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
		request = 1;
	list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
	urb->hcpriv = td;

	if (request) {
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
		if (td->pipe->info.timer_interval) {
			r8a66597->interval_map |= 1 << td->pipenum;
			mod_timer(&r8a66597->interval_timer[td->pipenum],
				  jiffies + msecs_to_jiffies(
					td->pipe->info.timer_interval));
		} else {
			ret = start_transfer(r8a66597, td);
			if (ret < 0) {
				list_del(&td->queue);
				kfree(td);
			}
1953 1954 1955 1956 1957
		}
	} else
		set_td_timer(r8a66597, td);

error:
1958 1959 1960
	if (ret)
		usb_hcd_unlink_urb_from_ep(hcd, urb);
error_not_linked:
1961 1962 1963 1964
	spin_unlock_irqrestore(&r8a66597->lock, flags);
	return ret;
}

1965 1966
static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
		int status)
1967 1968 1969 1970
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	struct r8a66597_td *td;
	unsigned long flags;
1971
	int rc;
1972 1973

	spin_lock_irqsave(&r8a66597->lock, flags);
1974 1975 1976 1977
	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
	if (rc)
		goto done;

1978 1979 1980 1981 1982
	if (urb->hcpriv) {
		td = urb->hcpriv;
		pipe_stop(r8a66597, td->pipe);
		pipe_irq_disable(r8a66597, td->pipenum);
		disable_irq_empty(r8a66597, td->pipenum);
1983
		finish_request(r8a66597, td, td->pipenum, urb, status);
1984
	}
1985
 done:
1986
	spin_unlock_irqrestore(&r8a66597->lock, flags);
1987
	return rc;
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
}

static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
				      struct usb_host_endpoint *hep)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
	struct r8a66597_td *td;
	struct urb *urb = NULL;
	u16 pipenum;
	unsigned long flags;

	if (pipe == NULL)
		return;
	pipenum = pipe->info.pipenum;

	if (pipenum == 0) {
		kfree(hep->hcpriv);
		hep->hcpriv = NULL;
		return;
	}

	spin_lock_irqsave(&r8a66597->lock, flags);
	pipe_stop(r8a66597, pipe);
	pipe_irq_disable(r8a66597, pipenum);
	disable_irq_empty(r8a66597, pipenum);
	td = r8a66597_get_td(r8a66597, pipenum);
	if (td)
		urb = td->urb;
2017
	finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
	kfree(hep->hcpriv);
	hep->hcpriv = NULL;
	spin_unlock_irqrestore(&r8a66597->lock, flags);
}

static int r8a66597_get_frame(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
}

static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
{
	int chix;
2032
	struct usb_device *childdev;
2033 2034 2035 2036 2037 2038

	if (udev->state == USB_STATE_CONFIGURED &&
	    udev->parent && udev->parent->devnum > 1 &&
	    udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
		map[udev->devnum/32] |= (1 << (udev->devnum % 32));

2039 2040
	usb_hub_for_each_child(udev, chix, childdev)
		collect_usb_address_map(childdev, map);
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
}

/* this function must be called with interrupt disabled */
static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
						   int addr)
{
	struct r8a66597_device *dev;
	struct list_head *list = &r8a66597->child_device;

	list_for_each_entry(dev, list, device_list) {
		if (dev->usb_address != addr)
			continue;

		return dev;
	}

2057
	printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
	return NULL;
}

static void update_usb_address_map(struct r8a66597 *r8a66597,
				   struct usb_device *root_hub,
				   unsigned long *map)
{
	int i, j, addr;
	unsigned long diff;
	unsigned long flags;

	for (i = 0; i < 4; i++) {
		diff = r8a66597->child_connect_map[i] ^ map[i];
		if (!diff)
			continue;

		for (j = 0; j < 32; j++) {
			if (!(diff & (1 << j)))
				continue;

			addr = i * 32 + j;
			if (map[i] & (1 << j))
				set_child_connect_map(r8a66597, addr);
			else {
				struct r8a66597_device *dev;

				spin_lock_irqsave(&r8a66597->lock, flags);
				dev = get_r8a66597_device(r8a66597, addr);
				disable_r8a66597_pipe_all(r8a66597, dev);
2087
				free_usb_address(r8a66597, dev, 0);
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
				put_child_connect_map(r8a66597, addr);
				spin_unlock_irqrestore(&r8a66597->lock, flags);
			}
		}
	}
}

static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
					struct usb_hcd *hcd)
{
	struct usb_bus *bus;
	unsigned long now_map[4];

	memset(now_map, 0, sizeof(now_map));

	list_for_each_entry(bus, &usb_bus_list, bus_list) {
		if (!bus->root_hub)
			continue;

		if (bus->busnum != hcd->self.busnum)
			continue;

		collect_usb_address_map(bus->root_hub, now_map);
		update_usb_address_map(r8a66597, bus->root_hub, now_map);
	}
}

static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	unsigned long flags;
	int i;

	r8a66597_check_detect_child(r8a66597, hcd);

	spin_lock_irqsave(&r8a66597->lock, flags);

	*buf = 0;	/* initialize (no change) */

2127
	for (i = 0; i < r8a66597->max_root_hub; i++) {
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
		if (r8a66597->root_hub[i].port & 0xffff0000)
			*buf |= 1 << (i + 1);
	}

	spin_unlock_irqrestore(&r8a66597->lock, flags);

	return (*buf != 0);
}

static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
				    struct usb_hub_descriptor *desc)
{
	desc->bDescriptorType = 0x29;
	desc->bHubContrCurrent = 0;
2142
	desc->bNbrPorts = r8a66597->max_root_hub;
2143 2144 2145
	desc->bDescLength = 9;
	desc->bPwrOn2PwrGood = 0;
	desc->wHubCharacteristics = cpu_to_le16(0x0011);
J
John Youn 已提交
2146 2147 2148
	desc->u.hs.DeviceRemovable[0] =
		((1 << r8a66597->max_root_hub) - 1) << 1;
	desc->u.hs.DeviceRemovable[1] = ~0;
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
}

static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
				u16 wIndex, char *buf, u16 wLength)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	int ret;
	int port = (wIndex & 0x00FF) - 1;
	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
	unsigned long flags;

	ret = 0;

	spin_lock_irqsave(&r8a66597->lock, flags);
	switch (typeReq) {
	case ClearHubFeature:
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
2175
		if (wIndex > r8a66597->max_root_hub)
2176 2177 2178 2179 2180 2181
			goto error;
		if (wLength != 0)
			goto error;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
2182
			rh->port &= ~USB_PORT_STAT_POWER;
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
			break;
		case USB_PORT_FEAT_SUSPEND:
			break;
		case USB_PORT_FEAT_POWER:
			r8a66597_port_power(r8a66597, port, 0);
			break;
		case USB_PORT_FEAT_C_ENABLE:
		case USB_PORT_FEAT_C_SUSPEND:
		case USB_PORT_FEAT_C_CONNECTION:
		case USB_PORT_FEAT_C_OVER_CURRENT:
		case USB_PORT_FEAT_C_RESET:
			break;
		default:
			goto error;
		}
		rh->port &= ~(1 << wValue);
		break;
	case GetHubDescriptor:
		r8a66597_hub_descriptor(r8a66597,
					(struct usb_hub_descriptor *)buf);
		break;
	case GetHubStatus:
		*buf = 0x00;
		break;
	case GetPortStatus:
2208
		if (wIndex > r8a66597->max_root_hub)
2209
			goto error;
A
Al Viro 已提交
2210
		*(__le32 *)buf = cpu_to_le32(rh->port);
2211 2212
		break;
	case SetPortFeature:
2213
		if (wIndex > r8a66597->max_root_hub)
2214 2215 2216 2217 2218 2219 2220 2221 2222
			goto error;
		if (wLength != 0)
			goto error;

		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
			break;
		case USB_PORT_FEAT_POWER:
			r8a66597_port_power(r8a66597, port, 1);
2223
			rh->port |= USB_PORT_STAT_POWER;
2224 2225 2226 2227
			break;
		case USB_PORT_FEAT_RESET: {
			struct r8a66597_device *dev = rh->dev;

2228
			rh->port |= USB_PORT_STAT_RESET;
2229 2230

			disable_r8a66597_pipe_all(r8a66597, dev);
2231
			free_usb_address(r8a66597, dev, 1);
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253

			r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
				      get_dvstctr_reg(port));
			mod_timer(&r8a66597->rh_timer,
				  jiffies + msecs_to_jiffies(50));
			}
			break;
		default:
			goto error;
		}
		rh->port |= 1 << wValue;
		break;
	default:
error:
		ret = -EPIPE;
		break;
	}

	spin_unlock_irqrestore(&r8a66597->lock, flags);
	return ret;
}

2254 2255 2256 2257 2258 2259
#if defined(CONFIG_PM)
static int r8a66597_bus_suspend(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	int port;

2260
	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2261

2262
	for (port = 0; port < r8a66597->max_root_hub; port++) {
2263 2264 2265
		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
		unsigned long dvstctr_reg = get_dvstctr_reg(port);

2266
		if (!(rh->port & USB_PORT_STAT_ENABLE))
2267 2268
			continue;

2269
		dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
2270
		r8a66597_bclr(r8a66597, UACT, dvstctr_reg);	/* suspend */
2271
		rh->port |= USB_PORT_STAT_SUSPEND;
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290

		if (rh->dev->udev->do_remote_wakeup) {
			msleep(3);	/* waiting last SOF */
			r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
			r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
			r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
		}
	}

	r8a66597->bus_suspended = 1;

	return 0;
}

static int r8a66597_bus_resume(struct usb_hcd *hcd)
{
	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
	int port;

2291
	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2292

2293
	for (port = 0; port < r8a66597->max_root_hub; port++) {
2294 2295 2296
		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
		unsigned long dvstctr_reg = get_dvstctr_reg(port);

2297
		if (!(rh->port & USB_PORT_STAT_SUSPEND))
2298 2299
			continue;

2300
		dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
2301
		rh->port &= ~USB_PORT_STAT_SUSPEND;
2302
		rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
		r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
		msleep(50);
		r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
	}

	return 0;

}
#else
#define	r8a66597_bus_suspend	NULL
#define	r8a66597_bus_resume	NULL
#endif

2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
static struct hc_driver r8a66597_hc_driver = {
	.description =		hcd_name,
	.hcd_priv_size =	sizeof(struct r8a66597),
	.irq =			r8a66597_irq,

	/*
	 * generic hardware linkage
	 */
	.flags =		HCD_USB2,

	.start =		r8a66597_start,
	.stop =			r8a66597_stop,

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue =		r8a66597_urb_enqueue,
	.urb_dequeue =		r8a66597_urb_dequeue,
	.endpoint_disable =	r8a66597_endpoint_disable,

	/*
	 * periodic schedule support
	 */
	.get_frame_number =	r8a66597_get_frame,

	/*
	 * root hub support
	 */
	.hub_status_data =	r8a66597_hub_status_data,
	.hub_control =		r8a66597_hub_control,
2346 2347
	.bus_suspend =		r8a66597_bus_suspend,
	.bus_resume =		r8a66597_bus_resume,
2348 2349 2350
};

#if defined(CONFIG_PM)
2351
static int r8a66597_suspend(struct device *dev)
2352
{
2353
	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2354 2355
	int port;

2356
	dev_dbg(dev, "%s\n", __func__);
2357 2358 2359

	disable_controller(r8a66597);

2360
	for (port = 0; port < r8a66597->max_root_hub; port++) {
2361 2362 2363 2364 2365
		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];

		rh->port = 0x00000000;
	}

2366 2367 2368
	return 0;
}

2369
static int r8a66597_resume(struct device *dev)
2370
{
2371
	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2372 2373
	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);

2374
	dev_dbg(dev, "%s\n", __func__);
2375 2376 2377 2378

	enable_controller(r8a66597);
	usb_root_hub_lost_power(hcd->self.root_hub);

2379 2380
	return 0;
}
2381

2382
static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2383 2384
	.suspend = r8a66597_suspend,
	.resume = r8a66597_resume,
2385 2386
	.poweroff = r8a66597_suspend,
	.restore = r8a66597_resume,
2387 2388 2389
};

#define R8A66597_DEV_PM_OPS	(&r8a66597_dev_pm_ops)
2390
#else	/* if defined(CONFIG_PM) */
2391
#define R8A66597_DEV_PM_OPS	NULL
2392 2393
#endif

2394
static int __devexit r8a66597_remove(struct platform_device *pdev)
2395 2396 2397 2398 2399 2400
{
	struct r8a66597		*r8a66597 = dev_get_drvdata(&pdev->dev);
	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);

	del_timer_sync(&r8a66597->rh_timer);
	usb_remove_hcd(hcd);
2401
	iounmap(r8a66597->reg);
2402 2403
	if (r8a66597->pdata->on_chip)
		clk_put(r8a66597->clk);
2404 2405 2406 2407
	usb_put_hcd(hcd);
	return 0;
}

2408
static int __devinit r8a66597_probe(struct platform_device *pdev)
2409
{
2410
	char clk_name[8];
2411
	struct resource *res = NULL, *ires;
2412 2413 2414 2415 2416 2417
	int irq = -1;
	void __iomem *reg = NULL;
	struct usb_hcd *hcd = NULL;
	struct r8a66597 *r8a66597;
	int ret = 0;
	int i;
2418
	unsigned long irq_trigger;
2419

2420 2421 2422
	if (usb_disabled())
		return -ENODEV;

2423 2424
	if (pdev->dev.dma_mask) {
		ret = -EINVAL;
2425
		dev_err(&pdev->dev, "dma not supported\n");
2426 2427 2428
		goto clean_up;
	}

2429
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2430 2431
	if (!res) {
		ret = -ENODEV;
2432
		dev_err(&pdev->dev, "platform_get_resource error.\n");
2433 2434 2435
		goto clean_up;
	}

2436 2437
	ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!ires) {
2438
		ret = -ENODEV;
2439 2440
		dev_err(&pdev->dev,
			"platform_get_resource IORESOURCE_IRQ error.\n");
2441 2442 2443
		goto clean_up;
	}

2444 2445 2446
	irq = ires->start;
	irq_trigger = ires->flags & IRQF_TRIGGER_MASK;

2447
	reg = ioremap(res->start, resource_size(res));
2448 2449
	if (reg == NULL) {
		ret = -ENOMEM;
2450
		dev_err(&pdev->dev, "ioremap error.\n");
2451 2452 2453
		goto clean_up;
	}

2454 2455 2456 2457 2458 2459
	if (pdev->dev.platform_data == NULL) {
		dev_err(&pdev->dev, "no platform data\n");
		ret = -ENODEV;
		goto clean_up;
	}

2460 2461 2462 2463
	/* initialize hcd */
	hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
	if (!hcd) {
		ret = -ENOMEM;
2464
		dev_err(&pdev->dev, "Failed to create hcd\n");
2465 2466 2467 2468 2469
		goto clean_up;
	}
	r8a66597 = hcd_to_r8a66597(hcd);
	memset(r8a66597, 0, sizeof(struct r8a66597));
	dev_set_drvdata(&pdev->dev, r8a66597);
2470 2471
	r8a66597->pdata = pdev->dev.platform_data;
	r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2472

2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
	if (r8a66597->pdata->on_chip) {
		snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
		r8a66597->clk = clk_get(&pdev->dev, clk_name);
		if (IS_ERR(r8a66597->clk)) {
			dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
				clk_name);
			ret = PTR_ERR(r8a66597->clk);
			goto clean_up2;
		}
		r8a66597->max_root_hub = 1;
	} else
		r8a66597->max_root_hub = 2;
2485

2486 2487 2488 2489
	spin_lock_init(&r8a66597->lock);
	init_timer(&r8a66597->rh_timer);
	r8a66597->rh_timer.function = r8a66597_timer;
	r8a66597->rh_timer.data = (unsigned long)r8a66597;
2490
	r8a66597->reg = reg;
2491

2492 2493 2494 2495 2496 2497
	/* make sure no interrupts are pending */
	ret = r8a66597_clock_enable(r8a66597);
	if (ret < 0)
		goto clean_up3;
	disable_controller(r8a66597);

2498 2499 2500 2501 2502
	for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
		INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
		init_timer(&r8a66597->td_timer[i]);
		r8a66597->td_timer[i].function = r8a66597_td_timer;
		r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2503 2504 2505
		setup_timer(&r8a66597->interval_timer[i],
				r8a66597_interval_timer,
				(unsigned long)r8a66597);
2506 2507 2508 2509
	}
	INIT_LIST_HEAD(&r8a66597->child_device);

	hcd->rsrc_start = res->start;
2510
	hcd->has_tt = 1;
2511

Y
Yong Zhang 已提交
2512
	ret = usb_add_hcd(hcd, irq, irq_trigger);
2513
	if (ret != 0) {
2514
		dev_err(&pdev->dev, "Failed to add hcd\n");
2515
		goto clean_up3;
2516 2517 2518 2519
	}

	return 0;

2520
clean_up3:
2521 2522
	if (r8a66597->pdata->on_chip)
		clk_put(r8a66597->clk);
2523 2524 2525
clean_up2:
	usb_put_hcd(hcd);

2526 2527 2528 2529 2530 2531 2532 2533 2534
clean_up:
	if (reg)
		iounmap(reg);

	return ret;
}

static struct platform_driver r8a66597_driver = {
	.probe =	r8a66597_probe,
B
Bill Pemberton 已提交
2535
	.remove =	r8a66597_remove,
2536 2537
	.driver		= {
		.name = (char *) hcd_name,
2538
		.owner	= THIS_MODULE,
2539
		.pm	= R8A66597_DEV_PM_OPS,
2540 2541 2542
	},
};

2543
module_platform_driver(r8a66597_driver);