arm编译器版本:3.32
内核版本:2.6.28.7
arm号:s3c2440
驱动源码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/device.h>
#include <asm/hardware.h>
#include <asm/regs-gpio.h>
#define DEVICE_NAME "leds"
#define LED_MAJOR 237
dev_t devnm=MKDEV(LED_MAJOR,0);
static struct cdev *leds_cdev;
static struct class *my_class;
MODULE_LICENSE("Dual BSD/GPL");
static int leds_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case 0: s3c2410_gpio_setpin(S3C2410_GPF5,cmd);return 0;break;
case 1: s3c2410_gpio_setpin(S3C2410_GPF5,cmd);return 0;break;
default: return -EINVAL;
}
}
static struct file_operations leds_fops={
.owner=THIS_MODULE,
.ioctl=leds_ioctl,
};
static int leds_cdev_init(struct cdev *dev,int index)
{
int err;
dev->owner=THIS_MODULE;
dev->ops=&leds_fops;
cdev_init(dev,&leds_fops);
err=cdev_add(dev,devnm,1);
if(err)
{
printk("cdev err!\n");
return err;
}
my_class=class_create(THIS_MODULE,DEVICE_NAME);
if(IS_ERR(my_class))
{
printk("err class!\n");
return -1;
}
device_create(my_class,NULL,devnm,NULL,"leds");
return 0;
}
static int leds_init(void)
{
int ret=0;
leds_cdev=cdev_alloc();
if(leds_cdev==NULL)
{
printk(" cdev_alloc err!\n");
return -1;
}
ret=register_chrdev(LED_MAJOR,DEVICE_NAME,&leds_fops);
if(ret<0)
{
printk("DEVICE_NAME cat't register number!\n");
return ret;
}
ret=leds_cdev_init(leds_cdev,0);
s3c2410_gpio_cfgpin(S3C2410_GPF5,S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5,1);
printk(KERN_ALERT "Hello ,world\n");
return 0;
}
static void leds_exit(void)
{
cdev_del(leds_cdev);
unregister_chrdev(LED_MAJOR,DEVICE_NAME);
device_destroy(my_class,devnm);
class_destroy(my_class);
printk(KERN_ALERT "Goodbye ,cruel world\n");
}
module_init(leds_init);
module_exit(leds_exit);
编译Makefile :
obj-m:=leds.o
KDIR:=/home/share/linux
PWD:=$(shell pwd)
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
@rm -rf *.mod.*
@rm -rf .*.cmd
@rm -rf *.o
@rm -rf Module.*
clean:
@rm -rf *.ko
测试程序:
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int main(void)
{
int on=0;
int fp;
fp=open("/dev/leds",0);
if(fp<0)
{
printf("open led err!\n");
return -1;
}
printf("start...................\n");
while(1)
{
printf("helo %d\n",on);
ioctl(fp,on,0);
on=!on;
usleep(400000);
}
close(fp);
return 0;
}