发布时间:2014-09-05 17:41:45作者:知识屋
逻辑操作*/
以下代码整理、摘录自《Android深度探索HAL与驱动开发-李宁》LED驱动篇
#include <linux/fs.h>#include <linux/cdev.h>#include <asm/uaccess.h>#include <linux/pci.h>#include <mach/map.h>#include <mach/regs-gpio.h>#include <mach/gpio-bank-m.h>#deifne DEVICE_NAME "s3c6410_leds"#define DEVICE_COUNT 1#define S3C6410_LEDS_MAJOR 0#define S3C6410_LEDS_MINOR 234#define PARAM_SIZE 3static int major = S3C6410_LEDS_MAJOR;static int minor = S3C6410_LEDS_MINOR;static dev_t dev_number;static int leds_state = 1;static char *params[] = {"string1","string2","string3"};static iint param_size = PARAM_SIZE;static struct class *leds_class = NULL;static int s3c6410_leds_ioctl (struct file *file, unsigned int cmd, unsigned long arg){ switch (cmd) { unsigned tmp; case 0: case 1: if (arg > 4) return -EINVAL; tmp = ioread32 (S3C64XX_GPMDAT); if (cmd == 1) tmp &= (~(1 << arg)); else tmp |= (1 << arg); iowrite32 (tmp, S3C64XX_GPMDAT); return 0; default : return -EINVAL; }} static ssize_t s3c6410_leds_write (struct file *file, const char __user *buf, size_t count, loff_t *ppos){ unsigned tmp = count; unsigned long i = 0; memset(mem, 0, 4); if (count > 4) tmp = 4; if (copy_from_user (mem, buf, tmp) ) return -EFAULT; else{ for( i=0; i<4; i++) { tmp = ioread32(S3C64XX_GPMDAT); if (mem[i] == '1') tmp &= (~(1 << i)); else tmp |= (1 << i); iowrite32(tmp, S3C64XX_GPMDAT); } return count; }}static struct file_operations dev_fops = {.owner = THIS_MODULE, .unlocked_ioctl = s3c6410_leds_ioctl, .write = s3c6410_leds_write};static struct cdev leds_cdev;static int leds_create_device(void){ int ret = 0; int err = 0; cdev_init (&leds_cdev, &dev_fops); leds_cdev.owner = THIS_MODULE; if (major > 0) { dev_number = MKDEV(major,minor); err = register_chrdev_region(dev_number, DEVICE_COUNT, DEVICE_NAME); if (err < 0) { printk(KERN_WANRING "register_chrdev_region error/n"); return err } } else{ err = alloc_chrdev_region(&leds_cdev.dev, 10, DEVICE_COUNT, DEVICE_NAME); if(err < 0) { printk (KERN_WARNING "alloc_chrdev_region error/n"); return err; } major = MAJOR(leds_cdev.dev); major = MINOR(leds_cdev.dev); dev_number = leds_cdev.dev; } ret = cdev_add(&leds_cdev,dev_number, DEVICE_COUNT); leds_class = class_create (THIS_MODULE, DEVICE_NAME); device_create (leds_class, NULL, dev_number, NULL, DEVICE_NAME); return ret;}static void leds_init_gpm(int leds_default){ int tmp = 0; tmp = ioread32(S3C64XX_GPMCON); tmp &= (~0xffff); tmp |= 0x1111; iowrite32(tmp,S3C64XX_GPMCON); tmp = ioread32(S3C64XX_GPMPUD); tmp &= (~0XFF); tmp |= 0xaa; iowrite32(tmp,S3C64XX_GPMPUD); tmp = ioread32(S3C64XX_GPMDAT); tmp &= (~0xf); tmp |= leds_default; iowrite32(tmp, S3C64XX_GPMDAT); }static leds_init( void){ int ret; ret = leds_create_device(); leds_init_gpm (~leds_state); printk(DEVICE_NAME"/tinitialized/n"); return ret;}static void leds_destroy_device(void){ device_destroy(leds_class, dev_number); if(leds_class) class_destroy(leds_class); unregister_chrdev_region(dev_number, DEVICE_NAME);}static void leds_exit(void){ leds_destroy_device(); printk(DEVICE_NAME"/texit/n");}module_init(leds_init);module_exit(leds_exit);module_param(leds_state, int, S_IRUGO|S_IWUSR);module_param_array(params, charp, ?m_size, S_IRUGO|S_IWUSR);MODULE_LICENSE("GPL");MODULE_AUTHOR("lining");
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能