Introduction
This topic is about teaching how to program direct to the linux kernel. We will first explain how to prepare your pc and the basis to start working with the kernel. After we will teach how to overwrite an USB module.
Verifying if your kernel hearder is updated
First, make sure that your kernel header is updated. Open the linux terminal and type the following command.
$ sudo apt-get update$ sudo apt-get install linux-headers-$(uname -r)Compiling linux kernel
We will be using some example code as a tutorial.Setting the modules
1) On the linux terminal, create an hello.c file.$ nano hello.c 2) Then copy this code:
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static __init int hello_init(void) { printk(KERN_INFO "Hello world!\n"); return 0;} static __exit void hello_exit(void) { printk("Goodbye cruel world!\n"); } module_init(hello_init);module_exit(hello_exit); Save it with ctrl+x.
3)On the terminal, create a makefile.
$ nano makefile
4) Then copy this code:obj-m += hello.oall: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean And save it with ctrl+x.
5) Back on the terminal, on the same folder that you saved both hello.c and makefile, compile the module.
$ make Trying the example
6) On the terminal, enter as the root user.
$ sudo su
7) Load the module on the kernel with the insmod command.# insmod hello.ko8) Unload the module from the kernel with the rmmod command.
# rmmod hello9) See messages "Hello World!" and "Goodbye cruel world!" in /var/log/message file.
# tail -f /var/log/messageMaking an USB driver
We will be making an USB driver using what we learned from the example before.Setting the modules
1) On the linux terminal, create an hello.c file.$ nano usb.c 2) Then copy this code:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/mutex.h>
MODULE_LICENSE("Dual BSD/GPL");
#define VENDOR_ID 0x2341
#define PRODUCT_ID 0x0043
static const struct usb_device_id table[] = {
{ USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE(usb, table);
static int myprobe(struct usb_interface *interface, const struct usb_device_id *id) {
printk(KERN_INFO "Probe()\n");
return 0;
}
static void mydisconnect(struct usb_interface *interface){
printk(KERN_INFO "Disconnect()\n");
}
static void mydraw_down(struct usb_skel *dev){
printk(KERN_INFO "DrawDown()\n");
}
static int mysuspend(struct usb_interface *intf, pm_message_t message) {
printk(KERN_INFO "Suspend()\n");
return 0;
}
static int myresume(struct usb_interfae *intf){
printk(KERN_INFO "Resume()\n");
return 0;
}
static int mypre_reset(struct usb_interfae *intf){
printk(KERN_INFO "Pre_reset()\n");
return 0;
}
static int mypost_reset(struct usb_interfae *intf){
printk(KERN_INFO "Post_reset()\n");
return 0;
}
static struct usb_driver mydriver = {
.name = "mydriver",
.probe = myprobe,
.disconnect = mydisconnect,
.suspend = mysuspend,
.resume = myresume,
.pre_reset = mypre_reset,
.post_reset = mypost_reset,
.id_table = table,
.supports_autosuspend = 1,
};
module_usb_driver(mydriver); Save it with ctrl+x.
3)On the terminal, create a makefile.
$ nano makefile
4) Then copy this code:obj-m += usb.oall: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean And save it with ctrl+x.
5) Back on the terminal, on the same folder that you saved both hello.c and makefile, compile the module.
$ make
Warning!
Trying this will overwrite the normal USB module that the kernel uses.
Trying the USB
6) On the terminal, enter as the root user.$ sudo su
7) Load the module on the kernel with the insmod command.# insmod usb.ko8) See messages in /var/log/message file.
# tail -f /var/log/messageNow try the USB.





