[comp.unix.xenix] Device Driver Example for XENIX

lee@minnow.UUCP (Gene Lee ) (08/17/87)

       This is a repost because I think the first one got lost.
         If not, Sooorrrrry.

        Enough people have sent me mail asking to see how a wrote
   a xenix device driver I decided to post what I had.  This is a collection
   of all the steps and files I used.  If I made a mistake or left something
   out, please let me know.  I dont know if this is applicable to IBM's
   xenix.

***************************************************************************
***************************************************************************
          *NOTE* this driver was written for SCO xenix 

***************************************************************************
***************************************************************************
          the following is a list of steps used to modify system
          files:

1)      add the deivces name to the xenixconf file
	example:
		.	
		.	
		.	
	     scsi    1         /* this is an existing line */
	     prot    1         /* this is the one I added */
				*NOTE prot is the same four chars on the
				   front of all my driver procedure names


2)     add the device to the master file
	 example:
		.	
		.	
		.	
	scsi	0  0137  014   ........../* this is an existing line */
	prot	0  0137	 104  prot  0   0   30  1  0   0   0   0  0

3)      fix up the make file adding our proto.o to the CONF= line
        example:
		CONF= oemsup.o ........ {/path/}proto.o

4)      edit the link_xenix file adding our proto.o to the ld line
	example: 
		ld -Mm -D ........ {/path/}proto.o

5)       make special file with mknod in the /dev directory
	example:
		mknod <filename>

6)       type make

7)       type hdinstall

8)       reboot



***************************************************************************
***************************************************************************
       this is the make file for proto.c (the device driver)

all: proto.o 
	touch all

CFLAGS = -K -M2em -O -DM_KERNEL -DNETWORK -UM_I86

proto.o: proto.c const.h
	cc $(CFLAGS) -c proto.c

***************************************************************************
***************************************************************************
     this is const.h .  It is used by proto.c

#define ATOD 0x304
#define DTOA 0x308
#define PORTA 0x300
#define PORTB 0x301
#define PORTC 0x302
#define PORTCNTRL 0x303 
#define ALLOUT 0x80
#define ALLIN 0x9B
#define IN_IN_OUT  0x9A
#define OUT_IN_OUT 0x8A
#define IN_OUT_OUT 0x98

#define dev_porta 0 
#define dev_portb 1
#define dev_portc 2
#define dev_atod 3
#define dev_dtoa 4

***************************************************************************
***************************************************************************
     this is proto.c .  It is the device driver code  

#include <sys/relsym86.h>
#include <sys/param.h>
#include <sys/dir.h>
#include <sys/user.h>
#include <sys/file.h>
#include <sys/conf.h>
#include <errno.h>
#include "const.h"         /* constants which define hardware adresses */

/*
          *NOTE* the value dev passed to the device driver is the
                 minor device number as defined by using the mknod
		 command
*/

protpoll(ps)
int ps;
{
/*   
   printf("In poll routime\n");
*/
}

/* ____________________________________________________________________ */

protopen(dev,flag)
int dev;
int flag;
{
/*   
   printf("In open routime\n");
*/
}


/* ____________________________________________________________________ */

protclose(dev)
int dev;
{
/*   
   printf("In close routime\n");
*/
}


/* ____________________________________________________________________ */

protread(dev)
int dev;
{
   int x;

   switch (dev) {    /* what is the minor device number of the special file */
   case dev_atod:
      x =in(ATOD);             /* read  a byte from the card */
      break;
   case dev_porta:
      x =in(PORTA);
      break;
   case dev_portb:
      x =in(PORTB);
      break;
   case dev_portc:
      x =in(PORTC);
      break;
   }
   passc(x);               /* return the byte read in from the card */
}


/* ____________________________________________________________________ */

protwrite(dev)
int dev;
{
   int x;

   x = cpass();                /* get the value passed by the user call */

   switch(dev) {
   case dev_dtoa:
      out(DTOA, x);            /* write the by to the card */
      break;
   case dev_atod:
      out(ATOD, x);
      break;
   case dev_porta:
      out(PORTA, x);
      break;
   case dev_portb:
      out(PORTB, x);
      break;
   case dev_portc:
      out(PORTC, x);
      break;
   }
}


/* ____________________________________________________________________ */

protparam(dev)
int dev;
{

   printf("in proto param\n");
}


/* ____________________________________________________________________ */

protintr(vec)
int vec;
{

/*
   printf("in proto intr\n");
*/
}


/* ____________________________________________________________________ */

protioctl(dev, request, arg)
int dev;
int request;
faddr_t arg;
{

   printf("in proto ioctl %d, %d\n", dev, request);
}


/* ____________________________________________________________________ */

protinit()
{

   out(PORTCNTRL, OUT_IN_OUT);         /* set up the proto board's PIA */

   out(PORTC, 0);     /* select A-TO-D channel number = 0 */
   in(ATOD);      /* dummy read to latch in channel */ 
/* delay(Hz * 0.1); */  /* delay 100 ms */

   printf("PROTO BOARD Initialized\n");
}

***************************************************************************
***************************************************************************
             this is a half baked example of using the device driver

main()
{
          int fp;

          fp = open("/dev/{filename}" ,"rw");
	  read(fp, buffer, 1);     /* do a inb() => buffer */
	  write(fp,buffer, 1);     /* do a outb(buffer)    */
	  close(fp);
}


***************************************************************************
********************************* THE END *********************************
-- 
Gene Lee UUCP: ...ihnp4!{meccts,dayton,rosevax}!ems!minnow!lee
UNISYS Corporation     ATT:  (612) 635-6334
If not for the courage of the fearless manager, the paycheck would be lost.