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