Linux Driver Development Prelude

Source: Internet
Author: User

1. Driver Development OverviewDrive Classification:1. General classification: Character device, block device, network device character device: is a kind of device that is accessed by byte, character driver is responsible for driving character device, so the driver usually realizes system call such as Open,close,read, such as: Serial port, LED, key block device: In most Unix system,                A block device is defined as a device in which a block (usually 512 bytes) is the smallest transmission unit and the block device cannot process data in bytes. Linux allows a block device to transmit any number of bytes. Therefore, the difference between a block and a character device is only driven by a different interface to the kernel. Common block devices include hard drives, FLASH,SD cards, and so on. Network device: A network device can be a hardware device, such as a network card, or it can be a purely software device, such as a loopback interface lo, a network interface responsible for sending and receiving data packets.
2. Bus classification USB device, PCI device, platform bus device
2. Hardware Access Technology Hardware Access EssenceThe driver controls the device, mainly by accessing the registers within the device to achieve control purposes, so we discuss how to access the hardware, it becomes how to access these registers.
Address MappingIn a Linux system, whether it is a kernel program or an application, intelligent use of virtual address, and the chip manual gives the Register address or RAM address is a physical address, cannot be used directly, so we read and write register the first step is to tell the physical address mapped to a virtual address.
1. Dynamic mappingIn the driver, use theIoremap functionMapping physical addresses to virtual address function prototypes: void *ioremap (physaddr, size); parameter description: physaddr: Physical Address to be mapped size: Mapped area length return value: mapped virtual address
2. Static mappingThe so-called static mapping, refers to the Linux system according to the user's pre-specified mapping relationship, after the kernel starts, the physical address is automatically mapped to a virtual address.Question 1: How do I specify a mapping relationship beforehand? In static mapping, the user is using the MAP_DESC structure to indicate the mapping between the physical address and the virtual address.
struct MAP_DESC {    unsigned long virtual;/* Mapped virtual address */    unsigned long pfn;     /* The page frame number where the physical address is located */    unsigned long length;  /* Map length */    unsigned int type;     /* The device type mapped */};
PFN: Use _PHYS_TO_PFN (Physical address) to calculate the physical page frame number where the physical address resides
Question 2: Where does the automatic mapping be done when the kernel is started? Take S3c6410cpu as an example: there is an array of structures in the ARCH/ARM/MACH-S3C64XX directory
static struct Map_desc s3c_iodesc[] __initdata = {...};
All of this is stored in this mapping relationship, and there is an initialization function below the directory:
void __init s3c64xx_init_io (struct map_desc *mach_desc, int size) {    unsigned long idcode;    /* Initialise The IO descriptors we need for initialisation *    /Iotable_init (S3c_iodesc, Array_size (S3c_iodesc));    ...}
Internal call Iotable_init Complete mapping
Register read/writeAfter the address mapping is complete, the registers can be read and written, and the Linux kernel provides a series of functions to read and write registers.
unsigned ioread8 (void *addr) unsigned ioread16 (void *addr) unsigned ioread32 (void *addr) unsigned readb (address) unsigned READW (address) unsigned READW (address) unsigned READL (address)
void Iowrite8 (U8 value, void *addr) void Iowrite16 (U16 value, void *addr) void Iowrite32 (u32 value, void *addr) void Writeb (U nsigned value, address) void Writew (unsigned value, address) void Writel (unsigned value, address)

Linux Driver Development Prelude

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.