您的位置:首页 > 其它

s3c2410下 platform 总线设备和驱动 led_drv.c led_dev.c 和test

2011-10-25 15:13 435 查看
/////led_drv.c

#include <linux/fs.h>

#include <linux/module.h>

#include <linux/errno.h>

#include <linux/kernel.h>

#include <linux/init.h>

#include <linux/cdev.h>

#include <linux/ioport.h>

#include <linux/pci.h>

#include <asm/uaccess.h>

#include <asm/io.h>

#include <asm/irq.h>

#include <linux/interrupt.h>

#include <asm/mach/irq.h>

#include <asm/arch/regs-gpio.h>

#include <linux/poll.h>

#include <linux/platform_device.h>

static int major = 0;

static volatile unsigned long *gpio_con;

static volatile unsigned long *gpio_dat;

static int pin1;

static int pin2;

static struct class *cls;

int led_open(struct inode *inode, struct file *file)

{

    /* 设置对应的引脚为输出引脚 */

    *gpio_con &= ~(0x3 << (pin1 * 2));

    *gpio_con |= (0x1 << (pin1 * 2));

    *gpio_con &= ~(0x3 << (pin2 * 2));

    *gpio_con |= (0x1 << (pin2 * 2));

    return 0;

}

ssize_t led_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)

{

    char ker_buf[1];

    

    /* 根据buf传入的值点/灭灯 */

    /* buf[0] - 亮/灭 , 0 - 亮, 1 - 灭 */

    

    if (size != 1)

    

    {

        

        return -EINVAL;

    

    }

    

    copy_from_user(ker_buf, buf, 1);

    

    

    if ((ker_buf[0] != 0) && (ker_buf[0] != 1))

        

        return -EINVAL;

    

    if (ker_buf[0])

    {

        // 某个LED

        *gpio_dat |= (0x1<< pin1);

        *gpio_dat |= (0x1<< pin2);

    }

    else

    {

        // 某个LED

        *gpio_dat &= ~(0x1<<pin1);

        *gpio_dat &= ~(0x1<<pin2);

    }

    

    return 1;

}

static struct file_operations led_fops = {

    .owner  = THIS_MODULE,

    .open   = led_open,

    .write  = led_write,

};

static int led_probe(struct platform_device *dev)

{

    struct resource *r;

    

    /* 根据平台设备确定点哪一个LED */

    r = platform_get_resource(dev, IORESOURCE_MEM, 0);

    

    gpio_con = ioremap(r->start, r->end - r->start + 1);

    gpio_dat = gpio_con + 1;

    /*第三个参数表示的是哪类资源的哪一个*/

    r = platform_get_resource(dev, IORESOURCE_IRQ, 0);

    pin1 = r->start;

    r = platform_get_resource(dev, IORESOURCE_IRQ, 1);

    pin2 = r->start;

    

    /* 注册驱动 */

    major = register_chrdev(0, "led", &led_fops);

    /* sysfs  ==> 挂接到/sys */

    cls = class_create(THIS_MODULE, "led_class");

    class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

    // mdev会根据/sys下的这些内容创建/dev/led

    return 0;

}

int led_remove(struct platform_device *dev)

{

    unregister_chrdev(major, "led");

    class_device_destroy(cls, MKDEV(major, 0));

    class_destroy(cls);

    

    iounmap(gpio_con);

        

    return 0;

}

static struct platform_driver led_driver = {

    .probe        = led_probe,  /* 平台总线下增加一个平台设备时,调用枚举函数 */

    .remove        = led_remove, /* 平台总线下去掉一个平台设备时,调用remove函数 */

    .driver        = {

        .name    = "myled",    /* 能支持名为"myled"的平台设备 */

        .owner    = THIS_MODULE,

    },

};

        

static int led_drv_init(void)

{

    platform_driver_register(&led_driver);

    return 0;

}

static int led_drv_exit(void)

{

    platform_driver_unregister(&led_driver);

    return 0;

}

module_init(led_drv_init);

module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

//////////////////////////////////////////////////////////////////////////////////////////

//////led_dev.c

#include <linux/fs.h>

#include <linux/module.h>

#include <linux/errno.h>

#include <linux/kernel.h>

#include <linux/init.h>

#include <linux/cdev.h>

#include <linux/ioport.h>

#include <linux/pci.h>

#include <asm/uaccess.h>

#include <asm/io.h>

#include <asm/irq.h>

#include <linux/interrupt.h>

#include <asm/mach/irq.h>

#include <asm/arch/regs-gpio.h>

#include <linux/poll.h>

#include <linux/platform_device.h>

static struct resource led_resource[] = {

    [0] = {

        .start = 0x56000050,

        .end   = 0x56000057,

        .flags = IORESOURCE_MEM,

    },

    [1] = {

        .start = 4,

        .end   = 5,

        .flags = IORESOURCE_IRQ,

    },

    [2] = {

        .start = 5,

        .end   = 5,

        .flags = IORESOURCE_IRQ,

    },

    

};

void led_release(struct device *dev)

{    

}

struct platform_device led_device = {

    .name          = "myled", /* 使用名为"myled"的平台驱动 */

    .id          = -1,

    .dev = {

        .release = led_release,

    },

    .num_resources      = ARRAY_SIZE(led_resource),

    .resource      = led_resource,

};

int led_dev_init(void)

{

    platform_device_register(&led_device);

    return 0;

}

void led_dev_exit(void)

{

    platform_device_unregister(&led_device);

}

module_init(led_dev_init);

module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

///////////////////////////////////////////////////////////////////////

//   test.c  

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

/* led_test <dev> <on|off>

 * led_test /dev/led on

 */

void print_usage(char *str)

{

    printf("Usage:\n");

    printf("%s <dev> <on|off>\n", str);

}

 

int main(int argc, char **argv)

{

    int fd;

    char buf[4];

    int ret;

    int i;

    if (argc != 3 && argc != 2)

    {

        print_usage(argv[0]);

        return -1;

    }

    fd = open(argv[1], O_RDWR);

 
4000
   if (fd < 0)

    {

        printf("can't open %s\n", argv[1]);

        return -1;

    }

    if (strcmp(argv[2], "on") == 0)

    {

        buf[0] = 0;

    }

    else

    {

        buf[0] = 1;

    }

    ret = write(fd, buf, 1);

    if (ret != 1)

    {

        printf("error!\n");

        return -1;

    }

    

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c struct module class 平台 file