《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04
0 准备工作。
0.0 系统环境:Ubuntu18.04.1 amd64。
0.1 安装必要软件包
|
1
|
sudo apt install build-essential bison flex libssl-dev libelf-dev |
1 下载内核源码,构建源码树
|
1
2
3
4
5
6
7
8
9
10
11
12
|
sudo apt install linux-source-4.15.0 -ycd /usr/src/linux-source-4.15.0/sudo tar xf linux-source-4.15.0.tar.bz2cd linux-source-4.15.0/sudo make oldconfigsudo make preparesudo make scripts |
2 编译驱动源码
源码需要改动几处:
copy_to_user()改为raw_copy_to_user();
copy_from_user()改为raw_copy_from_user();
init_MUTEX((&scull_device->sem);改为sema_init(&scull_device->sem, 1);
|
1
2
3
4
5
|
makesudo insmod scull.kosudo rmmod scull.ko |
附1:scull.c
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/slab.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/cdev.h>#include <asm/uaccess.h>#include "scull.h"int scull_major = SCULL_MAJOR;int scull_minor = 0;module_param(scull_major, int, S_IRUGO);module_param(scull_minor, int, S_IRUGO);struct scull_dev *scull_device;int scull_trim(struct scull_dev *dev){ if (dev) { if (dev->data) { kfree(dev->data); } dev->data = NULL; dev->size = 0; } return 0;}int scull_open(struct inode *inode, struct file *filp){ struct scull_dev *dev; dev = container_of(inode->i_cdev, struct scull_dev, cdev); filp->private_data = dev; if ((filp->f_flags & O_ACCMODE) == O_WRONLY) { if (down_interruptible(&dev->sem)) { return -ERESTARTSYS; } scull_trim(dev); up(&dev->sem); } return 0;}int scull_release(struct inode *inode, struct file *filp){ return 0;}ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos){ struct scull_dev *dev = filp->private_data; ssize_t retval = 0; if (down_interruptible(&dev->sem)) { return -ERESTARTSYS; } if (*f_pos >= dev->size) { goto out; } if (*f_pos + count > dev->size) { count = dev->size - *f_pos; } if (!dev->data) { goto out; } if (raw_copy_to_user(buf, dev->data + *f_pos, count)) { retval = -EFAULT; goto out; } *f_pos += count; retval = count; out: up(&dev->sem); return retval;}ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos){ struct scull_dev *dev = filp->private_data; ssize_t retval = -ENOMEM; if (down_interruptible(&dev->sem)) { return -ERESTARTSYS; } if (!dev->data) { dev->data = kmalloc(SCULL_BUFFER_SIZE, GFP_KERNEL); if (!dev->data) { goto out; } memset(dev->data, 0, SCULL_BUFFER_SIZE); } if (count > SCULL_BUFFER_SIZE - dev->size) { count = SCULL_BUFFER_SIZE - dev->size; } if (raw_copy_from_user(dev->data + dev->size, buf, count)) { retval = -EFAULT; goto out; } dev->size += count; retval = count; out: up(&dev->sem); return retval;}loff_t scull_llseek(struct file *filp, loff_t off, int whence){ struct scull_dev *dev = filp->private_data; loff_t newpos; switch(whence) { case 0: newpos = off; break; case 1: newpos = filp->f_pos + off; break; case 2: newpos = dev->size + off; break; default: return -EINVAL; } if (newpos < 0) { return -EINVAL; } filp->f_pos = newpos; return newpos;}struct file_operations scull_fops = { .owner = THIS_MODULE, .llseek = scull_llseek, .read = scull_read, .write = scull_write, .open = scull_open, .release = scull_release, };void scull_cleanup_module(void){ dev_t devno = MKDEV(scull_major, scull_minor); if (scull_device) { scull_trim(scull_device); cdev_del(&scull_device->cdev); kfree(scull_device); } unregister_chrdev_region(devno, 1);}static void scull_setup_cdev(struct scull_dev *dev){ int err, devno = MKDEV(scull_major, scull_minor); cdev_init(&dev->cdev, &scull_fops); dev->cdev.owner = THIS_MODULE; dev->cdev.ops = &scull_fops; err = cdev_add(&dev->cdev, devno, 1); if (err) { printk(KERN_NOTICE "Error %d adding scull", err); }}static int __init scull_init_module(void){ int result; dev_t dev = 0; if (scull_major) { dev = MKDEV(scull_major, scull_minor); result = register_chrdev_region(dev, 1, "scull"); } else { result = alloc_chrdev_region(&dev, scull_minor, 1, "scull"); scull_major = MAJOR(dev); } if (result < 0) { printk(KERN_WARNING "scull: can't get major %d\n", scull_major); return result; } scull_device = kmalloc(sizeof(struct scull_dev), GFP_KERNEL); if (!scull_device) { result = -ENOMEM; goto fail; } memset(scull_device, 0, sizeof(struct scull_dev)); sema_init(&scull_device->sem, 1); scull_setup_cdev(scull_device); return 0; fail: scull_cleanup_module(); return result;}module_init(scull_init_module);module_exit(scull_cleanup_module);MODULE_LICENSE("GPL"); |
附2:scull.h
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#ifndef _SCULL_H#define _SCULL_H#define SCULL_MAJOR 0#define SCULL_BUFFER_SIZE PAGE_SIZEstruct scull_dev { char *data; unsigned long size; struct semaphore sem; struct cdev cdev;};#endif |
附3:Makefile
|
1
2
3
4
5
6
7
8
9
10
|
#sample driver moduleobj-m := scull.oKDIR = /root/linux-2.6.36.4/PWD:=$(shell pwd)INSTALLDIR=$(PWD)modules: $(MAKE) -C /lib/modules/`uname -r`/build M=`pwd` modulesclean: rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions.PHONY:modules clean |
《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04的更多相关文章
- Linux设备驱动程序 第三版 读书笔记(一)
Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...
- Linux设备驱动程序学习----3.模块的编译和装载
模块的编译和装载 更多内容请参考Linux设备驱动程序学习----目录 1. 设置测试系统 第1步,要先从kernel.org的镜像网站上获取一个主线内核,并安装到自己的系统中,因为学习驱动程序的编写 ...
- linux设备驱动程序_hello word 模块编译各种问题集锦
在看楼经典书籍<linux设备驱动程序>后,第一个程序就是编写一个hello word 模块. 原以为非常easy,真正弄起来,发现问题不少啊.前两天编过一次,因为没有记录,今天看的时候又 ...
- Linux - Unix环境高级编程(第三版) 代码编译
Unix环境高级编程(第三版) 代码编译 本文地址:http://blog.csdn.net/caroline_wendy 时间:2014.10.2 1. 下载代码:http://www.apuebo ...
- Linux设备驱动程序学习之分配内存
内核为设备驱动提供了一个统一的内存管理接口,所以模块无需涉及分段和分页等问题. 我已经在第一个scull模块中使用了 kmalloc 和 kfree 来分配和释放内存空间. kmalloc 函数内幕 ...
- Linux设备驱动程序学习----1.设备驱动程序简介
设备驱动程序简介 更多内容请参考Linux设备驱动程序学习----目录 1. 简介 Linux系统的优点是,系统内部实现细节对所有人都是公开的.Linux内核由大量复杂的代码组成,设备驱动程序可以 ...
- linux设备驱动程序--hello-world
linux字符设备驱动程序--hello_world 基于4.14内核, beagleBone green平台 PC端的设备驱动程序 有过电脑使用经验的人都知道,当我们将外部硬件设备比如鼠标键盘插入到 ...
- APUE学习--第三版apue编译
第三版apue编译: 1. 首先在 http://www.apuebook.com/ 下载源码解压: tar zxvf src.3e.tar.gz 看完Readme可知,直接执 ...
- linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)
原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...
随机推荐
- mysql死锁检查
1.查看当前数据库进程 show processlist; show full processlist; #前者只显示前100条,后者显示所有的进程 2.一键杀死某个用户的所有进程 select ...
- ubuntu-网络配置文件
vim /etc/NetworkManager/system-connections/Wired connectione 1
- Tomcat启动超时设置
Tomcat启动超时设置: 处理方法: 1. 在server中找到当前Tomcat双击. 2.在视图中进行修改.(如下图:)
- hive 外部表和内部表的区别和相互转换
Hive内部表和外部表区别 1.创建内部表时,内部表的数据文件是保存在指定的路径的:如若创建外部表,则只记录数据所在的路径,不会对数据位置做改变. 2.删除表的时候,内部表元数据和数据会跟着一起删除. ...
- java安全相关知识
基本概念 JVM:java虚拟机,Java编译程序将生成Java虚拟机上可运行的目标代码,使得Java程序可以再不同平台不加修改的运行.JVM包含完善的硬件架构,主要分为五大模块-类装载器子系统.运行 ...
- React系列,初识
学习react对于新手来说,还没有学react往往就会被webpack,npm等搞的晕头转向,所以我们今天就从最简单的方式入手 <script src="react.js"& ...
- 《你说对就队》第八次团队作业:Alpha冲刺 第三天
<你说对就队>第八次团队作业:Alpha冲刺 第三天 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...
- sql:拼接字符串、截取字符串、取字符串长度
--第一段 SELECT substr('1233***6795', 0, (select instr('1233***6795', '***', 1, 1) from dual) - 1) from ...
- 找回IntelliJ IDEA中丢失的Run Dashboard视图
一般项目中包含多个springboot项目的时候都会出现run dashboard视图,但如果一开始它提示的时候,不点击展示,就再也找不到这个视图了,给我们后续启动一个一个的启动项目带来了很大的不便, ...
- PPT扁平化设计总结
注:以下内容基本都来自知乎,由于已经不记得网址了,所以未能附上所有相关链接,抱歉. PPT扁平化设计原则一.亲密:意思相近的内容放在一起二.对齐:页面上的某两个元素之间总是围绕一条直线对齐三.对比:有 ...