usbh_gsm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_serial.h"
  8. #undef USB_DBG_TAG
  9. #define USB_DBG_TAG "usbh_gsm"
  10. #include "usb_log.h"
  11. struct usbh_gsm {
  12. struct usb_endpoint_descriptor *intin;
  13. struct usbh_urb intin_urb;
  14. struct usb_osal_timer *modem_timer;
  15. uint16_t modem_status;
  16. };
  17. static int usbh_gsm_attach(struct usbh_serial *serial)
  18. {
  19. struct usb_endpoint_descriptor *ep_desc;
  20. struct usbh_gsm *gsm_class = usb_osal_malloc(sizeof(struct usbh_gsm));
  21. if (!gsm_class) {
  22. USB_LOG_ERR("No memory for gsm_class\r\n");
  23. return -USB_ERR_NOMEM;
  24. }
  25. memset(gsm_class, 0, sizeof(struct usbh_gsm));
  26. serial->priv = gsm_class;
  27. for (uint8_t i = 0; i < serial->hport->config.intf[serial->intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  28. ep_desc = &serial->hport->config.intf[serial->intf].altsetting[0].ep[i].ep_desc;
  29. if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
  30. if (ep_desc->bEndpointAddress & 0x80) {
  31. USBH_EP_INIT(gsm_class->intin, ep_desc);
  32. break;
  33. } else {
  34. }
  35. }
  36. }
  37. if (!gsm_class->intin) {
  38. USB_LOG_WRN("Do not find interrupt endpoint, so disable modem status monitor\r\n");
  39. }
  40. return 0;
  41. }
  42. static void usbh_gsm_detach(struct usbh_serial *serial)
  43. {
  44. struct usbh_gsm *gsm_class;
  45. if (!serial || !serial->priv) {
  46. return;
  47. }
  48. gsm_class = (struct usbh_gsm *)serial->priv;
  49. if (gsm_class->intin) {
  50. usbh_kill_urb(&gsm_class->intin_urb);
  51. }
  52. serial->priv = NULL;
  53. usb_osal_free(gsm_class);
  54. }
  55. static int usbh_gsm_set_line_coding(struct usbh_serial *serial, struct cdc_line_coding *line_coding)
  56. {
  57. return 0;
  58. }
  59. static int usbh_gsm_set_line_state(struct usbh_serial *serial, bool dtr, bool rts)
  60. {
  61. return 0;
  62. }
  63. static const struct usbh_serial_driver gsm_driver = {
  64. .driver_name = "gsm",
  65. .ignore_rx_header = 0,
  66. .ignore_tx_header = 0,
  67. .attach = usbh_gsm_attach,
  68. .detach = usbh_gsm_detach,
  69. .set_flow_control = NULL,
  70. .set_line_coding = usbh_gsm_set_line_coding,
  71. .get_line_coding = NULL,
  72. .set_line_state = usbh_gsm_set_line_state,
  73. .get_modem_status = NULL,
  74. };
  75. static int usbh_gsm_connect(struct usbh_hubport *hport, uint8_t intf)
  76. {
  77. return usbh_serial_probe(hport, intf, &gsm_driver) ? 0 : -USB_ERR_NOMEM;
  78. }
  79. static int usbh_gsm_disconnect(struct usbh_hubport *hport, uint8_t intf)
  80. {
  81. struct usbh_serial *serial = (struct usbh_serial *)hport->config.intf[intf].priv;
  82. if (serial) {
  83. usbh_serial_remove(serial);
  84. }
  85. return 0;
  86. }
  87. const struct usbh_class_driver gsm_class_driver = {
  88. .driver_name = "gsm",
  89. .connect = usbh_gsm_connect,
  90. .disconnect = usbh_gsm_disconnect
  91. };
  92. static const uint16_t gsm_id_table[][2] = {
  93. { 0x2C7C, 0x0120 }, /* Quectel EC20 */
  94. { 0x2C7C, 0x0121 }, /* Quectel EC21 */
  95. { 0x2C7C, 0x0125 }, /* Quectel EC25 */
  96. { 0x2C7C, 0x0191 }, /* Quectel EG91 */
  97. { 0x2C7C, 0x0195 }, /* Quectel EG95 */
  98. { 0x2C7C, 0x6002 }, /* Quectel EC200/EC600/EC800/EG91x */
  99. { 0x1E0E, 0x9001 }, /* SIMCOM SIM7600 */
  100. { 0x2ECC, 0x3012 }, /* Chinamobile ML307R */
  101. { 0, 0 },
  102. };
  103. CLASS_INFO_DEFINE const struct usbh_class_info gsm_class_info = {
  104. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  105. .bInterfaceClass = 0xff,
  106. .bInterfaceSubClass = 0x00,
  107. .bInterfaceProtocol = 0x00,
  108. .id_table = gsm_id_table,
  109. .class_driver = &gsm_class_driver
  110. };