pnp.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 651 行 · 第 1/2 页

C
651
字号
    /*     * Here should really perform the range check, and     * return a failure if not successful.     */    pnp_write (IO_RANGE_CHECK, 0);    DELAY(1000); /* XXX is it really necessary ? */    pnp_write (ACTIVATE, d->enable ? 1 : 0);    DELAY(1000); /* XXX is it really necessary ? */    return 1 ;}/* * To finalize a card's initialization, and before accessing its * registers, we need to bring the card in WaitForKey. To this purpose, * we need to issue a WaitForKey command, which brings _all_ cards * in that state. So, before configuring the next board, we must also * sent the Init-Key to bring cards to the SLEEP state again. * * In fact, one could hope that cards respond to normal I/O accesses * even in the SLEEP state, which could be done by issuing a WAKE[0]. * This seems to work on the CS4236, but not on the CS4232 on my Zappa * motherboard . */intenable_pnp_card(){    /* the next wake should bring the card in WaitForKey ? */    pnp_write (WAKE, 0);    pnp_write(CONFIG_CONTROL, 0x02);	/* All cards in WaitForKey */    DELAY(1000); /* XXX is it really necessary ? */    return 1 ; /* success */}/* * Configure PnP devices. pnp_id is made of: *	4 bytes: board id (which can be printed as an ascii string); *	4 bytes: board serial number (often 0 or -1 ?) */static voidconfig_pnp_device(pnp_id *p, int csn){    static struct pnp_dlist_node *nod = NULL;    int i;    u_char *data = (u_char *)p;    u_char *comp = (u_char *)&p->comp_id;    /* these are for autoconfigure a-la pci */    struct pnp_device *dvp, **dvpp;    char *name = NULL;    printf("CSN %d Vendor ID: %c%c%c%02x%02x [0x%08lx] Serial 0x%08lx Comp ID: %c%c%c%02x%02x [0x%08lx]\n",	csn,	((data[0] & 0x7c) >> 2) + '@',	(((data[0] & 0x03) << 3) | ((data[1] & 0xe0) >> 5)) + '@',	(data[1] & 0x1f) + '@', data[2], data[3],	p->vendor_id, p->serial,	((comp[0] & 0x7c) >> 2) + '@',	(((comp[0] & 0x03) << 3) | ((comp[1] & 0xe0) >> 5)) + '@',	(comp[1] & 0x1f) + '@', comp[2], comp[3],	p->comp_id);    doing_pnp_probe = 1 ;    current_csn = csn ;    current_pnp_id = p->vendor_id ;    current_pnp_serial = p->serial ;    /*     * use kernel table to override possible devices     */    for (i = 0 ; i < MAX_PNP_LDN; i++) {	if (pnp_ldn_overrides[i].csn == csn &&		pnp_ldn_overrides[i].override == 1) {	    struct pnp_cinfo d;	    if (bootverbose)		printf("PnP: override config for CSN %d LDN %d "		    "vend_id 0x%08x\n", csn, pnp_ldn_overrides[i].ldn,		    current_pnp_id);	    /* next assignement is done otherwise read fails */	    d.vendor_id = current_pnp_id ;	    read_pnp_parms(&d, pnp_ldn_overrides[i].ldn);	    if (pnp_ldn_overrides[i].enable == 0) {		/* just disable ... */		d.enable = 0;		write_pnp_parms(&d, pnp_ldn_overrides[i].ldn);	    } else {		/* set all parameters */		/* next assignement is done otherwise write fails */		pnp_ldn_overrides[i].vendor_id = current_pnp_id ;		write_pnp_parms(&pnp_ldn_overrides[i],		    pnp_ldn_overrides[i].ldn);	    }	}    }    /* lookup device in ioconfiguration */    dvpp = (struct pnp_device **)pnpdevice_set.ls_items;    while ((dvp = *dvpp++)) {	if (dvp->pd_probe) {	    if ( ((name = (*dvp->pd_probe)(csn, p->vendor_id)) && *name) ||		(p->comp_id &&		    (name = (*dvp->pd_probe)(csn, p->comp_id))))		break;	}    }    if (dvp && name && *name && dvp->pd_count) { /* found a matching device */	int unit ;	/* pnpcb->pnpcb_seen |= ( 1ul << csn ) ; */	/* get and increment the unit */	unit = (*dvp->pd_count)++;	/*	 * now call the attach routine. The board has not been	 * configured yet, so better not access isa registers in	 * the attach routine until enable_pnp_card() has been done.	 */		if (nod == NULL)		nod = malloc(sizeof(struct pnp_dlist_node), M_DEVBUF, M_NOWAIT);	if (nod == NULL)		panic("malloc failed for PnP resource use");	bzero(nod, sizeof(*nod));	nod->pnp = dvp;	nod->dev.id_unit = unit ;	if (dvp->pd_attach)	    (*dvp->pd_attach) (csn, p->vendor_id, name, &(nod->dev));	printf("%s%d (%s <%s> sn 0x%08lx)", nod->dev.id_driver &&	    nod->dev.id_driver->name ? nod->dev.id_driver->name : "unknown",	    unit, dvp->pd_name, name, p->serial);	if (nod->dev.id_alive) {	    if (nod->dev.id_irq != 0 && nod->dev.id_intr != NULL) {		/* the board uses interrupts. Register it. */		if (dvp->imask)		    INTRMASK( *(dvp->imask), nod->dev.id_irq );		register_intr(ffs(nod->dev.id_irq) - 1, nod->dev.id_id,		    nod->dev.id_ri_flags, nod->dev.id_intr,		    dvp->imask, nod->dev.id_unit);		INTREN(nod->dev.id_irq);	    }	    if (nod->dev.id_alive != 0) {	        if (nod->dev.id_iobase == -1) 		    printf(" at ?");		else {		    printf(" at 0x%x", nod->dev.id_iobase);		    if ((nod->dev.id_iobase + nod->dev.id_alive -1) !=			nod->dev.id_iobase) {			printf("-0x%x", nod->dev.id_iobase + nod->dev.id_alive			    - 1);		    }		}	    }	    if (nod->dev.id_irq)		printf(" irq %d", ffs(nod->dev.id_irq) - 1);	    if (nod->dev.id_drq != -1)		printf(" drq %d", nod->dev.id_drq);	    if (nod->dev.id_maddr)		printf(" maddr 0x%lx", kvtop(nod->dev.id_maddr));	    if (nod->dev.id_msize)		printf(" msize %d", nod->dev.id_msize);	    if (nod->dev.id_flags)		printf(" flags 0x%x", nod->dev.id_flags);	    if (nod->dev.id_iobase && !(nod->dev.id_iobase & 0xf300)) {		printf(" on motherboard");		printf(" id %d", nod->dev.id_id);	    } else if (nod->dev.id_iobase >= 0x1000 &&		!(nod->dev.id_iobase & 0x300)) {		printf (" on eisa slot %d",		    nod->dev.id_iobase >> 12);	    } else {		printf (" on isa");	    }	    printf("\n");	    if (pnp_device_list_last_ptr == NULL)		pnp_device_list = nod;	    else	        *pnp_device_list_last_ptr = nod;	    pnp_device_list_last_ptr = &(nod->next);	    nod = NULL;	} else	    printf(" failed to attach\n");    }    doing_pnp_probe = 0 ;}/* * Scan Resource Data for Compatible Device ID. * * This function exits as soon as it gets a Compatible Device ID, an error * reading *ANY* Resource Data or ir reaches the end of Resource Data. * In the first case the return value will be TRUE, FALSE otherwise. */static intpnp_scan_resdata(pnp_id *p, int csn){    u_char tag, resinfo[8];    int large_len, scanning = 1024, retval = FALSE;    while (scanning-- > 0 && pnp_get_resource_info(&tag, 1)) {	if (PNP_RES_TYPE(tag) == 0) {	    /* Small resource */	    switch (PNP_SRES_NUM(tag)) {		case COMP_DEVICE_ID:		    /* Got a compatible device id resource */		    if (pnp_get_resource_info(resinfo, PNP_SRES_LEN(tag))) {		        bcopy(resinfo, &p->comp_id, 4);			retval = TRUE;			if (bootverbose)			    printf("PnP: CSN %d COMP_DEVICE_ID = 0x%08lx\n", csn, p->comp_id);		    }		    /*		     * We found what we were looking for, or got an error from		     * pnp_get_resource, => stop scanning (FALLTHROUGH)		     */		case END_TAG:		    scanning = 0;		    break;		default:		    /* Skip this resource */		    if (pnp_get_resource_info(NULL, PNP_SRES_LEN(tag)) == 0)			scanning = 0;		    break;	    }	} else	    /* Large resource, skip it */	    if (!(pnp_get_resource_info((u_char *)&large_len, 2) && pnp_get_resource_info(NULL, large_len)))		scanning = 0;    }    return retval;}/* * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port * value (caller should try multiple READ_DATA locations before giving * up). Upon exiting, all cards are aware that they should use * pnp_rd_port as the READ_DATA port. * * In the first pass, a csn is assigned to each board and pnp_id's * are saved to an array, pnp_devices. In the second pass, each * card is woken up and the device configuration is called. */static intpnp_isolation_protocol(){    int csn;    pnp_send_Initiation_LFSR();    pnp_write(CONFIG_CONTROL, 0x04);	/* Reset CSN for All Cards */    for (csn = 1; (csn < MAX_PNP_CARDS); csn++) {	/* Wake up cards without a CSN */	pnp_write(WAKE, 0);	pnp_write(SET_RD_DATA, pnp_rd_port);	outb(_PNP_ADDRESS, SERIAL_ISOLATION);	DELAY(1000);	/* Delay 1 msec */	if (pnp_get_serial( &(pnp_devices[csn-1]) ) ) {	    pnp_write(SET_CSN, csn);	    /* pnp_write(CONFIG_CONTROL, 2); */	    if (!pnp_scan_resdata(&(pnp_devices[csn-1]), csn))		pnp_devices[csn-1].comp_id = NULL;	} else	    break;    }    num_pnp_cards = csn - 1;    for (csn = 1; csn <= num_pnp_cards ; csn++) {	/*	 * make sure cards are in SLEEP state	 */	pnp_send_Initiation_LFSR();	pnp_write(WAKE, csn);	config_pnp_device( &(pnp_devices[csn-1]), csn);	/*	 * Put all cards in WaitForKey, just in case the previous	 * attach routine forgot it.	 */	pnp_write(CONFIG_CONTROL, 0x02);	DELAY(1000); /* XXX is it really necessary ? */    }    return num_pnp_cards ;}/* * pnp_configure() * * autoconfiguration of pnp devices. This routine just runs the * isolation protocol over several ports, until one is successful. * * may be called more than once ? * */voidpnp_configure(){    int num_pnp_devs;    if (pnp_ldn_overrides[0].csn == 0) {	if (bootverbose)	    printf("Initializing PnP override table\n");	bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));        pnp_ldn_overrides[0].csn = 255 ;    }    printf("Probing for PnP devices:\n");    /* Try various READ_DATA ports from 0x203-0x3ff */    for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {	if (bootverbose)	    printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);	num_pnp_devs = pnp_isolation_protocol();	if (num_pnp_devs)	    break;    }    if (!num_pnp_devs) {	if (bootverbose)	    printf("No Plug-n-Play devices were found\n");	return;    }}

⌨️ 快捷键说明

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