Linux塊裝置方式的nvram驅動例項

2021-07-28 22:08:54 字數 3629 閱讀 4452

一般可以通過字元裝置驅動的方式訪問nvram,在這裡,通過塊裝置驅動方式去驅動nvram,以了解和熟悉塊裝置驅動編寫。

測試系統:fedora7

nvram總可接大小:8k x 64頁 x 8片 = 4 m 位元組

測試硬體:intel板卡

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define nvram_name "board_nvram"

#define nvram_size 0x80000

#define nvram_blocksize 512

struct nvram_dev_t;

struct nvram_dev_t nvram_dev;

static

int hardsect=nvram_blocksize;

static

int major=224;

static

int minor = 0;

static

int iobaadr = 0x300; //板卡io空間基位址

static

unsigned

long membaadr=0xdc000; //記憶體空間基位址

static

unsigned

long mem_len=8192; //頁對映長度bytes

char* preg;

void selectpage(unsigned page)

static

void nvram_transfer(struct nvram_dev_t *dev, unsigned

long sector, unsigned

long nsect, char *buffer, int write)

offset=(sector * hardsect) & (mem_len-1);

index= (sector* hardsect)>>13;

rdptr = preg+ offset;

r_size=nbytes;

if(r_size>(mem_len-offset))else

time=1;

if (write) //write

while(time)

else

//read

while(time)

}static

int nvram_xfer_bio(struct nvram_dev_t *dev, struct bio *bio)

return

0; /* always "succeed" */

}static

int nvram_make_request(request_queue_t *queue,struct bio *bio)

int nvram_open(struct inode *inode, struct file *filp)

int nvram_release(struct inode *inode, struct file *filp)

int nvram_ioctl (struct inode *inode, struct file *filp,

unsigned

int cmd, unsigned

long arg)

return -enotty; /* unknown command */

}struct block_device_operations nvram_bdops =;

static

void __exit nvram_exit(void)

if (nvram_dev.queue)

unregister_blkdev(major, "nvram");

unregister_chrdev_region(mkdev(major,minor), 1);

release_region(iobaadr, 13);

release_mem_region(membaadr, mem_len);

iounmap(preg);

printk("board nvram driver unloaded\n");

}static

int nvram_init(void)

nvram_dev.disk= alloc_disk(1);

if (!nvram_dev.disk)

nvram_dev.queue= blk_alloc_queue(gfp_kernel);

if (!nvram_dev.queue)

if (register_blkdev(major, "nvram"))

if (null == request_region(iobaadr, 13, nvram_name))

if (null == request_mem_region(membaadr, mem_len, nvram_name))

preg= ioremap(membaadr,mem_len);

disk= nvram_dev.disk;

blk_queue_make_request(nvram_dev.queue,&nvram_make_request);

blk_queue_hardsect_size(nvram_dev.queue, hardsect);

/* rd_size is given in kb */

disk->major = major;

disk->first_minor = 0;

disk->fops = &nvram_bdops;

disk->queue = nvram_dev.queue;

sprintf(disk->disk_name, "nvram%d", 0);

set_capacity(disk, 1024);

add_disk(disk);

outb_p(0x55,iobaadr+9); //nvram 片選使能

printk("nvram is initialized\n");

return

0; out:

unregister_chrdev_region(devno, 1);

put_disk(nvram_dev.disk);

blk_cleanup_queue(nvram_dev.queue);

return err;

}module_init(nvram_init);

module_exit(nvram_exit);

module_license("dual bsd/gpl");

測試:

insmod board_nvram.ko

mkfs.ext2 /dev/nvram0

mount /dev/nvram0 /mnt

linux的塊裝置層

ll rw block 是塊裝置驅動層,向上提供按block讀寫塊裝置到某個記憶體位址的 是以page為目標單位 方法 bread 是塊裝置緩衝層,磁碟上的block用頁快取 先從這個快取裡找,找不到再呼叫ll rw block 讀進來 各個檔案系統,要向vfs層提供read inode 這樣的介面...

Linux塊裝置驅動

塊裝置提供塊裝置提供裝置的訪問,裝置的訪問,可以隨機的以固定大小的塊傳輸資料,例如我們最為常見的磁碟裝置,當然塊裝置和字元裝置有較大差別,塊裝置有自己的驅動介面。簡單來說,核心決定乙個塊是固定的4096 位元組,當然該值可以隨著依賴檔案系統的變化而改變。塊裝置驅動採用register blkdev向...

Linux塊裝置驅動之記憶體模擬塊裝置

用記憶體代替塊裝置的總結,相對來簡單得多,對記憶體操作想必大家都很熟悉,直接分配一塊記憶體就可以直接讀寫操作了 參考 drivers block xd.c drivers block z2ram.c define ramblock size 1024 1024 乙個扇區是512位元組 static ...