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