Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * R8A66597 HCD (Host Controller Driver)
   4 *
   5 * Copyright (C) 2006-2007 Renesas Solutions Corp.
   6 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
   7 * Portions Copyright (C) 2004-2005 David Brownell
   8 * Portions Copyright (C) 1999 Roman Weissgaerber
   9 *
  10 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/kernel.h>
  15#include <linux/sched.h>
  16#include <linux/errno.h>
  17#include <linux/timer.h>
  18#include <linux/delay.h>
  19#include <linux/list.h>
  20#include <linux/interrupt.h>
  21#include <linux/usb.h>
  22#include <linux/usb/hcd.h>
  23#include <linux/platform_device.h>
  24#include <linux/io.h>
  25#include <linux/mm.h>
  26#include <linux/irq.h>
  27#include <linux/slab.h>
  28#include <asm/cacheflush.h>
  29
  30#include "r8a66597.h"
  31
  32MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
  33MODULE_LICENSE("GPL");
  34MODULE_AUTHOR("Yoshihiro Shimoda");
  35MODULE_ALIAS("platform:r8a66597_hcd");
  36
  37#define DRIVER_VERSION	"2009-05-26"
  38
  39static const char hcd_name[] = "r8a66597_hcd";
  40
  41static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
  42static int r8a66597_get_frame(struct usb_hcd *hcd);
  43
  44/* this function must be called with interrupt disabled */
  45static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  46			    unsigned long reg)
  47{
  48	u16 tmp;
  49
  50	tmp = r8a66597_read(r8a66597, INTENB0);
  51	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  52	r8a66597_bset(r8a66597, 1 << pipenum, reg);
  53	r8a66597_write(r8a66597, tmp, INTENB0);
  54}
  55
  56/* this function must be called with interrupt disabled */
  57static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  58			     unsigned long reg)
  59{
  60	u16 tmp;
  61
  62	tmp = r8a66597_read(r8a66597, INTENB0);
  63	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  64	r8a66597_bclr(r8a66597, 1 << pipenum, reg);
  65	r8a66597_write(r8a66597, tmp, INTENB0);
  66}
  67
  68static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
  69			   u16 usbspd, u8 upphub, u8 hubport, int port)
  70{
  71	u16 val;
  72	unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
  73
  74	val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
  75	r8a66597_write(r8a66597, val, devadd_reg);
  76}
  77
  78static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
  79{
  80	u16 tmp;
  81	int i = 0;
  82
  83	if (r8a66597->pdata->on_chip) {
  84		clk_prepare_enable(r8a66597->clk);
  85		do {
  86			r8a66597_write(r8a66597, SCKE, SYSCFG0);
  87			tmp = r8a66597_read(r8a66597, SYSCFG0);
  88			if (i++ > 1000) {
  89				printk(KERN_ERR "r8a66597: reg access fail.\n");
  90				return -ENXIO;
  91			}
  92		} while ((tmp & SCKE) != SCKE);
  93		r8a66597_write(r8a66597, 0x04, 0x02);
  94	} else {
  95		do {
  96			r8a66597_write(r8a66597, USBE, SYSCFG0);
  97			tmp = r8a66597_read(r8a66597, SYSCFG0);
  98			if (i++ > 1000) {
  99				printk(KERN_ERR "r8a66597: reg access fail.\n");
 100				return -ENXIO;
 101			}
 102		} while ((tmp & USBE) != USBE);
 103		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 104		r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
 105			      XTAL, SYSCFG0);
 106
 107		i = 0;
 108		r8a66597_bset(r8a66597, XCKE, SYSCFG0);
 109		do {
 110			msleep(1);
 111			tmp = r8a66597_read(r8a66597, SYSCFG0);
 112			if (i++ > 500) {
 113				printk(KERN_ERR "r8a66597: reg access fail.\n");
 114				return -ENXIO;
 115			}
 116		} while ((tmp & SCKE) != SCKE);
 117	}
 118
 119	return 0;
 120}
 121
 122static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
 123{
 124	r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
 125	udelay(1);
 126
 127	if (r8a66597->pdata->on_chip) {
 128		clk_disable_unprepare(r8a66597->clk);
 129	} else {
 130		r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
 131		r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
 132		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 133	}
 134}
 135
 136static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
 137{
 138	u16 val;
 139
 140	val = port ? DRPD : DCFM | DRPD;
 141	r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
 142	r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
 143
 144	r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
 145	r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
 146	r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
 147}
 148
 149static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
 150{
 151	u16 val, tmp;
 152
 153	r8a66597_write(r8a66597, 0, get_intenb_reg(port));
 154	r8a66597_write(r8a66597, 0, get_intsts_reg(port));
 155
 156	r8a66597_port_power(r8a66597, port, 0);
 157
 158	do {
 159		tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
 160		udelay(640);
 161	} while (tmp == EDGESTS);
 162
 163	val = port ? DRPD : DCFM | DRPD;
 164	r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
 165	r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
 166}
 167
 168static int enable_controller(struct r8a66597 *r8a66597)
 169{
 170	int ret, port;
 171	u16 vif = r8a66597->pdata->vif ? LDRV : 0;
 172	u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
 173	u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
 174
 175	ret = r8a66597_clock_enable(r8a66597);
 176	if (ret < 0)
 177		return ret;
 178
 179	r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
 180	r8a66597_bset(r8a66597, USBE, SYSCFG0);
 181
 182	r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
 183	r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
 184	r8a66597_bset(r8a66597, BRDY0, BRDYENB);
 185	r8a66597_bset(r8a66597, BEMP0, BEMPENB);
 186
 187	r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
 188	r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
 189	r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
 190	r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
 191
 192	r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
 193
 194	for (port = 0; port < r8a66597->max_root_hub; port++)
 195		r8a66597_enable_port(r8a66597, port);
 196
 197	return 0;
 198}
 199
 200static void disable_controller(struct r8a66597 *r8a66597)
 201{
 202	int port;
 203
 204	/* disable interrupts */
 205	r8a66597_write(r8a66597, 0, INTENB0);
 206	r8a66597_write(r8a66597, 0, INTENB1);
 207	r8a66597_write(r8a66597, 0, BRDYENB);
 208	r8a66597_write(r8a66597, 0, BEMPENB);
 209	r8a66597_write(r8a66597, 0, NRDYENB);
 210
 211	/* clear status */
 212	r8a66597_write(r8a66597, 0, BRDYSTS);
 213	r8a66597_write(r8a66597, 0, NRDYSTS);
 214	r8a66597_write(r8a66597, 0, BEMPSTS);
 215
 216	for (port = 0; port < r8a66597->max_root_hub; port++)
 217		r8a66597_disable_port(r8a66597, port);
 218
 219	r8a66597_clock_disable(r8a66597);
 220}
 221
 222static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
 223				       struct usb_device *udev)
 224{
 225	struct r8a66597_device *dev;
 226
 227	if (udev->parent && udev->parent->devnum != 1)
 228		udev = udev->parent;
 229
 230	dev = dev_get_drvdata(&udev->dev);
 231	if (dev)
 232		return dev->address;
 233	else
 234		return 0;
 235}
 236
 237static int is_child_device(char *devpath)
 238{
 239	return (devpath[2] ? 1 : 0);
 240}
 241
 242static int is_hub_limit(char *devpath)
 243{
 244	return ((strlen(devpath) >= 4) ? 1 : 0);
 245}
 246
 247static void get_port_number(struct r8a66597 *r8a66597,
 248			    char *devpath, u16 *root_port, u16 *hub_port)
 249{
 250	if (root_port) {
 251		*root_port = (devpath[0] & 0x0F) - 1;
 252		if (*root_port >= r8a66597->max_root_hub)
 253			printk(KERN_ERR "r8a66597: Illegal root port number.\n");
 254	}
 255	if (hub_port)
 256		*hub_port = devpath[2] & 0x0F;
 257}
 258
 259static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
 260{
 261	u16 usbspd = 0;
 262
 263	switch (speed) {
 264	case USB_SPEED_LOW:
 265		usbspd = LSMODE;
 266		break;
 267	case USB_SPEED_FULL:
 268		usbspd = FSMODE;
 269		break;
 270	case USB_SPEED_HIGH:
 271		usbspd = HSMODE;
 272		break;
 273	default:
 274		printk(KERN_ERR "r8a66597: unknown speed\n");
 275		break;
 276	}
 277
 278	return usbspd;
 279}
 280
 281static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
 282{
 283	int idx;
 284
 285	idx = address / 32;
 286	r8a66597->child_connect_map[idx] |= 1 << (address % 32);
 287}
 288
 289static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
 290{
 291	int idx;
 292
 293	idx = address / 32;
 294	r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
 295}
 296
 297static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
 298{
 299	u16 pipenum = pipe->info.pipenum;
 300	const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
 301	const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
 302	const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
 303
 304	if (dma_ch > R8A66597_PIPE_NO_DMA)	/* dma fifo not use? */
 305		dma_ch = R8A66597_PIPE_NO_DMA;
 306
 307	pipe->fifoaddr = fifoaddr[dma_ch];
 308	pipe->fifosel = fifosel[dma_ch];
 309	pipe->fifoctr = fifoctr[dma_ch];
 310
 311	if (pipenum == 0)
 312		pipe->pipectr = DCPCTR;
 313	else
 314		pipe->pipectr = get_pipectr_addr(pipenum);
 315
 316	if (check_bulk_or_isoc(pipenum)) {
 317		pipe->pipetre = get_pipetre_addr(pipenum);
 318		pipe->pipetrn = get_pipetrn_addr(pipenum);
 319	} else {
 320		pipe->pipetre = 0;
 321		pipe->pipetrn = 0;
 322	}
 323}
 324
 325static struct r8a66597_device *
 326get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
 327{
 328	if (usb_pipedevice(urb->pipe) == 0)
 329		return &r8a66597->device0;
 330
 331	return dev_get_drvdata(&urb->dev->dev);
 332}
 333
 334static int make_r8a66597_device(struct r8a66597 *r8a66597,
 335				struct urb *urb, u8 addr)
 336{
 337	struct r8a66597_device *dev;
 338	int usb_address = urb->setup_packet[2];	/* urb->pipe is address 0 */
 339
 340	dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
 341	if (dev == NULL)
 342		return -ENOMEM;
 343
 344	dev_set_drvdata(&urb->dev->dev, dev);
 345	dev->udev = urb->dev;
 346	dev->address = addr;
 347	dev->usb_address = usb_address;
 348	dev->state = USB_STATE_ADDRESS;
 349	dev->ep_in_toggle = 0;
 350	dev->ep_out_toggle = 0;
 351	INIT_LIST_HEAD(&dev->device_list);
 352	list_add_tail(&dev->device_list, &r8a66597->child_device);
 353
 354	get_port_number(r8a66597, urb->dev->devpath,
 355			&dev->root_port, &dev->hub_port);
 356	if (!is_child_device(urb->dev->devpath))
 357		r8a66597->root_hub[dev->root_port].dev = dev;
 358
 359	set_devadd_reg(r8a66597, dev->address,
 360		       get_r8a66597_usb_speed(urb->dev->speed),
 361		       get_parent_r8a66597_address(r8a66597, urb->dev),
 362		       dev->hub_port, dev->root_port);
 363
 364	return 0;
 365}
 366
 367/* this function must be called with interrupt disabled */
 368static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
 369{
 370	u8 addr;	/* R8A66597's address */
 371	struct r8a66597_device *dev;
 372
 373	if (is_hub_limit(urb->dev->devpath)) {
 374		dev_err(&urb->dev->dev, "External hub limit reached.\n");
 375		return 0;
 376	}
 377
 378	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 379	if (dev && dev->state >= USB_STATE_ADDRESS)
 380		return dev->address;
 381
 382	for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
 383		if (r8a66597->address_map & (1 << addr))
 384			continue;
 385
 386		dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
 387		r8a66597->address_map |= 1 << addr;
 388
 389		if (make_r8a66597_device(r8a66597, urb, addr) < 0)
 390			return 0;
 391
 392		return addr;
 393	}
 394
 395	dev_err(&urb->dev->dev,
 396		"cannot communicate with a USB device more than 10.(%x)\n",
 397		r8a66597->address_map);
 398
 399	return 0;
 400}
 401
 402/* this function must be called with interrupt disabled */
 403static void free_usb_address(struct r8a66597 *r8a66597,
 404			     struct r8a66597_device *dev, int reset)
 405{
 406	int port;
 407
 408	if (!dev)
 409		return;
 410
 411	dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
 412
 413	dev->state = USB_STATE_DEFAULT;
 414	r8a66597->address_map &= ~(1 << dev->address);
 415	dev->address = 0;
 416	/*
 417	 * Only when resetting USB, it is necessary to erase drvdata. When
 418	 * a usb device with usb hub is disconnect, "dev->udev" is already
 419	 * freed on usb_desconnect(). So we cannot access the data.
 420	 */
 421	if (reset)
 422		dev_set_drvdata(&dev->udev->dev, NULL);
 423	list_del(&dev->device_list);
 424	kfree(dev);
 425
 426	for (port = 0; port < r8a66597->max_root_hub; port++) {
 427		if (r8a66597->root_hub[port].dev == dev) {
 428			r8a66597->root_hub[port].dev = NULL;
 429			break;
 430		}
 431	}
 432}
 433
 434static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
 435			      u16 mask, u16 loop)
 436{
 437	u16 tmp;
 438	int i = 0;
 439
 440	do {
 441		tmp = r8a66597_read(r8a66597, reg);
 442		if (i++ > 1000000) {
 443			printk(KERN_ERR "r8a66597: register%lx, loop %x "
 444			       "is timeout\n", reg, loop);
 445			break;
 446		}
 447		ndelay(1);
 448	} while ((tmp & mask) != loop);
 449}
 450
 451/* this function must be called with interrupt disabled */
 452static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
 453{
 454	u16 tmp;
 455
 456	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
 457	if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
 458		r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
 459	r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
 460}
 461
 462/* this function must be called with interrupt disabled */
 463static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
 464{
 465	u16 tmp;
 466
 467	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
 468	if ((tmp & PID_STALL11) != PID_STALL11)	/* force stall? */
 469		r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
 470	r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
 471	r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
 472}
 473
 474/* this function must be called with interrupt disabled */
 475static void clear_all_buffer(struct r8a66597 *r8a66597,
 476			     struct r8a66597_pipe *pipe)
 477{
 
 
 478	if (!pipe || pipe->info.pipenum == 0)
 479		return;
 480
 481	pipe_stop(r8a66597, pipe);
 482	r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
 483	r8a66597_read(r8a66597, pipe->pipectr);
 484	r8a66597_read(r8a66597, pipe->pipectr);
 485	r8a66597_read(r8a66597, pipe->pipectr);
 486	r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
 487}
 488
 489/* this function must be called with interrupt disabled */
 490static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
 491				 struct r8a66597_pipe *pipe, int toggle)
 492{
 493	if (toggle)
 494		r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
 495	else
 496		r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
 497}
 498
 499static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
 500{
 501	if (r8a66597->pdata->on_chip)
 502		return MBW_32;
 503	else
 504		return MBW_16;
 505}
 506
 507/* this function must be called with interrupt disabled */
 508static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
 509{
 510	unsigned short mbw = mbw_value(r8a66597);
 511
 512	r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
 513	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
 514}
 515
 516/* this function must be called with interrupt disabled */
 517static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
 518					 struct r8a66597_pipe *pipe)
 519{
 520	unsigned short mbw = mbw_value(r8a66597);
 521
 522	cfifo_change(r8a66597, 0);
 523	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
 524	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
 525
 526	r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
 527		      pipe->fifosel);
 528	r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
 529}
 530
 531static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
 532{
 533	struct r8a66597_pipe *pipe = hep->hcpriv;
 534
 535	if (usb_pipeendpoint(urb->pipe) == 0)
 536		return 0;
 537	else
 538		return pipe->info.pipenum;
 539}
 540
 541static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
 542{
 543	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 544
 545	return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
 546}
 547
 548static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
 549					  int urb_pipe)
 550{
 551	if (!dev)
 552		return NULL;
 553
 554	return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
 555}
 556
 557/* this function must be called with interrupt disabled */
 558static void pipe_toggle_set(struct r8a66597 *r8a66597,
 559			    struct r8a66597_pipe *pipe,
 560			    struct urb *urb, int set)
 561{
 562	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 563	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
 564	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
 565
 566	if (!toggle)
 567		return;
 568
 569	if (set)
 570		*toggle |= 1 << endpoint;
 571	else
 572		*toggle &= ~(1 << endpoint);
 573}
 574
 575/* this function must be called with interrupt disabled */
 576static void pipe_toggle_save(struct r8a66597 *r8a66597,
 577			     struct r8a66597_pipe *pipe,
 578			     struct urb *urb)
 579{
 580	if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
 581		pipe_toggle_set(r8a66597, pipe, urb, 1);
 582	else
 583		pipe_toggle_set(r8a66597, pipe, urb, 0);
 584}
 585
 586/* this function must be called with interrupt disabled */
 587static void pipe_toggle_restore(struct r8a66597 *r8a66597,
 588				struct r8a66597_pipe *pipe,
 589				struct urb *urb)
 590{
 591	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 592	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
 593	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
 594
 595	if (!toggle)
 596		return;
 597
 598	r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
 599}
 600
 601/* this function must be called with interrupt disabled */
 602static void pipe_buffer_setting(struct r8a66597 *r8a66597,
 603				struct r8a66597_pipe_info *info)
 604{
 605	u16 val = 0;
 606
 607	if (info->pipenum == 0)
 608		return;
 609
 610	r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
 611	r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
 612	r8a66597_write(r8a66597, info->pipenum, PIPESEL);
 613	if (!info->dir_in)
 614		val |= R8A66597_DIR;
 615	if (info->type == R8A66597_BULK && info->dir_in)
 616		val |= R8A66597_DBLB | R8A66597_SHTNAK;
 617	val |= info->type | info->epnum;
 618	r8a66597_write(r8a66597, val, PIPECFG);
 619
 620	r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
 621		       PIPEBUF);
 622	r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
 623		       PIPEMAXP);
 624	r8a66597_write(r8a66597, info->interval, PIPEPERI);
 625}
 626
 627/* this function must be called with interrupt disabled */
 628static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
 629{
 630	struct r8a66597_pipe_info *info;
 631	struct urb *urb = td->urb;
 632
 633	if (td->pipenum > 0) {
 634		info = &td->pipe->info;
 635		cfifo_change(r8a66597, 0);
 636		pipe_buffer_setting(r8a66597, info);
 637
 638		if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
 639				   usb_pipeout(urb->pipe)) &&
 640		    !usb_pipecontrol(urb->pipe)) {
 641			r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
 642			pipe_toggle_set(r8a66597, td->pipe, urb, 0);
 643			clear_all_buffer(r8a66597, td->pipe);
 644			usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
 645				      usb_pipeout(urb->pipe), 1);
 646		}
 647		pipe_toggle_restore(r8a66597, td->pipe, urb);
 648	}
 649}
 650
 651/* this function must be called with interrupt disabled */
 652static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
 653			     struct usb_endpoint_descriptor *ep)
 654{
 655	u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
 656
 657	memset(array, 0, sizeof(array));
 658	switch (usb_endpoint_type(ep)) {
 659	case USB_ENDPOINT_XFER_BULK:
 660		if (usb_endpoint_dir_in(ep))
 661			array[i++] = 4;
 662		else {
 663			array[i++] = 3;
 664			array[i++] = 5;
 665		}
 666		break;
 667	case USB_ENDPOINT_XFER_INT:
 668		if (usb_endpoint_dir_in(ep)) {
 669			array[i++] = 6;
 670			array[i++] = 7;
 671			array[i++] = 8;
 672		} else
 673			array[i++] = 9;
 674		break;
 675	case USB_ENDPOINT_XFER_ISOC:
 676		if (usb_endpoint_dir_in(ep))
 677			array[i++] = 2;
 678		else
 679			array[i++] = 1;
 680		break;
 681	default:
 682		printk(KERN_ERR "r8a66597: Illegal type\n");
 683		return 0;
 684	}
 685
 686	i = 1;
 687	min = array[0];
 688	while (array[i] != 0) {
 689		if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
 690			min = array[i];
 691		i++;
 692	}
 693
 694	return min;
 695}
 696
 697static u16 get_r8a66597_type(__u8 type)
 698{
 699	u16 r8a66597_type;
 700
 701	switch (type) {
 702	case USB_ENDPOINT_XFER_BULK:
 703		r8a66597_type = R8A66597_BULK;
 704		break;
 705	case USB_ENDPOINT_XFER_INT:
 706		r8a66597_type = R8A66597_INT;
 707		break;
 708	case USB_ENDPOINT_XFER_ISOC:
 709		r8a66597_type = R8A66597_ISO;
 710		break;
 711	default:
 712		printk(KERN_ERR "r8a66597: Illegal type\n");
 713		r8a66597_type = 0x0000;
 714		break;
 715	}
 716
 717	return r8a66597_type;
 718}
 719
 720static u16 get_bufnum(u16 pipenum)
 721{
 722	u16 bufnum = 0;
 723
 724	if (pipenum == 0)
 725		bufnum = 0;
 726	else if (check_bulk_or_isoc(pipenum))
 727		bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
 728	else if (check_interrupt(pipenum))
 729		bufnum = 4 + (pipenum - 6);
 730	else
 731		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 732
 733	return bufnum;
 734}
 735
 736static u16 get_buf_bsize(u16 pipenum)
 737{
 738	u16 buf_bsize = 0;
 739
 740	if (pipenum == 0)
 741		buf_bsize = 3;
 742	else if (check_bulk_or_isoc(pipenum))
 743		buf_bsize = R8A66597_BUF_BSIZE - 1;
 744	else if (check_interrupt(pipenum))
 745		buf_bsize = 0;
 746	else
 747		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 748
 749	return buf_bsize;
 750}
 751
 752/* this function must be called with interrupt disabled */
 753static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
 754				     struct r8a66597_device *dev,
 755				     struct r8a66597_pipe *pipe,
 756				     struct urb *urb)
 757{
 758	int i;
 759	struct r8a66597_pipe_info *info = &pipe->info;
 760	unsigned short mbw = mbw_value(r8a66597);
 761
 762	/* pipe dma is only for external controlles */
 763	if (r8a66597->pdata->on_chip)
 764		return;
 765
 766	if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
 767		for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
 768			if ((r8a66597->dma_map & (1 << i)) != 0)
 769				continue;
 770
 771			dev_info(&dev->udev->dev,
 772				 "address %d, EndpointAddress 0x%02x use "
 773				 "DMA FIFO\n", usb_pipedevice(urb->pipe),
 774				 info->dir_in ?
 775				 	USB_ENDPOINT_DIR_MASK + info->epnum
 776					: info->epnum);
 777
 778			r8a66597->dma_map |= 1 << i;
 779			dev->dma_map |= 1 << i;
 780			set_pipe_reg_addr(pipe, i);
 781
 782			cfifo_change(r8a66597, 0);
 783			r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
 784				      mbw | CURPIPE, pipe->fifosel);
 785
 786			r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
 787					  pipe->info.pipenum);
 788			r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
 789			break;
 790		}
 791	}
 792}
 793
 794/* this function must be called with interrupt disabled */
 795static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
 796				 struct usb_host_endpoint *hep,
 797				 struct r8a66597_pipe_info *info)
 798{
 799	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 800	struct r8a66597_pipe *pipe = hep->hcpriv;
 801
 802	dev_dbg(&dev->udev->dev, "enable_pipe:\n");
 803
 804	pipe->info = *info;
 805	set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
 806	r8a66597->pipe_cnt[pipe->info.pipenum]++;
 807	dev->pipe_cnt[pipe->info.pipenum]++;
 808
 809	enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
 810}
 811
 812static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
 813			      int status)
 814__releases(r8a66597->lock)
 815__acquires(r8a66597->lock)
 816{
 817	if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
 818		void *ptr;
 819
 820		for (ptr = urb->transfer_buffer;
 821		     ptr < urb->transfer_buffer + urb->transfer_buffer_length;
 822		     ptr += PAGE_SIZE)
 823			flush_dcache_page(virt_to_page(ptr));
 824	}
 825
 826	usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
 827	spin_unlock(&r8a66597->lock);
 828	usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
 829	spin_lock(&r8a66597->lock);
 830}
 831
 832/* this function must be called with interrupt disabled */
 833static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
 834{
 835	struct r8a66597_td *td, *next;
 836	struct urb *urb;
 837	struct list_head *list = &r8a66597->pipe_queue[pipenum];
 838
 839	if (list_empty(list))
 840		return;
 841
 842	list_for_each_entry_safe(td, next, list, queue) {
 843		if (td->address != address)
 844			continue;
 845
 846		urb = td->urb;
 847		list_del(&td->queue);
 848		kfree(td);
 849
 850		if (urb)
 851			r8a66597_urb_done(r8a66597, urb, -ENODEV);
 852
 853		break;
 854	}
 855}
 856
 857/* this function must be called with interrupt disabled */
 858static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
 859				      struct r8a66597_device *dev)
 860{
 861	int check_ep0 = 0;
 862	u16 pipenum;
 863
 864	if (!dev)
 865		return;
 866
 867	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
 868		if (!dev->pipe_cnt[pipenum])
 869			continue;
 870
 871		if (!check_ep0) {
 872			check_ep0 = 1;
 873			force_dequeue(r8a66597, 0, dev->address);
 874		}
 875
 876		r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
 877		dev->pipe_cnt[pipenum] = 0;
 878		force_dequeue(r8a66597, pipenum, dev->address);
 879	}
 880
 881	dev_dbg(&dev->udev->dev, "disable_pipe\n");
 882
 883	r8a66597->dma_map &= ~(dev->dma_map);
 884	dev->dma_map = 0;
 885}
 886
 887static u16 get_interval(struct urb *urb, __u8 interval)
 888{
 889	u16 time = 1;
 890	int i;
 891
 892	if (urb->dev->speed == USB_SPEED_HIGH) {
 893		if (interval > IITV)
 894			time = IITV;
 895		else
 896			time = interval ? interval - 1 : 0;
 897	} else {
 898		if (interval > 128) {
 899			time = IITV;
 900		} else {
 901			/* calculate the nearest value for PIPEPERI */
 902			for (i = 0; i < 7; i++) {
 903				if ((1 << i) < interval &&
 904				    (1 << (i + 1) > interval))
 905					time = 1 << i;
 906			}
 907		}
 908	}
 909
 910	return time;
 911}
 912
 913static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
 914{
 915	__u8 i;
 916	unsigned long time = 1;
 917
 918	if (usb_pipeisoc(urb->pipe))
 919		return 0;
 920
 921	if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
 922		for (i = 0; i < (interval - 1); i++)
 923			time *= 2;
 924		time = time * 125 / 1000;	/* uSOF -> msec */
 925	} else {
 926		time = interval;
 927	}
 928
 929	return time;
 930}
 931
 932/* this function must be called with interrupt disabled */
 933static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
 934			   struct usb_host_endpoint *hep,
 935			   struct usb_endpoint_descriptor *ep)
 936{
 937	struct r8a66597_pipe_info info;
 938
 939	info.pipenum = get_empty_pipenum(r8a66597, ep);
 940	info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
 941	info.epnum = usb_endpoint_num(ep);
 942	info.maxpacket = usb_endpoint_maxp(ep);
 943	info.type = get_r8a66597_type(usb_endpoint_type(ep));
 944	info.bufnum = get_bufnum(info.pipenum);
 945	info.buf_bsize = get_buf_bsize(info.pipenum);
 946	if (info.type == R8A66597_BULK) {
 947		info.interval = 0;
 948		info.timer_interval = 0;
 949	} else {
 950		info.interval = get_interval(urb, ep->bInterval);
 951		info.timer_interval = get_timer_interval(urb, ep->bInterval);
 952	}
 953	if (usb_endpoint_dir_in(ep))
 954		info.dir_in = 1;
 955	else
 956		info.dir_in = 0;
 957
 958	enable_r8a66597_pipe(r8a66597, urb, hep, &info);
 959}
 960
 961static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
 962{
 963	struct r8a66597_device *dev;
 964
 965	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 966	dev->state = USB_STATE_CONFIGURED;
 967}
 968
 969static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
 970			    u16 pipenum)
 971{
 972	if (pipenum == 0 && usb_pipeout(urb->pipe))
 973		enable_irq_empty(r8a66597, pipenum);
 974	else
 975		enable_irq_ready(r8a66597, pipenum);
 976
 977	if (!usb_pipeisoc(urb->pipe))
 978		enable_irq_nrdy(r8a66597, pipenum);
 979}
 980
 981static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
 982{
 983	disable_irq_ready(r8a66597, pipenum);
 984	disable_irq_nrdy(r8a66597, pipenum);
 985}
 986
 987static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
 988{
 989	mod_timer(&r8a66597->rh_timer,
 990			jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
 991}
 992
 993static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
 994					int connect)
 995{
 996	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
 997
 998	rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
 999	rh->scount = R8A66597_MAX_SAMPLING;
1000	if (connect)
1001		rh->port |= USB_PORT_STAT_CONNECTION;
1002	else
1003		rh->port &= ~USB_PORT_STAT_CONNECTION;
1004	rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
1005
1006	r8a66597_root_hub_start_polling(r8a66597);
1007}
1008
1009/* this function must be called with interrupt disabled */
1010static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1011					u16 syssts)
1012__releases(r8a66597->lock)
1013__acquires(r8a66597->lock)
1014{
1015	if (syssts == SE0) {
1016		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1017		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1018	} else {
1019		if (syssts == FS_JSTS)
1020			r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1021		else if (syssts == LS_JSTS)
1022			r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1023
1024		r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1025		r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1026
1027		if (r8a66597->bus_suspended)
1028			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1029	}
1030
1031	spin_unlock(&r8a66597->lock);
1032	usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1033	spin_lock(&r8a66597->lock);
1034}
1035
1036/* this function must be called with interrupt disabled */
1037static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1038{
1039	u16 speed = get_rh_usb_speed(r8a66597, port);
1040	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1041
1042	rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
1043	if (speed == HSMODE)
1044		rh->port |= USB_PORT_STAT_HIGH_SPEED;
1045	else if (speed == LSMODE)
1046		rh->port |= USB_PORT_STAT_LOW_SPEED;
1047
1048	rh->port &= ~USB_PORT_STAT_RESET;
1049	rh->port |= USB_PORT_STAT_ENABLE;
1050}
1051
1052/* this function must be called with interrupt disabled */
1053static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1054{
1055	struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1056
1057	disable_r8a66597_pipe_all(r8a66597, dev);
1058	free_usb_address(r8a66597, dev, 0);
1059
1060	start_root_hub_sampling(r8a66597, port, 0);
1061}
1062
1063/* this function must be called with interrupt disabled */
1064static void prepare_setup_packet(struct r8a66597 *r8a66597,
1065				 struct r8a66597_td *td)
1066{
1067	int i;
1068	__le16 *p = (__le16 *)td->urb->setup_packet;
1069	unsigned long setup_addr = USBREQ;
1070
1071	r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1072		       DCPMAXP);
1073	r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1074
1075	for (i = 0; i < 4; i++) {
1076		r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1077		setup_addr += 2;
1078	}
1079	r8a66597_write(r8a66597, SUREQ, DCPCTR);
1080}
1081
1082/* this function must be called with interrupt disabled */
1083static void prepare_packet_read(struct r8a66597 *r8a66597,
1084				struct r8a66597_td *td)
1085{
1086	struct urb *urb = td->urb;
1087
1088	if (usb_pipecontrol(urb->pipe)) {
1089		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1090		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1091		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1092		if (urb->actual_length == 0) {
1093			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1094			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1095		}
1096		pipe_irq_disable(r8a66597, td->pipenum);
1097		pipe_start(r8a66597, td->pipe);
1098		pipe_irq_enable(r8a66597, urb, td->pipenum);
1099	} else {
1100		if (urb->actual_length == 0) {
1101			pipe_irq_disable(r8a66597, td->pipenum);
1102			pipe_setting(r8a66597, td);
1103			pipe_stop(r8a66597, td->pipe);
1104			r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1105
1106			if (td->pipe->pipetre) {
1107				r8a66597_write(r8a66597, TRCLR,
1108						td->pipe->pipetre);
1109				r8a66597_write(r8a66597,
1110						DIV_ROUND_UP
1111						  (urb->transfer_buffer_length,
1112						   td->maxpacket),
1113						td->pipe->pipetrn);
1114				r8a66597_bset(r8a66597, TRENB,
1115						td->pipe->pipetre);
1116			}
1117
1118			pipe_start(r8a66597, td->pipe);
1119			pipe_irq_enable(r8a66597, urb, td->pipenum);
1120		}
1121	}
1122}
1123
1124/* this function must be called with interrupt disabled */
1125static void prepare_packet_write(struct r8a66597 *r8a66597,
1126				 struct r8a66597_td *td)
1127{
1128	u16 tmp;
1129	struct urb *urb = td->urb;
1130
1131	if (usb_pipecontrol(urb->pipe)) {
1132		pipe_stop(r8a66597, td->pipe);
1133		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1134		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1135		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1136		if (urb->actual_length == 0) {
1137			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1138			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1139		}
1140	} else {
1141		if (urb->actual_length == 0)
1142			pipe_setting(r8a66597, td);
1143		if (td->pipe->pipetre)
1144			r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1145	}
1146	r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1147
1148	fifo_change_from_pipe(r8a66597, td->pipe);
1149	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1150	if (unlikely((tmp & FRDY) == 0))
1151		pipe_irq_enable(r8a66597, urb, td->pipenum);
1152	else
1153		packet_write(r8a66597, td->pipenum);
1154	pipe_start(r8a66597, td->pipe);
1155}
1156
1157/* this function must be called with interrupt disabled */
1158static void prepare_status_packet(struct r8a66597 *r8a66597,
1159				  struct r8a66597_td *td)
1160{
1161	struct urb *urb = td->urb;
1162
1163	r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1164	pipe_stop(r8a66597, td->pipe);
1165
1166	if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1167		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1168		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1169		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1170		r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1171		r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1172		enable_irq_empty(r8a66597, 0);
1173	} else {
1174		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1175		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1176		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1177		r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1178		enable_irq_ready(r8a66597, 0);
1179	}
1180	enable_irq_nrdy(r8a66597, 0);
1181	pipe_start(r8a66597, td->pipe);
1182}
1183
1184static int is_set_address(unsigned char *setup_packet)
1185{
1186	if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1187			setup_packet[1] == USB_REQ_SET_ADDRESS)
1188		return 1;
1189	else
1190		return 0;
1191}
1192
1193/* this function must be called with interrupt disabled */
1194static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1195{
1196	BUG_ON(!td);
1197
1198	switch (td->type) {
1199	case USB_PID_SETUP:
1200		if (is_set_address(td->urb->setup_packet)) {
1201			td->set_address = 1;
1202			td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1203								     td->urb);
1204			if (td->urb->setup_packet[2] == 0)
1205				return -EPIPE;
1206		}
1207		prepare_setup_packet(r8a66597, td);
1208		break;
1209	case USB_PID_IN:
1210		prepare_packet_read(r8a66597, td);
1211		break;
1212	case USB_PID_OUT:
1213		prepare_packet_write(r8a66597, td);
1214		break;
1215	case USB_PID_ACK:
1216		prepare_status_packet(r8a66597, td);
1217		break;
1218	default:
1219		printk(KERN_ERR "r8a66597: invalid type.\n");
1220		break;
1221	}
1222
1223	return 0;
1224}
1225
1226static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1227{
1228	if (usb_pipeisoc(urb->pipe)) {
1229		if (urb->number_of_packets == td->iso_cnt)
1230			return 1;
1231	}
1232
1233	/* control or bulk or interrupt */
1234	if ((urb->transfer_buffer_length <= urb->actual_length) ||
1235	    (td->short_packet) || (td->zero_packet))
1236		return 1;
1237
1238	return 0;
1239}
1240
1241/* this function must be called with interrupt disabled */
1242static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1243{
1244	unsigned long time;
1245
1246	BUG_ON(!td);
1247
1248	if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1249	    !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1250		r8a66597->timeout_map |= 1 << td->pipenum;
1251		switch (usb_pipetype(td->urb->pipe)) {
1252		case PIPE_INTERRUPT:
1253		case PIPE_ISOCHRONOUS:
1254			time = 30;
1255			break;
1256		default:
1257			time = 50;
1258			break;
1259		}
1260
1261		mod_timer(&r8a66597->timers[td->pipenum].td,
1262			  jiffies + msecs_to_jiffies(time));
1263	}
1264}
1265
1266/* this function must be called with interrupt disabled */
1267static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1268		u16 pipenum, struct urb *urb, int status)
1269__releases(r8a66597->lock) __acquires(r8a66597->lock)
1270{
1271	int restart = 0;
1272	struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1273
1274	r8a66597->timeout_map &= ~(1 << pipenum);
1275
1276	if (likely(td)) {
1277		if (td->set_address && (status != 0 || urb->unlinked))
1278			r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1279
1280		pipe_toggle_save(r8a66597, td->pipe, urb);
1281		list_del(&td->queue);
1282		kfree(td);
1283	}
1284
1285	if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1286		restart = 1;
1287
1288	if (likely(urb)) {
1289		if (usb_pipeisoc(urb->pipe))
1290			urb->start_frame = r8a66597_get_frame(hcd);
1291
1292		r8a66597_urb_done(r8a66597, urb, status);
1293	}
1294
1295	if (restart) {
1296		td = r8a66597_get_td(r8a66597, pipenum);
1297		if (unlikely(!td))
1298			return;
1299
1300		start_transfer(r8a66597, td);
1301		set_td_timer(r8a66597, td);
1302	}
1303}
1304
1305static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1306{
1307	u16 tmp;
1308	int rcv_len, bufsize, urb_len, size;
1309	u16 *buf;
1310	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1311	struct urb *urb;
1312	int finish = 0;
1313	int status = 0;
1314
1315	if (unlikely(!td))
1316		return;
1317	urb = td->urb;
1318
1319	fifo_change_from_pipe(r8a66597, td->pipe);
1320	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1321	if (unlikely((tmp & FRDY) == 0)) {
1322		pipe_stop(r8a66597, td->pipe);
1323		pipe_irq_disable(r8a66597, pipenum);
1324		printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1325		finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1326		return;
1327	}
1328
1329	/* prepare parameters */
1330	rcv_len = tmp & DTLN;
1331	if (usb_pipeisoc(urb->pipe)) {
1332		buf = (u16 *)(urb->transfer_buffer +
1333				urb->iso_frame_desc[td->iso_cnt].offset);
1334		urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1335	} else {
1336		buf = (void *)urb->transfer_buffer + urb->actual_length;
1337		urb_len = urb->transfer_buffer_length - urb->actual_length;
1338	}
1339	bufsize = min(urb_len, (int) td->maxpacket);
1340	if (rcv_len <= bufsize) {
1341		size = rcv_len;
1342	} else {
1343		size = bufsize;
1344		status = -EOVERFLOW;
1345		finish = 1;
1346	}
1347
1348	/* update parameters */
1349	urb->actual_length += size;
1350	if (rcv_len == 0)
1351		td->zero_packet = 1;
1352	if (rcv_len < bufsize) {
1353		td->short_packet = 1;
1354	}
1355	if (usb_pipeisoc(urb->pipe)) {
1356		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1357		urb->iso_frame_desc[td->iso_cnt].status = status;
1358		td->iso_cnt++;
1359		finish = 0;
1360	}
1361
1362	/* check transfer finish */
1363	if (finish || check_transfer_finish(td, urb)) {
1364		pipe_stop(r8a66597, td->pipe);
1365		pipe_irq_disable(r8a66597, pipenum);
1366		finish = 1;
1367	}
1368
1369	/* read fifo */
1370	if (urb->transfer_buffer) {
1371		if (size == 0)
1372			r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1373		else
1374			r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1375					   buf, size);
1376	}
1377
1378	if (finish && pipenum != 0)
1379		finish_request(r8a66597, td, pipenum, urb, status);
1380}
1381
1382static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1383{
1384	u16 tmp;
1385	int bufsize, size;
1386	u16 *buf;
1387	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1388	struct urb *urb;
1389
1390	if (unlikely(!td))
1391		return;
1392	urb = td->urb;
1393
1394	fifo_change_from_pipe(r8a66597, td->pipe);
1395	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1396	if (unlikely((tmp & FRDY) == 0)) {
1397		pipe_stop(r8a66597, td->pipe);
1398		pipe_irq_disable(r8a66597, pipenum);
1399		printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1400		finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1401		return;
1402	}
1403
1404	/* prepare parameters */
1405	bufsize = td->maxpacket;
1406	if (usb_pipeisoc(urb->pipe)) {
1407		buf = (u16 *)(urb->transfer_buffer +
1408				urb->iso_frame_desc[td->iso_cnt].offset);
1409		size = min(bufsize,
1410			   (int)urb->iso_frame_desc[td->iso_cnt].length);
1411	} else {
1412		buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1413		size = min_t(u32, bufsize,
1414			   urb->transfer_buffer_length - urb->actual_length);
1415	}
1416
1417	/* write fifo */
1418	if (pipenum > 0)
1419		r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1420	if (urb->transfer_buffer) {
1421		r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
1422		if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1423			r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1424	}
1425
1426	/* update parameters */
1427	urb->actual_length += size;
1428	if (usb_pipeisoc(urb->pipe)) {
1429		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1430		urb->iso_frame_desc[td->iso_cnt].status = 0;
1431		td->iso_cnt++;
1432	}
1433
1434	/* check transfer finish */
1435	if (check_transfer_finish(td, urb)) {
1436		disable_irq_ready(r8a66597, pipenum);
1437		enable_irq_empty(r8a66597, pipenum);
1438		if (!usb_pipeisoc(urb->pipe))
1439			enable_irq_nrdy(r8a66597, pipenum);
1440	} else
1441		pipe_irq_enable(r8a66597, urb, pipenum);
1442}
1443
1444
1445static void check_next_phase(struct r8a66597 *r8a66597, int status)
1446{
1447	struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1448	struct urb *urb;
1449	u8 finish = 0;
1450
1451	if (unlikely(!td))
1452		return;
1453	urb = td->urb;
1454
1455	switch (td->type) {
1456	case USB_PID_IN:
1457	case USB_PID_OUT:
1458		if (check_transfer_finish(td, urb))
1459			td->type = USB_PID_ACK;
1460		break;
1461	case USB_PID_SETUP:
1462		if (urb->transfer_buffer_length == urb->actual_length)
1463			td->type = USB_PID_ACK;
1464		else if (usb_pipeout(urb->pipe))
1465			td->type = USB_PID_OUT;
1466		else
1467			td->type = USB_PID_IN;
1468		break;
1469	case USB_PID_ACK:
1470		finish = 1;
1471		break;
1472	}
1473
1474	if (finish || status != 0 || urb->unlinked)
1475		finish_request(r8a66597, td, 0, urb, status);
1476	else
1477		start_transfer(r8a66597, td);
1478}
1479
1480static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1481{
1482	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1483
1484	if (td) {
1485		u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1486
1487		if (pid == PID_NAK)
1488			return -ECONNRESET;
1489		else
1490			return -EPIPE;
1491	}
1492	return 0;
1493}
1494
1495static void irq_pipe_ready(struct r8a66597 *r8a66597)
1496{
1497	u16 check;
1498	u16 pipenum;
1499	u16 mask;
1500	struct r8a66597_td *td;
1501
1502	mask = r8a66597_read(r8a66597, BRDYSTS)
1503	       & r8a66597_read(r8a66597, BRDYENB);
1504	r8a66597_write(r8a66597, ~mask, BRDYSTS);
1505	if (mask & BRDY0) {
1506		td = r8a66597_get_td(r8a66597, 0);
1507		if (td && td->type == USB_PID_IN)
1508			packet_read(r8a66597, 0);
1509		else
1510			pipe_irq_disable(r8a66597, 0);
1511		check_next_phase(r8a66597, 0);
1512	}
1513
1514	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1515		check = 1 << pipenum;
1516		if (mask & check) {
1517			td = r8a66597_get_td(r8a66597, pipenum);
1518			if (unlikely(!td))
1519				continue;
1520
1521			if (td->type == USB_PID_IN)
1522				packet_read(r8a66597, pipenum);
1523			else if (td->type == USB_PID_OUT)
1524				packet_write(r8a66597, pipenum);
1525		}
1526	}
1527}
1528
1529static void irq_pipe_empty(struct r8a66597 *r8a66597)
1530{
1531	u16 tmp;
1532	u16 check;
1533	u16 pipenum;
1534	u16 mask;
1535	struct r8a66597_td *td;
1536
1537	mask = r8a66597_read(r8a66597, BEMPSTS)
1538	       & r8a66597_read(r8a66597, BEMPENB);
1539	r8a66597_write(r8a66597, ~mask, BEMPSTS);
1540	if (mask & BEMP0) {
1541		cfifo_change(r8a66597, 0);
1542		td = r8a66597_get_td(r8a66597, 0);
1543		if (td && td->type != USB_PID_OUT)
1544			disable_irq_empty(r8a66597, 0);
1545		check_next_phase(r8a66597, 0);
1546	}
1547
1548	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1549		check = 1 << pipenum;
1550		if (mask &  check) {
1551			struct r8a66597_td *td;
1552			td = r8a66597_get_td(r8a66597, pipenum);
1553			if (unlikely(!td))
1554				continue;
1555
1556			tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1557			if ((tmp & INBUFM) == 0) {
1558				disable_irq_empty(r8a66597, pipenum);
1559				pipe_irq_disable(r8a66597, pipenum);
1560				finish_request(r8a66597, td, pipenum, td->urb,
1561						0);
1562			}
1563		}
1564	}
1565}
1566
1567static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1568{
1569	u16 check;
1570	u16 pipenum;
1571	u16 mask;
1572	int status;
1573
1574	mask = r8a66597_read(r8a66597, NRDYSTS)
1575	       & r8a66597_read(r8a66597, NRDYENB);
1576	r8a66597_write(r8a66597, ~mask, NRDYSTS);
1577	if (mask & NRDY0) {
1578		cfifo_change(r8a66597, 0);
1579		status = get_urb_error(r8a66597, 0);
1580		pipe_irq_disable(r8a66597, 0);
1581		check_next_phase(r8a66597, status);
1582	}
1583
1584	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1585		check = 1 << pipenum;
1586		if (mask & check) {
1587			struct r8a66597_td *td;
1588			td = r8a66597_get_td(r8a66597, pipenum);
1589			if (unlikely(!td))
1590				continue;
1591
1592			status = get_urb_error(r8a66597, pipenum);
1593			pipe_irq_disable(r8a66597, pipenum);
1594			pipe_stop(r8a66597, td->pipe);
1595			finish_request(r8a66597, td, pipenum, td->urb, status);
1596		}
1597	}
1598}
1599
1600static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1601{
1602	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1603	u16 intsts0, intsts1, intsts2;
1604	u16 intenb0, intenb1, intenb2;
1605	u16 mask0, mask1, mask2;
1606	int status;
1607
1608	spin_lock(&r8a66597->lock);
1609
1610	intsts0 = r8a66597_read(r8a66597, INTSTS0);
1611	intsts1 = r8a66597_read(r8a66597, INTSTS1);
1612	intsts2 = r8a66597_read(r8a66597, INTSTS2);
1613	intenb0 = r8a66597_read(r8a66597, INTENB0);
1614	intenb1 = r8a66597_read(r8a66597, INTENB1);
1615	intenb2 = r8a66597_read(r8a66597, INTENB2);
1616
1617	mask2 = intsts2 & intenb2;
1618	mask1 = intsts1 & intenb1;
1619	mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1620	if (mask2) {
1621		if (mask2 & ATTCH) {
1622			r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1623			r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1624
1625			/* start usb bus sampling */
1626			start_root_hub_sampling(r8a66597, 1, 1);
1627		}
1628		if (mask2 & DTCH) {
1629			r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1630			r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1631			r8a66597_usb_disconnect(r8a66597, 1);
1632		}
1633		if (mask2 & BCHG) {
1634			r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1635			r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1636			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1637		}
1638	}
1639
1640	if (mask1) {
1641		if (mask1 & ATTCH) {
1642			r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1643			r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1644
1645			/* start usb bus sampling */
1646			start_root_hub_sampling(r8a66597, 0, 1);
1647		}
1648		if (mask1 & DTCH) {
1649			r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1650			r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1651			r8a66597_usb_disconnect(r8a66597, 0);
1652		}
1653		if (mask1 & BCHG) {
1654			r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1655			r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1656			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1657		}
1658
1659		if (mask1 & SIGN) {
1660			r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1661			status = get_urb_error(r8a66597, 0);
1662			check_next_phase(r8a66597, status);
1663		}
1664		if (mask1 & SACK) {
1665			r8a66597_write(r8a66597, ~SACK, INTSTS1);
1666			check_next_phase(r8a66597, 0);
1667		}
1668	}
1669	if (mask0) {
1670		if (mask0 & BRDY)
1671			irq_pipe_ready(r8a66597);
1672		if (mask0 & BEMP)
1673			irq_pipe_empty(r8a66597);
1674		if (mask0 & NRDY)
1675			irq_pipe_nrdy(r8a66597);
1676	}
1677
1678	spin_unlock(&r8a66597->lock);
1679	return IRQ_HANDLED;
1680}
1681
1682/* this function must be called with interrupt disabled */
1683static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1684{
1685	u16 tmp;
1686	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1687
1688	if (rh->port & USB_PORT_STAT_RESET) {
1689		unsigned long dvstctr_reg = get_dvstctr_reg(port);
1690
1691		tmp = r8a66597_read(r8a66597, dvstctr_reg);
1692		if ((tmp & USBRST) == USBRST) {
1693			r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1694				      dvstctr_reg);
1695			r8a66597_root_hub_start_polling(r8a66597);
1696		} else
1697			r8a66597_usb_connect(r8a66597, port);
1698	}
1699
1700	if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
1701		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1702		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1703	}
1704
1705	if (rh->scount > 0) {
1706		tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1707		if (tmp == rh->old_syssts) {
1708			rh->scount--;
1709			if (rh->scount == 0)
1710				r8a66597_check_syssts(r8a66597, port, tmp);
1711			else
1712				r8a66597_root_hub_start_polling(r8a66597);
1713		} else {
1714			rh->scount = R8A66597_MAX_SAMPLING;
1715			rh->old_syssts = tmp;
1716			r8a66597_root_hub_start_polling(r8a66597);
1717		}
1718	}
1719}
1720
1721static void r8a66597_interval_timer(struct timer_list *t)
1722{
1723	struct r8a66597_timers *timers = from_timer(timers, t, interval);
1724	struct r8a66597 *r8a66597 = timers->r8a66597;
1725	unsigned long flags;
1726	u16 pipenum;
1727	struct r8a66597_td *td;
1728
1729	spin_lock_irqsave(&r8a66597->lock, flags);
1730
1731	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1732		if (!(r8a66597->interval_map & (1 << pipenum)))
1733			continue;
1734		if (timer_pending(&r8a66597->timers[pipenum].interval))
1735			continue;
1736
1737		td = r8a66597_get_td(r8a66597, pipenum);
1738		if (td)
1739			start_transfer(r8a66597, td);
1740	}
1741
1742	spin_unlock_irqrestore(&r8a66597->lock, flags);
1743}
1744
1745static void r8a66597_td_timer(struct timer_list *t)
1746{
1747	struct r8a66597_timers *timers = from_timer(timers, t, td);
1748	struct r8a66597 *r8a66597 = timers->r8a66597;
1749	unsigned long flags;
1750	u16 pipenum;
1751	struct r8a66597_td *td, *new_td = NULL;
1752	struct r8a66597_pipe *pipe;
1753
1754	spin_lock_irqsave(&r8a66597->lock, flags);
1755	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1756		if (!(r8a66597->timeout_map & (1 << pipenum)))
1757			continue;
1758		if (timer_pending(&r8a66597->timers[pipenum].td))
1759			continue;
1760
1761		td = r8a66597_get_td(r8a66597, pipenum);
1762		if (!td) {
1763			r8a66597->timeout_map &= ~(1 << pipenum);
1764			continue;
1765		}
1766
1767		if (td->urb->actual_length) {
1768			set_td_timer(r8a66597, td);
1769			break;
1770		}
1771
1772		pipe = td->pipe;
1773		pipe_stop(r8a66597, pipe);
1774
1775		/* Select a different address or endpoint */
1776		new_td = td;
1777		do {
1778			list_move_tail(&new_td->queue,
1779				       &r8a66597->pipe_queue[pipenum]);
1780			new_td = r8a66597_get_td(r8a66597, pipenum);
1781			if (!new_td) {
1782				new_td = td;
1783				break;
1784			}
1785		} while (td != new_td && td->address == new_td->address &&
1786			td->pipe->info.epnum == new_td->pipe->info.epnum);
1787
1788		start_transfer(r8a66597, new_td);
1789
1790		if (td == new_td)
1791			r8a66597->timeout_map &= ~(1 << pipenum);
1792		else
1793			set_td_timer(r8a66597, new_td);
1794		break;
1795	}
1796	spin_unlock_irqrestore(&r8a66597->lock, flags);
1797}
1798
1799static void r8a66597_timer(struct timer_list *t)
1800{
1801	struct r8a66597 *r8a66597 = from_timer(r8a66597, t, rh_timer);
1802	unsigned long flags;
1803	int port;
1804
1805	spin_lock_irqsave(&r8a66597->lock, flags);
1806
1807	for (port = 0; port < r8a66597->max_root_hub; port++)
1808		r8a66597_root_hub_control(r8a66597, port);
1809
1810	spin_unlock_irqrestore(&r8a66597->lock, flags);
1811}
1812
1813static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1814{
1815	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1816
1817	if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1818	    (urb->dev->state == USB_STATE_CONFIGURED))
1819		return 1;
1820	else
1821		return 0;
1822}
1823
1824static int r8a66597_start(struct usb_hcd *hcd)
1825{
1826	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1827
1828	hcd->state = HC_STATE_RUNNING;
1829	return enable_controller(r8a66597);
1830}
1831
1832static void r8a66597_stop(struct usb_hcd *hcd)
1833{
1834	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1835
1836	disable_controller(r8a66597);
1837}
1838
1839static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1840{
1841	unsigned int usb_address = usb_pipedevice(urb->pipe);
1842	u16 root_port, hub_port;
1843
1844	if (usb_address == 0) {
1845		get_port_number(r8a66597, urb->dev->devpath,
1846				&root_port, &hub_port);
1847		set_devadd_reg(r8a66597, 0,
1848			       get_r8a66597_usb_speed(urb->dev->speed),
1849			       get_parent_r8a66597_address(r8a66597, urb->dev),
1850			       hub_port, root_port);
1851	}
1852}
1853
1854static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1855					    struct urb *urb,
1856					    struct usb_host_endpoint *hep)
1857{
1858	struct r8a66597_td *td;
1859	u16 pipenum;
1860
1861	td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1862	if (td == NULL)
1863		return NULL;
1864
1865	pipenum = r8a66597_get_pipenum(urb, hep);
1866	td->pipenum = pipenum;
1867	td->pipe = hep->hcpriv;
1868	td->urb = urb;
1869	td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1870	td->maxpacket = usb_maxpacket(urb->dev, urb->pipe);
 
1871	if (usb_pipecontrol(urb->pipe))
1872		td->type = USB_PID_SETUP;
1873	else if (usb_pipein(urb->pipe))
1874		td->type = USB_PID_IN;
1875	else
1876		td->type = USB_PID_OUT;
1877	INIT_LIST_HEAD(&td->queue);
1878
1879	return td;
1880}
1881
1882static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1883				struct urb *urb,
1884				gfp_t mem_flags)
1885{
1886	struct usb_host_endpoint *hep = urb->ep;
1887	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1888	struct r8a66597_td *td = NULL;
1889	int ret, request = 0;
1890	unsigned long flags;
1891
1892	spin_lock_irqsave(&r8a66597->lock, flags);
1893	if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1894		ret = -ENODEV;
1895		goto error_not_linked;
1896	}
1897
1898	ret = usb_hcd_link_urb_to_ep(hcd, urb);
1899	if (ret)
1900		goto error_not_linked;
1901
1902	if (!hep->hcpriv) {
1903		hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1904				GFP_ATOMIC);
1905		if (!hep->hcpriv) {
1906			ret = -ENOMEM;
1907			goto error;
1908		}
1909		set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1910		if (usb_pipeendpoint(urb->pipe))
1911			init_pipe_info(r8a66597, urb, hep, &hep->desc);
1912	}
1913
1914	if (unlikely(check_pipe_config(r8a66597, urb)))
1915		init_pipe_config(r8a66597, urb);
1916
1917	set_address_zero(r8a66597, urb);
1918	td = r8a66597_make_td(r8a66597, urb, hep);
1919	if (td == NULL) {
1920		ret = -ENOMEM;
1921		goto error;
1922	}
1923	if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1924		request = 1;
1925	list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1926	urb->hcpriv = td;
1927
1928	if (request) {
1929		if (td->pipe->info.timer_interval) {
1930			r8a66597->interval_map |= 1 << td->pipenum;
1931			mod_timer(&r8a66597->timers[td->pipenum].interval,
1932				  jiffies + msecs_to_jiffies(
1933					td->pipe->info.timer_interval));
1934		} else {
1935			ret = start_transfer(r8a66597, td);
1936			if (ret < 0) {
1937				list_del(&td->queue);
1938				kfree(td);
1939			}
1940		}
1941	} else
1942		set_td_timer(r8a66597, td);
1943
1944error:
1945	if (ret)
1946		usb_hcd_unlink_urb_from_ep(hcd, urb);
1947error_not_linked:
1948	spin_unlock_irqrestore(&r8a66597->lock, flags);
1949	return ret;
1950}
1951
1952static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1953		int status)
1954{
1955	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1956	struct r8a66597_td *td;
1957	unsigned long flags;
1958	int rc;
1959
1960	spin_lock_irqsave(&r8a66597->lock, flags);
1961	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1962	if (rc)
1963		goto done;
1964
1965	if (urb->hcpriv) {
1966		td = urb->hcpriv;
1967		pipe_stop(r8a66597, td->pipe);
1968		pipe_irq_disable(r8a66597, td->pipenum);
1969		disable_irq_empty(r8a66597, td->pipenum);
1970		finish_request(r8a66597, td, td->pipenum, urb, status);
1971	}
1972 done:
1973	spin_unlock_irqrestore(&r8a66597->lock, flags);
1974	return rc;
1975}
1976
1977static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1978				      struct usb_host_endpoint *hep)
1979__acquires(r8a66597->lock)
1980__releases(r8a66597->lock)
1981{
1982	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1983	struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1984	struct r8a66597_td *td;
1985	struct urb *urb = NULL;
1986	u16 pipenum;
1987	unsigned long flags;
1988
1989	if (pipe == NULL)
1990		return;
1991	pipenum = pipe->info.pipenum;
1992
1993	spin_lock_irqsave(&r8a66597->lock, flags);
1994	if (pipenum == 0) {
1995		kfree(hep->hcpriv);
1996		hep->hcpriv = NULL;
1997		spin_unlock_irqrestore(&r8a66597->lock, flags);
1998		return;
1999	}
2000
 
2001	pipe_stop(r8a66597, pipe);
2002	pipe_irq_disable(r8a66597, pipenum);
2003	disable_irq_empty(r8a66597, pipenum);
2004	td = r8a66597_get_td(r8a66597, pipenum);
2005	if (td)
2006		urb = td->urb;
2007	finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
2008	kfree(hep->hcpriv);
2009	hep->hcpriv = NULL;
2010	spin_unlock_irqrestore(&r8a66597->lock, flags);
2011}
2012
2013static int r8a66597_get_frame(struct usb_hcd *hcd)
2014{
2015	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2016	return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2017}
2018
2019static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2020{
2021	int chix;
2022	struct usb_device *childdev;
2023
2024	if (udev->state == USB_STATE_CONFIGURED &&
2025	    udev->parent && udev->parent->devnum > 1 &&
2026	    udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2027		map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2028
2029	usb_hub_for_each_child(udev, chix, childdev)
2030		collect_usb_address_map(childdev, map);
2031}
2032
2033/* this function must be called with interrupt disabled */
2034static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2035						   int addr)
2036{
2037	struct r8a66597_device *dev;
2038	struct list_head *list = &r8a66597->child_device;
2039
2040	list_for_each_entry(dev, list, device_list) {
2041		if (dev->usb_address != addr)
2042			continue;
2043
2044		return dev;
2045	}
2046
2047	printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2048	return NULL;
2049}
2050
2051static void update_usb_address_map(struct r8a66597 *r8a66597,
2052				   struct usb_device *root_hub,
2053				   unsigned long *map)
2054{
2055	int i, j, addr;
2056	unsigned long diff;
2057	unsigned long flags;
2058
2059	for (i = 0; i < 4; i++) {
2060		diff = r8a66597->child_connect_map[i] ^ map[i];
2061		if (!diff)
2062			continue;
2063
2064		for (j = 0; j < 32; j++) {
2065			if (!(diff & (1 << j)))
2066				continue;
2067
2068			addr = i * 32 + j;
2069			if (map[i] & (1 << j))
2070				set_child_connect_map(r8a66597, addr);
2071			else {
2072				struct r8a66597_device *dev;
2073
2074				spin_lock_irqsave(&r8a66597->lock, flags);
2075				dev = get_r8a66597_device(r8a66597, addr);
2076				disable_r8a66597_pipe_all(r8a66597, dev);
2077				free_usb_address(r8a66597, dev, 0);
2078				put_child_connect_map(r8a66597, addr);
2079				spin_unlock_irqrestore(&r8a66597->lock, flags);
2080			}
2081		}
2082	}
2083}
2084
2085static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2086					struct usb_hcd *hcd)
2087{
2088	struct usb_bus *bus;
2089	unsigned long now_map[4];
2090
2091	memset(now_map, 0, sizeof(now_map));
2092
2093	mutex_lock(&usb_bus_idr_lock);
2094	bus = idr_find(&usb_bus_idr, hcd->self.busnum);
2095	if (bus && bus->root_hub) {
 
 
 
 
2096		collect_usb_address_map(bus->root_hub, now_map);
2097		update_usb_address_map(r8a66597, bus->root_hub, now_map);
2098	}
2099	mutex_unlock(&usb_bus_idr_lock);
2100}
2101
2102static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2103{
2104	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2105	unsigned long flags;
2106	int i;
2107
2108	r8a66597_check_detect_child(r8a66597, hcd);
2109
2110	spin_lock_irqsave(&r8a66597->lock, flags);
2111
2112	*buf = 0;	/* initialize (no change) */
2113
2114	for (i = 0; i < r8a66597->max_root_hub; i++) {
2115		if (r8a66597->root_hub[i].port & 0xffff0000)
2116			*buf |= 1 << (i + 1);
2117	}
2118
2119	spin_unlock_irqrestore(&r8a66597->lock, flags);
2120
2121	return (*buf != 0);
2122}
2123
2124static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2125				    struct usb_hub_descriptor *desc)
2126{
2127	desc->bDescriptorType = USB_DT_HUB;
2128	desc->bHubContrCurrent = 0;
2129	desc->bNbrPorts = r8a66597->max_root_hub;
2130	desc->bDescLength = 9;
2131	desc->bPwrOn2PwrGood = 0;
2132	desc->wHubCharacteristics =
2133		cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
2134	desc->u.hs.DeviceRemovable[0] =
2135		((1 << r8a66597->max_root_hub) - 1) << 1;
2136	desc->u.hs.DeviceRemovable[1] = ~0;
2137}
2138
2139static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2140				u16 wIndex, char *buf, u16 wLength)
2141{
2142	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2143	int ret;
2144	int port = (wIndex & 0x00FF) - 1;
2145	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2146	unsigned long flags;
2147
2148	ret = 0;
2149
2150	spin_lock_irqsave(&r8a66597->lock, flags);
2151	switch (typeReq) {
2152	case ClearHubFeature:
2153	case SetHubFeature:
2154		switch (wValue) {
2155		case C_HUB_OVER_CURRENT:
2156		case C_HUB_LOCAL_POWER:
2157			break;
2158		default:
2159			goto error;
2160		}
2161		break;
2162	case ClearPortFeature:
2163		if (wIndex > r8a66597->max_root_hub)
2164			goto error;
2165		if (wLength != 0)
2166			goto error;
2167
2168		switch (wValue) {
2169		case USB_PORT_FEAT_ENABLE:
2170			rh->port &= ~USB_PORT_STAT_POWER;
2171			break;
2172		case USB_PORT_FEAT_SUSPEND:
2173			break;
2174		case USB_PORT_FEAT_POWER:
2175			r8a66597_port_power(r8a66597, port, 0);
2176			break;
2177		case USB_PORT_FEAT_C_ENABLE:
2178		case USB_PORT_FEAT_C_SUSPEND:
2179		case USB_PORT_FEAT_C_CONNECTION:
2180		case USB_PORT_FEAT_C_OVER_CURRENT:
2181		case USB_PORT_FEAT_C_RESET:
2182			break;
2183		default:
2184			goto error;
2185		}
2186		rh->port &= ~(1 << wValue);
2187		break;
2188	case GetHubDescriptor:
2189		r8a66597_hub_descriptor(r8a66597,
2190					(struct usb_hub_descriptor *)buf);
2191		break;
2192	case GetHubStatus:
2193		*buf = 0x00;
2194		break;
2195	case GetPortStatus:
2196		if (wIndex > r8a66597->max_root_hub)
2197			goto error;
2198		*(__le32 *)buf = cpu_to_le32(rh->port);
2199		break;
2200	case SetPortFeature:
2201		if (wIndex > r8a66597->max_root_hub)
2202			goto error;
2203		if (wLength != 0)
2204			goto error;
2205
2206		switch (wValue) {
2207		case USB_PORT_FEAT_SUSPEND:
2208			break;
2209		case USB_PORT_FEAT_POWER:
2210			r8a66597_port_power(r8a66597, port, 1);
2211			rh->port |= USB_PORT_STAT_POWER;
2212			break;
2213		case USB_PORT_FEAT_RESET: {
2214			struct r8a66597_device *dev = rh->dev;
2215
2216			rh->port |= USB_PORT_STAT_RESET;
2217
2218			disable_r8a66597_pipe_all(r8a66597, dev);
2219			free_usb_address(r8a66597, dev, 1);
2220
2221			r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2222				      get_dvstctr_reg(port));
2223			mod_timer(&r8a66597->rh_timer,
2224				  jiffies + msecs_to_jiffies(50));
2225			}
2226			break;
2227		default:
2228			goto error;
2229		}
2230		rh->port |= 1 << wValue;
2231		break;
2232	default:
2233error:
2234		ret = -EPIPE;
2235		break;
2236	}
2237
2238	spin_unlock_irqrestore(&r8a66597->lock, flags);
2239	return ret;
2240}
2241
2242#if defined(CONFIG_PM)
2243static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2244{
2245	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2246	int port;
2247
2248	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2249
2250	for (port = 0; port < r8a66597->max_root_hub; port++) {
2251		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2252		unsigned long dvstctr_reg = get_dvstctr_reg(port);
2253
2254		if (!(rh->port & USB_PORT_STAT_ENABLE))
2255			continue;
2256
2257		dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
2258		r8a66597_bclr(r8a66597, UACT, dvstctr_reg);	/* suspend */
2259		rh->port |= USB_PORT_STAT_SUSPEND;
2260
2261		if (rh->dev->udev->do_remote_wakeup) {
2262			msleep(3);	/* waiting last SOF */
2263			r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2264			r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2265			r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2266		}
2267	}
2268
2269	r8a66597->bus_suspended = 1;
2270
2271	return 0;
2272}
2273
2274static int r8a66597_bus_resume(struct usb_hcd *hcd)
2275{
2276	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2277	int port;
2278
2279	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2280
2281	for (port = 0; port < r8a66597->max_root_hub; port++) {
2282		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2283		unsigned long dvstctr_reg = get_dvstctr_reg(port);
2284
2285		if (!(rh->port & USB_PORT_STAT_SUSPEND))
2286			continue;
2287
2288		dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
2289		rh->port &= ~USB_PORT_STAT_SUSPEND;
2290		rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
2291		r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2292		msleep(USB_RESUME_TIMEOUT);
2293		r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2294	}
2295
2296	return 0;
2297
2298}
2299#else
2300#define	r8a66597_bus_suspend	NULL
2301#define	r8a66597_bus_resume	NULL
2302#endif
2303
2304static const struct hc_driver r8a66597_hc_driver = {
2305	.description =		hcd_name,
2306	.hcd_priv_size =	sizeof(struct r8a66597),
2307	.irq =			r8a66597_irq,
2308
2309	/*
2310	 * generic hardware linkage
2311	 */
2312	.flags =		HCD_USB2,
2313
2314	.start =		r8a66597_start,
2315	.stop =			r8a66597_stop,
2316
2317	/*
2318	 * managing i/o requests and associated device resources
2319	 */
2320	.urb_enqueue =		r8a66597_urb_enqueue,
2321	.urb_dequeue =		r8a66597_urb_dequeue,
2322	.endpoint_disable =	r8a66597_endpoint_disable,
2323
2324	/*
2325	 * periodic schedule support
2326	 */
2327	.get_frame_number =	r8a66597_get_frame,
2328
2329	/*
2330	 * root hub support
2331	 */
2332	.hub_status_data =	r8a66597_hub_status_data,
2333	.hub_control =		r8a66597_hub_control,
2334	.bus_suspend =		r8a66597_bus_suspend,
2335	.bus_resume =		r8a66597_bus_resume,
2336};
2337
2338#if defined(CONFIG_PM)
2339static int r8a66597_suspend(struct device *dev)
2340{
2341	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2342	int port;
2343
2344	dev_dbg(dev, "%s\n", __func__);
2345
2346	disable_controller(r8a66597);
2347
2348	for (port = 0; port < r8a66597->max_root_hub; port++) {
2349		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2350
2351		rh->port = 0x00000000;
2352	}
2353
2354	return 0;
2355}
2356
2357static int r8a66597_resume(struct device *dev)
2358{
2359	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2360	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);
2361
2362	dev_dbg(dev, "%s\n", __func__);
2363
2364	enable_controller(r8a66597);
2365	usb_root_hub_lost_power(hcd->self.root_hub);
2366
2367	return 0;
2368}
2369
2370static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2371	.suspend = r8a66597_suspend,
2372	.resume = r8a66597_resume,
2373	.poweroff = r8a66597_suspend,
2374	.restore = r8a66597_resume,
2375};
2376
2377#define R8A66597_DEV_PM_OPS	(&r8a66597_dev_pm_ops)
2378#else	/* if defined(CONFIG_PM) */
2379#define R8A66597_DEV_PM_OPS	NULL
2380#endif
2381
2382static void r8a66597_remove(struct platform_device *pdev)
2383{
2384	struct r8a66597		*r8a66597 = platform_get_drvdata(pdev);
2385	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);
2386
2387	del_timer_sync(&r8a66597->rh_timer);
2388	usb_remove_hcd(hcd);
2389	iounmap(r8a66597->reg);
2390	if (r8a66597->pdata->on_chip)
2391		clk_put(r8a66597->clk);
2392	usb_put_hcd(hcd);
 
2393}
2394
2395static int r8a66597_probe(struct platform_device *pdev)
2396{
2397	char clk_name[8];
2398	struct resource *res = NULL, *ires;
2399	int irq = -1;
2400	void __iomem *reg = NULL;
2401	struct usb_hcd *hcd = NULL;
2402	struct r8a66597 *r8a66597;
2403	int ret = 0;
2404	int i;
2405	unsigned long irq_trigger;
2406
2407	if (usb_disabled())
2408		return -ENODEV;
2409
 
 
 
 
 
 
2410	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2411	if (!res) {
2412		ret = -ENODEV;
2413		dev_err(&pdev->dev, "platform_get_resource error.\n");
2414		goto clean_up;
2415	}
2416
2417	ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2418	if (!ires) {
2419		ret = -ENODEV;
2420		dev_err(&pdev->dev,
2421			"platform_get_resource IORESOURCE_IRQ error.\n");
2422		goto clean_up;
2423	}
2424
2425	irq = ires->start;
2426	irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2427
2428	reg = ioremap(res->start, resource_size(res));
2429	if (reg == NULL) {
2430		ret = -ENOMEM;
2431		dev_err(&pdev->dev, "ioremap error.\n");
2432		goto clean_up;
2433	}
2434
2435	if (pdev->dev.platform_data == NULL) {
2436		dev_err(&pdev->dev, "no platform data\n");
2437		ret = -ENODEV;
2438		goto clean_up;
2439	}
2440
2441	/* initialize hcd */
2442	hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2443	if (!hcd) {
2444		ret = -ENOMEM;
2445		dev_err(&pdev->dev, "Failed to create hcd\n");
2446		goto clean_up;
2447	}
2448	r8a66597 = hcd_to_r8a66597(hcd);
2449	memset(r8a66597, 0, sizeof(struct r8a66597));
2450	platform_set_drvdata(pdev, r8a66597);
2451	r8a66597->pdata = dev_get_platdata(&pdev->dev);
2452	r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2453
2454	if (r8a66597->pdata->on_chip) {
2455		snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2456		r8a66597->clk = clk_get(&pdev->dev, clk_name);
2457		if (IS_ERR(r8a66597->clk)) {
2458			dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2459				clk_name);
2460			ret = PTR_ERR(r8a66597->clk);
2461			goto clean_up2;
2462		}
2463		r8a66597->max_root_hub = 1;
2464	} else
2465		r8a66597->max_root_hub = 2;
2466
2467	spin_lock_init(&r8a66597->lock);
2468	timer_setup(&r8a66597->rh_timer, r8a66597_timer, 0);
 
 
2469	r8a66597->reg = reg;
2470
2471	/* make sure no interrupts are pending */
2472	ret = r8a66597_clock_enable(r8a66597);
2473	if (ret < 0)
2474		goto clean_up3;
2475	disable_controller(r8a66597);
2476
2477	for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2478		INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2479		r8a66597->timers[i].r8a66597 = r8a66597;
2480		timer_setup(&r8a66597->timers[i].td, r8a66597_td_timer, 0);
2481		timer_setup(&r8a66597->timers[i].interval,
2482			    r8a66597_interval_timer, 0);
 
 
2483	}
2484	INIT_LIST_HEAD(&r8a66597->child_device);
2485
2486	hcd->rsrc_start = res->start;
2487	hcd->has_tt = 1;
2488
2489	ret = usb_add_hcd(hcd, irq, irq_trigger);
2490	if (ret != 0) {
2491		dev_err(&pdev->dev, "Failed to add hcd\n");
2492		goto clean_up3;
2493	}
2494	device_wakeup_enable(hcd->self.controller);
2495
2496	return 0;
2497
2498clean_up3:
2499	if (r8a66597->pdata->on_chip)
2500		clk_put(r8a66597->clk);
2501clean_up2:
2502	usb_put_hcd(hcd);
2503
2504clean_up:
2505	if (reg)
2506		iounmap(reg);
2507
2508	return ret;
2509}
2510
2511static struct platform_driver r8a66597_driver = {
2512	.probe =	r8a66597_probe,
2513	.remove_new =	r8a66597_remove,
2514	.driver		= {
2515		.name = hcd_name,
 
2516		.pm	= R8A66597_DEV_PM_OPS,
2517	},
2518};
2519
2520module_platform_driver(r8a66597_driver);
v3.15
 
   1/*
   2 * R8A66597 HCD (Host Controller Driver)
   3 *
   4 * Copyright (C) 2006-2007 Renesas Solutions Corp.
   5 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
   6 * Portions Copyright (C) 2004-2005 David Brownell
   7 * Portions Copyright (C) 1999 Roman Weissgaerber
   8 *
   9 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; version 2 of the License.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  23 *
  24 */
  25
  26#include <linux/module.h>
  27#include <linux/kernel.h>
  28#include <linux/sched.h>
  29#include <linux/errno.h>
  30#include <linux/timer.h>
  31#include <linux/delay.h>
  32#include <linux/list.h>
  33#include <linux/interrupt.h>
  34#include <linux/usb.h>
  35#include <linux/usb/hcd.h>
  36#include <linux/platform_device.h>
  37#include <linux/io.h>
  38#include <linux/mm.h>
  39#include <linux/irq.h>
  40#include <linux/slab.h>
  41#include <asm/cacheflush.h>
  42
  43#include "r8a66597.h"
  44
  45MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
  46MODULE_LICENSE("GPL");
  47MODULE_AUTHOR("Yoshihiro Shimoda");
  48MODULE_ALIAS("platform:r8a66597_hcd");
  49
  50#define DRIVER_VERSION	"2009-05-26"
  51
  52static const char hcd_name[] = "r8a66597_hcd";
  53
  54static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
  55static int r8a66597_get_frame(struct usb_hcd *hcd);
  56
  57/* this function must be called with interrupt disabled */
  58static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  59			    unsigned long reg)
  60{
  61	u16 tmp;
  62
  63	tmp = r8a66597_read(r8a66597, INTENB0);
  64	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  65	r8a66597_bset(r8a66597, 1 << pipenum, reg);
  66	r8a66597_write(r8a66597, tmp, INTENB0);
  67}
  68
  69/* this function must be called with interrupt disabled */
  70static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  71			     unsigned long reg)
  72{
  73	u16 tmp;
  74
  75	tmp = r8a66597_read(r8a66597, INTENB0);
  76	r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  77	r8a66597_bclr(r8a66597, 1 << pipenum, reg);
  78	r8a66597_write(r8a66597, tmp, INTENB0);
  79}
  80
  81static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
  82			   u16 usbspd, u8 upphub, u8 hubport, int port)
  83{
  84	u16 val;
  85	unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
  86
  87	val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
  88	r8a66597_write(r8a66597, val, devadd_reg);
  89}
  90
  91static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
  92{
  93	u16 tmp;
  94	int i = 0;
  95
  96	if (r8a66597->pdata->on_chip) {
  97		clk_prepare_enable(r8a66597->clk);
  98		do {
  99			r8a66597_write(r8a66597, SCKE, SYSCFG0);
 100			tmp = r8a66597_read(r8a66597, SYSCFG0);
 101			if (i++ > 1000) {
 102				printk(KERN_ERR "r8a66597: reg access fail.\n");
 103				return -ENXIO;
 104			}
 105		} while ((tmp & SCKE) != SCKE);
 106		r8a66597_write(r8a66597, 0x04, 0x02);
 107	} else {
 108		do {
 109			r8a66597_write(r8a66597, USBE, SYSCFG0);
 110			tmp = r8a66597_read(r8a66597, SYSCFG0);
 111			if (i++ > 1000) {
 112				printk(KERN_ERR "r8a66597: reg access fail.\n");
 113				return -ENXIO;
 114			}
 115		} while ((tmp & USBE) != USBE);
 116		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 117		r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
 118			      XTAL, SYSCFG0);
 119
 120		i = 0;
 121		r8a66597_bset(r8a66597, XCKE, SYSCFG0);
 122		do {
 123			msleep(1);
 124			tmp = r8a66597_read(r8a66597, SYSCFG0);
 125			if (i++ > 500) {
 126				printk(KERN_ERR "r8a66597: reg access fail.\n");
 127				return -ENXIO;
 128			}
 129		} while ((tmp & SCKE) != SCKE);
 130	}
 131
 132	return 0;
 133}
 134
 135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
 136{
 137	r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
 138	udelay(1);
 139
 140	if (r8a66597->pdata->on_chip) {
 141		clk_disable_unprepare(r8a66597->clk);
 142	} else {
 143		r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
 144		r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
 145		r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 146	}
 147}
 148
 149static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
 150{
 151	u16 val;
 152
 153	val = port ? DRPD : DCFM | DRPD;
 154	r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
 155	r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
 156
 157	r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
 158	r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
 159	r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
 160}
 161
 162static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
 163{
 164	u16 val, tmp;
 165
 166	r8a66597_write(r8a66597, 0, get_intenb_reg(port));
 167	r8a66597_write(r8a66597, 0, get_intsts_reg(port));
 168
 169	r8a66597_port_power(r8a66597, port, 0);
 170
 171	do {
 172		tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
 173		udelay(640);
 174	} while (tmp == EDGESTS);
 175
 176	val = port ? DRPD : DCFM | DRPD;
 177	r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
 178	r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
 179}
 180
 181static int enable_controller(struct r8a66597 *r8a66597)
 182{
 183	int ret, port;
 184	u16 vif = r8a66597->pdata->vif ? LDRV : 0;
 185	u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
 186	u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
 187
 188	ret = r8a66597_clock_enable(r8a66597);
 189	if (ret < 0)
 190		return ret;
 191
 192	r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
 193	r8a66597_bset(r8a66597, USBE, SYSCFG0);
 194
 195	r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
 196	r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
 197	r8a66597_bset(r8a66597, BRDY0, BRDYENB);
 198	r8a66597_bset(r8a66597, BEMP0, BEMPENB);
 199
 200	r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
 201	r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
 202	r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
 203	r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
 204
 205	r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
 206
 207	for (port = 0; port < r8a66597->max_root_hub; port++)
 208		r8a66597_enable_port(r8a66597, port);
 209
 210	return 0;
 211}
 212
 213static void disable_controller(struct r8a66597 *r8a66597)
 214{
 215	int port;
 216
 217	/* disable interrupts */
 218	r8a66597_write(r8a66597, 0, INTENB0);
 219	r8a66597_write(r8a66597, 0, INTENB1);
 220	r8a66597_write(r8a66597, 0, BRDYENB);
 221	r8a66597_write(r8a66597, 0, BEMPENB);
 222	r8a66597_write(r8a66597, 0, NRDYENB);
 223
 224	/* clear status */
 225	r8a66597_write(r8a66597, 0, BRDYSTS);
 226	r8a66597_write(r8a66597, 0, NRDYSTS);
 227	r8a66597_write(r8a66597, 0, BEMPSTS);
 228
 229	for (port = 0; port < r8a66597->max_root_hub; port++)
 230		r8a66597_disable_port(r8a66597, port);
 231
 232	r8a66597_clock_disable(r8a66597);
 233}
 234
 235static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
 236				       struct usb_device *udev)
 237{
 238	struct r8a66597_device *dev;
 239
 240	if (udev->parent && udev->parent->devnum != 1)
 241		udev = udev->parent;
 242
 243	dev = dev_get_drvdata(&udev->dev);
 244	if (dev)
 245		return dev->address;
 246	else
 247		return 0;
 248}
 249
 250static int is_child_device(char *devpath)
 251{
 252	return (devpath[2] ? 1 : 0);
 253}
 254
 255static int is_hub_limit(char *devpath)
 256{
 257	return ((strlen(devpath) >= 4) ? 1 : 0);
 258}
 259
 260static void get_port_number(struct r8a66597 *r8a66597,
 261			    char *devpath, u16 *root_port, u16 *hub_port)
 262{
 263	if (root_port) {
 264		*root_port = (devpath[0] & 0x0F) - 1;
 265		if (*root_port >= r8a66597->max_root_hub)
 266			printk(KERN_ERR "r8a66597: Illegal root port number.\n");
 267	}
 268	if (hub_port)
 269		*hub_port = devpath[2] & 0x0F;
 270}
 271
 272static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
 273{
 274	u16 usbspd = 0;
 275
 276	switch (speed) {
 277	case USB_SPEED_LOW:
 278		usbspd = LSMODE;
 279		break;
 280	case USB_SPEED_FULL:
 281		usbspd = FSMODE;
 282		break;
 283	case USB_SPEED_HIGH:
 284		usbspd = HSMODE;
 285		break;
 286	default:
 287		printk(KERN_ERR "r8a66597: unknown speed\n");
 288		break;
 289	}
 290
 291	return usbspd;
 292}
 293
 294static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
 295{
 296	int idx;
 297
 298	idx = address / 32;
 299	r8a66597->child_connect_map[idx] |= 1 << (address % 32);
 300}
 301
 302static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
 303{
 304	int idx;
 305
 306	idx = address / 32;
 307	r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
 308}
 309
 310static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
 311{
 312	u16 pipenum = pipe->info.pipenum;
 313	const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
 314	const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
 315	const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
 316
 317	if (dma_ch > R8A66597_PIPE_NO_DMA)	/* dma fifo not use? */
 318		dma_ch = R8A66597_PIPE_NO_DMA;
 319
 320	pipe->fifoaddr = fifoaddr[dma_ch];
 321	pipe->fifosel = fifosel[dma_ch];
 322	pipe->fifoctr = fifoctr[dma_ch];
 323
 324	if (pipenum == 0)
 325		pipe->pipectr = DCPCTR;
 326	else
 327		pipe->pipectr = get_pipectr_addr(pipenum);
 328
 329	if (check_bulk_or_isoc(pipenum)) {
 330		pipe->pipetre = get_pipetre_addr(pipenum);
 331		pipe->pipetrn = get_pipetrn_addr(pipenum);
 332	} else {
 333		pipe->pipetre = 0;
 334		pipe->pipetrn = 0;
 335	}
 336}
 337
 338static struct r8a66597_device *
 339get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
 340{
 341	if (usb_pipedevice(urb->pipe) == 0)
 342		return &r8a66597->device0;
 343
 344	return dev_get_drvdata(&urb->dev->dev);
 345}
 346
 347static int make_r8a66597_device(struct r8a66597 *r8a66597,
 348				struct urb *urb, u8 addr)
 349{
 350	struct r8a66597_device *dev;
 351	int usb_address = urb->setup_packet[2];	/* urb->pipe is address 0 */
 352
 353	dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
 354	if (dev == NULL)
 355		return -ENOMEM;
 356
 357	dev_set_drvdata(&urb->dev->dev, dev);
 358	dev->udev = urb->dev;
 359	dev->address = addr;
 360	dev->usb_address = usb_address;
 361	dev->state = USB_STATE_ADDRESS;
 362	dev->ep_in_toggle = 0;
 363	dev->ep_out_toggle = 0;
 364	INIT_LIST_HEAD(&dev->device_list);
 365	list_add_tail(&dev->device_list, &r8a66597->child_device);
 366
 367	get_port_number(r8a66597, urb->dev->devpath,
 368			&dev->root_port, &dev->hub_port);
 369	if (!is_child_device(urb->dev->devpath))
 370		r8a66597->root_hub[dev->root_port].dev = dev;
 371
 372	set_devadd_reg(r8a66597, dev->address,
 373		       get_r8a66597_usb_speed(urb->dev->speed),
 374		       get_parent_r8a66597_address(r8a66597, urb->dev),
 375		       dev->hub_port, dev->root_port);
 376
 377	return 0;
 378}
 379
 380/* this function must be called with interrupt disabled */
 381static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
 382{
 383	u8 addr;	/* R8A66597's address */
 384	struct r8a66597_device *dev;
 385
 386	if (is_hub_limit(urb->dev->devpath)) {
 387		dev_err(&urb->dev->dev, "External hub limit reached.\n");
 388		return 0;
 389	}
 390
 391	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 392	if (dev && dev->state >= USB_STATE_ADDRESS)
 393		return dev->address;
 394
 395	for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
 396		if (r8a66597->address_map & (1 << addr))
 397			continue;
 398
 399		dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
 400		r8a66597->address_map |= 1 << addr;
 401
 402		if (make_r8a66597_device(r8a66597, urb, addr) < 0)
 403			return 0;
 404
 405		return addr;
 406	}
 407
 408	dev_err(&urb->dev->dev,
 409		"cannot communicate with a USB device more than 10.(%x)\n",
 410		r8a66597->address_map);
 411
 412	return 0;
 413}
 414
 415/* this function must be called with interrupt disabled */
 416static void free_usb_address(struct r8a66597 *r8a66597,
 417			     struct r8a66597_device *dev, int reset)
 418{
 419	int port;
 420
 421	if (!dev)
 422		return;
 423
 424	dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
 425
 426	dev->state = USB_STATE_DEFAULT;
 427	r8a66597->address_map &= ~(1 << dev->address);
 428	dev->address = 0;
 429	/*
 430	 * Only when resetting USB, it is necessary to erase drvdata. When
 431	 * a usb device with usb hub is disconnect, "dev->udev" is already
 432	 * freed on usb_desconnect(). So we cannot access the data.
 433	 */
 434	if (reset)
 435		dev_set_drvdata(&dev->udev->dev, NULL);
 436	list_del(&dev->device_list);
 437	kfree(dev);
 438
 439	for (port = 0; port < r8a66597->max_root_hub; port++) {
 440		if (r8a66597->root_hub[port].dev == dev) {
 441			r8a66597->root_hub[port].dev = NULL;
 442			break;
 443		}
 444	}
 445}
 446
 447static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
 448			      u16 mask, u16 loop)
 449{
 450	u16 tmp;
 451	int i = 0;
 452
 453	do {
 454		tmp = r8a66597_read(r8a66597, reg);
 455		if (i++ > 1000000) {
 456			printk(KERN_ERR "r8a66597: register%lx, loop %x "
 457			       "is timeout\n", reg, loop);
 458			break;
 459		}
 460		ndelay(1);
 461	} while ((tmp & mask) != loop);
 462}
 463
 464/* this function must be called with interrupt disabled */
 465static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
 466{
 467	u16 tmp;
 468
 469	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
 470	if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
 471		r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
 472	r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
 473}
 474
 475/* this function must be called with interrupt disabled */
 476static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
 477{
 478	u16 tmp;
 479
 480	tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
 481	if ((tmp & PID_STALL11) != PID_STALL11)	/* force stall? */
 482		r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
 483	r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
 484	r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
 485}
 486
 487/* this function must be called with interrupt disabled */
 488static void clear_all_buffer(struct r8a66597 *r8a66597,
 489			     struct r8a66597_pipe *pipe)
 490{
 491	u16 tmp;
 492
 493	if (!pipe || pipe->info.pipenum == 0)
 494		return;
 495
 496	pipe_stop(r8a66597, pipe);
 497	r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
 498	tmp = r8a66597_read(r8a66597, pipe->pipectr);
 499	tmp = r8a66597_read(r8a66597, pipe->pipectr);
 500	tmp = r8a66597_read(r8a66597, pipe->pipectr);
 501	r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
 502}
 503
 504/* this function must be called with interrupt disabled */
 505static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
 506				 struct r8a66597_pipe *pipe, int toggle)
 507{
 508	if (toggle)
 509		r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
 510	else
 511		r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
 512}
 513
 514static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
 515{
 516	if (r8a66597->pdata->on_chip)
 517		return MBW_32;
 518	else
 519		return MBW_16;
 520}
 521
 522/* this function must be called with interrupt disabled */
 523static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
 524{
 525	unsigned short mbw = mbw_value(r8a66597);
 526
 527	r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
 528	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
 529}
 530
 531/* this function must be called with interrupt disabled */
 532static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
 533					 struct r8a66597_pipe *pipe)
 534{
 535	unsigned short mbw = mbw_value(r8a66597);
 536
 537	cfifo_change(r8a66597, 0);
 538	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
 539	r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
 540
 541	r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
 542		      pipe->fifosel);
 543	r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
 544}
 545
 546static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
 547{
 548	struct r8a66597_pipe *pipe = hep->hcpriv;
 549
 550	if (usb_pipeendpoint(urb->pipe) == 0)
 551		return 0;
 552	else
 553		return pipe->info.pipenum;
 554}
 555
 556static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
 557{
 558	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 559
 560	return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
 561}
 562
 563static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
 564					  int urb_pipe)
 565{
 566	if (!dev)
 567		return NULL;
 568
 569	return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
 570}
 571
 572/* this function must be called with interrupt disabled */
 573static void pipe_toggle_set(struct r8a66597 *r8a66597,
 574			    struct r8a66597_pipe *pipe,
 575			    struct urb *urb, int set)
 576{
 577	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 578	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
 579	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
 580
 581	if (!toggle)
 582		return;
 583
 584	if (set)
 585		*toggle |= 1 << endpoint;
 586	else
 587		*toggle &= ~(1 << endpoint);
 588}
 589
 590/* this function must be called with interrupt disabled */
 591static void pipe_toggle_save(struct r8a66597 *r8a66597,
 592			     struct r8a66597_pipe *pipe,
 593			     struct urb *urb)
 594{
 595	if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
 596		pipe_toggle_set(r8a66597, pipe, urb, 1);
 597	else
 598		pipe_toggle_set(r8a66597, pipe, urb, 0);
 599}
 600
 601/* this function must be called with interrupt disabled */
 602static void pipe_toggle_restore(struct r8a66597 *r8a66597,
 603				struct r8a66597_pipe *pipe,
 604				struct urb *urb)
 605{
 606	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 607	unsigned char endpoint = usb_pipeendpoint(urb->pipe);
 608	unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
 609
 610	if (!toggle)
 611		return;
 612
 613	r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
 614}
 615
 616/* this function must be called with interrupt disabled */
 617static void pipe_buffer_setting(struct r8a66597 *r8a66597,
 618				struct r8a66597_pipe_info *info)
 619{
 620	u16 val = 0;
 621
 622	if (info->pipenum == 0)
 623		return;
 624
 625	r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
 626	r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
 627	r8a66597_write(r8a66597, info->pipenum, PIPESEL);
 628	if (!info->dir_in)
 629		val |= R8A66597_DIR;
 630	if (info->type == R8A66597_BULK && info->dir_in)
 631		val |= R8A66597_DBLB | R8A66597_SHTNAK;
 632	val |= info->type | info->epnum;
 633	r8a66597_write(r8a66597, val, PIPECFG);
 634
 635	r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
 636		       PIPEBUF);
 637	r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
 638		       PIPEMAXP);
 639	r8a66597_write(r8a66597, info->interval, PIPEPERI);
 640}
 641
 642/* this function must be called with interrupt disabled */
 643static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
 644{
 645	struct r8a66597_pipe_info *info;
 646	struct urb *urb = td->urb;
 647
 648	if (td->pipenum > 0) {
 649		info = &td->pipe->info;
 650		cfifo_change(r8a66597, 0);
 651		pipe_buffer_setting(r8a66597, info);
 652
 653		if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
 654				   usb_pipeout(urb->pipe)) &&
 655		    !usb_pipecontrol(urb->pipe)) {
 656			r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
 657			pipe_toggle_set(r8a66597, td->pipe, urb, 0);
 658			clear_all_buffer(r8a66597, td->pipe);
 659			usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
 660				      usb_pipeout(urb->pipe), 1);
 661		}
 662		pipe_toggle_restore(r8a66597, td->pipe, urb);
 663	}
 664}
 665
 666/* this function must be called with interrupt disabled */
 667static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
 668			     struct usb_endpoint_descriptor *ep)
 669{
 670	u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
 671
 672	memset(array, 0, sizeof(array));
 673	switch (usb_endpoint_type(ep)) {
 674	case USB_ENDPOINT_XFER_BULK:
 675		if (usb_endpoint_dir_in(ep))
 676			array[i++] = 4;
 677		else {
 678			array[i++] = 3;
 679			array[i++] = 5;
 680		}
 681		break;
 682	case USB_ENDPOINT_XFER_INT:
 683		if (usb_endpoint_dir_in(ep)) {
 684			array[i++] = 6;
 685			array[i++] = 7;
 686			array[i++] = 8;
 687		} else
 688			array[i++] = 9;
 689		break;
 690	case USB_ENDPOINT_XFER_ISOC:
 691		if (usb_endpoint_dir_in(ep))
 692			array[i++] = 2;
 693		else
 694			array[i++] = 1;
 695		break;
 696	default:
 697		printk(KERN_ERR "r8a66597: Illegal type\n");
 698		return 0;
 699	}
 700
 701	i = 1;
 702	min = array[0];
 703	while (array[i] != 0) {
 704		if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
 705			min = array[i];
 706		i++;
 707	}
 708
 709	return min;
 710}
 711
 712static u16 get_r8a66597_type(__u8 type)
 713{
 714	u16 r8a66597_type;
 715
 716	switch (type) {
 717	case USB_ENDPOINT_XFER_BULK:
 718		r8a66597_type = R8A66597_BULK;
 719		break;
 720	case USB_ENDPOINT_XFER_INT:
 721		r8a66597_type = R8A66597_INT;
 722		break;
 723	case USB_ENDPOINT_XFER_ISOC:
 724		r8a66597_type = R8A66597_ISO;
 725		break;
 726	default:
 727		printk(KERN_ERR "r8a66597: Illegal type\n");
 728		r8a66597_type = 0x0000;
 729		break;
 730	}
 731
 732	return r8a66597_type;
 733}
 734
 735static u16 get_bufnum(u16 pipenum)
 736{
 737	u16 bufnum = 0;
 738
 739	if (pipenum == 0)
 740		bufnum = 0;
 741	else if (check_bulk_or_isoc(pipenum))
 742		bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
 743	else if (check_interrupt(pipenum))
 744		bufnum = 4 + (pipenum - 6);
 745	else
 746		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 747
 748	return bufnum;
 749}
 750
 751static u16 get_buf_bsize(u16 pipenum)
 752{
 753	u16 buf_bsize = 0;
 754
 755	if (pipenum == 0)
 756		buf_bsize = 3;
 757	else if (check_bulk_or_isoc(pipenum))
 758		buf_bsize = R8A66597_BUF_BSIZE - 1;
 759	else if (check_interrupt(pipenum))
 760		buf_bsize = 0;
 761	else
 762		printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 763
 764	return buf_bsize;
 765}
 766
 767/* this function must be called with interrupt disabled */
 768static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
 769				     struct r8a66597_device *dev,
 770				     struct r8a66597_pipe *pipe,
 771				     struct urb *urb)
 772{
 773	int i;
 774	struct r8a66597_pipe_info *info = &pipe->info;
 775	unsigned short mbw = mbw_value(r8a66597);
 776
 777	/* pipe dma is only for external controlles */
 778	if (r8a66597->pdata->on_chip)
 779		return;
 780
 781	if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
 782		for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
 783			if ((r8a66597->dma_map & (1 << i)) != 0)
 784				continue;
 785
 786			dev_info(&dev->udev->dev,
 787				 "address %d, EndpointAddress 0x%02x use "
 788				 "DMA FIFO\n", usb_pipedevice(urb->pipe),
 789				 info->dir_in ?
 790				 	USB_ENDPOINT_DIR_MASK + info->epnum
 791					: info->epnum);
 792
 793			r8a66597->dma_map |= 1 << i;
 794			dev->dma_map |= 1 << i;
 795			set_pipe_reg_addr(pipe, i);
 796
 797			cfifo_change(r8a66597, 0);
 798			r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
 799				      mbw | CURPIPE, pipe->fifosel);
 800
 801			r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
 802					  pipe->info.pipenum);
 803			r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
 804			break;
 805		}
 806	}
 807}
 808
 809/* this function must be called with interrupt disabled */
 810static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
 811				 struct usb_host_endpoint *hep,
 812				 struct r8a66597_pipe_info *info)
 813{
 814	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 815	struct r8a66597_pipe *pipe = hep->hcpriv;
 816
 817	dev_dbg(&dev->udev->dev, "enable_pipe:\n");
 818
 819	pipe->info = *info;
 820	set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
 821	r8a66597->pipe_cnt[pipe->info.pipenum]++;
 822	dev->pipe_cnt[pipe->info.pipenum]++;
 823
 824	enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
 825}
 826
 827static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
 828			      int status)
 829__releases(r8a66597->lock)
 830__acquires(r8a66597->lock)
 831{
 832	if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
 833		void *ptr;
 834
 835		for (ptr = urb->transfer_buffer;
 836		     ptr < urb->transfer_buffer + urb->transfer_buffer_length;
 837		     ptr += PAGE_SIZE)
 838			flush_dcache_page(virt_to_page(ptr));
 839	}
 840
 841	usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
 842	spin_unlock(&r8a66597->lock);
 843	usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
 844	spin_lock(&r8a66597->lock);
 845}
 846
 847/* this function must be called with interrupt disabled */
 848static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
 849{
 850	struct r8a66597_td *td, *next;
 851	struct urb *urb;
 852	struct list_head *list = &r8a66597->pipe_queue[pipenum];
 853
 854	if (list_empty(list))
 855		return;
 856
 857	list_for_each_entry_safe(td, next, list, queue) {
 858		if (td->address != address)
 859			continue;
 860
 861		urb = td->urb;
 862		list_del(&td->queue);
 863		kfree(td);
 864
 865		if (urb)
 866			r8a66597_urb_done(r8a66597, urb, -ENODEV);
 867
 868		break;
 869	}
 870}
 871
 872/* this function must be called with interrupt disabled */
 873static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
 874				      struct r8a66597_device *dev)
 875{
 876	int check_ep0 = 0;
 877	u16 pipenum;
 878
 879	if (!dev)
 880		return;
 881
 882	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
 883		if (!dev->pipe_cnt[pipenum])
 884			continue;
 885
 886		if (!check_ep0) {
 887			check_ep0 = 1;
 888			force_dequeue(r8a66597, 0, dev->address);
 889		}
 890
 891		r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
 892		dev->pipe_cnt[pipenum] = 0;
 893		force_dequeue(r8a66597, pipenum, dev->address);
 894	}
 895
 896	dev_dbg(&dev->udev->dev, "disable_pipe\n");
 897
 898	r8a66597->dma_map &= ~(dev->dma_map);
 899	dev->dma_map = 0;
 900}
 901
 902static u16 get_interval(struct urb *urb, __u8 interval)
 903{
 904	u16 time = 1;
 905	int i;
 906
 907	if (urb->dev->speed == USB_SPEED_HIGH) {
 908		if (interval > IITV)
 909			time = IITV;
 910		else
 911			time = interval ? interval - 1 : 0;
 912	} else {
 913		if (interval > 128) {
 914			time = IITV;
 915		} else {
 916			/* calculate the nearest value for PIPEPERI */
 917			for (i = 0; i < 7; i++) {
 918				if ((1 << i) < interval &&
 919				    (1 << (i + 1) > interval))
 920					time = 1 << i;
 921			}
 922		}
 923	}
 924
 925	return time;
 926}
 927
 928static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
 929{
 930	__u8 i;
 931	unsigned long time = 1;
 932
 933	if (usb_pipeisoc(urb->pipe))
 934		return 0;
 935
 936	if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
 937		for (i = 0; i < (interval - 1); i++)
 938			time *= 2;
 939		time = time * 125 / 1000;	/* uSOF -> msec */
 940	} else {
 941		time = interval;
 942	}
 943
 944	return time;
 945}
 946
 947/* this function must be called with interrupt disabled */
 948static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
 949			   struct usb_host_endpoint *hep,
 950			   struct usb_endpoint_descriptor *ep)
 951{
 952	struct r8a66597_pipe_info info;
 953
 954	info.pipenum = get_empty_pipenum(r8a66597, ep);
 955	info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
 956	info.epnum = usb_endpoint_num(ep);
 957	info.maxpacket = usb_endpoint_maxp(ep);
 958	info.type = get_r8a66597_type(usb_endpoint_type(ep));
 959	info.bufnum = get_bufnum(info.pipenum);
 960	info.buf_bsize = get_buf_bsize(info.pipenum);
 961	if (info.type == R8A66597_BULK) {
 962		info.interval = 0;
 963		info.timer_interval = 0;
 964	} else {
 965		info.interval = get_interval(urb, ep->bInterval);
 966		info.timer_interval = get_timer_interval(urb, ep->bInterval);
 967	}
 968	if (usb_endpoint_dir_in(ep))
 969		info.dir_in = 1;
 970	else
 971		info.dir_in = 0;
 972
 973	enable_r8a66597_pipe(r8a66597, urb, hep, &info);
 974}
 975
 976static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
 977{
 978	struct r8a66597_device *dev;
 979
 980	dev = get_urb_to_r8a66597_dev(r8a66597, urb);
 981	dev->state = USB_STATE_CONFIGURED;
 982}
 983
 984static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
 985			    u16 pipenum)
 986{
 987	if (pipenum == 0 && usb_pipeout(urb->pipe))
 988		enable_irq_empty(r8a66597, pipenum);
 989	else
 990		enable_irq_ready(r8a66597, pipenum);
 991
 992	if (!usb_pipeisoc(urb->pipe))
 993		enable_irq_nrdy(r8a66597, pipenum);
 994}
 995
 996static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
 997{
 998	disable_irq_ready(r8a66597, pipenum);
 999	disable_irq_nrdy(r8a66597, pipenum);
1000}
1001
1002static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1003{
1004	mod_timer(&r8a66597->rh_timer,
1005			jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1006}
1007
1008static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1009					int connect)
1010{
1011	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1012
1013	rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1014	rh->scount = R8A66597_MAX_SAMPLING;
1015	if (connect)
1016		rh->port |= USB_PORT_STAT_CONNECTION;
1017	else
1018		rh->port &= ~USB_PORT_STAT_CONNECTION;
1019	rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
1020
1021	r8a66597_root_hub_start_polling(r8a66597);
1022}
1023
1024/* this function must be called with interrupt disabled */
1025static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1026					u16 syssts)
1027__releases(r8a66597->lock)
1028__acquires(r8a66597->lock)
1029{
1030	if (syssts == SE0) {
1031		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1032		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1033	} else {
1034		if (syssts == FS_JSTS)
1035			r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1036		else if (syssts == LS_JSTS)
1037			r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1038
1039		r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1040		r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1041
1042		if (r8a66597->bus_suspended)
1043			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1044	}
1045
1046	spin_unlock(&r8a66597->lock);
1047	usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1048	spin_lock(&r8a66597->lock);
1049}
1050
1051/* this function must be called with interrupt disabled */
1052static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1053{
1054	u16 speed = get_rh_usb_speed(r8a66597, port);
1055	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1056
1057	rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
1058	if (speed == HSMODE)
1059		rh->port |= USB_PORT_STAT_HIGH_SPEED;
1060	else if (speed == LSMODE)
1061		rh->port |= USB_PORT_STAT_LOW_SPEED;
1062
1063	rh->port &= ~USB_PORT_STAT_RESET;
1064	rh->port |= USB_PORT_STAT_ENABLE;
1065}
1066
1067/* this function must be called with interrupt disabled */
1068static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1069{
1070	struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1071
1072	disable_r8a66597_pipe_all(r8a66597, dev);
1073	free_usb_address(r8a66597, dev, 0);
1074
1075	start_root_hub_sampling(r8a66597, port, 0);
1076}
1077
1078/* this function must be called with interrupt disabled */
1079static void prepare_setup_packet(struct r8a66597 *r8a66597,
1080				 struct r8a66597_td *td)
1081{
1082	int i;
1083	__le16 *p = (__le16 *)td->urb->setup_packet;
1084	unsigned long setup_addr = USBREQ;
1085
1086	r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1087		       DCPMAXP);
1088	r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1089
1090	for (i = 0; i < 4; i++) {
1091		r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1092		setup_addr += 2;
1093	}
1094	r8a66597_write(r8a66597, SUREQ, DCPCTR);
1095}
1096
1097/* this function must be called with interrupt disabled */
1098static void prepare_packet_read(struct r8a66597 *r8a66597,
1099				struct r8a66597_td *td)
1100{
1101	struct urb *urb = td->urb;
1102
1103	if (usb_pipecontrol(urb->pipe)) {
1104		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1105		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1106		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1107		if (urb->actual_length == 0) {
1108			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1109			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1110		}
1111		pipe_irq_disable(r8a66597, td->pipenum);
1112		pipe_start(r8a66597, td->pipe);
1113		pipe_irq_enable(r8a66597, urb, td->pipenum);
1114	} else {
1115		if (urb->actual_length == 0) {
1116			pipe_irq_disable(r8a66597, td->pipenum);
1117			pipe_setting(r8a66597, td);
1118			pipe_stop(r8a66597, td->pipe);
1119			r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1120
1121			if (td->pipe->pipetre) {
1122				r8a66597_write(r8a66597, TRCLR,
1123						td->pipe->pipetre);
1124				r8a66597_write(r8a66597,
1125						DIV_ROUND_UP
1126						  (urb->transfer_buffer_length,
1127						   td->maxpacket),
1128						td->pipe->pipetrn);
1129				r8a66597_bset(r8a66597, TRENB,
1130						td->pipe->pipetre);
1131			}
1132
1133			pipe_start(r8a66597, td->pipe);
1134			pipe_irq_enable(r8a66597, urb, td->pipenum);
1135		}
1136	}
1137}
1138
1139/* this function must be called with interrupt disabled */
1140static void prepare_packet_write(struct r8a66597 *r8a66597,
1141				 struct r8a66597_td *td)
1142{
1143	u16 tmp;
1144	struct urb *urb = td->urb;
1145
1146	if (usb_pipecontrol(urb->pipe)) {
1147		pipe_stop(r8a66597, td->pipe);
1148		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1149		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1150		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1151		if (urb->actual_length == 0) {
1152			r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1153			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1154		}
1155	} else {
1156		if (urb->actual_length == 0)
1157			pipe_setting(r8a66597, td);
1158		if (td->pipe->pipetre)
1159			r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1160	}
1161	r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1162
1163	fifo_change_from_pipe(r8a66597, td->pipe);
1164	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1165	if (unlikely((tmp & FRDY) == 0))
1166		pipe_irq_enable(r8a66597, urb, td->pipenum);
1167	else
1168		packet_write(r8a66597, td->pipenum);
1169	pipe_start(r8a66597, td->pipe);
1170}
1171
1172/* this function must be called with interrupt disabled */
1173static void prepare_status_packet(struct r8a66597 *r8a66597,
1174				  struct r8a66597_td *td)
1175{
1176	struct urb *urb = td->urb;
1177
1178	r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1179	pipe_stop(r8a66597, td->pipe);
1180
1181	if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1182		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1183		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1184		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1185		r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1186		r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1187		enable_irq_empty(r8a66597, 0);
1188	} else {
1189		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1190		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1191		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1192		r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1193		enable_irq_ready(r8a66597, 0);
1194	}
1195	enable_irq_nrdy(r8a66597, 0);
1196	pipe_start(r8a66597, td->pipe);
1197}
1198
1199static int is_set_address(unsigned char *setup_packet)
1200{
1201	if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1202			setup_packet[1] == USB_REQ_SET_ADDRESS)
1203		return 1;
1204	else
1205		return 0;
1206}
1207
1208/* this function must be called with interrupt disabled */
1209static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1210{
1211	BUG_ON(!td);
1212
1213	switch (td->type) {
1214	case USB_PID_SETUP:
1215		if (is_set_address(td->urb->setup_packet)) {
1216			td->set_address = 1;
1217			td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1218								     td->urb);
1219			if (td->urb->setup_packet[2] == 0)
1220				return -EPIPE;
1221		}
1222		prepare_setup_packet(r8a66597, td);
1223		break;
1224	case USB_PID_IN:
1225		prepare_packet_read(r8a66597, td);
1226		break;
1227	case USB_PID_OUT:
1228		prepare_packet_write(r8a66597, td);
1229		break;
1230	case USB_PID_ACK:
1231		prepare_status_packet(r8a66597, td);
1232		break;
1233	default:
1234		printk(KERN_ERR "r8a66597: invalid type.\n");
1235		break;
1236	}
1237
1238	return 0;
1239}
1240
1241static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1242{
1243	if (usb_pipeisoc(urb->pipe)) {
1244		if (urb->number_of_packets == td->iso_cnt)
1245			return 1;
1246	}
1247
1248	/* control or bulk or interrupt */
1249	if ((urb->transfer_buffer_length <= urb->actual_length) ||
1250	    (td->short_packet) || (td->zero_packet))
1251		return 1;
1252
1253	return 0;
1254}
1255
1256/* this function must be called with interrupt disabled */
1257static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1258{
1259	unsigned long time;
1260
1261	BUG_ON(!td);
1262
1263	if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1264	    !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1265		r8a66597->timeout_map |= 1 << td->pipenum;
1266		switch (usb_pipetype(td->urb->pipe)) {
1267		case PIPE_INTERRUPT:
1268		case PIPE_ISOCHRONOUS:
1269			time = 30;
1270			break;
1271		default:
1272			time = 300;
1273			break;
1274		}
1275
1276		mod_timer(&r8a66597->td_timer[td->pipenum],
1277			  jiffies + msecs_to_jiffies(time));
1278	}
1279}
1280
1281/* this function must be called with interrupt disabled */
1282static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1283		u16 pipenum, struct urb *urb, int status)
1284__releases(r8a66597->lock) __acquires(r8a66597->lock)
1285{
1286	int restart = 0;
1287	struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1288
1289	r8a66597->timeout_map &= ~(1 << pipenum);
1290
1291	if (likely(td)) {
1292		if (td->set_address && (status != 0 || urb->unlinked))
1293			r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1294
1295		pipe_toggle_save(r8a66597, td->pipe, urb);
1296		list_del(&td->queue);
1297		kfree(td);
1298	}
1299
1300	if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1301		restart = 1;
1302
1303	if (likely(urb)) {
1304		if (usb_pipeisoc(urb->pipe))
1305			urb->start_frame = r8a66597_get_frame(hcd);
1306
1307		r8a66597_urb_done(r8a66597, urb, status);
1308	}
1309
1310	if (restart) {
1311		td = r8a66597_get_td(r8a66597, pipenum);
1312		if (unlikely(!td))
1313			return;
1314
1315		start_transfer(r8a66597, td);
1316		set_td_timer(r8a66597, td);
1317	}
1318}
1319
1320static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1321{
1322	u16 tmp;
1323	int rcv_len, bufsize, urb_len, size;
1324	u16 *buf;
1325	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1326	struct urb *urb;
1327	int finish = 0;
1328	int status = 0;
1329
1330	if (unlikely(!td))
1331		return;
1332	urb = td->urb;
1333
1334	fifo_change_from_pipe(r8a66597, td->pipe);
1335	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1336	if (unlikely((tmp & FRDY) == 0)) {
1337		pipe_stop(r8a66597, td->pipe);
1338		pipe_irq_disable(r8a66597, pipenum);
1339		printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1340		finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1341		return;
1342	}
1343
1344	/* prepare parameters */
1345	rcv_len = tmp & DTLN;
1346	if (usb_pipeisoc(urb->pipe)) {
1347		buf = (u16 *)(urb->transfer_buffer +
1348				urb->iso_frame_desc[td->iso_cnt].offset);
1349		urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1350	} else {
1351		buf = (void *)urb->transfer_buffer + urb->actual_length;
1352		urb_len = urb->transfer_buffer_length - urb->actual_length;
1353	}
1354	bufsize = min(urb_len, (int) td->maxpacket);
1355	if (rcv_len <= bufsize) {
1356		size = rcv_len;
1357	} else {
1358		size = bufsize;
1359		status = -EOVERFLOW;
1360		finish = 1;
1361	}
1362
1363	/* update parameters */
1364	urb->actual_length += size;
1365	if (rcv_len == 0)
1366		td->zero_packet = 1;
1367	if (rcv_len < bufsize) {
1368		td->short_packet = 1;
1369	}
1370	if (usb_pipeisoc(urb->pipe)) {
1371		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1372		urb->iso_frame_desc[td->iso_cnt].status = status;
1373		td->iso_cnt++;
1374		finish = 0;
1375	}
1376
1377	/* check transfer finish */
1378	if (finish || check_transfer_finish(td, urb)) {
1379		pipe_stop(r8a66597, td->pipe);
1380		pipe_irq_disable(r8a66597, pipenum);
1381		finish = 1;
1382	}
1383
1384	/* read fifo */
1385	if (urb->transfer_buffer) {
1386		if (size == 0)
1387			r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1388		else
1389			r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1390					   buf, size);
1391	}
1392
1393	if (finish && pipenum != 0)
1394		finish_request(r8a66597, td, pipenum, urb, status);
1395}
1396
1397static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1398{
1399	u16 tmp;
1400	int bufsize, size;
1401	u16 *buf;
1402	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1403	struct urb *urb;
1404
1405	if (unlikely(!td))
1406		return;
1407	urb = td->urb;
1408
1409	fifo_change_from_pipe(r8a66597, td->pipe);
1410	tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1411	if (unlikely((tmp & FRDY) == 0)) {
1412		pipe_stop(r8a66597, td->pipe);
1413		pipe_irq_disable(r8a66597, pipenum);
1414		printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1415		finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1416		return;
1417	}
1418
1419	/* prepare parameters */
1420	bufsize = td->maxpacket;
1421	if (usb_pipeisoc(urb->pipe)) {
1422		buf = (u16 *)(urb->transfer_buffer +
1423				urb->iso_frame_desc[td->iso_cnt].offset);
1424		size = min(bufsize,
1425			   (int)urb->iso_frame_desc[td->iso_cnt].length);
1426	} else {
1427		buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1428		size = min_t(u32, bufsize,
1429			   urb->transfer_buffer_length - urb->actual_length);
1430	}
1431
1432	/* write fifo */
1433	if (pipenum > 0)
1434		r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1435	if (urb->transfer_buffer) {
1436		r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
1437		if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1438			r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1439	}
1440
1441	/* update parameters */
1442	urb->actual_length += size;
1443	if (usb_pipeisoc(urb->pipe)) {
1444		urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1445		urb->iso_frame_desc[td->iso_cnt].status = 0;
1446		td->iso_cnt++;
1447	}
1448
1449	/* check transfer finish */
1450	if (check_transfer_finish(td, urb)) {
1451		disable_irq_ready(r8a66597, pipenum);
1452		enable_irq_empty(r8a66597, pipenum);
1453		if (!usb_pipeisoc(urb->pipe))
1454			enable_irq_nrdy(r8a66597, pipenum);
1455	} else
1456		pipe_irq_enable(r8a66597, urb, pipenum);
1457}
1458
1459
1460static void check_next_phase(struct r8a66597 *r8a66597, int status)
1461{
1462	struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1463	struct urb *urb;
1464	u8 finish = 0;
1465
1466	if (unlikely(!td))
1467		return;
1468	urb = td->urb;
1469
1470	switch (td->type) {
1471	case USB_PID_IN:
1472	case USB_PID_OUT:
1473		if (check_transfer_finish(td, urb))
1474			td->type = USB_PID_ACK;
1475		break;
1476	case USB_PID_SETUP:
1477		if (urb->transfer_buffer_length == urb->actual_length)
1478			td->type = USB_PID_ACK;
1479		else if (usb_pipeout(urb->pipe))
1480			td->type = USB_PID_OUT;
1481		else
1482			td->type = USB_PID_IN;
1483		break;
1484	case USB_PID_ACK:
1485		finish = 1;
1486		break;
1487	}
1488
1489	if (finish || status != 0 || urb->unlinked)
1490		finish_request(r8a66597, td, 0, urb, status);
1491	else
1492		start_transfer(r8a66597, td);
1493}
1494
1495static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1496{
1497	struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1498
1499	if (td) {
1500		u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1501
1502		if (pid == PID_NAK)
1503			return -ECONNRESET;
1504		else
1505			return -EPIPE;
1506	}
1507	return 0;
1508}
1509
1510static void irq_pipe_ready(struct r8a66597 *r8a66597)
1511{
1512	u16 check;
1513	u16 pipenum;
1514	u16 mask;
1515	struct r8a66597_td *td;
1516
1517	mask = r8a66597_read(r8a66597, BRDYSTS)
1518	       & r8a66597_read(r8a66597, BRDYENB);
1519	r8a66597_write(r8a66597, ~mask, BRDYSTS);
1520	if (mask & BRDY0) {
1521		td = r8a66597_get_td(r8a66597, 0);
1522		if (td && td->type == USB_PID_IN)
1523			packet_read(r8a66597, 0);
1524		else
1525			pipe_irq_disable(r8a66597, 0);
1526		check_next_phase(r8a66597, 0);
1527	}
1528
1529	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1530		check = 1 << pipenum;
1531		if (mask & check) {
1532			td = r8a66597_get_td(r8a66597, pipenum);
1533			if (unlikely(!td))
1534				continue;
1535
1536			if (td->type == USB_PID_IN)
1537				packet_read(r8a66597, pipenum);
1538			else if (td->type == USB_PID_OUT)
1539				packet_write(r8a66597, pipenum);
1540		}
1541	}
1542}
1543
1544static void irq_pipe_empty(struct r8a66597 *r8a66597)
1545{
1546	u16 tmp;
1547	u16 check;
1548	u16 pipenum;
1549	u16 mask;
1550	struct r8a66597_td *td;
1551
1552	mask = r8a66597_read(r8a66597, BEMPSTS)
1553	       & r8a66597_read(r8a66597, BEMPENB);
1554	r8a66597_write(r8a66597, ~mask, BEMPSTS);
1555	if (mask & BEMP0) {
1556		cfifo_change(r8a66597, 0);
1557		td = r8a66597_get_td(r8a66597, 0);
1558		if (td && td->type != USB_PID_OUT)
1559			disable_irq_empty(r8a66597, 0);
1560		check_next_phase(r8a66597, 0);
1561	}
1562
1563	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1564		check = 1 << pipenum;
1565		if (mask &  check) {
1566			struct r8a66597_td *td;
1567			td = r8a66597_get_td(r8a66597, pipenum);
1568			if (unlikely(!td))
1569				continue;
1570
1571			tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1572			if ((tmp & INBUFM) == 0) {
1573				disable_irq_empty(r8a66597, pipenum);
1574				pipe_irq_disable(r8a66597, pipenum);
1575				finish_request(r8a66597, td, pipenum, td->urb,
1576						0);
1577			}
1578		}
1579	}
1580}
1581
1582static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1583{
1584	u16 check;
1585	u16 pipenum;
1586	u16 mask;
1587	int status;
1588
1589	mask = r8a66597_read(r8a66597, NRDYSTS)
1590	       & r8a66597_read(r8a66597, NRDYENB);
1591	r8a66597_write(r8a66597, ~mask, NRDYSTS);
1592	if (mask & NRDY0) {
1593		cfifo_change(r8a66597, 0);
1594		status = get_urb_error(r8a66597, 0);
1595		pipe_irq_disable(r8a66597, 0);
1596		check_next_phase(r8a66597, status);
1597	}
1598
1599	for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1600		check = 1 << pipenum;
1601		if (mask & check) {
1602			struct r8a66597_td *td;
1603			td = r8a66597_get_td(r8a66597, pipenum);
1604			if (unlikely(!td))
1605				continue;
1606
1607			status = get_urb_error(r8a66597, pipenum);
1608			pipe_irq_disable(r8a66597, pipenum);
1609			pipe_stop(r8a66597, td->pipe);
1610			finish_request(r8a66597, td, pipenum, td->urb, status);
1611		}
1612	}
1613}
1614
1615static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1616{
1617	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1618	u16 intsts0, intsts1, intsts2;
1619	u16 intenb0, intenb1, intenb2;
1620	u16 mask0, mask1, mask2;
1621	int status;
1622
1623	spin_lock(&r8a66597->lock);
1624
1625	intsts0 = r8a66597_read(r8a66597, INTSTS0);
1626	intsts1 = r8a66597_read(r8a66597, INTSTS1);
1627	intsts2 = r8a66597_read(r8a66597, INTSTS2);
1628	intenb0 = r8a66597_read(r8a66597, INTENB0);
1629	intenb1 = r8a66597_read(r8a66597, INTENB1);
1630	intenb2 = r8a66597_read(r8a66597, INTENB2);
1631
1632	mask2 = intsts2 & intenb2;
1633	mask1 = intsts1 & intenb1;
1634	mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1635	if (mask2) {
1636		if (mask2 & ATTCH) {
1637			r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1638			r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1639
1640			/* start usb bus sampling */
1641			start_root_hub_sampling(r8a66597, 1, 1);
1642		}
1643		if (mask2 & DTCH) {
1644			r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1645			r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1646			r8a66597_usb_disconnect(r8a66597, 1);
1647		}
1648		if (mask2 & BCHG) {
1649			r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1650			r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1651			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1652		}
1653	}
1654
1655	if (mask1) {
1656		if (mask1 & ATTCH) {
1657			r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1658			r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1659
1660			/* start usb bus sampling */
1661			start_root_hub_sampling(r8a66597, 0, 1);
1662		}
1663		if (mask1 & DTCH) {
1664			r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1665			r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1666			r8a66597_usb_disconnect(r8a66597, 0);
1667		}
1668		if (mask1 & BCHG) {
1669			r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1670			r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1671			usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1672		}
1673
1674		if (mask1 & SIGN) {
1675			r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1676			status = get_urb_error(r8a66597, 0);
1677			check_next_phase(r8a66597, status);
1678		}
1679		if (mask1 & SACK) {
1680			r8a66597_write(r8a66597, ~SACK, INTSTS1);
1681			check_next_phase(r8a66597, 0);
1682		}
1683	}
1684	if (mask0) {
1685		if (mask0 & BRDY)
1686			irq_pipe_ready(r8a66597);
1687		if (mask0 & BEMP)
1688			irq_pipe_empty(r8a66597);
1689		if (mask0 & NRDY)
1690			irq_pipe_nrdy(r8a66597);
1691	}
1692
1693	spin_unlock(&r8a66597->lock);
1694	return IRQ_HANDLED;
1695}
1696
1697/* this function must be called with interrupt disabled */
1698static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1699{
1700	u16 tmp;
1701	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1702
1703	if (rh->port & USB_PORT_STAT_RESET) {
1704		unsigned long dvstctr_reg = get_dvstctr_reg(port);
1705
1706		tmp = r8a66597_read(r8a66597, dvstctr_reg);
1707		if ((tmp & USBRST) == USBRST) {
1708			r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1709				      dvstctr_reg);
1710			r8a66597_root_hub_start_polling(r8a66597);
1711		} else
1712			r8a66597_usb_connect(r8a66597, port);
1713	}
1714
1715	if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
1716		r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1717		r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1718	}
1719
1720	if (rh->scount > 0) {
1721		tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1722		if (tmp == rh->old_syssts) {
1723			rh->scount--;
1724			if (rh->scount == 0)
1725				r8a66597_check_syssts(r8a66597, port, tmp);
1726			else
1727				r8a66597_root_hub_start_polling(r8a66597);
1728		} else {
1729			rh->scount = R8A66597_MAX_SAMPLING;
1730			rh->old_syssts = tmp;
1731			r8a66597_root_hub_start_polling(r8a66597);
1732		}
1733	}
1734}
1735
1736static void r8a66597_interval_timer(unsigned long _r8a66597)
1737{
1738	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
 
1739	unsigned long flags;
1740	u16 pipenum;
1741	struct r8a66597_td *td;
1742
1743	spin_lock_irqsave(&r8a66597->lock, flags);
1744
1745	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1746		if (!(r8a66597->interval_map & (1 << pipenum)))
1747			continue;
1748		if (timer_pending(&r8a66597->interval_timer[pipenum]))
1749			continue;
1750
1751		td = r8a66597_get_td(r8a66597, pipenum);
1752		if (td)
1753			start_transfer(r8a66597, td);
1754	}
1755
1756	spin_unlock_irqrestore(&r8a66597->lock, flags);
1757}
1758
1759static void r8a66597_td_timer(unsigned long _r8a66597)
1760{
1761	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
 
1762	unsigned long flags;
1763	u16 pipenum;
1764	struct r8a66597_td *td, *new_td = NULL;
1765	struct r8a66597_pipe *pipe;
1766
1767	spin_lock_irqsave(&r8a66597->lock, flags);
1768	for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1769		if (!(r8a66597->timeout_map & (1 << pipenum)))
1770			continue;
1771		if (timer_pending(&r8a66597->td_timer[pipenum]))
1772			continue;
1773
1774		td = r8a66597_get_td(r8a66597, pipenum);
1775		if (!td) {
1776			r8a66597->timeout_map &= ~(1 << pipenum);
1777			continue;
1778		}
1779
1780		if (td->urb->actual_length) {
1781			set_td_timer(r8a66597, td);
1782			break;
1783		}
1784
1785		pipe = td->pipe;
1786		pipe_stop(r8a66597, pipe);
1787
 
1788		new_td = td;
1789		do {
1790			list_move_tail(&new_td->queue,
1791				       &r8a66597->pipe_queue[pipenum]);
1792			new_td = r8a66597_get_td(r8a66597, pipenum);
1793			if (!new_td) {
1794				new_td = td;
1795				break;
1796			}
1797		} while (td != new_td && td->address == new_td->address);
 
1798
1799		start_transfer(r8a66597, new_td);
1800
1801		if (td == new_td)
1802			r8a66597->timeout_map &= ~(1 << pipenum);
1803		else
1804			set_td_timer(r8a66597, new_td);
1805		break;
1806	}
1807	spin_unlock_irqrestore(&r8a66597->lock, flags);
1808}
1809
1810static void r8a66597_timer(unsigned long _r8a66597)
1811{
1812	struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1813	unsigned long flags;
1814	int port;
1815
1816	spin_lock_irqsave(&r8a66597->lock, flags);
1817
1818	for (port = 0; port < r8a66597->max_root_hub; port++)
1819		r8a66597_root_hub_control(r8a66597, port);
1820
1821	spin_unlock_irqrestore(&r8a66597->lock, flags);
1822}
1823
1824static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1825{
1826	struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1827
1828	if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1829	    (urb->dev->state == USB_STATE_CONFIGURED))
1830		return 1;
1831	else
1832		return 0;
1833}
1834
1835static int r8a66597_start(struct usb_hcd *hcd)
1836{
1837	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1838
1839	hcd->state = HC_STATE_RUNNING;
1840	return enable_controller(r8a66597);
1841}
1842
1843static void r8a66597_stop(struct usb_hcd *hcd)
1844{
1845	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1846
1847	disable_controller(r8a66597);
1848}
1849
1850static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1851{
1852	unsigned int usb_address = usb_pipedevice(urb->pipe);
1853	u16 root_port, hub_port;
1854
1855	if (usb_address == 0) {
1856		get_port_number(r8a66597, urb->dev->devpath,
1857				&root_port, &hub_port);
1858		set_devadd_reg(r8a66597, 0,
1859			       get_r8a66597_usb_speed(urb->dev->speed),
1860			       get_parent_r8a66597_address(r8a66597, urb->dev),
1861			       hub_port, root_port);
1862	}
1863}
1864
1865static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1866					    struct urb *urb,
1867					    struct usb_host_endpoint *hep)
1868{
1869	struct r8a66597_td *td;
1870	u16 pipenum;
1871
1872	td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1873	if (td == NULL)
1874		return NULL;
1875
1876	pipenum = r8a66597_get_pipenum(urb, hep);
1877	td->pipenum = pipenum;
1878	td->pipe = hep->hcpriv;
1879	td->urb = urb;
1880	td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1881	td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1882				      !usb_pipein(urb->pipe));
1883	if (usb_pipecontrol(urb->pipe))
1884		td->type = USB_PID_SETUP;
1885	else if (usb_pipein(urb->pipe))
1886		td->type = USB_PID_IN;
1887	else
1888		td->type = USB_PID_OUT;
1889	INIT_LIST_HEAD(&td->queue);
1890
1891	return td;
1892}
1893
1894static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1895				struct urb *urb,
1896				gfp_t mem_flags)
1897{
1898	struct usb_host_endpoint *hep = urb->ep;
1899	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1900	struct r8a66597_td *td = NULL;
1901	int ret, request = 0;
1902	unsigned long flags;
1903
1904	spin_lock_irqsave(&r8a66597->lock, flags);
1905	if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1906		ret = -ENODEV;
1907		goto error_not_linked;
1908	}
1909
1910	ret = usb_hcd_link_urb_to_ep(hcd, urb);
1911	if (ret)
1912		goto error_not_linked;
1913
1914	if (!hep->hcpriv) {
1915		hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1916				GFP_ATOMIC);
1917		if (!hep->hcpriv) {
1918			ret = -ENOMEM;
1919			goto error;
1920		}
1921		set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1922		if (usb_pipeendpoint(urb->pipe))
1923			init_pipe_info(r8a66597, urb, hep, &hep->desc);
1924	}
1925
1926	if (unlikely(check_pipe_config(r8a66597, urb)))
1927		init_pipe_config(r8a66597, urb);
1928
1929	set_address_zero(r8a66597, urb);
1930	td = r8a66597_make_td(r8a66597, urb, hep);
1931	if (td == NULL) {
1932		ret = -ENOMEM;
1933		goto error;
1934	}
1935	if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1936		request = 1;
1937	list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1938	urb->hcpriv = td;
1939
1940	if (request) {
1941		if (td->pipe->info.timer_interval) {
1942			r8a66597->interval_map |= 1 << td->pipenum;
1943			mod_timer(&r8a66597->interval_timer[td->pipenum],
1944				  jiffies + msecs_to_jiffies(
1945					td->pipe->info.timer_interval));
1946		} else {
1947			ret = start_transfer(r8a66597, td);
1948			if (ret < 0) {
1949				list_del(&td->queue);
1950				kfree(td);
1951			}
1952		}
1953	} else
1954		set_td_timer(r8a66597, td);
1955
1956error:
1957	if (ret)
1958		usb_hcd_unlink_urb_from_ep(hcd, urb);
1959error_not_linked:
1960	spin_unlock_irqrestore(&r8a66597->lock, flags);
1961	return ret;
1962}
1963
1964static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1965		int status)
1966{
1967	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1968	struct r8a66597_td *td;
1969	unsigned long flags;
1970	int rc;
1971
1972	spin_lock_irqsave(&r8a66597->lock, flags);
1973	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1974	if (rc)
1975		goto done;
1976
1977	if (urb->hcpriv) {
1978		td = urb->hcpriv;
1979		pipe_stop(r8a66597, td->pipe);
1980		pipe_irq_disable(r8a66597, td->pipenum);
1981		disable_irq_empty(r8a66597, td->pipenum);
1982		finish_request(r8a66597, td, td->pipenum, urb, status);
1983	}
1984 done:
1985	spin_unlock_irqrestore(&r8a66597->lock, flags);
1986	return rc;
1987}
1988
1989static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1990				      struct usb_host_endpoint *hep)
 
 
1991{
1992	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1993	struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1994	struct r8a66597_td *td;
1995	struct urb *urb = NULL;
1996	u16 pipenum;
1997	unsigned long flags;
1998
1999	if (pipe == NULL)
2000		return;
2001	pipenum = pipe->info.pipenum;
2002
 
2003	if (pipenum == 0) {
2004		kfree(hep->hcpriv);
2005		hep->hcpriv = NULL;
 
2006		return;
2007	}
2008
2009	spin_lock_irqsave(&r8a66597->lock, flags);
2010	pipe_stop(r8a66597, pipe);
2011	pipe_irq_disable(r8a66597, pipenum);
2012	disable_irq_empty(r8a66597, pipenum);
2013	td = r8a66597_get_td(r8a66597, pipenum);
2014	if (td)
2015		urb = td->urb;
2016	finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
2017	kfree(hep->hcpriv);
2018	hep->hcpriv = NULL;
2019	spin_unlock_irqrestore(&r8a66597->lock, flags);
2020}
2021
2022static int r8a66597_get_frame(struct usb_hcd *hcd)
2023{
2024	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2025	return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2026}
2027
2028static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2029{
2030	int chix;
2031	struct usb_device *childdev;
2032
2033	if (udev->state == USB_STATE_CONFIGURED &&
2034	    udev->parent && udev->parent->devnum > 1 &&
2035	    udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2036		map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2037
2038	usb_hub_for_each_child(udev, chix, childdev)
2039		collect_usb_address_map(childdev, map);
2040}
2041
2042/* this function must be called with interrupt disabled */
2043static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2044						   int addr)
2045{
2046	struct r8a66597_device *dev;
2047	struct list_head *list = &r8a66597->child_device;
2048
2049	list_for_each_entry(dev, list, device_list) {
2050		if (dev->usb_address != addr)
2051			continue;
2052
2053		return dev;
2054	}
2055
2056	printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2057	return NULL;
2058}
2059
2060static void update_usb_address_map(struct r8a66597 *r8a66597,
2061				   struct usb_device *root_hub,
2062				   unsigned long *map)
2063{
2064	int i, j, addr;
2065	unsigned long diff;
2066	unsigned long flags;
2067
2068	for (i = 0; i < 4; i++) {
2069		diff = r8a66597->child_connect_map[i] ^ map[i];
2070		if (!diff)
2071			continue;
2072
2073		for (j = 0; j < 32; j++) {
2074			if (!(diff & (1 << j)))
2075				continue;
2076
2077			addr = i * 32 + j;
2078			if (map[i] & (1 << j))
2079				set_child_connect_map(r8a66597, addr);
2080			else {
2081				struct r8a66597_device *dev;
2082
2083				spin_lock_irqsave(&r8a66597->lock, flags);
2084				dev = get_r8a66597_device(r8a66597, addr);
2085				disable_r8a66597_pipe_all(r8a66597, dev);
2086				free_usb_address(r8a66597, dev, 0);
2087				put_child_connect_map(r8a66597, addr);
2088				spin_unlock_irqrestore(&r8a66597->lock, flags);
2089			}
2090		}
2091	}
2092}
2093
2094static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2095					struct usb_hcd *hcd)
2096{
2097	struct usb_bus *bus;
2098	unsigned long now_map[4];
2099
2100	memset(now_map, 0, sizeof(now_map));
2101
2102	list_for_each_entry(bus, &usb_bus_list, bus_list) {
2103		if (!bus->root_hub)
2104			continue;
2105
2106		if (bus->busnum != hcd->self.busnum)
2107			continue;
2108
2109		collect_usb_address_map(bus->root_hub, now_map);
2110		update_usb_address_map(r8a66597, bus->root_hub, now_map);
2111	}
 
2112}
2113
2114static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2115{
2116	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2117	unsigned long flags;
2118	int i;
2119
2120	r8a66597_check_detect_child(r8a66597, hcd);
2121
2122	spin_lock_irqsave(&r8a66597->lock, flags);
2123
2124	*buf = 0;	/* initialize (no change) */
2125
2126	for (i = 0; i < r8a66597->max_root_hub; i++) {
2127		if (r8a66597->root_hub[i].port & 0xffff0000)
2128			*buf |= 1 << (i + 1);
2129	}
2130
2131	spin_unlock_irqrestore(&r8a66597->lock, flags);
2132
2133	return (*buf != 0);
2134}
2135
2136static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2137				    struct usb_hub_descriptor *desc)
2138{
2139	desc->bDescriptorType = 0x29;
2140	desc->bHubContrCurrent = 0;
2141	desc->bNbrPorts = r8a66597->max_root_hub;
2142	desc->bDescLength = 9;
2143	desc->bPwrOn2PwrGood = 0;
2144	desc->wHubCharacteristics = cpu_to_le16(0x0011);
 
2145	desc->u.hs.DeviceRemovable[0] =
2146		((1 << r8a66597->max_root_hub) - 1) << 1;
2147	desc->u.hs.DeviceRemovable[1] = ~0;
2148}
2149
2150static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2151				u16 wIndex, char *buf, u16 wLength)
2152{
2153	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2154	int ret;
2155	int port = (wIndex & 0x00FF) - 1;
2156	struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2157	unsigned long flags;
2158
2159	ret = 0;
2160
2161	spin_lock_irqsave(&r8a66597->lock, flags);
2162	switch (typeReq) {
2163	case ClearHubFeature:
2164	case SetHubFeature:
2165		switch (wValue) {
2166		case C_HUB_OVER_CURRENT:
2167		case C_HUB_LOCAL_POWER:
2168			break;
2169		default:
2170			goto error;
2171		}
2172		break;
2173	case ClearPortFeature:
2174		if (wIndex > r8a66597->max_root_hub)
2175			goto error;
2176		if (wLength != 0)
2177			goto error;
2178
2179		switch (wValue) {
2180		case USB_PORT_FEAT_ENABLE:
2181			rh->port &= ~USB_PORT_STAT_POWER;
2182			break;
2183		case USB_PORT_FEAT_SUSPEND:
2184			break;
2185		case USB_PORT_FEAT_POWER:
2186			r8a66597_port_power(r8a66597, port, 0);
2187			break;
2188		case USB_PORT_FEAT_C_ENABLE:
2189		case USB_PORT_FEAT_C_SUSPEND:
2190		case USB_PORT_FEAT_C_CONNECTION:
2191		case USB_PORT_FEAT_C_OVER_CURRENT:
2192		case USB_PORT_FEAT_C_RESET:
2193			break;
2194		default:
2195			goto error;
2196		}
2197		rh->port &= ~(1 << wValue);
2198		break;
2199	case GetHubDescriptor:
2200		r8a66597_hub_descriptor(r8a66597,
2201					(struct usb_hub_descriptor *)buf);
2202		break;
2203	case GetHubStatus:
2204		*buf = 0x00;
2205		break;
2206	case GetPortStatus:
2207		if (wIndex > r8a66597->max_root_hub)
2208			goto error;
2209		*(__le32 *)buf = cpu_to_le32(rh->port);
2210		break;
2211	case SetPortFeature:
2212		if (wIndex > r8a66597->max_root_hub)
2213			goto error;
2214		if (wLength != 0)
2215			goto error;
2216
2217		switch (wValue) {
2218		case USB_PORT_FEAT_SUSPEND:
2219			break;
2220		case USB_PORT_FEAT_POWER:
2221			r8a66597_port_power(r8a66597, port, 1);
2222			rh->port |= USB_PORT_STAT_POWER;
2223			break;
2224		case USB_PORT_FEAT_RESET: {
2225			struct r8a66597_device *dev = rh->dev;
2226
2227			rh->port |= USB_PORT_STAT_RESET;
2228
2229			disable_r8a66597_pipe_all(r8a66597, dev);
2230			free_usb_address(r8a66597, dev, 1);
2231
2232			r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2233				      get_dvstctr_reg(port));
2234			mod_timer(&r8a66597->rh_timer,
2235				  jiffies + msecs_to_jiffies(50));
2236			}
2237			break;
2238		default:
2239			goto error;
2240		}
2241		rh->port |= 1 << wValue;
2242		break;
2243	default:
2244error:
2245		ret = -EPIPE;
2246		break;
2247	}
2248
2249	spin_unlock_irqrestore(&r8a66597->lock, flags);
2250	return ret;
2251}
2252
2253#if defined(CONFIG_PM)
2254static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2255{
2256	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2257	int port;
2258
2259	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2260
2261	for (port = 0; port < r8a66597->max_root_hub; port++) {
2262		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2263		unsigned long dvstctr_reg = get_dvstctr_reg(port);
2264
2265		if (!(rh->port & USB_PORT_STAT_ENABLE))
2266			continue;
2267
2268		dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
2269		r8a66597_bclr(r8a66597, UACT, dvstctr_reg);	/* suspend */
2270		rh->port |= USB_PORT_STAT_SUSPEND;
2271
2272		if (rh->dev->udev->do_remote_wakeup) {
2273			msleep(3);	/* waiting last SOF */
2274			r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2275			r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2276			r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2277		}
2278	}
2279
2280	r8a66597->bus_suspended = 1;
2281
2282	return 0;
2283}
2284
2285static int r8a66597_bus_resume(struct usb_hcd *hcd)
2286{
2287	struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2288	int port;
2289
2290	dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2291
2292	for (port = 0; port < r8a66597->max_root_hub; port++) {
2293		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2294		unsigned long dvstctr_reg = get_dvstctr_reg(port);
2295
2296		if (!(rh->port & USB_PORT_STAT_SUSPEND))
2297			continue;
2298
2299		dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
2300		rh->port &= ~USB_PORT_STAT_SUSPEND;
2301		rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
2302		r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2303		msleep(50);
2304		r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2305	}
2306
2307	return 0;
2308
2309}
2310#else
2311#define	r8a66597_bus_suspend	NULL
2312#define	r8a66597_bus_resume	NULL
2313#endif
2314
2315static struct hc_driver r8a66597_hc_driver = {
2316	.description =		hcd_name,
2317	.hcd_priv_size =	sizeof(struct r8a66597),
2318	.irq =			r8a66597_irq,
2319
2320	/*
2321	 * generic hardware linkage
2322	 */
2323	.flags =		HCD_USB2,
2324
2325	.start =		r8a66597_start,
2326	.stop =			r8a66597_stop,
2327
2328	/*
2329	 * managing i/o requests and associated device resources
2330	 */
2331	.urb_enqueue =		r8a66597_urb_enqueue,
2332	.urb_dequeue =		r8a66597_urb_dequeue,
2333	.endpoint_disable =	r8a66597_endpoint_disable,
2334
2335	/*
2336	 * periodic schedule support
2337	 */
2338	.get_frame_number =	r8a66597_get_frame,
2339
2340	/*
2341	 * root hub support
2342	 */
2343	.hub_status_data =	r8a66597_hub_status_data,
2344	.hub_control =		r8a66597_hub_control,
2345	.bus_suspend =		r8a66597_bus_suspend,
2346	.bus_resume =		r8a66597_bus_resume,
2347};
2348
2349#if defined(CONFIG_PM)
2350static int r8a66597_suspend(struct device *dev)
2351{
2352	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2353	int port;
2354
2355	dev_dbg(dev, "%s\n", __func__);
2356
2357	disable_controller(r8a66597);
2358
2359	for (port = 0; port < r8a66597->max_root_hub; port++) {
2360		struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2361
2362		rh->port = 0x00000000;
2363	}
2364
2365	return 0;
2366}
2367
2368static int r8a66597_resume(struct device *dev)
2369{
2370	struct r8a66597		*r8a66597 = dev_get_drvdata(dev);
2371	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);
2372
2373	dev_dbg(dev, "%s\n", __func__);
2374
2375	enable_controller(r8a66597);
2376	usb_root_hub_lost_power(hcd->self.root_hub);
2377
2378	return 0;
2379}
2380
2381static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2382	.suspend = r8a66597_suspend,
2383	.resume = r8a66597_resume,
2384	.poweroff = r8a66597_suspend,
2385	.restore = r8a66597_resume,
2386};
2387
2388#define R8A66597_DEV_PM_OPS	(&r8a66597_dev_pm_ops)
2389#else	/* if defined(CONFIG_PM) */
2390#define R8A66597_DEV_PM_OPS	NULL
2391#endif
2392
2393static int r8a66597_remove(struct platform_device *pdev)
2394{
2395	struct r8a66597		*r8a66597 = platform_get_drvdata(pdev);
2396	struct usb_hcd		*hcd = r8a66597_to_hcd(r8a66597);
2397
2398	del_timer_sync(&r8a66597->rh_timer);
2399	usb_remove_hcd(hcd);
2400	iounmap(r8a66597->reg);
2401	if (r8a66597->pdata->on_chip)
2402		clk_put(r8a66597->clk);
2403	usb_put_hcd(hcd);
2404	return 0;
2405}
2406
2407static int r8a66597_probe(struct platform_device *pdev)
2408{
2409	char clk_name[8];
2410	struct resource *res = NULL, *ires;
2411	int irq = -1;
2412	void __iomem *reg = NULL;
2413	struct usb_hcd *hcd = NULL;
2414	struct r8a66597 *r8a66597;
2415	int ret = 0;
2416	int i;
2417	unsigned long irq_trigger;
2418
2419	if (usb_disabled())
2420		return -ENODEV;
2421
2422	if (pdev->dev.dma_mask) {
2423		ret = -EINVAL;
2424		dev_err(&pdev->dev, "dma not supported\n");
2425		goto clean_up;
2426	}
2427
2428	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2429	if (!res) {
2430		ret = -ENODEV;
2431		dev_err(&pdev->dev, "platform_get_resource error.\n");
2432		goto clean_up;
2433	}
2434
2435	ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2436	if (!ires) {
2437		ret = -ENODEV;
2438		dev_err(&pdev->dev,
2439			"platform_get_resource IORESOURCE_IRQ error.\n");
2440		goto clean_up;
2441	}
2442
2443	irq = ires->start;
2444	irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2445
2446	reg = ioremap(res->start, resource_size(res));
2447	if (reg == NULL) {
2448		ret = -ENOMEM;
2449		dev_err(&pdev->dev, "ioremap error.\n");
2450		goto clean_up;
2451	}
2452
2453	if (pdev->dev.platform_data == NULL) {
2454		dev_err(&pdev->dev, "no platform data\n");
2455		ret = -ENODEV;
2456		goto clean_up;
2457	}
2458
2459	/* initialize hcd */
2460	hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2461	if (!hcd) {
2462		ret = -ENOMEM;
2463		dev_err(&pdev->dev, "Failed to create hcd\n");
2464		goto clean_up;
2465	}
2466	r8a66597 = hcd_to_r8a66597(hcd);
2467	memset(r8a66597, 0, sizeof(struct r8a66597));
2468	platform_set_drvdata(pdev, r8a66597);
2469	r8a66597->pdata = dev_get_platdata(&pdev->dev);
2470	r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2471
2472	if (r8a66597->pdata->on_chip) {
2473		snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2474		r8a66597->clk = clk_get(&pdev->dev, clk_name);
2475		if (IS_ERR(r8a66597->clk)) {
2476			dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2477				clk_name);
2478			ret = PTR_ERR(r8a66597->clk);
2479			goto clean_up2;
2480		}
2481		r8a66597->max_root_hub = 1;
2482	} else
2483		r8a66597->max_root_hub = 2;
2484
2485	spin_lock_init(&r8a66597->lock);
2486	init_timer(&r8a66597->rh_timer);
2487	r8a66597->rh_timer.function = r8a66597_timer;
2488	r8a66597->rh_timer.data = (unsigned long)r8a66597;
2489	r8a66597->reg = reg;
2490
2491	/* make sure no interrupts are pending */
2492	ret = r8a66597_clock_enable(r8a66597);
2493	if (ret < 0)
2494		goto clean_up3;
2495	disable_controller(r8a66597);
2496
2497	for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2498		INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2499		init_timer(&r8a66597->td_timer[i]);
2500		r8a66597->td_timer[i].function = r8a66597_td_timer;
2501		r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2502		setup_timer(&r8a66597->interval_timer[i],
2503				r8a66597_interval_timer,
2504				(unsigned long)r8a66597);
2505	}
2506	INIT_LIST_HEAD(&r8a66597->child_device);
2507
2508	hcd->rsrc_start = res->start;
2509	hcd->has_tt = 1;
2510
2511	ret = usb_add_hcd(hcd, irq, irq_trigger);
2512	if (ret != 0) {
2513		dev_err(&pdev->dev, "Failed to add hcd\n");
2514		goto clean_up3;
2515	}
2516	device_wakeup_enable(hcd->self.controller);
2517
2518	return 0;
2519
2520clean_up3:
2521	if (r8a66597->pdata->on_chip)
2522		clk_put(r8a66597->clk);
2523clean_up2:
2524	usb_put_hcd(hcd);
2525
2526clean_up:
2527	if (reg)
2528		iounmap(reg);
2529
2530	return ret;
2531}
2532
2533static struct platform_driver r8a66597_driver = {
2534	.probe =	r8a66597_probe,
2535	.remove =	r8a66597_remove,
2536	.driver		= {
2537		.name = hcd_name,
2538		.owner	= THIS_MODULE,
2539		.pm	= R8A66597_DEV_PM_OPS,
2540	},
2541};
2542
2543module_platform_driver(r8a66597_driver);