time_ctl
This commit is contained in:
@@ -109,6 +109,7 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key;
|
|||||||
学过网络编程的同学都清楚这两个调用是干什么的,用来等待个文件流的操作。
|
学过网络编程的同学都清楚这两个调用是干什么的,用来等待个文件流的操作。
|
||||||
其实这并不是网络编程的专利,在内核中本来就是一种监听等待的机制。书上说的很明白,我们来看看他的数据结构图。
|
其实这并不是网络编程的专利,在内核中本来就是一种监听等待的机制。书上说的很明白,我们来看看他的数据结构图。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
很简单,当我们调用POLL的时候其实就创建了一个POLL_TABLE添加一个等待的队列,而这个队列上有三个结构项
|
很简单,当我们调用POLL的时候其实就创建了一个POLL_TABLE添加一个等待的队列,而这个队列上有三个结构项
|
||||||
1.指向被打开的文件类型指针。
|
1.指向被打开的文件类型指针。
|
||||||
|
|||||||
BIN
higher_scull/image/polol.png
Normal file
BIN
higher_scull/image/polol.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
62
timer_s/README.md
Normal file
62
timer_s/README.md
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
## 内核时间,延时与缓存
|
||||||
|
|
||||||
|
## 时间
|
||||||
|
|
||||||
|
内核通过定时器中断来跟踪时间流,记录每次时钟周期的滴答数,现代大多数默认时1000HZ ,每次开机后内核会初始化时间变量,然后每过一个时钟周期,变量加一,我们应当充分的信任内核不要随便去改动,除非有特殊的理由。
|
||||||
|
|
||||||
|
jiffies 变量用来存储时钟中断次数,是一个64位的无符号长整形。它的访问一直是原子的。
|
||||||
|
|
||||||
|
内核定义:
|
||||||
|
|
||||||
|
```
|
||||||
|
extern u64 __jiffy_data jiffies_64;
|
||||||
|
extern unsigned long volatile __jiffy_data jiffies;
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
比较当前值和缓存值的几个宏:
|
||||||
|
|
||||||
|
```
|
||||||
|
#define time_after(a,b) \
|
||||||
|
(typecheck(unsigned long, a) && \
|
||||||
|
typecheck(unsigned long, b) && \
|
||||||
|
((long)((b) - (a)) < 0))
|
||||||
|
#define time_before(a,b) time_after(b,a)
|
||||||
|
|
||||||
|
#define time_after_eq(a,b) \
|
||||||
|
(typecheck(unsigned long, a) && \
|
||||||
|
typecheck(unsigned long, b) && \
|
||||||
|
((long)((a) - (b)) >= 0))
|
||||||
|
#define time_before_eq(a,b) time_after_eq(b,a)
|
||||||
|
|
||||||
|
```
|
||||||
|
时钟中断计时法和现实时间的转化。
|
||||||
|
|
||||||
|
```
|
||||||
|
static __always_inline unsigned long usecs_to_jiffies(const unsigned int u);
|
||||||
|
extern unsigned long timespec64_to_jiffies(const struct timespec64 *value);
|
||||||
|
extern void jiffies_to_timespec64(const unsigned long jiffies,truct timespec64 *value);
|
||||||
|
static inline unsigned long timespec_to_jiffies(const struct timespec *value);
|
||||||
|
static inline void jiffies_to_timespec(const unsigned long jiffies,struct timespec *value);
|
||||||
|
extern unsigned long timeval_to_jiffies(const struct timeval *value);
|
||||||
|
extern void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value);
|
||||||
|
extern clock_t jiffies_to_clock_t(unsigned long x);
|
||||||
|
extern unsigned long clock_t_to_jiffies(unsigned long x);
|
||||||
|
```
|
||||||
|
|
||||||
|
使用64位寄存器直接获取变量。
|
||||||
|
static inline u64 get_jiffies_64(void)
|
||||||
|
{
|
||||||
|
return (u64)jiffies;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
内核就是通过这个全局的变量来获取时间的,一般来说驱动程序是不需要知道墙上时间的,真实世界时间一般放在用户空间使用。
|
||||||
|
|
||||||
|
在CODE代码中有一个获取时间模块,我们加载后,可以从/proc/currentime中获取当前时间
|
||||||
|
|
||||||
|
结果如下
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
2
timer_s/code/.tmp_versions/scull.mod
Normal file
2
timer_s/code/.tmp_versions/scull.mod
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/home/hacker/git/Linux_Scull/timer_s/code/scull.ko
|
||||||
|
/home/hacker/git/Linux_Scull/timer_s/code/main.o
|
||||||
12
timer_s/code/Makefile
Normal file
12
timer_s/code/Makefile
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
scull-objs := main.o
|
||||||
|
obj-m := scull.o
|
||||||
|
CURRENT_PATH := ${shell pwd}
|
||||||
|
CURRENT_KERNEL_PATH := ${shell uname -r}
|
||||||
|
LINUX_KERNEL_PATH := /usr/src/kernels/$(CURRENT_KERNEL_PATH)
|
||||||
|
|
||||||
|
all:
|
||||||
|
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
|
||||||
|
clean:
|
||||||
|
rm *.o *.order *.symvers *.mod.c *.ko
|
||||||
|
|
||||||
|
|
||||||
491
timer_s/code/main.c
Normal file
491
timer_s/code/main.c
Normal file
@@ -0,0 +1,491 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
> File Name: main.c
|
||||||
|
> Author:
|
||||||
|
> Mail:
|
||||||
|
> Created Time: 2017年03月24日 星期五 11时41分42秒
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
//#include <linux/config.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/moduleparam.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
|
||||||
|
#include <linux/kernel.h> /* printk() */
|
||||||
|
#include <linux/slab.h> /* kmalloc() */
|
||||||
|
#include <linux/fs.h> /* everything... */
|
||||||
|
#include <linux/errno.h> /* error codes */
|
||||||
|
#include <linux/types.h> /* size_t */
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/fcntl.h> /* O_ACCMODE */
|
||||||
|
#include <linux/seq_file.h> /*创建/proc 文件以用来和用户交互数据*/
|
||||||
|
#include <linux/cdev.h>
|
||||||
|
#include <linux/proc_ns.h> /*proc 在新版中移动到了此目录下*/
|
||||||
|
//#include <asm/system.h> /* cli(), *_flags */
|
||||||
|
#include <asm/uaccess.h> /* copy_*_user */
|
||||||
|
|
||||||
|
#include <net/snmp.h>
|
||||||
|
#include <linux/ipv6.h>
|
||||||
|
#include <net/if_inet6.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "scull.h" /* local definitions */
|
||||||
|
|
||||||
|
int scull_major = SCULL_MAJOR;
|
||||||
|
int scull_minor = 0;
|
||||||
|
int scull_nr_devs = SCULL_NR_DEVS;
|
||||||
|
int scull_quantum = SCULL_QUANTUM;
|
||||||
|
int scull_qset = SCULL_QSET;
|
||||||
|
|
||||||
|
|
||||||
|
struct scull_dev *scull_devices;
|
||||||
|
|
||||||
|
struct file_operations scull_fops = { /*文件操作函数*/
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.llseek = scull_llseek,
|
||||||
|
.read = scull_read,
|
||||||
|
.write = scull_write,
|
||||||
|
// .ioctl = scull_ioctl, 最新内核删掉了这个接口
|
||||||
|
.open = scull_open,
|
||||||
|
.release= scull_release,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Here are our sequence iteration methods. Our "position" is
|
||||||
|
* simply the device number.
|
||||||
|
*/
|
||||||
|
static void *scull_seq_start(struct seq_file *s, loff_t *pos)
|
||||||
|
{
|
||||||
|
if (*pos >= scull_nr_devs)
|
||||||
|
return NULL; /* No more to read */
|
||||||
|
return scull_devices + *pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)
|
||||||
|
{
|
||||||
|
(*pos)++;
|
||||||
|
if (*pos >= scull_nr_devs)
|
||||||
|
return NULL;
|
||||||
|
return scull_devices + *pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void scull_seq_stop(struct seq_file *s, void *v)
|
||||||
|
{
|
||||||
|
/* Actually, there's nothing to do here */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int scull_seq_show(struct seq_file *s, void *v)
|
||||||
|
{
|
||||||
|
struct timeval tv1;
|
||||||
|
struct timespec tv2;
|
||||||
|
unsigned long j1;
|
||||||
|
u64 j2;
|
||||||
|
|
||||||
|
/* get them four */
|
||||||
|
j1 = jiffies;
|
||||||
|
j2 = get_jiffies_64();
|
||||||
|
do_gettimeofday(&tv1);
|
||||||
|
tv2 = current_kernel_time();
|
||||||
|
|
||||||
|
/* print */
|
||||||
|
//len=0;
|
||||||
|
//len +=
|
||||||
|
seq_printf(s,"0x%08lx 0x%016Lx %10i.%06i\n"
|
||||||
|
"%40i.%09i\n",
|
||||||
|
j1, j2,
|
||||||
|
(int) tv1.tv_sec, (int) tv1.tv_usec,
|
||||||
|
(int) tv2.tv_sec, (int) tv2.tv_nsec);
|
||||||
|
//*start = buf;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The proc filesystem: function to read and entry
|
||||||
|
*/
|
||||||
|
|
||||||
|
int scull_read_procmem(char *buf, char **start, off_t offset,
|
||||||
|
int count, int *eof, void *data)
|
||||||
|
{
|
||||||
|
int i, j, len = 0;
|
||||||
|
int limit = count - 80; /* Don't print more than this */
|
||||||
|
|
||||||
|
for (i = 0; i < scull_nr_devs && len <= limit; i++) {
|
||||||
|
struct scull_dev *d = &scull_devices[i];
|
||||||
|
struct scull_qset *qs = d->data;
|
||||||
|
if (down_interruptible(&d->sem))
|
||||||
|
return -ERESTARTSYS;
|
||||||
|
len += sprintf(buf+len,"\nDevice %i: qset %i, q %i, sz %li\n",
|
||||||
|
i, d->qset, d->quantum, d->size);
|
||||||
|
for (; qs && len <= limit; qs = qs->next) { /* scan the list */
|
||||||
|
len += sprintf(buf + len, " item at %p, qset at %p\n",
|
||||||
|
qs, qs->data);
|
||||||
|
if (qs->data && !qs->next) /* dump only the last item */
|
||||||
|
for (j = 0; j < d->qset; j++) {
|
||||||
|
if (qs->data[j])
|
||||||
|
len += sprintf(buf + len,
|
||||||
|
" % 4i: %8p\n",
|
||||||
|
j, qs->data[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
up(&scull_devices[i].sem);
|
||||||
|
}
|
||||||
|
*eof = 1;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*便利设备链表*/
|
||||||
|
struct scull_qset *scull_follow(struct scull_dev *dev, int n)
|
||||||
|
{
|
||||||
|
struct scull_qset *qs = dev->data;
|
||||||
|
|
||||||
|
/* Allocate first qset explicitly if need be */
|
||||||
|
if (! qs) {
|
||||||
|
qs = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
|
||||||
|
if (qs == NULL)
|
||||||
|
return NULL; /* Never mind */
|
||||||
|
memset(qs, 0, sizeof(struct scull_qset));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then follow the list */
|
||||||
|
while (n--) {
|
||||||
|
if (!qs->next) {
|
||||||
|
qs->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
|
||||||
|
if (qs->next == NULL)
|
||||||
|
return NULL; /* Never mind */
|
||||||
|
memset(qs->next, 0, sizeof(struct scull_qset));
|
||||||
|
}
|
||||||
|
qs = qs->next;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return qs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*安装DEV 结构到这个scull_devices*/
|
||||||
|
static void scull_setup_cdev(struct scull_dev *dev,int index){
|
||||||
|
|
||||||
|
int err,devno = MKDEV(scull_major,scull_minor + index);
|
||||||
|
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("error %d adding scull%d",err,index);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*几个函数调用方法*/
|
||||||
|
|
||||||
|
|
||||||
|
int scull_p_init(dev_t dev){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void scull_p_cleanup(void){
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
int scull_access_init(dev_t dev){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void scull_access_cleanup(void){
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
/*删除设备的空间*/
|
||||||
|
int scull_trim(struct scull_dev *dev){
|
||||||
|
|
||||||
|
struct scull_qset *next,*dptr;
|
||||||
|
int qset = dev->qset;
|
||||||
|
int i;
|
||||||
|
for(dptr = dev->data;dptr;dptr = next){
|
||||||
|
if(dptr->data){
|
||||||
|
for(i = 0;i < qset;i++){
|
||||||
|
kfree(dptr->data[i]);
|
||||||
|
}
|
||||||
|
kfree(dptr->data);
|
||||||
|
dptr->data = NULL;
|
||||||
|
}
|
||||||
|
next = dptr->next;
|
||||||
|
kfree(dptr);
|
||||||
|
}
|
||||||
|
dev->size = 0;
|
||||||
|
dev->quantum = scull_quantum;
|
||||||
|
dev->qset = scull_qset;
|
||||||
|
dev->data = NULL;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int scull_open(struct inode* inode,struct file *filp){
|
||||||
|
struct scull_dev *dev; /* device information */
|
||||||
|
|
||||||
|
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
|
||||||
|
filp->private_data = dev; /* for other methods */
|
||||||
|
|
||||||
|
/* now trim to 0 the length of the device if open was write-only */
|
||||||
|
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
|
||||||
|
if (down_interruptible(&dev->sem))
|
||||||
|
return -ERESTARTSYS;
|
||||||
|
scull_trim(dev); /* ignore errors */
|
||||||
|
up(&dev->sem);
|
||||||
|
}
|
||||||
|
return 0; /* success */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t scull_read(struct file *filp, char __user *buf, size_t count,loff_t *f_pos){
|
||||||
|
struct scull_dev *dev = filp->private_data;
|
||||||
|
struct scull_qset *dptr; /* the first listitem */
|
||||||
|
int quantum = dev->quantum, qset = dev->qset;
|
||||||
|
int itemsize = quantum * qset; /* how many bytes in the listitem */
|
||||||
|
int item, s_pos, q_pos, rest;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* find listitem, qset index, and offset in the quantum */
|
||||||
|
item = (long)*f_pos / itemsize;
|
||||||
|
rest = (long)*f_pos % itemsize;
|
||||||
|
s_pos = rest / quantum; q_pos = rest % quantum;
|
||||||
|
|
||||||
|
/* follow the list up to the right position (defined elsewhere) */
|
||||||
|
dptr = scull_follow(dev, item);
|
||||||
|
|
||||||
|
if (dptr == NULL || !dptr->data || ! dptr->data[s_pos])
|
||||||
|
goto out; /* don't fill holes */
|
||||||
|
|
||||||
|
/* read only up to the end of this quantum */
|
||||||
|
if (count > quantum - q_pos)
|
||||||
|
count = quantum - q_pos;
|
||||||
|
|
||||||
|
if (copy_to_user(buf, dptr->data[s_pos] + q_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;
|
||||||
|
struct scull_qset *dptr;
|
||||||
|
int quantum = dev->quantum, qset = dev->qset;
|
||||||
|
int itemsize = quantum * qset;
|
||||||
|
int item, s_pos, q_pos, rest;
|
||||||
|
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
|
||||||
|
|
||||||
|
if (down_interruptible(&dev->sem))
|
||||||
|
return -ERESTARTSYS;
|
||||||
|
|
||||||
|
/* find listitem, qset index and offset in the quantum */
|
||||||
|
item = (long)*f_pos / itemsize;
|
||||||
|
rest = (long)*f_pos % itemsize;
|
||||||
|
s_pos = rest / quantum; q_pos = rest % quantum;
|
||||||
|
|
||||||
|
/* follow the list up to the right position */
|
||||||
|
dptr = scull_follow(dev, item);
|
||||||
|
if (dptr == NULL)
|
||||||
|
goto out;
|
||||||
|
if (!dptr->data) {
|
||||||
|
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
|
||||||
|
if (!dptr->data)
|
||||||
|
goto out;
|
||||||
|
memset(dptr->data, 0, qset * sizeof(char *));
|
||||||
|
}
|
||||||
|
if (!dptr->data[s_pos]) {
|
||||||
|
dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
|
||||||
|
if (!dptr->data[s_pos])
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
/* write only up to the end of this quantum */
|
||||||
|
if (count > quantum - q_pos)
|
||||||
|
count = quantum - q_pos;
|
||||||
|
|
||||||
|
if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {
|
||||||
|
retval = -EFAULT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
*f_pos += count;
|
||||||
|
retval = count;
|
||||||
|
|
||||||
|
/* update the size */
|
||||||
|
if (dev->size < *f_pos)
|
||||||
|
dev->size = *f_pos;
|
||||||
|
|
||||||
|
out:
|
||||||
|
up(&dev->sem);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
loff_t scull_llseek(struct file *filp, loff_t off, int whence){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int scull_ioctl(struct inode *inode, struct file *filp,unsigned int cmd, unsigned long arg){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int scull_release(struct inode * inode,struct file*filp){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tie the sequence operators up.
|
||||||
|
*/
|
||||||
|
static struct seq_operations scull_seq_ops = {
|
||||||
|
.start = scull_seq_start,
|
||||||
|
.next = scull_seq_next,
|
||||||
|
.stop = scull_seq_stop,
|
||||||
|
.show = scull_seq_show
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now to implement the /proc file we need only make an open
|
||||||
|
* method which sets up the sequence operators.
|
||||||
|
*/
|
||||||
|
static int scull_proc_open(struct inode *inode, struct file *file)
|
||||||
|
{
|
||||||
|
return seq_open(file, &scull_seq_ops);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create a set of file operations for our proc file.
|
||||||
|
*/
|
||||||
|
static struct file_operations scull_proc_ops = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = scull_proc_open,
|
||||||
|
.read = seq_read,
|
||||||
|
.llseek = seq_lseek,
|
||||||
|
.release = seq_release
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*proc 文件的创建与移除*/
|
||||||
|
|
||||||
|
static void scull_create_proc(void)
|
||||||
|
{
|
||||||
|
struct proc_dir_entry *entry;
|
||||||
|
/*create_proc_read_entry("scullmem", 0 {default mode},
|
||||||
|
NULL { parent dir } , {scull_read_procmem},
|
||||||
|
NULL { client data} );
|
||||||
|
entry = create_proc_entry("scullseq", 0, NULL);
|
||||||
|
*/
|
||||||
|
proc_create("scullmem",0,NULL,&scull_proc_ops);
|
||||||
|
entry = proc_create("scullseq",0,NULL,&scull_proc_ops);
|
||||||
|
|
||||||
|
//if (entry)
|
||||||
|
// entry->proc_fops = &scull_proc_ops;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void scull_remove_proc(void)
|
||||||
|
{
|
||||||
|
/* no problem if it was not registered */
|
||||||
|
remove_proc_entry("scullmem", NULL /* parent dir */);
|
||||||
|
remove_proc_entry("scullseq", NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*_______________________________________________________________________________________*/
|
||||||
|
|
||||||
|
|
||||||
|
void scull_cleanup_module(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
dev_t devno = MKDEV(scull_major, scull_minor);
|
||||||
|
|
||||||
|
/* Get rid of our char dev entries */
|
||||||
|
if (scull_devices) {
|
||||||
|
for (i = 0; i < scull_nr_devs; i++) {
|
||||||
|
scull_trim(scull_devices + i);
|
||||||
|
cdev_del(&scull_devices[i].cdev);
|
||||||
|
}
|
||||||
|
kfree(scull_devices);
|
||||||
|
}
|
||||||
|
scull_remove_proc();/*创建测试的proc 文件*/
|
||||||
|
/* cleanup_module is never called if registering failed */
|
||||||
|
unregister_chrdev_region(devno, scull_nr_devs);
|
||||||
|
|
||||||
|
/* and call the cleanup functions for friend devices */
|
||||||
|
//scull_p_cleanup();
|
||||||
|
//scull_access_cleanup();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int scull_init_module(void) /*获取主设备号,或者创建设备编号*/
|
||||||
|
{
|
||||||
|
int result ,i;
|
||||||
|
dev_t dev = 0;
|
||||||
|
|
||||||
|
if(scull_major){
|
||||||
|
dev = MKDEV(scull_major,scull_minor); /*将两个设备号转换为dev_t类型*/
|
||||||
|
result = register_chrdev_region(dev,scull_nr_devs,"scull");/*申请设备编号*/
|
||||||
|
}else{
|
||||||
|
result = alloc_chrdev_region(&dev,scull_minor,scull_nr_devs,"scull");/*分配主设备号*/
|
||||||
|
scull_major = MAJOR(dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result < 0){
|
||||||
|
printk("scull : cant get major %d\n",scull_major);
|
||||||
|
return result;
|
||||||
|
}else{
|
||||||
|
printk("make a dev %d %d\n",scull_major,scull_minor);
|
||||||
|
}
|
||||||
|
/*分配设备的结构体*/
|
||||||
|
scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev),GFP_KERNEL);
|
||||||
|
|
||||||
|
if(!scull_devices){
|
||||||
|
result = -1;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
memset(scull_devices,0,scull_nr_devs * sizeof(struct scull_dev));
|
||||||
|
|
||||||
|
for(i = 0;i < scull_nr_devs;i++){
|
||||||
|
scull_devices[i].quantum = scull_quantum;
|
||||||
|
scull_devices[i].qset = scull_qset;
|
||||||
|
sema_init(&scull_devices[i].sem,1);
|
||||||
|
/*sema_init 是内核用来新代替dev_INIT 的函数,初始化互斥量*/
|
||||||
|
scull_setup_cdev(&scull_devices[i],i);
|
||||||
|
/*注册每一个设备到总控结构体*/
|
||||||
|
}
|
||||||
|
|
||||||
|
dev = MKDEV(scull_major,scull_minor + scull_nr_devs);
|
||||||
|
//dev += scull_p_init(dev);
|
||||||
|
//dev += scull_access_init(dev);
|
||||||
|
scull_create_proc(); /*创建/proc 下文件*/
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
scull_cleanup_module();
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module_init(scull_init_module);
|
||||||
|
module_exit(scull_cleanup_module);
|
||||||
|
|
||||||
|
|
||||||
169
timer_s/code/scull.h
Normal file
169
timer_s/code/scull.h
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
> File Name: scull.h
|
||||||
|
> Author:
|
||||||
|
> Mail:
|
||||||
|
> Created Time: 2017年03月24日 星期五 11时43分30秒
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _SCULL_H
|
||||||
|
#define _SCULL_H
|
||||||
|
|
||||||
|
#include <linux/ioctl.h> /* needed for the _IOW etc stuff used later */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macros to help debugging
|
||||||
|
*/
|
||||||
|
|
||||||
|
#undef PDEBUG /* undef it, just in case */
|
||||||
|
#ifdef SCULL_DEBUG
|
||||||
|
# ifdef __KERNEL__
|
||||||
|
/* This one if debugging is on, and kernel space */
|
||||||
|
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)
|
||||||
|
# else
|
||||||
|
/* This one for user space */
|
||||||
|
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
# define PDEBUG(fmt, args...) /* not debugging: nothing */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef PDEBUGG
|
||||||
|
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
|
||||||
|
|
||||||
|
#ifndef SCULL_MAJOR
|
||||||
|
#define SCULL_MAJOR 0 /* dynamic major by default */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SCULL_NR_DEVS
|
||||||
|
#define SCULL_NR_DEVS 4 /* scull0 through scull3 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SCULL_P_NR_DEVS
|
||||||
|
#define SCULL_P_NR_DEVS 4 /* scullpipe0 through scullpipe3 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The bare device is a variable-length region of memory.
|
||||||
|
* Use a linked list of indirect blocks.
|
||||||
|
*
|
||||||
|
* "scull_dev->data" points to an array of pointers, each
|
||||||
|
* pointer refers to a memory area of SCULL_QUANTUM bytes.
|
||||||
|
*
|
||||||
|
* The array (quantum-set) is SCULL_QSET long.
|
||||||
|
*/
|
||||||
|
#ifndef SCULL_QUANTUM
|
||||||
|
#define SCULL_QUANTUM 4000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SCULL_QSET
|
||||||
|
#define SCULL_QSET 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The pipe device is a simple circular buffer. Here its default size
|
||||||
|
*/
|
||||||
|
#ifndef SCULL_P_BUFFER
|
||||||
|
#define SCULL_P_BUFFER 4000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Representation of scull quantum sets.
|
||||||
|
*/
|
||||||
|
struct scull_qset {
|
||||||
|
void **data;
|
||||||
|
struct scull_qset *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct scull_dev {
|
||||||
|
struct scull_qset *data; /* Pointer to first quantum set */
|
||||||
|
int quantum; /* the current quantum size */
|
||||||
|
int qset; /* the current array size */
|
||||||
|
unsigned long size; /* amount of data stored here */
|
||||||
|
unsigned int access_key; /* used by sculluid and scullpriv */
|
||||||
|
struct semaphore sem; /* mutual exclusion semaphore */
|
||||||
|
struct cdev cdev; /* Char device structure */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Split minors in two parts
|
||||||
|
*/
|
||||||
|
#define TYPE(minor) (((minor) >> 4) & 0xf) /* high nibble */
|
||||||
|
#define NUM(minor) ((minor) & 0xf) /* low nibble */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The different configurable parameters
|
||||||
|
*/
|
||||||
|
extern int scull_major; /* main.c */
|
||||||
|
extern int scull_nr_devs;
|
||||||
|
extern int scull_quantum;
|
||||||
|
extern int scull_qset;
|
||||||
|
|
||||||
|
extern int scull_p_buffer; /* pipe.c */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Prototypes for shared functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
int scull_p_init(dev_t dev);
|
||||||
|
void scull_p_cleanup(void);
|
||||||
|
int scull_access_init(dev_t dev);
|
||||||
|
void scull_access_cleanup(void);
|
||||||
|
|
||||||
|
int scull_trim(struct scull_dev *dev);
|
||||||
|
|
||||||
|
ssize_t scull_read(struct file *filp, char __user *buf, size_t count,
|
||||||
|
loff_t *f_pos);
|
||||||
|
ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
|
||||||
|
loff_t *f_pos);
|
||||||
|
loff_t scull_llseek(struct file *filp, loff_t off, int whence);
|
||||||
|
int scull_ioctl(struct inode *inode, struct file *filp,
|
||||||
|
unsigned int cmd, unsigned long arg);
|
||||||
|
int scull_open(struct inode *inode,struct file *filp);
|
||||||
|
int scull_release(struct inode *inode,struct file *filp);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ioctl definitions
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Use 'k' as magic number */
|
||||||
|
#define SCULL_IOC_MAGIC 'k'
|
||||||
|
/* Please use a different 8-bit number in your code */
|
||||||
|
|
||||||
|
#define SCULL_IOCRESET _IO(SCULL_IOC_MAGIC, 0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* S means "Set" through a ptr,
|
||||||
|
* T means "Tell" directly with the argument value
|
||||||
|
* G means "Get": reply by setting through a pointer
|
||||||
|
* Q means "Query": response is on the return value
|
||||||
|
* X means "eXchange": switch G and S atomically
|
||||||
|
* H means "sHift": switch T and Q atomically
|
||||||
|
*/
|
||||||
|
#define SCULL_IOCSQUANTUM _IOW(SCULL_IOC_MAGIC, 1, int)
|
||||||
|
#define SCULL_IOCSQSET _IOW(SCULL_IOC_MAGIC, 2, int)
|
||||||
|
#define SCULL_IOCTQUANTUM _IO(SCULL_IOC_MAGIC, 3)
|
||||||
|
#define SCULL_IOCTQSET _IO(SCULL_IOC_MAGIC, 4)
|
||||||
|
#define SCULL_IOCGQUANTUM _IOR(SCULL_IOC_MAGIC, 5, int)
|
||||||
|
#define SCULL_IOCGQSET _IOR(SCULL_IOC_MAGIC, 6, int)
|
||||||
|
#define SCULL_IOCQQUANTUM _IO(SCULL_IOC_MAGIC, 7)
|
||||||
|
#define SCULL_IOCQQSET _IO(SCULL_IOC_MAGIC, 8)
|
||||||
|
#define SCULL_IOCXQUANTUM _IOWR(SCULL_IOC_MAGIC, 9, int)
|
||||||
|
#define SCULL_IOCXQSET _IOWR(SCULL_IOC_MAGIC,10, int)
|
||||||
|
#define SCULL_IOCHQUANTUM _IO(SCULL_IOC_MAGIC, 11)
|
||||||
|
#define SCULL_IOCHQSET _IO(SCULL_IOC_MAGIC, 12)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The other entities only have "Tell" and "Query", because they're
|
||||||
|
* not printed in the book, and there's no need to have all six.
|
||||||
|
* (The previous stuff was only there to show different ways to do it.
|
||||||
|
*/
|
||||||
|
#define SCULL_P_IOCTSIZE _IO(SCULL_IOC_MAGIC, 13)
|
||||||
|
#define SCULL_P_IOCQSIZE _IO(SCULL_IOC_MAGIC, 14)
|
||||||
|
/* ... more to come */
|
||||||
|
|
||||||
|
#define SCULL_IOC_MAXNR 14
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
BIN
timer_s/image/ss.png
Normal file
BIN
timer_s/image/ss.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
Reference in New Issue
Block a user