![]() |
![]() |
| ||||
Chapter 5Driver AutoconfigurationAutoconfiguration is the process of getting the driver's code and static data loaded into memory and registered with the system. It also involves configuring (attaching) individual device instances that are controlled by the driver. This chapter provides information on the following subjects: Driver Loading and UnloadingThe system loads driver binary modules from the drv subdirectory of the kernel module directory for autoconfiguration (see Copying the Driver to a Module Directory). Once a module is read into memory with all symbols resolved, the system will call the _init(9E) entry point for that module. Upon successful completion of _init(), the driver is properly registered with the system, or "loaded." At this point, the driver is not actively managing any device; that will happen as part of device configuration. The system unloads driver binary modules either to conserve system memory or at the explicit request of a user. Before deleting the driver code and data from memory, the _fini(9E) entry point of the driver is invoked. The driver is unloaded if and only if _fini() returns success. Figure 5-1 illustrates a structural overview of a device driver. The shaded area of this figure highlights the driver data structures and entry points. The upper half of the shaded area contains data structures and entry points supporting driver loading and unloading; the lower half, driver configuration. Figure 5-1 Module Loading and Autoconfiguration Entry Points ![]() Data Structures Required for DriversDrivers are required to statically initialize a number of data structures to support autoconfiguration. These structures include modlinkage(9S), modldrv(9S), dev_ops(9S), and cb_ops(9S). The data structures illustrated in Figure 5-1 must be provided and initialized correctly for the driver to load and for its routines to be called. If an operation is not supported by the driver, the address of the routine nodev(9F) can be used to fill it in. If the driver supports the entry point, but does not need to do anything except return success, the address of the routine nulldev(9F) can be used. Note - These structures should be initialized at compile-time. They should not be accessed or changed by the driver at any other time. modlinkage Structure
The first field is the version number of the module loading subsystem and should be MODREV_1. The second field points to driver's modldrv structure defined next. The last element of the structure should always be NULL. modldrv Structure
This structure describes the module in more detail. The first field provides information on how to install and uninstall the module. It should be set to &mod_driverops for driver modules. The second field is a string to be displayed by modinfo(1M). It should contain sufficient information for identifying the version of source code that generated the driver binary. The last field points to the driver's dev_ops structure defined in the following section. dev_ops Structure
The dev_ops(9S) structure enables the kernel to find the autoconfiguration entry points of the device driver. The devo_rev field identifies the revision number of the structure itself, and must be set to DEVO_REV. The devo_refcnt field must be initialized to zero. The function address fields should be filled in with the address of the appropriate driver entry point. The exceptions are:
The devo_cb_ops member should include the address of the cb_ops(9S) structure. The devo_bus_ops field must be set to NULL. cb_ops Structure
The cb_ops(9S) structure contains the entry points for the character and block operations of the device driver. Any entry points the driver does not support should be initialized to nodev(9F). For example, character device drivers should set all the block-only fields, such as cb_stategy, to nodev(9F). Note that the mmap(9E) entry point is maintained for compatibility with previous releases, and drivers should use the devmap(9E) entry point for device memory mapping. If devmap(9E) is supported, set mmap(9E) to nodev(9F). The streamtab field indicates whether this is a STREAMS-based driver. The device drivers discussed in this book (with the exception of Chapter 16, Drivers for Network Devices) are not STREAMS based. For a non-STREAMS-based driver, the streamtab field must be set to NULL. The cb_flag member contains the following flags:
cb_rev is the cb_ops(9S) structure revision number. This field must be set to CB_REV. | ||||
| ||||