include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / usb / host / r8a66597-hcd.c
index 0a699ef..d478ffa 100644 (file)
@@ -26,7 +26,6 @@
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
-#include <linux/smp_lock.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/timer.h>
 #include <linux/usb.h>
 #include <linux/platform_device.h>
 #include <linux/io.h>
+#include <linux/mm.h>
 #include <linux/irq.h>
+#include <linux/slab.h>
+#include <asm/cacheflush.h>
 
 #include "../core/hcd.h"
 #include "r8a66597.h"
 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Yoshihiro Shimoda");
+MODULE_ALIAS("platform:r8a66597_hcd");
 
-#define DRIVER_VERSION "29 May 2007"
+#define DRIVER_VERSION "2009-05-26"
 
 static const char hcd_name[] = "r8a66597_hcd";
 
-/* module parameters */
-static unsigned short clock = XTAL12;
-module_param(clock, ushort, 0644);
-MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
-               "(default=0)");
-
-static unsigned short vif = LDRV;
-module_param(vif, ushort, 0644);
-MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
-
-static unsigned short endian;
-module_param(endian, ushort, 0644);
-MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
-
-static unsigned short irq_sense = INTL;
-module_param(irq_sense, ushort, 0644);
-MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
-               "(default=32)");
-
 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
 static int r8a66597_get_frame(struct usb_hcd *hcd);
 
@@ -105,39 +89,112 @@ static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
        r8a66597_write(r8a66597, val, devadd_reg);
 }
 
-static int enable_controller(struct r8a66597 *r8a66597)
+static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
 {
        u16 tmp;
        int i = 0;
 
-       do {
-               r8a66597_write(r8a66597, USBE, SYSCFG0);
-               tmp = r8a66597_read(r8a66597, SYSCFG0);
-               if (i++ > 1000) {
-                       err("register access fail.");
-                       return -ENXIO;
-               }
-       } while ((tmp & USBE) != USBE);
-       r8a66597_bclr(r8a66597, USBE, SYSCFG0);
-       r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
+       if (r8a66597->pdata->on_chip) {
+#ifdef CONFIG_HAVE_CLK
+               clk_enable(r8a66597->clk);
+#endif
+               do {
+                       r8a66597_write(r8a66597, SCKE, SYSCFG0);
+                       tmp = r8a66597_read(r8a66597, SYSCFG0);
+                       if (i++ > 1000) {
+                               printk(KERN_ERR "r8a66597: reg access fail.\n");
+                               return -ENXIO;
+                       }
+               } while ((tmp & SCKE) != SCKE);
+               r8a66597_write(r8a66597, 0x04, 0x02);
+       } else {
+               do {
+                       r8a66597_write(r8a66597, USBE, SYSCFG0);
+                       tmp = r8a66597_read(r8a66597, SYSCFG0);
+                       if (i++ > 1000) {
+                               printk(KERN_ERR "r8a66597: reg access fail.\n");
+                               return -ENXIO;
+                       }
+               } while ((tmp & USBE) != USBE);
+               r8a66597_bclr(r8a66597, USBE, SYSCFG0);
+               r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
+                             XTAL, SYSCFG0);
+
+               i = 0;
+               r8a66597_bset(r8a66597, XCKE, SYSCFG0);
+               do {
+                       msleep(1);
+                       tmp = r8a66597_read(r8a66597, SYSCFG0);
+                       if (i++ > 500) {
+                               printk(KERN_ERR "r8a66597: reg access fail.\n");
+                               return -ENXIO;
+                       }
+               } while ((tmp & SCKE) != SCKE);
+       }
+
+       return 0;
+}
+
+static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
+{
+       r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
+       udelay(1);
+
+       if (r8a66597->pdata->on_chip) {
+#ifdef CONFIG_HAVE_CLK
+               clk_disable(r8a66597->clk);
+#endif
+       } else {
+               r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
+               r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
+               r8a66597_bclr(r8a66597, USBE, SYSCFG0);
+       }
+}
+
+static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
+{
+       u16 val;
+
+       val = port ? DRPD : DCFM | DRPD;
+       r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
+       r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
+
+       r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
+       r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
+       r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
+}
+
+static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
+{
+       u16 val, tmp;
+
+       r8a66597_write(r8a66597, 0, get_intenb_reg(port));
+       r8a66597_write(r8a66597, 0, get_intsts_reg(port));
+
+       r8a66597_port_power(r8a66597, port, 0);
 
-       i = 0;
-       r8a66597_bset(r8a66597, XCKE, SYSCFG0);
        do {
-               msleep(1);
-               tmp = r8a66597_read(r8a66597, SYSCFG0);
-               if (i++ > 500) {
-                       err("register access fail.");
-                       return -ENXIO;
-               }
-       } while ((tmp & SCKE) != SCKE);
+               tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
+               udelay(640);
+       } while (tmp == EDGESTS);
 
-       r8a66597_bset(r8a66597, DCFM | DRPD, SYSCFG0);
-       r8a66597_bset(r8a66597, DRPD, SYSCFG1);
+       val = port ? DRPD : DCFM | DRPD;
+       r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
+       r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
+}
+
+static int enable_controller(struct r8a66597 *r8a66597)
+{
+       int ret, port;
+       u16 vif = r8a66597->pdata->vif ? LDRV : 0;
+       u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
+       u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
+
+       ret = r8a66597_clock_enable(r8a66597);
+       if (ret < 0)
+               return ret;
 
        r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
-       r8a66597_bset(r8a66597, HSE, SYSCFG0);
-       r8a66597_bset(r8a66597, HSE, SYSCFG1);
        r8a66597_bset(r8a66597, USBE, SYSCFG0);
 
        r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
@@ -145,53 +202,39 @@ static int enable_controller(struct r8a66597 *r8a66597)
        r8a66597_bset(r8a66597, BRDY0, BRDYENB);
        r8a66597_bset(r8a66597, BEMP0, BEMPENB);
 
-       r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA0CFG);
-       r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA1CFG);
-
        r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
        r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
        r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
