linux 块设备驱动 摘要

来源:互联网 发布:python 远程 编辑:程序博客网 时间:2024/05/17 07:12

摘自  Linux 设备驱动 Edition 3

建立一个块设备驱动的步骤及所需要实现的函数、需要初始化的结构体


Registration

Block Driver Registration

/*

 be used to obtain a major number, it does not make any
disk drives available to the system.

*/

int register_blkdev(unsigned int major, const char *name)

/* canceling a block driver registration */
int unregister_blkdev(unsigned int major, const char *name)


Disk Registration


Block device operations

/*

Functions that work just like their char driver equivalents; they are called whenever the device is opened and closed. Ablock driver might respond to an open
call by spinning up the device, locking the door (for removable media), etc. If
you lock media into the device, you should certainly unlock it in the release
method.

*/

int (*open)(struct inode *inode, struct file *filp)
int (*release)(struct inode *inode, struct file *filp)


/*

Method that implements the ioctl system call. The block layer first intercepts a
large number of standard requests, however; so most block driver ioctl methods
are fairly short.

*/
int (*ioctl)(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)




The gendisk structure

struct gendisk {
int major; /* major number of driver */
int first_minor;
int minors;                     /* maximum number of minors, =1 for
                                         * disks that can't be partitioned. */
char disk_name[32];/* name of major driver */
struct hd_struct **part;/* [indexed by minor] */
struct block_device_operations *fops;
struct request_queue *queue;
void *private_data;
sector_t capacity;

int flags;
char devfs_name[64];/* devfs crap */
int number; /* more of the same */
struct device *driverfs_dev;
struct kobject kobj;

struct timer_rand_state *random;
int policy;

atomic_t sync_io;/* RAID */
unsigned long stamp;
int in_flight;
#ifdef CONFIG_SMP
struct disk_stats *dkstats;
#else
struct disk_stats dkstats;
#endif
};

/*

requires special kernel
manipulation to initialize gendisk

*/

struct gendisk *alloc_disk(int minors);

/*

call add_disk until your driver
is completely initialized and ready to respond to requests on that disk.

*/

void add_disk(struct gendisk *gd);



例子


定义块设备
struct sbull_dev {
        int size;                       /* Device size in sectors */
        u8 *data;                       /* The data array */
        short users;                    /* How many users */
        short media_change;             /* Flag a media change? */
        spinlock_t lock;                /* For mutual exclusion */
        struct request_queue *queue;    /* The device request queue */
        struct gendisk *gd;             /* The gendisk structure */
        struct timer_list timer;        /* For simulated media changes */
}

定义对块设备的操作

static int sbull_open(struct inode *inode, struct file *filp)
{
    struct sbull_dev *dev = inode->i_bdev->bd_disk->private_data;
    del_timer_sync(&dev->timer);
    filp->private_data = dev;
    spin_lock(&dev->lock);
    if (! dev->users)
        check_disk_change(inode->i_bdev);
    dev->users++;
    spin_unlock(&dev->lock);
    return 0;

}


static int sbull_release(struct inode *inode, struct file *filp)
{
    struct sbull_dev *dev = inode->i_bdev->bd_disk->private_data;
    spin_lock(&dev->lock);
    dev->users--;
    if (!dev->users) {
        dev->timer.expires = jiffies + INVALIDATE_DELAY;
        add_timer(&dev->timer);
    }
    spin_unlock(&dev->lock);
    return 0;

}

int sbull_ioctl (struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
    long size;
    struct hd_geometry geo;
    struct sbull_dev *dev = filp->private_data;
    switch(cmd) {
        case HDIO_GETGEO:
        /*
         * Get geometry: since we are a virtual device, we have to make
         * up something plausible.  So we claim 16 sectors, four heads,
         * and calculate the corresponding number of cylinders.  We set the
         * start of data at sector four.
         */
        size = dev->size*(hardsect_size/KERNEL_SECTOR_SIZE);
        geo.cylinders = (size & ~0x3f) >> 6;
        geo.heads = 4;
        geo.sectors = 16;
        geo.start = 4;
        if (copy_to_user((void __user *) arg, &geo, sizeof(geo)))
            return -EFAULT;

        return 0;
    }
    return -ENOTTY; /* unknown command */


处理request的方法



static void sbull_transfer(struct sbull_dev *dev, unsigned long sector,
        unsigned long nsect, char *buffer, int write)
{
    unsigned long offset = sector*KERNEL_SECTOR_SIZE;
    unsigned long nbytes = nsect*KERNEL_SECTOR_SIZE;
    if ((offset + nbytes) > dev->size) {
        printk (KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes);
        return;
    }
    if (write)
        memcpy(dev->data + offset, buffer, nbytes);
    else
        memcpy(buffer, dev->data + offset, nbytes);

}


static void sbull_request(request_queue_t *q)
{
    struct request *req;
    while ((req = elv_next_request(q)) != NULL) {
        struct sbull_dev *dev = req->rq_disk->private_data;
        if (! blk_fs_request(req)) {

            printk (KERN_NOTICE "Skip non-fs request\n");
            end_request(req, 0);
            continue;
        }
        sbull_transfer(dev, req->sector, req->current_nr_sectors,
                req->buffer, rq_data_dir(req));
        end_request(req, 1);

}






sbull_major = register_blkdev(sbull_major, "sbull");

if (sbull_major <= 0) {

    printk(KERN_WARNING "sbull: unable to get major number\n");
    return -EBUSY;
 }

sbull_dev *dev;

memset (dev, 0, sizeof (struct sbull_dev));
dev->size = nsectors*hardsect_size;
dev->data = vmalloc(dev->size);
if (dev->data = = NULL) {
    printk (KERN_NOTICE "vmalloc failure.\n");
    return;
}
spin_lock_init(&dev->lock);

dev->queue = blk_init_queue(sbull_request, &dev->lock);

dev->gd = alloc_disk(SBULL_MINORS);
if (! dev->gd) {
    printk (KERN_NOTICE "alloc_disk failure\n");
    goto out_vfree;
}
dev->gd->major = sbull_major;

dev->gd->first_minor = which*SBULL_MINORS;
dev->gd->fops = &sbull_ops;
dev->gd->queue = dev->queue;
dev->gd->private_data = dev;
snprintf (dev->gd->disk_name, 32, "sbull%c", which + 'a');
set_capacity(dev->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE));
add_disk(dev->gd);


原创粉丝点击