diagfwd_hsic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /* Copyright (c) 2012-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/ratelimit.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/smux.h>
  23. #include <asm/current.h>
  24. #ifdef CONFIG_DIAG_OVER_USB
  25. #include <mach/usbdiag.h>
  26. #endif
  27. #include "diagchar_hdlc.h"
  28. #include "diagmem.h"
  29. #include "diagchar.h"
  30. #include "diagfwd.h"
  31. #include "diagfwd_hsic.h"
  32. #include "diagfwd_smux.h"
  33. #include "diagfwd_bridge.h"
  34. #define READ_HSIC_BUF_SIZE 2048
  35. struct diag_hsic_dev *diag_hsic;
  36. static void diag_read_hsic_work_fn(struct work_struct *work)
  37. {
  38. unsigned char *buf_in_hsic = NULL;
  39. int num_reads_submitted = 0;
  40. int err = 0;
  41. int write_ptrs_available;
  42. struct diag_hsic_dev *hsic_struct = container_of(work,
  43. struct diag_hsic_dev, diag_read_hsic_work);
  44. int index = hsic_struct->id;
  45. static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
  46. if (!diag_hsic[index].hsic_ch) {
  47. pr_err("DIAG in %s: diag_hsic[index].hsic_ch == 0\n", __func__);
  48. return;
  49. }
  50. /*
  51. * Determine the current number of available buffers for writing after
  52. * reading from the HSIC has completed.
  53. */
  54. if (driver->logging_mode == MEMORY_DEVICE_MODE)
  55. write_ptrs_available = diag_hsic[index].poolsize_hsic_write -
  56. diag_hsic[index].
  57. num_hsic_buf_tbl_entries;
  58. else
  59. write_ptrs_available = diag_hsic[index].poolsize_hsic_write -
  60. diag_hsic[index].count_hsic_write_pool;
  61. /*
  62. * Queue up a read on the HSIC for all available buffers in the
  63. * pool, exhausting the pool.
  64. */
  65. do {
  66. /*
  67. * If no more write buffers are available,
  68. * stop queuing reads
  69. */
  70. if (write_ptrs_available <= 0)
  71. break;
  72. write_ptrs_available--;
  73. /*
  74. * No sense queuing a read if the HSIC bridge was
  75. * closed in another thread
  76. */
  77. if (!diag_hsic[index].hsic_ch)
  78. break;
  79. buf_in_hsic = diagmem_alloc(driver, READ_HSIC_BUF_SIZE,
  80. index+POOL_TYPE_HSIC);
  81. if (buf_in_hsic) {
  82. /*
  83. * Initiate the read from the HSIC. The HSIC read is
  84. * asynchronous. Once the read is complete the read
  85. * callback function will be called.
  86. */
  87. pr_debug("diag: read from HSIC\n");
  88. num_reads_submitted++;
  89. err = diag_bridge_read(index, (char *)buf_in_hsic,
  90. READ_HSIC_BUF_SIZE);
  91. if (err) {
  92. num_reads_submitted--;
  93. /* Return the buffer to the pool */
  94. diagmem_free(driver, buf_in_hsic,
  95. index+POOL_TYPE_HSIC);
  96. if (__ratelimit(&rl))
  97. pr_err("diag: Error initiating HSIC read, err: %d\n",
  98. err);
  99. /*
  100. * An error occurred, discontinue queuing
  101. * reads
  102. */
  103. break;
  104. }
  105. }
  106. } while (buf_in_hsic);
  107. /*
  108. * If there are read buffers available and for some reason the
  109. * read was not queued, and if no unrecoverable error occurred
  110. * (-ENODEV is an unrecoverable error), then set up the next read
  111. */
  112. if ((diag_hsic[index].count_hsic_pool <
  113. diag_hsic[index].poolsize_hsic) &&
  114. (num_reads_submitted == 0) && (err != -ENODEV) &&
  115. (diag_hsic[index].hsic_ch != 0))
  116. queue_work(diag_bridge[index].wq,
  117. &diag_hsic[index].diag_read_hsic_work);
  118. }
  119. static void diag_hsic_read_complete_callback(void *ctxt, char *buf,
  120. int buf_size, int actual_size)
  121. {
  122. int err = -2;
  123. int index = (int)ctxt;
  124. static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
  125. if (!diag_hsic[index].hsic_ch) {
  126. /*
  127. * The HSIC channel is closed. Return the buffer to
  128. * the pool. Do not send it on.
  129. */
  130. diagmem_free(driver, buf, index+POOL_TYPE_HSIC);
  131. pr_debug("diag: In %s: hsic_ch == 0, actual_size: %d\n",
  132. __func__, actual_size);
  133. return;
  134. }
  135. /*
  136. * Note that zero length is valid and still needs to be sent to
  137. * the USB only when we are logging data to the USB
  138. */
  139. if ((actual_size > 0) ||
  140. ((actual_size == 0) && (driver->logging_mode == USB_MODE))) {
  141. if (!buf) {
  142. pr_err("diag: Out of diagmem for HSIC\n");
  143. } else {
  144. /*
  145. * Send data in buf to be written on the
  146. * appropriate device, e.g. USB MDM channel
  147. */
  148. diag_bridge[index].write_len = actual_size;
  149. err = diag_device_write((void *)buf, index+HSIC_DATA,
  150. NULL);
  151. /* If an error, return buffer to the pool */
  152. if (err) {
  153. diagmem_free(driver, buf, index +
  154. POOL_TYPE_HSIC);
  155. if (__ratelimit(&rl))
  156. pr_err("diag: In %s, error calling diag_device_write, err: %d\n",
  157. __func__, err);
  158. }
  159. }
  160. } else {
  161. /*
  162. * The buffer has an error status associated with it. Do not
  163. * pass it on. Note that -ENOENT is sent when the diag bridge
  164. * is closed.
  165. */
  166. diagmem_free(driver, buf, index+POOL_TYPE_HSIC);
  167. pr_debug("diag: In %s: error status: %d\n", __func__,
  168. actual_size);
  169. }
  170. /*
  171. * If for some reason there was no HSIC data to write to the
  172. * mdm channel, set up another read
  173. */
  174. if (err &&
  175. ((driver->logging_mode == MEMORY_DEVICE_MODE) ||
  176. (diag_bridge[index].usb_connected &&
  177. !diag_hsic[index].hsic_suspend))) {
  178. queue_work(diag_bridge[index].wq,
  179. &diag_hsic[index].diag_read_hsic_work);
  180. }
  181. }
  182. static void diag_hsic_write_complete_callback(void *ctxt, char *buf,
  183. int buf_size, int actual_size)
  184. {
  185. int index = (int)ctxt;
  186. /* The write of the data to the HSIC bridge is complete */
  187. diag_hsic[index].in_busy_hsic_write = 0;
  188. wake_up_interruptible(&driver->wait_q);
  189. if (!diag_hsic[index].hsic_ch) {
  190. pr_err("DIAG in %s: hsic_ch == 0, ch = %d\n", __func__, index);
  191. return;
  192. }
  193. if (actual_size < 0)
  194. pr_err("DIAG in %s: actual_size: %d\n", __func__, actual_size);
  195. if (diag_bridge[index].usb_connected &&
  196. (driver->logging_mode == USB_MODE))
  197. queue_work(diag_bridge[index].wq,
  198. &diag_bridge[index].diag_read_work);
  199. }
  200. static int diag_hsic_suspend(void *ctxt)
  201. {
  202. int index = (int)ctxt;
  203. pr_debug("diag: hsic_suspend\n");
  204. /* Don't allow suspend if a write in the HSIC is in progress */
  205. if (diag_hsic[index].in_busy_hsic_write)
  206. return -EBUSY;
  207. /*
  208. * Don't allow suspend if in MEMORY_DEVICE_MODE and if there
  209. * has been hsic data requested
  210. */
  211. if (driver->logging_mode == MEMORY_DEVICE_MODE &&
  212. diag_hsic[index].hsic_ch)
  213. return -EBUSY;
  214. diag_hsic[index].hsic_suspend = 1;
  215. return 0;
  216. }
  217. static void diag_hsic_resume(void *ctxt)
  218. {
  219. int index = (int)ctxt;
  220. pr_debug("diag: hsic_resume\n");
  221. diag_hsic[index].hsic_suspend = 0;
  222. if ((diag_hsic[index].count_hsic_pool <
  223. diag_hsic[index].poolsize_hsic) &&
  224. ((driver->logging_mode == MEMORY_DEVICE_MODE) ||
  225. (diag_bridge[index].usb_connected)))
  226. queue_work(diag_bridge[index].wq,
  227. &diag_hsic[index].diag_read_hsic_work);
  228. }
  229. struct diag_bridge_ops hsic_diag_bridge_ops[MAX_HSIC_CH] = {
  230. {
  231. .ctxt = NULL,
  232. .read_complete_cb = diag_hsic_read_complete_callback,
  233. .write_complete_cb = diag_hsic_write_complete_callback,
  234. .suspend = diag_hsic_suspend,
  235. .resume = diag_hsic_resume,
  236. },
  237. {
  238. .ctxt = NULL,
  239. .read_complete_cb = diag_hsic_read_complete_callback,
  240. .write_complete_cb = diag_hsic_write_complete_callback,
  241. .suspend = diag_hsic_suspend,
  242. .resume = diag_hsic_resume,
  243. }
  244. };
  245. void diag_hsic_close(int ch_id)
  246. {
  247. if (diag_hsic[ch_id].hsic_device_enabled) {
  248. diag_hsic[ch_id].hsic_ch = 0;
  249. if (diag_hsic[ch_id].hsic_device_opened) {
  250. diag_hsic[ch_id].hsic_device_opened = 0;
  251. diag_bridge_close(ch_id);
  252. pr_debug("diag: %s: closed successfully ch %d\n",
  253. __func__, ch_id);
  254. } else {
  255. pr_debug("diag: %s: already closed ch %d\n",
  256. __func__, ch_id);
  257. }
  258. } else {
  259. pr_debug("diag: %s: HSIC device already removed ch %d\n",
  260. __func__, ch_id);
  261. }
  262. }
  263. /* diagfwd_cancel_hsic is called to cancel outstanding read/writes */
  264. int diagfwd_cancel_hsic(int reopen)
  265. {
  266. int err, i;
  267. /* Cancel it for all active HSIC bridges */
  268. for (i = 0; i < MAX_HSIC_CH; i++) {
  269. if (!diag_bridge[i].enabled)
  270. continue;
  271. mutex_lock(&diag_bridge[i].bridge_mutex);
  272. if (diag_hsic[i].hsic_device_enabled) {
  273. if (diag_hsic[i].hsic_device_opened) {
  274. diag_hsic[i].hsic_ch = 0;
  275. diag_hsic[i].hsic_device_opened = 0;
  276. diag_bridge_close(i);
  277. if (reopen) {
  278. hsic_diag_bridge_ops[i].ctxt =
  279. (void *)(i);
  280. err = diag_bridge_open(i,
  281. &hsic_diag_bridge_ops[i]);
  282. if (err) {
  283. pr_err("diag: HSIC %d channel open error: %d\n",
  284. i, err);
  285. } else {
  286. pr_debug("diag: opened HSIC channel: %d\n",
  287. i);
  288. diag_hsic[i].
  289. hsic_device_opened = 1;
  290. diag_hsic[i].hsic_ch = 1;
  291. }
  292. diag_hsic[i].hsic_data_requested = 1;
  293. } else {
  294. diag_hsic[i].hsic_data_requested = 0;
  295. }
  296. }
  297. }
  298. mutex_unlock(&diag_bridge[i].bridge_mutex);
  299. }
  300. return 0;
  301. }
  302. /*
  303. * diagfwd_write_complete_hsic is called after the asynchronous
  304. * usb_diag_write() on mdm channel is complete
  305. */
  306. int diagfwd_write_complete_hsic(struct diag_request *diag_write_ptr, int index)
  307. {
  308. unsigned char *buf = (diag_write_ptr) ? diag_write_ptr->buf : NULL;
  309. if (buf) {
  310. /* Return buffers to their pools */
  311. diagmem_free(driver, (unsigned char *)buf, index +
  312. POOL_TYPE_HSIC);
  313. diagmem_free(driver, (unsigned char *)diag_write_ptr,
  314. index +
  315. POOL_TYPE_HSIC_WRITE);
  316. }
  317. if (!diag_hsic[index].hsic_ch) {
  318. pr_err("diag: In %s: hsic_ch == 0\n", __func__);
  319. return 0;
  320. }
  321. /* Read data from the HSIC */
  322. queue_work(diag_bridge[index].wq,
  323. &diag_hsic[index].diag_read_hsic_work);
  324. return 0;
  325. }
  326. void diag_usb_read_complete_hsic_fn(struct work_struct *w)
  327. {
  328. struct diag_bridge_dev *bridge_struct = container_of(w,
  329. struct diag_bridge_dev, usb_read_complete_work);
  330. diagfwd_read_complete_bridge(
  331. diag_bridge[bridge_struct->id].usb_read_ptr);
  332. }
  333. void diag_read_usb_hsic_work_fn(struct work_struct *work)
  334. {
  335. struct diag_bridge_dev *bridge_struct = container_of(work,
  336. struct diag_bridge_dev, diag_read_work);
  337. int index = bridge_struct->id;
  338. if (!diag_hsic[index].hsic_ch) {
  339. pr_err("diag: in %s: hsic_ch == 0\n", __func__);
  340. return;
  341. }
  342. /*
  343. * If there is no data being read from the usb mdm channel
  344. * and there is no mdm channel data currently being written
  345. * to the HSIC
  346. */
  347. if (!diag_hsic[index].in_busy_hsic_read_on_device &&
  348. !diag_hsic[index].in_busy_hsic_write) {
  349. APPEND_DEBUG('x');
  350. /* Setup the next read from usb mdm channel */
  351. diag_hsic[index].in_busy_hsic_read_on_device = 1;
  352. diag_bridge[index].usb_read_ptr->buf =
  353. diag_bridge[index].usb_buf_out;
  354. diag_bridge[index].usb_read_ptr->length = USB_MAX_OUT_BUF;
  355. diag_bridge[index].usb_read_ptr->context = (void *)index;
  356. usb_diag_read(diag_bridge[index].ch,
  357. diag_bridge[index].usb_read_ptr);
  358. APPEND_DEBUG('y');
  359. }
  360. /* If for some reason there was no mdm channel read initiated,
  361. * queue up the reading of data from the mdm channel
  362. */
  363. if (!diag_hsic[index].in_busy_hsic_read_on_device &&
  364. (driver->logging_mode == USB_MODE))
  365. queue_work(diag_bridge[index].wq,
  366. &(diag_bridge[index].diag_read_work));
  367. }
  368. static int diag_hsic_probe(struct platform_device *pdev)
  369. {
  370. int err = 0;
  371. /* pdev->Id will indicate which HSIC is working. 0 stands for HSIC
  372. * or CP1 1 indicates HS-USB or CP2
  373. */
  374. pr_debug("diag: in %s, ch = %d\n", __func__, pdev->id);
  375. mutex_lock(&diag_bridge[pdev->id].bridge_mutex);
  376. if (!diag_hsic[pdev->id].hsic_inited) {
  377. spin_lock_init(&diag_hsic[pdev->id].hsic_spinlock);
  378. diag_hsic[pdev->id].num_hsic_buf_tbl_entries = 0;
  379. if (diag_hsic[pdev->id].hsic_buf_tbl == NULL)
  380. diag_hsic[pdev->id].hsic_buf_tbl =
  381. kzalloc(NUM_HSIC_BUF_TBL_ENTRIES *
  382. sizeof(struct diag_write_device), GFP_KERNEL);
  383. if (diag_hsic[pdev->id].hsic_buf_tbl == NULL) {
  384. mutex_unlock(&diag_bridge[pdev->id].bridge_mutex);
  385. return -ENOMEM;
  386. }
  387. diag_hsic[pdev->id].id = pdev->id;
  388. diag_hsic[pdev->id].count_hsic_pool = 0;
  389. diag_hsic[pdev->id].count_hsic_write_pool = 0;
  390. diag_hsic[pdev->id].itemsize_hsic = READ_HSIC_BUF_SIZE;
  391. diag_hsic[pdev->id].poolsize_hsic = N_MDM_WRITE;
  392. diag_hsic[pdev->id].itemsize_hsic_write =
  393. sizeof(struct diag_request);
  394. diag_hsic[pdev->id].poolsize_hsic_write = N_MDM_WRITE;
  395. diagmem_hsic_init(pdev->id);
  396. INIT_WORK(&(diag_hsic[pdev->id].diag_read_hsic_work),
  397. diag_read_hsic_work_fn);
  398. diag_hsic[pdev->id].hsic_data_requested =
  399. (driver->logging_mode == MEMORY_DEVICE_MODE) ? 0 : 1;
  400. diag_hsic[pdev->id].hsic_inited = 1;
  401. }
  402. /*
  403. * The probe function was called after the usb was connected
  404. * on the legacy channel OR ODL is turned on and hsic data is
  405. * requested. Communication over usb mdm and HSIC needs to be
  406. * turned on.
  407. */
  408. if ((diag_bridge[pdev->id].usb_connected &&
  409. (driver->logging_mode != MEMORY_DEVICE_MODE)) ||
  410. ((driver->logging_mode == MEMORY_DEVICE_MODE) &&
  411. diag_hsic[pdev->id].hsic_data_requested)) {
  412. if (diag_hsic[pdev->id].hsic_device_opened) {
  413. /* should not happen. close it before re-opening */
  414. pr_warn("diag: HSIC channel already opened in probe\n");
  415. diag_bridge_close(pdev->id);
  416. }
  417. hsic_diag_bridge_ops[pdev->id].ctxt = (void *)(pdev->id);
  418. err = diag_bridge_open(pdev->id,
  419. &hsic_diag_bridge_ops[pdev->id]);
  420. if (err) {
  421. pr_err("diag: could not open HSIC, err: %d\n", err);
  422. diag_hsic[pdev->id].hsic_device_opened = 0;
  423. mutex_unlock(&diag_bridge[pdev->id].bridge_mutex);
  424. return err;
  425. }
  426. pr_info("diag: opened HSIC bridge, ch = %d\n", pdev->id);
  427. diag_hsic[pdev->id].hsic_device_opened = 1;
  428. diag_hsic[pdev->id].hsic_ch = 1;
  429. diag_hsic[pdev->id].in_busy_hsic_read_on_device = 0;
  430. diag_hsic[pdev->id].in_busy_hsic_write = 0;
  431. if (diag_bridge[pdev->id].usb_connected) {
  432. /* Poll USB mdm channel to check for data */
  433. queue_work(diag_bridge[pdev->id].wq,
  434. &diag_bridge[pdev->id].diag_read_work);
  435. }
  436. /* Poll HSIC channel to check for data */
  437. queue_work(diag_bridge[pdev->id].wq,
  438. &diag_hsic[pdev->id].diag_read_hsic_work);
  439. }
  440. /* The HSIC (diag_bridge) platform device driver is enabled */
  441. diag_hsic[pdev->id].hsic_device_enabled = 1;
  442. mutex_unlock(&diag_bridge[pdev->id].bridge_mutex);
  443. return err;
  444. }
  445. static int diag_hsic_remove(struct platform_device *pdev)
  446. {
  447. pr_debug("diag: %s called\n", __func__);
  448. if (diag_hsic[pdev->id].hsic_device_enabled) {
  449. mutex_lock(&diag_bridge[pdev->id].bridge_mutex);
  450. diag_hsic_close(pdev->id);
  451. diag_hsic[pdev->id].hsic_device_enabled = 0;
  452. mutex_unlock(&diag_bridge[pdev->id].bridge_mutex);
  453. }
  454. return 0;
  455. }
  456. static int diagfwd_hsic_runtime_suspend(struct device *dev)
  457. {
  458. dev_dbg(dev, "pm_runtime: suspending...\n");
  459. return 0;
  460. }
  461. static int diagfwd_hsic_runtime_resume(struct device *dev)
  462. {
  463. dev_dbg(dev, "pm_runtime: resuming...\n");
  464. return 0;
  465. }
  466. static const struct dev_pm_ops diagfwd_hsic_dev_pm_ops = {
  467. .runtime_suspend = diagfwd_hsic_runtime_suspend,
  468. .runtime_resume = diagfwd_hsic_runtime_resume,
  469. };
  470. struct platform_driver msm_hsic_ch_driver = {
  471. .probe = diag_hsic_probe,
  472. .remove = diag_hsic_remove,
  473. .driver = {
  474. .name = "diag_bridge",
  475. .owner = THIS_MODULE,
  476. .pm = &diagfwd_hsic_dev_pm_ops,
  477. },
  478. };