• linux 驱动入门2


    不吃苦中苦,难为人上人。努力,给老婆孩子提供个良好的生活居住环境。

    http://www.cnblogs.com/nan-jing/articles/5775038.html

    这里提到。有这么多牛人。如果比得上人家。自古华山一条路,没别的招,不断的理解,不断的读,不断的写。哎。白了头。
    阳痿早泄性无能,尿频尿急尿不净。钱不没挣多少。一身病。

    上文可以完整的通过mknod创了设备节点。哪我如何知道设备号呢。
    通过cat /proc/devices可以看到你创建设备的主设备号。好,没问题,可以读写了,哪我也没看到linux里有多少mknod啊。莫非是能自动创建设备节点? 哎,是的。可以在驱动代码里创建设备节点。

    这句话不完全对。是可以在驱动里写代码。让用户空间的应用创建设备节点。哪个应用? mdev. 哪我如何通知mdev创建设备节点呢?
    用着两句
    class_create();
    device_create();

    注意测试环境,我的环境virtualbox + ubuntu16.04
    好了。看代码。把上面的代码修改下。具体就修改一个函数就好了。
    下面把我的测试代码发出来把
    #include<linux/module.h>
    #include <linux/fs.h>
    #include <linux/errno.h>
    #include <linux/mm.h>
    #include <linux/sched.h>
    #include <linux/init.h>
    #include <linux/cdev.h>
    #include <asm/io.h>
    //#include <asm/system.h>
    #include <asm/uaccess.h>
    #include <linux/slab.h>
    #include <linux/device.h>

    MODULE_AUTHOR("qcom.shanghai@gmail.com");
    MODULE_LICENSE("Dual BSD/GPL");

    #define GLOBALMEM_SIZE 0X1000
    #define MEM_CLEAR 0x1

    static int globalmem_major = 0;

    struct globalmem_dev{
    struct cdev cdev;
    unsigned char mem[GLOBALMEM_SIZE];
    };

    struct globalmem_dev *globalmem_devp;

    struct class *globalmem_class;

    static int globalmem_open(struct inode *inode, struct file *filp){
    struct globalmem_dev *dev;
    dev = container_of(inode->i_cdev, struct globalmem_dev, cdev);
    filp->private_data = dev;
    printk(KERN_ALERT "globalmem_open!");
    return 0;
    }

    static int globalmem_release(struct inode *inode, struct file *filp){
    printk(KERN_ALERT "globalmem_release!");
    return 0;
    }

    static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos){
    unsigned long p = *ppos;
    unsigned int count = size;
    int ret = 0;
    struct globalmem_dev *dev = filp->private_data;

    if (p >= GLOBALMEM_SIZE){
    return 0;
    }
    if (count > GLOBALMEM_SIZE - p){

    //honeywell 臧春杰
    count = GLOBALMEM_SIZE - p;
    }

    if (copy_to_user(buf, (void *)(dev->mem + p), count)){
    ret = - EFAULT;
    }else {
    *ppos += count;
    ret = count;
    printk(KERN_ALERT "read %u bytes from %lu ", count, p);
    }
    return ret;
    }

    static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos){
    unsigned long p = *ppos;
    unsigned int count = size;

    int ret = 0;
    struct globalmem_dev *dev = filp->private_data;

    if (p >= GLOBALMEM_SIZE){
    return 0;
    }

    if (count > GLOBALMEM_SIZE - p){
    count = GLOBALMEM_SIZE - p;
    }
    if (copy_from_user(dev->mem + p, buf, count)){
    ret = - EFAULT;
    }else {
    *ppos += count;
    ret = count;
    printk(KERN_ALERT "write %u bytes from %lu ", count, p);
    }

    return ret;
    }

    static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig){
    loff_t ret = 0;
    switch(orig){
    case 0:
    if (offset < 0){
    ret = - EFAULT;
    break;
    }
    if ((unsigned int)offset > GLOBALMEM_SIZE){
    ret = - EFAULT;
    break;
    }
    filp->f_pos = (unsigned int) offset;
    ret = filp->f_pos;
    break;

    case 1:
    if ((filp->f_pos + offset) > GLOBALMEM_SIZE){
    ret = - EFAULT;
    break;
    }

    if ((filp->f_pos + offset) < 0){
    ret = - EFAULT;
    break;
    }
    filp->f_pos += offset;
    ret = filp->f_pos;
    break;

    default:
    ret = - EFAULT;
    break;
    }

    return ret;

    }

    static long globalmem_ioctl(struct file *flip, unsigned int cmd, unsigned long arg){
    printk(KERN_ALERT "globalmem_ioctl");
    return 10L;
    }

    static const struct file_operations globalmem_fops = {
    .owner = THIS_MODULE,
    .llseek = globalmem_llseek,
    .read = globalmem_read,
    .write = globalmem_write,
    .unlocked_ioctl = globalmem_ioctl,
    .open = globalmem_open,
    .release = globalmem_release,
    };

    static void globalmem_setup_cdev(struct globalmem_dev *dev, int index){
    int err, devno = MKDEV(globalmem_major, index);
    cdev_init(&(dev->cdev), &globalmem_fops);
    dev->cdev.owner = THIS_MODULE;
    err = cdev_add(&dev->cdev, devno, 1);
    if (err){
    printk(KERN_ALERT"cdev_add failed!");
    }
    }

    //honeywell 臧春杰
    static void create_dev_node(int index){
    device_create(globalmem_class, NULL, MKDEV(globalmem_major, index), NULL, "globalmem_cj1");

    //honeywell 臧春杰
    device_create(globalmem_class, NULL, MKDEV(globalmem_major, index + 1), NULL, "globalmem_cj2");
    }

    static int __init globalmem_init(void){
    int result;
    dev_t devno = MKDEV(globalmem_major, 0);
    if (globalmem_major){
    result = register_chrdev_region(devno, 2, "globalmem");
    }else {
    result = alloc_chrdev_region(&devno,0, 2, "globalmem");
    globalmem_major = MAJOR(devno);
    }
    printk(KERN_ALERT "major-------------->%d ", globalmem_major);
    if (result < 0){
    return result;
    }

    //honeywell 臧春杰

    globalmem_class = class_create(THIS_MODULE, "globalmem_cls");
    create_dev_node(0);

    globalmem_devp = kmalloc(2 * sizeof(struct globalmem_dev), GFP_KERNEL);
    if (! globalmem_devp){
    result = -ENOMEM;
    goto fail_malloc;
    }
    memset(globalmem_devp, 0, 2 * sizeof(struct globalmem_dev));
    globalmem_setup_cdev(&globalmem_devp[0], 0);
    globalmem_setup_cdev(&globalmem_devp[1], 1);

    return 0;

    fail_malloc:
    unregister_chrdev_region(devno, 1);
    return result;
    }


    static void __exit globalmem_exit(void){
    cdev_del(&globalmem_devp[0].cdev);

    cdev_del(&globalmem_devp[1].cdev);
    kfree(globalmem_devp);
    unregister_chrdev_region(MKDEV(globalmem_major, 0), 2);
    class_destroy(globalmem_class);
    }


    module_param(globalmem_major, int, S_IRUGO);

    //honeywell 臧春杰
    module_init(globalmem_init);
    module_exit(globalmem_exit);

     

    这个代码我在ubuntu16.04上编译测试过。

    结果如下


    ubuntu@ubuntu-VirtualBox:~/driver/test3$ ls -l /dev/ | grep global
    crw------- 1 root root 247, 0 8月 25 13:14 globalmem_cj1
    crw------- 1 root root 247, 1 8月 25 13:14 globalmem_cj2

    可以直接拷贝,写个makefile编译,后续我再加点自动创建设备节点。就不用自己mknod了。

  • 相关阅读:
    Linux内核驱动--硬件访问I/O【原创】
    Linux内核驱动--mmap设备方法【原创】
    Linux系统调用的运行过程【转】
    蓝牙Bluetooth技术手册规范下载【转】
    FarBox--另类有趣的网站服务【转】
    蓝牙HID协议笔记【转】
    linux 串口0x03,0x13的问题【转】
    CC254x/CC2540/CC2541库函数速查(转)
    BLE获取iphone mac地址的方法--【原创】
    用secureCRT操作ubuntu终端
  • 原文地址:https://www.cnblogs.com/nan-jing/p/5806399.html
Copyright © 2020-2023  润新知