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