SAMSUNG2440简易LED驱动
扫描二维码
随时随地手机看文章
进入驱动学习后写的一个针对我们用的2440板子的LED驱动,应用程序略去了,主要通过GPIO_data结构体传递控制信息,支持多线程。
/* Ioctl_c.h*/
#ifndef __IOCTL_C_H__
#define __IOCTL_C_H__
typedef struct GPIO_data
{
char port;
unsigned int bit;
unsigned int value;
}GPIO_data;
#define GPIO_IOC_MAGIC 15
#define GPIO_IO_SET_CFG _IOW(GPIO_IOC_MAGIC,0,sizeof(GPIO_data))
#define GPIO_IO_GET_CFG _IOWR(GPIO_IOC_MAGIC,1,sizeof(GPIO_data))
#define GPIO_IO_WRITE _IOW(GPIO_IOC_MAGIC,2,sizeof(GPIO_data))
#define GPIO_IO_READ _IOWR(GPIO_IOC_MAGIC,3,sizeof(GPIO_data))
#define PORT_LENGTH 0x10
#define DAT_OFFSET 0x04
#define CFG_OFFSET 0
#endif
/* mydev.c */
#include
#include
#include
#include
#include
#include
#include
#include ccess.h>
#include
#include
#include
#include
#include
#include gpio.h>
#include
#include
#include "ioctl_c.h"
#include
unsigned int test_major=253;
unsigned int test_minor=0;
struct cdev cdevc;
MODULE_LICENSE("Dual BSD/GPL");
struct semaphore my_lock;
static int gpio_write(unsigned long ptr)
{
unsigned long regs_offset=0,data_regs_addr;
int original=0,recent=0;
struct GPIO_data *struct_ptr;
down_interruptible(&my_lock);
//copy_from_user(struct_ptr,(struct GPIO_data *)ptr,sizeof(struct GPIO_data));
struct_ptr = (struct GPIO_data *)ptr;
regs_offset = (int)(struct_ptr->port)-97;
data_regs_addr = regs_offset*PORT_LENGTH+DAT_OFFSET;
original = __raw_readl(S3C2410_GPIOREG(data_regs_addr));
recent = (original & ~(1<<(struct_ptr->bit)))|((struct_ptr->value)<<(struct_ptr->bit));
__raw_writel(recent,S3C2410_GPIOREG(data_regs_addr));
up(&my_lock);
return 0;
}
static int gpio_read(unsigned long ptr)
{
unsigned long regs_offset=0,data_regs_addr;
struct GPIO_data *struct_ptr;
int original=0;
down_interruptible(&my_lock);
//copy_from_user(struct_ptr,(struct GPIO_data *)ptr,sizeof(struct GPIO_data));
regs_offset = (int)(struct_ptr->port)-97;
data_regs_addr = regs_offset*PORT_LENGTH+DAT_OFFSET;
original = __raw_readl(S3C2410_GPIOREG(data_regs_addr));
struct_ptr->value = (original&(1<< struct_ptr->bit))? 1:0;
//copy_to_user((struct GPIO_data *)ptr,struct_ptr,sizeof(struct GPIO_data));
up(&my_lock);
return 0;
}
static int gpio_setconf(unsigned long ptr)
{
unsigned long regs_offset=0,cfg_regs_addr;
int original=0,recent=0;
struct GPIO_data *struct_ptr=NULL;
down_interruptible(&my_lock);
//copy_from_user(struct_ptr,(struct GPIO_data *)ptr,sizeof(struct GPIO_data));
struct_ptr = (struct GPIO_data *)ptr;
regs_offset = (int)(struct_ptr->port)-97;
cfg_regs_addr = regs_offset*PORT_LENGTH+CFG_OFFSET;
original = __raw_readl(S3C2410_GPIOREG(cfg_regs_addr));
recent = (original & ~(1<<(struct_ptr->bit)))|((struct_ptr->value)<<(struct_ptr->bit));
__raw_writel(recent,S3C2410_GPIOREG(cfg_regs_addr));
up(&my_lock);
return 0;
}
static int gpio_getconf(unsigned long ptr)
{
unsigned long regs_offset=0,cfg_regs_addr;
struct GPIO_data *struct_ptr=NULL;
int original=0;
down_interruptible(&my_lock);
//copy_from_user(struct_ptr,(struct GPIO_data *)ptr,sizeof(struct GPIO_data));
regs_offset = (int)(struct_ptr->port)-97;
cfg_regs_addr = regs_offset*PORT_LENGTH+CFG_OFFSET;
original = __raw_readl(S3C2410_GPIOREG(cfg_regs_addr));
struct_ptr->value = (original&(1<< struct_ptr->bit))? 1:0;
//copy_to_user((struct GPIO_data *)ptr,struct_ptr,sizeof(struct GPIO_data));
up(&my_lock);
return 0;
}
static int ioctl_test(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case GPIO_IO_GET_CFG: gpio_getconf(arg);break;
case GPIO_IO_SET_CFG: gpio_setconf(arg);break;
case GPIO_IO_WRITE: gpio_write(arg);break;
case GPIO_IO_READ: gpio_read(arg);break;
default:break;
}
return 0;
}
static int open_test(struct inode *inode,struct file *file)
{
printk("nOpen_testn");
return 0;
}
static void release_test(struct inode *inode,struct file *file)
{
printk("nRelease_testn");
}
struct file_operations test_fops={
.owner=THIS_MODULE,
.read=read_test,
.write=write_test,
.ioctl=ioctl_test,
.open=open_test,
.release=release_test,
};
int mydev_c_init_module(void)
{
int result;
dev_t dev=0;
sema_init(&my_lock,1);
dev=MKDEV(test_major,test_minor);
result=register_chrdev_region(dev,1,"mydev_test");
printk("major=%d,minor=%dn",test_major,test_minor);
if(result<0)
{
printk(KERN_INFO "test:can't get major numbern");
return result;
}
cdev_init(&cdevc,&test_fops);
cdevc.owner=THIS_MODULE;
cdevc.ops=&test_fops;
result=cdev_add(&cdevc,dev,1);
if(result)
printk("Error %d adding test ",result);
return 0;
}
void mydev_c_remv_module(void)
{
dev_t dev=0;
dev=MKDEV(test_major,test_minor);
cdev_del(&cdevc);
unregister_chrdev_region(dev,1);
printk("nRemove modulen");
}
module_init(mydev_c_init_module);
module_exit(mydev_c_remv_module);
/* Makefile */
ifeq ($(KERNELRELEASE),)
KERNELDIR = /home/yangfan/bootloader_kernel/utu-linux_for_s3c2440_V1.5.3
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
obj-m := mydev.o
endif