JZ2440笔记:总线驱动设备模型

(1)总线驱动设备模型中的hello world

vi led_dev.c

#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>


static struct resource led_resource[] ={
	[0] = {
		.start = 0x56000050,
		.end = 0x56000050+8-1,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = 4,
		.end = 4,
		.flags = IORESOURCE_IRQ,
	}
};

static void led_release(struct device *pdev)
{
	
}

static struct platform_device led_dev ={
	.name = "myled",
	.id = -1,
	.num_resources = ARRAY_SIZE(led_resource),
	.resource = led_resource,
	.dev = {
		.release = led_release,
	},
};

static int led_dev_init(void)
{
	platform_device_register(&led_dev);
	return 0;
}

static void led_dev_exit(void)
{
	platform_device_unregister(&led_dev);
}

module_init(led_dev_init); 
module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

vi led_drv.c

#include <linux/module.h>
#include <linux/platform_device.h>

static int led_probe(struct platform_device *pdev)
{
	printk("led_probe, found led!\n");
	return 0;
}

static int led_remove(struct platform_device *pdev)
{
	printk("led_remove, remove led!\n");
	return 0;
}



static struct platform_driver led_drv ={
	.probe = led_probe,
	.remove = led_remove,
	.driver = {
		.name = "myled",
	},
};

static int led_drv_init(void)
{
	platform_driver_register(&led_drv);
	return 0;
}

static void led_drv_exit(void)
{
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init); 
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

编译后测试:

# insmod led_drv.ko
led_drv: loading out-of-tree module taints kernel.
# insmod led_dev.ko
led_probe, found led!

(2)总线驱动设备模型(添加字符设备驱动惯例)

vi led_drv.c

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>
#include <asm/io.h>

static int major;
static struct class *led_class;
static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;
static int pin;

static int led_open(struct inode *inode,struct file *file)
{
	*gpfcon &= ~(0x3<<(pin*2));
	*gpfcon |=  (0x1<<(pin*2));
	return 0;
}

static ssize_t led_write(struct file *file,const char __user *buf,size_t len,loff_t *ppos)
{
	char val;
    copy_from_user(&val,buf,len);
	if(val==1)
		*gpfdat &= ~((1<<pin));
	else
		*gpfdat |= ((1<<pin));
	return 0;
}

static struct file_operations led_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.write = led_write,
};


static int led_probe(struct platform_device *pdev)
{
	struct resource *res;
	res = platform_get_resource(pdev,IORESOURCE_MEM,0);
	gpfcon = ioremap(res->start, res->end - res->start + 1);
	gpfdat = gpfcon + 1;
	res = platform_get_resource(pdev,IORESOURCE_IRQ,0);
	pin = res->start;
		
	major = register_chrdev(0,"myled",&led_fops);
	led_class = class_create(THIS_MODULE,"myled");
	device_create(led_class,NULL,MKDEV(major,0),NULL,"led");
	printk("led_probe, found led!\n");
	return 0;
}

static int led_remove(struct platform_device *pdev)
{
	device_destroy(led_class,MKDEV(major,0));
	class_destroy(led_class);
	unregister_chrdev(major,"myled");
	iounmap(gpfcon);
	printk("led_remove, remove led!\n");
	return 0;
}



static struct platform_driver led_drv ={
	.probe = led_probe,
	.remove = led_remove,
	.driver = {
		.name = "myled",
	},
};

static int led_drv_init(void)
{
	platform_driver_register(&led_drv);
	return 0;
}

static void led_drv_exit(void)
{
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init); 
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

vi ledtest.c测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
        int fd;
        char val;

        if(argc!=2)
        {
                printf("Usage: \n");
                printf("%s <on|off>\n",argv[0]);
                return 0;
        }

        fd = open("/dev/led",O_RDWR);
        if(fd<0)
        {
        printf("can't open /dev/led !\n");
                return 0;
        }

        if(strcmp(argv[1],"on")==0)
                val = 1;
        else if(strcmp(argv[1],"off")==0)
                val = 0;
        else
        {
                printf("Usage: \n");
                printf("%s </dev/led?> <on|off>\n",argv[0]);
                return 0;
        }

        write(fd,&val,1);
        return 0;
}

编译后测试:

insmod led_drv.ko
# insmod led_dev.ko
led_probe, found led!
# ls -l /dev/led
crw-rw----    1 0        0         248,   0 Jan  1 16:38 /dev/led
# ./ledtest on
# ./ledtest off
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值