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