//This file for explain how to use a simple driver
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// GPIO DEVICE MAJOR
#define SIMPLE_GPIO_MAJOR 97
#define OURS_GPIO_DEBUG
#define VERSION "PXA270RP-V1.00-090224"
//define LED STATUS
#define LED_ON 0
#define LED_OFF 1
struct gpio_dev
{
struct cdev cdev;
unsigned char value;
};
struct gpio_dev *gpio_devp;
static int gpio_major=SIMPLE_GPIO_MAJOR;
void showversion(void)
{
printk("*********************************************\n");
printk("\t %s \t\n", VERSION);
printk("*********************************************\n\n");
}
// ------------------- READ ------------------------
ssize_t SIMPLE_GPIO_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_read [ --kernel--]\n");
#endif
return count;
}
// ------------------- WRITE -----------------------
ssize_t SIMPLE_GPIO_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_write [ --kernel--]\n");
#endif
return count;
}
// ------------------- IOCTL -----------------------
ssize_t SIMPLE_GPIO_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long data)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_ioctl [ --kernel--]\n");
#endif
switch(cmd)
{
case LED_ON: {GPCR3 |=0x1;break;}
case LED_OFF: {GPSR3 |=0x1;break;}
default:
{printk("led control:no cmd run\n");
return (-ENOTTY);
}
}
return 0;
}
// ------------------- OPEN ------------------------
ssize_t SIMPLE_GPIO_open (struct inode * inode ,struct file * file)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_open [ --kernel--]\n");
#endif
return 0;
}
// ------------------- RELEASE/CLOSE ---------------
ssize_t SIMPLE_GPIO_release (struct inode * inode ,struct file * file)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_release [ --kernel--]\n");
#endif
return 0;
}
// -------------------------------------------------
struct file_operations gpio_fops ={
.open= SIMPLE_GPIO_open,
.read= SIMPLE_GPIO_read,
.write= SIMPLE_GPIO_write,
.ioctl= SIMPLE_GPIO_ioctl,
.release= SIMPLE_GPIO_release,
};
static void gpio_setup_cdev(struct gpio_dev *dev,int index)
{
int err, devno=MKDEV(gpio_major,index);
cdev_init(&dev->cdev,&gpio_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&gpio_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Error %d adding gpio%d",err,index);
}
int gpio_init(void)
{
int result;
dev_t dev=MKDEV(gpio_major,0);
if(gpio_major)
result=register_chrdev_region(dev,1,"gpio");
else
{
result=alloc_chrdev_region(&dev,0,1,"gpio");
gpio_major=MAJOR(dev);
}
if(result<0)
return result;
gpio_devp =kmalloc (sizeof(struct gpio_dev),GFP_KERNEL);
if (!gpio_devp)
{ result= - ENOMEM;
goto fail_malloc;
}
memset(gpio_devp,0,sizeof(struct gpio_dev));
gpio_setup_cdev(gpio_devp,0);
//init GPIO
GAFR3_L &=0xfffffffc; //SET GPIO96
GPDR3 |=0x1;//SET GPIO96 OUTPUT MODE
GPSR3|=0X1;//OFF THE LED
printk("gpio_init\n");
showversion();
return 0;
fail_malloc:unregister_chrdev_region(dev,1);
return result;
}
void gpio_cleanup(void)
{
cdev_del(&gpio_devp->cdev);
kfree(gpio_devp);
unregister_chrdev_region(MKDEV(gpio_major,0),1);
printk("gpio_cleanup\n");
}
MODULE_DESCRIPTION("simple gpio driver module");
MODULE_AUTHOR("guliangzeng");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("gpio module");
module_init(gpio_init);
module_exit(gpio_cleanup);
#include
#include
#include
#include // open() close()
#include // read() write()
#define DEVICE_NAME "/dev/gpio"
//define LED STATUS
#define LED_ON 0
#define LED_OFF 1
//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
int fd;
int ret;
char *i;
printf("\nstart gpio_led_driver test\n\n");
fd = open(DEVICE_NAME, O_RDWR);
printf("fd = %d\n",fd);
if (fd == -1)
{
printf("open device %s error\n",DEVICE_NAME);
}
else
{
while(1)
{ ioctl(fd,LED_OFF);
sleep(1);
ioctl(fd,LED_ON);
sleep(1);
}
// close
ret = close(fd);
printf ("ret=%d\n",ret);
printf ("close gpio_led_driver test\n");
}
return 0;
}// end main
阅读(679) | 评论(0) | 转发(0) |