-
        r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
 
        r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
-       r8a66597_bclr(r8a66597, DTCHE, INTENB1);
-       r8a66597_bset(r8a66597, ATTCHE, INTENB1);
-       r8a66597_bclr(r8a66597, DTCHE, INTENB2);
-       r8a66597_bset(r8a66597, ATTCHE, INTENB2);
+
+       for (port = 0; port < r8a66597->max_root_hub; port++)
+               r8a66597_enable_port(r8a66597, port);
 
        return 0;
 }
 
 static void disable_controller(struct r8a66597 *r8a66597)
 {
-       u16 tmp;
+       int port;
 
+       /* disable interrupts */
        r8a66597_write(r8a66597, 0, INTENB0);
        r8a66597_write(r8a66597, 0, INTENB1);
-       r8a66597_write(r8a66597, 0, INTENB2);
-       r8a66597_write(r8a66597, 0, INTSTS0);
-       r8a66597_write(r8a66597, 0, INTSTS1);
-       r8a66597_write(r8a66597, 0, INTSTS2);
+       r8a66597_write(r8a66597, 0, BRDYENB);
+       r8a66597_write(r8a66597, 0, BEMPENB);
+       r8a66597_write(r8a66597, 0, NRDYENB);
 
-       r8a66597_port_power(r8a66597, 0, 0);
-       r8a66597_port_power(r8a66597, 1, 0);
-
-       do {
-               tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
-               udelay(640);
-       } while (tmp == EDGESTS);
+       /* clear status */
+       r8a66597_write(r8a66597, 0, BRDYSTS);
+       r8a66597_write(r8a66597, 0, NRDYSTS);
+       r8a66597_write(r8a66597, 0, BEMPSTS);
 
-       r8a66597_bclr(r8a66597, DCFM | DRPD, SYSCFG0);
-       r8a66597_bclr(r8a66597, DRPD, SYSCFG1);
-       r8a66597_bclr(r8a66597, HSE, SYSCFG0);
-       r8a66597_bclr(r8a66597, HSE, SYSCFG1);
+       for (port = 0; port < r8a66597->max_root_hub; port++)
+               r8a66597_disable_port(r8a66597, port);
 
-       r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
-       udelay(1);
-       r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
-       r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
-       r8a66597_bclr(r8a66597, USBE, SYSCFG0);
+       r8a66597_clock_disable(r8a66597);
 }
 
 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
@@ -219,12 +262,13 @@ static int is_hub_limit(char *devpath)
        return ((strlen(devpath) >= 4) ? 1 : 0);
 }
 
-static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
+static void get_port_number(struct r8a66597 *r8a66597,
+                           char *devpath, u16 *root_port, u16 *hub_port)
 {
        if (root_port) {
                *root_port = (devpath[0] & 0x0F) - 1;
-               if (*root_port >= R8A66597_MAX_ROOT_HUB)
-                       err("illegal root port number");
+               if (*root_port >= r8a66597->max_root_hub)
+                       printk(KERN_ERR "r8a66597: Illegal root port number.\n");
        }
        if (hub_port)
                *hub_port = devpath[2] & 0x0F;
@@ -245,7 +289,7 @@ static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
                usbspd = HSMODE;
                break;
        default:
-               err("unknown speed");
+               printk(KERN_ERR "r8a66597: unknown speed\n");
                break;
        }
 
@@ -271,9 +315,9 @@ static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
 {
        u16 pipenum = pipe->info.pipenum;
-       unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
-       unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
-       unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
+       const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
+       const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
+       const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
 
        if (dma_ch > R8A66597_PIPE_NO_DMA)      /* dma fifo not use? */
                dma_ch = R8A66597_PIPE_NO_DMA;
@@ -325,7 +369,8 @@ static int make_r8a66597_device(struct r8a66597 *r8a66597,
        INIT_LIST_HEAD(&dev->device_list);
        list_add_tail(&dev->device_list, &r8a66597->child_device);
 
-       get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
+       get_port_number(r8a66597, urb->dev->devpath,
+                       &dev->root_port, &dev->hub_port);
        if (!is_child_device(urb->dev->devpath))
                r8a66597->root_hub[dev->root_port].dev = dev;
 
@@ -344,7 +389,7 @@ static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
        struct r8a66597_device *dev;
 
        if (is_hub_limit(urb->dev->devpath)) {
-               err("Externel hub limit reached.");
+               dev_err(&urb->dev->dev, "External hub limit reached.\n");
                return 0;
        }
 
@@ -365,15 +410,16 @@ static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
                return addr;
        }
 
-       err("cannot communicate with a USB device more than 10.(%x)",
-           r8a66597->address_map);
+       dev_err(&urb->dev->dev,
+               "cannot communicate with a USB device more than 10.(%x)\n",
+               r8a66597->address_map);
 
        return 0;
 }
 
 /* this function must be called with interrupt disabled */
 static void free_usb_address(struct r8a66597 *r8a66597,
-                            struct r8a66597_device *dev)
+                            struct r8a66597_device *dev, int reset)
 {
        int port;
 
@@ -385,11 +431,17 @@ static void free_usb_address(struct r8a66597 *r8a66597,
        dev->state = USB_STATE_DEFAULT;
        r8a66597->address_map &= ~(1 << dev->address);
        dev->address = 0;
-       dev_set_drvdata(&dev->udev->dev, NULL);
+       /*
+        * Only when resetting USB, it is necessary to erase drvdata. When
+        * a usb device with usb hub is disconnect, "dev->udev" is already
+        * freed on usb_desconnect(). So we cannot access the data.
+        */
+       if (reset)
+               dev_set_drvdata(&dev->udev->dev, NULL);
        list_del(&dev->device_list);
        kfree(dev);
 
-       for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
+       for (port = 0; port < r8a66597->max_root_hub; port++) {
                if (r8a66597->root_hub[port].dev == dev) {
                        r8a66597->root_hub[port].dev = NULL;
                        break;
@@ -406,7 +458,8 @@ static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
        do {
                tmp = r8a66597_read(r8a66597, reg);
                if (i++ > 1000000) {
-                       err("register%lx, loop %x is timeout", reg, loop);
+                       printk(KERN_ERR "r8a66597: register%lx, loop %x "
+                              "is timeout\n", reg, loop);
                        break;
                }
                ndelay(1);
@@ -463,10 +516,20 @@ static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
                r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
 }
 
+static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
+{
+       if (r8a66597->pdata->on_chip)
+               return MBW_32;
+       else
+               return MBW_16;
+}
+
 /* this function must be called with interrupt disabled */
 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
 {
-       r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
+       unsigned short mbw = mbw_value(r8a66597);
+
+       r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
        r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
 }
 
@@ -474,11 +537,13 @@ static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
                                         struct r8a66597_pipe *pipe)
 {
+       unsigned short mbw = mbw_value(r8a66597);
+
        cfifo_change(r8a66597, 0);
-       r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
-       r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
+       r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
+       r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
 
-       r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
+       r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
                      pipe->fifosel);
        r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
 }
@@ -576,13 +641,9 @@ static void pipe_buffer_setting(struct r8a66597 *r8a66597,
                       PIPEBUF);
        r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
                       PIPEMAXP);
-       if (info->interval)
-               info->interval--;
        r8a66597_write(r8a66597, info->interval, PIPEPERI);
 }
 
