Writing Device Drivers

Mapping Setup Example

Example 6–1 is a simple example demonstrating the setup and use of the DDI data access interfaces. This driver is for a fictional little endian device that accepts one character at a time and generates an interrupt when ready for another. This device implements two register sets: the first is an 8-bit CSR register, and the second is an 8-bit data register.


Example 6–1 Mapping Setup

    #define CSR_REG 0
    #define DATA_REG 1

    /*
     * Initialize the device access attributes for the register
     * mapping
     */
    dev_acc_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
    dev_acc_attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
    dev_acc_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

    /*
     * Map in the csr register (register 0)
     */
    if (ddi_regs_map_setup(dip, CSR_REG, (caddr_t *)&(pio_p->csr), 0,
      sizeof (Pio_csr), &dev_acc_attr, &pio_p->csr_handle) != DDI_SUCCESS) {
        mutex_destroy(&pio_p->mutex);
        ddi_soft_state_free(pio_softstate, instance);
        return (DDI_FAILURE);
    }

    /*
     * Map in the data register (register 1)
     */
    if (ddi_regs_map_setup(dip, DATA_REG, (caddr_t *)&(pio_p->data), 0,
      sizeof (uchar_t), &dev_acc_attr, &pio_p->data_handle) \
                    != DDI_SUCCESS) {
        mutex_destroy(&pio_p->mutex);
        ddi_regs_map_free(&pio_p->csr_handle);
        ddi_soft_state_free(pio_softstate, instance);
        return (DDI_FAILURE);
    }