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