📄 usb-ocp-ohci.c
字号:
/* * URB OCP OHCI HCD (Host Controller Driver) for USB. * (C) Copyright 2002 MontaVista Software * current Mantainer: * Armin Kuster <akuster@mvista.com> * * * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> * (C) Copyright 2000-2001 David Brownell <dbrownell@users.sourceforge.net> * * [ Initialisation is based on Linus' ] * [ uhci code and gregs ohci fragments ] * [ (C) Copyright 1999 Linus Torvalds ] * [ (C) Copyright 1999 Gregory P. Smith] * * * History: * * 2002/earlier: Ported to OCP support and from initail driver from * Brad on the redwood. * based on usb-ohci.c v 5.3 * The OCP bus is Big Endian so the le32* is * not needed * * v1.0 2002/03/27 initial release */ #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/sched.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() */#undef DEBUG#include <linux/usb.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/system.h>#include <asm/unaligned.h>#define OHCI_USE_NPS // force NoPowerSwitching mode// #define OHCI_VERBOSE_DEBUG /* not always helpful */#include "usb-ohci.h"#undef readl#undef writel#define readl(addr) in_be32(addr)#define writel(b,addr) out_be32(addr, b)#define CPU_TO_LE32(x) (x)#define CPU_TO_LE16(x) (x)#define LE32_TO_CPUP(p) (*(__u32*)(p))#define LE16_TO_CPUP(p) (*(__u16*)(p))#define LE16_TO_CPU(x) (x)#define LE32_TO_CPU(x) (x)static void *ocp_ohci;static struct pci_dev dev;/* * Version Information */#define DRIVER_VERSION "v1.0"#define DRIVER_AUTHOR "Armin Kuster <akuster@mvista>, Brad Parker"#define DRIVER_DESC "USB OCP OHCI Host Controller Driver"/* For initializing controller (mask in an HCFS mode too) */#define OHCI_CONTROL_INIT \ (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE#define OHCI_UNLINK_TIMEOUT (HZ / 10)static LIST_HEAD (ohci_hcd_list);static spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;#define USB_ZERO_PACKET 0x0040 // Finish bulk OUTs always with zero length packet#define min_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })#define max_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })/*-------------------------------------------------------------------------*//* AMD-756 (D2 rev) reports corrupt register contents in some cases. * The erratum (#4) description is incorrect. AMD's workaround waits * till some bits (mostly reserved) are clear; ok for all revs. */#define read_roothub(hc, register, mask) ({ \ u32 temp = readl (&hc->regs->roothub.register); \ if (hc->flags & OHCI_QUIRK_AMD756) \ while (temp & mask) \ temp = readl (&hc->regs->roothub.register); \ temp; })static u32 roothub_a (struct ohci *hc) { return read_roothub (hc, a, 0xfc0fe000); }static inline u32 roothub_b (struct ohci *hc) { return readl (&hc->regs->roothub.b); }static inline u32 roothub_status (struct ohci *hc) { return readl (&hc->regs->roothub.status); }static u32 roothub_portstatus (struct ohci *hc, int i) { return read_roothub (hc, portstatus [i], 0xffe0fce0); }/*-------------------------------------------------------------------------* * URB support functions *-------------------------------------------------------------------------*/ /* free HCD-private data associated with this URB */static void urb_free_priv (struct ohci *hc, urb_priv_t * urb_priv){ int i; int last = urb_priv->length - 1; int len; int dir; struct td *td; if (last >= 0) { /* ISOC, BULK, INTR data buffer starts at td 0 * CTRL setup starts at td 0 */ td = urb_priv->td [0]; len = td->urb->transfer_buffer_length, dir = usb_pipeout (td->urb->pipe) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE; /* unmap CTRL URB setup */ if (usb_pipecontrol (td->urb->pipe)) { pci_unmap_single (hc->ohci_dev, td->data_dma, 8, PCI_DMA_TODEVICE); /* CTRL data buffer starts at td 1 if len > 0 */ if (len && last > 0) td = urb_priv->td [1]; } /* unmap data buffer */ if (len && td->data_dma) pci_unmap_single (hc->ohci_dev, td->data_dma, len, dir); for (i = 0; i <= last; i++) { td = urb_priv->td [i]; if (td) td_free (hc, td); } } kfree (urb_priv);} static void urb_rm_priv_locked (urb_t * urb) { urb_priv_t * urb_priv = urb->hcpriv; if (urb_priv) { urb->hcpriv = NULL; if (urb->dev == NULL) { printk("urb_rm_priv_locked(urb=%p) dev=NULL!\n", urb); return; }#ifdef DO_TIMEOUTS if (urb->timeout) { list_del (&urb->urb_list); urb->timeout -= jiffies; }#endif /* Release int/iso bandwidth */ if (urb->bandwidth) { switch (usb_pipetype(urb->pipe)) { case PIPE_INTERRUPT: usb_release_bandwidth (urb->dev, urb, 0); break; case PIPE_ISOCHRONOUS: usb_release_bandwidth (urb->dev, urb, 1); break; default: break; } }if (urb->dev->bus) urb_free_priv ((struct ohci *)urb->dev->bus->hcpriv, urb_priv);else printk("urb_rm_priv_locked(urb=%p) usb->dev->bus=NULL!\n", urb); usb_dec_dev_use (urb->dev); urb->dev = NULL; }}static void urb_rm_priv (urb_t * urb){ unsigned long flags; spin_lock_irqsave (&usb_ed_lock, flags); urb_rm_priv_locked (urb); spin_unlock_irqrestore (&usb_ed_lock, flags);}/*-------------------------------------------------------------------------*/ #ifdef DEBUGstatic int sohci_get_current_frame_number (struct usb_device * dev);/* debug| print the main components of an URB * small: 0) header + data packets 1) just header */ static void urb_print (urb_t * urb, char * str, int small){ unsigned int pipe= urb->pipe; if (!urb->dev) { dbg("%s URB: no dev", str); return; } if (!urb->dev->bus) { dbg("%s URB: no bus, urb=%p, usb->dev=%p", str, urb, urb->dev); return; } #ifndef OHCI_VERBOSE_DEBUG if (urb->status != 0)#endif dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,flags:%4x,len:%d/%d,stat:%d(%x)", str, sohci_get_current_frame_number (urb->dev), usb_pipedevice (pipe), usb_pipeendpoint (pipe), usb_pipeout (pipe)? 'O': 'I', usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"): (usb_pipecontrol (pipe)? "CTRL": "BULK"), urb->transfer_flags, urb->actual_length, urb->transfer_buffer_length, urb->status, urb->status);#ifdef OHCI_VERBOSE_DEBUG if (!small) { int i, len; if (usb_pipecontrol (pipe)) { printk (KERN_DEBUG __FILE__ ": cmd(8):"); for (i = 0; i < 8 ; i++) printk (" %02x", ((__u8 *) urb->setup_packet) [i]); printk ("\n"); } if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) { printk (KERN_DEBUG __FILE__ ": data(%d/%d):", urb->actual_length, urb->transfer_buffer_length); len = usb_pipeout (pipe)? urb->transfer_buffer_length: urb->actual_length; for (i = 0; i < 16 && i < len; i++) printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]); printk ("%s stat:%d\n", i < len? "...": "", urb->status); } } #endif}/* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/void ep_print_int_eds (ohci_t * ohci, char * str) { int i, j; __u32 * ed_p;return; for (i= 0; i < 32; i++) { j = 5; ed_p = &(ohci->hcca->int_table [i]); if (*ed_p == 0) continue; printk (KERN_DEBUG __FILE__ ": %s branch int %2d(%2x):", str, i, i); while (*ed_p != 0 && j--) { ed_t *ed = dma_to_ed (ohci, LE32_TO_CPUP(ed_p)); printk (" ed: %4x;", ed->hwINFO); ed_p = &ed->hwNextED; } printk ("\n"); }}static void ohci_dump_intr_mask (char *label, __u32 mask){ dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s", label, mask, (mask & OHCI_INTR_MIE) ? " MIE" : "", (mask & OHCI_INTR_OC) ? " OC" : "", (mask & OHCI_INTR_RHSC) ? " RHSC" : "", (mask & OHCI_INTR_FNO) ? " FNO" : "", (mask & OHCI_INTR_UE) ? " UE" : "", (mask & OHCI_INTR_RD) ? " RD" : "", (mask & OHCI_INTR_SF) ? " SF" : "", (mask & OHCI_INTR_WDH) ? " WDH" : "", (mask & OHCI_INTR_SO) ? " SO" : "" );}static void maybe_print_eds (char *label, __u32 value){ if (value) dbg ("%s %08x", label, value);}static char *hcfs2string (int state){ switch (state) { case OHCI_USB_RESET: return "reset"; case OHCI_USB_RESUME: return "resume"; case OHCI_USB_OPER: return "operational"; case OHCI_USB_SUSPEND: return "suspend"; } return "?";}static void maybe_follow_tds (char *label, __u32 value){ td_t *td = (td_t *)bus_to_virt(value); printk("%s %08x\n", label, value); while (value) { printk("td: %p (phys %p) index %d\n", td, (void *)value, td->index); printk(" hwINFO %08x, hwCBP %08x, hwBE %08x, hwNextTD %08x\n", td->hwINFO, td->hwCBP, td->hwBE, td->hwNextTD); printk(" ed %p, next_dl_td %p, urb %p\n", td->ed, td->next_dl_td, td->urb); value = td->hwNextTD; td = (ed_t *)bus_to_virt(value); }}static void maybe_follow_eds (char *label, __u32 value){ ed_t *ed = (ed_t *)bus_to_virt(value); printk("%s %08x\n", label, value); while (value) { printk("ed: %p (phys %p)\n", ed, (void *)value); printk(" hwINFO %08x, hwHeadP %08x, hwTailP %08x\n", ed->hwINFO, ed->hwHeadP, ed->hwTailP); printk(" hwNextED %08x, ed_prev %p\n", ed->hwNextED, ed->ed_prev); printk(" int period %d, branch %d, load %d, interval %d\n", ed->int_period, ed->int_branch, ed->int_load, ed->int_interval); printk(" state %d, type %d, last_iso %d\n", ed->state, ed->type, ed->last_iso); maybe_follow_tds("hwHeadP", ed->hwHeadP); value = ed->hwNextED; ed = (ed_t *)bus_to_virt(value); }}// dump control and status registersstatic void ohci_dump_status (ohci_t *controller){ struct ohci_regs *regs = controller->regs; __u32 temp; temp = readl (®s->revision) & 0xff; if (temp != 0x10) dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f)); temp = readl (®s->control); dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp, (temp & OHCI_CTRL_RWE) ? " RWE" : "", (temp & OHCI_CTRL_RWC) ? " RWC" : "", (temp & OHCI_CTRL_IR) ? " IR" : "", hcfs2string (temp & OHCI_CTRL_HCFS), (temp & OHCI_CTRL_BLE) ? " BLE" : "", (temp & OHCI_CTRL_CLE) ? " CLE" : "", (temp & OHCI_CTRL_IE) ? " IE" : "", (temp & OHCI_CTRL_PLE) ? " PLE" : "", temp & OHCI_CTRL_CBSR ); temp = readl (®s->cmdstatus); dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp, (temp & OHCI_SOC) >> 16, (temp & OHCI_OCR) ? " OCR" : "", (temp & OHCI_BLF) ? " BLF" : "", (temp & OHCI_CLF) ? " CLF" : "", (temp & OHCI_HCR) ? " HCR" : "" ); ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus)); ohci_dump_intr_mask ("intrenable", readl (®s->intrenable)); // intrdisable always same as intrenable // ohci_dump_intr_mask ("intrdisable", readl (®s->intrdisable)); maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent)); maybe_follow_eds ("ed_controlhead", readl (®s->ed_controlhead)); maybe_follow_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent)); maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead)); maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent)); maybe_follow_tds ("donehead", readl (®s->donehead));}static void ohci_dump_roothub (ohci_t *controller, int verbose){ __u32 temp, ndp, i; temp = roothub_a (controller); ndp = (temp & RH_A_NDP); if (verbose) { dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp, ((temp & RH_A_POTPGT) >> 24) & 0xff, (temp & RH_A_NOCP) ? " NOCP" : "", (temp & RH_A_OCPM) ? " OCPM" : "", (temp & RH_A_DT) ? " DT" : "", (temp & RH_A_NPS) ? " NPS" : "", (temp & RH_A_PSM) ? " PSM" : "", ndp ); temp = roothub_b (controller); dbg ("roothub.b: %08x PPCM=%04x DR=%04x", temp, (temp & RH_B_PPCM) >> 16, (temp & RH_B_DR) ); temp = roothub_status (controller); dbg ("roothub.status: %08x%s%s%s%s%s%s", temp, (temp & RH_HS_CRWE) ? " CRWE" : "", (temp & RH_HS_OCIC) ? " OCIC" : "", (temp & RH_HS_LPSC) ? " LPSC" : "", (temp & RH_HS_DRWE) ? " DRWE" : "", (temp & RH_HS_OCI) ? " OCI" : "", (temp & RH_HS_LPS) ? " LPS" : "" ); } for (i = 0; i < ndp; i++) { temp = roothub_portstatus (controller, i); dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s", i, temp, (temp & RH_PS_PRSC) ? " PRSC" : "", (temp & RH_PS_OCIC) ? " OCIC" : "", (temp & RH_PS_PSSC) ? " PSSC" : "", (temp & RH_PS_PESC) ? " PESC" : "", (temp & RH_PS_CSC) ? " CSC" : "", (temp & RH_PS_LSDA) ? " LSDA" : "", (temp & RH_PS_PPS) ? " PPS" : "", (temp & RH_PS_PRS) ? " PRS" : "", (temp & RH_PS_POCI) ? " POCI" : "", (temp & RH_PS_PSS) ? " PSS" : "",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -