Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | /* UHCI HCD (Host Controller Driver) for USB, main part for HCD frame (c) 1999-2002 Georg Acher + Deti Fliegl + Thomas Sailer georg@acher.org deti@fliegl.de sailer@ife.ee.ethz.ch with the help of David Brownell, david-b@pacbell.net Adam Richter, adam@yggdrasil.com Roman Weissgaerber, weissg@vienna.at HW-initalization based on material of Randy Dunlap + Johannes Erdfelt + Gregory P. Smith + Linus Torvalds $Id: usb-uhci-hcd.c,v 1.1 2002/05/14 20:36:57 acher Exp $ */ #include <linux/config.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ioport.h> #include <linux/jiffies.h> #include <linux/slab.h> #include <linux/smp_lock.h> #include <linux/errno.h> #include <linux/init.h> #include <linux/timer.h> #include <linux/list.h> #include <linux/interrupt.h> /* for in_interrupt () */ #include <asm/io.h> #include <asm/irq.h> #include <asm/system.h> #include <asm/unaligned.h> #include <asm/byteorder.h> #include <linux/usb.h> #define CONFIG_USB_DEBUG #ifdef CONFIG_USB_DEBUG #define DEBUG #else #undef DEBUG #endif #include "../core/hcd.h" #include "usb-uhci-hcd.h" #define DRIVER_VERSION "$Revision: 1.1 $" #define DRIVER_AUTHOR "Georg Acher, Deti Fliegl, Thomas Sailer" #define DRIVER_DESC "USB 1.1 Universal Host Controller Interface driver (HCD)" /*--------------------------------------------------------------------------*/ // Values you may tweak /* CONFIG_USB_UHCI_HIGH_BANDWITH turns on Full Speed Bandwidth * Reclamation: feature that puts loop on descriptor loop when * there's some transfer going on. With FSBR, USB performance * is optimal, but PCI can be slowed down up-to 5 times, slowing down * system performance (eg. framebuffer devices). */ #define CONFIG_USB_UHCI_HIGH_BANDWIDTH /* *_DEPTH_FIRST puts descriptor in depth-first mode. This has * somehow similar effect to FSBR (higher speed), but does not * slow PCI down. OTOH USB performace is slightly slower than * in FSBR case and single device could hog whole USB, starving * other devices. */ #define USE_CTRL_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first #define USE_BULK_DEPTH_FIRST 0 // 0: Breadth first, 1: Depth first /* Turning off both CONFIG_USB_UHCI_HIGH_BANDWITH and *_DEPTH_FIRST * will lead to <64KB/sec performance over USB for bulk transfers targeting * one device's endpoint. You probably do not want to do that. */ // stop bandwidth reclamation after (roughly) 50ms #define IDLE_TIMEOUT (HZ/20) // Suppress HC interrupt error messages for 5s #define ERROR_SUPPRESSION_TIME (HZ*5) // HC watchdog #define WATCHDOG_TIMEOUT (4*HZ) #define MAX_REANIMATIONS 5 #define DEBUG_SYMBOLS #ifdef DEBUG_SYMBOLS #ifndef EXPORT_SYMTAB #define EXPORT_SYMTAB #endif #endif #define queue_dbg dbg #define async_dbg dbg #define init_dbg dbg /*--------------------------------------------------------------------------*/ // NO serviceable parts below! /*--------------------------------------------------------------------------*/ static struct uhci *devs = NULL; /* used by userspace UHCI data structure dumper */ struct uhci **uhci_devices = &devs; /* A few prototypes */ static int uhci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb); static int hc_reset (struct uhci_hcd *uhci); static void uhci_stop (struct usb_hcd *hcd); static int process_transfer (struct uhci_hcd *uhci, struct urb *urb, int mode); static int process_iso (struct uhci_hcd *uhci, struct urb *urb, int mode); static int process_interrupt (struct uhci_hcd *uhci, struct urb *urb, int mode); static int process_urb (struct uhci_hcd *uhci, struct list_head *p); static int uhci_urb_enqueue (struct usb_hcd *hcd, struct urb *urb, int mem_flags); static int hc_defibrillate(struct uhci_hcd * uhci); static int hc_irq_run(struct uhci_hcd *uhci); #include "usb-uhci-dbg.c" #include "usb-uhci-mem.c" #include "usb-uhci-hub.c" #include "usb-uhci-q.c" #define PIPESTRING(x) (x==PIPE_BULK?"Bulk":(x==PIPE_INTERRUPT?"Interrupt":(x==PIPE_CONTROL?"Control":"Iso"))) /*--------------------------------------------------------------------------*/ static int uhci_urb_enqueue (struct usb_hcd *hcd, struct urb *urb, int mem_flags) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); urb_priv_t *urb_priv; int ret = 0, type; unsigned long flags; struct urb *queued_urb=NULL; int bustime; type = usb_pipetype (urb->pipe); // err("submit_urb: scheduling %p (%s), tb %p, len %i", urb, // PIPESTRING(type),urb->transfer_buffer,urb->transfer_buffer_length); if (uhci->need_init) { if (in_interrupt()) return -ESHUTDOWN; spin_lock_irqsave (&uhci->urb_list_lock, flags); ret = hc_defibrillate(uhci); spin_unlock_irqrestore (&uhci->urb_list_lock, flags); if (ret) return ret; } if (!uhci->running) return -ESHUTDOWN; spin_lock_irqsave (&uhci->urb_list_lock, flags); queued_urb = search_dev_ep (uhci, urb); // returns already queued urb for that pipe if (queued_urb) { queue_dbg("found bulk urb %p\n", queued_urb); if (( type != PIPE_BULK) || ((type == PIPE_BULK) && (!(urb->transfer_flags & USB_QUEUE_BULK) || !(queued_urb->transfer_flags & USB_QUEUE_BULK)))) { spin_unlock_irqrestore (&uhci->urb_list_lock, flags); err("ENXIO (%s) %08x, flags %x, urb %p, burb %p, propably device driver bug...", PIPESTRING(type), urb->pipe,urb->transfer_flags,urb,queued_urb); return -ENXIO; // urb already queued } } urb_priv = uhci_alloc_priv(mem_flags); if (!urb_priv) { spin_unlock_irqrestore (&uhci->urb_list_lock, flags); return -ENOMEM; } urb->hcpriv = urb_priv; urb_priv->urb=urb; INIT_LIST_HEAD (&urb_priv->desc_list); if (type == PIPE_CONTROL) urb_priv->setup_packet_dma = pci_map_single(uhci->uhci_pci, urb->setup_packet, sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE); if (urb->transfer_buffer_length) urb_priv->transfer_buffer_dma = pci_map_single(uhci->uhci_pci, urb->transfer_buffer, urb->transfer_buffer_length, usb_pipein(urb->pipe) ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE); // for bulk queuing it is essential that interrupts are disabled until submission // all other type enable interrupts again switch (type) { case PIPE_BULK: if (queued_urb) { while (((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb) // find last queued bulk queued_urb=((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb; ((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb=urb; } atomic_inc (&uhci->avoid_bulk); ret = uhci_submit_bulk_urb (uhci, urb, queued_urb); atomic_dec (&uhci->avoid_bulk); spin_unlock_irqrestore (&uhci->urb_list_lock, flags); break; case PIPE_ISOCHRONOUS: spin_unlock_irqrestore (&uhci->urb_list_lock, flags); if (urb->bandwidth == 0) { /* not yet checked/allocated */ bustime = usb_check_bandwidth (urb->dev, urb); if (bustime < 0) ret = bustime; else { ret = uhci_submit_iso_urb(uhci, urb, mem_flags); if (ret == 0) usb_claim_bandwidth (urb->dev, urb, bustime, 1); } } else { /* bandwidth is already set */ ret = uhci_submit_iso_urb(uhci, urb, mem_flags); } break; case PIPE_INTERRUPT: spin_unlock_irqrestore (&uhci->urb_list_lock, flags); if (urb->bandwidth == 0) { /* not yet checked/allocated */ bustime = usb_check_bandwidth (urb->dev, urb); if (bustime < 0) ret = bustime; else { ret = uhci_submit_int_urb(uhci, urb); if (ret == 0) usb_claim_bandwidth (urb->dev, urb, bustime, 0); } } else { /* bandwidth is already set */ ret = uhci_submit_int_urb(uhci, urb); } break; case PIPE_CONTROL: spin_unlock_irqrestore (&uhci->urb_list_lock, flags); ret = uhci_submit_control_urb (uhci, urb); break; default: spin_unlock_irqrestore (&uhci->urb_list_lock, flags); ret = -EINVAL; } // err("submit_urb: scheduled with ret: %d", ret); if (ret != 0) uhci_free_priv(uhci, urb, urb_priv); return ret; } /*--------------------------------------------------------------------------*/ static int uhci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb) { unsigned long flags=0; struct uhci_hcd *uhci; dbg("uhci_urb_dequeue called for %p",urb); uhci = hcd_to_uhci (hcd); if (urb->transfer_flags & USB_ASYNC_UNLINK) { int ret; spin_lock_irqsave (&uhci->urb_list_lock, flags); ret = uhci_unlink_urb_async(uhci, urb, UNLINK_ASYNC_STORE_URB); spin_unlock_irqrestore (&uhci->urb_list_lock, flags); return ret; } else return uhci_unlink_urb_sync(uhci, urb); } /*--------------------------------------------------------------------------*/ static int uhci_get_frame (struct usb_hcd *hcd) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); return inw ((int)uhci->hcd.regs + USBFRNUM); } /*--------------------------------------------------------------------------*/ // Init and shutdown functions for HW /*--------------------------------------------------------------------------*/ static int hc_reset (struct uhci_hcd *uhci) { unsigned int io_addr = (int)uhci->hcd.regs; uhci->apm_state = 0; uhci->running = 0; outw (USBCMD_GRESET, io_addr + USBCMD); uhci_wait_ms (50); /* Global reset for 50ms */ outw (0, io_addr + USBCMD); uhci_wait_ms (10); return 0; } /*--------------------------------------------------------------------------*/ static int hc_irq_run(struct uhci_hcd *uhci) { unsigned int io_addr = (int)uhci->hcd.regs; /* Turn on all interrupts */ outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR); /* Start at frame 0 */ outw (0, io_addr + USBFRNUM); outl (uhci->framelist_dma, io_addr + USBFLBASEADD); /* Run and mark it configured with a 64-byte max packet */ outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD); uhci->apm_state = 1; uhci->running = 1; uhci->last_hcd_irq = jiffies+4*HZ; return 0; } /*--------------------------------------------------------------------------*/ static int hc_start (struct uhci_hcd *uhci) { unsigned int io_addr = (int)uhci->hcd.regs; int timeout = 10; struct usb_device *udev; init_dbg("hc_start uhci %p",uhci); /* * Reset the HC - this will force us to get a * new notification of any already connected * ports due to the virtual disconnect that it * implies. */ outw (USBCMD_HCRESET, io_addr + USBCMD); while (inw (io_addr + USBCMD) & USBCMD_HCRESET) { if (!--timeout) { err("USBCMD_HCRESET timed out!"); break; } udelay(1); } hc_irq_run(uhci); /* connect the virtual root hub */ uhci->hcd.self.root_hub = udev = usb_alloc_dev (NULL, &uhci->hcd.self); uhci->hcd.state = USB_STATE_READY; if (!udev) { uhci->running = 0; // FIXME cleanup return -ENOMEM; } usb_connect (udev); udev->speed = USB_SPEED_FULL; if (usb_register_root_hub (udev, &uhci->hcd.pdev->dev) != 0) { usb_free_dev (udev); uhci->running = 0; // FIXME cleanup return -ENODEV; } return 0; } /*--------------------------------------------------------------------------*/ // Start up UHCI, find ports, init DMA lists static int __devinit uhci_start (struct usb_hcd *hcd) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); int ret; int io_addr=(int)hcd->regs, io_size=0x20; // FIXME init_dbg("uhci_start hcd %p uhci %p, pdev %p",hcd,uhci,hcd->pdev); /* disable legacy emulation, Linux takes over... */ pci_write_config_word (hcd->pdev, USBLEGSUP, 0); /* UHCI specs says devices must have 2 ports, but goes on to say */ /* they may have more but give no way to determine how many they */ /* have, so default to 2 */ /* According to the UHCI spec, Bit 7 is always set to 1. So we try */ /* to use this to our advantage */ for (uhci->maxports = 0; uhci->maxports < (io_size - 0x10) / 2; uhci->maxports++) { unsigned int portstatus; portstatus = inw (io_addr + 0x10 + (uhci->maxports * 2)); dbg("port %i, adr %x status %x", uhci->maxports, io_addr + 0x10 + (uhci->maxports * 2), portstatus); if (!(portstatus & 0x0080)) break; } warn("Detected %d ports", uhci->maxports); if (uhci->maxports < 2 || uhci->maxports > 8) { dbg("Port count misdetected, forcing to 2 ports"); uhci->maxports = 2; } ret=init_skel(uhci); if (ret) return ret; hc_reset (uhci); if (hc_start (uhci) < 0) { err ("can't start %s", uhci->hcd.self.bus_name); uhci_stop (hcd); return -EBUSY; } // Enable PIRQ pci_write_config_word (hcd->pdev, USBLEGSUP, USBLEGSUP_DEFAULT); set_td_ioc(uhci->td128ms); // start watchdog interrupt uhci->last_hcd_irq=jiffies+5*HZ; return 0; } /*--------------------------------------------------------------------------*/ static void uhci_free_config (struct usb_hcd *hcd, struct usb_device *udev) { dbg("uhci_free_config for dev %p", udev); uhci_unlink_urbs (hcd_to_uhci (hcd), udev, 0); // Forced unlink of remaining URBs } /*--------------------------------------------------------------------------*/ static void uhci_stop (struct usb_hcd *hcd) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); init_dbg("%s: stop controller", hcd->bus_name); uhci->running=0; hc_reset (uhci); wait_ms (1); uhci_unlink_urbs (uhci, 0, CLEAN_FORCED); // Forced unlink of remaining URBs uhci_cleanup_unlink (uhci, CLEAN_FORCED); // force cleanup of async killed URBs cleanup_skel (uhci); } /*--------------------------------------------------------------------------*/ // UHCI INTERRUPT PROCESSING /*--------------------------------------------------------------------------*/ static void uhci_irq (struct usb_hcd *hcd) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); unsigned int io_addr = (int)hcd->regs; unsigned short status; struct list_head *p, *p2; int restarts, work_done; /* * Read the interrupt status, and write it back to clear the * interrupt cause */ status = inw (io_addr + USBSTS); if (!status) /* shared interrupt, not mine */ return; dbg("interrupt"); uhci->last_hcd_irq=jiffies; // for watchdog if (status != 1) { // Avoid too much error messages at a time if (time_after(jiffies, uhci->last_error_time + ERROR_SUPPRESSION_TIME)) { warn("interrupt, status %x, frame# %i", status, UHCI_GET_CURRENT_FRAME(uhci)); uhci->last_error_time = jiffies; } // remove host controller halted state if ((status&0x20) && (uhci->running)) { err("Host controller halted, waiting for timeout."); // outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD); } //uhci_show_status (s); } /* * traverse the list in *reverse* direction, because new entries * may be added at the end. * also, because process_urb may unlink the current urb, * we need to advance the list before * New: check for max. workload and restart count */ spin_lock (&uhci->urb_list_lock); restarts=0; work_done=0; restart: uhci->unlink_urb_done=0; p = uhci->urb_list.prev; while (p != &uhci->urb_list && (work_done < 1024)) { p2 = p; p = p->prev; process_urb (uhci, p2); work_done++; if (uhci->unlink_urb_done) { uhci->unlink_urb_done=0; restarts++; if (restarts<16) // avoid endless restarts goto restart; else break; } } if (time_after(jiffies, uhci->timeout_check + (HZ/30))) uhci_check_timeouts(uhci); clean_descs(uhci, CLEAN_NOT_FORCED); uhci_cleanup_unlink(uhci, CLEAN_NOT_FORCED); uhci_switch_timer_int(uhci); spin_unlock (&uhci->urb_list_lock); outw (status, io_addr + USBSTS); //dbg("uhci_interrupt: done"); } /*--------------------------------------------------------------------------*/ // POWER MANAGEMENT #ifdef CONFIG_PM static int uhci_suspend (struct usb_hcd *hcd, u32 state) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); hc_reset(uhci); return 0; } /*--------------------------------------------------------------------------*/ static int uhci_resume (struct usb_hcd *hcd) { struct uhci_hcd *uhci = hcd_to_uhci (hcd); hc_start(uhci); return 0; } #endif /*--------------------------------------------------------------------------*/ static const char hcd_name [] = "usb-uhci-hcd"; static const struct hc_driver uhci_driver = { description: hcd_name, // generic hardware linkage irq: uhci_irq, flags: HCD_USB11, // basic lifecycle operations start: uhci_start, #ifdef CONFIG_PM suspend: uhci_suspend, resume: uhci_resume, #endif stop: uhci_stop, // memory lifecycle (except per-request) hcd_alloc: uhci_hcd_alloc, hcd_free: uhci_hcd_free, // managing i/o requests and associated device resources urb_enqueue: uhci_urb_enqueue, urb_dequeue: uhci_urb_dequeue, free_config: uhci_free_config, // scheduling support get_frame_number: uhci_get_frame, // root hub support hub_status_data: uhci_hub_status_data, hub_control: uhci_hub_control, }; #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC MODULE_AUTHOR (DRIVER_AUTHOR); MODULE_DESCRIPTION (DRIVER_INFO); MODULE_LICENSE ("GPL"); static const struct pci_device_id __devinitdata pci_ids [] = { { /* handle any USB UHCI controller */ class: (PCI_CLASS_SERIAL_USB << 8) | 0x00, class_mask: ~0, driver_data: (unsigned long) &uhci_driver, /* no matter who makes it */ vendor: PCI_ANY_ID, device: PCI_ANY_ID, subvendor: PCI_ANY_ID, subdevice: PCI_ANY_ID, }, { /* end: all zeroes */ } }; MODULE_DEVICE_TABLE (pci, pci_ids); /* pci driver glue; this is a "new style" PCI driver module */ static struct pci_driver uhci_pci_driver = { name: (char *) hcd_name, id_table: pci_ids, probe: usb_hcd_pci_probe, remove: usb_hcd_pci_remove, #ifdef CONFIG_PM suspend: usb_hcd_pci_suspend, resume: usb_hcd_pci_resume, #endif }; /*-------------------------------------------------------------------------*/ static int __init uhci_hcd_init (void) { init_dbg (DRIVER_INFO); init_dbg ("block sizes: hq %d td %d", sizeof (struct qh), sizeof (struct td)); return pci_module_init (&uhci_pci_driver); } static void __exit uhci_hcd_cleanup (void) { pci_unregister_driver (&uhci_pci_driver); } module_init (uhci_hcd_init); module_exit (uhci_hcd_cleanup); |