日期:2014-05-16 浏览次数:20717 次
struct mcp251x { struct cdev cdev; struct device sdev; struct class *cclass; int dev; /* device number */ } static struct mcp251x *mcp251x_dev; static int mcp251x_count=2; static int __init mcp251x_init(void) { mcp251x_dev = kmalloc(sizeof(struct mcp251x)*mcp251x_count,GFP_KERNEL); for (i=0; i<mcp251x_count; i++) { chip = &mcp251x_dev[i]; alloc_chrdev_region(&chip->dev,/*base minor*/ 0, 1, DRIVER_NAME);//这里是不是重复申请了设备号,应该就一个主设备号啊 chip->cclass = class_create(THIS_MODULE, DRIVER_NAME); cdev_init(&chip->cdev, &mcp251x_fops); chip->cdev.owner = THIS_MODULE; chip->cdev.ops = &mcp251x_fops; ret = cdev_add (&chip->cdev, chip->dev, 1); /* Fail gracefully if need be */ if (ret) printk (KERN_NOTICE "Error %d adding can_lpc2xxx\n", ret); if (device_create(chip->cclass, NULL, MKDEV(MAJOR(chip->dev), i), chip, DRIVER_NAME) == NULL) { printk("err----%s %d \n",__FILE__,__LINE__); } } }