-
-
 /* this function must be called with interrupt disabled */
 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
 {
@@ -614,9 +675,9 @@ static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
        u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
 
        memset(array, 0, sizeof(array));
-       switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
+       switch (usb_endpoint_type(ep)) {
        case USB_ENDPOINT_XFER_BULK:
-               if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
+               if (usb_endpoint_dir_in(ep))
                        array[i++] = 4;
                else {
                        array[i++] = 3;
@@ -624,7 +685,7 @@ static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
                }
                break;
        case USB_ENDPOINT_XFER_INT:
-               if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+               if (usb_endpoint_dir_in(ep)) {
                        array[i++] = 6;
                        array[i++] = 7;
                        array[i++] = 8;
@@ -632,13 +693,13 @@ static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
                        array[i++] = 9;
                break;
        case USB_ENDPOINT_XFER_ISOC:
-               if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
+               if (usb_endpoint_dir_in(ep))
                        array[i++] = 2;
                else
                        array[i++] = 1;
                break;
        default:
-               err("Illegal type");
+               printk(KERN_ERR "r8a66597: Illegal type\n");
                return 0;
        }
 
@@ -668,7 +729,7 @@ static u16 get_r8a66597_type(__u8 type)
                r8a66597_type = R8A66597_ISO;
                break;
        default:
-               err("Illegal type");
+               printk(KERN_ERR "r8a66597: Illegal type\n");
                r8a66597_type = 0x0000;
                break;
        }
@@ -687,7 +748,7 @@ static u16 get_bufnum(u16 pipenum)
        else if (check_interrupt(pipenum))
                bufnum = 4 + (pipenum - 6);
        else
-               err("Illegal pipenum (%d)", pipenum);
+               printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 
        return bufnum;
 }
@@ -703,7 +764,7 @@ static u16 get_buf_bsize(u16 pipenum)
        else if (check_interrupt(pipenum))
                buf_bsize = 0;
        else
-               err("Illegal pipenum (%d)", pipenum);
+               printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
 
        return buf_bsize;
 }
@@ -716,24 +777,31 @@ static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
 {
        int i;
        struct r8a66597_pipe_info *info = &pipe->info;
+       unsigned short mbw = mbw_value(r8a66597);
+
+       /* pipe dma is only for external controlles */
+       if (r8a66597->pdata->on_chip)
+               return;
 
        if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
                for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
                        if ((r8a66597->dma_map & (1 << i)) != 0)
                                continue;
 
-                       info("address %d, EndpointAddress 0x%02x use DMA FIFO",
-                            usb_pipedevice(urb->pipe),
-                            info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
-                                           : info->epnum);
+                       dev_info(&dev->udev->dev,
+                                "address %d, EndpointAddress 0x%02x use "
+                                "DMA FIFO\n", usb_pipedevice(urb->pipe),
+                                info->dir_in ?
+                                       USB_ENDPOINT_DIR_MASK + info->epnum
+                                       : info->epnum);
 
                        r8a66597->dma_map |= 1 << i;
                        dev->dma_map |= 1 << i;
                        set_pipe_reg_addr(pipe, i);
 
                        cfifo_change(r8a66597, 0);
-                       r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
-                                     MBW | CURPIPE, pipe->fifosel);
+                       r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
+                                     mbw | CURPIPE, pipe->fifosel);
 
                        r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
                                          pipe->info.pipenum);
@@ -761,6 +829,26 @@ static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
        enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
 }
 
