The difference between Linux kernel drivers 2.6 and 2.4

Source: Internet
Author: User
Tags posix

With the release of Linux2.6, the drivers for each device are rewritten to varying degrees due to the new changes made by the 2.6 kernel. For the convenience of Linux enthusiasts, I put my own share out of this worthless file. This article lists the vast majority of changes in the 2.6 kernel with previous versions, but unfortunately the use of individual functions is not listed in detail due to limited time and effort.

1. Use the new entrance
Must include <linux/init.h>
Module_init (Your_init_func);
Module_exit (Your_exit_func);
Old version: int init_module (void);
void Cleanup_module (Voi);
Both of the 2.4 can be used, as the following entry function does not have to display any header files.

2. GPL
Module_license ("Dual BSD/GPL");
Old version: Module_license ("GPL");

3. Module parameters
Must explicitly include <linux/moduleparam.h>
Module_param (name, type, perm);
Module_param_named (name, value, type, perm);
Parameter definition
Module_param_string (name, string, Len, perm);
Module_param_array (name, type, num, perm);
Old version: Module_parm (Variable,type);
Module_parm_desc (Variable,type);

4. Module aliases
Module_alias ("Alias-name");
This is new, in the old version in the/etc/modules.conf configuration, now in the code can be implemented.

5. Module Count
int Try_module_get (&module);
Module_put ();
Old version: Mod_inc_use_count and Mod_dec_use_count

Http://www.fsl.cs.sunysb.edu/~sean/parser.cgi?modules

In 2.4 modules, the Mod_inc_use_count macro was used to prevent unloading of the module while there was an open file. The 2.6 kernel, however, knows not to unload a module that owns a character device that's currently open.
However, this requires, the module is explicit in specifying ownership of character devices, using the This_module mac Ro.

You also has to take out all calls to Mod_inc_use_count and Mod_dec_use_count.

static struct File_operations FoPs =
{
. Owner = This_module,
. Read = Device_read,
. write = Device_write,
. open = Device_open,
. Release = Device_release
}

The 2.6 kernel considers modules that with the deprecated facility to being unsafe, and does not permit their unloading, even With Rmmod-f.

2.6,2.5 Kbuild does not need to add mod_inc_use_count everywhere to eliminate module offload competition (module unload race)

6. Symbol Export
Only the exported symbols shown can be used by other modules, and all symbols are not exported by default, without the use of export_no_symbols
Boss Ben: All symbols are exported by default, unless you use Export_no_symbols

7. Kernel version Check
When you need to include <linux/module.h> in multiple files, you do not have to define __no_version__
Old version: When <linux/module.h> is included in multiple files, __no_version__ must be defined in other files except the main file to prevent the version from being duplicated.

8. Device number
Kdev_t was abolished and not available, the new dev_t expanded to 32-bit, 12-bit main device number, 20-bit device number.
unsigned int iminor (struct inode *inode);
unsigned int imajor (struct inode *inode);
Old version: 8-bit main device number, 8-bit secondary device number
int MAJOR (kdev_t dev);
int MINOR (kdev_t dev);

9, memory allocation header file changes
All memory allocation functions are contained in the header file <linux/slab.h>, while the original <linux/malloc.h> does not exist
Old version: Memory allocation function included in header file <linux/malloc.h>

10. Preliminary examination of structure
GCC begins the initialization of the struct struct body with ANSI C:
static struct Some_structure = {
. field1 = value,
. Field2 = value,
..
};
Old version: Non-standard preliminary format
static struct Some_structure = {
Field1:value,
Field2:value,
..
};

11. User Mode Helper
int Call_usermodehelper (char *path, Char **argv, char **envp, int wait);
New Wait parameter

12, Request_module ()
Request_module ("foo-device-%d", number);
Old version:
Char module_name[32];
printf (module_name, "foo-device-%d", number);
Request_module (module_name);

