Re: [PATCH] spi: add ch341a usb2spi driver

From: Johannes Thumshirn
Date: Mon Jul 08 2024 - 04:04:56 EST


On 05/07/2024 19:41, Mark Brown wrote:
+config SPI_CH341
+ tristate "CH341 USB2SPI adapter"
+ depends on SPI_MASTER && USB
+ help
+ Enables the SPI controller on the CH341a USB to serial chip
+
#
# Add new SPI master controllers in alphabetical order above this line
#

Like the comment says please keep these files sorted (I appreciate
there's some things been missed).

Oops. Fixed, sorry.


+++ b/drivers/spi/spi-ch341.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * QiHeng Electronics ch341a USB-to-SPI adapter driver
+ *
+ * Copyright (C) 2024 Johannes Thumshirn <jth@xxxxxxxxxx>

Please make the entire comment block a C++ one so things look clearer.

I've never seen that in kernel code, but sure changed it.


+static void ch341_set_cs(struct spi_device *spi, bool is_high)
+{
+ struct ch341_spi_dev *ch341 =
+ spi_controller_get_devdata(spi->controller);
+
+ memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
+ ch341->tx_buf[0] = CH341A_CMD_UIO_STREAM;
+ ch341->tx_buf[1] = CH341A_CMD_UIO_STM_OUT | (is_high ? 0x36 : 0x37);
+
+ if (is_high) {
+ ch341->tx_buf[2] = CH341A_CMD_UIO_STM_DIR | 0x3f;
+ ch341->tx_buf[3] = CH341A_CMD_UIO_STM_END;
+ } else {
+ ch341->tx_buf[2] = CH341A_CMD_UIO_STM_END;
+ }
+
+ (void)usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf,
+ (is_high ? 4 : 3), NULL, CH341_DEFAULT_TIMEOUT);

The cast to void here is very suspicious, what's it doing?

Explicitly ignoring errors from usb_bulk_msg() as ->set_cs() can't really handle error returns.

I've changed it to an error print.



+static int ch341_config_stream(struct ch341_spi_dev *ch341, int speed)
+{
+ memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
+ ch341->tx_buf[0] = CH341A_CMD_I2C_STREAM;
+ ch341->tx_buf[1] = CH341A_CMD_I2C_STM_SET | (speed & 0x7);
+ ch341->tx_buf[2] = CH341A_CMD_I2C_STM_END;
+
+ return usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, 3,
+ NULL, CH341_DEFAULT_TIMEOUT);

No validation of speed?

TBH I haven't found a command that reads the current settings of the device.

Attachment: OpenPGP_0x0393969D2D760850.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature