Another way to write Tiny210 character device driver

Publisher:平安心境Latest update time:2018-10-12 Source: eefocusKeywords:Tiny210 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//Another way to write a character device driver:

#include "linux/device.h"

#include "linux/module.h"

#include "linux/kernel.h"

#include "linux/fs.h"

#include "linux/init.h"

#include "linux/delay.h"

#include "linux/irq.h"

#include "asm/uaccess.h"

#include "asm/irq.h"

#include "asm/io.h"

#include "linux/poll.h"

#include "linux/cdev.h"

// 1. Determine the major device number 

static int major;

static int hello_open(struct inode *inode, struct file *file)

{

    printk("hello_open\n");

    return 0;

}

static int hello2_open(struct inode *inode, struct file *file)

{

    printk("hello2_open\n");

    return 0;

}

// 2. Construct file_operations 

static struct file_operations hello_fops = {

    .owner = THIS_MODULE,

    .open  = hello_open,

};

static struct file_operations hello2_fops = {

    .owner = THIS_MODULE,

    .open  = hello2_open,

};

#define HELLO_CNT   2

static struct cdev hello_cdev;

static struct cdev hello2_cdev;

static struct class *cls;

static int hello_init(void)

{

    dev_t devid;

    

    // 3. Tell the kernel 

#if 0

    major = register_chrdev(0, "hello", &hello_fops); // (major,  0), (major, 1), ..., (major, 255)都对应hello_fops 

#else

    if (major) {

        devid = MKDEV(major, 0);

        register_chrdev_region(devid, HELLO_CNT, "hello"); // (major,0~1) corresponds to hello_fops, (major, 2~255) does not correspond to hello_fops 

    } else {

        alloc_chrdev_region(&devid, 0, HELLO_CNT, "hello"); // (major, 0~1) corresponds to hello_fops, (major, 2~255) does not correspond to hello_fops 

        major = MAJOR(devid);                     

    }

    

    cdev_init(&hello_cdev, &hello_fops);

    cdev_add(&hello_cdev, devid, HELLO_CNT);

    div = MKDEV(major, 2);

    register_chrdev_region(devid, 1, "hello2");

    cdev_init(&hello2_cdev, &hello2_fops);

    cdev_add(&hello2_cdev, devid, 1);

    

#endif

    cls = class_create(THIS_MODULE, "hello");

    device_create(cls, NULL, MKDEV(major, 0), NULL, "hello0"); // /dev/hello0 

    device_create(cls, NULL, MKDEV(major, 1), NULL, "hello1"); // /dev/hello1 

    device_create(cls, NULL, MKDEV(major, 2), NULL, "hello2"); // /dev/hello2 

    device_create(cls, NULL, MKDEV(major, 3), NULL, "hello3"); // /dev/hello3 

    

    return 0;

}

static void hello_exit(void)

{

    device_destroy(cls, MKDEV(major, 0));

    device_destroy(cls, MKDEV(major, 1));

    device_destroy(cls, MKDEV(major, 2));

    device_destroy(cls, MKDEV(major, 3));

    class_destroy(cls);

    cdev_del(&hello_cdev);

    unregister_chrdev_region(MKDEV(major, 0), HELLO_CNT);

    cdev_del(&hello2_cdev);

    unregister_chrdev_region(MKDEV(major, 2), 1);

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE("GPL");

===================================================================

test program:

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "sys/types.h"

#include "sys/stat.h"

#include "fcntl.h"

// hello_test /dev/hello0

void print_usage(char *file)

{

    printf("%s 《dev》\n", file); //“《” is actually “<”

}

int main(int argc, char **argv)

{

    int fd;

    

    if (argc != 2)

    {

        print_usage(argv[0]);

        return 0;

    }

    fd = open(argv[1], O_RDWR);

    if (fd < 0)

        printf("can't open %s\n", argv[1]);

    else

        printf("can open %s\n", argv[1]);

    return 0;

}


Keywords:Tiny210 Reference address:Another way to write Tiny210 character device driver

Previous article:u-boot-2014.04 startup process analysis
Next article:Tiny210 IIC driver at24cxx access

Recommended ReadingLatest update time:2024-11-17 00:22

Tiny210 button layer separation (bus-driver-device model)
led_dev.c driver source code: #include "linux/module.h" #include "linux/version.h" #include "linux/init.h" #include "linux/kernel.h" #include "linux/types.h" #include "linux/interrupt.h" #include "linux/list.h" #include "linux/timer.h" #include "linux/init.h" #include "linux/serial_core.h" #include "linux/platform_dev
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号