驱动-BEEP驱动(第四天)
- linux-2.6.32.内核重要文件目录:
- linux-2.6.32.2/arch/arm/mach-s3c2410/include/mach/regs-gpio.h
- linux-2.6.32.2/arch/arm/plat-s3c24xx/gpio.c
- linux-2.6.32.2/linux/asm-generic/io.h
- linux-2.6.32.2/include/linux/wait.h
- asm -- linux-2.6.32.2/linux/asm-generic
- mach -- linux-2.6.32.2/arch/arm/mach-s3c2410/include/mach
- plat -- linux-2.6.32.2/arch/arm/plat-s3c24xx/include/plat
-- linux-2.6.32.2/arch/arm/plat/include/plat
- 驱动代码
#include<linux/errno.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/slab.h>
#include<linux/input.h>
#include<linux/init.h>
#include<linux/serio.h>
#include<linux/delay.h>
#include<linux/clk.h>
#include<linux/miscdevice.h>
#include<linux/gpio.h>
#include<asm/io.h>
#include<asm/irq.h>
#include<asm/uaccess.h>
#include<math/regs-clock.h>
#include<plat/regs-timer.h>
#include<math/regs-gpio.h>
#include<linux/cdev.h>
static int beep_major=0;
MODULE_AUTHOR("LIU");
MODULE_LICENSE("Dual BSD/GPL");
#define BEEP_MAGIC 'K'
#define BEEP_START_CMD _IO(BEEP_MAGIC,1)
#define BEEP_STOP_CMD _IO(BEEP_MAGIC,2)
int beep_open(struct inode *inode,struct file *filp)
{
return 0;
}
ssize_t beep_read(struct file *file,char __user *buff,size_t)
{
return 0;
}
ssize_t beep_write(struct file *file,const char __user *buff,size_t)
{
return 0;
}
void beep_stop(void)
{
s3c2410_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPIO_OUTPUT);
s3c2410_gpio_setpin(S3C2410_GPB(0),0);
}
void beep_start(void){
s3c2410_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPIO_OUTPUT);
s3c2410_gpio_setpin(S3C2410_GPB(0),1);
}
static int beep_ioctl(struct inode *inode,struct file *file,unsigned){
switch(cmd){
case BEEP_START_CMD:{
beep_start();
break;
}
case BEEP_STOP_CMD:{
beep_stop();
break;
}
default:
break;
}
return 0;
}
static void beep_setup_cdev(struct cdev *dev,int minor,struct file_operation *fops){
int err,devno = MKDEV(beep_major,minor);
cdev_init(dev,fops);
dev->owner = THIS_MODULE;
dev->ops =fops;
err=cdev_add(dev,devno,1);
if(err){
printk("error %d adding beep %d",err,minor);
}
}
static struct file_operations beep_remap_ops={
.owner = THIS_MODULE,
.open = beep_open,
.release = beep_release,
.read = beep_read,
.write = beep_write,
.ioclt = beep_ioctl,
};
static struct cdev BeepDevs;
static int beep_init(void)
{
int result;
dev_t dev=MKDEV(beep_major,0);
char dev_name[]="beep";
if(beep_major)
result=register_chrdev_region(dev,1,dev_name);
else
{
result=alloc_chrdev_region(&dev,0,1,dev_name);
beep_major = MAJOR(dev);
}
if(result<0)
{
printk("beep:unable to get major %d\n",beep_major);
}
if(beep_major ==0)
beep_major=result;
beep_setup_cdev(&BeepDevs,0,&beep_remap_ops);
printk("beep device install,with major %d\n",beep_major);
printk("The device name is:%s\n",dev_name);
return 0;
}
static void beep_cleanup(void)
{
cdev_del(&BeepDevs);
unregister_chrdev_region(MKDEV(beep_major,0),1);
printk("beep driver uninstll sucess!");
}
module_init(beep_init);
module_exit(beep_cleanup);