驱动的头文件MstarGpio.h
#ifndef _MSTAR_KERNEL_GPIO_KLSDJFAJLKAJKAL_H_
#define _MSTAR_KERNEL_GPIO_KLSDJFAJLKAJKAL_H_
#include <linux/ioctl.h>
/*Macros to help debuging*/
#undef PDEBUG
#ifdef DEMO_DEBUG
#ifdef __KERNEL__
#define PDEBUG(fmt, args...) printk(KERN_DEBUG "mstar_gpio:" fmt,## args)
#else
#define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
#endif
#else
#define PDEBUG(fmt, args...)
#endif
#define MSTAR_GPIO_MAJOR 224
#define MSTAR_GPIO_MINOR 0
#define Cmd_GPIO_Write 221
#define Cmd_GPIO_Read 222
#define Cmd_GPIO_DirectionOut 223
#define Cmd_GPIO_DirectionIn 224
#define mstar_gpio_NAME "MstarGpio"
struct mstar_gpio_dev {
struct cdev cdev;
};
typedef struct _mstar_gpio_data_struct {
int num;
int value;
}mstar_gpio_data_struct;
#endif
//
驱动文件MstarGpio.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/cdev.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include "MstarGpio.h"
MODULE_AUTHOR("peacerfu");
MODULE_LICENSE("Dual BSD/GPL");
struct mstar_gpio_dev *mstar_gpio_devices;
static unsigned char mstar_gpio_inc = 0;//全局变量,每次只能打开一个设备
static u8 mstar_gpio_buffer[256];
int mstar_gpio_open(struct inode *inode, struct file *filp)
{
// struct mstar_gpio_dev *dev;
// if (mstar_gpio_inc > 0) return -ERESTARTSYS;
mstar_gpio_inc++;
// dev = container_of(inode->i_cdev, struct mstar_gpio_dev, cdev);
//filp->private_data = dev;
return 0;
}
int mstar_gpio_release(struct inode *inode,
///
ko编译的脚本Makefile
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are listed here.
mymodule-objs := MstarGpio.o
obj-m := MstarGpio.o
else
PWD := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /home/ipnc/release_0529/kernel
all:
# $(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
$(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-buildroot-linux-uclibcgnueabihf-
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_
endif