Linux Device Driver note-character Device Driver

Source: Internet
Author: User
Article Title: linux Device Driver note-character Device Driver. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

※"Global" means that if a device is enabled multiple times, all file descriptors that open it share the data. "Persistence" means that if the device is turned on again after it is disabled, data will not be lost.

※Real drivers use interruptions to synchronize with their devices

Master and secondary device numbers

※The primary device ID identifies the driver corresponding to the device. The secondary device ID is used by the kernel to correctly identify the device indicated by the device file. We can get a direct pointer to the kernel device through the secondary device number, or use the secondary device number as the index of the local device array, no matter which method, except that the sub-device number points to the device implemented by the driver, the kernel itself basically does not care about any other messages about the sub-device number.

※Internal expression of the device number

NIn the kernel,Dev_tType (in Is used to save the device number-including the master device number and sub-device number.

NMAJOR (dev_t dev );MINOR (dev_t dev)MKDEV (int major, int minor)

※Assign and release device numbers

N :

Int register_chrdev_region (dev_t first, unsigned int count, char * name );

int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, 
  
                        unsigned int count, char *name);
  
void unregister_chrdev_region(dev_t first, unsigned int count);
  
  
    
  
The driver needs to connect the device number with the internal functions used to perform device operations.
   

※Dynamically allocate the master device number

NSome master device numbers have been statically assigned to most public devices. In the kernel source code treeDocumentation/device.txtFile to find the list of these devices.

NOnce the driver is widely used, randomly selected master device numbers may cause conflicts and troubles.

NWe strongly recommend that you do not randomly select a device number that is not currently used as the master device number, but use the dynamic allocation mechanism to obtain your master device number.

NThe disadvantage of dynamic allocation is that because the number assigned to your master device cannot always be the same, you cannot create a device node in advance. However, this is not a problem because once a device number is assigned, you can/Proc/devicesRead. To load a device driverInsmodIs replaced with a simple script./Proc/devicesObtain the assigned master device number and create a node.

#!/bin/sh
  
module="scull"
  
device="scull"
  
mode="664"
  
  
    
  
# invoke insmod with all arguments we got
  
# and use a pathname, as newer modutils don't look in . by default
  
  
    
  
/sbin/insmod ./$module.ko $* || exit 1
  
  
    
  
# remove stale nodes
  
  
    
  
rm -f /dev/${device}[0-3]
  
major=$(awk "\\$2=  =\"$module\" {print \\$1}" /proc/devices)
  
mknod /dev/${device}0 c $major 0
  
mknod /dev/${device}1 c $major 1
  
mknod /dev/${device}2 c $major 2
  
mknod /dev/${device}3 c $major 3
  
  
    
  
# give appropriate group/permissions, and change the group.
  
# Not all distributions have staff, some have "wheel" instead.
  
  
    
  
group="staff"
  
grep -q '^staff:' /etc/group || group="wheel"
  
  
    
  
chgrp $group /dev/${device}[0-3]
  
chmod $mode  /dev/${device}[0-3]
  

NThe best way to allocate the master device number is to use dynamic allocation by default, while leaving room for specifying the master device number when loading or even compiling.

NHere's the code we use inScull'S source to get a major number:

if (scull_major) {
  
    dev = MKDEV(scull_major, scull_minor);
  
    result = register_chrdev_region(dev, scull_nr_devs, "scull");
  
} else {
  
    result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs, "scull");
  
    scull_major = MAJOR(dev);
  
}
if (result < 0) {
  
    printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
  
    return result;
  
}
   
  
   

[1] [2] [3] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.