Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: Sean Davidson <davidson@zk3.dec.com>
Subject: Re: Load loadable kernel driver
Date: Fri, 29 Jun 2001 16:58:34 -0400	[thread overview]
Message-ID: <3B3CEBFA.62619DE6@zk3.dec.com> (raw)
In-Reply-To: 2f6bec0a.0106280036.16304a21@posting.google.com

Here is an example of a most simple loadable module.  This example has
the different includes needed to compile V4 and V5 modules.  Missing
the *_attributes structure or the *_configure routine will cause missing
symbols.  Also not include all the correct header files will also cause
missing symbols.

You should really only copy the loadable module into the /subsys or
/var/subsys directories.


Sean Davidson
Compaq Tru64 UNIX Release Engineering


 = = = = = = = = /etc/sysconfigtab entries = = = = =

nhdzz:
  Debug = 1

 = = = = = = = = == = = = = = = =

/*
 *
 * Dummy nhd disk driver
 *
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/conf.h>
#include <sys/sysconfig.h>
#include <sys/disklabel.h>
#if defined(OSVERS_V4) || defined(OSVERS_V5)
#include <sys/malloc.h>
#include <sys/buf.h>
#if defined(OSVERS_V5)
#include <sys/common_protos.h>
#endif
#endif
#include <io/common/devio.h>
#include <io/common/devgetinfo.h>

#define NHD_NODEV	(-1)
#define NHD_INSTANCE	(1)
#define NHD_UNITMAX	255
#define NHD_DEV_NULL	(0)

#define nhdzz_unit(x)	((minor(x) >> 3) & 0x7)

struct nhdzz_devs {
	int		d_flags;		/* flags */
	size_t		d_size;			/* size of device */
	unsigned char *	d_buf;			/* controll xfer buffer */
};

/*
 * forward declarations
 */
int nodev(), nulldev();
int nhdzz_open(), nhdzz_close(), nhdzz_read(), nhdzz_write(), nhdzz_ioctl();
int nhdzz_dump(), nhdzz_size();
void nhdzz_strategy();

/*
 * softc structure
 */
struct nhdzz_softc {
	int		nhdzz_openflag;		/* Open flag */
	int		nhdzz_count;		/* Count chars out to device */
	int		nhdzz_state;		/* Device state */
} nhdzz_softc[1];

/*
 * other data
 */
int			nhdzz_config = FALSE;	/* configured status */
int			nhdzz_unit_count;	/* current number of units */
struct nhdzz_devs	*nhdzz_devs[NHD_UNITMAX]; /* disk device structures */
static unsigned char	nhdzz_data_buffer[65536]; /* disk data buffer */


#define NHDZZ_DEVS_NULL	((struct nhdzz_devs *) NULL)


/*
 * system configuration data
 */
static int		nhdzz_units = 2;
static int		nhdzz_majnum = NHD_NODEV;
#define nhdzz_bmajnum	nhdzz_majnum
#define nhdzz_cmajnum	nhdzz_majnum
static int		nhdzz_version = 1;
static int		nhdzz_debug = 0;
static unsigned char	nhdzz_cfgname[CFG_ATTR_NAME_SZ] = "NHDa Device";
static unsigned char	nhdzz_modtype[CFG_ATTR_NAME_SZ] = "Dynamic";

/*
 * device switch table
 */
struct dsent nhdzz_devsw_entry = {
	nhdzz_open,				/* d_open */
	nhdzz_close,				/* d_close */
	(int(*)(struct buf *))nhdzz_strategy,				/* d_strategy */
	nhdzz_read,				/* d_read */
	nhdzz_write,				/* d_write */
	nhdzz_ioctl,				/* d_ioctl */
	nhdzz_dump,				/* d_dump */
	nhdzz_size,				/* d_psize */
	nodev,					/* d_stop */
	nodev,					/* d_reset */
	nodev,					/* d_select */
	0,					/* d_mmap */
	0,					/* d_segmap */
	NULL,					/* d_ttys */
	DEV_FUNNEL_NULL,			/* d_funnell */
	0,					/* d_bflags */
	0					/* d_cflags */
};

/*
 * configuration attributes
 */
cfg_subsys_attr_t nhdzz_attributes[] = {

{"Module_Config_Name",  CFG_ATTR_STRTYPE, CFG_OP_QUERY,
			(caddr_t)nhdzz_cfgname, 2, CFG_ATTR_NAME_SZ, 0},

{"Module_Type",		CFG_ATTR_STRTYPE, CFG_OP_QUERY,
			(caddr_t)nhdzz_modtype, 2, CFG_ATTR_NAME_SZ, 0},

{"Number_of_units",	CFG_ATTR_INTTYPE, CFG_OP_QUERY |
			CFG_OP_RECONFIGURE | CFG_OP_CONFIGURE,
			(caddr_t)&nhdzz_units, 1, NHD_UNITMAX, 0},

{"Major_device_number",	CFG_ATTR_INTTYPE, CFG_OP_QUERY |
			CFG_OP_RECONFIGURE | CFG_OP_CONFIGURE,
			(caddr_t)&nhdzz_majnum, 0, 512, 0},

{"Version",		CFG_ATTR_INTTYPE, CFG_OP_QUERY,
			(caddr_t)&nhdzz_version, 0, 9999999, 0},

{"Debug",		CFG_ATTR_INTTYPE, CFG_OP_QUERY |
			CFG_OP_RECONFIGURE | CFG_OP_CONFIGURE,
			(caddr_t)&nhdzz_debug, 0, 255, 0},

{"", 0, 0, 0, 0, 0, 0}
};


/*
 * configure
 */
int
nhdzz_configure(optype, indata, indatalen, outdata, outdatalen)
	cfg_op_t optype;
	cfg_attr_t *indata;
	size_t indatalen;
	cfg_attr_t *outdata;
	size_t outdatalen;
{
	int i, error = ESUCCESS;
	unsigned char *buf;
	struct nhdzz_devs *vdisk;

	switch (optype) {

		case CFG_OP_CONFIGURE:

			/*
			 * check for device already configured
			 */
			if (nhdzz_config == TRUE) {
				error = EBUSY;
				break;
			}

			/*
			 * set device configured
			 */
			nhdzz_config = TRUE;
			printf("nhdzz: configured\n");
			printf("nhdzz: debug = %d\n", nhdzz_debug);
			break;

		case CFG_OP_UNCONFIGURE:

			/*
			 * make sure configuration was successfull
			 */
			if (nhdzz_config != TRUE) {
				error = ENXIO;
				break;
			}

			/*
			 * set device unconfigured
			 */
			nhdzz_config = FALSE;
			printf("nhdzz: unconfigured\n");
			break;

		case CFG_OP_QUERY:
			break;

		case CFG_OP_RECONFIGURE:

			/*
			 * don't allow the number of units to get smaller
			 */
			if (nhdzz_units < nhdzz_unit_count) {
				error = EINVAL;
			}
			break;

		default:
			error = EINVAL;
			break;
	}
	return(error);
}

/*
 * open
 */
