来自:http://xianzilu.spaces.live.com/blog/cns!4201FDC93932DDAF!211.entry
我写驱动的时候总希望能找到一个样例参考一下,可惜网上的例子基本找不到。还好友善之臂的文档里有些例子,但是说的很不详细,要是直接输入会有 很多的编译错误。我的这个例子是一个控制LED的例子,用Linux就控制LED,当然是相当的弱智的哈哈。我用的是S3C2410,LED连接在 GPB7~10上,灌电流方式驱动,IO配置寄存器GPBCON的物理地址0x56000010,IO数据寄存器GPBDAT的物理地址 0x56000014。程序中的几个关键点,在我昨天的BLOG中有叙述。
首先编写一个叫做leds_test.c的文件,内容如下:
#include <linux/config.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
//Please configure your kernel first to use the following headers, because the directory "asm" is a short cut to your arch's "asm" directory.
//So do the headers in the "hardware" sub directory.
#include <asm/io.h> //This header is for ioremap(), iounmap().
#include <asm/uaccess.h> //This header is for get_user(), put_user().
#define NAME "led_test"
MODULE_AUTHOR("Lu Xianzi <
[email protected]>"); //This line and the following 4 lines can be omitted.
MODULE_DESCRIPTION("LED Test Driver");
MODULE_LICENSE("GPL");
module_param(major, int, 0);
MODULE_PARM_DESC(major, "Major device number");
static int major = 231; //Define device major
unsigned long * pREG; //Definition of register base.
static ssize_t led_test_write(struct file *file, const char __user *data, size_t len, loff_t *ppos)
{
char buf[256];
size_t i;
for (i = 0; i < len && i < 254; i++)
if (get_user(buf[i], data + i))
return -EFAULT;
buf[i] = '/0';
printk("LED Test - write: user_data %s/n", buf);
return (len < 255 ? len : 255);
}
static ssize_t led_test_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
{
char rbuf[4];
size_t i;
long tmp;
tmp = * (volatile unsigned long *)(pREG + 1);
rbuf[0] = tmp % 256;
rbuf[1] = (tmp >> 8) % 256;
rbuf[2] = (tmp >> 16) % 256;
rbuf[3] = (tmp >> 24) % 256;
if (len > 4)
return 0;
for (i = 0; i < len && i < 4; i++)
if (put_user(rbuf[i], buf + i))
return -EFAULT;
printk("LED Test - read/n");
return (len < 4 ? len : 4);
}
static int led_test_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned long tmp;
printk("LED Test - ioctl: param %u %lu/n", cmd, arg);
switch (cmd)
{
case 0:
case 1:
if (arg > 3)
return -EINVAL;
if (!cmd)
* (volatile unsigned long *)(pREG + 1) |= (0x80 << arg);
else
* (volatile unsigned long *)(pREG + 1) &= ~(0x80 << arg);
tmp = * (volatile unsigned long *)pREG;
printk("GPBCON = 0x%lx/n", tmp);
tmp = * (volatile unsigned long *)(pREG + 1);
printk("GPBDAT = 0x%lx/n", tmp);
break;
default:
return -EINVAL;
}
return 1;
}
static int led_test_open(struct inode *inode, struct file *file)
{
unsigned m = iminor(inode);
if (m > 63)
return -EINVAL;
printk("LED Test driver opened!/n");
return nonseekable_open(inode, file);
}
static int led_test_release(struct inode *inode, struct file *file)
{
printk("LED Test driver released!/n");
return 0;
}
static struct file_operations led_test_fops = {
.owner = THIS_MODULE,
.ioctl = led_test_ioctl,
.write = led_test_write,
.read = led_test_read,
.open = led_test_open,
.release = led_test_release,
};
static int __init led_test_init(void)
{
int ret;
unsigned long tmp;
printk("LXZ LED Test Driver./n");
ret = register_chrdev(major, NAME, &led_test_fops);
if (ret < 0) {
printk("Unable to register character device!/n");
return ret;
}
pREG = ioremap(0x56000010, 0x20);
printk("Virtual addr base = 0x%lx/n", (unsigned long)pREG);
tmp = * (volatile unsigned long *)pREG;
printk("GPBCON = 0x%lx/n", tmp);
tmp = * (volatile unsigned long *)(pREG + 1);
printk("GPBDAT = 0x%lx/n", tmp);
printk("Seting LED Test Driver.../n");
* (volatile unsigned long *)pREG = 0x155555;
* (volatile unsigned long *)(pREG + 1) = 0xfff;
tmp = * (volatile unsigned long *)pREG;
printk("GPBCON = 0x%lx/n", tmp);
tmp = * (volatile unsigned long *)(pREG + 1);
printk("GPBDAT = 0x%lx/n", tmp);
printk("LED Test Driver initiated./n");
return 0;
}
static void __exit led_test_cleanup(void)
{
int ret;
iounmap(pREG);
ret = unregister_chrdev(major, NAME);
if (ret < 0)
printk("Unable to register character device!/n");
else
printk("LED Test Driver unloaded!");
}
module_init(led_test_init);
module_exit(led_test_cleanup);
在驱动程序的目录下建立一个名为“Makefile”的文件,其内容只有一行:
obj-m := leds_test.o
值得说明的是,编译模块的make file的文件名必须是Makefile,而不能是makefile
编译之,我的linux内核存在/home/lxz/linux-2.6.11.7,所以编译命令为
make -k -C /home/lxz/linux-2.6.11.7 SUBDIRS=$PWD modules
//或者使用如下模板:
ifneq ($(KERNELRELEASE),)
obj-m := helloworld.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build #直接指定为绝对路径即可
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
关于GNU make工具扩展的说明
在上面的Makefile中使用了
obj-m := 这个赋值语句的含义说明要使用目标文件helloworld.o建立一个模块,最后生成
的模块的名字就是helloworld.ko,如果你有一个名为module.ko的模块依赖于两个文件
file1.o和file2.o,那么我们可以使用module-obj扩展,如下所示
obj-m := module.o
module-objs := file1.o file2.o
//
编译后生成几个文件,其中leds_test.ko是我们需要的驱动模块。
然后在另外一个目录中编写一个叫做leds.c的文件,其内容如下
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd;
int on, led_no;
char buf[256] = {"1234567890"};
unsigned long tmp;
if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2], "%d", &on) != 1 || on < 0 || on > 1 || led_no < 0 || led_no >3)
{
fprintf(stderr, "Usage: leds led_no 0|1/n");
exit(1);
}
fd = open("/dev/leds", O_RDWR);
if (fd < 0)
{
perror("open device leds");
exit(1);
}
ioctl(fd, on, led_no);
write(fd, buf, 10);
read(fd, buf, 4);
tmp = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
printf("User program read: GPBDAT = 0x%lx/n", tmp);
close(fd);
return 0;
}
编译之,输入
arm-linux-gcc -o leds leds.c
然后把生成的leds_test.ko和leds这2个文件拷贝到你的文件系统中,如/home下,启动Linux。之后的过程如下:
/ # cd /home
/home # insmod leds_test.ko
Lxz LED Test Driver.
Virtual addr base = 0xc485e010
GPBCON = 0x44555
GPBDAT = 0x540
Seting LED Test Driver...
GPBCON = 0x155555
GPBDAT = 0x7ff
LED Test Driver initiated.
/home # mknod /dev/leds c 231 0
/home # ./leds 0 1
LED Test driver opened!
LED Test - ioctl: param 1 0
GPBCON = 0x155555
GPBDAT = 0x77f
LED Test - write: user_data 1234567890
LED Test - read
User proLED Test driver released!
gram read: GPBDAT = 0x77f
/home #
这里有个非常有趣的事情,你会发现内核的printk函数比客户程序的printf函数打印时出现一些混乱,我想应该是因为Linux不是一个实时系统,内核和用户程序分时执行的结果。
如果要卸载驱动模块,如下:
/ # rmmod leds_test
LED Test Driver unloaded!
//
注:我再用busybox1.14.2+armgcc4.3.2的时候,发现只能不带ko来rmmod,带了ko反而不好用。不知道为什么。
另外如果ko不是放到/lib/modules/2.xxx,rmmod还有提示,奇怪。知道的大侠请指教一下。