Shash316
Shash316

Reputation: 2218

Linux Device Driver Registration procedure

I am a linux newbie, trying to understand Linux Device Model. I had been going through Linux 3.1.6 code base, particularly the driver part and found that

  1. some of the drivers were using (for example i2c-bus device : linux-3.1.6/drivers/i2c/i2c-dev.c) *register_chrdev()* and
  2. few others (for example pci bus : linux-3.1.6/drivers/pci/bus.c) were using *device_register()*.

My question is when to use register_chrdev (yes, I know its for a character device, but why not use device_register) and device_register ?

Does that depend on where does the driver developer wants his device/driver to be listed down, like devfs vs sysfs ? Or the interface exposed to the user space to access the device ?

Upvotes: 4

Views: 6119

Answers (3)

Manish
Manish

Reputation: 79

Use register_chrdev when you are creating a character device driver and want to provide a character device interface to user-space programs.When you register a character device using register_chrdev, you are essentially informing the kernel that your module is providing a character device interface that user-space programs can interact with using standard I/O system calls like open, read, write, and close.

Use device_register when you are working on a subsystem or module that involves various types of devices and you want to manage them within the kernel's device framework. device_register is used to register a platform or other types of devices with the kernel's device framework. This framework provides an abstraction layer for managing device objects, which can include various types of devices, such as platform devices, virtual devices, and more. Let say your are developing a susbystem which needs managing a particular HW plaform.

#include <linux/module.h>
#include <linux/device.h>

MODULE_LICENSE("GPL");

static struct platform_device my_platform_device = {
    .name = "my_platform_device",
    .id = -1,
};

static int __init my_subsystem_init(void) {
    int ret = platform_device_register(&my_platform_device);
    if (ret < 0) {
        pr_err("my_subsystem: Platform device registration failed\n");
        return ret;
    }
    pr_info("my_subsystem: Platform device registered successfully\n");
    return 0;
}

static void __exit my_subsystem_exit(void) {
    platform_device_unregister(&my_platform_device);
    pr_info("my_subsystem: Platform device unregistered\n");
}

    enter code here

module_init(my_subsystem_init);
module_exit(my_subsystem_exit);

Upvotes: 0

shingaridavesh
shingaridavesh

Reputation: 991

See when you register a device as a character device specifically then following thing happens:

Major Number is given in accordance. If you use any device depending on functionality whose registration is based on character device (like tty, input etc), then those will have their respective major number. Thats why its said that dont assign major number statically if not sure.

And

There are certain file operations which correspond to operations that could be performed on char devices only.

Do ask if any query.

Upvotes: 0

j&#248;rgensen
j&#248;rgensen

Reputation: 10551

One function registers a character device association (hooking up major:minors to your function), the other just creates an abstract device object (only), so to speak. The two are complementary. The device object is used for the generation of an event so that udev can, if there is also a cdev association registered, create a node in /dev. (Compare with, for example, drivers/char/misc.c.)

Upvotes: 3

Related Questions