- 論壇徽章:
- 0
|
jz2440開(kāi)發(fā)板,2.6.22.6內(nèi)核
自己書(shū)寫(xiě)的按鍵驅(qū)動(dòng)程序 好的驅(qū)動(dòng)到我那里能加載成功,也就是整個(gè)系統(tǒng)搭建沒(méi)有問(wèn)題,只是自己書(shū)寫(xiě)的程序有問(wèn)題。
insmod之后出現(xiàn): insmod bottuns.ko
register succsessfull//這里是在入口函數(shù)里加的打印信息,證明進(jìn)入到了入口函數(shù)
insmod: cannot insert 'bottuns.ko': Success (-997531552)
結(jié)果用這個(gè)lsmod查看之后,發(fā)現(xiàn)沒(méi)有掛載上
源碼如下:
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/init.h>
#include<linux/delay.h>
#include<asm/irq.h>
#include<asm/io.h>
#include<asm/arch/regs-gpio.h>
#include<asm/hardware.h>
#include<asm/uaccess.h>
//這定義一個(gè)類(lèi),一會(huì)先建立一個(gè)類(lèi),然后再在類(lèi)的下面
//
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static struct class *button_class;
static struct class_device *button_class_dev;
/*函數(shù)整體規(guī)劃open函數(shù)里設(shè)置管腳
read 函數(shù)里進(jìn)行管腳值的讀取,但是
對(duì)引腳進(jìn)行操作之前要注意一點(diǎn),應(yīng)該將
物理地址映射成虛擬地址
*/
static int button_open(struct inode *inode,struct file *file)
{
/*將引腳配置為輸入
因?yàn)楝F(xiàn)在查詢模式,所以都設(shè)為輸入引腳
GPF0,GPF2.GPG3.GPG11,
*/
*gpfcon&=~((0x3<<(0*2))|(0x3<<(2*2)));
*gpgcon&=~((0x3<<(3*2))|(0x3<<(11*2)));
return 0;
}
ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/*該函數(shù)的作用是讀
也就是返回四個(gè)引腳的點(diǎn)平
所以要讀取dat寄存器
取相應(yīng)的按鍵值*/
unsigned char key_vals[4];
int regval;
if (size != sizeof(key_vals))
return -EINVAL;
/*讀gpf0,2*/
regval=*gpfdat;//先把寄存器整體的值讀回來(lái)
key_vals[0]=(regval&(1<<0))?1:0;
key_vals[1]=(regval&(1<<2))?1:0;
/*讀取這個(gè)gpg3,gpg11*/
key_vals[2]=(regval&(1<<3))?1:0;
key_vals[3]=(regval&(1<<11))?1:0;
/*將數(shù)據(jù)回傳給用戶空間*/
copy_to_user(buf,key_vals,sizeof( key_vals));
return sizeof( key_vals);
}
static struct file_operations button_fops={
.owner= THIS_MODULE,
.open= button_open,
.read=button_read,
};
/*入口函數(shù),
進(jìn)行地質(zhì)映射
加載驅(qū)動(dòng)程序*/
int major;
static int button_init(void)
{
major = register_chrdev(0, "button", &button_fops);
button_class = class_create(THIS_MODULE, "button");
button_class_dev = class_device_create(button_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
printk("register succsessfull\n");
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
}
/*出口函數(shù),卸載驅(qū)動(dòng)程序,取消映射*/
static int button_exit(void)
{
unregister_chrdev(major,"button");
/*剛才是自動(dòng)分配主設(shè)備號(hào)碼,以及創(chuàng)建設(shè)備節(jié)點(diǎn)*/
/*現(xiàn)在要自動(dòng)的移除主設(shè)備號(hào)碼,以及設(shè)備節(jié)點(diǎn)*/
class_device_unregister(button_class_dev);
class_destroy(button_class);
printk("unregister succsesful\n");
iounmap(gpfcon);
iounmap(gpgcon);
}
/*入口函數(shù)和這個(gè)出口函數(shù)都需要修飾一下*/
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
|
|