Now I have read that device drivers in Linux needs to run in kernel mode. But why is that? ... why can't the device driver also runs in user mode?
Drivers run in kernel mode while applications run in user mode for many reason. For example
- a driver has need of high priority to service device I/O in a predictable manner (and otherwise can risk loosing some data). For example, drivers may need to run without incurring page faults. Putting driver memory in the kernel is one easy way of accomplishing this. (In truth, this is, of course, much more complicated: usually drivers have a small amount of super high priority code along with some less critical additional code that also runs in the kernel, and the two share memory.)
- I/O instructions and device memory mapped I/O addresses are typically protected from application code, so applications are not allowed direct access to these resources. Forcing applications to use kernel/system calls allows the operating system to share devices between multiple applications, or even multiple virtual machines.
- Having applications use a kernel/system read/write interface to access devices allows device substitution and upgrading device drivers independently from the application.
Applications, by contrast, typically run in user mode, with a degree of abstraction from the underlying devices and their implementation.
I mean when my application communicated directly with the USB driver, it was running in user mode.
Yes, but the application's communication with the USB driver is done via kernel/system calls, so the kernel is involved in this. Further, the USB driver itself is typically in the kernel; just library routines to access them are in the application or application-used libraries.
If you write your own device driver, you could put all your application's functionality into the device driver and thus not require a user mode application at all. However, that is generally not a great structuring of functionality for a number of reasons. Guidelines like Separation of Concerns would be violated, for example.