Initialization parameters of Linux driver development

Source: Internet
Author: User

First on the code:

#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/ Moduleparam.h>static char* whom = "World"; static int howmany = 1;module_param (howmany, int, s_irugo); Module_param (WH OM, Charp, s_irugo); static int hello_init (void) {        int i;        PRINTK (Kern_alert "Hello, world! I ' m lizhixing.\n ");        for (i=0; i< howmany; ++i)        {                printk (kern_alert "Hello,%s!\n", whom);        }        return 0;}; static void Hello_exit (void) {        printk (kern_alert "Good Bye, Cruel world. Tommorow is another day! \ n ");}; Module_init (Hello_init); Module_exit (Hello_exit);


The Linux driver can accept parameters when initializing, the command is as follows:

Insmod Hello.ko howmany=10 whom= "Daddy"

Here 2 parameters are passed, Howmany and whom respectively.

In order for the module to be able to receive these two parameters:

1. Contains the header file moduleparam.h, this header file defines what the parameters need.
2. Declare the parameters. Several types of support are:
Bool/invbool, are all Boolean, Invbool apology, will be true and false interchange, good wonderful design, understand not .....
Charp, that's the string.
Int/long/short/uint/ulong/ushort, these are familiar old faces that can not be cooked.
There is the ability to support the array parameter, and that's it. If you want to use a different type, you need to use the method of registering the callback function to handle it yourself.
3. Register this parameter with Module_param (name, type, permission) so that the kernel will assign the variable using the arguments in the command line.

Load module:
Insmod Hello.ko howmany=10 whom= "Daddy"

DMESG, there are print out:








Initialization parameters of Linux driver development

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.