我正在尝试输入简单的scull设备驱动程序,并有下一个问题。
insmod scull.ko
/var/log/kern.log
major:245号码和次要号码:0号码。mknod /dev/scull 245 0
cat /dev/scull
出现重击错误。我错误地做了什么,请帮忙。也许我忘记了什么。
内核日志
Apr 14 19:17:14 XLDD kernel: [ 5765.234335] SCULL: device number registration...
Apr 14 19:17:14 XLDD kernel: [ 5765.234349] SCULL: debug, scull_init 34 device: F500000 MAJOR: 245 MINOR: 0 count: 1
Apr 14 19:17:14 XLDD kernel: [ 5765.234353] SCULL: success device registration
Apr 14 19:17:15 XLDD kernel: [ 5766.384094] SCULL: device number unregistration...
Apr 14 19:17:15 XLDD kernel: [ 5766.384109] SCULL: debug, scull_exit 66 device: F500000 MAJOR: 245 MINOR: 0 count: 1
Apr 14 19:17:15 XLDD kernel: [ 5766.384113] SCULL: done device registration
我尝试做:
root@XLDD:/usr/src# make
make -C /usr/src/linux-headers-4.10.0-42-generic M=`pwd`
make[1]: Entering directory '/usr/src/linux-headers-4.10.0-42-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory '/usr/src/linux-headers-4.10.0-42-generic'
root@XLDD:/usr/src# insmod scull.ko
root@XLDD:/usr/src# mknod /dev/scull c 245 0
root@XLDD:/usr/src# echo "Hello" > /dev/scull
bash: /dev/scull: No such device or address
root@XLDD:/usr/src#
scull.c
#include <linux/module.h> /* MODULE_LICENSE() MODULE_AUTHOR() */
#include <linux/init.h> /* init() exit() */
#include <linux/types.h> /* Used for uint8 e.t.c types */
#include <linux/kdev_t.h> /* Undefined what it is for */
#include <linux/fs.h> /* Used for working with inode */
#include <linux/errno.h> /* Used for errors handling */
#include <linux/cdev.h> /* Used for device registration */
#include "scull.h"
#include "scull_operations.h"
#include "scull_helpers.h"
static int scull_init(void)
{
int alloc_result = 0;
/*!
* Character device registration:
* 0 - success
* negative - unsuccess
*/
printk(KERN_ALERT "SCULL: device number registration...\n");
alloc_result = alloc_chrdev_region(&scull_dev.dev, SCULL_DEV_FIRSTMIRROR, SCULL_DEV_COUNT,SCULL_DEV_NAME);
/*!
* Checking character device number registration
*/
if(alloc_result != 0)
{
printk(KERN_ALERT "SCULL: unsuccess device number registration error code:%d\n",alloc_result);
}
else
{
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s %d device: %X MAJOR: %d MINOR: %d count: %u\n",__func__,__LINE__,scull_dev.dev,MAJOR(scull_dev.dev),MINOR(scull_dev.dev),SCULL_DEV_COUNT);
#endif /* SCULL_DEBUG */
printk(KERN_ALERT "SCULL: success device registration\n");
}
/*!
* Filling scull file operations
*/
scull_fops.owner = THIS_MODULE;
scull_fops.open = scull_open;
scull_fops.read = scull_read;
scull_fops.write = scull_write;
/*!
* Initialize a scull_dev structure
* remembering fops, making it ready to add to the system with cdev_add
*/
cdev_init(&scull_cdev, &scull_fops);
return 0;
}
static void scull_exit(void)
{
/*!
* Remove a character device from the system
*/
cdev_del(&scull_cdev); /*!< Remove a character device from the system */
/*!
* Character device number unregistration:
*/
printk(KERN_ALERT "SCULL: device number unregistration...\n");
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s %d device: %X MAJOR: %d MINOR: %d count: %u\n",__func__,__LINE__,scull_dev.dev,MAJOR(scull_dev.dev),MINOR(scull_dev.dev),SCULL_DEV_COUNT);
#endif /* SCULL_DEBUG */
unregister_chrdev_region(scull_dev.dev, SCULL_DEV_COUNT); /*!< Character device number unregistration */
printk(KERN_ALERT "SCULL: done device registration\n");
}
module_init(scull_init);
module_exit(scull_exit);
scull.h中
#ifndef SCULL_CONFIG_H
#define SCULL_CONFIG_H
#include <linux/types.h> /* struct dev_t */
#include <linux/fs.h> /* file operations */
#include <linux/cdev.h> /* struct cdev */
#define SCULL_DEV_COUNT 1
#define SCULL_DEV_NAME "scull"
#define SCULL_DEV_FIRSTMIRROR 0
MODULE_LICENSE("GNU");
MODULE_AUTHOR("Andriy Veres");
/*!
* data, that store big (12 bits) and little (20 bits)
* device numbers (driver and device numbers correspondly).
*
*/
struct cdev scull_cdev;
struct file_operations scull_fops;
struct scull_dev {
dev_t dev; /* used for device registration */
//struct scull_qset * data; /* Pointer to first quantum */
//int quantum; /* Size of current quantum */
//int qset; /* Size of quantum set */
//unsigned long size; /* Size of data stored hear */
//unsigned int access_key; /* used by sculluid and scullpriv */
//struct semaphore sem; /* Semaphore both exeption */
//struct cdev cdev; /* Symbol device structure */
}scull_dev;
/*!
* Uncomment for debug
*/
#define SCULL_DEBUG
#endif /* SCULL_CONFIG_H */
scull_helpers.c
#include "scull_helpers.h"
int scull_trim(scull_dev * dev)
{
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s %d device: \n",__func__,__LINE__);
#endif /* SCULL_DEBUG */
return 0;
}
scull_helpers.h
#ifndef SCULL_HELPERS_H
#define SCULL_HELPERS_H
#include "scull.h"
int scull_trim(struct scull_dev * dev);
#endif /* SCULL_HELPERS_H */
scull_operations.c
#include "scull_operations.h"
int scull_open(struct inode *inode, struct file *filp)
{
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s\n",__func__,__LINE__);
#endif /* SCULL_DEBUG */
return 0; /* success */
}
int scull_release(struct inode *inode, struct file *filp)
{
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s\n",__func__,__LINE__);
#endif /* SCULL_DEBUG */
return 0;
}
ssize_t scull_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
ssize_t result = count;
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s result: %d\n",__func__,__LINE__,result);
#endif /* SCULL_DEBUG */
return result;
}
ssize_t scull_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
ssize_t result = count;
#ifdef SCULL_DEBUG
printk(KERN_ALERT "SCULL: debug, %s result: %d\n",__func__,__LINE__,result);
#endif /* SCULL_DEBUG */
return result;
}
scull_helpers.h
#ifndef SCULL_OPEARATIONS_H
#define SCULL_OPEARATIONS_H
int (*scull_open) (struct inode *, struct file *);
int (*scull_release) (struct inode *, struct file *);
ssize_t (*scull_read) (struct file *, char *, size_t, loff_t *);
ssize_t (*scull_write) (struct file *, const char *, size_t, loff_t *);
#endif /* SCULL_OPERATIONS_H */