linux 下块设备驱动开发学习笔记 2(sbull驱动分析)

【Linux】LDD3中sbull源码编译问题梳理 LDD3,linux设备驱动第三版用的内核是2.6.10,在编译的时候我用的是2.6.32 2.6.32相比2.6.10在块设备驱动上,简直是动了大手术,编译基本满满错误,这里梳理一下我遇到全部的错误 很多都是网上找的资料,只有小部分是我写的: 1、包含文件 错误:没有#include 改法:删掉 2、整一个块设备驱动的struct block_device_operation 阅读详情

//这个驱动是针对gendisk下的buff来实现的,没有用到bio

#include <linux/config.h>

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>  //包含头文件


#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/timer.h>
#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/hdreg.h> /* HDIO_GETGEO */
#include <linux/kdev_t.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h> /* invalidate_bdev */
#include <linux/bio.h>


MODULE_LICENSE("Dual BSD/GPL");
static int sbull_major = 0;  //主设备号
module_param(sbull_major, int, 0);  //模参数,这个主设备号可以在加载模块时指定一个值
static int hardsect_size = 512;//一个sector大小
module_param(hardsect_size, int, 0);//同上
static int nsectors = 1024; /* How big the drive is */
module_param(nsectors, int, 0);
static int ndevices = 4;
module_param(ndevices, int, 0);
/*
 * The different "request modes" we can use.
 */
enum {
RM_SIMPLE  = 0,/* The extra-simple request function */
RM_FULL    = 1,/* The full-blown version */
RM_NOQUEUE = 2,/* Use make_request */
};
static int request_mode = RM_SIMPLE;
module_param(request_mode, int, 0);
/*
 * Minor number and partition management.
 */
#define SBULL_MINORS 16
#define MINOR_SHIFT 4
#define DEVNUM(kdevnum) (MINOR(kdev_t_to_nr(kdevnum)) >> MINOR_SHIFT

#define KERNEL_SECTOR_SIZE 512

#define INVALIDATE_DELAY 30*HZ


/*
 * The internal representation of our device.
 */
struct sbull_dev {  //用户自定义块设备结构体
        int size;                       /* Device size in sectors */
        u8 *data;                       /* The data array */
        short users;                    /* How many users */
        short media_change;             /* Flag a media change? */
        spinlock_t lock;                /* For mutual exclusion */
        struct request_queue *queue;    /* The device request queue */
        struct gendisk *gd;             /* The gendisk structure */
        struct timer_list timer;        /* For simulated media changes */
};


static struct sbull_dev *Devices = NULL;


/*
 * Handle an I/O request.
 */
static void sbull_transfer(struct sbull_dev *dev, unsigned long sector,unsigned long nsect, char *buffer, int write)
{
unsigned long offset = sector*KERNEL_SECTOR_SIZE;
unsigned long nbytes = nsect*KERNEL_SECTOR_SIZE;


if ((offset + nbytes) > dev->size) {
printk (KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes);
return;
}
if (write)
memcpy(dev->data + offset, buffer, nbytes);
else
memcpy(buffer, dev->data + offset, nbytes);
}

/*
 * The simple form of the request function.
 */
static void sbull_request(request_queue_t *q)//当上面有请求时,会调用这个函数
{
struct request *req;


while ((req = elv_next_request(q)) != NULL) {//编离这个请求队列,找出一个可用的请求
struct sbull_dev *dev = req->rq_disk->private_data;
if (! blk_fs_request(req)) {//这个请求是 不是一个文件系统的请求
printk (KERN_NOTICE "Skip non-fs request\n");
end_request(req, 0);//如果不是,就结束这个请求
continue;
}
    //     printk (KERN_NOTICE "Req dev %d dir %ld sec %ld, nr %d f %lx\n",
    //     dev - Devices, rq_data_dir(req),
    //     req->sector, req->current_nr_sectors,
    //     req->flags);
sbull_transfer(dev, req->sector, req->current_nr_sectors,
req->buffer, rq_data_dir(req));//进行数据的复制
end_request(req, 1);
}
}




/*
 * Transfer a single BIO.
 */
static int sbull_xfer_bio(struct sbull_dev *dev, struct bio *bio)
{
int i;
struct bio_vec *bvec;
sector_t sector = bio->bi_sector;


/* Do each segment independently. */
bio_for_each_segment(bvec, bio, i) {
char *buffer = __bio_kmap_atomic(bio, i, KM_USER0);
sbull_transfer(dev, sector, bio_cur_sectors(bio),
buffer, bio_data_dir(bio) == WRITE);
sector += bio_cur_sectors(bio);
__bio_kunmap_atomic(bio, KM_USER0);
}
return 0; /* Always "succeed" */
}