+static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
+                             int status)
+__releases(r8a66597->lock)
+__acquires(r8a66597->lock)
+{
+       if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
+               void *ptr;
+
+               for (ptr = urb->transfer_buffer;
+                    ptr < urb->transfer_buffer + urb->transfer_buffer_length;
+                    ptr += PAGE_SIZE)
+                       flush_dcache_page(virt_to_page(ptr));
+       }
+
+       usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
+       spin_unlock(&r8a66597->lock);
+       usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
+       spin_lock(&r8a66597->lock);
+}
+
 /* this function must be called with interrupt disabled */
 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
 {
@@ -772,8 +860,6 @@ static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
                return;
 
        list_for_each_entry_safe(td, next, list, queue) {
-               if (!td)
-                       continue;
                if (td->address != address)
                        continue;
 
@@ -781,15 +867,9 @@ static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
                list_del(&td->queue);
                kfree(td);
 
-               if (urb) {
-                       usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
-                                       urb);
+               if (urb)
+                       r8a66597_urb_done(r8a66597, urb, -ENODEV);
 
-                       spin_unlock(&r8a66597->lock);
-                       usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
-                                       -ENODEV);
-                       spin_lock(&r8a66597->lock);
-               }
                break;
        }
 }
@@ -824,6 +904,51 @@ static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
        dev->dma_map = 0;
 }
 
+static u16 get_interval(struct urb *urb, __u8 interval)
+{
+       u16 time = 1;
+       int i;
+
+       if (urb->dev->speed == USB_SPEED_HIGH) {
+               if (interval > IITV)
+                       time = IITV;
+               else
+                       time = interval ? interval - 1 : 0;
+       } else {
+               if (interval > 128) {
+                       time = IITV;
+               } else {
+                       /* calculate the nearest value for PIPEPERI */
+                       for (i = 0; i < 7; i++) {
+                               if ((1 << i) < interval &&
+                                   (1 << (i + 1) > interval))
+                                       time = 1 << i;
+                       }
+               }
+       }
+
+       return time;
+}
+
+static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
+{
+       __u8 i;
+       unsigned long time = 1;
+
+       if (usb_pipeisoc(urb->pipe))
+               return 0;
+
+       if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
+               for (i = 0; i < (interval - 1); i++)
+                       time *= 2;
+               time = time * 125 / 1000;       /* uSOF -> msec */
+       } else {
+               time = interval;
+       }
+
+       return time;
+}
+
 /* this function must be called with interrupt disabled */
 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
                           struct usb_host_endpoint *hep,
@@ -833,14 +958,19 @@ static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
 
        info.pipenum = get_empty_pipenum(r8a66597, ep);
        info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
-       info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
-       info.maxpacket = ep->wMaxPacketSize;
-       info.type = get_r8a66597_type(ep->bmAttributes
-                                     & USB_ENDPOINT_XFERTYPE_MASK);
+       info.epnum = usb_endpoint_num(ep);
+       info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
+       info.type = get_r8a66597_type(usb_endpoint_type(ep));
        info.bufnum = get_bufnum(info.pipenum);
        info.buf_bsize = get_buf_bsize(info.pipenum);
-       info.interval = ep->bInterval;
-       if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
+       if (info.type == R8A66597_BULK) {
+               info.interval = 0;
+               info.timer_interval = 0;
+       } else {
+               info.interval = get_interval(urb, ep->bInterval);
+               info.timer_interval = get_timer_interval(urb, ep->bInterval);
+       }
+       if (usb_endpoint_dir_in(ep))
                info.dir_in = 1;
        else
                info.dir_in = 0;
@@ -874,13 +1004,53 @@ static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
        disable_irq_nrdy(r8a66597, pipenum);
 }
 
-/* this function must be called with interrupt disabled */
-static void r8a66597_usb_preconnect(struct r8a66597 *r8a66597, int port)
+static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
 {
-       r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
-                                        | (1 << USB_PORT_FEAT_C_CONNECTION);
-       r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
-       r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
+       mod_timer(&r8a66597->rh_timer,
+                       jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
+}
+
+static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
+                                       int connect)
+{
+       struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
+
+       rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
+       rh->scount = R8A66597_MAX_SAMPLING;
+       if (connect)
+               rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
+       else
+               rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
+       rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
+
+       r8a66597_root_hub_start_polling(r8a66597);
+}
+
+/* this function must be called with interrupt disabled */
+static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
+                                       u16 syssts)
+__releases(r8a66597->lock)
+__acquires(r8a66597->lock)
+{
+       if (syssts == SE0) {
+               r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
+               r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
+       } else {
+               if (syssts == FS_JSTS)
+                       r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
+               else if (syssts == LS_JSTS)
+                       r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
+
+               r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
+               r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
+
+               if (r8a66597->bus_suspended)
+                       usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
+       }
+
+       spin_unlock(&r8a66597->lock);
+       usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
+       spin_lock(&r8a66597->lock);
 }
 
 /* this function must be called with interrupt disabled */
@@ -889,6 +1059,8 @@ static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
        u16 speed = get_rh_usb_speed(r8a66597, port);
        struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
 
+       rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
+                     (1 << USB_PORT_FEAT_LOWSPEED));
        if (speed == HSMODE)
                rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
        else if (speed == LSMODE)
@@ -903,13 +1075,10 @@ static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
 {
        struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
 
-       r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
-       r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
-
        disable_r8a66597_pipe_all(r8a66597, dev);
-       free_usb_address(r8a66597, dev);
+       free_usb_address(r8a66597, dev, 0);
 
-       r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
+       start_root_hub_sampling(r8a66597, port, 0);
 }
 
 /* this function must be called with interrupt disabled */
