内核空间 访问 用户空间 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");