13, the change of character device caused by dev_t
1, the primary and secondary device number is
unsigned iminor (struct inode *inode);
unsigned imajor (struct inode *inode);
2, the old Register_chrdev () usage has not changed, remains backwards compatible, but cannot access devices with a device number greater than 256.
3, the new interface is
a) registers the character device range
int register_chrdev_region (dev_t from, unsigned count, char *name);
B) Dynamic request main device number
int alloc_chrdev_region (dev_t *dev, unsigned baseminor, unsigned count, char *name);
Look at these two functions depressed bar ^_^! How to relate to the file_operations structure? Don't worry!
C) contains <linux/cdev.h>, using struct Cdev and file_operations connection
struct Cdev *cdev_alloc (void);
void Cdev_init (struct cdev *cdev, struct file_operations *fops);
int Cdev_add (struct cdev *cdev, dev_t Dev, unsigned count);
(for Cdev structure, and fops connection, add the device to the system!) It's so complicated! )
D) void Cdev_del (struct cdev *cdev); The
will run only if the Cdev_add execution succeeds.
e) Auxiliary function
Kobject_put (&cdev->kobj);
struct Kobject *cdev_get (struct Cdev *cdev);
void Cdev_put (struct cdev *cdev);
This part of the change has some relevance to the new/sys/dev.

14, new access to/proc
<linux/seq_file.h>
can only get string in previous/proc, seq_file operation can get many kinds of data such as long.
Related functions:
static struct seq_operations must implement the individual member functions in this file_operations-like data.
seq_printf ();
int seq_putc (struct seq_file *m, char c);
int seq_puts (struct seq_file *m, const char *s);
int Seq_escape (struct seq_file *m, const char *s, const char *ESC);
int Seq_path (struct seq_file *m, struct vfsmount *mnt,
struct dentry *dentry, char *esc);
Seq_open (file, &ct_seq_ops);
and so on

15, the underlying memory allocation
1, <linux/malloc.h> header file changed to <linux/slab.h>
2, the allocation flag Gfp_buffer was canceled, replaced by Gfp_noio and GFP_ Nofs
3, new __gfp_repeat,__gfp_nofail,__gfp_noretry allocation flag
4, page allocation function alloc_pages (), Get_free_page () is included in <linux/ Gfp.h>
5, several new functions have been added to the NUMA system:
A) struct page *alloc_pages_node (int node_id, unsigned int gfp_mask, unsigned int ord ER);
b) void free_hot_page (struct page *page);
c) void free_cold_page (struct page *page);
6, add memory pools
<linux/mempool.h>
mempool_t *mempool_create (int min_nr, mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, void *pool_data);
void *mempool_alloc (mempool_t *pool, int gfp_mask);
void Mempool_free (void *element, mempool_t *pool);
int mempool_resize (mempool_t *pool, int new_min_nr, int gfp_mask);

16, PER-CPU variable
Get_cpu_var ();
Put_cpu_var ();
void *alloc_percpu (type);
void free_percpu (const void *);
Per_cpu_ptr (void *ptr, int cpu)
Get_cpu_ptr (PTR)
Put_cpu_ptr (PTR)
Older versions use
Define_per_cpu (type, name) ;
Export_per_cpu_symbol (name);
EXPORT_PER_CPU_SYMBOL_GPL (name);
Declare_per_cpu (type, name);
define_per_cpu (int, mypcint); The
2.6 kernel uses a stripped-down scheduling method for these macros to be unsafe.

17, Kernel time change
1, the current platform of the Hz is
alpha:1024/1200; arm:100/128/200/1000; cris:100; i386:1000; ia-64:1024; m68k:100; m68k-nommu:50-1000; mips:100/128/1000; mips64:100; pa-risc:100/1000; powerpc32:100; powerpc64:1000; s/390:100; sparc32:100; sparc64:100; superh:100/1000; uml:100; v850:24-100; x86-64:1000.
2, due to the change of Hz, the original jiffies counter quickly overflow, introduced a new counter jiffies_64
3, #include <linux/jiffies.h>
u64 my_time = get_ Jiffies_64 ();
4, the new time structure adds the nanosecond member variable
struct TIMESPEC current_kernel_time (void);
5, his timer function has not changed, new
void add_timer_on (struct timer_list *timer, int cpu);
6, new nanosecond delay function
Ndelay (),
7, POSIX clocks reference KERNEL/POSIX-TIMERS.C

