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