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

📄 usb-uhci.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
	status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |		(urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |		(((urb->transfer_buffer_length - 1) & 0x7ff) << 21);	info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);	fill_td (td, status, info, virt_to_bus (urb->transfer_buffer));	list_add_tail (&td->desc_list, &urb_priv->desc_list);	urb->status = -EINPROGRESS;	queue_urb (s, urb);	insert_td_horizontal (s, s->int_chain[nint], td);	// store in INT-TDs	usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));	return 0;}/*-------------------------------------------------------------------*/_static int uhci_submit_iso_urb (urb_t *urb){	uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;	urb_priv_t *urb_priv = urb->hcpriv;#ifdef ISO_SANITY_CHECK	int pipe=urb->pipe;	int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));#endif	int n, ret, last=0;	uhci_desc_t *td, **tdm;	int status, destination;	unsigned long flags;	__save_flags(flags);	__cli();		      // Disable IRQs to schedule all ISO-TDs in time	ret = iso_find_start (urb);	// adjusts urb->start_frame for later use		if (ret)		goto err;	tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);	if (!tdm) {		ret = -ENOMEM;		goto err;	}	// First try to get all TDs	for (n = 0; n < urb->number_of_packets; n++) {		dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);		if (!urb->iso_frame_desc[n].length) {			// allows ISO striping by setting length to zero in iso_descriptor			tdm[n] = 0;			continue;		}#ifdef ISO_SANITY_CHECK		if(urb->iso_frame_desc[n].length > maxsze) {			err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);			tdm[n] = 0;			ret=-EINVAL;				}		else#endif		ret = alloc_td (&td, UHCI_PTR_DEPTH);		if (ret) {			int i;	// Cleanup allocated TDs			for (i = 0; i < n; n++)				if (tdm[i])					 delete_desc(tdm[i]);			kfree (tdm);			goto err;		}		last=n;		tdm[n] = td;	}	status = TD_CTRL_ACTIVE | TD_CTRL_IOS;	//| (urb->transfer_flags&USB_DISABLE_SPD?0:TD_CTRL_SPD);	destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);		// Queue all allocated TDs	for (n = 0; n < urb->number_of_packets; n++) {		td = tdm[n];		if (!td)			continue;					if (n  == last)			status |= TD_CTRL_IOC;		fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),			 virt_to_bus (urb->transfer_buffer + urb->iso_frame_desc[n].offset));		list_add_tail (&td->desc_list, &urb_priv->desc_list);			if (n == last) {			urb->status = -EINPROGRESS;			queue_urb (s, urb);		}		insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td);	// store in iso-tds		//uhci_show_td(td);	}	kfree (tdm);	dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);	ret = 0;      err:	__restore_flags(flags);	return ret;}/*-------------------------------------------------------------------*/// returns: 0 (no transfer queued), urb* (this urb already queued) _static urb_t* search_dev_ep (uhci_t *s, urb_t *urb){	struct list_head *p;	urb_t *tmp;	unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);	dbg("search_dev_ep:");	p=s->urb_list.next;	for (; p != &s->urb_list; p = p->next) {		tmp = list_entry (p, urb_t, urb_list);		dbg("urb: %p", tmp);		// we can accept this urb if it is not queued at this time 		// or if non-iso transfer requests should be scheduled for the same device and pipe		if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||		    (urb == tmp)) {			return tmp;	// found another urb already queued for processing		}	}	return 0;}/*-------------------------------------------------------------------*/_static int uhci_submit_urb (urb_t *urb){	uhci_t *s;	urb_priv_t *urb_priv;	int ret = 0;	unsigned long flags;	urb_t *queued_urb=NULL;	int bustime;			if (!urb->dev || !urb->dev->bus)		return -ENODEV;	s = (uhci_t*) urb->dev->bus->hcpriv;	//dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));		if (!s->running)		return -ENODEV;			if (usb_pipedevice (urb->pipe) == s->rh.devnum)		return rh_submit_urb (urb);	/* virtual root hub */	usb_inc_dev_use (urb->dev);	spin_lock_irqsave (&s->urb_list_lock, flags);	queued_urb = search_dev_ep (s, urb); // returns already queued urb for that pipe	if (queued_urb) {		queue_dbg("found bulk urb %p\n", queued_urb);		if ((usb_pipetype (urb->pipe) != PIPE_BULK) ||		    ((usb_pipetype (urb->pipe) == PIPE_BULK) &&		     (!(urb->transfer_flags & USB_QUEUE_BULK) || !(queued_urb->transfer_flags & USB_QUEUE_BULK)))) {			spin_unlock_irqrestore (&s->urb_list_lock, flags);			usb_dec_dev_use (urb->dev);			err("ENXIO %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,queued_urb);			return -ENXIO;	// urb already queued		}	}#ifdef DEBUG_SLAB	urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);#else	urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);#endif	if (!urb_priv) {		usb_dec_dev_use (urb->dev);		spin_unlock_irqrestore (&s->urb_list_lock, flags);		return -ENOMEM;	}	urb->hcpriv = urb_priv;	INIT_LIST_HEAD (&urb_priv->desc_list);	urb_priv->flags = 0;	dbg("submit_urb: scheduling %p", urb);	urb_priv->next_queued_urb = NULL;	urb_priv->prev_queued_urb = NULL;	urb_priv->bottom_qh = NULL;	urb_priv->next_qh = NULL;		if (usb_pipetype (urb->pipe) == 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 (&s->avoid_bulk);		ret = uhci_submit_bulk_urb (urb, queued_urb);		atomic_dec (&s->avoid_bulk);		spin_unlock_irqrestore (&s->urb_list_lock, flags);	}	else {		spin_unlock_irqrestore (&s->urb_list_lock, flags);		switch (usb_pipetype (urb->pipe)) {		case PIPE_ISOCHRONOUS:						if (urb->bandwidth == 0) {      /* not yet checked/allocated */				if (urb->number_of_packets <= 0) {					ret = -EINVAL;					break;				}				bustime = usb_check_bandwidth (urb->dev, urb);				if (bustime < 0) {					ret = bustime;					break;				}				ret = uhci_submit_iso_urb(urb);				if (ret == 0)					usb_claim_bandwidth (urb->dev, urb, bustime, 1);			} else {        /* bandwidth is already set */				ret = uhci_submit_iso_urb(urb);			}			break;		case PIPE_INTERRUPT:			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(urb);					if (ret == 0)						usb_claim_bandwidth (urb->dev, urb, bustime, 0);				}			} else {        /* bandwidth is already set */				ret = uhci_submit_int_urb(urb);			}			break;		case PIPE_CONTROL:			ret = uhci_submit_control_urb (urb);			break;		default:			ret = -EINVAL;		}	}	dbg("submit_urb: scheduled with ret: %d", ret);		if (ret != 0) {		usb_dec_dev_use (urb->dev);#ifdef DEBUG_SLAB		kmem_cache_free(urb_priv_kmem, urb_priv);#else		kfree (urb_priv);#endif		return ret;	}	return 0;}// Checks for URB timeout and removes bandwidth reclamation // if URB idles too long_static void uhci_check_timeouts(uhci_t *s){	struct list_head *p,*p2;	urb_t *urb;	int type;		p = s->urb_list.prev;		while (p != &s->urb_list) {		urb_priv_t *hcpriv;		p2 = p;		p = p->prev;		urb = list_entry (p2, urb_t, urb_list);		type = usb_pipetype (urb->pipe);		hcpriv = (urb_priv_t*)urb->hcpriv;						if ( urb->timeout && 			((hcpriv->started + urb->timeout) < jiffies)) {			urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;			async_dbg("uhci_check_timeout: timeout for %p",urb);			uhci_unlink_urb_async(s, urb);		}#ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH		else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&  		     (hcpriv->use_loop) &&		     ((hcpriv->started + IDLE_TIMEOUT) < jiffies))			disable_desc_loop(s, urb);#endif	}	s->timeout_check=jiffies;}/*------------------------------------------------------------------- Virtual Root Hub -------------------------------------------------------------------*/_static __u8 root_hub_dev_des[] ={	0x12,			/*  __u8  bLength; */	0x01,			/*  __u8  bDescriptorType; Device */	0x00,			/*  __u16 bcdUSB; v1.0 */	0x01,	0x09,			/*  __u8  bDeviceClass; HUB_CLASSCODE */	0x00,			/*  __u8  bDeviceSubClass; */	0x00,			/*  __u8  bDeviceProtocol; */	0x08,			/*  __u8  bMaxPacketSize0; 8 Bytes */	0x00,			/*  __u16 idVendor; */	0x00,	0x00,			/*  __u16 idProduct; */	0x00,	0x00,			/*  __u16 bcdDevice; */	0x00,	0x00,			/*  __u8  iManufacturer; */	0x02,			/*  __u8  iProduct; */	0x01,			/*  __u8  iSerialNumber; */	0x01			/*  __u8  bNumConfigurations; */};/* Configuration descriptor */_static __u8 root_hub_config_des[] ={	0x09,			/*  __u8  bLength; */	0x02,			/*  __u8  bDescriptorType; Configuration */	0x19,			/*  __u16 wTotalLength; */	0x00,	0x01,			/*  __u8  bNumInterfaces; */	0x01,			/*  __u8  bConfigurationValue; */	0x00,			/*  __u8  iConfiguration; */	0x40,			/*  __u8  bmAttributes; 				   Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */	0x00,			/*  __u8  MaxPower; */     /* interface */	0x09,			/*  __u8  if_bLength; */	0x04,			/*  __u8  if_bDescriptorType; Interface */	0x00,			/*  __u8  if_bInterfaceNumber; */	0x00,			/*  __u8  if_bAlternateSetting; */	0x01,			/*  __u8  if_bNumEndpoints; */	0x09,			/*  __u8  if_bInterfaceClass; HUB_CLASSCODE */	0x00,			/*  __u8  if_bInterfaceSubClass; */	0x00,			/*  __u8  if_bInterfaceProtocol; */	0x00,			/*  __u8  if_iInterface; */     /* endpoint */	0x07,			/*  __u8  ep_bLength; */	0x05,			/*  __u8  ep_bDescriptorType; Endpoint */	0x81,			/*  __u8  ep_bEndpointAddress; IN Endpoint 1 */	0x03,			/*  __u8  ep_bmAttributes; Interrupt */	0x08,			/*  __u16 ep_wMaxPacketSize; 8 Bytes */	0x00,	0xff			/*  __u8  ep_bInterval; 255 ms */};_static __u8 root_hub_hub_des[] ={	0x09,			/*  __u8  bLength; */	0x29,			/*  __u8  bDescriptorType; Hub-descriptor */	0x02,			/*  __u8  bNbrPorts; */	0x00,			/* __u16  wHubCharacteristics; */	0x00,	0x01,			/*  __u8  bPwrOn2pwrGood; 2ms */	0x00,			/*  __u8  bHubContrCurrent; 0 mA */	0x00,			/*  __u8  DeviceRemovable; *** 7 Ports max *** */	0xff			/*  __u8  PortPwrCtrlMask; *** 7 ports max *** */};/*-------------------------------------------------------------------------*//* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */_static int rh_send_irq (urb_t *urb){	int len = 1;	int i;	uhci_t *uhci = urb->dev->bus->hcpriv;	unsigned int io_addr = uhci->io_addr;	__u16 data = 0;	for (i = 0; i < uhci->rh.numports; i++) {		data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);		len = (i + 1) / 8 + 1;	}	*(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);	urb->actual_length = len;	urb->status = 0;		if ((data > 0) && (uhci->rh.send != 0)) {		dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",		     inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);		urb->complete (urb);	}	return 0;}/*-------------------------------------------------------------------------*//* Virtual Root Hub INTs are polled by this timer every "intervall" ms */_static int rh_init_int_timer (urb_t *urb);_static void rh_int_timer_do (unsigned long ptr){	int len;	urb_t *urb = (urb_t*) ptr;	uhci_t *uhci = urb->dev->bus->hcpriv;	if (uhci->rh.send) {		len = rh_send_irq (urb);		if (len > 0) {			urb->actual_length = len;			if (urb->complete)				urb->complete (urb);		}	}	rh_init_int_timer (urb);}/*-------------------------------------------------------------------------*//* Root Hub INTs are polled by this timer, polling interval 20ms *//* This time is also used for URB-timeout checking */_static int rh_init_int_timer (urb_t *urb){	uhci_t *uhci = urb->dev->bus->hcpriv;	uhci->rh.interval = urb->interval;	init_timer (&uhci->rh.rh_int_timer);	uhci->rh.rh_int_timer.function = rh_int_timer_do;	uhci->rh.rh_int_timer.data = (unsigned long) urb;	uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;	add_timer (&uhci->rh.rh_int_timer);	return 0;}/*-------------------------------------------------------------------------*/#define OK(x) 			len = (x); break#define CLR_RH_PORTSTAT(x) \		status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \		status = (status & 0xfff5) & ~(x); \		outw(status, io_addr+USBPORTSC1+2*(wIndex-1))#define SET_RH_PORTSTAT(x) \		status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); \		status = (status & 0xfff5) | (x); \		outw(status, io_addr+USBPORTSC1+2*(wIndex-1))/*-------------------------------------------------------------------------*//**** ** Root Hub Control Pipe *************************/_static int rh_submit_urb (urb_t *urb){	struct usb_device *usb_dev = urb->dev;	uhci_t *uhci = usb_dev->bus->hcpriv;	unsigned int pipe = urb->pipe;	devrequest *cmd = (devrequest *) urb->setup_packet;

⌨️ 快捷键说明

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