linux led驱动例程(杂项设备)

    #include <linux/module.h>//与module相关的信息
    #include <linux/kernel.h>
    #include <linux/init.h>      //与init相关的函数
    #include <linux/platform_device.h>
    #include <linux/miscdevice.h>
    #include <linux/fs.h>
    /***gpio申请和释放***/
    #include <linux/gpio.h>
    /*******************/
    #include <plat/gpio-cfg.h>
    /*********************/
    #include <mach/gpio-exynos4.h>
    #include <mach/gpio.h>
    /***************用到的函数与结构体******************
    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\n",cmd,arg);
        if(cmd>1)
        {
           printk(KERN_INFO "cmd should be 0 or 1\n");
        }
        if(arg>1)   
        printk(KERN_INFO "arg must be 1\n");
        gpio_set_value(EXYNOS4_GPL2(0), cmd);//关闭
        return 0;
    }
    static int hello_release(struct inode *inode, struct file *file)
    {
        printk(KERN_INFO "hello_release\n");    
        return 0;
    }
    static int hello_open(struct inode *inode, struct file *file)
    {
        printk(KERN_INFO "hello_open\n");   
        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!\n");
        ret=gpio_request(EXYNOS4_GPL2(0),"LED1");
        if(ret<0)
        {
           printk(KERN_INFO "request gpio failed\n");
           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\n");   
    }
    static int hello_suspend(struct platform_device *pdv)
    {
        printk(KERN_INFO "Hello_suspend\n");
        return 0;
    }
    static int hello_resume(struct platform_device *pdv)
    {
        printk(KERN_INFO "Hello_resume\n");
        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\n");
        Driverstate=platform_driver_register(&hello_driver);
        printk(KERN_INFO "Driverstate is %d\n",Driverstate);
        return 0;
    }

    static void hellodriver_exit()
    {

        printk(KERN_INFO "Hello_exit\n");
        platform_driver_unregister(&hello_driver);
    }


    module_init(hellodriver_init);
    module_exit(hellodriver_exit);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值