18, Work queue (Workqueue)
1, Task queue interface functions are canceled, new Workqueue interface functions
struct workqueue_struct *create_workqueue (const char *name);
Declare_work (name, void (*function) (void *), void *data);
Init_work (struct work_struct *work,
Void (*function) (void *), void *data);
Prepare_work (struct work_struct *work,
Void (*function) (void *), void *data);
2, declare struct WORK_STRUCT structure
int queue_work (struct workqueue_struct *queue, struct work_struct *work);
int queue_delayed_work (struct workqueue_struct *queue, struct work_struct *work,
unsigned long delay);
int cancel_delayed_work (struct work_struct *work);
void Flush_workqueue (struct workqueue_struct *queue);
void Destroy_workqueue (struct workqueue_struct *queue);
int schedule_work (struct work_struct *work);
int schedule_delayed_work (struct work_struct *work, unsigned long delay);

19, added "Libfs" to create VFS
LIBFS provides a large number of APIs for creating a new file system.
The main is the implementation of struct File_system_type.
Reference source code:
Drivers/hotplug/pci_hotplug_core.c
Drivers/usb/core/inode.c
Drivers/oprofile/oprofilefs.c
Fs/ramfs/inode.c
FS/NFSD/NFSCTL.C (Simple_fill_super () example)

20, the change of DMA
The unchanged are:
void *pci_alloc_consistent (struct Pci_dev *dev, size_t size, dma_addr_t *dma_handle);
void Pci_free_consistent (struct Pci_dev *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle);
The changes are:
1. void *dma_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, int flag);
void dma_free_coherent (struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle);
2, enumerated the mapping direction:
Enum Dma_data_direction {
dma_bidirectional = 0,
Dma_to_device = 1,
Dma_from_device = 2,
Dma_none = 3,
};
3. Single Mapping
dma_addr_t dma_map_single (struct device *dev, void *addr, size_t size, enum dma_data_direction direction);
void Dma_unmap_single (struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction direction);
4. Page Map
dma_addr_t dma_map_page (struct device *dev, struct page *page, unsigned long offset, size_t size, enum dma_data_direction direction);
void Dma_unmap_page (struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction direction);
5. Functions related to Scatter/gather:
int Dma_map_sg (struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction);
void Dma_unmap_sg (struct device *dev, struct scatterlist *sg, int nhwentries, enum dma_data_direction direction);
6. Non-conformance mapping (noncoherent DMA mappings)
void *dma_alloc_noncoherent (struct device *dev, size_t size, dma_addr_t *dma_handle, int flag);
void Dma_sync_single_range (struct device *dev, dma_addr_t dma_handle, unsigned long offset, size_t size,
enum dma_data_direction direction);
void dma_free_noncoherent (struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle);
7. DAC (double address cycle)
int Pci_dac_set_dma_mask (struct Pci_dev *dev, u64 mask);
void Pci_dac_dma_sync_single (struct Pci_dev *dev, dma64_addr_t dma_addr, size_t len, int direction);

21, mutual exclusion
New Seqlock is mainly used for:
1, a small amount of data protection
2, the data is relatively simple (no pointer), and the use of high frequency
3, the access to data that does not produce any side effects
4, the writer is not starved to death at the time of access
< Linux/seqlock.h>
Initialize
seqlock_t lock1 = seqlock_unlocked;
or seqlock_t Lock2; Seqlock_init (&LOCK2);
void Write_seqlock (seqlock_t *sl);
void Write_sequnlock (seqlock_t *sl);
int Write_tryseqlock (seqlock_t *sl);
void Write_seqlock_irqsave (seqlock_t *SL, long flags);
void Write_sequnlock_irqrestore (seqlock_t *SL, long flags);
void Write_seqlock_irq (seqlock_t *sl);
void Write_sequnlock_irq (seqlock_t *sl);
void Write_seqlock_bh (seqlock_t *sl);
void Write_sequnlock_bh (seqlock_t *sl);
unsigned int read_seqbegin (seqlock_t *sl);
int Read_seqretry (seqlock_t *sl, unsigned int iv);
unsigned int read_seqbegin_irqsave (seqlock_t *SL, long flags);
int Read_seqretry_irqrestore (seqlock_t *sl, unsigned int IV, long flags);

22, the kernel can be stripped
<linux/preempt.h>
Preempt_disable ();
Preempt_enable_no_resched ();
Preempt_enable_noresched ();
Preempt_check_resched ();

23. Sleep and Wake up
1, the original function is available, add the following functions:
Prepare_to_wait_exclusive ();
Prepare_to_wait ();
2. Wait for the queue to change
typedef int (*wait_queue_func_t) (wait_queue_t *wait, unsigned mode, int sync);
void Init_waitqueue_func_entry (wait_queue_t *queue, wait_queue_func_t func);

