/*-
* Copyright (c) 2006 Michael Lorenz
* All rights reserved.
*
* 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.
*/
sc->sc_free = 15;
for (i = 0; i < 16; i++) {
sc->sc_devtable[i].original_addr = 0;
sc->sc_devtable[i].current_addr = 0;
sc->sc_devtable[i].handler_id = 0;
sc->sc_devtable[i].cookie = NULL;
sc->sc_devtable[i].handler = NULL;
}
/* bus reset (?) */
nadb_send_sync(sc, 0, 0, NULL);
delay(200000);
/*
* scan only addresses 1 - 7
* if something responds move it to >7 and see if something else is
* there. If not move the previous one back.
* XXX we don't check for collisions if we use up all addresses >7
*/
for (i = 1; i < 8; i++) {
DPRINTF("\n%d: ", i);
last_moved_up = 0;
nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
/* found something? */
while (sc->sc_len > 2) {
/* something answered, so move it up */
DPRINTF("Found a device on address %d\n", i);
cmd[0] = sc->sc_free | 0x60;
cmd[1] = 0xfe;
nadb_send_sync(sc, ADBLISTEN(i, 3), 2, cmd);
/* see if it really moved */
nadb_send_sync(sc, ADBTALK(sc->sc_free, 3), 0, NULL);
if (sc->sc_len > 2) {
/* ok */
DPRINTF("moved it to %d\n", sc->sc_free);
nadb_register(sc, sc->sc_free, i, sc->sc_data[3]);
last_moved_up = sc->sc_free;
sc->sc_free--;
}
/* see if something else is there */
nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
}
if (last_moved_up != 0) {
/* move last one back to original address */
cmd[0] = i | 0x60;
cmd[1] = 0xfe;
nadb_send_sync(sc, ADBLISTEN(last_moved_up, 3), 2, cmd);
nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
if (sc->sc_len > 2) {
DPRINTF("moved %d back to %d\n", last_moved_up, i);
nadb_remove(sc, last_moved_up);
nadb_register(sc, i, i, sc->sc_data[3]);
sc->sc_free = last_moved_up;
}
}
}
/* now attach the buggers we've found */
aaa.ops = sc->sc_ops;
for (i = 0; i < 16; i++) {
if (sc->sc_devtable[i].current_addr != 0) {
DPRINTF("dev: %d %d %02x\n",
sc->sc_devtable[i].current_addr,
sc->sc_devtable[i].original_addr,
sc->sc_devtable[i].handler_id);
aaa.dev = &sc->sc_devtable[i];
if (config_found(sc->sc_dev, &aaa, nadb_devprint,
CFARGS_NONE)) {
devmask |= (1 << i);
} else {
aprint_normal(" not configured\n");
}
}
}
/* now enable autopolling */
DPRINTF("devmask: %04x\n", devmask);
sc->sc_ops->autopoll(sc->sc_ops->cookie, devmask);
}
int
nadb_print(void *aux, const char *what)
{
aprint_normal(": Apple Desktop Bus\n");
return 0;
}