/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* isapnp_wait_status():
* Wait for the next byte of resource data to become available
*/
static int
isapnp_wait_status(struct isapnp_softc *sc)
{
int i;
/* wait up to 1 ms for each resource byte */
for (i = 0; i < 10; i++) {
if (isapnp_read_reg(sc, ISAPNP_STATUS) & 1)
return 0;
DELAY(100);
}
return 1;
}
/* isapnp_newdev():
* Add a new logical device to the current card; expand the configuration
* resources of the current card if needed.
*/
static struct isapnp_attach_args *
isapnp_newdev(struct isapnp_attach_args *card)
{
struct isapnp_attach_args *ipa, *dev = ISAPNP_MALLOC(sizeof(*dev));
/* isapnp_merge():
* Merge the common device configurations to the subconfigurations
*/
static void
isapnp_merge(struct isapnp_attach_args *c, const struct isapnp_attach_args *d)
{
int i;
for (i = 0; i < d->ipa_nio; i++)
c->ipa_io[c->ipa_nio++] = d->ipa_io[i];
for (i = 0; i < d->ipa_nmem; i++)
c->ipa_mem[c->ipa_nmem++] = d->ipa_mem[i];
for (i = 0; i < d->ipa_nmem32; i++)
c->ipa_mem32[c->ipa_nmem32++] = d->ipa_mem32[i];
for (i = 0; i < d->ipa_nirq; i++)
c->ipa_irq[c->ipa_nirq++] = d->ipa_irq[i];
for (i = 0; i < d->ipa_ndrq; i++)
c->ipa_drq[c->ipa_ndrq++] = d->ipa_drq[i];
}
/* isapnp_flatten():
* Flatten the tree to a list of config entries.
*/
static struct isapnp_attach_args *
isapnp_flatten(struct isapnp_attach_args *card)
{
struct isapnp_attach_args *dev, *conf, *d, *c, *pa;
dev = card->ipa_child;
ISAPNP_FREE(card);
for (conf = c = NULL, d = dev; d; d = dev) {
dev = d->ipa_sibling;
if (d->ipa_child == NULL) {
/*
* No subconfigurations; all configuration info
* is in the device node.
*/
d->ipa_sibling = NULL;
pa = d;
}
else {
/*
* Push down device configuration info to the
* subconfigurations
*/
for (pa = d->ipa_child; pa; pa = pa->ipa_sibling)
isapnp_merge(pa, d);
pa = d->ipa_child;
ISAPNP_FREE(d);
}
if (c == NULL)
c = conf = pa;
else
c->ipa_sibling = pa;
while (c->ipa_sibling)
c = c->ipa_sibling;
}
return conf;
}
/* isapnp_process_tag():
* Process a resource tag
*/
static int
isapnp_process_tag(u_char tag, u_char len, u_char *buf, struct isapnp_attach_args **card, struct isapnp_attach_args **dev, struct isapnp_attach_args **conf)
{
char str[64];
struct isapnp_region *r;
struct isapnp_pin *p;
struct isapnp_attach_args *pa;
#define NEXT_BYTE \
if (isapnp_wait_status(sc)) \
goto bad; \
d = isapnp_read_reg(sc, ISAPNP_RESOURCE_DATA)
for (i = 0; i < ISAPNP_SERIAL_SIZE; i++) {
NEXT_BYTE;
if (d != sc->sc_id[c][i] && i != ISAPNP_SERIAL_SIZE - 1) {
if (!warned) {
aprint_error_dev(sc->sc_dev,
"card %d violates PnP spec; byte %d\n",
c + 1, i);
warned++;
}
if (i == 0) {
/*
* Magic! If this is the first byte, we
* assume that the tag data begins here.
*/
goto parse;
}
}
}
do {
NEXT_BYTE;
parse:
if (d & ISAPNP_LARGE_TAG) {
tag = d;
NEXT_BYTE;
buf[0] = d;
NEXT_BYTE;
buf[1] = d;
len = (buf[1] << 8) | buf[0];
}
else {
tag = (d >> 3) & 0xf;
len = d & 0x7;
}
for (p = buf, i = 0; i < len; i++) {
NEXT_BYTE;
if (i < ISAPNP_MAX_TAGSIZE)
*p++ = d;
}
if (len >= ISAPNP_MAX_TAGSIZE) {
aprint_error_dev(sc->sc_dev,
"Maximum tag size exceeded, card %d\n",
c + 1);
len = ISAPNP_MAX_TAGSIZE - 1;
if (++warned == 10)
goto bad;
}
if (isapnp_process_tag(tag, len, buf, &card, &dev,
&conf) == -1) {
aprint_error_dev(sc->sc_dev,
"No current device for tag, card %d\n",
c + 1);
if (++warned == 10)
goto bad;
}
}
while (tag != ISAPNP_TAG_END);
return isapnp_flatten(card);
bad:
for (card = isapnp_flatten(card); card; ) {
dev = card->ipa_sibling;
ISAPNP_FREE(card);
card = dev;
}
aprint_normal_dev(sc->sc_dev, "%s, card %d\n",
warned >= 10 ? "Too many tag errors" : "Resource timeout", c + 1);
return NULL;
}