Home | History | Annotate | Line # | Download | only in isapnp
isapnpres.c revision 1.15
      1  1.15  christos /*	$NetBSD: isapnpres.c,v 1.15 2005/12/11 12:22:16 christos Exp $	*/
      2   1.1  christos 
      3   1.9  christos /*-
      4   1.9  christos  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5   1.9  christos  * All rights reserved.
      6   1.9  christos  *
      7   1.9  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.9  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19   1.1  christos  *    must display the following acknowledgement:
     20   1.9  christos  *        This product includes software developed by the NetBSD
     21   1.9  christos  *        Foundation, Inc. and its contributors.
     22   1.9  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.9  christos  *    contributors may be used to endorse or promote products derived
     24   1.9  christos  *    from this software without specific prior written permission.
     25   1.1  christos  *
     26   1.9  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.9  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.9  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.9  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.9  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.9  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.9  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.9  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.9  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.9  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.9  christos  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  christos  */
     38   1.1  christos 
     39   1.1  christos /*
     40   1.1  christos  * Resource parser for Plug and Play cards.
     41   1.1  christos  */
     42  1.11     lukem 
     43  1.11     lukem #include <sys/cdefs.h>
     44  1.15  christos __KERNEL_RCSID(0, "$NetBSD: isapnpres.c,v 1.15 2005/12/11 12:22:16 christos Exp $");
     45   1.1  christos 
     46   1.1  christos #include <sys/param.h>
     47   1.1  christos #include <sys/systm.h>
     48   1.1  christos #include <sys/device.h>
     49   1.1  christos #include <sys/malloc.h>
     50   1.1  christos 
     51   1.1  christos #include <machine/bus.h>
     52   1.1  christos 
     53   1.1  christos #include <dev/isa/isavar.h>
     54   1.1  christos 
     55   1.1  christos #include <dev/isapnp/isapnpreg.h>
     56   1.1  christos #include <dev/isapnp/isapnpvar.h>
     57   1.1  christos 
     58   1.1  christos 
     59  1.13     perry static int isapnp_wait_status(struct isapnp_softc *);
     60   1.4  christos static struct isapnp_attach_args *
     61  1.13     perry     isapnp_newdev(struct isapnp_attach_args *);
     62   1.4  christos static struct isapnp_attach_args *
     63  1.13     perry     isapnp_newconf(struct isapnp_attach_args *);
     64  1.13     perry static void isapnp_merge(struct isapnp_attach_args *,
     65  1.13     perry     const struct isapnp_attach_args *);
     66   1.4  christos static struct isapnp_attach_args *
     67  1.13     perry     isapnp_flatten(struct isapnp_attach_args *);
     68  1.13     perry static int isapnp_process_tag(u_char, u_char, u_char *,
     69   1.4  christos     struct isapnp_attach_args **, struct isapnp_attach_args **,
     70  1.13     perry     struct isapnp_attach_args **);
     71   1.1  christos 
     72   1.1  christos 
     73   1.1  christos /* isapnp_wait_status():
     74   1.1  christos  *	Wait for the next byte of resource data to become available
     75   1.1  christos  */
     76   1.1  christos static int
     77   1.1  christos isapnp_wait_status(sc)
     78   1.1  christos 	struct isapnp_softc *sc;
     79   1.1  christos {
     80   1.1  christos 	int i;
     81   1.1  christos 
     82   1.8     mikel 	/* wait up to 1 ms for each resource byte */
     83   1.1  christos 	for (i = 0; i < 10; i++) {
     84   1.1  christos 		if (isapnp_read_reg(sc, ISAPNP_STATUS) & 1)
     85   1.1  christos 			return 0;
     86   1.8     mikel 		DELAY(100);
     87   1.1  christos 	}
     88   1.1  christos 	return 1;
     89   1.1  christos }
     90   1.1  christos 
     91   1.4  christos 
     92   1.4  christos /* isapnp_newdev():
     93   1.4  christos  *	Add a new logical device to the current card; expand the configuration
     94   1.4  christos  *	resources of the current card if needed.
     95   1.4  christos  */
     96   1.4  christos static struct isapnp_attach_args *
     97   1.4  christos isapnp_newdev(card)
     98   1.4  christos 	struct isapnp_attach_args *card;
     99   1.4  christos {
    100   1.4  christos 	struct isapnp_attach_args *ipa, *dev = ISAPNP_MALLOC(sizeof(*dev));
    101   1.4  christos 
    102   1.4  christos 	memset(dev, 0, sizeof(*dev));
    103   1.4  christos 
    104   1.4  christos 	dev->ipa_pref = ISAPNP_DEP_ACCEPTABLE;
    105   1.4  christos 	memcpy(dev->ipa_devident, card->ipa_devident,
    106   1.4  christos 	    sizeof(card->ipa_devident));
    107   1.4  christos 
    108   1.4  christos 	if (card->ipa_child == NULL)
    109   1.4  christos 		card->ipa_child = dev;
    110   1.4  christos 	else {
    111  1.14     perry 		for (ipa = card->ipa_child; ipa->ipa_sibling != NULL;
    112   1.4  christos 		    ipa = ipa->ipa_sibling)
    113   1.4  christos 			continue;
    114   1.4  christos 		ipa->ipa_sibling = dev;
    115   1.4  christos 	}
    116   1.4  christos 
    117   1.4  christos 
    118   1.4  christos 	return dev;
    119   1.4  christos }
    120   1.4  christos 
    121   1.4  christos 
    122   1.4  christos /* isapnp_newconf():
    123   1.4  christos  *	Add a new alternate configuration to a logical device
    124   1.4  christos  */
    125   1.4  christos static struct isapnp_attach_args *
    126   1.4  christos isapnp_newconf(dev)
    127   1.4  christos 	struct isapnp_attach_args *dev;
    128   1.4  christos {
    129   1.4  christos 	struct isapnp_attach_args *ipa, *conf = ISAPNP_MALLOC(sizeof(*conf));
    130   1.4  christos 
    131   1.4  christos 	memset(conf, 0, sizeof(*conf));
    132   1.4  christos 
    133   1.4  christos 	memcpy(conf->ipa_devident, dev->ipa_devident,
    134   1.4  christos 	    sizeof(conf->ipa_devident));
    135   1.4  christos 	memcpy(conf->ipa_devlogic, dev->ipa_devlogic,
    136   1.4  christos 	    sizeof(conf->ipa_devlogic));
    137   1.7     mikel 	memcpy(conf->ipa_devcompat, dev->ipa_devcompat,
    138   1.7     mikel 	    sizeof(conf->ipa_devcompat));
    139   1.4  christos 	memcpy(conf->ipa_devclass, dev->ipa_devclass,
    140   1.4  christos 	    sizeof(conf->ipa_devclass));
    141   1.4  christos 
    142   1.4  christos 	if (dev->ipa_child == NULL)
    143   1.4  christos 		dev->ipa_child = conf;
    144   1.4  christos 	else {
    145   1.4  christos 		for (ipa = dev->ipa_child; ipa->ipa_sibling;
    146   1.4  christos 		    ipa = ipa->ipa_sibling)
    147   1.4  christos 			continue;
    148   1.4  christos 		ipa->ipa_sibling = conf;
    149   1.4  christos 	}
    150   1.4  christos 
    151   1.4  christos 	return conf;
    152   1.4  christos }
    153   1.4  christos 
    154   1.4  christos 
    155   1.4  christos /* isapnp_merge():
    156   1.4  christos  *	Merge the common device configurations to the subconfigurations
    157   1.4  christos  */
    158   1.4  christos static void
    159   1.5  christos isapnp_merge(c, d)
    160   1.5  christos 	struct isapnp_attach_args *c;
    161   1.5  christos 	const struct isapnp_attach_args *d;
    162   1.4  christos {
    163   1.4  christos 	int i;
    164   1.4  christos 
    165   1.4  christos 	for (i = 0; i < d->ipa_nio; i++)
    166   1.4  christos 		c->ipa_io[c->ipa_nio++] = d->ipa_io[i];
    167   1.4  christos 
    168   1.4  christos 	for (i = 0; i < d->ipa_nmem; i++)
    169   1.4  christos 		c->ipa_mem[c->ipa_nmem++] = d->ipa_mem[i];
    170   1.4  christos 
    171   1.4  christos 	for (i = 0; i < d->ipa_nmem32; i++)
    172   1.4  christos 		c->ipa_mem32[c->ipa_nmem32++] = d->ipa_mem32[i];
    173   1.4  christos 
    174   1.4  christos 	for (i = 0; i < d->ipa_nirq; i++)
    175   1.4  christos 		c->ipa_irq[c->ipa_nirq++] = d->ipa_irq[i];
    176   1.4  christos 
    177   1.4  christos 	for (i = 0; i < d->ipa_ndrq; i++)
    178   1.4  christos 		c->ipa_drq[c->ipa_ndrq++] = d->ipa_drq[i];
    179   1.4  christos }
    180   1.4  christos 
    181   1.4  christos 
    182   1.4  christos /* isapnp_flatten():
    183   1.4  christos  *	Flatten the tree to a list of config entries.
    184   1.4  christos  */
    185   1.4  christos static struct isapnp_attach_args *
    186   1.4  christos isapnp_flatten(card)
    187   1.4  christos 	struct isapnp_attach_args *card;
    188   1.4  christos {
    189   1.4  christos 	struct isapnp_attach_args *dev, *conf, *d, *c, *pa;
    190   1.4  christos 
    191   1.4  christos 	dev = card->ipa_child;
    192   1.4  christos 	ISAPNP_FREE(card);
    193   1.4  christos 
    194   1.4  christos 	for (conf = c = NULL, d = dev; d; d = dev) {
    195   1.4  christos 		dev = d->ipa_sibling;
    196   1.4  christos 		if (d->ipa_child == NULL) {
    197   1.4  christos 			/*
    198   1.4  christos 			 * No subconfigurations; all configuration info
    199   1.4  christos 			 * is in the device node.
    200   1.4  christos 			 */
    201   1.4  christos 			d->ipa_sibling = NULL;
    202   1.4  christos 			pa = d;
    203   1.4  christos 		}
    204   1.4  christos 		else {
    205   1.4  christos 			/*
    206   1.4  christos 			 * Push down device configuration info to the
    207   1.4  christos 			 * subconfigurations
    208   1.4  christos 			 */
    209   1.4  christos 			for (pa = d->ipa_child; pa; pa = pa->ipa_sibling)
    210   1.5  christos 				isapnp_merge(pa, d);
    211   1.4  christos 
    212   1.4  christos 			pa = d->ipa_child;
    213   1.4  christos 			ISAPNP_FREE(d);
    214   1.4  christos 		}
    215   1.4  christos 
    216   1.4  christos 		if (c == NULL)
    217   1.4  christos 			c = conf = pa;
    218   1.4  christos 		else
    219   1.4  christos 			c->ipa_sibling = pa;
    220   1.4  christos 
    221   1.4  christos 		while (c->ipa_sibling)
    222   1.4  christos 			c = c->ipa_sibling;
    223   1.4  christos 	}
    224   1.4  christos 	return conf;
    225   1.4  christos }
    226   1.4  christos 
    227   1.4  christos 
    228   1.1  christos /* isapnp_process_tag():
    229   1.1  christos  *	Process a resource tag
    230   1.1  christos  */
    231   1.4  christos static int
    232   1.4  christos isapnp_process_tag(tag, len, buf, card, dev, conf)
    233   1.1  christos 	u_char tag, len, *buf;
    234   1.4  christos 	struct isapnp_attach_args **card, **dev, **conf;
    235   1.1  christos {
    236   1.1  christos 	char str[64];
    237   1.1  christos 	struct isapnp_region *r;
    238   1.1  christos 	struct isapnp_pin *p;
    239   1.4  christos 	struct isapnp_attach_args *pa;
    240   1.1  christos 
    241   1.1  christos #define COPY(a, b) strncpy((a), (b), sizeof(a)), (a)[sizeof(a) - 1] = '\0'
    242   1.1  christos 
    243   1.1  christos 	switch (tag) {
    244   1.1  christos 	case ISAPNP_TAG_VERSION_NUM:
    245   1.1  christos 		DPRINTF(("PnP version %d.%d, Vendor version %d.%d\n",
    246   1.1  christos 		    buf[0] >> 4, buf[0] & 0xf, buf[1] >> 4,  buf[1] & 0xf));
    247   1.4  christos 		return 0;
    248   1.1  christos 
    249   1.1  christos 	case ISAPNP_TAG_LOGICAL_DEV_ID:
    250   1.1  christos 		(void) isapnp_id_to_vendor(str, buf);
    251   1.1  christos 		DPRINTF(("Logical device id %s\n", str));
    252   1.4  christos 
    253   1.4  christos 		*dev = isapnp_newdev(*card);
    254   1.4  christos 		COPY((*dev)->ipa_devlogic, str);
    255   1.4  christos 		return 0;
    256   1.1  christos 
    257   1.1  christos 	case ISAPNP_TAG_COMPAT_DEV_ID:
    258   1.1  christos 		(void) isapnp_id_to_vendor(str, buf);
    259   1.1  christos 		DPRINTF(("Compatible device id %s\n", str));
    260   1.7     mikel 
    261   1.7     mikel 		if (*dev == NULL)
    262   1.7     mikel 			return -1;
    263   1.7     mikel 
    264   1.7     mikel 		if (*(*dev)->ipa_devcompat == '\0')
    265   1.7     mikel 			COPY((*dev)->ipa_devcompat, str);
    266   1.4  christos 		return 0;
    267   1.4  christos 
    268   1.4  christos 	case ISAPNP_TAG_DEP_START:
    269   1.4  christos 		if (len == 0)
    270   1.4  christos 			buf[0] = ISAPNP_DEP_ACCEPTABLE;
    271   1.4  christos 
    272   1.4  christos 		if (*dev == NULL)
    273   1.4  christos 			return -1;
    274   1.4  christos 
    275   1.4  christos 		*conf = isapnp_newconf(*dev);
    276   1.4  christos 		(*conf)->ipa_pref = buf[0];
    277   1.4  christos #ifdef DEBUG_ISAPNP
    278   1.6     mikel 		isapnp_print_dep_start(">>> Start dependent function ",
    279   1.4  christos 		    (*conf)->ipa_pref);
    280   1.4  christos #endif
    281   1.4  christos 		return 0;
    282  1.14     perry 
    283   1.4  christos 	case ISAPNP_TAG_DEP_END:
    284   1.6     mikel 		DPRINTF(("<<<End dependent functions\n"));
    285   1.4  christos 		*conf = NULL;
    286   1.4  christos 		return 0;
    287   1.4  christos 
    288   1.4  christos 	case ISAPNP_TAG_ANSI_IDENT_STRING:
    289   1.4  christos 		buf[len] = '\0';
    290   1.4  christos 		DPRINTF(("ANSI Ident: %s\n", buf));
    291   1.4  christos 		if (*dev == NULL)
    292   1.4  christos 			COPY((*card)->ipa_devident, buf);
    293   1.4  christos 		else
    294   1.4  christos 			COPY((*dev)->ipa_devclass, buf);
    295   1.4  christos 		return 0;
    296   1.4  christos 
    297   1.4  christos 	case ISAPNP_TAG_END:
    298   1.4  christos 		*dev = NULL;
    299   1.4  christos 		return 0;
    300   1.4  christos 
    301   1.4  christos 	default:
    302   1.4  christos 		/* Handled below */
    303   1.1  christos 		break;
    304   1.4  christos 	}
    305   1.4  christos 
    306   1.4  christos 
    307   1.4  christos 	/*
    308   1.4  christos 	 * Decide which configuration we add the tag to
    309   1.4  christos 	 */
    310   1.4  christos 	if (*conf)
    311   1.4  christos 		pa = *conf;
    312   1.4  christos 	else if (*dev)
    313   1.4  christos 		pa = *dev;
    314   1.4  christos 	else
    315   1.4  christos 		/* error */
    316   1.4  christos 		return -1;
    317   1.4  christos 
    318   1.4  christos 	switch (tag) {
    319   1.1  christos 	case ISAPNP_TAG_IRQ_FORMAT:
    320   1.1  christos 		if (len < 2)
    321   1.1  christos 			break;
    322   1.1  christos 
    323   1.1  christos 		if (len != 3)
    324   1.1  christos 			buf[2] = ISAPNP_IRQTYPE_EDGE_PLUS;
    325   1.1  christos 
    326   1.1  christos 		p = &pa->ipa_irq[pa->ipa_nirq++];
    327   1.1  christos 		p->bits = buf[0] | (buf[1] << 8);
    328   1.1  christos 		p->flags = buf[2];
    329   1.1  christos #ifdef DEBUG_ISAPNP
    330   1.1  christos 		isapnp_print_irq("", p);
    331   1.1  christos #endif
    332   1.1  christos 		break;
    333   1.1  christos 
    334   1.1  christos 	case ISAPNP_TAG_DMA_FORMAT:
    335   1.1  christos 		if (buf[0] == 0)
    336   1.1  christos 			break;
    337   1.1  christos 
    338   1.1  christos 		p = &pa->ipa_drq[pa->ipa_ndrq++];
    339   1.1  christos 		p->bits = buf[0];
    340   1.1  christos 		p->flags = buf[1];
    341   1.1  christos #ifdef DEBUG_ISAPNP
    342   1.1  christos 		isapnp_print_drq("", p);
    343   1.1  christos #endif
    344   1.1  christos 		break;
    345   1.1  christos 
    346   1.1  christos 
    347   1.1  christos 	case ISAPNP_TAG_IO_PORT_DESC:
    348   1.1  christos 		r = &pa->ipa_io[pa->ipa_nio++];
    349   1.1  christos 		r->flags = buf[0];
    350   1.1  christos 		r->minbase = (buf[2] << 8) | buf[1];
    351   1.1  christos 		r->maxbase = (buf[4] << 8) | buf[3];
    352   1.1  christos 		r->align = buf[5];
    353   1.1  christos 		r->length = buf[6];
    354  1.10  christos 		if (r->length == 0)
    355  1.10  christos 		    pa->ipa_nio--;
    356   1.1  christos #ifdef DEBUG_ISAPNP
    357   1.1  christos 		isapnp_print_io("", r);
    358   1.1  christos #endif
    359   1.1  christos 		break;
    360   1.1  christos 
    361   1.1  christos 	case ISAPNP_TAG_FIXED_IO_PORT_DESC:
    362   1.1  christos 		r = &pa->ipa_io[pa->ipa_nio++];
    363   1.1  christos 		r->flags = 0;
    364   1.6     mikel 		r->minbase = (buf[1] << 8) | buf[0];
    365   1.1  christos 		r->maxbase = r->minbase;
    366   1.1  christos 		r->align = 1;
    367   1.6     mikel 		r->length = buf[2];
    368  1.10  christos 		if (r->length == 0)
    369  1.10  christos 		    pa->ipa_nio--;
    370   1.1  christos #ifdef DEBUG_ISAPNP
    371   1.6     mikel 		isapnp_print_io("FIXED ", r);
    372   1.1  christos #endif
    373   1.1  christos 		break;
    374   1.1  christos 
    375   1.1  christos 	case ISAPNP_TAG_VENDOR_DEF:
    376   1.6     mikel 		DPRINTF(("Vendor defined (short)\n"));
    377   1.1  christos 		break;
    378   1.1  christos 
    379   1.1  christos 	case ISAPNP_TAG_MEM_RANGE_DESC:
    380   1.1  christos 		r = &pa->ipa_mem[pa->ipa_nmem++];
    381   1.6     mikel 		r->flags = buf[0];
    382   1.6     mikel 		r->minbase = (buf[2] << 16) | (buf[1] << 8);
    383   1.6     mikel 		r->maxbase = (buf[4] << 16) | (buf[3] << 8);
    384   1.1  christos 		r->align = (buf[6] << 8) | buf[5];
    385   1.6     mikel 		r->length = (buf[8] << 16) | (buf[7] << 8);
    386  1.10  christos 		if (r->length == 0)
    387  1.10  christos 		    pa->ipa_nmem--;
    388   1.1  christos #ifdef DEBUG_ISAPNP
    389   1.6     mikel 		isapnp_print_mem("", r);
    390   1.1  christos #endif
    391   1.1  christos 		break;
    392   1.1  christos 
    393   1.1  christos 
    394   1.1  christos 	case ISAPNP_TAG_UNICODE_IDENT_STRING:
    395   1.6     mikel 		DPRINTF(("Unicode Ident\n"));
    396   1.1  christos 		break;
    397   1.1  christos 
    398   1.1  christos 	case ISAPNP_TAG_VENDOR_DEFINED:
    399   1.6     mikel 		DPRINTF(("Vendor defined (long)\n"));
    400   1.1  christos 		break;
    401   1.1  christos 
    402   1.1  christos 	case ISAPNP_TAG_MEM32_RANGE_DESC:
    403   1.1  christos 		r = &pa->ipa_mem32[pa->ipa_nmem32++];
    404   1.1  christos 		r->flags = buf[0];
    405   1.1  christos 		r->minbase = (buf[4] << 24) | (buf[3] << 16) |
    406   1.1  christos 		    (buf[2] << 8) | buf[1];
    407   1.1  christos 		r->maxbase = (buf[8] << 24) | (buf[7] << 16) |
    408   1.1  christos 		    (buf[6] << 8) | buf[5];
    409  1.14     perry 		r->align = (buf[12] << 24) | (buf[11] << 16) |
    410   1.1  christos 		    (buf[10] << 8) | buf[9];
    411   1.1  christos 		r->length = (buf[16] << 24) | (buf[15] << 16) |
    412   1.1  christos 		    (buf[14] << 8) | buf[13];
    413  1.10  christos 		if (r->length == 0)
    414  1.10  christos 		    pa->ipa_nmem32--;
    415   1.1  christos #ifdef DEBUG_ISAPNP
    416   1.6     mikel 		isapnp_print_mem("32-bit ", r);
    417   1.1  christos #endif
    418   1.1  christos 		break;
    419   1.1  christos 
    420   1.1  christos 	case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC:
    421   1.1  christos 		r = &pa->ipa_mem32[pa->ipa_nmem32++];
    422   1.1  christos 		r->flags = buf[0];
    423   1.1  christos 		r->minbase = (buf[4] << 24) | (buf[3] << 16) |
    424   1.1  christos 		    (buf[2] << 8) | buf[1];
    425   1.1  christos 		r->maxbase = r->minbase;
    426   1.1  christos 		r->align = 1;
    427   1.1  christos 		r->length = (buf[8] << 24) | (buf[7] << 16) |
    428   1.1  christos 		    (buf[6] << 8) | buf[5];
    429  1.10  christos 		if (r->length == 0)
    430  1.10  christos 		    pa->ipa_nmem32--;
    431   1.1  christos #ifdef DEBUG_ISAPNP
    432   1.6     mikel 		isapnp_print_mem("FIXED 32-bit ", r);
    433   1.1  christos #endif
    434   1.1  christos 		break;
    435   1.1  christos 
    436   1.1  christos 	default:
    437   1.1  christos #ifdef DEBUG_ISAPNP
    438   1.1  christos 		{
    439   1.1  christos 			int i;
    440   1.1  christos 			printf("tag %.2x, len %d: ", tag, len);
    441   1.1  christos 			for (i = 0; i < len; i++)
    442   1.1  christos 				printf("%.2x ", buf[i]);
    443   1.1  christos 			printf("\n");
    444   1.1  christos 		}
    445   1.1  christos #endif
    446   1.1  christos 		break;
    447   1.1  christos 	}
    448   1.4  christos 	return 0;
    449   1.1  christos }
    450   1.1  christos 
    451   1.1  christos 
    452   1.1  christos /* isapnp_get_resource():
    453   1.1  christos  *	Read the resources for card c
    454   1.1  christos  */
    455   1.1  christos struct isapnp_attach_args *
    456   1.1  christos isapnp_get_resource(sc, c)
    457   1.1  christos 	struct isapnp_softc *sc;
    458   1.1  christos 	int c;
    459   1.1  christos {
    460   1.1  christos 	u_char d, tag;
    461   1.1  christos 	u_short len;
    462   1.1  christos 	int i;
    463   1.4  christos 	int warned = 0;
    464   1.4  christos 	struct isapnp_attach_args *card, *dev = NULL, *conf = NULL;
    465   1.4  christos 	u_char buf[ISAPNP_MAX_TAGSIZE], *p;
    466   1.1  christos 
    467   1.4  christos 	memset(buf, 0, sizeof(buf));
    468   1.4  christos 
    469   1.4  christos 	card = ISAPNP_MALLOC(sizeof(*card));
    470   1.4  christos 	memset(card, 0, sizeof(*card));
    471   1.4  christos 
    472   1.4  christos #define NEXT_BYTE \
    473   1.4  christos 		if (isapnp_wait_status(sc)) \
    474   1.4  christos 			goto bad; \
    475   1.4  christos 		d = isapnp_read_reg(sc, ISAPNP_RESOURCE_DATA)
    476   1.1  christos 
    477   1.1  christos 	for (i = 0; i < ISAPNP_SERIAL_SIZE; i++) {
    478   1.4  christos 		NEXT_BYTE;
    479   1.1  christos 
    480   1.1  christos 		if (d != sc->sc_id[c][i] && i != ISAPNP_SERIAL_SIZE - 1) {
    481   1.4  christos 			if (!warned) {
    482   1.4  christos 				printf("%s: card %d violates PnP spec; byte %d\n",
    483   1.4  christos 				    sc->sc_dev.dv_xname, c + 1, i);
    484   1.4  christos 				warned++;
    485   1.4  christos 			}
    486   1.4  christos 			if (i == 0) {
    487   1.4  christos 				/*
    488   1.4  christos 				 * Magic! If this is the first byte, we
    489   1.4  christos 				 * assume that the tag data begins here.
    490   1.4  christos 				 */
    491   1.4  christos 				goto parse;
    492   1.4  christos 			}
    493   1.1  christos 		}
    494   1.1  christos 	}
    495   1.1  christos 
    496   1.1  christos 	do {
    497   1.1  christos 		NEXT_BYTE;
    498   1.4  christos parse:
    499   1.1  christos 
    500   1.1  christos 		if (d & ISAPNP_LARGE_TAG) {
    501   1.1  christos 			tag = d;
    502   1.1  christos 			NEXT_BYTE;
    503   1.1  christos 			buf[0] = d;
    504   1.1  christos 			NEXT_BYTE;
    505   1.1  christos 			buf[1] = d;
    506   1.1  christos 			len = (buf[1] << 8) | buf[0];
    507   1.1  christos 		}
    508   1.1  christos 		else {
    509   1.1  christos 			tag = (d >> 3) & 0xf;
    510   1.1  christos 			len = d & 0x7;
    511   1.1  christos 		}
    512   1.1  christos 
    513   1.1  christos 		for (p = buf, i = 0; i < len; i++) {
    514   1.1  christos 			NEXT_BYTE;
    515   1.1  christos 			if (i < ISAPNP_MAX_TAGSIZE)
    516   1.1  christos 				*p++ = d;
    517   1.1  christos 		}
    518   1.1  christos 
    519   1.1  christos 		if (len >= ISAPNP_MAX_TAGSIZE) {
    520   1.4  christos 			printf("%s: Maximum tag size exceeded, card %d\n",
    521   1.4  christos 			    sc->sc_dev.dv_xname, c + 1);
    522   1.1  christos 			len = ISAPNP_MAX_TAGSIZE;
    523   1.4  christos 			if (++warned == 10)
    524   1.4  christos 				goto bad;
    525   1.1  christos 		}
    526   1.1  christos 
    527  1.12  christos 		if (isapnp_process_tag(tag, len, buf, &card, &dev,
    528  1.12  christos 		    &conf) == -1) {
    529   1.4  christos 			printf("%s: No current device for tag, card %d\n",
    530   1.4  christos 			    sc->sc_dev.dv_xname, c + 1);
    531  1.12  christos 			if (++warned == 10)
    532  1.12  christos 				goto bad;
    533  1.12  christos 		}
    534   1.1  christos 	}
    535   1.1  christos 	while (tag != ISAPNP_TAG_END);
    536   1.4  christos 	return isapnp_flatten(card);
    537   1.4  christos 
    538   1.1  christos bad:
    539   1.4  christos 	for (card = isapnp_flatten(card); card; ) {
    540   1.4  christos 		dev = card->ipa_sibling;
    541   1.4  christos 		ISAPNP_FREE(card);
    542   1.4  christos 		card = dev;
    543   1.1  christos 	}
    544   1.4  christos 	printf("%s: %s, card %d\n", sc->sc_dev.dv_xname,
    545   1.4  christos 	    warned >= 10 ? "Too many tag errors" : "Resource timeout", c + 1);
    546   1.1  christos 	return NULL;
    547   1.1  christos }
    548