Created
April 22, 2020 04:21
-
-
Save smalinux/3d6a5b000a32b97250b2d0c86b90711b to your computer and use it in GitHub Desktop.
Hello, world USB driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[root@hp kernel_mod]# make i | |
cp sma_usb.ko /lib/modules/5.6.3/ | |
sudo modprobe sma_usb | |
[root@hp kernel_mod]# make c | |
dmesg -c | |
[ 4529.628355] Hello Guy | |
[ 4529.628392] usbcore: registered new interface driver my first usb driver | |
[root@hp kernel_mod]# make c | |
dmesg -c | |
[ 4540.367850] usb 3-3: USB disconnect, device number 11 | |
[root@hp kernel_mod]# make c | |
dmesg -c | |
[ 4545.358673] usb 3-3: new full-speed USB device number 12 using xhci_hcd | |
[ 4545.489752] usb 3-3: New USB device found, idVendor=045e, idProduct=0800, bcdDevice= 9.44 | |
[ 4545.489759] usb 3-3: New USB device strings: Mfr=1, Product=2, SerialNumber=0 | |
[ 4545.489762] usb 3-3: Product: Microsoft® Nano Transceiver v2.0 | |
[ 4545.489765] usb 3-3: Manufacturer: Microsoft | |
[ 4545.495030] input: Microsoft Microsoft® Nano Transceiver v2.0 as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0/0003:045E:0800.000A/input/input44 | |
[ 4545.547377] hid-generic 0003:045E:0800.000A: input,hidraw0: USB HID v1.11 Keyboard [Microsoft Microsoft® Nano Transceiver v2.0] on usb-0000:00:14.0-3/input0 | |
[ 4545.553893] input: Microsoft Microsoft® Nano Transceiver v2.0 Mouse as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.1/0003:045E:0800.000B/input/input45 | |
[ 4545.554372] input: Microsoft Microsoft® Nano Transceiver v2.0 Consumer Control as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.1/0003:045E:0800.000B/input/input46 | |
[ 4545.554594] hid-generic 0003:045E:0800.000B: input,hidraw1: USB HID v1.11 Mouse [Microsoft Microsoft® Nano Transceiver v2.0] on usb-0000:00:14.0-3/input1 | |
[ 4545.561376] input: Microsoft Microsoft® Nano Transceiver v2.0 Consumer Control as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.2/0003:045E:0800.000C/input/input47 | |
[ 4545.613190] input: Microsoft Microsoft® Nano Transceiver v2.0 System Control as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.2/0003:045E:0800.000C/input/input49 | |
[ 4545.614471] hid-generic 0003:045E:0800.000C: input,hiddev96,hidraw2: USB HID v1.11 Device [Microsoft Microsoft® Nano Transceiver v2.0] on usb-0000:00:14.0-3/input2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set module name from the list: | |
program = sma_usb | |
# 'The list' | |
#---------- | |
obj-m += sma_usb.o | |
# obj-m += sma_chardev.o | |
# obj-m += sma_skeleton.o | |
# obj-m += hello.o | |
# obj-m += hello-5.o | |
# obj-m += simple_keylogger.o | |
# obj-m += task_struct.o | |
# obj-m += current.o | |
# obj-m += scull.o | |
# obj-m += chardev.o | |
# obj-m += my_printk.o | |
# obj-m += sma_task_struct_id_2.o | |
# obj-m += sma_list.o | |
# obj-m += kdatasize.o | |
# obj-m += sma_sys.o | |
# obj-m += sma_timer.o | |
# obj-m := sma_jiffies.o | |
# obj-m := sma_list_ex2.o | |
######## Not Working ######## | |
# program = syscall_mkdir | |
# program = keylogger_1 | |
# program = smash_MBR | |
all: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules | |
gcc datasize.c -o datasize | |
# mknod smalinux_node c 10 100 || exit 0 | |
# insert | |
i: | |
# depmod -ae https://stackoverflow.com/a/34800857/5688267 | |
cp $(program).ko /lib/modules/$(shell uname -r)/ | |
sudo modprobe $(program) | |
# remove | |
r: | |
modprobe -r $(program) | |
rm /lib/modules/$(shell uname -r)/$(program).ko | |
p: | |
dmesg | |
l: | |
lsmod | grep $(program) | |
# clear dmesg | |
c: | |
dmesg -c | |
mknod: | |
echo "Make node" | |
# $ mknod /dev/devname c majorNum MinorNum | |
rmnod: | |
echo "Remove node" | |
# rm -fr /dev/$(program) | |
# Cleaning the code style | |
style: | |
/home/smalinux/Downloads/linux-5.6.3/scripts/Lindent $(program).c | |
info: | |
modinfo $(program).ko #userspace program prints .modinfo section | |
clean: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> /*included for all kernel modules*/ | |
#include <linux/kernel.h> /*included for KERN_DEBUG*/ | |
#include <linux/init.h> /*included for __init and __exit macros*/ | |
#include <linux/usb.h> | |
#include <linux/usb/input.h> | |
#include <linux/hid.h> | |
#define MY_USB_VENDOR_ID 0x045e | |
#define MY_USB_PRODUCT_ID 0x0800 | |
static struct usb_device_id my_usb_table[] = { | |
{ USB_DEVICE(MY_USB_VENDOR_ID, MY_USB_PRODUCT_ID) }, | |
{}, | |
}; | |
MODULE_DEVICE_TABLE(usb, my_usb_table); | |
static int | |
my_pdrv_probe(struct usb_interface *pdev, const struct usb_device_id *id) { | |
pr_err("Hello\n"); | |
return 0; | |
} | |
static void my_pdrv_remove(struct usb_interface *pdev) { | |
pr_err("Bye\n"); | |
} | |
static struct usb_driver mypdrv = { | |
.name = "my first usb driver", | |
.id_table = my_usb_table, | |
.probe = my_pdrv_probe, | |
.disconnect = my_pdrv_remove, | |
}; | |
// module_usb_driver(mypdrv); | |
static int __init my_pdev_init(void) | |
{ | |
pr_err("Hello Guy\n"); | |
/* Refister with kernel */ | |
usb_register(&mypdrv); | |
return 0; | |
} | |
static void __exit my_pdev_remove(void) | |
{ | |
pr_err("Bye Guy\n"); | |
/* Unregister from kernel */ | |
usb_deregister(&mypdrv); | |
} | |
module_init(my_pdev_init); | |
module_exit(my_pdev_remove); | |
MODULE_LICENSE("GPL"); | |
MODULE_AUTHOR("Sohaib Mohammed"); | |
MODULE_DESCRIPTION("first usb driver module"); | |
MODULE_VERSION("0.1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment