Skip to contentSkip to conversation

spi: add ch341a usb2spi driver

PATCH Superseded Johannes Thumshirn 2 participants 5 messages Jul 5, 2024 – Jul 8, 2024 SPI

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: add ch341a usb2spi driver

Add a driver for the QiHeng Electronics ch341a USB-to-SPI adapter.

This driver is loosly based on the ch341a module from the flashrom project.

---
 drivers/spi/Kconfig     |   6 +
 drivers/spi/Makefile    |   1 +
 drivers/spi/spi-ch341.c | 236 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 243 insertions(+)
 create mode 100644 drivers/spi/spi-ch341.c
drivers/spi/Kconfig +6 -0
index a2c99ff33e0a..79b3c20d7537 100644
@@ -1198,6 +1198,12 @@ config SPI_AMD
 	help
 	  Enables SPI controller driver for AMD SoC.
 
+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
 #
drivers/spi/Makefile +1 -0
index e694254dec04..9de506d1d135 100644
@@ -154,6 +154,7 @@ obj-$(CONFIG_SPI_XTENSA_XTFPGA)		+= spi-xtensa-xtfpga.o
 obj-$(CONFIG_SPI_ZYNQ_QSPI)		+= spi-zynq-qspi.o
 obj-$(CONFIG_SPI_ZYNQMP_GQSPI)		+= spi-zynqmp-gqspi.o
 obj-$(CONFIG_SPI_AMD)			+= spi-amd.o
+obj-$(CONFIG_SPI_CH341)			+= spi-ch341.o
 
 # SPI slave protocol handlers
 obj-$(CONFIG_SPI_SLAVE_TIME)		+= spi-slave-time.o
drivers/spi/spi-ch341.c new file +236 -0

236 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines · Johannes Thumshirn
drivers/spi/Kconfig +0 -0
@@ -1198,6 +1198,12 @@ config SPI_AMD
 	help
 	  Enables SPI controller driver for AMD SoC.

drivers/spi/Kconfig config SPI_AMD · line 1209

Quoted 10 lines
drivers/spi/Kconfig +6 -0
 
+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).

drivers/spi/Kconfig line 5

Quoted 7 lines

+++ b/drivers/spi/spi-ch341.c

drivers/spi/Kconfig +5 -0
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * QiHeng Electronics ch341a USB-to-SPI adapter driver
+ *
+ * Copyright (C) 2024 Johannes Thumshirn <[email protected]>

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

Quoted 8 lines
drivers/spi/Kconfig +8 -0
+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);

drivers/spi/Kconfig line 23

Quoted 10 lines
drivers/spi/Kconfig +10 -0
+
+	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?

drivers/spi/Kconfig line 32

Quoted 9 lines
drivers/spi/Kconfig +9 -0
+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?

#2 lore raw
Quoted 4 lines · Mark Brown
Quoted 4 lines
drivers/spi/Kconfig +4 -0
+config SPI_CH341
+	tristate "CH341 USB2SPI adapter"
+	depends on SPI_MASTER && USB
+	help
Quoted 8 lines

drivers/spi/Kconfig config SPI_AMD · line 1209

Quoted 5 lines
drivers/spi/Kconfig +2 -0
+	  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.

Quoted 10 lines

drivers/spi/spi-ch341.c line 5

Quoted 8 lines

+++ b/drivers/spi/spi-ch341.c

drivers/spi/spi-ch341.c new file +5 -0
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * QiHeng Electronics ch341a USB-to-SPI adapter driver
+ *
+ * Copyright (C) 2024 Johannes Thumshirn <[email protected]>

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.

Quoted 13 lines
Quoted 13 lines
drivers/spi/spi-ch341.c new file +12 -0
+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;
Quoted 8 lines

drivers/spi/spi-ch341.c line 59

Quoted 6 lines
drivers/spi/spi-ch341.c new file +6 -0
+	} 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.

Quoted 12 lines

drivers/spi/spi-ch341.c line 118

Quoted 10 lines
drivers/spi/spi-ch341.c new file +9 -0
+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.

maintainer
#3 lore raw
Quoted 5 lines · Johannes Thumshirn
Quoted 1 line · Mark Brown

No validation of speed?

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

Why would reading things from the device be relevant to validating what we are writing?

#4 lore raw
Quoted 9 lines · Mark Brown
Quoted 5 lines · Johannes Thumshirn
Quoted 1 line · Mark Brown

No validation of speed?

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

Why would reading things from the device be relevant to validating what we are writing?

Then I don't really understand your comment.

Why would I need to validate a constant? But indeed it doesn't make any sense at all to pass in the speed parameter.

Will fix in v2.