summaryrefslogtreecommitdiffstats
path: root/share/examples/drivers/make_device_driver.sh
blob: a614c8ec2fe54f2fd870f896f639875cbe6bff49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/sh 
# This writes a skeleton driver and puts it into the kernel tree for you
#arg1 is lowercase "foo" 
#
# It also creates a directory under /usr/src/lkm to help you create
#loadable kernel modules, though without much use except for development.
#
# Trust me, RUN THIS SCRIPT :)
# $FreeBSD$"
#
#-------cut here------------------
if [ "${1}X" = "X" ] 
then
	echo "Hey , how about some help here.. give me a device name!"
	exit 1
fi
UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"` 

HERE=`pwd`
cd /sys
TOP=`pwd`

echo ${TOP}/modules/${1}
echo ${TOP}/i386/conf/files.${UPPER}
echo ${TOP}/i386/conf/${UPPER}
echo ${TOP}/dev/${1}
echo ${TOP}/dev/${1}/${1}.c
echo ${TOP}/sys/${1}io.h
echo ${TOP}/modules/${1}
echo ${TOP}/modules/${1}/Makefile

rm -rf ${TOP}/dev/${1}
rm -rf ${TOP}/modules/${1}
rm ${TOP}/i386/conf/files.${UPPER}
rm ${TOP}/i386/conf/${UPPER}
rm ${TOP}/sys/${1}io.h

if [ -d ${TOP}/modules/${1} ]
then
	echo "There appears to already be a module called ${1}"
	exit 1
else
	mkdir ${TOP}/modules/${1}
fi

#######################################################################
#######################################################################
#
# Create configuration information needed to create a kernel
# containing this driver.
#
# Not really needed if we are going to do this as a module.
#######################################################################
# First add the file to a local file list.
#######################################################################

cat >${TOP}/i386/conf/files.${UPPER} <<DONE
i386/isa/${1}.c	 optional ${1} device-driver
DONE

#######################################################################
# Then create a configuration file for a kernel that contains this driver.
#######################################################################
cat >${TOP}/i386/conf/${UPPER} <<DONE
# Configuration file for kernel type: ${UPPER}
ident	${UPPER}
# \$FreeBSD$"
DONE

grep -v GENERIC < /sys/i386/conf/GENERIC >>${TOP}/i386/conf/${UPPER}

cat >>${TOP}/i386/conf/${UPPER} <<DONE
options	DDB		# trust me, you'll need this
device ${1} at isa?
DONE

if [ ! -d ${TOP}/dev/${1} ]
then
	mkdir -p ${TOP}/dev/${1}
fi


















cat >${TOP}/dev/${1}/${1}.c <<DONE
/*
 * Copyright ME
 *
 * ${1} driver
 * \$FreeBSD$
 */


#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>		/* cdevsw stuff */
#include <sys/kernel.h>		/* SYSINIT stuff */
#include <sys/uio.h>		/* SYSINIT stuff */
#include <sys/malloc.h>		/* malloc region definitions */
#include <sys/module.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <sys/time.h>

#include <isa/isavar.h>
#include "isa_if.h"
#include <sys/${1}io.h>		/* ${1} IOCTL definitions */

#define ${UPPER}DEV2SOFTC(dev) ((dev)->si_drv1)
#define ${UPPER}_INB(port) bus_space_read_1( bt, bh, (port))
#define ${UPPER}_OUTB(port, val) bus_space_write_1( bt, bh, (port), (val))
#define SOME_PORT 123
#define EXPECTED_VALUE 0x42


/* Function prototypes (these should all be static) */
static int ${1}_isa_probe (device_t);
static int ${1}_isa_attach (device_t);
static int ${1}_isa_detach (device_t);

static d_open_t		${1}open;
static d_close_t	${1}close;
static d_read_t		${1}read;
static d_write_t	${1}write;
static d_ioctl_t	${1}ioctl;
static d_mmap_t		${1}mmap;
static d_poll_t		${1}poll;
#ifdef ${UPPER}_MODULE
static	ointhand2_t	${1}intr; /* should actually have type inthand2_t */
#endif
 
