xref: /freebsd/share/examples/drivers/make_device_driver.sh (revision 7dd17df71d928ce75de2d797d4e9442c77de4919)
1#!/bin/sh
2# This writes a skeleton driver and puts it into the kernel tree for you.
3# It also adds FOO and files.FOO configuration files so you can compile
4# a kernel with your FOO driver linked in.
5# To do so:
6# cd /sys/i386/conf; config FOO; cd ../../compile/FOO; make depend; make
7#
8# More interestingly, it creates a modules/foo directory
9# which it populates, to allow you to compile a FOO module
10# which can be lonked with your presently running kernel (if you feel brave).
11# To do so:
12# cd /sys/modules/foo; make depend; make; make install; kldload foo
13#
14# arg1 to this script is expected to be lowercase "foo"
15#
16# Trust me, RUN THIS SCRIPT :)
17#
18# $FreeBSD$"
19#
20#
21if [ "${1}X" = "X" ]
22then
23	echo "Hey , how about some help here.. give me a device name!"
24	exit 1
25fi
26UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"`
27
28HERE=`pwd`
29cd /sys
30TOP=`pwd`
31
32RCS_KEYWORD=FreeBSD
33
34if [ -d ${TOP}/modules/${1} ]
35then
36	echo "There appears to already be a module called ${1}"
37	echo -n "Should it be overwritten? [Y]"
38	read VAL
39	if [ "-z" "$VAL" ]
40	then
41	  VAL=YES
42	fi
43	case ${VAL} in
44	[yY]*)
45	  echo "Cleaning up from prior runs"
46	  rm -rf ${TOP}/dev/${1}
47	  rm -rf ${TOP}/modules/${1}
48	  rm ${TOP}/i386/conf/files.${UPPER}
49	  rm ${TOP}/i386/conf/${UPPER}
50	  rm ${TOP}/sys/${1}io.h
51	  ;;
52	*)
53	  exit 1
54	  ;;
55	esac
56fi
57
58echo "The following files will be created:"
59echo ${TOP}/modules/${1}
60echo ${TOP}/i386/conf/files.${UPPER}
61echo ${TOP}/i386/conf/${UPPER}
62echo ${TOP}/dev/${1}
63echo ${TOP}/dev/${1}/${1}.c
64echo ${TOP}/sys/${1}io.h
65echo ${TOP}/modules/${1}
66echo ${TOP}/modules/${1}/Makefile
67
68
69	mkdir ${TOP}/modules/${1}
70
71#######################################################################
72#######################################################################
73#
74# Create configuration information needed to create a kernel
75# containing this driver.
76#
77# Not really needed if we are going to do this as a module.
78#######################################################################
79# First add the file to a local file list.
80#######################################################################
81
82cat >${TOP}/i386/conf/files.${UPPER} <<DONE
83dev/${1}/${1}.c	 optional ${1}
84DONE
85
86#######################################################################
87# Then create a configuration file for a kernel that contains this driver.
88#######################################################################
89cat >${TOP}/i386/conf/${UPPER} <<DONE
90# Configuration file for kernel type: ${UPPER}
91ident	${UPPER}
92# \$${RCS_KEYWORD}: $
93DONE
94
95grep -v GENERIC < /sys/i386/conf/GENERIC >>${TOP}/i386/conf/${UPPER}
96
97cat >>${TOP}/i386/conf/${UPPER} <<DONE
98options		DDB		# trust me, you'll need this
99device		${1}
100DONE
101
102if [ ! -d ${TOP}/dev/${1} ]
103then
104	mkdir -p ${TOP}/dev/${1}
105fi
106
107
108
109
110cat >${TOP}/dev/${1}/${1}.c <<DONE
111/*
112 * Copyright (c) [year] [your name]
113 * All rights reserved.
114 *
115 * Redistribution and use in source and binary forms, with or without
116 * modification, are permitted provided that the following conditions
117 * are met:
118 * 1. Redistributions of source code must retain the above copyright
119 *    notice, this list of conditions and the following disclaimer.
120 * 2. Redistributions in binary form must reproduce the above copyright
121 *    notice, this list of conditions and the following disclaimer in the
122 *    documentation and/or other materials provided with the distribution.
123 *
124 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
125 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
127 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
130 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
132 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
133 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
134 * SUCH DAMAGE.
135 *
136 *
137 * ${1} driver
138 * \$${RCS_KEYWORD}: $
139 */
140
141/*
142 * http://www.daemonnews.org/200008/isa.html is required reading.
143 * hopefully it will make it's way into the handbook.
144 */
145
146#include <sys/param.h>
147#include <sys/systm.h>
148#include <sys/conf.h>		/* cdevsw stuff */
149#include <sys/kernel.h>		/* SYSINIT stuff */
150#include <sys/uio.h>		/* SYSINIT stuff */
151#include <sys/malloc.h>		/* malloc region definitions */
152#include <sys/module.h>
153#include <sys/bus.h>
154#include <machine/bus.h>
155#include <machine/resource.h>
156#include <machine/bus_pio.h>
157#include <machine/bus_memio.h>
158#include <sys/rman.h>
159#include <sys/time.h>
160
161#include <pci/pcireg.h>
162#include <pci/pcivar.h>
163
164#include <isa/isavar.h>
165#include "isa_if.h"
166#include <sys/${1}io.h>		/* ${1} IOCTL definitions */
167
168/* XXX These should be defined in terms of bus-space ops */
169#define ${UPPER}_INB(port) inb(port_start)
170#define ${UPPER}_OUTB(port, val) ( port_start, (val))
171#define SOME_PORT 123
172#define EXPECTED_VALUE 0x42
173
174/* 
175 * device specific Misc defines 
176 */
177#define BUFFERSIZE	1024
178#define NUMPORTS	4
179#define MEMSIZE		(4 * 1024) /* imaginable h/w buffer size */
180
181/*
182 * One of these per allocated device
183 */
184struct ${1}_softc {
185	bus_space_tag_t bt;
186	bus_space_handle_t bh;
187	int rid_ioport;
188	int rid_memory;
189	int rid_irq;
190	int rid_drq;
191	struct resource* res_ioport;	/* resource for port range */
192	struct resource* res_memory;	/* resource for mem range */
193	struct resource* res_irq;	/* resource for irq range */
194	struct resource* res_drq;	/* resource for dma channel */
195	device_t device;
196	dev_t dev;
197	void	*intr_cookie;
198	void	*vaddr;			/* Virtual address of mem resource */
199	char	buffer[BUFFERSIZE];	/* if we needed to buffer something */
200} ;
201
202typedef	struct ${1}_softc *sc_p;
203
204
205/* Function prototypes (these should all be static) */
206static int ${1}_deallocate_resources(device_t device);
207static int ${1}_allocate_resources(device_t device);
208static int ${1}_attach(device_t device, sc_p scp);
209static int ${1}_detach(device_t device, sc_p scp);
210
211static d_open_t		${1}open;
212static d_close_t	${1}close;
213static d_read_t		${1}read;
214static d_write_t	${1}write;
215static d_ioctl_t	${1}ioctl;
216static d_mmap_t		${1}mmap;
217static d_poll_t		${1}poll;
218static	void		${1}intr(void *arg);
219 
220#define CDEV_MAJOR 20
221static struct cdevsw ${1}_cdevsw = {
222	/* open */	${1}open,
223	/* close */	${1}close,
224	/* read */	${1}read,
225	/* write */	${1}write,
226	/* ioctl */	${1}ioctl,
227	/* poll */	${1}poll,
228	/* mmap */	${1}mmap,
229	/* strategy */	nostrategy,	/* not a block type device */
230	/* name */	"${1}",
231	/* maj */	CDEV_MAJOR,
232	/* dump */	nodump,		/* not a block type device */
233	/* psize */	nopsize,	/* not a block type device */
234	/* flags */	0,
235	/* bmaj */	-1
236};
237
238static devclass_t ${1}_devclass;
239 
240/*****************************************\
241* ISA Attachment structures and functions
242\*****************************************/
243static void ${1}_isa_identify (driver_t *, device_t);
244static int ${1}_isa_probe (device_t);
245static int ${1}_isa_attach (device_t);
246static int ${1}_isa_detach (device_t);
247
248static struct isa_pnp_id ${1}_ids[] = {
249	{0x12345678,	"ABCco Widget"},
250	{0xfedcba98,	"shining moon Widget ripoff"},
251	{0,		NULL}
252};
253
254static device_method_t ${1}_methods[] = {
255	DEVMETHOD(device_identify,	${1}_isa_identify),
256	DEVMETHOD(device_probe,		${1}_isa_probe),
257	DEVMETHOD(device_attach,	${1}_isa_attach),
258	DEVMETHOD(device_detach,	${1}_isa_detach),
259	{ 0, 0 }
260};
261
262static driver_t ${1}_isa_driver = {
263	"${1}",
264	${1}_methods,
265	sizeof (struct ${1}_softc)
266};
267
268
269DRIVER_MODULE(${1}, isa, ${1}_isa_driver, ${1}_devclass, 0, 0);
270
271/*
272 * Here list some port addresses we might expect our widget to appear at:
273 */
274static struct localhints {
275	int ioport;
276	int irq;
277	int drq;
278	int mem;
279} res[] = {
280	{ 0x210, 11, 2, 0xcd000},
281	{ 0x310, 12, 3, 0xdd000},
282	{ 0x320, 9, 6, 0xd4000},
283	{0,0,0,0}
284};
285
286#define MAXHINTS 10 /* just an arbitrary safty limit */
287/*
288 * Called once when the driver is somehow connected with the bus,
289 * (Either linked in and the bus is started, or loaded as a module).
290 *
291 * The aim of this routine in an ISA driver is to add child entries to
292 * the parent bus so that it looks as if the devices were detected by
293 * some pnp-like method, or at least mentionned in the hints.
294 *
295 * For NON-PNP "dumb" devices:
296 * Add entries into the bus's list of likely devices, so that
297 * our 'probe routine' will be called for them.
298 * This is similar to what the 'hints' code achieves, except this is
299 * loadable with the driver.
300 * In the 'dumb' case we end up with more children than needed but
301 * some (or all) of them will fail probe() and only waste a little memory.
302 *
303 * For NON-PNP "Smart" devices:
304 * If the device has a NON-PNP way of being detected and setting/sensing
305 * the card, then do that here and add a child for each set of
306 * hardware found. 
307 *
308 * For PNP devices:
309 * If the device is always PNP capable then this function can be removed.
310 *
311 * If the device is mentionned in the 'hints' file then this
312 * function can be removed. All devices mentionned in the hints
313 * file get added as children for probing, whether or not the
314 * driver is linked in. So even as a module it MAY still be there.
315 * See isa/isahint.c for hints being added in.
316 */
317static void
318${1}_isa_identify (driver_t *driver, device_t parent)
319{
320	u_int32_t	irq=0;
321	u_int32_t	ioport;
322	device_t	child;
323	int i;
324
325
326	/*
327	 * If we've already got ${UPPER} attached somehow, don't try again.
328	 * Maybe it was in the hints file. or it was loaded before.
329	 */
330	if (device_find_child(parent, "${1}", 0)) {
331		printf("${UPPER}: already attached\n");
332		return;
333	}
334/* XXX look at dev/acpica/acpi_isa.c for use of ISA_ADD_CONFIG() macro */
335/* XXX What is ISA_SET_CONFIG_CALLBACK(parent, child, pnpbios_set_config, 0) ?*/
336	for (i = 0; i < MAXHINTS; i++) {
337		if (((ioport = res[i].ioport) == 0)
338		&&  ((irq = res[i].irq) == 0)) {
339			return; /* we've added all our local hints */
340		}
341
342		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "${1}", -1);
343		bus_set_resource(child, SYS_RES_IOPORT,	0, ioport, NUMPORTS);
344		bus_set_resource(child, SYS_RES_IRQ,	0, irq, 1);
345		bus_set_resource(child, SYS_RES_DRQ,	0, res[i].drq, 1);
346		bus_set_resource(child, SYS_RES_MEMORY,	0, res[i].mem, MEMSIZE);
347
348
349#if 0
350		/*
351		 * If we wanted to pretend PNP found it
352		 * we could do this, and put matching entries
353		 * in the PNP table, but I think it's probably too hacky.
354		 * As you see, some people have done it though.
355		 * Basically EISA (remember that?) would do this I think
356		 */
357		isa_set_vendorid(child, PNP_EISAID("ESS1888"));
358		isa_set_logicalid(child, PNP_EISAID("ESS1888"));
359#endif
360	}
361#if 0
362	Do some smart probing (e.g. like the lnc driver)
363	and add a child for each one found.
364#endif
365
366	return;
367}
368/*
369 * The ISA code calls this for each device it knows about,
370 * whether via the PNP code or via the hints etc.
371 * If the device nas no PNP capabilities, remove all the 
372 * PNP entries, but keep the call to ISA_PNP_PROBE()
373 * As it will guard against accidentally recognising
374 * foreign hardware. This is because we will be called to check against
375 * ALL PNP hardware.
376 */
377static int
378${1}_isa_probe (device_t device)
379{
380	int error;
381	device_t parent = device_get_parent(device);
382	sc_p scp = device_get_softc(device);
383	u_long	port_start, port_count;
384
385
386	bzero(scp, sizeof(*scp));
387	scp->device = device;
388
389	/*
390	 * Check this device for a PNP match in our table..
391	 * There are several possible outcomes.
392	 * error == 0		We match a PNP ).
393	 * error == ENXIO,	It is a PNP device but not in out table.
394	 * error == ENOENT,	I is not a PNP device.. try heuristic probes.
395	 *    -- logic from if_ed_isa.c, added info from isa/isa_if.m:
396	 */
397	error = ISA_PNP_PROBE(parent, device, ${1}_ids);
398	switch (error) {
399	case 0:
400		/*
401		 * We found a PNP device.
402		 * Do nothing, as it's all done in attach()
403		 */
404		break;
405	case ENOENT:
406		/*
407		 * Well it didn't show up in the PNP tables
408		 * so look directly at known ports (if we have any)
409		 * in case we are looking for an old pre-PNP card.
410		 * 
411		 * Hopefully the  'identify' routine will have picked these
412		 * up for us first.
413		 *
414		 * The ports etc should come from a 'hints' section
415		 * which is read in by code in isa/isahint.c
416		 * and kern/subr_bus.c to create resource entries,
417		 * or have been added by the 'identify routine above.
418		 *
419		 * First make a temporary resource reservation.
420		 * If we can't get the resources we need then
421		 * we need to abort.  Possibly this indicates
422		 * the resources were used by another device.
423		 */
424		if ((error = (${1}_allocate_resources(device)))) {
425			error = ENXIO;
426			goto errexit;
427		}
428
429		/*
430		 * find out the values of any resources we
431		 * need for our dumb probe.
432		 */
433		error = bus_get_resource(device, SYS_RES_IOPORT, 0,
434			&port_start, &port_count);
435
436		/* dummy heuristic type probe */
437		if ( inb(port_start) != EXPECTED_VALUE) {
438			/* 
439			 * It isn't what we hoped, so quit looking for it.
440			 */
441			error = ENXIO;
442		} else {
443			u_long membase = bus_get_resource_start(device,
444					SYS_RES_MEMORY, 0 /*rid*/);
445			u_long memsize;
446			/*
447			 * If we discover in some way that the device has
448			 * XXX bytes of memory window, we can override
449			 * or set the memory size in the child resource list.
450			 */
451			memsize = inb(port_start + 1) * 1024; /* for example */
452			error = bus_set_resource(device, SYS_RES_MEMORY,
453				/*rid*/0, membase, memsize);
454			/*
455			 * We found one..
456			 * Return -2 if we would LIKE the device
457			 * Return -1 if we want it a lot
458			 * Return 0 if we MUST get the device
459			 * This allows drivers to 'bid' for a device.
460			 */
461			device_set_desc(device, "ACME Widget model 1234");
462			error = 0; /* we really want it */
463		}
464		/*
465		 * Unreserve the resources for now because
466		 * another driver may bid for device too.
467		 * If we lose the bid, but still hold the resouces, we will
468		 * effectively have diabled the other driver from getting them
469		 * which will result in neither driver getting the device.
470		 * We will ask for them again in attach if we win.
471		 */
472		${1}_deallocate_resources(device);
473		break;
474	case  ENXIO:
475		/* It was PNP but not ours, leave imediatly */
476	default:
477		error = ENXIO;
478	}
479errexit:
480	return (error);
481}
482
483/*
484 * Called if the probe succeeded and our bid won the device.
485 * We can be destructive here as we know we have the device.
486 * This is the first place we can be sure we have a softc structure.
487 * You would do ISA specific attach things here, but generically there aren't
488 * any (yey new-bus!).
489 */
490static int
491${1}_isa_attach (device_t device)
492{
493	sc_p	scp	= device_get_softc(device);
494        int	error;
495
496        error =  ${1}_attach(device, scp);
497        if (error) {
498                ${1}_isa_detach(device);
499        }
500        return (error);
501
502}
503
504/* 
505 * detach the driver (e.g. module unload)
506 * call the bus independent version
507 * and undo anything we did in the ISA attach routine.
508 */
509static int
510${1}_isa_detach (device_t device)
511{
512	sc_p	scp	= device_get_softc(device);
513        int	error;
514
515        error =  ${1}_detach(device, scp);
516        return (error);
517}
518
519/***************************************\
520* PCI Attachment structures and code	*
521\***************************************/
522
523static int	${1}_pci_probe	__P((device_t));
524static int	${1}_pci_attach	__P((device_t));
525static int	${1}_pci_detach	__P((device_t));
526
527static device_method_t ${1}_pci_methods[] = {
528	/* Device interface */
529	DEVMETHOD(device_probe,		${1}_pci_probe),
530	DEVMETHOD(device_attach,	${1}_pci_attach),
531	DEVMETHOD(device_detach,	${1}_pci_detach),
532	{ 0, 0 }
533};
534
535static driver_t ${1}_pci_driver = {
536	"${1}",
537	${1}_pci_methods,
538	sizeof(struct ${1}_softc),
539};
540
541
542DRIVER_MODULE(${1}, pci, ${1}_pci_driver, ${1}_devclass, 0, 0);
543
544static struct _pcsid
545{
546	u_int32_t	type;
547	const char	*desc;
548} pci_ids[] = {
549	{ 0x1234abcd,	"ACME PCI Widgetplus"	},
550	{ 0x1234fedc,	"Happy moon brand RIPOFFplus"	},
551	{ 0x00000000,	NULL					}
552};
553
554static int
555${1}_pci_probe (device_t device)
556{
557	u_int32_t	type = pci_get_devid(device);
558	struct _pcsid	*ep =pci_ids;
559
560	while (ep->type && ep->type != type)
561		++ep;
562	if (ep->desc) {
563		device_set_desc(device, ep->desc);
564		return 0;
565	} else {
566		return ENXIO;
567	}
568}
569
570static int
571${1}_pci_attach(device_t device)
572{
573	sc_p	scp	= device_get_softc(device);
574        int	error;
575
576        error =  ${1}_attach(device, scp);
577        if (error) {
578                ${1}_pci_detach(device);
579        }
580        return (error);
581}
582
583static int
584${1}_pci_detach (device_t device)
585{
586	sc_p	scp	= device_get_softc(device);
587        int	error;
588
589        error =  ${1}_detach(device, scp);
590        return (error);
591}
592
593
594/****************************************\
595*  Common Attachment subfunctions	*
596\****************************************/
597static int
598${1}_attach(device_t device, sc_p scp)
599{
600	int	unit	= device_get_unit(device);
601	device_t parent	= device_get_parent(device);
602
603	scp->dev = make_dev(&${1}_cdevsw, 0,
604			UID_ROOT, GID_OPERATOR, 0600, "${1}%d", unit);
605	scp->dev->si_drv1 = scp;
606
607	if (${1}_allocate_resources(device)) {
608		goto errexit;
609	}
610
611	scp->bt = rman_get_bustag(scp->res_ioport);
612	scp->bh = rman_get_bushandle(scp->res_ioport);
613
614	/* register the interrupt handler */
615	/*
616	 * The type should be one of:
617	 *	INTR_TYPE_TTY
618	 *	(INTR_TYPE_TTY | INTR_TYPE_FAST) 
619	 *	INTR_TYPE_BIO 
620	 *	INTR_TYPE_CAM 
621	 *	INTR_TYPE_NET 
622	 *	INTR_TYPE_MISC 
623	 * This will probably change with SMPng.
624	 */
625	if (scp->res_irq) {
626		/* default to the tty mask for registration */  /* XXX */
627		if (BUS_SETUP_INTR(parent, device, scp->res_irq, INTR_TYPE_TTY,
628				${1}intr, scp, &scp->intr_cookie) == 0) {
629			/* do something if successfull */
630		} else {
631			goto errexit;
632		}
633	}
634
635	/*
636	 * If we want to access the memory we will need
637	 * to know where it was mapped.
638	 */
639	scp->vaddr = rman_get_virtual(scp->res_memory);
640	return 0;
641
642errexit:
643	/*
644	 * Undo anything we may have done
645	 */
646	${1}_detach(device, scp);
647	return (ENXIO);
648}
649
650static int
651${1}_detach(device_t device, sc_p scp)
652{
653	device_t parent = device_get_parent(device);
654
655	/*
656	 * At this point stick a strong piece of wood into the device
657	 * to make sure it is stopped safely. The alternative is to 
658	 * simply REFUSE to detach if it's busy. What you do depends on 
659	 * your specific situation.
660	 */
661	/* ZAP some register */
662
663	/*
664	 * Take our interrupt handler out of the list of handlers
665	 * that can handle this irq.
666	 */
667	if (scp->intr_cookie != NULL) {
668		if (BUS_TEARDOWN_INTR(parent, device,
669			scp->res_irq, scp->intr_cookie) != 0) {
670				printf("intr teardown failed.. continuing\n");
671		}
672		scp->intr_cookie = NULL;
673	}
674
675	/*
676	 * deallocate any system resources we may have
677	 * allocated on behalf of this driver.
678	 */
679	scp->vaddr = NULL;
680	return ${1}_deallocate_resources(device);
681}
682
683static int
684${1}_allocate_resources(device_t device)
685{
686	int error;
687	sc_p scp = device_get_softc(device);
688	int	size = 16; /* SIZE of port range used */
689
690	scp->res_ioport = bus_alloc_resource(device, SYS_RES_IOPORT,
691			&scp->rid_ioport, 0ul, ~0ul, size, RF_ACTIVE);
692	if (scp->res_ioport == NULL) {
693		goto errexit;
694	}
695
696	scp->res_irq = bus_alloc_resource(device, SYS_RES_IRQ,
697			&scp->rid_irq, 0ul, ~0ul, 1, RF_SHAREABLE|RF_ACTIVE);
698	if (scp->res_irq == NULL) {
699		goto errexit;
700	}
701
702	scp->res_drq = bus_alloc_resource(device, SYS_RES_DRQ,
703			&scp->rid_drq, 0ul, ~0ul, 1, RF_ACTIVE);
704	if (scp->res_drq == NULL) {
705		goto errexit;
706	}
707
708	scp->res_memory = bus_alloc_resource(device, SYS_RES_MEMORY,
709			&scp->rid_memory, 0ul, ~0ul, MSIZE, RF_ACTIVE);
710	if (scp->res_memory == NULL) {
711		goto errexit;
712	}
713	return (0);
714
715errexit:
716	error = ENXIO;
717	/* cleanup anything we may have assigned. */
718	${1}_deallocate_resources(device);
719	return (ENXIO); /* for want of a better idea */
720}
721
722static int
723${1}_deallocate_resources(device_t device)
724{
725	sc_p scp = device_get_softc(device);
726
727	if (scp->res_irq != 0) {
728		bus_deactivate_resource(device, SYS_RES_IRQ,
729			scp->rid_irq, scp->res_irq);
730		bus_release_resource(device, SYS_RES_IRQ,
731			scp->rid_irq, scp->res_irq);
732		scp->res_irq = 0;
733	}
734	if (scp->res_ioport != 0) {
735		bus_deactivate_resource(device, SYS_RES_IOPORT,
736			scp->rid_ioport, scp->res_ioport);
737		bus_release_resource(device, SYS_RES_IOPORT,
738			scp->rid_ioport, scp->res_ioport);
739		scp->res_ioport = 0;
740	}
741	if (scp->res_ioport != 0) {
742		bus_deactivate_resource(device, SYS_RES_MEMORY,
743			scp->rid_memory, scp->res_memory);
744		bus_release_resource(device, SYS_RES_MEMORY,
745			scp->rid_memory, scp->res_memory);
746		scp->res_ioport = 0;
747	}
748	if (scp->res_drq != 0) {
749		bus_deactivate_resource(device, SYS_RES_DRQ,
750			scp->rid_drq, scp->res_drq);
751		bus_release_resource(device, SYS_RES_DRQ,
752			scp->rid_drq, scp->res_drq);
753		scp->res_drq = 0;
754	}
755	if (scp->dev) {
756		destroy_dev(scp->dev);
757	}
758	return (0);
759}
760
761static void
762${1}intr(void *arg)
763{
764	sc_p scp	= arg;
765
766	/* 
767	 * well we got an interupt, now what?
768	 */
769	return;
770}
771
772static int
773${1}ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
774{
775	sc_p scp	= dev->si_drv1;
776
777	switch (cmd) {
778	case DHIOCRESET:
779		/* whatever resets it */
780#if 0
781		${UPPER}_OUTB(SOME_PORT, 0xff) ;
782#endif
783		break;
784	default:
785		return ENXIO;
786	}
787	return (0);
788}
789/*
790 * You also need read, write, open, close routines.
791 * This should get you started
792 */
793static int
794${1}open(dev_t dev, int oflags, int devtype, struct proc *p)
795{
796	sc_p scp	= dev->si_drv1;
797
798	/* 
799	 * Do processing
800	 */
801	return (0);
802}
803
804static int
805${1}close(dev_t dev, int fflag, int devtype, struct proc *p)
806{
807	sc_p scp	= dev->si_drv1;
808
809	/* 
810	 * Do processing
811	 */
812	return (0);
813}
814
815static int
816${1}read(dev_t dev, struct uio *uio, int ioflag)
817{
818	sc_p scp	= dev->si_drv1;
819	int	 toread;
820
821
822	/* 
823	 * Do processing
824	 * read from buffer
825	 */
826	toread = (min(uio->uio_resid, sizeof(scp->buffer)));
827	return(uiomove(scp->buffer, toread, uio));
828}
829
830static int
831${1}write(dev_t dev, struct uio *uio, int ioflag)
832{
833	sc_p scp	= dev->si_drv1;
834	int	towrite;
835
836	/* 
837	 * Do processing
838	 * write to buffer
839	 */
840	towrite = (min(uio->uio_resid, sizeof(scp->buffer)));
841	return(uiomove(scp->buffer, towrite, uio));
842}
843
844static int
845${1}mmap(dev_t dev, vm_offset_t offset, int nprot)
846{
847	sc_p scp	= dev->si_drv1;
848
849	/* 
850	 * Given a byte offset into your device, return the PHYSICAL
851	 * page number that it would map to.
852	 */
853#if 0	/* if we had a frame buffer or whatever.. do this */
854	if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) {
855		return (-1);
856	}
857	return i386_btop((FRAMEBASE + offset));
858#else
859	return (-1);
860#endif
861}
862
863static int
864${1}poll(dev_t dev, int which, struct proc *p)
865{
866	sc_p scp	= dev->si_drv1;
867
868	/* 
869	 * Do processing
870	 */
871	return (0); /* this is the wrong value I'm sure */
872}
873
874DONE
875
876cat >${TOP}/sys/${1}io.h <<DONE
877/*
878 * Definitions needed to access the ${1} device (ioctls etc)
879 * see mtio.h , ioctl.h as examples
880 */
881#ifndef SYS_DHIO_H
882#define SYS_DHIO_H
883
884#ifndef KERNEL
885#include <sys/types.h>
886#endif
887#include <sys/ioccom.h>
888
889/*
890 * define an ioctl here
891 */
892#define DHIOCRESET _IO('D', 0) /* reset the ${1} device */
893#endif
894DONE
895
896if [ ! -d ${TOP}/modules/${1} ]
897then
898	mkdir -p ${TOP}/modules/${1}
899fi
900
901cat >${TOP}/modules/${1}/Makefile <<DONE
902#	${UPPER} Loadable Kernel Module
903#
904# \$${RCS_KEYWORD}: $
905 
906.PATH:  \${.CURDIR}/../../dev/${1}
907KMOD    = ${1}
908SRCS    = ${1}.c
909SRCS    += opt_inet.h device_if.h bus_if.h pci_if.h isa_if.h
910  
911# you may need to do this is your device is an if_xxx driver
912opt_inet.h:
913	echo "#define INET 1" > opt_inet.h
914	   
915.include <bsd.kmod.mk>
916DONE
917
918(cd ${TOP}/modules/${1}; make depend; make )
919exit
920
921config ${UPPER}
922cd ../../compile/${UPPER}
923make depend
924make ${1}.o
925make
926exit
927
928#--------------end of script---------------
929#
930#edit to your taste..
931#
932#
933
934
935
936
937