Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad check of the parameter of the SPI_IOC_RD_LSB_FIRST ioctl call #25

Closed
ostor1960 opened this issue Feb 1, 2022 · 0 comments
Closed

Comments

@ostor1960
Copy link
Contributor

ostor1960 commented Feb 1, 2022

I’am using the
linux: 5.4.0-xilinx-v2020.2
spi-tools: a3f1f68

I have an issue with using the SPI_IOC_RD_LSB_FIRST and SPI_IOC_WR_LSB_FIRST ioctl calls.
With the spi-config –l 1 command I could set this parameter in the kernel spi driver.
But –q query command returns always lsb=0.

I see an unconsistency in the usage of the parameter of these calls in the spi-tools and spidev kernel.

In the spio-tools/spiconfig.c the parameter valid values are : 0, SPI_LSB_FIRST
if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, & byte) < 0) {
perror("SPI_IOC_RD_LSB_FIRST");
exit(EXIT_FAILURE);
}
config.lsb = (byte == SPI_LSB_FIRST ? 1 : 0);

 if ((config.lsb != new_config.lsb) && (new_config.lsb != -1)) {
       byte = (new_config.lsb ? SPI_LSB_FIRST : 0);
       if (ioctl(fd, SPI_IOC_WR_LSB_FIRST, & byte) < 0) {
            perror("SPI_IOC_WR_LSB_FIRST");
            exit(EXIT_FAILURE);
       }
 }

In the spidev.c the valid parameter values are: 0, 1
case SPI_IOC_RD_LSB_FIRST:
retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
(__u8 __user *)arg);
break;

 case SPI_IOC_WR_LSB_FIRST:
       retval = get_user(tmp, (__u8 __user *)arg);
       if (retval == 0) {
            u32  save = spi->mode;

            if (tmp)
                  spi->mode |= SPI_LSB_FIRST;
            else
                  spi->mode &= ~SPI_LSB_FIRST;
            retval = spi_setup(spi);
            if (retval < 0)
                  spi->mode = save;
            else
                  dev_dbg(&spi->dev, "%csb first\n",
                             tmp ? 'l' : 'm');
       }
       break;

I read on the page https://www.kernel.org/doc/Documentation/spi/spidev
"SPI_IOC_RD_LSB_FIRST, SPI_IOC_WR_LSB_FIRST ... pass a pointer to a byte
which will return (RD) or assign (WR) the bit justification used to
transfer SPI words. Zero indicates MSB-first; other values indicate
the less common LSB-first encoding. In both cases the specified value
is right-justified in each word, so that unused (TX) or undefined (RX)
bits are in the MSBs."

I think that modifying the spi-tools SPI_IOC_RD_BITS_PER_WORD not checking against the SPI_LSB_FIRST value, but non zero, we can resolve the issue :
if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, & byte) < 0) {
perror("SPI_IOC_RD_LSB_FIRST");
exit(EXIT_FAILURE);
}
config.lsb = (byte ? 1 : 0);

@cpb- cpb- closed this as completed in bc35fc7 Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant