ioctl write

[code=C/C++][/code]
#include <linux/config.h>  
#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/fs.h>  
#include <linux/init.h>  
#include <linux/devfs_fs_kernel.h>  
#include <linux/miscdevice.h>  
#include <linux/delay.h>  
#include <asm/irq.h>  
#include <asm/io.h>  
#include <asm/arch/regs-gpio.h>  
#include <asm/hardware.h>  
 
#define DEVICE_NAME "led"  
#define LED_MAJOR 231  
#define LED_BASE (0xE1180000)  
 
ssize_t eduk4_led_write(struct file *filp, const char __user *buf, size_t count,
  loff_t *f_pos)
{  
unsigned char status;  
 

if (copy_from_user(&status, buf, 1)) {
retval = -EFAULT;
goto out;
}   
    
outb(status, LED_BASE);

return 1;
out:
return 0;   
}  

ssize_t eduk4_led_read_read(struct file *filp, char __user *buf, size_t count,
  loff_t *f_pos)
{
unsigned char status;

 	status = inb(LED_BASE);  
if (copy_to_user(buf, &status, 1)) {
retval = -EFAULT;
goto out;
}
return 1;
out:
return 0;
}
 
static struct file_operations eduk4_led_fops = {  
.owner = THIS_MODULE,  
.read = eduk4_led_read,
.write = edu4_led_write,  
};  
 
static int __init eduk4_led_init(void)  
{  
int ret;  
unsigned char status;  
 
ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &eduk4_led_fops);  
if (ret < 0) {  
printk(DEVICE_NAME " can't register major number\n");  
return ret;  
}  
 
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,  
DEVICE_NAME);  
 
status = inb(LED_BASE);  
outb(status & 0xf0,LED_BASE);   
    
printk(DEVICE_NAME " initialized\n");  
return 0;  
}  
 
static void __exit eduk4_led_exit(void)  
{  
unsigned char status;  
 
  status = inb(LED_BASE);  
  outb(status | 0x0f,LED_BASE);  
 
printk(DEVICE_NAME " remove\n");  
devfs_remove(DEVICE_NAME);  
unregister_chrdev(LED_MAJOR, DEVICE_NAME);  
}  
 
module_init(eduk4_led_init);  
module_exit(eduk4_led_exit);  
MODULE_LICENSE("BSD/GPL");  



测试应用程序

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
 
#include <sys/ioctl.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <sys/select.h>  
#include <sys/time.h>  
 
static int led_fd;  
 
int main(void)  
{
unsigned char status;
unsigned char read_status;  
  // open device  
led_fd = open("/dev/led", 0);  
 
if (led_fd < 0) {  
perror("open device led");  
exit(1);  
  }  
    
printf("Please look at the leds\n");  
 
  // led all off  
status = 0xf;
write(led_fd, &status, sizeof(status));
read(led_fd, &read_status, 1);
printf("the leds status is %d \n", _read_status);   
close(led_fd);  
return 0;  
}  
大体上就是这样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值