免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1018 | 回复: 0
打印 上一主题 下一主题

fist char driver and app [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-04 17:02 |只看该作者 |倒序浏览
fist char driver and app

driver:
  #include      //dev_t
#include       //struct cdev
#include         //alloc_chrdev_region()
#include     //class_create()
#include  //copy_to_user
dev_t                  devid;
static struct cdev     *maytures_cdev;
static int             maytures_Major = 0;
static int             maytures_Minor = 0;
static struct class    *maytures_class;
int dev_ch;
static int maytures_open(struct inode *inode, struct file *file)
{
/*open*/
printk(KERN_ALERT "open_maytures.\n");
return 0;
}
static ssize_t maytures_read(struct file *file, char __user *buf,
    size_t count, loff_t * ppos)
{
printk(KERN_ALERT "read_maytures.\n");
if(copy_to_user(buf, &dev_ch, sizeof(int))){
  printk(KERN_ALERT "sent_maytures.=dev=%d,%d\n",dev_ch,*buf);
  return -EFAULT;
}
}
static ssize_t maytures_write(struct file *file, const char __user *buf,
     size_t count, loff_t * ppos)
{
printk(KERN_ALERT "write_maytures.\n");
if(copy_from_user(&dev_ch, buf, sizeof(int))){
  printk(KERN_ALERT "get_maytures.=dev=%d,%d\n",dev_ch,*buf);
  return -EFAULT;
}
}
static struct file_operations maytures_fops = {
.owner = THIS_MODULE,
.open = maytures_open,
.read = maytures_read,
.write = maytures_write
};
static int __init maytures_init(void)
{
    int err;
dev_ch = 168;
    //初始化cdev
    maytures_cdev        = cdev_alloc();
    cdev_init(maytures_cdev, &maytures_fops);
    maytures_cdev->owner = THIS_MODULE;
    //动态获取主设备号(dev_t devid中包含"主设备号"和"次设备号"信息)
    alloc_chrdev_region(&devid, 66, 1, "maytures");
    maytures_Major = MAJOR(devid);
    maytures_Minor = MINOR(devid);
    printk(KERN_ALERT "I was assigned major number %d.\n", maytures_Major);
    printk(KERN_ALERT "I was assigned minor number %d.\n", maytures_Minor);
    //注册字符设备 (1)
    err = cdev_add(maytures_cdev, devid, 1);
    if (err) {
        printk(KERN_NOTICE "Error %d adding device\n", err);
        return -1;
    }
// WM8350_SW_RESET_CHIP_ID_MASK = 0x0;
    maytures_class = class_create(THIS_MODULE, "maytures_class1");
    if (IS_ERR(maytures_class)) {
        printk(KERN_ALERT "create class error\n");
        return -1;
    }
    device_create(maytures_class, NULL, devid, NULL, "maytures" "%d", MINOR(devid));   
    return 0;
}
static void __exit maytures_exit(void)
{
    unregister_chrdev_region(devid, 1);
    cdev_del(maytures_cdev);
    device_destroy(maytures_class, devid);
    class_destroy(maytures_class);
printk(KERN_ALERT "exit module.\n");
}
module_init(maytures_init);
module_exit(maytures_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("maytures ");
MODULE_DESCRIPTION("A sample driver");
MODULE_SUPPORTED_DEVICE("testdevice");
driver make file :

PWD := $(shell pwd)
KERNELDIR := /home/cmz/MyProjects/omap3/code/ema_psp-02.01.03.11
all: default
obj-m += maytures_chrdev.o
#ARCH=arm
#CROSS_COMPILE=arm-none-linux-gnueabi-
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
        rm *.mod.c *.o *.ko
app:   
//#include
//#include
#include
#include "stdio.h"
main()
{
int fd,num;
//打开

printf("hello,open!\n");
fd = open("/dev/maytures66",O_RDWR);
if(fd != -1){
  printf("opened sucesses!\n");
  read(fd,&num,sizeof(int));
  printf("from kernel --%d!\n",num);
  /*输入数据送kernel*/
  scanf("%d",&num);
  printf("to kernel --%d!\n",num);
  write(fd,&num,sizeof(int));
   /*read kernel data*/
  read(fd,&num,sizeof(int));
  printf("from kernel --%d!\n",num);
  close(fd);
}else{
  printf("open,failed!\n");
}

}
app makefile:

ARCH=arm-v7
CROSS_COMPILE=arm-none-linux-gnueabi-
EXE=read
all:$(EXE)
$(EXE):read.c
        $(CROSS_COMPILE)gcc -o $@ read.c
clean:
        rm -rf read



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107072/showart_2139202.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP