- "whitespace change" that coverts existing driver example code
to be style(9) compliant, matching format of new driver code Approved: blackend (mentor)
This commit is contained in:
parent
c6674adc3d
commit
0faa41e1ff
Notes:
svn2git
2020-12-08 03:00:23 +00:00
svn path=/head/; revision=18239
1 changed files with 79 additions and 79 deletions
|
@ -181,7 +181,7 @@ KMOD=skeleton
|
||||||
<title>Example of a Sample Echo Pseudo-Device Driver for
|
<title>Example of a Sample Echo Pseudo-Device Driver for
|
||||||
&os; 4.X</title>
|
&os; 4.X</title>
|
||||||
|
|
||||||
<programlisting>/*
|
<programlisting>/*
|
||||||
* Simple `echo' pseudo-device KLD
|
* Simple `echo' pseudo-device KLD
|
||||||
*
|
*
|
||||||
* Murray Stokely
|
* Murray Stokely
|
||||||
|
@ -191,7 +191,7 @@ KMOD=skeleton
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/module.h>
|
#include <sys/module.h>
|
||||||
#include <sys/systm.h> /* uprintf */
|
#include <sys/systm.h> /* uprintf */
|
||||||
#include <sys/errno.h>
|
#include <sys/errno.h>
|
||||||
#include <sys/param.h> /* defines used in kernel.h */
|
#include <sys/param.h> /* defines used in kernel.h */
|
||||||
#include <sys/kernel.h> /* types used in module initialization */
|
#include <sys/kernel.h> /* types used in module initialization */
|
||||||
|
@ -202,32 +202,32 @@ KMOD=skeleton
|
||||||
#define BUFFERSIZE 256
|
#define BUFFERSIZE 256
|
||||||
|
|
||||||
/* Function prototypes */
|
/* Function prototypes */
|
||||||
d_open_t echo_open;
|
d_open_t echo_open;
|
||||||
d_close_t echo_close;
|
d_close_t echo_close;
|
||||||
d_read_t echo_read;
|
d_read_t echo_read;
|
||||||
d_write_t echo_write;
|
d_write_t echo_write;
|
||||||
|
|
||||||
/* Character device entry points */
|
/* Character device entry points */
|
||||||
static struct cdevsw echo_cdevsw = {
|
static struct cdevsw echo_cdevsw = {
|
||||||
echo_open,
|
echo_open,
|
||||||
echo_close,
|
echo_close,
|
||||||
echo_read,
|
echo_read,
|
||||||
echo_write,
|
echo_write,
|
||||||
noioctl,
|
noioctl,
|
||||||
nopoll,
|
nopoll,
|
||||||
nommap,
|
nommap,
|
||||||
nostrategy,
|
nostrategy,
|
||||||
"echo",
|
"echo",
|
||||||
33, /* reserved for lkms - /usr/src/sys/conf/majors */
|
33, /* reserved for lkms - /usr/src/sys/conf/majors */
|
||||||
nodump,
|
nodump,
|
||||||
nopsize,
|
nopsize,
|
||||||
D_TTY,
|
D_TTY,
|
||||||
-1
|
-1
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_echo {
|
struct s_echo {
|
||||||
char msg[BUFFERSIZE];
|
char msg[BUFFERSIZE];
|
||||||
int len;
|
int len;
|
||||||
} t_echo;
|
} t_echo;
|
||||||
|
|
||||||
/* vars */
|
/* vars */
|
||||||
|
@ -240,76 +240,77 @@ MALLOC_DECLARE(M_ECHOBUF);
|
||||||
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");
|
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function acts is called by the kld[un]load(2) system calls to
|
* This function is called by the kld[un]load(2) system calls to
|
||||||
* determine what actions to take when a module is loaded or unloaded.
|
* determine what actions to take when a module is loaded or unloaded.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
echo_loader(struct module *m, int what, void *arg)
|
echo_loader(struct module *m, int what, void *arg)
|
||||||
{
|
{
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
switch (what) {
|
switch (what) {
|
||||||
case MOD_LOAD: /* kldload */
|
case MOD_LOAD: /* kldload */
|
||||||
sdev = make_dev(<literal>&</literal>echo_cdevsw,
|
sdev = make_dev(<literal>&</literal>echo_cdevsw,
|
||||||
0,
|
0,
|
||||||
UID_ROOT,
|
UID_ROOT,
|
||||||
GID_WHEEL,
|
GID_WHEEL,
|
||||||
0600,
|
0600,
|
||||||
"echo");
|
"echo");
|
||||||
/* kmalloc memory for use by this driver */
|
/* kmalloc memory for use by this driver */
|
||||||
/* malloc(256,M_ECHOBUF,M_WAITOK); */
|
MALLOC(echomsg, t_echo *, sizeof(t_echo), M_ECHOBUF, M_WAITOK);
|
||||||
MALLOC(echomsg, t_echo *, sizeof(t_echo), M_ECHOBUF, M_WAITOK);
|
printf("Echo device loaded.\n");
|
||||||
printf("Echo device loaded.\n");
|
break;
|
||||||
break;
|
case MOD_UNLOAD:
|
||||||
case MOD_UNLOAD:
|
destroy_dev(sdev);
|
||||||
destroy_dev(sdev);
|
FREE(echomsg,M_ECHOBUF);
|
||||||
FREE(echomsg,M_ECHOBUF);
|
printf("Echo device unloaded.\n");
|
||||||
printf("Echo device unloaded.\n");
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
err = EINVAL;
|
||||||
err = EINVAL;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
return(err);
|
||||||
return(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
echo_open(dev_t dev, int oflags, int devtype, struct proc *p)
|
echo_open(dev_t dev, int oflags, int devtype, struct proc *p)
|
||||||
{
|
{
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
uprintf("Opened device \"echo\" successfully.\n");
|
uprintf("Opened device \"echo\" successfully.\n");
|
||||||
return(err);
|
return(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
echo_close(dev_t dev, int fflag, int devtype, struct proc *p)
|
echo_close(dev_t dev, int fflag, int devtype, struct proc *p)
|
||||||
{
|
{
|
||||||
uprintf("Closing device \"echo.\"\n");
|
uprintf("Closing device \"echo.\"\n");
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The read function just takes the buf that was saved via
|
* The read function just takes the buf that was saved via
|
||||||
* echo_write() and returns it to userland for accessing.
|
* echo_write() and returns it to userland for accessing.
|
||||||
* uio(9)
|
* uio(9)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
echo_read(dev_t dev, struct uio *uio, int ioflag)
|
echo_read(dev_t dev, struct uio *uio, int ioflag)
|
||||||
{
|
{
|
||||||
int err = 0;
|
int err = 0;
|
||||||
int amt;
|
int amt;
|
||||||
|
|
||||||
/* How big is this read operation? Either as big as the user wants,
|
/*
|
||||||
or as big as the remaining data */
|
* How big is this read operation? Either as big as the user wants,
|
||||||
amt = MIN(uio->uio_resid, (echomsg->len - uio->uio_offset > 0) ? echomsg->len - uio->uio_offset : 0);
|
* or as big as the remaining data
|
||||||
if ((err = uiomove(echomsg->msg + uio->uio_offset,amt,uio)) != 0) {
|
*/
|
||||||
uprintf("uiomove failed!\n");
|
amt = MIN(uio->uio_resid, (echomsg->len - uio->uio_offset > 0) ?
|
||||||
}
|
echomsg->len - uio->uio_offset : 0);
|
||||||
|
if ((err = uiomove(echomsg->msg + uio->uio_offset,amt,uio)) != 0) {
|
||||||
return err;
|
uprintf("uiomove failed!\n");
|
||||||
|
}
|
||||||
|
return(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -320,22 +321,21 @@ echo_read(dev_t dev, struct uio *uio, int ioflag)
|
||||||
int
|
int
|
||||||
echo_write(dev_t dev, struct uio *uio, int ioflag)
|
echo_write(dev_t dev, struct uio *uio, int ioflag)
|
||||||
{
|
{
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
/* Copy the string in from user memory to kernel memory */
|
/* Copy the string in from user memory to kernel memory */
|
||||||
err = copyin(uio->uio_iov->iov_base, echomsg->msg, MIN(uio->uio_iov->iov_len,BUFFERSIZE));
|
err = copyin(uio->uio_iov->iov_base, echomsg->msg,
|
||||||
|
MIN(uio->uio_iov->iov_len,BUFFERSIZE));
|
||||||
|
|
||||||
/* Now we need to null terminate */
|
/* Now we need to null terminate, then record the length */
|
||||||
*(echomsg->msg + MIN(uio->uio_iov->iov_len,BUFFERSIZE)) = 0;
|
*(echomsg->msg + MIN(uio->uio_iov->iov_len,BUFFERSIZE)) = 0;
|
||||||
/* Record the length */
|
echomsg->len = MIN(uio->uio_iov->iov_len,BUFFERSIZE);
|
||||||
echomsg->len = MIN(uio->uio_iov->iov_len,BUFFERSIZE);
|
|
||||||
|
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
uprintf("Write failed: bad address!\n");
|
uprintf("Write failed: bad address!\n");
|
||||||
}
|
}
|
||||||
|
count++;
|
||||||
count++;
|
return(err);
|
||||||
return(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DEV_MODULE(echo,echo_loader,NULL);</programlisting>
|
DEV_MODULE(echo,echo_loader,NULL);</programlisting>
|
||||||
|
|
Loading…
Reference in a new issue