Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel driver: what model for NVRAM access?

I just wrote a RTC driver for an NXP RTC chip on my board, it works great. This chip also has some battery backed RAM that I'd like to make available to a user space application. The RTC framework doesn't support this. It's only 512 bytes but I'm tossed between doing a seekable CHAR driver or a full blown BLOCK driver. I've never done a block driver before but it appears to require a bit more information than a simple CHAR.

I could also interface with IOCTLS but that doesn't feel as clean as it could be. What feels like the best way to make these bytes available to userland?

[EDIT] I forgot to mention that that the RTC chip is hanging off an I2C port, it's not mapped into memory, thus not making it a good candidate for mmaping. [/EDIT]

like image 629
Ken Farr Avatar asked Jun 29 '10 00:06

Ken Farr


People also ask

What is Nvram Linux?

The /dev/nvram character special file provides access to the machine device driver for accessing or modifying machine-specific nonvolatile RAM.

What is driver model in Linux?

The Linux Kernel Driver Model is a unification of all the disparate driver models that were previously used in the kernel. It is intended to augment the bus-specific drivers for bridges and devices by consolidating a set of data and operations into globally accessible data structures.

What are the two types of drivers in Linux?

The Linux kernel supports two main types of USB drivers: drivers on a host system and drivers on a device.

How does Linux know which driver to load?

How does the Linux kernel know which drivers to load at boot? The kernel generates events for devices on e.g. the PCI bus when they are plugged (either hot or cold; events are queued until userspace runs AFAIR).


2 Answers

Block drivers are only for devices that look like disk drives. Are you going to put a filesystem on your 512 bytes? No? Make it a character device.

You could just do it like other drivers have. Check out drivers/char/nvram.c. That creates a char device you can open(), read(), write(), lseek(), and close().

like image 150
JayM Avatar answered Nov 13 '22 12:11

JayM


I think a character device driver implementing mmap should be adequate. Linux Device Drives covers that in chapter 15.

Edit:

Well, i2c is a serial bus, so mmap is not an option. I will refer you to Essential Linux Device Drivers book. I believe it has a sample i2c EEPROM char device driver in Chapter 8. Hope this helps.

like image 43
Nikolai Fetissov Avatar answered Nov 13 '22 10:11

Nikolai Fetissov