1 /* 2 * SL811HS HCD (Host Controller Driver) for USB. 3 * 4 * Copyright (C) 2004 Psion Teklogix (for NetBook PRO) 5 * Copyright (C) 2004-2005 David Brownell 6 * 7 * Periodic scheduling is based on Roman's OHCI code 8 * Copyright (C) 1999 Roman Weissgaerber 9 * 10 * The SL811HS controller handles host side USB (like the SL11H, but with 11 * another register set and SOF generation) as well as peripheral side USB 12 * (like the SL811S). This driver version doesn't implement the Gadget API 13 * for the peripheral role; or OTG (that'd need much external circuitry). 14 * 15 * For documentation, see the SL811HS spec and the "SL811HS Embedded Host" 16 * document (providing significant pieces missing from that spec); plus 17 * the SL811S spec if you want peripheral side info. 18 */ 19 20 /* 21 * Status: Passed basic stress testing, works with hubs, mice, keyboards, 22 * and usb-storage. 23 * 24 * TODO: 25 * - usb suspend/resume triggered by sl811 (with USB_SUSPEND) 26 * - various issues noted in the code 27 * - performance work; use both register banks; ... 28 * - use urb->iso_frame_desc[] with ISO transfers 29 */ 30 31 #undef VERBOSE 32 #undef PACKET_TRACE 33 34 #include <linux/config.h> 35 36 #ifdef CONFIG_USB_DEBUG 37 # define DEBUG 38 #else 39 # undef DEBUG 40 #endif 41 42 #include <linux/module.h> 43 #include <linux/moduleparam.h> 44 #include <linux/kernel.h> 45 #include <linux/delay.h> 46 #include <linux/ioport.h> 47 #include <linux/sched.h> 48 #include <linux/slab.h> 49 #include <linux/smp_lock.h> 50 #include <linux/errno.h> 51 #include <linux/init.h> 52 #include <linux/timer.h> 53 #include <linux/list.h> 54 #include <linux/interrupt.h> 55 #include <linux/usb.h> 56 #include <linux/usb_sl811.h> 57 58 #include <asm/io.h> 59 #include <asm/irq.h> 60 #include <asm/system.h> 61 #include <asm/byteorder.h> 62 63 #include "../core/hcd.h" 64 #include "sl811.h" 65 66 67 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver"); 68 MODULE_LICENSE("GPL"); 69 70 #define DRIVER_VERSION "19 May 2005" 71 72 73 #ifndef DEBUG 74 # define STUB_DEBUG_FILE 75 #endif 76 77 /* for now, use only one transfer register bank */ 78 #undef USE_B 79 80 /* this doesn't understand urb->iso_frame_desc[], but if you had a driver 81 * that just queued one ISO frame per URB then iso transfers "should" work 82 * using the normal urb status fields. 83 */ 84 #define DISABLE_ISO 85 86 // #define QUIRK2 87 #define QUIRK3 88 89 static const char hcd_name[] = "sl811-hcd"; 90 91 /*-------------------------------------------------------------------------*/ 92 93 static void port_power(struct sl811 *sl811, int is_on) 94 { 95 struct usb_hcd *hcd = sl811_to_hcd(sl811); 96 97 /* hub is inactive unless the port is powered */ 98 if (is_on) { 99 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER)) 100 return; 101 102 sl811->port1 = (1 << USB_PORT_FEAT_POWER); 103 sl811->irq_enable = SL11H_INTMASK_INSRMV; 104 hcd->self.controller->power.power_state = PMSG_ON; 105 } else { 106 sl811->port1 = 0; 107 sl811->irq_enable = 0; 108 hcd->state = HC_STATE_HALT; 109 hcd->self.controller->power.power_state = PMSG_SUSPEND; 110 } 111 sl811->ctrl1 = 0; 112 sl811_write(sl811, SL11H_IRQ_ENABLE, 0); 113 sl811_write(sl811, SL11H_IRQ_STATUS, ~0); 114 115 if (sl811->board && sl811->board->port_power) { 116 /* switch VBUS, at 500mA unless hub power budget gets set */ 117 DBG("power %s\n", is_on ? "on" : "off"); 118 sl811->board->port_power(hcd->self.controller, is_on); 119 } 120 121 /* reset as thoroughly as we can */ 122 if (sl811->board && sl811->board->reset) 123 sl811->board->reset(hcd->self.controller); 124 else { 125 sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0); 126 mdelay(20); 127 } 128 129 sl811_write(sl811, SL11H_IRQ_ENABLE, 0); 130 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 131 sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT); 132 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable); 133 134 // if !is_on, put into lowpower mode now 135 } 136 137 /*-------------------------------------------------------------------------*/ 138 139 /* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue, 140 * and may start I/O. Endpoint queues are scanned during completion irq 141 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation. 142 * 143 * Using an external DMA engine to copy a packet at a time could work, 144 * though setup/teardown costs may be too big to make it worthwhile. 145 */ 146 147 /* SETUP starts a new control request. Devices are not allowed to 148 * STALL or NAK these; they must cancel any pending control requests. 149 */ 150 static void setup_packet( 151 struct sl811 *sl811, 152 struct sl811h_ep *ep, 153 struct urb *urb, 154 u8 bank, 155 u8 control 156 ) 157 { 158 u8 addr; 159 u8 len; 160 void __iomem *data_reg; 161 162 addr = SL811HS_PACKET_BUF(bank == 0); 163 len = sizeof(struct usb_ctrlrequest); 164 data_reg = sl811->data_reg; 165 sl811_write_buf(sl811, addr, urb->setup_packet, len); 166 167 /* autoincrementing */ 168 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr); 169 writeb(len, data_reg); 170 writeb(SL_SETUP /* | ep->epnum */, data_reg); 171 writeb(usb_pipedevice(urb->pipe), data_reg); 172 173 /* always OUT/data0 */ ; 174 sl811_write(sl811, bank + SL11H_HOSTCTLREG, 175 control | SL11H_HCTLMASK_OUT); 176 ep->length = 0; 177 PACKET("SETUP qh%p\n", ep); 178 } 179 180 /* STATUS finishes control requests, often after IN or OUT data packets */ 181 static void status_packet( 182 struct sl811 *sl811, 183 struct sl811h_ep *ep, 184 struct urb *urb, 185 u8 bank, 186 u8 control 187 ) 188 { 189 int do_out; 190 void __iomem *data_reg; 191 192 do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe); 193 data_reg = sl811->data_reg; 194 195 /* autoincrementing */ 196 sl811_write(sl811, bank + SL11H_BUFADDRREG, 0); 197 writeb(0, data_reg); 198 writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg); 199 writeb(usb_pipedevice(urb->pipe), data_reg); 200 201 /* always data1; sometimes IN */ 202 control |= SL11H_HCTLMASK_TOGGLE; 203 if (do_out) 204 control |= SL11H_HCTLMASK_OUT; 205 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control); 206 ep->length = 0; 207 PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "", 208 do_out ? "out" : "in", ep); 209 } 210 211 /* IN packets can be used with any type of endpoint. here we just 212 * start the transfer, data from the peripheral may arrive later. 213 * urb->iso_frame_desc is currently ignored here... 214 */ 215 static void in_packet( 216 struct sl811 *sl811, 217 struct sl811h_ep *ep, 218 struct urb *urb, 219 u8 bank, 220 u8 control 221 ) 222 { 223 u8 addr; 224 u8 len; 225 void __iomem *data_reg; 226 227 /* avoid losing data on overflow */ 228 len = ep->maxpacket; 229 addr = SL811HS_PACKET_BUF(bank == 0); 230 if (!(control & SL11H_HCTLMASK_ISOCH) 231 && usb_gettoggle(urb->dev, ep->epnum, 0)) 232 control |= SL11H_HCTLMASK_TOGGLE; 233 data_reg = sl811->data_reg; 234 235 /* autoincrementing */ 236 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr); 237 writeb(len, data_reg); 238 writeb(SL_IN | ep->epnum, data_reg); 239 writeb(usb_pipedevice(urb->pipe), data_reg); 240 241 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control); 242 ep->length = min((int)len, 243 urb->transfer_buffer_length - urb->actual_length); 244 PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "", 245 !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len); 246 } 247 248 /* OUT packets can be used with any type of endpoint. 249 * urb->iso_frame_desc is currently ignored here... 250 */ 251 static void out_packet( 252 struct sl811 *sl811, 253 struct sl811h_ep *ep, 254 struct urb *urb, 255 u8 bank, 256 u8 control 257 ) 258 { 259 void *buf; 260 u8 addr; 261 u8 len; 262 void __iomem *data_reg; 263 264 buf = urb->transfer_buffer + urb->actual_length; 265 prefetch(buf); 266 267 len = min((int)ep->maxpacket, 268 urb->transfer_buffer_length - urb->actual_length); 269 270 if (!(control & SL11H_HCTLMASK_ISOCH) 271 && usb_gettoggle(urb->dev, ep->epnum, 1)) 272 control |= SL11H_HCTLMASK_TOGGLE; 273 addr = SL811HS_PACKET_BUF(bank == 0); 274 data_reg = sl811->data_reg; 275 276 sl811_write_buf(sl811, addr, buf, len); 277 278 /* autoincrementing */ 279 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr); 280 writeb(len, data_reg); 281 writeb(SL_OUT | ep->epnum, data_reg); 282 writeb(usb_pipedevice(urb->pipe), data_reg); 283 284 sl811_write(sl811, bank + SL11H_HOSTCTLREG, 285 control | SL11H_HCTLMASK_OUT); 286 ep->length = len; 287 PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "", 288 !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len); 289 } 290 291 /*-------------------------------------------------------------------------*/ 292 293 /* caller updates on-chip enables later */ 294 295 static inline void sofirq_on(struct sl811 *sl811) 296 { 297 if (sl811->irq_enable & SL11H_INTMASK_SOFINTR) 298 return; 299 VDBG("sof irq on\n"); 300 sl811->irq_enable |= SL11H_INTMASK_SOFINTR; 301 } 302 303 static inline void sofirq_off(struct sl811 *sl811) 304 { 305 if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR)) 306 return; 307 VDBG("sof irq off\n"); 308 sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR; 309 } 310 311 /*-------------------------------------------------------------------------*/ 312 313 /* pick the next endpoint for a transaction, and issue it. 314 * frames start with periodic transfers (after whatever is pending 315 * from the previous frame), and the rest of the time is async 316 * transfers, scheduled round-robin. 317 */ 318 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank) 319 { 320 struct sl811h_ep *ep; 321 struct urb *urb; 322 int fclock; 323 u8 control; 324 325 /* use endpoint at schedule head */ 326 if (sl811->next_periodic) { 327 ep = sl811->next_periodic; 328 sl811->next_periodic = ep->next; 329 } else { 330 if (sl811->next_async) 331 ep = sl811->next_async; 332 else if (!list_empty(&sl811->async)) 333 ep = container_of(sl811->async.next, 334 struct sl811h_ep, schedule); 335 else { 336 /* could set up the first fullspeed periodic 337 * transfer for the next frame ... 338 */ 339 return NULL; 340 } 341 342 #ifdef USE_B 343 if ((bank && sl811->active_b == ep) || sl811->active_a == ep) 344 return NULL; 345 #endif 346 347 if (ep->schedule.next == &sl811->async) 348 sl811->next_async = NULL; 349 else 350 sl811->next_async = container_of(ep->schedule.next, 351 struct sl811h_ep, schedule); 352 } 353 354 if (unlikely(list_empty(&ep->hep->urb_list))) { 355 DBG("empty %p queue?\n", ep); 356 return NULL; 357 } 358 359 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list); 360 control = ep->defctrl; 361 362 /* if this frame doesn't have enough time left to transfer this 363 * packet, wait till the next frame. too-simple algorithm... 364 */ 365 fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6; 366 fclock -= 100; /* setup takes not much time */ 367 if (urb->dev->speed == USB_SPEED_LOW) { 368 if (control & SL11H_HCTLMASK_PREAMBLE) { 369 /* also note erratum 1: some hubs won't work */ 370 fclock -= 800; 371 } 372 fclock -= ep->maxpacket << 8; 373 374 /* erratum 2: AFTERSOF only works for fullspeed */ 375 if (fclock < 0) { 376 if (ep->period) 377 sl811->stat_overrun++; 378 sofirq_on(sl811); 379 return NULL; 380 } 381 } else { 382 fclock -= 12000 / 19; /* 19 64byte packets/msec */ 383 if (fclock < 0) { 384 if (ep->period) 385 sl811->stat_overrun++; 386 control |= SL11H_HCTLMASK_AFTERSOF; 387 388 /* throttle bulk/control irq noise */ 389 } else if (ep->nak_count) 390 control |= SL11H_HCTLMASK_AFTERSOF; 391 } 392 393 394 switch (ep->nextpid) { 395 case USB_PID_IN: 396 in_packet(sl811, ep, urb, bank, control); 397 break; 398 case USB_PID_OUT: 399 out_packet(sl811, ep, urb, bank, control); 400 break; 401 case USB_PID_SETUP: 402 setup_packet(sl811, ep, urb, bank, control); 403 break; 404 case USB_PID_ACK: /* for control status */ 405 status_packet(sl811, ep, urb, bank, control); 406 break; 407 default: 408 DBG("bad ep%p pid %02x\n", ep, ep->nextpid); 409 ep = NULL; 410 } 411 return ep; 412 } 413 414 #define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2) 415 416 static inline void start_transfer(struct sl811 *sl811) 417 { 418 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) 419 return; 420 if (sl811->active_a == NULL) { 421 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF)); 422 if (sl811->active_a != NULL) 423 sl811->jiffies_a = jiffies + MIN_JIFFIES; 424 } 425 #ifdef USE_B 426 if (sl811->active_b == NULL) { 427 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF)); 428 if (sl811->active_b != NULL) 429 sl811->jiffies_b = jiffies + MIN_JIFFIES; 430 } 431 #endif 432 } 433 434 static void finish_request( 435 struct sl811 *sl811, 436 struct sl811h_ep *ep, 437 struct urb *urb, 438 struct pt_regs *regs, 439 int status 440 ) __releases(sl811->lock) __acquires(sl811->lock) 441 { 442 unsigned i; 443 444 if (usb_pipecontrol(urb->pipe)) 445 ep->nextpid = USB_PID_SETUP; 446 447 spin_lock(&urb->lock); 448 if (urb->status == -EINPROGRESS) 449 urb->status = status; 450 urb->hcpriv = NULL; 451 spin_unlock(&urb->lock); 452 453 spin_unlock(&sl811->lock); 454 usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, regs); 455 spin_lock(&sl811->lock); 456 457 /* leave active endpoints in the schedule */ 458 if (!list_empty(&ep->hep->urb_list)) 459 return; 460 461 /* async deschedule? */ 462 if (!list_empty(&ep->schedule)) { 463 list_del_init(&ep->schedule); 464 if (ep == sl811->next_async) 465 sl811->next_async = NULL; 466 return; 467 } 468 469 /* periodic deschedule */ 470 DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch); 471 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) { 472 struct sl811h_ep *temp; 473 struct sl811h_ep **prev = &sl811->periodic[i]; 474 475 while (*prev && ((temp = *prev) != ep)) 476 prev = &temp->next; 477 if (*prev) 478 *prev = ep->next; 479 sl811->load[i] -= ep->load; 480 } 481 ep->branch = PERIODIC_SIZE; 482 sl811->periodic_count--; 483 sl811_to_hcd(sl811)->self.bandwidth_allocated 484 -= ep->load / ep->period; 485 if (ep == sl811->next_periodic) 486 sl811->next_periodic = ep->next; 487 488 /* we might turn SOFs back on again for the async schedule */ 489 if (sl811->periodic_count == 0) 490 sofirq_off(sl811); 491 } 492 493 static void 494 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank, struct pt_regs *regs) 495 { 496 u8 status; 497 struct urb *urb; 498 int urbstat = -EINPROGRESS; 499 500 if (unlikely(!ep)) 501 return; 502 503 status = sl811_read(sl811, bank + SL11H_PKTSTATREG); 504 505 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list); 506 507 /* we can safely ignore NAKs */ 508 if (status & SL11H_STATMASK_NAK) { 509 // PACKET("...NAK_%02x qh%p\n", bank, ep); 510 if (!ep->period) 511 ep->nak_count++; 512 ep->error_count = 0; 513 514 /* ACK advances transfer, toggle, and maybe queue */ 515 } else if (status & SL11H_STATMASK_ACK) { 516 struct usb_device *udev = urb->dev; 517 int len; 518 unsigned char *buf; 519 520 /* urb->iso_frame_desc is currently ignored here... */ 521 522 ep->nak_count = ep->error_count = 0; 523 switch (ep->nextpid) { 524 case USB_PID_OUT: 525 // PACKET("...ACK/out_%02x qh%p\n", bank, ep); 526 urb->actual_length += ep->length; 527 usb_dotoggle(udev, ep->epnum, 1); 528 if (urb->actual_length 529 == urb->transfer_buffer_length) { 530 if (usb_pipecontrol(urb->pipe)) 531 ep->nextpid = USB_PID_ACK; 532 533 /* some bulk protocols terminate OUT transfers 534 * by a short packet, using ZLPs not padding. 535 */ 536 else if (ep->length < ep->maxpacket 537 || !(urb->transfer_flags 538 & URB_ZERO_PACKET)) 539 urbstat = 0; 540 } 541 break; 542 case USB_PID_IN: 543 // PACKET("...ACK/in_%02x qh%p\n", bank, ep); 544 buf = urb->transfer_buffer + urb->actual_length; 545 prefetchw(buf); 546 len = ep->maxpacket - sl811_read(sl811, 547 bank + SL11H_XFERCNTREG); 548 if (len > ep->length) { 549 len = ep->length; 550 urb->status = -EOVERFLOW; 551 } 552 urb->actual_length += len; 553 sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0), 554 buf, len); 555 usb_dotoggle(udev, ep->epnum, 0); 556 if (urb->actual_length == urb->transfer_buffer_length) 557 urbstat = 0; 558 else if (len < ep->maxpacket) { 559 if (urb->transfer_flags & URB_SHORT_NOT_OK) 560 urbstat = -EREMOTEIO; 561 else 562 urbstat = 0; 563 } 564 if (usb_pipecontrol(urb->pipe) 565 && (urbstat == -EREMOTEIO 566 || urbstat == 0)) { 567 568 /* NOTE if the status stage STALLs (why?), 569 * this reports the wrong urb status. 570 */ 571 spin_lock(&urb->lock); 572 if (urb->status == -EINPROGRESS) 573 urb->status = urbstat; 574 spin_unlock(&urb->lock); 575 576 urb = NULL; 577 ep->nextpid = USB_PID_ACK; 578 } 579 break; 580 case USB_PID_SETUP: 581 // PACKET("...ACK/setup_%02x qh%p\n", bank, ep); 582 if (urb->transfer_buffer_length == urb->actual_length) 583 ep->nextpid = USB_PID_ACK; 584 else if (usb_pipeout(urb->pipe)) { 585 usb_settoggle(udev, 0, 1, 1); 586 ep->nextpid = USB_PID_OUT; 587 } else { 588 usb_settoggle(udev, 0, 0, 1); 589 ep->nextpid = USB_PID_IN; 590 } 591 break; 592 case USB_PID_ACK: 593 // PACKET("...ACK/status_%02x qh%p\n", bank, ep); 594 urbstat = 0; 595 break; 596 } 597 598 /* STALL stops all transfers */ 599 } else if (status & SL11H_STATMASK_STALL) { 600 PACKET("...STALL_%02x qh%p\n", bank, ep); 601 ep->nak_count = ep->error_count = 0; 602 urbstat = -EPIPE; 603 604 /* error? retry, until "3 strikes" */ 605 } else if (++ep->error_count >= 3) { 606 if (status & SL11H_STATMASK_TMOUT) 607 urbstat = -ETIMEDOUT; 608 else if (status & SL11H_STATMASK_OVF) 609 urbstat = -EOVERFLOW; 610 else 611 urbstat = -EPROTO; 612 ep->error_count = 0; 613 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n", 614 bank, status, ep, urbstat); 615 } 616 617 if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS)) 618 finish_request(sl811, ep, urb, regs, urbstat); 619 } 620 621 static inline u8 checkdone(struct sl811 *sl811) 622 { 623 u8 ctl; 624 u8 irqstat = 0; 625 626 if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) { 627 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)); 628 if (ctl & SL11H_HCTLMASK_ARM) 629 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0); 630 DBG("%s DONE_A: ctrl %02x sts %02x\n", 631 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost", 632 ctl, 633 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG))); 634 irqstat |= SL11H_INTMASK_DONE_A; 635 } 636 #ifdef USE_B 637 if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) { 638 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)); 639 if (ctl & SL11H_HCTLMASK_ARM) 640 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0); 641 DBG("%s DONE_B: ctrl %02x sts %02x\n", 642 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost", 643 ctl, 644 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG))); 645 irqstat |= SL11H_INTMASK_DONE_A; 646 } 647 #endif 648 return irqstat; 649 } 650 651 static irqreturn_t sl811h_irq(struct usb_hcd *hcd, struct pt_regs *regs) 652 { 653 struct sl811 *sl811 = hcd_to_sl811(hcd); 654 u8 irqstat; 655 irqreturn_t ret = IRQ_NONE; 656 unsigned retries = 5; 657 658 spin_lock(&sl811->lock); 659 660 retry: 661 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP; 662 if (irqstat) { 663 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat); 664 irqstat &= sl811->irq_enable; 665 } 666 667 #ifdef QUIRK2 668 /* this may no longer be necessary ... */ 669 if (irqstat == 0) { 670 irqstat = checkdone(sl811); 671 if (irqstat) 672 sl811->stat_lost++; 673 } 674 #endif 675 676 /* USB packets, not necessarily handled in the order they're 677 * issued ... that's fine if they're different endpoints. 678 */ 679 if (irqstat & SL11H_INTMASK_DONE_A) { 680 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF), regs); 681 sl811->active_a = NULL; 682 sl811->stat_a++; 683 } 684 #ifdef USE_B 685 if (irqstat & SL11H_INTMASK_DONE_B) { 686 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF), regs); 687 sl811->active_b = NULL; 688 sl811->stat_b++; 689 } 690 #endif 691 if (irqstat & SL11H_INTMASK_SOFINTR) { 692 unsigned index; 693 694 index = sl811->frame++ % (PERIODIC_SIZE - 1); 695 sl811->stat_sof++; 696 697 /* be graceful about almost-inevitable periodic schedule 698 * overruns: continue the previous frame's transfers iff 699 * this one has nothing scheduled. 700 */ 701 if (sl811->next_periodic) { 702 // ERR("overrun to slot %d\n", index); 703 sl811->stat_overrun++; 704 } 705 if (sl811->periodic[index]) 706 sl811->next_periodic = sl811->periodic[index]; 707 } 708 709 /* khubd manages debouncing and wakeup */ 710 if (irqstat & SL11H_INTMASK_INSRMV) { 711 sl811->stat_insrmv++; 712 713 /* most stats are reset for each VBUS session */ 714 sl811->stat_wake = 0; 715 sl811->stat_sof = 0; 716 sl811->stat_a = 0; 717 sl811->stat_b = 0; 718 sl811->stat_lost = 0; 719 720 sl811->ctrl1 = 0; 721 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 722 723 sl811->irq_enable = SL11H_INTMASK_INSRMV; 724 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable); 725 726 /* usbcore nukes other pending transactions on disconnect */ 727 if (sl811->active_a) { 728 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0); 729 finish_request(sl811, sl811->active_a, 730 container_of(sl811->active_a 731 ->hep->urb_list.next, 732 struct urb, urb_list), 733 NULL, -ESHUTDOWN); 734 sl811->active_a = NULL; 735 } 736 #ifdef USE_B 737 if (sl811->active_b) { 738 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0); 739 finish_request(sl811, sl811->active_b, 740 container_of(sl811->active_b 741 ->hep->urb_list.next, 742 struct urb, urb_list), 743 NULL, -ESHUTDOWN); 744 sl811->active_b = NULL; 745 } 746 #endif 747 748 /* port status seems weird until after reset, so 749 * force the reset and make khubd clean up later. 750 */ 751 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION) 752 | (1 << USB_PORT_FEAT_CONNECTION); 753 754 } else if (irqstat & SL11H_INTMASK_RD) { 755 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) { 756 DBG("wakeup\n"); 757 sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND; 758 sl811->stat_wake++; 759 } else 760 irqstat &= ~SL11H_INTMASK_RD; 761 } 762 763 if (irqstat) { 764 if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)) 765 start_transfer(sl811); 766 ret = IRQ_HANDLED; 767 if (retries--) 768 goto retry; 769 } 770 771 if (sl811->periodic_count == 0 && list_empty(&sl811->async)) 772 sofirq_off(sl811); 773 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable); 774 775 spin_unlock(&sl811->lock); 776 777 return ret; 778 } 779 780 /*-------------------------------------------------------------------------*/ 781 782 /* usb 1.1 says max 90% of a frame is available for periodic transfers. 783 * this driver doesn't promise that much since it's got to handle an 784 * IRQ per packet; irq handling latencies also use up that time. 785 * 786 * NOTE: the periodic schedule is a sparse tree, with the load for 787 * each branch minimized. see fig 3.5 in the OHCI spec for example. 788 */ 789 #define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */ 790 791 static int balance(struct sl811 *sl811, u16 period, u16 load) 792 { 793 int i, branch = -ENOSPC; 794 795 /* search for the least loaded schedule branch of that period 796 * which has enough bandwidth left unreserved. 797 */ 798 for (i = 0; i < period ; i++) { 799 if (branch < 0 || sl811->load[branch] > sl811->load[i]) { 800 int j; 801 802 for (j = i; j < PERIODIC_SIZE; j += period) { 803 if ((sl811->load[j] + load) 804 > MAX_PERIODIC_LOAD) 805 break; 806 } 807 if (j < PERIODIC_SIZE) 808 continue; 809 branch = i; 810 } 811 } 812 return branch; 813 } 814 815 /*-------------------------------------------------------------------------*/ 816 817 static int sl811h_urb_enqueue( 818 struct usb_hcd *hcd, 819 struct usb_host_endpoint *hep, 820 struct urb *urb, 821 unsigned mem_flags 822 ) { 823 struct sl811 *sl811 = hcd_to_sl811(hcd); 824 struct usb_device *udev = urb->dev; 825 unsigned int pipe = urb->pipe; 826 int is_out = !usb_pipein(pipe); 827 int type = usb_pipetype(pipe); 828 int epnum = usb_pipeendpoint(pipe); 829 struct sl811h_ep *ep = NULL; 830 unsigned long flags; 831 int i; 832 int retval = 0; 833 834 #ifdef DISABLE_ISO 835 if (type == PIPE_ISOCHRONOUS) 836 return -ENOSPC; 837 #endif 838 839 /* avoid all allocations within spinlocks */ 840 if (!hep->hcpriv) 841 ep = kzalloc(sizeof *ep, mem_flags); 842 843 spin_lock_irqsave(&sl811->lock, flags); 844 845 /* don't submit to a dead or disabled port */ 846 if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)) 847 || !HC_IS_RUNNING(hcd->state)) { 848 retval = -ENODEV; 849 kfree(ep); 850 goto fail; 851 } 852 853 if (hep->hcpriv) { 854 kfree(ep); 855 ep = hep->hcpriv; 856 } else if (!ep) { 857 retval = -ENOMEM; 858 goto fail; 859 860 } else { 861 INIT_LIST_HEAD(&ep->schedule); 862 ep->udev = usb_get_dev(udev); 863 ep->epnum = epnum; 864 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out); 865 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE; 866 usb_settoggle(udev, epnum, is_out, 0); 867 868 if (type == PIPE_CONTROL) 869 ep->nextpid = USB_PID_SETUP; 870 else if (is_out) 871 ep->nextpid = USB_PID_OUT; 872 else 873 ep->nextpid = USB_PID_IN; 874 875 if (ep->maxpacket > H_MAXPACKET) { 876 /* iso packets up to 240 bytes could work... */ 877 DBG("dev %d ep%d maxpacket %d\n", 878 udev->devnum, epnum, ep->maxpacket); 879 retval = -EINVAL; 880 goto fail; 881 } 882 883 if (udev->speed == USB_SPEED_LOW) { 884 /* send preamble for external hub? */ 885 if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD)) 886 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE; 887 } 888 switch (type) { 889 case PIPE_ISOCHRONOUS: 890 case PIPE_INTERRUPT: 891 if (urb->interval > PERIODIC_SIZE) 892 urb->interval = PERIODIC_SIZE; 893 ep->period = urb->interval; 894 ep->branch = PERIODIC_SIZE; 895 if (type == PIPE_ISOCHRONOUS) 896 ep->defctrl |= SL11H_HCTLMASK_ISOCH; 897 ep->load = usb_calc_bus_time(udev->speed, !is_out, 898 (type == PIPE_ISOCHRONOUS), 899 usb_maxpacket(udev, pipe, is_out)) 900 / 1000; 901 break; 902 } 903 904 ep->hep = hep; 905 hep->hcpriv = ep; 906 } 907 908 /* maybe put endpoint into schedule */ 909 switch (type) { 910 case PIPE_CONTROL: 911 case PIPE_BULK: 912 if (list_empty(&ep->schedule)) 913 list_add_tail(&ep->schedule, &sl811->async); 914 break; 915 case PIPE_ISOCHRONOUS: 916 case PIPE_INTERRUPT: 917 urb->interval = ep->period; 918 if (ep->branch < PERIODIC_SIZE) { 919 /* NOTE: the phase is correct here, but the value 920 * needs offsetting by the transfer queue depth. 921 * All current drivers ignore start_frame, so this 922 * is unlikely to ever matter... 923 */ 924 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1)) 925 + ep->branch; 926 break; 927 } 928 929 retval = balance(sl811, ep->period, ep->load); 930 if (retval < 0) 931 goto fail; 932 ep->branch = retval; 933 retval = 0; 934 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1)) 935 + ep->branch; 936 937 /* sort each schedule branch by period (slow before fast) 938 * to share the faster parts of the tree without needing 939 * dummy/placeholder nodes 940 */ 941 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch); 942 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) { 943 struct sl811h_ep **prev = &sl811->periodic[i]; 944 struct sl811h_ep *here = *prev; 945 946 while (here && ep != here) { 947 if (ep->period > here->period) 948 break; 949 prev = &here->next; 950 here = *prev; 951 } 952 if (ep != here) { 953 ep->next = here; 954 *prev = ep; 955 } 956 sl811->load[i] += ep->load; 957 } 958 sl811->periodic_count++; 959 hcd->self.bandwidth_allocated += ep->load / ep->period; 960 sofirq_on(sl811); 961 } 962 963 /* in case of unlink-during-submit */ 964 spin_lock(&urb->lock); 965 if (urb->status != -EINPROGRESS) { 966 spin_unlock(&urb->lock); 967 finish_request(sl811, ep, urb, NULL, 0); 968 retval = 0; 969 goto fail; 970 } 971 urb->hcpriv = hep; 972 spin_unlock(&urb->lock); 973 974 start_transfer(sl811); 975 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable); 976 fail: 977 spin_unlock_irqrestore(&sl811->lock, flags); 978 return retval; 979 } 980 981 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 982 { 983 struct sl811 *sl811 = hcd_to_sl811(hcd); 984 struct usb_host_endpoint *hep; 985 unsigned long flags; 986 struct sl811h_ep *ep; 987 int retval = 0; 988 989 spin_lock_irqsave(&sl811->lock, flags); 990 hep = urb->hcpriv; 991 if (!hep) 992 goto fail; 993 994 ep = hep->hcpriv; 995 if (ep) { 996 /* finish right away if this urb can't be active ... 997 * note that some drivers wrongly expect delays 998 */ 999 if (ep->hep->urb_list.next != &urb->urb_list) { 1000 /* not front of queue? never active */ 1001 1002 /* for active transfers, we expect an IRQ */ 1003 } else if (sl811->active_a == ep) { 1004 if (time_before_eq(sl811->jiffies_a, jiffies)) { 1005 /* happens a lot with lowspeed?? */ 1006 DBG("giveup on DONE_A: ctrl %02x sts %02x\n", 1007 sl811_read(sl811, 1008 SL811_EP_A(SL11H_HOSTCTLREG)), 1009 sl811_read(sl811, 1010 SL811_EP_A(SL11H_PKTSTATREG))); 1011 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 1012 0); 1013 sl811->active_a = NULL; 1014 } else 1015 urb = NULL; 1016 #ifdef USE_B 1017 } else if (sl811->active_b == ep) { 1018 if (time_before_eq(sl811->jiffies_a, jiffies)) { 1019 /* happens a lot with lowspeed?? */ 1020 DBG("giveup on DONE_B: ctrl %02x sts %02x\n", 1021 sl811_read(sl811, 1022 SL811_EP_B(SL11H_HOSTCTLREG)), 1023 sl811_read(sl811, 1024 SL811_EP_B(SL11H_PKTSTATREG))); 1025 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 1026 0); 1027 sl811->active_b = NULL; 1028 } else 1029 urb = NULL; 1030 #endif 1031 } else { 1032 /* front of queue for inactive endpoint */ 1033 } 1034 1035 if (urb) 1036 finish_request(sl811, ep, urb, NULL, 0); 1037 else 1038 VDBG("dequeue, urb %p active %s; wait4irq\n", urb, 1039 (sl811->active_a == ep) ? "A" : "B"); 1040 } else 1041 fail: 1042 retval = -EINVAL; 1043 spin_unlock_irqrestore(&sl811->lock, flags); 1044 return retval; 1045 } 1046 1047 static void 1048 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep) 1049 { 1050 struct sl811h_ep *ep = hep->hcpriv; 1051 1052 if (!ep) 1053 return; 1054 1055 /* assume we'd just wait for the irq */ 1056 if (!list_empty(&hep->urb_list)) 1057 msleep(3); 1058 if (!list_empty(&hep->urb_list)) 1059 WARN("ep %p not empty?\n", ep); 1060 1061 usb_put_dev(ep->udev); 1062 kfree(ep); 1063 hep->hcpriv = NULL; 1064 } 1065 1066 static int 1067 sl811h_get_frame(struct usb_hcd *hcd) 1068 { 1069 struct sl811 *sl811 = hcd_to_sl811(hcd); 1070 1071 /* wrong except while periodic transfers are scheduled; 1072 * never matches the on-the-wire frame; 1073 * subject to overruns. 1074 */ 1075 return sl811->frame; 1076 } 1077 1078 1079 /*-------------------------------------------------------------------------*/ 1080 1081 /* the virtual root hub timer IRQ checks for hub status */ 1082 static int 1083 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf) 1084 { 1085 struct sl811 *sl811 = hcd_to_sl811(hcd); 1086 #ifdef QUIRK3 1087 unsigned long flags; 1088 1089 /* non-SMP HACK: use root hub timer as i/o watchdog 1090 * this seems essential when SOF IRQs aren't in use... 1091 */ 1092 local_irq_save(flags); 1093 if (!timer_pending(&sl811->timer)) { 1094 if (sl811h_irq( /* ~0, */ hcd, NULL) != IRQ_NONE) 1095 sl811->stat_lost++; 1096 } 1097 local_irq_restore(flags); 1098 #endif 1099 1100 if (!(sl811->port1 & (0xffff << 16))) 1101 return 0; 1102 1103 /* tell khubd port 1 changed */ 1104 *buf = (1 << 1); 1105 return 1; 1106 } 1107 1108 static void 1109 sl811h_hub_descriptor ( 1110 struct sl811 *sl811, 1111 struct usb_hub_descriptor *desc 1112 ) { 1113 u16 temp = 0; 1114 1115 desc->bDescriptorType = 0x29; 1116 desc->bHubContrCurrent = 0; 1117 1118 desc->bNbrPorts = 1; 1119 desc->bDescLength = 9; 1120 1121 /* per-port power switching (gang of one!), or none */ 1122 desc->bPwrOn2PwrGood = 0; 1123 if (sl811->board && sl811->board->port_power) { 1124 desc->bPwrOn2PwrGood = sl811->board->potpg; 1125 if (!desc->bPwrOn2PwrGood) 1126 desc->bPwrOn2PwrGood = 10; 1127 temp = 0x0001; 1128 } else 1129 temp = 0x0002; 1130 1131 /* no overcurrent errors detection/handling */ 1132 temp |= 0x0010; 1133 1134 desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp); 1135 1136 /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */ 1137 desc->bitmap[0] = 0 << 1; 1138 desc->bitmap[1] = ~0; 1139 } 1140 1141 static void 1142 sl811h_timer(unsigned long _sl811) 1143 { 1144 struct sl811 *sl811 = (void *) _sl811; 1145 unsigned long flags; 1146 u8 irqstat; 1147 u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE; 1148 const u32 mask = (1 << USB_PORT_FEAT_CONNECTION) 1149 | (1 << USB_PORT_FEAT_ENABLE) 1150 | (1 << USB_PORT_FEAT_LOWSPEED); 1151 1152 spin_lock_irqsave(&sl811->lock, flags); 1153 1154 /* stop special signaling */ 1155 sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE; 1156 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1157 udelay(3); 1158 1159 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS); 1160 1161 switch (signaling) { 1162 case SL11H_CTL1MASK_SE0: 1163 DBG("end reset\n"); 1164 sl811->port1 = (1 << USB_PORT_FEAT_C_RESET) 1165 | (1 << USB_PORT_FEAT_POWER); 1166 sl811->ctrl1 = 0; 1167 /* don't wrongly ack RD */ 1168 if (irqstat & SL11H_INTMASK_INSRMV) 1169 irqstat &= ~SL11H_INTMASK_RD; 1170 break; 1171 case SL11H_CTL1MASK_K: 1172 DBG("end resume\n"); 1173 sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND); 1174 break; 1175 default: 1176 DBG("odd timer signaling: %02x\n", signaling); 1177 break; 1178 } 1179 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat); 1180 1181 if (irqstat & SL11H_INTMASK_RD) { 1182 /* usbcore nukes all pending transactions on disconnect */ 1183 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) 1184 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION) 1185 | (1 << USB_PORT_FEAT_C_ENABLE); 1186 sl811->port1 &= ~mask; 1187 sl811->irq_enable = SL11H_INTMASK_INSRMV; 1188 } else { 1189 sl811->port1 |= mask; 1190 if (irqstat & SL11H_INTMASK_DP) 1191 sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED); 1192 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD; 1193 } 1194 1195 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) { 1196 u8 ctrl2 = SL811HS_CTL2_INIT; 1197 1198 sl811->irq_enable |= SL11H_INTMASK_DONE_A; 1199 #ifdef USE_B 1200 sl811->irq_enable |= SL11H_INTMASK_DONE_B; 1201 #endif 1202 if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) { 1203 sl811->ctrl1 |= SL11H_CTL1MASK_LSPD; 1204 ctrl2 |= SL811HS_CTL2MASK_DSWAP; 1205 } 1206 1207 /* start SOFs flowing, kickstarting with A registers */ 1208 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA; 1209 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0); 1210 sl811_write(sl811, SL811HS_CTLREG2, ctrl2); 1211 1212 /* autoincrementing */ 1213 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0); 1214 writeb(SL_SOF, sl811->data_reg); 1215 writeb(0, sl811->data_reg); 1216 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 1217 SL11H_HCTLMASK_ARM); 1218 1219 /* khubd provides debounce delay */ 1220 } else { 1221 sl811->ctrl1 = 0; 1222 } 1223 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1224 1225 /* reenable irqs */ 1226 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable); 1227 spin_unlock_irqrestore(&sl811->lock, flags); 1228 } 1229 1230 static int 1231 sl811h_hub_control( 1232 struct usb_hcd *hcd, 1233 u16 typeReq, 1234 u16 wValue, 1235 u16 wIndex, 1236 char *buf, 1237 u16 wLength 1238 ) { 1239 struct sl811 *sl811 = hcd_to_sl811(hcd); 1240 int retval = 0; 1241 unsigned long flags; 1242 1243 spin_lock_irqsave(&sl811->lock, flags); 1244 1245 switch (typeReq) { 1246 case ClearHubFeature: 1247 case SetHubFeature: 1248 switch (wValue) { 1249 case C_HUB_OVER_CURRENT: 1250 case C_HUB_LOCAL_POWER: 1251 break; 1252 default: 1253 goto error; 1254 } 1255 break; 1256 case ClearPortFeature: 1257 if (wIndex != 1 || wLength != 0) 1258 goto error; 1259 1260 switch (wValue) { 1261 case USB_PORT_FEAT_ENABLE: 1262 sl811->port1 &= (1 << USB_PORT_FEAT_POWER); 1263 sl811->ctrl1 = 0; 1264 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1265 sl811->irq_enable = SL11H_INTMASK_INSRMV; 1266 sl811_write(sl811, SL11H_IRQ_ENABLE, 1267 sl811->irq_enable); 1268 break; 1269 case USB_PORT_FEAT_SUSPEND: 1270 if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))) 1271 break; 1272 1273 /* 20 msec of resume/K signaling, other irqs blocked */ 1274 DBG("start resume...\n"); 1275 sl811->irq_enable = 0; 1276 sl811_write(sl811, SL11H_IRQ_ENABLE, 1277 sl811->irq_enable); 1278 sl811->ctrl1 |= SL11H_CTL1MASK_K; 1279 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1280 1281 mod_timer(&sl811->timer, jiffies 1282 + msecs_to_jiffies(20)); 1283 break; 1284 case USB_PORT_FEAT_POWER: 1285 port_power(sl811, 0); 1286 break; 1287 case USB_PORT_FEAT_C_ENABLE: 1288 case USB_PORT_FEAT_C_SUSPEND: 1289 case USB_PORT_FEAT_C_CONNECTION: 1290 case USB_PORT_FEAT_C_OVER_CURRENT: 1291 case USB_PORT_FEAT_C_RESET: 1292 break; 1293 default: 1294 goto error; 1295 } 1296 sl811->port1 &= ~(1 << wValue); 1297 break; 1298 case GetHubDescriptor: 1299 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf); 1300 break; 1301 case GetHubStatus: 1302 *(__le32 *) buf = cpu_to_le32(0); 1303 break; 1304 case GetPortStatus: 1305 if (wIndex != 1) 1306 goto error; 1307 *(__le32 *) buf = cpu_to_le32(sl811->port1); 1308 1309 #ifndef VERBOSE 1310 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */ 1311 #endif 1312 DBG("GetPortStatus %08x\n", sl811->port1); 1313 break; 1314 case SetPortFeature: 1315 if (wIndex != 1 || wLength != 0) 1316 goto error; 1317 switch (wValue) { 1318 case USB_PORT_FEAT_SUSPEND: 1319 if (sl811->port1 & (1 << USB_PORT_FEAT_RESET)) 1320 goto error; 1321 if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))) 1322 goto error; 1323 1324 DBG("suspend...\n"); 1325 sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA; 1326 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1327 break; 1328 case USB_PORT_FEAT_POWER: 1329 port_power(sl811, 1); 1330 break; 1331 case USB_PORT_FEAT_RESET: 1332 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) 1333 goto error; 1334 if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER))) 1335 break; 1336 1337 /* 50 msec of reset/SE0 signaling, irqs blocked */ 1338 sl811->irq_enable = 0; 1339 sl811_write(sl811, SL11H_IRQ_ENABLE, 1340 sl811->irq_enable); 1341 sl811->ctrl1 = SL11H_CTL1MASK_SE0; 1342 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1); 1343 sl811->port1 |= (1 << USB_PORT_FEAT_RESET); 1344 mod_timer(&sl811->timer, jiffies 1345 + msecs_to_jiffies(50)); 1346 break; 1347 default: 1348 goto error; 1349 } 1350 sl811->port1 |= 1 << wValue; 1351 break; 1352 1353 default: 1354 error: 1355 /* "protocol stall" on error */ 1356 retval = -EPIPE; 1357 } 1358 1359 spin_unlock_irqrestore(&sl811->lock, flags); 1360 return retval; 1361 } 1362 1363 #ifdef CONFIG_PM 1364 1365 static int 1366 sl811h_hub_suspend(struct usb_hcd *hcd) 1367 { 1368 // SOFs off 1369 DBG("%s\n", __FUNCTION__); 1370 return 0; 1371 } 1372 1373 static int 1374 sl811h_hub_resume(struct usb_hcd *hcd) 1375 { 1376 // SOFs on 1377 DBG("%s\n", __FUNCTION__); 1378 return 0; 1379 } 1380 1381 #else 1382 1383 #define sl811h_hub_suspend NULL 1384 #define sl811h_hub_resume NULL 1385 1386 #endif 1387 1388 1389 /*-------------------------------------------------------------------------*/ 1390 1391 #ifdef STUB_DEBUG_FILE 1392 1393 static inline void create_debug_file(struct sl811 *sl811) { } 1394 static inline void remove_debug_file(struct sl811 *sl811) { } 1395 1396 #else 1397 1398 #include <linux/proc_fs.h> 1399 #include <linux/seq_file.h> 1400 1401 static void dump_irq(struct seq_file *s, char *label, u8 mask) 1402 { 1403 seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask, 1404 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "", 1405 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "", 1406 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "", 1407 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "", 1408 (mask & SL11H_INTMASK_RD) ? " rd" : "", 1409 (mask & SL11H_INTMASK_DP) ? " dp" : ""); 1410 } 1411 1412 static int proc_sl811h_show(struct seq_file *s, void *unused) 1413 { 1414 struct sl811 *sl811 = s->private; 1415 struct sl811h_ep *ep; 1416 unsigned i; 1417 1418 seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n", 1419 sl811_to_hcd(sl811)->product_desc, 1420 hcd_name, DRIVER_VERSION, 1421 sl811->port1); 1422 1423 seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv); 1424 seq_printf(s, "current session: done_a %ld done_b %ld " 1425 "wake %ld sof %ld overrun %ld lost %ld\n\n", 1426 sl811->stat_a, sl811->stat_b, 1427 sl811->stat_wake, sl811->stat_sof, 1428 sl811->stat_overrun, sl811->stat_lost); 1429 1430 spin_lock_irq(&sl811->lock); 1431 1432 if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND) 1433 seq_printf(s, "(suspended)\n\n"); 1434 else { 1435 u8 t = sl811_read(sl811, SL11H_CTLREG1); 1436 1437 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t, 1438 (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "", 1439 ({char *s; switch (t & SL11H_CTL1MASK_FORCE) { 1440 case SL11H_CTL1MASK_NORMAL: s = ""; break; 1441 case SL11H_CTL1MASK_SE0: s = " se0/reset"; break; 1442 case SL11H_CTL1MASK_K: s = " k/resume"; break; 1443 default: s = "j"; break; 1444 }; s; }), 1445 (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "", 1446 (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : ""); 1447 1448 dump_irq(s, "irq_enable", 1449 sl811_read(sl811, SL11H_IRQ_ENABLE)); 1450 dump_irq(s, "irq_status", 1451 sl811_read(sl811, SL11H_IRQ_STATUS)); 1452 seq_printf(s, "frame clocks remaining: %d\n", 1453 sl811_read(sl811, SL11H_SOFTMRREG) << 6); 1454 } 1455 1456 seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a, 1457 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)), 1458 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG))); 1459 seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b, 1460 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)), 1461 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG))); 1462 seq_printf(s, "\n"); 1463 list_for_each_entry (ep, &sl811->async, schedule) { 1464 struct urb *urb; 1465 1466 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d" 1467 " nak %d err %d\n", 1468 (ep == sl811->active_a) ? "(A) " : "", 1469 (ep == sl811->active_b) ? "(B) " : "", 1470 ep, ep->epnum, 1471 ({ char *s; switch (ep->nextpid) { 1472 case USB_PID_IN: s = "in"; break; 1473 case USB_PID_OUT: s = "out"; break; 1474 case USB_PID_SETUP: s = "setup"; break; 1475 case USB_PID_ACK: s = "status"; break; 1476 default: s = "?"; break; 1477 }; s;}), 1478 ep->maxpacket, 1479 ep->nak_count, ep->error_count); 1480 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) { 1481 seq_printf(s, " urb%p, %d/%d\n", urb, 1482 urb->actual_length, 1483 urb->transfer_buffer_length); 1484 } 1485 } 1486 if (!list_empty(&sl811->async)) 1487 seq_printf(s, "\n"); 1488 1489 seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE); 1490 1491 for (i = 0; i < PERIODIC_SIZE; i++) { 1492 ep = sl811->periodic[i]; 1493 if (!ep) 1494 continue; 1495 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]); 1496 1497 /* DUMB: prints shared entries multiple times */ 1498 do { 1499 seq_printf(s, 1500 " %s%sqh%d/%p (%sdev%d ep%d%s max %d) " 1501 "err %d\n", 1502 (ep == sl811->active_a) ? "(A) " : "", 1503 (ep == sl811->active_b) ? "(B) " : "", 1504 ep->period, ep, 1505 (ep->udev->speed == USB_SPEED_FULL) 1506 ? "" : "ls ", 1507 ep->udev->devnum, ep->epnum, 1508 (ep->epnum == 0) ? "" 1509 : ((ep->nextpid == USB_PID_IN) 1510 ? "in" 1511 : "out"), 1512 ep->maxpacket, ep->error_count); 1513 ep = ep->next; 1514 } while (ep); 1515 } 1516 1517 spin_unlock_irq(&sl811->lock); 1518 seq_printf(s, "\n"); 1519 1520 return 0; 1521 } 1522 1523 static int proc_sl811h_open(struct inode *inode, struct file *file) 1524 { 1525 return single_open(file, proc_sl811h_show, PDE(inode)->data); 1526 } 1527 1528 static struct file_operations proc_ops = { 1529 .open = proc_sl811h_open, 1530 .read = seq_read, 1531 .llseek = seq_lseek, 1532 .release = single_release, 1533 }; 1534 1535 /* expect just one sl811 per system */ 1536 static const char proc_filename[] = "driver/sl811h"; 1537 1538 static void create_debug_file(struct sl811 *sl811) 1539 { 1540 struct proc_dir_entry *pde; 1541 1542 pde = create_proc_entry(proc_filename, 0, NULL); 1543 if (pde == NULL) 1544 return; 1545 1546 pde->proc_fops = &proc_ops; 1547 pde->data = sl811; 1548 sl811->pde = pde; 1549 } 1550 1551 static void remove_debug_file(struct sl811 *sl811) 1552 { 1553 if (sl811->pde) 1554 remove_proc_entry(proc_filename, NULL); 1555 } 1556 1557 #endif 1558 1559 /*-------------------------------------------------------------------------*/ 1560 1561 static void 1562 sl811h_stop(struct usb_hcd *hcd) 1563 { 1564 struct sl811 *sl811 = hcd_to_sl811(hcd); 1565 unsigned long flags; 1566 1567 del_timer_sync(&hcd->rh_timer); 1568 1569 spin_lock_irqsave(&sl811->lock, flags); 1570 port_power(sl811, 0); 1571 spin_unlock_irqrestore(&sl811->lock, flags); 1572 } 1573 1574 static int 1575 sl811h_start(struct usb_hcd *hcd) 1576 { 1577 struct sl811 *sl811 = hcd_to_sl811(hcd); 1578 1579 /* chip has been reset, VBUS power is off */ 1580 hcd->state = HC_STATE_RUNNING; 1581 1582 if (sl811->board) { 1583 hcd->can_wakeup = sl811->board->can_wakeup; 1584 hcd->power_budget = sl811->board->power * 2; 1585 } 1586 1587 /* enable power and interupts */ 1588 port_power(sl811, 1); 1589 1590 return 0; 1591 } 1592 1593 /*-------------------------------------------------------------------------*/ 1594 1595 static struct hc_driver sl811h_hc_driver = { 1596 .description = hcd_name, 1597 .hcd_priv_size = sizeof(struct sl811), 1598 1599 /* 1600 * generic hardware linkage 1601 */ 1602 .irq = sl811h_irq, 1603 .flags = HCD_USB11 | HCD_MEMORY, 1604 1605 /* Basic lifecycle operations */ 1606 .start = sl811h_start, 1607 .stop = sl811h_stop, 1608 1609 /* 1610 * managing i/o requests and associated device resources 1611 */ 1612 .urb_enqueue = sl811h_urb_enqueue, 1613 .urb_dequeue = sl811h_urb_dequeue, 1614 .endpoint_disable = sl811h_endpoint_disable, 1615 1616 /* 1617 * periodic schedule support 1618 */ 1619 .get_frame_number = sl811h_get_frame, 1620 1621 /* 1622 * root hub support 1623 */ 1624 .hub_status_data = sl811h_hub_status_data, 1625 .hub_control = sl811h_hub_control, 1626 .hub_suspend = sl811h_hub_suspend, 1627 .hub_resume = sl811h_hub_resume, 1628 }; 1629 1630 /*-------------------------------------------------------------------------*/ 1631 1632 static int __devexit 1633 sl811h_remove(struct device *dev) 1634 { 1635 struct usb_hcd *hcd = dev_get_drvdata(dev); 1636 struct sl811 *sl811 = hcd_to_sl811(hcd); 1637 struct platform_device *pdev; 1638 struct resource *res; 1639 1640 pdev = container_of(dev, struct platform_device, dev); 1641 1642 remove_debug_file(sl811); 1643 usb_remove_hcd(hcd); 1644 1645 /* some platforms may use IORESOURCE_IO */ 1646 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1647 if (res) 1648 iounmap(sl811->data_reg); 1649 1650 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1651 if (res) 1652 iounmap(sl811->addr_reg); 1653 1654 usb_put_hcd(hcd); 1655 return 0; 1656 } 1657 1658 static int __devinit 1659 sl811h_probe(struct device *dev) 1660 { 1661 struct usb_hcd *hcd; 1662 struct sl811 *sl811; 1663 struct platform_device *pdev; 1664 struct resource *addr, *data; 1665 int irq; 1666 void __iomem *addr_reg; 1667 void __iomem *data_reg; 1668 int retval; 1669 u8 tmp, ioaddr = 0; 1670 1671 /* basic sanity checks first. board-specific init logic should 1672 * have initialized these three resources and probably board 1673 * specific platform_data. we don't probe for IRQs, and do only 1674 * minimal sanity checking. 1675 */ 1676 pdev = container_of(dev, struct platform_device, dev); 1677 irq = platform_get_irq(pdev, 0); 1678 if (pdev->num_resources < 3 || irq < 0) 1679 return -ENODEV; 1680 1681 /* refuse to confuse usbcore */ 1682 if (dev->dma_mask) { 1683 DBG("no we won't dma\n"); 1684 return -EINVAL; 1685 } 1686 1687 /* the chip may be wired for either kind of addressing */ 1688 addr = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1689 data = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1690 retval = -EBUSY; 1691 if (!addr || !data) { 1692 addr = platform_get_resource(pdev, IORESOURCE_IO, 0); 1693 data = platform_get_resource(pdev, IORESOURCE_IO, 1); 1694 if (!addr || !data) 1695 return -ENODEV; 1696 ioaddr = 1; 1697 1698 addr_reg = (void __iomem *) addr->start; 1699 data_reg = (void __iomem *) data->start; 1700 } else { 1701 addr_reg = ioremap(addr->start, 1); 1702 if (addr_reg == NULL) { 1703 retval = -ENOMEM; 1704 goto err2; 1705 } 1706 1707 data_reg = ioremap(data->start, 1); 1708 if (data_reg == NULL) { 1709 retval = -ENOMEM; 1710 goto err4; 1711 } 1712 } 1713 1714 /* allocate and initialize hcd */ 1715 hcd = usb_create_hcd(&sl811h_hc_driver, dev, dev->bus_id); 1716 if (!hcd) { 1717 retval = -ENOMEM; 1718 goto err5; 1719 } 1720 hcd->rsrc_start = addr->start; 1721 sl811 = hcd_to_sl811(hcd); 1722 1723 spin_lock_init(&sl811->lock); 1724 INIT_LIST_HEAD(&sl811->async); 1725 sl811->board = dev->platform_data; 1726 init_timer(&sl811->timer); 1727 sl811->timer.function = sl811h_timer; 1728 sl811->timer.data = (unsigned long) sl811; 1729 sl811->addr_reg = addr_reg; 1730 sl811->data_reg = data_reg; 1731 1732 spin_lock_irq(&sl811->lock); 1733 port_power(sl811, 0); 1734 spin_unlock_irq(&sl811->lock); 1735 msleep(200); 1736 1737 tmp = sl811_read(sl811, SL11H_HWREVREG); 1738 switch (tmp >> 4) { 1739 case 1: 1740 hcd->product_desc = "SL811HS v1.2"; 1741 break; 1742 case 2: 1743 hcd->product_desc = "SL811HS v1.5"; 1744 break; 1745 default: 1746 /* reject case 0, SL11S is less functional */ 1747 DBG("chiprev %02x\n", tmp); 1748 retval = -ENXIO; 1749 goto err6; 1750 } 1751 1752 /* The chip's IRQ is level triggered, active high. A requirement 1753 * for platform device setup is to cope with things like signal 1754 * inverters (e.g. CF is active low) or working only with edge 1755 * triggers (e.g. most ARM CPUs). Initial driver stress testing 1756 * was on a system with single edge triggering, so most sorts of 1757 * triggering arrangement should work. 1758 */ 1759 retval = usb_add_hcd(hcd, irq, SA_INTERRUPT | SA_SHIRQ); 1760 if (retval != 0) 1761 goto err6; 1762 1763 create_debug_file(sl811); 1764 return retval; 1765 1766 err6: 1767 usb_put_hcd(hcd); 1768 err5: 1769 if (!ioaddr) 1770 iounmap(data_reg); 1771 err4: 1772 if (!ioaddr) 1773 iounmap(addr_reg); 1774 err2: 1775 DBG("init error, %d\n", retval); 1776 return retval; 1777 } 1778 1779 #ifdef CONFIG_PM 1780 1781 /* for this device there's no useful distinction between the controller 1782 * and its root hub, except that the root hub only gets direct PM calls 1783 * when CONFIG_USB_SUSPEND is enabled. 1784 */ 1785 1786 static int 1787 sl811h_suspend(struct device *dev, pm_message_t state, u32 phase) 1788 { 1789 struct usb_hcd *hcd = dev_get_drvdata(dev); 1790 struct sl811 *sl811 = hcd_to_sl811(hcd); 1791 int retval = 0; 1792 1793 if (phase != SUSPEND_POWER_DOWN) 1794 return retval; 1795 1796 if (state.event == PM_EVENT_FREEZE) 1797 retval = sl811h_hub_suspend(hcd); 1798 else if (state.event == PM_EVENT_SUSPEND) 1799 port_power(sl811, 0); 1800 if (retval == 0) 1801 dev->power.power_state = state; 1802 return retval; 1803 } 1804 1805 static int 1806 sl811h_resume(struct device *dev, u32 phase) 1807 { 1808 struct usb_hcd *hcd = dev_get_drvdata(dev); 1809 struct sl811 *sl811 = hcd_to_sl811(hcd); 1810 1811 if (phase != RESUME_POWER_ON) 1812 return 0; 1813 1814 /* with no "check to see if VBUS is still powered" board hook, 1815 * let's assume it'd only be powered to enable remote wakeup. 1816 */ 1817 if (dev->power.power_state.event == PM_EVENT_SUSPEND 1818 || !hcd->can_wakeup) { 1819 sl811->port1 = 0; 1820 port_power(sl811, 1); 1821 return 0; 1822 } 1823 1824 dev->power.power_state = PMSG_ON; 1825 return sl811h_hub_resume(hcd); 1826 } 1827 1828 #else 1829 1830 #define sl811h_suspend NULL 1831 #define sl811h_resume NULL 1832 1833 #endif 1834 1835 1836 /* this driver is exported so sl811_cs can depend on it */ 1837 struct device_driver sl811h_driver = { 1838 .name = (char *) hcd_name, 1839 .bus = &platform_bus_type, 1840 1841 .probe = sl811h_probe, 1842 .remove = __devexit_p(sl811h_remove), 1843 1844 .suspend = sl811h_suspend, 1845 .resume = sl811h_resume, 1846 }; 1847 EXPORT_SYMBOL(sl811h_driver); 1848 1849 /*-------------------------------------------------------------------------*/ 1850 1851 static int __init sl811h_init(void) 1852 { 1853 if (usb_disabled()) 1854 return -ENODEV; 1855 1856 INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION); 1857 return driver_register(&sl811h_driver); 1858 } 1859 module_init(sl811h_init); 1860 1861 static void __exit sl811h_cleanup(void) 1862 { 1863 driver_unregister(&sl811h_driver); 1864 } 1865 module_exit(sl811h_cleanup); 1866