|
我写的驱动
#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 <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/device.h>
#define DEVICE_NAME "EmbedSky_leds"
#define LED_MAJOR 231
#define IOCTL_LED_ON 1
#define IOCTL_LED_OFF 0
static unsigned long led_table[]=
{
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};
static unsigned int led_cfg_table[]=
{
S3C2410_GPB5_OUTP,
S3C2410_GPB6_OUTP,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
};
static int EmbedSky_leds_open(struct inode *inode, struct file *file)
{
int i;
for(i=0; i<4; i++)
{
s3c2410_gpio_cfgpin(led_table[i],led_cfg_table[i]);
}
return 0;
}
static int EmbedSky_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
if(arg>4)
{
return -EINVAL;
}
switch(cmd)
{
case IOCTL_LED_ON:
s3c2410_gpio_setpin(led_table[arg],0);
return 0;
case IOCTL_LED_OFF:
s3c2410_gpio_setpin(led_table[arg],1);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations EmbedSky_leds_fops =
{
.owner = THIS_MODULE,
.open = EmbedSky_leds_open,
.ioctl = EmbedSky_leds_ioctl,
};
static char __initdata banner[]="TQ2440/SKY2440 ";
static struct class *led_class;
static int __init EmbedSky_leds_init(void)
{
int ret;
printk(banner);
ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &EmbedSky_leds_fops);
if(ret<0)
{
printk(DEVICE_NAME"can't register major number\n");
return ret;
}
led_class = class_create(THIS_MODULE, DEVICE_NAME);
if(IS_ERR(led_class))
{
printk("Err: failed in EmbedSky-leds class.\n");
return -1;
}
device_create(led_class, NULL, MKDEV(LED_MAJOR,0),NULL,DEVICE_NAME);
printk(DEVICE_NAME" initialized\n");
return 0;
}
static void __exit EmbedSky_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
device_destroy(led_class, MKDEV(LED_MAJOR,0));
class_destroy(led_class);
}
module_init(EmbedSky_leds_init);
module_exit(EmbedSky_leds_exit);
|
|