Linux driver learning-one difference between 2.4 and 2.6

Source: Internet
Author: User


2.4 use the kernel to register the driver:
Int register_chrdev (unsigned int major, const char * Name, struct file_operations * FoPs );
2.4 use the kernel to log out of the driver:
Int unregister_chrdev (unsigned int major, const char * Name );
2.4 After the kernel driver is registered, use the following code to create a device file:
Static devfs_handle_t devfs_handle;
Devfs_handle = devfs_register (null, device_name, devfs_fl_default,
Button_major, & sbc2410_buttons_fops, null );
2.4 run the following code to remove a device file:
Devfs_unregister (devfs_handle );
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
2.6 Use the driver I to register the device Number:
(1) If the master device number is known in advance, use:
Int register_chrdev_region (dev_t first, unsigned int count, char * Name );
(2) If the master device number is 0, dynamic allocation is required:
Int alloc_chrdev_region (dev_t * Dev, unsigned int firstminor,
Unsigned int count, char * Name );
2.6 release the device number to use:
Void unregister_chrdev_region (dev_t first, unsigned int count );
2.6 kernel characters must be used for device driver registration:
Struct cdev * my_cdev = cdev_alloc ();
My_cdev-> Ops = & chr_fops;
Void cdev_init (struct cdev * cdev, struct file_operations * FoPs );
Int cdev_add (struct cdev * Dev, dev_t num, unsigned int count );
2.6 kernel character device driver to be removed:
Void cdev_del (struct cdev * Dev );
2.6 After the kernel driver is registered, use the following code to create a device file:
Devfs_mk_cdev (mkdev (led_major, led_minor ),
S_ifchr | s_irusr | s_iwusr | s_irgrp, device_name );
2.6 run the following code to remove a device file:
Devfs_remove (device_name );
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
You can also run the following command to create a device file:
Mknod/dev/device file name character device (C is a character device, B is a block device) master device number times device number
Example: mknod/dev/testchar C 100 0
Device deletion entry:
Rm/dev/testchar
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
2.4 driver overall writing framework:
Static int _ init my_init (void)
{
// Register the device driver
Register_chrdev (unsigned int major, const char * Name, struct file_operations * FoPs );
// Create a device file
Static devfs_handle_t devfs_handle;
Devfs_handle = devfs_register (null, device_name, devfs_fl_default,
Button_major, & sbc2410_buttons_fops, null );
}
Static int _ exit my_exit (void)
{
// Remove the Device File
Devfs_unregister (devfs_handle );
// Cancel the device driver
Unregister_chrdev (unsigned int major, const char * Name );
}
Module_init (my_init );
Module_exit (my_exit );
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------------
2.6 driver overall writing framework:
Static int _ init my_init (void)
{
// Assign the device number
If (master device number)
{
Sbc2440_leds_dev = mkdev (led_major, led_minor );
Result = register_chrdev_region (sbc2440_leds_dev, Count, device_name );
}
Else
{
Result = alloc_chrdev_region (& sbc2440_leds_dev, led_minor, Count, device_name );
Led_major = major (sbc2440_leds_dev );
}
// Register the character Device Driver
Sbc2440_leds_cdev = cdev_alloc ();
If (sbc2440_leds_cdev! = NULL)
{
Cdev_init (sbc2440_leds_cdev, & sbc2440_leds_fops );
Sbc2440_leds_cdev-> Ops = & sbc2440_leds_fops;
Sbc2440_leds_cdev-> owner = this_module;
If (cdev_add (sbc2440_leds_cdev, sbc2440_leds_dev, count ))
Printk (kern_notice "someting wrong when adding sbc2440_leds_cdev! /N ");
Else
Printk ("Success adding sbc2440_leds_cdev! /N ");
}

// Create a device file
Devfs_handle = devfs_register (null, device_name, devfs_fl_default,
Button_major, & sbc2410_buttons_fops, null );
}
Static int _ exit my_exit (void)
{
// Remove the Device File
Devfs_remove (device_name );
// Deregister the character device
Cdev_del (sbc2440_leds_cdev );
// Release the device ID:
Unregister_chrdev_region (sbc2440_leds_dev, count );
}
Module_init (my_init );
Module_exit (my_exit );
------------------------

Driver Compilation:
Before the driver is compiled, the kernel used must be compiled. Otherwise, the driver cannot be compiled.
Drive useful functions
Set_irq_type Function
The type in set_irq_type (IRQ, type) is as follows:
# Define irqt_noedge (0)
# The rising edge of define irqt_rising (_ irqt_risedge) is valid.
# Define irqt_falling (_ irqt_faledge) the lower elevation is valid.
# Define irqt_bothedge (_ irqt_risedge |__ irqt_faledge) is valid along bilateral edges.
# Define irqt_low (_ irqt_lowlvl) low level valid
# Define irqt_high (_ irqt_highlvl) valid at a high level
# Define irqt_probe (1 <4)
Key driver summary:
Set_gpio_mode_user (k-> gpio_port, gpio_mode_in );
Up = read_gpio_bit (k-> gpio_port );
S3c2410_gpio_cfgpin (k-> gpio_port, K-> gpio_set );
Set_irq_type (k-> irq_no, irqt_rising );
The functions of the above Code are as follows:
The first is to set the usage mode of the gpio port, which is the input mode. There are four modes in total:
1, gpio_mode_in (input mode)
2, gpio_mode_out (output mode)
3, gpio_mode_alt0 (third function)
4, gpio_mode_alt1 (fourth function)
The second is the signal to read the gpio port.
The third is the initialization port.
The fourth is to set the interrupt trigger mode.
Enables reading signals from the IO port, which is valid for reading the rising edge.
Before applying for interruption, you must initialize the port. Otherwise, the port cannot be used. At the end of the interrupt handler, you must reset the port to restore it to the Initial State before applying for interruption.
S_irusr | s_iwusr | s_irgrp | s_iwgrp
S_irusr
Permits (allowed) the file's owner to read it.
S_iwusr
Permits the file's owner to write to it.
S_irgrp
Permits the file's group to read it.
S_iwgrp
Permits the file's group to write to it.

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.