static int led_drv_close(struct inode *node, struct file *file)
{
printk("%s %s line %dn", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
static int __init led_drv_init(void)
{
int err;
major = register_chrdev(0, "myled", &led_drv);
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if(IS_ERR(led_class)) {
unregister_chrdev(major, "myled");
printk(KERN_WARNING "class creatge failed %dn", err);
return -1;
}
printk("%s %sled_drv_initn", __FILE__, __FUNCTION__);
return 0;
}
static void __exit led_drv_exit(void)
{
printk("%s %sled_drv_exitn", __FILE__, __FUNCTION__);
class_destroy(led_class);
unregister_chrdev(major, "myled");
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");
led_drv.h
#ifndef __LED_DRV_H_
#define __LED_DRV_H_
void led_class_create_device(int index);
void led_class_destroy_device(int index);
void register_led_operations(struct led_operations *led_opr);
#endif
led_ops.h
#ifndef __LED_OPS_H_
#define __LED_OPS_H_
struct led_operations {
int (*init)(int which); /* init led, which:led num */
int (*ctl)(int which, char status); /* control led, whic:led num,status:0-On 1-Off */
};
struct led_operations *get_board_led_ops(void);
#endif
led_resource.h
#ifndef _LED_RESOUCE_H
#define _LED_RESOUCE_H
/* bit[31:16] = group */
/* bit[15:0] = which pin */
#define GROUP(x) (x>>16)
#define PIN(x) (x&0xffff)
#define GROUP_PIN(g, p) ((g<<16| (p)))
struct resource *get_led_resource(void);
#endif
led_app.c:
#include
#include
#include
#include
#include
#include
void showUsage(void)
{
printf("app [dev_path] [on,off]n");
}
int main(int argc, char *argv[])
{
char status;
if(argc < 3) {
showUsage();
return -1;
}
int fd = open(argv[1], O_RDWR);
if(fd < 0) {
printf("app open device failed path:%s", argv[1]);
return -1;
}
if(0 == strcmp(argv[2], "on")) {
status = 1;
int ret = write(fd, &status, 1);
if(ret <= 0) {
printf("app 写入设备失败 %s",argv[2]);
返回-1;
} else {
printf("app write device %x", status);
}
} else if(0 == strcmp(argv[2], "off")) {
status = 0;
int ret = write(fd, &status, 1);
if(ret <= 0) {
printf("app write device fialed %s",argv[2]);
返回-1;
} else {
printf("app write device %x", status);
}
}
返回 0;
}
static int led_drv_close(struct inode *node, struct file *file)
{
printk("%s %s line %dn", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
static int __init led_drv_init(void)
{
int err;
major = register_chrdev(0, "myled", &led_drv);
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if(IS_ERR(led_class)) {
unregister_chrdev(major, "myled");
printk(KERN_WARNING "class creatge failed %dn", err);
return -1;
}
printk("%s %sled_drv_initn", __FILE__, __FUNCTION__);
return 0;
}
static void __exit led_drv_exit(void)
{
printk("%s %sled_drv_exitn", __FILE__, __FUNCTION__);
class_destroy(led_class);
unregister_chrdev(major, "myled");
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");
led_drv.h
#ifndef __LED_DRV_H_
#define __LED_DRV_H_
void led_class_create_device(int index);
void led_class_destroy_device(int index);
void register_led_operations(struct led_operations *led_opr);
#endif
led_ops.h
#ifndef __LED_OPS_H_
#define __LED_OPS_H_
struct led_operations {
int (*init)(int which); /* init led, which:led num */
int (*ctl)(int which, char status); /* control led, whic:led num,status:0-On 1-Off */
};
struct led_operations *get_board_led_ops(void);
#endif
led_resource.h
#ifndef _LED_RESOUCE_H
#define _LED_RESOUCE_H
/* bit[31:16] = group */
/* bit[15:0] = which pin */
#define GROUP(x) (x>>16)
#define PIN(x) (x&0xffff)
#define GROUP_PIN(g, p) ((g<<16| (p)))
struct resource *get_led_resource(void);
#endif
led_app.c:
#include
#include
#include
#include
#include
#include
void showUsage(void)
{
printf("app [dev_path] [on,off]n");
}
int main(int argc, char *argv[])
{
char status;
if(argc < 3) {
showUsage();
return -1;
}
int fd = open(argv[1], O_RDWR);
if(fd < 0) {
printf("app open device failed path:%s", argv[1]);
return -1;
}
if(0 == strcmp(argv[2], "on")) {
status = 1;
int ret = write(fd, &status, 1);
if(ret <= 0) {
printf("app 写入设备失败 %s",argv[2]);
返回-1;
} else {
printf("app write device %x", status);
}
} else if(0 == strcmp(argv[2], "off")) {
status = 0;
int ret = write(fd, &status, 1);
if(ret <= 0) {
printf("app write device fialed %s",argv[2]);
返回-1;
} else {
printf("app write device %x", status);
}
}
返回 0;
}
举报