spi-ch341.c 5.9 KB

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