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