Device Registration in scull

Internally, scull struct scull_dev use a type of structure that represents each device structure is defined as:

 

struct scull_dev {

struct scull_qset *data;  /* Pointer to first quantum set */ int quantum;              /* the current quantum size */

int qset;  /* the current array size */

unsigned long size;  /* amount of data stored here */

unsigned int access_key;  /* used by sculluid and scullpriv */ struct semaphore sem;         /* mutual exclusion semaphore  */ struct cdev cdev; /* Char device structure */

};

 

We discuss them in the face of various members of the structure, but now we focus on cdev, our struct cdev kernel interface device and this structure must be initialized and added to the system described above; processing this task scull Code Yes:

 

static void scull_setup_cdev(struct scull_dev *dev, int index)

{

int err, devno = MKDEV(scull_major, scull_minor + index);

 

cdev_init(&dev->cdev, &scull_fops); dev->cdev.owner = THIS_MODULE;

dev->cdev.ops = &scull_fops;

err = cdev_add (& dev-> cdev, devno, 1);

 

 

 

/* Fail gracefully if need be */ if (err)

printk(KERN_NOTICE "Error %d adding scull%d", err, index);

}

 

Because cdev structure embedded in the struct scull_dev inside, cdev_init must be called to initialize the structure.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11106330.html