其中,I2C_Param是与用户空间交互用的参数结构体,用户空间必须定义相同的结构体以保证交互的正确性。g_tvp5158_obj 和 tvp5158_i2c_read/write 均为前面文章中定义的变量和函数。
(5)在__init 代码中注册本字符设备驱动
static int __init tvp5158_i2c_init(void) { int result; dev_t dev = 0; result = alloc_chrdev_region(&dev, 0, 1, “tvp5158_dev”); if (result < 0) { printk(KERN_WARNING "I2C: can't get device major num \n"); return result; } g_tvp5158_dev.major = MAJOR(dev); sema_init(&g_tvp5158_dev.semLock, 1); cdev_init(&g_tvp5158_dev.cdev, &tvp5158_dev_FileOps); g_tvp5158_dev.cdev.owner = THIS_MODULE; g_tvp5158_dev.cdev.ops = &tvp5158_dev_FileOps; cdev_add(&g_tvp5158_dev.cdev, dev, 1); return i2c_add_driver(&tvp5158_i2c_driver);; }