@@ -917,7 +1086,7 @@ static void prepare_setup_packet(struct r8a66597 *r8a66597,
                                 struct r8a66597_td *td)
 {
        int i;
-       u16 *p = (u16 *)td->urb->setup_packet;
+       __le16 *p = (__le16 *)td->urb->setup_packet;
        unsigned long setup_addr = USBREQ;
 
        r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
@@ -925,7 +1094,7 @@ static void prepare_setup_packet(struct r8a66597 *r8a66597,
        r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
 
        for (i = 0; i < 4; i++) {
-               r8a66597_write(r8a66597, p[i], setup_addr);
+               r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
                setup_addr += 2;
        }
        r8a66597_write(r8a66597, SUREQ, DCPCTR);
@@ -959,9 +1128,9 @@ static void prepare_packet_read(struct r8a66597 *r8a66597,
                                r8a66597_write(r8a66597, TRCLR,
                                                td->pipe->pipetre);
                                r8a66597_write(r8a66597,
-                                               (urb->transfer_buffer_length
-                                               + td->maxpacket - 1)
-                                               / td->maxpacket,
+                                               DIV_ROUND_UP
+                                                 (urb->transfer_buffer_length,
+                                                  td->maxpacket),
                                                td->pipe->pipetrn);
                                r8a66597_bset(r8a66597, TRENB,
                                                td->pipe->pipetre);
@@ -1020,8 +1189,7 @@ static void prepare_status_packet(struct r8a66597 *r8a66597,
                r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
                r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
                r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
-               r8a66597_write(r8a66597, BCLR, CFIFOCTR);
-               r8a66597_write(r8a66597, BVAL, CFIFOCTR);
+               r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
                enable_irq_empty(r8a66597, 0);
        } else {
                r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
@@ -1069,7 +1237,7 @@ static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
                prepare_status_packet(r8a66597, td);
                break;
        default:
-               err("invalid type.");
+               printk(KERN_ERR "r8a66597: invalid type.\n");
                break;
        }
 
@@ -1142,10 +1310,7 @@ __releases(r8a66597->lock) __acquires(r8a66597->lock)
                if (usb_pipeisoc(urb->pipe))
                        urb->start_frame = r8a66597_get_frame(hcd);
 
-               usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
-               spin_unlock(&r8a66597->lock);
-               usb_hcd_giveback_urb(hcd, urb, status);
-               spin_lock(&r8a66597->lock);
+               r8a66597_urb_done(r8a66597, urb, status);
        }
 
        if (restart) {
@@ -1177,7 +1342,7 @@ static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
        if (unlikely((tmp & FRDY) == 0)) {
                pipe_stop(r8a66597, td->pipe);
                pipe_irq_disable(r8a66597, pipenum);
-               err("in fifo not ready (%d)", pipenum);
+               printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
                finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
                return;
        }
@@ -1252,7 +1417,7 @@ static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
        if (unlikely((tmp & FRDY) == 0)) {
                pipe_stop(r8a66597, td->pipe);
                pipe_irq_disable(r8a66597, pipenum);
-               err("out write fifo not ready. (%d)", pipenum);
+               printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
                finish_request(r8a66597, td, pipenum, urb, -EPIPE);
                return;
        }
@@ -1266,7 +1431,7 @@ static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
                           (int)urb->iso_frame_desc[td->iso_cnt].length);
        } else {
                buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
-               size = min((int)bufsize,
+               size = min_t(u32, bufsize,
                           urb->transfer_buffer_length - urb->actual_length);
        }
 
@@ -1453,15 +1618,6 @@ static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
        }
 }
 
-static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
-{
-       struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
-
-       rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
-       rh->scount = R8A66597_MAX_SAMPLING;
-       mod_timer(&r8a66597->rh_timer, jiffies + msecs_to_jiffies(50));
-}
-
 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
 {
        struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
@@ -1488,13 +1644,18 @@ static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
                        r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
 
                        /* start usb bus sampling */
-                       start_root_hub_sampling(r8a66597, 1);
+                       start_root_hub_sampling(r8a66597, 1, 1);
                }
                if (mask2 & DTCH) {
                        r8a66597_write(r8a66597, ~DTCH, INTSTS2);
                        r8a66597_bclr(r8a66597, DTCHE, INTENB2);
                        r8a66597_usb_disconnect(r8a66597, 1);
                }
+               if (mask2 & BCHG) {
+                       r8a66597_write(r8a66597, ~BCHG, INTSTS2);
+                       r8a66597_bclr(r8a66597, BCHGE, INTENB2);
+                       usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
+               }
        }
 
        if (mask1) {
@@ -1503,13 +1664,19 @@ static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
                        r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
 
                        /* start usb bus sampling */
-                       start_root_hub_sampling(r8a66597, 0);
+                       start_root_hub_sampling(r8a66597, 0, 1);
                }
                if (mask1 & DTCH) {
                        r8a66597_write(r8a66597, ~DTCH, INTSTS1);
                        r8a66597_bclr(r8a66597, DTCHE, INTENB1);
                        r8a66597_usb_disconnect(r8a66597, 0);
                }
+               if (mask1 & BCHG) {
+                       r8a66597_write(r8a66597, ~BCHG, INTSTS1);
+                       r8a66597_bclr(r8a66597, BCHGE, INTENB1);
+                       usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
+               }
+
                if (mask1 & SIGN) {
                        r8a66597_write(r8a66597, ~SIGN, INTSTS1);
                        status = get_urb_error(r8a66597, 0);
@@ -1546,41 +1713,55 @@ static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
                if ((tmp & USBRST) == USBRST) {
                        r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
                                      dvstctr_reg);
-                       mod_timer(&r8a66597->rh_timer,
-                                 jiffies + msecs_to_jiffies(50));
+                       r8a66597_root_hub_start_polling(r8a66597);
                } else
                        r8a66597_usb_connect(r8a66597, port);
        }
 
