当前位置: 技术问答>linux和unix
[求助] 字符设备驱动,写数据和读数据都有问题,望大家帮忙[已解决,来人接分结贴]。
来源: 互联网 发布时间:2016-08-30
本文导语: 本帖最后由 lisan04 于 2010-05-25 12:32:29 编辑 先上代码: #include #include #include #include #include #include #define MEMSIZE 2048 struct leedriver { struct cdev cdev; unsigned char mem[MEMSIZE]; }; struct leedriver *leedriverp; static int ...
#include
#include
#include
#include
#include
#include
#define MEMSIZE 2048
struct leedriver
{
struct cdev cdev;
unsigned char mem[MEMSIZE];
};
struct leedriver *leedriverp;
static int leedriver_major = 101;
ssize_t leedriver_read(struct file *filp, char __user *buf,
size_t count, loff_t *fops)
{
printk("leedriver_readn");
unsigned long p = *fops;
int ret;
ret = 0;
struct leedriver *dev = filp->private_data;
if (p > MEMSIZE)
goto out;
if (p + count > MEMSIZE)
count = MEMSIZE - p;
if (copy_to_user(buf, dev->mem + p, count))
{
ret = -EFAULT;
}
else
{
*fops += count;
ret = count;
printk("leedriver_read finished...n""-----------------------n");
}
out:
return ret;
}
ssize_t leedriver_write(struct file *filp, char __user *buf,
size_t count, loff_t *fops)
{
printk("leedriver_writen");
unsigned long p =(unsigned long) *fops;
int ret;
ret = 0;
struct leedriver *dev = filp->private_data;
if (p > MEMSIZE)
goto out;
if (p + count > MEMSIZE)
count = MEMSIZE - p;
if (copy_from_user((dev->mem + p), buf, count))
{
ret = -EFAULT;
goto out;
}
else
{
*fops += count;
ret = count;
return ret;
}
out:
return ret;
}
int leedriver_open(struct inode *inode, struct file *filp)
{
filp->private_data = leedriverp;
printk("leedriver_openn");
return 0;
}
int leedriver_release(struct inode *inode, struct file *filp)
{
printk("leedriver_releasen");
return 0;
}
struct file_operations leedriver_fops =
{
.owner = THIS_MODULE,
.read = leedriver_read,
.write = leedriver_write,
.open = leedriver_open,
.release = leedriver_release,
};
static void leedriver_cdev_setup(struct leedriver *dev,int index)
{
int err, devno = MKDEV(leedriver_major,index);
cdev_init(&dev->cdev, &leedriver_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &leedriver_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk("cdev_add failed...n");
}
static int leedriver_init(void)
{
printk("This is printk in stage module init..n");
int result;
dev_t devno = MKDEV(leedriver_major, 0);
if(leedriver_major)
result = register_chrdev_region(devno, 1, "leedriver");
else
{
result = alloc_chrdev_region(&devno, 0, 1, "leedriver" );
leedriver_major = MAJOR(devno);
}
if (result cdev);
kfree(leedriverp);
unregister_chrdev_region(MKDEV(leedriver_major,0),1);
}
module_init(leedriver_init);
module_exit(leedriver_exit);
加载驱动,然后mknod /dev/leedriver c 101 0
再echo 'hello' > /dev/leedriver
出现写错误,错误信息为
May 24 17:23:55 sinyer kernel: [10575.576804] *pde = 17563067 *pte = 00000000
May 24 17:23:55 sinyer kernel: [10575.576822] Modules linked in: scull(P) nls_iso8859_1 nls_cp437 vfat fat binfmt_misc nfsd exportfs nfs lockd nfs_acl auth_rpcgss sunrpc snd_hda_codec_via snd_hda_intel snd_hda_codec snd_hwdep snd_pcm_oss snd_mixer_oss snd_pcm iptable_filter ip_tables x_tables snd_seq_dummy ppdev snd_seq_oss snd_seq_midi snd_rawmidi snd_seq_midi_event parport_pc asus_atk0110 snd_seq snd_timer snd_seq_device psmouse serio_raw snd soundcore lp snd_page_alloc parport fbcon tileblit font bitblit softcursor r8169 mii i915 drm i2c_algo_bit video output intel_agp agpgart [last unloaded: scull]
May 24 17:23:55 sinyer kernel: [10575.576903]
May 24 17:23:55 sinyer kernel: [10575.576909] Pid: 5729, comm: bash Tainted: P D (2.6.31-21-generic #59-Ubuntu) System Product Name
May 24 17:23:55 sinyer kernel: [10575.576914] EIP: 0060:[] EFLAGS: 00010246 CPU: 1
May 24 17:23:55 sinyer kernel: [10575.576920] EIP is at iret_exc+0x712/0x9f8
May 24 17:23:55 sinyer kernel: [10575.576923] EAX: 00000000 EBX: 00000004 ECX: 00000004 EDX: e99b2000
May 24 17:23:55 sinyer kernel: [10575.576928] ESI: 090e4408 EDI: 0000003c EBP: e99b3f4c ESP: e99b3f38
May 24 17:23:55 sinyer kernel: [10575.576932] DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
May 24 17:23:55 sinyer kernel: [10575.576942] 00000004 00000004 00000004 e999a380 090e4408 e99b3f64 f8c370d1 f8c3732b
May 24 17:23:55 sinyer kernel: [10575.576954] e999a380 00000004 090e4408 e99b3f8c c01e84ca e99b3f98 e999a380 f4b5c300
May 24 17:23:55 sinyer kernel: [10575.576966] f8c37080 e99b3f94 e999a380 fffffff7 090e4408 e99b3fac c01e8f8d e99b3f98
May 24 17:23:55 sinyer kernel: [10575.576988] [] ? leedriver_write+0x51/0x80 [scull]
May 24 17:23:55 sinyer kernel: [10575.576996] [] ? vfs_write+0x9a/0x190
May 24 17:23:55 sinyer kernel: [10575.577003] [] ? leedriver_write+0x0/0x80 [scull]
May 24 17:23:55 sinyer kernel: [10575.577009] [] ? sys_write+0x3d/0x70
May 24 17:23:55 sinyer kernel: [10575.577015] [] ? syscall_call+0x7/0xb
May 24 17:23:55 sinyer kernel: [10575.577104] ---[ end trace 85a180e5865bd17c ]---
May 24 17:23:55 sinyer kernel: [10575.577375] leedriver_release
|
来得早不如来得巧阿。。
|
奇怪
接分呵呵
接分呵呵