linux驅動基本結構分析

來源:互聯網
上載者:User

/*driver test code */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/device.h>

MODULE_LICENSE("GPL");

static int char_read(struct file *filp, char __user *buffer, size_t, loff_t*);
static int char_open(struct inode*, struct file*);
static int char_write(struct file *filp, const char __user *buffer, size_t, loff_t*);
static int char_release(struct inode*, struct file*);

static int chropen;
struct cdev *chardev;
struct class *my_class;
static int len;
static dev_t dev;

static char *to;
static const struct file_operations char_ops={
    .read = char_read,
    .write = char_write,
    .open = char_open,
    .release = char_release,
};

static int __init char_init(void)
{
    printk(KERN_ALERT"Initing......\n");
   
    chardev = cdev_alloc();

    if(chardev == NULL){
        return -1;
    }
    if(alloc_chrdev_region(&dev, 0, 10, "xmimx"))  /*動態分配裝置號*/

  {
    printk(KERN_ALERT"Register char dev error\n");
    return -1;
    }
    chropen = 0;
    len = 0;
    cdev_init(chardev, &char_ops);
    if(cdev_add(chardev, dev, 1)){
        printk(KERN_ALERT"Add char dev error!\n");
    }
   
     my_class = class_create(THIS_MODULE, "xmimx_class");  /*建立一個類*/

    if(IS_ERR(my_class)) {
        printk("Err: failed in creating class.\n");
        return -1;
    }

    device_create(my_class, NULL, dev, NULL, "xmimx"); /*建立裝置節點,並向系統註冊該裝置*/
    return 0;
}

#define DP_MAJOR MAJOR(dev)
#define DP_MINOR MINOR(dev)   

static int char_open(struct inode *inode, struct file *file)
{
    if(chropen == 0)
        chropen++;
    else{
        printk(KERN_ALERT"Another process open the char device\n");
        return -1;
    }
    try_module_get(THIS_MODULE);
    return 0;
}

static int char_release(struct inode *inode,struct file *file)
{
    chropen--;
    module_put(THIS_MODULE);
    return 0;
}

static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
    unsigned long nn;
    nn = copy_to_user(buffer, to, length);
    printk("buffer = %s\n", buffer);
    return length;
}

static int char_write(struct file *filp, const char __user *buffer, size_t length, loff_t *offset)
{
    unsigned long n;
    to = (char *)kmalloc((sizeof(char)) * (length+1), GFP_KERNEL);
    memset(to, '\0', length+1);
    n = copy_from_user(to, buffer, length);
    printk("n = %ld\n", n);
    printk("to = %s\n", to);
    return length;
}

static void __exit module_close(void)
{
    len=0;
    printk(KERN_ALERT"Unloading..........\n");

    unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
    cdev_del(chardev);
    device_destroy(my_class, dev);
    class_destroy(my_class);
}

module_init(char_init);
module_exit(module_close);

 

 

/********************************************************************************/

/*app code*/

#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<linux/rtc.h>
#include<linux/ioctl.h>
#include<stdio.h>
#include<stdlib.h>

main()
{
    int fd;
    int i;
    char data[256];

    int retval;
    fd=open("/dev/xmimx",O_RDWR);
    if(fd==-1)
    {
        perror("error open\n");
        exit(-1);
    }
    printf("open /dev/xmimx ok\n");

    retval=write(fd,"David Zeng",10);
    if(retval==-1)
    {
        perror("write error\n");
        exit(-1);
    }

   
    memset(data, '\0', 256);

    retval=read(fd,data,10);

    if(retval==-1)
    {
        perror("read error\n");
        exit(-1);
    }

    data[retval] = '\0';

    printf("read ok:%s\n",data);
    close(fd);
}

/***************************************************/

/*****makefile*****/

TARGET=demo2
KDIR=/usr/src/linux-headers-2.6.32-25-generic
PWD=$(shell pwd)

obj-m:=$(TARGET).o

default:
 make -C $(KDIR) M=$(PWD) modules

clean:
 rm -rf *.o *.ko *.mod.c .*.cmd *~ .tmp_versions modules.order Module.symvers $(TARGET)_test

install:
 sudo insmod $(TARGET).ko

uninstall:
 sudo rmmod $(TARGET)

test:
 gcc -o $(TARGET)_test $(TARGET)_test.c

/**********相關注釋請參考其他幾篇文章******/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.