Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Linux Driver?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this "what is Linux Driver" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "what is Linux Driver" article.

Linux Driver is the Linux driver. The linux system design integrates the device driver into the file system. Any Linux driver needs a device file, otherwise the application will not be able to interact with the driver.

Equipment classification character equipment

Block equipment

Network equipment

The location of the driver in kernel the design of linux system integrates the device driver into the file system

A more specific hierarchical invocation structure is as follows:

Driver device model for Linux Device Modelusb devices:

Information recorded by the node of the device file:

File type

Main equipment number

The primary device number and type of the secondary device number determine the type and interface of the driver, while the secondary device number indicates the number of devices of the same kind.

The devfs file system was introduced in linux 2.4.The main advantages are:

You can use the program to create a device file in the / dev directory when the device is initialized and delete it when the device is uninstalled.

The device driver can specify the device name, owner, and permission bits, and the user-space program can still modify the owner and permission bits.

Instead of assigning the primary device number to the device driver and dealing with the secondary device number, you can pass the 0 primary device number directly to register_chrdev () in the program to dynamically obtain the available primary device number, and specify the secondary device number in devfs_register ().

Udev device file system linux 2.6 began to abandon devfs and introduced the udev file system, based on the following points:

The work done by devfs is assured that it can be done in user mode.

Some bug have not been fixed for quite a long time.

The maintainer and author of devfs stopped the maintenance of the code.

The difference between udev and devfs is:

Devfs automatically loads drivers when the device is turned on udev automatically loads drivers when the device is discovered

The sysfs file system udev works in user mode, using hot-swappable events sent by the kernel when the device is added or removed, while the hotplug event, the details of the device are output from the kernel to the sysfs file system at / sys.

Udev uses the information in sysfs to create device file nodes, etc.

Sysfs is seen as a file system in the same category as proc, devfs, and devpty. The file system is a virtual file system that produces a hierarchical view of all system hardware, very similar to the proc file system that provides process and state information.

Tree / sys/dev/ ├── block │ ├── 7:0->.. /.. / devices/virtual/block/loop0 │ ├── 7:1->.. /.. / devices/virtual/block/loop1 │ ├── 7:2->.. /.. / devices/virtual/block/loop2 │ ├── 7:3->.. /.. / devices/virtual/block/loop3 │ ├── 7:4->.. /.. / devices/virtual/block/loop4 │ ├── 7:5->.. /.. / devices/virtual/block/loop5 │ ├── 7:6->.. /.. / devices/virtual/block/loop6 │ ├── 7:7->.. /.. / devices/virtual/block/loop7 │ ├── 8:0->.. /.. / devices/pci0000:00/0000:00:17.0/ata1/host0/target0: 0:0/0:0:0:0/block/sda │ ├── 8:1->.. /.. / devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1 │ ├── 8:16->.. / devices/pci0000:00/0000:00:14.0/usb1/1-8 Great 1-8.2 Great 1-8.2 │ 1. 0/host4/target4:0:0/4:0:0:0/block/sdb │ ├── 8:2->.. /.. / devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda2 │ ├── 8:32->.. / devices/pci0000:00/0000:00:14.0/usb1/1-8 Compact 1-8.2 Greater 1- 8.2:1.0/host4/target4:0:0/4:0:0:1/block/sdc │ ├── 8:48->.. / devices/pci0000:00/0000:00:14.0/usb1/1-8 devices/pci0000:00/0000:00:14.0/usb1/1 1-8.2 devices/pci0000 1-8.2:1.0/host4/target4:0:0/4:0:0:2/block/sdd │ ├── 8 │ 5->.. /.. / devices/pci0000: 00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda5 │ └── 8:6->.. /.. / devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda6 └── char ├── 10 │ 1->.. /.. / devices/virtual/misc/psaux ├── 10 : 183->.. / devices/virtual/misc/hw_random ├── 10 ├── 184->.. /.. / devices/virtual/misc/microcode ├── 10 ├── 20->. /.. / devices/virtual/misc/tun ├── 10 ├── 10 ├── 223->.. /.. / devices/virtual/misc/uinput ├── 10 ├── 10 ├── 22 28->.. /.. / devices/virtual/misc/mcelog ├── 10 >. /.. / devices/virtual/misc/hpet ├── 10 devices/virtual/misc/hpet 229->. /.. / devices/virtual/misc/fuse ├── 10 devices/virtual/misc/hpet 231->. /.. / devices/virtual/misc/snapshot ├── 1015 32->.. /.. / devices/virtual/misc/kvm 12345678910111213141516171819202122232425262728293031/sys directory shows the device level directories in all systems. The relationship is roughly as follows

Kobjectlinux3.4

Struct kobject {const char * name; struct list_head entry; / / used for mount to kset list struct kobject * parent; struct kset * kset; struct kobj_type * ktype; / / points to the object type description with a pointer struct sysfs_dirent * sd; / / struct kref kref; / / reference count unsigned int state_initialized:1; unsigned int state_in_sysfs:1; unsigned int state_add_uevent_sent:1 Unsigned int state_remove_uevent_sent:1; unsigned int uevent_suppress:1;}; 123456789101112131415 where sysfs_dirent

