How can I write to TTY from a kernel module? - linux

First post to SO, so I'll try to make the question right.
I'm making a simple Linux kernel module with the goal of echoing data back to the TTY shell from where the kernel module was loaded. The problem I having is the kernel "Ooops"-ing with the following message (caught with " watch 'dmesg | tail -50' "). The kernel module's name is Seraphim:
[ 184.222748] SELinux: initialized (dev proc, type proc), uses genfs_contexts
[ 1877.456607] seraphim: module verification failed: signature and/or required key missing - tainting kernel
[ 1877.457321] ------------------
[ 1877.457324] Seraphim started.
[ 1877.457348] BUG: unable to handle kernel NULL pointer dereference at 0000000000000218
[ 1877.457411] IP: [<ffffffffa0012030>] seraphim_entry+0x30/0x1000 [seraphim]
[ 1877.457462] PGD 115a2e067 PUD 10aca8067 PMD 0
[ 1877.457498] Oops: 0000 [#1] SMP
[ 1877.457524] Modules linked in: seraphim(OF+) rfcomm bnep nf_conntrack_netbios_ns nf_conn track_broadcast ipt_MASQUERADE ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge stp llce btable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_ma etc.
The code used for writing data to the TTY terminal follows:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tty.h>
static void printString(char *string) {
struct tty_struct *tty;
tty = current->signal->tty;
if(tty != NULL) {
(tty->driver->ops->write) (tty, string, strlen(string));
}
else
printk("tty equals to zero");
}
What am I doing wrong?
I was following the tutorial at http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf but it was out of date (the kernel I am using is 3.11.10-200 on Fedora 19), so I had to rummage through 3.11.10-200 source files to find the adequate structures.

use tty = get_current_tty(); instead of tty = current->signal->tty;
that's it
you need to lock the tty before accessing it and get_current_tty does it internally
NOTE: get_current_tty is under EXPORT_SYMBOL_GPL, hence your module or code

Related

Failing to insert a custom Kernel module

I'm working on a STM32MP1 target with yocto. In general I can compile a helloworld .ko file and load it into the kernel. But when I want to compile following:
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/types.h>
static struct platform_device *pdev;
static int __init fake_eth_add(void)
{
int inst_id = 0;
pdev = platform_device_alloc("fake-eth", inst_id);
platform_device_add(pdev);
pr_info("fake-eth added");
return 0;
}
static void __exit fake_eth_put(void)
{
pr_info("fake-fake removed");
platform_device_put(pdev);
}
module_init(fake_eth_add);
module_exit(fake_eth_put);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("John Madieu <john.madieu#gmail.com>");
I get following errors:
ERROR: modpost: "platform_device_put" [/home/rdm/ethtest/eth-ins.ko] undefined!
ERROR: modpost: "platform_device_add" [/home/rdm/ethtest/eth-ins.ko] undefined!
ERROR: modpost: "platform_device_alloc" [/home/rdm/ethtest/eth-ins.ko] undefined!
I check if the platform_device_alloc Symbol/Function is defined and it does:
root#txmp-1570:/lib/modules/5.7.1/source/kernel# grep -w platform_device_alloc /proc/kallsyms
c0547cfc T platform_device_alloc
Now I have learned that the symbols must be in the Module.symvers File tagged as EXPORT_SYMBOL_GPL. The symbols were not there, so I added them to the Module.symvers file:
0x00000000 platform_device_alloc vmlinux EXPORT_SYMBOL_GPL
0x00000000 platform_device_add vmlinux EXPORT_SYMBOL_GPL
0x00000000 platform_device_put vmlinux EXPORT_SYMBOL_GPL
Now the code compiles! But when I want to load it with insmod, then I get following error:
root#txmp-1570:/home/rdm/ethtest# insmod eth-ins.ko
insmod: ERROR: could not insert module eth-ins.ko: Unknown symbol in module
And the dmseg logs:
[ 2954.658383] eth_ins: Unknown symbol platform_device_put (err -2)
[ 2954.664194] eth_ins: Unknown symbol platform_device_add (err -2)
[ 2954.672737] eth_ins: Unknown symbol platform_device_alloc (err -2)
What am I missing? Why did I have to append the Module.symvers file by myself? Did I miss something regarding setting up the enviromnent for kernel driver development?

ubuntu18.04 vscode kernel module program ERROR: identifier "KBUILD_MODNAME" is undefine

My environment: Ubuntu 18.04, kernel: linux-5.3.0-53 vscode: lastest
Command for fixing linux/module.h not find asm/xxx.h
cd /usr/src/linux-headers-5.3.0-53/include
sudo ln -s asm-generic/ asm
My c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include",
"/usr/src/linux-headers-5.3.0-53/include",
"/usr/src/linux-headers-5.3.0-53-generic/include",
"/usr/src/linux-headers-5.3.0-53/arch/x86/include",
"/usr/src/linux-headers-5.3.0-53/include/uapi",
"/usr/lib/gcc/x86_64-linux-gnu/7.5.0/include"
],
"defines": [
"__GNUC__",
"__KERNEL__"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
My hello_module.c, a simple Linux kernel module program.
//a simple linux kernel module program.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL"); //error here
static int hello_init(void){
printk(KERN_ALERT "hello world\n");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "goodbye world\n");
}
module_init(hello_init);
module_exit(hello_exit);
then ERROR:
identifier "KBUILD_MODNAME" is undefined
How to fix? Please help me..
KBUILD_MODNAME is automatically defined by the Makefile when you build your kernel module, so you will not find it anywhere in the kernel header files.
To stop vscode from complaining about undefined KBUILD_MODNAME identifier, just add a dummy definition to your c_cpp_properties.json file in the "defines" section
"defines": [
"KBUILD_MODNAME=\"hello_module\"",
"__GNUC__",
"__KERNEL__"
],
That should make vscode happy =)
The value you define for KBUILD_MODNAME does not really matter, since it is automatically defined during the make process.
I have not understood if this is your only problem, or if you also have problems to compile the module. If yes let me know and we will figure out how to solve that too!

PCI driver 'Oops: Kernel access of bad area' error

I wanted to write a simple PCI express driver for Xilinx FPGA. But I am not able to request memory region for PCI.
Question is: How to claim that I/O memory area for custom driver. I want to write 3. byte of that area using driver.
Below are the details. What am I missing ? Thanks
1-) I am getting this error:
[ 4.345350] Unable to handle kernel paging request for data at address 0x00000005
[ 4.353978] Faulting instruction address: 0x80000000002c9370
[ 4.358337] Oops: Kernel access of bad area, sig: 11 [#1]
[ 4.362426] BE SMP NR_CPUS=24 CoreNet Generic
[ 4.365477] Modules linked in: fpgapcie(O+) ucc_uart
[ 4.369139] CPU: 0 PID: 1999 Comm: udevd Tainted: G O 4.19.26+gc0c2141 #1
[ 4.375924] NIP: 80000000002c9370 LR: 80000000002c9350 CTR: c00000000053acfc
[ 4.381753] REGS: c0000001ee2bb1c0 TRAP: 0300 Tainted: G O (4.19.26+gc0c2141)
[ 4.389146] MSR: 000000008002b000 <CE,EE,FP,ME> CR: 22228242 XER: 20000000
[ 4.394982] DEAR: 0000000000000005 ESR: 0000000000800000 IRQMASK: 0
GPR00: 80000000002c9350 c0000001ee2bb440 80000000002d1f00 000000000000001a
GPR04: 0000000000000001 000000000000022d c000000000f30548 c000000001013000
GPR08: 00000001fec37000 0000000000000003 0000000000000000 0000000000000020
GPR12: 0000000028228444 c000000001013000 0000000000020000 000000013c323ac8
GPR16: 000000013c323ae0 80000000002cc000 c000000000a194b0 c0000001f0eaa1c0
GPR20: 00000000006000c0 c000000000ed9da0 0000000000000000 0000000000000100
GPR24: 000000000000001c 000000000f700000 c0000001f3034880 0000000000000000
GPR28: c0000001f337b800 00000000000000f7 c0000001f337b8a0 0000000000000000
2-) Code piece in PCI probe function:
static int pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
int ret, minor;
struct cdev *cdev;
dev_t devno;
unsigned long pci_io_addr = 0;
/* add this pci device in pci_cdev */
if ((minor = pci_cdev_add(pci_cdev, MAX_DEVICE, dev)) < 0)
goto error;
/* compute major/minor number */
devno = MKDEV(major, minor);
/* allocate struct cdev */
cdev = cdev_alloc();
/* initialise struct cde
cdev_init(cdev, &pci_ops);
cdev->owner = THIS_MODULE;
/* register cdev */
ret = cdev_add(cdev, devno, 1);
if (ret < 0) {
dev_err(&(dev->dev), "Can't register character device\n");
goto error;
}
pci_cdev[minor].cdev = cdev;
dev_info(&(dev->dev), "%s The major device number is %d (%d).\n",
"Registeration is a success", MAJOR(devno), MINOR(devno));
dev_info(&(dev->dev), "If you want to talk to the device driver,\n");
dev_info(&(dev->dev), "you'll have to create a device file. \n");
dev_info(&(dev->dev), "We suggest you use:\n");
dev_info(&(dev->dev), "mknod %s c %d %d\n", DEVICE_NAME, MAJOR(devno), MINOR(devno));
dev_info(&(dev->dev), "The device file name is important, because\n");
dev_info(&(dev->dev), "the ioctl program assumes that's the\n");
dev_info(&(dev->dev), "file you'll use.\n");
/* enable the device */
pci_enable_device(dev);
/* 'alloc' IO to talk with the card */
if (pci_request_region(dev, BAR_IO, "IO-pci") == 0) {
printk(KERN_ALERT "The memory you requested from fpgapcie is already reserved by CORE pci driver.");
}
check that BAR_IO is *really* IO region
if ((pci_resource_flags(dev, BAR_IO) & IORESOURCE_IO) != IORESOURCE_IO) {
dev_err(&(dev->dev), "BAR2 isn't an IO region\n");
cdev_del(cdev);
goto error;
}
pci_io_addr = pci_resource_start(dev,BAR_IO);
printk(KERN_INFO "PCI start adress: %02X", &pci_io_addr);
outb(pci_io_addr+3, 5);
printk(KERN_INFO "Message from PCI device to user: 5");
return 1;
error:
printk(KERN_INFO "An error occuder while probing pci");
return 0;
}
3-) lspci -v output:
0001:01:00.0 Memory controller: Xilinx Corporation Device 7021
Subsystem: Xilinx Corporation Device 0007
Flags: bus master, fast devsel, latency 0, IRQ 41
Memory at c10000000 (32-bit, non-prefetchable) [size=2K]
Capabilities: [40] Power Management version 3
Capabilities: [48] MSI: Enable- Count=1/1 Maskable- 64bit+
Capabilities: [60] Express Endpoint, MSI 00
Capabilities: [100] Device Serial Number 00-00-00-01-01-00-0a-35
Kernel driver in use: yusufpci
Kernel modules: fpgapcie
4-) full dmesg:
[ 4.285663] Module pci init
[ 4.294787] yusufpci 0001:01:00.0: Registeration is a success The major device number is 247 (0).
[ 4.302367] yusufpci 0001:01:00.0: If you want to talk to the device driver,
[ 4.308116] yusufpci 0001:01:00.0: you'll have to create a device file.
[ 4.313516] yusufpci 0001:01:00.0: We suggest you use:
[ 4.317354] yusufpci 0001:01:00.0: mknod virtual_pci c 247 0
[ 4.321713] yusufpci 0001:01:00.0: The device file name is important, because
[ 4.327553] yusufpci 0001:01:00.0: the ioctl program assumes that's the
[ 4.332866] yusufpci 0001:01:00.0: file you'll use.
[ 4.336459] The memory you requested from fpgapcie is already reserved by CORE pci driver. This is not an error.
[ 4.336463] PCI start adress: EE2BB4B0
[ 4.345350] Unable to handle kernel paging request for data at address 0x00000005
[ 4.353978] Faulting instruction address: 0x80000000002c9370
[ 4.358337] Oops: Kernel access of bad area, sig: 11 [#1]
[ 4.362426] BE SMP NR_CPUS=24 CoreNet Generic
[ 4.365477] Modules linked in: fpgapcie(O+) ucc_uart
[ 4.369139] CPU: 0 PID: 1999 Comm: udevd Tainted: G O 4.19.26+gc0c2141 #1
[ 4.375924] NIP: 80000000002c9370 LR: 80000000002c9350 CTR: c00000000053acfc
[ 4.381753] REGS: c0000001ee2bb1c0 TRAP: 0300 Tainted: G O (4.19.26+gc0c2141)
[ 4.389146] MSR: 000000008002b000 <CE,EE,FP,ME> CR: 22228242 XER: 20000000
[ 4.394982] DEAR: 0000000000000005 ESR: 0000000000800000 IRQMASK: 0
GPR00: 80000000002c9350 c0000001ee2bb440 80000000002d1f00 000000000000001a
GPR04: 0000000000000001 000000000000022d c000000000f30548 c000000001013000
GPR08: 00000001fec37000 0000000000000003 0000000000000000 0000000000000020
GPR12: 0000000028228444 c000000001013000 0000000000020000 000000013c323ac8
GPR16: 000000013c323ae0 80000000002cc000 c000000000a194b0 c0000001f0eaa1c0
GPR20: 00000000006000c0 c000000000ed9da0 0000000000000000 0000000000000100
GPR24: 000000000000001c 000000000f700000 c0000001f3034880 0000000000000000
GPR28: c0000001f337b800 00000000000000f7 c0000001f337b8a0 0000000000000000
[ 4.453632] NIP [80000000002c9370] .pci_probe+0x220/0x2b4 [fpgapcie]
[ 4.458680] LR [80000000002c9350] .pci_probe+0x200/0x2b4 [fpgapcie]
[ 4.463639] Call Trace:
[ 4.464775] [c0000001ee2bb440] [80000000002c9350] .pci_probe+0x200/0x2b4 [fpgapcie] (unreliable)
[ 4.472262] [c0000001ee2bb500] [c0000000004b77c8] .pci_device_probe+0x11c/0x1f4
[ 4.478270] [c0000001ee2bb5a0] [c000000000561ebc] .really_probe+0x26c/0x38c
[ 4.483927] [c0000001ee2bb640] [c0000000005621ac] .driver_probe_device+0x78/0x154
[ 4.490106] [c0000001ee2bb6d0] [c0000000005623d8] .__driver_attach+0x150/0x154
[ 4.496025] [c0000001ee2bb760] [c00000000055f424] .bus_for_each_dev+0x94/0xdc
[ 4.501856] [c0000001ee2bb800] [c0000000005615fc] .driver_attach+0x24/0x38
[ 4.507426] [c0000001ee2bb870] [c000000000560ec8] .bus_add_driver+0x264/0x2a4
[ 4.513258] [c0000001ee2bb910] [c000000000563384] .driver_register+0x88/0x178
[ 4.519089] [c0000001ee2bb990] [c0000000004b5a68] .__pci_register_driver+0x50/0x64
[ 4.525355] [c0000001ee2bba00] [80000000002c9564] .pci_init_module+0xc0/0x444 [fpgapcie]
[ 4.532144] [c0000001ee2bba80] [c0000000000020b4] .do_one_initcall+0x64/0x224
[ 4.537978] [c0000001ee2bbb50] [c0000000000f443c] .do_init_module+0x70/0x260
[ 4.543722] [c0000001ee2bbbf0] [c0000000000f6564] .load_module+0x1e6c/0x2400
[ 4.549467] [c0000001ee2bbd10] [c0000000000f6d28] .__se_sys_finit_module+0xcc/0x100
[ 4.555819] [c0000001ee2bbe30] [c0000000000006b0] system_call+0x60/0x6c
[ 4.561127] Instruction dump:
[ 4.562785] e86a8080 38810070 f9210070 4800041d e8410028 e9210070 3d420000 e94a8088
[ 4.569231] 39290003 5529063e e94a0000 7c0004ac <992a0005> 39200001 3d420000 992d0684
[ 4.575854] ---[ end trace 2d15cff7ba1b3255 ]---
Problem solved. But when I write the third byte of Memory Mapped area, FPGA programmed to answer with lighting its GPIO leds. I tried to write first 15 byte of MMIO but it did not work. The leds did not lighted. But the code stopped giving errors.
I also cannot read the bytes on MMIO space using readb() function. It's
giving
unrecoverable machine check error
Problem solved using this code.
pci_request_regions(dev, "fpgapcie");
pci_io_startaddr = pci_resource_start(dev,BAR_IO);
pci_io_endaddr = pci_resource_end(dev,BAR_IO);
pci_io_size = pci_resource_len(dev,BAR_IO);
printk(KERN_INFO "Region start: %lX, Region end: %lX, Size: % lX",pci_io_startaddr,pci_io_endaddr,pci_io_size);
pci_io_addr = ioremap(pci_io_startaddr, pci_io_endaddr);
printk(KERN_INFO "PCI start adress: %lX", pci_io_addr);
for(i = 0;i<15;i++) /* Write first 15 byte */
{
writeb(2, pci_io_addr+i);
printk(KERN_INFO "%lX, Message from PCI device to user: 2", pci_io_addr+i);
}
and the dmesg output:
fpgapcie: loading out-of-tree module taints kernel.
fpgapcie 0001:01:00.0: Registeration is a success The major device number is 247 (0).
fpgapcie 0001:01:00.0: If you want to talk to the device driver,
fsl-fman-port ffe488000.port fm1-gb0: renamed from eth0
fpgapcie 0001:01:00.0: you'll have to create a device file.
fpgapcie 0001:01:00.0: We suggest you use:
fpgapcie 0001:01:00.0: mknod virtual_pci c 247 0
fpgapcie 0001:01:00.0: The device file name is important, because
fpgapcie 0001:01:00.0: the ioctl program assumes that's the
fpgapcie 0001:01:00.0: file you'll use.
Region start: 210000000, Region end: 2100007FF, Size: 800
PCI start adress: 8000080088900000
8000080088900000, Message from PCI device to user: 2
8000080088900001, Message from PCI device to user: 2
8000080088900002, Message from PCI device to user: 2
8000080088900003, Message from PCI device to user: 2
8000080088900004, Message from PCI device to user: 2
8000080088900005, Message from PCI device to user: 2
8000080088900006, Message from PCI device to user: 2
8000080088900007, Message from PCI device to user: 2
8000080088900008, Message from PCI device to user: 2
8000080088900009, Message from PCI device to user: 2
800008008890000A, Message from PCI device to user: 2
800008008890000B, Message from PCI device to user: 2
800008008890000C, Message from PCI device to user: 2
800008008890000D, Message from PCI device to user: 2
800008008890000E, Message from PCI device to user: 2

Android O porting

I am trying to port android O to my device but encountering issue due to vendor partition not being created (which is mandatory for Android O), as a result the SELinux policy is not being fetched and boot process is being terminated. How can I create a vendor partition to flash the vendor image into.
my device runs on a Qualcomm msm8953 SOC.
below are the boot logs.
309800] nq-nci 5-0028: nqx_probe: probing nqxx failed, check hardware
[ 7.317273] Freeing unused kernel memory: 1196K
[ 7.321143] Freeing alternatives memory: 112K
[ 7.329257] init: init first stage started!
[ 7.332716] init: Using Android DT directory /proc/device-tree/firmware/android/
[ 7.724741] init: bool android::init::FirstStageMount::InitRequiredDevices(): partition(s) not found in /sys, waiting for their uevent(s): vendor
[ 8.878132] of_batterydata_get_best_profile: 2951034_foxda_ef501esp_3000mah_averaged_masterslave_jun6th2017 found
[ 8.894872] FG: fg_batt_profile_init: Battery SOC: 97, V: 4249242uV
[ 8.900528] of_batterydata_get_best_profile: 2951034_foxda_ef501esp_3000mah_averaged_masterslave_jun6th2017 found
[ 8.910371] SMBCHG: smbchg_config_chg_battery_type: Vfloat changed from 4400mV to 4350mV for battery-type 2951034_foxda_ef501esp_3000mah_averaged_masterslave_jun6th2017
[ 17.746065] init: Wait for partitions returned after 10009ms
[ 17.750729] init: bool android::init::FirstStageMount::InitRequiredDevices(): partition(s) not found after polling timeout: vendor
[ 17.768123] init: Failed to mount required partitions early ...
[ 17.773010] init: panic: rebooting to bootloader
[ 17.777611] init: Reboot start, reason: reboot, rebootTarget: bootloader
[ 17.784364] init: android::WriteStringToFile open failed: No such file or directory
[ 17.791947] init: Shutdown timeout: 0 ms
[ 17.795852] init: property_set("persist.vendor.crash.detect", "false") failed: __system_property_add failed
[ 17.805838] init: waitid failed: No child processes
[ 17.810437] init: vold not running, skipping vold shutdown
[ 17.916293] init: powerctl_shutdown_time_ms:138:0
[ 17.919991] init: Reboot ending, jumping to kernel
[ 17.924752] msm_thermal:msm_thermal_update_freq Freq mitigation task is not initialized
[ 17.978348] mdss_fb_release_all: try to close unopened fb 1! from pid:1 name:init
[ 17.984816] mdss_fb_release_all: try to close unopened fb 0! from pid:1 name:init
[ 17.993627] reboot: Restarting system with command 'bootloader'
[ 17.998538] Going down for restart now
[ 18.002831] qcom,qpnp-power-on qpnp-power-on-12: PMIC#SID2: configuring PON for reset
I am encountering this myself and I believe it has to do with a mix of incomplete defconfig and dm-verity in the SoC's dts. Unless you have worked it out by now.
In particular to incomplete defconfig, I had to add options like CONFIG_PINCTRL_MSM8937=y as my android device is technically a 8937. Also, in arch/arm/boot/dts/qcom/msm8937.dtsi I needed to pull the verify flag from fsmgr_flags.
I now have other issues, but I am getting closer to booting.

