【韋東山驅動代碼移植高通平臺之二十一】proc節點的創建

proc節點的創建

proc是一種特殊的,由軟件創建的文件系統。內核可以使用它向外界導出信息,還可以由用戶寫入數據。

使用proc_create函數來創建節點

傳入的file_operations結構體需要用戶來定義,定義open, read, write等函數

 

參考代碼一

#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 s32 key_vol_read_proc(struct file *file, char __user *user_buf,
		size_t count, loff_t *ppos)
{
	int value, len, ret;
	u8 buf[32];

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	value = *tlmm_in_out;
	value &= 0x1;

	len = scnprintf(buf, sizeof(buf), "%d\n",
			value);
	ret = simple_read_from_buffer(user_buf, count, ppos,
			buf, len);

	return ret;
}


static const struct file_operations proc_key_vol_operations = {
	.open		= simple_open,
	.read		= key_vol_read_proc,
	.owner = THIS_MODULE,
};

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;
	}

	return 0;

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);

	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");

 

參考代碼二

#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 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;
	}

	return 0;

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);

	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");

 

測試方法

兩種方法都會創建出 /proc/keyvol節點

cat /proc/keyvol

可以獲得GPIO91的引腳狀態,爲1的話表明按鍵是鬆開的,爲0的話表明按鍵按下了

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章