#define CDEV_MAJOR 20
static struct cdevsw ${1}_cdevsw = {
	/* open */	${1}open,
	/* close */	${1}close,
	/* read */	${1}read,
	/* write */	${1}write,
	/* ioctl */	${1}ioctl,
	/* poll */	${1}poll,
	/* mmap */	${1}mmap,
	/* strategy */	nostrategy,	/* not a block type device */
	/* name */	"${1}",
	/* maj */	CDEV_MAJOR,
	/* dump */	nodump,		/* not a block type device */
	/* psize */	nopsize,	/* not a block type device */
	/* flags */	0,
	/* bmaj */	-1
};
 
/* 
 * device specific Misc defines 
 */
#define BUFFERSIZE 1024
#define NUMPORTS 4

/*
 * One of these per allocated device
 */
struct ${1}_softc {
	bus_space_tag_t bt;
	bus_space_handle_t bh;
	int port_rid;
	struct resource* port_res;	/* resource for port range */
	dev_t dev;
	device_t device;
	char	buffer[BUFFERSIZE];
} ;

typedef	struct ${1}_softc *sc_p;

devclass_t ${1}_devclass;

static struct isa_pnp_id ${1}_ids[] = {
	{0x12345678, "ABCco Widget"},
	{0xfedcba98, "shining moon Widget ripoff"},
	{0}
};

static device_method_t ${1}_methods[] = {
	DEVMETHOD(device_probe,		${1}_isa_probe),
	DEVMETHOD(device_attach,	${1}_isa_attach),
	DEVMETHOD(device_detach,	${1}_isa_detach),
	{ 0, 0 }
};

static driver_t ${1}_isa_driver = {
	"${1}",
	${1}_methods,
	sizeof (struct ${1}_softc)
};

DRIVER_MODULE(${1}, isa, ${1}_isa_driver, ${1}_devclass, 0, 0);


/*
 * The ISA code calls this for each device it knows about,
 * whether via the PNP code or via the hints etc.
 */
static int
${1}_isa_probe (device_t device)
{
	int error;
	sc_p scp = device_get_softc(device);
	bus_space_handle_t  bh;
	bus_space_tag_t bt;
	struct resource *port_res;
	int rid = 0;
	int	size = 16; /* SIZE of port range used */


	bzero(scp, sizeof(*scp));
	scp->device = device;

	/*
	 * Check for a PNP match..
	 * There are several possible outcomes.
	 * error == 0		We match a PNP device (possibly several?).
	 * error == ENXIO,	It is a PNP device but not ours.
	 * error == ENOENT,	I is not a PNP device.. try heuristic probes.
	 *    -- logic from if_ed_isa.c, added info from isa/isa_if.m:
	 */
	error = ISA_PNP_PROBE(device_get_parent(device), device, ${1}_ids);
	switch (error) {
	case 0:
		/*
		 * We found a PNP device.
		 * Fall through into the code that just looks
		 * for a non PNP device as that should 
		 * act as a good filter for bad stuff.
		 */
	case ENOENT:
		/*
		 * Well it didn't show up in the PNP tables
		 * so look directly at known ports (if we have any)
		 * in case we are looking for an old pre-PNP card.
		 *
		 * The ports etc should come from a 'hints' section
		 * buried somewhere. XXX - still not figured out.
		 * which is read in by code in isa/isahint.c
		 */
  
        	port_res = bus_alloc_resource(device, SYS_RES_IOPORT, &rid,
                                 0ul, ~0ul, size, RF_ACTIVE);
        	if (port_res == NULL) {
			error = ENXIO;
			break;
        	}

                scp->port_rid = rid;
                scp->port_res = port_res;
		scp->bt = bt = rman_get_bustag(port_res);
		scp->bh = bh = rman_get_bushandle(port_res);

		if ( ${UPPER}_INB(SOME_PORT) != EXPECTED_VALUE) {
			/* 
			 * It isn't what we expected,
			 * so release everything and quit looking for it.
			 */
			bus_release_resource(device, SYS_RES_IOPORT,
							rid, port_res);
			return (ENXIO);
		}
		error = 0;
		break;
	case  ENXIO:
		/* not ours, leave imediatly */
	default:
		error = ENXIO;
	}
	return (error);
}

/*
 * Called if the probe succeeded.
 * We can be destructive here as we know we have the device.
 */
