今天首次接觸Linux字元裝置的編程,花了很長的時間,針對所發現的問題進行整理如下:
有以下注意點:
1,編譯模組不需要編譯核心。按照如下的Makefile的方式即可在ubuntu下進行裝置模組編譯
2,insmod後,需要使用mknod進行裝置映射。此時,需要在cat /proc/devices中確認好主裝置值
例如:mknod /dev/tfs 250 0
3,編寫測試代碼時,需要注意許可權的使用,因為映射到/dev/tfs中的許可權只有root許可權才可以進行rw。其他的許可權為唯讀。
先附上字元裝置驅動的源碼:
#include <linux/module.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/errno.h>#include <asm/uaccess.h>#include <linux/slab.h>MODULE_LICENSE("Dual BSG/GPL");unsigned int fs_major = 0;static char *data;static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos);static ssize_t test_write(struct file *file, const char *buffer, size_t count, loff_t *f_pos);static int test_open(struct inode *inode, struct file *file);static int test_release(struct inode *inode, struct file *file);int init_module(void);void cleanup_module(void);static struct file_operations chr_fops = {read:test_read,write:test_write,open:test_open,release:test_release };static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos){int len;if (count<0)return -EINVAL;len = strlen(data);if (len<count)count = len;copy_to_user(buf, data, count+1);return count;}static ssize_t test_write(struct file *file, const char *buffer, size_t count, loff_t *f_pos){if (count < 0 )return -EINVAL;kfree(data);data = (char *) kmalloc(sizeof(char)*(count+1), GFP_KERNEL);if (!data)return -ENOMEM;copy_from_user(data, buffer, count+1);return count;}static int test_open(struct inode *inode, struct file *file){try_module_get(THIS_MODULE); printk("This is open \n");return 0;}static int test_release(struct inode *inode, struct file *file){module_put(THIS_MODULE); printk("This is released\n");return 0;}int init_module(void){int res;res = register_chrdev(0, "tfs", &chr_fops);if (res<0){printk("Can't get major name\n");return 0;}if (fs_major == 0)fs_major=res;printk("Major name is %d", fs_major);return 0;}void clean_module(void){unregister_chrdev(fs_major, "tfs");printk("tfs has removed");}
附上Makefile的源碼(注意Makefile的大小寫,不能為makefile)
obj-m +=memrw_drv.oall:make -C /lib/modules/`uname -r`/build M=$(PWD) modulesclean:make -C /lib/modules/`uname -r`/build M=$(PWD) clean
附上字元裝置的測試源碼:
#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#define DEVPATH"/dev/tfs"int main(int argc, char **argv){int fd, i, nwrite, nread, len;char *buf = "String to Driver\n";char read_buf[18] = { 0 };fd = open(DEVPATH, O_RDWR);if (fd<0){perror("open");exit(1);}elseprintf("device opened!\n");fflush(stdout);len = strlen(buf);nwrite = write(fd, buf, len);if(nwrite<0){perror("write");exit(1);}nread = read(fd, read_buf, 18);if(nread<0){perror("read");exit(1);}elseprintf("Read String is from driver -> %s" , read_buf);fflush(stdout);close(fd);return 0;}