diagfwd_sdio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Copyright (c) 2011, 2013, 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/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/diagchar.h>
  16. #include <linux/sched.h>
  17. #include <linux/err.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/current.h>
  22. #ifdef CONFIG_DIAG_OVER_USB
  23. #include <mach/usbdiag.h>
  24. #endif
  25. #include "diagchar_hdlc.h"
  26. #include "diagmem.h"
  27. #include "diagchar.h"
  28. #include "diagfwd.h"
  29. #include "diagfwd_sdio.h"
  30. void __diag_sdio_send_req(void)
  31. {
  32. int r = 0;
  33. void *buf = driver->buf_in_sdio;
  34. if (driver->sdio_ch && (!driver->in_busy_sdio)) {
  35. r = sdio_read_avail(driver->sdio_ch);
  36. if (r > IN_BUF_SIZE) {
  37. if (r < MAX_IN_BUF_SIZE) {
  38. pr_err("diag: SDIO sending"
  39. " packets more than %d bytes\n", r);
  40. buf = krealloc(buf, r, GFP_KERNEL);
  41. } else {
  42. pr_err("diag: SDIO sending"
  43. " in packets more than %d bytes\n", MAX_IN_BUF_SIZE);
  44. return;
  45. }
  46. }
  47. if (r > 0) {
  48. if (!buf)
  49. printk(KERN_INFO "Out of diagmem for SDIO\n");
  50. else {
  51. APPEND_DEBUG('i');
  52. sdio_read(driver->sdio_ch, buf, r);
  53. if (((!driver->usb_connected) && (driver->
  54. logging_mode == USB_MODE)) || (driver->
  55. logging_mode == NO_LOGGING_MODE)) {
  56. /* Drop the diag payload */
  57. driver->in_busy_sdio = 0;
  58. return;
  59. }
  60. APPEND_DEBUG('j');
  61. driver->write_ptr_mdm->length = r;
  62. driver->in_busy_sdio = 1;
  63. diag_device_write(buf, SDIO_DATA,
  64. driver->write_ptr_mdm);
  65. }
  66. }
  67. }
  68. }
  69. static void diag_read_sdio_work_fn(struct work_struct *work)
  70. {
  71. __diag_sdio_send_req();
  72. }
  73. static void diag_sdio_notify(void *ctxt, unsigned event)
  74. {
  75. if (event == SDIO_EVENT_DATA_READ_AVAIL)
  76. queue_work(driver->diag_sdio_wq,
  77. &(driver->diag_read_sdio_work));
  78. if (event == SDIO_EVENT_DATA_WRITE_AVAIL)
  79. wake_up_interruptible(&driver->wait_q);
  80. }
  81. static int diag_sdio_close(void)
  82. {
  83. queue_work(driver->diag_sdio_wq, &(driver->diag_close_sdio_work));
  84. return 0;
  85. }
  86. static void diag_close_sdio_work_fn(struct work_struct *work)
  87. {
  88. pr_debug("diag: sdio close called\n");
  89. if (sdio_close(driver->sdio_ch))
  90. pr_err("diag: could not close SDIO channel\n");
  91. else
  92. driver->sdio_ch = NULL; /* channel successfully closed */
  93. }
  94. int diagfwd_connect_sdio(void)
  95. {
  96. int err;
  97. err = usb_diag_alloc_req(driver->mdm_ch, N_MDM_SDIO_WRITE,
  98. N_MDM_SDIO_READ);
  99. if (err)
  100. pr_err("diag: unable to alloc USB req on mdm ch\n");
  101. driver->in_busy_sdio = 0;
  102. if (!driver->sdio_ch) {
  103. err = sdio_open("SDIO_DIAG", &driver->sdio_ch, driver,
  104. diag_sdio_notify);
  105. if (err)
  106. pr_info("diag: could not open SDIO channel\n");
  107. else
  108. pr_info("diag: opened SDIO channel\n");
  109. } else {
  110. pr_info("diag: SDIO channel already open\n");
  111. }
  112. /* Poll USB channel to check for data*/
  113. queue_work(driver->diag_sdio_wq, &(driver->diag_read_mdm_work));
  114. /* Poll SDIO channel to check for data*/
  115. queue_work(driver->diag_sdio_wq, &(driver->diag_read_sdio_work));
  116. return 0;
  117. }
  118. int diagfwd_disconnect_sdio(void)
  119. {
  120. if (driver->sdio_ch && (driver->logging_mode == USB_MODE)) {
  121. driver->in_busy_sdio = 1;
  122. diag_sdio_close();
  123. }
  124. return 0;
  125. }
  126. int diagfwd_write_complete_sdio(void)
  127. {
  128. driver->in_busy_sdio = 0;
  129. APPEND_DEBUG('q');
  130. queue_work(driver->diag_sdio_wq, &(driver->diag_read_sdio_work));
  131. return 0;
  132. }
  133. int diagfwd_read_complete_sdio(void)
  134. {
  135. queue_work(driver->diag_sdio_wq, &(driver->diag_read_mdm_work));
  136. return 0;
  137. }
  138. void diag_read_mdm_work_fn(struct work_struct *work)
  139. {
  140. if (driver->sdio_ch) {
  141. wait_event_interruptible(driver->wait_q, ((sdio_write_avail
  142. (driver->sdio_ch) >= driver->read_len_mdm) ||
  143. !(driver->sdio_ch)));
  144. if (!(driver->sdio_ch)) {
  145. pr_alert("diag: sdio channel not valid");
  146. return;
  147. }
  148. if (driver->sdio_ch && driver->usb_buf_mdm_out &&
  149. (driver->read_len_mdm > 0))
  150. sdio_write(driver->sdio_ch, driver->usb_buf_mdm_out,
  151. driver->read_len_mdm);
  152. APPEND_DEBUG('x');
  153. driver->usb_read_mdm_ptr->buf = driver->usb_buf_mdm_out;
  154. driver->usb_read_mdm_ptr->length = USB_MAX_OUT_BUF;
  155. usb_diag_read(driver->mdm_ch, driver->usb_read_mdm_ptr);
  156. APPEND_DEBUG('y');
  157. }
  158. }
  159. static int diag_sdio_probe(struct platform_device *pdev)
  160. {
  161. int err;
  162. err = sdio_open("SDIO_DIAG", &driver->sdio_ch, driver,
  163. diag_sdio_notify);
  164. if (err)
  165. printk(KERN_INFO "DIAG could not open SDIO channel");
  166. else {
  167. printk(KERN_INFO "DIAG opened SDIO channel");
  168. queue_work(driver->diag_sdio_wq, &(driver->diag_read_mdm_work));
  169. }
  170. return err;
  171. }
  172. static int diag_sdio_remove(struct platform_device *pdev)
  173. {
  174. pr_debug("\n diag: sdio remove called");
  175. /* Disable SDIO channel to prevent further read/write */
  176. driver->sdio_ch = NULL;
  177. return 0;
  178. }
  179. static int diagfwd_sdio_runtime_suspend(struct device *dev)
  180. {
  181. dev_dbg(dev, "pm_runtime: suspending...\n");
  182. return 0;
  183. }
  184. static int diagfwd_sdio_runtime_resume(struct device *dev)
  185. {
  186. dev_dbg(dev, "pm_runtime: resuming...\n");
  187. return 0;
  188. }
  189. static const struct dev_pm_ops diagfwd_sdio_dev_pm_ops = {
  190. .runtime_suspend = diagfwd_sdio_runtime_suspend,
  191. .runtime_resume = diagfwd_sdio_runtime_resume,
  192. };
  193. static struct platform_driver msm_sdio_ch_driver = {
  194. .probe = diag_sdio_probe,
  195. .remove = diag_sdio_remove,
  196. .driver = {
  197. .name = "SDIO_DIAG",
  198. .owner = THIS_MODULE,
  199. .pm = &diagfwd_sdio_dev_pm_ops,
  200. },
  201. };
  202. void diagfwd_sdio_init(void)
  203. {
  204. int ret;
  205. driver->read_len_mdm = 0;
  206. if (driver->buf_in_sdio == NULL)
  207. driver->buf_in_sdio = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
  208. if (driver->buf_in_sdio == NULL)
  209. goto err;
  210. if (driver->usb_buf_mdm_out == NULL)
  211. driver->usb_buf_mdm_out = kzalloc(USB_MAX_OUT_BUF, GFP_KERNEL);
  212. if (driver->usb_buf_mdm_out == NULL)
  213. goto err;
  214. if (driver->write_ptr_mdm == NULL)
  215. driver->write_ptr_mdm = kzalloc(
  216. sizeof(struct diag_request), GFP_KERNEL);
  217. if (driver->write_ptr_mdm == NULL)
  218. goto err;
  219. if (driver->usb_read_mdm_ptr == NULL)
  220. driver->usb_read_mdm_ptr = kzalloc(
  221. sizeof(struct diag_request), GFP_KERNEL);
  222. if (driver->usb_read_mdm_ptr == NULL)
  223. goto err;
  224. driver->diag_sdio_wq = create_singlethread_workqueue("diag_sdio_wq");
  225. #ifdef CONFIG_DIAG_OVER_USB
  226. driver->mdm_ch = usb_diag_open(DIAG_MDM, driver,
  227. diag_usb_legacy_notifier);
  228. if (IS_ERR(driver->mdm_ch)) {
  229. printk(KERN_ERR "Unable to open USB diag MDM channel\n");
  230. goto err;
  231. }
  232. INIT_WORK(&(driver->diag_read_mdm_work), diag_read_mdm_work_fn);
  233. #endif
  234. INIT_WORK(&(driver->diag_read_sdio_work), diag_read_sdio_work_fn);
  235. INIT_WORK(&(driver->diag_close_sdio_work), diag_close_sdio_work_fn);
  236. ret = platform_driver_register(&msm_sdio_ch_driver);
  237. if (ret)
  238. printk(KERN_INFO "DIAG could not register SDIO device");
  239. else
  240. printk(KERN_INFO "DIAG registered SDIO device");
  241. return;
  242. err:
  243. printk(KERN_INFO "\n Could not initialize diag buf for SDIO");
  244. kfree(driver->buf_in_sdio);
  245. kfree(driver->usb_buf_mdm_out);
  246. kfree(driver->write_ptr_mdm);
  247. kfree(driver->usb_read_mdm_ptr);
  248. if (driver->diag_sdio_wq)
  249. destroy_workqueue(driver->diag_sdio_wq);
  250. }
  251. void diagfwd_sdio_exit(void)
  252. {
  253. platform_driver_unregister(&msm_sdio_ch_driver);
  254. #ifdef CONFIG_DIAG_OVER_USB
  255. usb_diag_close(driver->mdm_ch);
  256. #endif
  257. kfree(driver->buf_in_sdio);
  258. kfree(driver->usb_buf_mdm_out);
  259. kfree(driver->write_ptr_mdm);
  260. kfree(driver->usb_read_mdm_ptr);
  261. destroy_workqueue(driver->diag_sdio_wq);
  262. }