platform设备驱动

kernel/arch/unicore/mach-sep0611/io.c:
    map_desc 数组描述了开始有内核自动建立的虚拟空间到物理地址空间的映射(静态映射)

kernel/arch/unicore/mach-sep0611/devices.c:
    描述了板级支持包,描述了各种资源的物理空间,以及各种platform_device实例的部分内容取值

kernel/arch/unicore/mach-sep0611/mach-tiger-test.c:
    static struct platform_device *devices[] __initdata写明了支持platform架构的设备驱动

以globalfifo作为支持platform设备的驱动程序为例:
    在kernel/arch/unicore/mach-sep0611/mach-tiger-test.c添加
    static struct platform_device globalfifo_device =
    {
        .name = "globalfifo",
            .id = -1,
    };
    在kernel/arch/unicore/mach-sep0611/mach-tiger-test.c的platform_device *数组devices中添加
    &globalfifo_device,  

驱动程序:

#include <linux/module.h>                                                                                          
#include <linux/types.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 <linux/device.h>
#include <linux/poll.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>

#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define GLOBALFIFO_SIZE 0x1000
#define MEM_CLEAR 0x1
#define GLOBALFIFO_MAJOR 234
#define DEVICE_NAME "globalfifo"

static int globalfifo_major = GLOBALFIFO_MAJOR;


struct globalfifo_dev *globalfifo_devp; //设备结构体指针

struct globalfifo_dev
{
    struct cdev cdev;/*cdev结构体*/
    unsigned int current_len; /*fifo有效数据长度*/
    unsigned char mem[GLOBALFIFO_SIZE]; /*全局内存*/
    struct semaphore sem; /*并发控制用的信号量*/
    wait_queue_head_t r_wait; /*阻塞读用的等待队列头*/
    wait_queue_head_t w_wait; /*阻塞写用的等待队列头*/
};


/*globalfifo轮询操作*/
static unsigned int globalfifo_poll(struct file *filp, poll_table *wait)

{
    unsigned int mask = 0;
    struct globalfifo_dev *dev = filp->private_data;/*获得设备结构体指针*/

    down(&dev->sem);

    poll_wait(filp, &dev->r_wait, wait);
    poll_wait(filp, &dev->w_wait, wait);

    /*fifo非空*/
    if(dev->current_len != 0)
    {
        mask |= POLLIN | POLLRDNORM; /*标示数据可获得*/
    }

    /*fifo非满*/
    if(dev->current_len != GLOBALFIFO_SIZE)
    {
        mask |= POLLOUT | POLLWRNORM; /*标示数据可写入*/
    }

    up(&dev->sem);
    return mask;
}

/*globalfifo读函数*/
static ssize_t globalfifo_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
    int ret;
    struct globalfifo_dev *dev = filp->private_data;/*获得设备结构体指针*/
    DECLARE_WAITQUEUE(wait, current);/*定义等待队列*/

    down(&dev->sem);/*获得信号量*/
    add_wait_queue(&dev->r_wait, &wait);/*进入读等待队列*/

    /*等待FIFO非空*/
    while(dev->current_len == 0)
    {
        if(filp->f_flags & O_NONBLOCK)           

        {
            ret = -EAGAIN;
            goto out;
        }

        __set_current_state(TASK_INTERRUPTIBLE);/*改变进程状态为睡眠*/
        up(&dev->sem);
        schedule();/*调度其它进程执行*/

        if(signal_pending(current))
        {
            /*如果是因为信号被唤醒*/
        ret = -ERESTARTSYS;
        goto out2;
        }

        down(&dev->sem);
    }

    /*拷贝到用户空间*/
    if(count > dev->current_len)
    {
        count = dev->current_len;
    }
    if(copy_to_user(buf, (void*)(dev->mem), count))
    {
        ret = -EFAULT;
        goto out;
    }
    else
    {
        memcpy(dev->mem, dev->mem + count, dev->current_len - count);/*fifo数据前移*/
        dev->current_len -= count; /*有效数据长度减小*/
        printk(KERN_INFO "read %d bytes, current_len:%d\n", count, dev->current_len);

        wake_up_interruptible(&dev->w_wait);/*唤醒写等待队列*/

        ret = count;
    }
out:
    up(&dev->sem);/*释放信号*/
out2:
    remove_wait_queue(&dev->r_wait, &wait);/*移除等待队列*/
    set_current_state(TASK_RUNNING);

    return ret;
}

/*globalfifo 写操作*/
static ssize_t globalfifo_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
    struct globalfifo_dev *dev = filp->private_data;/*获得设备结构体指针*/
    int ret;
    DECLARE_WAITQUEUE(wait, current); /*定义等待队列*/

    down(&dev->sem); /*获取信号量*/
    add_wait_queue(&dev->w_wait, &wait); /*进入写等待队列*/

    /*等待FIFO非满*/
    while(dev->current_len == GLOBALFIFO_SIZE)
    {
        if(filp->f_flags & O_NONBLOCK)
        {
            /*如果是非阻塞访问*/
            ret = -EAGAIN;
            goto out;
        }
        __set_current_state(TASK_INTERRUPTIBLE); /*改变进程状态为睡眠*/
        up(&dev->sem);

        schedule();/*调度其它进程执行*/

        if(signal_pending(current))
        {
            /*如果是因为信号唤醒*/
            ret = -ERESTARTSYS;
            goto out2;
        }
        down(&dev->sem);/*获得信号量*/
    }

    /*从用户空间拷贝到内核空间*/
    if(count > GLOBALFIFO_SIZE - dev->current_len)
        count = GLOBALFIFO_SIZE - dev->current_len;

    if(copy_from_user(dev->mem + dev->current_len, buf, count))
    {
        ret = -EFAULT;
        goto out;
    }
    else
    {
        dev ->current_len += count;
        printk(KERN_INFO "written %d bytes, current_len: %d\n", count, dev->current_len);

        wake_up_interruptible(&dev->r_wait);/*唤醒读等待队列*/

        ret = count;
    }

