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

version 1.2, 2013/06/23 14:10:19 version 1.3, 2013/06/23 14:16:27
Line 1 Line 1
 [allowframebreaks]  
   
 ### Table of Contents  
 [[!toc ]]  [[!toc ]]
   
 Introduction  ## Introduction
 ============  
   
 Why was this tutorial created?  
 ------------------------------  
   
 ### Why was this tutorial created?  ### Why was this tutorial created?
   
Line 21  Why was this tutorial created? Line 14  Why was this tutorial created?
   
 -   Device drivers are fun  -   Device drivers are fun
   
 What won’t be covered here?  
 ---------------------------  
   
 ### What won’t be covered here?  ### What won’t be covered here?
   
 We don’t have much time, so several ~~advanced~~ topics were omitted:  We don’t have much time, so several ~~advanced~~ topics were omitted:
Line 45  We don’t have much time, so several ~~ Line 35  We don’t have much time, so several ~~
 However, once you finish this tutorial, you should be able to pursue  However, once you finish this tutorial, you should be able to pursue
 this knowledge yourself.  this knowledge yourself.
   
 What is a driver anyway?  
 ------------------------  
   
 ### What is a driver anyway?  ### What is a driver anyway?
   
 -   The interface between user space and hardware, implemented as a part  -   The interface between user space and hardware, implemented as a part
Line 58  What is a driver anyway? Line 45  What is a driver anyway?
 -   Sometimes they have machine dependent assembler parts, but this is a  -   Sometimes they have machine dependent assembler parts, but this is a
     rare case      rare case
   
 What do you need to write a driver?  
 -----------------------------------  
   
 ### What do you need to write a driver?  ### What do you need to write a driver?
   
 -   C programming skills  -   C programming skills
Line 99  What do you need to write a driver? Line 83  What do you need to write a driver?
 The NetBSD driver model  The NetBSD driver model
 =======================  =======================
   
 The NetBSD kernel basics  
 ------------------------  
   
 ### The NetBSD kernel basics  ### The NetBSD kernel basics
   
 -   NetBSD has a classic monolithic UNIX-like kernel - all drivers are  -   NetBSD has a classic monolithic UNIX-like kernel - all drivers are
Line 138  The NetBSD kernel basics Line 119  The NetBSD kernel basics
   
     - kernel configuration files for a given port      - kernel configuration files for a given port
   
 Kernel autoconfiguration framework  
 ----------------------------------  
   
 ### Kernel autoconfiguration framework - autoconf(9)  ### Kernel autoconfiguration framework - autoconf(9)
   
 -   Autoconfiguration is the process of matching hardware devices with  -   Autoconfiguration is the process of matching hardware devices with
Line 238  Kernel autoconfiguration framework Line 216  Kernel autoconfiguration framework
 Example driver from scratch  Example driver from scratch
 ===========================  ===========================
   
 Development environment  
 -----------------------  
   
 ### Development environment  ### Development environment
   
 -   Out of scope of this course, but very well documented  -   Out of scope of this course, but very well documented
Line 267  Development environment Line 242  Development environment
   
     is calling nbconfig and nbmake tools, no magic involved      is calling nbconfig and nbmake tools, no magic involved
   
 Quick introduction to GXemul  
 ----------------------------  
   
 ### Quick introduction to GXemul  ### Quick introduction to GXemul
   
 -   A framework for full-system computer architecture emulation,  -   A framework for full-system computer architecture emulation,
Line 286  Quick introduction to GXemul Line 258  Quick introduction to GXemul
 -   It will be used to show (almost) a real-life example of the driver  -   It will be used to show (almost) a real-life example of the driver
     development process      development process
   
 Our hardware - a fake PCI card  
 ------------------------------  
   
 ### Our hardware - functional description  ### Our hardware - functional description
   
 -   Business applications often use arithmetic operations like addition  -   Business applications often use arithmetic operations like addition
Line 372  Our hardware - a fake PCI card Line 341  Our hardware - a fake PCI card
   
 -   Read the result from RESULT register  -   Read the result from RESULT register
   
 Adding a new driver to the NetBSD kernel  
 ----------------------------------------  
   
 ### Adding a new driver to the NetBSD kernel  ### Adding a new driver to the NetBSD kernel
   
 -   We’ll discuss the steps needed to add a new MI PCI device driver to  -   We’ll discuss the steps needed to add a new MI PCI device driver to
