simple block driver example-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3977375
  • 博文数量: 536
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(536)

文章存档

2024年(3)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

分类:

2007-04-17 13:59:12

simple block driver example

this is the source for the "simple block driver" example, which is discussed in detail in this article, which is part of the the lwn.net porting drivers to 2.6 series.

/*
 * a sample, extra-simple block driver.
 *
 * 凯发app官方网站 copyright 2003 eklektix, inc. redistributable under the terms
 * of the gnu gpl.
 */


#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h> /* printk() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>

module_license("dual bsd/gpl");
static char *version = "1.3";

static int major_num = 0;
module_param(major_num, int, 0);
static int hardsect_size = 512;
module_param(hardsect_size, int, 0);
static int nsectors = 1024; /* how big the drive is */
module_param(nsectors, int, 0);

/*
 * we can tweak our hardware sector size, but the kernel talks to us
 * in terms of small sectors, always.
 */

#define kernel_sector_size 512

/*
 * our request queue.
 */

static struct request_queue *queue;

/*
 * the internal representation of our device.
 */

static struct sbd_device {
    unsigned long size;
    spinlock_t lock;
    u8 *data;
    struct gendisk *gd;
} device;


/*
 * handle an i/o request.
 */

static void sbd_transfer(struct sbd_device *dev, unsigned long sector,
        unsigned long nsect, char *buffer, int write)
{
    unsigned long offset = sector*hardsect_size;
    unsigned long nbytes = nsect*hardsect_size;
    
    if ((offset nbytes) > dev->size) {
    printk (kern_notice "sbd: 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 sbd_request(request_queue_t *q)
{
    struct request *req;

    while ((req = elv_next_request(q)) != null) {
    if (! blk_fs_request(req)) {
     printk (kern_notice "skip non-cmd request\n");
     end_request(req, 0);
     continue;
    }
    sbd_transfer(&device, req->sector, req->current_nr_sectors,
            req->buffer, rq_data_dir(req));
    end_request(req, 1);
    }
}



/*
 * ioctl.
 */

int sbd_ioctl (struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
    long size;
    struct hd_geometry geo;

    switch(cmd) {
    /*
     * the only command we need to interpret is hdio_getgeo, since
     * we can't partition the drive otherwise. we have no real
     * geometry, of course, so make something up.
     */

     case hdio_getgeo:
        size = device.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 *) arg, &geo, sizeof(geo)))
            return -efault;
        return 0;
    }

    return -enotty; /* unknown command */
}

/*
 * the device operations structure.
 */

static struct block_device_operations sbd_ops = {
    .owner = this_module,
    .ioctl     = sbd_ioctl
};

static int __init sbd_init(void)
{
/*
 * set up our internal device.
 */

    device.size = nsectors*hardsect_size;
    spin_lock_init(&device.lock);
    device.data = vmalloc(device.size);
    if (device.data == null)
    return -enomem;
/*
 * get a request queue.
 */

    queue = blk_init_queue(sbd_request, &device.lock);
    if (queue == null)
     goto out;
    blk_queue_hardsect_size(queue, hardsect_size);
/*
 * get registered.
 */

    major_num = register_blkdev(major_num, "sbd");
    if (major_num <= 0) {
    printk(kern_warning "sbd: unable to get major number\n");
    goto out;
    }
/*
 * and the gendisk structure.
 */

    device.gd = alloc_disk(16);
    if (! device.gd)
    goto out_unregister;
    device.gd->major = major_num;
    device.gd->first_minor = 0;
    device.gd->fops = &sbd_ops;
    device.gd->private_data = &device;
    strcpy (device.gd->disk_name, "sbd0");
    set_capacity(device.gd, nsectors*(hardsect_size/kernel_sector_size));
    device.gd->queue = queue;
    add_disk(device.gd);

    return 0;

  out_unregister:
    unregister_blkdev(major_num, "sbd");
  out:
    vfree(device.data);
    return -enomem;
}

static void __exit sbd_exit(void)
{
    del_gendisk(device.gd);
    put_disk(device.gd);
    unregister_blkdev(major_num, "sbd");
    blk_cleanup_queue(queue);
    vfree(device.data);
}
    
module_init(sbd_init);
module_exit(sbd_exit);

ref:
阅读(3295) | 评论(0) | 转发(0) |
0

上一篇:learn more english

下一篇:printk 与 syslog

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图