/*
 * Transfer a full request.
 */
static int sbull_xfer_request(struct sbull_dev *dev, struct request *req)
{
struct bio *bio;
int nsect = 0;
    
rq_for_each_bio(bio, req) {
sbull_xfer_bio(dev, bio);
nsect += bio->bi_size/KERNEL_SECTOR_SIZE;
}
return nsect;
}






/*
 * Smarter request function that "handles clustering".
 */
static void sbull_full_request(request_queue_t *q)
{
struct request *req;
int sectors_xferred;
struct sbull_dev *dev = q->queuedata;


while ((req = elv_next_request(q)) != NULL) {
if (! blk_fs_request(req)) {
printk (KERN_NOTICE "Skip non-fs request\n");
end_request(req, 0);
continue;
}
sectors_xferred = sbull_xfer_request(dev, req);
if (! end_that_request_first(req, 1, sectors_xferred)) {
blkdev_dequeue_request(req);
end_that_request_last(req);
}
}
}






/*
 * The direct make request version.
 */
static int sbull_make_request(request_queue_t *q, struct bio *bio)
{
struct sbull_dev *dev = q->queuedata;
int status;


status = sbull_xfer_bio(dev, bio);
bio_endio(bio, bio->bi_size, status);
return 0;
}




/*
 * Open and close.
 */


static int sbull_open(struct inode *inode, struct file *filp)
{
struct sbull_dev *dev = inode->i_bdev->bd_disk->private_data;


del_timer_sync(&dev->timer);
filp->private_data = dev;
spin_lock(&dev->lock);
if (! dev->users) 
check_disk_change(inode->i_bdev);
dev->users++;
spin_unlock(&dev->lock);
return 0;
}


static int sbull_release(struct inode *inode, struct file *filp)
{
struct sbull_dev *dev = inode->i_bdev->bd_disk->private_data;


spin_lock(&dev->lock);
dev->users--;


if (!dev->users) {
dev->timer.expires = jiffies + INVALIDATE_DELAY;
add_timer(&dev->timer);
}
spin_unlock(&dev->lock);


return 0;
}


/*
 * Look for a (simulated) media change.
 */
int sbull_media_changed(struct gendisk *gd)
{
struct sbull_dev *dev = gd->private_data;

return dev->media_change;
}


/*
 * Revalidate.  WE DO NOT TAKE THE LOCK HERE, for fear of deadlocking
 * with open.  That needs to be reevaluated.
 */
int sbull_revalidate(struct gendisk *gd)
{
struct sbull_dev *dev = gd->private_data;

if (dev->media_change) {
dev->media_change = 0;
memset (dev->data, 0, dev->size);
}
return 0;
}


/*
 * The "invalidate" function runs out of the device timer; it sets
 * a flag to simulate the removal of the media.
 */
void sbull_invalidate(unsigned long ldev)
{
struct sbull_dev *dev = (struct sbull_dev *) ldev;


spin_lock(&dev->lock);
if (dev->users || !dev->data) 
printk (KERN_WARNING "sbull: timer sanity check failed\n");
else
dev->media_change = 1;
spin_unlock(&dev->lock);
}


/*
 * The ioctl() implementation
 */


int sbull_ioctl (struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
long size;
struct hd_geometry geo;
struct sbull_dev *dev = filp->private_data;


switch(cmd) {
   case HDIO_GETGEO:
        /*
* Get geometry: since we are a virtual device, we have to make
* up something plausible.  So we claim 16 sectors, four heads,
* and calculate the corresponding number of cylinders.  We set the
* start of data at sector four.
*/
size = dev->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 __user *) arg, &geo, sizeof(geo)))
return -EFAULT;
return 0;
}


return -ENOTTY; /* unknown command */
}






/*
 * The device operations structure.
 */
static struct block_device_operations sbull_ops = {
.owner           = THIS_MODULE,
.open         = sbull_open,
.release = sbull_release,
.media_changed   = sbull_media_changed,
.revalidate_disk = sbull_revalidate,
.ioctl         = sbull_ioctl
};




/*
 * Set up our internal device.
 */
