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