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