How can I write a kernel module that can keep track of the SCSI commands sent to a particular device? - linux

Given a SCSI device as input, I am trying to implement a kernel module that can :
Get the list of SCSI commands sent to that particular device and count the number of times that command was given.
How do I proceed to implement this?
I am a beginner at programming kernel modules, In fact I have only written a "Hello, World" program till now.

I believe there is already debugging and logging support on device driver side.
As a startpoint you should investigate the driver in the linux kernel which starts here:
https://github.com/torvalds/linux/blob/master/drivers/scsi/scsi_module.c
And for the debugging you find this one:
https://github.com/torvalds/linux/blob/master/drivers/scsi/scsi_logging.c
And it is always a good idea to read the manual first! Give it a chance here:
https://www.kernel.org/doc/Documentation/scsi/
If you can't get logging activated, you should come back and ask a more specific question. But I believe you have no need to write your own logger at all.
From http://www.theunixway.com/2013/10/ol56-enable-additional-scsi-logging-or.html
Enabling scsi logging on kernel:
sysctl -q -w dev.scsi.logging_level=<N>
or
echo <N> > /proc/dev/scsi/logging_level
N is a bit field which contains different categories of logging information. Please refer to the documentation of the actual kernel driver version you use.

Related

Access USART from kernel to send and recive

Hy i wanted to Use the USART3 from my STM32MP157-DK1 Module. i wanted to write a simple driver witch listens on the RX and writes it as Kernel Log and the TX to send an a every send. To my question whats the common whay to access the uart.
What i have tried was to add the USART in the device tree so the tty driver makes it usable. But i wanted to read and write then ofer the dev/tty* file and what i know it is not recomended to open files from Kernel Space. Can someone tell me how the common way is. An example would also be helpfull. Sorry for the question but i am new in this area.
In my case i looked it up how the TTY Driver from the System does it and i do it now like this way (drivers/tty/serial/stm32-usart.c). I also found out i could use the secend CPU and share the invormations with Interrupts.
The example code can be found here:
stm32.usart.c

Get Wi-Fi signal strength via libnl?

I want to read the Wi-Fi signal strength from a userspace program on Linux.
As /proc/net/wireless is not available on the system, I have found out that libnl should be used to acquire the information.
Compared to just reading a value in /proc, communication via libnl seems to be very complicated. I have read various documentation, but I'm still not sure exactly what to do.
Can anyone give an example on how to read the Wi-Fi signal strength via libnl?
If you already have iw library https://git.kernel.org/pub/scm/linux/kernel/git/jberg/iw.git.
You can type sudo iw dev wlan0 scan (instead of 'wlan0' interface you can type the one you need) in your terminal. There is signal line that I think you are interested in. All the work for getting this information to user is made in scan.c file of iw library. So you can just check the source code and get some tips from there.

Where could I find the oops info from kernel logs

I am a newer of driver development. I have configured my linux kernel according to the Linux Device Driver chaper 4, enabled a lot of debug configuration. When I try to test a driver written by me, the kernel issues an oops. This oops, however, immediately flushed by chunks of other debug information. So, where could I find out the oops info which occurred in a flash.
By the way, can anyone explain the meaning of debug information below?
[ 1698.129712] evbug: Event. Dev: input0, Type: 0, Code: 0, Value: 0
This type of message flushed by screen and I even can't stop them.
To avoid a lot of useless information (in your case) you have to enable only and only what you really need to debug your module. I highly recommend to disable everything you enabled back. Then case-by-case you may enable debug features.
Next, there is a nice framework in kernel called Dynamic Debug. It allows at runtime enable or disable certain debug messages (be sure you have CONFIG_DYNAMIC_DEBUG=y in the Linux kernel configuration). More detailed description is available in Documentation/dynamic-debug-howto.txt.
evbug is a module to monitor input events in the kernel. There is one of the message it can issue. It's very simple one you may check at drivers/input/evbug.c. Unfortunately, it uses printk() calls directly and you can't manipulate its output through dynamic debug.
At the end the answer to your topic question is check output of dmesg command. But be aware that the kernel buffer for output is small enough and if you have a lot of logs you may miss some of them.

How can I get edge events via GPIO on Linux without a busy-loop?

I'm working an a system with embedded Linux (Kernel 2.6.31).
It is a AT91SAM9G20 chip inside, and some of the Pins are forwarded to the outside.
Now I want to use them as GPIO Inputs.
I read the gpio.txt documentation about using the GPIOs via filesystem, and that works very well 'til here. I connected some switches to the gpio-pins and I can see the result in /sys/class/gpio/gpioX/value. But now I'd like to react on a change without busy-waiting in a loop. (i.e echo "Switch1 was pressed").
I guess I need interrupts here, but I couldn't find out how to use them without writing my own kernel driver. I'm relatively new to Linux and C (I normally program in Java), so I'd like to handle the Interrupts via sysfs too. But my problem is, that there is no "edge"-file in my GPIO directory (I guess because this is only since Kernel version 2.6.33+). Is that right? Instead of "edge" I've got a uevent file in there, which is not described in gpio.txt.
In the gpio.txt documentation there was a Standard Kernel Driver mentioned: "gpio_keys". Is it possible to use this for my problem?
I guess it would be better to work with this driver than allowing a userspace program to manipulate kernel tasks.
I found a lot of codesnippets for writing my own driver, but I wasn't even able to find out which of the 600 gpio.h files to include, and how to refer to the library (cross compiler couldn't find the gpio.h file).
Sorry for newbie questions, I hope you could give me some advices.
Thanks in advance
See this for an example on how to do that. Basically, the thing you're missing is the usage of the select or poll system calls.

Hardware recognition in user space Linux

I want to be able to inspect my Linux machine hardware in C.
How do I get the information that appear in dmesg in C ?
For example, for keyboard the message is input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/input0
I want to get in my program the label, AT Translated Set 2, and the path, /devices/platform/i8042/serio0/input/input0.
Any idea how to do so ?
amit
You can also look at the contents of files in /proc.
/proc/version, /proc/ioports, /proc/iomem, /proc/meminfo, and others have a lot of information in them.
The files under /proc/bus have additional information about the system's hardware devices.
I'd also recommend looking at the source code for usbutils and pciutils for example code to get the output from the lsusb and lspci commands respectively.
It uses the klogctl call.
Best thing is to read the source code yourself, it can be downloaded here: util-linux
You would want to use libhal, and connect to the hal daemon. If HAL isn't there, look for a mounted sysfs partition. Check out /sys/devices, or work backwards through /sys/bus.
If neither HAL nor sysfs is available, reconsider your approach or restrict your scope.

Resources