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