内核空间 访问 用户空间 flip_open

内核空间 访问  用户空间 flip_open, 简单历程,可以作为调试手段。

本例程每隔一段时间往sdcard目录下的指定文件写入内容

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>


#include <linux/errno.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>

#include <mach/hardware.h>
#include <linux/wakelock.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/uaccess.h>

#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/fs.h>


#define filepath "/sdcard/test.txt"

static struct delayed_work polling_work;
//static int my_work(struct work_struct *work)
static int my_work(void)
{
	struct file *f;
	int err;
	int16_t thrd;
	u8 reg;
	char *buf="helloworld bsp \n\n";
	mm_segment_t old_fs;
	
	printk("syscall my_work\n");



	printk("syscall open\n");
	
	f = filp_open(filepath,O_RDWR|O_CREAT|O_APPEND,0);
	if (IS_ERR(f)) {
		pr_err("%s: Can't open calibration file\n", __func__);
		set_fs(old_fs);
		err = PTR_ERR(f);
		goto done;
	}

	old_fs = get_fs();
	set_fs(get_ds());
	
	printk("syscall write\n");
	
	err = f->f_op->write(f,buf, strlen(buf),&f->f_pos);
	//if (err != sizeof(int)) {
	//	pr_err("%s: Can't write the cal data to file\n", __func__);
	//	err = -EIO;
	//}
	set_fs(old_fs);
	
	filp_close(f,NULL);
	//filp_close(f, current->files);
	schedule_delayed_work(&polling_work,msecs_to_jiffies(5000));
	printk("close ok\n");
	return 0;
done:
	set_fs(old_fs);
	filp_close(f,NULL);
	return err;


}


static int syscall_drv_init(void)
{
	printk("syscall drv init\n");
	INIT_DELAYED_WORK(&polling_work,my_work);
	schedule_delayed_work(&polling_work,msecs_to_jiffies(30000));
	return 0;
}

static void syscall_drv_exit(void)
{
	printk("syscall drv exit\n");
}

module_init(syscall_drv_init);
module_exit(syscall_drv_exit);


MODULE_LICENSE("GPL");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值