Diff for /wikisrc/tutorials/bus_space_tutorial.mdwn between versions 1.1 and 1.2

version 1.1, 2013/06/23 13:59:13 version 1.2, 2013/06/23 14:10:19
Line 1 Line 1
 [allowframebreaks]  [allowframebreaks]
   
 ### Table of Contents  ### Table of Contents
   [[!toc ]]
   
 Introduction  Introduction
 ============  ============
Line 162  Kernel autoconfiguration framework Line 163  Kernel autoconfiguration framework
     -   Additional information about the driver state or device      -   Additional information about the driver state or device
         configuration          configuration
   
 [fragile]  
   
 ### Autoconfiguration as seen in the dmesg  ### Autoconfiguration as seen in the dmesg
   
     NetBSD 6.99.12 (GENERIC) #7: Fri Oct  5 18:43:21 CEST 2012      NetBSD 6.99.12 (GENERIC) #7: Fri Oct  5 18:43:21 CEST 2012
Line 393  Adding a new driver to the NetBSD kernel Line 392  Adding a new driver to the NetBSD kernel
 Matching the PCI device  Matching the PCI device
 -----------------------  -----------------------
   
 [fragile]  
   
 ### Modifying the PCI device database  ### Modifying the PCI device database
   
     unmatched vendor 0xfabc product 0x0001 (Co-processor       unmatched vendor 0xfabc product 0x0001 (Co-processor 
Line 412  Matching the PCI device Line 409  Matching the PCI device
 -   To regenerate pcidevs\*.h run awk -f devlist2h.awk pcidevs or  -   To regenerate pcidevs\*.h run awk -f devlist2h.awk pcidevs or
     Makefile.pcidevs if you’re on NetBSD      Makefile.pcidevs if you’re on NetBSD
   
 [fragile]  
   
 ### Modifying the PCI device database - example  ### Modifying the PCI device database - example
   
     --- pcidevs 29 Sep 2012 10:26:14 -0000  1.1139      --- pcidevs 29 Sep 2012 10:26:14 -0000  1.1139
Line 437  Matching the PCI device Line 432  Matching the PCI device
      product O2MICRO 00F7       0x00f7  Integrated OHCI IEEE 1394 Host Controller       product O2MICRO 00F7       0x00f7  Integrated OHCI IEEE 1394 Host Controller
      product O2MICRO OZ6729     0x6729  OZ6729 PCI-PCMCIA Bridge       product O2MICRO OZ6729     0x6729  OZ6729 PCI-PCMCIA Bridge
   
 [fragile]  
   
 ### Modifying the PCI device database - example  ### Modifying the PCI device database - example
   
     Fake Cards Advanced Addition Accelerator (Co-processor       Fake Cards Advanced Addition Accelerator (Co-processor 
Line 566  Matching the PCI device Line 559  Matching the PCI device
 -   We won’t cover detach and activate now, as they are not needed for a  -   We won’t cover detach and activate now, as they are not needed for a
     simple driver      simple driver
   
 [fragile]  
   
 ### Adding the new PCI driver - main driver example  ### Adding the new PCI driver - main driver example
   
 -   src/sys/dev/pci/faa.c  -   src/sys/dev/pci/faa.c
Line 575  Matching the PCI device Line 566  Matching the PCI device
 <!-- -->  <!-- -->
   
     #include <sys/cdefs.h>      #include <sys/cdefs.h>
     __KERNEL_RCSID(0, "$NetBSD$");      __KERNEL_RCSID(0, "$NetBSD: bus_space_tutorial.mdwn,v 1.1 2013/06/23 13:59:13 mspo Exp $");
     #include <sys/param.h>      #include <sys/param.h>
     #include <sys/device.h>      #include <sys/device.h>
     #include <dev/pci/pcivar.h>      #include <dev/pci/pcivar.h>
Line 600  Matching the PCI device Line 591  Matching the PCI device
     {       { 
     }      }
   
 [fragile]  
   
 ### Adding the new PCI driver - auxiliary includes  ### Adding the new PCI driver - auxiliary includes
   
 -   src/sys/dev/pci/faareg.h  -   src/sys/dev/pci/faareg.h
Line 630  Matching the PCI device Line 619  Matching the PCI device
     };      };
     #endif /* FAAVAR_H */      #endif /* FAAVAR_H */
   
 [fragile]  
   
 ### Adding the new PCI driver - registering the driver (courtesy)  ### Adding the new PCI driver - registering the driver (courtesy)
   
 -   src/sys/dev/DEVNAMES  -   src/sys/dev/DEVNAMES
Line 649  Matching the PCI device Line 636  Matching the PCI device
      fb         news68k       fb         news68k
      fb         newsmips       fb         newsmips
   
 [fragile]  
   
 ### Adding the new PCI driver - registering the driver  ### Adding the new PCI driver - registering the driver
   
 -   See config(5)  -   See config(5)
Line 672  Matching the PCI device Line 657  Matching the PCI device
     +file   dev/pci/faa.c       faa       +file   dev/pci/faa.c       faa 
     +      +
   
 [fragile]  
   
 ### Adding the new PCI driver to the kernel configuration  ### Adding the new PCI driver to the kernel configuration
   
 -   src/sys/arch/cobalt/conf/GENERIC  -   src/sys/arch/cobalt/conf/GENERIC
Line 711  Matching the PCI device Line 694  Matching the PCI device
   
 -   Since it is not implemented, the kernel will not attach the driver  -   Since it is not implemented, the kernel will not attach the driver
   
 [fragile]  
   
 ### Matching the PCI device  ### Matching the PCI device
   
 -   Modify the faa\_match function to match the specified PCI device  -   Modify the faa\_match function to match the specified PCI device
Line 736  Matching the PCI device Line 717  Matching the PCI device
 Attaching to the PCI device  Attaching to the PCI device
 ---------------------------  ---------------------------
   
 [fragile]  
   
 ### Attaching to the PCI device  ### Attaching to the PCI device
   
     faa0 at pci0 dev 12 function 0      faa0 at pci0 dev 12 function 0
Line 784  Mapping the hardware resources Line 763  Mapping the hardware resources
 -   It’s a machine-dependent process but it’s also conveniently hidden  -   It’s a machine-dependent process but it’s also conveniently hidden
     from the programmer by the bus\_space framework      from the programmer by the bus\_space framework
   
 [fragile]  
   
 ### Mapping the hardware resources  ### Mapping the hardware resources
   
 -   The generic bus\_space(9) way to map space  -   The generic bus\_space(9) way to map space
Line 819  Mapping the hardware resources Line 796  Mapping the hardware resources
   
 -   Separate space and address  -   Separate space and address
   
 [fragile]  
   
 ### Mapping the hardware resources  ### Mapping the hardware resources
   
 -   The PCI-specific way to map space  -   The PCI-specific way to map space
Line 870  Mapping the hardware resources Line 845  Mapping the hardware resources
   
 -   The last four parameters are filled after successful mapping  -   The last four parameters are filled after successful mapping
   
 [fragile]  
   
 ### Mapping the registers using BAR - adding auxiliary includes  ### Mapping the registers using BAR - adding auxiliary includes
   
 -   src/sys/dev/pci/faareg.h  -   src/sys/dev/pci/faareg.h
Line 893  Mapping the hardware resources Line 866  Mapping the hardware resources
   
     };      };
   
 [fragile]  
   
 ### Mapping the registers using BAR - main driver code  ### Mapping the registers using BAR - main driver code
   
 -   src/sys/dev/pci/faa.c  -   src/sys/dev/pci/faa.c
