Questions tagged [linux-device-driver]

Device drivers take on a special role in the Linux kernel. They are distinct programmatically abstracted “black boxes” that make a particular piece of hardware respond to a well-defined internal programming interface; they hide completely the details of how the device works.

User activities are performed by means of a set of standardized calls that are independent of the specific driver. Mapping those calls to device-specific operations that act on real hardware is the role of the device driver. This programming interface is such that drivers can be built separately from the rest of the kernel and “plugged in” at runtime when needed. This modularity makes Linux device drivers easy to write, to the point that there are now thousands of them available.

There are a number of reasons to be interested in the writing of Linux device drivers.

  • The rate at which new hardware becomes available (and obsolete!) alone guarantees that driver writers will be busy for the foreseeable future.

  • Individuals may need to know about drivers in order to gain access to a particular device that is of interest to them.

  • Hardware vendors, by making a Linux device driver available for their products, can add the large and growing Linux user-base to their potential markets.

The open-source nature of the Linux system means that if the driver writer wishes, the source to a driver can be quickly disseminated to millions of users.

Source -

  1. Linux Device Drivers 3rd edition

  2. Linux module programming guide

4991 questions
31
votes
3 answers

How to do a TRUE rescan of PCIe bus

I have an FPGA (Like most of the people asking this question) that gets configured after my Linux kernel does the initial PCIe bus scan and enumeration. As you can guess, the FPGA implements a PCIe endpoint. I would Like to have the PCIe core…
whh4000
  • 905
  • 1
  • 12
  • 30
30
votes
2 answers

What is the use of __iomem in linux while writing device drivers?

I have seen that __iomem is used to store the return type of ioremap(), but I have used u32 in ARM architecture for it and it works well. So what difference does __iomem make here? And in which circumstances should I use it exactly?
29
votes
2 answers

What does request_mem_region() actually do and when it is needed?

I'm studying on writing embedded linux driver, and decided to fire a few GPIOs to make sure I understand the book (LDD3, chap9.4.1) correctly. I am able to control the correct GPIO pins as intended (making it high and low, I probed with a…
I'm a frog dragon
  • 7,865
  • 7
  • 46
  • 49
29
votes
4 answers

How to include C backtrace in a kernel module code?

So I am trying to find out what kernel processes are calling some functions in a block driver. I thought including backtrace() in the C library would make it easy. But I am having trouble to load the backtrace. I copied this example function to…
ndasusers
  • 1,131
  • 2
  • 10
  • 6
27
votes
1 answer

Linux driver: ioctl or sysfs?

I'm writing a driver to control some custom hardware. In the old days (i.e. 15yrs ago) I was doing this with ioctls, but am now digging into sysfs as a possible alternative. As I understand it, ioctls aren't totally deprecated, but sysfs is…
ColH
  • 453
  • 5
  • 10
27
votes
3 answers

Hard time in understanding MODULE_DEVICE_TABLE(usb, id_table) usage

I have a hard time understanding the exact usage of MODULE_DEVICE_TABLE(usb, id_table) AFAIK this will generate the map files that will be used later by modprobe whenever a new device is inserted, it will match it against those map files and load…
silentnights
  • 813
  • 3
  • 11
  • 21
26
votes
2 answers

nvidia-smi process hangs and can't be killed with SIGKILL either

I'm on Ubuntu 14.04, CUDA toolkit 8, driver version 367.48. When I give nvidia-smi command, it just hangs indefinitely. When I login again and try to kill that nvidia-smi process, with kill -9 for example, it just isn't killed. If I give…
bio
  • 501
  • 1
  • 5
  • 16
26
votes
2 answers

Linux: how do i know the module that exports a device node?

If a have a /dev device node and its major/minor numbers how do i know the kernel module name that exported this node?
Inso Reiges
  • 1,889
  • 3
  • 17
  • 30
26
votes
2 answers

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

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…
superK
  • 3,932
  • 6
  • 30
  • 54
25
votes
2 answers

Understanding loff_t *offp for file_operations

I'm designing a device driver that simply reads and writes to a character buffer. My question is however regarding the two functions in the file_operations structure read and write. I don't truly understand what loff_t *offp really is. I know that…
Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133
25
votes
1 answer

My attributes are way too racy, what should I do?

In a linux device driver, creating sysfs attributes in probe is way too racy--specifically, it experiences a race condition with userspace. The recommended workaround is to add your attributes to various default attribute groups so they can be…
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
25
votes
6 answers

"FATAL: Module not found error" using modprobe

I have a problem with modprobe command... I compiled the hello world module and loaded it with insmod, it works fine and when I do lsmod, I can see it in the output list. But when I insert this module using modprobe I am getting a FATAL…
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
25
votes
6 answers

Does Linux kernel have main function?

I am learning Device Driver and Kernel programming.According to Jonathan Corbet book we do not have main() function in device drivers. #include #include static int my_init(void) { return 0; } static void…
someone
  • 1,638
  • 3
  • 21
  • 36
23
votes
1 answer

Linux Kernel Modules: When to use try_module_get / module_put

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…
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
23
votes
1 answer

Explain list_for_each_entry and list_for_each_entry_safe

Can anyone explain the working of list_for_each_entry and ...entry_safe loop in linux. It is like list_for_each_entry(type *cursor, struct list_head *list, member) list_for_each_entry_safe(type *cursor, type *next, struct list_head…
goodies
  • 521
  • 2
  • 6
  • 14