2

I have received a driver (PCAN driver for PCI card, using rtdm), that creates /dev/pcan0 and /dev/pcan1 is compiled as a netdev driver.

There are many facilities that come with this driver, but they are all targeted at a user-level program reading CAN messages. What I need however is to read these messages from a kernel module. The PCAN driver doesn't export any variables/functions which means it doesn't provide a kernel-level API for me to use.

I looked briefly at the code and reading from the /dev device and writing to it don't use copy_from_user or copy_to_user. Therefore, I thought it should be safe for me to open the /dev/pcan0 from my kernel module and read from it.

Now my question is, how do I open/read from a /dev device from a kernel module?

P.S. I want to read from the CAN bus from an RTAI real-time thread, do you think that would cause a problem (for example every read passing through linux kernel and thus breaking real-time conditions?)

Shahbaz
  • 46,337
  • 19
  • 116
  • 182

2 Answers2

3

You can use syscalls directly from kernel space: sys_open(), sys_read(), sys_close(). There's Linuxjournal article about that.

P/S: copy_from_user() works perfectly with kernel-space addresses.

Dan Kruchinin
  • 2,945
  • 1
  • 17
  • 21
  • Thanks for the `copy_from_user` tip. – Shahbaz Nov 18 '11 at 13:35
  • I knew that opening a file in kernel space is not a good idea, but I didn't think doing so with /dev/ devices was as bad – Shahbaz Nov 18 '11 at 13:36
  • I mean, after all, /dev open/read/write operations basically boil down to a function call to the driver. But thanks, I will try to come up with a different solution – Shahbaz Nov 18 '11 at 13:37
  • 1
    @Shahbaz r/w any files from kernel space isn't a good idea, but some people do that if they have no choice. Typically, if I have an access to the source code of the module I try to provide API functions for in-kernel operations as well as r/w handlers for user-space. – Dan Kruchinin Nov 18 '11 at 14:13
  • Alright thanks. I found out that I in fact could compiler the driver with RTAI and use (not tested yet) rtdm (real-time driver model) to access the driver from kernel space. So, no files anymore. – Shahbaz Nov 18 '11 at 15:17
0

Given that I was using RTDM, there were two options:

  • Using RTDM direct functions, such rt_dev_open, rt_dev_read etc
    • This is not implemented in the current version of pcan driver
  • Using RTDM ioctl
    • This was the solution and it worked
Shahbaz
  • 46,337
  • 19
  • 116
  • 182