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