Linux driver with new kernel-3.8

Source: Internet
Author: User

It took a long time to program the Linux character device for the first time today. The problems found are as follows:

Note the following:

1. the compiling module does not need to compile the kernel. Compile the device module in Ubuntu as follows:

2. After insmod, you need to use mknod for device ing. In this case, check the value of the master device in CAT/proc/devices.

Example: mknod/dev/TFS 250 0

3. When writing test code, pay attention to the use of permissions, because only the root permission is allowed to perform RW for permissions mapped to/dev/TFS. Other permissions are read-only.

First, attach the source code of the character device driver:

#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");}

The source code of makefile is attached. (make sure that the makefile is case-sensitive and cannot be 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 

The test source code of the character device is attached:

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.