Tiny210块设备驱动之内存模拟磁盘

发布者:电竞狂人最新更新时间:2018-10-12 来源: eefocus关键字:Tiny210  块设备驱动  内存模拟磁盘 手机看文章 扫描二维码
随时随地手机看文章

ramblock.c驱动源码:

#include "linux/module.h"

#include "linux/errno.h"

#include "linux/interrupt.h"

#include "linux/mm.h"

#include "linux/fs.h"

#include "linux/kernel.h"

#include "linux/timer.h"

#include "linux/genhd.h"

#include "linux/hdreg.h"

#include "linux/ioport.h"

#include "linux/init.h"

#include "linux/wait.h"

#include "linux/blkdev.h"

#include "linux/blkpg.h"

#include "linux/delay.h"

#include "linux/io.h"

#include "linux/gfp.h"

#include "linux/slab.h"

#include "asm/system.h"

#include "asm/uaccess.h"

#include "asm/dma.h"

#define RAMDISK_SIZE (1024*1024)

static DEFINE_SPINLOCK(ramdisk_lock);

static const struct block_device_operations ramdisk_fops = {

    .owner    = THIS_MODULE,

};

static struct gendisk *ramblock_disk;

static struct request_queue *ramblock_queue;

static unsigned char *ram_buff;

static int major;

static void ramblock_do_request(struct request_queue * q)

{

    struct request *req;

    printk("do:ramblock_do_request\n");

    req = blk_fetch_request(q);

    while (req) 

    {

        //源或目的

        unsigned long offset = blk_rq_pos(req) * 512;

        //目的或源

        //req->buffer

        

        //长度

        unsigned long len  = blk_rq_cur_bytes(req);

        if (rq_data_dir(req) == READ)

            memcpy(req->buffer, ram_buff+offset, len);

        else

            memcpy(ram_buff+offset, req->buffer, len);

        // wrap up, 0 = success, -errno = fail 

        if (!__blk_end_request_cur(req, 0))

            req = blk_fetch_request(q);

    }

}

static int ramblock_init(void)

{

    //获得主设备号

    major = register_blkdev(major, "ramdisk");

    //1.分配一个gendisk结构体

    ramblock_disk = alloc_disk(16);

    

    //2.设置

    //2.1设置一个请求队列

    ramblock_queue = blk_init_queue(ramblock_do_request, &ramdisk_lock);

    ramblock_disk->queue = ramblock_queue;

    //2.2设置其他信息

    ramblock_disk->major = major;

    ramblock_disk->first_minor = 0;

    sprintf(ramblock_disk->disk_name, "ramdisk");

    ramblock_disk->fops = &ramdisk_fops;

    set_capacity(ramblock_disk, RAMDISK_SIZE);

    //3.硬件相关

    ram_buff = kzalloc(RAMDISK_SIZE, GFP_KERNEL);

    //4.注册

    add_disk(ramblock_disk);

    return 0;

}

static void ramblock_exit(void)

{

    unregister_blkdev(major, "ramdisk");

    del_gendisk(ramblock_disk);

    put_disk(ramblock_disk);

    blk_cleanup_queue(ramblock_queue);

    kfree(ram_buff);

}

module_init(ramblock_init);

module_exit(ramblock_exit);

MODULE_LICENSE("GPL");


关键字:Tiny210  块设备驱动  内存模拟磁盘 引用地址:Tiny210块设备驱动之内存模拟磁盘

上一篇:Tiny210驱动之NAND_FLASH驱动程序
下一篇:Tiny210驱动之USB设备驱动程序

推荐阅读最新更新时间:2024-03-16 16:15

Tiny210驱动之LED测试
first_drv.c驱动源码: #include linux/device.h #include linux/module.h #include linux/kernel.h #include linux/fs.h #include linux/init.h #include linux/delay.h #include asm/uaccess.h #include asm/irq.h #include asm/io.h static struct class *firstdrv_class; volatile unsigned long *gpj2con = NULL; volatile unsigned l
[单片机]
Tiny210裸机之nand_flash操作
start.S源码: .global _start _start: ldr sp, =0xD0030000 @初始化堆栈 b main //跳到main ==================================================================== main.c源码: #include command.h #include clock.h #include led.h #include uart.h #include lib.h #include nand.h #define CFG_PROMPT WY_BOOT # // Monitor Command Pr
[单片机]
OK6410设备驱动内存模拟nor_flash
ram_nor.c源码: //参考: drivers\mtd\chips\map_ram.c #include linux/module.h #include linux/types.h #include linux/kernel.h #include linux/sched.h #include linux/init.h #include asm/io.h #include asm/byteorder.h #include linux/errno.h #include linux/slab.h #include linux/delay.h #include linux/interrupt.h #inclu
[单片机]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved