⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb-ohci.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * URB OHCI HCD (Host Controller Driver) for USB. * * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> * (C) Copyright 2000 David Brownell <david-b@pacbell.net> *  * [ Initialisation is based on Linus'  ] * [ uhci code and gregs ohci fragments ] * [ (C) Copyright 1999 Linus Torvalds  ] * [ (C) Copyright 1999 Gregory P. Smith] *  *  * History: *  * 2000/09/26 fixed races in removing the private portion of the urb * 2000/09/07 disable bulk and control lists when unlinking the last *	endpoint descriptor in order to avoid unrecoverable errors on *	the Lucent chips. * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some *	urb unlink probs, indentation fixes * 2000/08/11 various oops fixes mostly affecting iso and cleanup from *	device unplugs. * 2000/06/28 use PCI hotplug framework, for better power management *	and for Cardbus support (David Brownell) * 2000/earlier:  fixes for NEC/Lucent chips; suspend/resume handling *	when the controller loses power; handle UE; cleanup; ... * * v5.2 1999/12/07 URB 3rd preview,  * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi) * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume  * 	i386: HUB, Keyboard, Mouse, Printer  * * v4.3 1999/10/27 multiple HCs, bulk_request * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl. * v4.0 1999/08/18  * v3.0 1999/06/25  * v2.1 1999/05/09  code clean up * v2.0 1999/05/04  * v1.0 1999/04/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/malloc.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 */// #define OHCI_MEM_SLAB// #define OHCI_MEM_FLAGS	SLAB_POISON	/* no redzones; see mm/slab.c */#include "usb-ohci.h"#ifdef CONFIG_PMAC_PBOOK/* All this PMAC_PBOOK stuff should disappear when those * platforms fully support the 2.4 kernel PCI APIs. */#include <linux/adb.h>#include <linux/pmu.h>#ifndef CONFIG_PM#define CONFIG_PM#endif#endif/* 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;/*-------------------------------------------------------------------------* * 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;	for (i = 0; i < urb_priv->length; i++) {		if (urb_priv->td [i]) {			td_free (hc, urb_priv->td [i]);		}	}	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;		/* 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;			}		}		urb_free_priv ((struct ohci *)urb->dev->bus, urb_priv);		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 || !urb->dev->bus) {		dbg("%s URB: no dev", str);		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;	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 = (ed_t *) bus_to_virt(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 "?";}// dump control and status registersstatic void ohci_dump_status (ohci_t *controller){	struct ohci_regs	*regs = controller->regs;	__u32			temp;	temp = readl (&regs->revision) & 0xff;	if (temp != 0x10)		dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));	temp = readl (&regs->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 (&regs->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 (&regs->intrstatus));	ohci_dump_intr_mask ("intrenable", readl (&regs->intrenable));	// intrdisable always same as intrenable	// ohci_dump_intr_mask ("intrdisable", readl (&regs->intrdisable));	maybe_print_eds ("ed_periodcurrent", readl (&regs->ed_periodcurrent));	maybe_print_eds ("ed_controlhead", readl (&regs->ed_controlhead));	maybe_print_eds ("ed_controlcurrent", readl (&regs->ed_controlcurrent));	maybe_print_eds ("ed_bulkhead", readl (&regs->ed_bulkhead));	maybe_print_eds ("ed_bulkcurrent", readl (&regs->ed_bulkcurrent));	maybe_print_eds ("donehead", readl (&regs->donehead));}static void ohci_dump_roothub (ohci_t *controller, int verbose){	struct ohci_regs	*regs = controller->regs;	__u32			temp, ndp, i;	temp = readl (&regs->roothub.a);	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 = readl (&regs->roothub.b);		dbg ("roothub.b: %08x PPCM=%04x DR=%04x",			temp,			(temp & RH_B_PPCM) >> 16,			(temp & RH_B_DR)			);		temp = readl (&regs->roothub.status);		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 = readl (&regs->roothub.portstatus [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" : "",			(temp & RH_PS_PES) ? " PES" : "",			(temp & RH_PS_CCS) ? " CCS" : ""			);	}}static void ohci_dump (ohci_t *controller, int verbose){	dbg ("OHCI controller usb-%s state", controller->ohci_dev->slot_name);	// dumps some of the state we know about	ohci_dump_status (controller);	if (verbose)		ep_print_int_eds (controller, "hcca");	dbg ("hcca frame #%04x", controller->hcca.frame_no);	ohci_dump_roothub (controller, 1);}#endif/*-------------------------------------------------------------------------* * Interface functions (URB) *-------------------------------------------------------------------------*//* return a request to the completion handler */ static int sohci_return_urb (urb_t * urb){	urb_priv_t * urb_priv = urb->hcpriv;	urb_t * urbt;	unsigned long flags;	int i;		if (!urb_priv)		return -1; /* urb already unlinked */	/* just to be sure */	if (!urb->complete) {		urb_rm_priv (urb);		return -1;	}	#ifdef DEBUG	urb_print (urb, "RET", usb_pipeout (urb->pipe));#endif	switch (usb_pipetype (urb->pipe)) {  		case PIPE_INTERRUPT:			urb->complete (urb); /* call complete and requeue URB */	  			urb->actual_length = 0;  			urb->status = USB_ST_URB_PENDING;  			if (urb_priv->state != URB_DEL)  				td_submit_urb (urb);  			break;  					case PIPE_ISOCHRONOUS:			for (urbt = urb->next; urbt && (urbt != urb); urbt = urbt->next);			if (urbt) { /* send the reply and requeue URB */					urb->complete (urb);								spin_lock_irqsave (&usb_ed_lock, flags);				urb->actual_length = 0;  				urb->status = USB_ST_URB_PENDING;  				urb->start_frame = urb_priv->ed->last_iso + 1;  				if (urb_priv->state != URB_DEL) {  					for (i = 0; i < urb->number_of_packets; i++) {  						urb->iso_frame_desc[i].actual_length = 0;  						urb->iso_frame_desc[i].status = -EXDEV;  					}  					td_submit_urb (urb);  				}  				spin_unlock_irqrestore (&usb_ed_lock, flags);  				  			} else { /* unlink URB, call complete */				urb_rm_priv (urb);				urb->complete (urb); 				}					break;  						case PIPE_BULK:		case PIPE_CONTROL: /* unlink URB, call complete */			urb_rm_priv (urb);			urb->complete (urb);				break;	}	return 0;}/*-------------------------------------------------------------------------*//* get a transfer request */ static int sohci_submit_urb (urb_t * urb){	ohci_t * ohci;	ed_t * ed;	urb_priv_t * urb_priv;	unsigned int pipe = urb->pipe;	int i, size = 0;	unsigned long flags;	int bustime = 0;		if (!urb->dev || !urb->dev->bus)		return -ENODEV;		if (urb->hcpriv)			/* urb already in use */		return -EINVAL;//	if(usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe))) //		return -EPIPE;		usb_inc_dev_use (urb->dev);	ohci = (ohci_t *) urb->dev->bus->hcpriv;	#ifdef DEBUG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -