diagfwd.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /* Copyright (c) 2008-2018, 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/module.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/sched.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/diagchar.h>
  22. #include <linux/delay.h>
  23. #include <linux/reboot.h>
  24. #include <linux/of.h>
  25. #include <linux/kmemleak.h>
  26. #ifdef CONFIG_DIAG_OVER_USB
  27. #include <linux/usb/usbdiag.h>
  28. #endif
  29. #include <soc/qcom/socinfo.h>
  30. #include <soc/qcom/restart.h>
  31. #include "diagmem.h"
  32. #include "diagchar.h"
  33. #include "diagfwd.h"
  34. #include "diagfwd_peripheral.h"
  35. #include "diagfwd_cntl.h"
  36. #include "diagchar_hdlc.h"
  37. #include "diag_dci.h"
  38. #include "diag_masks.h"
  39. #include "diag_usb.h"
  40. #include "diag_mux.h"
  41. #include "diag_ipc_logging.h"
  42. #define STM_CMD_VERSION_OFFSET 4
  43. #define STM_CMD_MASK_OFFSET 5
  44. #define STM_CMD_DATA_OFFSET 6
  45. #define STM_CMD_NUM_BYTES 7
  46. #define STM_RSP_SUPPORTED_INDEX 7
  47. #define STM_RSP_STATUS_INDEX 8
  48. #define STM_RSP_NUM_BYTES 9
  49. struct diag_md_hdlc_reset_work {
  50. int pid;
  51. struct work_struct work;
  52. };
  53. static int timestamp_switch;
  54. module_param(timestamp_switch, int, 0644);
  55. int wrap_enabled;
  56. uint16_t wrap_count;
  57. static struct diag_hdlc_decode_type *hdlc_decode;
  58. #define DIAG_NUM_COMMON_CMD 1
  59. static uint8_t common_cmds[DIAG_NUM_COMMON_CMD] = {
  60. DIAG_CMD_LOG_ON_DMND
  61. };
  62. static uint8_t hdlc_timer_in_progress;
  63. /* Determine if this device uses a device tree */
  64. #ifdef CONFIG_OF
  65. static int has_device_tree(void)
  66. {
  67. struct device_node *node;
  68. node = of_find_node_by_path("/");
  69. if (node) {
  70. of_node_put(node);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. #else
  76. static int has_device_tree(void)
  77. {
  78. return 0;
  79. }
  80. #endif
  81. int chk_config_get_id(void)
  82. {
  83. switch (socinfo_get_msm_cpu()) {
  84. case MSM_CPU_8X60:
  85. return APQ8060_TOOLS_ID;
  86. case MSM_CPU_8960:
  87. case MSM_CPU_8960AB:
  88. return AO8960_TOOLS_ID;
  89. case MSM_CPU_8064:
  90. case MSM_CPU_8064AB:
  91. case MSM_CPU_8064AA:
  92. return APQ8064_TOOLS_ID;
  93. case MSM_CPU_8930:
  94. case MSM_CPU_8930AA:
  95. case MSM_CPU_8930AB:
  96. return MSM8930_TOOLS_ID;
  97. case MSM_CPU_8974:
  98. return MSM8974_TOOLS_ID;
  99. case MSM_CPU_8625:
  100. return MSM8625_TOOLS_ID;
  101. case MSM_CPU_8084:
  102. return APQ8084_TOOLS_ID;
  103. case MSM_CPU_8916:
  104. return MSM8916_TOOLS_ID;
  105. case MSM_CPU_8939:
  106. return MSM8939_TOOLS_ID;
  107. case MSM_CPU_8994:
  108. return MSM8994_TOOLS_ID;
  109. case MSM_CPU_8226:
  110. return APQ8026_TOOLS_ID;
  111. case MSM_CPU_8909:
  112. return MSM8909_TOOLS_ID;
  113. case MSM_CPU_8992:
  114. return MSM8992_TOOLS_ID;
  115. case MSM_CPU_8996:
  116. return MSM_8996_TOOLS_ID;
  117. default:
  118. if (driver->use_device_tree) {
  119. if (machine_is_msm8974())
  120. return MSM8974_TOOLS_ID;
  121. else if (machine_is_apq8074())
  122. return APQ8074_TOOLS_ID;
  123. else
  124. return 0;
  125. } else {
  126. return 0;
  127. }
  128. }
  129. }
  130. /*
  131. * This will return TRUE for targets which support apps only mode and hence SSR.
  132. * This applies to 8960 and newer targets.
  133. */
  134. int chk_apps_only(void)
  135. {
  136. if (driver->use_device_tree)
  137. return 1;
  138. switch (socinfo_get_msm_cpu()) {
  139. case MSM_CPU_8960:
  140. case MSM_CPU_8960AB:
  141. case MSM_CPU_8064:
  142. case MSM_CPU_8064AB:
  143. case MSM_CPU_8064AA:
  144. case MSM_CPU_8930:
  145. case MSM_CPU_8930AA:
  146. case MSM_CPU_8930AB:
  147. case MSM_CPU_8627:
  148. case MSM_CPU_9615:
  149. case MSM_CPU_8974:
  150. return 1;
  151. default:
  152. return 0;
  153. }
  154. }
  155. /*
  156. * This will return TRUE for targets which support apps as master.
  157. * Thus, SW DLOAD and Mode Reset are supported on apps processor.
  158. * This applies to 8960 and newer targets.
  159. */
  160. int chk_apps_master(void)
  161. {
  162. if (driver->use_device_tree)
  163. return 1;
  164. else
  165. return 0;
  166. }
  167. int chk_polling_response(void)
  168. {
  169. if (!(driver->polling_reg_flag) && chk_apps_master())
  170. /*
  171. * If the apps processor is master and no other processor
  172. * has registered to respond for polling
  173. */
  174. return 1;
  175. else if (!(driver->diagfwd_cntl[PERIPHERAL_MODEM] &&
  176. driver->diagfwd_cntl[PERIPHERAL_MODEM]->ch_open) &&
  177. (driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask))
  178. /*
  179. * If the apps processor is not the master and the modem
  180. * is not up or we did not receive the feature masks from Modem
  181. */
  182. return 1;
  183. else
  184. return 0;
  185. }
  186. /*
  187. * This function should be called if you feel that the logging process may
  188. * need to be woken up. For instance, if the logging mode is MEMORY_DEVICE MODE
  189. * and while trying to read data from data channel there are no buffers
  190. * available to read the data into, then this function should be called to
  191. * determine if the logging process needs to be woken up.
  192. */
  193. void chk_logging_wakeup(void)
  194. {
  195. int i;
  196. int j;
  197. int pid = 0;
  198. for (j = 0; j < NUM_MD_SESSIONS; j++) {
  199. if (!driver->md_session_map[j])
  200. continue;
  201. pid = driver->md_session_map[j]->pid;
  202. /* Find the index of the logging process */
  203. for (i = 0; i < driver->num_clients; i++) {
  204. if (driver->client_map[i].pid != pid)
  205. continue;
  206. if (driver->data_ready[i] & USER_SPACE_DATA_TYPE)
  207. continue;
  208. /*
  209. * At very high logging rates a race condition can
  210. * occur where the buffers containing the data from
  211. * a channel are all in use, but the data_ready flag
  212. * is cleared. In this case, the buffers never have
  213. * their data read/logged. Detect and remedy this
  214. * situation.
  215. */
  216. driver->data_ready[i] |= USER_SPACE_DATA_TYPE;
  217. atomic_inc(&driver->data_ready_notif[i]);
  218. pr_debug("diag: Force wakeup of logging process\n");
  219. wake_up_interruptible(&driver->wait_q);
  220. break;
  221. }
  222. /*
  223. * Diag Memory Device is in normal. Check only for the first
  224. * index as all the indices point to the same session
  225. * structure.
  226. */
  227. if ((driver->md_session_mask == DIAG_CON_ALL) && (j == 0))
  228. break;
  229. }
  230. }
  231. static void pack_rsp_and_send(unsigned char *buf, int len,
  232. int pid)
  233. {
  234. int err;
  235. int retry_count = 0, i, rsp_ctxt;
  236. uint32_t write_len = 0;
  237. unsigned long flags;
  238. unsigned char *rsp_ptr = driver->encoded_rsp_buf;
  239. struct diag_pkt_frame_t header;
  240. struct diag_md_session_t *session_info = NULL, *info = NULL;
  241. if (!rsp_ptr || !buf)
  242. return;
  243. if (len > DIAG_MAX_RSP_SIZE || len < 0) {
  244. pr_err("diag: In %s, invalid len %d, permissible len %d\n",
  245. __func__, len, DIAG_MAX_RSP_SIZE);
  246. return;
  247. }
  248. mutex_lock(&driver->md_session_lock);
  249. session_info = diag_md_session_get_pid(pid);
  250. info = (session_info) ? session_info :
  251. diag_md_session_get_peripheral(APPS_DATA);
  252. /*
  253. * Explicitly check for the Peripheral Modem here
  254. * is necessary till a way to identify a peripheral
  255. * if its supporting qshrink4 feature.
  256. */
  257. if (info && info->peripheral_mask) {
  258. if (info->peripheral_mask == DIAG_CON_ALL ||
  259. (info->peripheral_mask & (1 << APPS_DATA)) ||
  260. (info->peripheral_mask & (1 << PERIPHERAL_MODEM))) {
  261. rsp_ctxt = SET_BUF_CTXT(APPS_DATA, TYPE_CMD, 1);
  262. } else {
  263. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  264. if (info->peripheral_mask & (1 << i))
  265. break;
  266. }
  267. rsp_ctxt = SET_BUF_CTXT(i, TYPE_CMD, 1);
  268. }
  269. } else
  270. rsp_ctxt = driver->rsp_buf_ctxt;
  271. mutex_unlock(&driver->md_session_lock);
  272. /*
  273. * Keep trying till we get the buffer back. It should probably
  274. * take one or two iterations. When this loops till UINT_MAX, it
  275. * means we did not get a write complete for the previous
  276. * response.
  277. */
  278. while (retry_count < UINT_MAX) {
  279. if (!driver->rsp_buf_busy)
  280. break;
  281. /*
  282. * Wait for sometime and try again. The value 10000 was chosen
  283. * empirically as an optimum value for USB to complete a write
  284. */
  285. usleep_range(10000, 10100);
  286. retry_count++;
  287. /*
  288. * There can be a race conditon that clears the data ready flag
  289. * for responses. Make sure we don't miss previous wakeups for
  290. * draining responses when we are in Memory Device Mode.
  291. */
  292. if (driver->logging_mode == DIAG_MEMORY_DEVICE_MODE ||
  293. driver->logging_mode == DIAG_MULTI_MODE) {
  294. mutex_lock(&driver->md_session_lock);
  295. chk_logging_wakeup();
  296. mutex_unlock(&driver->md_session_lock);
  297. }
  298. }
  299. if (driver->rsp_buf_busy) {
  300. pr_err("diag: unable to get hold of response buffer\n");
  301. return;
  302. }
  303. driver->rsp_buf_busy = 1;
  304. header.start = CONTROL_CHAR;
  305. header.version = 1;
  306. header.length = len;
  307. memcpy(rsp_ptr, &header, sizeof(header));
  308. write_len += sizeof(header);
  309. memcpy(rsp_ptr + write_len, buf, len);
  310. write_len += len;
  311. *(uint8_t *)(rsp_ptr + write_len) = CONTROL_CHAR;
  312. write_len += sizeof(uint8_t);
  313. err = diag_mux_write(DIAG_LOCAL_PROC, rsp_ptr, write_len, rsp_ctxt);
  314. if (err) {
  315. pr_err("diag: In %s, unable to write to mux, err: %d\n",
  316. __func__, err);
  317. spin_lock_irqsave(&driver->rsp_buf_busy_lock, flags);
  318. driver->rsp_buf_busy = 0;
  319. spin_unlock_irqrestore(&driver->rsp_buf_busy_lock, flags);
  320. }
  321. }
  322. static void encode_rsp_and_send(unsigned char *buf, int len,
  323. int pid)
  324. {
  325. struct diag_send_desc_type send = { NULL, NULL, DIAG_STATE_START, 0 };
  326. struct diag_hdlc_dest_type enc = { NULL, NULL, 0 };
  327. unsigned char *rsp_ptr = driver->encoded_rsp_buf;
  328. int err, i, rsp_ctxt, retry_count = 0;
  329. unsigned long flags;
  330. struct diag_md_session_t *session_info = NULL, *info = NULL;
  331. if (!rsp_ptr || !buf)
  332. return;
  333. if (len > DIAG_MAX_RSP_SIZE || len < 0) {
  334. pr_err("diag: In %s, invalid len %d, permissible len %d\n",
  335. __func__, len, DIAG_MAX_RSP_SIZE);
  336. return;
  337. }
  338. mutex_lock(&driver->md_session_lock);
  339. session_info = diag_md_session_get_pid(pid);
  340. info = (session_info) ? session_info :
  341. diag_md_session_get_peripheral(APPS_DATA);
  342. /*
  343. * Explicitly check for the Peripheral Modem here
  344. * is necessary till a way to identify a peripheral
  345. * if its supporting qshrink4 feature.
  346. */
  347. if (info && info->peripheral_mask) {
  348. if (info->peripheral_mask == DIAG_CON_ALL ||
  349. (info->peripheral_mask & (1 << APPS_DATA)) ||
  350. (info->peripheral_mask & (1 << PERIPHERAL_MODEM))) {
  351. rsp_ctxt = SET_BUF_CTXT(APPS_DATA, TYPE_CMD, 1);
  352. } else {
  353. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  354. if (info->peripheral_mask & (1 << i))
  355. break;
  356. }
  357. rsp_ctxt = SET_BUF_CTXT(i, TYPE_CMD, 1);
  358. }
  359. } else
  360. rsp_ctxt = driver->rsp_buf_ctxt;
  361. mutex_unlock(&driver->md_session_lock);
  362. /*
  363. * Keep trying till we get the buffer back. It should probably
  364. * take one or two iterations. When this loops till UINT_MAX, it
  365. * means we did not get a write complete for the previous
  366. * response.
  367. */
  368. while (retry_count < UINT_MAX) {
  369. if (!driver->rsp_buf_busy)
  370. break;
  371. /*
  372. * Wait for sometime and try again. The value 10000 was chosen
  373. * empirically as an optimum value for USB to complete a write
  374. */
  375. usleep_range(10000, 10100);
  376. retry_count++;
  377. /*
  378. * There can be a race conditon that clears the data ready flag
  379. * for responses. Make sure we don't miss previous wakeups for
  380. * draining responses when we are in Memory Device Mode.
  381. */
  382. if (driver->logging_mode == DIAG_MEMORY_DEVICE_MODE ||
  383. driver->logging_mode == DIAG_MULTI_MODE) {
  384. mutex_lock(&driver->md_session_lock);
  385. chk_logging_wakeup();
  386. mutex_unlock(&driver->md_session_lock);
  387. }
  388. }
  389. if (driver->rsp_buf_busy) {
  390. pr_err("diag: unable to get hold of response buffer\n");
  391. return;
  392. }
  393. spin_lock_irqsave(&driver->rsp_buf_busy_lock, flags);
  394. driver->rsp_buf_busy = 1;
  395. spin_unlock_irqrestore(&driver->rsp_buf_busy_lock, flags);
  396. send.state = DIAG_STATE_START;
  397. send.pkt = buf;
  398. send.last = (void *)(buf + len - 1);
  399. send.terminate = 1;
  400. enc.dest = rsp_ptr;
  401. enc.dest_last = (void *)(rsp_ptr + DIAG_MAX_HDLC_BUF_SIZE - 1);
  402. diag_hdlc_encode(&send, &enc);
  403. driver->encoded_rsp_len = (int)(enc.dest - (void *)rsp_ptr);
  404. err = diag_mux_write(DIAG_LOCAL_PROC, rsp_ptr, driver->encoded_rsp_len,
  405. rsp_ctxt);
  406. if (err) {
  407. pr_err("diag: In %s, Unable to write to device, err: %d\n",
  408. __func__, err);
  409. spin_lock_irqsave(&driver->rsp_buf_busy_lock, flags);
  410. driver->rsp_buf_busy = 0;
  411. spin_unlock_irqrestore(&driver->rsp_buf_busy_lock, flags);
  412. }
  413. memset(buf, '\0', DIAG_MAX_RSP_SIZE);
  414. }
  415. static void diag_send_rsp(unsigned char *buf, int len,
  416. int pid)
  417. {
  418. struct diag_md_session_t *session_info = NULL, *info = NULL;
  419. uint8_t hdlc_disabled;
  420. mutex_lock(&driver->md_session_lock);
  421. info = diag_md_session_get_pid(pid);
  422. session_info = (info) ? info :
  423. diag_md_session_get_peripheral(APPS_DATA);
  424. if (session_info)
  425. hdlc_disabled = session_info->hdlc_disabled;
  426. else
  427. hdlc_disabled = driver->hdlc_disabled;
  428. mutex_unlock(&driver->md_session_lock);
  429. if (hdlc_disabled)
  430. pack_rsp_and_send(buf, len, pid);
  431. else
  432. encode_rsp_and_send(buf, len, pid);
  433. }
  434. void diag_update_pkt_buffer(unsigned char *buf, uint32_t len, int type)
  435. {
  436. unsigned char *ptr = NULL;
  437. unsigned char *temp = buf;
  438. int *in_busy = NULL;
  439. uint32_t *length = NULL;
  440. uint32_t max_len = 0;
  441. if (!buf || len == 0) {
  442. pr_err("diag: In %s, Invalid ptr %pK and length %d\n",
  443. __func__, buf, len);
  444. return;
  445. }
  446. switch (type) {
  447. case PKT_TYPE:
  448. ptr = driver->apps_req_buf;
  449. length = &driver->apps_req_buf_len;
  450. max_len = DIAG_MAX_REQ_SIZE;
  451. in_busy = &driver->in_busy_pktdata;
  452. break;
  453. case DCI_PKT_TYPE:
  454. ptr = driver->dci_pkt_buf;
  455. length = &driver->dci_pkt_length;
  456. max_len = DCI_BUF_SIZE;
  457. in_busy = &driver->in_busy_dcipktdata;
  458. break;
  459. default:
  460. pr_err("diag: Invalid type %d in %s\n", type, __func__);
  461. return;
  462. }
  463. mutex_lock(&driver->diagchar_mutex);
  464. if (CHK_OVERFLOW(ptr, ptr, ptr + max_len, len)) {
  465. memcpy(ptr, temp, len);
  466. *length = len;
  467. *in_busy = 1;
  468. } else {
  469. pr_alert("diag: In %s, no space for response packet, len: %d, type: %d\n",
  470. __func__, len, type);
  471. }
  472. mutex_unlock(&driver->diagchar_mutex);
  473. }
  474. void diag_update_userspace_clients(unsigned int type)
  475. {
  476. int i;
  477. mutex_lock(&driver->diagchar_mutex);
  478. for (i = 0; i < driver->num_clients; i++)
  479. if (driver->client_map[i].pid != 0 &&
  480. !(driver->data_ready[i] & type)) {
  481. driver->data_ready[i] |= type;
  482. atomic_inc(&driver->data_ready_notif[i]);
  483. }
  484. wake_up_interruptible(&driver->wait_q);
  485. mutex_unlock(&driver->diagchar_mutex);
  486. }
  487. void diag_update_md_clients(unsigned int type)
  488. {
  489. int i, j;
  490. mutex_lock(&driver->diagchar_mutex);
  491. mutex_lock(&driver->md_session_lock);
  492. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  493. if (driver->md_session_map[i] != NULL)
  494. for (j = 0; j < driver->num_clients; j++) {
  495. if (driver->client_map[j].pid != 0 &&
  496. driver->client_map[j].pid ==
  497. driver->md_session_map[i]->pid) {
  498. if (!(driver->data_ready[j] & type)) {
  499. driver->data_ready[j] |= type;
  500. atomic_inc(
  501. &driver->data_ready_notif[j]);
  502. }
  503. break;
  504. }
  505. }
  506. }
  507. mutex_unlock(&driver->md_session_lock);
  508. wake_up_interruptible(&driver->wait_q);
  509. mutex_unlock(&driver->diagchar_mutex);
  510. }
  511. void diag_update_sleeping_process(int process_id, int data_type)
  512. {
  513. int i;
  514. mutex_lock(&driver->diagchar_mutex);
  515. for (i = 0; i < driver->num_clients; i++)
  516. if (driver->client_map[i].pid == process_id) {
  517. if (!(driver->data_ready[i] & data_type)) {
  518. driver->data_ready[i] |= data_type;
  519. atomic_inc(&driver->data_ready_notif[i]);
  520. }
  521. break;
  522. }
  523. wake_up_interruptible(&driver->wait_q);
  524. mutex_unlock(&driver->diagchar_mutex);
  525. }
  526. static int diag_send_data(struct diag_cmd_reg_t *entry, unsigned char *buf,
  527. int len)
  528. {
  529. if (!entry)
  530. return -EIO;
  531. if (entry->proc == APPS_DATA) {
  532. diag_update_pkt_buffer(buf, len, PKT_TYPE);
  533. diag_update_sleeping_process(entry->pid, PKT_TYPE);
  534. return 0;
  535. }
  536. return diagfwd_write(entry->proc, TYPE_CMD, buf, len);
  537. }
  538. void diag_process_stm_mask(uint8_t cmd, uint8_t data_mask, int data_type)
  539. {
  540. int status = 0;
  541. if (data_type >= PERIPHERAL_MODEM && data_type <= PERIPHERAL_SENSORS) {
  542. if (driver->feature[data_type].stm_support) {
  543. status = diag_send_stm_state(data_type, cmd);
  544. if (status == 0)
  545. driver->stm_state[data_type] = cmd;
  546. }
  547. driver->stm_state_requested[data_type] = cmd;
  548. } else if (data_type == APPS_DATA) {
  549. driver->stm_state[data_type] = cmd;
  550. driver->stm_state_requested[data_type] = cmd;
  551. }
  552. }
  553. int diag_process_stm_cmd(unsigned char *buf, unsigned char *dest_buf)
  554. {
  555. uint8_t version, mask, cmd;
  556. uint8_t rsp_supported = 0;
  557. uint8_t rsp_status = 0;
  558. int i;
  559. if (!buf || !dest_buf) {
  560. pr_err("diag: Invalid pointers buf: %pK, dest_buf %pK in %s\n",
  561. buf, dest_buf, __func__);
  562. return -EIO;
  563. }
  564. version = *(buf + STM_CMD_VERSION_OFFSET);
  565. mask = *(buf + STM_CMD_MASK_OFFSET);
  566. cmd = *(buf + STM_CMD_DATA_OFFSET);
  567. /*
  568. * Check if command is valid. If the command is asking for
  569. * status, then the processor mask field is to be ignored.
  570. */
  571. if ((version != 2) || (cmd > STM_AUTO_QUERY) ||
  572. ((cmd != STATUS_STM && cmd != STM_AUTO_QUERY) &&
  573. ((mask == 0) || (0 != (mask >> 4))))) {
  574. /* Command is invalid. Send bad param message response */
  575. dest_buf[0] = BAD_PARAM_RESPONSE_MESSAGE;
  576. for (i = 0; i < STM_CMD_NUM_BYTES; i++)
  577. dest_buf[i+1] = *(buf + i);
  578. return STM_CMD_NUM_BYTES+1;
  579. } else if (cmd != STATUS_STM && cmd != STM_AUTO_QUERY) {
  580. if (mask & DIAG_STM_MODEM)
  581. diag_process_stm_mask(cmd, DIAG_STM_MODEM,
  582. PERIPHERAL_MODEM);
  583. if (mask & DIAG_STM_LPASS)
  584. diag_process_stm_mask(cmd, DIAG_STM_LPASS,
  585. PERIPHERAL_LPASS);
  586. if (mask & DIAG_STM_WCNSS)
  587. diag_process_stm_mask(cmd, DIAG_STM_WCNSS,
  588. PERIPHERAL_WCNSS);
  589. if (mask & DIAG_STM_SENSORS)
  590. diag_process_stm_mask(cmd, DIAG_STM_SENSORS,
  591. PERIPHERAL_SENSORS);
  592. if (mask & DIAG_STM_WDSP)
  593. diag_process_stm_mask(cmd, DIAG_STM_WDSP,
  594. PERIPHERAL_WDSP);
  595. if (mask & DIAG_STM_CDSP)
  596. diag_process_stm_mask(cmd, DIAG_STM_CDSP,
  597. PERIPHERAL_CDSP);
  598. if (mask & DIAG_STM_APPS)
  599. diag_process_stm_mask(cmd, DIAG_STM_APPS, APPS_DATA);
  600. }
  601. for (i = 0; i < STM_CMD_NUM_BYTES; i++)
  602. dest_buf[i] = *(buf + i);
  603. /* Set mask denoting which peripherals support STM */
  604. if (driver->feature[PERIPHERAL_MODEM].stm_support)
  605. rsp_supported |= DIAG_STM_MODEM;
  606. if (driver->feature[PERIPHERAL_LPASS].stm_support)
  607. rsp_supported |= DIAG_STM_LPASS;
  608. if (driver->feature[PERIPHERAL_WCNSS].stm_support)
  609. rsp_supported |= DIAG_STM_WCNSS;
  610. if (driver->feature[PERIPHERAL_SENSORS].stm_support)
  611. rsp_supported |= DIAG_STM_SENSORS;
  612. if (driver->feature[PERIPHERAL_WDSP].stm_support)
  613. rsp_supported |= DIAG_STM_WDSP;
  614. if (driver->feature[PERIPHERAL_CDSP].stm_support)
  615. rsp_supported |= DIAG_STM_CDSP;
  616. rsp_supported |= DIAG_STM_APPS;
  617. /* Set mask denoting STM state/status for each peripheral/APSS */
  618. if (driver->stm_state[PERIPHERAL_MODEM])
  619. rsp_status |= DIAG_STM_MODEM;
  620. if (driver->stm_state[PERIPHERAL_LPASS])
  621. rsp_status |= DIAG_STM_LPASS;
  622. if (driver->stm_state[PERIPHERAL_WCNSS])
  623. rsp_status |= DIAG_STM_WCNSS;
  624. if (driver->stm_state[PERIPHERAL_SENSORS])
  625. rsp_status |= DIAG_STM_SENSORS;
  626. if (driver->stm_state[PERIPHERAL_WDSP])
  627. rsp_status |= DIAG_STM_WDSP;
  628. if (driver->stm_state[PERIPHERAL_CDSP])
  629. rsp_status |= DIAG_STM_CDSP;
  630. if (driver->stm_state[APPS_DATA])
  631. rsp_status |= DIAG_STM_APPS;
  632. dest_buf[STM_RSP_SUPPORTED_INDEX] = rsp_supported;
  633. dest_buf[STM_RSP_STATUS_INDEX] = rsp_status;
  634. return STM_RSP_NUM_BYTES;
  635. }
  636. int diag_process_time_sync_query_cmd(unsigned char *src_buf, int src_len,
  637. unsigned char *dest_buf, int dest_len)
  638. {
  639. int write_len = 0;
  640. struct diag_cmd_time_sync_query_req_t *req = NULL;
  641. struct diag_cmd_time_sync_query_rsp_t rsp;
  642. if (!src_buf || !dest_buf || src_len <= 0 || dest_len <= 0) {
  643. pr_err("diag: Invalid input in %s, src_buf: %pK, src_len: %d, dest_buf: %pK, dest_len: %d",
  644. __func__, src_buf, src_len, dest_buf, dest_len);
  645. return -EINVAL;
  646. }
  647. req = (struct diag_cmd_time_sync_query_req_t *)src_buf;
  648. rsp.header.cmd_code = req->header.cmd_code;
  649. rsp.header.subsys_id = req->header.subsys_id;
  650. rsp.header.subsys_cmd_code = req->header.subsys_cmd_code;
  651. rsp.version = req->version;
  652. rsp.time_api = driver->uses_time_api;
  653. memcpy(dest_buf, &rsp, sizeof(rsp));
  654. write_len = sizeof(rsp);
  655. return write_len;
  656. }
  657. int diag_process_diag_id_query_cmd(unsigned char *src_buf, int src_len,
  658. unsigned char *dest_buf, int dest_len)
  659. {
  660. int write_len = 0;
  661. struct diag_cmd_diag_id_query_req_t *req = NULL;
  662. struct diag_cmd_diag_id_query_rsp_t rsp;
  663. struct list_head *start;
  664. struct list_head *temp;
  665. struct diag_id_tbl_t *item = NULL;
  666. int rsp_len = 0;
  667. int num_entries = 0;
  668. uint8_t process_name_len = 0;
  669. if (!src_buf || !dest_buf || src_len <= 0 || dest_len <= 0) {
  670. pr_err("diag: Invalid input in %s, src_buf:%pK, src_len:%d, dest_buf:%pK, dest_len:%d\n",
  671. __func__, src_buf, src_len, dest_buf, dest_len);
  672. return -EINVAL;
  673. }
  674. req = (struct diag_cmd_diag_id_query_req_t *) src_buf;
  675. rsp.header.cmd_code = req->header.cmd_code;
  676. rsp.header.subsys_id = req->header.subsys_id;
  677. rsp.header.subsys_cmd_code = req->header.subsys_cmd_code;
  678. rsp.version = req->version;
  679. rsp.entry.process_name = NULL;
  680. rsp.entry.len = 0;
  681. rsp.entry.diag_id = 0;
  682. write_len = sizeof(rsp.header) + sizeof(rsp.version) +
  683. sizeof(rsp.num_entries);
  684. rsp_len = write_len;
  685. mutex_lock(&driver->diag_id_mutex);
  686. list_for_each_safe(start, temp, &driver->diag_id_list) {
  687. item = list_entry(start, struct diag_id_tbl_t, link);
  688. memcpy(dest_buf + write_len, &item->diag_id,
  689. sizeof(item->diag_id));
  690. write_len = write_len + sizeof(item->diag_id);
  691. process_name_len = strlen(item->process_name) + 1;
  692. memcpy(dest_buf + write_len, &process_name_len,
  693. sizeof(process_name_len));
  694. write_len = write_len + sizeof(process_name_len);
  695. memcpy(dest_buf + write_len, item->process_name,
  696. strlen(item->process_name) + 1);
  697. write_len = write_len + strlen(item->process_name) + 1;
  698. num_entries++;
  699. }
  700. mutex_unlock(&driver->diag_id_mutex);
  701. rsp.num_entries = num_entries;
  702. memcpy(dest_buf, &rsp, rsp_len);
  703. return write_len;
  704. }
  705. int diag_process_time_sync_switch_cmd(unsigned char *src_buf, int src_len,
  706. unsigned char *dest_buf, int dest_len)
  707. {
  708. uint8_t peripheral, status = 0;
  709. struct diag_cmd_time_sync_switch_req_t *req = NULL;
  710. struct diag_cmd_time_sync_switch_rsp_t rsp;
  711. struct diag_ctrl_msg_time_sync time_sync_msg;
  712. int msg_size = sizeof(struct diag_ctrl_msg_time_sync);
  713. int err = 0, write_len = 0;
  714. if (!src_buf || !dest_buf || src_len <= 0 || dest_len <= 0) {
  715. pr_err("diag: Invalid input in %s, src_buf: %pK, src_len: %d, dest_buf: %pK, dest_len: %d",
  716. __func__, src_buf, src_len, dest_buf, dest_len);
  717. return -EINVAL;
  718. }
  719. req = (struct diag_cmd_time_sync_switch_req_t *)src_buf;
  720. rsp.header.cmd_code = req->header.cmd_code;
  721. rsp.header.subsys_id = req->header.subsys_id;
  722. rsp.header.subsys_cmd_code = req->header.subsys_cmd_code;
  723. rsp.version = req->version;
  724. rsp.time_api = req->time_api;
  725. if ((req->version > 1) || (req->time_api > 1) ||
  726. (req->persist_time > 0)) {
  727. dest_buf[0] = BAD_PARAM_RESPONSE_MESSAGE;
  728. rsp.time_api_status = 0;
  729. rsp.persist_time_status = PERSIST_TIME_NOT_SUPPORTED;
  730. memcpy(dest_buf + 1, &rsp, sizeof(rsp));
  731. write_len = sizeof(rsp) + 1;
  732. timestamp_switch = 0;
  733. return write_len;
  734. }
  735. time_sync_msg.ctrl_pkt_id = DIAG_CTRL_MSG_TIME_SYNC_PKT;
  736. time_sync_msg.ctrl_pkt_data_len = 5;
  737. time_sync_msg.version = 1;
  738. time_sync_msg.time_api = req->time_api;
  739. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  740. err = diagfwd_write(peripheral, TYPE_CNTL, &time_sync_msg,
  741. msg_size);
  742. if (err && err != -ENODEV) {
  743. pr_err("diag: In %s, unable to write to peripheral: %d, type: %d, len: %d, err: %d\n",
  744. __func__, peripheral, TYPE_CNTL,
  745. msg_size, err);
  746. status |= (1 << peripheral);
  747. }
  748. }
  749. driver->time_sync_enabled = 1;
  750. driver->uses_time_api = req->time_api;
  751. switch (req->time_api) {
  752. case 0:
  753. timestamp_switch = 0;
  754. break;
  755. case 1:
  756. timestamp_switch = 1;
  757. break;
  758. default:
  759. timestamp_switch = 0;
  760. break;
  761. }
  762. rsp.time_api_status = status;
  763. rsp.persist_time_status = PERSIST_TIME_NOT_SUPPORTED;
  764. memcpy(dest_buf, &rsp, sizeof(rsp));
  765. write_len = sizeof(rsp);
  766. return write_len;
  767. }
  768. int diag_cmd_log_on_demand(unsigned char *src_buf, int src_len,
  769. unsigned char *dest_buf, int dest_len)
  770. {
  771. int write_len = 0;
  772. struct diag_log_on_demand_rsp_t header;
  773. if (!driver->diagfwd_cntl[PERIPHERAL_MODEM] ||
  774. !driver->diagfwd_cntl[PERIPHERAL_MODEM]->ch_open ||
  775. !driver->log_on_demand_support)
  776. return 0;
  777. if (!src_buf || !dest_buf || src_len <= 0 || dest_len <= 0) {
  778. pr_err("diag: Invalid input in %s, src_buf: %pK, src_len: %d, dest_buf: %pK, dest_len: %d",
  779. __func__, src_buf, src_len, dest_buf, dest_len);
  780. return -EINVAL;
  781. }
  782. header.cmd_code = DIAG_CMD_LOG_ON_DMND;
  783. header.log_code = *(uint16_t *)(src_buf + 1);
  784. header.status = 1;
  785. memcpy(dest_buf, &header, sizeof(struct diag_log_on_demand_rsp_t));
  786. write_len += sizeof(struct diag_log_on_demand_rsp_t);
  787. return write_len;
  788. }
  789. int diag_cmd_get_mobile_id(unsigned char *src_buf, int src_len,
  790. unsigned char *dest_buf, int dest_len)
  791. {
  792. int write_len = 0;
  793. struct diag_pkt_header_t *header = NULL;
  794. struct diag_cmd_ext_mobile_rsp_t rsp;
  795. if (!src_buf || src_len != sizeof(*header) || !dest_buf ||
  796. dest_len < sizeof(rsp))
  797. return -EIO;
  798. header = (struct diag_pkt_header_t *)src_buf;
  799. rsp.header.cmd_code = header->cmd_code;
  800. rsp.header.subsys_id = header->subsys_id;
  801. rsp.header.subsys_cmd_code = header->subsys_cmd_code;
  802. rsp.version = 2;
  803. rsp.padding[0] = 0;
  804. rsp.padding[1] = 0;
  805. rsp.padding[2] = 0;
  806. rsp.family = 0;
  807. rsp.chip_id = (uint32_t)socinfo_get_id();
  808. memcpy(dest_buf, &rsp, sizeof(rsp));
  809. write_len += sizeof(rsp);
  810. return write_len;
  811. }
  812. int diag_check_common_cmd(struct diag_pkt_header_t *header)
  813. {
  814. int i;
  815. if (!header)
  816. return -EIO;
  817. for (i = 0; i < DIAG_NUM_COMMON_CMD; i++) {
  818. if (header->cmd_code == common_cmds[i])
  819. return 1;
  820. }
  821. return 0;
  822. }
  823. static int diag_cmd_chk_stats(unsigned char *src_buf, int src_len,
  824. unsigned char *dest_buf, int dest_len)
  825. {
  826. int payload = 0;
  827. int write_len = 0;
  828. struct diag_pkt_header_t *header = NULL;
  829. struct diag_cmd_stats_rsp_t rsp;
  830. if (!src_buf || src_len < sizeof(struct diag_pkt_header_t) ||
  831. !dest_buf || dest_len < sizeof(rsp))
  832. return -EINVAL;
  833. header = (struct diag_pkt_header_t *)src_buf;
  834. if (header->cmd_code != DIAG_CMD_DIAG_SUBSYS ||
  835. header->subsys_id != DIAG_SS_DIAG)
  836. return -EINVAL;
  837. switch (header->subsys_cmd_code) {
  838. case DIAG_CMD_OP_GET_MSG_ALLOC:
  839. payload = driver->msg_stats.alloc_count;
  840. break;
  841. case DIAG_CMD_OP_GET_MSG_DROP:
  842. payload = driver->msg_stats.drop_count;
  843. break;
  844. case DIAG_CMD_OP_RESET_MSG_STATS:
  845. diag_record_stats(DATA_TYPE_F3, PKT_RESET);
  846. break;
  847. case DIAG_CMD_OP_GET_LOG_ALLOC:
  848. payload = driver->log_stats.alloc_count;
  849. break;
  850. case DIAG_CMD_OP_GET_LOG_DROP:
  851. payload = driver->log_stats.drop_count;
  852. break;
  853. case DIAG_CMD_OP_RESET_LOG_STATS:
  854. diag_record_stats(DATA_TYPE_LOG, PKT_RESET);
  855. break;
  856. case DIAG_CMD_OP_GET_EVENT_ALLOC:
  857. payload = driver->event_stats.alloc_count;
  858. break;
  859. case DIAG_CMD_OP_GET_EVENT_DROP:
  860. payload = driver->event_stats.drop_count;
  861. break;
  862. case DIAG_CMD_OP_RESET_EVENT_STATS:
  863. diag_record_stats(DATA_TYPE_EVENT, PKT_RESET);
  864. break;
  865. default:
  866. return -EINVAL;
  867. }
  868. memcpy(&rsp.header, header, sizeof(struct diag_pkt_header_t));
  869. rsp.payload = payload;
  870. write_len = sizeof(rsp);
  871. memcpy(dest_buf, &rsp, sizeof(rsp));
  872. return write_len;
  873. }
  874. static int diag_cmd_disable_hdlc(unsigned char *src_buf, int src_len,
  875. unsigned char *dest_buf, int dest_len)
  876. {
  877. struct diag_pkt_header_t *header = NULL;
  878. struct diag_cmd_hdlc_disable_rsp_t rsp;
  879. int write_len = 0;
  880. if (!src_buf || src_len < sizeof(*header) ||
  881. !dest_buf || dest_len < sizeof(rsp)) {
  882. return -EIO;
  883. }
  884. header = (struct diag_pkt_header_t *)src_buf;
  885. if (header->cmd_code != DIAG_CMD_DIAG_SUBSYS ||
  886. header->subsys_id != DIAG_SS_DIAG ||
  887. header->subsys_cmd_code != DIAG_CMD_OP_HDLC_DISABLE) {
  888. return -EINVAL;
  889. }
  890. memcpy(&rsp.header, header, sizeof(struct diag_pkt_header_t));
  891. rsp.framing_version = 1;
  892. rsp.result = 0;
  893. write_len = sizeof(rsp);
  894. memcpy(dest_buf, &rsp, sizeof(rsp));
  895. return write_len;
  896. }
  897. void diag_send_error_rsp(unsigned char *buf, int len,
  898. int pid)
  899. {
  900. /* -1 to accommodate the first byte 0x13 */
  901. if (len > (DIAG_MAX_RSP_SIZE - 1)) {
  902. pr_err("diag: cannot send err rsp, huge length: %d\n", len);
  903. return;
  904. }
  905. *(uint8_t *)driver->apps_rsp_buf = DIAG_CMD_ERROR;
  906. memcpy((driver->apps_rsp_buf + sizeof(uint8_t)), buf, len);
  907. diag_send_rsp(driver->apps_rsp_buf, len + 1, pid);
  908. }
  909. int diag_process_apps_pkt(unsigned char *buf, int len, int pid)
  910. {
  911. int i, p_mask = 0;
  912. int mask_ret, peripheral = -EINVAL;
  913. int write_len = 0;
  914. unsigned char *temp = NULL;
  915. struct diag_cmd_reg_entry_t entry;
  916. struct diag_cmd_reg_entry_t *temp_entry = NULL;
  917. struct diag_cmd_reg_t *reg_item = NULL;
  918. struct diagfwd_info *fwd_info = NULL;
  919. uint32_t pd_mask = 0;
  920. struct diag_md_session_t *info = NULL;
  921. if (!buf)
  922. return -EIO;
  923. /* Check if the command is a supported mask command */
  924. mask_ret = diag_process_apps_masks(buf, len, pid);
  925. if (mask_ret > 0) {
  926. diag_send_rsp(driver->apps_rsp_buf, mask_ret, pid);
  927. return 0;
  928. }
  929. temp = buf;
  930. entry.cmd_code = (uint16_t)(*(uint8_t *)temp);
  931. temp += sizeof(uint8_t);
  932. entry.subsys_id = (uint16_t)(*(uint8_t *)temp);
  933. temp += sizeof(uint8_t);
  934. entry.cmd_code_hi = (uint16_t)(*(uint16_t *)temp);
  935. entry.cmd_code_lo = (uint16_t)(*(uint16_t *)temp);
  936. temp += sizeof(uint16_t);
  937. pr_debug("diag: In %s, received cmd %02x %02x %02x\n",
  938. __func__, entry.cmd_code, entry.subsys_id, entry.cmd_code_hi);
  939. if (*buf == DIAG_CMD_LOG_ON_DMND && driver->log_on_demand_support &&
  940. driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask) {
  941. write_len = diag_cmd_log_on_demand(buf, len,
  942. driver->apps_rsp_buf,
  943. DIAG_MAX_RSP_SIZE);
  944. if (write_len > 0)
  945. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  946. return 0;
  947. }
  948. mutex_lock(&driver->cmd_reg_mutex);
  949. temp_entry = diag_cmd_search(&entry, ALL_PROC);
  950. if (temp_entry) {
  951. reg_item = container_of(temp_entry, struct diag_cmd_reg_t,
  952. entry);
  953. mutex_lock(&driver->md_session_lock);
  954. info = diag_md_session_get_pid(pid);
  955. if (info) {
  956. p_mask = info->peripheral_mask;
  957. mutex_unlock(&driver->md_session_lock);
  958. MD_PERIPHERAL_PD_MASK(TYPE_CMD, reg_item->proc,
  959. pd_mask);
  960. if ((MD_PERIPHERAL_MASK(reg_item->proc) &
  961. p_mask) || (pd_mask & p_mask))
  962. write_len = diag_send_data(reg_item, buf, len);
  963. } else {
  964. mutex_unlock(&driver->md_session_lock);
  965. if (MD_PERIPHERAL_MASK(reg_item->proc) &
  966. driver->logging_mask) {
  967. mutex_unlock(&driver->cmd_reg_mutex);
  968. diag_send_error_rsp(buf, len, pid);
  969. return write_len;
  970. }
  971. else
  972. write_len = diag_send_data(reg_item, buf, len);
  973. }
  974. mutex_unlock(&driver->cmd_reg_mutex);
  975. return write_len;
  976. }
  977. mutex_unlock(&driver->cmd_reg_mutex);
  978. #if defined(CONFIG_DIAG_OVER_USB)
  979. /* Check for the command/respond msg for the maximum packet length */
  980. if ((*buf == 0x4b) && (*(buf+1) == 0x12) &&
  981. (*(uint16_t *)(buf+2) == 0x0055)) {
  982. for (i = 0; i < 4; i++)
  983. *(driver->apps_rsp_buf+i) = *(buf+i);
  984. *(uint32_t *)(driver->apps_rsp_buf+4) = DIAG_MAX_REQ_SIZE;
  985. diag_send_rsp(driver->apps_rsp_buf, 8, pid);
  986. return 0;
  987. } else if ((*buf == 0x4b) && (*(buf+1) == 0x12) &&
  988. (*(uint16_t *)(buf+2) == DIAG_DIAG_STM)) {
  989. len = diag_process_stm_cmd(buf, driver->apps_rsp_buf);
  990. if (len > 0) {
  991. diag_send_rsp(driver->apps_rsp_buf, len, pid);
  992. return 0;
  993. }
  994. return len;
  995. }
  996. /* Check for time sync query command */
  997. else if ((*buf == DIAG_CMD_DIAG_SUBSYS) &&
  998. (*(buf+1) == DIAG_SS_DIAG) &&
  999. (*(uint16_t *)(buf+2) == DIAG_GET_TIME_API)) {
  1000. write_len = diag_process_time_sync_query_cmd(buf, len,
  1001. driver->apps_rsp_buf,
  1002. DIAG_MAX_RSP_SIZE);
  1003. if (write_len > 0)
  1004. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1005. return 0;
  1006. }
  1007. /* Check for time sync switch command */
  1008. else if ((*buf == DIAG_CMD_DIAG_SUBSYS) &&
  1009. (*(buf+1) == DIAG_SS_DIAG) &&
  1010. (*(uint16_t *)(buf+2) == DIAG_SET_TIME_API)) {
  1011. write_len = diag_process_time_sync_switch_cmd(buf, len,
  1012. driver->apps_rsp_buf,
  1013. DIAG_MAX_RSP_SIZE);
  1014. if (write_len > 0)
  1015. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1016. return 0;
  1017. }
  1018. /* Check for diag id command */
  1019. else if ((*buf == DIAG_CMD_DIAG_SUBSYS) &&
  1020. (*(buf+1) == DIAG_SS_DIAG) &&
  1021. (*(uint16_t *)(buf+2) == DIAG_GET_DIAG_ID)) {
  1022. write_len = diag_process_diag_id_query_cmd(buf, len,
  1023. driver->apps_rsp_buf,
  1024. DIAG_MAX_RSP_SIZE);
  1025. if (write_len > 0)
  1026. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1027. return 0;
  1028. }
  1029. /* Check for download command */
  1030. else if ((chk_apps_master()) && (*buf == 0x3A)) {
  1031. /* send response back */
  1032. driver->apps_rsp_buf[0] = *buf;
  1033. diag_send_rsp(driver->apps_rsp_buf, 1, pid);
  1034. msleep(5000);
  1035. /* call download API */
  1036. msm_set_restart_mode(RESTART_DLOAD);
  1037. pr_crit("diag: download mode set, Rebooting SoC..\n");
  1038. kernel_restart(NULL);
  1039. /* Not required, represents that command isn't sent to modem */
  1040. return 0;
  1041. }
  1042. /* Check for polling for Apps only DIAG */
  1043. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1044. (*(buf+2) == 0x03)) {
  1045. /* If no one has registered for polling */
  1046. if (chk_polling_response()) {
  1047. /* Respond to polling for Apps only DIAG */
  1048. for (i = 0; i < 3; i++)
  1049. driver->apps_rsp_buf[i] = *(buf+i);
  1050. for (i = 0; i < 13; i++)
  1051. driver->apps_rsp_buf[i+3] = 0;
  1052. diag_send_rsp(driver->apps_rsp_buf, 16, pid);
  1053. return 0;
  1054. }
  1055. }
  1056. /* Return the Delayed Response Wrap Status */
  1057. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1058. (*(buf+2) == 0x04) && (*(buf+3) == 0x0)) {
  1059. memcpy(driver->apps_rsp_buf, buf, 4);
  1060. driver->apps_rsp_buf[4] = wrap_enabled;
  1061. diag_send_rsp(driver->apps_rsp_buf, 5, pid);
  1062. return 0;
  1063. }
  1064. /* Wrap the Delayed Rsp ID */
  1065. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1066. (*(buf+2) == 0x05) && (*(buf+3) == 0x0)) {
  1067. wrap_enabled = true;
  1068. memcpy(driver->apps_rsp_buf, buf, 4);
  1069. driver->apps_rsp_buf[4] = wrap_count;
  1070. diag_send_rsp(driver->apps_rsp_buf, 6, pid);
  1071. return 0;
  1072. }
  1073. /* Mobile ID Rsp */
  1074. else if ((*buf == DIAG_CMD_DIAG_SUBSYS) &&
  1075. (*(buf+1) == DIAG_SS_PARAMS) &&
  1076. (*(buf+2) == DIAG_EXT_MOBILE_ID) && (*(buf+3) == 0x0)) {
  1077. write_len = diag_cmd_get_mobile_id(buf, len,
  1078. driver->apps_rsp_buf,
  1079. DIAG_MAX_RSP_SIZE);
  1080. if (write_len > 0) {
  1081. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1082. return 0;
  1083. }
  1084. }
  1085. /*
  1086. * If the apps processor is master and no other
  1087. * processor has registered for polling command.
  1088. * If modem is not up and we have not received feature
  1089. * mask update from modem, in that case APPS should
  1090. * respond for 0X7C command
  1091. */
  1092. else if (chk_apps_master() &&
  1093. !(driver->polling_reg_flag) &&
  1094. !(driver->diagfwd_cntl[PERIPHERAL_MODEM]->ch_open) &&
  1095. !(driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask)) {
  1096. /* respond to 0x0 command */
  1097. if (*buf == 0x00) {
  1098. for (i = 0; i < 55; i++)
  1099. driver->apps_rsp_buf[i] = 0;
  1100. diag_send_rsp(driver->apps_rsp_buf, 55, pid);
  1101. return 0;
  1102. }
  1103. /* respond to 0x7c command */
  1104. else if (*buf == 0x7c) {
  1105. driver->apps_rsp_buf[0] = 0x7c;
  1106. for (i = 1; i < 8; i++)
  1107. driver->apps_rsp_buf[i] = 0;
  1108. /* Tools ID for APQ 8060 */
  1109. *(int *)(driver->apps_rsp_buf + 8) =
  1110. chk_config_get_id();
  1111. *(unsigned char *)(driver->apps_rsp_buf + 12) = '\0';
  1112. *(unsigned char *)(driver->apps_rsp_buf + 13) = '\0';
  1113. diag_send_rsp(driver->apps_rsp_buf, 14, pid);
  1114. return 0;
  1115. }
  1116. }
  1117. write_len = diag_cmd_chk_stats(buf, len, driver->apps_rsp_buf,
  1118. DIAG_MAX_RSP_SIZE);
  1119. if (write_len > 0) {
  1120. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1121. return 0;
  1122. }
  1123. write_len = diag_cmd_disable_hdlc(buf, len, driver->apps_rsp_buf,
  1124. DIAG_MAX_RSP_SIZE);
  1125. if (write_len > 0) {
  1126. /*
  1127. * This mutex lock is necessary since we need to drain all the
  1128. * pending buffers from peripherals which may be HDLC encoded
  1129. * before disabling HDLC encoding on Apps processor.
  1130. */
  1131. mutex_lock(&driver->hdlc_disable_mutex);
  1132. diag_send_rsp(driver->apps_rsp_buf, write_len, pid);
  1133. /*
  1134. * Set the value of hdlc_disabled after sending the response to
  1135. * the tools. This is required since the tools is expecting a
  1136. * HDLC encoded response for this request.
  1137. */
  1138. pr_debug("diag: In %s, disabling HDLC encoding\n",
  1139. __func__);
  1140. mutex_lock(&driver->md_session_lock);
  1141. info = diag_md_session_get_pid(pid);
  1142. if (info)
  1143. info->hdlc_disabled = 1;
  1144. else
  1145. driver->hdlc_disabled = 1;
  1146. peripheral =
  1147. diag_md_session_match_pid_peripheral(pid, 0);
  1148. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  1149. if (peripheral > 0 && info) {
  1150. if (peripheral & (1 << i))
  1151. driver->p_hdlc_disabled[i] =
  1152. info->hdlc_disabled;
  1153. else if (!diag_md_session_get_peripheral(i))
  1154. driver->p_hdlc_disabled[i] =
  1155. driver->hdlc_disabled;
  1156. } else {
  1157. if (!diag_md_session_get_peripheral(i))
  1158. driver->p_hdlc_disabled[i] =
  1159. driver->hdlc_disabled;
  1160. }
  1161. }
  1162. mutex_unlock(&driver->md_session_lock);
  1163. diag_update_md_clients(HDLC_SUPPORT_TYPE);
  1164. mutex_unlock(&driver->hdlc_disable_mutex);
  1165. return 0;
  1166. }
  1167. #endif
  1168. /* We have now come to the end of the function. */
  1169. if (chk_apps_only())
  1170. diag_send_error_rsp(buf, len, pid);
  1171. return 0;
  1172. }
  1173. void diag_process_hdlc_pkt(void *data, unsigned int len, int pid)
  1174. {
  1175. int err = 0;
  1176. int ret = 0;
  1177. if (len > DIAG_MAX_HDLC_BUF_SIZE) {
  1178. pr_err("diag: In %s, invalid length: %d\n", __func__, len);
  1179. return;
  1180. }
  1181. mutex_lock(&driver->diag_hdlc_mutex);
  1182. pr_debug("diag: In %s, received packet of length: %d, req_buf_len: %d\n",
  1183. __func__, len, driver->hdlc_buf_len);
  1184. if (driver->hdlc_buf_len >= DIAG_MAX_REQ_SIZE) {
  1185. pr_err("diag: In %s, request length is more than supported len. Dropping packet.\n",
  1186. __func__);
  1187. goto fail;
  1188. }
  1189. hdlc_decode->dest_ptr = driver->hdlc_buf + driver->hdlc_buf_len;
  1190. hdlc_decode->dest_size = DIAG_MAX_HDLC_BUF_SIZE - driver->hdlc_buf_len;
  1191. hdlc_decode->src_ptr = data;
  1192. hdlc_decode->src_size = len;
  1193. hdlc_decode->src_idx = 0;
  1194. hdlc_decode->dest_idx = 0;
  1195. ret = diag_hdlc_decode(hdlc_decode);
  1196. /*
  1197. * driver->hdlc_buf is of size DIAG_MAX_HDLC_BUF_SIZE. But the decoded
  1198. * packet should be within DIAG_MAX_REQ_SIZE.
  1199. */
  1200. if (driver->hdlc_buf_len + hdlc_decode->dest_idx <= DIAG_MAX_REQ_SIZE) {
  1201. driver->hdlc_buf_len += hdlc_decode->dest_idx;
  1202. } else {
  1203. pr_err_ratelimited("diag: In %s, Dropping packet. pkt_size: %d, max: %d\n",
  1204. __func__,
  1205. driver->hdlc_buf_len + hdlc_decode->dest_idx,
  1206. DIAG_MAX_REQ_SIZE);
  1207. goto fail;
  1208. }
  1209. if (ret == HDLC_COMPLETE) {
  1210. err = crc_check(driver->hdlc_buf, driver->hdlc_buf_len);
  1211. if (err) {
  1212. /* CRC check failed. */
  1213. pr_err_ratelimited("diag: In %s, bad CRC. Dropping packet\n",
  1214. __func__);
  1215. goto fail;
  1216. }
  1217. driver->hdlc_buf_len -= HDLC_FOOTER_LEN;
  1218. if (driver->hdlc_buf_len < 1) {
  1219. pr_err_ratelimited("diag: In %s, message is too short, len: %d, dest len: %d\n",
  1220. __func__, driver->hdlc_buf_len,
  1221. hdlc_decode->dest_idx);
  1222. goto fail;
  1223. }
  1224. err = diag_process_apps_pkt(driver->hdlc_buf,
  1225. driver->hdlc_buf_len, pid);
  1226. if (err < 0)
  1227. goto fail;
  1228. } else {
  1229. goto end;
  1230. }
  1231. driver->hdlc_buf_len = 0;
  1232. mutex_unlock(&driver->diag_hdlc_mutex);
  1233. return;
  1234. fail:
  1235. /*
  1236. * Tools needs to get a response in order to start its
  1237. * recovery algorithm. Send an error response if the
  1238. * packet is not in expected format.
  1239. */
  1240. diag_send_error_rsp(driver->hdlc_buf, driver->hdlc_buf_len, pid);
  1241. driver->hdlc_buf_len = 0;
  1242. end:
  1243. mutex_unlock(&driver->diag_hdlc_mutex);
  1244. }
  1245. int diagfwd_mux_open(int id, int mode)
  1246. {
  1247. uint8_t i;
  1248. unsigned long flags;
  1249. switch (mode) {
  1250. case DIAG_USB_MODE:
  1251. driver->usb_connected = 1;
  1252. break;
  1253. case DIAG_MEMORY_DEVICE_MODE:
  1254. break;
  1255. default:
  1256. return -EINVAL;
  1257. }
  1258. if (driver->rsp_buf_busy) {
  1259. /*
  1260. * When a client switches from callback mode to USB mode
  1261. * explicitly, there can be a situation when the last response
  1262. * is not drained to the user space application. Reset the
  1263. * in_busy flag in this case.
  1264. */
  1265. spin_lock_irqsave(&driver->rsp_buf_busy_lock, flags);
  1266. driver->rsp_buf_busy = 0;
  1267. spin_unlock_irqrestore(&driver->rsp_buf_busy_lock, flags);
  1268. }
  1269. for (i = 0; i < NUM_PERIPHERALS; i++) {
  1270. diagfwd_open(i, TYPE_DATA);
  1271. diagfwd_open(i, TYPE_CMD);
  1272. }
  1273. queue_work(driver->diag_real_time_wq, &driver->diag_real_time_work);
  1274. return 0;
  1275. }
  1276. int diagfwd_mux_close(int id, int mode)
  1277. {
  1278. uint8_t i;
  1279. switch (mode) {
  1280. case DIAG_USB_MODE:
  1281. driver->usb_connected = 0;
  1282. break;
  1283. case DIAG_MEMORY_DEVICE_MODE:
  1284. break;
  1285. default:
  1286. return -EINVAL;
  1287. }
  1288. if ((driver->logging_mode == DIAG_MULTI_MODE &&
  1289. driver->md_session_mode == DIAG_MD_NONE) ||
  1290. (driver->md_session_mode == DIAG_MD_PERIPHERAL)) {
  1291. /*
  1292. * This case indicates that the USB is removed
  1293. * but there is a client running in background
  1294. * with Memory Device mode.
  1295. */
  1296. } else {
  1297. /*
  1298. * With sysfs parameter to clear masks set,
  1299. * peripheral masks are cleared on ODL exit and
  1300. * USB disconnection and buffers are not marked busy.
  1301. * This enables read and drop of stale packets.
  1302. *
  1303. * With sysfs parameter to clear masks cleared,
  1304. * masks are not cleared and buffers are to be marked
  1305. * busy to ensure traffic generated by peripheral
  1306. * are not read
  1307. */
  1308. if (!(diag_mask_param())) {
  1309. for (i = 0; i < NUM_PERIPHERALS; i++) {
  1310. diagfwd_close(i, TYPE_DATA);
  1311. diagfwd_close(i, TYPE_CMD);
  1312. }
  1313. }
  1314. /* Re enable HDLC encoding */
  1315. pr_debug("diag: In %s, re-enabling HDLC encoding\n",
  1316. __func__);
  1317. mutex_lock(&driver->hdlc_disable_mutex);
  1318. if (driver->md_session_mode == DIAG_MD_NONE) {
  1319. driver->hdlc_disabled = 0;
  1320. /*
  1321. * HDLC encoding is re-enabled when
  1322. * there is logical/physical disconnection of diag
  1323. * to USB.
  1324. */
  1325. for (i = 0; i < NUM_MD_SESSIONS; i++)
  1326. driver->p_hdlc_disabled[i] =
  1327. driver->hdlc_disabled;
  1328. }
  1329. mutex_unlock(&driver->hdlc_disable_mutex);
  1330. queue_work(driver->diag_wq,
  1331. &(driver->update_user_clients));
  1332. }
  1333. queue_work(driver->diag_real_time_wq,
  1334. &driver->diag_real_time_work);
  1335. return 0;
  1336. }
  1337. static uint8_t hdlc_reset;
  1338. static void hdlc_reset_timer_start(int pid)
  1339. {
  1340. struct diag_md_session_t *info = NULL;
  1341. mutex_lock(&driver->md_session_lock);
  1342. info = diag_md_session_get_pid(pid);
  1343. if (!hdlc_timer_in_progress) {
  1344. hdlc_timer_in_progress = 1;
  1345. if (info)
  1346. mod_timer(&info->hdlc_reset_timer,
  1347. jiffies + msecs_to_jiffies(200));
  1348. else
  1349. mod_timer(&driver->hdlc_reset_timer,
  1350. jiffies + msecs_to_jiffies(200));
  1351. }
  1352. mutex_unlock(&driver->md_session_lock);
  1353. }
  1354. /*
  1355. * diag_timer_work_fn
  1356. * Queued in workqueue to protect md_session_info structure
  1357. *
  1358. * Update hdlc_disabled for each peripheral
  1359. * which are not in any md_session_info.
  1360. *
  1361. */
  1362. static void diag_timer_work_fn(struct work_struct *work)
  1363. {
  1364. int i = 0;
  1365. struct diag_md_session_t *session_info = NULL;
  1366. mutex_lock(&driver->hdlc_disable_mutex);
  1367. driver->hdlc_disabled = 0;
  1368. mutex_lock(&driver->md_session_lock);
  1369. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  1370. session_info = diag_md_session_get_peripheral(i);
  1371. if (!session_info)
  1372. driver->p_hdlc_disabled[i] =
  1373. driver->hdlc_disabled;
  1374. }
  1375. mutex_unlock(&driver->md_session_lock);
  1376. mutex_unlock(&driver->hdlc_disable_mutex);
  1377. }
  1378. /*
  1379. * diag_md_timer_work_fn
  1380. * Queued in workqueue to protect md_session_info structure
  1381. *
  1382. * Update hdlc_disabled for each peripheral
  1383. * which are in any md_session_info
  1384. *
  1385. */
  1386. static void diag_md_timer_work_fn(struct work_struct *work)
  1387. {
  1388. int peripheral = -EINVAL, i = 0;
  1389. struct diag_md_session_t *session_info = NULL;
  1390. struct diag_md_hdlc_reset_work *hdlc_work = container_of(work,
  1391. struct diag_md_hdlc_reset_work, work);
  1392. if (!hdlc_work)
  1393. return;
  1394. mutex_lock(&driver->hdlc_disable_mutex);
  1395. mutex_lock(&driver->md_session_lock);
  1396. session_info = diag_md_session_get_pid(hdlc_work->pid);
  1397. if (session_info)
  1398. session_info->hdlc_disabled = 0;
  1399. peripheral =
  1400. diag_md_session_match_pid_peripheral(hdlc_work->pid, 0);
  1401. if (peripheral > 0 && session_info) {
  1402. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  1403. if (peripheral & (1 << i))
  1404. driver->p_hdlc_disabled[i] =
  1405. session_info->hdlc_disabled;
  1406. }
  1407. }
  1408. kfree(hdlc_work);
  1409. mutex_unlock(&driver->md_session_lock);
  1410. mutex_unlock(&driver->hdlc_disable_mutex);
  1411. }
  1412. static void hdlc_reset_timer_func(unsigned long data)
  1413. {
  1414. pr_debug("diag: In %s, re-enabling HDLC encoding\n",
  1415. __func__);
  1416. if (hdlc_reset) {
  1417. queue_work(driver->diag_wq, &(driver->diag_hdlc_reset_work));
  1418. queue_work(driver->diag_wq, &(driver->update_user_clients));
  1419. }
  1420. hdlc_timer_in_progress = 0;
  1421. }
  1422. void diag_md_hdlc_reset_timer_func(unsigned long pid)
  1423. {
  1424. struct diag_md_hdlc_reset_work *hdlc_reset_work = NULL;
  1425. pr_debug("diag: In %s, re-enabling HDLC encoding\n",
  1426. __func__);
  1427. hdlc_reset_work = kmalloc(sizeof(*hdlc_reset_work), GFP_ATOMIC);
  1428. if (!hdlc_reset_work) {
  1429. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  1430. "diag: Could not allocate hdlc_reset_work\n");
  1431. hdlc_timer_in_progress = 0;
  1432. return;
  1433. }
  1434. if (hdlc_reset) {
  1435. hdlc_reset_work->pid = pid;
  1436. INIT_WORK(&hdlc_reset_work->work, diag_md_timer_work_fn);
  1437. queue_work(driver->diag_wq, &(hdlc_reset_work->work));
  1438. queue_work(driver->diag_wq, &(driver->update_md_clients));
  1439. }
  1440. hdlc_timer_in_progress = 0;
  1441. }
  1442. static void diag_hdlc_start_recovery(unsigned char *buf, int len,
  1443. int pid)
  1444. {
  1445. int i, peripheral = -EINVAL;
  1446. static uint32_t bad_byte_counter;
  1447. unsigned char *start_ptr = NULL;
  1448. struct diag_pkt_frame_t *actual_pkt = NULL;
  1449. struct diag_md_session_t *info = NULL;
  1450. hdlc_reset = 1;
  1451. hdlc_reset_timer_start(pid);
  1452. actual_pkt = (struct diag_pkt_frame_t *)buf;
  1453. for (i = 0; i < len; i++) {
  1454. if (actual_pkt->start == CONTROL_CHAR &&
  1455. actual_pkt->version == 1 &&
  1456. actual_pkt->length < len &&
  1457. (*(uint8_t *)(buf +
  1458. sizeof(struct diag_pkt_frame_t) +
  1459. actual_pkt->length) == CONTROL_CHAR)) {
  1460. start_ptr = &buf[i];
  1461. break;
  1462. }
  1463. bad_byte_counter++;
  1464. if (bad_byte_counter > (DIAG_MAX_REQ_SIZE +
  1465. sizeof(struct diag_pkt_frame_t) + 1)) {
  1466. bad_byte_counter = 0;
  1467. pr_err("diag: In %s, re-enabling HDLC encoding\n",
  1468. __func__);
  1469. mutex_lock(&driver->hdlc_disable_mutex);
  1470. mutex_lock(&driver->md_session_lock);
  1471. info = diag_md_session_get_pid(pid);
  1472. if (info)
  1473. info->hdlc_disabled = 0;
  1474. else
  1475. driver->hdlc_disabled = 0;
  1476. peripheral =
  1477. diag_md_session_match_pid_peripheral(pid, 0);
  1478. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  1479. if (peripheral > 0 && info) {
  1480. if (peripheral & (1 << i))
  1481. driver->p_hdlc_disabled[i] =
  1482. info->hdlc_disabled;
  1483. else if (
  1484. !diag_md_session_get_peripheral(i))
  1485. driver->p_hdlc_disabled[i] =
  1486. driver->hdlc_disabled;
  1487. } else {
  1488. if (!diag_md_session_get_peripheral(i))
  1489. driver->p_hdlc_disabled[i] =
  1490. driver->hdlc_disabled;
  1491. }
  1492. }
  1493. mutex_unlock(&driver->md_session_lock);
  1494. mutex_unlock(&driver->hdlc_disable_mutex);
  1495. diag_update_md_clients(HDLC_SUPPORT_TYPE);
  1496. return;
  1497. }
  1498. }
  1499. if (start_ptr) {
  1500. /* Discard any partial packet reads */
  1501. mutex_lock(&driver->hdlc_recovery_mutex);
  1502. driver->incoming_pkt.processing = 0;
  1503. mutex_unlock(&driver->hdlc_recovery_mutex);
  1504. diag_process_non_hdlc_pkt(start_ptr, len - i, pid);
  1505. }
  1506. }
  1507. void diag_process_non_hdlc_pkt(unsigned char *buf, int len, int pid)
  1508. {
  1509. int err = 0;
  1510. uint16_t pkt_len = 0;
  1511. uint32_t read_bytes = 0;
  1512. const uint32_t header_len = sizeof(struct diag_pkt_frame_t);
  1513. struct diag_pkt_frame_t *actual_pkt = NULL;
  1514. unsigned char *data_ptr = NULL;
  1515. struct diag_partial_pkt_t *partial_pkt = NULL;
  1516. mutex_lock(&driver->hdlc_recovery_mutex);
  1517. if (!buf || len <= 0) {
  1518. mutex_unlock(&driver->hdlc_recovery_mutex);
  1519. return;
  1520. }
  1521. partial_pkt = &driver->incoming_pkt;
  1522. if (!partial_pkt->processing) {
  1523. mutex_unlock(&driver->hdlc_recovery_mutex);
  1524. goto start;
  1525. }
  1526. if (partial_pkt->remaining > len) {
  1527. if ((partial_pkt->read_len + len) > partial_pkt->capacity) {
  1528. pr_err("diag: Invalid length %d, %d received in %s\n",
  1529. partial_pkt->read_len, len, __func__);
  1530. mutex_unlock(&driver->hdlc_recovery_mutex);
  1531. goto end;
  1532. }
  1533. memcpy(partial_pkt->data + partial_pkt->read_len, buf, len);
  1534. read_bytes += len;
  1535. buf += read_bytes;
  1536. partial_pkt->read_len += len;
  1537. partial_pkt->remaining -= len;
  1538. } else {
  1539. if ((partial_pkt->read_len + partial_pkt->remaining) >
  1540. partial_pkt->capacity) {
  1541. pr_err("diag: Invalid length during partial read %d, %d received in %s\n",
  1542. partial_pkt->read_len,
  1543. partial_pkt->remaining, __func__);
  1544. mutex_unlock(&driver->hdlc_recovery_mutex);
  1545. goto end;
  1546. }
  1547. memcpy(partial_pkt->data + partial_pkt->read_len, buf,
  1548. partial_pkt->remaining);
  1549. read_bytes += partial_pkt->remaining;
  1550. buf += read_bytes;
  1551. partial_pkt->read_len += partial_pkt->remaining;
  1552. partial_pkt->remaining = 0;
  1553. }
  1554. if (partial_pkt->remaining == 0) {
  1555. actual_pkt = (struct diag_pkt_frame_t *)(partial_pkt->data);
  1556. data_ptr = partial_pkt->data + header_len;
  1557. if (*(uint8_t *)(data_ptr + actual_pkt->length) !=
  1558. CONTROL_CHAR) {
  1559. mutex_unlock(&driver->hdlc_recovery_mutex);
  1560. diag_hdlc_start_recovery(buf, len, pid);
  1561. mutex_lock(&driver->hdlc_recovery_mutex);
  1562. }
  1563. err = diag_process_apps_pkt(data_ptr,
  1564. actual_pkt->length, pid);
  1565. if (err) {
  1566. pr_err("diag: In %s, unable to process incoming data packet, err: %d\n",
  1567. __func__, err);
  1568. mutex_unlock(&driver->hdlc_recovery_mutex);
  1569. goto end;
  1570. }
  1571. partial_pkt->read_len = 0;
  1572. partial_pkt->total_len = 0;
  1573. partial_pkt->processing = 0;
  1574. mutex_unlock(&driver->hdlc_recovery_mutex);
  1575. goto start;
  1576. }
  1577. mutex_unlock(&driver->hdlc_recovery_mutex);
  1578. goto end;
  1579. start:
  1580. while (read_bytes < len) {
  1581. actual_pkt = (struct diag_pkt_frame_t *)buf;
  1582. pkt_len = actual_pkt->length;
  1583. if (actual_pkt->start != CONTROL_CHAR) {
  1584. diag_hdlc_start_recovery(buf, len, pid);
  1585. diag_send_error_rsp(buf, len, pid);
  1586. goto end;
  1587. }
  1588. mutex_lock(&driver->hdlc_recovery_mutex);
  1589. if (pkt_len + header_len > partial_pkt->capacity) {
  1590. pr_err("diag: In %s, incoming data is too large for the request buffer %d\n",
  1591. __func__, pkt_len);
  1592. mutex_unlock(&driver->hdlc_recovery_mutex);
  1593. diag_hdlc_start_recovery(buf, len, pid);
  1594. break;
  1595. }
  1596. if ((pkt_len + header_len) > (len - read_bytes)) {
  1597. partial_pkt->read_len = len - read_bytes;
  1598. partial_pkt->total_len = pkt_len + header_len;
  1599. partial_pkt->remaining = partial_pkt->total_len -
  1600. partial_pkt->read_len;
  1601. partial_pkt->processing = 1;
  1602. memcpy(partial_pkt->data, buf, partial_pkt->read_len);
  1603. mutex_unlock(&driver->hdlc_recovery_mutex);
  1604. break;
  1605. }
  1606. data_ptr = buf + header_len;
  1607. if (*(uint8_t *)(data_ptr + actual_pkt->length) !=
  1608. CONTROL_CHAR) {
  1609. mutex_unlock(&driver->hdlc_recovery_mutex);
  1610. diag_hdlc_start_recovery(buf, len, pid);
  1611. mutex_lock(&driver->hdlc_recovery_mutex);
  1612. }
  1613. else
  1614. hdlc_reset = 0;
  1615. err = diag_process_apps_pkt(data_ptr,
  1616. actual_pkt->length, pid);
  1617. if (err) {
  1618. mutex_unlock(&driver->hdlc_recovery_mutex);
  1619. break;
  1620. }
  1621. read_bytes += header_len + pkt_len + 1;
  1622. buf += header_len + pkt_len + 1; /* advance to next pkt */
  1623. mutex_unlock(&driver->hdlc_recovery_mutex);
  1624. }
  1625. end:
  1626. return;
  1627. }
  1628. static int diagfwd_mux_read_done(unsigned char *buf, int len, int ctxt)
  1629. {
  1630. if (!buf || len <= 0)
  1631. return -EINVAL;
  1632. if (!driver->hdlc_disabled)
  1633. diag_process_hdlc_pkt(buf, len, 0);
  1634. else
  1635. diag_process_non_hdlc_pkt(buf, len, 0);
  1636. diag_mux_queue_read(ctxt);
  1637. return 0;
  1638. }
  1639. static int diagfwd_mux_write_done(unsigned char *buf, int len, int buf_ctxt,
  1640. int ctxt)
  1641. {
  1642. unsigned long flags;
  1643. int peripheral = -1;
  1644. int type = -1;
  1645. int num = -1;
  1646. if (!buf || len < 0)
  1647. return -EINVAL;
  1648. peripheral = GET_BUF_PERIPHERAL(buf_ctxt);
  1649. type = GET_BUF_TYPE(buf_ctxt);
  1650. num = GET_BUF_NUM(buf_ctxt);
  1651. switch (type) {
  1652. case TYPE_DATA:
  1653. if (peripheral >= 0 && peripheral < NUM_PERIPHERALS) {
  1654. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  1655. "Marking buffer as free after write done p: %d, t: %d, buf_num: %d\n",
  1656. peripheral, type, num);
  1657. diagfwd_write_done(peripheral, type, num);
  1658. diag_ws_on_copy(DIAG_WS_MUX);
  1659. } else if (peripheral == APPS_DATA) {
  1660. diagmem_free(driver, (unsigned char *)buf,
  1661. POOL_TYPE_HDLC);
  1662. buf = NULL;
  1663. } else {
  1664. pr_err_ratelimited("diag: Invalid peripheral %d in %s, type: %d\n",
  1665. peripheral, __func__, type);
  1666. }
  1667. break;
  1668. case TYPE_CMD:
  1669. if (peripheral >= 0 && peripheral < NUM_PERIPHERALS) {
  1670. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  1671. "Marking buffer as free after write done p: %d, t: %d, buf_num: %d\n",
  1672. peripheral, type, num);
  1673. diagfwd_write_done(peripheral, type, num);
  1674. }
  1675. if (peripheral == APPS_DATA ||
  1676. ctxt == DIAG_MEMORY_DEVICE_MODE) {
  1677. spin_lock_irqsave(&driver->rsp_buf_busy_lock, flags);
  1678. driver->rsp_buf_busy = 0;
  1679. driver->encoded_rsp_len = 0;
  1680. spin_unlock_irqrestore(&driver->rsp_buf_busy_lock,
  1681. flags);
  1682. }
  1683. break;
  1684. default:
  1685. pr_err_ratelimited("diag: Incorrect data type %d, buf_ctxt: %d in %s\n",
  1686. type, buf_ctxt, __func__);
  1687. break;
  1688. }
  1689. return 0;
  1690. }
  1691. static struct diag_mux_ops diagfwd_mux_ops = {
  1692. .open = diagfwd_mux_open,
  1693. .close = diagfwd_mux_close,
  1694. .read_done = diagfwd_mux_read_done,
  1695. .write_done = diagfwd_mux_write_done
  1696. };
  1697. int diagfwd_init(void)
  1698. {
  1699. int ret;
  1700. int i;
  1701. wrap_enabled = 0;
  1702. wrap_count = 0;
  1703. driver->use_device_tree = has_device_tree();
  1704. for (i = 0; i < DIAG_NUM_PROC; i++)
  1705. driver->real_time_mode[i] = 1;
  1706. driver->supports_separate_cmdrsp = 1;
  1707. driver->supports_apps_hdlc_encoding = 1;
  1708. driver->supports_apps_header_untagging = 1;
  1709. driver->supports_pd_buffering = 1;
  1710. for (i = 0; i < NUM_PERIPHERALS; i++)
  1711. driver->peripheral_untag[i] = 0;
  1712. mutex_init(&driver->diag_hdlc_mutex);
  1713. mutex_init(&driver->diag_cntl_mutex);
  1714. mutex_init(&driver->mode_lock);
  1715. driver->encoded_rsp_buf = kzalloc(DIAG_MAX_HDLC_BUF_SIZE +
  1716. APF_DIAG_PADDING, GFP_KERNEL);
  1717. if (!driver->encoded_rsp_buf)
  1718. goto err;
  1719. kmemleak_not_leak(driver->encoded_rsp_buf);
  1720. hdlc_decode = kzalloc(sizeof(struct diag_hdlc_decode_type),
  1721. GFP_KERNEL);
  1722. if (!hdlc_decode)
  1723. goto err;
  1724. setup_timer(&driver->hdlc_reset_timer, hdlc_reset_timer_func, 0);
  1725. kmemleak_not_leak(hdlc_decode);
  1726. driver->encoded_rsp_len = 0;
  1727. driver->rsp_buf_busy = 0;
  1728. spin_lock_init(&driver->rsp_buf_busy_lock);
  1729. driver->user_space_data_busy = 0;
  1730. driver->hdlc_buf_len = 0;
  1731. INIT_LIST_HEAD(&driver->cmd_reg_list);
  1732. driver->cmd_reg_count = 0;
  1733. mutex_init(&driver->cmd_reg_mutex);
  1734. INIT_WORK(&(driver->diag_hdlc_reset_work),
  1735. diag_timer_work_fn);
  1736. for (i = 0; i < NUM_PERIPHERALS; i++) {
  1737. driver->feature[i].separate_cmd_rsp = 0;
  1738. driver->feature[i].stm_support = DISABLE_STM;
  1739. driver->feature[i].rcvd_feature_mask = 0;
  1740. driver->feature[i].peripheral_buffering = 0;
  1741. driver->feature[i].pd_buffering = 0;
  1742. driver->feature[i].encode_hdlc = 0;
  1743. driver->feature[i].untag_header =
  1744. DISABLE_PKT_HEADER_UNTAGGING;
  1745. driver->feature[i].mask_centralization = 0;
  1746. driver->feature[i].log_on_demand = 0;
  1747. driver->feature[i].sent_feature_mask = 0;
  1748. driver->feature[i].diag_id_support = 0;
  1749. }
  1750. for (i = 0; i < NUM_MD_SESSIONS; i++) {
  1751. driver->buffering_mode[i].peripheral = i;
  1752. driver->buffering_mode[i].mode = DIAG_BUFFERING_MODE_STREAMING;
  1753. driver->buffering_mode[i].high_wm_val = DEFAULT_HIGH_WM_VAL;
  1754. driver->buffering_mode[i].low_wm_val = DEFAULT_LOW_WM_VAL;
  1755. }
  1756. for (i = 0; i < NUM_STM_PROCESSORS; i++) {
  1757. driver->stm_state_requested[i] = DISABLE_STM;
  1758. driver->stm_state[i] = DISABLE_STM;
  1759. }
  1760. if (driver->hdlc_buf == NULL) {
  1761. driver->hdlc_buf = kzalloc(DIAG_MAX_HDLC_BUF_SIZE, GFP_KERNEL);
  1762. if (!driver->hdlc_buf)
  1763. goto err;
  1764. kmemleak_not_leak(driver->hdlc_buf);
  1765. }
  1766. if (driver->user_space_data_buf == NULL)
  1767. driver->user_space_data_buf = kzalloc(USER_SPACE_DATA,
  1768. GFP_KERNEL);
  1769. if (driver->user_space_data_buf == NULL)
  1770. goto err;
  1771. kmemleak_not_leak(driver->user_space_data_buf);
  1772. if (!driver->client_map) {
  1773. driver->client_map = kcalloc(driver->num_clients,
  1774. sizeof(struct diag_client_map), GFP_KERNEL);
  1775. if (!driver->client_map)
  1776. goto err;
  1777. }
  1778. kmemleak_not_leak(driver->client_map);
  1779. if (!driver->data_ready) {
  1780. driver->data_ready = kcalloc(driver->num_clients,
  1781. sizeof(int), GFP_KERNEL);
  1782. if (!driver->data_ready)
  1783. goto err;
  1784. }
  1785. kmemleak_not_leak(driver->data_ready);
  1786. for (i = 0; i < THRESHOLD_CLIENT_LIMIT; i++)
  1787. atomic_set(&driver->data_ready_notif[i], 0);
  1788. if (driver->apps_req_buf == NULL) {
  1789. driver->apps_req_buf = kzalloc(DIAG_MAX_REQ_SIZE, GFP_KERNEL);
  1790. if (!driver->apps_req_buf)
  1791. goto err;
  1792. kmemleak_not_leak(driver->apps_req_buf);
  1793. }
  1794. if (driver->dci_pkt_buf == NULL) {
  1795. driver->dci_pkt_buf = kzalloc(DCI_BUF_SIZE, GFP_KERNEL);
  1796. if (!driver->dci_pkt_buf)
  1797. goto err;
  1798. kmemleak_not_leak(driver->dci_pkt_buf);
  1799. }
  1800. if (driver->apps_rsp_buf == NULL) {
  1801. driver->apps_rsp_buf = kzalloc(DIAG_MAX_RSP_SIZE, GFP_KERNEL);
  1802. if (driver->apps_rsp_buf == NULL)
  1803. goto err;
  1804. kmemleak_not_leak(driver->apps_rsp_buf);
  1805. }
  1806. driver->diag_wq = create_singlethread_workqueue("diag_wq");
  1807. if (!driver->diag_wq)
  1808. goto err;
  1809. ret = diag_mux_register(DIAG_LOCAL_PROC, DIAG_LOCAL_PROC,
  1810. &diagfwd_mux_ops);
  1811. if (ret) {
  1812. pr_err("diag: Unable to register with USB, err: %d\n", ret);
  1813. goto err;
  1814. }
  1815. return 0;
  1816. err:
  1817. pr_err("diag: In %s, couldn't initialize diag\n", __func__);
  1818. diag_usb_exit(DIAG_USB_LOCAL);
  1819. kfree(driver->encoded_rsp_buf);
  1820. kfree(driver->hdlc_buf);
  1821. kfree(driver->client_map);
  1822. kfree(driver->data_ready);
  1823. kfree(driver->apps_req_buf);
  1824. kfree(driver->dci_pkt_buf);
  1825. kfree(driver->apps_rsp_buf);
  1826. kfree(hdlc_decode);
  1827. kfree(driver->user_space_data_buf);
  1828. if (driver->diag_wq)
  1829. destroy_workqueue(driver->diag_wq);
  1830. return -ENOMEM;
  1831. }
  1832. void diagfwd_exit(void)
  1833. {
  1834. kfree(driver->encoded_rsp_buf);
  1835. kfree(driver->hdlc_buf);
  1836. kfree(hdlc_decode);
  1837. kfree(driver->client_map);
  1838. kfree(driver->data_ready);
  1839. kfree(driver->apps_req_buf);
  1840. kfree(driver->dci_pkt_buf);
  1841. kfree(driver->apps_rsp_buf);
  1842. kfree(driver->user_space_data_buf);
  1843. destroy_workqueue(driver->diag_wq);
  1844. }