📄 main.c
字号:
/* -*- C -*-
* main.c -- the bare scullp char module
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: _main.c.in,v 1.19 2001/07/18 22:28:18 rubini Exp $
*/
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
/* modversion stuff: no #ifdef needed if 2.0 support is not needed */
#ifdef CONFIG_MODVERSIONS
# include <linux/modversions.h>
#endif
#include <linux/kernel.h> /* printk() */
#include <linux/malloc.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 <asm/system.h> /* cli(), *_flags */
#include "scullp.h" /* local definitions */
/*
* I don't use static symbols here, because register_symtab is called
*/
int scullp_major = SCULLP_MAJOR;
int scullp_devs = SCULLP_DEVS; /* number of bare scullp devices */
int scullp_qset = SCULLP_QSET;
int scullp_order = SCULLP_ORDER;
MODULE_PARM(scullp_major, "i");
MODULE_PARM(scullp_devs, "i");
MODULE_PARM(scullp_qset, "i");
MODULE_PARM(scullp_order, "i");
MODULE_AUTHOR("Alessandro Rubini");
ScullP_Dev *scullp_devices; /* allocated in scullp_init */
int scullp_trim(ScullP_Dev *dev);
#ifdef SCULLP_USE_PROC /* don't waste space if unused */
/*
* The proc filesystem: function to read and entry
*/
void scullp_proc_offset(char *buf, char **start, off_t *offset, int *len)
{
if (*offset == 0)
return;
if (*offset >= *len) { /* Not there yet */
*offset -= *len;
*len = 0;
}
else { /* We're into the interesting stuff now */
*start = buf + *offset;
*offset = 0;
}
}
int scullp_read_procmem(char *buf, char **start, off_t offset,
int count, int *eof, void *data)
{
int i, j, order, qset, len = 0;
int limit = count - 80; /* Don't print more than this */
ScullP_Dev *d;
*start = buf;
for(i=0; i<scullp_devs; i++) {
d=&scullp_devices[i];
if (down_interruptible (&d->sem))
return -ERESTARTSYS;
qset=d->qset; /* retrieve the features of each device */
order=d->order;
len += sprintf(buf+len,"\nDevice %i: qset %i, order %i, sz %li\n",
i, qset, order, (long)(d->size));
for (; d; d=d->next) { /* scan the list */
len += sprintf(buf+len," item at %p, qset at %p\n",d,d->data);
scullp_proc_offset (buf, start, &offset, &len);
if (len > limit)
goto out;
if (d->data && !d->next) /* dump only the last item - save space */
for (j=0; j<qset; j++) {
if (d->data[j])
len += sprintf(buf+len," % 4i:%8p\n",j,d->data[j]);
scullp_proc_offset (buf, start, &offset, &len);
if (len > limit)
goto out;
}
}
out:
up (&scullp_devices[i].sem);
if (len > limit)
break;
}
*eof = 1;
return len;
}
#ifdef USE_PROC_REGISTER
static int scullp_get_info (char *buf, char **start, off_t offset,
int len, int unused)
{
int eof = 0;
return scullp_read_procmem(buf, start, offset, len, &eof, NULL);
}
struct proc_dir_entry scullp_proc_entry = {
0, /* low_ino: the inode -- dynamic */
9, "scullpmem", /* len of name and name */
S_IFREG | S_IRUGO, /* mode */
1, 0, 0, /* nlinks, owner, group */
0, NULL, /* size - unused; operations -- use default */
scullp_get_info, /* function used to read data */
/* nothing more */
};
static inline void create_proc_read_entry (const char *name, mode_t mode,
struct proc_dir_entry *base, void *read_func, void *data)
{
proc_register_dynamic (&proc_root, &scullp_proc_entry);
}
static inline void remove_proc_entry (char *name, void *parent)
{
proc_unregister (&proc_root, scullp_proc_entry.low_ino);
}
#endif /* USE_PROC_REGISTER */
#endif /* SCULLP_USE_PROC */
/*
* Open and close
*/
int scullp_open (struct inode *inode, struct file *filp)
{
int num = MINOR(inode->i_rdev);
ScullP_Dev *dev; /* device information */
/* check the device number */
if (num >= scullp_devs) return -ENODEV;
dev = &scullp_devices[num];
/* 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;
scullp_trim(dev); /* ignore errors */
up (&dev->sem);
}
/* and use filp->private_data to point to the device data */
filp->private_data = dev;
MOD_INC_USE_COUNT;
return 0; /* success */
}
int scullp_release (struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
return 0;
}
/*
* Follow the list
*/
ScullP_Dev *scullp_follow(ScullP_Dev *dev, int n)
{
while (n--) {
if (!dev->next) {
dev->next = kmalloc(sizeof(ScullP_Dev), GFP_KERNEL);
memset(dev->next, 0, sizeof(ScullP_Dev));
}
dev = dev->next;
continue;
}
return dev;
}
/*
* Data management: read and write
*/
ssize_t scullp_read (struct file *filp, char *buf, size_t count,
loff_t *f_pos)
{
ScullP_Dev *dev = filp->private_data; /* the first listitem */
ScullP_Dev *dptr;
int quantum = PAGE_SIZE << dev->order;
int 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 nothing;
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 = scullp_follow(dev, item);
if (!dptr->data)
goto nothing; /* don't fill holes */
if (!dptr->data[s_pos])
goto nothing;
if (count > quantum - q_pos)
count = quantum - q_pos; /* read only up to the end of this quantum */
if (copy_to_user (buf, dptr->data[s_pos]+q_pos, count)) {
retval = -EFAULT;
goto nothing;
}
up (&dev->sem);
*f_pos += count;
return count;
nothing:
up (&dev->sem);
return retval;
}
ssize_t scullp_write (struct file *filp, const char *buf, size_t count,
loff_t *f_pos)
{
ScullP_Dev *dev = filp->private_data;
ScullP_Dev *dptr;
int quantum = PAGE_SIZE << dev->order;
int qset = dev->qset;
int itemsize = quantum * qset;
int item, s_pos, q_pos, rest;
ssize_t retval = -ENOMEM; /* our most likely error */
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 = scullp_follow(dev, item);
if (!dptr->data) {
dptr->data = kmalloc(qset * sizeof(void *), GFP_KERNEL);
if (!dptr->data)
goto nomem;
memset(dptr->data, 0, qset * sizeof(char *));
}
/* Here's the allocation of a single quantum */
if (!dptr->data[s_pos]) {
dptr->data[s_pos] =
(void *)__get_free_pages(GFP_KERNEL, dptr->order);
if (!dptr->data[s_pos])
goto nomem;
memset(dptr->data[s_pos], 0, PAGE_SIZE << dptr->order);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -