zoukankan      html  css  js  c++  java
  • ERROR: Removing 'hello': Device or resource busy

        其实之前做过一段时间的驱动开发,但是很久没碰了,要写论文了,所以驱动的东西还得再捡起来,今天才重写第一个入门的hello world就出问题了,不过还好通过强大的baidu和google解决了,记录学习的过程.

       先看看代码 hello.c

    #include <linux/init.h>
    #include <linux/module.h>
    
    static int __init hello_init(void)
    {
    	printk(KERN_ALERT "hello world init,2014_2_19");
    	return 0;
    }
    
    
    static void __exit hello_exit(void)
    {
    	printk(KERN_ALERT "hello world exit,2014_2_19");
    }
    
    
    module_init(hello_init);
    module_exit(hello_exit);
    MODULE_LICENSE("GPL");
    

    Makefile文件

    obj-m	:=hello.o
    
    KERNELDIR	?=/lib/modules/$(shell uname -r)/build
    PWD		?=$(shell pwd)
    
    build:kernel_modules
    kernel_modules:
    	make -C $(KERNELDIR) M=$(PWD) modules
    clean:
    	make -C $(KERNELDIR) M=$(PWD) clean
    

    看起来都很简单,于是make后,生成.ko文件

    输入命令

    sudo insmod hello.ko

    接着lsmod

    u1@u1:~/first_module$ lsmod
    Module                  Size  Used by
    hello                  12425  0 [permanent]
    bnep                   18436  2 
    rfcomm                 47946  0 
    bluetooth             166112  8 bnep,rfcomm
    ppdev                  17113  0 

          查看内核打印信息,使用dmesg | grep hello 

    看到

    u1@u1:~/first_module$ dmesg | grep hello
    [   89.773378] hello world init,2014_2_19

    可以看到模块顺利加载了 并且初始化了


    接着 sudo rmmod hello

    u1@u1:~/first_module$ sudo rmmod hello
    ERROR: Removing 'hello': Device or resource busy

    好吧 出问题了 找了好一会 看了各种解释 发现都没问题 后来 看了他的 http://blog.csdn.net/shuzui1985/article/details/8795261  才发现 原来是gcc版本的问题 因为之前之编译tq210上的源代码的时候 我把gcc改为了4.4 但是内核的gcc版本为4.6

    看下面操作就明白了

    u1@u1:~/first_module$ ls -l /usr/bin/gcc
    lrwxrwxrwx 1 root root 16 2014-02-19 19:02 /usr/bin/gcc -> /usr/bin/gcc-4.4
    u1@u1:~/first_module$ gcc -v
    使用内建 specs。
    目标:x86_64-linux-gnu
    配置为:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.6-11ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    线程模型:posix
    gcc 版本 4.4.6 (Ubuntu/Linaro 4.4.6-11ubuntu2) 
    u1@u1:~/first_module$ cat /proc/version
    Linux version 3.0.0-12-generic (buildd@crested) (gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) ) #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011
    u1@u1:~/first_module$ 

    ls -l /usr/bin/gcc  用于查看当前的gcc版本 会发现软连接到了gcc4.4
    cat /proc/version   查看的是系统编译内核时所用的gcc为4.6


    所以果断将gcc的版本改为4.6 即可

    修改gcc的版本

    u1@u1:~/first_module$ su
    密码: 
    root@u1:/home/u1/first_module# rm /usr/bin/gcc
    root@u1:/home/u1/first_module# ln -s /usr/bin/gcc-4.6 /usr/bin/gcc
    root@u1:/home/u1/first_module# gcc -v
    使用内建 specs。
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
    目标:x86_64-linux-gnu
    配置为:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    线程模型:posix
    gcc 版本 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 
    root@u1:/home/u1/first_module# 

    看到上述信息 表示gcc的版本变为了4.6

    再此 make clean  然后make

    root@u1:/home/u1/first_module# su u1
    u1@u1:~/first_module$ make clean
    make -C /lib/modules/3.0.0-12-generic/build M=/home/u1/first_module clean
    make[1]: 正在进入目录 `/usr/src/linux-headers-3.0.0-12-generic'
      CLEAN   /home/u1/first_module/.tmp_versions
      CLEAN   /home/u1/first_module/Module.symvers
    make[1]:正在离开目录 `/usr/src/linux-headers-3.0.0-12-generic'
    u1@u1:~/first_module$ ls
    hello.c  Makefile
    u1@u1:~/first_module$ make
    make -C /lib/modules/3.0.0-12-generic/build M=/home/u1/first_module modules
    make[1]: 正在进入目录 `/usr/src/linux-headers-3.0.0-12-generic'
      CC [M]  /home/u1/first_module/hello.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/u1/first_module/hello.mod.o
      LD [M]  /home/u1/first_module/hello.ko
    make[1]:正在离开目录 `/usr/src/linux-headers-3.0.0-12-generic'

    现在只有reboot之后 才能重新加载了 

    所以  sudo reboot

    u1@u1:~/first_module$ sudo insmod hello.ko
    [sudo] password for u1: 
    u1@u1:~/first_module$ dmesg | grep hello
    [   84.835319] hello world init,2014_2_19
    u1@u1:~/first_module$ sudo rmmod hello
    u1@u1:~/first_module$ dmesg | grep hello
    [   84.835319] hello world init,2014_2_19
    [  107.730749] hello world exit,2014_2_19
    u1@u1:~/first_module$ lsmod
    Module                  Size  Used by
    ppdev                  17113  0 
    snd_hda_codec_realtek   330769  1 
    snd_hda_intel          33390  2 
    snd_hda_codec         104802  2 snd_hda_codec_realtek,snd_hda_intel
    snd_hwdep              13668  1 sn




  • 相关阅读:
    ios添加pre和post build action
    Highcharts将数据以图表的形式展现
    【deep learning学习笔记】注释yusugomori的LR代码 --- 模型测试
    九乘九口诀算法
    JMS学习的个人理解笔记
    php与文件操作
    java 泛型
    约定
    写给自己,毕业三年,薪水涨了12倍,你信吗
    好技术领导和差技术领导区别在哪里--转
  • 原文地址:https://www.cnblogs.com/liangxinzhi/p/4275581.html
Copyright © 2011-2022 走看看