linux-device-driver

In my read function myread it is continuously printing the read data when i do cat /driver/mydriver. I need to print only once how to do that

空扰寡人 提交于 2019-12-08 02:20:18
问题 I wrote h into driver by doing echo: echo -n h /dev/mydriver When I do cat /dev/mydriver, myread function is printing h continuously. I wanted to print once. How to do that. static char m; static ssize_t myread(struct file *f, char __user *buf, size_t len, loff_t *off) { printk(KERN_INFO "Read()\n"); if (copy_to_user(buf, &m, 1) != 0) return -EFAULT; else return 1; } static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off) { printk(KERN_INFO "Write()\n"); if

Why does my hrtimer callback return too early after forwarding it?

房东的猫 提交于 2019-12-07 23:05:29
I want to use a hrtimer to control two hardware gpio pins to do some bus signalling. I set up a hrtimer in a kernel module like this #include <linux/slab.h> #include <linux/delay.h> #include <linux/ktime.h> #include <linux/hrtimer.h> #define PIN_A_HIGH_TO_A_LOW_US 48 /* microseconds */ #define PIN_A_LOW_TO_B_LOW_US 24 /* microseconds */ static struct kt_data { struct hrtimer timer; ktime_t period; } *data; typedef enum { eIdle = 0, eSetPinALow, eSetPinBLow, } teControlState; static enum hrtimer_restart TimerCallback(struct hrtimer *var); static void StopTimer(void); static teControlState cycle

SystemTap script to profile latency of functions

。_饼干妹妹 提交于 2019-12-07 22:32:40
问题 My goal is to profile the execution time of each function in a kernel module. Using the sample scripts I saw online, I came up with the following script to fulfill my need. But occasionally I get negative values for calculated latencies. Although, they happen rarely but I guess that indicates something is wrong with my script. Can anyone help me with that please? probe module(@1).function(@2).call { begin = gettimeofday_ns() } probe module(@1).function(@2).return { if (begin>0) stats <<<

Finding processor id in which process is running [through command/interface similar to top]

风流意气都作罢 提交于 2019-12-07 18:09:21
问题 Is there any way i can get the CPU which CPU the process is running? 回答1: You can use /proc/[id]/stat file and read the 39th value (seperated by spaces). This tells which cpu id the process of [pid] is currently running on: cat /proc/[id]/stat | awk '{print $39}' For more info: man proc. 回答2: If you wanna get it in code, it is the variable current points to. 回答3: You can use "ps" command as: You can use "ps" command as: ps -p <pid> -o pid,comm,psr Ex: ps -p 1 -o pid,comm,psr PID COMMAND PSR 1

Doubts regarding container_of macro in linux

主宰稳场 提交于 2019-12-07 15:50:24
问题 Why do we use container_of macro ? container_of(pointer, container_type, container_field); It is said in the LDD that "This macro takes a pointer to a field named container_field , within a structure of type container_type , and returns a pointer to the containing structure". My questions are: If we want a pointer to the structure (ie container_type) we can directly assign, right? Then why the pointer of one of its field in taken to assign an address to the whole structure? Can anybody please

Get man pages for driver functions

旧时模样 提交于 2019-12-07 15:44:41
问题 I am new to linux device drivers. I would like to know if there are man pages for the driver functions like register_chrdev. If yes, then how do I get them? Is there a package that I can download using apt-get? 回答1: The Linux kernel pages in the man program reside in section 9, which is (un)fortunately a non-standard section. See this. Popular Linux distributions like Ubuntu don't distribute the man pages that fall under this section through their software channels (?). They're still

Creating a debug target in Linux 2.6 driver module makefile

你说的曾经没有我的故事 提交于 2019-12-07 11:22:11
问题 I'm trying to be able to execute "make debug" at the command line and it will build my driver module with the -DDEBUG_OUTPUT define, which will cause certain sections of code to be compiled in. In 2.4 kernel makefiles, this is pretty easy. I just create a debug: target and included "-DDEBUG_OUTPUT" in the cc compilation command arguments for that target. Easy. Unfortunately (for me), 2.6 completely changed how modules are compiled, and I can ONLY seem to find the trivial "all" and "clean"

How do you get a struct device for a Linux character device

我与影子孤独终老i 提交于 2019-12-07 11:12:48
问题 I have a Linux kernel module that implements a character device driver. I've read through Linux Device Drivers and followed several tutorials. At this point, I have a simple module that provides open , release , and write file operations. I'm trying to use the Generic DMA Layer to create a streaming DMA mapping. I'm confused by the following excerpt from LDD: Many of the functions below require a struct device . This structure is the low-level representation of a device within the Linux

how to context switch in a kernel module thread?

♀尐吖头ヾ 提交于 2019-12-07 08:12:28
I'm wondering if there is a way for a kernel module thread to switch its current mm. Kernel threads usually use the last mm that was active before it was scheduled. Is it possible to switch to a particular mm in a kernel thread? I'm looking for a linux equivalent to KeStackAttachProcess in windows. Context_switch() and switch_mm() are not exported so I'm wondering what I can use to switch context in my kernel module thread. There is no such function available currently to switch context. However, the same thing can be achieved in a different way. The best way i have found to do anything in the

What is dev_id parameter in request_irq?

北战南征 提交于 2019-12-07 07:47:51
问题 In the function declaration int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), unsigned long irqflags, const char *devname, void *dev_id); Is dev_id an 'in' parameter or an 'out' parameter ? Where do we get this number from ? 回答1: Dev_id is an input argument and must be globally unique. Normally the address of the device data structure is used as the Dev_id . It has value NULL if the interrupt line is NOT shared. It holds relevance only when the interrupt