Linux

 

 

 

 

APIs for Device Driver Development  

 

This page is really a cheatsheet for me. It is not for explaining the details of each APIs. When you are writing a device driver code, you often want to find out which header file defines a specific API you are interested in. I've spent quite a lot of time searching through many header files. Being tired of doing the same thing over and over, I decided to write a cheatsheet to save my time and effort.  

If what you want to find is not in this page, you can manually open several header files or try with How to find header file or c source file containing a specific string ?

 

 

 

linux/module.h

 

 

#define module_init(x)  __initcall(x);

#define module_exit(x)  __exitcall(x);

#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)

#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)

#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)

#define MODULE_VERSION(_version) MODULE_INFO(version, _version)

#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)

 

 

 

linux/fs.h

 

 

static const struct file_operations __fops = {              \

    .owner   = THIS_MODULE,                     \

    .open    = __fops ## _open,                 \

    .release = simple_attr_release,                 \

    .read    = simple_attr_read,                    \

    .write   = simple_attr_write,                   \

    .llseek  = generic_file_llseek,                 \

}

 

 

int simple_attr_open(struct inode *inode, struct file *file,

             int (*get)(void *, u64 *), int (*set)(void *, u64),

             const char *fmt);

int simple_attr_release(struct inode *inode, struct file *file);

ssize_t simple_attr_read(struct file *file, char __user *buf,

             size_t len, loff_t *ppos);

ssize_t simple_attr_write(struct file *file, const char __user *buf,

              size_t len, loff_t *ppos);

 

extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);

extern int register_chrdev_region(dev_t, unsigned, const char *);

extern int __register_chrdev(unsigned int major, unsigned int baseminor,

                 unsigned int count, const char *name,

                 const struct file_operations *fops);

extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,

                unsigned int count, const char *name);

extern void unregister_chrdev_region(dev_t, unsigned);

extern void chrdev_show(struct seq_file *,off_t);

 

static inline int register_chrdev(unsigned int major, const char *name,

                  const struct file_operations *fops)

{

    return __register_chrdev(major, 0, 256, name, fops);

}

 

static inline void unregister_chrdev(unsigned int major, const char *name)

{

    __unregister_chrdev(major, 0, 256, name);

}

 

 

 

linux/device.h

 

 

#define class_create(owner, name)       \

({                      \

    static struct lock_class_key __key; \

    __class_create(owner, name, &__key);    \

})

 

extern struct class * __must_check __class_create(struct module *owner,

                          const char *name,

                          struct lock_class_key *key);

 

struct device *device_create(struct class *cls, struct device *parent,

                 dev_t devt, void *drvdata,

                 const char *fmt, ...);

 

 

 

linux/cdev.h

 

 

struct cdev {

    struct kobject kobj;

    struct module *owner;

    const struct file_operations *ops;

    struct list_head list;

    dev_t dev;

    unsigned int count;

};

 

void cdev_init(struct cdev *, const struct file_operations *);

 

struct cdev *cdev_alloc(void);

 

void cdev_put(struct cdev *p);

 

int cdev_add(struct cdev *, dev_t, unsigned);

 

void cdev_del(struct cdev *);

 

void cd_forget(struct inode *);

 

 

 

Reference :

 

[1] The Linux driver implementer’s API guide