文件名称:i2c-dev.c驱动详细分析
文件大小:14KB
文件格式:C
更新时间:2020-06-21 09:54:18
i2c驱动分析
static int __init i2c_dev_init(void) { printk("i2c init ...\n"); int res = register_chrdev(I2C_MAJOR,"i2c",&i2cdev_fops);//注册设备号 if(res) goto out; i2c_dev_class =class_create(THIS_MODULE,"i2c-dev");//在/proc/devices下创建设备节点 if(IS_ERR(i2c_dev_class))//判断是否创建成功 { res =PTR_ERR(i2c_dev_class); goto out_unreg_chrdev; } //跟踪适配器将被添加或删除到总线之后 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier); if(res) goto out_unreg_class; //绑定到已经存在的适配器 i2c_for_each_dev(NULL,i2cdev_attach_adapter); return 0; out_unreg_class: classs_destroy(i2c_dev_class);//销毁i2c类 out_unreg_chrdev: unregister_chrdev(I2C_MAJOR,"i2c"); out: printk(KERN_ERR "%s: Driver Initialisation failed\n",__FILE__); return res; } static void __exit i2c_dev_exit(void) { printk("i2c exit ...\n"); bus_unregister_notifier(&i2c_bus_type,&i2cdev_notifier);//从总线上卸载I2C i2c_for_each_dev(NULL,i2cdev_detach_adapter); //遍历销毁 class_destroy(i2c_dev_class); unregister_chrdev(I2C_MAJOR, "i2c"); //卸载i2c驱动 } module_init(i2c_dev_init); module_exit(i2c_dev_exit); MODULE_LICENSE("GPL");