linux ledドライバルーチン(その他のデバイス)
12865 ワード
#include // module
#include
#include // init
#include
#include
#include
/***gpio ***/
#include
/*******************/
#include
/*********************/
#include
#include
/*************** ******************
extern int platform_driver_register(struct platform_driver *);
extern void platform_driver_unregister(struct platform_driver *);
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct miscdevice {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};
extern int misc_register(struct miscdevice * misc);
extern int misc_deregister(struct miscdevice *misc);
struct file_operations {
struct module *owner;
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
static inline int gpio_request(unsigned gpio, const char *label)
static inline void gpio_free(unsigned gpio)
static inline void gpio_set_value(unsigned gpio, int value)
EXYNOS4_GPIO_L2
s3c_gpio_cfgpin(unsigned int pin, unsigned int to);
S3C_GPIO_OUTPUT
**********************************/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangsan");
#define DRIVER_NAME "hello_ctl"
#define DEVICE_NAME "hello_ctl"
static long hello_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk(KERN_INFO "The cmd is %d,arg is %d
",cmd,arg);
if(cmd>1)
{
printk(KERN_INFO "cmd should be 0 or 1
");
}
if(arg>1)
printk(KERN_INFO "arg must be 1
");
gpio_set_value(EXYNOS4_GPL2(0), cmd);//
return 0;
}
static int hello_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "hello_release
");
return 0;
}
static int hello_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "hello_open
");
return 0;
}
static struct file_operations hello_ops={
.owner=THIS_MODULE,
.open=hello_open,
.release=hello_release,
.unlocked_ioctl=hello_ioctl,
};
static struct miscdevice hello_dev={
.minor=MISC_DYNAMIC_MINOR,
.name=DEVICE_NAME,
.fops=&hello_ops,
};
static int hello_probe(struct platform_device *pdv)
{
int ret;
printk(KERN_INFO "program inited!
");
ret=gpio_request(EXYNOS4_GPL2(0),"LED1");
if(ret<0)
{
printk(KERN_INFO "request gpio failed
");
return ret;
}
s3c_gpio_cfgpin(EXYNOS4_GPL2(0),S3C_GPIO_OUTPUT);
gpio_set_value(EXYNOS4_GPL2(0), 0);//
misc_register(&hello_dev);
return 0;
}
static int hello_remove(struct platform_device *pdv)
{
gpio_free(EXYNOS4_GPL2(0));
misc_deregister(&hello_dev);
return 0;
}
static void hello_shutdown(struct platform_device *pdv)
{
printk(KERN_INFO "hello_shutdown
");
}
static int hello_suspend(struct platform_device *pdv)
{
printk(KERN_INFO "Hello_suspend
");
return 0;
}
static int hello_resume(struct platform_device *pdv)
{
printk(KERN_INFO "Hello_resume
");
return 0;
}
struct platform_driver hello_driver={
.probe=hello_probe,
.remove=hello_remove,
.shutdown=hello_shutdown,
.suspend=hello_suspend,
.resume=hello_resume,
.driver={
.name=DRIVER_NAME,
.owner=THIS_MODULE,
}
};
static int hellodriver_init()
{
int Driverstate;
//printk(KERN_INFO "Hello_init
");
Driverstate=platform_driver_register(&hello_driver);
printk(KERN_INFO "Driverstate is %d
",Driverstate);
return 0;
}
static void hellodriver_exit()
{
printk(KERN_INFO "Hello_exit
");
platform_driver_unregister(&hello_driver);
}
module_init(hellodriver_init);
module_exit(hellodriver_exit);