📄 usb_uhci.c
字号:
/* * Part of this code has been derived from linux: * Universal Host Controller Interface driver for USB (take II). * * (c) 1999-2001 Georg Acher, acher@in.tum.de (executive slave) (base guitar) * Deti Fliegl, deti@fliegl.de (executive slave) (lead voice) * Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader) * Roman Weissgaerber, weissg@vienna.at (virt root hub) (studio porter) * (c) 2000 Yggdrasil Computing, Inc. (port of new PCI interface support * from usb-ohci.c by Adam Richter, adam@yggdrasil.com). * (C) 2000 David Brownell, david-b@pacbell.net (usb-ohci.c) * * HW-initalization based on material of * * (C) Copyright 1999 Linus Torvalds * (C) Copyright 1999 Johannes Erdfelt * (C) Copyright 1999 Randy Dunlap * (C) Copyright 1999 Gregory P. Smith * * * Adapted for U-Boot: * (C) Copyright 2001 Denis Peter, MPL AG Switzerland * * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * *//********************************************************************** * How it works: * ------------- * The framelist / Transfer descriptor / Queue Heads are similar like * in the linux usb_uhci.c. * * During initialization, the following skeleton is allocated in init_skel: * * framespecific | common chain * * framelist[] * [ 0 ]-----> TD ---------\ * [ 1 ]-----> TD ----------> TD ------> QH -------> QH -------> QH ---> NULL * ... TD ---------/ * [1023]-----> TD --------/ * * ^^ ^^ ^^ ^^ ^^ * 7 TDs for 1 TD for Start of Start of End Chain * INT (2-128ms) 1ms-INT CTRL Chain BULK Chain * * * Since this is a bootloader, the isochronous transfer descriptor have been removed. * * Interrupt Transfers. * -------------------- * For Interupt transfers USB_MAX_TEMP_INT_TD Transfer descriptor are available. They * will be inserted after the appropriate (depending the interval setting) skeleton TD. * If an interrupt has been detected the dev->irqhandler is called. The status and number * of transfered bytes is stored in dev->irq_status resp. dev->irq_act_len. If the * dev->irqhandler returns 0, the interrupt TD is removed and disabled. If an 1 is returned, * the interrupt TD will be reactivated. * * Control Transfers * ----------------- * Control Transfers are issued by filling the tmp_td with the appropriate data and connect * them to the qh_cntrl queue header. Before other control/bulk transfers can be issued, * the programm has to wait for completion. This does not allows asynchronous data transfer. * * Bulk Transfers * -------------- * Bulk Transfers are issued by filling the tmp_td with the appropriate data and connect * them to the qh_bulk queue header. Before other control/bulk transfers can be issued, * the programm has to wait for completion. This does not allows asynchronous data transfer. * * */#include <common.h>#include <pci.h>#ifdef CONFIG_USB_UHCI#include <usb.h>#include "usb_uhci.h"#define USB_MAX_TEMP_TD 128 /* number of temporary TDs for bulk and control transfers */#define USB_MAX_TEMP_INT_TD 32 /* number of temporary TDs for Interrupt transfers */#undef USB_UHCI_DEBUG#ifdef USB_UHCI_DEBUG#define USB_UHCI_PRINTF(fmt,args...) printf (fmt ,##args)#else#define USB_UHCI_PRINTF(fmt,args...)#endifstatic int irqvec = -1; /* irq vector, if -1 uhci is stopped / reseted */unsigned int usb_base_addr; /* base address */static uhci_td_t td_int[8]; /* Interrupt Transfer descriptors */static uhci_qh_t qh_cntrl; /* control Queue Head */static uhci_qh_t qh_bulk; /* bulk Queue Head */static uhci_qh_t qh_end; /* end Queue Head */static uhci_td_t td_last; /* last TD (linked with end chain) *//* temporary tds */static uhci_td_t tmp_td[USB_MAX_TEMP_TD]; /* temporary bulk/control td's */static uhci_td_t tmp_int_td[USB_MAX_TEMP_INT_TD]; /* temporary interrupt td's */static unsigned long framelist[1024] __attribute__ ((aligned (0x1000))); /* frame list */static struct virt_root_hub rh; /* struct for root hub *//********************************************************************** * some forward decleration */int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe, void *buffer, int transfer_len,struct devrequest *setup);/* fill a td with the approproiate data. Link, status, info and buffer * are used by the USB controller itselfes, dev is used to identify the * "connected" device */void usb_fill_td(uhci_td_t* td,unsigned long link,unsigned long status, unsigned long info, unsigned long buffer, unsigned long dev){ td->link=swap_32(link); td->status=swap_32(status); td->info=swap_32(info); td->buffer=swap_32(buffer); td->dev_ptr=dev;}/* fill a qh with the approproiate data. Head and element are used by the USB controller * itselfes. As soon as a valid dev_ptr is filled, a td chain is connected to the qh. * Please note, that after completion of the td chain, the entry element is removed / * marked invalid by the USB controller. */void usb_fill_qh(uhci_qh_t* qh,unsigned long head,unsigned long element){ qh->head=swap_32(head); qh->element=swap_32(element); qh->dev_ptr=0L;}/* get the status of a td->status */unsigned long usb_uhci_td_stat(unsigned long status){ unsigned long result=0; result |= (status & TD_CTRL_NAK) ? USB_ST_NAK_REC : 0; result |= (status & TD_CTRL_STALLED) ? USB_ST_STALLED : 0; result |= (status & TD_CTRL_DBUFERR) ? USB_ST_BUF_ERR : 0; result |= (status & TD_CTRL_BABBLE) ? USB_ST_BABBLE_DET : 0; result |= (status & TD_CTRL_CRCTIMEO) ? USB_ST_CRC_ERR : 0; result |= (status & TD_CTRL_BITSTUFF) ? USB_ST_BIT_ERR : 0; result |= (status & TD_CTRL_ACTIVE) ? USB_ST_NOT_PROC : 0; return result;}/* get the status and the transfered len of a td chain. * called from the completion handler */int usb_get_td_status(uhci_td_t *td,struct usb_device *dev){ unsigned long temp,info; unsigned long stat; uhci_td_t *mytd=td; if(dev->devnum==rh.devnum) return 0; dev->act_len=0; stat=0; do { temp=swap_32((unsigned long)mytd->status); stat=usb_uhci_td_stat(temp); info=swap_32((unsigned long)mytd->info); if(((info & 0xff)!= USB_PID_SETUP) && (((info >> 21) & 0x7ff)!= 0x7ff) && (temp & 0x7FF)!=0x7ff) { /* if not setup and not null data pack */ dev->act_len+=(temp & 0x7FF) + 1; /* the transfered len is act_len + 1 */ } if(stat) { /* status no ok */ dev->status=stat; return -1; } temp=swap_32((unsigned long)mytd->link); mytd=(uhci_td_t *)(temp & 0xfffffff0); }while((temp & 0x1)==0); /* process all TDs */ dev->status=stat; return 0; /* Ok */}/*------------------------------------------------------------------- * LOW LEVEL STUFF * assembles QHs und TDs for control, bulk and iso *-------------------------------------------------------------------*//* Submits a control message. That is a Setup, Data and Status transfer. * Routine does not wait for completion. */int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, int transfer_len,struct devrequest *setup){ unsigned long destination, status; int maxsze = usb_maxpacket(dev, pipe); unsigned long dataptr; int len; int pktsze; int i=0; if (!maxsze) { USB_UHCI_PRINTF("uhci_submit_control_urb: pipesize for pipe %lx is zero\n", pipe); return -1; } if(((pipe>>8)&0x7f)==rh.devnum) { /* this is the root hub -> redirect it */ return uhci_submit_rh_msg(dev,pipe,buffer,transfer_len,setup); } USB_UHCI_PRINTF("uhci_submit_control start len %x, maxsize %x\n",transfer_len,maxsze); /* The "pipe" thing contains the destination in bits 8--18 */ destination = (pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; /* Setup stage */ /* 3 errors */ status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27); /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD); */ /* Build the TD for the control request, try forever, 8 bytes of data */ usb_fill_td(&tmp_td[i],UHCI_PTR_TERM ,status, destination | (7 << 21),(unsigned long)setup,(unsigned long)dev);#if 0 { char *sp=(char *)setup; printf("SETUP to pipe %lx: %x %x %x %x %x %x %x %x\n", pipe, sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]); }#endif dataptr = (unsigned long)buffer; len=transfer_len; /* If direction is "send", change the frame from SETUP (0x2D) to OUT (0xE1). Else change it from SETUP to IN (0x69). */ destination = (pipe & PIPE_DEVEP_MASK) | ((pipe & USB_DIR_IN)==0 ? USB_PID_OUT : USB_PID_IN); while (len > 0) { /* data stage */ pktsze = len; i++; if (pktsze > maxsze) pktsze = maxsze; destination ^= 1 << TD_TOKEN_TOGGLE; /* toggle DATA0/1 */ usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, destination | ((pktsze - 1) << 21),dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */ tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]); dataptr += pktsze; len -= pktsze; } /* Build the final TD for control status */ /* It's only IN if the pipe is out AND we aren't expecting data */ destination &= ~UHCI_PID; if (((pipe & USB_DIR_IN)==0) || (transfer_len == 0)) destination |= USB_PID_IN; else destination |= USB_PID_OUT; destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */ i++; status &=~TD_CTRL_SPD; /* no limit on errors on final packet , 0 bytes of data */ usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),0,(unsigned long)dev); tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]); /* queue status td */ /* usb_show_td(i+1);*/ USB_UHCI_PRINTF("uhci_submit_control end (%d tmp_tds used)\n",i); /* first mark the control QH element terminated */ qh_cntrl.element=0xffffffffL; /* set qh active */ qh_cntrl.dev_ptr=(unsigned long)dev; /* fill in tmp_td_chain */ qh_cntrl.element=swap_32((unsigned long)&tmp_td[0]); return 0;}/*------------------------------------------------------------------- * Prepare TDs for bulk transfers. */int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len){ unsigned long destination, status,info; unsigned long dataptr; int maxsze = usb_maxpacket(dev, pipe); int len; int i=0; if(transfer_len < 0) { printf("Negative transfer length in submit_bulk\n"); return -1; } if (!maxsze) return -1; /* The "pipe" thing contains the destination in bits 8--18. */ destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe); /* 3 errors */ status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27); /* ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27); */ /* Build the TDs for the bulk request */ len = transfer_len; dataptr = (unsigned long)buffer; do { int pktsze = len; if (pktsze > maxsze) pktsze = maxsze; /* pktsze bytes of data */ info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) | (usb_gettoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE); if((len-pktsze)==0) status |= TD_CTRL_IOC; /* last one generates INT */ usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, info,dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */ if(i>0) tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]); i++; dataptr += pktsze; len -= pktsze; usb_dotoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)); } while (len > 0); /* first mark the bulk QH element terminated */ qh_bulk.element=0xffffffffL; /* set qh active */ qh_bulk.dev_ptr=(unsigned long)dev; /* fill in tmp_td_chain */ qh_bulk.element=swap_32((unsigned long)&tmp_td[0]); return 0;}/* search a free interrupt td */uhci_td_t *uhci_alloc_int_td(void){ int i; for(i=0;i<USB_MAX_TEMP_INT_TD;i++) { if(tmp_int_td[i].dev_ptr==0) /* no device assigned -> free TD */ return &tmp_int_td[i]; } return NULL;}#if 0void uhci_show_temp_int_td(void){ int i; for(i=0;i<USB_MAX_TEMP_INT_TD;i++) { if((tmp_int_td[i].dev_ptr&0x01)!=0x1L) /* no device assigned -> free TD */ printf("temp_td %d is assigned to dev %lx\n",i,tmp_int_td[i].dev_ptr); } printf("all others temp_tds are free\n");}#endif/*------------------------------------------------------------------- * submits USB interrupt (ie. polling ;-) */int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len, int interval){ int nint, n; unsigned long status, destination; unsigned long info,tmp; uhci_td_t *mytd; if (interval < 0 || interval >= 256) return -1; if (interval == 0) nint = 0; else { for (nint = 0, n = 1; nint <= 8; nint++, n += n) /* round interval down to 2^n */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -