linux c 编程 ------ 通过设备节点调用驱动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux c 编程 ------ 通过设备节点调用驱动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2883字,纯文字阅读大概需要5分钟。
内容图文
![linux c 编程 ------ 通过设备节点调用驱动](/upload/InfoBanner/zyjiaocheng/1177/cede5157f5504da5a117b9f14049d0f3.jpg)
驱动程序如下,加载驱动后,会在/dev文件夹下生成一个文件hello_device_node,是此驱动的设备节点
#include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/miscdevice.h> #include <linux/fs.h> #define DRIVER_NAME "hello" #define NODE_NAME "hello_device_node" MODULE_LICENSE("Dual BSD/GPL"); // required MODULE_AUTHOR("liuShuiDeng"); staticlong hello_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { printk("\ncmd is %d, arg is %ld\n", cmd, arg); return0; } staticint hello_fs_release(struct inode *inode, struct file *file) { printk(KERN_EMERG "\nhello_fs_release\n"); return0; } staticint hello_fs_open(struct inode *inode, struct file *file) { printk(KERN_EMERG "\nhello_fs_open\n"); return0; } staticstruct file_operations hello_fops = { .owner = THIS_MODULE, .open = hello_fs_open, .release = hello_fs_release, .unlocked_ioctl = hello_fs_ioctl, }; staticstruct miscdevice hello_miscdevice = { .minor = MISC_DYNAMIC_MINOR, .name = NODE_NAME, .fops = &hello_fops, }; staticint hello_probe(struct platform_device *p) { printk(KERN_EMERG "\nhello_probe\n"); misc_register(&hello_miscdevice); return0; } staticint hello_remove(struct platform_device *p) { printk(KERN_EMERG "\nhello_remove\n"); misc_deregister(&hello_miscdevice); return0; } staticvoid hello_shutdown(struct platform_device *p) { printk(KERN_EMERG "\nhello_shutdown\n"); } staticint hello_suspend(struct platform_device *p, pm_message_t state) { printk(KERN_EMERG "\nhello_suspend\n"); return0; } staticint hello_resume(struct platform_device *p) { printk(KERN_EMERG "\nhello_resume\n"); return0; } staticstruct platform_driver hello_driver={ .probe = hello_probe, .remove = hello_remove, .shutdown = hello_shutdown, .suspend = hello_suspend, .resume = hello_resume, .driver = { .name = DRIVER_NAME, .owner = THIS_MODULE, }, }; staticint hello_init(void) //insmod xxx.ko, execute it{ printk(KERN_EMERG "\nhello world enter~\n\n"); platform_driver_register(&hello_driver); return0; } staticvoid hello_exit(void) //rmmod xxx( note: not xxx.ko ), execute it{ printk(KERN_EMERG "\nhello world exit~\n\n"); platform_driver_unregister(&hello_driver); } module_init(hello_init); module_exit(hello_exit);
应用程序如下
编译驱动程序的编译器和编译应用程序的编译器建议用同一个
编译应用程序指令:arm-none-linux-gnueabi-gcc -o invoke_hello invoke_hello.c
修改权限指令:chmod 777 invoke_hello
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> void main() { int fd; char *hello_node = "/dev/hello_device_node"; fd = open(hello_node, O_RDWR|O_NDELAY); if(fd < 0) { printf("APP---->can‘t open \"%s\"\n",hello_node); }else { printf("APP--->open \"%s\" successfully\n", hello_node); ioctl(fd,1,2); } close(fd); }
原文:https://www.cnblogs.com/god-of-death/p/9502652.html
内容总结
以上是互联网集市为您收集整理的linux c 编程 ------ 通过设备节点调用驱动全部内容,希望文章能够帮你解决linux c 编程 ------ 通过设备节点调用驱动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。