Linux Driver with new kernel-3.8

來源:互聯網
上載者:User

今天首次接觸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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.