一. 一个简洁的scull_ch3
在看的第三章 字符设备驱动程序时,发现源码中没有比较简洁的scull,(类似于helloworld,只包含所讲的内容的代码).
下面就整一个scull_ch3,
a. 只包含所讲的内容,文件操作接口只有 open read write release
b. 删去了 proc pipe等以后章节讲的代码
c. 增加了自动创建设备节点,省去了手动mknod的麻烦.
-
#include <linux/module.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/seq_file.h>
-
#include <linux/cdev.h>
-
#include <linux/device.h> /*class device*/
-
-
#include <asm/uaccess.h> /*copy_from_user*/
-
-
#define SCULL_MAJOR 0 /* dynamic major by default */
-
#define SCULL_NR_DEVS 4 /* scull0 through scull3 */
-
#define SCULL_QUANTUM 4000
-
#define SCULL_QSET 1000
-
-
int scull_major = SCULL_MAJOR;
-
int scull_minor = 0;
-
int scull_nr_devs = SCULL_NR_DEVS; /* number of bare scull devices */
-
int scull_quantum = SCULL_QUANTUM;
-
int scull_qset = SCULL_QSET;
-
-
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 */
-
struct cdev cdev; /* Char device structure */
-
struct class *cls;
-
};
-
struct scull_dev *scull_devices;
-
-
int scull_trim(struct scull_dev *dev)
-
{
-
struct scull_qset *next, *dptr;
-
int qset = dev->qset; /* "dev" is not-null */
-
int i;
-
/* all the list items */
-
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;
-
}
-
static int scull_open(struct inode* node, struct file *filp)
-
{
-
struct scull_dev *dev; /* device information */
-
-
dev = container_of(node->i_cdev, struct scull_dev, cdev);
-
filp->private_data = dev;
-
return 0; /* success */
-
}
-
-
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;
-
}
-
-
-
static 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 (*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:
-
return retval;
-
}
-
-
static 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 */
-
-
-
/* 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:
-
return retval;
-
}
-
-
static int scull_release(struct inode *node, struct file* filp)
-
{
-
printk(KERN_ALERT "scull release!\n");
-
return 0;
-
}
-
static const struct file_operations scull_fops =
-
{
-
.owner = THIS_MODULE,
-
.open = scull_open,
-
.read = scull_read,
-
.write = scull_write,
-
.release = scull_release,
-
};
-
-
static void __exit scull_exit(void)
-
{
-
int i;
-
dev_t devno = MKDEV(scull_major, scull_minor);
-
-
printk(KERN_ALERT "goodbye scull dirver\n");
-
/* 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);
-
//以下去掉增加自动创建的设备结点
-
device_destroy(scull_devices[i].cls, MKDEV(scull_major, i));
-
class_destroy(scull_devices[i].cls);
-
}
-
kfree(scull_devices);
-
}
-
unregister_chrdev_region(devno, scull_nr_devs);
-
return ;
-
}
-
-
static void scull_setup_cdev(struct scull_dev *dev, int index)
-
{
-
int err, devno = MKDEV(scull_major, scull_minor + index);
-
char buf[128];
-
memset(buf, 0, sizeof(buf));
-
-
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%d", err, index);
-
//以下增加自动创建设备结点
-
sprintf(buf, "scull_class%d", index);
-
dev->cls = class_create(THIS_MODULE, buf);
-
if(IS_ERR(dev->cls))
-
printk(KERN_ALERT "class create error!\n");
-
-
device_create(dev->cls, NULL, devno, NULL, buf);
-
}
-
-
static int __init scull_init(void)
-
{
-
int result, i;
-
dev_t dev = 0;
-
printk(KERN_NOTICE "scull_init\n");
-
if (scull_major) {
-
dev = MKDEV(scull_major, scull_minor);
-
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(KERN_WARNING "scull: can't get major %d\n", scull_major);
-
goto fail;
-
}
-
-
/* allocate the devices -- we can't have them static, as the number
-
* can be specified at load time */
-
scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);
-
if (!scull_devices) {
-
result = -ENOMEM;
-
goto fail; /* Make this more graceful */
-
}
-
memset(scull_devices, 0, scull_nr_devs * sizeof(struct scull_dev));
-
-
/* Initialize each device. */
-
for (i = 0; i < scull_nr_devs; i++) {
-
scull_devices[i].quantum = scull_quantum;
-
scull_devices[i].qset = scull_qset;
-
scull_setup_cdev(&scull_devices[i], i);
-
}
-
return 0;
-
fail:
-
scull_exit();
-
return result;
-
}
-
-
MODULE_LICENSE("GPL");
-
MODULE_AUTHOR("wangcong02345");
-
module_init(scull_init);
-
module_exit(scull_exit);
附代码下载:
ch3_scull.rar
下载后改名: ch3_scull.tar.gz
阅读(1906) | 评论(0) | 转发(0) |