Linux Audio

Check our new training course

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