+       if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
+               r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
+               r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
+       }
+
        if (rh->scount > 0) {
                tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
                if (tmp == rh->old_syssts) {
                        rh->scount--;
-                       if (rh->scount == 0) {
-                               if (tmp == FS_JSTS) {
-                                       r8a66597_bset(r8a66597, HSE,
-                                                     get_syscfg_reg(port));
-                                       r8a66597_usb_preconnect(r8a66597, port);
-                               } else if (tmp == LS_JSTS) {
-                                       r8a66597_bclr(r8a66597, HSE,
-                                                     get_syscfg_reg(port));
-                                       r8a66597_usb_preconnect(r8a66597, port);
-                               } else if (tmp == SE0)
-                                       r8a66597_bset(r8a66597, ATTCHE,
-                                                     get_intenb_reg(port));
-                       } else {
-                               mod_timer(&r8a66597->rh_timer,
-                                         jiffies + msecs_to_jiffies(50));
-                       }
+                       if (rh->scount == 0)
+                               r8a66597_check_syssts(r8a66597, port, tmp);
+                       else
+                               r8a66597_root_hub_start_polling(r8a66597);
                } else {
                        rh->scount = R8A66597_MAX_SAMPLING;
                        rh->old_syssts = tmp;
-                       mod_timer(&r8a66597->rh_timer,
-                                 jiffies + msecs_to_jiffies(50));
+                       r8a66597_root_hub_start_polling(r8a66597);
                }
        }
 }
 
