20130801 linux添加驱动 在dev下可以看到

友善之臂的Tiny210 linux3.0.8 for android

实现:在linux内核下添加LED的驱动 LED@LZM@FJICC并且可以在# /dev/下看到。

#ls /dev/  看到驱动LED@LZM@FJICC

1.进入linux/drivers/char/新建一个文件lzm_fjicc.c

编写内容:

//lzm_fjicc.c

#include <linux/kernel.h>

#include <linux/module.h>

#include <linux/miscdevice.h>

#include <linux/fs.h>

#include <linux/types.h>

#include <linux/moduleparam.h>

#include <linux/slab.h>

#include <linux/ioctl.h>

#include <linux/cdev.h>

#include <linux/delay.h>

#include <mach/gpio.h>

#include <mach/regs-gpio.h>

#include <plat/gpio-cfg.h>

 

#define DEVICE_NAME "LED@LZM@FJICC"

static int led_gpios[] = {

       S5PV210_GPJ2(0),

       S5PV210_GPJ2(1),

       S5PV210_GPJ2(2),

       S5PV210_GPJ2(3),

};

 

#define LED_NUMARRAY_SIZE(led_gpios)

 

 

static long fjicc_leds_ioctl(struct file *filp, unsigned int cmd,

              unsigned long arg)

{

       switch(cmd) {

              case 0:

              case 1:

                     if (arg > LED_NUM) {

                            return -EINVAL;

                     }

 

                     gpio_set_value(led_gpios[arg], !cmd);

                     //printk(DEVICE_NAME": %d %d\n", arg, cmd);

                     break;

 

              default:

                     return -EINVAL;

       }

 

       return 0;

}

 

static struct file_operations fjicc_led_dev_fops = {

       .owner                  = THIS_MODULE,

       .unlocked_ioctl       = fjicc_leds_ioctl,

};

 

static struct miscdevice fjicc_led_dev = {

       .minor                   = MISC_DYNAMIC_MINOR,

       .name                    = DEVICE_NAME,

       .fops                     = &fjicc_led_dev_fops,

};

 

static int __init fjicc_led_dev_init(void) {

       int ret;

       int i;

 

       for (i = 0; i < LED_NUM; i++) {

              ret = gpio_request(led_gpios[i], "LED");

              if (ret) {

                     printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,

                                   led_gpios[i], ret);

                     return ret;

              }

 

              s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);

              gpio_set_value(led_gpios[i], 1);

       }

 

       ret = misc_register(&fjicc_led_dev);

 

       printk(DEVICE_NAME"\tinitialized\n");

 

       return ret;

}

 

static void __exit fjicc_led_dev_exit(void) {

       int i;

 

       for (i = 0; i < LED_NUM; i++) {

              gpio_free(led_gpios[i]);

       }

 

       misc_deregister(&fjicc_led_dev);

}

 

module_init(fjicc_led_dev_init);

module_exit(fjicc_led_dev_exit);

 

MODULE_LICENSE("GPL");

MODULE_AUTHOR("LZM@FJICC");

 

2.然后修改char目录下的KconfigMakefile文件

Kconfig:

config LEDS_LZM_FJICC

       tristate "LEDS_LZM_FJICC"

       depends on MACH_MINI210

       default y

       help

         This option enables support for LEDs connected to GPIO lines

         on Mini210 boards.

 

Makefile:

obj-$(CONFIG_LEDS_LZM_FJICC)   += lzm_fjicc.o

 

3.进到linux根目录,然后#make menuconfig

就可以在配置菜单中看到LEDS_LZM_FJICC,具体在devices driversàchracter devicesà就可以看到了。

4.配置好了,make zImage就可以了。

5.烧写了系统后,就可以查看驱动了。#ls /dev/

6.提升驱动权限:#chmod 777 /dev/LED@LZM@FJICC

你可能感兴趣的:(linux)