Character and Block Drivers

There are three kinds of drivers that are recognized by the OS and are visible to applications. Two are POSIX standard device driver types, one is non-standard. There are also internal OS components that may also be considered to be drivers or, more correctly, lower-half drivers. But we will not consider drivers not visible to applications here.

The standard driver types include:

  • Character Drivers. First there are the character drivers These are drivers that support user accessibility via read(), write() etc. The others do not naturally. Character drivers implement a stream of incoming or outgoing bytes.
  • Block Drivers. These are used to support files systems that supported block-oriented I/O, not a character stream. The user cannot directly access block drivers.

The user can, however, access block drivers indirectly through a character driver proxy. Both character and block drivers are represented by device nodes, usually in /dev. But if you try to open the block driver, something very strange happens: A temporary, nameless proxy character driver is automatically instantiated that maps a character driver's byte stream into blocks and mediates the driver access to the block driver. This is the logic in drivers/bch. BCH stands for block to character. So from the application point of view, the both seem to be character drivers and applications can interact with both in the same way.

This capability is exploited, for example, by the NuttX file system formatting applications like mkfatfs to format a FAT system on a block driver.

There is also the complement, the loop device that converts a character driver into a block driver. Loop devices are commonly used to format a file system image in RAM.

MTD Drivers

And the non-standard driver is:

  • The Memory Technology Driver (MTD). This naming was borrowed from infradead.org, but does not derive from any of their MTD logic. The MTD driver manages memory-based devices like FLASH or EEPROM. And MTD FLASH memory driver is very similar to a block driver but FLASH has some different properties, most notably that you have to erase FLASH before you write to it.

MTD has the same conveniences as block drivers: Then can appear as device nodes under /dev and can be proxied to behave like character drivers if the opened as character drivers. Plus they have some additional twists: MTD drivers can be stacked one on top of another to extend the capabilities of the lower level MTD driver. For example, drivers/mtd/sector512.c is an MTD driver that when layered on top of another MTD driver, it changes the apparent page size of the FLASH to 512 bytes.

drivers/mtd/mtd_partitions.c can be used to break up a large FLASH into separate, independent partitions, each of which looks like another MTD driver.

drivers/mtd/ftl.c is also interesting. FTL stands for FLASH Translation Layer. The FTL driver is an MTD driver that when layered on top of another MTD driver, converts the MTD driver to a block driver. The permutations are endless.

  • No labels