linux-device-driver

how does open works for normal file and device drivers

▼魔方 西西 提交于 2019-11-27 16:45:27
问题 Currently, I am learning Linux device drivers. And got stuck over how opening a device file works ? What I got until now... Consider the a simple code that opens a normal file.. #incldue<stdio.h> int main() { FILE fp; char buffer[20]; fp = fopen(/home/yoggi/foo.txt, "r"); fread(buffer, 5, 1, fp); } In above program, The fopen(), c-library function, is a wrapper function to the system call open() , which intern calls sys_open() or file_open() in VFS layer function. As linux supports a number

IOCTL Linux device driver [closed]

我是研究僧i 提交于 2019-11-27 16:39:23
Can anyone explain me, What is IOCTL ? What is it used for? How can I use it? Why can't I define new function that does the same work as IOCTL ? Inductiveload An ioctl , which means "input-output control" is a kind of device-specific system call. There are only a few system calls in Linux (300-400), which are not enough to express all the unique functions devices may have. So a driver can define an ioctl which allows a userspace application to send it orders. However, ioctls are not very flexible and tend to get a bit cluttered (dozens of "magic numbers" which just work... or not), and can

adding i2c client devices on x86_64

与世无争的帅哥 提交于 2019-11-27 15:24:55
On my x86_64 board, there is i2c-bus coming out of a MFD device. There are devices on to this i2c-bus. I am able to detect these devices using i2cdetect program. # i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- 4c -- -- -- 50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- I need

Can an interrupt handler be preempted by the same interrupt handler?

混江龙づ霸主 提交于 2019-11-27 15:15:50
问题 Does the CPU disable all interrupts on local CPU before calling the interrupt handler? Or does it only disable that particular interrupt line, which is being served? 回答1: x86 disables all local interrupts (except NMI of course) before jumping to the interrupt vector. Linux normally masks the specific interrupt and re-enables the rest of the interrupts (which aren't masked), unless a specific flags is passed to the interrupt handler registration. Note that while this means your interrupt

How to add a peridic timer callback in a linux kernel module

让人想犯罪 __ 提交于 2019-11-27 15:10:23
问题 I am working on a Linux kernel module that registers a callback for interrupts that come from a custom-made board and puts the received data in a queue behind a char device interface to be processed by an application. This module needs to constantly monitor and measure the interrupts and data that comes from the board even if no interrupt comes from the board, so it has another callback that triggers according to time. Current implementation uses RTC interrupt as a constant timer source. I

How remap_pfn_range remaps kernel memory to user space?

穿精又带淫゛_ 提交于 2019-11-27 14:44:07
问题 remap_pfn_range function (used in mmap call in driver) can be used to map kernel memory to user space. How is it done? Can anyone explain precise steps? Kernel Mode is a privileged mode (PM) while user space is non privileged (NPM). In PM CPU can access all memory while in NPM some memory is restricted - cannot be accessed by CPU. When remap_pfn_range is called, how is that range of memory which was restricted only to PM is now accessible to user space? Looking at remap_pfn_range code there

How do the files in '/dev' match Linux's model of a device? [closed]

亡梦爱人 提交于 2019-11-27 14:14:37
Here is my understanding in opening to a file for reading/writing. In the application layer, I can invoke the fopen() function. The fwrite() function will invoke a system call open() . After the OS receives the open() call, it will pass the command to VFS(virtual file system). VFS looks up the file name, including any directories needed and does the necessary access checks. If this is in RAM cache then no disk access is needed. If not, the VFS sends a read request to the specific file system which is probably EXT4. Then the EXT4 file system driver will determine in what disk block that

Is it possible to set CFLAGS to a linux kernel module Makefile?

我只是一个虾纸丫 提交于 2019-11-27 13:34:59
Eg: a common device module's Makefile obj-m:=jc.o default: $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules clean: $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules clean I consider if I can set CFLAGS to the file. When I change default section to $(MAKE) -O2 -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules But it didn't work. Any help? Thanks a lot. -O2 would be an option to make (or $(MAKE) , as you're using it) in what you tried. Obviously, the compiler (probably gcc ) needs this flag, not make . Kbuild understands a make variable

What is the difference between module_init and subsys_initcall while initializing the driver?

最后都变了- 提交于 2019-11-27 12:28:21
What is the difference between module_init and subsys_initcall while initializing the driver? The difference relates to timing, or more precisely, order of execution. That is, the procedure declared as subsys_initcall is guaranteed to be executed before the procedure declared as module_init . This ordering ensures that subsystem and platform drivers are initialized before device drivers try to utilize the former's functionality (e.g. a device driver registers as a subsystem device). The actual macro definition for each depends on if the kernel is configured for (loadable) modules or not. The

Linux Kernel Modules: When to use try_module_get / module_put

核能气质少年 提交于 2019-11-27 12:20:48
问题 I was reading the LKMPG ( See Section 4.1.4. Unregistering A Device ) and it wasn't clear to me when to use the try_module_get / module_put functions. Some of the LKMPG examples use them, some don't. To add to the confusion, try_module_get appears 282 times in 193 files in the 2.6.24 source, yet in Linux Device Drivers ( LDD3 ) and Essential Linux Device Drivers, they appears in not even a single code example. I thought maybe they were tied to the old register_chrdev interface ( superseded in