Hello World : Module Parameter =============================== .. tab-set:: .. tab-item:: Hello World : Module Parameter * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to pass module parameters to Linux kernel module ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below APIs ? * `module_param `_ * `MODULE_PARM_DESC `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Passing arguments ` * :ref:`Example 1 : passing integer as argument ` * :ref:`Makefile for prgm1.c ` * :ref:`Compile and Load ` * :ref:`Example 2 : passing short as argument ` * :ref:`Makefile for prgm2.c ` * :ref:`Compile and Load ` * :ref:`Example 3 : passing integer as argument ` * :ref:`Makefile for prgm3.c ` * :ref:`Compile and Load ` * :ref:`Example 4 : passing integer as argument ` * :ref:`Makefile for prgm4.c ` * :ref:`Compile and Load ` * :ref:`Example 5 : passing integer as argument ` * :ref:`Makefile for prgm5.c ` * :ref:`Compile and Load ` * :ref:`Summary ` .. _p2_moduleWithParameter_0: .. tab-set:: .. tab-item:: Passing arguments * To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the module_param() macro. .. _p2_moduleWithParameter_1: .. tab-set:: .. tab-item:: Example 1 : passing integer as argument * Define the module parameter for integer .. code-block:: c module_param(myint, int, 0644); MODULE_PARM_DESC(myint, "An integer"); * Pass the module parameter during module load .. code-block:: c $ sudo insmod ./prgm1.ko myint=56 * See the full program below, .. tab-set:: .. tab-item:: prgm1.c .. literalinclude:: p2_moduleWithParameter/prgm1.c :language: c :linenos: :emphasize-lines: 10, 12, 13 .. _p2_moduleWithParameter_2: .. tab-set:: .. tab-item:: Makefile for prgm1.c .. code-block:: c :linenos: obj-m += prgm1.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean .. _p2_moduleWithParameter_3: .. tab-set:: .. tab-item:: Compile and Load .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 9, 11 $ make all $ sudo insmod ./prgm1.ko myint=56 $ dmesg [495039.286549] Hello, world [495039.286550] myint is an integer: 56 $ sudo rmmod prgm1 $ dmesg [495039.286549] Hello, world [495039.286550] myint is an integer: 56 [495049.549716] Goodbye, world .. _p2_moduleWithParameter_4: .. tab-set:: .. tab-item:: Example 2 : passing short as argument * Define the module parameter for short .. code-block:: c module_param(myshort, short, 0660); MODULE_PARM_DESC(myshort, "A short integer"); * Pass the module parameter during module load .. code-block:: c $ sudo insmod ./prgm2.ko myshort=10 * See the full program below, .. tab-set:: .. tab-item:: prgm2.c .. literalinclude:: p2_moduleWithParameter/prgm2.c :language: c :linenos: :emphasize-lines: 10, 12, 13 .. _p2_moduleWithParameter_5: .. tab-set:: .. tab-item:: Makefile for prgm2.c .. code-block:: c :linenos: obj-m += prgm2.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean .. _p2_moduleWithParameter_6: .. tab-set:: .. tab-item:: Compile and Load .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 9, 11 $ make all $ sudo insmod ./prgm2.ko myshort=10 $ dmesg [495335.891744] Hello, world [495335.891745] myshort is a short integer: 10 $ sudo rmmod prgm2 $ dmesg [495335.891744] Hello, world [495335.891745] myshort is a short integer: 10 [495343.492308] Goodbye, world .. _p2_moduleWithParameter_7: .. tab-set:: .. tab-item:: Example 3 : passing long as argument * Define the module parameter for long .. code-block:: c module_param(mylong, long, 0400); MODULE_PARM_DESC(mylong, "A long integer"); * Pass the module parameter during module load .. code-block:: c $ sudo insmod ./prgm3.ko mylong=500 * See the full program below, .. tab-set:: .. tab-item:: prgm3.c .. literalinclude:: p2_moduleWithParameter/prgm3.c :language: c :linenos: :emphasize-lines: 10, 12, 13 .. _p2_moduleWithParameter_8: .. tab-set:: .. tab-item:: Makefile for prgm3.c .. code-block:: c :linenos: obj-m += prgm3.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean .. _p2_moduleWithParameter_9: .. tab-set:: .. tab-item:: Compile and Load .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 9, 11 $ make all $ sudo insmod ./prgm3.ko mylong=500 $ dmesg [495455.686602] Hello, world [495455.686604] mylong is a long integer: 500 $ sudo rmmod prgm3 $ dmesg [495455.686602] Hello, world [495455.686604] mylong is a long integer: 500 [495464.038982] Goodbye, world .. _p2_moduleWithParameter_10: .. tab-set:: .. tab-item:: Example 4 : passing string as argument * Define the module parameter for string .. code-block:: c module_param(mystring, charp, 0000); MODULE_PARM_DESC(mystring, "A character string"); * Pass the module parameter during module load .. code-block:: c $ sudo insmod ./prgm4.ko mystring="linux" * See the full program below, .. tab-set:: .. tab-item:: prgm4.c .. literalinclude:: p2_moduleWithParameter/prgm4.c :language: c :linenos: :emphasize-lines: 10, 12, 13 .. _p2_moduleWithParameter_11: .. tab-set:: .. tab-item:: Makefile for prgm4.c .. code-block:: c :linenos: obj-m += prgm4.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean .. _p2_moduleWithParameter_12: .. tab-set:: .. tab-item:: Compile and Load .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 9, 11 $ make all $ sudo insmod ./prgm4.ko mystring="linux" $ dmesg [495668.254605] Hello, world [495668.254607] mystring is a string: linux $ sudo rmmod prgm4 $ dmesg [495668.254605] Hello, world [495668.254607] mystring is a string: linux [495697.631035] Goodbye, world .. _p2_moduleWithParameter_13: .. tab-set:: .. tab-item:: Example 5 : passing integer array as argument * Define the module parameter for integer array .. code-block:: c module_param_array(myintArray, int, &arr_argc, 0000); MODULE_PARM_DESC(myintArray, "An array of integers"); * Pass the module parameter during module load .. code-block:: c $ sudo insmod ./prgm5.ko myintArray=10,20,30 * See the full program below, .. tab-set:: .. tab-item:: prgm5.c .. literalinclude:: p2_moduleWithParameter/prgm5.c :language: c :linenos: :emphasize-lines: 10, 11, 13, 14 .. _p2_moduleWithParameter_14: .. tab-set:: .. tab-item:: Makefile for prgm5.c .. code-block:: c :linenos: obj-m += prgm5.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean .. _p2_moduleWithParameter_15: .. tab-set:: .. tab-item:: Compile and Load .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 13, 15 $ make all $ sudo insmod ./prgm5.ko myintArray=10,20,30 $ dmesg [504234.493933] Hello, world [504234.493935] myintArray[0] = 10 [504234.493935] myintArray[1] = 20 [504234.493936] myintArray[2] = 30 [504234.493936] got 3 arguments for myintArray. $ sudo rmmod prgm5 $ dmesg [504234.493933] Hello, world [504234.493935] myintArray[0] = 10 [504234.493935] myintArray[1] = 20 [504234.493936] myintArray[2] = 30 [504234.493936] got 3 arguments for myintArray. [504240.118160] Goodbye, world .. _p2_moduleWithParameter_16: .. tab-set:: .. tab-item:: Summary =============================== ======================================= API Learning =============================== ======================================= module_param macro used to initialize the arguments MODULE_PARM_DESC used to document arguments that the module can take =============================== ======================================= .. card:: See Also * Other topic * :doc:`p1_basicModule` * Next Chapter * :doc:`../chapter2_kthreads/chapter2_kthreads` * Other Chapters * :doc:`../chapter3_tasklets/chapter3_tasklets` * :doc:`../chapter4_workqueue/chapter4_workqueue` * :doc:`../chapter5_timer/chapter5_timer` * :doc:`../chapter8_linkedList/chapter8_linkedList` * :doc:`../chapter9_fileSystem/chapter9_fileSystem` * :doc:`../chapter10_netlink/chapter10_netlink`