static int
${1}_isa_attach (device_t device)
{
	int	unit = device_get_unit(device);
	sc_p scp = device_get_softc(device);

	scp->dev = make_dev(&${1}_cdevsw, 0, 0, 0, 0600, "${1}%d", unit);
	scp->dev->si_drv1 = scp;
	return 0;
}

static int
${1}_isa_detach (device_t device)
{
	sc_p scp = device_get_softc(device);

	bus_release_resource(device, SYS_RES_IOPORT,
					scp->port_rid, scp->port_res);
	destroy_dev(scp->dev);
	return (0);
}

static void
${1}intr(void *arg)
{
	/*device_t dev = (device_t)arg;*/
	/* sc_p scp	= device_get_softc(dev);*/

	/* 
	 * well we got an interupt, now what?
	 */
	return;
}

static int
${1}ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);
	bus_space_handle_t  bh = scp->bh;
	bus_space_tag_t bt = scp->bt;

	switch (cmd) {
	case DHIOCRESET:
		/* whatever resets it */
		${UPPER}_OUTB(SOME_PORT, 0xff) ;
		break;
	default:
		return ENXIO;
	}
	return (0);
}
/*
 * You also need read, write, open, close routines.
 * This should get you started
 */
static int
${1}open(dev_t dev, int oflags, int devtype, struct proc *p)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);

	/* 
	 * Do processing
	 */
	return (0);
}

static int
${1}close(dev_t dev, int fflag, int devtype, struct proc *p)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);

	/* 
	 * Do processing
	 */
	return (0);
}

static int
${1}read(dev_t dev, struct uio *uio, int ioflag)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);
	int	 toread;


	/* 
	 * Do processing
	 * read from buffer
	 */
	toread = (min(uio->uio_resid, sizeof(scp->buffer)));
	return(uiomove(scp->buffer, toread, uio));
}

static int
${1}write(dev_t dev, struct uio *uio, int ioflag)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);
	int	towrite;

	/* 
	 * Do processing
	 * write to buffer
	 */
	towrite = (min(uio->uio_resid, sizeof(scp->buffer)));
	return(uiomove(scp->buffer, towrite, uio));
}

static int
${1}mmap(dev_t dev, vm_offset_t offset, int nprot)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);

	/* 
	 * Do processing
	 */
#if 0	/* if we had a frame buffer or whatever.. do this */
	if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) {
		return (-1);
	}
	return i386_btop((FRAMEBASE + offset));
#else
	return (-1);
#endif
}

static int
${1}poll(dev_t dev, int which, struct proc *p)
{
	sc_p scp	= ${UPPER}DEV2SOFTC(dev);

	/* 
	 * Do processing
	 */
	return (0); /* this is the wrong value I'm sure */
}

DONE

cat >${TOP}/sys/${1}io.h <<DONE
/*
 * Definitions needed to access the ${1} device (ioctls etc)
 * see mtio.h , ioctl.h as examples
 */
#ifndef SYS_DHIO_H
#define SYS_DHIO_H

#ifndef KERNEL
#include <sys/types.h>
#endif
#include <sys/ioccom.h>

/*
 * define an ioctl here
 */
#define DHIOCRESET _IO('D', 0) /* reset the ${1} device */
#endif
DONE

if [ ! -d ${TOP}/modules/${1} ]
then
	mkdir -p ${TOP}/modules/${1}
fi

cat >${TOP}/modules/${1}/Makefile <<DONE
#	${UPPER} Loadable Kernel Module
#
# $FreeBSD$
 
.PATH:  \${.CURDIR}/../../dev/${1}
KMOD    = ${1}
SRCS    = ${1}.c
SRCS    += opt_inet.h device_if.h bus_if.h pci_if.h isa_if.h
  
# you may need to do this is your device is an if_xxx driver
opt_inet.h:
	echo "#define INET 1" > opt_inet.h
	   
.include <bsd.kmod.mk>
DONE

(cd ${TOP}/modules/${1}; make depend; make )
exit

config ${UPPER}
cd ../../compile/${UPPER}
make depend
make ${1}.o
make
exit

#--------------end of script---------------
#
#edit to your taste..
#
# 




OpenPOWER on IntegriCloud