Acquisition of embedded Linux major and minor device numbers

Linux's device management is tightly integrated with the file system. Various devices are stored in the /dev directory as files, which are called device files. The application can open, close, and read and write these device files and complete the operation of the device just like ordinary data files. In order to manage these devices, the system numbers the devices. Each device number is divided into a major device number and a minor device number. The major device number is used to distinguish different types of devices, while the minor device number is used to distinguish multiple devices of the same type. For commonly used devices, Linux has a conventional number, such as the main device number of the hard disk is 3.

A character device or block device has a major device number and a minor device number. The major device number and minor device number are collectively referred to as the device number. The major number is used to indicate a specific driver. The minor device number is used to indicate each device that uses this driver. For example, an embedded system has two LED indicators, which need to be turned on or off independently. Then, you can write a character device driver for an LED lamp. You can register its major device number as device No. 5 with the minor device numbers 1 and 2, respectively. Here, the minor device number represents two LED lamps, respectively.

Device files are usually in the /dev directory. Such as:

As above, the first character preceded by c represents a character device. In character devices, there are major and minor device numbers. The above 1, 4, and 7 are the major device numbers, and 0, 1, 3, 7, 70, and 71 are all minor device numbers. In general, the major device number identifies the device driver associated with the device. For example, /dev/null and /dev/full are managed by driver #1, /dev/vcs and /dev/vcs1 are managed by driver #7, and /dev/ttyS6 is managed by driver #4.

The current Linux kernel allows multiple drivers to share a major device number, but more devices follow the principle of driving one master device number.

The kernel uses the minor device number to determine which device is currently pointed to. According to the written driver, you can get a pointer directly to the device from the kernel, or use the minor device number as an index of the device's local array. But in any case, the kernel itself barely knows what the minor device number is.

The internal representation of the device number is in the kernel, and the dev_t type (defined in the linux/types.h header file) is used to represent the device number, including the major and minor device numbers. For the 2.6.x kernel, dev_t is a 32-bit quantity where 12 bits are used to represent the major number and 20 bits are used to represent the minor number.

Defined in the linux/types.h header file

Acquisition of major and minor device numbers

In order to write a portable driver, you cannot assume the number of major and minor device numbers. The number of major and minor device numbers may be different in different models. You should use the MAJOR macro to get the major device number and the MINOR macro to get the minor device number. Here are the definitions of the two macros: (linux/kdev_t.h)

#define MINORBITS 20 /* minor device number */

#define MINORMASK ((1U << MINORBITS) - 1) /* minor device number mask */

#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) /*dev right shift 20 bits to get the major number */

#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) /* and the minor device mask and get the minor device number */

The MAJOR macro moves dev_t 20 bits to the right to get the major device number; the MINOR macro clears the high 12 bits of dev_t to get the minor device number. Instead, you can convert the major and minor device numbers to the device number type (dev_t), which can be done using the macro MKDEV.

#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))

The MKDEV macro shifts the major device number (ma) to the left by 20 bits and then compares it with the minor device number (mi) to obtain the device number.

Statically assigned device number

The statically assigned device number, which is the driver developer, statically specifies a device number. For some commonly used devices, the kernel developer has assigned a device number to it. These device numbers can be found in the kernel source documentation/devices.txt file. If only the developers themselves use these device drivers, they can choose an unused device number. This method does not generate device number conflicts when new hardware is not added. However, when adding new hardware, it is very likely to cause device number conflicts and affect the use of the device.

Dynamic allocation of device number

Due to the conflicting issue of statically allocated device numbers, the kernel community recommends that developers use methods for dynamically assigning device numbers. The function for dynamically allocating device numbers is alloc_chrdev_region().

4 Ports Usb Chargers

4 Port USb Charging

Ready to travel. The cable puts the charger on the table and extends to the distance from the power socket, and a travel adapter with foldable sales immediately turns it into a portable travel charger. This 4 -port USB charger has more protection safety systems to ensure the complete protection of you and your device. The mobile phone has its own charging circuit, which will be a adaptive current. No matter how much the power is, the phone can be charged safely.

4Port Usb Charger,Multi Port Usb Charger,Flat 4 Port Usb Charger,Wall Outlet 4 Usb Plugs

shenzhen ns-idae technology co.,ltd , https://www.szbestchargers.com