4237
- 收藏
- 点赞
- 分享
- 举报
linux设备驱动篇之LED驱动(二)
************************************************************************************
接 续:linux设备驱动篇之LED驱动(一)
原文声明:原文链接
PC 平 台:ubuntu12.10
开 发 板 :tq2440fl2440
内核版本:linux-3.0
作 者:fulinux
************************************************************************************
说明:这是继上一篇linux设备驱动篇LED设备驱动(一)的一个更为实用的程序,升级版本,更具参考价值
一、内核3.0的LED设备驱动程序:
/*********************************************************************************
* Copyright: (C) 2011 Guo Wenxue<[email]guowenxue@gmail.com[/email]>
* All rights reserved.
*
* Filename: s3c_led.c
* Description: This is the common LED driver runs on S3C24XX.
*
* Version: 1.0.0(10/27/2011~)
* Author: Guo Wenxue <[email]guowenxue@gmail.com[/email]>
* ChangeLog: 1, Release initial version on "10/27/2011 11:39:10 AM"
*
********************************************************************************/
#include "s3c_driver.h"
#define DRV_AUTHOR "Guo Wenxue <[email]guowenxue@gmail.com[/email]>"
#define DRV_DESC "S3C24XX LED driver"
/* Driver version*/
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DEV_NAME DEV_LED_NAME
//#define DEV_MAJOR DEV_LED_MAJOR
#ifndef DEV_MAJOR
#define DEV_MAJOR 0 /* dynamic major by default */
#endif
#define TIMER_TIMEOUT 40
static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;
/* ============================ Platform Device part ===============================*/
/* LED hardware informtation structure*/
struct s3c_led_info
{
unsigned char num; /* The LED number */
unsigned int gpio; /* Which GPIO the LED used */
unsigned char active_level; /* The GPIO pin level(HIGHLEVEL or LOWLEVEL) to turn on or off */
unsigned char status; /* Current LED status: OFF/ON */
unsigned char blink; /* Blink or not */
};
/* The LED platform device private data structure */
struct s3c_led_platform_data
{
struct s3c_led_info *leds;
int nleds;
};
/* LED hardware informtation data*/
static struct s3c_led_info s3c_leds[] = {
[0] = {
.num = 1,
.gpio = S3C2410_GPB(5),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
[1] = {
.num = 2,
.gpio = S3C2410_GPB(6),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[2] = {
.num = 3,
.gpio = S3C2410_GPB(8),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[3] = {
.num = 4,
.gpio = S3C2410_GPB(10),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
};
/* The LED platform device private data */
static struct s3c_led_platform_data s3c_led_data = {
.leds = s3c_leds,
.nleds = ARRAY_SIZE(s3c_leds),
};
struct led_device
{
struct s3c_led_platform_data *data;
struct cdev cdev;
struct class *dev_class;
struct timer_list blink_timer;
} led_device;
static void platform_led_release(struct device * dev)
{
int i;
struct s3c_led_platform_data *pdata = dev->platform_data;
dbg_print("%s():%d\n", __FUNCTION__,__LINE__);
/* Turn all LED off */
for(i=0; inleds; i++)
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
}
static struct platform_device s3c_led_device = {
.name = "s3c_led",
.id = 1,
.dev =
{
.platform_data = &s3c_led_data,
.release = platform_led_release,
},
};
/* ===================== led device driver part ===========================*/
void led_timer_handler(unsigned long data)
{
int i;
struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;
for(i=0; inleds; i++)
{
if(ON == pdata->leds.status)
{
s3c2410_gpio_setpin(pdata->leds.gpio, pdata->leds.active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
if(ENABLE == pdata->leds.blink ) /* LED should blink */
{
/* Switch status between 0 and 1 to turn LED ON or off */
pdata->leds.status = pdata->leds.status ^ 0x01;
}
mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT);
}
}
static int led_open(struct inode *inode, struct file *file)
{
struct led_device *pdev ;
struct s3c_led_platform_data *pdata;
pdev = container_of(inode->i_cdev,struct led_device, cdev);
pdata = pdev->data;
file->private_data = pdata;
return 0;
}
static int led_release(struct inode *inode, struct file *file)
{
return 0;
}
static void print_led_help(void)
{
printk("Follow is the ioctl() command for LED driver:\n");
printk("Enable Driver debug command: %u\n", SET_DRV_DEBUG);
printk("Get Driver verion command : %u\n", GET_DRV_VER);
printk("Turn LED on command : %u\n", LED_ON);
printk("Turn LED off command : %u\n", LED_OFF);
printk("Turn LED blink command : %u\n", LED_BLINK);
}
/* compatible with kernel version >=2.6.38*/
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct s3c_led_platform_data *pdata = file->private_data;
switch (cmd)
{
case SET_DRV_DEBUG:
dbg_print("%s driver debug now.\n", DISABLE == arg ? "Disable" : "Enable");
debug = (0==arg) ? DISABLE : ENABLE;
break;
case GET_DRV_VER:
print_version(DRV_VERSION);
return DRV_VERSION;
case LED_OFF:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = OFF;
pdata->leds[arg].blink = DISABLE;
break;
case LED_ON:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = ON;
pdata->leds[arg].blink = DISABLE;
break;
case LED_BLINK:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].blink = ENABLE;
pdata->leds[arg].status = ON;
break;
default:
dbg_print("%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
print_led_help();
return -EINVAL;
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
static int s3c_led_probe(struct platform_device *dev)
{
struct s3c_led_platform_data *pdata = dev->dev.platform_data;
int result = 0;
int i;
dev_t devno;
/* Initialize the LED status */
for(i=0; inleds; i++)
{
s3c2410_gpio_cfgpin(pdata->leds.gpio, S3C2410_GPIO_OUTPUT);
if(ON == pdata->leds.status)
{
s3c2410_gpio_setpin(pdata->leds.gpio, pdata->leds.active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
}
/* Alloc the device for driver */
if (0 != dev_major)
{
devno = MKDEV(dev_major, dev_minor);
result = register_chrdev_region(devno, 1, DEV_NAME);
}
else
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME);
dev_major = MAJOR(devno);
}
/* Alloc for device major failure */
if (result < 0)
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
/* Initialize button structure and register cdev*/
memset(&led_device, 0, sizeof(led_device));
led_device.data = dev->dev.platform_data;
cdev_init (&(led_device.cdev), &led_fops);
led_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(led_device.cdev), devno , 1);
if (result)
{
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
led_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(led_device.dev_class))
{
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
device_create(led_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
device_create (led_device.dev_class, NULL, devno, DEV_NAME);
#endif
/* Initial the LED blink timer */
init_timer(&(led_device.blink_timer));
led_device.blink_timer.function = led_timer_handler;
led_device.blink_timer.data = (unsigned long)pdata;
led_device.blink_timer.expires = jiffies + TIMER_TIMEOUT;
add_timer(&(led_device.blink_timer));
printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(led_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
static int s3c_led_remove(struct platform_device *dev)
{
dev_t devno = MKDEV(dev_major, dev_minor);
del_timer(&(led_device.blink_timer));
cdev_del(&(led_device.cdev));
device_destroy(led_device.dev_class, devno);
class_destroy(led_device.dev_class);
unregister_chrdev_region(devno, 1);
printk("S3C %s driver removed\n", DEV_NAME);
return 0;
}
static struct platform_driver s3c_led_driver = {
.probe = s3c_led_probe,
.remove = s3c_led_remove,
.driver = {
.name ="s3c_led",
.owner = THIS_MODULE,
},
};
static int __init s3c_led_init(void)
{
int ret = 0;
ret = platform_device_register(&s3c_led_device);
if(ret)
{
printk(KERN_ERR "%s:%d: Can't register platform device %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_dev;
}
dbg_print("Regist S3C LED Platform Device successfully.\n");
ret = platform_driver_register(&s3c_led_driver);
if(ret)
{
printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_drv;
}
dbg_print("Regist S3C LED Platform Driver successfully.\n");
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&s3c_led_driver);
fail_reg_plat_dev:
return ret;
}
static void __init s3c_led_exit(void)
{
dbg_print("%s():%d remove LED platform drvier\n", __FUNCTION__,__LINE__);
platform_driver_unregister(&s3c_led_driver);
dbg_print("%s():%d remove LED platform device\n", __FUNCTION__,__LINE__);
platform_device_unregister(&s3c_led_device);
}
module_init(s3c_led_init);
module_exit(s3c_led_exit);
module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
module_param(dev_minor, int, S_IRUGO);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:S3C24XX_led");
二、Makefile文件:
obj-m := plat_led.o
KERNELDIR := ~/kernel/linux-3.0/ #这个地方填上你开发板所使用的内核路径(内核要make一下才奏效)
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions *odule* $(TARGET)
三、下面是应用程序:
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: test_led.c
* Description: This file is used to test plat_led.ko
*
* Version: 1.0.0(04/15/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/15/2013 04:41:24 PM"
*
********************************************************************************/
#include "plat_ioctl.h"
#include
#include
#include
#include
#include
void main(void)
{
int fd;
fd = open("/dev/led", O_RDWR);
ioctl(fd,LED_ON,0);
ioctl(fd,LED_ON,1);
ioctl(fd,LED_ON,2);
ioctl(fd,LED_BLINK,3);
close(fd);
}
*********************************************************************************************************************
下面这个是另一个测试plat_led.ko的应用程序,这次不同的是它是可以通过命令的形式来控制led
*********************************************************************************************************
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: test_cmd.c
* Description: This file is used to test plat_led.ko
*
* Version: 1.0.0(04/15/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/15/2013 04:41:24 PM"
*
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include "plat_ioctl.h"
void usage(char *name);
int do_ioctl(char *dev_name, int cmd, int arg);
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int opt = 0;
static struct option long_options[] = {
{"ioctl", required_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
while((opt = getopt_long(argc, argv, "i:", long_options,NULL)) != -1 )
{
switch(opt)
{
case 'i':
if(5 != argc )
{
printf(" argc != 5");
}
else
{
do_ioctl(argv[2], atoi(argv[3]), atoi(argv[4]));
}
return 0;
default:
break;
}
}
return 0;
} /* ----- End of main() ----- */
int do_ioctl(char *dev_name, int cmd, int arg)
{
int fd = -1;
int retval;
if(((fd = open(dev_name, O_RDWR)) < 0))
{
printf("Open device \"%s\" failure: %s\n", dev_name, strerror(errno));
return -1;
}
retval = ioctl(fd, cmd, arg);
printf("ioctl(%s, %d, %d) return %d", dev_name, cmd, arg, retval);
close(fd);
return retval;
}
下面是在开发板上执行命令的图片。
****************************************************************************************************************************************
下面试命令形式的又一升级
****************************************************************************************************************************************
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: led_cmd.c
* Description: This file
*
* Version: 1.0.0(04/17/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/17/2013 12:17:28 PM"
*
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include "plat_ioctl.h"
void usage(char *name);
int do_ioctl(char *dev_name, char cmd, int arg);
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int opt = 0;
static struct option long_options[] = {
{"ioctl", required_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
while((opt = getopt_long(argc, argv, "i:", long_options,NULL)) != -1 )
{
switch(opt)
{
case 'i':
if(5 != argc )
{
printf(" argc != 5");
}
else
+ led_cmd.c
{
do_ioctl(argv[2], *argv[3], atoi(argv[4]));
}
return 0;
default:
break;
}
}
return 0;
} /* ----- End of main() ----- */
int do_ioctl(char *dev_name, char cmd, int arg)
{
int fd = -1;
int i;
int retval;
if(((fd = open(dev_name, O_RDWR)) < 0))
{
printf("Open device \"%s\" failure: %s\n", dev_name, strerror(errno));
return -1;
}
switch(cmd)
{
case 'n':
case 'N':
retval = ioctl(fd, LED_ON, arg);
break;
case 'o':
case 'O':
retval = ioctl(fd, LED_OFF, arg);
break;
case 'b':
retval = ioctl(fd, LED_BLINK, arg);
break;
case 'a':
arg = 0;
for(i=0;i<4;i++)
{
retval = ioctl(fd, LED_BLINK, i);
sleep(1);
}
i = 0;
break;
default:
break;
}
//printf("ioctl(%s, %c, %d) return %d", dev_name, cmd, arg, retval);
close(fd);
return retval;
}
全部闪烁:
接 续:linux设备驱动篇之LED驱动(一)
原文声明:原文链接
PC 平 台:ubuntu12.10
开 发 板 :tq2440fl2440
内核版本:linux-3.0
作 者:fulinux
************************************************************************************
说明:这是继上一篇linux设备驱动篇LED设备驱动(一)的一个更为实用的程序,升级版本,更具参考价值
一、内核3.0的LED设备驱动程序:
/*********************************************************************************
* Copyright: (C) 2011 Guo Wenxue<[email]guowenxue@gmail.com[/email]>
* All rights reserved.
*
* Filename: s3c_led.c
* Description: This is the common LED driver runs on S3C24XX.
*
* Version: 1.0.0(10/27/2011~)
* Author: Guo Wenxue <[email]guowenxue@gmail.com[/email]>
* ChangeLog: 1, Release initial version on "10/27/2011 11:39:10 AM"
*
********************************************************************************/
#include "s3c_driver.h"
#define DRV_AUTHOR "Guo Wenxue <[email]guowenxue@gmail.com[/email]>"
#define DRV_DESC "S3C24XX LED driver"
/* Driver version*/
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DEV_NAME DEV_LED_NAME
//#define DEV_MAJOR DEV_LED_MAJOR
#ifndef DEV_MAJOR
#define DEV_MAJOR 0 /* dynamic major by default */
#endif
#define TIMER_TIMEOUT 40
static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;
/* ============================ Platform Device part ===============================*/
/* LED hardware informtation structure*/
struct s3c_led_info
{
unsigned char num; /* The LED number */
unsigned int gpio; /* Which GPIO the LED used */
unsigned char active_level; /* The GPIO pin level(HIGHLEVEL or LOWLEVEL) to turn on or off */
unsigned char status; /* Current LED status: OFF/ON */
unsigned char blink; /* Blink or not */
};
/* The LED platform device private data structure */
struct s3c_led_platform_data
{
struct s3c_led_info *leds;
int nleds;
};
/* LED hardware informtation data*/
static struct s3c_led_info s3c_leds[] = {
[0] = {
.num = 1,
.gpio = S3C2410_GPB(5),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
[1] = {
.num = 2,
.gpio = S3C2410_GPB(6),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[2] = {
.num = 3,
.gpio = S3C2410_GPB(8),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[3] = {
.num = 4,
.gpio = S3C2410_GPB(10),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
};
/* The LED platform device private data */
static struct s3c_led_platform_data s3c_led_data = {
.leds = s3c_leds,
.nleds = ARRAY_SIZE(s3c_leds),
};
struct led_device
{
struct s3c_led_platform_data *data;
struct cdev cdev;
struct class *dev_class;
struct timer_list blink_timer;
} led_device;
static void platform_led_release(struct device * dev)
{
int i;
struct s3c_led_platform_data *pdata = dev->platform_data;
dbg_print("%s():%d\n", __FUNCTION__,__LINE__);
/* Turn all LED off */
for(i=0; i
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
}
static struct platform_device s3c_led_device = {
.name = "s3c_led",
.id = 1,
.dev =
{
.platform_data = &s3c_led_data,
.release = platform_led_release,
},
};
/* ===================== led device driver part ===========================*/
void led_timer_handler(unsigned long data)
{
int i;
struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;
for(i=0; i
{
if(ON == pdata->leds.status)
{
s3c2410_gpio_setpin(pdata->leds.gpio, pdata->leds.active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
if(ENABLE == pdata->leds.blink ) /* LED should blink */
{
/* Switch status between 0 and 1 to turn LED ON or off */
pdata->leds.status = pdata->leds.status ^ 0x01;
}
mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT);
}
}
static int led_open(struct inode *inode, struct file *file)
{
struct led_device *pdev ;
struct s3c_led_platform_data *pdata;
pdev = container_of(inode->i_cdev,struct led_device, cdev);
pdata = pdev->data;
file->private_data = pdata;
return 0;
}
static int led_release(struct inode *inode, struct file *file)
{
return 0;
}
static void print_led_help(void)
{
printk("Follow is the ioctl() command for LED driver:\n");
printk("Enable Driver debug command: %u\n", SET_DRV_DEBUG);
printk("Get Driver verion command : %u\n", GET_DRV_VER);
printk("Turn LED on command : %u\n", LED_ON);
printk("Turn LED off command : %u\n", LED_OFF);
printk("Turn LED blink command : %u\n", LED_BLINK);
}
/* compatible with kernel version >=2.6.38*/
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct s3c_led_platform_data *pdata = file->private_data;
switch (cmd)
{
case SET_DRV_DEBUG:
dbg_print("%s driver debug now.\n", DISABLE == arg ? "Disable" : "Enable");
debug = (0==arg) ? DISABLE : ENABLE;
break;
case GET_DRV_VER:
print_version(DRV_VERSION);
return DRV_VERSION;
case LED_OFF:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = OFF;
pdata->leds[arg].blink = DISABLE;
break;
case LED_ON:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = ON;
pdata->leds[arg].blink = DISABLE;
break;
case LED_BLINK:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].blink = ENABLE;
pdata->leds[arg].status = ON;
break;
default:
dbg_print("%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
print_led_help();
return -EINVAL;
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
static int s3c_led_probe(struct platform_device *dev)
{
struct s3c_led_platform_data *pdata = dev->dev.platform_data;
int result = 0;
int i;
dev_t devno;
/* Initialize the LED status */
for(i=0; i
{
s3c2410_gpio_cfgpin(pdata->leds.gpio, S3C2410_GPIO_OUTPUT);
if(ON == pdata->leds.status)
{
s3c2410_gpio_setpin(pdata->leds.gpio, pdata->leds.active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds.gpio, ~pdata->leds.active_level);
}
}
/* Alloc the device for driver */
if (0 != dev_major)
{
devno = MKDEV(dev_major, dev_minor);
result = register_chrdev_region(devno, 1, DEV_NAME);
}
else
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME);
dev_major = MAJOR(devno);
}
/* Alloc for device major failure */
if (result < 0)
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
/* Initialize button structure and register cdev*/
memset(&led_device, 0, sizeof(led_device));
led_device.data = dev->dev.platform_data;
cdev_init (&(led_device.cdev), &led_fops);
led_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(led_device.cdev), devno , 1);
if (result)
{
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
led_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(led_device.dev_class))
{
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
device_create(led_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
device_create (led_device.dev_class, NULL, devno, DEV_NAME);
#endif
/* Initial the LED blink timer */
init_timer(&(led_device.blink_timer));
led_device.blink_timer.function = led_timer_handler;
led_device.blink_timer.data = (unsigned long)pdata;
led_device.blink_timer.expires = jiffies + TIMER_TIMEOUT;
add_timer(&(led_device.blink_timer));
printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(led_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
static int s3c_led_remove(struct platform_device *dev)
{
dev_t devno = MKDEV(dev_major, dev_minor);
del_timer(&(led_device.blink_timer));
cdev_del(&(led_device.cdev));
device_destroy(led_device.dev_class, devno);
class_destroy(led_device.dev_class);
unregister_chrdev_region(devno, 1);
printk("S3C %s driver removed\n", DEV_NAME);
return 0;
}
static struct platform_driver s3c_led_driver = {
.probe = s3c_led_probe,
.remove = s3c_led_remove,
.driver = {
.name ="s3c_led",
.owner = THIS_MODULE,
},
};
static int __init s3c_led_init(void)
{
int ret = 0;
ret = platform_device_register(&s3c_led_device);
if(ret)
{
printk(KERN_ERR "%s:%d: Can't register platform device %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_dev;
}
dbg_print("Regist S3C LED Platform Device successfully.\n");
ret = platform_driver_register(&s3c_led_driver);
if(ret)
{
printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_drv;
}
dbg_print("Regist S3C LED Platform Driver successfully.\n");
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&s3c_led_driver);
fail_reg_plat_dev:
return ret;
}
static void __init s3c_led_exit(void)
{
dbg_print("%s():%d remove LED platform drvier\n", __FUNCTION__,__LINE__);
platform_driver_unregister(&s3c_led_driver);
dbg_print("%s():%d remove LED platform device\n", __FUNCTION__,__LINE__);
platform_device_unregister(&s3c_led_device);
}
module_init(s3c_led_init);
module_exit(s3c_led_exit);
module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
module_param(dev_minor, int, S_IRUGO);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:S3C24XX_led");
二、Makefile文件:
obj-m := plat_led.o
KERNELDIR := ~/kernel/linux-3.0/ #这个地方填上你开发板所使用的内核路径(内核要make一下才奏效)
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions *odule* $(TARGET)
三、下面是应用程序:
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: test_led.c
* Description: This file is used to test plat_led.ko
*
* Version: 1.0.0(04/15/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/15/2013 04:41:24 PM"
*
********************************************************************************/
#include "plat_ioctl.h"
#include
#include
#include
#include
#include
void main(void)
{
int fd;
fd = open("/dev/led", O_RDWR);
ioctl(fd,LED_ON,0);
ioctl(fd,LED_ON,1);
ioctl(fd,LED_ON,2);
ioctl(fd,LED_BLINK,3);
close(fd);
}
*********************************************************************************************************************
下面这个是另一个测试plat_led.ko的应用程序,这次不同的是它是可以通过命令的形式来控制led
*********************************************************************************************************
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: test_cmd.c
* Description: This file is used to test plat_led.ko
*
* Version: 1.0.0(04/15/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/15/2013 04:41:24 PM"
*
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include "plat_ioctl.h"
void usage(char *name);
int do_ioctl(char *dev_name, int cmd, int arg);
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int opt = 0;
static struct option long_options[] = {
{"ioctl", required_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
while((opt = getopt_long(argc, argv, "i:", long_options,NULL)) != -1 )
{
switch(opt)
{
case 'i':
if(5 != argc )
{
printf(" argc != 5");
}
else
{
do_ioctl(argv[2], atoi(argv[3]), atoi(argv[4]));
}
return 0;
default:
break;
}
}
return 0;
} /* ----- End of main() ----- */
int do_ioctl(char *dev_name, int cmd, int arg)
{
int fd = -1;
int retval;
if(((fd = open(dev_name, O_RDWR)) < 0))
{
printf("Open device \"%s\" failure: %s\n", dev_name, strerror(errno));
return -1;
}
retval = ioctl(fd, cmd, arg);
printf("ioctl(%s, %d, %d) return %d", dev_name, cmd, arg, retval);
close(fd);
return retval;
}
下面是在开发板上执行命令的图片。
****************************************************************************************************************************************
下面试命令形式的又一升级
****************************************************************************************************************************************
/*********************************************************************************
* Copyright: (C) 2013 fulinux<[email]fulinux@sina.com[/email]>
* All rights reserved.
*
* Filename: led_cmd.c
* Description: This file
*
* Version: 1.0.0(04/17/2013~)
* Author: fulinux <[email]fulinux@sina.com[/email]>
* ChangeLog: 1, Release initial version on "04/17/2013 12:17:28 PM"
*
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include "plat_ioctl.h"
void usage(char *name);
int do_ioctl(char *dev_name, char cmd, int arg);
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int opt = 0;
static struct option long_options[] = {
{"ioctl", required_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
while((opt = getopt_long(argc, argv, "i:", long_options,NULL)) != -1 )
{
switch(opt)
{
case 'i':
if(5 != argc )
{
printf(" argc != 5");
}
else
+ led_cmd.c
{
do_ioctl(argv[2], *argv[3], atoi(argv[4]));
}
return 0;
default:
break;
}
}
return 0;
} /* ----- End of main() ----- */
int do_ioctl(char *dev_name, char cmd, int arg)
{
int fd = -1;
int i;
int retval;
if(((fd = open(dev_name, O_RDWR)) < 0))
{
printf("Open device \"%s\" failure: %s\n", dev_name, strerror(errno));
return -1;
}
switch(cmd)
{
case 'n':
case 'N':
retval = ioctl(fd, LED_ON, arg);
break;
case 'o':
case 'O':
retval = ioctl(fd, LED_OFF, arg);
break;
case 'b':
retval = ioctl(fd, LED_BLINK, arg);
break;
case 'a':
arg = 0;
for(i=0;i<4;i++)
{
retval = ioctl(fd, LED_BLINK, i);
sleep(1);
}
i = 0;
break;
default:
break;
}
//printf("ioctl(%s, %c, %d) return %d", dev_name, cmd, arg, retval);
close(fd);
return retval;
}
全部闪烁:
我来回答
回答8个
时间排序
认可量排序
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2013-11-16 18:00:31
-
52014-11-04 16:06:30
-
2008-10-02 20:14:02
-
2015-04-13 15:54:08
-
2012-12-04 11:22:20
-
2013-11-17 19:27:38
-
2018-12-01 13:21:00
-
2017-07-02 10:29:57
-
2008-08-07 19:02:21
-
2013-11-24 22:02:35
-
2012-12-04 11:19:53
-
2019-01-09 13:38:58
-
2017-12-19 20:55:57
-
2013-11-19 20:34:08
-
2018-07-06 16:47:54
-
2015-01-13 22:56:29
-
2017-05-05 09:19:32
-
2015-01-26 22:36:17
-
2020-12-08 15:52:35
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认