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}", 346 DEVICE_UNIT_ANY); 347 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, NUMPORTS); 348 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 349 bus_set_resource(child, SYS_RES_DRQ, 0, res[i].drq, 1); 350 bus_set_resource(child, SYS_RES_MEMORY, 0, res[i].mem, MEMSIZE); 351 352#if 0 353 /* 354 * If we wanted to pretend PNP found it 355 * we could do this, and put matching entries 356 * in the PNP table, but I think it's probably too hacky. 357 * As you see, some people have done it though. 358 * Basically EISA (remember that?) would do this I think. 359 */ 360 isa_set_vendorid(child, PNP_EISAID("ESS1888")); 361 isa_set_logicalid(child, PNP_EISAID("ESS1888")); 362#endif 363 } 364#if 0 365 /* 366 * Do some smart probing (e.g. like the lnc driver) 367 * and add a child for each one found. 368 */ 369#endif 370 371 return; 372} 373/* 374 * The ISA code calls this for each device it knows about, 375 * whether via the PNP code or via the hints etc. 376 * If the device nas no PNP capabilities, remove all the 377 * PNP entries, but keep the call to ISA_PNP_PROBE() 378 * As it will guard against accidentally recognising 379 * foreign hardware. This is because we will be called to check against 380 * ALL PNP hardware. 381 */ 382static int 383${1}_isa_probe (device_t device) 384{ 385 int error; 386 device_t parent = device_get_parent(device); 387 struct ${1}_softc *scp = DEVICE2SOFTC(device); 388 u_long port_start, port_count; 389 390 bzero(scp, sizeof(*scp)); 391 scp->device = device; 392 393 /* 394 * Check this device for a PNP match in our table. 395 * There are several possible outcomes. 396 * error == 0 We match a PNP. 397 * error == ENXIO, It is a PNP device but not in our table. 398 * error == ENOENT, It is not a PNP device.. try heuristic probes. 399 * -- logic from if_ed_isa.c, added info from isa/isa_if.m: 400 * 401 * If we had a list of devices that we could handle really well, 402 * and a list which we could handle only basic functions, then 403 * we would call this twice, once for each list, 404 * and return a value of '-2' or something if we could 405 * only handle basic functions. This would allow a specific 406 * Widgetplus driver to make a better offer if it knows how to 407 * do all the extended functions. (See non-pnp part for more info). 408 */ 409 error = ISA_PNP_PROBE(parent, device, ${1}_ids); 410 switch (error) { 411 case 0: 412 /* 413 * We found a PNP device. 414 * Do nothing, as it's all done in attach(). 415 */ 416 break; 417 case ENOENT: 418 /* 419 * Well it didn't show up in the PNP tables 420 * so look directly at known ports (if we have any) 421 * in case we are looking for an old pre-PNP card. 422 * 423 * Hopefully the 'identify' routine will have picked these 424 * up for us first if they use some proprietary detection 425 * method. 426 * 427 * The ports, irqs etc should come from a 'hints' section 428 * which is read in by code in isa/isahint.c 429 * and kern/subr_bus.c to create resource entries, 430 * or have been added by the 'identify routine above. 431 * Note that HINTS based resource requests have NO 432 * SIZE for the memory or ports requests (just a base) 433 * so we may need to 'correct' this before we 434 * do any probing. 435 */ 436 /* 437 * Find out the values of any resources we 438 * need for our dumb probe. Also check we have enough ports 439 * in the request. (could be hints based). 440 * Should probably do the same for memory regions too. 441 */ 442 error = bus_get_resource(device, SYS_RES_IOPORT, 0, 443 &port_start, &port_count); 444 if (port_count != NUMPORTS) { 445 bus_set_resource(device, SYS_RES_IOPORT, 0, 446 port_start, NUMPORTS); 447 } 448 449 /* 450 * Make a temporary resource reservation. 451 * If we can't get the resources we need then 452 * we need to abort. Possibly this indicates 453 * the resources were used by another device 454 * in which case the probe would have failed anyhow. 455 */ 456 if ((error = (${1}_allocate_resources(device)))) { 457 error = ENXIO; 458 goto errexit; 459 } 460 461 /* Dummy heuristic type probe. */ 462 if (inb(port_start) != EXPECTED_VALUE) { 463 /* 464 * It isn't what we hoped, so quit looking for it. 465 */ 466 error = ENXIO; 467 } else { 468 u_long membase = bus_get_resource_start(device, 469 SYS_RES_MEMORY, 0 /*rid*/); 470 u_long memsize; 471 /* 472 * If we discover in some way that the device has 473 * XXX bytes of memory window, we can override 474 * or set the memory size in the child resource list. 475 */ 476 memsize = inb(port_start + 1) * 1024; /* for example */ 477 error = bus_set_resource(device, SYS_RES_MEMORY, 478 /*rid*/0, membase, memsize); 479 /* 480 * We found one, return non-positive numbers.. 481 * Return -N if we can't handle it, but not well. 482 * Return -2 if we would LIKE the device. 483 * Return -1 if we want it a lot. 484 * Return 0 if we MUST get the device. 485 * This allows drivers to 'bid' for a device. 486 */ 487 device_set_desc(device, "ACME Widget model 1234"); 488 error = -1; /* We want it but someone else 489 may be even better. */ 490 } 491 /* 492 * Unreserve the resources for now because 493 * another driver may bid for device too. 494 * If we lose the bid, but still hold the resources, we will 495 * effectively have disabled the other driver from getting them 496 * which will result in neither driver getting the device. 497 * We will ask for them again in attach if we win. 498 */ 499 ${1}_deallocate_resources(device); 500 break; 501 case ENXIO: 502 /* It was PNP but not ours, leave immediately. */ 503 default: 504 error = ENXIO; 505 } 506errexit: 507 return (error); 508} 509 510/* 511 * Called if the probe succeeded and our bid won the device. 512 * We can be destructive here as we know we have the device. 513 * This is the first place we can be sure we have a softc structure. 514 * You would do ISA specific attach things here, but generically there aren't 515 * any (yay new-bus!). 516 */ 517static int 518${1}_isa_attach (device_t device) 519{ 520 int error; 521 struct ${1}_softc *scp = DEVICE2SOFTC(device); 522 523 error = ${1}_attach(device, scp); 524 if (error) 525 ${1}_isa_detach(device); 526 return (error); 527} 528 529/* 530 * Detach the driver (e.g. module unload), 531 * call the bus independent version 532 * and undo anything we did in the ISA attach routine. 533 */ 534static int 535${1}_isa_detach (device_t device) 536{ 537 int error; 538 struct ${1}_softc *scp = DEVICE2SOFTC(device); 539 540 error = ${1}_detach(device, scp); 541 return (error); 542} 543 544/* 545 *************************************** 546 * PCI Attachment structures and code 547 *************************************** 548 */ 549 550static int ${1}_pci_probe(device_t); 551static int ${1}_pci_attach(device_t); 552static int ${1}_pci_detach(device_t); 553 554static device_method_t ${1}_pci_methods[] = { 555 /* Device interface */ 556 DEVMETHOD(device_probe, ${1}_pci_probe), 557 DEVMETHOD(device_attach, ${1}_pci_attach), 558 DEVMETHOD(device_detach, ${1}_pci_detach), 559 { 0, 0 } 560}; 561 562static driver_t ${1}_pci_driver = { 563 "${1}", 564 ${1}_pci_methods, 565 sizeof(struct ${1}_softc), 566}; 567 568DRIVER_MODULE(${1}, pci, ${1}_pci_driver, ${1}_devclass, 0, 0); 569/* 570 * Cardbus is a pci bus plus extra, so use the pci driver unless special 571 * things need to be done only in the cardbus case. 572 */ 573DRIVER_MODULE(${1}, cardbus, ${1}_pci_driver, ${1}_devclass, 0, 0); 574 575static struct _pcsid 576{ 577 u_int32_t type; 578 const char *desc; 579} pci_ids[] = { 580 { 0x1234abcd, "ACME PCI Widgetplus" }, 581 { 0x1243fedc, "Happy moon brand RIPOFFplus" }, 582 { 0x00000000, NULL } 583}; 584 585/* 586 * See if this card is specifically mentioned in our list of known devices. 587 * Theoretically we might also put in a weak bid for some devices that 588 * report themselves to be some generic type of device if we can handle 589 * that generic type. (other PCI_XXX calls give that info). 590 * This would allow a specific driver to over-ride us. 591 * 592 * See the comments in the ISA section regarding returning non-positive 593 * values from probe routines. 594 */ 595static int 596${1}_pci_probe (device_t device) 597{ 598 u_int32_t type = pci_get_devid(device); 599 struct _pcsid *ep =pci_ids; 600 601 while (ep->type && ep->type != type) 602 ++ep; 603 if (ep->desc) { 604 device_set_desc(device, ep->desc); 605 return 0; /* If there might be a better driver, return -2 */ 606 } else 607 return ENXIO; 608} 609 610static int 611${1}_pci_attach(device_t device) 612{ 613 int error; 614 struct ${1}_softc *scp = DEVICE2SOFTC(device); 615 616 error = ${1}_attach(device, scp); 617 if (error) 618 ${1}_pci_detach(device); 619 return (error); 620} 621 622static int 623${1}_pci_detach (device_t device) 624{ 625 int error; 626 struct ${1}_softc *scp = DEVICE2SOFTC(device); 627 628 error = ${1}_detach(device, scp); 629 return (error); 630} 631 632/* 633 **************************************** 634 * Common Attachment sub-functions 635 **************************************** 636 */ 637static int 638${1}_attach(device_t device, struct ${1}_softc * scp) 639{ 640 device_t parent = device_get_parent(device); 641 int unit = device_get_unit(device); 642 643 scp->dev = make_dev(&${1}_cdevsw, 0, 644 UID_ROOT, GID_OPERATOR, 0600, "${1}%d", unit); 645 scp->dev->si_drv1 = scp; 646 647 if (${1}_allocate_resources(device)) 648 goto errexit; 649 650 scp->bt = rman_get_bustag(scp->res_ioport); 651 scp->bh = rman_get_bushandle(scp->res_ioport); 652 653 /* Register the interrupt handler. */ 654 /* 655 * The type should be one of: 656 * INTR_TYPE_TTY 657 * INTR_TYPE_BIO 658 * INTR_TYPE_CAM 659 * INTR_TYPE_NET 660 * INTR_TYPE_MISC 661 * This will probably change with SMPng. INTR_TYPE_FAST may be 662 * OR'd into this type to mark the interrupt fast. However, fast 663 * interrupts cannot be shared at all so special precautions are 664 * necessary when coding fast interrupt routines. 665 */ 666 if (scp->res_irq) { 667 /* Default to the tty mask for registration. */ /* XXX */ 668 if (BUS_SETUP_INTR(parent, device, scp->res_irq, INTR_TYPE_TTY, 669 ${1}intr, scp, &scp->intr_cookie) == 0) { 670 /* Do something if successful. */ 671 } else 672 goto errexit; 673 } 674 675 /* 676 * If we want to access the memory we will need 677 * to know where it was mapped. 678 * 679 * Use of this function is discouraged, however. You should 680 * be accessing the device with the bus_space API if at all 681 * possible. 682 */ 683 scp->vaddr = rman_get_virtual(scp->res_memory); 684 return 0; 685 686errexit: 687 /* 688 * Undo anything we may have done. 689 */ 690 ${1}_detach(device, scp); 691 return (ENXIO); 692} 693 694static int 695${1}_detach(device_t device, struct ${1}_softc *scp) 696{ 697 device_t parent = device_get_parent(device); 698 699 /* 700 * At this point stick a strong piece of wood into the device 701 * to make sure it is stopped safely. The alternative is to 702 * simply REFUSE to detach if it's busy. What you do depends on 703 * your specific situation. 704 * 705 * Sometimes the parent bus will detach you anyway, even if you 706 * are busy. You must cope with that possibility. Your hardware 707 * might even already be gone in the case of cardbus or pccard 708 * devices. 709 */ 710 /* ZAP some register */ 711 712 /* 713 * Take our interrupt handler out of the list of handlers 714 * that can handle this irq. 715 */ 716 if (scp->intr_cookie != NULL) { 717 if (BUS_TEARDOWN_INTR(parent, device, 718 scp->res_irq, scp->intr_cookie) != 0) 719 printf("intr teardown failed.. continuing\n"); 720 scp->intr_cookie = NULL; 721 } 722 723 /* 724 * Deallocate any system resources we may have 725 * allocated on behalf of this driver. 726 */ 727 scp->vaddr = NULL; 728 return ${1}_deallocate_resources(device); 729} 730 731static int 732${1}_allocate_resources(device_t device) 733{ 734 int error; 735 struct ${1}_softc *scp = DEVICE2SOFTC(device); 736 int size = 16; /* SIZE of port range used. */ 737 738 scp->res_ioport = bus_alloc_resource(device, SYS_RES_IOPORT, 739 &scp->rid_ioport, 0ul, ~0ul, size, RF_ACTIVE); 740 if (scp->res_ioport == NULL) 741 goto errexit; 742 743 scp->res_irq = bus_alloc_resource(device, SYS_RES_IRQ, 744 &scp->rid_irq, 0ul, ~0ul, 1, RF_SHAREABLE|RF_ACTIVE); 745 if (scp->res_irq == NULL) 746 goto errexit; 747 748 scp->res_drq = bus_alloc_resource(device, SYS_RES_DRQ, 749 &scp->rid_drq, 0ul, ~0ul, 1, RF_ACTIVE); 750 if (scp->res_drq == NULL) 751 goto errexit; 752 753 scp->res_memory = bus_alloc_resource(device, SYS_RES_MEMORY, 754 &scp->rid_memory, 0ul, ~0ul, MSIZE, RF_ACTIVE); 755 if (scp->res_memory == NULL) 756 goto errexit; 757 return (0); 758 759errexit: 760 error = ENXIO; 761 /* Cleanup anything we may have assigned. */ 762 ${1}_deallocate_resources(device); 763 return (ENXIO); /* For want of a better idea. */ 764} 765 766static int 767${1}_deallocate_resources(device_t device) 768{ 769 struct ${1}_softc *scp = DEVICE2SOFTC(device); 770 771 if (scp->res_irq != 0) { 772 bus_deactivate_resource(device, SYS_RES_IRQ, 773 scp->rid_irq, scp->res_irq); 774 bus_release_resource(device, SYS_RES_IRQ, 775 scp->rid_irq, scp->res_irq); 776 scp->res_irq = 0; 777 } 778 if (scp->res_ioport != 0) { 779 bus_deactivate_resource(device, SYS_RES_IOPORT, 780 scp->rid_ioport, scp->res_ioport); 781 bus_release_resource(device, SYS_RES_IOPORT, 782 scp->rid_ioport, scp->res_ioport); 783 scp->res_ioport = 0; 784 } 785 if (scp->res_memory != 0) { 786 bus_deactivate_resource(device, SYS_RES_MEMORY, 787 scp->rid_memory, scp->res_memory); 788 bus_release_resource(device, SYS_RES_MEMORY, 789 scp->rid_memory, scp->res_memory); 790 scp->res_memory = 0; 791 } 792 if (scp->res_drq != 0) { 793 bus_deactivate_resource(device, SYS_RES_DRQ, 794 scp->rid_drq, scp->res_drq); 795 bus_release_resource(device, SYS_RES_DRQ, 796 scp->rid_drq, scp->res_drq); 797 scp->res_drq = 0; 798 } 799 if (scp->dev) 800 destroy_dev(scp->dev); 801 return (0); 802} 803 804static void 805${1}intr(void *arg) 806{ 807 struct ${1}_softc *scp = (struct ${1}_softc *) arg; 808 809 /* 810 * Well we got an interrupt, now what? 811 * 812 * Make sure that the interrupt routine will always terminate, 813 * even in the face of "bogus" data from the card. 814 */ 815 (void)scp; /* Delete this line after using scp. */ 816 return; 817} 818 819static int 820${1}ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 821{ 822 struct ${1}_softc *scp = DEV2SOFTC(dev); 823 824 (void)scp; /* Delete this line after using scp. */ 825 switch (cmd) { 826 case DHIOCRESET: 827 /* Whatever resets it. */ 828#if 0 829 ${UPPER}_OUTB(SOME_PORT, 0xff); 830#endif 831 break; 832 default: 833 return ENXIO; 834 } 835 return (0); 836} 837/* 838 * You also need read, write, open, close routines. 839 * This should get you started. 840 */ 841static int 842${1}open(struct cdev *dev, int oflags, int devtype, struct thread *td) 843{ 844 struct ${1}_softc *scp = DEV2SOFTC(dev); 845 846 /* 847 * Do processing. 848 */ 849 (void)scp; /* Delete this line after using scp. */ 850 return (0); 851} 852 853static int 854${1}close(struct cdev *dev, int fflag, int devtype, struct thread *td) 855{ 856 struct ${1}_softc *scp = DEV2SOFTC(dev); 857 858 /* 859 * Do processing. 860 */ 861 (void)scp; /* Delete this line after using scp. */ 862 return (0); 863} 864 865static int 866${1}read(struct cdev *dev, struct uio *uio, int ioflag) 867{ 868 struct ${1}_softc *scp = DEV2SOFTC(dev); 869 int toread; 870 871 /* 872 * Do processing. 873 * Read from buffer. 874 */ 875 (void)scp; /* Delete this line after using scp. */ 876 toread = (min(uio->uio_resid, sizeof(scp->buffer))); 877 return(uiomove(scp->buffer, toread, uio)); 878} 879 880static int 881${1}write(struct cdev *dev, struct uio *uio, int ioflag) 882{ 883 struct ${1}_softc *scp = DEV2SOFTC(dev); 884 int towrite; 885 886 /* 887 * Do processing. 888 * Write to buffer. 889 */ 890 (void)scp; /* Delete this line after using scp. */ 891 towrite = (min(uio->uio_resid, sizeof(scp->buffer))); 892 return(uiomove(scp->buffer, towrite, uio)); 893} 894 895static int 896${1}mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 897{ 898 struct ${1}_softc *scp = DEV2SOFTC(dev); 899 900 /* 901 * Given a byte offset into your device, return the PHYSICAL 902 * page number that it would map to. 903 */ 904 (void)scp; /* Delete this line after using scp. */ 905#if 0 /* If we had a frame buffer or whatever... do this. */ 906 if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) 907 return (-1); 908 return i386_btop((FRAMEBASE + offset)); 909#else 910 return (-1); 911#endif 912} 913 914static int 915${1}poll(struct cdev *dev, int which, struct thread *td) 916{ 917 struct ${1}_softc *scp = DEV2SOFTC(dev); 918 919 /* 920 * Do processing. 921 */ 922 (void)scp; /* Delete this line after using scp. */ 923 return (0); /* This is the wrong value I'm sure. */ 924} 925 926DONE 927 928cat >${TOP}/sys/${1}io.h <<DONE 929/* 930 * Definitions needed to access the ${1} device (ioctls etc) 931 * see mtio.h, ioctl.h as examples. 932 */ 933#ifndef SYS_DHIO_H 934#define SYS_DHIO_H 935 936#ifndef KERNEL 937#include <sys/types.h> 938#endif 939#include <sys/ioccom.h> 940 941/* 942 * Define an ioctl here. 943 */ 944#define DHIOCRESET _IO('D', 0) /* Reset the ${1} device. */ 945#endif 946DONE 947 948if [ ! -d ${TOP}/modules/${1} ]; then 949 mkdir -p ${TOP}/modules/${1} 950fi 951 952cat >${TOP}/modules/${1}/Makefile <<DONE 953# ${UPPER} Loadable Kernel Module 954 955.PATH: \${.CURDIR}/../../dev/${1} 956KMOD = ${1} 957SRCS = ${1}.c 958SRCS += opt_inet.h device_if.h bus_if.h pci_if.h isa_if.h 959 960# You may need to do this is your device is an if_xxx driver. 961opt_inet.h: 962 echo "#define INET 1" > opt_inet.h 963 964.include <bsd.kmod.mk> 965DONE 966 967echo -n "Do you want to build the '${1}' module? [Y]" 968read VAL 969if [ "-z" "$VAL" ]; then 970 VAL=YES 971fi 972case ${VAL} in 973[yY]*) 974 (cd ${TOP}/modules/${1}; make depend; make ) 975 ;; 976*) 977# exit 978 ;; 979esac 980 981echo "" 982echo -n "Do you want to build the '${UPPER}' kernel? [Y]" 983read VAL 984if [ "-z" "$VAL" ]; then 985 VAL=YES 986fi 987case ${VAL} in 988[yY]*) 989 ( 990 cd ${TOP}/i386/conf; \ 991 config ${UPPER}; \ 992 cd ${TOP}/i386/compile/${UPPER}; \ 993 make depend; \ 994 make; \ 995 ) 996 ;; 997*) 998# exit 999 ;; 1000esac 1001 1002#--------------end of script--------------- 1003# 1004# Edit to your taste... 1005# 1006# 1007