+static void r8a66597_interval_timer(unsigned long _r8a66597)
+{
+       struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
+       unsigned long flags;
+       u16 pipenum;
+       struct r8a66597_td *td;
+
+       spin_lock_irqsave(&r8a66597->lock, flags);
+
+       for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
+               if (!(r8a66597->interval_map & (1 << pipenum)))
+                       continue;
+               if (timer_pending(&r8a66597->interval_timer[pipenum]))
+                       continue;
+
+               td = r8a66597_get_td(r8a66597, pipenum);
+               if (td)
+                       start_transfer(r8a66597, td);
+       }
+
+       spin_unlock_irqrestore(&r8a66597->lock, flags);
+}
+
 static void r8a66597_td_timer(unsigned long _r8a66597)
 {
        struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
@@ -1636,11 +1817,12 @@ static void r8a66597_timer(unsigned long _r8a66597)
 {
        struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
        unsigned long flags;
+       int port;
 
        spin_lock_irqsave(&r8a66597->lock, flags);
 
-       r8a66597_root_hub_control(r8a66597, 0);
-       r8a66597_root_hub_control(r8a66597, 1);
+       for (port = 0; port < r8a66597->max_root_hub; port++)
+               r8a66597_root_hub_control(r8a66597, port);
 
        spin_unlock_irqrestore(&r8a66597->lock, flags);
 }
@@ -1677,7 +1859,7 @@ static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
        u16 root_port, hub_port;
 
        if (usb_address == 0) {
-               get_port_number(urb->dev->devpath,
+               get_port_number(r8a66597, urb->dev->devpath,
                                &root_port, &hub_port);
                set_devadd_reg(r8a66597, 0,
                               get_r8a66597_usb_speed(urb->dev->speed),
@@ -1762,10 +1944,17 @@ static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
        urb->hcpriv = td;
 
        if (request) {
-               ret = start_transfer(r8a66597, td);
-               if (ret < 0) {
-                       list_del(&td->queue);
-                       kfree(td);
+               if (td->pipe->info.timer_interval) {
+                       r8a66597->interval_map |= 1 << td->pipenum;
+                       mod_timer(&r8a66597->interval_timer[td->pipenum],
+                                 jiffies + msecs_to_jiffies(
+                                       td->pipe->info.timer_interval));
+               } else {
+                       ret = start_transfer(r8a66597, td);
+                       if (ret < 0) {
+                               list_del(&td->queue);
+                               kfree(td);
+                       }
                }
        } else
                set_td_timer(r8a66597, td);
@@ -1867,15 +2056,13 @@ static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
        struct list_head *list = &r8a66597->child_device;
 
        list_for_each_entry(dev, list, device_list) {
-               if (!dev)
-                       continue;
                if (dev->usb_address != addr)
                        continue;
 
                return dev;
        }
 
-       err("get_r8a66597_device fail.(%d)\n", addr);
+       printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
        return NULL;
 }
 
@@ -1905,7 +2092,7 @@ static void update_usb_address_map(struct r8a66597 *r8a66597,
                                spin_lock_irqsave(&r8a66597->lock, flags);
                                dev = get_r8a66597_device(r8a66597, addr);
                                disable_r8a66597_pipe_all(r8a66597, dev);
-                               free_usb_address(r8a66597, dev);
+                               free_usb_address(r8a66597, dev, 0);
                                put_child_connect_map(r8a66597, addr);
                                spin_unlock_irqrestore(&r8a66597->lock, flags);
                        }
@@ -1945,7 +2132,7 @@ static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
 
        *buf = 0;       /* initialize (no change) */
 
-       for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
+       for (i = 0; i < r8a66597->max_root_hub; i++) {
                if (r8a66597->root_hub[i].port & 0xffff0000)
                        *buf |= 1 << (i + 1);
        }
@@ -1960,11 +2147,11 @@ static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
 {
        desc->bDescriptorType = 0x29;
        desc->bHubContrCurrent = 0;
-       desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
+       desc->bNbrPorts = r8a66597->max_root_hub;
        desc->bDescLength = 9;
        desc->bPwrOn2PwrGood = 0;
        desc->wHubCharacteristics = cpu_to_le16(0x0011);
-       desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
+       desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
        desc->bitmap[1] = ~0;
 }
 
@@ -1992,14 +2179,14 @@ static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
                }
                break;
        case ClearPortFeature:
-               if (wIndex > R8A66597_MAX_ROOT_HUB)
+               if (wIndex > r8a66597->max_root_hub)
                        goto error;
                if (wLength != 0)
                        goto error;
 
                switch (wValue) {
                case USB_PORT_FEAT_ENABLE:
-                       rh->port &= (1 << USB_PORT_FEAT_POWER);
+                       rh->port &= ~(1 << USB_PORT_FEAT_POWER);
                        break;
                case USB_PORT_FEAT_SUSPEND:
                        break;
@@ -2025,12 +2212,12 @@ static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
                *buf = 0x00;
                break;
        case GetPortStatus:
-               if (wIndex > R8A66597_MAX_ROOT_HUB)
+               if (wIndex > r8a66597->max_root_hub)
                        goto error;
-               *(u32 *)buf = rh->port;
+               *(__le32 *)buf = cpu_to_le32(rh->port);
                break;
        case SetPortFeature:
-               if (wIndex > R8A66597_MAX_ROOT_HUB)
+               if (wIndex > r8a66597->max_root_hub)
                        goto error;
                if (wLength != 0)
                        goto error;
@@ -2048,7 +2235,7 @@ static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
                        rh->port |= (1 << USB_PORT_FEAT_RESET);
 
                        disable_r8a66597_pipe_all(r8a66597, dev);
-                       free_usb_address(r8a66597, dev);
+                       free_usb_address(r8a66597, dev, 1);
 
                        r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
                                      get_dvstctr_reg(port));
@@ -2071,6 +2258,68 @@ error:
        return ret;
 }
 
+#if defined(CONFIG_PM)
+static int r8a66597_bus_suspend(struct usb_hcd *hcd)
+{
+       struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
+       int port;
+
+       dbg("%s", __func__);
+
+       for (port = 0; port < r8a66597->max_root_hub; port++) {
+               struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
+               unsigned long dvstctr_reg = get_dvstctr_reg(port);
+
+               if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
+                       continue;
+
+               dbg("suspend port = %d", port);
+               r8a66597_bclr(r8a66597, UACT, dvstctr_reg);     /* suspend */
+               rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
+
+               if (rh->dev->udev->do_remote_wakeup) {
+                       msleep(3);      /* waiting last SOF */
+                       r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
+                       r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
+                       r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
+               }
+       }
+
+       r8a66597->bus_suspended = 1;
+
+       return 0;
+}
+
+static int r8a66597_bus_resume(struct usb_hcd *hcd)
+{
+       struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
+       int port;
+
+       dbg("%s", __func__);
+
+       for (port = 0; port < r8a66597->max_root_hub; port++) {
+               struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
+               unsigned long dvstctr_reg = get_dvstctr_reg(port);
+
+               if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
+                       continue;
+
+               dbg("resume port = %d", port);
+               rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
+               rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
+               r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
+               msleep(50);
+               r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
+       }
+
+       return 0;
+
+}
+#else
+#define        r8a66597_bus_suspend    NULL
+#define        r8a66597_bus_resume     NULL
+#endif
+
 static struct hc_driver r8a66597_hc_driver = {
        .description =          hcd_name,
        .hcd_priv_size =        sizeof(struct r8a66597),
@@ -2101,23 +2350,52 @@ static struct hc_driver r8a66597_hc_driver = {
         */
        .hub_status_data =      r8a66597_hub_status_data,
        .hub_control =          r8a66597_hub_control,
+       .bus_suspend =          r8a66597_bus_suspend,
+       .bus_resume =           r8a66597_bus_resume,
 };
 
 #if defined(CONFIG_PM)
-static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
+static int r8a66597_suspend(struct device *dev)
 {
-       pdev->dev.power.power_state = state;
+       struct r8a66597         *r8a66597 = dev_get_drvdata(dev);
+       int port;
+
+       dbg("%s", __func__);
+
+       disable_controller(r8a66597);
+
+       for (port = 0; port < r8a66597->max_root_hub; port++) {
+               struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
+
+               rh->port = 0x00000000;
+       }
+
        return 0;
 }
 
-static int r8a66597_resume(struct platform_device *pdev)
+static int r8a66597_resume(struct device *dev)
 {
-       pdev->dev.power.power_state = PMSG_ON;
+       struct r8a66597         *r8a66597 = dev_get_drvdata(dev);
+       struct usb_hcd          *hcd = r8a66597_to_hcd(r8a66597);
+
+       dbg("%s", __func__);
+
+       enable_controller(r8a66597);
+       usb_root_hub_lost_power(hcd->self.root_hub);
+
        return 0;
 }
+
+static const struct dev_pm_ops r8a66597_dev_pm_ops = {
+       .suspend = r8a66597_suspend,
+       .resume = r8a66597_resume,
+       .poweroff = r8a66597_suspend,
+       .restore = r8a66597_resume,
+};
+
+#define R8A66597_DEV_PM_OPS    (&r8a66597_dev_pm_ops)
 #else  /* if defined(CONFIG_PM) */
-#define r8a66597_suspend       NULL
-#define r8a66597_resume                NULL
+#define R8A66597_DEV_PM_OPS    NULL
 #endif
 
 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
@@ -2126,48 +2404,64 @@ static int __init_or_module r8a66597_remove(struct platform_device *pdev)
        struct usb_hcd          *hcd = r8a66597_to_hcd(r8a66597);
 
        del_timer_sync(&r8a66597->rh_timer);
-       iounmap((void *)r8a66597->reg);
        usb_remove_hcd(hcd);
+       iounmap((void *)r8a66597->reg);
+#ifdef CONFIG_HAVE_CLK
+       if (r8a66597->pdata->on_chip)
+               clk_put(r8a66597->clk);
+#endif
        usb_put_hcd(hcd);
        return 0;
 }
 
-#define resource_len(r) (((r)->end - (r)->start) + 1)
-static int __init r8a66597_probe(struct platform_device *pdev)
+static int __devinit r8a66597_probe(struct platform_device *pdev)
 {
-       struct resource *res = NULL;
+#ifdef CONFIG_HAVE_CLK
+       char clk_name[8];
+#endif
+       struct resource *res = NULL, *ires;
        int irq = -1;
        void __iomem *reg = NULL;
        struct usb_hcd *hcd = NULL;
        struct r8a66597 *r8a66597;
        int ret = 0;
        int i;
+       unsigned long irq_trigger;
 
        if (pdev->dev.dma_mask) {
                ret = -EINVAL;
-               err("dma not support");
+               dev_err(&pdev->dev, "dma not supported\n");
                goto clean_up;
        }
 
-       res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
-                                          (char *)hcd_name);
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!res) {
                ret = -ENODEV;
-               err("platform_get_resource_byname error.");
+               dev_err(&pdev->dev, "platform_get_resource error.\n");
                goto clean_up;
        }
 
-       irq = platform_get_irq(pdev, 0);
-       if (irq < 0) {
+       ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+       if (!ires) {
                ret = -ENODEV;
-               err("platform_get_irq error.");
+               dev_err(&pdev->dev,
+                       "platform_get_resource IORESOURCE_IRQ error.\n");
                goto clean_up;
        }
 
-       reg = ioremap(res->start, resource_len(res));
+       irq = ires->start;
+       irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
+
+       reg = ioremap(res->start, resource_size(res));
        if (reg == NULL) {
                ret = -ENOMEM;
-               err("ioremap error.");
+               dev_err(&pdev->dev, "ioremap error.\n");
+               goto clean_up;
+       }
+
+       if (pdev->dev.platform_data == NULL) {
+               dev_err(&pdev->dev, "no platform data\n");
+               ret = -ENODEV;
                goto clean_up;
        }
 
@@ -2175,12 +2469,29 @@ static int __init r8a66597_probe(struct platform_device *pdev)
        hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
        if (!hcd) {
                ret = -ENOMEM;
-               err("Failed to create hcd");
+               dev_err(&pdev->dev, "Failed to create hcd\n");
                goto clean_up;
        }
        r8a66597 = hcd_to_r8a66597(hcd);
        memset(r8a66597, 0, sizeof(struct r8a66597));
        dev_set_drvdata(&pdev->dev, r8a66597);
+       r8a66597->pdata = pdev->dev.platform_data;
+       r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
+
+       if (r8a66597->pdata->on_chip) {
+#ifdef CONFIG_HAVE_CLK
+               snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
+               r8a66597->clk = clk_get(&pdev->dev, clk_name);
+               if (IS_ERR(r8a66597->clk)) {
+                       dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
+                               clk_name);
+                       ret = PTR_ERR(r8a66597->clk);
+                       goto clean_up2;
+               }
+#endif
+               r8a66597->max_root_hub = 1;
+       } else
+               r8a66597->max_root_hub = 2;
 
        spin_lock_init(&r8a66597->lock);
        init_timer(&r8a66597->rh_timer);
@@ -2188,23 +2499,41 @@ static int __init r8a66597_probe(struct platform_device *pdev)
        r8a66597->rh_timer.data = (unsigned long)r8a66597;
        r8a66597->reg = (unsigned long)reg;
 
+       /* make sure no interrupts are pending */
+       ret = r8a66597_clock_enable(r8a66597);
+       if (ret < 0)
+               goto clean_up3;
+       disable_controller(r8a66597);
+
        for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
                INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
                init_timer(&r8a66597->td_timer[i]);
                r8a66597->td_timer[i].function = r8a66597_td_timer;
                r8a66597->td_timer[i].data = (unsigned long)r8a66597;
+               setup_timer(&r8a66597->interval_timer[i],
+                               r8a66597_interval_timer,
+                               (unsigned long)r8a66597);
        }
        INIT_LIST_HEAD(&r8a66597->child_device);
 
        hcd->rsrc_start = res->start;
