完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 elecfans跑堂 于 2015-8-28 11:44 编辑
/* 参考: * driversblockxd.c * driversblockz2ram.c */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static struct gendisk *ramblock_disk; static request_queue_t *ramblock_queue; static int major; static DEFINE_SPINLOCK(ramblock_lock); #define RAMBLOCK_SIZE (1024*1024) static unsigned char *ramblock_buf; static int ramblock_getgeo(struct block_device *bdev, struct hd_geometry *geo) { /* 容量=heads*cylinders*sectors*512 */ geo->heads = 2; geo->cylinders = 32; geo->sectors = RAMBLOCK_SIZE/2/32/512; return 0; } static struct block_device_operations ramblock_fops = { .owner = THIS_MODULE, .getgeo = ramblock_getgeo, }; static void do_ramblock_request(request_queue_t * q) { static int r_cnt = 0; static int w_cnt = 0; struct request *req; //printk("do_ramblock_request %dn", ++cnt); while ((req = elv_next_request(q)) != NULL) { /* 数据传输三要素: 源,目的,长度 */ /* 源/目的: */ unsigned long offset = req->sector * 512; /* 目的/源: */ // req->buffer /* 长度: */ unsigned long len = req->current_nr_sectors * 512; if (rq_data_dir(req) == READ) { //printk("do_ramblock_request read %dn", ++r_cnt); memcpy(req->buffer, ramblock_buf+offset, len); } else { //printk("do_ramblock_request write %dn", ++w_cnt); memcpy(ramblock_buf+offset, req->buffer, len); } end_request(req, 1); } } static int ramblock_init(void) { /* 1. 分配一个gendisk结构体 */ ramblock_disk = alloc_disk(16); /* 次设备号个数: 分区个数+1 */ /* 2. 设置 */ /* 2.1 分配/设置队列: 提供读写能力 */ ramblock_queue = blk_init_queue(do_ramblock_request, &ramblock_lock); ramblock_disk->queue = ramblock_queue; /* 2.2 设置其他属性: 比如容量 */ major = register_blkdev(0, "ramblock"); /* cat /proc/devices */ ramblock_disk->major = major; ramblock_disk->first_minor = 0; sprintf(ramblock_disk->disk_name, "ramblock"); ramblock_disk->fops = &ramblock_fops; set_capacity(ramblock_disk, RAMBLOCK_SIZE / 512); /* 3. 硬件相关操作 */ ramblock_buf = kzalloc(RAMBLOCK_SIZE, GFP_KERNEL); /* 4. 注册 */ add_disk(ramblock_disk); return 0; } static void ramblock_exit(void) { unregister_blkdev(major, "ramblock"); del_gendisk(ramblock_disk); put_disk(ramblock_disk); blk_cleanup_queue(ramblock_queue); kfree(ramblock_buf); } module_init(ramblock_init); module_exit(ramblock_exit); MODULE_LICENSE("GPL"); |
|
相关推荐
1个回答
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
FacenetPytorch人脸识别方案--基于米尔全志T527开发板
1605 浏览 0 评论
飞凌嵌入式ElfBoard ELF 1板卡-CAN编程示例之开发板测试
1104 浏览 0 评论
该问题是用APP给芯海科技的CST92F25芯片发指令是出现的
4230 浏览 1 评论
889 浏览 0 评论
1753 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-2 14:10 , Processed in 0.550310 second(s), Total 76, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号