File:  [NetBSD Developer Wiki] / wikisrc / tutorials / bus_space_tutorial.mdwn
Revision 1.3: download - view: text, annotated - select for diffs
Sun Jun 23 14:16:27 2013 UTC (8 years, 5 months ago) by mspo
Branches: MAIN
CVS tags: HEAD
toc cleanup

    1: [[!toc ]]
    2: 
    3: ## Introduction
    4: 
    5: ### Why was this tutorial created?
    6: 
    7: -   Introductory-level documentation is scarce
    8: 
    9: -   Writing device drivers is often considered black magic
   10: 
   11: -   Reading the man pages won’t give you the big picture
   12: 
   13: -   BSD systems are always in need of new drivers
   14: 
   15: -   Device drivers are fun
   16: 
   17: ### What won’t be covered here?
   18: 
   19: We don’t have much time, so several ~~advanced~~ topics were omitted:
   20: 
   21: -   Interrupt handling
   22: 
   23: -   Direct Memory Access and the bus\_dma framework
   24: 
   25: -   Power management
   26: 
   27: -   Driver detachment
   28: 
   29: -   Drivers as kernel modules
   30: 
   31: -   Examples for buses other than PCI
   32: 
   33: -   Pretty much everything else...
   34: 
   35: However, once you finish this tutorial, you should be able to pursue
   36: this knowledge yourself.
   37: 
   38: ### What is a driver anyway?
   39: 
   40: -   The interface between user space and hardware, implemented as a part
   41:     of the kernel
   42: 
   43: -   The NetBSD drivers are written mostly in C
   44: 
   45: -   Sometimes they have machine dependent assembler parts, but this is a
   46:     rare case
   47: 
   48: ### What do you need to write a driver?
   49: 
   50: -   C programming skills
   51: 
   52: -   Hardware documentation (or the ability to reverse engineer the
   53:     hardware)
   54: 
   55: -   A reference driver implementation will help but is not essential
   56: 
   57: -   A NetBSD installation and kernel source, or a cross-build
   58:     environment (the latter is usually preferred for development of
   59:     drivers)
   60: 
   61: -   A lot of time, coffee and patience
   62: 
   63: ### Why is writing the device drivers considered difficult?
   64: 
   65: -   It’s not as difficult as you may expect, in fact during this
   66:     tutorial we’ll prove that it’s quite easy
   67: 
   68: -   You need to think on a very low level
   69: 
   70:     -   Good understanding of computer architecture is a must
   71: 
   72: -   Often documentation is the main problem – writing the driver is not
   73:     possible if you don’t understand how the device works
   74: 
   75:     -   No access to documentation (uncooperative hardware vendors,
   76:         vendors out of business)
   77: 
   78:     -   Documentation is incomplete or plain wrong
   79: 
   80:     -   Reverse engineering can solve these problems but it’s a very
   81:         time consuming process
   82: 
   83: The NetBSD driver model
   84: =======================
   85: 
   86: ### The NetBSD kernel basics
   87: 
   88: -   NetBSD has a classic monolithic UNIX-like kernel - all drivers are
   89:     running in the same address space
   90: 
   91: -   Thanks to the above, communication between drivers and other kernel
   92:     layers is simple
   93: 
   94: -   However, it also means that one badly written driver can affect the
   95:     whole kernel
   96: 
   97: -   Numerous in-kernel frameworks standardise the way drivers are
   98:     written (bus\_space, autoconf, etc.)
   99: 
  100: ### The NetBSD source directory structure
  101: 
  102: -   We’ll only cover parts interesting for a device driver programmer
  103: 
  104: -   src/sys/
  105: 
  106:     - kernel source directory
  107: 
  108: -   src/sys/dev/
  109: 
  110:     - machine-independent device drivers
  111: 
  112: -   src/sys/arch/
  113: 
  114:     - port-specific or architecture-specific parts (such as the
  115:     low-level system initialisation procedures or machine-dependent
  116:     drivers)
  117: 
  118: -   src/sys/arch/\$PORTNAME/conf/
  119: 
  120:     - kernel configuration files for a given port
  121: 
  122: ### Kernel autoconfiguration framework - autoconf(9)
  123: 
  124: -   Autoconfiguration is the process of matching hardware devices with
  125:     an appropriate device driver
  126: 
  127: -   The kernel message buffer (dmesg) contains information about
  128:     autoconfiguration of devices
  129: 
  130: -   driver0 at bus0: Foo hardware
  131: 
  132:     -   Instance 0 of the driver has attached to instance 0 of the
  133:         particular bus
  134: 
  135:     -   Such messages often carry additional bus-specific information
  136:         about the exact location of the device (like the device and
  137:         function number on the PCI bus)
  138: 
  139: -   driver0: some message
  140: 
  141:     -   Additional information about the driver state or device
  142:         configuration
  143: 
  144: ### Autoconfiguration as seen in the dmesg
  145: 
  146:     NetBSD 6.99.12 (GENERIC) #7: Fri Oct  5 18:43:21 CEST 2012
  147:             rkujawa@saiko.local:/Users/rkujawa/netbsd-eurobsdcon2012/src/sys/arch/cobalt/compile/obj/GENERIC
  148:     Cobalt Qube 2
  149:     total memory = 32768 KB
  150:     avail memory = 27380 KB
  151:     mainbus0 (root)
  152:     com0 at mainbus0 addr 0x1c800000 level 3: ns16550a, working fifo
  153:     com0: console
  154:     cpu0 at mainbus0: QED RM5200 CPU (0x28a0) Rev. 10.0 with built-in FPU Rev. 1.0
  155:     cpu0: 48 TLB entries, 256MB max page size
  156:     cpu0: 32KB/32B 2-way set-associative L1 instruction cache
  157:     cpu0: 32KB/32B 2-way set-associative write-back L1 data cache
  158:     mcclock0 at mainbus0 addr 0x10000070: mc146818 compatible time-of-day clock
  159:     panel0 at mainbus0 addr 0x1f000000
  160:     gt0 at mainbus0 addr 0x14000000
  161:     pci0 at gt0
  162:     pchb0 at pci0 dev 0 function 0: Galileo GT-64011 System Controller, rev 1
  163:     pcib0 at pci0 dev 9 function 0
  164:     pcib0: VIA Technologies VT82C586 PCI-ISA Bridge, rev 57
  165:     viaide0 at pci0 dev 9 function 1
  166:     viaide0: VIA Technologies VT82C586 (Apollo VP) ATA33 controller
  167:     viaide0: primary channel interrupting at irq 14
  168:     atabus0 at viaide0 channel 0
  169:     viaide0: secondary channel interrupting at irq 15
  170:     atabus1 at viaide0 channel 1
  171:     wd0 at atabus0 drive 0
  172:     wd0: <netbsd-cobalt.img>
  173:     wd0: 750 MB, 1524 cyl, 16 head, 63 sec, 512 bytes/sect x 1536192 sectors
  174: 
  175: ### Autoconfiguration as seen in the dmesg
  176: 
  177: ![image](img_cobaltdevices.png)
  178: 
  179: ### The bus\_space(9) framework
  180: 
  181: -   “The goal of the bus\_space functions is to allow a single driver
  182:     source file to manipulate a set of devices on different system
  183:     architectures, and to allow a single driver object file to
  184:     manipulate a set of devices on multiple bus types on a single
  185:     architecture.”
  186: 
  187: -   Provides a set of functions implementing common operations on the
  188:     bus like mapping, reading, writing, copying, etc.
  189: 
  190: -   The bus\_space(9) is implemented at the machine-dependent level
  191:     (typically it’s a part of architecture-specific code), but all
  192:     implementations present the same interface[^1]
  193: 
  194: ### Machine independent drivers
  195: 
  196: -   If possible drivers should work on any hardware platform
  197: 
  198: -   High quality, machine-independent (MI) drivers are an important
  199:     factor that adds to NetBSD portability
  200: 
  201: -   Some drivers are completely MI, some have MD or bus dependent
  202:     attachments and some are completely MD
  203: 
  204:     -   A driver for a typical PCI card will be completely MI
  205: 
  206:     -   A driver for the components of a SoC will usually be completely
  207:         MD
  208: 
  209: -   The bus\_space abstraction helps to achieve portability,
  210:     transparently handling endianness issues and hiding bus
  211:     implementation details from the device driver
  212: 
  213: -   Even if we have MI drivers, writing the drivers is always
  214:     significant part of effort needed to port NetBSD to new hardware
  215: 
  216: Example driver from scratch
  217: ===========================
  218: 
  219: ### Development environment
  220: 
  221: -   Out of scope of this course, but very well documented
  222: 
  223: -   Cross compiling is an easy task with the build.sh script
  224: 
  225: -   Described in [Part V of the NetBSD
  226:     Guide](http://www.netbsd.org/docs/guide/en/part-compile.html)
  227: 
  228: -   Check out the NetBSD sources
  229: 
  230: -   \$ build.sh -m cobalt tools
  231: 
  232:     will build compiler, assembler, linker, etc. for cobalt port
  233: 
  234: -   \$ build.sh -m cobalt kernel=GENERIC
  235: 
  236:     will build the GENERIC kernel for cobalt
  237: 
  238: -   Call build.sh with a -u parameter to update (won’t rebuilding
  239:     everything)
  240: 
  241: -   build.sh
  242: 
  243:     is calling nbconfig and nbmake tools, no magic involved
  244: 
  245: ### Quick introduction to GXemul
  246: 
  247: -   A framework for full-system computer architecture emulation,
  248:     excellent for educational purposes
  249: 
  250: -   Capable of emulating several real machines supported by NetBSD
  251: 
  252: -   We’ll emulate a [Cobalt](http://en.wikipedia.org/wiki/Cobalt_Qube),
  253:     MIPS-based micro server with PCI bus
  254: 
  255: -   I’ve modified GXemul and implemented an emulation of an additional
  256:     PCI device
  257: 
  258: -   It will be used to show (almost) a real-life example of the driver
  259:     development process
  260: 
  261: ### Our hardware - functional description
  262: 
  263: -   Business applications often use arithmetic operations like addition
  264: 
  265: -   Fake Cards Inc. responded to market needs and created a new product,
  266:     Advanced Addition Accelerator
  267: 
  268: -   Pointy Haired Bosses will certainly buy it to accelerate their
  269:     business applications, so let’s create a driver for NetBSD!
  270: 
  271: ### Our hardware - technical details
  272: 
  273: -   Overview
  274: 
  275:     -   Implemented as a PCI device
  276: 
  277:     -   Arithmetic unit capable of addition of two numbers
  278: 
  279:     -   Four[^2] registers in the PCI memory space
  280: 
  281: -   PCI configuration space
  282: 
  283:     -   Identified by the PCI vendor ID 0xfabc and product ID 0x0001
  284: 
  285:     -   Base Address Register 0x10 used to configure the engine address
  286: 
  287:     -   4 x 32-bit registers = 16 bytes
  288: 
  289:     -   Other configuration registers irrelevant
  290: 
  291: [faaop]
  292: 
  293: ### Our hardware - technical details (memory mapped register set)
  294: 
  295: -   Advanced Addition Acceleration registers
  296: 
  297:    Register Name   Offset                          Description
  298:   --------------- -------- -----------------------------------------------------------
  299:       COMMAND       0x4           Register used to issue commands to the engine
  300:        DATA         0x8      Register used to load data to internal engine registers
  301:       RESULT        0xC     Register used to store the result of arithmetic operation
  302: 
  303: -   COMMAND register
  304: 
  305:    Bit   R/W                                Description
  306:   ----- ----- -----------------------------------------------------------------------
  307:     0     W    Execute ADD operation on values loaded into internal register A and B
  308:     1    R/W        Select internal register A for access through DATA register
  309:     2    R/W        Select internal register B for access through DATA register
  310: 
  311: -   Selecting internal register A and B at the same time will lead to
  312:     undefined behaviour
  313: 
  314: ### Our hardware - technical details (memory mapped register set)
  315: 
  316: -   DATA register
  317: 
  318:    Bit    R/W                     Description
  319:   ------ ----- --------------------------------------------------
  320:    0:31   R/W   Read/write the value in internal engine register
  321: 
  322: -   RESULT register
  323: 
  324:    Bit    R/W                Description
  325:   ------ ----- ----------------------------------------
  326:    0:31    R    Holds the result of last ADD operation
  327: 
  328: ### Our hardware - technical details (operation algorithm)
  329: 
  330: -   Select the internal register A for access (write 0x2 into COMMAND
  331:     register)
  332: 
  333: -   Write the first number into DATA register
  334: 
  335: -   Select the internal register B for access (write 0x4 into COMMAND
  336:     register)
  337: 
  338: -   Write the second number into DATA register
  339: 
  340: -   Issue the ADD operation (write 0x1 into COMMAND register)
  341: 
  342: -   Read the result from RESULT register
  343: 
  344: ### Adding a new driver to the NetBSD kernel
  345: 
  346: -   We’ll discuss the steps needed to add a new MI PCI device driver to
  347:     the NetBSD kernel
  348: 
  349:     -   Add the vendor and device ID to the database of PCI IDs
  350: 
  351:     -   Create a set of the driver source files in
  352:         src/sys/dev/\$BUSNAME/
  353: 
  354:     -   Add the new driver to src/sys/dev/\$BUSNAME/\$BUSNAME.files file
  355: 
  356:     -   Add the new driver to DEVNAMES[^3] file
  357: 
  358: ### Modifying the PCI device database
  359: 
  360:     unmatched vendor 0xfabc product 0x0001 (Co-processor 
  361:     processor, revision 0x01) at pci0 dev 12 function 0 
  362:     not configured
  363: 
  364: -   The kernel does not know anything about this vendor and device
  365: 
  366: -   Add it to the PCI device database - src/sys/dev/pci/pcidevs
  367: 
  368: -   vendor VENDORNAME 0xVENDORID Long Vendor Name
  369: 
  370: -   product VENDORNAME PRODUCTNAME 0xPRODUCTID Long Product Name
  371: 
  372: -   To regenerate pcidevs\*.h run awk -f devlist2h.awk pcidevs or
  373:     Makefile.pcidevs if you’re on NetBSD
  374: 
  375: ### Modifying the PCI device database - example
  376: 
  377:     --- pcidevs 29 Sep 2012 10:26:14 -0000  1.1139
  378:     +++ pcidevs 5 Oct 2012 08:52:59 -0000
  379:     @@ -669,6 +669,7 @@
  380:      vendor CHRYSALIS   0xcafe  Chrysalis-ITS
  381:      vendor MIDDLE_DIGITAL  0xdeaf  Middle Digital
  382:      vendor ARC     0xedd8  ARC Logic
  383:     +vendor FAKECARDS   0xfabc  Fake Cards
  384:      vendor INVALID     0xffff  INVALID VENDOR ID
  385:      
  386:      /*
  387:     @@ -2120,6 +2121,9 @@
  388:      /* Eumitcom products */
  389:      product EUMITCOM WL11000P  0x1100  WL11000P PCI WaveLAN/IEEE 802.11
  390:      
  391:     +/* FakeCards products */
  392:     +product FAKECARDS AAA      0x0001  Advanced Addition Accelerator
  393:     +
  394:      /* O2 Micro */
  395:      product O2MICRO 00F7       0x00f7  Integrated OHCI IEEE 1394 Host Controller
  396:      product O2MICRO OZ6729     0x6729  OZ6729 PCI-PCMCIA Bridge
  397: 
  398: ### Modifying the PCI device database - example
  399: 
  400:     Fake Cards Advanced Addition Accelerator (Co-processor 
  401:     processor, revision 0x01) at pci0 dev 12 function 0 
  402:     not configured
  403: 
  404: -   Now the kernel knows the vendor and product ID
  405: 
  406: -   But there’s still no driver for this device
  407: 
  408: ### Adding the new PCI driver
  409: 
  410: -   Choose a name - short, easy to remember, avoid numbers
  411: 
  412:     -   faa
  413: 
  414:         looks like a good name, but you can choose any name you like
  415: 
  416: -   Create a set of new files in src/sys/dev/pci
  417: 
  418:     -   faa.c
  419: 
  420:         - main driver code
  421: 
  422:     -   faareg.h
  423: 
  424:         - register definitions[^4]
  425: 
  426:     -   faavar.h
  427: 
  428:         - driver structures and functions used in other parts of the
  429:         kernel[^5]
  430: 
  431: -   Modify driver definitions
  432: 
  433:     -   src/sys/dev/pci/files.pci
  434: 
  435:     -   src/sys/dev/DEVNAMES
  436: 
  437: -   Configure the kernel to use the newly added driver -
  438:     src/sys/arch/\$PORTNAME/conf/GENERIC
  439: 
  440: ### Adding the new PCI driver - main driver
  441: 
  442: -   Kernel includes are at the beginning, followed by machine-specific
  443:     and bus-specific includes
  444: 
  445: -   Should also include faareg.h and faavar.h files
  446: 
  447: -   A minimal driver needs just two functions
  448: 
  449:     -   faa\_match
  450: 
  451:         (or faa\_probe for some buses)
  452: 
  453:     -   faa\_attach
  454: 
  455: -   The CFATTACH\_DECL\_NEW macro plugs the above functions into
  456:     autoconf(9) mechanism
  457: 
  458: ### Adding the new PCI driver - main driver
  459: 
  460: -   static int faa\_match(device\_t parent, cfdata\_t match, void
  461:     \*aux);
  462: 
  463:     -   Check if the driver should attach to a given device (for example
  464:         in case of PCI bus, it will be used to check vendor and product
  465:         ID)
  466: 
  467:     -   parent
  468: 
  469:         - pointer to parent’s driver device structure
  470: 
  471:     -   match
  472: 
  473:         - pointer to autoconf(9) details structure
  474: 
  475:     -   aux
  476: 
  477:         - despite the name the most important argument, usually contains
  478:         bus-specific structure describing device details
  479: 
  480: -   static void faa\_attach(device\_t parent, device\_t self, void
  481:     \*aux);
  482: 
  483:     -   Attach the driver to a given device
  484: 
  485:     -   parent
  486: 
  487:         - same as with match function
  488: 
  489:     -   self
  490: 
  491:         - pointer to driver’s device structure
  492: 
  493:     -   aux
  494: 
  495:         - same as with match function
  496: 
  497: -   See definitions of these functions in the
  498:     [driver(9)](http://netbsd.gw.com/cgi-bin/man-cgi?driver+9+NetBSD-current)
  499:     man page.
  500: 
  501: ### Adding the new PCI driver - main driver cont’d
  502: 
  503: -   CFATTACH\_DECL\_NEW(faa, sizeof(struct faa\_softc), faa\_match,
  504:     faa\_attach, NULL, NULL);
  505: 
  506:     -   driver name
  507: 
  508:     -   size of softc structure containing state of driver’s instance
  509: 
  510:     -   match/probe function
  511: 
  512:     -   attach function
  513: 
  514:     -   detach function
  515: 
  516:     -   activate function
  517: 
  518: -   The “\_NEW” name is unfortunate
  519: 
  520: -   Pass NULL for unimplemented functions
  521: 
  522: -   We won’t cover detach and activate now, as they are not needed for a
  523:     simple driver
  524: 
  525: ### Adding the new PCI driver - main driver example
  526: 
  527: -   src/sys/dev/pci/faa.c
  528: 
  529: <!-- -->
  530: 
  531:     #include <sys/cdefs.h>
  532:     __KERNEL_RCSID(0, "$NetBSD: bus_space_tutorial.mdwn,v 1.3 2013/06/23 14:16:27 mspo Exp $");
  533:     #include <sys/param.h>
  534:     #include <sys/device.h>
  535:     #include <dev/pci/pcivar.h>
  536:     #include <dev/pci/pcidevs.h>
  537:     #include <dev/pci/faareg.h>
  538:     #include <dev/pci/faavar.h>
  539: 
  540:     static int      faa_match(device_t, cfdata_t, void *);
  541:     static void     faa_attach(device_t, device_t, void *);
  542: 
  543:     CFATTACH_DECL_NEW(faa, sizeof(struct faa_softc),
  544:         faa_match, faa_attach, NULL, NULL);
  545: 
  546:     static int
  547:     faa_match(device_t parent, cfdata_t match, void *aux)
  548:     {
  549:             return 0;
  550:     }
  551: 
  552:     static void
  553:     faa_attach(device_t parent, device_t self, void *aux)
  554:     { 
  555:     }
  556: 
  557: ### Adding the new PCI driver - auxiliary includes
  558: 
  559: -   src/sys/dev/pci/faareg.h
  560: 
  561: <!-- -->
  562: 
  563:     #ifndef FAAREG_H
  564:     #define FAAREG_H
  565:     /* 
  566:      * Registers are defined using preprocessor:
  567:      * #define FAA_REGNAME  0x0
  568:      * We'll add them later, let's leave it empty for now.
  569:      */
  570:     #endif /* FAAREG_H */
  571: 
  572: -   src/sys/dev/pci/faavar.h
  573: 
  574: <!-- -->
  575: 
  576:     #ifndef FAAVAR_H
  577:     #define FAAVAR_H
  578: 
  579:     /* sc_dev is an absolute minimum, we'll add more later */
  580:     struct faa_softc {
  581:             device_t sc_dev;
  582:     };
  583:     #endif /* FAAVAR_H */
  584: 
  585: ### Adding the new PCI driver - registering the driver (courtesy)
  586: 
  587: -   src/sys/dev/DEVNAMES
  588: 
  589: <!-- -->
  590: 
  591:     --- DEVNAMES    1 Sep 2012 11:19:58 -0000   1.279
  592:     +++ DEVNAMES    6 Oct 2012 19:59:06 -0000
  593:     @@ -436,6 +436,7 @@
  594:      ex         MI
  595:      exphy          MI
  596:      ezload         MI      Attribute
  597:     +faa            MI
  598:      fb         luna68k
  599:      fb         news68k
  600:      fb         newsmips
  601: 
  602: ### Adding the new PCI driver - registering the driver
  603: 
  604: -   See config(5)
  605: 
  606: -   src/sys/dev/pci/files.pci
  607: 
  608: <!-- -->
  609: 
  610:     --- pci/files.pci   2 Aug 2012 00:17:44 -0000   1.360
  611:     +++ pci/files.pci   6 Oct 2012 19:59:10 -0000
  612:     @@ -1122,3 +1122,9 @@
  613:      device tdvfb: wsemuldisplaydev, rasops8, vcons, videomode
  614:      attach tdvfb at pci
  615:      file   dev/pci/tdvfb.c     tdvfb   
  616:     +
  617:     +# FakeCards Advanced Addition Accelerator
  618:     +device faa
  619:     +attach faa at pci
  620:     +file   dev/pci/faa.c       faa 
  621:     +
  622: 
  623: ### Adding the new PCI driver to the kernel configuration
  624: 
  625: -   src/sys/arch/cobalt/conf/GENERIC
  626: 
  627: <!-- -->
  628: 
  629:     --- GENERIC 10 Mar 2012 21:51:50 -0000  1.134
  630:     +++ GENERIC 6 Oct 2012 20:12:37 -0000
  631:     @@ -302,6 +302,9 @@
  632:      #fms*      at pci? dev ? function ?    # Forte Media FM801
  633:      #sv*       at pci? dev ? function ?    # S3 SonicVibes
  634:      
  635:     +# Fake Cards Advanced Addition Accelerator
  636:     +faa*       at pci? dev ? function ?
  637:     +
  638:      # Audio support
  639:      #audio*        at audiobus?
  640: 
  641: -   The above definition means that an instance of faa may be attached
  642:     to any PCI bus, any device, any function
  643: 
  644: -   The exact position of the rule in the configuration file is not
  645:     important in this case
  646: 
  647: -   See
  648:     [config(5)](http://netbsd.gw.com/cgi-bin/man-cgi?config+5+NetBSD-current)
  649:     for a description of the device definition language
  650: 
  651: ### Adding the new PCI driver - example
  652: 
  653: -   The driver should compile now
  654: 
  655: -   The driver’s match function will check if the driver is able to work
  656:     with a given device
  657: 
  658: -   Since it is not implemented, the kernel will not attach the driver
  659: 
  660: ### Matching the PCI device
  661: 
  662: -   Modify the faa\_match function to match the specified PCI device
  663: 
  664: -   Use PCI\_VENDOR and PCI\_PRODUCT macros to obtain the IDs
  665: 
  666: <!-- -->
  667: 
  668:     static int
  669:     faa_match(device_t parent, cfdata_t match, void *aux)
  670:     {
  671:             const struct pci_attach_args *pa = (const struct pci_attach_args *)aux;
  672: 
  673:             if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FAKECARDS) 
  674:                 && (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_FAKECARDS_AAA))
  675:                     return 1;
  676: 
  677:             return 0;
  678:     }
  679: 
  680: ### Attaching to the PCI device
  681: 
  682:     faa0 at pci0 dev 12 function 0
  683: 
  684: -   The driver has successfully matched and attached to the PCI device
  685:     but still is not doing anything useful
  686: 
  687: -   Let’s fill the attach function and actually program the hardware
  688: 
  689: ### Variable types used with bus\_space
  690: 
  691: -   bus\_space\_tag\_t
  692: 
  693:     – type used to describe a particular bus, usually passed to the
  694:     driver from MI bus structures
  695: 
  696: -   bus\_space\_handle\_t
  697: 
  698:     – used to describe a mapped range of bus space, usually created with
  699:     the bus\_space\_map() function
  700: 
  701: -   bus\_addr\_t
  702: 
  703:     – address on the bus
  704: 
  705: -   bus\_size\_t
  706: 
  707:     – an amount of space on the bus
  708: 
  709: -   Contents of these types are MD, so avoid modifying from within the
  710:     driver[^6]
  711: 
  712: ### Why do we need to “map” the resources?
  713: 
  714: -   “The bus space must be mapped before it can be used, and should be
  715:     unmapped when it is no longer needed”
  716: 
  717: -   It’s a machine-dependent process but it’s also conveniently hidden
  718:     from the programmer by the bus\_space framework
  719: 
  720: ### Mapping the hardware resources
  721: 
  722: -   The generic bus\_space(9) way to map space
  723: 
  724: <!-- -->
  725: 
  726:     bus_space_map(bus_space_tag_t space, bus_addr_t address, 
  727:     bus_size_t size, int flags, bus_space_handle_t  *handlep);
  728: 
  729: -   bus\_space\_map
  730: 
  731:     creates a mapping from the physical address to a kernel virtual
  732:     address
  733: 
  734: -   space
  735: 
  736:     – represents the bus on which the mapping will be created
  737: 
  738: -   address
  739: 
  740:     – typically represents the physical address for which a mapping will
  741:     be created
  742: 
  743: -   size
  744: 
  745:     – describes the amount of bus space to be mapped
  746: 
  747: -   handlep
  748: 
  749:     – pointer to mapped space (filled after successful mapping)
  750: 
  751: -   Separate space and address
  752: 
  753: ### Mapping the hardware resources
  754: 
  755: -   The PCI-specific way to map space
  756: 
  757: <!-- -->
  758: 
  759:     pci_mapreg_map(const struct pci_attach_args *pa, int reg, pcireg_t type, 
  760:     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep, 
  761:     bus_addr_t *basep, bus_size_t *sizep);
  762: 
  763: -   pci\_mapreg\_map
  764: 
  765:     creates mapping from physical address present in specified BAR
  766:     register to kernel virtual address
  767: 
  768: -   pa
  769: 
  770:     – struct describing PCI attachment details (passed through aux)
  771: 
  772: -   reg
  773: 
  774:     – BAR register number
  775: 
  776: -   type
  777: 
  778:     – Select mapping type (I/O, memory)
  779: 
  780: -   busflags
  781: 
  782:     – Passed to bus\_space\_map flags argument
  783: 
  784: -   tagp
  785: 
  786:     – pointer to bus\_space\_tag
  787: 
  788: -   handlep
  789: 
  790:     – pointer to a mapped space
  791: 
  792: -   basep
  793: 
  794:     – address of a mapped space
  795: 
  796: -   sizep
  797: 
  798:     – size of mapped space (equivalent to BAR size)
  799: 
  800: -   The last four parameters are filled after successful mapping
  801: 
  802: ### Mapping the registers using BAR - adding auxiliary includes
  803: 
  804: -   src/sys/dev/pci/faareg.h
  805: 
  806: <!-- -->
  807: 
  808:     #define FAA_MMREG_BAR   0x10
  809: 
  810: -   src/sys/dev/pci/faavar.h
  811: 
  812: <!-- -->
  813: 
  814:     struct faa_softc {
  815:             device_t sc_dev;
  816: 
  817:             bus_space_tag_t sc_regt;
  818:             bus_space_handle_t sc_regh;
  819:             bus_addr_t sc_reg_pa;
  820: 
  821:     };
  822: 
  823: ### Mapping the registers using BAR - main driver code
  824: 
  825: -   src/sys/dev/pci/faa.c
  826: 
  827: <!-- -->
  828: 
  829:     static void
  830:     faa_attach(device_t parent, device_t self, void *aux)
  831:     {
  832:             struct faa_softc *sc = device_private(self);
  833:             const struct pci_attach_args *pa = aux;
  834: 
  835:             sc->sc_dev = self;
  836: 
  837:             pci_aprint_devinfo(pa, NULL);
  838: 
  839:             if (pci_mapreg_map(pa, FAA_MMREG_BAR, PCI_MAPREG_TYPE_MEM, 0, 
  840:                 &sc->sc_regt, &sc->sc_regh, &sc->sc_reg_pa, 0) != 0 ) {
  841:                 aprint_error_dev(sc->sc_dev, "can't map the BAR\n");
  842:                 return;
  843:             }
  844: 
  845:             aprint_normal_dev(sc->sc_dev, "regs at 0x%08x\n", (uint32_t) sc->sc_reg_pa);
  846:     }
  847: 
  848: ### Accessing the hardware registers
  849: 
  850: -   The bus\_space\_read\_ and bus\_space\_write\_ functions are basic
  851:     methods of reading and writing the hardware registers
  852: 
  853: -   uintX\_t bus\_space\_read\_X(bus\_space\_tag\_t space,
  854:     bus\_space\_handle\_t handle, bus\_size\_t offset);
  855: 
  856: -   void bus\_space\_write\_X(bus\_space\_tag\_t space,
  857:     bus\_space\_handle\_t handle, bus\_size\_t offset, uintX\_t value);
  858: 
  859:     -   space
  860: 
  861:         - tag describing the bus
  862: 
  863:     -   handle
  864: 
  865:         - describes the exact location on the bus where read/write
  866:         should occur, this handle is obtained by bus\_space\_map
  867: 
  868:     -   offset
  869: 
  870:         - offset from handle location
  871: 
  872:     -   The read function returns the data read from the specified
  873:         location, while write has an argument value which should be
  874:         filled with data to be written
  875: 
  876: ### Variants of bus\_space\_read and bus\_space\_write
  877: 
  878:     Data       Read function         Write function
  879:   -------- --------------------- ----------------------
  880:    8-bit    bus\_space\_read\_1   bus\_space\_write\_1
  881:    16-bit   bus\_space\_read\_2   bus\_space\_write\_2
  882:    32-bit   bus\_space\_read\_4   bus\_space\_write\_4
  883:    64-bit   bus\_space\_read\_8   bus\_space\_write\_8
  884: 
  885: -   There are many more variants of read and write functions and they
  886:     are useful in certain situations, see the
  887:     [bus\_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current)
  888:     man page
  889: 
  890: ### Accessing the hardware registers - example
  891: 
  892: -   Create a function that will write a value into the DATA register of
  893:     our device, then read it back and check if the value is the same as
  894:     written
  895: 
  896: -   Define the DATA register in the driver
  897: 
  898: -   src/sys/dev/pci/faareg.h
  899: 
  900: <!-- -->
  901: 
  902:     #define FAA_DATA                0x8
  903:     #define FAA_COMMAND             0x4
  904:     #define FAA_COMMAND_STORE_A         __BIT(1)
  905: 
  906: -   Define the new function in main driver code
  907: 
  908: -   static bool faa\_check(struct faa\_softc \*sc);
  909: 
  910: ### Accessing the hardware registers - example
  911: 
  912: -   src/sys/dev/pci/faa.c
  913: 
  914: <!-- -->
  915: 
  916:     static void
  917:     faa_attach(device_t parent, device_t self, void *aux)
  918:     {
  919:        /* ... */
  920:        if (!faa_check(sc)) {
  921:             aprint_error_dev(sc->sc_dev, "hardware not responding\n");
  922:             return;
  923:        }
  924:     }
  925: 
  926:     static bool
  927:     faa_check(struct faa_softc *sc)
  928:     {
  929:             uint32_t testval = 0xff11ee22; 
  930:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_COMMAND, FAA_COMMAND_STORE_A);
  931:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_DATA, testval);
  932:             if (bus_space_read_4(sc->sc_regt, sc->sc_regh, FAA_DATA) == testval)
  933:                     return true;
  934: 
  935:             return false;
  936:     }
  937: 
  938: ### Accessing the hardware registers - running the example
  939: 
  940: -   Update the kernel binary and run it again
  941: 
  942: -   Check the GXemul log
  943: 
  944: <!-- -->
  945: 
  946:     [ faa: COMMAND register (0x4) WRITE value 0x2 ]
  947:     [ faa: DATA register (0x8) WRITE value 0xff11ee22 ]
  948:     [ faa: DATA register (0x8) READ value 0xff11ee22 ]
  949: 
  950: -   GXemul will conveniently display all accesses to our device
  951: 
  952: -   The faa driver still does attach without error, which means that the
  953:     check function is working properly
  954: 
  955: <!-- -->
  956: 
  957:     faa0 at pci0 dev 12 function 0: Fake Cards Advanced Addition Accelerator (rev. 0x01)
  958:     faa0: registers at 0x10110000
  959: 
  960: ### Implementing addition using the hardware
  961: 
  962: -   The basic principle of device operation should be laid out in the
  963:     data sheet
  964: 
  965: -   We need to implement an algorithm based on this description
  966: 
  967: -   Writing such an algorithm is often not needed, since the NetBSD
  968:     kernel already has frameworks for common device types (such as
  969:     atabus/wd for IDE and SATA hard disk controllers, wsdisplay/wscons
  970:     for frame buffers, etc.)
  971: 
  972: ### Implementing addition using the hardware
  973: 
  974: -   Define all registers
  975: 
  976: -   src/sys/dev/pci/faareg.h
  977: 
  978: <!-- -->
  979: 
  980:     #define FAA_STATUS              0x0
  981:     #define FAA_COMMAND             0x4
  982:     #define FAA_COMMAND_ADD             __BIT(0)        
  983:     #define FAA_COMMAND_STORE_A         __BIT(1)
  984:     #define FAA_COMMAND_STORE_B         __BIT(2)
  985:     #define FAA_DATA                0x8
  986:     #define FAA_RESULT              0xC
  987: 
  988: ### Implementing addition using the hardware
  989: 
  990: -   Add a new function to the main driver code
  991: 
  992: -   src/sys/dev/pci/faa.c
  993: 
  994: <!-- -->
  995: 
  996:     static void
  997:     faa_attach(device_t parent, device_t self, void *aux)
  998:     {
  999:             /* ... */
 1000:             aprint_normal_dev(sc->sc_dev, "just checking: 1 + 2 = %d\n", faa_add(sc, 1, 2));
 1001:     }
 1002: 
 1003:     static uint32_t
 1004:     faa_add(struct faa_softc *sc, uint32_t a, uint32_t b)
 1005:     {
 1006:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_COMMAND, FAA_COMMAND_STORE_A);
 1007:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_DATA, a);
 1008:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_COMMAND, FAA_COMMAND_STORE_B);
 1009:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_DATA, b);
 1010:             bus_space_write_4(sc->sc_regt, sc->sc_regh, FAA_COMMAND, FAA_COMMAND_ADD);
 1011:             return bus_space_read_4(sc->sc_regt, sc->sc_regh, FAA_RESULT);
 1012:     }
 1013: 
 1014: ### Implementing addition using the hardware - running the example
 1015: 
 1016: -   Update the kernel binary and run it again
 1017: 
 1018: -   Check GXemul log
 1019: 
 1020: <!-- -->
 1021: 
 1022:     [ faa: COMMAND register (0x4) WRITE value 0x2 ]
 1023:     [ faa: DATA register (0x8) WRITE value 0x1 ]
 1024:     [ faa: COMMAND register (0x4) WRITE value 0x4 ]
 1025:     [ faa: DATA register (0x8) WRITE value 0x2 ]
 1026:     [ faa: COMMAND register (0x4) WRITE value 0x1 ]
 1027:     [ faa: RESULT register (0xC) READ value 0x3 ]
 1028: 
 1029: -   Looks like it worked!
 1030: 
 1031: <!-- -->
 1032: 
 1033:     faa0 at pci0 dev 12 function 0: Fake Cards Advanced Addition Accelerator (rev. 0x01)
 1034:     faa0: registers at 0x10110000
 1035:     faa0: just checking: 1 + 2 = 3
 1036: 
 1037: ## Interacting with userspace
 1038: 
 1039: ### The kernel-user space interface
 1040: 
 1041: -   Now that the core functionality of the kernel driver is working, it
 1042:     should be exposed to user space
 1043: 
 1044: -   The interface between kernel driver and userspace can be designed in
 1045:     many different ways
 1046: 
 1047: -   The classic UNIX way of interfacing between the kernel and user
 1048:     space is a device file
 1049: 
 1050: -   Even when using device files there is no single interfacing method
 1051:     that fits all use cases
 1052: 
 1053: -   It’s up to the programmer to define the communication protocol
 1054: 
 1055: ### Device files
 1056: 
 1057: -   crw-r—– 1 root wheel 101, 1 Aug 12 21:53 /dev/file
 1058: 
 1059: -   The kernel identifies which driver should service the request to
 1060:     this file by using major and minor numbers (101 and 1 in the example
 1061:     above)
 1062: 
 1063: -   The major number identifies the driver
 1064: 
 1065: -   The minor number usually identifies the driver instance, although
 1066:     the driver is free to use it in any other way
 1067: 
 1068: -   In NetBSD device files are created statically
 1069: 
 1070:     -   By the MAKEDEV script during installation or boot
 1071: 
 1072:     -   Manually by using the mknod utility
 1073: 
 1074: ### Operations on device files
 1075: 
 1076: -   [open(2)](http://netbsd.gw.com/cgi-bin/man-cgi?read++NetBSD-current)
 1077:     and
 1078:     [close(2)](http://netbsd.gw.com/cgi-bin/man-cgi?read++NetBSD-current)
 1079: 
 1080: -   [read(2)](http://netbsd.gw.com/cgi-bin/man-cgi?read++NetBSD-current)
 1081:     and
 1082:     [write(2)](http://netbsd.gw.com/cgi-bin/man-cgi?write++NetBSD-current)
 1083: 
 1084: -   [ioctl(2)](http://netbsd.gw.com/cgi-bin/man-cgi?write++NetBSD-current)
 1085: 
 1086: -   [poll(2)](http://netbsd.gw.com/cgi-bin/man-cgi?poll++NetBSD-current)
 1087: 
 1088: -   [mmap(2)](http://netbsd.gw.com/cgi-bin/man-cgi?write++NetBSD-current)
 1089: 
 1090: -   and more…
 1091: 
 1092: -   Any mix of the above system calls might be used to interface between
 1093:     the kernel and user space
 1094: 
 1095: -   We’ll implement an ioctl(2)-based communication mechanism
 1096: 
 1097: ### Adding cdevsw
 1098: 
 1099: -   cdevsw
 1100: 
 1101:     is used to decide which operation on the character device file calls
 1102:     which driver function
 1103: 
 1104: -   Not all calls have to be implemented, although some device layers
 1105:     define a set of calls that a driver must implement
 1106: 
 1107: -   For example disk drivers must implement open, close, read, write and
 1108:     ioctl
 1109: 
 1110: -   src/sys/dev/pci/faa.c
 1111: 
 1112: <!-- -->
 1113: 
 1114:     dev_type_open(faaopen);
 1115:     dev_type_close(faaclose);
 1116:     dev_type_ioctl(faaioctl);
 1117: 
 1118:     const struct cdevsw faa_cdevsw = {
 1119:             faaopen, faaclose, noread, nowrite, faaioctl,
 1120:             nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
 1121:     };
 1122: 
 1123: ### Prototyping the cdevsw operations
 1124: 
 1125: -   The dev\_type\* macros are used to prototype the functions passed to
 1126:     cdevsw
 1127: 
 1128: -   Pass no followed by a function name to the appropriate cdevsw field
 1129:     if it is not implemented
 1130: 
 1131: -   There’s also bdevsw for block devices, but we won’t use it in this
 1132:     example
 1133: 
 1134: -   The last member of the cdevsw structure defines the device flags,
 1135:     originally it was used to define the device type (still used for
 1136:     disks, tape drives and ttys, for other devices pass D\_OTHER)
 1137: 
 1138: ### Implemeting the cdevsw operations - open / close
 1139: 
 1140: -   src/sys/dev/pci/faa.c
 1141: 
 1142: <!-- -->
 1143: 
 1144:     int
 1145:     faaopen(dev_t dev, int flags, int mode, struct lwp *l)
 1146:     {
 1147:             struct faa_softc *sc;
 1148:             sc = device_lookup_private(&faa_cd, minor(dev));
 1149: 
 1150:             if (sc == NULL)
 1151:                     return ENXIO;
 1152:             if (sc->sc_flags & FAA_OPEN)
 1153:                     return EBUSY;
 1154: 
 1155:             sc->sc_flags |= FAA_OPEN;
 1156:             return 0;
 1157:     }
 1158:     int
 1159:     faaclose(dev_t dev, int flag, int mode, struct lwp *l)
 1160:     {
 1161:             struct faa_softc *sc;
 1162:             sc = device_lookup_private(&faa_cd, minor(dev));
 1163: 
 1164:             if (sc->sc_flags & FAA_OPEN)
 1165:                     sc->sc_flags =~ FAA_OPEN;
 1166: 
 1167:             return 0;
 1168:     }
 1169: 
 1170: ### Defining the ioctls
 1171: 
 1172: -   ioctl(2)
 1173: 
 1174:     can be used to call kernel-level functions and exchange data between
 1175:     the kernel and user space
 1176: 
 1177: -   The classic way of passing data is by using structures, their
 1178:     definitions are shared between the kernel and user space code
 1179: 
 1180: -   The driver might support more than one ioctl, the \_IO\* macros are
 1181:     used to define the operation and associated structure used to
 1182:     exchange data
 1183: 
 1184:     -   \_IO
 1185: 
 1186:         - just a kernel function call, no data exchange
 1187: 
 1188:     -   \_IOR
 1189: 
 1190:         - kernel function call and data pass from kernel to user space
 1191: 
 1192:     -   \_IOW
 1193: 
 1194:         - kernel function call and data pass from user space to kernel
 1195: 
 1196:     -   \_IOWR
 1197: 
 1198:         - kernel function call and data exchange in both directions
 1199: 
 1200:     -   \#define DRIVERIO\_IOCTLNAME \_IOXXX(group, ioctl\_number, data
 1201:         structure)
 1202: 
 1203: ## Using ioctls
 1204: 
 1205: ### Defining the ioctls
 1206: 
 1207: -   src/sys/dev/pci/faaio.h
 1208: 
 1209: <!-- -->
 1210: 
 1211:     #include <sys/ioccom.h>
 1212:                          
 1213:     #define FAAIO_ADD   _IOWR(0, 1, struct faaio_add)
 1214: 
 1215:     struct faaio_add {
 1216:         uint32_t a;
 1217:         uint32_t b;
 1218:         uint32_t *result;
 1219:     };
 1220: 
 1221: -   In the above example the ioctl group is not defined (0), but a
 1222:     single letter identifier could appear as first argument to \_IOWR
 1223: 
 1224: ### Implemeting the cdevsw operations - ioctl
 1225: 
 1226: -   src/sys/dev/pci/faa.c
 1227: 
 1228: <!-- -->
 1229: 
 1230:     int
 1231:     faaioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
 1232:     {
 1233:             struct faa_softc *sc = device_lookup_private(&faa_cd, minor(dev));
 1234:             int err;
 1235: 
 1236:             switch (cmd) {
 1237:             case FAAIO_ADD:
 1238:                     err = faaioctl_add(sc, (struct faaio_add *) data);
 1239:                     break;
 1240:             default:
 1241:                     err = EINVAL;
 1242:                     break;
 1243:             }
 1244:             return(err);
 1245:     }
 1246:     static int
 1247:     faaioctl_add(struct faa_softc *sc, struct faaio_add *data)
 1248:     {
 1249:             uint32_t result; int err;
 1250: 
 1251:             aprint_normal_dev(sc->sc_dev, "got ioctl with a %d, b %d\n",
 1252:                 data->a, data->b);
 1253: 
 1254:             result = faa_add(sc, data->a, data->b);
 1255:             err = copyout(&result, data->result, sizeof(uint32_t));
 1256:             return err;
 1257:     }
 1258: 
 1259: ### Using copyout to pass data to userspace
 1260: 
 1261: -   The copy(9) functions are used to copy kernel space data from/to
 1262:     user space
 1263: 
 1264: -   copyout(kernel\_address, user space\_address, size);
 1265: 
 1266: -   Actually on Cobalt we could just do data-\>result = faa\_add();
 1267:     instead of calling the copyout function, but that is a bad idea
 1268: 
 1269: -   Some architectures (such as sparc64) have totally separate kernel
 1270:     and user address spaces $ \implies $ user space addresses are
 1271:     meaningless in the kernel
 1272: 
 1273: ### Defining device major number
 1274: 
 1275: -   Device major numbers for hardware drivers are usually defined in a
 1276:     per-port manner[^7]
 1277: 
 1278: -   src/sys/arch/\$PORTNAME/conf/majors.\$PORTNAME
 1279: 
 1280: -   src/sys/arch/cobalt/conf/majors.cobalt
 1281: 
 1282: -   The following defines a new character device file called /dev/faa\*
 1283:     with major number 101, but only if the faa driver is included in the
 1284:     kernel (last argument)
 1285: 
 1286: -   device-major faa char 101 faa
 1287: 
 1288: ### Creating the device node
 1289: 
 1290: -   The mknod utility can be used to create the device file manually
 1291: 
 1292: -   The driver name can be specified instead of the major number - it
 1293:     will be automatically resolved into the correct major number
 1294: 
 1295: -   mknod name [b | c] [major | driver] minor
 1296: 
 1297: -   mknod /dev/faa0 c faa 0
 1298: 
 1299: -   Created successfully
 1300: 
 1301: -   crw-r–r– 1 root wheel 101, 0 Oct 8 2012 /dev/faa0
 1302: 
 1303: ### An example user space program
 1304: 
 1305: -   The example program will open the device file and call ioctl(2) on
 1306:     it
 1307: 
 1308: -   As simple as possible, just to show how communication is done
 1309: 
 1310: -   Using ioctls from the user space
 1311: 
 1312:     -   Open the device file with O\_RDWR
 1313: 
 1314:     -   Call ioctl(2) with the operation number and structure as
 1315:         parameters
 1316: 
 1317: ### An example user space program - source
 1318: 
 1319:     void add(int, uint32_t, uint32_t);
 1320: 
 1321:     static const char* faa_device = "/dev/faa0";
 1322: 
 1323:     int
 1324:     main(int argc, char *argv[])
 1325:     {
 1326:             int devfd;
 1327: 
 1328:             if (argc != 3) {
 1329:                     printf("usage: %s a b\n", argv[0]);
 1330:                     return 1;
 1331:             }
 1332:             if ( (devfd = open(faa_device, O_RDWR)) == -1) {
 1333:                     perror("can't open device file");
 1334:                     return 1;
 1335:             }
 1336: 
 1337:             add(devfd, atoi(argv[1]), atoi(argv[2]));
 1338: 
 1339:             close(devfd);
 1340:             return 0;
 1341:     }
 1342: 
 1343: ### An example user space program - source
 1344: 
 1345:     void
 1346:     add(int devfd, uint32_t a, uint32_t b)
 1347:     {
 1348:             struct faaio_add faaio;
 1349:             uint32_t result = 0;
 1350: 
 1351:             faaio.result = &result;
 1352:             faaio.a = a;
 1353:             faaio.b = b;
 1354: 
 1355:             if (ioctl(devfd, FAAIO_ADD, &faaio) == -1) {
 1356:                     perror("ioctl failed");
 1357:             }
 1358:             printf("%d\n", result);
 1359:     }
 1360: 
 1361: ### An example user space program - running it
 1362: 
 1363:     # make
 1364:     cc -o aaa_add aaa_add.c
 1365:     # ./aaa_add 3 7
 1366:     faa0: got ioctl with a 3, b 7
 1367:     10
 1368: 
 1369: -   The program is successfully accessing the faa driver through the
 1370:     ioctl
 1371: 
 1372: -   The faa0:... line is a kernel message, normally only seen on the
 1373:     console terminal
 1374: 
 1375: ## A few tips
 1376: 
 1377: ### Avoiding common pitfalls
 1378: 
 1379: -   Always free resources allocated in the match or probe functions
 1380: 
 1381: -   Always use bus\_space methods, don’t access the hardware using a
 1382:     pointer dereference
 1383: 
 1384: -   If possible test on more than one hardware architecture, some bugs
 1385:     may surface
 1386: 
 1387: -   Don’t reinvent the wheel, try to use existing kernel frameworks as
 1388:     much as possible
 1389: 
 1390: -   Use copy(9) (or uiomove(9) or store(9)/fetch(9)) to move data
 1391:     between the kernel and user space
 1392: 
 1393: ### Basic driver debugging
 1394: 
 1395: -   Use aprint\_debug to print debug-level messages on console and log
 1396:     them (enabled by passing AB\_DEBUG from the boot loader)
 1397: 
 1398: -   Use the built-in DDB debugger
 1399: 
 1400:     -   Enabled by the kernel option DDB
 1401: 
 1402:     -   A kernel panic will start DDB if the DDB\_ONPANIC=1 kernel
 1403:         option is specified or the ddb.onpanic sysctl is set to 1.
 1404: 
 1405:     -   Run \# sysctl -w kern.panic\_now=1 to trigger a panic manually
 1406:         (DIAGNOSTIC option)
 1407: 
 1408: -   Remote debugging is possible on some ports
 1409: 
 1410:     -   With KGDB through the serial port
 1411: 
 1412:     -   With IPKDB through the network
 1413: 
 1414: ## Summary
 1415: 
 1416: ### Further reading
 1417: 
 1418: -   Documentation, articles:
 1419: 
 1420:     -   [A Machine-Independent DMA Framework for NetBSD, Jason R.
 1421:         Thorpe](http://www.netbsd.org/docs/kernel/bus_dma.pdf)
 1422: 
 1423:     -   [Writing Drivers for NetBSD, Jochen
 1424:         Kunz](ftp://ftp.netbsd.org/pub/NetBSD/misc/ddwg/NetBSD-driver_writing-1.0.1e.pdf)
 1425: 
 1426:     -   [NetBSD Documentation: Writing a pseudo
 1427:         device](http://www.netbsd.org/docs/kernel/pseudo/)
 1428: 
 1429:     -   [autoconf(9)](http://netbsd.gw.com/cgi-bin/man-cgi?autoconf+9+NetBSD-current),
 1430:         [bus\_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space+9+NetBSD-current)
 1431:         [bus\_dma(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_dma+9+NetBSD-current)
 1432:         [driver(9)](http://netbsd.gw.com/cgi-bin/man-cgi?driver+9+NetBSD-current),
 1433:         [pci(9)](http://netbsd.gw.com/cgi-bin/man-cgi?pci+9+NetBSD-current)
 1434:         man pages, etc.
 1435: 
 1436: -   Example source code of drivers:
 1437: 
 1438:     -   tdvfb
 1439: 
 1440:         , voodoofb are fairly good frame buffer driver examples with
 1441:         documentation publicly available.
 1442: 
 1443:     -   etsec
 1444: 
 1445:         is a nice example of a more complicated network interface driver
 1446: 
 1447: ### Get the source code
 1448: 
 1449: -   Download the source code and materials for this tutorial
 1450: 
 1451: -   <https://github.com/rkujawa/busspace-tutorial>
 1452: 
 1453: -   <https://github.com/rkujawa/gxemul-tutorial>
 1454: 
 1455: ### Questions?
 1456: 
 1457: -   Do you have any questions?
 1458: 
 1459: ### The End…
 1460: 
 1461: ![image](NetBSD.png)
 1462: 
 1463: Thank you!
 1464: 
 1465: [^1]: At least they should, some functions are missing on less popular
 1466:     ports
 1467: 
 1468: [^2]: Only three of these registers are of any importance for us at this
 1469:     moment
 1470: 
 1471: [^3]: Required if you are NetBSD developer, optional otherwise.
 1472: 
 1473: [^4]: Might not exist if the driver is only a simple passthrough from a
 1474:     specific bus to another MI driver.
 1475: 
 1476: [^5]: Omitted if not needed.
 1477: 
 1478: [^6]: although you’ll often have to use bus\_size\_t
 1479: 
 1480: [^7]: It’s also possible to define a major in a machine-independent way
 1481:     in src/sys/conf/majors

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb