?? leds.c
字號:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <asm/hardware.h>
#include <asm/arch/regs-gpio.h>
#include <linux/device.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#define DEVICE_NAME "led"
#define LED_MAJOR 233
MODULE_AUTHOR("huangweijun");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("the first char device driver");
static unsigned long led_table [] = {
S3C2410_GPB7,
S3C2410_GPB8,
S3C2410_GPB9,
S3C2410_GPB10,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
S3C2410_GPB9_OUTP,
S3C2410_GPB10_OUTP,
};
static int leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 3) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg],!cmd);
break;
default:
return -EINVAL;
}
return 0;
}
static struct file_operations leds_fops = {
.owner = THIS_MODULE,
.ioctl = leds_ioctl,
};
static struct class *led_class;
static int __init leds_init(void)
{
int err = 0;
int i;
if(register_chrdev(LED_MAJOR,"led",&leds_fops)){
printk("led driver:Unable to register driver\n");
return -ENODEV;
}
led_class = class_create(THIS_MODULE, "led");
if(IS_ERR(led_class)){
err = PTR_ERR(led_class);
goto out_chrdev;
}
class_device_create(led_class,MKDEV(LED_MAJOR, 0),NULL,"led");
err = devfs_mk_cdev(MKDEV(LED_MAJOR,0),S_IFCHR | S_IRUGO | S_IWUSR,"led");
if(err)
goto out_class;
for(i=0;i<4;i++){
s3c2410_gpio_cfgpin(led_table[i],led_table[i+4]);
s3c2410_gpio_setpin(led_table[i],1);
}
printk("led driver initialized\n");
goto out;
out_class:
class_device_destroy(led_class,MKDEV(LED_MAJOR, 0));
class_destroy(led_class);
out_chrdev:
unregister_chrdev(LED_MAJOR, "led");
out:
return err;
}
static void __exit leds_exit(void)
{
class_device_destroy(led_class,MKDEV(LED_MAJOR,0));
class_destroy(led_class);
unregister_chrdev(LED_MAJOR,"led");
devfs_remove("led");
printk("led driver removed\n");
}
module_init(leds_init);
module_exit(leds_exit);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -