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