iTop4412驱动开发之一:第一个驱动,HelloWorld!

    技术2024-07-16  71

    #include <linux/module.h> #include <linux/init.h> #include <linux/platform_device.h> #include <linux/miscdevice.h> #include <linux/fs.h> MODULE_LICENSE("GPL"); //设备节点名 #define NODE_NAME "hello_node" static int hello_open(struct inode *node, struct file *fp) { printk("hello open.\n"); return 0; } static int hello_release(struct inode *node, struct file *fp) { printk("hello release.\n"); return 0; } static int hello_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) { printk("hello ioctl, cmd: %d, arg: %d\n", cmd, arg); return 0; } struct file_operations hello_ops = { .owner = THIS_MODULE, .open = hello_open, .release = hello_release, .unlocked_ioctl = hello_ioctl, }; //杂项设备 struct miscdevice hello_misc_dev = { .minor = MISC_DYNAMIC_MINOR, //自动分配次设备号 .name = NODE_NAME, .fops = &hello_ops, }; static int hello_probe(struct platform_device *dev) { printk("hello probe, device name: %s\n", dev->name); //注册杂项设备,自动生成设备节点 misc_register(&hello_misc_dev); return 0; } static int hello_remove(struct platform_device *dev) { printk("hello remove, device name: %s\n", dev->name); //注销杂项设备 misc_deregister(&hello_misc_dev); return 0; } //平台总线驱动 struct platform_driver hello_driver = { .probe = hello_probe, .remove = hello_remove, .driver = { .name = "hello_ctl", .owner = THIS_MODULE, } }; //平台总线设备 struct platform_device hello_dev = { .name = "hello_ctl", .id = -1, }; static int hello_init() { printk(KERN_EMERG "hello init.\n"); //先注册设备到平台总线 platform_device_register(&hello_dev); //再注册驱动到平台总线驱动 platform_driver_register(&hello_driver); return 0; } static void hello_exit() { printk(KERN_EMERG "hello exit.\n"); platform_driver_unregister(&hello_driver); platform_device_unregister(&hello_dev); } module_init(hello_init); module_exit(hello_exit);
    Processed: 0.008, SQL: 9