0

I need to use SPI in my kernel module.

It looks straight forward to call spi_* functions directly, e.g.:

....
#include <linux/spi/spi.h>
....
struct spi_device  *spi;
struct spi_message msg;
....
spi_message_add_tail(&xfer, &msg);
spi_sync(spi, &msg);
....

However, how to fill up the spi_device structure (e.g. defined here)?

In other words, how to refer a particular SPI device?

Thanks for directions!

Community
  • 1
  • 1
SergeyM
  • 165
  • 11
  • Not a recommended method but you can open the spi dev from devfs in kernel space – yashC Feb 05 '19 at 14:51
  • Yes, thanks, that works already, but I hate switching between kernel space and user space. The funny thing is I see that some other drivers use _spi*_ functions, but I still cannot understand how they pass the info. – SergeyM Feb 06 '19 at 15:09
  • I tried to look up but I am unable to find another way, Can you point me to drivers that use those spi functions ? – yashC Feb 07 '19 at 13:37
  • Not exactly I understand what kind of devices you are talking about. Which of them is master and which is slave? In case you are doing a driver for the slave device, the enumeration happens based on ACPI or Device Tree. There tables defines what slaves are connected to which masters (host controllers on board). – 0andriy Feb 07 '19 at 16:46
  • @yashC , you can find many references, if you run in the kernel tree: `find drivers/ -name "*.c" -exec grep -l spi_message_add_tail {} \;` – SergeyM Feb 08 '19 at 17:52
  • @0andriy , I have two master devices, e.g. `/dev/spidev0.0` and would like to use them from a kernel module. I don't like the idea to switch to user mode when I need them. – SergeyM Feb 08 '19 at 17:57
  • @SergeyManucharian, provide a correct ACPI or Device Tree table for that. That's how it's connected. – 0andriy Feb 08 '19 at 20:25
  • https://stackoverflow.com/questions/44711842/add-spi-slave-device-in-linux-4-9-device-tree-for-raspbery-pi – 0andriy Feb 08 '19 at 20:32
  • Possible duplicate of [How does kernel code knows which spi bus is using?](https://stackoverflow.com/questions/31070439/how-does-kernel-code-knows-which-spi-bus-is-using) – 0andriy Feb 08 '19 at 20:39
  • @0andriy, I don't understand how those questions (links) are related to mine? I do have my DeviceTree, I do have the corresponding device nodes in /dev, everything perfectly works at the SPI side. I use those devices from user space. Now I want to send a SPI message from within a different kernel module (instead of a user program). So, how can I refer a particular master device to send that message from? I thought, there must be a way to do everything in kernel space. – SergeyM Feb 08 '19 at 21:29
  • You should **not** use them from user space. Read this answer: https://stackoverflow.com/questions/53634892/linux-spidev-why-it-shouldnt-be-directly-in-devicetree/ Perhaps you need to update your question to show what kind of device tree excerpt you have, etc. – 0andriy Feb 08 '19 at 21:32
  • «You should not use them from user space»։ /dev/spidev0.1 etc **are** to be used from **user** space. My question is how to use them from **kernel** space. I work with BeagleBone Black, the device tree's fragments are here for 2 SPI devices: [SPI0](https://github.com/RobertCNelson/bb.org-overlays/blob/master/src/arm/BB-SPIDEV0-00A0.dts) and [SPI1](https://github.com/RobertCNelson/bb.org-overlays/blob/master/src/arm/BB-SPIDEV1-00A0.dts). – SergeyM Feb 08 '19 at 21:46
  • The point is, you should never use `/dev/spidev*` at all. The example you shown is for providing them to **user** space for debugging (initial playing with) the device. – 0andriy Mar 03 '19 at 10:30
  • Have you already find the answer? I have a similar issue and looking for a proper way to use SPI driver from inside of my custom driver. – Mateusz Aug 05 '20 at 05:50
  • No, I haven't. I switch to user space and then back: old_fs = get_fs(); set_fs(KERNEL_DS); .... set_fs(old_fs); (I'm not sure how to write code in a comment) – SergeyM Aug 06 '20 at 13:55

0 Answers0