-       ret = usb_add_hcd(hcd, irq, 0);
+
+       ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
        if (ret != 0) {
-               err("Failed to add hcd");
-               goto clean_up;
+               dev_err(&pdev->dev, "Failed to add hcd\n");
+               goto clean_up3;
        }
 
        return 0;
 
+clean_up3:
+#ifdef CONFIG_HAVE_CLK
+       if (r8a66597->pdata->on_chip)
+               clk_put(r8a66597->clk);
+clean_up2:
+#endif
+       usb_put_hcd(hcd);
+
 clean_up:
        if (reg)
                iounmap(reg);
@@ -2215,10 +2544,10 @@ clean_up:
 static struct platform_driver r8a66597_driver = {
        .probe =        r8a66597_probe,
        .remove =       r8a66597_remove,
-       .suspend =      r8a66597_suspend,
-       .resume =       r8a66597_resume,
        .driver         = {
                .name = (char *) hcd_name,
+               .owner  = THIS_MODULE,
+               .pm     = R8A66597_DEV_PM_OPS,
        },
 };
 
@@ -2227,7 +2556,8 @@ static int __init r8a66597_init(void)
        if (usb_disabled())
                return -ENODEV;
 
-       info("driver %s, %s", hcd_name, DRIVER_VERSION);
+       printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
+              DRIVER_VERSION);
        return platform_driver_register(&r8a66597_driver);
 }
 module_init(r8a66597_init);