Line 965  Accessing the hardware registers Line 936  Accessing the hardware registers
     [bus\_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current)      [bus\_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current)
     man page      man page
   
 [fragile]  
   
 ### Accessing the hardware registers - example  ### Accessing the hardware registers - example
   
 -   Create a function that will write a value into the DATA register of  -   Create a function that will write a value into the DATA register of
Line 987  Accessing the hardware registers Line 956  Accessing the hardware registers
   
 -   static bool faa\_check(struct faa\_softc \*sc);  -   static bool faa\_check(struct faa\_softc \*sc);
   
 [fragile]  
   
 ### Accessing the hardware registers - example  ### Accessing the hardware registers - example
   
 -   src/sys/dev/pci/faa.c  -   src/sys/dev/pci/faa.c
Line 1017  Accessing the hardware registers Line 984  Accessing the hardware registers
             return false;              return false;
     }      }
   
 [fragile]  
   
 ### Accessing the hardware registers - running the example  ### Accessing the hardware registers - running the example
   
 -   Update the kernel binary and run it again  -   Update the kernel binary and run it again
Line 1053  Accessing the hardware registers Line 1018  Accessing the hardware registers
     atabus/wd for IDE and SATA hard disk controllers, wsdisplay/wscons      atabus/wd for IDE and SATA hard disk controllers, wsdisplay/wscons
     for frame buffers, etc.)      for frame buffers, etc.)
   
 [fragile]  
   
 ### Implementing addition using the hardware  ### Implementing addition using the hardware
   
 -   Define all registers  -   Define all registers
