#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#define DEVICE_NAME "led0"
static unsigned long led_table [] = {
S3C2410_GPB(5),
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
/*将I/O口设置为输出功能*/
static unsigned int led_cfg_table [] = {
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
/*对LED文件操作*/
static int sbc2440_leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
/*miscdevice*/
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.ioctl = sbc2440_leds_ioctl,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
/*设备的初始化函数*/
static int __init dev_init(void)
{
int ret;
int i;
for (i = 0; i < 4; i++) {
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);//off
}
ret = misc_register(&misc);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
/*设备注销程序*/
static void __exit dev_exit(void)
{
misc_deregister(&misc);
printk(DEVICE_NAME"\tclose!!\n");
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
ifneq ($(KERNELRELEASE),)
obj-m :=led.o
else
KERNELDIR :=/home/zuopeng/Desktop/MyLinux
all:
make -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.o *.ko *.mod.o *.mod.c *.symvers modul* *.*~
endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h> // open() close()
#include <unistd.h> // read() write()
#define LED_ON 1
#define LED_OFF 0
int main(void)
{
int fd,i;
fd = open("/dev/led0", 0);
if (fd < 0)
{
printf("open device /lib/modulew/2.6.32.2/myled-ioctl error!\n");
}
else
{
while(1)
{
for(i=0;i<4;i++)
{
printf("led%d is on!\n",i);
ioctl(fd,LED_ON,i);
sleep(1);//等待1秒再做下一步操作
printf("led%d is off!\n",i);
ioctl(fd,LED_OFF,i);
sleep(1);
}
if(i==4)
i=0;
}
close(fd);
}
return 0;
}
</span>
<span style="font-size:14px;">led_test:led_test.c
arm-linux-gcc -static led_test.c -o led_test
clean:
rm -f led_test *.*~
</span>
http://download.csdn.net/detail/xy010902100449/8594831
所有的驱动程序都应该对应一个具体的设备,这个LED驱动当然设备应该是LED。但是linux将它分成了一类叫做混杂设备。这类设备共享一个主设备号,但次设备号不同所有混杂设备形成一个链表,要访问一个设备时根据次设备号来查找相应的miscdevice。linux中用struct miscdevice来描述一个混杂设备。程序必须根据自己根文件系统内核位置来写MakeFile,否则编译很容易通过不了。
如图,在Linux内核中使用cdev结构体来描述字符设备,通过其成员dev_t来定义设备号(分为主、次设备号)以确定字符设备的唯一性。通过其成员file_operations来定义字符设备驱动提供给VFS的接口函数,如常见的open()、read()、write()等。
在Linux字符设备驱动中,模块加载函数通过register_chrdev_region( ) 或alloc_chrdev_region( )来静态或者动态获取设备号,通过cdev_init( )建立cdev与file_operations之间的连接,通过cdev_add( )向系统添加一个cdev以完成注册。模块卸载函数通过cdev_del( )来注销cdev,通过unregister_chrdev_region( )来释放设备号。
用户空间访问该设备的程序通过Linux系统调用,如open( )、read( )、write( ),来“调用”file_operations来定义字符设备驱动提供给VFS的接口函数。
字符设备驱动模型