Thursday, November 10, 2011

Module Interaction Through sysfs


For the past couple of weeks I have been writing my own kernel module for the Quad DAC mcp4728. Since I'm dealing with a DAC a user-space application must be able to change the output voltages when required. As we should all now know, user-space and kernel-space process cannot interact directly, this assumption lead me on to sysfs.

Sysfs gives a module a mechanism to interact with user-space, sysfs is a virtual file-system that exports information about devices and drivers from the kernel device model to user-space. Some of the entries in sysfs come as standard when you register a device, some you can add yourself. By creating files in the sysfs you are able link functions and ultimately variables within module to user-space.

In my case, the mcp4728 has four DACs ports A~D, with that in mind, I created four entries within sysfs named porta_voltage through to portd_voltage. I can then interact with these files from user-space using simple read/writes..simple. 

So how did I do it you may ask? Read chapter 14 of this book titled Linux Device Drivers, Third Edition and also look at the example in the kernel source located at samples/kobject/kobject-example.c, you will see the importance of the foor_show and foo_store functions. Put simply when you read the sysfs file foo_show is called in the module, and when you write to the file, foo_store is called. Simple.

No comments:

Post a Comment