static void setup_device(struct sbull_dev *dev, int which)
{
/*
* Get some memory.
*/
memset (dev, 0, sizeof (struct sbull_dev));//自定义block devce清零
dev->size = nsectors*hardsect_size;
dev->data = vmalloc(dev->size);//用这个变量来传递数据
if (dev->data == NULL) {
printk (KERN_NOTICE "vmalloc failure.\n");
return;
}
spin_lock_init(&dev->lock);//始始化自旋锁

/*
* The timer which "invalidates" the device.
*/
init_timer(&dev->timer);//初始化一个定时器
dev->timer.data = (unsigned long) dev;
dev->timer.function = sbull_invalidate;//这个定时器的处理函数

/*
* The I/O queue, depending on whether we are using our own
* make_request function or not.
*/
switch (request_mode) {//针对不同请求,来初始化一个处理函数 
   case RM_NOQUEUE:
dev->queue = blk_alloc_queue(GFP_KERNEL);
if (dev->queue == NULL)
goto out_vfree;
blk_queue_make_request(dev->queue, sbull_make_request);//这个自定义一个预处理清求处理函数,这个是没有调度算法,用于SD 等非机械类的磁盘。
break;


   case RM_FULL:
dev->queue = blk_init_queue(sbull_full_request, &dev->lock);//这个是基于bio,
if (dev->queue == NULL)
goto out_vfree;
break;


   default:
printk(KERN_NOTICE "Bad request mode %d, using simple\n", request_mode);
        /* fall into.. */

   case RM_SIMPLE://在这个例子中使用这个
dev->queue = blk_init_queue(sbull_request, &dev->lock);//初始一个请求队列,同时指定一个,处理函数
if (dev->queue == NULL)
goto out_vfree;
break;
}
blk_queue_hardsect_size(dev->queue, hardsect_size);
dev->queue->queuedata = dev;
/*
* And the gendisk structure.
*/
dev->gd = alloc_disk(SBULL_MINORS);
if (! dev->gd) {
printk (KERN_NOTICE "alloc_disk failure\n");
goto out_vfree;
}
dev->gd->major = sbull_major;
dev->gd->first_minor = which*SBULL_MINORS;
dev->gd->fops = &sbull_ops;
dev->gd->queue = dev->queue;
dev->gd->private_data = dev;//让这个private_data数据指向这个设备,这样在读写时可以用过inode来获取
snprintf (dev->gd->disk_name, 32, "sbull%c", which + 'a');
set_capacity(dev->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE));
add_disk(dev->gd);// 增加一个skull设备
return;


  out_vfree:
if (dev->data)
vfree(dev->data);
}






static int __init sbull_init(void)//模块加载函数
{
int i;
/*
* Get registered.
*/
sbull_major = register_blkdev(sbull_major, "sbull");//注册这个块设备
if (sbull_major <= 0) {
printk(KERN_WARNING "sbull: unable to get major number\n");
return -EBUSY;
}
/*
* Allocate the device array, and initialize each one.
*/
Devices = kmalloc(ndevices*sizeof (struct sbull_dev), GFP_KERNEL);//为自定义块设备结构体分配空间
if (Devices == NULL)
goto out_unregister;
for (i = 0; i < ndevices; i++) 
setup_device(Devices + i, i);//加载块设备,这时加载四个
    
return 0;
  out_unregister:
unregister_blkdev(sbull_major, "sbd");
return -ENOMEM;
}


static void sbull_exit(void)
{
int i;


for (i = 0; i < ndevices; i++) {
struct sbull_dev *dev = Devices + i;


del_timer_sync(&dev->timer);
if (dev->gd) {
del_gendisk(dev->gd);
put_disk(dev->gd);
}
if (dev->queue) {
if (request_mode == RM_NOQUEUE)
blk_put_queue(dev->queue);
else
blk_cleanup_queue(dev->queue);
}
if (dev->data)
vfree(dev->data);
}
unregister_blkdev(sbull_major, "sbull");
kfree(Devices);
}

module_init(sbull_init);
module_exit(sbull_exit);
进一步理解Linux操作系统的块设备 在前文《理解Linux操作系统的块设备》中我们从比较高层面(Hight Level)介绍了块设备的原理和块设备的特性。但是关于Linux操作系统块设备的实现原理可能还一知半解。本文将进一步深入的分析Linux的块设备,期望能让大家更加深入的理解块设备的实现细节。 其实在Linux操作系统中可以非常方便的实现一个块设备,或者说是块设备驱动。在Linux中我们熟知的RAID、多路径和Ceph的RBD... 阅读详情

相关推荐

linux sbull 源码

该源码是在原基础上修改可以运行在内核版本为2.6.32上的源代码文件。

linux查看块设备驱动,Linux 块设备驱动 实例

任务:用一片虚拟地址连续的内存空间模拟一个块设备,并为其写一个驱动/** Sample disk driver, from the beginning.*/#include #include #include #include #include #include /* printk() */#include /* kmalloc() */#include /...

weixin_26824207的博客 475

块设备驱动sbull.c

