日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LInux 字符设备驱动程序

發(fā)布時間:2023/11/29 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LInux 字符设备驱动程序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • #include?<linux/module.h>?
  • ?
  • #include?<linux/init.h>?
  • ?
  • #include?<linux/fs.h>?
  • ?
  • #include?<asm/uaccess.h>?
  • ?
  • MODULE_LICENSE("GPL");//?設(shè)置這個模塊的協(xié)議為?GPL??
  • ?
  • #define?MAJOR_NUM?2918?//主設(shè)備號?
  • ?
  • //聲明Read函數(shù)?
  • static?ssize_t?device_read(struct?file?*,?char?*,?size_t,?loff_t*);?
  • //聲明Write函數(shù)?
  • static?ssize_t?device_write(struct?file?*,?const?char?*,?size_t,?loff_t*);?
  • ?
  • //初始化字符設(shè)備驅(qū)動的file_operations結(jié)構(gòu)體?
  • ?
  • struct?file_operations?device_fops?=?
  • ?
  • {?
  • .read=device_read,?
  • .write=device_write,?
  • };?
  • ?
  • static?int?device_var?=?0;?//"globalvar"設(shè)備的全局變量?
  • ?
  • static?int?__init?device_init(void)?
  • {?
  • int?ret;?
  • //注冊設(shè)備驅(qū)動?
  • ret?=?register_chrdev(MAJOR_NUM,?"device",?&device_fops);?
  • if?(ret)?
  • {?
  • printk("device?register?failure");?
  • }?
  • else?
  • {?
  • printk("device?register?success");?
  • }?
  • return?ret;?
  • }?
  • ?
  • static?void?__exit?device_exit(void)?
  • {?
  • int?ret?=?0;?
  • //注銷設(shè)備驅(qū)動?
  • unregister_chrdev(MAJOR_NUM,?"device");?
  • if?(ret)?
  • {?
  • printk("device?unregister?failure");?
  • }?
  • else?
  • {?
  • printk("device?unregister?success");?
  • }?
  • }?
  • //READ?
  • static?ssize_t?device_read(struct?file?*filp,?char?*buf,?size_t?len,?loff_t?*off)?
  • {?
  • //將device_var從內(nèi)核空間復(fù)制到用戶空間?
  • if?(copy_to_user(buf,?&device_var,?sizeof(int)))?
  • {?
  • return?-?EFAULT;?
  • }?
  • return?sizeof(int);?
  • }?
  • static?ssize_t?device_write(struct?file?*filp,?const?char?*buf,?size_t?len,?loff_t?*off)?
  • {?
  • //將用戶空間的數(shù)據(jù)復(fù)制到內(nèi)核空間的device_var?
  • if?(copy_from_user(&device_var,?buf,?sizeof(int)))?
  • {?
  • return?-?EFAULT;?
  • }?
  • return?sizeof(int);?
  • }?
  • module_init(device_init);?
  • module_exit(device_exit);?
  • 編譯為模塊:

    命令為:make -C /lib/modules/3.0.0-7-generic/build M(等號)/home/shi/c_dev/modules

    Makefile為

    ?

  • #?From《Linux?Device?Drivers》3rd?Edition?
  • #?Makefile?
  • ifneq?($(KERNELRELEASE),)?
  • #?call?from?kernel?build?system?
  • ?
  • obj-m???:=?device.o?
  • ?
  • else?
  • ?
  • KERNELDIR??=?/lib/modules/$(shell?uname?-r)/build?
  • PWD???????:=?$(shell?pwd)?
  • ?
  • default:?
  • ????$(MAKE)?-C?$(KERNELDIR)?M=$(PWD)?modules?
  • ?
  • endif??
  • 完成之后,講模塊加入到內(nèi)核中去

    sudo insmod ./device.ko

    查看所有模塊

    lsmod

    刪除模塊

    sudo rmmod ./device.ko

    測試程序

    ?

  • #include?<sys/types.h>?
  • #include?<sys/stat.h>?
  • #include?<stdio.h>?
  • #include?<fcntl.h>?
  • main()?
  • {?
  • int?fd,num;?
  • //打開"/dev/device"?
  • fd?=?open("/dev/device",?O_RDWR,?S_IRUSR?|?S_IWUSR);?
  • if?(fd?!=?0)?
  • {?
  • //初次讀device?
  • read(fd,?&num,?sizeof(int));?
  • printf("The?device?is?%d\n",?num);?
  • //寫device?
  • printf("Please?input?the?num?written?to?device\n");?
  • scanf("%d",?&num);?
  • write(fd,?&num,?sizeof(int));?
  • //再次讀device?
  • read(fd,?&num,?sizeof(int));?
  • printf("The?device?is?%d\n",?num);?
  • //關(guān)閉"/dev/device"?
  • close(fd);?
  • }?
  • else?
  • {?
  • printf("Device?open?failure\n");?
  • }?
  • }??
  • 運行結(jié)果

    shi@smkoo:~/c_dev$ ./test
    The device is 134514219
    Please input the num written to device
    1
    The device is 1
    ?

    轉(zhuǎn)載于:https://blog.51cto.com/1793109/709506

    總結(jié)

    以上是生活随笔為你收集整理的LInux 字符设备驱动程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。