/ * sysfs_dirent-the building block of sysfs hierarchy. Each and * every sysfs node is represented by single sysfs_dirent. * As long as s_count reference is held, the sysfs_dirent itself is * accessible. Dereferencing s_elem or any other outer entity * requires s_active reference. * / struct sysfs_dirent {atomic_t scounters; atomic_t activities; # ifdef CONFIG_DEBUG_LOCK_ALLOC struct lockdep_map dep_map; # endif struct sysfs_dirent * swatches; const char * completion; struct sysfs_dirent name; struct rb_node squarrb; union {struct completion * completion; struct sysfs_dirent * removed_list;} u; const void * swords; / * namespace tag * / unsigned int s_hash / * ns + name hash * / union {struct sysfs_elem_dir slots dirt; struct sysfs_elem_symlink slots symlinks; struct sysfs_elem_attr swatches; struct sysfs_elem_bin_attr swatches binattrs; unsigned short scuttles; umode_t scuttle; unsigned int scuttino; struct sysfs_inode_attrs * scuttle;}; 123456789101112131415161718192021222324252627282930313233343536373839kset struct kset {struct list_head list; / / double list head spinlock_t list_lock; struct kobject kobj / / embedded kobject const struct kset_uevent_ops * uevent_ops; / / event action set}; 123456 where kset_uevent_ops defines the actions that kset can perform on the included kobject, including event filtering and exporting environment variable operations.

Struct kset_uevent_ops {int (* const filter) (struct kset * kset, struct kobject * kobj); / / event filtering const char * (* const name) (struct kset * kset, struct kobject * kobj); int (* const uevent) (struct kset * kset, struct kobject * kobj, struct kobj_uevent_env * env); / / Environment variable export} 1234567devicedevice data structure is used to describe the hierarchical relationship between device-related information and devices, and the relationship between devices, bus and drivers.

Struct device {struct device * parent; struct device_private * p; struct kobject kobj; const char * init_name; / * initial name of the device * / const struct device_type * type; struct mutex mutex; / * mutex to synchronize calls to * its driver. * / struct bus_type * bus; / * type of bus device is on * / struct device_driver * driver; / * which driver has allocated this device * / void * platform_data; / * Platform specific data, device core doesn't touch it * / struct dev_pm_info power; struct dev_pm_domain * pm_domain; # ifdef CONFIG_NUMA int numa_node; / * NUMA node this device is close to * / # endif U64 * dma_mask / * dma mask (if dma'able device) * / U64 coherent_dma_mask;/* Like dma_mask, but for alloc_coherent mappings as not all hardware supports 64 bit addresses for consistent allocations such descriptors. * / struct device_dma_parameters * dma_parms; struct list_head dma_pools; / * dma pools (if dma'ble) * / struct dma_coherent_mem * dma_mem; / * internal for coherent mem override * / # ifdef CONFIG_CMA struct cma * cma_area; / * contiguous memory area for dma allocations * / # endif / * arch specific additions * / struct dev_archdata archdata; struct device_node * of_node / * associated device tree node * / dev_t devt; / * dev_t, creates the sysfs "dev" * / U32 id; / * device instance * / spinlock_t devres_lock; struct list_head devres_head; struct klist_node knode_class; struct class * class; const struct attribute_group * groups; / * optional groups * / void (* release) (struct device * dev);}; 1234567891011121314151618192022325262728293031323435363839404143444648495051525355565758 related functions to operate device:

Device_register (): create related directories under / sys/devices device_unregister (): remove related directories get_device: increase reference count put_device: decrease reference count 1234device_driver struct device_driver {const char * name; struct bus_type * bus; struct module * owner; const char * mod_name; / * used for built-in modules * / bool suppress_bind_attrs / * disables bind/unbind via sysfs * / const struct of_device_id * of_match_table; int (* probe) (struct device * dev); int (* remove) (struct device * dev); void (* shutdown) (struct device * dev); int (* suspend) (struct device * dev, pm_message_t state); int (* resume) (struct device * dev); const struct attribute_group * * groups; const struct dev_pm_ops * pm; struct driver_private * p }; 12345678910111213141516171819202122

Struct bus_type {const char * name; const char * dev_name; struct device * dev_root; struct bus_attribute * bus_attrs; struct device_attribute * dev_attrs; struct driver_attribute * drv_attrs; int (* match) (struct device * dev, struct device_driver * drv); int (* uevent) (struct device * dev, struct kobj_uevent_env * env); int (* probe) (struct device * dev); int (* remove) (struct device * dev) Void (* shutdown) (struct device * dev); int (* suspend) (struct device * dev, pm_message_t state); int (* resume) (struct device * dev); const struct dev_pm_ops * pm; struct iommu_ops * iommu_ops; struct subsys_private * p;}; 1234567891011121314151617181920212223Kernel modules module installation and uninstall not all module is added when compiling the kernel, mainly considering the following:

All modules are compiled into the kernel by default, which will make the kernel bloated

Makes it difficult for the kernel to expand

Linux provides a mechanism for dynamically installing modules that can be compiled into ko files, delaying link time:

When needed, complete the link process through insmod, link the system kernel to export symbols, and complete the pluggable function of the module.

Correspondingly, the removal module is done through rmmod

Considering the interdependence between modules, you need to check whether the dependent module has been loaded while loading the module. Modprobe can automatically load the dependent module, which works according to the dependency information: depmod-A can generate dependency information for new modules. It is recommended to use modprobe to install and uninstall the module:

Modprobe module [parameter] modprobe-r modules; if the dependent module has been called by other modules, it will not be removed; on the contrary, remove 12 exported symbol table modules to achieve dependencies through everywhere symbol tables

EXPORT_SYMBOL (function_name) exports functional symbols EXPORT_SYMBOL_GPL (function_name) for use by modules that conform to the GPL protocol

The minor number is used by the kernel to determine exactly which device is being referred to. Depending on how your driver is written (as we will see below), you can either get a direct pointer to your device from the kernel, or you can use the minor number yourself as an index into a local array of devices. Either way, the kernel itself knows almost nothing about minor numbers beyond the fact that they refer to devices implemented by your driver.

Common errors in Module Stack drivers debuginvalid format

# insmod hello.ko Error inserting'. / hello.ko':-1 Invalid module format 12cat / var/log/messages

The above is the content of this article about "what is Linux Driver". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report