Compile USBSERIAL Module for target Kernel

Following source files must be donwloaded:

%ec%ba%a1%ec%b2%98

Makefile:

KERNEL_DIR=/home/jiafei427/workspace/tegra

CONFIG_MODULE_FORCE_UNLOAD=y

EXTRA_CFLAGS=-g -O0

obj-m += usbserial.o

usbserial-obj-y += console.o
#usbserial-obj-n += ezusb.o

usbserial-objs := usb-serial.o generic.o bus.o
PWD := $(shell pwd)
default:
$(MAKE) ARCH=arm64 CROSS_COMPILE=~/software/android-ndk-r11c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android- -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
#$(MAKE) ARCH=arm64 CROSS_COMPILE=~/software/android-ndk-r11c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android- -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean

 

#make

That’s it, then you will be able to get the usbserial.ko file to install.

 

**IMPORTANT**

Make sure you made the KERNEL source compiled with MODULE INSTALL SUPPORTED

otherwise, you will get following error:

jiafei427@CKUBU:~/workspace/kernel/modules/usb-serial$ make
make ARCH=arm64 CROSS_COMPILE=~/software/android-ndk-r11c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android- -C /home/jiafei427/workspace/tegra SUBDIRS=/home/jiafei427/workspace/kernel/modules/usb-serial modules
make[1]: Entering directory ‘/home/jiafei427/workspace/tegra’
CC [M] /home/jiafei427/workspace/kernel/modules/usb-serial/usb-serial.o
CC [M] /home/jiafei427/workspace/kernel/modules/usb-serial/generic.o
CC [M] /home/jiafei427/workspace/kernel/modules/usb-serial/bus.o
LD [M] /home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: “tty_port_tty_get” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “system_wq” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “bus_register” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “device_remove_file” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kmalloc” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “test_and_clear_bit” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “driver_register” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_out” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_unlock” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “single_open” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_alloc” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “handle_sysrq” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “single_release” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “seq_puts” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_open” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “find_next_bit” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “seq_printf” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_hangup” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “cancel_work_sync” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_kill_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_register_driver” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “mutex_unlock” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_autopm_get_interface” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “put_tty_driver” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “clear_bit” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “sprintf” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “seq_read” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “jiffies” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_set_operations” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_max_r” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “test_and_set_bit” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_close” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “device_del” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “dev_err” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_unlock_irqrestore” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_deregister” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__mutex_init” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “printk” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_ldisc_deref” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “driver_unregister” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_unpoison_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_poison_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “seq_putc” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_unlock_irq” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_init” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_insert_flip_string_fixed_flag” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “warn_slowpath_null” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_destroy” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_vhangup” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_ldisc_ref” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “idr_alloc” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “device_add” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “bus_unregister” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_store_new_id” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_insert_flip_string_flags” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_match_id” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_register_device” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “idr_remove” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “device_create_file” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_tty_wakeup” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_unregister_device” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_dev_info” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_submit_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_std_termios” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “idr_find_slowpath” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_get_dev” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “put_device” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_put_dev” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “schedule” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_lock_irq” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__raw_spin_lock_init” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_in_r” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_port_install” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_lock” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_get_intf” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “_raw_spin_lock_irqsave” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_termios_copy_hw” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_free” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_unregister_driver” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_hangup” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_show_dynids” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__wake_up” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “prepare_to_wait_event” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__tty_alloc_driver” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “mutex_lock_nested” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_disabled” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “warn_slowpath_fmt” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_termios_baud_rate” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “seq_lseek” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “schedule_timeout_interruptible” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “kfree” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “set_bit” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “device_initialize” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_match_one_id” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_register_driver” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “lockdep_init_map” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_kref_put” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “finish_wait” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “dev_warn” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “driver_attach” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “tty_flip_buffer_push” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_in” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “queue_work_on” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “snprintf” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “param_ops_ushort” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “dev_set_name” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_free_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_autopm_put_interface” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “__kfifo_out_r” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_alloc_urb” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
WARNING: “usb_put_intf” [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.ko] undefined!
CC /home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.o
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:8:1: error: variable ‘__this_module’ has initializer but incomplete type
__attribute__((section(“.gnu.linkonce.this_module”))) = {
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:9:2: error: unknown field ‘name’ specified in initializer
.name = KBUILD_MODNAME,
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:9:2: warning: excess elements in struct initializer
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:9:2: warning: (near initialization for ‘__this_module’)
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:10:2: error: unknown field ‘init’ specified in initializer
.init = init_module,
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:10:2: warning: excess elements in struct initializer
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:10:2: warning: (near initialization for ‘__this_module’)
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:14:2: error: unknown field ‘arch’ specified in initializer
.arch = MODULE_ARCH_INIT,
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:14:10: error: ‘MODULE_ARCH_INIT’ undeclared here (not in a function)
.arch = MODULE_ARCH_INIT,
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:14:2: warning: excess elements in struct initializer
.arch = MODULE_ARCH_INIT,
^
/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.c:14:2: warning: (near initialization for ‘__this_module’)
scripts/Makefile.modpost:114: recipe for target ‘/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.o’ failed
make[2]: *** [/home/jiafei427/workspace/kernel/modules/usb-serial/usbserial.mod.o] Error 1
Makefile:1401: recipe for target ‘modules’ failed
make[1]: *** [modules] Error 2
make[1]: Leaving directory ‘/home/jiafei427/workspace/tegra’
Makefile:17: recipe for target ‘default’ failed
make: *** [default] Error 2

#### make failed to build some targets (1 seconds) ####

 

Appendix: (Kernel Configuration Option Info.)

kernel_module

 


 

Earlier we discussed how to compile a kernel from the source.

This tutorial explains how to write a Kernel module using a simple Hello World example.

I. Utilities to Manipulate Kernel Modules

1. lsmod – List Modules that Loaded Already

lsmod command will list modules that are already loaded in the kernel as shown beblow.

# lsmod
Module                  Size  Used by
ppp_deflate            12806  0 
zlib_deflate           26445  1 ppp_deflate
bsd_comp               12785  0 
..

2. insmod – Insert Module into Kernel

insmod command will insert a new module into the kernel as shown below.

# insmod /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.ko

# lsmod | grep "squash"
squashfs               35834  0

3. modinfo – Display Module Info

modinfo command will display information about a kernel module as shown below.

# modinfo /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.ko

filename:       /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.ko
license:        GPL
author:         Phillip Lougher 
description:    squashfs 4.0, a compressed read-only filesystem
srcversion:     89B46A0667BD5F2494C4C72
depends:        
intree:         Y
vermagic:       3.5.0-19-generic SMP mod_unload modversions 686

4. rmmod – Remove Module from Kernel

rmmod command will remove a module from the kernel. You cannot remove a module which is already used by any program.

# rmmod squashfs.ko

5. modprobe – Add or Remove modules from the kernel

modprobe is an intelligent command which will load/unload modules based on the dependency between modules. Refer to modprobe commands for more detailed examples.

II. Write a Simple Hello World Kernel Module

1. Installing the linux headers

You need to install the linux-headers-.. first as shown below. Depending on your distro, use apt-get or yum.

# apt-get install build-essential linux-headers-$(uname -r)

2. Hello World Module Source Code

Next, create the following hello.c module in C programming language.

#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lakshmanan");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;    // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
}

