cdc-dlm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * cdc-dlm.c
  3. *
  4. * CDC Direct Line Modem control for MC97 modem
  5. *
  6. * Copyright (C) 2021 Sylvain Munaut
  7. * SPDX-License-Identifier: LGPL-3.0-or-later
  8. */
  9. #include <string.h>
  10. #include <no2usb/usb.h>
  11. #include <no2usb/usb_hw.h>
  12. #include <no2usb/usb_priv.h>
  13. #include <no2usb/usb_cdc_proto.h>
  14. #include "cdc-dlm.h"
  15. #include "mc97.h"
  16. #define INTF_CDC_DLM 4
  17. #define EP_CDC_DLM_NOTIF 0x83
  18. static void
  19. dlm_send_notif_ring_detect(void)
  20. {
  21. const struct usb_ctrl_req notif = {
  22. .bmRequestType = USB_REQ_READ | USB_REQ_TYPE_CLASS | USB_REQ_RCPT_INTF,
  23. .bRequest = USB_NOTIF_CDC_RING_DETECT,
  24. .wValue = 0,
  25. .wIndex = INTF_CDC_DLM,
  26. .wLength = 0,
  27. };
  28. usb_data_write(usb_ep_regs[3].in.bd[0].ptr, &notif, sizeof(struct usb_ctrl_req));
  29. usb_ep_regs[3].in.bd[0].csr = USB_BD_STATE_RDY_DATA | USB_BD_LEN(sizeof(struct usb_ctrl_req));
  30. }
  31. static int16_t g_cc = 0;
  32. static bool
  33. dlm_set_comm_feature_country_cb(struct usb_xfer *xfer)
  34. {
  35. uint16_t new_cc;
  36. if (xfer->len != sizeof(uint16_t))
  37. return USB_FND_ERROR;
  38. memcpy(&new_cc, xfer->data, sizeof(uint16_t));
  39. if (mc97_select_country(new_cc)) {
  40. g_cc = new_cc;
  41. return USB_FND_SUCCESS;
  42. }
  43. return USB_FND_ERROR;
  44. }
  45. static enum usb_fnd_resp
  46. dlm_set_comm_feature(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  47. {
  48. /* Only country selection supported */
  49. if (req->wValue != 0x02 /* COUNTRY_SETTING */)
  50. return USB_FND_ERROR;
  51. /* Setup call back */
  52. xfer->len = sizeof(uint16_t);
  53. xfer->cb_done = dlm_set_comm_feature_country_cb;
  54. return USB_FND_SUCCESS;
  55. }
  56. static enum usb_fnd_resp
  57. dlm_get_comm_feature(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  58. {
  59. /* Only country selection supported */
  60. if (req->wValue != 0x02 /* COUNTRY_SETTING */)
  61. return USB_FND_ERROR;
  62. /* Send the currently selected country code */
  63. xfer->len = sizeof(uint16_t);
  64. memcpy(xfer->data, &g_cc, sizeof(uint16_t));
  65. return USB_FND_SUCCESS;
  66. }
  67. static enum usb_fnd_resp
  68. dlm_clear_comm_feature(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  69. {
  70. /* Only country selection supported */
  71. if (req->wValue != 0x02 /* COUNTRY_SETTING */)
  72. return USB_FND_ERROR;
  73. /* Restore default */
  74. g_cc = 0;
  75. mc97_select_country(0);
  76. return USB_FND_SUCCESS;
  77. }
  78. static enum usb_fnd_resp
  79. dlm_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  80. {
  81. /* Check it's a class request to an interface */
  82. if (USB_REQ_TYPE_RCPT(req) != (USB_REQ_TYPE_CLASS | USB_REQ_RCPT_INTF))
  83. return USB_FND_CONTINUE;
  84. /* Check it's for the DLM interface */
  85. if ((req->wIndex & 0xff) != INTF_CDC_DLM)
  86. return USB_FND_CONTINUE;
  87. switch (req->wRequestAndType)
  88. {
  89. case USB_RT_CDC_SET_HOOK_STATE:
  90. switch (req->wValue) {
  91. case 0: mc97_set_hook(ON_HOOK); break;
  92. case 1: mc97_set_hook(OFF_HOOK); break;
  93. case 2: mc97_set_hook(CALLER_ID); break;
  94. default: return USB_FND_ERROR;
  95. }
  96. return USB_FND_SUCCESS;
  97. case USB_RT_CDC_SET_AUX_LINE_STATE:
  98. /* Control the relay with that */
  99. mc97_set_aux_relay(!req->wValue);
  100. return USB_FND_SUCCESS;
  101. case USB_RT_CDC_RING_AUX_JACK:
  102. /* Can't do that ... */
  103. return USB_FND_SUCCESS;
  104. /* Pulse is not supported yet (also disabled in bmCapabilities) */
  105. case USB_RT_CDC_PULSE_SETUP:
  106. case USB_RT_CDC_SEND_PULSE:
  107. case USB_RT_CDC_SET_PULSE_TIME:
  108. return USB_FND_ERROR;
  109. /* Implement SET_COMM_FEATURE for country selection ? */
  110. /* In theory not part of DLM but it's the closest to a standard
  111. * thing to support tweaking the codec params to match local specs
  112. * for a phone line */
  113. case USB_RT_CDC_SET_COMM_FEATURE:
  114. return dlm_set_comm_feature(req, xfer);
  115. case USB_RT_CDC_GET_COMM_FEATURE:
  116. return dlm_get_comm_feature(req, xfer);
  117. case USB_RT_CDC_CLEAR_COMM_FEATURE:
  118. return dlm_clear_comm_feature(req, xfer);
  119. }
  120. return USB_FND_ERROR;
  121. }
  122. static enum usb_fnd_resp
  123. dlm_set_conf(const struct usb_conf_desc *conf)
  124. {
  125. const struct usb_intf_desc *intf;
  126. intf = usb_desc_find_intf(conf, INTF_CDC_DLM, 0, NULL);
  127. usb_ep_boot(intf, EP_CDC_DLM_NOTIF, false);
  128. return USB_FND_SUCCESS;
  129. }
  130. static struct usb_fn_drv _dlm_drv = {
  131. .ctrl_req = dlm_ctrl_req,
  132. .set_conf = dlm_set_conf,
  133. };
  134. void
  135. cdc_dlm_init(void)
  136. {
  137. /* Register function driver */
  138. usb_register_function_driver(&_dlm_drv);
  139. }
  140. void
  141. cdc_dlm_poll(void)
  142. {
  143. /* Ring detection */
  144. static bool ring;
  145. if (mc97_get_ring_detect()) {
  146. if (!ring) {
  147. dlm_send_notif_ring_detect();
  148. ring = true;
  149. }
  150. } else {
  151. ring = false;
  152. }
  153. /* TODO:
  154. * - Pulse timing when pulse is implemented
  155. */
  156. }