isp1362-hcd: use bitmap_find_next_zero_area
[safe/jmp/linux-2.6] / drivers / usb / host / r8a66597-hcd.c
index d5f02dd..b7a661c 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>
@@ -46,31 +45,10 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Yoshihiro Shimoda");
 MODULE_ALIAS("platform:r8a66597_hcd");
 
-#define DRIVER_VERSION "10 Apr 2008"
+#define DRIVER_VERSION "2009-05-26"
 
 static const char hcd_name[] = "r8a66597_hcd";
 
-/* module parameters */
-#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
-static unsigned short clock = XTAL12;
-module_param(clock, ushort, 0644);
-MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
-               "(default=0)");
-#endif
-
-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);
 
@@ -113,39 +91,43 @@ static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
        u16 tmp;
        int i = 0;
 
-#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
-       do {
-               r8a66597_write(r8a66597, SCKE, SYSCFG0);
-               tmp = r8a66597_read(r8a66597, SYSCFG0);
-               if (i++ > 1000) {
-                       err("register access fail.");
-                       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) {
-                       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) {
-                       err("register access fail.");
-                       return -ENXIO;
-               }
-       } while ((tmp & SCKE) != SCKE);
-#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
+               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;
 }
@@ -154,11 +136,16 @@ static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
 {
        r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
        udelay(1);
-#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
-       r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
-       r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
-       r8a66597_bclr(r8a66597, USBE, SYSCFG0);
+
+       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)
@@ -196,6 +183,9 @@ static void r8a66597_disable_port(struct r8a66597 *r8a66597, int 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)
@@ -216,7 +206,7 @@ static int enable_controller(struct r8a66597 *r8a66597)
 
        r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
 
-       for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
+       for (port = 0; port < r8a66597->max_root_hub; port++)
                r8a66597_enable_port(r8a66597, port);
 
        return 0;
@@ -229,7 +219,7 @@ static void disable_controller(struct r8a66597 *r8a66597)
        r8a66597_write(r8a66597, 0, INTENB0);
        r8a66597_write(r8a66597, 0, INTSTS0);
 
-       for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
+       for (port = 0; port < r8a66597->max_root_hub; port++)
                r8a66597_disable_port(r8a66597, port);
 
        r8a66597_clock_disable(r8a66597);
@@ -260,12 +250,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;
@@ -286,7 +277,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;
        }
 
@@ -366,7 +357,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;
 
@@ -385,7 +377,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;
        }
 
@@ -406,8 +398,9 @@ 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;
 }
@@ -430,7 +423,7 @@ static void free_usb_address(struct r8a66597 *r8a66597,
        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;
@@ -447,7 +440,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);
@@ -504,10 +498,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);
 }
 
@@ -515,11 +519,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);
 }
@@ -651,9 +657,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;
@@ -661,7 +667,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;
@@ -669,13 +675,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;
        }
 
@@ -705,7 +711,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;
        }
@@ -724,7 +730,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;
 }
@@ -740,7 +746,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;
 }
@@ -751,27 +757,33 @@ static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
                                     struct r8a66597_pipe *pipe,
                                     struct urb *urb)
 {
-#if !defined(CONFIG_SUPERH_ON_CHIP_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);
@@ -779,7 +791,6 @@ static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
                        break;
                }
        }
-#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
 }
 
 /* this function must be called with interrupt disabled */
@@ -811,8 +822,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;
 
@@ -917,10 +926,9 @@ 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.epnum = usb_endpoint_num(ep);
        info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
-       info.type = get_r8a66597_type(ep->bmAttributes
-                                     & USB_ENDPOINT_XFERTYPE_MASK);
+       info.type = get_r8a66597_type(usb_endpoint_type(ep));
        info.bufnum = get_bufnum(info.pipenum);
        info.buf_bsize = get_buf_bsize(info.pipenum);
        if (info.type == R8A66597_BULK) {
@@ -930,7 +938,7 @@ static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
                info.interval = get_interval(urb, ep->bInterval);
                info.timer_interval = get_timer_interval(urb, ep->bInterval);
        }
-       if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
+       if (usb_endpoint_dir_in(ep))
                info.dir_in = 1;
        else
                info.dir_in = 0;
@@ -964,22 +972,49 @@ static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
        disable_irq_nrdy(r8a66597, pipenum);
 }
 
+static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
+{
+       mod_timer(&r8a66597->rh_timer,
+                       jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
+}
+
+static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
+                                       int connect)
+{
+       struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
+
+       rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
+       rh->scount = R8A66597_MAX_SAMPLING;
+       if (connect)
+               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)
 {
        if (syssts == SE0) {
+               r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
                r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
-               return;
-       }
+       } 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));
 
-       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));
 
-       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));
+       }
+
+       usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
 }
 
 /* this function must be called with interrupt disabled */
@@ -988,6 +1023,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)
@@ -1002,13 +1039,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);
 
-       r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
+       start_root_hub_sampling(r8a66597, port, 0);
 }
 
 /* this function must be called with interrupt disabled */
@@ -1167,7 +1201,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;
        }
 
@@ -1275,7 +1309,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;
        }
@@ -1350,7 +1384,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;
        }
@@ -1364,7 +1398,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);
        }
 
@@ -1551,23 +1585,6 @@ static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
        }
 }
 
-static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
-{
-       mod_timer(&r8a66597->rh_timer,
-                       jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
-}
-
-static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
-{
-       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;
-       r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
-                                        | (1 << USB_PORT_FEAT_C_CONNECTION);
-       r8a66597_root_hub_start_polling(r8a66597);
-}
-
 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
 {
        struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
@@ -1594,13 +1611,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) {
@@ -1609,13 +1631,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);
@@ -1756,11 +1784,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);
 }
@@ -1797,7 +1826,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),
@@ -1994,15 +2023,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;
 }
 
@@ -2072,7 +2099,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);
        }
@@ -2087,11 +2114,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;
 }
 
@@ -2119,14 +2146,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;
@@ -2152,12 +2179,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;
                *(__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;
@@ -2198,6 +2225,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),
@@ -2228,21 +2317,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)
 {
+       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)
 {
+       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)
@@ -2253,14 +2373,20 @@ static int __init_or_module r8a66597_remove(struct platform_device *pdev)
        del_timer_sync(&r8a66597->rh_timer);
        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;
@@ -2271,29 +2397,38 @@ static int __init r8a66597_probe(struct platform_device *pdev)
 
        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;
        }
 
@@ -2301,12 +2436,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);
@@ -2326,18 +2478,23 @@ static int __init r8a66597_probe(struct platform_device *pdev)
        INIT_LIST_HEAD(&r8a66597->child_device);
 
        hcd->rsrc_start = res->start;
-       if (irq_sense == INTL)
-               irq_trigger = IRQF_TRIGGER_LOW;
-       else
-               irq_trigger = IRQF_TRIGGER_FALLING;
+
        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);
@@ -2348,11 +2505,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,
        },
 };
 
@@ -2361,7 +2517,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);