ramdisk设备的驱动程序sbull,虚拟出几个磁盘,并可以对这个虚拟磁盘进行格式化、文件复制和查看等操作。

linux设备驱动程序第二版 加载快设备驱动程序

正如在第一章“Linux核心简介”中“设备与模块的分类”中所概述的一样,Unix的设备驱动程序并不仅限于字符设备。本章就来介绍一下第二大类的设备驱动程序——块设备驱动程序。所谓面向块的设备是指数据传输是以块为单位的(例如软盘和硬盘),这里硬件的块一般被称作“扇区(Sector)”。而名词“块”常用来指软件上的概念:驱动程序常常使用1KB大小的块,即使扇区大小为512字节。在这一章,我们将来构造

menuconfig的专栏 1875

【数据集】waymo motion dataset介绍与数据解析可视化

waymo motion数据集下载,数据描述,数据特征和地图特征,数据解析并完成数据可视化

shuaixio的博客 5187

Aruco标签定位、测距、三维位置估算 opencv应用

摄像头要一开始对准aruco标签才开始运行。+ 相机标定文件(参考我的上篇文章)+ aruco标签6*6。

dgut_guangdian的博客 4229

linux块设备驱动在哪里,Linux设备驱动之块设备驱动流程详解(2)

4 针对block_device_operations结构体;设备打开与释放;4.1块设备驱动的open()和release()函数不是必须的,一个简单的块设备驱动可以不提供open()和release()函数.块设备驱动的open()函数和字符设备驱动的open()和类似,都以相关inode和file结构体指针作为参数,当一个结点引用一个块设备时,inode->i_bdev->bd_...

weixin_33584986的博客 339

使用基于python的neurora进行表征相似性分析(RSA)

发现了一款用于表征分析的Python API,还是挺方便的,这里简单介绍一下进行表征相似性分析的使用方法。

zjh12312311的博客 2769

linux块设备分析与使用(转的)

linux块设备分析与使用  linux块设备分析与使用creatorsz111@126.com    本篇文章力求简单明了的解释Linux的块设备驱动,让大家对它少些畏惧,快速的开发一个块设备。分析完这篇之后,下一步就是分析MMC卡的驱动,争取分析之后可以达到优化读卡速度的目的。Linux的块设备看似比较

zgl07的专栏 1493

AI漫剧制作全流程零基础实操指南:从剧本到剪辑

AI漫剧是一种利用AI工具将剧本、分镜、出图、配音、剪辑整合为流水线的短视频内容形式。其技术原理基于文生图、图生图与文本转语音模型的组合,核心挑战在于角色一致性控制与分镜批量出图。这项技术大幅降低了视频制作门槛,使个人创作者无需演员和设备即可产出剧情向短内容,广泛应用于短视频平台的情景剧、玄幻、甜宠等题材。本文给出了一套零基础可照做的全流程实操方案,覆盖剧本提示词、分镜表、出图选型、配音合成与剪辑模板,帮助你快速跑通AI漫剧制作链路。

weixin_29048309的博客 558

弹窗提示报错: F:\c\CFX5\bin\Debug\net9.0-windows10.0.26100.0\CFX5.dll 面向无法动态枚举工具箱项目的平台...如何解决?

🏆本文收录于 《全栈Bug调优(实战版)》 专栏,该专栏专注于分享我在真实项目开发中遇到的各类疑难Bug及其深层成因,并系统提供高效、可复现的解决思路和实操方案。无论你是刚入行的新手开发者,还是拥有多年项目经验的资深工程师,本专栏都将为你提供一条系统化、高质量的问题排查与优化路径,助力你加速成长,攻克技术壁垒,迈向技术价值最大化与职业发展的更高峰🚀!

**My Coding Family** 952

编写并分析简单的块设备驱动程序

块设备也和字符设备一样可以通过/dev目录下的设备文件来访问,而网络设备不可以。此外块设备(例如磁盘)上能够容纳文件系统。

qq_58538265的博客 645

智能办公助手——OpenClaw办公自动化集成(2026办公版)

OpenClaw智能办公助手是一款集成任务管理、文档处理等功能的办公自动化系统。该系统采用Python实现,核心功能包括:1)任务管理系统,支持任务增删改查、状态跟踪和统计报表生成;2)文档智能助手,提供文档处理能力。任务管理模块具备优先级设置、状态变更、时间记录等功能,并能生成包含完成率等指标的任务报告。该系统旨在通过自动化办公流程提升工作效率,减少重复性工作。开发者可利用这些模块构建完整的智能办公解决方案。

一键难忘的博客 360

