Chinaunix首页 | 论坛 | 博客
  • 博客访问: 292683
  • 博文数量: 109
  • 博客积分: 2116
  • 博客等级: 大尉
  • 技术积分: 1062
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-22 15:38
文章分类

全部博文(109)

文章存档

2013年(2)

2011年(16)

2010年(90)

2009年(1)

我的朋友

分类: 嵌入式

2010-08-08 20:35:12

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
MODULE_AUTHOR("jry");

/* Declaration of memory device functions */
static int memory_init(void);
static int memory_open(struct inode *inode, struct file *filp);
static int memory_release(struct inode *inode, struct file *filp);
static ssize_t memory_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos);
static ssize_t memory_write(struct file *filp, const char *buf,
size_t count, loff_t *f_pos);
static void memory_exit(void);

/* Structure that declares the usual file */
/* access function */
struct file_operations memory_fops =
{
.owner = THIS_MODULE,
.read = memory_read,
.write = memory_write,
.open = memory_open,
.release = memory_release,
};

/* Declaration of the init and exit functions */
module_init(memory_init);
module_exit(memory_exit);


/* Global variables of the driver */


/* Major number */
static int memory_major = 60;

/* Buffer to store data */
static char *memory_buffer;

static int memory_init(void)
{
int result;

/* Registering device */
result = register_chrdev(memory_major, "memory", &memory_fops);
if(result < 0)
{
printk(KERN_ALERT "memory: cannot obtain major nubmer %d\n", memory_major);
return result;
}

/* Allocation memory for the buffer */
memory_buffer = kmalloc(1, GFP_KERNEL);
if (!memory_buffer) {
result = -ENOMEM;
goto fail;
}
memset(memory_buffer, 0, 1);

printk(KERN_ALERT "Inserting memory module.\n");
return 0;

fail:
memory_exit();
return result;
}

static int memory_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}

static int memory_release(struct inode *inode, struct file *filp)
{
return 0;
}

static ssize_t memory_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "read : %c\n", *memory_buffer);
/* Transering data to user space */
if (copy_to_user(buf, memory_buffer, 1)) {
printk("copy_to_user error!\n");
return -EFAULT;
}

/* Changing reading position as best suits */
if (*f_pos == 0) {
*f_pos += 1;
return 1;
} else {
return 0;
}
}

static ssize_t memory_write(struct file *filp, const char *buf,
size_t count, loff_t *f_pos)
{
char *tmp;
char ch;

printk(KERN_ALERT "Write %c\n", *buf);
tmp = (char *)buf + count - 1;
memcpy(&ch, tmp, 1);
printk(KERN_ALERT "%s %c.\n", __func__, ch);
if (copy_from_user(memory_buffer, tmp, 1)) {
printk("copy_from_user error!\n");
}
return 1;
}

static void memory_exit(void)
{
/* Freeing the major number */
unregister_chrdev(memory_major, "memory");

/* Freesing buffer memory */
if(memory_buffer) {
kfree(memory_buffer);
}

printk(KERN_ALERT "Removing memory module\n");
}

// register_chrdevunregister_chrdev 是一对
// kmalloc / kfree


==========================Makefile==========================

obj-m := memory.o
all:
$(MAKE) -C /lib/modules/`uname -r`/build M=`pwd` modules
clean:
rm -fr *.o .*.swp *.mod.c *.mod.o .tmp_versions .*.cmd \
Module.* modules.*

syntax highlighted by , v. 0.9.1
 

==============================测试===============================
装载: cat load.sh
#!/bin/sh
module="memory"
device="mmem"
mode="664"

group="jimi"

/sbin/insmod ./$module.ko $* || exit 1

major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)

rm -f /dev/${device}[0]
mknod /dev/${device}0 c $major 0
ln -sf ${device}0 /dev/${device}
chgrp $group /dev/${device}[0]
chmod $mode  /dev/${device}[0]


卸载 unload.h

#!/bin/sh

module="la_memory"
device="lamem"

rmmod $module
rm -f /dev/$device[0]
rm -f /dev/$device

syntax highlighted by , v. 0.9.1



-------------------------- Make -----------------------
jimi@gentoo ~/LDD3/memory $ make
make -C /lib/modules/`uname -r`/build M=`pwd` modules
make[1]: Entering directory `/usr/src/linux-2.6.29-gentoo-r5'
CC [M] /home/jimi/LDD3/memory/memory.o
/home/jimi/LDD3/memory/memory.c: In function 'memory_read':
/home/jimi/LDD3/memory/memory.c:94: warning: ignoring return value of 'copy_to_user', declared with attribute warn_unused_result
/home/jimi/LDD3/memory/memory.c: In function 'memory_write':
/home/jimi/LDD3/memory/memory.c:117: warning: assignment discards qualifiers from pointer target type
/home/jimi/LDD3/memory/memory.c:120: warning: ignoring return value of 'copy_from_user', declared with attribute warn_unused_result
Building modules, stage 2.
MODPOST 1 modules
CC /home/jimi/LDD3/memory/memory.mod.o
LD [M] /home/jimi/LDD3/memory/memory.ko
make[1]: Leaving directory `/usr/src/linux-2.6.29-gentoo-r5'

--------------------- Have not insmod memory driver --------------------
jimi@gentoo ~/LDD3/memory $ cat /dev/memory
cat: /dev/memory: No such device or address

--------------------- Install ------------------
jimi@gentoo ~/LDD3/memory $ sudo insmod ./memory.ko
jimi@gentoo ~/LDD3/memory $ dmesg | tail -n 1
[24578.024126] Inserting memory module.

--------------------- Test ------------------
jimi@gentoo ~/LDD3/memory $ echo -n abcdef > /dev/memory
jimi@gentoo ~/LDD3/memory $ echo `cat /dev/memory`
f

--------------------- dmesg ------------------
[24676.956192] memory_write f.
[24676.956224] memory_write f.
[24676.956227] memory_write f.
[24676.956230] memory_write f.
[24676.956233] memory_write f.
[24676.956236] memory_write f.
ps : I don't know why the char always is 'f'....-.-!
pps: I have solve, see the code!!
jimi@gentoo ~/LDD3_Learn/memory $ echo -n "123" > /dev/memory0
[ 3077.755905] Write 1
[ 3077.756162] memory_write 3.
[ 3077.756354] Write 2
[ 3077.756481] memory_write 3.
[ 3077.756623] Write 3
[ 3077.756747] memory_write 3.

--------------------- remove -------------------
jimi@gentoo ~/LDD3/memory $ dmesg | tail -n 1
[25791.592932] Removing memory module
阅读(1234) | 评论(0) | 转发(0) |
0

上一篇:NFS相关命令

下一篇:文件和目录

给主人留下些什么吧!~~