#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME "leds" /* 加載模式后,執(zhí)行”cat /proc/devices”命令看到的設備名稱 */
#define LED_MAJOR 231 /* 主設備號 */
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
/* 應用程序執(zhí)行ioctl(fd, cmd, arg)時的第2個參數(shù) */
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
/* 用來指定LED所用的GPIO引腳 */
static unsigned long led_table [] = {
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};
/* 用來指定GPIO引腳的功能:輸出 */
static unsigned int led_cfg_table [] = {
S3C2410_GPB5_OUTP,
S3C2410_GPB6_OUTP,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
};
/* 應用程序對設備文件/dev/leds執(zhí)行open(...)時,
* 就會調用s3c24xx_leds_open函數(shù)
*/
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
uint i;
for (i = 0; i < 4; i++) {
// 設置GPIO引腳的功能:本驅動中LED所涉及的GPIO引腳設為輸出功能 在這個包含文件中有說明#include <asm/arch/regs-gpio.h>
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
}
return 0;
}
/* 應用程序對設備文件/dev/leds執(zhí)行ioclt(...)時,
* 就會調用s3c24xx_leds_ioctl函數(shù)
*/
static int s3c24xx_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:
// 設置指定引腳的輸出電平為0
s3c2410_gpio_setpin(led_table[arg], 0);
return 0;
case IOCTL_LED_OFF:
// 設置指定引腳的輸出電平為1
s3c2410_gpio_setpin(led_table[arg], 1);
return 0;
default:
return -EINVAL;
}
}
/* 這個結構是字符設備驅動程序的核心
* 當應用程序操作設備文件時所調用的open、read、write等函數(shù),
* 最終會調用這個結構中指定的對應函數(shù)
*/
static struct file_operations s3c24xx_leds_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量 */
.open = s3c24xx_leds_open,
.ioctl = s3c24xx_leds_ioctl,
};
/*
* 執(zhí)行“insmod s3c24xx_leds.ko”命令時就會調用這個函數(shù)
*/
static int __init s3c24xx_leds_init(void)
{
int ret;
/* 注冊字符設備驅動程序
* 參數(shù)為主設備號、設備名字、file_operations結構;
* 這樣,主設備號就和具體的file_operations結構聯(lián)系起來了,
* 操作主設備為LED_MAJOR的設備文件時,就會調用s3c24xx_leds_fops中的相關成員函數(shù)
* LED_MAJOR可以設為0,表示由內核自動分配主設備號
*/
ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
/*
* 執(zhí)行”rmmod s3c24xx_leds.ko”命令時就會調用這個函數(shù)
*/
static void __exit s3c24xx_leds_exit(void)
{
/* 卸載驅動程序 */
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}
/* 這兩行指定驅動程序的初始化函數(shù)和卸載函數(shù) */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);
/* 描述驅動程序的一些信息,不是必須的 */
MODULE_AUTHOR("http://www.wikore.com"); // 驅動程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver"); // 一些描述信息
MODULE_LICENSE("GPL"); // 遵循的協(xié)議
測試程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
void usage(char *exename)
{
printf("Usage:\n");
printf(" %s <led_no> <on/off>\n", exename);
printf(" led_no = 1, 2, 3 or 4\n");
}
int main(int argc, char **argv)
{
unsigned int led_no;
int fd = -1;
if (argc != 3)
goto err;
fd = open("/dev/leds", 0); // 打開設備
if (fd < 0) {
printf("Can't open /dev/leds\n");
return -1;
}
led_no = strtoul(argv[1], 0, 0) - 1; // 操作哪個LED?
if (led_no > 3)
goto err;
if (!strcmp(argv[2], "on")) {
ioctl(fd, IOCTL_LED_ON, led_no); // 點亮它
} else if (!strcmp(argv[2], "off")) {
ioctl(fd, IOCTL_LED_OFF, led_no); // 熄滅它
} else {
goto err;
}
close(fd);
return 0;
err:
if (fd > 0)
close(fd);
usage(argv[0]);
return -1;
}