/*-
 * Copyright (c) 1998 Takanori Watanabe
 * 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 AUTHOR 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 AUTHOR 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.
 *
 *	[id for your version control system, if any]
 */
/*This program is prototype version. I'll write better one*/
#include <stdlib.h>
#include <fcntl.h>
#include <machine/smb.h>
const double vfactor[]={1,1,1,1.67,4,-4,-1.67};
const char *Inpname[]={
  "Vcore","Vit","VIO","+5V","+12V","-12V","-5V"
};
int main (int argc,char argv[])
{
	int alias;
	unsigned char byte=0;
	struct smbcmd cmd;
	int fandiv[3];
	bzero(&cmd, sizeof(cmd));
	cmd.data.byte_ptr = &byte;
	cmd.slave=0x5a;  /*LM79 Default value*/

	alias = open("/dev/smb0", O_RDWR);
	{
	  static const fandivide[]={1,2,4,8};

	  cmd.cmd=0x47;
	  if(ioctl(alias, SMB_READB, (caddr_t)&cmd)==-1){
	    perror("IOCTL");
	    return -1;
	  }
	  printf("%x\n",byte);
	  fandiv[0]=fandivide[(byte>>4)&3];
	  fandiv[1]=fandivide[(byte>>6)&3];
	  fandiv[2]=2;
	  printf("fan1:/%d fan2:/%d\n",fandiv[0],fandiv[1]);
	  cmd.data.byte_ptr=&byte;

	}
	for(;;){
	  int i;
	  printf("Voltage:\n");
	  for(i=0x20;i<0x27;i++){
	    cmd.cmd=i;
	    if(ioctl(alias, SMB_READB, (caddr_t)&cmd)==-1){
	      perror("IOCTL");
	      return -1;
	    }
	    printf("%s:%1.3f V ",Inpname[i-0x20],byte*vfactor[i-0x20]/64.);
	  }
	  printf("\n");
	  printf("Temprature:");
	  cmd.cmd=0x27;
	  if(ioctl(alias, SMB_READB, (caddr_t)&cmd)==-1){
	    perror("IOCTL");
	    return -1;
	  }
	  printf("%d C\n",(int)byte);
	  printf("Fans:\n");
	  for(i=0x28;i<0x2b;i++){
	    cmd.cmd=i;
	    if(ioctl(alias, SMB_READB, (caddr_t)&cmd)==-1){
	      perror("IOCTL");
	      return -1;
	    }
	    printf("%d:",i-0x28);
	    if(byte!=0xff){
	      printf("%d rpm ",(int)(1.35E6/(byte*fandiv[i-0x28])));
	    }
	    else{
	      printf("Not Connected? ");
	    }
	  }
	  printf("\n");
	  sleep(1);
	}
	close(alias);
	return 0;
}