24. New completion event (completion events)
<linux/completion.h>
Init_completion (&my_comp);
void Wait_for_completion (struct completion *comp);
void complete (struct completion *comp);
void Complete_all (struct completion *comp);

25, RCU (read-copy-update)
Rcu_read_lock ();
void Call_rcu (struct rcu_head *head, void (*func) (void *arg),
void *arg);

26. Interrupt Processing
1, interrupt processing has a return value.
Irq_retval (handled);
2, CLI (), STI (), Save_flags (), and restore_flags () are no longer valid and should be used Local_save
_flags () or local_irq_disable ().
3, SYNCHRONIZE_IRQ () function has changed
4, added int can_request_irq (unsigned int IRQ, unsigned long flags);
5, REQUEST_IRQ () and FREE_IRQ () changed from <linux/sched.h> to <linux/interrupt.h>

27. Asynchronous I/O (AIO)
<linux/aio.h>
ssize_t (*aio_read) (struct KIOCB *iocb, char __user *buffer, size_t count, loff_t POS);
ssize_t (*aio_write) (struct KIOCB *iocb, const char __user *buffer, size_t count, loff_t POS);
Int (*aio_fsync) (struct KIOCB *, int datasync);
Added to the file_operation structure.
IS_SYNC_KIOCB (struct KIOCB *IOCB);
int aio_complete (struct KIOCB *IOCB, long res, long res2);

28. Network Drive
1, struct net_device *alloc_netdev (int sizeof_priv, const char *name, void (*setup) (struct net_device *));
struct Net_device *alloc_etherdev (int sizeof_priv);
2. New Napi (new API)
void Netif_rx_schedule (struct net_device *dev);
void Netif_rx_complete (struct net_device *dev);
int Netif_rx_ni (struct sk_buff *skb);
(The old version is Netif_rx ())

29. USB Drive
The old version of Struct Usb_driver was canceled, and the new structure was
struct Usb_class_driver {
Char *name;
struct File_operations *fops;
mode_t mode;
int minor_base;
};
int usb_submit_urb (struct URB *urb, int mem_flags);
Int (*probe) (struct usb_interface *intf,
const struct USB_DEVICE_ID *id);

30. Block I/O layer
This part makes the most changes. Ominous.

31, Mmap ()
int remap_page_range (struct vm_area_struct *vma, unsigned long from, unsigned long to, unsigned long siz E, pgprot_t Prot);
int Io_remap_page_range (struct vm_area_struct *vma, unsigned long from, unsigned long to, unsigned long size, pgprot_t Prot);
struct page * (*nopage) (struct vm_area_struct *area, unsigned long address, int *type);
Int (*populate) (struct vm_area_struct *area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long PG Off, int nonblock);
int install_page (struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t Pro T);
struct page *vmalloc_to_page (void *address);

32, 0 copy block I/O (zero-copy block I/O)
struct Bio *bio_map_user (struct block_device *bdev, unsigned long uaddr, unsigned int len, int write_to_vm);
void Bio_unmap_user (struct bio *bio, int write_to_vm);
int get_user_pages (struct task_struct *task, struct mm_struct *mm, unsigned long start, int len, int write, int force, str UCT page **pages, struct vm_area_struct **vmas);

33. High-end memory Operation Kmaps
void *kmap_atomic (struct page *page, enum km_type type);
void kunmap_atomic (void *address, enum km_type type);
struct page *kmap_atomic_to_page (void *address);
Older versions: Kmap () and Kunmap ().

34. Drive Model
Mainly used for device management.
1, Sysfs
2, Kobjects

Recommended articles:
Http:/www-900.ibm.com/developerworks/cn/linux/kernel/l-kernel26/index.shtml
Http:/www-900.ibm.com/developerworks/cn/linux/l-inside/index.shtml

There is no need to define "__kernel__" and "MODULE" in 2.6.
compile with the following makefile file:

Code:

Obj-m: = hello.o

Kdir: =/lib/modules/$ (Shell uname-r)/build
PWD: = $ (shell pwd)
Default
$ (make)-C $ (Kdir) m=$ (PWD) modules


The difference between Linux kernel drivers 2.6 and 2.4

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.