module_init(hello_init);
module_exit(hello_cleanup);

Warning: All kernel modules will operate on kernel space, a highly privileged mode. So be careful with what you write in a kernel module.

3. Create Makefile to Compile Kernel Module

The following makefile can be used to compile the above basic hello world kernel module.

obj-m += hello.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

Use the make command to compile hello world kernel module as shown below.

# make

make -C /lib/modules/3.5.0-19-generic/build M=/home/lakshmanan/a modules
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-19-generic'
  CC [M]  /home/lakshmanan/a/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/lakshmanan/a/hello.mod.o
  LD [M]  /home/lakshmanan/a/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-19-generic'

The above will create hello.ko file, which is our sample Kernel module.

4. Insert or Remove the Sample Kernel Module

Now that we have our hello.ko file, we can insert this module to the kernel by using insmod command as shown below.

# insmod hello.ko

# dmesg | tail -1
[ 8394.731865] Hello world!

# rmmod hello.ko

# dmesg | tail -1
[ 8707.989819] Cleaning up module.

When a module is inserted into the kernel, the module_init macro will be invoked, which will call the function hello_init. Similarly, when the module is removed with rmmod, module_exit macro will be invoked, which will call the hello_exit. Using dmesg command, we can see the output from the sample Kernel module.


 

Refer.

 

How to Write Your Own Linux Kernel Module with a Simple Example

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s