Line 1071  Accessing the hardware registers Line 1034  Accessing the hardware registers
     #define FAA_DATA                0x8      #define FAA_DATA                0x8
     #define FAA_RESULT              0xC      #define FAA_RESULT              0xC
   
 [fragile]  
   
 ### Implementing addition using the hardware  ### Implementing addition using the hardware
   
 -   Add a new function to the main driver code  -   Add a new function to the main driver code
Line 1099  Accessing the hardware registers Line 1060  Accessing the hardware registers
             return bus_space_read_4(sc->sc_regt, sc->sc_regh, FAA_RESULT);              return bus_space_read_4(sc->sc_regt, sc->sc_regh, FAA_RESULT);
     }      }
   
 [fragile]  
   
 ### Implementing addition using the hardware - running the example  ### Implementing addition using the hardware - running the example
   
 -   Update the kernel binary and run it again  -   Update the kernel binary and run it again
Line 1188  Device files Line 1147  Device files
   
 -   We’ll implement an ioctl(2)-based communication mechanism  -   We’ll implement an ioctl(2)-based communication mechanism
   
 [fragile]  
   
 ### Adding cdevsw  ### Adding cdevsw
   
 -   cdevsw  -   cdevsw
Line 1231  Device files Line 1188  Device files
     originally it was used to define the device type (still used for      originally it was used to define the device type (still used for
     disks, tape drives and ttys, for other devices pass D\_OTHER)      disks, tape drives and ttys, for other devices pass D\_OTHER)
   
 [fragile]  
   
 ### Implemeting the cdevsw operations - open / close  ### Implemeting the cdevsw operations - open / close
   
 -   src/sys/dev/pci/faa.c  -   src/sys/dev/pci/faa.c
Line 1301  Device files Line 1256  Device files
 Using ioctls  Using ioctls
 ------------  ------------
   
 [fragile]  
   
 ### Defining the ioctls  ### Defining the ioctls
   
 -   src/sys/dev/pci/faaio.h  -   src/sys/dev/pci/faaio.h
Line 1322  Using ioctls Line 1275  Using ioctls
 -   In the above example the ioctl group is not defined (0), but a  -   In the above example the ioctl group is not defined (0), but a
     single letter identifier could appear as first argument to \_IOWR      single letter identifier could appear as first argument to \_IOWR
   
 [fragile]  
   
 ### Implemeting the cdevsw operations - ioctl  ### Implemeting the cdevsw operations - ioctl
   
 -   src/sys/dev/pci/faa.c  -   src/sys/dev/pci/faa.c
Line 1420  An example user space program Line 1371  An example user space program
     -   Call ioctl(2) with the operation number and structure as      -   Call ioctl(2) with the operation number and structure as
         parameters          parameters
   
 [fragile]  
   
 ### An example user space program - source  ### An example user space program - source
   
     void add(int, uint32_t, uint32_t);      void add(int, uint32_t, uint32_t);
Line 1448  An example user space program Line 1397  An example user space program
             return 0;              return 0;
     }      }
   
 [fragile]  
   
 ### An example user space program - source  ### An example user space program - source
   
     void      void
Line 1468  An example user space program Line 1415  An example user space program
             printf("%d\n", result);              printf("%d\n", result);
     }      }
   
 [fragile]  
   
 ### An example user space program - running it  ### An example user space program - running it
   
     # make      # make

Removed from v.1.1  
changed lines
  Added in v.1.2


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