• 字符设备驱动


    2.4内核

    static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
    static inline void unregister_chrdev(unsigned int major, const char *name)

    2.6内核

    #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
    #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
    #define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi)) //将主设备号和次设备号转换成dev_t类型
    
    int register_chrdev_region(dev_t from, unsigned count, const char *name) //注册设备号
    int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
    void unregister_chrdev_region(dev_t from, unsigned count)
    
    struct cdev *cdev_alloc(void)
    void cdev_init(struct cdev *cdev, const struct file_operations *fops) //初始化
    int cdev_add(struct cdev *p, dev_t dev, unsigned count) //添加到内核
    void cdev_del(struct cdev *p) //删除

    手动创建设备节点

    # cat /proc/devices //查看主设备号
    # mknod /dev/设备名 c|b(c是字符设备,b是块设备) 主设备号 次设备号 //创建
    # rm /dev/char //删除

    借助class创建设备节点
    从linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代

    #define class_create(owner, name)
    void class_destroy(struct class *cls)
    struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...)
    void device_destroy(struct class *class, dev_t devt)

    创建多个设备

    struct cdev *cdev;
    
    cdev = kzalloc(sizeof(struct cdev) * COUNT, GFP_KERNEL);
    for(i = 0; i < COUNT; i++)
    {
        cdev_init(&cdev[i], &fops);
        cdev_add(&cdev[i], dev+i, 1);
    }
    
    for(i = 0; i < COUNT; i++)
    {
        cdev_del(&cdev[i]);
    }
  • 相关阅读:
    P2824 [HEOI2016/TJOI2016]排序
    P4930「FJ2014集训」采药人的路径
    FlexboxLayout 的一些基本介绍与基本用法
    Android——打造万能适配器(CommonAdapter)
    关于list、set、map的几点总结
    equals()和hashCode()区别?
    Android开发艺术探索(一)——Activity的生命周期和启动模式
    Android开发艺术探索(三)——View的事件体系
    Android五大布局重新回顾
    Android基础知识回顾
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709802.html
Copyright © 2020-2023  润新知