xref: /linux/drivers/spi/spi-ch341.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1*8846739fSJohannes Thumshirn // SPDX-License-Identifier: GPL-2.0
2*8846739fSJohannes Thumshirn //
3*8846739fSJohannes Thumshirn // QiHeng Electronics ch341a USB-to-SPI adapter driver
4*8846739fSJohannes Thumshirn //
5*8846739fSJohannes Thumshirn // Copyright (C) 2024 Johannes Thumshirn <jth@kernel.org>
6*8846739fSJohannes Thumshirn //
7*8846739fSJohannes Thumshirn // Based on ch341a_spi.c from the flashrom project.
8*8846739fSJohannes Thumshirn 
9*8846739fSJohannes Thumshirn #include <linux/module.h>
10*8846739fSJohannes Thumshirn #include <linux/usb.h>
11*8846739fSJohannes Thumshirn #include <linux/spi/spi.h>
12*8846739fSJohannes Thumshirn 
13*8846739fSJohannes Thumshirn #define CH341_PACKET_LENGTH 32
14*8846739fSJohannes Thumshirn #define CH341_DEFAULT_TIMEOUT 1000
15*8846739fSJohannes Thumshirn 
16*8846739fSJohannes Thumshirn #define CH341A_CMD_UIO_STREAM 0xab
17*8846739fSJohannes Thumshirn 
18*8846739fSJohannes Thumshirn #define CH341A_CMD_UIO_STM_END 0x20
19*8846739fSJohannes Thumshirn #define CH341A_CMD_UIO_STM_DIR 0x40
20*8846739fSJohannes Thumshirn #define CH341A_CMD_UIO_STM_OUT 0x80
21*8846739fSJohannes Thumshirn 
22*8846739fSJohannes Thumshirn #define CH341A_CMD_I2C_STREAM 0xaa
23*8846739fSJohannes Thumshirn #define CH341A_CMD_I2C_STM_SET 0x60
24*8846739fSJohannes Thumshirn #define CH341A_CMD_I2C_STM_END 0x00
25*8846739fSJohannes Thumshirn 
26*8846739fSJohannes Thumshirn #define CH341A_CMD_SPI_STREAM 0xa8
27*8846739fSJohannes Thumshirn 
28*8846739fSJohannes Thumshirn #define CH341A_STM_I2C_100K 0x01
29*8846739fSJohannes Thumshirn 
30*8846739fSJohannes Thumshirn struct ch341_spi_dev {
31*8846739fSJohannes Thumshirn 	struct spi_controller *ctrl;
32*8846739fSJohannes Thumshirn 	struct usb_device *udev;
33*8846739fSJohannes Thumshirn 	unsigned int write_pipe;
34*8846739fSJohannes Thumshirn 	unsigned int read_pipe;
35*8846739fSJohannes Thumshirn 	int rx_len;
36*8846739fSJohannes Thumshirn 	void *rx_buf;
37*8846739fSJohannes Thumshirn 	u8 *tx_buf;
38*8846739fSJohannes Thumshirn 	struct urb *rx_urb;
39*8846739fSJohannes Thumshirn 	struct spi_device *spidev;
40*8846739fSJohannes Thumshirn };
41*8846739fSJohannes Thumshirn 
ch341_set_cs(struct spi_device * spi,bool is_high)42*8846739fSJohannes Thumshirn static void ch341_set_cs(struct spi_device *spi, bool is_high)
43*8846739fSJohannes Thumshirn {
44*8846739fSJohannes Thumshirn 	struct ch341_spi_dev *ch341 =
45*8846739fSJohannes Thumshirn 		spi_controller_get_devdata(spi->controller);
46*8846739fSJohannes Thumshirn 	int err;
47*8846739fSJohannes Thumshirn 
48*8846739fSJohannes Thumshirn 	memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
49*8846739fSJohannes Thumshirn 	ch341->tx_buf[0] = CH341A_CMD_UIO_STREAM;
50*8846739fSJohannes Thumshirn 	ch341->tx_buf[1] = CH341A_CMD_UIO_STM_OUT | (is_high ? 0x36 : 0x37);
51*8846739fSJohannes Thumshirn 
52*8846739fSJohannes Thumshirn 	if (is_high) {
53*8846739fSJohannes Thumshirn 		ch341->tx_buf[2] = CH341A_CMD_UIO_STM_DIR | 0x3f;
54*8846739fSJohannes Thumshirn 		ch341->tx_buf[3] = CH341A_CMD_UIO_STM_END;
55*8846739fSJohannes Thumshirn 	} else {
56*8846739fSJohannes Thumshirn 		ch341->tx_buf[2] = CH341A_CMD_UIO_STM_END;
57*8846739fSJohannes Thumshirn 	}
58*8846739fSJohannes Thumshirn 
59*8846739fSJohannes Thumshirn 	err = usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf,
60*8846739fSJohannes Thumshirn 			   (is_high ? 4 : 3), NULL, CH341_DEFAULT_TIMEOUT);
61*8846739fSJohannes Thumshirn 	if (err)
62*8846739fSJohannes Thumshirn 		dev_err(&spi->dev,
63*8846739fSJohannes Thumshirn 			"error sending USB message for setting CS (%d)\n", err);
64*8846739fSJohannes Thumshirn }
65*8846739fSJohannes Thumshirn 
ch341_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * trans)66*8846739fSJohannes Thumshirn static int ch341_transfer_one(struct spi_controller *host,
67*8846739fSJohannes Thumshirn 			      struct spi_device *spi,
68*8846739fSJohannes Thumshirn 			      struct spi_transfer *trans)
69*8846739fSJohannes Thumshirn {
70*8846739fSJohannes Thumshirn 	struct ch341_spi_dev *ch341 =
71*8846739fSJohannes Thumshirn 		spi_controller_get_devdata(spi->controller);
72*8846739fSJohannes Thumshirn 	int len;
73*8846739fSJohannes Thumshirn 	int ret;
74*8846739fSJohannes Thumshirn 
75*8846739fSJohannes Thumshirn 	len = min(CH341_PACKET_LENGTH, trans->len + 1);
76*8846739fSJohannes Thumshirn 
77*8846739fSJohannes Thumshirn 	memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
78*8846739fSJohannes Thumshirn 
79*8846739fSJohannes Thumshirn 	ch341->tx_buf[0] = CH341A_CMD_SPI_STREAM;
80*8846739fSJohannes Thumshirn 
81*8846739fSJohannes Thumshirn 	memcpy(ch341->tx_buf + 1, trans->tx_buf, len);
82*8846739fSJohannes Thumshirn 
83*8846739fSJohannes Thumshirn 	ret = usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, len,
84*8846739fSJohannes Thumshirn 			   NULL, CH341_DEFAULT_TIMEOUT);
85*8846739fSJohannes Thumshirn 	if (ret)
86*8846739fSJohannes Thumshirn 		return ret;
87*8846739fSJohannes Thumshirn 
88*8846739fSJohannes Thumshirn 	return usb_bulk_msg(ch341->udev, ch341->read_pipe, trans->rx_buf,
89*8846739fSJohannes Thumshirn 			    len - 1, NULL, CH341_DEFAULT_TIMEOUT);
90*8846739fSJohannes Thumshirn }
91*8846739fSJohannes Thumshirn 
ch341_recv(struct urb * urb)92*8846739fSJohannes Thumshirn static void ch341_recv(struct urb *urb)
93*8846739fSJohannes Thumshirn {
94*8846739fSJohannes Thumshirn 	struct ch341_spi_dev *ch341 = urb->context;
95*8846739fSJohannes Thumshirn 	struct usb_device *udev = ch341->udev;
96*8846739fSJohannes Thumshirn 
97*8846739fSJohannes Thumshirn 	switch (urb->status) {
98*8846739fSJohannes Thumshirn 	case 0:
99*8846739fSJohannes Thumshirn 		/* success */
100*8846739fSJohannes Thumshirn 		break;
101*8846739fSJohannes Thumshirn 	case -ENOENT:
102*8846739fSJohannes Thumshirn 	case -ECONNRESET:
103*8846739fSJohannes Thumshirn 	case -EPIPE:
104*8846739fSJohannes Thumshirn 	case -ESHUTDOWN:
105*8846739fSJohannes Thumshirn 		dev_dbg(&udev->dev, "rx urb terminated with status: %d\n",
106*8846739fSJohannes Thumshirn 			urb->status);
107*8846739fSJohannes Thumshirn 		return;
108*8846739fSJohannes Thumshirn 	default:
109*8846739fSJohannes Thumshirn 		dev_dbg(&udev->dev, "rx urb error: %d\n", urb->status);
110*8846739fSJohannes Thumshirn 		break;
111*8846739fSJohannes Thumshirn 	}
112*8846739fSJohannes Thumshirn }
113*8846739fSJohannes Thumshirn 
ch341_config_stream(struct ch341_spi_dev * ch341)114*8846739fSJohannes Thumshirn static int ch341_config_stream(struct ch341_spi_dev *ch341)
115*8846739fSJohannes Thumshirn {
116*8846739fSJohannes Thumshirn 	memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
117*8846739fSJohannes Thumshirn 	ch341->tx_buf[0] = CH341A_CMD_I2C_STREAM;
118*8846739fSJohannes Thumshirn 	ch341->tx_buf[1] = CH341A_CMD_I2C_STM_SET | CH341A_STM_I2C_100K;
119*8846739fSJohannes Thumshirn 	ch341->tx_buf[2] = CH341A_CMD_I2C_STM_END;
120*8846739fSJohannes Thumshirn 
121*8846739fSJohannes Thumshirn 	return usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, 3,
122*8846739fSJohannes Thumshirn 			    NULL, CH341_DEFAULT_TIMEOUT);
123*8846739fSJohannes Thumshirn }
124*8846739fSJohannes Thumshirn 
ch341_enable_pins(struct ch341_spi_dev * ch341,bool enable)125*8846739fSJohannes Thumshirn static int ch341_enable_pins(struct ch341_spi_dev *ch341, bool enable)
126*8846739fSJohannes Thumshirn {
127*8846739fSJohannes Thumshirn 	memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH);
128*8846739fSJohannes Thumshirn 	ch341->tx_buf[0] = CH341A_CMD_UIO_STREAM;
129*8846739fSJohannes Thumshirn 	ch341->tx_buf[1] = CH341A_CMD_UIO_STM_OUT | 0x37;
130*8846739fSJohannes Thumshirn 	ch341->tx_buf[2] = CH341A_CMD_UIO_STM_DIR | (enable ? 0x3f : 0x00);
131*8846739fSJohannes Thumshirn 	ch341->tx_buf[3] = CH341A_CMD_UIO_STM_END;
132*8846739fSJohannes Thumshirn 
133*8846739fSJohannes Thumshirn 	return usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, 4,
134*8846739fSJohannes Thumshirn 			    NULL, CH341_DEFAULT_TIMEOUT);
135*8846739fSJohannes Thumshirn }
136*8846739fSJohannes Thumshirn 
137*8846739fSJohannes Thumshirn static struct spi_board_info chip = {
138*8846739fSJohannes Thumshirn 	.modalias = "spi-ch341a",
139*8846739fSJohannes Thumshirn };
140*8846739fSJohannes Thumshirn 
ch341_probe(struct usb_interface * intf,const struct usb_device_id * id)141*8846739fSJohannes Thumshirn static int ch341_probe(struct usb_interface *intf,
142*8846739fSJohannes Thumshirn 		       const struct usb_device_id *id)
143*8846739fSJohannes Thumshirn {
144*8846739fSJohannes Thumshirn 	struct usb_device *udev = interface_to_usbdev(intf);
145*8846739fSJohannes Thumshirn 	struct usb_endpoint_descriptor *in, *out;
146*8846739fSJohannes Thumshirn 	struct ch341_spi_dev *ch341;
147*8846739fSJohannes Thumshirn 	struct spi_controller *ctrl;
148*8846739fSJohannes Thumshirn 	int ret;
149*8846739fSJohannes Thumshirn 
150*8846739fSJohannes Thumshirn 	ret = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL,
151*8846739fSJohannes Thumshirn 					NULL);
152*8846739fSJohannes Thumshirn 	if (ret)
153*8846739fSJohannes Thumshirn 		return ret;
154*8846739fSJohannes Thumshirn 
155*8846739fSJohannes Thumshirn 	ctrl = devm_spi_alloc_master(&udev->dev, sizeof(struct ch341_spi_dev));
156*8846739fSJohannes Thumshirn 	if (!ctrl)
157*8846739fSJohannes Thumshirn 		return -ENOMEM;
158*8846739fSJohannes Thumshirn 
159*8846739fSJohannes Thumshirn 	ch341 = spi_controller_get_devdata(ctrl);
160*8846739fSJohannes Thumshirn 	ch341->ctrl = ctrl;
161*8846739fSJohannes Thumshirn 	ch341->udev = udev;
162*8846739fSJohannes Thumshirn 	ch341->write_pipe = usb_sndbulkpipe(udev, usb_endpoint_num(out));
163*8846739fSJohannes Thumshirn 	ch341->read_pipe = usb_rcvbulkpipe(udev, usb_endpoint_num(in));
164*8846739fSJohannes Thumshirn 
165*8846739fSJohannes Thumshirn 	ch341->rx_len = usb_endpoint_maxp(in);
166*8846739fSJohannes Thumshirn 	ch341->rx_buf = devm_kzalloc(&udev->dev, ch341->rx_len, GFP_KERNEL);
167*8846739fSJohannes Thumshirn 	if (!ch341->rx_buf)
168*8846739fSJohannes Thumshirn 		return -ENOMEM;
169*8846739fSJohannes Thumshirn 
170*8846739fSJohannes Thumshirn 	ch341->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
171*8846739fSJohannes Thumshirn 	if (!ch341->rx_urb)
172*8846739fSJohannes Thumshirn 		return -ENOMEM;
173*8846739fSJohannes Thumshirn 
174*8846739fSJohannes Thumshirn 	ch341->tx_buf =
175*8846739fSJohannes Thumshirn 		devm_kzalloc(&udev->dev, CH341_PACKET_LENGTH, GFP_KERNEL);
176*8846739fSJohannes Thumshirn 	if (!ch341->tx_buf)
177*8846739fSJohannes Thumshirn 		return -ENOMEM;
178*8846739fSJohannes Thumshirn 
179*8846739fSJohannes Thumshirn 	usb_fill_bulk_urb(ch341->rx_urb, udev, ch341->read_pipe, ch341->rx_buf,
180*8846739fSJohannes Thumshirn 			  ch341->rx_len, ch341_recv, ch341);
181*8846739fSJohannes Thumshirn 
182*8846739fSJohannes Thumshirn 	ret = usb_submit_urb(ch341->rx_urb, GFP_KERNEL);
183*8846739fSJohannes Thumshirn 	if (ret) {
184*8846739fSJohannes Thumshirn 		usb_free_urb(ch341->rx_urb);
185*8846739fSJohannes Thumshirn 		return -ENOMEM;
186*8846739fSJohannes Thumshirn 	}
187*8846739fSJohannes Thumshirn 
188*8846739fSJohannes Thumshirn 	ctrl->bus_num = -1;
189*8846739fSJohannes Thumshirn 	ctrl->mode_bits = SPI_CPHA;
190*8846739fSJohannes Thumshirn 	ctrl->transfer_one = ch341_transfer_one;
191*8846739fSJohannes Thumshirn 	ctrl->set_cs = ch341_set_cs;
192*8846739fSJohannes Thumshirn 	ctrl->auto_runtime_pm = false;
193*8846739fSJohannes Thumshirn 
194*8846739fSJohannes Thumshirn 	usb_set_intfdata(intf, ch341);
195*8846739fSJohannes Thumshirn 
196*8846739fSJohannes Thumshirn 	ret = ch341_config_stream(ch341);
197*8846739fSJohannes Thumshirn 	if (ret)
198*8846739fSJohannes Thumshirn 		return ret;
199*8846739fSJohannes Thumshirn 
200*8846739fSJohannes Thumshirn 	ret = ch341_enable_pins(ch341, true);
201*8846739fSJohannes Thumshirn 	if (ret)
202*8846739fSJohannes Thumshirn 		return ret;
203*8846739fSJohannes Thumshirn 
204*8846739fSJohannes Thumshirn 	ret = spi_register_controller(ctrl);
205*8846739fSJohannes Thumshirn 	if (ret)
206*8846739fSJohannes Thumshirn 		return ret;
207*8846739fSJohannes Thumshirn 
208*8846739fSJohannes Thumshirn 	ch341->spidev = spi_new_device(ctrl, &chip);
209*8846739fSJohannes Thumshirn 	if (!ch341->spidev)
210*8846739fSJohannes Thumshirn 		return -ENOMEM;
211*8846739fSJohannes Thumshirn 
212*8846739fSJohannes Thumshirn 	return 0;
213*8846739fSJohannes Thumshirn }
214*8846739fSJohannes Thumshirn 
ch341_disconnect(struct usb_interface * intf)215*8846739fSJohannes Thumshirn static void ch341_disconnect(struct usb_interface *intf)
216*8846739fSJohannes Thumshirn {
217*8846739fSJohannes Thumshirn 	struct ch341_spi_dev *ch341 = usb_get_intfdata(intf);
218*8846739fSJohannes Thumshirn 
219*8846739fSJohannes Thumshirn 	spi_unregister_device(ch341->spidev);
220*8846739fSJohannes Thumshirn 	spi_unregister_controller(ch341->ctrl);
221*8846739fSJohannes Thumshirn 	ch341_enable_pins(ch341, false);
222*8846739fSJohannes Thumshirn 	usb_free_urb(ch341->rx_urb);
223*8846739fSJohannes Thumshirn }
224*8846739fSJohannes Thumshirn 
225*8846739fSJohannes Thumshirn static const struct usb_device_id ch341_id_table[] = {
226*8846739fSJohannes Thumshirn 	{ USB_DEVICE(0x1a86, 0x5512) },
227*8846739fSJohannes Thumshirn 	{ }
228*8846739fSJohannes Thumshirn };
229*8846739fSJohannes Thumshirn MODULE_DEVICE_TABLE(usb, ch341_id_table);
230*8846739fSJohannes Thumshirn 
231*8846739fSJohannes Thumshirn static struct usb_driver ch341a_usb_driver = {
232*8846739fSJohannes Thumshirn 	.name = "spi-ch341",
233*8846739fSJohannes Thumshirn 	.probe = ch341_probe,
234*8846739fSJohannes Thumshirn 	.disconnect = ch341_disconnect,
235*8846739fSJohannes Thumshirn 	.id_table = ch341_id_table,
236*8846739fSJohannes Thumshirn };
237*8846739fSJohannes Thumshirn module_usb_driver(ch341a_usb_driver);
238*8846739fSJohannes Thumshirn 
239*8846739fSJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <jth@kernel.org>");
240*8846739fSJohannes Thumshirn MODULE_DESCRIPTION("QiHeng Electronics ch341 USB2SPI");
241*8846739fSJohannes Thumshirn MODULE_LICENSE("GPL v2");
242