- 论坛徽章:
- 0
|
- #include <linux/cdev.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/fs.h>
- #include <linux/kernel.h>
- #include <asm/uaccess.h>
- MODULE_LICENSE("Dual BSD/GPL");
- /*scull simple character utility for loading localities*/
- struct scull_qset {
- void **data;
- struct scull_qset *next;
- };
- struct scull_dev {
- struct scull_qset *data;
- int quantum;
- int qset;
- unsigned long size;
- unsigned int access_key;
- struct semaphore sem;
- struct cdev cdev;
- };
- int scull_open(struct inode *inode, struct file *filp);
- 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);
- int scull_release(struct inode *inode, struct file *filp);
- int scull_major = 0;
- static dev_t dev_num;
- int scull_minor = 0;
- int scull_nr_devs = 0;
- struct file_operations scull_fops = {
- .owner = THIS_MODULE,
- .open = scull_open,
- .read = scull_read,
- .write = scull_write,
- .release = scull_release,
- .llseek = NULL,
- .ioctl = NULL,
- };
- int scull_release(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- int scull_open(struct inode *inode, struct file *filp)
- {
- printk("scull open\n");
- return 0;
- }
- ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
- {
- printk("scull read\n");
- return 0;
- }
- ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
- {
- printk("scull write\n");
- return 0;
- }
- static int scull_init(void)
- {
- printk("scull init\n");
- struct scull_dev scull_dev;
- int result, err;
- if (scull_major) {
- dev_num = MKDEV(scull_major, scull_minor);
- result = register_chrdev_region(dev_num, scull_nr_devs, "scull");
- } else {
- result = alloc_chrdev_region(&dev_num, scull_minor, scull_nr_devs, "scull");
- scull_major = MAJOR(dev_num);
- }
- if (result < 0) {
- //printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
- printk("scull: can't get major %d\n", scull_major);
- return result;
- }
- //scull_setup_cdev(&scull_dev, 1);
- cdev_init(&scull_dev.cdev, &scull_fops);
- scull_dev.cdev.owner = THIS_MODULE;
- //dev->cdev.ops = &scull_fops; //jason mark : not need.
- printk("cdev num is %d\n", MAJOR(dev_num), MINOR(dev_num));
- err = cdev_add(&scull_dev.cdev, dev_num, 1);
- if (err)
- printk("--->JASON scull setup cdev err is %d\n", err);
- return 0;
- }
- static void scull_exit(void)
- {
- printk("dev major and minor is %d, %d\n", MAJOR(dev_num), MINOR(dev_num));
- unregister_chrdev_region(dev_num, 1);
- printk("scull exit\n");
- }
- module_init(scull_init);
- module_exit(scull_exit);
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(void)
- {
- int fd, ret;
- char *buf = "a scull test";
- char readbuf[30];
- memset(readbuf, 0, 30);
- fd = open("dev/scull", O_RDWR);
- if (fd < 0) {
- printf("fd is error\n");
- return 1;
- }
-
- ret = write(fd, buf, strlen(buf) + 1);
- if (ret < 0) {
- printf("ret is not goodd\n");
- return 1;
- }
- close(fd);
- fd = open("dev/scull", O_RDONLY);
- ret = read(fd, readbuf, 30);
- printf("%s\n", readbuf);
- close(fd);
- return(0);
- }
复制代码
#makefile :
obj-m := scull.o
KERNELDIR := /lib/modules/2.6.20.20/build
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
test_scull: test_scull.c
@gcc -o test_scull test_scull.c
clean :
rm -rf *.ko
rm -rf *.cmd
rm -rf *.o
我之行的过程是 :
1。 insmod scull.ko
2. mknod /dev/scull 253 c 0
3. ./test_scull
但结果是fd is error , log 信息里边看到 init 又成功,分配的major 是253, 但没有看到open 的信息。。
弄了两天了没弄出来,, 大家帮帮忙。。
原来代码很多的,我简化了一些,方便大家看,但也应该看到open的信息阿。。
[ 本帖最后由 ldy2534 于 2008-1-4 12:16 编辑 ] |
|