zzb-Dream-90Time

本文转载自:http://www.hovercool.com/en/%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97#a_.E5.9B.9B.E3.80.81.E9.85.8D.E7.BD.AE.E7.B3.BB.E7.BB.9F.E7.9A.84autoconfig

一、编写驱动核心程序

这里说的驱动核心程序是指运行在内核空间的,完全按linux驱动格式编写的,基本上与android没什么关系,一般包括xxx.h和xxx.c文件。

进入到kernel/drivers目录,新建snsled目录,然后建立对应的snsled.h和snsled.c文件:

//snsled.h

#ifndef _SNSLED_H_
#define _SNSLED_H_

#define SNSLED_NUM (1)

#define SNSLED_CLASS_NAME  "snsled"
#define SNSLED_DEVICE_NAME	"snsled"
#define SNSLED_NODE_NAME   "snsled"
#define SNSLED_PROC_NAME	"snsled"

#define SNSLED_IOC_MAGIC   \'k\'

#define SNSLED_IO_ON        2323	//_IO(SNSLED_IOC_MAGIC, 0)
#define SNSLED_IO_OFF       2324	//_IO(SNSLED_IOC_MAGIC, 1)
#define SNSLED_IOW_PWM     	2325	//_IOW(SNSLED_IOC_MAGIC, 2, int)
#define SNSLED_IOR_PWM     	2326	//_IOR(SNSLED_IOC_MAGIC, 3, int)

struct snsled_cntx {
    int r1;
    struct semaphore sem; 
	struct cdev cdev;
};

#endif

//snsled.c

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>	/* printk() */
#include <linux/slab.h>		/* kmalloc() */
#include <linux/fs.h>		/* everything... */
#include <linux/errno.h>	/* error codes */
#include <linux/types.h>	/* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h>	/* O_ACCMODE */
#include <linux/seq_file.h>
#include <linux/cdev.h>

#include <asm/system.h>		/* cli(), *_flags */
#include <asm/uaccess.h>	/* copy_*_user */

//#include <asm/semaphore.h> /* semaphore */
#include <linux/semaphore.h>  
#include <linux/device.h>   /*class_create*/  

#include "snsled.h"		/* local definitions */


/*
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/leds.h>
#include <linux/leds-mt65xx.h>
#include <linux/workqueue.h>
#include <linux/wakelock.h>
#include <linux/slab.h>

#include <cust_leds.h>*/

#if defined (CONFIG_ARCH_MT6573)
#include <mach/mt6573_pwm.h>
#include <mach/mt6573_gpio.h>
#include <mach/pmu6573_sw.h>

#elif defined (CONFIG_ARCH_MT6516)
#include <mach/mt6516_pwm.h>
#include <mach/mt6516_gpio.h>

#endif


/*====macros====*/
#define BUF_SIZE (64)

#define SNS_LED_CONTROL_LINE				GPIO99	//GPIO39
#define SNS_LED_CONTROL_LINE_GPIO_MODE		GPIO_MODE_00	
#define SNS_LED_CONTROL_LINE_PWM_MODE		GPIO_MODE_01



/*====declares====*/
ssize_t snsled_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos);
ssize_t snsled_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos);
long snsled_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
int snsled_open(struct inode *inode, struct file *filp);
int snsled_release(struct inode *inode, struct file *filp);

int snsled_turn_on(void

分类:

技术点:

相关文章: