LED驅動實例2

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>


#define DEVICE_NAME     "leds"  /* 加載模式後,執行”cat /proc/devices”命令看到的設備名稱 */
#define LED_MAJOR       231     /* 主設備號 */

/* 應用程序執行ioctl(fd, cmd, arg)時的第2個參數 */
#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

/* 用來指定LED所用的GPIO引腳 */
static unsigned long led_table [] = {
    S3C2410_GPF7,
    S3C2410_GPF6,
    S3C2410_GPF5,
    S3C2410_GPF4,
};

/* 用來指定GPIO引腳的功能:輸出 */
static unsigned int led_cfg_table [] = {
    S3C2410_GPF7_OUTP,
    S3C2410_GPF6_OUTP,
    S3C2410_GPF5_OUTP,
    S3C2410_GPF4_OUTP,
};

#undef NDEBUG
/*Comment/uncomment the following line to disable/enable debugging,
 OR define(or NOT) it in Makefile.
*/
#define NDEBUG

#undef PDEBUG
/* undef it, just in case */
#define VERBOSE

#ifdef NDEBUG
    #ifdef __KERNEL__
        #ifdef VERBOSE
            /* This one if debugging is on, and kernel space */
            #define PDEBUG(fmt, args...) printk( KERN_ERR "%s:%d " fmt, __FILE__, __LINE__, ## args)
        #else
            #define PDEBUG(fmt, args...) printk( KERN_ERR "kdebug: " fmt, ## args)
        #endif
    #else
         /* This one for user space */
        #define PDEBUG(fmt, args...) fprintf(stderr, "%s:%d " fmt,  __FILE__, __LINE__, ## args)
    #endif
#else
     /* not debugging: nothing */
    #define PDEBUG(fmt, args...) do {} while(0)
#endif


/* 應用程序對設備文件/dev/leds執行open(...)時,
 * 就會調用s3c24xx_leds_open函數
 */
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
    int i;
   
    for (i = 0; i < 4; i++) {
        // 設置GPIO引腳的功能:本驅動中LED所涉及的GPIO引腳設爲輸出功能
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
     // 關閉內部上拉
     s3c2410_gpio_pullup(led_table[i], 1);
    }
    return 0;
}

/* 應用程序對設備文件/dev/leds執行ioclt(...)時,
 * 就會調用s3c24xx_leds_ioctl函數
 */
static int s3c24xx_leds_ioctl(
    struct inode *inode,
    struct file *file,
    unsigned int cmd,
    unsigned long arg)
{
    if (arg > 4) {

        return -EINVAL;
    }
   
    switch(cmd) {
    case IOCTL_LED_ON:
        // 設置指定引腳的輸出電平爲0V
        s3c2410_gpio_setpin(led_table[arg], 0);
        return 0;

    case IOCTL_LED_OFF:
        // 設置指定引腳的輸出電平爲3.3V
        s3c2410_gpio_setpin(led_table[arg], 1);
        return 0;

    default:
        return -EINVAL;
    }
}

static inline void s3c24xx_leds_proc_usage(char *exename)
{
    printk(KERN_ERR "Usage:\n");
    printk(KERN_ERR "echo \"<led_no> <on/off>\" > /proc/%s\n", exename);
    printk(KERN_ERR "led_no = 1, 2, 3 or 4\n");
}

static ssize_t s3c24xx_leds_proc_write (struct file *file,
   const char __user *buffer, size_t count, loff_t *offset)
{
 char *buf, *tok, **tokptrs;
 char *whitespace = " ,\t\r\n"; //空格和","分隔
 int ret = -EINVAL, leds_state, leds_no = -1, ntoks;
 int i = 0;

 if (!capable(CAP_SYS_ADMIN))
  return -EPERM;
 if (count == 0)
  return 0;

 if (!(buf = kmalloc(count + 1, GFP_KERNEL)))
  return -ENOMEM;
 if (copy_from_user((void *)buf, buffer, count)) {
  ret = -EFAULT;
  goto out0;
 }
 buf[count] = '\0';

 PDEBUG("s3c24xx_leds: buf= %s\n", buf);
 
 if (!(tokptrs = (char **)get_zeroed_page(GFP_KERNEL))) {
  ret = -ENOMEM;
  goto out1;
 }
 
 ret = -EINVAL;
 ntoks = 0;
 do {
  buf = buf + strspn(buf, whitespace);
  tok = strsep(&buf, whitespace);
  if (*tok == '\0') {
   if (ntoks == 0) {
    goto out1;
   } else
    break;
  }
  PDEBUG("in do_while loop%d buf=%s, tok=%s\n",ntoks, buf, tok);
  if (ntoks == (PAGE_SIZE / sizeof(char **)))
   goto out1;
  tokptrs[ntoks++] = tok;
 } while(buf);
 
 PDEBUG("s3c24xx_leds: ntoks= %d, buf2= %s\n", ntoks, buf);
 
 if (ntoks <2)
   goto out1;
 
 leds_state = -1;

 //The following two lines just for debug
 i= strcmp(tokptrs[ntoks-1], "on");
 PDEBUG("s3c24xx_leds states:%s,strcmp result=%d\n", tokptrs[ntoks-1],i);

 if (strcmp(tokptrs[ntoks-1], "on")==0) {
  leds_state = 1;
 } else if (strcmp(tokptrs[ntoks-1], "off") == 0) {
  leds_state = 0; 
 } else {
  goto out1;
 }
 for (i = 0; i< ntoks-1; i++) {
     leds_no = simple_strtoul(tokptrs[i], NULL, 0);
  PDEBUG("operate led-%d\n", leds_no);
  if(leds_no < 1 || leds_no >4) {
    PDEBUG("Error leds number %d\n", leds_no);
    s3c24xx_leds_proc_usage(DEVICE_NAME);
    continue;
  }
  s3c2410_gpio_setpin(led_table[leds_no-1], 1-leds_state); // LED ON or OFF
  ret = 0;
 }
out1:
 free_page((unsigned long)tokptrs);
out0:
 kfree(buf);
 if (ret == 0)
  return count;
 else {
   s3c24xx_leds_proc_usage(DEVICE_NAME); 
  return ret;
 }
}

