天天看點

一步一步學習 Linux 驅動之字元裝置 LED

#include <linux/miscdevice.h>  
#include <linux/delay.h>  
#include <asm/irq.h>  
#include <mach/regs-gpio.h>  
#include <mach/hardware.h>  
#include <linux/kernel.h>  
#include <linux/module.h>  
#include <linux/init.h>  
#include <linux/mm.h>  
#include <linux/fs.h>  
#include <linux/types.h>  
#include <linux/delay.h>  
#include <linux/moduleparam.h>  
#include <linux/slab.h>  
#include <linux/errno.h>  
#include <linux/ioctl.h>  
#include <linux/cdev.h>  
#include <linux/string.h>  
#include <linux/list.h>  
#include <linux/pci.h>  
#include <linux/gpio.h>  
#include <asm/uaccess.h>  
#include <asm/atomic.h>  
#include <asm/unistd.h>  
  
  
#define DEVICE_NAME "led0"  
  
static unsigned long led_table [] = {  
    S3C2410_GPB(5),  
    S3C2410_GPB(6),  
    S3C2410_GPB(7),  
    S3C2410_GPB(8),  
};  
/*将I/O口設定為輸出功能*/  
static unsigned int led_cfg_table [] = {  
    S3C2410_GPIO_OUTPUT,  
    S3C2410_GPIO_OUTPUT,  
    S3C2410_GPIO_OUTPUT,  
    S3C2410_GPIO_OUTPUT,  
};  
 /*對LED檔案操作*/   
static int sbc2440_leds_ioctl(  
    struct inode *inode,   
    struct file *file,   
    unsigned int cmd,   
    unsigned long arg)  
{  
    switch(cmd) {  
    case 0:  
    case 1:  
        if (arg > 4) {  
            return -EINVAL;  
        }  
        s3c2410_gpio_setpin(led_table[arg], !cmd);  
        return 0;  
    default:  
        return -EINVAL;  
    }  
}  
/*miscdevice*/
static struct file_operations dev_fops = {  
    .owner  =   THIS_MODULE,  
    .ioctl  =   sbc2440_leds_ioctl,  
};  
  
static struct miscdevice misc = {  
    .minor = MISC_DYNAMIC_MINOR,  
    .name = DEVICE_NAME,  
    .fops = &dev_fops,  
};  
 /*裝置的初始化函數*/
static int __init dev_init(void)  
{  
    int ret;  
  
    int i;  
      
    for (i = 0; i < 4; i++) {  
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);  
        s3c2410_gpio_setpin(led_table[i], 1);//off  
    }  
  
    ret = misc_register(&misc);  
  
    printk (DEVICE_NAME"\tinitialized\n");  
  
    return ret;  
}  
 /*裝置登出程式*/ 
static void __exit dev_exit(void)  
{  
    misc_deregister(&misc);
	printk(DEVICE_NAME"\tclose!!\n");
}  
  
module_init(dev_init);  
module_exit(dev_exit);  
MODULE_LICENSE("GPL");  
MODULE_AUTHOR("FriendlyARM Inc.");  

           
ifneq ($(KERNELRELEASE),)

obj-m :=led.o

else

KERNELDIR :=/home/zuopeng/Desktop/MyLinux

all:
	 make -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
	rm -f *.o *.ko *.mod.o *.mod.c *.symvers modul* *.*~

endif

           
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h> // open() close()
#include <unistd.h> // read() write()
#define LED_ON 1
#define LED_OFF 0

int main(void)
{
    int fd,i;
    fd = open("/dev/led0", 0);
    if (fd < 0)
    {
        printf("open device /lib/modulew/2.6.32.2/myled-ioctl error!\n");
    }
    else
    {
        while(1)
        {
            for(i=0;i<4;i++)
            {
                printf("led%d is on!\n",i);
                ioctl(fd,LED_ON,i);
                 sleep(1);//等待1秒再做下一步操作
                 printf("led%d is off!\n",i);
                ioctl(fd,LED_OFF,i);
                sleep(1);
             }
             if(i==4)
                 i=0;
        }
        close(fd);
    }
    return 0;
}
</span>           
<span style="font-size:14px;">led_test:led_test.c
	arm-linux-gcc -static  led_test.c -o led_test
clean:
	rm -f led_test *.*~
</span>           

http://download.csdn.net/detail/xy010902100449/8594831

 所有的驅動程式都應該對應一個具體的裝置,這個LED驅動當然裝置應該是LED。但是linux将它分成了一類叫做混雜裝置。這類裝置共享一個主裝置号,但次裝置号不同所有混雜裝置形成一個連結清單,要通路一個裝置時根據次裝置号來查找相應的miscdevice。linux中用struct miscdevice來描述一個混雜裝置。程式必須根據自己根檔案系統核心位置來寫MakeFile,否則編譯很容易通過不了。

一步一步學習 Linux 驅動之字元裝置 LED

如圖,在Linux核心中使用cdev結構體來描述字元裝置,通過其成員dev_t來定義裝置号(分為主、次裝置号)以确定字元裝置的唯一性。通過其成員file_operations來定義字元裝置驅動提供給VFS的接口函數,如常見的open()、read()、write()等。

在Linux字元裝置驅動中,子產品加載函數通過register_chrdev_region( ) 或alloc_chrdev_region( )來靜态或者動态擷取裝置号,通過cdev_init( )建立cdev與file_operations之間的連接配接,通過cdev_add( )向系統添加一個cdev以完成注冊。子產品解除安裝函數通過cdev_del( )來登出cdev,通過unregister_chrdev_region( )來釋放裝置号。

使用者空間通路該裝置的程式通過Linux系統調用,如open( )、read( )、write( ),來“調用”file_operations來定義字元裝置驅動提供給VFS的接口函數。

字元裝置驅動模型

一步一步學習 Linux 驅動之字元裝置 LED

繼續閱讀