Why do device drivers in Linux need to run in kernel mode

drivershardwarelinux

Say I have a device that is connected to the computer through a USB port, and I created an application to communicate with this device. In this application I used the USB driver to communicate with the device.

Then later I decided that I don't want my application to communicate directly with the USB driver, so I removed the code that communicates with the USB driver from my application and created a device driver that contains this code, and so now my application will communicate with the newly created device driver.


Now I have read that device drivers in Linux need to run in kernel mode. But why is that?

I mean when my application communicated directly with the USB driver, it was running in user mode. So why when I moved the code that communicates with the USB driver to the device driver, now the device driver needs to run in kernel mode, why can't the device driver also run in user mode?

Best Answer

The moved code could still run in userland, it just wouldn't be a driver, it would be a library or similar. I believe your so-called driver is misnamed and is in fact not a driver, just an intermediate shared library or some such.

Device drivers are able to access privileged functions and have access to stuff userland software is not. That is why they have to be in kernel mode. That's also why they are more scrutinized and more difficult to load and run, because of the trouble they could cause. Dodgy drivers are capable of deadlocking a system and can make the system unstable, with no way for the OS to shut it down.

If you have a set of code that doesn't require that privileged access, it's pointless to build it into a driver. It would be like trying to get a building permit before moving furniture, a lot of hassle for no benefit.