/* 這個結構是字符設備驅動程序的核心
 * 當應用程序操作設備文件時所調用的open、read、write等函數,
 * 最終會調用這個結構中指定的對應函數
 */
static struct file_operations s3c24xx_leds_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
    .open   =   s3c24xx_leds_open,    
    .ioctl  =   s3c24xx_leds_ioctl,
};

static struct file_operations s3c24xx_leds_proc_ops = {
    .owner = THIS_MODULE,
    .write = s3c24xx_leds_proc_write,
};

/*
 * 執行“insmod s3c24xx_leds.ko”命令時就會調用這個函數
 */
static int __init s3c24xx_leds_init(void)
{
    int ret;
    struct proc_dir_entry *entry;
   
    /* 註冊字符設備驅動程序
     * 參數爲主設備號、設備名字、file_operations結構;
     * 這樣,主設備號就和具體的file_operations結構聯繫起來了,
     * 操作主設備爲LED_MAJOR的設備文件時,就會調用s3c24xx_leds_fops中的相關成員函數
     * LED_MAJOR可以設爲0,表示由內核自動分配主設備號
     */
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
 
    /*創建proc接口*/
/*struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent)
這是最直接,包裝最少的創建方法。
參數 name 是要創建的 proc 文件名。mode 是該文件權限值,例如 S_IRUGO,可傳入0表示採用系統默認值?
parent 指定該文件的上層 proc 目錄項,如果爲 NULL,表示創建在 /proc 根目錄下。
create_proc_entry() 完成的任務主要包括:檢測 mode 值,分配 proc_dir_entry 結構,註冊 proc_dir_entry*/

    entry = create_proc_entry(DEVICE_NAME, S_IFREG | S_IRUGO, NULL);
    if (!entry)
        return -ENOMEM;

    entry->proc_fops = &s3c24xx_leds_proc_ops;
   
    printk(DEVICE_NAME " initialized\n");
    PDEBUG("LEDS INIT\n");
 return 0;
}

/*
 * 執行”rmmod s3c24xx_leds.ko”命令時就會調用這個函數
 */
static void __exit s3c24xx_leds_exit(void)
{
   remove_proc_entry(DEVICE_NAME, NULL);  //與create_proc_entry函數相反
   /* 卸載驅動程序 */
   unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

/* 這兩行指定驅動程序的初始化函數和卸載函數 */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);

/* 描述驅動程序的一些信息,不是必須的 */
MODULE_AUTHOR("StephenYee, Shenzhen Office, Farsight Inc.,");             // 驅動程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的協議

 

 

應用程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

void usage(char *exename)
{
    printf("Usage:\n");
    printf("    %s <led_no> <on/off>\n", exename);
    printf("    led_no = 1, 2, 3 or 4\n");
}

int main(int argc, char **argv)
{
    unsigned int led_no;
    int fd = -1;
   
    if (argc != 3)
        goto err;
       
    fd = open("/dev/leds", 0);  // 打開設備
    if (fd < 0) {
        printf("Can't open /dev/leds\n");
        return -1;
    }
   
    led_no = strtoul(argv[1], 0, 0) - 1;    // 操作哪個LED?
    if (led_no > 3)
        goto err;
   
    if (!strcmp(argv[2], "on")) {
        ioctl(fd, IOCTL_LED_ON, led_no);    // 點亮它
    } else if (!strcmp(argv[2], "off")) {
        ioctl(fd, IOCTL_LED_OFF, led_no);   // 熄滅它
    } else {
        goto err;
    }
   
    close(fd);
    return 0;
   
err:
    if (fd > 0)
        close(fd);
    usage(argv[0]);
    return -1;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章