我写了一个很简单的字符驱动,在用open系统调用打开设设备文件时出错

techie 2012-04-26 08:18:25
驱动安装#insmod uart.ko //字装之后在/proc/devices下面能看到252 uart 字符设备。

用#mknod /dev/uart c 252 0 在/dev/下面创建了设备节点。

我的程序就出错在open系统调用:fd = open("/dev/uart",O_RDONLY); //我试过把这行给注释掉,这样就没有错了;我不能理解:为什么我创建了设备节点却还是在open时出错了,请高手们指点下!


下面是在运行应用程序后打印出的oops消息:
[root@FriendlyARM app]# ./hello
open start!Unable to handle kernel NULL pointer dereference at virtual address 1
pgd = cd610000
[00000001] *pgd=5d60c831, *pte=00000000, *ppte=00000000
Internal error: Oops: 17 [#1] PREEMPT
last sysfs file: /sys/block/mmcblk0/dev
Modules linked in: uart fa_cpu_pfn(P)
CPU: 0 Tainted: P (2.6.38-FriendlyARM #13)
PC is at chrdev_open+0xe0/0x190
LR is at chrdev_open+0xc8/0x190
pc : [<c01e4e88>] lr : [<c01e4e70>] psr: 20000013
sp : ccfcbe58 ip : 22222222 fp : cd575000
r10: cd15ea80 r9 : c01e4da8 r8 : 00000000
r7 : 00000000 r6 : cd65de88 r5 : cd60bdc0 r4 : cd15ea80
r3 : 00000001 r2 : ccfcbe50 r1 : c0193cd8 r0 : 00000000
Flags: nzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: 00c5387d Table: 5d610008 DAC: 00000015
Process hello (pid: 1122, stack limit = 0xccfca268)
Stack: (0xccfcbe58 to 0xccfcc000)
be40: cd15ea80 00000000
be60: c051c634 00000000 cd6b0280 cd20cbd0 cd60bdc0 c01e031c ccfcbee4 00000000
be80: 00000000 00000000 00000024 ccfca000 40247220 c01ed194 ccfcbee4 00000000
bea0: 00000000 00000101 ccfcbee4 00000024 00000000 c01ed610 00000001 00000001
bec0: c07334e0 00000000 ccfcbee0 ffffff9c 00000000 00000001 ccfcbeec c0183194
bee0: c0787e58 cd6b0280 cd20cbd0 c01a1548 00000002 00000000 00000000 cd019900
bf00: 00000000 cd15ea80 00000101 00000004 00000001 00000000 0000000c 00000000
bf20: cd47d6e8 00000000 00000001 00000000 cd67c624 00000000 00000001 00000001
bf40: 00000000 00000000 cd67c608 cd67c600 befefcf4 c0517474 cd67c608 00000003
bf60: ffffff9c cd575000 00000000 00000000 ccfca000 40247220 befefcf4 c01e1144
bf80: 00000000 00008618 00000000 00000000 00000000 00000005 c0172788 ccfca000
bfa0: 00000000 c01725e0 00000000 00000000 00008618 00000000 40247220 000084d0
bfc0: 00000000 00000000 00000000 00000005 00000000 00000000 40040000 befefcf4
bfe0: 00000000 befefce8 000084f0 401cf43c 60000010 00008618 00000000 00000000
[<c01e4e88>] (chrdev_open+0xe0/0x190) from [<c01e031c>] (__dentry_open.clone.12)
[<c01e031c>] (__dentry_open.clone.12+0x164/0x26c) from [<c01ed194>] (finish_ope)
[<c01ed194>] (finish_open+0x80/0x158) from [<c01ed610>] (do_filp_open+0x11c/0x5)
[<c01ed610>] (do_filp_open+0x11c/0x520) from [<c01e1144>] (do_sys_open+0x58/0xe)
[<c01e1144>] (do_sys_open+0x58/0xe4) from [<c01725e0>] (ret_fast_syscall+0x0/0x)
Code: e5963028 e3530000 01a08007 0a000018 (e5938000)
one_wire_status: 4
---[ end trace 865c88d55446bd44 ]---
Unable to handle kernel NULL pointer dereference at virtual address 00000000
pgd = c0004000
[00000000] *pgd=00000000
Internal error: Oops: 80000007 [#2] PREEMPT
last sysfs file: /sys/block/mmcblk0/dev
Modules linked in: uart fa_cpu_pfn(P)
CPU: 0 Tainted: P D (2.6.38-FriendlyARM #13)
PC is at 0x0
LR is at _raw_spin_unlock_irqrestore+0x18/0x38
pc : [<00000000>] lr : [<c0517444>] psr: 600001b3
sp : ccfcbc5c ip : cd459c20 fp : 00000001
r10: 0000000b r9 : cd459c18 r8 : 00000000
r7 : 00000462 r6 : 00040002 r5 : 00000000 r4 : 00000011
r3 : 00000000 r2 : ccfcbc38 r1 : 20000193 r0 : 00000011
Flags: nZCv IRQs off FIQs on Mode SVC_32 ISA Thumb Segment user
Control: 00c5387d Table: 5d610008 DAC: 00000015
Process hello (pid: 1122, stack limit = 0xccfca268)
Stack: (0xccfcbc5c to 0xccfcc000)
bc40: 00000002
bc60: cd459c50 00000000 ccfcbc94 c0180264 c0180208 cd459c20 c07242f0 00000000
bc80: 00000001 00000017 00000001 00000001 ccfcbca4 c0183194 20000113 c07242f0
bca0: cd459e80 00000001 00000001 00000001 ccfcbcc4 c0183194 00000001 00000001
bcc0: ccfcbcd4 cd459c20 00000011 ccfcbce0 00000000 cd459d18 cd459d18 c018a2cc
bce0: ccfcbce0 ccfcbce0 ccfcbd10 ccfcbe10 cd459c20 ccfca268 00000000 00000017
bd00: 00010000 cd6198f4 cd575000 c0176284 ccfca268 0000000b 00000001 00000017
bd20: cd6198c0 ccfcbe10 00000000 c0178bc8 cd459c20 00000001 ccfcbe10 cd6198c0
bd40: 00000017 c0178dc0 00000001 00000017 00000017 00000007 00000001 ccfcbe10
bd60: c071f50c 20000113 cd15ea80 c0168218 00000004 00000000 20000193 00000000
bd80: cd459c20 00000000 ccfcbdac c018343c c074c33c 20000193 ccef2144 ccef2150
bda0: 00000001 00000000 ccfcbdd4 c017e2fc 00000000 ccef2140 60000113 00000001
bdc0: 00000001 00000004 00000001 00000001 ccfcbde4 c0183194 c072f0e8 60000013
bde0: ffffffff c051637c 22222222 22222222 00000001 00000001 ffffffff ccfcbe44
be00: cd65de88 00000000 00000000 c01721cc 00000000 c0193cd8 ccfcbe50 00000001
be20: cd15ea80 cd60bdc0 cd65de88 00000000 00000000 c01e4da8 cd15ea80 cd575000
be40: 22222222 ccfcbe58 c01e4e70 c01e4e88 20000013 ffffffff cd15ea80 00000000
be60: c051c634 00000000 cd6b0280 cd20cbd0 cd60bdc0 c01e031c ccfcbee4 00000000
be80: 00000000 00000000 00000024 ccfca000 40247220 c01ed194 ccfcbee4 00000000
bea0: 00000000 00000101 ccfcbee4 00000024 00000000 c01ed610 00000001 00000001
bec0: c07334e0 00000000 ccfcbee0 ffffff9c 00000000 00000001 ccfcbeec c0183194
bee0: c0787e58 cd6b0280 cd20cbd0 c01a1548 00000002 00000000 00000000 cd019900
bf00: 00000000 cd15ea80 00000101 00000004 00000001 00000000 0000000c 00000000
bf20: cd47d6e8 00000000 00000001 00000000 cd67c624 00000000 00000001 00000001
bf40: 00000000 00000000 cd67c608 cd67c600 befefcf4 c0517474 cd67c608 00000003
bf60: ffffff9c cd575000 00000000 00000000 ccfca000 40247220 befefcf4 c01e1144
bf80: 00000000 00008618 00000000 00000000 00000000 00000005 c0172788 ccfca000
bfa0: 00000000 c01725e0 00000000 00000000 00008618 00000000 40247220 000084d0
bfc0: 00000000 00000000 00000000 00000005 00000000 00000000 40040000 befefcf4
bfe0: 00000000 befefce8 000084f0 401cf43c 60000010 00008618 00000000 00000000
[<c0517444>] (_raw_spin_unlock_irqrestore+0x18/0x38) from [<cd459c50>] (0xcd459)
Code: bad PC value
---[ end trace 865c88d55446bd45 ]---
Fixing recursive fault but reboot is needed!
one_wire_status: 4
BUG: scheduling while atomic: hello/1122/0x00000002
Modules linked in: uart fa_cpu_pfn(P)
[<c0177c44>] (unwind_backtrace+0x0/0xe4) from [<c0514da0>] (schedule+0x68/0x370)
[<c0514da0>] (schedule+0x68/0x370) from [<c0189ea8>] (do_exit+0xc8/0x5e4)
[<c0189ea8>] (do_exit+0xc8/0x5e4) from [<c0176284>] (die+0x190/0x1c0)
[<c0176284>] (die+0x190/0x1c0) from [<c0178bc8>] (__do_kernel_fault+0x64/0x84)
[<c0178bc8>] (__do_kernel_fault+0x64/0x84) from [<c0178dc0>] (do_page_fault+0x1)
one_wire_status: 2
[<c0178dc0>] (do_page_fault+0x1d8/0x1f4) from [<c01682b4>] (do_PrefetchAbort+0x)
[<c01682b4>] (do_PrefetchAbort+0x34/0x98) from [<c0172350>] (__pabt_svc+0x50/0x)
one_wire_status: 2
Exception stack(0xccfcbc10 to 0xccfcbc58)
bc00: 00000011 20000193 ccfcbc38 00000000
bc20: 00000011 00000000 00040002 00000462 00000000 cd459c18 0000000b 00000001
one_wire_status: 2
bc40: cd459c20 ccfcbc5c c0517444 00000000 600001b3 ffffffff
[<c0172350>] (__pabt_svc+0x50/0xa0) from [<c0517444>] (_raw_spin_unlock_irqrest)
[<c0517444>] (_raw_spin_unlock_irqrestore+0x18/0x38) from [<cd459c50>] (0xcd459)
one_wire_status: 3
...全文
2133 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
Panda_熊猫 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
我试过了
static int uart_open(struct inode * inode, struct file *filp)
{
rGPFCON = (rGPFCON & ~(0x0fU<<28))|(0x5U<<28);
rGPFPUD = (rGPFPUD & ~(0x0fU<<28))|(0xaU<<28);

return 0;
}
代码里面的内容……
[/Quote]

操作的地址是错的。
colwer 2012-10-09
  • 打赏
  • 举报
回复
看下你的 /dev/uart 的权限先
tanjinc 2012-09-16
  • 打赏
  • 举报
回复
你试着把错误码打印出来,即errno, 因为open错误的话,会自动的给errno赋值,再查看对于的错误信息。我的经验
lwfbibi 2012-09-10
  • 打赏
  • 举报
回复
读写寄存器之前需要进行IO内存映射
centimetre13 2012-09-07
  • 打赏
  • 举报
回复
试试sudo
lanlang12 2012-05-30
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 的回复:]
不知道你open函数里面是干啥的,但是看到你static int __init uart_init()函数里面好像也有点问题。
应该这样申请吧:
if(uart_major){
dev_t dev = MKDEV(uart_major,0);
result=register_chrdev_region(dev,1,NAME);
}
else
{
printk(KERN_……
[/Quote]

if(result<0)判断语句放外面
lanlang12 2012-05-30
  • 打赏
  • 举报
回复
不知道你open函数里面是干啥的,但是看到你static int __init uart_init()函数里面好像也有点问题。
应该这样申请吧:
if(uart_major){
dev_t dev = MKDEV(uart_major,0);
result=register_chrdev_region(dev,1,NAME);
}
else
{
printk(KERN_ALERT "alloc char dev region!");
result = alloc_chrdev_region(&dev,0,1,NAME);
uart_major = MAJOR(dev);
uart_minor = MINOR(dev);
if(result<0)
return result;
}
这是LDD3中的流程
falloutmx 2012-05-28
  • 打赏
  • 举报
回复
说明还没执行到uart_open呗,找找tty_open.建议你学习下tty驱动的流程,方便查找问题
techie 2012-05-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

引用 17 楼 的回复:
谢谢,大家,我有点不明白了。为什么直接用指针不行呢,是出于安全设计而用虚拟地址和物理地址吗?那得用什么方法来更改寄存器的值?,


涉及I/O端口 I/O端口 I/O空间等等概念。 18楼的LDD3给出了详细说明。LZ自己看吧。
[/Quote]
前两天开发板没有在手边,我还没有来得及试下,今天试了下,结果是:还是不行啊!
运行应用程序,得到输出结果是:
open start!
open error: -1

我在uart_open里加了一行 printk(KERN_ALERT "RUN OPEN!"); 也没有显示RUN OPEN!
都不清楚到底如何查出问题在哪些里,
曹大夯 2012-05-25
  • 打赏
  • 举报
回复
访问物理地址或者物理端口是有固定的操作方式的。参考
Communicating with Hardware


  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]
谢谢,大家,我有点不明白了。为什么直接用指针不行呢,是出于安全设计而用虚拟地址和物理地址吗?那得用什么方法来更改寄存器的值?,
[/Quote]

涉及I/O端口 I/O端口 I/O空间等等概念。 18楼的LDD3给出了详细说明。LZ自己看吧。
techie 2012-05-24
  • 打赏
  • 举报
回复
谢谢,大家,我有点不明白了。为什么直接用指针不行呢,是出于安全设计而用虚拟地址和物理地址吗?那得用什么方法来更改寄存器的值?,
  • 打赏
  • 举报
回复
同意14楼. 先把uart_open函数里面的内容去掉,加一个printk打印。 如果该函数执行正常,
那么应用层的open就是正确的。 (因为你uart_open里面那几句有可能有错~~)
这样先排除一个问题。

我也注意到你是动态申请设备号的,dev_t dev = MKDEV(uart_major,0);
我以前写的都比较偷懒,使用的旧版本的注册方式,新的还没用过,你可以试下旧的。
我空间里GPIO模拟MDC/MDIO的那个帖子里,应该有注册部分的。
elalll 2012-05-14
  • 打赏
  • 举报
回复
换个设备号试试 看看是不是设备号重了
xuyinglin 2012-05-09
  • 打赏
  • 举报
回复
我试过了
static int uart_open(struct inode * inode, struct file *filp)
{
rGPFCON = (rGPFCON & ~(0x0fU<<28))|(0x5U<<28);
rGPFPUD = (rGPFPUD & ~(0x0fU<<28))|(0xaU<<28);

return 0;
}
代码里面的内容有问题 我也不知道你想干嘛 但是把内容屏蔽掉就可以了
qinzhe009 2012-05-09
  • 打赏
  • 举报
回复
问个题外话。为什么Makefile里面要用obj-m?
techie 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

应该是你静态指定的设备号有问题,你试试动态申请设备号看行不行?
[/Quote]
设备号就是动态的,因为我指定的是0 而申请到的是252,说明用到的是动态申请了.高手帮我检查下,我这个设备的注册有没得问题
希蒙 2012-04-29
  • 打赏
  • 举报
回复
应该是你静态指定的设备号有问题,你试试动态申请设备号看行不行?
techie 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

难道没有返回值 还是return 1 当成了 return 0?
[/Quote]

我觉得可能是注册设备驱动出了错可是我也没得一个详细的正确操作step做参考。纠结~~
falloutmx 2012-04-28
  • 打赏
  • 举报
回复
是怎么解决的?
加载更多回复(8)

21,597

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