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