sysfs節點的創建
sysfs節點也可以用來導出內核測信息,通過還是也可以有用戶空間寫入數據。
使用device_create_file可以創建一個sysfs節點出來
也可以使用sysfs_create_group來創建一個sysfs目錄,該目錄下可以一次性創建一個或者多個文件節點出來。如果需要多個節點的話,這種創建方式效率比較高。
#include <linux/module.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/io.h>
#include <linux/sched.h>
//#include <asm/irq.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#define TAG "keyvol"
#define INT_GPIO 91
#define READ_KEY_VOL 10
static int key_major;
static struct cdev key_cdev;
static struct class *key_class;
volatile unsigned long *tlmm_gpio_cfg;
volatile unsigned long *tlmm_in_out;
static int key_press = 0;
static int key_value = 0;
static int irq;
static struct fasync_struct *fasync;
static struct proc_dir_entry *key_vol_proc = NULL;
struct device *keyvol_device;
static wait_queue_head_t kwait;
static irqreturn_t key_irq_thread(int irq, void *data)
{
int value;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
value = *tlmm_in_out;
value &= 0x1;
if (value ) {
key_value = 0;
} else {
key_value = 1;
}
wake_up_interruptible(&kwait);
kill_fasync(&fasync, SIGIO, POLL_IN);
key_press = 1;
return IRQ_HANDLED;
}
static ssize_t key_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
if(count != 1 )
return -EINVAL;
if((file->f_flags & O_NONBLOCK) && !key_press)
return -EAGAIN;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
if (!key_press) {
wait_event_interruptible(kwait, key_press);
}
if(key_press) {
if(!copy_to_user(buffer, &key_value, 1)) {
printk(TAG"%s key is press\n", __func__);
key_press = 0;
} else {
printk(TAG"%s copy to user error\n", __func__);
return -EFAULT;
}
}
return count;
}
ssize_t key_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
return count;
}
static int key_open(struct inode *inode, struct file *file)
{
int ret;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
irq = gpio_to_irq(INT_GPIO);
printk(TAG"%s irq is %d\n", __func__, irq);
ret = request_threaded_irq(irq, NULL, key_irq_thread, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, "vol_key", NULL);
printk(TAG"%s ret is %d\n", __func__, ret);
return ret;
}
static unsigned int key_poll(struct file *file, poll_table *wait)
{
unsigned int mask;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
poll_wait(file, &kwait, wait);
if (key_press) {
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
static int key_fasync(int fd, struct file *file, int on)
{
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
return fasync_helper(fd, file, on, &fasync);
}
static long key_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int __user *ip = (int __user *)arg;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
switch (cmd) {
case READ_KEY_VOL:
if(key_press) {
key_press = 0;
if (put_user(1, ip))
return -EFAULT;
} else {
if (put_user(0, ip))
return -EFAULT;
}
return 0;
break;
default:
break;
}
return -EINVAL;
}
static int key_release(struct inode *inode, struct file *file)
{
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
free_irq(irq, NULL);
return 0;
}
static const struct file_operations key_ops = {
.owner = THIS_MODULE,
.read = key_read,
.write = key_write,
.open = key_open,
.poll = key_poll,
.fasync = key_fasync,
.unlocked_ioctl = key_ioctl,
.release = key_release,
};
static int proc_key_vol_proc_show(struct seq_file *file, void* data)
{
int value, len;
u8 buf[32];
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
value = *tlmm_in_out;
value &= 0x1;
len = scnprintf(buf, sizeof(buf), "%d",
value);
seq_printf(file, "%s\n", buf);
return 0;
}
static int proc_key_vol_proc_open(struct inode* inode, struct file* file)
{
return single_open(file, proc_key_vol_proc_show, inode->i_private);
}
static const struct file_operations proc_key_vol_operations = {
.open = proc_key_vol_proc_open,
.read = seq_read,
.owner = THIS_MODULE,
};
static ssize_t keyvol_value_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int value;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
value = *tlmm_in_out;
value &= 0x1;
return sprintf(buf, "%d\n", value);
}
static ssize_t keyvol_value_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
printk(TAG" func:%s line:%d, donot have meaning\n", __func__, __LINE__);
return size;
}
static DEVICE_ATTR(keyvol_val, 0664, keyvol_value_show, keyvol_value_store);
static ssize_t keyvol_stat_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int value;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
value = *tlmm_in_out;
value &= 0x1;
return sprintf(buf, "%d\n", value);
}
static ssize_t keyvol_stat_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
printk(TAG" func:%s line:%d, donot have meaning\n", __func__, __LINE__);
return count;
}
static DEVICE_ATTR(keyvol_stat, S_IRUGO | S_IWUSR,
keyvol_stat_show, keyvol_stat_store);
static struct attribute *keyvol_att_als[] = {
&dev_attr_keyvol_stat.attr,
NULL,
};
static struct attribute_group dev_attr_keyvol_group = {
.name = "keyvolgr",
.attrs = keyvol_att_als,
};
static int my_key_init(void)
{
int retval;
dev_t dev_id;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
retval = alloc_chrdev_region(&dev_id, 0, 1, "key"); //0,1
key_major = MAJOR(dev_id);
printk(TAG"major is %d\n", key_major);
if (retval < 0) {
printk(TAG"can't get major number\n");
goto error;
}
cdev_init(&key_cdev, &key_ops);
retval = cdev_add(&key_cdev, dev_id, 1); //1
if (retval < 0) {
printk(TAG"cannot add cdev\n");
goto cleanup_alloc_chrdev_region;
}
key_class = class_create(THIS_MODULE, "key");
if (IS_ERR(key_class)) {
printk(TAG "Error creating key class.\n");
cdev_del(&key_cdev);
retval = PTR_ERR(key_class);
goto cleanup_cdev_add;
}
keyvol_device = device_create(key_class, NULL, MKDEV(key_major, 0), NULL, "keyvol");
if(keyvol_device == NULL) {
printk(TAG"device_create failed\n");
retval = -ENOMEM;
goto cleanup_class;;
}
tlmm_gpio_cfg = (volatile unsigned long *)ioremap(0x105B000, 8);
if (tlmm_gpio_cfg == NULL) {
printk(TAG"ioremap failed!\n");
retval = -ENOMEM;
goto cleanup_device;
}
tlmm_in_out = tlmm_gpio_cfg + 1;
*tlmm_gpio_cfg |= 0x3;
init_waitqueue_head(&kwait);
key_vol_proc = proc_create("keyvol", 0, NULL, &proc_key_vol_operations);
if (key_vol_proc == NULL) {
printk(TAG"Couldn't create proc entry!\n");
retval = -ENOMEM;
goto cleanup_ioremap;;
}
retval = device_create_file(keyvol_device, &dev_attr_keyvol_val);
if (retval) {
printk(TAG"sys file creation failed\n");
goto cleanup_proc;
}
retval = sysfs_create_group(&keyvol_device->kobj, &dev_attr_keyvol_group);
if (retval) {
printk(TAG"device group create file failed\n");
goto cleanup_device_file;
}
return 0;
cleanup_device_file:
device_remove_file(keyvol_device, &dev_attr_keyvol_val);
cleanup_proc:
proc_remove(key_vol_proc);
cleanup_ioremap:
iounmap(tlmm_gpio_cfg);
cleanup_device:
device_destroy(key_class, MKDEV(key_major, 0));
cleanup_class:
class_destroy(key_class);
cleanup_cdev_add:
cdev_del(&key_cdev);
cleanup_alloc_chrdev_region:
unregister_chrdev_region(dev_id, 0);
error:
return retval;
}
static void key_exit(void)
{
dev_t dev_id = MKDEV(key_major, 0);
sysfs_remove_group(&keyvol_device->kobj, &dev_attr_keyvol_group);
device_remove_file(keyvol_device, &dev_attr_keyvol_val);
proc_remove(key_vol_proc);
iounmap(tlmm_gpio_cfg);
device_destroy(key_class, MKDEV(key_major, 0));
class_destroy(key_class);
cdev_del(&key_cdev);
unregister_chrdev_region(dev_id, 0);
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
}
module_init(my_key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");
測試方法
/sys/class/key/keyvol目錄下菜單keyvol_val節點是由device_create_file函數創建的,同一目錄下keyvolgr目錄以及其下的節點是由sysfs_create_group來創建的
cat /sys/class/key/keyvol/keyvol_val
或者
cat /sys/class/key/keyvol/keyvolgr/keyvol_stat
都可以獲得GPIO91的引腳狀態,爲1的話表明按鍵是鬆開的,爲0的話表明按鍵按下了