添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • 内核是我们通常所说的“Linux技术奇迹”的最重要特征
  • 内核既是操作系统的心脏,也是它的大脑,因为内核控制着基本的硬件。内核是操作系统的核心,具有很多最基本功能,如虚拟内存、多任务、共享库、需求加载、共享的写时拷贝(copy-on-write)可执行程序和TCP/IP网络功能
  • Linux内核本身并不是操作系统,它是一个完整操作系统的组成部分。Red Hat、Novell、Debian和Gentoo等Linux发行商都采用Linux内核,然后加入更多的工具、库和应用程序来构建一个完整的操作系统
  • register_chrdev()为什么逐渐被淘汰?有什么缺点?

  • register_chrdev()分为静态注册和动态注册,而新型的注册将静态注册和动态注册分为两个个体,解决了前者产生资源浪费的问题
  • 设备驱动简介

    设备驱动程序是内核的一部分。

    OS通过各种驱动程序来操作硬件设备,设备驱动程序是内核的一部分,硬件驱动程序是OS最基本的组成部分。

    Linux将最基本的核心代码编译在内核当中,其他代码编译到内核或者内核的模块文件,需要时再加载。常见的内核模块驱动程序比如声卡和网卡,linux基础驱动包括CPU,PCI总线,TCP/IP协议,APM(高级电源管理)等。

    加载驱动就是加载内核模块。

    lsmod列出当前系统中加载的模块

    设备驱动程序与外界的接口

    设备驱动程序必须为内核或者其子系统提供一个标准接口。

    设备驱动编程

    设备驱动程序以模块的方式动态加载到内核中。在驱动开发时没有main()函数,模块在调用insmod命令时被加载,在该函数中完成设备的注册。调用rmmod命令时被卸载。设备完成注册加载后,用户的应用程序可以对该设备进行一定的操作,如open()、read()、write()等。

    字符设备的注册

    在内核中使用struct cdev结构来描述字符设备,我们在驱动设备中将已分配到的设备号以及设备操作接口(struct file_operations结构)赋予struct cdev结构变量。

    使用cdev_alloc()函数向系统申请分配struct cdev结构,再用cdev_init()函数初始化已分配到的结构与file_operations结构关联。

    调用cdev_add()函数将设备号与struct cdev结构进行关联并向内核正式报告新设备的注册,新设备可以使用了。

    设备驱动结构函数

    打开设备的函数接口open()

    释放设备的函数接口realease()

    读写设备read() write()函数

    问题三:运行load脚本出现错误 insmod: error inserting './test_drv.ko': -1 File exists

  • 这个原因一般是因为,运行过 ./test_drv_load(没加sudo) ,但是失败了,但是运行到了加载驱动的模块的语句,驱动已经加载,所以再次运行加载模块会提示该错。
  • 根据书第十一章最后关于字符设备的test实验中代码,将test_drv.c,test.c,Makefile,test_drv_load,test_drv_unload文件敲入,保存在文件夹内。[注]:敲入书中代码时要多检查,避免错误。

    这是test_drv.c的源代码:

    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/fs.h>
    #include <linux/kernel.h>
    #include <linux/slab.h>
    #include <linux/types.h>
    #include <linux/errno.h>
    #include <linux/cdev.h>
    #include <asm/uaccess.h>
    #define     TEST_DEVICE_NAME    "test_dev"
    #define     BUFF_SZ         1024
    static struct cdev test_dev;
    unsigned int major =0;
    static char *data = NULL;
    /*º¯ÊýÉùÃ÷*/
    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);
    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);
        count = (len > count)?count:len;
        if (copy_to_user(buf, data, count))
            return -EFAULT;
        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;
        memset(data, 0, BUFF_SZ);
        count = (BUFF_SZ > count)?count:BUFF_SZ;
        if (copy_from_user(data, buffer, count))
            return -EFAULT;
        return count;
    static int test_open(struct inode *inode, struct file *file)
        printk("This is open operation\n");
        data = (char*)kmalloc(sizeof(char) * BUFF_SZ, GFP_KERNEL);
        if (!data)
            return -ENOMEM;
        memset(data, 0, BUFF_SZ);
        return 0;
    static int test_release(struct inode *inode,struct file *file)
        printk("This is release operation\n");
        if (data)
            kfree(data);
            data = NULL;
        return 0;
    static void test_setup_cdev(struct cdev *dev, int minor,
            struct file_operations *fops)
        int err, devno = MKDEV(major, minor);
        cdev_init(dev, fops);
        dev->owner = THIS_MODULE;
        dev->ops = fops;
        err = cdev_add (dev, devno, 1);
        if (err)
            printk (KERN_NOTICE "Error %d adding test %d", err, minor);
    static struct file_operations test_fops = 
        .owner   = THIS_MODULE,
        .read    = test_read,
        .write   = test_write,
        .open    = test_open,
        .release = test_release,
    int init_module(void)
        int result;
        dev_t dev = MKDEV(major, 0);
        if (major)
            result = register_chrdev_region(dev, 1, TEST_DEVICE_NAME);
            result = alloc_chrdev_region(&dev, 0, 1, TEST_DEVICE_NAME);
            major = MAJOR(dev);
        if (result < 0) 
            printk(KERN_WARNING "Test device: unable to get major %d\n", major);
            return result;
        test_setup_cdev(&test_dev, 0, &test_fops);
        printk("The major of the test device is %d\n", major);
        return 0;
    void cleanup_module(void) 
        cdev_del(&test_dev);
        unregister_chrdev_region(MKDEV(major, 0), 1);
        printk("Test device uninstalled\n");
    
    test.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #define     TEST_DEVICE_FILENAME        "/dev/test_dev"
    #define     BUFF_SZ             1024
    int main()
        int fd, nwrite, nread;
        char buff[BUFF_SZ];
        fd = open(TEST_DEVICE_FILENAME, O_RDWR);
        if (fd < 0)
            perror("open");
            exit(1);
            printf("Input some words to kernel(enter 'quit' to exit):");
            memset(buff, 0, BUFF_SZ);
            if (fgets(buff, BUFF_SZ, stdin) == NULL)
                perror("fgets");
                break;
            buff[strlen(buff) - 1] = '\0';
            if (write(fd, buff, strlen(buff)) < 0)
                perror("write");
                break;
            if (read(fd, buff, BUFF_SZ) < 0)
                perror("read");
                break;
                printf("The read string is from kernel:%s\n", buff);
        } while(strncmp(buff, "quit", 4));
        close(fd);
        exit(0);
    
    Makefile:
    ifeq ($(KERNELRELEASE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
    modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
    clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
    .PHONY: modules modules_install clean
        obj-m := test_drv.o
    endif
    test_drv_load脚本内容如下:
    #!/bin/sh
    module="test_drv"
    device="test_dev"
    mode="664"
    group="david"
    # remove stale nodes
    rm -f /dev/${device} 
    # invoke insmod with all arguments we got
    # and use a pathname, as newer modutils don't look in . by default
    /sbin/insmod -f ./$module.ko $* || exit 1
    major=`cat /proc/devices | awk "\\$2==\"$device\" {print \\$1}"`
    mknod /dev/${device} c $major 0
    # give appropriate group/permissions
    chgrp $group /dev/${device}
    chmod $mode  /dev/${device}
    
    test_drv_unload脚本内容:
    #!/bin/sh
    module="test_drv"
    device="test_dev"
    # invoke rmmod with all arguments we got
    /sbin/rmmod $module $* || exit 1
    # remove nodes
    rm -f /dev/${device}
    exit 0
    

    实验体会与总结:

  • 本周学习了嵌入式Linux应用程序开发,来做这个实验。
  • 嵌入式系统属于:用于控制、监视或者辅助操作机器和设备的装置,是一个控制程序存储在ROM中的嵌入式处理器控制板。
  • Linux基础部分从Linux的安装过程、基本操作命令学起,通过了解嵌入式Linux的环境搭建,以及嵌入式Linux的I/O与文件系统的开发、进程控制开发、进程间通信开发和实践,让我对嵌入式这个词有了更为深入的了解
  • 虽然一些实验具体的步骤只是照猫画虎,但是我还是对嵌入式系统有了一定的思路。这次实验使用康奈尔笔记,与之前只是看从不动笔做比较,让我学习的条理性有了很大的提升。而老师发的那张纸中左边是重点部分,右边是笔记部分让我更有条理性,更好的把握住了重点。让我的学习效率有了显著的提升。-
  •