out:
    up(&dev->sem);/*释放信号量*/
out2:
    remove_wait_queue(&dev->w_wait, &wait);
    set_current_state(TASK_RUNNING);
    return ret;
}


static loff_t globalfifo_llseek(struct file *filp, loff_t offset, int orig)
{
    loff_t ret;
    switch(orig)
    {
        case 0:
            if(offset < 0)
            {
                ret = -EINVAL;

                break;
            }
            if(offset > GLOBALFIFO_SIZE)
            {
                ret = -EINVAL;
                break;
            }
            filp->f_pos = (unsigned int)offset;
            ret = filp->f_pos;
            break;
        case 1:
            if((filp->f_pos + offset) > GLOBALFIFO_SIZE)
            {
                ret = -EINVAL;
                break;
            }
            if((filp->f_pos + offset) < 0)
            {
                ret = -EINVAL;
                break;
            }
            filp->f_pos +=offset;
            ret = filp->f_pos;
            break;
        default:
            ret = -EINVAL;
    }
    return ret;
}

static int globalfifo_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
    struct globalfifo_dev *dev = filp->private_data;
    switch(cmd)
    {
        case MEM_CLEAR:
            if(down_interruptible(&dev->sem))
            {
                return -ERESTARTSYS;
            }           

            memset(dev->mem, 0, GLOBALFIFO_SIZE);
            up(&dev->sem);//释放信号量
            printk(KERN_INFO "globalfifo is set to zero\n");
            break;
        default:
            return -EINVAL;
    }
    return 0;
}

int globalfifo_release(struct inode *inode, struct file *filp)
{
    return 0;
}

int globalfifo_open(struct inode *inode, struct file *filp)
{
    filp->private_data = globalfifo_devp;//将设备结构体指针赋值给文件私有数据指针
    return 0;
}

struct file_operations globalfifo_fops =
{
    .owner = THIS_MODULE,
    .llseek = globalfifo_llseek,
    .read = globalfifo_read,
    .write = globalfifo_write,
    .release = globalfifo_release,
    .open = globalfifo_open,
    .ioctl = globalfifo_ioctl,
    .poll = globalfifo_poll,
};


static void globalfifo_setup_cdev(struct globalfifo_dev *dev, int index)
{
    int err;
    int devno = MKDEV(globalfifo_major, index);

    cdev_init(&dev->cdev, &globalfifo_fops);  

    dev->cdev.owner = THIS_MODULE;
    dev->cdev.ops = &globalfifo_fops;//?????
    err = cdev_add(&dev->cdev, devno, 1);

    if(err)
        printk(KERN_NOTICE "Error %d adding LED %d", err, index);
}

static int __devinit globalfifo_probe(struct platform_device *pdev)
{
    printk(KERN_NOTICE "%s\n", __func__);
    int ret;
    dev_t devno = MKDEV(globalfifo_major, 0);

    /*申请设备号*/
    if(globalfifo_major)
    {
        ret = register_chrdev_region(devno, 1, "globalfifo");
    }
    else
    {
        ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo");
        globalfifo_major = MAJOR(devno);
    }

    if(ret < 0)
    {
        return ret;
    }

    /*动态申请设备结构提内存*/
    globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL);
    if(!globalfifo_devp)
    {
        /*申请失败*/
        ret = -ENOMEM;
        goto fail_malloc;
    }

    memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev));

    globalfifo_setup_cdev(globalfifo_devp, 0);
    sema_init(&globalfifo_devp->sem, 1);/*初始化信号量*/
    init_waitqueue_head(&globalfifo_devp->r_wait);//初始化读等待队列
    init_waitqueue_head(&globalfifo_devp->w_wait);//初始化写等待队列头

    return 0;

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


static int __devexit globalfifo_remove(struct platform_device *pdev)
{
    cdev_del(&globalfifo_devp->cdev);//注销cdev
    kfree(globalfifo_devp);//释放设备结构体内存
    unregister_chrdev_region(MKDEV(globalfifo_major, 0), 1);//释放设备号
    return 0;
}

static struct platform_driver globalfifo_device_driver =
{
    .probe = globalfifo_probe,
    .remove = __devexit_p(globalfifo_remove),
    .driver =
            {
                .name = "globalfifo",
                .owner = THIS_MODULE,
            }
};

static int __init globalfifo_init(void)
{
    printk(KERN_NOTICE "%s\n", __func__);
    return platform_driver_register(&globalfifo_device_driver);
}

void __exit globalfifo_exit(void)
{                            

    platform_driver_unregister(&globalfifo_device_driver);
}

module_param(globalfifo_major, int, S_IRUGO);

module_init(globalfifo_init);
module_exit(globalfifo_exit);
MODULE_LICENSE("Dual BSD/GPL");  

    

原文地址:https://www.cnblogs.com/openix/p/2871677.html