FAQ Database Discussion Community

Is a spinlock necessary in this Linux device driver code?

Is the following Linux device driver code safe, or do I need to protect access to interrupt_flag with a spinlock? static DECLARE_WAIT_QUEUE_HEAD(wq_head); static int interrupt_flag = 0; static ssize_t my_write(struct file* filp, const char* __user buffer, size_t length, loff_t* offset) { interrupt_flag = 0; wait_event_interruptible(wq_head, interrupt_flag != 0); } static...

What is wmb() in linux driver

In one of the PCI DMA driver i have seen wmb() many times. What actually does wmb() function do? Somewhere its mentioned as "holy water that doesn't hurts". Whats the purpose of that function?

What's the difference between pci_enable_device and pcim_enable_device?

This book's PCI chapter explain about: int pci_enable_device(struct pci_dev *dev); however there's also: int pcim_enable_device (struct pci_dev * pdev); But besides stating it's a "Managed pci_enable_device", it has no explanation. So what's the difference between these two? What does it mean, that it's "managed"? Which one I should use? ...

Calling complete() on automatic variable of type struct completion defined in “waiting” thread

I try to understand use of completion in a piece of code. Basically, one kernel thread creates automatic variable struct completion which is, I assume, allocated on the thread's stack. Then it pushes pointer of the completion struct to another thread (using fifo) and waits for completion. struct completion done;...

linux kernel driver debug log

I'm writing a Linux kernel driver for a device that has quite a bit going on and a bunch of interaction from user-space. In order to debug and troubleshoot these interactions I'm storing them in a data array in the driver and need to export this data to user-space via...

Device driver without open method

I was wondering that is it mandatory to have a open release method for device driver's. The only job that open does is allocation of structures and putting them into file->priv_data, so that other methods can access. So if I have all static allocations and do not care about things...

Is there any way to see on linux the USB devices on realtime?

My question is simple, is there a way to see in a terminal what usb devices do I have connected to my computer in real time? I already know lsusb, but it just show me the devices on the moment I ask it, I would like to be able to...

How to get lid state using linux kernel module?

I can read the status of my laptop lid by reading /proc/acpi/button/lid/LID0/state file. Now I want to read it from kernel module. I found the source file drivers/acpi/button.c in kernel source. But still I didn't understand how to use it. It exporting acpi_lid_notifier_register, acpi_lid_notifier_unregiste and acpi_lid_open functions. How to write...

Modprobe mutiple instances of a kernel module to prevent race conditions (device tree)

I am writing a kernel module for an embedded Linux system. The module works fine and is automatically loaded by using the compatible entry in the device tree. It creates a device in /dev/, which i can access using ioctl massages. The module manages several discrete memory regions. Since the...

How to generate modules.dep file?

I tried to build a linux module(Hello World)I created a directory(test) created the hello.c file and the Makefile the same directory.After compiling the Makefile,I didn't got any modules.dep file. Is this an issue?If yes,how do I generate modules.dep file?

What fpu_xrstor_checking does? extra commentary?

I am doing my own version of do_fork() (for many purposes). So, basically, I am copying the process from one place to another, possibly to another machine. Copying the code and pasting it here would be tough. But this explanation should good enough, I believe. My code works most of...

How to install section 9 of man command in debian

As a newbie of Linux Driver Developer, I often need to find a kernel API's usage. In this post's answer, amrith92 said there exit section 9 of man command. After testing on my Debian, however, It seems no section 9 on in my system. How could I install section 9...

How do I generate loadable modules during Linux kernel build?

I seem to successfully build a kernel image, but I can not generate all the modules I expect. I expect more modules since I see them enabled in the gconfig window. Here is a copy of my make session. Seems like make goes into the devices directories. I can not...

Why do we need to call poll_wait in poll?

