1 // SPDX-License-Identifier: GPL-2.0
3 // QiHeng Electronics ch341a USB-to-SPI adapter driver
5 // Copyright (C) 2024 Johannes Thumshirn <jth@kernel.org>
7 // Based on ch341a_spi.c from the flashrom project.
9 #include <linux/module.h>
10 #include <linux/usb.h>
11 #include <linux/spi/spi.h>
13 #define CH341_PACKET_LENGTH 32
14 #define CH341_DEFAULT_TIMEOUT 1000
16 #define CH341A_CMD_UIO_STREAM 0xab
18 #define CH341A_CMD_UIO_STM_END 0x20
19 #define CH341A_CMD_UIO_STM_DIR 0x40
20 #define CH341A_CMD_UIO_STM_OUT 0x80
22 #define CH341A_CMD_I2C_STREAM 0xaa
23 #define CH341A_CMD_I2C_STM_SET 0x60
24 #define CH341A_CMD_I2C_STM_END 0x00
26 #define CH341A_CMD_SPI_STREAM 0xa8
28 #define CH341A_STM_I2C_100K 0x01
30 struct ch341_spi_dev
{
31 struct spi_controller
*ctrl
;
32 struct usb_device
*udev
;
33 unsigned int write_pipe
;
34 unsigned int read_pipe
;
39 struct spi_device
*spidev
;
42 static void ch341_set_cs(struct spi_device
*spi
, bool is_high
)
44 struct ch341_spi_dev
*ch341
=
45 spi_controller_get_devdata(spi
->controller
);
48 memset(ch341
->tx_buf
, 0, CH341_PACKET_LENGTH
);
49 ch341
->tx_buf
[0] = CH341A_CMD_UIO_STREAM
;
50 ch341
->tx_buf
[1] = CH341A_CMD_UIO_STM_OUT
| (is_high
? 0x36 : 0x37);
53 ch341
->tx_buf
[2] = CH341A_CMD_UIO_STM_DIR
| 0x3f;
54 ch341
->tx_buf
[3] = CH341A_CMD_UIO_STM_END
;
56 ch341
->tx_buf
[2] = CH341A_CMD_UIO_STM_END
;
59 err
= usb_bulk_msg(ch341
->udev
, ch341
->write_pipe
, ch341
->tx_buf
,
60 (is_high
? 4 : 3), NULL
, CH341_DEFAULT_TIMEOUT
);
63 "error sending USB message for setting CS (%d)\n", err
);
66 static int ch341_transfer_one(struct spi_controller
*host
,
67 struct spi_device
*spi
,
68 struct spi_transfer
*trans
)
70 struct ch341_spi_dev
*ch341
=
71 spi_controller_get_devdata(spi
->controller
);
75 len
= min(CH341_PACKET_LENGTH
, trans
->len
+ 1);
77 memset(ch341
->tx_buf
, 0, CH341_PACKET_LENGTH
);
79 ch341
->tx_buf
[0] = CH341A_CMD_SPI_STREAM
;
81 memcpy(ch341
->tx_buf
+ 1, trans
->tx_buf
, len
);
83 ret
= usb_bulk_msg(ch341
->udev
, ch341
->write_pipe
, ch341
->tx_buf
, len
,
84 NULL
, CH341_DEFAULT_TIMEOUT
);
88 return usb_bulk_msg(ch341
->udev
, ch341
->read_pipe
, trans
->rx_buf
,
89 len
- 1, NULL
, CH341_DEFAULT_TIMEOUT
);
92 static void ch341_recv(struct urb
*urb
)
94 struct ch341_spi_dev
*ch341
= urb
->context
;
95 struct usb_device
*udev
= ch341
->udev
;
97 switch (urb
->status
) {
105 dev_dbg(&udev
->dev
, "rx urb terminated with status: %d\n",
109 dev_dbg(&udev
->dev
, "rx urb error: %d\n", urb
->status
);
114 static int ch341_config_stream(struct ch341_spi_dev
*ch341
)
116 memset(ch341
->tx_buf
, 0, CH341_PACKET_LENGTH
);
117 ch341
->tx_buf
[0] = CH341A_CMD_I2C_STREAM
;
118 ch341
->tx_buf
[1] = CH341A_CMD_I2C_STM_SET
| CH341A_STM_I2C_100K
;
119 ch341
->tx_buf
[2] = CH341A_CMD_I2C_STM_END
;
121 return usb_bulk_msg(ch341
->udev
, ch341
->write_pipe
, ch341
->tx_buf
, 3,
122 NULL
, CH341_DEFAULT_TIMEOUT
);
125 static int ch341_enable_pins(struct ch341_spi_dev
*ch341
, bool enable
)
127 memset(ch341
->tx_buf
, 0, CH341_PACKET_LENGTH
);
128 ch341
->tx_buf
[0] = CH341A_CMD_UIO_STREAM
;
129 ch341
->tx_buf
[1] = CH341A_CMD_UIO_STM_OUT
| 0x37;
130 ch341
->tx_buf
[2] = CH341A_CMD_UIO_STM_DIR
| (enable
? 0x3f : 0x00);
131 ch341
->tx_buf
[3] = CH341A_CMD_UIO_STM_END
;
133 return usb_bulk_msg(ch341
->udev
, ch341
->write_pipe
, ch341
->tx_buf
, 4,
134 NULL
, CH341_DEFAULT_TIMEOUT
);
137 static struct spi_board_info chip
= {
138 .modalias
= "spi-ch341a",
141 static int ch341_probe(struct usb_interface
*intf
,
142 const struct usb_device_id
*id
)
144 struct usb_device
*udev
= interface_to_usbdev(intf
);
145 struct usb_endpoint_descriptor
*in
, *out
;
146 struct ch341_spi_dev
*ch341
;
147 struct spi_controller
*ctrl
;
150 ret
= usb_find_common_endpoints(intf
->cur_altsetting
, &in
, &out
, NULL
,
155 ctrl
= devm_spi_alloc_host(&udev
->dev
, sizeof(struct ch341_spi_dev
));
159 ch341
= spi_controller_get_devdata(ctrl
);
162 ch341
->write_pipe
= usb_sndbulkpipe(udev
, usb_endpoint_num(out
));
163 ch341
->read_pipe
= usb_rcvbulkpipe(udev
, usb_endpoint_num(in
));
165 ch341
->rx_len
= usb_endpoint_maxp(in
);
166 ch341
->rx_buf
= devm_kzalloc(&udev
->dev
, ch341
->rx_len
, GFP_KERNEL
);
170 ch341
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
175 devm_kzalloc(&udev
->dev
, CH341_PACKET_LENGTH
, GFP_KERNEL
);
179 usb_fill_bulk_urb(ch341
->rx_urb
, udev
, ch341
->read_pipe
, ch341
->rx_buf
,
180 ch341
->rx_len
, ch341_recv
, ch341
);
182 ret
= usb_submit_urb(ch341
->rx_urb
, GFP_KERNEL
);
184 usb_free_urb(ch341
->rx_urb
);
189 ctrl
->mode_bits
= SPI_CPHA
;
190 ctrl
->transfer_one
= ch341_transfer_one
;
191 ctrl
->set_cs
= ch341_set_cs
;
192 ctrl
->auto_runtime_pm
= false;
194 usb_set_intfdata(intf
, ch341
);
196 ret
= ch341_config_stream(ch341
);
200 ret
= ch341_enable_pins(ch341
, true);
204 ret
= spi_register_controller(ctrl
);
208 ch341
->spidev
= spi_new_device(ctrl
, &chip
);
215 static void ch341_disconnect(struct usb_interface
*intf
)
217 struct ch341_spi_dev
*ch341
= usb_get_intfdata(intf
);
219 spi_unregister_device(ch341
->spidev
);
220 spi_unregister_controller(ch341
->ctrl
);
221 ch341_enable_pins(ch341
, false);
222 usb_free_urb(ch341
->rx_urb
);
225 static const struct usb_device_id ch341_id_table
[] = {
226 { USB_DEVICE(0x1a86, 0x5512) },
229 MODULE_DEVICE_TABLE(usb
, ch341_id_table
);
231 static struct usb_driver ch341a_usb_driver
= {
233 .probe
= ch341_probe
,
234 .disconnect
= ch341_disconnect
,
235 .id_table
= ch341_id_table
,
237 module_usb_driver(ch341a_usb_driver
);
239 MODULE_AUTHOR("Johannes Thumshirn <jth@kernel.org>");
240 MODULE_DESCRIPTION("QiHeng Electronics ch341 USB2SPI");
241 MODULE_LICENSE("GPL v2");