The error message "Bad address" comes from the error code EFAULT
, which happens when you pass an address to the kernel which is not a valid virtual address in your process's virtual address space. The address to your tr
structure is clearly valid, so the problem must be with one of its members.
According to the definition of struct spi_ioc_transfer
, the .tx_buf
and .rx_buf
members must be pointers to userspace buffers, or null. You're setting .tx_buf
to the integer 254, which is not a valid userspace pointer, so that's where the bad address is coming from.
I'm not familiar with this IOCTL, so my best guess is that you need to bass the data in binary. One way to do that would be this:
struct spi_ioc_transfer tr = {
.len = sizeof(msg), // Length of rx and tx buffers
...
.tx_buf = (u64)&msg, // Pointer to tx buffer
...
};
If you need to send it as ASCII instead, then you should use a function such as snprintf(3)
to convert the integer to an ASCII string, and then point the TX buffer at that string and set the length accordingly.