#include <sys/param.h>
#include <sys/device.h>
#include <sys/fcntl.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/task.h>
#include <sys/timeout.h>
#include <sys/systm.h>
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <dev/ofw/openfirm.h>
#include <dev/wscons/wsconsio.h>
#include <dev/adb/adb.h>
#include <macppc/dev/adbvar.h>
#include <macppc/dev/pm_direct.h>
#include <macppc/dev/viareg.h>
#include "apm.h"
#define printf_intr printf
#ifdef DEBUG
#ifndef ADB_DEBUG
#define ADB_DEBUG
#endif
#endif
int adb_polling;
#ifdef ADB_DEBUG
int adb_debug;
#endif
#define vPB 0x0000
#define vPB3 0x08
#define vPB4 0x10
#define vPB5 0x20
#define vSR_INT 0x04
#define vSR_OUT 0x10
#define ADB_ACTION_NOTREADY 0x1
#define ADB_ACTION_IDLE 0x2
#define ADB_ACTION_OUT 0x3
#define ADB_ACTION_IN 0x4
#define ADB_SET_STATE_IDLE_CUDA() via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
#define ADB_SET_STATE_TIP() via_reg_and(VIA1, vBufB, ~vPB5)
#define ADB_CLR_STATE_TIP() via_reg_or(VIA1, vBufB, vPB5)
#define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
#define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
#define ADB_SET_SR_INPUT() via_reg_and(VIA1, vACR, ~vSR_OUT)
#define ADB_SET_SR_OUTPUT() via_reg_or(VIA1, vACR, vSR_OUT)
#define ADB_SR() read_via_reg(VIA1, vSR)
#define ADB_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x84)
#define ADB_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x04)
#define ADB_VIA_CLR_INTR() write_via_reg(VIA1, vIFR, 0x04)
#define ADB_INTR_IS_OFF (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
#define ADB_INTR_IS_ON (0 == (read_via_reg(VIA1, vBufB) & vPB3))
#define ADB_SR_INTR_IS_ON (vSR_INT == (read_via_reg(VIA1, \
vIFR) & vSR_INT))
#define ADB_DELAY 150
#define ADB_MAX_MSG_LENGTH 16
#define ADB_MAX_HDR_LENGTH 8
#define ADB_QUEUE 32
#define ADB_TICKLE_TICKS 4
struct adbCommand {
u_char header[ADB_MAX_HDR_LENGTH];
u_char data[ADB_MAX_MSG_LENGTH];
u_char *saveBuf;
u_char *compRout;
u_char *compData;
u_int cmd;
u_int unsol;
u_int ack_only;
};
int adbHardware = ADB_HW_UNKNOWN;
int adbActionState = ADB_ACTION_NOTREADY;
int adbWaiting;
int adbWriteDelay;
int adbWaitingCmd;
u_char *adbBuffer;
void *adbCompRout;
void *adbCompData;
int adbStarting = 1;
u_char adbInputBuffer[ADB_MAX_MSG_LENGTH];
u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH];
int adbSentChars;
struct adbCommand adbInbound[ADB_QUEUE];
int adbInCount;
int adbInHead;
int adbInTail;
int tickle_count;
int tickle_serial;
int adb_cuda_serial;
struct timeout adb_cuda_timeout;
struct timeout adb_softintr_timeout;
int adbempty;
extern struct cfdriver adb_cd;
volatile u_char *Via1Base;
#ifdef ADB_DEBUG
void print_single(u_char *);
#endif
void adb_intr_cuda(void);
void adb_soft_intr(void);
int send_adb_cuda(u_char *, u_char *, void *, void *, int);
void adb_cuda_tickle(void *);
void adb_pass_up(struct adbCommand *);
void adb_op_comprout(caddr_t, caddr_t, int);
void adb_reinit(struct adb_softc *);
int count_adbs(struct adb_softc *);
int get_ind_adb_info(struct adb_softc *, ADBDataBlock *, int);
int get_adb_info(ADBDataBlock *, int);
int adb_op(Ptr, Ptr, Ptr, short);
void adb_hw_setup(void);
int adb_cmd_result(u_char *);
void setsoftadb(void);
int adb_intr(void *arg);
void adb_cuda_autopoll(void);
void adb_cuda_fileserver_mode(void);
uint8_t pmu_backlight;
int pmu_get_backlight(struct wskbd_backlight *);
int pmu_set_backlight(struct wskbd_backlight *);
extern int (*wskbd_get_backlight)(struct wskbd_backlight *);
extern int (*wskbd_set_backlight)(struct wskbd_backlight *);
#ifndef SMALL_KERNEL
void adb_shutdown(void *);
struct task adb_shutdown_task = TASK_INITIALIZER(adb_shutdown, NULL);
#ifdef SUSPEND
void adb_suspend(void *);
struct task adb_suspend_task = TASK_INITIALIZER(adb_suspend, NULL);
struct taskq *adb_suspendq;
#endif
#endif
#ifdef ADB_DEBUG
void
print_single(u_char *str)
{
int x;
if (str == NULL) {
printf_intr("no data - null pointer\n");
return;
}
if (*str == '\0') {
printf_intr("nothing returned\n");
return;
}
if (*str > 20) {
printf_intr("ADB: ACK > 20 no way!\n");
*str = 20;
}
printf_intr("(length=0x%x):", *str);
for (x = 1; x <= *str; x++)
printf_intr(" 0x%02x", str[x]);
printf_intr("\n");
}
#endif
void
adb_cuda_tickle(void *unused)
{
volatile int s;
if (adbActionState == ADB_ACTION_IN) {
if (tickle_serial == adb_cuda_serial) {
if (++tickle_count > 0) {
s = splhigh();
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
ADB_SET_STATE_IDLE_CUDA();
splx(s);
}
} else {
tickle_serial = adb_cuda_serial;
tickle_count = 0;
}
} else {
tickle_serial = adb_cuda_serial;
tickle_count = 0;
}
timeout_add(&adb_cuda_timeout, ADB_TICKLE_TICKS);
}
void
adb_intr_cuda(void)
{
volatile int i, ending;
volatile unsigned int s;
struct adbCommand packet;
s = splhigh();
ADB_VIA_CLR_INTR();
ADB_VIA_INTR_DISABLE();
switch_start:
switch (adbActionState) {
case ADB_ACTION_IDLE:
adbInputBuffer[1] = ADB_SR();
adb_cuda_serial++;
if (ADB_INTR_IS_OFF)
break;
ADB_SET_SR_INPUT();
ADB_SET_STATE_TIP();
adbInputBuffer[0] = 1;
adbActionState = ADB_ACTION_IN;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("idle 0x%02x ", adbInputBuffer[1]);
#endif
break;
case ADB_ACTION_IN:
adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
if (ADB_INTR_IS_OFF)
ending = 1;
else
ending = 0;
if (1 == ending) {
#ifdef ADB_DEBUG
if (adb_debug) {
printf_intr("in end 0x%02x ",
adbInputBuffer[adbInputBuffer[0]]);
print_single(adbInputBuffer);
}
#endif
memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
if ((adbWaiting == 1) &&
(adbInputBuffer[4] == adbWaitingCmd) &&
((adbInputBuffer[2] == 0x00) ||
(adbInputBuffer[2] == 0x01))) {
packet.saveBuf = adbBuffer;
packet.compRout = adbCompRout;
packet.compData = adbCompData;
packet.unsol = 0;
packet.ack_only = 0;
adb_pass_up(&packet);
adbWaitingCmd = 0;
adbWaiting = 0;
adbBuffer = NULL;
adbCompRout = NULL;
adbCompData = NULL;
} else {
packet.unsol = 1;
packet.ack_only = 0;
adb_pass_up(&packet);
}
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
ADB_SET_STATE_IDLE_CUDA();
if (adbWriteDelay == 1) {
delay(ADB_DELAY);
adbSentChars = 0;
adbActionState = ADB_ACTION_OUT;
if (ADB_INTR_IS_ON) {
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
adbSentChars = 0;
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
break;
}
ADB_SET_STATE_TIP();
ADB_SET_SR_OUTPUT();
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
}
} else {
ADB_TOGGLE_STATE_ACK_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("in 0x%02x ",
adbInputBuffer[adbInputBuffer[0]]);
#endif
}
break;
case ADB_ACTION_OUT:
i = ADB_SR();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr out 0x%02x ", i);
#endif
adbSentChars++;
if (ADB_INTR_IS_ON) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr was on ");
#endif
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
adbSentChars = 0;
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
adbWriteDelay = 1;
delay(ADB_DELAY);
goto switch_start;
break;
}
if (adbOutputBuffer[0] == adbSentChars) {
if (0 == adb_cmd_result(adbOutputBuffer)) {
adbWaiting = 1;
adbWaitingCmd = adbOutputBuffer[2];
} else {
memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
packet.saveBuf = adbBuffer;
packet.compRout = adbCompRout;
packet.compData = adbCompData;
packet.cmd = adbWaitingCmd;
packet.unsol = 0;
packet.ack_only = 1;
adb_pass_up(&packet);
adbWaitingCmd = 0;
adbBuffer = NULL;
adbCompRout = NULL;
adbCompData = NULL;
}
adbWriteDelay = 0;
adbActionState = ADB_ACTION_IDLE;
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("write done ");
#endif
} else {
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
ADB_TOGGLE_STATE_ACK_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("toggle ");
#endif
}
break;
case ADB_ACTION_NOTREADY:
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: not yet initialized\n");
#endif
break;
default:
;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr: unknown ADB state\n");
#endif
}
ADB_VIA_INTR_ENABLE();
splx(s);
}
int
send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data,
int command)
{
int s, len;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("SEND\n");
#endif
if (adbActionState == ADB_ACTION_NOTREADY)
return 1;
s = splhigh();
if ((adbActionState == ADB_ACTION_IDLE) &&
(ADB_INTR_IS_OFF)) {
} else
if (adbWriteDelay == 0)
adbWriteDelay = 1;
else {
splx(s);
return 1;
}
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("QUEUE\n");
#endif
if ((long)in == (long)0) {
if ((command & 0x0c) == 0x08)
len = buffer[0];
else
len = 0;
adbOutputBuffer[0] = 2 + len;
adbOutputBuffer[1] = 0x00;
adbOutputBuffer[2] = (u_char)command;
memcpy(adbOutputBuffer + 3, buffer + 1, len);
} else
memcpy(adbOutputBuffer, in, in[0] + 2);
adbSentChars = 0;
adbBuffer = buffer;
adbCompRout = compRout;
adbCompData = data;
adbWaitingCmd = adbOutputBuffer[2];
if (adbWriteDelay != 1) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("out start NOW");
#endif
delay(ADB_DELAY);
adbActionState = ADB_ACTION_OUT;
ADB_SET_SR_OUTPUT();
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
ADB_SET_STATE_ACKOFF_CUDA();
ADB_SET_STATE_TIP();
}
adbWriteDelay = 1;
splx(s);
if (adb_polling)
while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
|| (adbWaiting == 1))
if (ADB_SR_INTR_IS_ON) {
adb_intr_cuda();
if (cold)
delay(ADB_DELAY);
adb_soft_intr();
}
return 0;
}
int
adb_intr(void *arg)
{
switch (adbHardware) {
case ADB_HW_PMU:
pm_intr();
break;
case ADB_HW_CUDA:
adb_intr_cuda();
break;
}
return 1;
}
void
adb_pass_up(struct adbCommand *in)
{
int start = 0, len = 0, cmd = 0;
ADBDataBlock block;
if (adbInCount >= ADB_QUEUE) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: ring buffer overflow\n");
#endif
return;
}
if (in->ack_only) {
len = in->data[0];
cmd = in->cmd;
start = 0;
} else {
switch (adbHardware) {
case ADB_HW_CUDA:
if (in->unsol)
if (0 != in->data[2])
return;
cmd = in->data[4];
if (in->data[0] < 5)
len = 0;
else
len = in->data[0]-4;
start = 4;
break;
case ADB_HW_PMU:
cmd = in->data[1];
if (in->data[0] < 2)
len = 0;
else
len = in->data[0]-1;
start = 1;
break;
case ADB_HW_UNKNOWN:
return;
}
if (in->unsol) {
if (adbStarting)
return;
if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
return;
}
}
if (in->unsol) {
adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
} else {
adbInbound[adbInTail].compRout = (void *)in->compRout;
adbInbound[adbInTail].compData = (void *)in->compData;
adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
}
#ifdef ADB_DEBUG
if (adb_debug && in->data[1] == 2)
printf_intr("adb: caught error\n");
#endif
memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
adbInbound[adbInTail].data[0] = len;
adbInbound[adbInTail].cmd = cmd;
adbInCount++;
if (++adbInTail >= ADB_QUEUE)
adbInTail = 0;
if (adb_polling)
adb_soft_intr();
else
setsoftadb();
}
void
adb_soft_intr(void)
{
int s;
int cmd = 0;
u_char *buffer;
u_char *comprout;
u_char *compdata;
while (adbInCount) {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("%x %x %x ",
adbInCount, adbInHead, adbInTail);
#endif
buffer = adbInbound[adbInHead].saveBuf;
comprout = adbInbound[adbInHead].compRout;
compdata = adbInbound[adbInHead].compData;
cmd = adbInbound[adbInHead].cmd;
if (buffer)
memcpy(buffer, adbInbound[adbInHead].data,
adbInbound[adbInHead].data[0] + 1);
#ifdef ADB_DEBUG
if (adb_debug & 0x80) {
printf_intr("%p %p %p %x ",
buffer, comprout, compdata, (short)cmd);
printf_intr("buf: ");
print_single(adbInbound[adbInHead].data);
}
#endif
s = splhigh();
adbInCount--;
if (++adbInHead >= ADB_QUEUE)
adbInHead = 0;
splx(s);
if (comprout) {
((int (*)(u_char *, u_char *, int)) comprout)
(buffer, compdata, cmd);
}
}
}
#ifndef SMALL_KERNEL
void
adb_shutdown(void *arg)
{
extern int allowpowerdown;
if (allowpowerdown == 1) {
allowpowerdown = 0;
prsignal(initprocess, SIGUSR2);
}
}
#ifdef SUSPEND
void
adb_suspend(void *arg)
{
extern struct cfdriver apm_cd;
if (apm_cd.cd_ndevs > 0)
sleep_state(apm_cd.cd_devs[0], SLEEP_SUSPEND);
}
#endif
#endif
void
adb_lid_closed_intr(void)
{
#ifndef SMALL_KERNEL
switch (lid_action) {
#ifdef SUSPEND
case 1:
task_add(adb_suspendq, &adb_suspend_task);
break;
#endif
case 2:
break;
}
#endif
}
void
adb_power_button_intr(void)
{
#ifndef SMALL_KERNEL
switch (pwr_action) {
case 1:
task_add(systq, &adb_shutdown_task);
break;
#ifdef SUSPEND
case 2:
task_add(adb_suspendq, &adb_suspend_task);
break;
#endif
}
#endif
}
int
adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
{
int result;
switch (adbHardware) {
case ADB_HW_PMU:
result = pm_adb_op((u_char *)buffer, (void *)compRout,
(void *)data, (int)command);
if (result == 0)
return 0;
else
return -1;
break;
case ADB_HW_CUDA:
result = send_adb_cuda(NULL, (u_char *)buffer,
(void *)compRout, (void *)data, (int)command);
if (result == 0)
return 0;
else
return -1;
break;
default:
return -1;
}
}
void
adb_hw_setup(void)
{
volatile int i;
switch (adbHardware) {
case ADB_HW_PMU:
write_via_reg(VIA1, vIFR, 0x90);
break;
case ADB_HW_CUDA:
via_reg_or(VIA1, vDirB, 0x30);
via_reg_and(VIA1, vDirB, 0xf7);
via_reg_and(VIA1, vACR, ~vSR_OUT);
write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
adbActionState = ADB_ACTION_IDLE;
write_via_reg(VIA1, vIER, 0x84);
ADB_SET_STATE_IDLE_CUDA();
i = ADB_SR();
ADB_VIA_INTR_DISABLE();
ADB_SET_STATE_IDLE_CUDA();
delay(ADB_DELAY);
ADB_SET_STATE_TIP();
delay(ADB_DELAY);
ADB_TOGGLE_STATE_ACK_CUDA();
delay(ADB_DELAY);
ADB_CLR_STATE_TIP();
delay(ADB_DELAY);
ADB_SET_STATE_IDLE_CUDA();
i = ADB_SR();
ADB_VIA_INTR_ENABLE();
break;
case ADB_HW_UNKNOWN:
default:
write_via_reg(VIA1, vIER, 0x04);
break;
}
}
void
adb_reinit(struct adb_softc *sc)
{
u_char send_string[ADB_MAX_MSG_LENGTH];
ADBDataBlock data;
volatile int i, x;
int s;
int command;
int result;
int saveptr;
int device;
int nonewtimes;
int ADBNumDevices = 0;
if (adbHardware != ADB_HW_PMU)
s = splhigh();
adbStarting = 1;
for (i = 0; i < 16; i++)
sc->sc_devtable[i].handler_id = 0;
adb_hw_setup();
delay(1000);
adb_op_sync((Ptr)0, (short)0x00);
delay(200000);
for (i = 1; i < 16; i++) {
send_string[0] = 0;
command = ADBTALK(i, 3);
result = adb_op_sync((Ptr)send_string, (short)command);
if (send_string[0] != 0) {
switch (send_string[2]) {
case 0:
case 0xfd:
case 0xfe:
case 0xff:
continue;
}
++ADBNumDevices;
KASSERT(ADBNumDevices < 16);
sc->sc_devtable[ADBNumDevices].handler_id =
(int)send_string[2];
sc->sc_devtable[ADBNumDevices].orig_addr = i;
sc->sc_devtable[ADBNumDevices].curr_addr = i;
sc->sc_devtable[ADBNumDevices].data = NULL;
sc->sc_devtable[ADBNumDevices].handler = NULL;
}
}
for (saveptr = 15; saveptr > 0; saveptr--)
if (-1 == get_adb_info(&data, saveptr))
break;
#ifdef ADB_DEBUG
if (adb_debug & 0x80) {
printf_intr("first free is: 0x%02x\n", saveptr);
printf_intr("devices: %i\n", ADBNumDevices);
}
#endif
nonewtimes = 0;
while (saveptr > 0 && nonewtimes++ < 11) {
for (i = 1; i <= ADBNumDevices; i++) {
device = sc->sc_devtable[i].curr_addr;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("moving device 0x%02x to 0x%02x "
"(index 0x%02x) ", device, saveptr, i);
#endif
command = ADBTALK(device, 3);
adb_op_sync((Ptr)send_string, (short)command);
command = ADBLISTEN(device, 3);
send_string[0] = 2;
send_string[1] = (u_char)(saveptr | 0x60);
send_string[2] = 0xfe;
adb_op_sync((Ptr)send_string, (short)command);
delay(500);
command = ADBTALK(saveptr, 3);
adb_op_sync((Ptr)send_string, (short)command);
delay(500);
if (send_string[0] == 0) {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("failed, continuing\n");
#endif
continue;
}
command = ADBTALK(device, 3);
result = adb_op_sync((Ptr)send_string, (short)command);
if (send_string[0] != 0) {
switch (send_string[2]) {
case 0:
case 0xfd:
case 0xfe:
case 0xff:
continue;
}
sc->sc_devtable[i].curr_addr = saveptr;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("old device at index %i\n",i);
#endif
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("new device found\n");
#endif
if (saveptr > ADBNumDevices) {
++ADBNumDevices;
KASSERT(ADBNumDevices < 16);
}
sc->sc_devtable[ADBNumDevices].handler_id =
(int)send_string[2];
sc->sc_devtable[ADBNumDevices].orig_addr = device;
sc->sc_devtable[ADBNumDevices].curr_addr = device;
sc->sc_devtable[ADBNumDevices].data = NULL;
sc->sc_devtable[ADBNumDevices].handler = NULL;
for (x = saveptr; x > 0; x--) {
if (-1 == get_adb_info(&data, x)) {
saveptr = x;
break;
}
}
if (x == 0)
saveptr = 0;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("new free is 0x%02x\n",
saveptr);
#endif
nonewtimes = 0;
} else {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("moving back...\n");
#endif
command = ADBLISTEN(saveptr, 3);
send_string[0] = 2;
send_string[1] = (u_char)(device | 0x60);
send_string[2] = 0xfe;
adb_op_sync((Ptr)send_string, (short)command);
delay(1000);
}
}
}
#ifdef ADB_DEBUG
if (adb_debug) {
for (i = 1; i <= ADBNumDevices; i++) {
x = get_ind_adb_info(sc, &data, i);
if (x != -1)
printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
i, x, data.devType);
}
}
#endif
#ifdef ADB_DEBUG
if (adb_debug) {
if (0 == ADBNumDevices)
printf_intr("adb: no devices found\n");
}
#endif
adbStarting = 0;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: adb_reinit complete\n");
#endif
if (adbHardware == ADB_HW_CUDA) {
timeout_set(&adb_cuda_timeout, adb_cuda_tickle, NULL);
timeout_add(&adb_cuda_timeout, ADB_TICKLE_TICKS);
}
if (adbHardware != ADB_HW_PMU)
splx(s);
}
int
adb_cmd_result(u_char *in)
{
switch (adbHardware) {
case ADB_HW_CUDA:
if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
return 0;
if ((in[1] == 0x01) && (in[2] == 0x03))
return 0;
return 1;
case ADB_HW_PMU:
return 1;
default:
return 1;
}
}
int
adb_op_sync(Ptr buffer, short command)
{
int tmout;
int result;
volatile int flag = 0;
result = adb_op(buffer, (void *)adb_op_comprout,
(void *)&flag, command);
if (result == 0) {
for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
delay(10);
if (!flag && tmout > 0)
delay(tmout);
if (!flag)
result = -2;
}
return result;
}
void
adb_op_comprout(caddr_t buffer, caddr_t compdata, int cmd)
{
*(int *)compdata = 0x01;
}
int
count_adbs(struct adb_softc *sc)
{
int i;
int found;
found = 0;
for (i = 1; i < 16; i++)
if (0 != sc->sc_devtable[i].handler_id)
found++;
return found;
}
int
get_ind_adb_info(struct adb_softc *sc, ADBDataBlock * info, int index)
{
if ((index < 1) || (index > 15))
return (-1);
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("index 0x%x handler id 0x%x\n", index,
sc->sc_devtable[index].handler_id);
#endif
if (0 == sc->sc_devtable[index].handler_id)
return (-1);
info->devType = sc->sc_devtable[index].handler_id;
info->origADBAddr = sc->sc_devtable[index].orig_addr;
info->dbServiceRtPtr = (Ptr)sc->sc_devtable[index].handler;
info->dbDataAreaAddr = (Ptr)sc->sc_devtable[index].data;
return (sc->sc_devtable[index].curr_addr);
}
int
get_adb_info(ADBDataBlock * info, int adbAddr)
{
struct adb_softc *sc = adb_cd.cd_devs[0];
int i;
if (sc == NULL)
return (-1);
if ((adbAddr < 1) || (adbAddr > 15))
return (-1);
for (i = 1; i < 15; i++)
if (sc->sc_devtable[i].curr_addr == adbAddr) {
info->devType = sc->sc_devtable[i].handler_id;
info->origADBAddr = sc->sc_devtable[i].orig_addr;
info->dbServiceRtPtr = (Ptr)sc->sc_devtable[i].handler;
info->dbDataAreaAddr = sc->sc_devtable[i].data;
return 0;
}
return (-1);
}
int
set_adb_info(ADBSetInfoBlock * info, int adbAddr)
{
struct adb_softc *sc = adb_cd.cd_devs[0];
int i;
if (sc == NULL)
return (-1);
if ((adbAddr < 1) || (adbAddr > 15))
return (-1);
for (i = 1; i < 15; i++)
if (sc->sc_devtable[i].curr_addr == adbAddr) {
sc->sc_devtable[i].handler =
(void *)(info->siServiceRtPtr);
sc->sc_devtable[i].data = info->siDataAreaAddr;
return 0;
}
return (-1);
}
int
adb_read_date_time(time_t *time)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
int retcode;
volatile int flag = 0;
u_int32_t t;
switch (adbHardware) {
case ADB_HW_PMU:
pm_read_date_time(time);
retcode = 0;
break;
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x03;
result = send_adb_cuda((u_char *)output, (u_char *)output,
(void *)adb_op_comprout, (void *)&flag, (int)0);
if (result != 0) {
retcode = -1;
break;
}
while (0 == flag)
;
delay(20);
memcpy(&t, output + 1, sizeof(t));
*time = (time_t)t;
retcode = 0;
break;
case ADB_HW_UNKNOWN:
default:
retcode = -1;
break;
}
if (retcode == 0) {
#define DIFF19041970 2082844800
*time -= DIFF19041970;
} else {
*time = 0;
}
return retcode;
}
int
adb_set_date_time(time_t time)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
volatile int flag = 0;
u_int32_t t;
time += DIFF19041970;
switch (adbHardware) {
case ADB_HW_CUDA:
t = time;
output[0] = 0x06;
output[1] = 0x01;
output[2] = 0x09;
output[3] = (u_char)(t >> 24);
output[4] = (u_char)(t >> 16);
output[5] = (u_char)(t >> 8);
output[6] = (u_char)(t);
result = send_adb_cuda((u_char *)output, NULL,
(void *)adb_op_comprout, (void *)&flag, (int)0);
if (result != 0)
return -1;
while (0 == flag)
;
return 0;
case ADB_HW_PMU:
pm_set_date_time(time);
return 0;
default:
return -1;
}
}
int
adb_poweroff(void)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
adb_polling = 1;
switch (adbHardware) {
case ADB_HW_PMU:
pmu_fileserver_mode(0);
pm_adb_poweroff();
for (;;)
;
return 0;
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x0a;
result = send_adb_cuda((u_char *)output, NULL,
NULL, NULL, (int)0);
if (result != 0)
return -1;
for (;;)
;
return 0;
default:
return -1;
}
}
void
setsoftadb(void)
{
if (!timeout_initialized(&adb_softintr_timeout))
timeout_set(&adb_softintr_timeout, (void *)adb_soft_intr, NULL);
timeout_add(&adb_softintr_timeout, 1);
}
void
adb_cuda_autopoll(void)
{
volatile int flag = 0;
int result;
u_char output[16];
output[0] = 0x03;
output[1] = 0x01;
output[2] = 0x01;
output[3] = 0x01;
result = send_adb_cuda(output, output, adb_op_comprout,
(void *)&flag, 0);
if (result != 0)
return;
while (flag == 0);
}
void
adb_cuda_fileserver_mode(void)
{
volatile int flag = 0;
int result;
u_char output[16];
output[0] = 0x03;
output[1] = 0x01;
output[2] = 0x13;
output[3] = 0x01;
result = send_adb_cuda(output, output, adb_op_comprout,
(void *)&flag, 0);
if (result != 0)
return;
while (flag == 0);
}
void
adb_restart(void)
{
int result;
u_char output[16];
adb_polling = 1;
switch (adbHardware) {
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x11;
result = send_adb_cuda((u_char *)output, NULL,
NULL, NULL, (int)0);
if (result != 0)
return;
while (1);
case ADB_HW_PMU:
pm_adb_restart();
while (1);
}
}
int adbmatch(struct device *, void *, void *);
void adbattach(struct device *, struct device *, void *);
const struct cfattach adb_ca = {
sizeof(struct adb_softc), adbmatch, adbattach
};
int
adbmatch(struct device *parent, void *cf, void *aux)
{
struct confargs *ca = aux;
if (ca->ca_nreg < 8)
return 0;
if (ca->ca_nintr < 4)
return 0;
if (strcmp(ca->ca_name, "via-cuda") == 0)
return 1;
if (strcmp(ca->ca_name, "via-pmu") == 0)
return 1;
return 0;
}
void
adbattach(struct device *parent, struct device *self, void *aux)
{
struct adb_softc *sc = (struct adb_softc *)self;
struct confargs *ca = aux;
struct confargs nca;
char name[32];
int node;
ADBDataBlock adbdata;
struct adb_attach_args aa_args;
int totaladbs;
int adbindex, adbaddr;
#if !defined(SMALL_KERNEL) && defined(SUSPEND)
adb_suspendq = taskq_create(sc->sc_dev.dv_xname, 1, IPL_TTY, 0);
if (adb_suspendq == NULL) {
printf(": can't create taskq\n");
return;
}
#endif
ca->ca_reg[0] += ca->ca_baseaddr;
sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
Via1Base = sc->sc_regbase;
if (strcmp(ca->ca_name, "via-cuda") == 0)
adbHardware = ADB_HW_CUDA;
else if (strcmp(ca->ca_name, "via-pmu") == 0) {
adbHardware = ADB_HW_PMU;
pm_in_adbattach(sc->sc_dev.dv_xname);
adbempty = 1;
for (node = OF_child(ca->ca_node); node; node = OF_peer(node)) {
if (OF_getprop(node, "name", name, sizeof name) <= 0)
continue;
if (strcmp(name, "adb") == 0) {
adbempty = 0;
break;
}
}
}
adb_polling = 1;
if (!adbempty) {
adb_reinit(sc);
totaladbs = count_adbs(sc);
printf(": irq %d, %s, %d target%s", ca->ca_intr[0], ca->ca_name,
totaladbs, (totaladbs == 1) ? "" : "s");
}
printf("\n");
mac_intr_establish(parent, ca->ca_intr[0], IST_LEVEL, IPL_TTY,
adb_intr, sc, sc->sc_dev.dv_xname);
time_read = adb_read_date_time;
time_write = adb_set_date_time;
#if NAPM > 0
if (adbHardware == ADB_HW_PMU) {
nca.ca_name = "apm";
nca.ca_node = node;
config_found(self, &nca, NULL);
}
#endif
for (node = OF_child(ca->ca_node); node; node = OF_peer(node)) {
if (OF_getprop(node, "name", name, sizeof name) <= 0)
continue;
if (strcmp(name, "pmu-i2c") == 0) {
nca.ca_name = "piic";
nca.ca_node = node;
config_found(self, &nca, NULL);
}
}
if (adbHardware == ADB_HW_CUDA)
adb_cuda_fileserver_mode();
if (adbHardware == ADB_HW_PMU) {
wskbd_get_backlight = pmu_get_backlight;
wskbd_set_backlight = pmu_set_backlight;
pmu_fileserver_mode(1);
}
if (adbempty)
return;
for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
adbaddr = get_ind_adb_info(sc, &adbdata, adbindex);
aa_args.name = adb_device_name;
aa_args.origaddr = adbdata.origADBAddr;
aa_args.adbaddr = adbaddr;
aa_args.handler_id = adbdata.devType;
(void)config_found(self, &aa_args, adbprint);
}
if (adbHardware == ADB_HW_CUDA)
adb_cuda_autopoll();
adb_polling = 0;
}
int
pmu_get_backlight(struct wskbd_backlight *kbl)
{
kbl->min = 0;
kbl->max = 0xff;
kbl->curval = pmu_backlight;
return 0;
}
int
pmu_set_backlight(struct wskbd_backlight *kbl)
{
pmu_backlight = kbl->curval;
return pmu_set_kbl(pmu_backlight);
}