Line 389  Adding a new driver to the NetBSD kernel Line 355  Adding a new driver to the NetBSD kernel
   
     -   Add the new driver to DEVNAMES[^3] file      -   Add the new driver to DEVNAMES[^3] file
   
 Matching the PCI device  
 -----------------------  
   
 ### 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 566  Matching the PCI device Line 529  Matching the PCI device
 <!-- -->  <!-- -->
   
     #include <sys/cdefs.h>      #include <sys/cdefs.h>
     __KERNEL_RCSID(0, "$NetBSD: bus_space_tutorial.mdwn,v 1.1 2013/06/23 13:59:13 mspo Exp $");      __KERNEL_RCSID(0, "$NetBSD: bus_space_tutorial.mdwn,v 1.2 2013/06/23 14:10:19 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 714  Matching the PCI device Line 677  Matching the PCI device
             return 0;              return 0;
     }      }
   
 Attaching to the PCI device  
 ---------------------------  
   
 ### 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 726  Attaching to the PCI device Line 686  Attaching to the PCI device
   
 -   Let’s fill the attach function and actually program the hardware  -   Let’s fill the attach function and actually program the hardware
   
 Variable types used with bus\_space  
 -----------------------------------  
   
 ### Variable types used with bus\_space  ### Variable types used with bus\_space
   
 -   bus\_space\_tag\_t  -   bus\_space\_tag\_t
Line 752  Variable types used with bus\_space Line 709  Variable types used with bus\_space
 -   Contents of these types are MD, so avoid modifying from within the  -   Contents of these types are MD, so avoid modifying from within the
     driver[^6]      driver[^6]
   
 Mapping the hardware resources  
 ------------------------------  
   
 ### Why do we need to “map” the resources?  ### Why do we need to “map” the resources?
   
 -   “The bus space must be mapped before it can be used, and should be  -   “The bus space must be mapped before it can be used, and should be
Line 891  Mapping the hardware resources Line 845  Mapping the hardware resources
             aprint_normal_dev(sc->sc_dev, "regs at 0x%08x\n", (uint32_t) sc->sc_reg_pa);              aprint_normal_dev(sc->sc_dev, "regs at 0x%08x\n", (uint32_t) sc->sc_reg_pa);
     }      }
   
 Accessing the hardware registers  
 --------------------------------  
   
 ### Accessing the hardware registers  ### Accessing the hardware registers
   
 -   The bus\_space\_read\_ and bus\_space\_write\_ functions are basic  -   The bus\_space\_read\_ and bus\_space\_write\_ functions are basic
Line 1083  Accessing the hardware registers Line 1034  Accessing the hardware registers
     faa0: registers at 0x10110000      faa0: registers at 0x10110000
     faa0: just checking: 1 + 2 = 3      faa0: just checking: 1 + 2 = 3
   
 Interacting with userspace  ## Interacting with userspace
 ==========================  
   
 Device files  
 ------------  
   
 ### The kernel-user space interface  ### The kernel-user space interface
   
Line 1253  Device files Line 1200  Device files
     -   \#define DRIVERIO\_IOCTLNAME \_IOXXX(group, ioctl\_number, data      -   \#define DRIVERIO\_IOCTLNAME \_IOXXX(group, ioctl\_number, data
         structure)          structure)
   
 Using ioctls  ## Using ioctls
 ------------  
   
 ### Defining the ioctls  ### Defining the ioctls
   
Line 1354  Using ioctls Line 1300  Using ioctls
   
 -   crw-r–r– 1 root wheel 101, 0 Oct 8 2012 /dev/faa0  -   crw-r–r– 1 root wheel 101, 0 Oct 8 2012 /dev/faa0
   
 An example user space program  
 -----------------------------  
   
 ### An example user space program  ### An example user space program
   
 -   The example program will open the device file and call ioctl(2) on  -   The example program will open the device file and call ioctl(2) on
Line 1429  An example user space program Line 1372  An example user space program
 -   The faa0:... line is a kernel message, normally only seen on the  -   The faa0:... line is a kernel message, normally only seen on the
     console terminal      console terminal
   
 A few tips  ## A few tips
 ==========  
   
 Avoiding common pitfalls  
 ------------------------  
   
 ### Avoiding common pitfalls  ### Avoiding common pitfalls
   
Line 1451  Avoiding common pitfalls Line 1390  Avoiding common pitfalls
 -   Use copy(9) (or uiomove(9) or store(9)/fetch(9)) to move data  -   Use copy(9) (or uiomove(9) or store(9)/fetch(9)) to move data
     between the kernel and user space      between the kernel and user space
   
 Basic driver debugging  
 ----------------------  
   
 ### Basic driver debugging  ### Basic driver debugging
   
 -   Use aprint\_debug to print debug-level messages on console and log  -   Use aprint\_debug to print debug-level messages on console and log
Line 1475  Basic driver debugging Line 1411  Basic driver debugging
   
     -   With IPKDB through the network      -   With IPKDB through the network
   
 Summary  ## Summary
 =======  
   
 The end  
 -------  
   
 ### Further reading  ### Further reading
   

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


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