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