In LDD3, i saw such codes static unsigned int scull_p_poll(struct file *filp, poll_table *wait) { struct scull_pipe *dev = filp->private_data; unsigned int mask = 0; /* * The buffer is circular; it is considered full * if "wp" is right behind "rp" and empty if the * two are equal....

How to define platform_data in a Linux 3.8 device tree structure (DTS) file

I'm trying to get the at86rf230 kernel driver running on a BeagleBone Black to communicate with my radio. I have confirmed that I am able to interact with the device using some userspace SPI code. Here's the fragment of the DTS file I'm working with: [email protected] { target = <&am33xx_pinmux>;...

Android-Kernel: Is there any way to dump ftrace to a file when kernel panic occurs

I write all my module's logs to ftrace., and could not get my log prior to kernel panic occurs. Is there any way/trick to dump the entire /d/tracing/trace into a file when kernel panic occurs? enabled all ftrace related configs and tried with, "echo -n 0x1 > /proc/sys/kernel/ftrace_dump_on_oops" But didn't...

What is a major node and a minor node?

What is a major node and minor node? I was reading this article on files in the /dev directory and came across the description for /dev/null. According to the article, it says /dev/null is basiclly something you can write to and it will be lost. So I can do something...

How are some Linux device drivers automatically loaded/unloaded?

With regards to Linux device drivers, my understanding (obtained from reading this excellent DIY article) is that there are essentially six events/parts of a device driver "lifecycle": Load - the compiled driver is loaded as a Linux kernel module via insmod Open - the device is opened/readied for usage Read/Write...

how to connect omap to oled display

as you guys see the title , I would like to connect Micro OLED display to dart4460(omap4460). I am not sure but I guess I can use parallel LCD port in dart4460 to connect OLED display. here are what I want to connect displays. 1) http://1103zzeykfq2w1en1xnkh94yk.wpengine.netdna-cdn.com/wp-content/uploads/2013/07/SXGA-Datasheet-w-DF12-rev-13.pdf 2) http://www.olightek.com/products_detail/&productId=32.html : below...

Is it true that Tagged initialization allows the reordering of structure members?

I am currently reading "Linux Device Driver" third ed. Extract: The scull device driver implements only the most important device methods. Its file_operations structure is initialized as follows: struct file_operations scull_fops = { .owner = THIS_MODULE, .llseek = scull_llseek, .read = scull_read, .write = scull_write, .ioctl = scull_ioctl, .open =...

ret_from_syscall source code and when it is called

In the below call trace we see function called ret_from_syscall. Which function is this ? When it will called during system call ? Where is the corresponding code for this ? May 7 16:40:34.322086 warn TCU-0 kernel: [cf83ddc0] [00009751] 0x9751 (unreliable) May 7 16:40:34.322086 warn TCU-0 kernel: [cf83ddd0] [c00469ac] do_syslog+0x198/0x424...

Driver mmap operation Page table creation

I am trying to understand mmap operation in a driver. I know why we are doing mmap. I am confused with the following 2 implementations of mmap: int my_mmap(struct file *filp, struct vm_area_struct *vma) { vma->vm_ops = &mmap_vm_ops; vma->vm_flags |= VM_RESERVED; /* assign the file private data to the vm...

ioremap returns NULL

I'm trying to create a customised UART driver for Raspberry PI. I try to ioremap the MMIO. But ioremap returns always NULL. #define UART_REG_BASE_ADDR 0x7E20100 pUart = ioremap(UART_REG_BASE_ADDR, 1024); if (pUart==NULL) { printk("Couldn't remap UART MMIO\n"); return -EIO; } dmesg dumps following: [ 2906.579848] CPU: 0 PID: 4983 Comm: insmod...

losing data when using an exported symbol with parameters in linux module

I'm trying to export a function so that it can be used from an other linux module, the exported function is successfully detected by the second module and can be called, but it looks like I'm using data with the passed parameters. All the examples I found on the internet...

Do Access points use softMAC or hardMAC?

I am trying to understand the working of wireless in linux. I started with wpa_supplicant, hostapd applications with the help of their documentation and source code.Understood the flow and basic functionalities of : wpa_supplicant,nl80211(driver interface) libnl library(socket communication between user space and kernel using netlink protocol) cfg80211(kernel interface used for...

Is it possible to use dma_set_mask() to tell kernel not to use memory under 4G

My pcie device has a bug that it cant dma to addresses below 4G, What mask should i use with dma_set_mask to tell this to the kernel?

in Linux, when reading an I2C-based RTC, who handles counter carry-over conditions?

When reading multiple bytes from an I2C-based RTC, it seems that it is possible that while reading each byte, one of the values may increment. For instance, if the time is: 2014-12-31 23:59:59 as you're reading this value, the time may roll-over to 2015-01-01 00:00:00 so you may actually read:...

Moxa RealTTY module compilation error on Linux kernel 3.16

I used MOXA devices on Debian 7.0 with kernel 3.2. I was able to successfully compiled Moxa RealTTY driver for this kernel, but after I upgraded to Debian 8.0 with kernel 3.16 I can't compile it. I asked MOXA for newest driver, but so far no answer. Latest driver was...

how to get a clock from a device tree node

I have the following issue: I want to define the clock a CPU should use during frequency transitions in the device tree rather than in the clock driver code (in this way it will be more generic). I want to define the "transition-clock" property in the device tree, something like:...

How to write to data register in C, without touching previous writes?

Iam a beginner in C and have this scenario: I can successfully write to a gpio port with data register 0x103 and direction register 0x95. If I want to write to another pin I have to "overwrite" the previously pin. As if I first write 00010000 and then want to...

How to share same header files between kernel modules and userspace applications.

I want to implement a simple module in which an ioctl() method is used. In the kernel module, I use kernel macros, such as _IO(), _IOWR(), etc., to define my own ioctl sub-commands. In facts, I don't care the actual values of these definitions for that I will always use...

Link of compressed pages in zsmalloc

I am trying to understand zsmalloc allocator used in zram and zswap but I am stuck in code of init_zspage in zsmalloc.c link = (struct link_free *)kmap_atomic(page) + off / sizeof(*link); Here link is not initialized. The problem is what will be the value of sizeof(*link) if it not initializes....

beaglebone black insmod modprobe g_mass_storage did NOT work

How to insert module g_mass_storage.ko? It is located at /lib/modules/3.8.6/kernel/drivers/usb/gadget/ with -rw-r--r-- permissions. # insmod g_mass_storage.ko Output: Error: could not load module g_mass_storage.ko: No such file or directory # modprobe g_mass_storage.ko Output: FATAL: Module g_mass_storage.ko not found. How do we fix this? # uname -a Output: Linux beaglebone 3.8.6 #1...

disabling CONFIG_NET_DMA

I'm facing the exact problem mentioned in the following thread :- Using DMA API in linux kernel but channel is never available I'm not able to see any DMA channel in /sys/class/dma. The solution for this has been suggested as to disable the CONFIG_NET_DMA. But can you guys tell me...

Why udelay and ndelay is not accurate in linux kernel?

I make a function like this trace_printk("111111"); udelay(4000); trace_printk("222222"); and the log shows it's 4.01 ms , it'OK But when i call like this trace_printk("111111"); ndelay(10000); ndelay(10000); ndelay(10000); ndelay(10000); .... ....//totally 400 ndelay calls trace_printk("222222"); the log will shows 4.7 ms. It's not acceptable. Why the error of ndelay is...

Read from device file

I have a kernel module myModule with major number 100 and minor number 120. Here is what I did: make all sudo insmod ./myModule.ko mknod ~/myModule c 100 120 When I check the log with cat /proc/syslog then it shows the printk() things from the init function. But I have...

Linux: Compiling userspace code for handling driver's ioctl

I'm new to kernel compilation and am trying to cross-compile a userspace program for an existing kernel driver for an ARM board. Basically, I'm trying to access the ioctl facilities of the driver. I'm using arm-linux-gnueabihf-gcc (4.9.2). I can compile the kernel successfully. The userspace code looks like following getefuse.c...

How to offload NAPI poll function to workqueue

I'm working with linux 3.3, Ethernet driver for smsc911x. and I want to move the NAPI poll function to workqueue. My questions are : 1. How do I pass the NAPI poll function arguments to the work_struct? 2. How do I get the NAPI poll function arguments back from the...

System.map file and /proc/kallsyms

I just want to know the difference between these two.. I am using linux 3.16 generic kernel . I Know that both are the kernel symbol table..When i did a Kompare on both the files , both are having the same content? So what is the need of having both?

workqueue: __WORK_INITIALIZER compiler warning

Just to learn, I try to compile an old device driver example on Ubuntu 14, and I get a warning. I guess this warning is triggered because some inconsistency between the type returned from __WORK_INITIALIZER and the work_struct structure. What I don't understand is how that can happen. :) This...

Which usb driver is called in order to transfer data?

I would like to change the behaviour of one of my USB flash drive by editing its driver, but i can't find which driver is called. I searched and i found that the drivers/usb/storage/transport.c is responsible for some exchanges beetween the host and the device (configuration), but i want to...

Measure memory usage of a certain Linux driver module

I want to check whether the kernel driver module which I just finished has a memory leak problem. But I don't know how to do it in kernel space. Dynamic memory allocation is very hard to handle I think. Anyone has the experience in debugging such memory problem in kernel...

Linux kernel module to monitor a particular process

I would like to write a kernel module in Linux that can monitor all the memory accesses made by a particular process(that I specify by name in the kernel module). I would also like to keep track of all the signals generated by the process and log all memory accesses...

hrtimer runs even thogh it is not started

I'm performing hrtimer_init(hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); And after a while ( 30, maybe 60 seconds ), i perform: hrtimer_start(hr_timer, ktime_set(0, timeout), HRTIMER_MODE_REL); And immediatelly after, in less than 1 second, i perform ktime_to_timeval(hrtimer_expires_remaining(hr_timer)); The output timeval struct contains the time since hrtimer_init, and not from the start. What is akward, is...

why is u8 u16 u32 u64 used instead of unsigned int in kernel programming

I see u8 u16 u32 u64 data types being used in kernel code. And I am wondering why is there need to use u8 or u16 or u32 or u64 and not unsigned int?

Accessing pins of Linux hardware inside device driver

I read this excellent DIY article on writing Linux device drivers, but am still not seeing the "forest through the trees" on a major item here. Ultimately the end user software (in user space) needs to communicate with the hardware (that the device drivers are driving/wrapping/adapting). Hardware is driven by...

racecondition between put_page and pci_unmap_sg

I am working on a device driver that includes dma. If I just use get_user_pages_fast and put_page everything is working. As soon as I add calls to pci_(un)map_sg it seems to become racy. After the test program finishes I sometimes get a BUG, however i do not always get the...

Is there a way to find out the exact source tree for specific Raspberry Pi kernel

I'm trying to compile a kernel module on my raspberry and I keep getting 'disagrees about version of symbol module_layout' when trying to load the compiled module. I would like to avoid compiling the whole kernel so what I'm doing is using the img produced by the Raspberry pi foundation...

I want to sleep while holding a mutex

I need to write to and read from a Ethernet chip's FIFO and for that I want to use DMA controller. So I modified an existing driver and made the process to sleep using wait_event_interruptible after triggering DMA transfer and made it woken up by ISR using wake_up_interruptible. My problem...

Linux: how to disable Intel DPST (Display Power Saving Technology) - i915 driver

I have an Android tablet (Teclast X98) with an extremely annoying feature that it constantly keeps adjusting the brightness of the display all by itself. Initially I thought maybe the tablet has a faulty display unit - the pumping of the brightness is that annoying. After a lot of investigation...

Get struct device pointer for this module?

Is there a way to get pointer to "struct device" for current module in kernel driver ? Thank you, Ran...

When to Update ALSA Audio Driver Buffer Pointer

I am writing an USB Audio Playback driver using ALSA APIs. For that I was trying to understand existing audio drivers in Linux kernel. But I get confused on when to update the kernel audio buffer pointer. We know kernel puts new audio data in a ring buffer and our...

Connect to wifi network after install driver manually linux

how to connect to WiFi network after manual driver install I have successfully installed the driver as so sudo cp -R . /usr/src/rtl8812AU_8821AU_linux sudo dkms add -m rtl8812AU_8821AU_linux -v 1.0 sudo dkms build -m rtl8812AU_8821AU_linux -v 1.0 sudo dkms install -m rtl8812AU_8821AU_linux -v 1.0 Now how do i connect to...

Pool of Memory in Kernel driver for Multiple processes

Suppose we want to maintain a pool of memory in a device driver or module. How can that pool be created and be available to multiple processes lets say 4 processes, accessing this driver/module. Assume 1 MB of memory in the pool. When I was reading LDD I came across...

miscdevice node permission needs to be generic

I want to be able to give access to all users to my driver. I want to be able to give a 660 device node permission to my driver node. The driver node is created as apart of the miscdevice registration through the framework. But I always get the /dev/mydev...

Choosing memory allocation api in linux kernel for allocating huge memory frequently

I have to allocate huge amount of memory frequently in my kernel driver. Which memory allocation api is better to use and why ?