diagfwd_smux.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/termios.h>
  13. #include <linux/slab.h>
  14. #include <linux/diagchar.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <mach/usbdiag.h>
  18. #include "diagchar.h"
  19. #include "diagfwd.h"
  20. #include "diagfwd_smux.h"
  21. #include "diagfwd_hsic.h"
  22. #include "diagfwd_bridge.h"
  23. void diag_smux_event(void *priv, int event_type, const void *metadata)
  24. {
  25. unsigned char *rx_buf;
  26. int len;
  27. switch (event_type) {
  28. case SMUX_CONNECTED:
  29. pr_info("diag: SMUX_CONNECTED received\n");
  30. driver->smux_connected = 1;
  31. driver->in_busy_smux = 0;
  32. /* read data from USB MDM channel & Initiate first write */
  33. queue_work(diag_bridge[SMUX].wq,
  34. &diag_bridge[SMUX].diag_read_work);
  35. break;
  36. case SMUX_DISCONNECTED:
  37. driver->smux_connected = 0;
  38. driver->lcid = LCID_INVALID;
  39. msm_smux_close(LCID_VALID);
  40. pr_info("diag: SMUX_DISCONNECTED received\n");
  41. break;
  42. case SMUX_WRITE_DONE:
  43. pr_debug("diag: SMUX Write done\n");
  44. break;
  45. case SMUX_WRITE_FAIL:
  46. pr_info("diag: SMUX Write Failed\n");
  47. break;
  48. case SMUX_READ_FAIL:
  49. pr_info("diag: SMUX Read Failed\n");
  50. break;
  51. case SMUX_READ_DONE:
  52. len = ((struct smux_meta_read *)metadata)->len;
  53. rx_buf = ((struct smux_meta_read *)metadata)->buffer;
  54. driver->write_ptr_mdm->length = len;
  55. diag_device_write(driver->buf_in_smux, SMUX_DATA,
  56. driver->write_ptr_mdm);
  57. break;
  58. };
  59. }
  60. int diagfwd_write_complete_smux(void)
  61. {
  62. pr_debug("diag: clear in_busy_smux\n");
  63. driver->in_busy_smux = 0;
  64. return 0;
  65. }
  66. int diagfwd_read_complete_smux(void)
  67. {
  68. queue_work(diag_bridge[SMUX].wq, &diag_bridge[SMUX].diag_read_work);
  69. return 0;
  70. }
  71. int diag_get_rx_buffer(void *priv, void **pkt_priv, void **buffer, int size)
  72. {
  73. if (!driver->in_busy_smux) {
  74. *pkt_priv = (void *)0x1234;
  75. *buffer = driver->buf_in_smux;
  76. pr_debug("diag: set in_busy_smux as 1\n");
  77. driver->in_busy_smux = 1;
  78. } else {
  79. pr_debug("diag: read buffer for SMUX is BUSY\n");
  80. return -EAGAIN;
  81. }
  82. return 0;
  83. }
  84. void diag_usb_read_complete_smux_fn(struct work_struct *w)
  85. {
  86. diagfwd_read_complete_bridge(diag_bridge[SMUX].usb_read_ptr);
  87. }
  88. void diag_read_usb_smux_work_fn(struct work_struct *work)
  89. {
  90. int ret;
  91. if (driver->diag_smux_enabled) {
  92. if (driver->lcid && diag_bridge[SMUX].usb_buf_out &&
  93. (diag_bridge[SMUX].read_len > 0) &&
  94. driver->smux_connected) {
  95. ret = msm_smux_write(driver->lcid, NULL,
  96. diag_bridge[SMUX].usb_buf_out,
  97. diag_bridge[SMUX].read_len);
  98. if (ret)
  99. pr_err("diag: writing to SMUX ch, r = %d, lcid = %d\n",
  100. ret, driver->lcid);
  101. }
  102. diag_bridge[SMUX].usb_read_ptr->buf =
  103. diag_bridge[SMUX].usb_buf_out;
  104. diag_bridge[SMUX].usb_read_ptr->length = USB_MAX_OUT_BUF;
  105. diag_bridge[SMUX].usb_read_ptr->context = (void *)SMUX;
  106. usb_diag_read(diag_bridge[SMUX].ch,
  107. diag_bridge[SMUX].usb_read_ptr);
  108. return;
  109. }
  110. }
  111. static int diagfwd_smux_runtime_suspend(struct device *dev)
  112. {
  113. dev_dbg(dev, "pm_runtime: suspending...\n");
  114. return 0;
  115. }
  116. static int diagfwd_smux_runtime_resume(struct device *dev)
  117. {
  118. dev_dbg(dev, "pm_runtime: resuming...\n");
  119. return 0;
  120. }
  121. static const struct dev_pm_ops diagfwd_smux_dev_pm_ops = {
  122. .runtime_suspend = diagfwd_smux_runtime_suspend,
  123. .runtime_resume = diagfwd_smux_runtime_resume,
  124. };
  125. int diagfwd_connect_smux(void)
  126. {
  127. void *priv = NULL;
  128. int ret = 0;
  129. if (driver->lcid == LCID_INVALID) {
  130. ret = msm_smux_open(LCID_VALID, priv, diag_smux_event,
  131. diag_get_rx_buffer);
  132. if (!ret) {
  133. driver->lcid = LCID_VALID;
  134. msm_smux_tiocm_set(driver->lcid, TIOCM_DTR, 0);
  135. pr_info("diag: open SMUX ch, r = %d\n", ret);
  136. } else {
  137. pr_err("diag: failed to open SMUX ch, r = %d\n", ret);
  138. return ret;
  139. }
  140. }
  141. /* Poll USB channel to check for data*/
  142. queue_work(diag_bridge[SMUX].wq, &(diag_bridge[SMUX].diag_read_work));
  143. return ret;
  144. }
  145. static int diagfwd_smux_probe(struct platform_device *pdev)
  146. {
  147. int ret = 0;
  148. pr_info("diag: SMUX probe called\n");
  149. driver->lcid = LCID_INVALID;
  150. driver->diag_smux_enabled = 1;
  151. if (driver->buf_in_smux == NULL) {
  152. driver->buf_in_smux = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
  153. if (driver->buf_in_smux == NULL)
  154. goto err;
  155. }
  156. /* Only required for Local loopback test
  157. * ret = msm_smux_set_ch_option(LCID_VALID,
  158. SMUX_CH_OPTION_LOCAL_LOOPBACK, 0);
  159. * if (ret)
  160. * pr_err("diag: error setting SMUX ch option, r = %d\n", ret);
  161. */
  162. if (driver->write_ptr_mdm == NULL)
  163. driver->write_ptr_mdm = kzalloc(sizeof(struct diag_request),
  164. GFP_KERNEL);
  165. if (driver->write_ptr_mdm == NULL)
  166. goto err;
  167. ret = diagfwd_connect_smux();
  168. return ret;
  169. err:
  170. pr_err("diag: Could not initialize SMUX buffer\n");
  171. kfree(driver->buf_in_smux);
  172. return ret;
  173. }
  174. static int diagfwd_smux_remove(struct platform_device *pdev)
  175. {
  176. driver->lcid = LCID_INVALID;
  177. driver->smux_connected = 0;
  178. driver->diag_smux_enabled = 0;
  179. driver->in_busy_smux = 1;
  180. kfree(driver->buf_in_smux);
  181. driver->buf_in_smux = NULL;
  182. return 0;
  183. }
  184. struct platform_driver msm_diagfwd_smux_driver = {
  185. .probe = diagfwd_smux_probe,
  186. .remove = diagfwd_smux_remove,
  187. .driver = {
  188. .name = "SMUX_DIAG",
  189. .owner = THIS_MODULE,
  190. .pm = &diagfwd_smux_dev_pm_ops,
  191. },
  192. };