linux 3.2.0 vmem_disk驱动源码

9430阅读 0评论2020-02-22 fangdikui
分类:LINUX


点击(此处)折叠或打开

  1. /* 参考:
  2.  * drivers\block\xd.c
  3.  * drivers\block\z2ram.c
  4.  */
  5.  
  6. #include <linux/module.h>
  7. #include <linux/errno.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/mm.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timer.h>
  13. #include <linux/genhd.h>
  14. #include <linux/hdreg.h>
  15. #include <linux/ioport.h>
  16. #include <linux/init.h>
  17. #include <linux/wait.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/blkpg.h>
  20. #include <linux/delay.h>
  21. #include <linux/io.h>
  22.  
  23. #include <asm/system.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/dma.h>
  26.  
  27. static struct gendisk *ramblock_disk;
  28. static struct request_queue *ramblock_queue;
  29.  
  30. static int major;
  31.  
  32. static DEFINE_SPINLOCK(ramblock_lock);//自旋锁
  33.  
  34. #define RAMBLOCK_SIZE (1024*1024)
  35. static unsigned char *ramblock_buf;
  36. //获得几何信息函数,把几何信息保存在geo结构体中。
  37. //为了测试的时候可以使用分区工具fdisk,假装内存也具有一些几何信息
  38. static int ramblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  39. {
  40.     /* 块设备容量(字节为单位)=heads*cylinders*sectors*512 ,其中sector为一环里面的扇区数*/
  41.     geo->heads = 2;    //磁头个数,或面数
  42.     geo->cylinders = 32;    //柱面数或环数
  43.     geo->sectors = RAMBLOCK_SIZE/2/32/512;//一环里面的扇区数,
  44.     //根据:块设备容量(字节为单位)=heads*cylinders*sectors*512,
  45.     //模拟磁盘才可以这样算,实际的磁盘这些信息都是固定的。
  46.     return 0;
  47. }
  48.  
  49.  
  50. static struct block_device_operations ramblock_fops = {
  51.     .owner    = THIS_MODULE,
  52.     .getgeo    = ramblock_getgeo,
  53. };
  54.  
  55. //这里要处理的是保存在request_queue *q 队列里面的一系列读写请求,不是某一次某一个,而是多
  56. //个在这里统一优化,处理。这是块设备特殊的地方,不立即读写,合并为一大块读写等等以提高效率。
  57. static void do_ramblock_request(struct request_queue * q)
  58. {
  59.     struct request *req;

  60.     req = blk_fetch_request(q);
  61.     while (req) {
  62.         unsigned long start = blk_rq_pos(req) << 9;
  63.         unsigned long len = blk_rq_cur_bytes(req);
  64.         int err = 0;

  65.         if (start + len > RAMBLOCK_SIZE) {
  66.             pr_err("ramblock" ": bad access: block=%llu, "
  67.              "count=%u\n",
  68.              (unsigned long long)blk_rq_pos(req),
  69.              blk_rq_cur_sectors(req));
  70.             err = -EIO;
  71.             goto done;
  72.         }
  73.         while (len) {
  74.             //unsigned long addr = start & Z2RAM_CHUNKMASK;
  75.             //unsigned long size = Z2RAM_CHUNKSIZE - addr;
  76.             //if (len < size)
  77.                 //size = len;
  78.             //addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
  79.             if (rq_data_dir(req) == READ)
  80.                 memcpy(req->buffer, ramblock_buf+start, len);
  81.             else
  82.                 memcpy(ramblock_buf+start, req->buffer, len);
  83.             //start += size;
  84.             //len -= size;
  85.             len -= len;
  86.         }
  87.     done:
  88.         if (!__blk_end_request_cur(req, err))
  89.             req = blk_fetch_request(q);
  90.     }


  91. #if 0
  92.     static int r_cnt = 0;
  93.     static int w_cnt = 0;
  94.     struct request *req;
  95.     
  96.     //printk("do_ramblock_request %d\n", ++cnt);
  97.     
  98.     //利用elv_next_request(q)函数取出优化后的一个个request结构体,解析,进行实际读写
  99.     while ((req = blk_fetch_request(q)) != NULL) {
  100.         
  101.         /* 数据传输三要素:,目的,长度 */
  102.         
  103.         /*/目的: */
  104.         //unsigned long offset = req->__sector * 512;

  105.         unsigned long offset =blk_rq_pos(req);
  106.         //req->sector的含义是:当前这个req要从第sector个扇区开始读/写数据。
  107.         //这些东西内核优化队列的时候会设置好,我们拿来用就行
  108.         
  109.         /* 目的/: */    
  110.         // req->buffer
  111.  
  112.         /* 长度: */        
  113.         //unsigned long len = req->current_nr_sectors * 512;    //字节为单位

  114.         unsigned long len = blk_rq_bytes (req);
  115.         
  116.         //利用rq_data_dir(req)函数获得这个req的读写操作标志,分别处理读和写操作。
  117.         if (rq_data_dir(req) == READ)
  118.         {
  119.             //printk("do_ramblock_request read %d\n", ++r_cnt);
  120.             memcpy(req->buffer, ramblock_buf+offset, len);
  121.             //数据从ramblock_buf+offset读到req->buffer
  122.         }
  123.         else
  124.         {
  125.             //printk("do_ramblock_request write %d\n", ++w_cnt);
  126.             memcpy(ramblock_buf+offset, req->buffer, len);
  127.             //数据从req->buffer写到ramblock_buf+offset
  128.         }        
  129.         
  130.         __blk_end_request_all(req, 1);//通知完成当前请求成功,end_request()会将当前请求从请求
  131.                     //队列中剥离
  132.     }
  133. #endif
  134. }
  135.  
  136. static int ramblock_init(void)
  137. {
  138.     /* 1. 分配一个gendisk结构体 */
  139.     ramblock_disk = alloc_disk(16); /* 次设备号个数: 分区个数+1 */
  140.  
  141.     /* 2. 设置 */
  142.     /* 2.1 分配/设置队列 */
  143.     ramblock_queue = blk_init_queue(do_ramblock_request, &ramblock_lock);
  144.     ramblock_disk->queue = ramblock_queue;
  145.     
  146.     /* 2.2 设置其他属性: 比如容量 */
  147.     major = register_blkdev(0, "ramblock"); /* cat /proc/devices */    
  148.     ramblock_disk->major = major;
  149.     ramblock_disk->first_minor = 0;
  150.     sprintf(ramblock_disk->disk_name, "ramblock");
  151.     ramblock_disk->fops = &ramblock_fops;
  152.     set_capacity(ramblock_disk, RAMBLOCK_SIZE / 512);
  153.  
  154.     /* 3. 硬件相关操作 */
  155.     ramblock_buf = kzalloc(RAMBLOCK_SIZE, GFP_KERNEL);
  156.     //ramblock_buf指向块设备起始地址
  157.  
  158.     /* 4. 注册 */
  159.     add_disk(ramblock_disk);
  160.  
  161.     return 0;
  162. }
  163.  
  164. static void ramblock_exit(void)
  165. {
  166.     unregister_blkdev(major, "ramblock");
  167.     del_gendisk(ramblock_disk);
  168.     put_disk(ramblock_disk);
  169.     blk_cleanup_queue(ramblock_queue);
  170.  
  171.     kfree(ramblock_buf);
  172. }
  173.  
  174. module_init(ramblock_init);
  175. module_exit(ramblock_exit);
  176.  
  177. MODULE_LICENSE("GPL");


上一篇:alsa Machine、Platform和Codec 驱动实例源码解析(基于am335x)
下一篇:tty 驱动学习