int
nhdzz_open(dev, flags, mode)
	dev_t dev;
	int flags;
	int mode;
{
	int unit;

	/*
	 * make sure unit is in range
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(ENXIO);
	return(0);
}

/*
 * close
 */
int
nhdzz_close(dev, flags, mode)
	dev_t dev;
	int flags;
	int mode;
{
	int unit;

	/*
	 * verify unit and device structure
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(ENXIO);
	return(0);
}

/*
 * block read/write
 */
void nhdzz_strategy(bp)
	struct buf *bp;
{
	struct partition *pp;
	int bn, sz;
	int unit;

	/*
	 * check for good unit and being online
	 */
	unit = nhdzz_unit(bp->b_dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL) {
		bp->b_error = ENXIO;
		bp->b_flags |= B_ERROR;
		biodone(bp);
		return;
	}

	bp->b_resid = 0;
	biodone(bp);
	return;
}

/*
 * raw read
 */
int
nhdzz_read(dev, uio)
	dev_t dev;
	struct uio *uio;
{
	int unit, error;
	struct buf *bp;

	/*
	 * verify unit and device structure
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(ENXIO);

	/*
	 * get a buffer for i/o and just do a physical i/o read
	 */
	MALLOC(bp, struct buf *, sizeof(struct buf), M_DEVBUF, M_WAITOK);
	bzero(bp, sizeof(struct buf));
	error = physio(nhdzz_strategy, bp, dev, B_READ, minphys, uio);
	FREE(bp, M_DEVBUF);
	return(error);
}

/*
 * raw write
 */
int
nhdzz_write(dev, uio)
	dev_t dev;
	struct uio *uio;
{
	int unit, error;
	struct buf *bp;

	/*
	 * verify unit and device structure
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(ENXIO);

	/*
	 * get a buffer for i/o and just do a physical i/o write
	 */
	MALLOC(bp, struct buf *, sizeof(struct buf), M_DEVBUF, M_WAITOK);
	bzero(bp, sizeof(struct buf));
	error = physio(nhdzz_strategy, bp, dev, B_WRITE, minphys, uio);
	FREE(bp, M_DEVBUF);
	return(error);
}

/*
 * size
 */
int
nhdzz_size(dev)
	dev_t dev;
{
	int unit;

	/*
	 * verify unit and device structure
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(-1);

	/*
	 * return size of virtual disk
	 */
	return(2097152);
}

/*
 * dump
 */
int
nhdzz_dump(dev)
	dev_t dev;
{
	return(ENXIO);
}

/*
 * ioctl
 */
nhdzz_ioctl(dev, cmd, data, flag)
	dev_t dev;
	u_long cmd;
	caddr_t data;
	int flag;
{
	int unit, flags, size, i, junk, error = 0;
	unsigned long addr;
	struct nhdzz_devs *vdisk;
	struct nhdzz_ioctl *ioctl_data = (struct nhdzz_ioctl *) data;
	DEVGEOMST *devgeom;
	v1_device_info_t *devi_p;
	v1_disk_dev_info_t *diskp;
	struct devget *devget;
	struct disklabel *lp;
	struct partition *pp;
	struct pt_tbl *pt;
	struct buf *bp;

	/*
	 * trim the ioctl command and verify user is super user
	 */
	cmd = (cmd & 0xffffffff);
	if (error = suser(u.u_cred, &u.u_acflag))
		return(error);

	/*
	 * verify that unit is valid
	 */
	unit = nhdzz_unit(dev);
	if (unit < 0 || unit >= nhdzz_units ||
	    nhdzz_devs[unit] == NHDZZ_DEVS_NULL)
		return(ENXIO);

	/*
	 * process ioctl command
	 */
	switch (cmd) {

	case DIOCGDINFO:

		break;

	case DIOCGPART:

		break;

	case DIOCSDINFO:

		break;

	case DIOCWLABEL:

		break;

	case DIOCWDINFO:

		break;

	case DIOCGDEFPT:

		break;

	case DIOCGCURPT:

		break;

	case DEVIOCGET:

		break;

	case DEVGETINFO:

		break;

	case DEVGETGEOM:

		break;

	default:
		error = ENXIO;
	}
	return(error);
}


      parent reply	other threads:[~2001-06-29 20:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-28  8:36 Load loadable kernel driver Ginny E. Kim
2001-06-29 12:03 ` Andrew Duane USG
2001-07-06  6:50   ` Ginny E. Kim
2001-06-29 13:46 ` Joe Mario
2001-07-02  6:39   ` Ginny E. Kim
2001-07-06 19:55     ` Joe Mario
2001-07-09  2:10       ` Ginny E. Kim
2001-07-09 14:08         ` Brian Haley
2001-07-10  4:29           ` Ginny E. Kim
2001-07-10 15:46             ` Brian Haley
2001-07-09 14:57         ` Joe Mario
2001-06-29 20:58 ` Sean Davidson [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox