diagfwd.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  1. /* Copyright (c) 2008-2014, 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/platform_device.h>
  18. #include <linux/sched.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/diagchar.h>
  23. #include <linux/delay.h>
  24. #include <linux/reboot.h>
  25. #include <linux/of.h>
  26. #include <linux/kmemleak.h>
  27. #ifdef CONFIG_DIAG_OVER_USB
  28. #include <mach/usbdiag.h>
  29. #endif
  30. #include <mach/msm_smd.h>
  31. #include <mach/socinfo.h>
  32. #include <mach/restart.h>
  33. #include "diagmem.h"
  34. #include "diagchar.h"
  35. #include "diagfwd.h"
  36. #include "diagfwd_cntl.h"
  37. #include "diagfwd_hsic.h"
  38. #include "diagchar_hdlc.h"
  39. #ifdef CONFIG_DIAG_SDIO_PIPE
  40. #include "diagfwd_sdio.h"
  41. #endif
  42. #include "diag_dci.h"
  43. #include "diag_masks.h"
  44. #include "diagfwd_bridge.h"
  45. #define STM_CMD_VERSION_OFFSET 4
  46. #define STM_CMD_MASK_OFFSET 5
  47. #define STM_CMD_DATA_OFFSET 6
  48. #define STM_CMD_NUM_BYTES 7
  49. #define STM_RSP_VALID_INDEX 7
  50. #define STM_RSP_SUPPORTED_INDEX 8
  51. #define STM_RSP_SMD_COMPLY_INDEX 9
  52. #define STM_RSP_NUM_BYTES 10
  53. #define STM_COMMAND_VALID 1
  54. #define SMD_DRAIN_BUF_SIZE 4096
  55. int diag_debug_buf_idx;
  56. unsigned char diag_debug_buf[1024];
  57. /* Number of entries in table of buffers */
  58. static unsigned int buf_tbl_size = 10;
  59. struct diag_master_table entry;
  60. int wrap_enabled;
  61. uint16_t wrap_count;
  62. void encode_rsp_and_send(int buf_length)
  63. {
  64. struct diag_send_desc_type send = { NULL, NULL, DIAG_STATE_START, 0 };
  65. struct diag_hdlc_dest_type enc = { NULL, NULL, 0 };
  66. struct diag_smd_info *data = &(driver->smd_data[MODEM_DATA]);
  67. int err;
  68. unsigned long flags;
  69. if (buf_length > APPS_BUF_SIZE) {
  70. pr_err("diag: In %s, invalid len %d, permissible len %d\n",
  71. __func__, buf_length, APPS_BUF_SIZE);
  72. return;
  73. }
  74. send.state = DIAG_STATE_START;
  75. send.pkt = driver->apps_rsp_buf;
  76. send.last = (void *)(driver->apps_rsp_buf + buf_length);
  77. send.terminate = 1;
  78. if (!data->in_busy_1) {
  79. spin_lock_irqsave(&data->in_busy_lock, flags);
  80. enc.dest = data->buf_in_1;
  81. enc.dest_last = (void *)(data->buf_in_1 + APPS_BUF_SIZE - 1);
  82. diag_hdlc_encode(&send, &enc);
  83. data->write_ptr_1->buf = data->buf_in_1;
  84. data->write_ptr_1->length = (int)(enc.dest -
  85. (void *)(data->buf_in_1));
  86. data->in_busy_1 = 1;
  87. err = diag_device_write(data->buf_in_1, data->peripheral,
  88. data->write_ptr_1);
  89. if (err) {
  90. pr_err("diag: In %s, Unable to write to device, err: %d\n",
  91. __func__, err);
  92. data->in_busy_1 = 0;
  93. }
  94. memset(driver->apps_rsp_buf, '\0', APPS_BUF_SIZE);
  95. spin_unlock_irqrestore(&data->in_busy_lock, flags);
  96. }
  97. }
  98. /* Determine if this device uses a device tree */
  99. #ifdef CONFIG_OF
  100. static int has_device_tree(void)
  101. {
  102. struct device_node *node;
  103. node = of_find_node_by_path("/");
  104. if (node) {
  105. of_node_put(node);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. #else
  111. static int has_device_tree(void)
  112. {
  113. return 0;
  114. }
  115. #endif
  116. int chk_config_get_id(void)
  117. {
  118. /* For all Fusion targets, Modem will always be present */
  119. if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
  120. return 0;
  121. if (driver->use_device_tree) {
  122. if (machine_is_msm8974())
  123. return MSM8974_TOOLS_ID;
  124. else
  125. return 0;
  126. } else {
  127. switch (socinfo_get_msm_cpu()) {
  128. case MSM_CPU_8X60:
  129. return APQ8060_TOOLS_ID;
  130. case MSM_CPU_8960:
  131. case MSM_CPU_8960AB:
  132. return AO8960_TOOLS_ID;
  133. case MSM_CPU_8064:
  134. case MSM_CPU_8064AB:
  135. case MSM_CPU_8064AA:
  136. return APQ8064_TOOLS_ID;
  137. case MSM_CPU_8930:
  138. case MSM_CPU_8930AA:
  139. case MSM_CPU_8930AB:
  140. return MSM8930_TOOLS_ID;
  141. case MSM_CPU_8974:
  142. return MSM8974_TOOLS_ID;
  143. case MSM_CPU_8625:
  144. return MSM8625_TOOLS_ID;
  145. default:
  146. return 0;
  147. }
  148. }
  149. }
  150. /*
  151. * This will return TRUE for targets which support apps only mode and hence SSR.
  152. * This applies to 8960 and newer targets.
  153. */
  154. int chk_apps_only(void)
  155. {
  156. if (driver->use_device_tree)
  157. return 1;
  158. switch (socinfo_get_msm_cpu()) {
  159. case MSM_CPU_8960:
  160. case MSM_CPU_8960AB:
  161. case MSM_CPU_8064:
  162. case MSM_CPU_8064AB:
  163. case MSM_CPU_8064AA:
  164. case MSM_CPU_8930:
  165. case MSM_CPU_8930AA:
  166. case MSM_CPU_8930AB:
  167. case MSM_CPU_8627:
  168. case MSM_CPU_9615:
  169. case MSM_CPU_8974:
  170. return 1;
  171. default:
  172. return 0;
  173. }
  174. }
  175. /*
  176. * This will return TRUE for targets which support apps as master.
  177. * Thus, SW DLOAD and Mode Reset are supported on apps processor.
  178. * This applies to 8960 and newer targets.
  179. */
  180. int chk_apps_master(void)
  181. {
  182. if (driver->use_device_tree)
  183. return 1;
  184. else if (soc_class_is_msm8960() || soc_class_is_msm8930() ||
  185. soc_class_is_apq8064() || cpu_is_msm9615())
  186. return 1;
  187. else
  188. return 0;
  189. }
  190. int chk_polling_response(void)
  191. {
  192. if (!(driver->polling_reg_flag) && chk_apps_master())
  193. /*
  194. * If the apps processor is master and no other processor
  195. * has registered to respond for polling
  196. */
  197. return 1;
  198. else if (!((driver->smd_data[MODEM_DATA].ch) &&
  199. (driver->rcvd_feature_mask[MODEM_DATA])) &&
  200. (chk_apps_master()))
  201. /*
  202. * If the apps processor is not the master and the modem
  203. * is not up or we did not receive the feature masks from Modem
  204. */
  205. return 1;
  206. else
  207. return 0;
  208. }
  209. /*
  210. * This function should be called if you feel that the logging process may
  211. * need to be woken up. For instance, if the logging mode is MEMORY_DEVICE MODE
  212. * and while trying to read data from a SMD data channel there are no buffers
  213. * available to read the data into, then this function should be called to
  214. * determine if the logging process needs to be woken up.
  215. */
  216. void chk_logging_wakeup(void)
  217. {
  218. int i;
  219. /* Find the index of the logging process */
  220. for (i = 0; i < driver->num_clients; i++)
  221. if (driver->client_map[i].pid ==
  222. driver->logging_process_id)
  223. break;
  224. if (i < driver->num_clients) {
  225. /* At very high logging rates a race condition can
  226. * occur where the buffers containing the data from
  227. * an smd channel are all in use, but the data_ready
  228. * flag is cleared. In this case, the buffers never
  229. * have their data read/logged. Detect and remedy this
  230. * situation.
  231. */
  232. if ((driver->data_ready[i] & USER_SPACE_DATA_TYPE) == 0) {
  233. driver->data_ready[i] |= USER_SPACE_DATA_TYPE;
  234. pr_debug("diag: Force wakeup of logging process\n");
  235. wake_up_interruptible(&driver->wait_q);
  236. }
  237. }
  238. }
  239. int diag_add_hdlc_encoding(struct diag_smd_info *smd_info, void *buf,
  240. int total_recd, uint8_t *encode_buf,
  241. int *encoded_length)
  242. {
  243. struct diag_send_desc_type send = { NULL, NULL, DIAG_STATE_START, 0 };
  244. struct diag_hdlc_dest_type enc = { NULL, NULL, 0 };
  245. struct data_header {
  246. uint8_t control_char;
  247. uint8_t version;
  248. uint16_t length;
  249. };
  250. struct data_header *header;
  251. int header_size = sizeof(struct data_header);
  252. uint8_t *end_control_char;
  253. uint8_t *payload;
  254. uint8_t *temp_buf;
  255. uint8_t *temp_encode_buf;
  256. int src_pkt_len;
  257. int encoded_pkt_length;
  258. int max_size;
  259. int total_processed = 0;
  260. int bytes_remaining;
  261. int success = 1;
  262. temp_buf = buf;
  263. temp_encode_buf = encode_buf;
  264. bytes_remaining = *encoded_length;
  265. while (total_processed < total_recd) {
  266. header = (struct data_header *)temp_buf;
  267. /* Perform initial error checking */
  268. if (header->control_char != CONTROL_CHAR ||
  269. header->version != 1) {
  270. success = 0;
  271. break;
  272. }
  273. payload = temp_buf + header_size;
  274. end_control_char = payload + header->length;
  275. if (*end_control_char != CONTROL_CHAR) {
  276. success = 0;
  277. break;
  278. }
  279. max_size = 2 * header->length + 3;
  280. if (bytes_remaining < max_size) {
  281. pr_err("diag: In %s, Not enough room to encode remaining data for peripheral: %d, bytes available: %d, max_size: %d\n",
  282. __func__, smd_info->peripheral,
  283. bytes_remaining, max_size);
  284. success = 0;
  285. break;
  286. }
  287. /* Prepare for encoding the data */
  288. send.state = DIAG_STATE_START;
  289. send.pkt = payload;
  290. send.last = (void *)(payload + header->length - 1);
  291. send.terminate = 1;
  292. enc.dest = temp_encode_buf;
  293. enc.dest_last = (void *)(temp_encode_buf + max_size);
  294. enc.crc = 0;
  295. diag_hdlc_encode(&send, &enc);
  296. /* Prepare for next packet */
  297. src_pkt_len = (header_size + header->length + 1);
  298. total_processed += src_pkt_len;
  299. temp_buf += src_pkt_len;
  300. encoded_pkt_length = (uint8_t *)enc.dest - temp_encode_buf;
  301. bytes_remaining -= encoded_pkt_length;
  302. temp_encode_buf = enc.dest;
  303. }
  304. *encoded_length = (int)(temp_encode_buf - encode_buf);
  305. return success;
  306. }
  307. static int check_bufsize_for_encoding(struct diag_smd_info *smd_info, void *buf,
  308. int total_recd)
  309. {
  310. int buf_size = IN_BUF_SIZE;
  311. int max_size = 2 * total_recd + 3;
  312. unsigned char *temp_buf;
  313. if (max_size > IN_BUF_SIZE) {
  314. if (max_size > MAX_IN_BUF_SIZE) {
  315. pr_err_ratelimited("diag: In %s, SMD sending packet of %d bytes that may expand to %d bytes, peripheral: %d\n",
  316. __func__, total_recd, max_size,
  317. smd_info->peripheral);
  318. max_size = MAX_IN_BUF_SIZE;
  319. }
  320. if (buf == smd_info->buf_in_1_raw) {
  321. /* Only realloc if we need to increase the size */
  322. if (smd_info->buf_in_1_size < max_size) {
  323. temp_buf = krealloc(smd_info->buf_in_1,
  324. max_size, GFP_KERNEL);
  325. if (temp_buf) {
  326. smd_info->buf_in_1 = temp_buf;
  327. smd_info->buf_in_1_size = max_size;
  328. }
  329. }
  330. buf_size = smd_info->buf_in_1_size;
  331. } else {
  332. /* Only realloc if we need to increase the size */
  333. if (smd_info->buf_in_2_size < max_size) {
  334. temp_buf = krealloc(smd_info->buf_in_2,
  335. max_size, GFP_KERNEL);
  336. if (temp_buf) {
  337. smd_info->buf_in_2 = temp_buf;
  338. smd_info->buf_in_2_size = max_size;
  339. }
  340. }
  341. buf_size = smd_info->buf_in_2_size;
  342. }
  343. }
  344. return buf_size;
  345. }
  346. void diag_ws_on_notify()
  347. {
  348. /*
  349. * Do not deal with reference count here as there can be
  350. * spurious interrupts
  351. */
  352. pm_stay_awake(driver->diag_dev);
  353. }
  354. void diag_ws_on_read(int pkt_len)
  355. {
  356. unsigned long flags;
  357. spin_lock_irqsave(&driver->ws_lock, flags);
  358. if (pkt_len > 0) {
  359. driver->ws_ref_count++;
  360. } else {
  361. if (driver->ws_ref_count < 1) {
  362. pm_relax(driver->diag_dev);
  363. driver->ws_ref_count = 0;
  364. driver->copy_count = 0;
  365. }
  366. }
  367. spin_unlock_irqrestore(&driver->ws_lock, flags);
  368. }
  369. void diag_ws_on_copy()
  370. {
  371. unsigned long flags;
  372. spin_lock_irqsave(&driver->ws_lock, flags);
  373. driver->copy_count++;
  374. spin_unlock_irqrestore(&driver->ws_lock, flags);
  375. }
  376. void diag_ws_on_copy_complete()
  377. {
  378. unsigned long flags;
  379. spin_lock_irqsave(&driver->ws_lock, flags);
  380. driver->ws_ref_count -= driver->copy_count;
  381. if (driver->ws_ref_count < 1) {
  382. pm_relax(driver->diag_dev);
  383. driver->ws_ref_count = 0;
  384. }
  385. driver->copy_count = 0;
  386. spin_unlock_irqrestore(&driver->ws_lock, flags);
  387. }
  388. void diag_ws_reset()
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&driver->ws_lock, flags);
  392. pm_relax(driver->diag_dev);
  393. driver->ws_ref_count = 0;
  394. driver->copy_count = 0;
  395. spin_unlock_irqrestore(&driver->ws_lock, flags);
  396. }
  397. /* Process the data read from the smd data channel */
  398. int diag_process_smd_read_data(struct diag_smd_info *smd_info, void *buf,
  399. int total_recd)
  400. {
  401. struct diag_request *write_ptr_modem = NULL;
  402. int *in_busy_ptr = 0;
  403. int err = 0;
  404. unsigned long flags;
  405. /*
  406. * Do not process data on command channel if the
  407. * channel is not designated to do so
  408. */
  409. if ((smd_info->type == SMD_CMD_TYPE) &&
  410. !driver->separate_cmdrsp[smd_info->peripheral]) {
  411. /* This print is for debugging */
  412. pr_err("diag, In %s, received data on non-designated command channel: %d\n",
  413. __func__, smd_info->peripheral);
  414. goto err;
  415. }
  416. /* If the data is already hdlc encoded */
  417. if (!smd_info->encode_hdlc) {
  418. if (smd_info->buf_in_1 == buf) {
  419. write_ptr_modem = smd_info->write_ptr_1;
  420. in_busy_ptr = &smd_info->in_busy_1;
  421. } else if (smd_info->buf_in_2 == buf) {
  422. write_ptr_modem = smd_info->write_ptr_2;
  423. in_busy_ptr = &smd_info->in_busy_2;
  424. } else {
  425. pr_err("diag: In %s, no match for in_busy_1, peripheral: %d\n",
  426. __func__, smd_info->peripheral);
  427. goto err;
  428. }
  429. if (write_ptr_modem) {
  430. spin_lock_irqsave(&smd_info->in_busy_lock, flags);
  431. write_ptr_modem->length = total_recd;
  432. *in_busy_ptr = 1;
  433. err = diag_device_write(buf, smd_info->peripheral,
  434. write_ptr_modem);
  435. spin_unlock_irqrestore(&smd_info->in_busy_lock, flags);
  436. if (err) {
  437. pr_err_ratelimited("diag: In %s, diag_device_write error: %d\n",
  438. __func__, err);
  439. goto err;
  440. }
  441. }
  442. } else {
  443. /* The data is raw and needs to be hdlc encoded */
  444. if (smd_info->buf_in_1_raw == buf) {
  445. write_ptr_modem = smd_info->write_ptr_1;
  446. in_busy_ptr = &smd_info->in_busy_1;
  447. } else if (smd_info->buf_in_2_raw == buf) {
  448. write_ptr_modem = smd_info->write_ptr_2;
  449. in_busy_ptr = &smd_info->in_busy_2;
  450. } else {
  451. pr_err("diag: In %s, no match for in_busy_1, peripheral: %d\n",
  452. __func__, smd_info->peripheral);
  453. goto err;
  454. }
  455. if (write_ptr_modem) {
  456. int success = 0;
  457. int write_length = 0;
  458. unsigned char *write_buf = NULL;
  459. write_length = check_bufsize_for_encoding(smd_info, buf,
  460. total_recd);
  461. if (write_length) {
  462. write_buf = (buf == smd_info->buf_in_1_raw) ?
  463. smd_info->buf_in_1 : smd_info->buf_in_2;
  464. success = diag_add_hdlc_encoding(smd_info, buf,
  465. total_recd, write_buf,
  466. &write_length);
  467. if (success) {
  468. spin_lock_irqsave(
  469. &smd_info->in_busy_lock, flags);
  470. write_ptr_modem->length = write_length;
  471. *in_busy_ptr = 1;
  472. err = diag_device_write(write_buf,
  473. smd_info->peripheral,
  474. write_ptr_modem);
  475. spin_unlock_irqrestore(
  476. &smd_info->in_busy_lock, flags);
  477. if (err) {
  478. pr_err_ratelimited("diag: In %s, diag_device_write error: %d\n",
  479. __func__, err);
  480. goto err;
  481. }
  482. }
  483. } else {
  484. goto err;
  485. }
  486. }
  487. }
  488. return 0;
  489. err:
  490. if ((smd_info->type == SMD_DATA_TYPE ||
  491. smd_info->type == SMD_CMD_TYPE) &&
  492. driver->logging_mode == MEMORY_DEVICE_MODE)
  493. diag_ws_on_read(0);
  494. return 0;
  495. }
  496. void diag_smd_queue_read(struct diag_smd_info *smd_info)
  497. {
  498. if (!smd_info || !smd_info->ch)
  499. return;
  500. if ((smd_info->type == SMD_DATA_TYPE ||
  501. smd_info->type == SMD_CMD_TYPE) &&
  502. driver->logging_mode == MEMORY_DEVICE_MODE) {
  503. diag_ws_on_notify();
  504. }
  505. switch (smd_info->type) {
  506. case SMD_DCI_TYPE:
  507. case SMD_DCI_CMD_TYPE:
  508. queue_work(driver->diag_dci_wq,
  509. &(smd_info->diag_read_smd_work));
  510. break;
  511. case SMD_DATA_TYPE:
  512. queue_work(smd_info->wq,
  513. &(smd_info->diag_read_smd_work));
  514. break;
  515. case SMD_CNTL_TYPE:
  516. case SMD_CMD_TYPE:
  517. queue_work(driver->diag_wq,
  518. &(smd_info->diag_read_smd_work));
  519. break;
  520. default:
  521. pr_err("diag: In %s, invalid type: %d\n", __func__,
  522. smd_info->type);
  523. if ((smd_info->type == SMD_DATA_TYPE ||
  524. smd_info->type == SMD_CMD_TYPE) &&
  525. driver->logging_mode == MEMORY_DEVICE_MODE)
  526. diag_ws_on_read(0);
  527. return;
  528. }
  529. }
  530. static int diag_smd_resize_buf(struct diag_smd_info *smd_info, void **buf,
  531. unsigned int *buf_size,
  532. unsigned int requested_size)
  533. {
  534. int success = 0;
  535. void *temp_buf = NULL;
  536. unsigned int new_buf_size = requested_size;
  537. if (!smd_info)
  538. return success;
  539. if (requested_size <= MAX_IN_BUF_SIZE) {
  540. pr_debug("diag: In %s, SMD peripheral: %d sending in packets up to %d bytes\n",
  541. __func__, smd_info->peripheral, requested_size);
  542. } else {
  543. pr_err_ratelimited("diag: In %s, SMD peripheral: %d, Packet size sent: %d, Max size supported (%d) exceeded. Data beyond max size will be lost\n",
  544. __func__, smd_info->peripheral, requested_size,
  545. MAX_IN_BUF_SIZE);
  546. new_buf_size = MAX_IN_BUF_SIZE;
  547. }
  548. /* Only resize if the buffer can be increased in size */
  549. if (new_buf_size <= *buf_size) {
  550. success = 1;
  551. return success;
  552. }
  553. temp_buf = krealloc(*buf, new_buf_size, GFP_KERNEL);
  554. if (temp_buf) {
  555. /* Match the buffer and reset the pointer and size */
  556. if (smd_info->encode_hdlc) {
  557. /*
  558. * This smd channel is supporting HDLC encoding
  559. * on the apps
  560. */
  561. void *temp_hdlc = NULL;
  562. if (*buf == smd_info->buf_in_1_raw) {
  563. smd_info->buf_in_1_raw = temp_buf;
  564. smd_info->buf_in_1_raw_size = new_buf_size;
  565. temp_hdlc = krealloc(smd_info->buf_in_1,
  566. MAX_IN_BUF_SIZE,
  567. GFP_KERNEL);
  568. if (temp_hdlc) {
  569. smd_info->buf_in_1 = temp_hdlc;
  570. smd_info->buf_in_1_size =
  571. MAX_IN_BUF_SIZE;
  572. }
  573. } else if (*buf == smd_info->buf_in_2_raw) {
  574. smd_info->buf_in_2_raw = temp_buf;
  575. smd_info->buf_in_2_raw_size = new_buf_size;
  576. temp_hdlc = krealloc(smd_info->buf_in_2,
  577. MAX_IN_BUF_SIZE,
  578. GFP_KERNEL);
  579. if (temp_hdlc) {
  580. smd_info->buf_in_2 = temp_hdlc;
  581. smd_info->buf_in_2_size =
  582. MAX_IN_BUF_SIZE;
  583. }
  584. }
  585. } else {
  586. if (*buf == smd_info->buf_in_1) {
  587. smd_info->buf_in_1 = temp_buf;
  588. smd_info->buf_in_1_size = new_buf_size;
  589. } else if (*buf == smd_info->buf_in_2) {
  590. smd_info->buf_in_2 = temp_buf;
  591. smd_info->buf_in_2_size = new_buf_size;
  592. }
  593. }
  594. *buf = temp_buf;
  595. *buf_size = new_buf_size;
  596. success = 1;
  597. } else {
  598. pr_err_ratelimited("diag: In %s, SMD peripheral: %d. packet size sent: %d, resize to support failed. Data beyond %d will be lost\n",
  599. __func__, smd_info->peripheral, requested_size,
  600. *buf_size);
  601. }
  602. return success;
  603. }
  604. void diag_smd_send_req(struct diag_smd_info *smd_info)
  605. {
  606. void *buf = NULL, *temp_buf = NULL;
  607. int total_recd = 0, r = 0, pkt_len;
  608. int loop_count = 0;
  609. int notify = 0;
  610. int buf_size = 0;
  611. int resize_success = 0;
  612. int buf_full = 0;
  613. if (!smd_info) {
  614. pr_err("diag: In %s, no smd info. Not able to read.\n",
  615. __func__);
  616. return;
  617. }
  618. /* Determine the buffer to read the data into. */
  619. if (smd_info->type == SMD_DATA_TYPE) {
  620. /* If the data is raw and not hdlc encoded */
  621. if (smd_info->encode_hdlc) {
  622. if (!smd_info->in_busy_1) {
  623. buf = smd_info->buf_in_1_raw;
  624. buf_size = smd_info->buf_in_1_raw_size;
  625. } else if (!smd_info->in_busy_2) {
  626. buf = smd_info->buf_in_2_raw;
  627. buf_size = smd_info->buf_in_2_raw_size;
  628. }
  629. } else {
  630. if (!smd_info->in_busy_1) {
  631. buf = smd_info->buf_in_1;
  632. buf_size = smd_info->buf_in_1_size;
  633. } else if (!smd_info->in_busy_2) {
  634. buf = smd_info->buf_in_2;
  635. buf_size = smd_info->buf_in_2_size;
  636. }
  637. }
  638. } else if (smd_info->type == SMD_CMD_TYPE) {
  639. /* If the data is raw and not hdlc encoded */
  640. if (smd_info->encode_hdlc) {
  641. if (!smd_info->in_busy_1) {
  642. buf = smd_info->buf_in_1_raw;
  643. buf_size = smd_info->buf_in_1_raw_size;
  644. }
  645. } else {
  646. if (!smd_info->in_busy_1) {
  647. buf = smd_info->buf_in_1;
  648. buf_size = smd_info->buf_in_1_size;
  649. }
  650. }
  651. } else if (!smd_info->in_busy_1) {
  652. buf = smd_info->buf_in_1;
  653. buf_size = smd_info->buf_in_1_size;
  654. }
  655. if (!buf && (smd_info->type == SMD_DCI_TYPE ||
  656. smd_info->type == SMD_DCI_CMD_TYPE))
  657. diag_dci_try_deactivate_wakeup_source();
  658. if (smd_info->ch && buf) {
  659. pkt_len = smd_cur_packet_size(smd_info->ch);
  660. if (pkt_len == 0 && (smd_info->type == SMD_DCI_TYPE ||
  661. smd_info->type == SMD_DCI_CMD_TYPE))
  662. diag_dci_try_deactivate_wakeup_source();
  663. if (pkt_len > buf_size)
  664. resize_success = diag_smd_resize_buf(smd_info, &buf,
  665. &buf_size, pkt_len);
  666. temp_buf = buf;
  667. while (pkt_len && (pkt_len != total_recd)) {
  668. loop_count++;
  669. r = smd_read_avail(smd_info->ch);
  670. pr_debug("diag: In %s, SMD peripheral: %d, received pkt %d %d\n",
  671. __func__, smd_info->peripheral, r, total_recd);
  672. if (!r) {
  673. /* Nothing to read from SMD */
  674. wait_event(driver->smd_wait_q,
  675. ((smd_info->ch == 0) ||
  676. smd_read_avail(smd_info->ch)));
  677. /* If the smd channel is open */
  678. if (smd_info->ch) {
  679. pr_debug("diag: In %s, SMD peripheral: %d, return from wait_event\n",
  680. __func__, smd_info->peripheral);
  681. continue;
  682. } else {
  683. pr_debug("diag: In %s, SMD peripheral: %d, return from wait_event ch closed\n",
  684. __func__, smd_info->peripheral);
  685. goto fail_return;
  686. }
  687. }
  688. if (pkt_len < r) {
  689. pr_err("diag: In %s, SMD peripheral: %d, sending incorrect pkt\n",
  690. __func__, smd_info->peripheral);
  691. goto fail_return;
  692. }
  693. if (pkt_len > r) {
  694. pr_debug("diag: In %s, SMD sending partial pkt %d %d %d %d %d %d\n",
  695. __func__, pkt_len, r, total_recd, loop_count,
  696. smd_info->peripheral, smd_info->type);
  697. }
  698. /* Protect from going beyond the end of the buffer */
  699. if (total_recd < buf_size) {
  700. if (total_recd + r > buf_size) {
  701. r = buf_size - total_recd;
  702. buf_full = 1;
  703. }
  704. total_recd += r;
  705. /* Keep reading for complete packet */
  706. smd_read(smd_info->ch, temp_buf, r);
  707. temp_buf += r;
  708. } else {
  709. /*
  710. * This block handles the very rare case of a
  711. * packet that is greater in length than what
  712. * we can support. In this case, we
  713. * incrementally drain the remaining portion
  714. * of the packet that will not fit in the
  715. * buffer, so that the entire packet is read
  716. * from the smd.
  717. */
  718. int drain_bytes = (r > SMD_DRAIN_BUF_SIZE) ?
  719. SMD_DRAIN_BUF_SIZE : r;
  720. unsigned char *drain_buf = kzalloc(drain_bytes,
  721. GFP_KERNEL);
  722. if (drain_buf) {
  723. total_recd += drain_bytes;
  724. smd_read(smd_info->ch, drain_buf,
  725. drain_bytes);
  726. kfree(drain_buf);
  727. } else {
  728. pr_err("diag: In %s, SMD peripheral: %d, unable to allocate drain buffer\n",
  729. __func__, smd_info->peripheral);
  730. break;
  731. }
  732. }
  733. }
  734. if ((smd_info->type == SMD_DATA_TYPE ||
  735. smd_info->type == SMD_CMD_TYPE) &&
  736. driver->logging_mode == MEMORY_DEVICE_MODE)
  737. diag_ws_on_read(total_recd);
  738. if (total_recd > 0) {
  739. if (!buf) {
  740. pr_err("diag: In %s, SMD peripheral: %d, Out of diagmem for Modem\n",
  741. __func__, smd_info->peripheral);
  742. } else if (smd_info->process_smd_read_data) {
  743. /*
  744. * If the buffer was totally filled, reset
  745. * total_recd appropriately
  746. */
  747. if (buf_full)
  748. total_recd = buf_size;
  749. notify = smd_info->process_smd_read_data(
  750. smd_info, buf, total_recd);
  751. /* Poll SMD channels to check for data */
  752. if (notify)
  753. diag_smd_notify(smd_info,
  754. SMD_EVENT_DATA);
  755. }
  756. }
  757. } else if (smd_info->ch && !buf &&
  758. (driver->logging_mode == MEMORY_DEVICE_MODE)) {
  759. chk_logging_wakeup();
  760. } else {
  761. if ((smd_info->type == SMD_DATA_TYPE ||
  762. smd_info->type == SMD_CMD_TYPE) &&
  763. driver->logging_mode == MEMORY_DEVICE_MODE) {
  764. diag_ws_on_read(0);
  765. }
  766. }
  767. return;
  768. fail_return:
  769. if ((smd_info->type == SMD_DATA_TYPE ||
  770. smd_info->type == SMD_CMD_TYPE) &&
  771. driver->logging_mode == MEMORY_DEVICE_MODE)
  772. diag_ws_on_read(0);
  773. if (smd_info->type == SMD_DCI_TYPE ||
  774. smd_info->type == SMD_DCI_CMD_TYPE)
  775. diag_dci_try_deactivate_wakeup_source();
  776. return;
  777. }
  778. void diag_read_smd_work_fn(struct work_struct *work)
  779. {
  780. struct diag_smd_info *smd_info = container_of(work,
  781. struct diag_smd_info,
  782. diag_read_smd_work);
  783. diag_smd_send_req(smd_info);
  784. }
  785. #ifdef CONFIG_DIAG_OVER_USB
  786. static int diag_write_to_usb(struct usb_diag_ch *ch,
  787. struct diag_request *write_ptr)
  788. {
  789. int err = 0;
  790. uint8_t retry_count, max_retries;
  791. if (!ch || !write_ptr)
  792. return -EIO;
  793. retry_count = 0;
  794. max_retries = 3;
  795. while (retry_count < max_retries) {
  796. retry_count++;
  797. /* If USB is not connected, don't try to write */
  798. if (!driver->usb_connected) {
  799. err = -ENODEV;
  800. break;
  801. }
  802. err = usb_diag_write(ch, write_ptr);
  803. if (err == -EAGAIN) {
  804. /*
  805. * USB is not configured. Wait for sometime and
  806. * try again. The value 10000 was chosen empirically
  807. * as an optimum value for USB to be configured.
  808. */
  809. usleep_range(10000, 10100);
  810. continue;
  811. } else {
  812. break;
  813. }
  814. }
  815. return err;
  816. }
  817. #endif
  818. int diag_device_write(void *buf, int data_type, struct diag_request *write_ptr)
  819. {
  820. int i, err = 0, index;
  821. index = 0;
  822. if (driver->logging_mode == MEMORY_DEVICE_MODE) {
  823. if (data_type == APPS_DATA) {
  824. for (i = 0; i < driver->buf_tbl_size; i++)
  825. if (driver->buf_tbl[i].length == 0) {
  826. driver->buf_tbl[i].buf = buf;
  827. driver->buf_tbl[i].length =
  828. driver->used;
  829. #ifdef DIAG_DEBUG
  830. pr_debug("diag: ENQUEUE buf ptr and length is %p , %d\n",
  831. driver->buf_tbl[i].buf,
  832. driver->buf_tbl[i].length);
  833. #endif
  834. break;
  835. }
  836. }
  837. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  838. else if (data_type == HSIC_DATA || data_type == HSIC_2_DATA) {
  839. unsigned long flags;
  840. int foundIndex = -1;
  841. index = data_type - HSIC_DATA;
  842. spin_lock_irqsave(&diag_hsic[index].hsic_spinlock,
  843. flags);
  844. for (i = 0; i < diag_hsic[index].poolsize_hsic_write;
  845. i++) {
  846. if (diag_hsic[index].hsic_buf_tbl[i].length
  847. == 0) {
  848. diag_hsic[index].hsic_buf_tbl[i].buf
  849. = buf;
  850. diag_hsic[index].hsic_buf_tbl[i].length
  851. = diag_bridge[index].write_len;
  852. diag_hsic[index].
  853. num_hsic_buf_tbl_entries++;
  854. foundIndex = i;
  855. break;
  856. }
  857. }
  858. spin_unlock_irqrestore(&diag_hsic[index].hsic_spinlock,
  859. flags);
  860. if (foundIndex == -1)
  861. err = -1;
  862. else
  863. pr_debug("diag: ENQUEUE HSIC buf ptr and length is %p , %d, ch %d\n",
  864. buf, diag_bridge[index].write_len,
  865. index);
  866. }
  867. #endif
  868. for (i = 0; i < driver->num_clients; i++)
  869. if (driver->client_map[i].pid ==
  870. driver->logging_process_id)
  871. break;
  872. if (i < driver->num_clients) {
  873. pr_debug("diag: wake up logging process\n");
  874. driver->data_ready[i] |= USER_SPACE_DATA_TYPE;
  875. wake_up_interruptible(&driver->wait_q);
  876. } else
  877. return -EINVAL;
  878. } else if (driver->logging_mode == NO_LOGGING_MODE) {
  879. if ((data_type >= MODEM_DATA) && (data_type <= WCNSS_DATA)) {
  880. driver->smd_data[data_type].in_busy_1 = 0;
  881. driver->smd_data[data_type].in_busy_2 = 0;
  882. queue_work(driver->smd_data[data_type].wq,
  883. &(driver->smd_data[data_type].
  884. diag_read_smd_work));
  885. if (data_type == MODEM_DATA &&
  886. driver->separate_cmdrsp[data_type]) {
  887. driver->smd_cmd[data_type].in_busy_1 = 0;
  888. driver->smd_cmd[data_type].in_busy_2 = 0;
  889. queue_work(driver->diag_wq,
  890. &(driver->smd_cmd[data_type].
  891. diag_read_smd_work));
  892. }
  893. }
  894. #ifdef CONFIG_DIAG_SDIO_PIPE
  895. else if (data_type == SDIO_DATA) {
  896. driver->in_busy_sdio = 0;
  897. queue_work(driver->diag_sdio_wq,
  898. &(driver->diag_read_sdio_work));
  899. }
  900. #endif
  901. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  902. else if (data_type == HSIC_DATA || data_type == HSIC_2_DATA) {
  903. index = data_type - HSIC_DATA;
  904. if (diag_hsic[index].hsic_ch)
  905. queue_work(diag_bridge[index].wq,
  906. &(diag_hsic[index].
  907. diag_read_hsic_work));
  908. }
  909. #endif
  910. err = -1;
  911. }
  912. #ifdef CONFIG_DIAG_OVER_USB
  913. else if (driver->logging_mode == USB_MODE) {
  914. if (data_type == APPS_DATA) {
  915. driver->write_ptr_svc = (struct diag_request *)
  916. (diagmem_alloc(driver, sizeof(struct diag_request),
  917. POOL_TYPE_WRITE_STRUCT));
  918. if (driver->write_ptr_svc) {
  919. driver->write_ptr_svc->length = driver->used;
  920. driver->write_ptr_svc->buf = buf;
  921. err = diag_write_to_usb(driver->legacy_ch,
  922. driver->write_ptr_svc);
  923. /* Free the buffer if write failed */
  924. if (err) {
  925. diagmem_free(driver,
  926. (unsigned char *)driver->
  927. write_ptr_svc,
  928. POOL_TYPE_WRITE_STRUCT);
  929. }
  930. } else {
  931. err = -ENOMEM;
  932. }
  933. } else if ((data_type >= MODEM_DATA) &&
  934. (data_type <= WCNSS_DATA)) {
  935. write_ptr->buf = buf;
  936. #ifdef DIAG_DEBUG
  937. printk(KERN_INFO "writing data to USB,"
  938. "pkt length %d\n", write_ptr->length);
  939. print_hex_dump(KERN_DEBUG, "Written Packet Data to"
  940. " USB: ", 16, 1, DUMP_PREFIX_ADDRESS,
  941. buf, write_ptr->length, 1);
  942. #endif /* DIAG DEBUG */
  943. err = diag_write_to_usb(driver->legacy_ch, write_ptr);
  944. }
  945. #ifdef CONFIG_DIAG_SDIO_PIPE
  946. else if (data_type == SDIO_DATA) {
  947. if (machine_is_msm8x60_fusion() ||
  948. machine_is_msm8x60_fusn_ffa()) {
  949. write_ptr->buf = buf;
  950. err = usb_diag_write(driver->mdm_ch, write_ptr);
  951. } else
  952. pr_err("diag: Incorrect sdio data "
  953. "while USB write\n");
  954. }
  955. #endif
  956. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  957. else if (data_type == HSIC_DATA || data_type == HSIC_2_DATA) {
  958. index = data_type - HSIC_DATA;
  959. if (diag_hsic[index].hsic_device_enabled) {
  960. struct diag_request *write_ptr_mdm;
  961. write_ptr_mdm = (struct diag_request *)
  962. diagmem_alloc(driver,
  963. sizeof(struct diag_request),
  964. index +
  965. POOL_TYPE_HSIC_WRITE);
  966. if (write_ptr_mdm) {
  967. write_ptr_mdm->buf = buf;
  968. write_ptr_mdm->length =
  969. diag_bridge[index].write_len;
  970. write_ptr_mdm->context = (void *)index;
  971. err = usb_diag_write(
  972. diag_bridge[index].ch, write_ptr_mdm);
  973. /* Return to the pool immediately */
  974. if (err) {
  975. diagmem_free(driver,
  976. write_ptr_mdm,
  977. index +
  978. POOL_TYPE_HSIC_WRITE);
  979. pr_err_ratelimited("diag: HSIC write failure, err: %d, ch %d\n",
  980. err, index);
  981. }
  982. } else {
  983. pr_err("diag: allocate write fail\n");
  984. err = -1;
  985. }
  986. } else {
  987. pr_err("diag: Incorrect HSIC data "
  988. "while USB write\n");
  989. err = -1;
  990. }
  991. } else if (data_type == SMUX_DATA) {
  992. write_ptr->buf = buf;
  993. write_ptr->context = (void *)SMUX;
  994. pr_debug("diag: writing SMUX data\n");
  995. err = usb_diag_write(diag_bridge[SMUX].ch,
  996. write_ptr);
  997. }
  998. #endif
  999. APPEND_DEBUG('d');
  1000. }
  1001. #endif /* DIAG OVER USB */
  1002. return err;
  1003. }
  1004. void diag_update_pkt_buffer(unsigned char *buf, int type)
  1005. {
  1006. unsigned char *ptr = NULL;
  1007. unsigned char *temp = buf;
  1008. unsigned int length;
  1009. int *in_busy = NULL;
  1010. if (!buf) {
  1011. pr_err("diag: Invalid buffer in %s\n", __func__);
  1012. return;
  1013. }
  1014. switch (type) {
  1015. case PKT_TYPE:
  1016. ptr = driver->pkt_buf;
  1017. length = driver->pkt_length;
  1018. in_busy = &driver->in_busy_pktdata;
  1019. break;
  1020. case DCI_PKT_TYPE:
  1021. ptr = driver->dci_pkt_buf;
  1022. length = driver->dci_pkt_length;
  1023. in_busy = &driver->in_busy_dcipktdata;
  1024. break;
  1025. default:
  1026. pr_err("diag: Invalid type %d in %s\n", type, __func__);
  1027. return;
  1028. }
  1029. if (!ptr || length == 0) {
  1030. pr_err("diag: Invalid ptr %p and length %d in %s",
  1031. ptr, length, __func__);
  1032. return;
  1033. }
  1034. mutex_lock(&driver->diagchar_mutex);
  1035. if (CHK_OVERFLOW(ptr, ptr, ptr + PKT_SIZE, length)) {
  1036. memcpy(ptr, temp , length);
  1037. *in_busy = 1;
  1038. } else {
  1039. printk(KERN_CRIT " Not enough buffer space for PKT_RESP\n");
  1040. }
  1041. mutex_unlock(&driver->diagchar_mutex);
  1042. }
  1043. void diag_update_userspace_clients(unsigned int type)
  1044. {
  1045. int i;
  1046. mutex_lock(&driver->diagchar_mutex);
  1047. for (i = 0; i < driver->num_clients; i++)
  1048. if (driver->client_map[i].pid != 0)
  1049. driver->data_ready[i] |= type;
  1050. wake_up_interruptible(&driver->wait_q);
  1051. mutex_unlock(&driver->diagchar_mutex);
  1052. }
  1053. void diag_update_sleeping_process(int process_id, int data_type)
  1054. {
  1055. int i;
  1056. mutex_lock(&driver->diagchar_mutex);
  1057. for (i = 0; i < driver->num_clients; i++)
  1058. if (driver->client_map[i].pid == process_id) {
  1059. driver->data_ready[i] |= data_type;
  1060. break;
  1061. }
  1062. wake_up_interruptible(&driver->wait_q);
  1063. mutex_unlock(&driver->diagchar_mutex);
  1064. }
  1065. int diag_send_data(struct diag_master_table entry, unsigned char *buf,
  1066. int len, int type)
  1067. {
  1068. int success = 1;
  1069. driver->pkt_length = len;
  1070. /* If the process_id corresponds to an apps process */
  1071. if (entry.process_id != NON_APPS_PROC) {
  1072. /* If the message is to be sent to the apps process */
  1073. if (type != MODEM_DATA) {
  1074. diag_update_pkt_buffer(buf, PKT_TYPE);
  1075. diag_update_sleeping_process(entry.process_id,
  1076. PKT_TYPE);
  1077. }
  1078. } else {
  1079. if (len > 0) {
  1080. if (entry.client_id < NUM_SMD_DATA_CHANNELS) {
  1081. struct diag_smd_info *smd_info;
  1082. int index = entry.client_id;
  1083. if (!driver->rcvd_feature_mask[
  1084. entry.client_id]) {
  1085. pr_debug("diag: In %s, feature mask for peripheral: %d not received yet\n",
  1086. __func__, entry.client_id);
  1087. return 0;
  1088. }
  1089. smd_info = (driver->separate_cmdrsp[index] &&
  1090. index < NUM_SMD_CMD_CHANNELS) ?
  1091. &driver->smd_cmd[index] :
  1092. &driver->smd_data[index];
  1093. if (smd_info->ch) {
  1094. mutex_lock(&smd_info->smd_ch_mutex);
  1095. smd_write(smd_info->ch, buf, len);
  1096. mutex_unlock(&smd_info->smd_ch_mutex);
  1097. } else {
  1098. pr_err("diag: In %s, smd channel %d not open, peripheral: %d, type: %d\n",
  1099. __func__, index,
  1100. smd_info->peripheral,
  1101. smd_info->type);
  1102. }
  1103. } else {
  1104. pr_alert("diag: In %s, incorrect channel: %d",
  1105. __func__, entry.client_id);
  1106. success = 0;
  1107. }
  1108. }
  1109. }
  1110. return success;
  1111. }
  1112. void diag_process_stm_mask(uint8_t cmd, uint8_t data_mask, int data_type,
  1113. uint8_t *rsp_supported, uint8_t *rsp_smd_comply)
  1114. {
  1115. int status = 0;
  1116. if (data_type >= MODEM_DATA && data_type <= WCNSS_DATA) {
  1117. if (driver->peripheral_supports_stm[data_type]) {
  1118. status = diag_send_stm_state(
  1119. &driver->smd_cntl[data_type], cmd);
  1120. if (status == 1)
  1121. *rsp_smd_comply |= data_mask;
  1122. *rsp_supported |= data_mask;
  1123. } else if (driver->smd_cntl[data_type].ch) {
  1124. *rsp_smd_comply |= data_mask;
  1125. }
  1126. if ((*rsp_smd_comply & data_mask) &&
  1127. (*rsp_supported & data_mask))
  1128. driver->stm_state[data_type] = cmd;
  1129. driver->stm_state_requested[data_type] = cmd;
  1130. } else if (data_type == APPS_DATA) {
  1131. *rsp_supported |= data_mask;
  1132. *rsp_smd_comply |= data_mask;
  1133. driver->stm_state[data_type] = cmd;
  1134. driver->stm_state_requested[data_type] = cmd;
  1135. }
  1136. }
  1137. int diag_process_stm_cmd(unsigned char *buf, unsigned char *dest_buf)
  1138. {
  1139. uint8_t version, mask, cmd;
  1140. uint8_t rsp_supported = 0;
  1141. uint8_t rsp_smd_comply = 0;
  1142. int i;
  1143. if (!buf || !dest_buf) {
  1144. pr_err("diag: Invalid pointers buf: %p, dest_buf %p in %s\n",
  1145. buf, dest_buf, __func__);
  1146. return -EIO;
  1147. }
  1148. version = *(buf + STM_CMD_VERSION_OFFSET);
  1149. mask = *(buf + STM_CMD_MASK_OFFSET);
  1150. cmd = *(buf + STM_CMD_DATA_OFFSET);
  1151. /*
  1152. * Check if command is valid. If the command is asking for
  1153. * status, then the processor mask field is to be ignored.
  1154. */
  1155. if ((version != 1) || (cmd > STATUS_STM) ||
  1156. ((cmd != STATUS_STM) && ((mask == 0) || (0 != (mask >> 4))))) {
  1157. /* Command is invalid. Send bad param message response */
  1158. dest_buf[0] = BAD_PARAM_RESPONSE_MESSAGE;
  1159. for (i = 0; i < STM_CMD_NUM_BYTES; i++)
  1160. dest_buf[i+1] = *(buf + i);
  1161. return STM_CMD_NUM_BYTES+1;
  1162. } else if (cmd == STATUS_STM) {
  1163. /*
  1164. * Only the status is being queried, so fill in whether diag
  1165. * over stm is supported or not
  1166. */
  1167. for (i = 0; i < NUM_SMD_CONTROL_CHANNELS; i++)
  1168. if (driver->peripheral_supports_stm[i])
  1169. rsp_supported |= 1 << i;
  1170. rsp_supported |= DIAG_STM_APPS;
  1171. } else {
  1172. if (mask & DIAG_STM_MODEM)
  1173. diag_process_stm_mask(cmd, DIAG_STM_MODEM, MODEM_DATA,
  1174. &rsp_supported, &rsp_smd_comply);
  1175. if (mask & DIAG_STM_LPASS)
  1176. diag_process_stm_mask(cmd, DIAG_STM_LPASS, LPASS_DATA,
  1177. &rsp_supported, &rsp_smd_comply);
  1178. if (mask & DIAG_STM_WCNSS)
  1179. diag_process_stm_mask(cmd, DIAG_STM_WCNSS, WCNSS_DATA,
  1180. &rsp_supported, &rsp_smd_comply);
  1181. if (mask & DIAG_STM_APPS)
  1182. diag_process_stm_mask(cmd, DIAG_STM_APPS, APPS_DATA,
  1183. &rsp_supported, &rsp_smd_comply);
  1184. }
  1185. for (i = 0; i < STM_CMD_NUM_BYTES; i++)
  1186. dest_buf[i] = *(buf + i);
  1187. dest_buf[STM_RSP_VALID_INDEX] = STM_COMMAND_VALID;
  1188. dest_buf[STM_RSP_SUPPORTED_INDEX] = rsp_supported;
  1189. dest_buf[STM_RSP_SMD_COMPLY_INDEX] = rsp_smd_comply;
  1190. return STM_RSP_NUM_BYTES;
  1191. }
  1192. int diag_apps_responds()
  1193. {
  1194. if (chk_apps_only()) {
  1195. if (driver->smd_data[MODEM_DATA].ch &&
  1196. driver->rcvd_feature_mask[MODEM_DATA]) {
  1197. return 0;
  1198. }
  1199. return 1;
  1200. }
  1201. return 0;
  1202. }
  1203. int diag_process_apps_pkt(unsigned char *buf, int len)
  1204. {
  1205. uint16_t subsys_cmd_code;
  1206. int subsys_id, ssid_first, ssid_last, ssid_range;
  1207. int packet_type = 1, i, cmd_code;
  1208. unsigned char *temp = buf;
  1209. int data_type;
  1210. int mask_ret;
  1211. int status = 0;
  1212. #if defined(CONFIG_DIAG_OVER_USB)
  1213. unsigned char *ptr;
  1214. #endif
  1215. /* Check if the command is a supported mask command */
  1216. mask_ret = diag_process_apps_masks(buf, len);
  1217. if (mask_ret <= 0)
  1218. return mask_ret;
  1219. /* Check for registered clients and forward packet to apropriate proc */
  1220. cmd_code = (int)(*(char *)buf);
  1221. temp++;
  1222. subsys_id = (int)(*(char *)temp);
  1223. temp++;
  1224. subsys_cmd_code = *(uint16_t *)temp;
  1225. temp += 2;
  1226. data_type = APPS_DATA;
  1227. /* Dont send any command other than mode reset */
  1228. if (chk_apps_master() && cmd_code == MODE_CMD) {
  1229. if (subsys_id != RESET_ID)
  1230. data_type = MODEM_DATA;
  1231. }
  1232. pr_debug("diag: %d %d %d", cmd_code, subsys_id, subsys_cmd_code);
  1233. for (i = 0; i < diag_max_reg; i++) {
  1234. entry = driver->table[i];
  1235. if (entry.process_id != NO_PROCESS) {
  1236. if (entry.cmd_code == cmd_code && entry.subsys_id ==
  1237. subsys_id && entry.cmd_code_lo <=
  1238. subsys_cmd_code &&
  1239. entry.cmd_code_hi >= subsys_cmd_code) {
  1240. status = diag_send_data(entry, buf, len,
  1241. data_type);
  1242. if (status)
  1243. packet_type = 0;
  1244. } else if (entry.cmd_code == 255
  1245. && cmd_code == 75) {
  1246. if (entry.subsys_id ==
  1247. subsys_id &&
  1248. entry.cmd_code_lo <=
  1249. subsys_cmd_code &&
  1250. entry.cmd_code_hi >=
  1251. subsys_cmd_code) {
  1252. status = diag_send_data(entry, buf,
  1253. len, data_type);
  1254. if (status)
  1255. packet_type = 0;
  1256. }
  1257. } else if (entry.cmd_code == 255 &&
  1258. entry.subsys_id == 255) {
  1259. if (entry.cmd_code_lo <=
  1260. cmd_code &&
  1261. entry.
  1262. cmd_code_hi >= cmd_code) {
  1263. if (cmd_code == MODE_CMD &&
  1264. subsys_id == RESET_ID &&
  1265. entry.process_id ==
  1266. NON_APPS_PROC)
  1267. continue;
  1268. status = diag_send_data(entry, buf, len,
  1269. data_type);
  1270. if (status)
  1271. packet_type = 0;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. #if defined(CONFIG_DIAG_OVER_USB)
  1277. /* Check for the command/respond msg for the maximum packet length */
  1278. if ((*buf == 0x4b) && (*(buf+1) == 0x12) &&
  1279. (*(uint16_t *)(buf+2) == 0x0055)) {
  1280. for (i = 0; i < 4; i++)
  1281. *(driver->apps_rsp_buf+i) = *(buf+i);
  1282. *(uint32_t *)(driver->apps_rsp_buf+4) = PKT_SIZE;
  1283. encode_rsp_and_send(7);
  1284. return 0;
  1285. } else if ((*buf == 0x4b) && (*(buf+1) == 0x12) &&
  1286. (*(uint16_t *)(buf+2) == DIAG_DIAG_STM)) {
  1287. len = diag_process_stm_cmd(buf, driver->apps_rsp_buf);
  1288. if (len > 0) {
  1289. encode_rsp_and_send(len - 1);
  1290. return 0;
  1291. }
  1292. return len;
  1293. }
  1294. /* Check for Apps Only & get event mask request */
  1295. else if (diag_apps_responds() && *buf == 0x81) {
  1296. driver->apps_rsp_buf[0] = 0x81;
  1297. driver->apps_rsp_buf[1] = 0x0;
  1298. *(uint16_t *)(driver->apps_rsp_buf + 2) = 0x0;
  1299. *(uint16_t *)(driver->apps_rsp_buf + 4) = EVENT_LAST_ID + 1;
  1300. for (i = 0; i < EVENT_LAST_ID/8 + 1; i++)
  1301. *(unsigned char *)(driver->apps_rsp_buf + 6 + i) = 0x0;
  1302. encode_rsp_and_send(6 + EVENT_LAST_ID/8);
  1303. return 0;
  1304. }
  1305. /* Get log ID range & Check for Apps Only */
  1306. else if (diag_apps_responds() && (*buf == 0x73) &&
  1307. *(int *)(buf+4) == 1) {
  1308. driver->apps_rsp_buf[0] = 0x73;
  1309. *(int *)(driver->apps_rsp_buf + 4) = 0x1; /* operation ID */
  1310. *(int *)(driver->apps_rsp_buf + 8) = 0x0; /* success code */
  1311. *(int *)(driver->apps_rsp_buf + 12) =
  1312. LOG_GET_ITEM_NUM(log_code_last_tbl[0]);
  1313. *(int *)(driver->apps_rsp_buf + 16) =
  1314. LOG_GET_ITEM_NUM(log_code_last_tbl[1]);
  1315. *(int *)(driver->apps_rsp_buf + 20) =
  1316. LOG_GET_ITEM_NUM(log_code_last_tbl[2]);
  1317. *(int *)(driver->apps_rsp_buf + 24) =
  1318. LOG_GET_ITEM_NUM(log_code_last_tbl[3]);
  1319. *(int *)(driver->apps_rsp_buf + 28) =
  1320. LOG_GET_ITEM_NUM(log_code_last_tbl[4]);
  1321. *(int *)(driver->apps_rsp_buf + 32) =
  1322. LOG_GET_ITEM_NUM(log_code_last_tbl[5]);
  1323. *(int *)(driver->apps_rsp_buf + 36) =
  1324. LOG_GET_ITEM_NUM(log_code_last_tbl[6]);
  1325. *(int *)(driver->apps_rsp_buf + 40) =
  1326. LOG_GET_ITEM_NUM(log_code_last_tbl[7]);
  1327. *(int *)(driver->apps_rsp_buf + 44) =
  1328. LOG_GET_ITEM_NUM(log_code_last_tbl[8]);
  1329. *(int *)(driver->apps_rsp_buf + 48) =
  1330. LOG_GET_ITEM_NUM(log_code_last_tbl[9]);
  1331. *(int *)(driver->apps_rsp_buf + 52) =
  1332. LOG_GET_ITEM_NUM(log_code_last_tbl[10]);
  1333. *(int *)(driver->apps_rsp_buf + 56) =
  1334. LOG_GET_ITEM_NUM(log_code_last_tbl[11]);
  1335. *(int *)(driver->apps_rsp_buf + 60) =
  1336. LOG_GET_ITEM_NUM(log_code_last_tbl[12]);
  1337. *(int *)(driver->apps_rsp_buf + 64) =
  1338. LOG_GET_ITEM_NUM(log_code_last_tbl[13]);
  1339. *(int *)(driver->apps_rsp_buf + 68) =
  1340. LOG_GET_ITEM_NUM(log_code_last_tbl[14]);
  1341. *(int *)(driver->apps_rsp_buf + 72) =
  1342. LOG_GET_ITEM_NUM(log_code_last_tbl[15]);
  1343. encode_rsp_and_send(75);
  1344. return 0;
  1345. }
  1346. /* Respond to Get SSID Range request message */
  1347. else if (diag_apps_responds() && (*buf == 0x7d) &&
  1348. (*(buf+1) == 0x1)) {
  1349. driver->apps_rsp_buf[0] = 0x7d;
  1350. driver->apps_rsp_buf[1] = 0x1;
  1351. driver->apps_rsp_buf[2] = 0x1;
  1352. driver->apps_rsp_buf[3] = 0x0;
  1353. /* -1 to un-account for OEM SSID range */
  1354. *(int *)(driver->apps_rsp_buf + 4) = MSG_MASK_TBL_CNT - 1;
  1355. *(uint16_t *)(driver->apps_rsp_buf + 8) = MSG_SSID_0;
  1356. *(uint16_t *)(driver->apps_rsp_buf + 10) = MSG_SSID_0_LAST;
  1357. *(uint16_t *)(driver->apps_rsp_buf + 12) = MSG_SSID_1;
  1358. *(uint16_t *)(driver->apps_rsp_buf + 14) = MSG_SSID_1_LAST;
  1359. *(uint16_t *)(driver->apps_rsp_buf + 16) = MSG_SSID_2;
  1360. *(uint16_t *)(driver->apps_rsp_buf + 18) = MSG_SSID_2_LAST;
  1361. *(uint16_t *)(driver->apps_rsp_buf + 20) = MSG_SSID_3;
  1362. *(uint16_t *)(driver->apps_rsp_buf + 22) = MSG_SSID_3_LAST;
  1363. *(uint16_t *)(driver->apps_rsp_buf + 24) = MSG_SSID_4;
  1364. *(uint16_t *)(driver->apps_rsp_buf + 26) = MSG_SSID_4_LAST;
  1365. *(uint16_t *)(driver->apps_rsp_buf + 28) = MSG_SSID_5;
  1366. *(uint16_t *)(driver->apps_rsp_buf + 30) = MSG_SSID_5_LAST;
  1367. *(uint16_t *)(driver->apps_rsp_buf + 32) = MSG_SSID_6;
  1368. *(uint16_t *)(driver->apps_rsp_buf + 34) = MSG_SSID_6_LAST;
  1369. *(uint16_t *)(driver->apps_rsp_buf + 36) = MSG_SSID_7;
  1370. *(uint16_t *)(driver->apps_rsp_buf + 38) = MSG_SSID_7_LAST;
  1371. *(uint16_t *)(driver->apps_rsp_buf + 40) = MSG_SSID_8;
  1372. *(uint16_t *)(driver->apps_rsp_buf + 42) = MSG_SSID_8_LAST;
  1373. *(uint16_t *)(driver->apps_rsp_buf + 44) = MSG_SSID_9;
  1374. *(uint16_t *)(driver->apps_rsp_buf + 46) = MSG_SSID_9_LAST;
  1375. *(uint16_t *)(driver->apps_rsp_buf + 48) = MSG_SSID_10;
  1376. *(uint16_t *)(driver->apps_rsp_buf + 50) = MSG_SSID_10_LAST;
  1377. *(uint16_t *)(driver->apps_rsp_buf + 52) = MSG_SSID_11;
  1378. *(uint16_t *)(driver->apps_rsp_buf + 54) = MSG_SSID_11_LAST;
  1379. *(uint16_t *)(driver->apps_rsp_buf + 56) = MSG_SSID_12;
  1380. *(uint16_t *)(driver->apps_rsp_buf + 58) = MSG_SSID_12_LAST;
  1381. *(uint16_t *)(driver->apps_rsp_buf + 60) = MSG_SSID_13;
  1382. *(uint16_t *)(driver->apps_rsp_buf + 62) = MSG_SSID_13_LAST;
  1383. *(uint16_t *)(driver->apps_rsp_buf + 64) = MSG_SSID_14;
  1384. *(uint16_t *)(driver->apps_rsp_buf + 66) = MSG_SSID_14_LAST;
  1385. *(uint16_t *)(driver->apps_rsp_buf + 68) = MSG_SSID_15;
  1386. *(uint16_t *)(driver->apps_rsp_buf + 70) = MSG_SSID_15_LAST;
  1387. *(uint16_t *)(driver->apps_rsp_buf + 72) = MSG_SSID_16;
  1388. *(uint16_t *)(driver->apps_rsp_buf + 74) = MSG_SSID_16_LAST;
  1389. *(uint16_t *)(driver->apps_rsp_buf + 76) = MSG_SSID_17;
  1390. *(uint16_t *)(driver->apps_rsp_buf + 78) = MSG_SSID_17_LAST;
  1391. *(uint16_t *)(driver->apps_rsp_buf + 80) = MSG_SSID_18;
  1392. *(uint16_t *)(driver->apps_rsp_buf + 82) = MSG_SSID_18_LAST;
  1393. *(uint16_t *)(driver->apps_rsp_buf + 84) = MSG_SSID_19;
  1394. *(uint16_t *)(driver->apps_rsp_buf + 86) = MSG_SSID_19_LAST;
  1395. *(uint16_t *)(driver->apps_rsp_buf + 88) = MSG_SSID_20;
  1396. *(uint16_t *)(driver->apps_rsp_buf + 90) = MSG_SSID_20_LAST;
  1397. *(uint16_t *)(driver->apps_rsp_buf + 92) = MSG_SSID_21;
  1398. *(uint16_t *)(driver->apps_rsp_buf + 94) = MSG_SSID_21_LAST;
  1399. *(uint16_t *)(driver->apps_rsp_buf + 96) = MSG_SSID_22;
  1400. *(uint16_t *)(driver->apps_rsp_buf + 98) = MSG_SSID_22_LAST;
  1401. *(uint16_t *)(driver->apps_rsp_buf + 100) = MSG_SSID_23;
  1402. *(uint16_t *)(driver->apps_rsp_buf + 102) = MSG_SSID_23_LAST;
  1403. encode_rsp_and_send(103);
  1404. return 0;
  1405. }
  1406. /* Check for Apps Only Respond to Get Subsys Build mask */
  1407. else if (diag_apps_responds() && (*buf == 0x7d) &&
  1408. (*(buf+1) == 0x2)) {
  1409. ssid_first = *(uint16_t *)(buf + 2);
  1410. ssid_last = *(uint16_t *)(buf + 4);
  1411. ssid_range = 4 * (ssid_last - ssid_first + 1);
  1412. /* frame response */
  1413. driver->apps_rsp_buf[0] = 0x7d;
  1414. driver->apps_rsp_buf[1] = 0x2;
  1415. *(uint16_t *)(driver->apps_rsp_buf + 2) = ssid_first;
  1416. *(uint16_t *)(driver->apps_rsp_buf + 4) = ssid_last;
  1417. driver->apps_rsp_buf[6] = 0x1;
  1418. driver->apps_rsp_buf[7] = 0x0;
  1419. ptr = driver->apps_rsp_buf + 8;
  1420. /* bld time masks */
  1421. switch (ssid_first) {
  1422. case MSG_SSID_0:
  1423. if (ssid_range > sizeof(msg_bld_masks_0)) {
  1424. pr_warning("diag: truncating ssid range for ssid 0");
  1425. ssid_range = sizeof(msg_bld_masks_0);
  1426. }
  1427. for (i = 0; i < ssid_range; i += 4)
  1428. *(int *)(ptr + i) = msg_bld_masks_0[i/4];
  1429. break;
  1430. case MSG_SSID_1:
  1431. if (ssid_range > sizeof(msg_bld_masks_1)) {
  1432. pr_warning("diag: truncating ssid range for ssid 1");
  1433. ssid_range = sizeof(msg_bld_masks_1);
  1434. }
  1435. for (i = 0; i < ssid_range; i += 4)
  1436. *(int *)(ptr + i) = msg_bld_masks_1[i/4];
  1437. break;
  1438. case MSG_SSID_2:
  1439. if (ssid_range > sizeof(msg_bld_masks_2)) {
  1440. pr_warning("diag: truncating ssid range for ssid 2");
  1441. ssid_range = sizeof(msg_bld_masks_2);
  1442. }
  1443. for (i = 0; i < ssid_range; i += 4)
  1444. *(int *)(ptr + i) = msg_bld_masks_2[i/4];
  1445. break;
  1446. case MSG_SSID_3:
  1447. if (ssid_range > sizeof(msg_bld_masks_3)) {
  1448. pr_warning("diag: truncating ssid range for ssid 3");
  1449. ssid_range = sizeof(msg_bld_masks_3);
  1450. }
  1451. for (i = 0; i < ssid_range; i += 4)
  1452. *(int *)(ptr + i) = msg_bld_masks_3[i/4];
  1453. break;
  1454. case MSG_SSID_4:
  1455. if (ssid_range > sizeof(msg_bld_masks_4)) {
  1456. pr_warning("diag: truncating ssid range for ssid 4");
  1457. ssid_range = sizeof(msg_bld_masks_4);
  1458. }
  1459. for (i = 0; i < ssid_range; i += 4)
  1460. *(int *)(ptr + i) = msg_bld_masks_4[i/4];
  1461. break;
  1462. case MSG_SSID_5:
  1463. if (ssid_range > sizeof(msg_bld_masks_5)) {
  1464. pr_warning("diag: truncating ssid range for ssid 5");
  1465. ssid_range = sizeof(msg_bld_masks_5);
  1466. }
  1467. for (i = 0; i < ssid_range; i += 4)
  1468. *(int *)(ptr + i) = msg_bld_masks_5[i/4];
  1469. break;
  1470. case MSG_SSID_6:
  1471. if (ssid_range > sizeof(msg_bld_masks_6)) {
  1472. pr_warning("diag: truncating ssid range for ssid 6");
  1473. ssid_range = sizeof(msg_bld_masks_6);
  1474. }
  1475. for (i = 0; i < ssid_range; i += 4)
  1476. *(int *)(ptr + i) = msg_bld_masks_6[i/4];
  1477. break;
  1478. case MSG_SSID_7:
  1479. if (ssid_range > sizeof(msg_bld_masks_7)) {
  1480. pr_warning("diag: truncating ssid range for ssid 7");
  1481. ssid_range = sizeof(msg_bld_masks_7);
  1482. }
  1483. for (i = 0; i < ssid_range; i += 4)
  1484. *(int *)(ptr + i) = msg_bld_masks_7[i/4];
  1485. break;
  1486. case MSG_SSID_8:
  1487. if (ssid_range > sizeof(msg_bld_masks_8)) {
  1488. pr_warning("diag: truncating ssid range for ssid 8");
  1489. ssid_range = sizeof(msg_bld_masks_8);
  1490. }
  1491. for (i = 0; i < ssid_range; i += 4)
  1492. *(int *)(ptr + i) = msg_bld_masks_8[i/4];
  1493. break;
  1494. case MSG_SSID_9:
  1495. if (ssid_range > sizeof(msg_bld_masks_9)) {
  1496. pr_warning("diag: truncating ssid range for ssid 9");
  1497. ssid_range = sizeof(msg_bld_masks_9);
  1498. }
  1499. for (i = 0; i < ssid_range; i += 4)
  1500. *(int *)(ptr + i) = msg_bld_masks_9[i/4];
  1501. break;
  1502. case MSG_SSID_10:
  1503. if (ssid_range > sizeof(msg_bld_masks_10)) {
  1504. pr_warning("diag: truncating ssid range for ssid 10");
  1505. ssid_range = sizeof(msg_bld_masks_10);
  1506. }
  1507. for (i = 0; i < ssid_range; i += 4)
  1508. *(int *)(ptr + i) = msg_bld_masks_10[i/4];
  1509. break;
  1510. case MSG_SSID_11:
  1511. if (ssid_range > sizeof(msg_bld_masks_11)) {
  1512. pr_warning("diag: truncating ssid range for ssid 11");
  1513. ssid_range = sizeof(msg_bld_masks_11);
  1514. }
  1515. for (i = 0; i < ssid_range; i += 4)
  1516. *(int *)(ptr + i) = msg_bld_masks_11[i/4];
  1517. break;
  1518. case MSG_SSID_12:
  1519. if (ssid_range > sizeof(msg_bld_masks_12)) {
  1520. pr_warning("diag: truncating ssid range for ssid 12");
  1521. ssid_range = sizeof(msg_bld_masks_12);
  1522. }
  1523. for (i = 0; i < ssid_range; i += 4)
  1524. *(int *)(ptr + i) = msg_bld_masks_12[i/4];
  1525. break;
  1526. case MSG_SSID_13:
  1527. if (ssid_range > sizeof(msg_bld_masks_13)) {
  1528. pr_warning("diag: truncating ssid range for ssid 13");
  1529. ssid_range = sizeof(msg_bld_masks_13);
  1530. }
  1531. for (i = 0; i < ssid_range; i += 4)
  1532. *(int *)(ptr + i) = msg_bld_masks_13[i/4];
  1533. break;
  1534. case MSG_SSID_14:
  1535. if (ssid_range > sizeof(msg_bld_masks_14)) {
  1536. pr_warning("diag: truncating ssid range for ssid 14");
  1537. ssid_range = sizeof(msg_bld_masks_14);
  1538. }
  1539. for (i = 0; i < ssid_range; i += 4)
  1540. *(int *)(ptr + i) = msg_bld_masks_14[i/4];
  1541. break;
  1542. case MSG_SSID_15:
  1543. if (ssid_range > sizeof(msg_bld_masks_15)) {
  1544. pr_warning("diag: truncating ssid range for ssid 15");
  1545. ssid_range = sizeof(msg_bld_masks_15);
  1546. }
  1547. for (i = 0; i < ssid_range; i += 4)
  1548. *(int *)(ptr + i) = msg_bld_masks_15[i/4];
  1549. break;
  1550. case MSG_SSID_16:
  1551. if (ssid_range > sizeof(msg_bld_masks_16)) {
  1552. pr_warning("diag: truncating ssid range for ssid 16");
  1553. ssid_range = sizeof(msg_bld_masks_16);
  1554. }
  1555. for (i = 0; i < ssid_range; i += 4)
  1556. *(int *)(ptr + i) = msg_bld_masks_16[i/4];
  1557. break;
  1558. case MSG_SSID_17:
  1559. if (ssid_range > sizeof(msg_bld_masks_17)) {
  1560. pr_warning("diag: truncating ssid range for ssid 17");
  1561. ssid_range = sizeof(msg_bld_masks_17);
  1562. }
  1563. for (i = 0; i < ssid_range; i += 4)
  1564. *(int *)(ptr + i) = msg_bld_masks_17[i/4];
  1565. break;
  1566. case MSG_SSID_18:
  1567. if (ssid_range > sizeof(msg_bld_masks_18)) {
  1568. pr_warning("diag: truncating ssid range for ssid 18");
  1569. ssid_range = sizeof(msg_bld_masks_18);
  1570. }
  1571. for (i = 0; i < ssid_range; i += 4)
  1572. *(int *)(ptr + i) = msg_bld_masks_18[i/4];
  1573. break;
  1574. case MSG_SSID_19:
  1575. if (ssid_range > sizeof(msg_bld_masks_19)) {
  1576. pr_warning("diag: truncating ssid range for ssid 19");
  1577. ssid_range = sizeof(msg_bld_masks_19);
  1578. }
  1579. for (i = 0; i < ssid_range; i += 4)
  1580. *(int *)(ptr + i) = msg_bld_masks_19[i/4];
  1581. break;
  1582. case MSG_SSID_20:
  1583. if (ssid_range > sizeof(msg_bld_masks_20)) {
  1584. pr_warning("diag: truncating ssid range for ssid 20");
  1585. ssid_range = sizeof(msg_bld_masks_20);
  1586. }
  1587. for (i = 0; i < ssid_range; i += 4)
  1588. *(int *)(ptr + i) = msg_bld_masks_20[i/4];
  1589. break;
  1590. case MSG_SSID_21:
  1591. if (ssid_range > sizeof(msg_bld_masks_21)) {
  1592. pr_warning("diag: truncating ssid range for ssid 21");
  1593. ssid_range = sizeof(msg_bld_masks_21);
  1594. }
  1595. for (i = 0; i < ssid_range; i += 4)
  1596. *(int *)(ptr + i) = msg_bld_masks_21[i/4];
  1597. break;
  1598. case MSG_SSID_22:
  1599. if (ssid_range > sizeof(msg_bld_masks_22)) {
  1600. pr_warning("diag: truncating ssid range for ssid 22");
  1601. ssid_range = sizeof(msg_bld_masks_22);
  1602. }
  1603. for (i = 0; i < ssid_range; i += 4)
  1604. *(int *)(ptr + i) = msg_bld_masks_22[i/4];
  1605. break;
  1606. }
  1607. encode_rsp_and_send(8 + ssid_range - 1);
  1608. return 0;
  1609. }
  1610. /* Check for download command */
  1611. else if ((cpu_is_msm8x60() || chk_apps_master()) && (*buf == 0x3A)) {
  1612. /* send response back */
  1613. driver->apps_rsp_buf[0] = *buf;
  1614. encode_rsp_and_send(0);
  1615. msleep(5000);
  1616. /* call download API */
  1617. msm_set_restart_mode(RESTART_DLOAD);
  1618. printk(KERN_CRIT "diag: download mode set, Rebooting SoC..\n");
  1619. kernel_restart(NULL);
  1620. /* Not required, represents that command isnt sent to modem */
  1621. return 0;
  1622. }
  1623. /* Check for polling for Apps only DIAG */
  1624. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1625. (*(buf+2) == 0x03)) {
  1626. /* If no one has registered for polling */
  1627. if (chk_polling_response()) {
  1628. /* Respond to polling for Apps only DIAG */
  1629. for (i = 0; i < 3; i++)
  1630. driver->apps_rsp_buf[i] = *(buf+i);
  1631. for (i = 0; i < 13; i++)
  1632. driver->apps_rsp_buf[i+3] = 0;
  1633. encode_rsp_and_send(15);
  1634. return 0;
  1635. }
  1636. }
  1637. /* Return the Delayed Response Wrap Status */
  1638. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1639. (*(buf+2) == 0x04) && (*(buf+3) == 0x0)) {
  1640. memcpy(driver->apps_rsp_buf, buf, 4);
  1641. driver->apps_rsp_buf[4] = wrap_enabled;
  1642. encode_rsp_and_send(4);
  1643. return 0;
  1644. }
  1645. /* Wrap the Delayed Rsp ID */
  1646. else if ((*buf == 0x4b) && (*(buf+1) == 0x32) &&
  1647. (*(buf+2) == 0x05) && (*(buf+3) == 0x0)) {
  1648. wrap_enabled = true;
  1649. memcpy(driver->apps_rsp_buf, buf, 4);
  1650. driver->apps_rsp_buf[4] = wrap_count;
  1651. encode_rsp_and_send(5);
  1652. return 0;
  1653. }
  1654. /* Check for ID for NO MODEM present */
  1655. else if (chk_polling_response()) {
  1656. /* respond to 0x0 command */
  1657. if (*buf == 0x00) {
  1658. for (i = 0; i < 55; i++)
  1659. driver->apps_rsp_buf[i] = 0;
  1660. encode_rsp_and_send(54);
  1661. return 0;
  1662. }
  1663. /* respond to 0x7c command */
  1664. else if (*buf == 0x7c) {
  1665. driver->apps_rsp_buf[0] = 0x7c;
  1666. for (i = 1; i < 8; i++)
  1667. driver->apps_rsp_buf[i] = 0;
  1668. /* Tools ID for APQ 8060 */
  1669. *(int *)(driver->apps_rsp_buf + 8) =
  1670. chk_config_get_id();
  1671. *(unsigned char *)(driver->apps_rsp_buf + 12) = '\0';
  1672. *(unsigned char *)(driver->apps_rsp_buf + 13) = '\0';
  1673. encode_rsp_and_send(13);
  1674. return 0;
  1675. }
  1676. }
  1677. #endif
  1678. return packet_type;
  1679. }
  1680. #ifdef CONFIG_DIAG_OVER_USB
  1681. void diag_send_error_rsp(int index)
  1682. {
  1683. int i;
  1684. /* -1 to accomodate the first byte 0x13 */
  1685. if (index > APPS_BUF_SIZE-1) {
  1686. pr_err("diag: cannot send err rsp, huge length: %d\n", index);
  1687. return;
  1688. }
  1689. driver->apps_rsp_buf[0] = 0x13; /* error code 13 */
  1690. for (i = 0; i < index; i++)
  1691. driver->apps_rsp_buf[i+1] = *(driver->hdlc_buf+i);
  1692. encode_rsp_and_send(index - 3);
  1693. }
  1694. #else
  1695. static inline void diag_send_error_rsp(int index) {}
  1696. #endif
  1697. void diag_process_hdlc(void *data, unsigned len)
  1698. {
  1699. struct diag_hdlc_decode_type hdlc;
  1700. int ret, type = 0, crc_chk = 0;
  1701. mutex_lock(&driver->diag_hdlc_mutex);
  1702. pr_debug("diag: HDLC decode fn, len of data %d\n", len);
  1703. hdlc.dest_ptr = driver->hdlc_buf;
  1704. hdlc.dest_size = USB_MAX_OUT_BUF;
  1705. hdlc.src_ptr = data;
  1706. hdlc.src_size = len;
  1707. hdlc.src_idx = 0;
  1708. hdlc.dest_idx = 0;
  1709. hdlc.escaping = 0;
  1710. ret = diag_hdlc_decode(&hdlc);
  1711. if (ret) {
  1712. crc_chk = crc_check(hdlc.dest_ptr, hdlc.dest_idx);
  1713. if (crc_chk) {
  1714. /* CRC check failed. */
  1715. pr_err_ratelimited("diag: In %s, bad CRC. Dropping packet\n",
  1716. __func__);
  1717. mutex_unlock(&driver->diag_hdlc_mutex);
  1718. return;
  1719. }
  1720. }
  1721. /*
  1722. * If the message is 3 bytes or less in length then the message is
  1723. * too short. A message will need 4 bytes minimum, since there are
  1724. * 2 bytes for the CRC and 1 byte for the ending 0x7e for the hdlc
  1725. * encoding
  1726. */
  1727. if (hdlc.dest_idx < 4) {
  1728. pr_err_ratelimited("diag: In %s, message is too short, len: %d, dest len: %d\n",
  1729. __func__, len, hdlc.dest_idx);
  1730. mutex_unlock(&driver->diag_hdlc_mutex);
  1731. return;
  1732. }
  1733. if (ret) {
  1734. type = diag_process_apps_pkt(driver->hdlc_buf,
  1735. hdlc.dest_idx - 3);
  1736. if (type < 0) {
  1737. mutex_unlock(&driver->diag_hdlc_mutex);
  1738. return;
  1739. }
  1740. } else if (driver->debug_flag) {
  1741. pr_err("diag: In %s, partial packet received, dropping packet, len: %d\n",
  1742. __func__, len);
  1743. print_hex_dump(KERN_DEBUG, "Dropped Packet Data: ", 16, 1,
  1744. DUMP_PREFIX_ADDRESS, data, len, 1);
  1745. driver->debug_flag = 0;
  1746. }
  1747. /* send error responses from APPS for Central Routing */
  1748. if (type == 1 && chk_apps_only()) {
  1749. diag_send_error_rsp(hdlc.dest_idx);
  1750. type = 0;
  1751. }
  1752. /* implies this packet is NOT meant for apps */
  1753. if (!(driver->smd_data[MODEM_DATA].ch) && type == 1) {
  1754. if (chk_apps_only()) {
  1755. diag_send_error_rsp(hdlc.dest_idx);
  1756. } else { /* APQ 8060, Let Q6 respond */
  1757. if (driver->smd_data[LPASS_DATA].ch) {
  1758. mutex_lock(&driver->smd_data[LPASS_DATA].
  1759. smd_ch_mutex);
  1760. smd_write(driver->smd_data[LPASS_DATA].ch,
  1761. driver->hdlc_buf,
  1762. hdlc.dest_idx - 3);
  1763. mutex_unlock(&driver->smd_data[LPASS_DATA].
  1764. smd_ch_mutex);
  1765. }
  1766. }
  1767. type = 0;
  1768. }
  1769. #ifdef DIAG_DEBUG
  1770. pr_debug("diag: hdlc.dest_idx = %d", hdlc.dest_idx);
  1771. for (i = 0; i < hdlc.dest_idx; i++)
  1772. printk(KERN_DEBUG "\t%x", *(((unsigned char *)
  1773. driver->hdlc_buf)+i));
  1774. #endif /* DIAG DEBUG */
  1775. /* ignore 2 bytes for CRC, one for 7E and send */
  1776. if ((driver->smd_data[MODEM_DATA].ch) && (ret) && (type) &&
  1777. (hdlc.dest_idx > 3)) {
  1778. APPEND_DEBUG('g');
  1779. mutex_lock(&driver->smd_data[MODEM_DATA].smd_ch_mutex);
  1780. smd_write(driver->smd_data[MODEM_DATA].ch,
  1781. driver->hdlc_buf, hdlc.dest_idx - 3);
  1782. mutex_unlock(&driver->smd_data[MODEM_DATA].smd_ch_mutex);
  1783. APPEND_DEBUG('h');
  1784. #ifdef DIAG_DEBUG
  1785. printk(KERN_INFO "writing data to SMD, pkt length %d\n", len);
  1786. print_hex_dump(KERN_DEBUG, "Written Packet Data to SMD: ", 16,
  1787. 1, DUMP_PREFIX_ADDRESS, data, len, 1);
  1788. #endif /* DIAG DEBUG */
  1789. }
  1790. mutex_unlock(&driver->diag_hdlc_mutex);
  1791. }
  1792. void diag_reset_smd_data(int queue)
  1793. {
  1794. int i;
  1795. unsigned long flags;
  1796. for (i = 0; i < NUM_SMD_DATA_CHANNELS; i++) {
  1797. spin_lock_irqsave(&driver->smd_data[i].in_busy_lock, flags);
  1798. driver->smd_data[i].in_busy_1 = 0;
  1799. driver->smd_data[i].in_busy_2 = 0;
  1800. spin_unlock_irqrestore(&driver->smd_data[i].in_busy_lock,
  1801. flags);
  1802. if (queue)
  1803. /* Poll SMD data channels to check for data */
  1804. queue_work(driver->smd_data[i].wq,
  1805. &(driver->smd_data[i].diag_read_smd_work));
  1806. }
  1807. if (driver->supports_separate_cmdrsp) {
  1808. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++) {
  1809. spin_lock_irqsave(&driver->smd_cmd[i].in_busy_lock,
  1810. flags);
  1811. driver->smd_cmd[i].in_busy_1 = 0;
  1812. driver->smd_cmd[i].in_busy_2 = 0;
  1813. spin_unlock_irqrestore(&driver->smd_cmd[i].in_busy_lock,
  1814. flags);
  1815. if (queue)
  1816. /* Poll SMD data channels to check for data */
  1817. queue_work(driver->diag_wq,
  1818. &(driver->smd_cmd[i].
  1819. diag_read_smd_work));
  1820. }
  1821. }
  1822. }
  1823. #ifdef CONFIG_DIAG_OVER_USB
  1824. /* 2+1 for modem ; 2 for LPASS ; 1 for WCNSS */
  1825. #define N_LEGACY_WRITE (driver->poolsize + 6)
  1826. /* Additionally support number of command data and dci channels */
  1827. #define N_LEGACY_WRITE_CMD ((N_LEGACY_WRITE) + 4)
  1828. #define N_LEGACY_READ 1
  1829. static void diag_usb_connect_work_fn(struct work_struct *w)
  1830. {
  1831. diagfwd_connect();
  1832. }
  1833. static void diag_usb_disconnect_work_fn(struct work_struct *w)
  1834. {
  1835. diagfwd_disconnect();
  1836. }
  1837. int diagfwd_connect(void)
  1838. {
  1839. int err;
  1840. int i;
  1841. printk(KERN_DEBUG "diag: USB connected\n");
  1842. err = usb_diag_alloc_req(driver->legacy_ch,
  1843. (driver->supports_separate_cmdrsp ?
  1844. N_LEGACY_WRITE_CMD : N_LEGACY_WRITE),
  1845. N_LEGACY_READ);
  1846. if (err)
  1847. goto exit;
  1848. driver->usb_connected = 1;
  1849. diag_reset_smd_data(RESET_AND_QUEUE);
  1850. for (i = 0; i < NUM_SMD_CONTROL_CHANNELS; i++) {
  1851. /* Poll SMD CNTL channels to check for data */
  1852. diag_smd_notify(&(driver->smd_cntl[i]), SMD_EVENT_DATA);
  1853. }
  1854. queue_work(driver->diag_real_time_wq,
  1855. &driver->diag_real_time_work);
  1856. /* Poll USB channel to check for data*/
  1857. queue_work(driver->diag_wq, &(driver->diag_read_work));
  1858. #ifdef CONFIG_DIAG_SDIO_PIPE
  1859. if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa()) {
  1860. if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
  1861. diagfwd_connect_sdio();
  1862. else
  1863. printk(KERN_INFO "diag: No USB MDM ch");
  1864. }
  1865. #endif
  1866. return 0;
  1867. exit:
  1868. pr_err("diag: unable to alloc USB req on legacy ch, err: %d", err);
  1869. return err;
  1870. }
  1871. int diagfwd_disconnect(void)
  1872. {
  1873. int i;
  1874. unsigned long flags;
  1875. struct diag_smd_info *smd_info = NULL;
  1876. printk(KERN_DEBUG "diag: USB disconnected\n");
  1877. driver->usb_connected = 0;
  1878. driver->debug_flag = 1;
  1879. if (driver->logging_mode == USB_MODE) {
  1880. for (i = 0; i < NUM_SMD_DATA_CHANNELS; i++) {
  1881. smd_info = &driver->smd_data[i];
  1882. spin_lock_irqsave(&smd_info->in_busy_lock, flags);
  1883. smd_info->in_busy_1 = 1;
  1884. smd_info->in_busy_2 = 1;
  1885. spin_unlock_irqrestore(&smd_info->in_busy_lock, flags);
  1886. }
  1887. if (driver->supports_separate_cmdrsp) {
  1888. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++) {
  1889. smd_info = &driver->smd_cmd[i];
  1890. spin_lock_irqsave(&smd_info->in_busy_lock,
  1891. flags);
  1892. smd_info->in_busy_1 = 1;
  1893. smd_info->in_busy_2 = 1;
  1894. spin_unlock_irqrestore(&smd_info->in_busy_lock,
  1895. flags);
  1896. }
  1897. }
  1898. }
  1899. queue_work(driver->diag_real_time_wq,
  1900. &driver->diag_real_time_work);
  1901. #ifdef CONFIG_DIAG_SDIO_PIPE
  1902. if (machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())
  1903. if (driver->mdm_ch && !IS_ERR(driver->mdm_ch))
  1904. diagfwd_disconnect_sdio();
  1905. #endif
  1906. /* TBD - notify and flow control SMD */
  1907. return 0;
  1908. }
  1909. static int diagfwd_check_buf_match(int num_channels,
  1910. struct diag_smd_info *data, unsigned char *buf)
  1911. {
  1912. int i;
  1913. int found_it = 0;
  1914. unsigned long flags;
  1915. spin_lock_irqsave(&data->in_busy_lock, flags);
  1916. for (i = 0; i < num_channels; i++) {
  1917. if (buf == (void *)data[i].buf_in_1) {
  1918. data[i].in_busy_1 = 0;
  1919. found_it = 1;
  1920. break;
  1921. } else if (buf == (void *)data[i].buf_in_2) {
  1922. data[i].in_busy_2 = 0;
  1923. found_it = 1;
  1924. break;
  1925. }
  1926. }
  1927. spin_unlock_irqrestore(&data->in_busy_lock, flags);
  1928. if (found_it) {
  1929. if (data[i].type == SMD_DATA_TYPE)
  1930. queue_work(data[i].wq,
  1931. &(data[i].diag_read_smd_work));
  1932. else
  1933. queue_work(driver->diag_wq,
  1934. &(data[i].diag_read_smd_work));
  1935. }
  1936. return found_it;
  1937. }
  1938. int diagfwd_write_complete(struct diag_request *diag_write_ptr)
  1939. {
  1940. unsigned char *buf = diag_write_ptr->buf;
  1941. int found_it = 0;
  1942. /* Determine if the write complete is for data from modem/apps/q6 */
  1943. found_it = diagfwd_check_buf_match(NUM_SMD_DATA_CHANNELS,
  1944. driver->smd_data, buf);
  1945. if (!found_it && driver->supports_separate_cmdrsp)
  1946. found_it = diagfwd_check_buf_match(NUM_SMD_CMD_CHANNELS,
  1947. driver->smd_cmd, buf);
  1948. #ifdef CONFIG_DIAG_SDIO_PIPE
  1949. if (!found_it) {
  1950. if (buf == (void *)driver->buf_in_sdio) {
  1951. if (machine_is_msm8x60_fusion() ||
  1952. machine_is_msm8x60_fusn_ffa())
  1953. diagfwd_write_complete_sdio();
  1954. else
  1955. pr_err("diag: Incorrect buffer pointer while WRITE");
  1956. found_it = 1;
  1957. }
  1958. }
  1959. #endif
  1960. if (!found_it) {
  1961. if (driver->logging_mode != USB_MODE)
  1962. pr_debug("diag: freeing buffer when not in usb mode\n");
  1963. diagmem_free(driver, (unsigned char *)buf,
  1964. POOL_TYPE_HDLC);
  1965. diagmem_free(driver, (unsigned char *)diag_write_ptr,
  1966. POOL_TYPE_WRITE_STRUCT);
  1967. }
  1968. return 0;
  1969. }
  1970. int diagfwd_read_complete(struct diag_request *diag_read_ptr)
  1971. {
  1972. int status = diag_read_ptr->status;
  1973. unsigned char *buf = diag_read_ptr->buf;
  1974. /* Determine if the read complete is for data on legacy/mdm ch */
  1975. if (buf == (void *)driver->usb_buf_out) {
  1976. driver->read_len_legacy = diag_read_ptr->actual;
  1977. APPEND_DEBUG('s');
  1978. #ifdef DIAG_DEBUG
  1979. printk(KERN_INFO "read data from USB, pkt length %d",
  1980. diag_read_ptr->actual);
  1981. print_hex_dump(KERN_DEBUG, "Read Packet Data from USB: ", 16, 1,
  1982. DUMP_PREFIX_ADDRESS, diag_read_ptr->buf,
  1983. diag_read_ptr->actual, 1);
  1984. #endif /* DIAG DEBUG */
  1985. if (driver->logging_mode == USB_MODE) {
  1986. if (status != -ECONNRESET && status != -ESHUTDOWN)
  1987. queue_work(driver->diag_wq,
  1988. &(driver->diag_proc_hdlc_work));
  1989. else
  1990. queue_work(driver->diag_wq,
  1991. &(driver->diag_read_work));
  1992. }
  1993. }
  1994. #ifdef CONFIG_DIAG_SDIO_PIPE
  1995. else if (buf == (void *)driver->usb_buf_mdm_out) {
  1996. if (machine_is_msm8x60_fusion() ||
  1997. machine_is_msm8x60_fusn_ffa()) {
  1998. driver->read_len_mdm = diag_read_ptr->actual;
  1999. diagfwd_read_complete_sdio();
  2000. } else
  2001. pr_err("diag: Incorrect buffer pointer while READ");
  2002. }
  2003. #endif
  2004. else
  2005. printk(KERN_ERR "diag: Unknown buffer ptr from USB");
  2006. return 0;
  2007. }
  2008. void diag_read_work_fn(struct work_struct *work)
  2009. {
  2010. APPEND_DEBUG('d');
  2011. driver->usb_read_ptr->buf = driver->usb_buf_out;
  2012. driver->usb_read_ptr->length = USB_MAX_OUT_BUF;
  2013. usb_diag_read(driver->legacy_ch, driver->usb_read_ptr);
  2014. APPEND_DEBUG('e');
  2015. }
  2016. void diag_process_hdlc_fn(struct work_struct *work)
  2017. {
  2018. APPEND_DEBUG('D');
  2019. diag_process_hdlc(driver->usb_buf_out, driver->read_len_legacy);
  2020. diag_read_work_fn(work);
  2021. APPEND_DEBUG('E');
  2022. }
  2023. void diag_usb_legacy_notifier(void *priv, unsigned event,
  2024. struct diag_request *d_req)
  2025. {
  2026. switch (event) {
  2027. case USB_DIAG_CONNECT:
  2028. queue_work(driver->diag_usb_wq,
  2029. &driver->diag_usb_connect_work);
  2030. break;
  2031. case USB_DIAG_DISCONNECT:
  2032. queue_work(driver->diag_usb_wq,
  2033. &driver->diag_usb_disconnect_work);
  2034. break;
  2035. case USB_DIAG_READ_DONE:
  2036. diagfwd_read_complete(d_req);
  2037. break;
  2038. case USB_DIAG_WRITE_DONE:
  2039. diagfwd_write_complete(d_req);
  2040. break;
  2041. default:
  2042. printk(KERN_ERR "Unknown event from USB diag\n");
  2043. break;
  2044. }
  2045. }
  2046. #endif /* DIAG OVER USB */
  2047. void diag_smd_notify(void *ctxt, unsigned event)
  2048. {
  2049. struct diag_smd_info *smd_info = (struct diag_smd_info *)ctxt;
  2050. if (!smd_info)
  2051. return;
  2052. if (event == SMD_EVENT_CLOSE) {
  2053. smd_info->ch = 0;
  2054. wake_up(&driver->smd_wait_q);
  2055. if (smd_info->type == SMD_DATA_TYPE) {
  2056. smd_info->notify_context = event;
  2057. queue_work(driver->diag_cntl_wq,
  2058. &(smd_info->diag_notify_update_smd_work));
  2059. } else if (smd_info->type == SMD_DCI_TYPE) {
  2060. /* Notify the clients of the close */
  2061. diag_dci_notify_client(smd_info->peripheral_mask,
  2062. DIAG_STATUS_CLOSED);
  2063. } else if (smd_info->type == SMD_CNTL_TYPE) {
  2064. diag_cntl_stm_notify(smd_info,
  2065. CLEAR_PERIPHERAL_STM_STATE);
  2066. }
  2067. return;
  2068. } else if (event == SMD_EVENT_OPEN) {
  2069. if (smd_info->ch_save)
  2070. smd_info->ch = smd_info->ch_save;
  2071. if (smd_info->type == SMD_CNTL_TYPE) {
  2072. smd_info->notify_context = event;
  2073. queue_work(driver->diag_cntl_wq,
  2074. &(smd_info->diag_notify_update_smd_work));
  2075. } else if (smd_info->type == SMD_DCI_TYPE) {
  2076. smd_info->notify_context = event;
  2077. queue_work(driver->diag_dci_wq,
  2078. &(smd_info->diag_notify_update_smd_work));
  2079. /* Notify the clients of the open */
  2080. diag_dci_notify_client(smd_info->peripheral_mask,
  2081. DIAG_STATUS_OPEN);
  2082. }
  2083. diag_smd_queue_read(smd_info);
  2084. wake_up(&driver->smd_wait_q);
  2085. } else if (event == SMD_EVENT_DATA) {
  2086. diag_smd_queue_read(smd_info);
  2087. wake_up(&driver->smd_wait_q);
  2088. if (smd_info->type == SMD_DCI_TYPE ||
  2089. smd_info->type == SMD_DCI_CMD_TYPE) {
  2090. diag_dci_try_activate_wakeup_source();
  2091. }
  2092. }
  2093. }
  2094. static int diag_smd_probe(struct platform_device *pdev)
  2095. {
  2096. int r = 0;
  2097. int index = -1;
  2098. const char *channel_name = NULL;
  2099. if (pdev->id == SMD_APPS_MODEM) {
  2100. index = MODEM_DATA;
  2101. channel_name = "DIAG";
  2102. }
  2103. #if defined(CONFIG_MSM_N_WAY_SMD)
  2104. else if (pdev->id == SMD_APPS_QDSP) {
  2105. index = LPASS_DATA;
  2106. channel_name = "DIAG";
  2107. }
  2108. #endif
  2109. else if (pdev->id == SMD_APPS_WCNSS) {
  2110. index = WCNSS_DATA;
  2111. channel_name = "APPS_RIVA_DATA";
  2112. }
  2113. if (index != -1) {
  2114. r = smd_named_open_on_edge(channel_name,
  2115. pdev->id,
  2116. &driver->smd_data[index].ch,
  2117. &driver->smd_data[index],
  2118. diag_smd_notify);
  2119. driver->smd_data[index].ch_save = driver->smd_data[index].ch;
  2120. }
  2121. pm_runtime_set_active(&pdev->dev);
  2122. pm_runtime_enable(&pdev->dev);
  2123. pr_debug("diag: In %s, open SMD port, Id = %d, r = %d\n",
  2124. __func__, pdev->id, r);
  2125. return 0;
  2126. }
  2127. static int diag_smd_cmd_probe(struct platform_device *pdev)
  2128. {
  2129. int r = 0;
  2130. int index = -1;
  2131. const char *channel_name = NULL;
  2132. if (!driver->supports_separate_cmdrsp)
  2133. return 0;
  2134. if (pdev->id == SMD_APPS_MODEM) {
  2135. index = MODEM_DATA;
  2136. channel_name = "DIAG_CMD";
  2137. }
  2138. if (index != -1) {
  2139. r = smd_named_open_on_edge(channel_name,
  2140. pdev->id,
  2141. &driver->smd_cmd[index].ch,
  2142. &driver->smd_cmd[index],
  2143. diag_smd_notify);
  2144. driver->smd_cmd[index].ch_save =
  2145. driver->smd_cmd[index].ch;
  2146. }
  2147. pr_debug("diag: In %s, open SMD CMD port, Id = %d, r = %d\n",
  2148. __func__, pdev->id, r);
  2149. return 0;
  2150. }
  2151. static int diag_smd_runtime_suspend(struct device *dev)
  2152. {
  2153. dev_dbg(dev, "pm_runtime: suspending...\n");
  2154. return 0;
  2155. }
  2156. static int diag_smd_runtime_resume(struct device *dev)
  2157. {
  2158. dev_dbg(dev, "pm_runtime: resuming...\n");
  2159. return 0;
  2160. }
  2161. static const struct dev_pm_ops diag_smd_dev_pm_ops = {
  2162. .runtime_suspend = diag_smd_runtime_suspend,
  2163. .runtime_resume = diag_smd_runtime_resume,
  2164. };
  2165. static struct platform_driver msm_smd_ch1_driver = {
  2166. .probe = diag_smd_probe,
  2167. .driver = {
  2168. .name = "DIAG",
  2169. .owner = THIS_MODULE,
  2170. .pm = &diag_smd_dev_pm_ops,
  2171. },
  2172. };
  2173. static struct platform_driver diag_smd_lite_driver = {
  2174. .probe = diag_smd_probe,
  2175. .driver = {
  2176. .name = "APPS_RIVA_DATA",
  2177. .owner = THIS_MODULE,
  2178. .pm = &diag_smd_dev_pm_ops,
  2179. },
  2180. };
  2181. static struct platform_driver
  2182. smd_lite_data_cmd_drivers[NUM_SMD_CMD_CHANNELS] = {
  2183. {
  2184. /* Modem data */
  2185. .probe = diag_smd_cmd_probe,
  2186. .driver = {
  2187. .name = "DIAG_CMD",
  2188. .owner = THIS_MODULE,
  2189. .pm = &diag_smd_dev_pm_ops,
  2190. },
  2191. }
  2192. };
  2193. int device_supports_separate_cmdrsp(void)
  2194. {
  2195. return driver->use_device_tree;
  2196. }
  2197. void diag_smd_destructor(struct diag_smd_info *smd_info)
  2198. {
  2199. if (smd_info->type == SMD_DATA_TYPE)
  2200. destroy_workqueue(smd_info->wq);
  2201. if (smd_info->ch)
  2202. smd_close(smd_info->ch);
  2203. smd_info->ch = 0;
  2204. smd_info->ch_save = 0;
  2205. kfree(smd_info->buf_in_1);
  2206. kfree(smd_info->buf_in_2);
  2207. kfree(smd_info->write_ptr_1);
  2208. kfree(smd_info->write_ptr_2);
  2209. kfree(smd_info->buf_in_1_raw);
  2210. kfree(smd_info->buf_in_2_raw);
  2211. }
  2212. int diag_smd_constructor(struct diag_smd_info *smd_info, int peripheral,
  2213. int type)
  2214. {
  2215. smd_info->peripheral = peripheral;
  2216. smd_info->type = type;
  2217. smd_info->encode_hdlc = 0;
  2218. mutex_init(&smd_info->smd_ch_mutex);
  2219. spin_lock_init(&smd_info->in_busy_lock);
  2220. switch (peripheral) {
  2221. case MODEM_DATA:
  2222. smd_info->peripheral_mask = DIAG_CON_MPSS;
  2223. break;
  2224. case LPASS_DATA:
  2225. smd_info->peripheral_mask = DIAG_CON_LPASS;
  2226. break;
  2227. case WCNSS_DATA:
  2228. smd_info->peripheral_mask = DIAG_CON_WCNSS;
  2229. break;
  2230. default:
  2231. pr_err("diag: In %s, unknown peripheral, peripheral: %d\n",
  2232. __func__, peripheral);
  2233. goto err;
  2234. }
  2235. smd_info->ch = 0;
  2236. smd_info->ch_save = 0;
  2237. if (smd_info->buf_in_1 == NULL) {
  2238. smd_info->buf_in_1 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
  2239. if (smd_info->buf_in_1 == NULL)
  2240. goto err;
  2241. smd_info->buf_in_1_size = IN_BUF_SIZE;
  2242. kmemleak_not_leak(smd_info->buf_in_1);
  2243. }
  2244. if (smd_info->write_ptr_1 == NULL) {
  2245. smd_info->write_ptr_1 = kzalloc(sizeof(struct diag_request),
  2246. GFP_KERNEL);
  2247. if (smd_info->write_ptr_1 == NULL)
  2248. goto err;
  2249. kmemleak_not_leak(smd_info->write_ptr_1);
  2250. }
  2251. /* The smd data type needs two buffers */
  2252. if (smd_info->type == SMD_DATA_TYPE) {
  2253. if (smd_info->buf_in_2 == NULL) {
  2254. smd_info->buf_in_2 = kzalloc(IN_BUF_SIZE, GFP_KERNEL);
  2255. if (smd_info->buf_in_2 == NULL)
  2256. goto err;
  2257. smd_info->buf_in_2_size = IN_BUF_SIZE;
  2258. kmemleak_not_leak(smd_info->buf_in_2);
  2259. }
  2260. if (smd_info->write_ptr_2 == NULL) {
  2261. smd_info->write_ptr_2 =
  2262. kzalloc(sizeof(struct diag_request),
  2263. GFP_KERNEL);
  2264. if (smd_info->write_ptr_2 == NULL)
  2265. goto err;
  2266. kmemleak_not_leak(smd_info->write_ptr_2);
  2267. }
  2268. if (driver->supports_apps_hdlc_encoding) {
  2269. /* In support of hdlc encoding */
  2270. if (smd_info->buf_in_1_raw == NULL) {
  2271. smd_info->buf_in_1_raw = kzalloc(IN_BUF_SIZE,
  2272. GFP_KERNEL);
  2273. if (smd_info->buf_in_1_raw == NULL)
  2274. goto err;
  2275. smd_info->buf_in_1_raw_size = IN_BUF_SIZE;
  2276. kmemleak_not_leak(smd_info->buf_in_1_raw);
  2277. }
  2278. if (smd_info->buf_in_2_raw == NULL) {
  2279. smd_info->buf_in_2_raw = kzalloc(IN_BUF_SIZE,
  2280. GFP_KERNEL);
  2281. if (smd_info->buf_in_2_raw == NULL)
  2282. goto err;
  2283. smd_info->buf_in_2_raw_size = IN_BUF_SIZE;
  2284. kmemleak_not_leak(smd_info->buf_in_2_raw);
  2285. }
  2286. }
  2287. }
  2288. if (smd_info->type == SMD_CMD_TYPE &&
  2289. driver->supports_apps_hdlc_encoding) {
  2290. /* In support of hdlc encoding */
  2291. if (smd_info->buf_in_1_raw == NULL) {
  2292. smd_info->buf_in_1_raw = kzalloc(IN_BUF_SIZE,
  2293. GFP_KERNEL);
  2294. if (smd_info->buf_in_1_raw == NULL)
  2295. goto err;
  2296. smd_info->buf_in_1_raw_size = IN_BUF_SIZE;
  2297. kmemleak_not_leak(smd_info->buf_in_1_raw);
  2298. }
  2299. }
  2300. /* The smd data type needs separate work queues for reads */
  2301. if (type == SMD_DATA_TYPE) {
  2302. switch (peripheral) {
  2303. case MODEM_DATA:
  2304. smd_info->wq = create_singlethread_workqueue(
  2305. "diag_modem_data_read_wq");
  2306. break;
  2307. case LPASS_DATA:
  2308. smd_info->wq = create_singlethread_workqueue(
  2309. "diag_lpass_data_read_wq");
  2310. break;
  2311. case WCNSS_DATA:
  2312. smd_info->wq = create_singlethread_workqueue(
  2313. "diag_wcnss_data_read_wq");
  2314. break;
  2315. default:
  2316. smd_info->wq = NULL;
  2317. break;
  2318. }
  2319. } else {
  2320. smd_info->wq = NULL;
  2321. }
  2322. INIT_WORK(&(smd_info->diag_read_smd_work), diag_read_smd_work_fn);
  2323. /*
  2324. * The update function assigned to the diag_notify_update_smd_work
  2325. * work_struct is meant to be used for updating that is not to
  2326. * be done in the context of the smd notify function. The
  2327. * notify_context variable can be used for passing additional
  2328. * information to the update function.
  2329. */
  2330. smd_info->notify_context = 0;
  2331. smd_info->general_context = 0;
  2332. switch (type) {
  2333. case SMD_DATA_TYPE:
  2334. case SMD_CMD_TYPE:
  2335. INIT_WORK(&(smd_info->diag_notify_update_smd_work),
  2336. diag_clean_reg_fn);
  2337. INIT_WORK(&(smd_info->diag_general_smd_work),
  2338. diag_cntl_smd_work_fn);
  2339. break;
  2340. case SMD_CNTL_TYPE:
  2341. INIT_WORK(&(smd_info->diag_notify_update_smd_work),
  2342. diag_mask_update_fn);
  2343. INIT_WORK(&(smd_info->diag_general_smd_work),
  2344. diag_cntl_smd_work_fn);
  2345. break;
  2346. case SMD_DCI_TYPE:
  2347. case SMD_DCI_CMD_TYPE:
  2348. INIT_WORK(&(smd_info->diag_notify_update_smd_work),
  2349. diag_update_smd_dci_work_fn);
  2350. INIT_WORK(&(smd_info->diag_general_smd_work),
  2351. diag_cntl_smd_work_fn);
  2352. break;
  2353. default:
  2354. pr_err("diag: In %s, unknown type, type: %d\n", __func__, type);
  2355. goto err;
  2356. }
  2357. /*
  2358. * Set function ptr for function to call to process the data that
  2359. * was just read from the smd channel
  2360. */
  2361. switch (type) {
  2362. case SMD_DATA_TYPE:
  2363. case SMD_CMD_TYPE:
  2364. smd_info->process_smd_read_data = diag_process_smd_read_data;
  2365. break;
  2366. case SMD_CNTL_TYPE:
  2367. smd_info->process_smd_read_data =
  2368. diag_process_smd_cntl_read_data;
  2369. break;
  2370. case SMD_DCI_TYPE:
  2371. case SMD_DCI_CMD_TYPE:
  2372. smd_info->process_smd_read_data =
  2373. diag_process_smd_dci_read_data;
  2374. break;
  2375. default:
  2376. pr_err("diag: In %s, unknown type, type: %d\n", __func__, type);
  2377. goto err;
  2378. }
  2379. return 1;
  2380. err:
  2381. kfree(smd_info->buf_in_1);
  2382. kfree(smd_info->buf_in_2);
  2383. kfree(smd_info->write_ptr_1);
  2384. kfree(smd_info->write_ptr_2);
  2385. kfree(smd_info->buf_in_1_raw);
  2386. kfree(smd_info->buf_in_2_raw);
  2387. return 0;
  2388. }
  2389. void diagfwd_init(void)
  2390. {
  2391. int success;
  2392. int i;
  2393. wrap_enabled = 0;
  2394. wrap_count = 0;
  2395. diag_debug_buf_idx = 0;
  2396. driver->read_len_legacy = 0;
  2397. driver->use_device_tree = has_device_tree();
  2398. driver->real_time_mode = 1;
  2399. /*
  2400. * The number of entries in table of buffers
  2401. * should not be any smaller than hdlc poolsize.
  2402. */
  2403. driver->buf_tbl_size = (buf_tbl_size < driver->poolsize_hdlc) ?
  2404. driver->poolsize_hdlc : buf_tbl_size;
  2405. driver->supports_separate_cmdrsp = device_supports_separate_cmdrsp();
  2406. driver->supports_apps_hdlc_encoding = 1;
  2407. mutex_init(&driver->diag_hdlc_mutex);
  2408. mutex_init(&driver->diag_cntl_mutex);
  2409. mutex_init(&driver->cmd_reg_mutex);
  2410. spin_lock_init(&driver->ws_lock);
  2411. driver->ws_ref_count = 0;
  2412. driver->copy_count = 0;
  2413. for (i = 0; i < NUM_SMD_CONTROL_CHANNELS; i++) {
  2414. driver->separate_cmdrsp[i] = 0;
  2415. driver->peripheral_supports_stm[i] = DISABLE_STM;
  2416. driver->rcvd_feature_mask[i] = 0;
  2417. }
  2418. for (i = 0; i < NUM_STM_PROCESSORS; i++) {
  2419. driver->stm_state_requested[i] = DISABLE_STM;
  2420. driver->stm_state[i] = DISABLE_STM;
  2421. }
  2422. for (i = 0; i < NUM_SMD_DATA_CHANNELS; i++) {
  2423. success = diag_smd_constructor(&driver->smd_data[i], i,
  2424. SMD_DATA_TYPE);
  2425. if (!success)
  2426. goto err;
  2427. }
  2428. if (driver->supports_separate_cmdrsp) {
  2429. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++) {
  2430. success = diag_smd_constructor(&driver->smd_cmd[i], i,
  2431. SMD_CMD_TYPE);
  2432. if (!success)
  2433. goto err;
  2434. }
  2435. }
  2436. if (driver->usb_buf_out == NULL &&
  2437. (driver->usb_buf_out = kzalloc(USB_MAX_OUT_BUF,
  2438. GFP_KERNEL)) == NULL)
  2439. goto err;
  2440. kmemleak_not_leak(driver->usb_buf_out);
  2441. if (driver->hdlc_buf == NULL
  2442. && (driver->hdlc_buf = kzalloc(HDLC_MAX, GFP_KERNEL)) == NULL)
  2443. goto err;
  2444. kmemleak_not_leak(driver->hdlc_buf);
  2445. if (driver->user_space_data_buf == NULL)
  2446. driver->user_space_data_buf = kzalloc(USER_SPACE_DATA,
  2447. GFP_KERNEL);
  2448. if (driver->user_space_data_buf == NULL)
  2449. goto err;
  2450. kmemleak_not_leak(driver->user_space_data_buf);
  2451. if (driver->client_map == NULL &&
  2452. (driver->client_map = kzalloc
  2453. ((driver->num_clients) * sizeof(struct diag_client_map),
  2454. GFP_KERNEL)) == NULL)
  2455. goto err;
  2456. kmemleak_not_leak(driver->client_map);
  2457. if (driver->buf_tbl == NULL)
  2458. driver->buf_tbl = kzalloc(driver->buf_tbl_size *
  2459. sizeof(struct diag_write_device), GFP_KERNEL);
  2460. if (driver->buf_tbl == NULL)
  2461. goto err;
  2462. kmemleak_not_leak(driver->buf_tbl);
  2463. if (driver->data_ready == NULL &&
  2464. (driver->data_ready = kzalloc(driver->num_clients * sizeof(int)
  2465. , GFP_KERNEL)) == NULL)
  2466. goto err;
  2467. kmemleak_not_leak(driver->data_ready);
  2468. if (driver->table == NULL &&
  2469. (driver->table = kzalloc(diag_max_reg*
  2470. sizeof(struct diag_master_table),
  2471. GFP_KERNEL)) == NULL)
  2472. goto err;
  2473. kmemleak_not_leak(driver->table);
  2474. if (driver->usb_read_ptr == NULL) {
  2475. driver->usb_read_ptr = kzalloc(
  2476. sizeof(struct diag_request), GFP_KERNEL);
  2477. if (driver->usb_read_ptr == NULL)
  2478. goto err;
  2479. kmemleak_not_leak(driver->usb_read_ptr);
  2480. }
  2481. if (driver->pkt_buf == NULL &&
  2482. (driver->pkt_buf = kzalloc(PKT_SIZE,
  2483. GFP_KERNEL)) == NULL)
  2484. goto err;
  2485. kmemleak_not_leak(driver->pkt_buf);
  2486. if (driver->dci_pkt_buf == NULL) {
  2487. driver->dci_pkt_buf = kzalloc(PKT_SIZE, GFP_KERNEL);
  2488. if (!driver->dci_pkt_buf)
  2489. goto err;
  2490. }
  2491. kmemleak_not_leak(driver->dci_pkt_buf);
  2492. if (driver->apps_rsp_buf == NULL) {
  2493. driver->apps_rsp_buf = kzalloc(APPS_BUF_SIZE, GFP_KERNEL);
  2494. if (driver->apps_rsp_buf == NULL)
  2495. goto err;
  2496. kmemleak_not_leak(driver->apps_rsp_buf);
  2497. }
  2498. driver->diag_wq = create_singlethread_workqueue("diag_wq");
  2499. driver->diag_usb_wq = create_singlethread_workqueue("diag_usb_wq");
  2500. #ifdef CONFIG_DIAG_OVER_USB
  2501. INIT_WORK(&(driver->diag_usb_connect_work),
  2502. diag_usb_connect_work_fn);
  2503. INIT_WORK(&(driver->diag_usb_disconnect_work),
  2504. diag_usb_disconnect_work_fn);
  2505. INIT_WORK(&(driver->diag_proc_hdlc_work), diag_process_hdlc_fn);
  2506. INIT_WORK(&(driver->diag_read_work), diag_read_work_fn);
  2507. driver->legacy_ch = usb_diag_open(DIAG_LEGACY, driver,
  2508. diag_usb_legacy_notifier);
  2509. if (IS_ERR(driver->legacy_ch)) {
  2510. printk(KERN_ERR "Unable to open USB diag legacy channel\n");
  2511. goto err;
  2512. }
  2513. #endif
  2514. platform_driver_register(&msm_smd_ch1_driver);
  2515. platform_driver_register(&diag_smd_lite_driver);
  2516. if (driver->supports_separate_cmdrsp) {
  2517. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++)
  2518. platform_driver_register(&smd_lite_data_cmd_drivers[i]);
  2519. }
  2520. return;
  2521. err:
  2522. pr_err("diag: Could not initialize diag buffers");
  2523. for (i = 0; i < NUM_SMD_DATA_CHANNELS; i++)
  2524. diag_smd_destructor(&driver->smd_data[i]);
  2525. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++)
  2526. diag_smd_destructor(&driver->smd_cmd[i]);
  2527. kfree(driver->buf_msg_mask_update);
  2528. kfree(driver->buf_log_mask_update);
  2529. kfree(driver->buf_event_mask_update);
  2530. kfree(driver->usb_buf_out);
  2531. kfree(driver->hdlc_buf);
  2532. kfree(driver->client_map);
  2533. kfree(driver->buf_tbl);
  2534. kfree(driver->data_ready);
  2535. kfree(driver->table);
  2536. kfree(driver->pkt_buf);
  2537. kfree(driver->dci_pkt_buf);
  2538. kfree(driver->usb_read_ptr);
  2539. kfree(driver->apps_rsp_buf);
  2540. kfree(driver->user_space_data_buf);
  2541. if (driver->diag_wq)
  2542. destroy_workqueue(driver->diag_wq);
  2543. if (driver->diag_usb_wq)
  2544. destroy_workqueue(driver->diag_usb_wq);
  2545. }
  2546. void diagfwd_exit(void)
  2547. {
  2548. int i;
  2549. for (i = 0; i < NUM_SMD_DATA_CHANNELS; i++)
  2550. diag_smd_destructor(&driver->smd_data[i]);
  2551. #ifdef CONFIG_DIAG_OVER_USB
  2552. usb_diag_close(driver->legacy_ch);
  2553. #endif
  2554. platform_driver_unregister(&msm_smd_ch1_driver);
  2555. platform_driver_unregister(&diag_smd_lite_driver);
  2556. if (driver->supports_separate_cmdrsp) {
  2557. for (i = 0; i < NUM_SMD_CMD_CHANNELS; i++) {
  2558. diag_smd_destructor(&driver->smd_cmd[i]);
  2559. platform_driver_unregister(
  2560. &smd_lite_data_cmd_drivers[i]);
  2561. }
  2562. }
  2563. kfree(driver->buf_msg_mask_update);
  2564. kfree(driver->buf_log_mask_update);
  2565. kfree(driver->buf_event_mask_update);
  2566. kfree(driver->usb_buf_out);
  2567. kfree(driver->hdlc_buf);
  2568. kfree(driver->client_map);
  2569. kfree(driver->buf_tbl);
  2570. kfree(driver->data_ready);
  2571. kfree(driver->table);
  2572. kfree(driver->pkt_buf);
  2573. kfree(driver->dci_pkt_buf);
  2574. kfree(driver->usb_read_ptr);
  2575. kfree(driver->apps_rsp_buf);
  2576. kfree(driver->user_space_data_buf);
  2577. destroy_workqueue(driver->diag_wq);
  2578. destroy_workqueue(driver->diag_usb_wq);
  2579. }