iloader iOS侧载工具教程:三平台 3 分钟装好并完成首次侧载

手里有 IPA 文件,却不知道往 iPhone 上装?iloader 是一款免费开源的 iOS 侧载工具,不用越狱,也不碰命令行。下载安装、登录 Apple ID,3 分钟完成第一次侧载。 [![iloader iOS侧载工具应用图标](https://raw.gitcode.com/GitHub_Trending/iloa/iloader/raw/a36a6612702b0941ec207a3

gitblog_00004的博客 903

【AutoSAR CAN】04 - 如何使用Davinci Configurator Pro工具配置CAN的波特率

改参数为Tx Arbitration start delay value ,可用于延迟仲裁过程的起始点,用来补偿CAN收发器及线路的传输延迟,以优化传输性能。有关详细信息,请参阅硬件手册。该参数指定采样点模式,即选择多少个采样点的数量,如果选择了每比特三个样本选项,则该点标记第三个样本的位置;我们要配置CAN的波特率,其实就是要弄明白 CAN 控制器的位时序即可,具体的理论部分和EB的配置部门可参考文章《该参数表示传播延迟有多少个时间单位Tq,即上文中的Propagation delay/PROGSEG。.

2607

基于STM32、HAL库的MCP4013T-103E/CH 数字电位器驱动程序设计

define MCP4013_I2C_ADDR (0x5F << 1) // HAL库使用7位地址左移1位,或直接使用0xBE/0xBF。#include "i2c.h" // 包含你的I2C初始化相关的头文件,例如由CubeMX生成的 i2c.h。大于127的值会被截断。// MCP4013的读操作:主机发起读取请求,从机返回当前滑片位置的一个字节。// 使用HAL_I2C_Master_Transmit直接发送数据。// 使用HAL_I2C_Master_Receive读取一个字节。

corlin6688的博客 128

linux块设备驱动之实例

前两篇blog已经基本熟悉了块设备的相关结构,这里来总结下ldd3中提到的一些块设备驱动例题; 注册: sbull_major = register_blkdev(sbull_major, "sbull"); if (0 >= sbull_major){ printk(KERN_WARNING "sbull: u

YuZhiHui_No1的专栏 3463

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Linux中块设备驱动程序分析

基于《Linux设备驱动程序》书中的sbull程序以对Linux块设备驱动总结分析。 开始之前先来了解这个块设备中的核心数据结构: struct sbull_dev {         int size;                       /* Device size in sectors */         u8 *data;                       

宽简厚重—博约 1883

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

linux 下块设备驱动开发学习笔记 2(sbull驱动在vmware上测试)

在我们make完,生成一个ko文件后,我们就可以测试。 1、insmode skull.ko 2、more /proc/devices 3、ls  /dev/sbull sbulla    sbullb   sbullc  sbulld 4、fdisk  /dev/sbulla m-n-p-1- -w -p 这时就在dev下有一个sbulla1的分区 5、格式化,使指定文件系统

Bean Huo 1769

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

SBULL块设备驱动程序分析

#define SBULL_MINORS  16         /* 每个sbull设备所支持的次设备号的数量 */  #define KERNEL_SECTOR_SIZE 512  // 本地定义的常量,使用该常量进行内核512字节到实际                                      // 扇区大小的转换  #define INVALIDA

liu0808的专栏 731

linux 块设备驱动(四)——简单的sbull实例

#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/slab.h> #in...

weixin_30794499的博客 295

块设备驱动1--自编ramdisk(在linux-3.2.36上的新接口)

这是块设备驱动的第一期,我们就从ldd3的sbull开始吧,但是ldd3用的linux版本太老了,你直接用它的例子在linux-3.2.x上是很麻烦的。 我主要做的就是在高版本上的移植。 里面有个NOQUEUE宏,可以选择不用一个请求队列 。 自己对着ldd3中的讲解看看吧 /*********************************** Copyright(C), 2013

xxxxxlllllxl的专栏 1538

Linux设备驱动程序第三版之块设备驱动程序

本篇分析Linux设备驱动程序第三版的第十六章,块设备驱动程序的代码,主要是其sbull模块在内核2.6.35.6-45版本下编译有错,这篇文章就是讲sbull模块迁移到了2.6.35.6-45这个模块下了,实现了正确编译,加载。修改之后的代码如下所示,相关函数的功能也在代码中进行说明了。 #include #include #include #include #include

hustfoxy的专栏 1426
上一篇: linux 下块设备驱动开发学习笔记 1
下一篇: linux 下块设备驱动开发学习笔记 2(sbull驱动在vmware上测试)
Bean Huo
博客等级 码龄19年 40粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值