Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Linux character driver connects a device number to a struct cdev and its file operations. A basic implementation reserves a device-number range, initializes and adds the cdev, and—if it should appear in the device model—creates a class and registers a corresponding struct device. These are separate steps: cdev_add() makes callbacks reachable immediately, while device_create() registers a device with sysfs; neither alone defines the driver’s read/write behavior or guarantees a particular /dev node policy.

The examples below describe the registration model documented for Linux 7.1. Check API signatures and surrounding details against the kernel version you are targeting; the kernel documentation describes itself as a work in progress.

Understand the character-device mapping

A character device is addressed through a device number, represented by dev_t. The number identifies the device endpoint; the cdev associates that number or range with the driver’s file_operations. Those operations implement the behavior userspace invokes through file descriptors, such as reading, writing, or issuing supported commands.

There are three distinct concerns to keep straight:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
  • Number allocation: reserve a device-number range and retain the assigned dev_t.
  • File-operation dispatch: initialize and add a struct cdev for that range.
  • Device-model visibility: optionally register a struct device under a class so it appears in sysfs.

The registration name passed to the number-allocation API is not itself a promise about the name or creation policy of a /dev node.

Choose how to reserve device numbers

Dynamic allocation for most standalone drivers

alloc_chrdev_region() dynamically reserves a range and returns the assigned device number through a dev_t output parameter. Check its return value before proceeding, and keep the resulting number for both cdev registration and any matching device-model registration.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Fixed allocation only when there is a reason

register_chrdev_region() reserves a known range. Use it when a fixed number is justified by the interface or deployment requirements; otherwise, dynamic allocation avoids assuming that a particular range is available. Both approaches reserve numbers, but the chosen number must be used consistently by the cdev and the associated device object.

See the Linux kernel’s Char devices API reference for the versioned API descriptions.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Initialize and activate the cdev

  1. Prepare the driver’s state and file operations. The callbacks and every object they access must be ready before the cdev becomes reachable.
  2. Initialize the cdev. Call cdev_init() with the cdev and the driver’s file_operations.
  3. Add the cdev for the intended range. Call cdev_add() with the cdev, the allocated device number, and the number of device numbers it covers; check the return code.

Activation is immediate: the kernel API says that cdev_add() “adds the device represented by p to the system, making it live immediately.” Treat a successful return as the point at which userspace can reach the file operations. Do not publish the cdev before callback dependencies are initialized, and make error handling account for any registrations already completed.

Add a device-model entry when needed

A cdev can provide file-operation dispatch without creating a class-based device entry. If the driver should participate in the driver model and expose a device through sysfs, create the class first, then call device_create() with that class and the same dev_t handled by the cdev. Check its returned pointer using the kernel’s error-pointer handling rather than treating every non-NULL pointer as success.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

device_create() registers a struct device under the class and exposes device information in sysfs, including a dev attribute. This is distinct from implementing file operations. Whether a corresponding /dev node is created, and its naming and permissions, depend on the userspace environment; the API documentation does not establish one universal node-creation policy. Consult the device drivers infrastructure documentation for the device-model helper details.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a registration approach that fits object lifetime

Approach When it fits Important consideration
cdev_init() and cdev_add() Direct cdev management when the driver needs to control cdev registration separately. Coordinate the cdev’s lifetime with the private state accessed by its callbacks.
cdev_device_add() When the cdev and struct device share a lifetime-managed containing object. The API warns that opens may occur even if the combined add operation fails, so callbacks and state must already be safe.

These are useful patterns, not an exhaustive list of Linux device interfaces. For real hardware, a subsystem-specific interface may be more suitable than a bespoke character device.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Design teardown around open file descriptors

Unwind successful registrations and release the reserved device-number range during teardown. The order should reflect which steps succeeded: remove the device-model entry if it was created, prevent new cdev opens, and release the number range only when the driver’s registration work is complete.

Most importantly, removing a cdev does not make existing file descriptors disappear. The kernel documentation states that after cdev_del(), the cdev can no longer be opened, but already-open cdevs remain and their file operations can still be called. Keep the private state and any resources those callbacks use valid until existing users can no longer reach them. A driver therefore needs an explicit lifetime strategy—such as references tied to open/release activity or another managed ownership arrangement—rather than freeing callback state immediately after cdev_del().

Add ioctl only for a durable command interface

Use established file operations for simple byte-oriented behavior. Add ioctl commands only when the device needs operations that do not fit those interfaces. Once userspace depends on an ioctl command and payload layout, changing it can break compatibility.

For new commands, use the documented _IO, _IOR, _IOW, and _IOWR macros to encode the command’s direction and associated data type. Choose the command type, number, and payload layout deliberately, and plan for the userspace ABI to remain stable. The kernel ioctl interface documentation explains the macros and compatibility concerns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.