環境:
主機:Fedora 12
目標板:MINI6410
目標板LINUX內核版本:2.6.38
實現功能:
使用read函數讀取內核空間開辟的數組,使用write函數從用戶空間寫入數據到內核空間開辟的數組
說明:
Linux中內核空間和用戶空間有不同的內存定義,只能通過交互函數來互相訪問.
//檢測用戶空間地址是否合法,type選項:VERIFY_READ,VERIFY_WRITE
int access_ok(int type,const void *addr,unsigned long size);
//從用戶空間讀取內存
unsigned long copy_from_user(void *to,const void *from,unsigned long n);
//向用戶空間內存寫入
unsigned long copy_to_user(void *to,void *from,unsigned long len);
//寫入單值
int put_user(數據,ptr);
//讀取單值
int get_user(數據,ptr);
驅動源代碼:
test_driver.c:
[cpp]
- #include <linux/miscdevice.h>
- #include <linux/delay.h>
- #include <asm/irq.h>
- //#include <mach/regs-gpio.h>
- #include <mach/hardware.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/mm.h>
- #include <linux/fs.h>
- #include <linux/types.h>
- #include <linux/delay.h>
- #include <linux/moduleparam.h>
- #include <linux/slab.h>
- #include <linux/errno.h>
- #include <linux/ioctl.h>
- #include <linux/cdev.h>
- #include <linux/string.h>
- #include <linux/list.h>
- #include <linux/pci.h>
- #include <asm/uaccess.h>
- #include <asm/atomic.h>
- #include <asm/unistd.h>
- #include <linux/major.h>
-
- #include <mach/map.h>
- #include <mach/regs-clock.h>
- #include <mach/regs-gpio.h>
-
- #include <plat/gpio-cfg.h>
- #include <mach/gpio-bank-e.h>
- #include <mach/gpio-bank-k.h>
- #include <mach/gpio-bank-h.h>
- #include <mach/gpio-bank-n.h>
- #include <mach/gpio-bank-l.h>
- #include <mach/gpio-bank-p.h>
-
- #include <linux/device.h>
-
- #include <linux/jiffies.h>
- #include <linux/string.h>
-
- #define DEVICE_NAME "test_driver"
- #define T_MAJORS 800
-
- //設備結構
- static struct _Test_Driver_Device
- {
- struct cdev fun_cdev;
- };
- struct _Test_Driver_Device *Test_Driver_Device;
-
- static dev_t dev;
- static struct class *test_class;
-
- //開辟緩存,用來讀寫
- #define LEN_BUF 256
- static unsigned char Buffer[LEN_BUF];
-
- //初始化互斥鎖
- static DEFINE_MUTEX(sem);
-
- //功能:初始化緩存
- static void init_buf(void)
- {
- memset(Buffer,0,LEN_BUF);
- }
-
- //功能:讀取緩存
- //返回:讀取的字節數
- ssize_t test_driver_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
- {
- //判斷文件當前位置是否越界
- if (*f_pos >= LEN_BUF)
- {
- return 0;
- }
- //判斷讀取的字節總數是否越界
- if (count > (LEN_BUF - *f_pos))
- {
- count = 256 - *f_pos;
- }
- //拷貝數據到用戶空間
- if (copy_to_user(buf,Buffer + *f_pos,count))
- {
- return -EFAULT;
- }
- //改變文件的當前位置
- *f_pos += count;
-
- return count;
- }
-
- //功能:寫入緩存
- //返回:寫入的字節數
- ssize_t test_driver_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_pos)
- {
- ssize_t err = -ENOMEM;
-
- //判斷文件當前位置是否越界
- if (*f_pos >= LEN_BUF)
- {
- goto ERR;
- }
- //判斷寫入的字節總數是否越界
- if (count > (LEN_BUF - *f_pos))
- {
- count = 256 - *f_pos;
- }
- //從用戶空間拷貝數據
- if (copy_from_user(Buffer + *f_pos,buf,count))
- {
- err = -EFAULT;
- goto ERR;
- }
- //改變文件的當前位置
- *f_pos += count;
-
- return count;
-
- ERR:
- return err;
- }
-
- //功能:偏移指針
- //返回:當前指針
- //off是偏移量,whence是偏移開始處
- ssize_t test_driver_llseek(struct file *filp,size_t count,loff_t off,int whence)
- {
- loff_t pos;
- //獲取當前指針
- pos = filp->f_pos;
- switch (whence)
- {
- //從0開始
- case 0:
- {
- pos = off;
- break;
- }
- //從當前位置開始
- case 1:
- {
- pos += off;
- break;
- }
- case 2:
- default:
- {
- return -EINVAL;
- }
- }
- //判斷是否超出范圍
- if (pos > LEN_BUF || pos < 0)
- {
- return -EINVAL;
- }
-
- //更改當前偏移量
- filp->f_pos = pos;
- return filp->f_pos;
- }
-
- static struct file_operations io_dev_fops = {
- .owner = THIS_MODULE,
- .llseek = test_driver_llseek,
- .write = test_driver_write,
- .read = test_driver_read,
- };
-
- static int __init dev_init(void)
- {
- int ret;
- unsigned temp;
-
- init_buf();
-
- //分配結構體
- Test_Driver_Device = kmalloc(sizeof(struct _Test_Driver_Device),GFP_KERNEL);
- if (!Test_Driver_Device)
- {
- unregister_chrdev_region(dev,1);
- device_destroy(test_class, dev);
- class_destroy(test_class);
-
- return -ENOMEM;
- }
-
- dev = MKDEV(T_MAJORS,0);
- cdev_init(&Test_Driver_Device->fun_cdev,&io_dev_fops);
- ret = register_chrdev_region(dev,1,DEVICE_NAME);
- if (ret < 0) return 0;
- ret = cdev_add(&Test_Driver_Device->fun_cdev,dev,1);
- if (ret < 0) return 0;
-
- printk (DEVICE_NAME"\tjdh:test_driver initialized!!\n");
-
- test_class = class_create(THIS_MODULE, "test_class1");
- if (IS_ERR(test_class))
- {
- printk(KERN_INFO "create class error\n");
- return -1;
- }
- device_create(test_class, NULL, dev, NULL, "test_driver");
-
- return ret;
- }
-
- static void __exit dev_exit(void)
- {
- unregister_chrdev_region(dev,1);
-
- device_destroy(test_class, dev);
- class_destroy(test_class);
- }
-
- module_init(dev_init);
- module_exit(dev_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("JDH");