Why is the probe function in my kernel module not being called?

While following, among others, this tutorial (1) and reading certain chapters in the Linux Device Drivers book, I cannot get the pr_debug() statements in the probe function to show any output in dmesg.
Here's my code:
#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 VENDOR_ID 0x0930
#define DEVICE_ID 0x6545
MODULE_LICENSE("GPL");
MODULE_AUTHOR("dev");
MODULE_DESCRIPTION("eusb driver");
static struct usb_device_id eusb_table[] = {
{ USB_DEVICE(VENDOR_ID, DEVICE_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, eusb_table);
static int eusb_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
pr_debug("USB probe function called\n");
return 0;
}
static void eusb_disconnect(struct usb_interface *interface)
{
pr_debug("USB disconnect function called\n");
}
static struct usb_driver eusb_driver = {
//.owner = THIS_MODULE,
.name = "eusb",
.probe = eusb_probe,
.disconnect = eusb_disconnect,
.id_table = eusb_table
};
static int __init eusb_init(void)
{
int result = 0;
pr_debug("Hello world!\n");
result = usb_register(&eusb_driver);
if(result){
pr_debug("error %d while registering usb\n", result);}
else{pr_debug("no error while registering usb\n");}
return 0;
}
static void __exit eusb_exit(void)
{
usb_deregister(&eusb_driver);
pr_debug("Exiting module\n");
}
module_init(eusb_init);
module_exit(eusb_exit);
and Makefile:
obj-m := eusb.o
CFLAGS_eusb.o := -DDEBUG
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
Make finishes without errors, after insmod I can see the module listed in lsmod and the pr_debug() in the init and exit functions show output in dmesg.
When inserting the device however the probe function seems to not get called (or the pr_debug() statements do not show any output in dmesg).
Dmesg output:
[ 7777.521236] Hello world!
[ 7777.521264] usbcore: registered new interface driver eusb
[ 7777.521266] no error while registering usb
[ 7780.597087] usb 1-6: USB disconnect, device number 9
[ 7797.686970] usb 1-6: new high-speed USB device number 10 using xhci_hcd
[ 7797.857324] usb 1-6: New USB device found, idVendor=0930, idProduct=6545
[ 7797.857328] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 7797.857330] usb 1-6: Product: DataTraveler 2.0
[ 7797.857331] usb 1-6: Manufacturer: Kingston
[ 7797.857333] usb 1-6: SerialNumber: 08606E6D407FED10571E5067
[ 7797.858787] usb-storage 1-6:1.0: USB Mass Storage device detected
[ 7797.858902] scsi host11: usb-storage 1-6:1.0
[ 7798.931417] scsi 11:0:0:0: Direct-Access Kingston DataTraveler 2.0 PMAP PQ: 0 ANSI: 4
[ 7798.931824] sd 11:0:0:0: Attached scsi generic sg3 type 0
[ 7800.184749] sd 11:0:0:0: [sdc] 60964864 512-byte logical blocks: (31.2 GB/29.0 GiB)
[ 7800.186338] sd 11:0:0:0: [sdc] Write Protect is off
[ 7800.186343] sd 11:0:0:0: [sdc] Mode Sense: 23 00 00 00
[ 7800.187948] sd 11:0:0:0: [sdc] No Caching mode page found
[ 7800.187952] sd 11:0:0:0: [sdc] Assuming drive cache: write through
[ 7800.220477] sdc: sdc1 sdc2 sdc3
[ 7800.225068] sd 11:0:0:0: [sdc] Attached SCSI removable disk
[ 7802.798403] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 7802.799507] ISO 9660 Extensions: RRIP_1991A
I have tried with another device, tried with printk() instead of pr_debug(). I found several questions on SO with the same problem, but my code is as far as I can tell almost/completely the same as the code in the answers.
I have also tried with:
USB_INTERFACE_INFO(
USB_INTERFACE_CLASS_HID,
USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_KEYBOARD)
instead of USB_DEVICE() (the other device was a keyboard).
I noticed that some answers talk about platform_driver instead of usb_driver, but I think this is not relevant for me, as it is never mentioned in the tutorials.
Where am I going wrong?
Is your purpose just educational? Otherwise the driver is already there and it's called usb-storage. Seems even that driver enumerates your device first. Try to disable usb-storage and load your driver again. Or better use the virtual machine (KVM / Qemu) to try your drivers.
As you can see your Hello world! was printed which implies the module was loaded successfully. Often all the modules claiming to handle the device via usb_device_id* table are loaded. See 9.3.2.3. Module Loading.
Now, the probe function is called only for the driver the device was bound to. In the case where there is no priority set and two drivers are capable of binding, any of them might get the first opportunity.
I have written a blog post which might clear things further (if not, do comment here).
Further, you might want to go through these two relevant mailing list threads
https://lists.kernelnewbies.org/pipermail/kernelnewbies/2011-October/003639.html
https://marc.info/?l=linux-usb&m=162123892329464&w=2

Resources