Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu đang bị lỗi
File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.
Embedded FreeBSD Cookbook phần 4 doc
Nội dung xem thử
Mô tả chi tiết
61 Chapter Four
Device Driver
The sample DIO device driver source contains a shell implementation of both
ISA and PCI device drivers. Because the PCI-DIO24 is a PCI card, the first
modification I made to the DIO source code was to remove all references to
the ISA callback functions. Once this was complete, I performed a complete
build to make sure the remaining code was still correct.
DIO Driver Functions
The dio_pci_probe Function
The first task of the device driver is to detect if its hardware is present. This
is called probing the device. Device probing is performed by the probe device
method. Dio.c contains a function for probing a PCI device implemented
in the function dio_pci_probe.
dio_pci_probe calls the kernel function pci_get_devid to return the
Vendor ID and Device ID for this device. pci_get_dev returns a 32-bit longword of which the upper 16 bits contain the PCI Device ID and the lower 16
bits contains the PCI vendor ID for this card. The following code shows the
modifications to the pci_ids structure for the PCI-DIO24.
static struct _pcsid
{
u_int32_t type;
const char *desc;
} pci_ids[] = {
{ 0x00281307, “Measurement Computing PCI-DIO24 Digital
I/O Card” },
{ 0x00000000, NULL }
};
The DIO device driver can now be built and loaded, and it successfully
recognizes the PCI-DIO24 card. The complete dio_pci_probe function is
listed below.
static int
dio_pci_probe (device_t device)
{
u_int32_t type = pci_get_devid(device);
struct _pcsid *ep =pci_ids;
while (ep->type && ep->type != type)
62 Embedded FreeBSD
Cookbook
++ep;
if (ep->desc) {
device_set_desc(device, ep->desc);
return 0; /* If there might be a better driver, return -2 */
} else {
return ENXIO;
}
}
dio_pci_probe scans the pci_ids structure in an attempt to match the
vendor and device IDs. When a match is found, the verbal description of the
device is updated using the kernel function device_set_desc.
TIP
FreeBSD has a utility that dumps information about PCI devices, called pciconf.
Utilities such as pciconf are used for debugging and verifying your hardware is working correctly before developing a device driver.
The pciconf utility is used to detect that the hardware is successfully loaded and
recognized by FreeBSD. pciconf –l will give you a list of PCI devices present in your
system. Once you have installed a hardware peripheral, it should always be listed in the
output of the pciconf command.
The dio_pci_attach Function
Once a device driver successfully probes the hardware, the kernel calls the
driver attach method. The attach method is responsible for allocating hardware resources and for the PCI-DIO24 making the character device. The
PCI_DIO24 board has three hardware resources, an interrupt and two IO
ports. dio.c contains the attach method dio_pci_attach.
static int
dio_pci_attach(device_t device)
{
interror;
struct dio_softc *scp = DEVICE2SOFTC(device);
error = dio_attach(device, scp);
if (error) {
dio_pci_detach(device);
}