diag_dci.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318
  1. /* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/diagchar.h>
  16. #include <linux/sched.h>
  17. #include <linux/err.h>
  18. #include <linux/delay.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_wakeup.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/ratelimit.h>
  25. #include <linux/reboot.h>
  26. #include <asm/current.h>
  27. #include <soc/qcom/restart.h>
  28. #include <linux/vmalloc.h>
  29. #ifdef CONFIG_DIAG_OVER_USB
  30. #include <linux/usb/usbdiag.h>
  31. #endif
  32. #include "diagchar_hdlc.h"
  33. #include "diagmem.h"
  34. #include "diagchar.h"
  35. #include "diagfwd.h"
  36. #include "diagfwd_cntl.h"
  37. #include "diag_dci.h"
  38. #include "diag_masks.h"
  39. #include "diagfwd_bridge.h"
  40. #include "diagfwd_peripheral.h"
  41. #include "diag_ipc_logging.h"
  42. static struct timer_list dci_drain_timer;
  43. static int dci_timer_in_progress;
  44. static struct work_struct dci_data_drain_work;
  45. struct diag_dci_partial_pkt_t partial_pkt;
  46. unsigned int dci_max_reg = 100;
  47. unsigned int dci_max_clients = 10;
  48. struct mutex dci_log_mask_mutex;
  49. struct mutex dci_event_mask_mutex;
  50. /*
  51. * DCI_HANDSHAKE_RETRY_TIME: Time to wait (in microseconds) before checking the
  52. * connection status again.
  53. *
  54. * DCI_HANDSHAKE_WAIT_TIME: Timeout (in milliseconds) to check for dci
  55. * connection status
  56. */
  57. #define DCI_HANDSHAKE_RETRY_TIME 500000
  58. #define DCI_HANDSHAKE_WAIT_TIME 200
  59. spinlock_t ws_lock;
  60. unsigned long ws_lock_flags;
  61. struct dci_ops_tbl_t dci_ops_tbl[NUM_DCI_PROC] = {
  62. {
  63. .ctx = 0,
  64. .send_log_mask = diag_send_dci_log_mask,
  65. .send_event_mask = diag_send_dci_event_mask,
  66. .peripheral_status = 0,
  67. .mempool = 0,
  68. },
  69. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  70. {
  71. .ctx = DIAGFWD_MDM_DCI,
  72. .send_log_mask = diag_send_dci_log_mask_remote,
  73. .send_event_mask = diag_send_dci_event_mask_remote,
  74. .peripheral_status = 0,
  75. .mempool = POOL_TYPE_MDM_DCI_WRITE,
  76. }
  77. #endif
  78. };
  79. struct dci_channel_status_t dci_channel_status[NUM_DCI_PROC] = {
  80. {
  81. .id = 0,
  82. .open = 0,
  83. .retry_count = 0
  84. },
  85. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  86. {
  87. .id = DIAGFWD_MDM_DCI,
  88. .open = 0,
  89. .retry_count = 0
  90. }
  91. #endif
  92. };
  93. /* Number of milliseconds anticipated to process the DCI data */
  94. #define DCI_WAKEUP_TIMEOUT 1
  95. #define DCI_CAN_ADD_BUF_TO_LIST(buf) \
  96. (buf && buf->data && !buf->in_busy && buf->data_len > 0) \
  97. #ifdef CONFIG_DEBUG_FS
  98. struct diag_dci_data_info *dci_traffic;
  99. struct mutex dci_stat_mutex;
  100. void diag_dci_record_traffic(int read_bytes, uint8_t ch_type,
  101. uint8_t peripheral, uint8_t proc)
  102. {
  103. static int curr_dci_data;
  104. static unsigned long iteration;
  105. struct diag_dci_data_info *temp_data = dci_traffic;
  106. if (!temp_data)
  107. return;
  108. mutex_lock(&dci_stat_mutex);
  109. if (curr_dci_data == DIAG_DCI_DEBUG_CNT)
  110. curr_dci_data = 0;
  111. temp_data += curr_dci_data;
  112. temp_data->iteration = iteration + 1;
  113. temp_data->data_size = read_bytes;
  114. temp_data->peripheral = peripheral;
  115. temp_data->ch_type = ch_type;
  116. temp_data->proc = proc;
  117. diag_get_timestamp(temp_data->time_stamp);
  118. curr_dci_data++;
  119. iteration++;
  120. mutex_unlock(&dci_stat_mutex);
  121. }
  122. #else
  123. void diag_dci_record_traffic(int read_bytes, uint8_t ch_type,
  124. uint8_t peripheral, uint8_t proc) { }
  125. #endif
  126. static int check_peripheral_dci_support(int peripheral_id, int dci_proc_id)
  127. {
  128. int dci_peripheral_list = 0;
  129. if (dci_proc_id < 0 || dci_proc_id >= NUM_DCI_PROC) {
  130. pr_err("diag:In %s,not a supported DCI proc id\n", __func__);
  131. return 0;
  132. }
  133. if (peripheral_id < 0 || peripheral_id >= NUM_PERIPHERALS) {
  134. pr_err("diag:In %s,not a valid peripheral id\n", __func__);
  135. return 0;
  136. }
  137. dci_peripheral_list = dci_ops_tbl[dci_proc_id].peripheral_status;
  138. if (dci_peripheral_list <= 0 || dci_peripheral_list > DIAG_CON_ALL) {
  139. pr_err("diag:In %s,not a valid dci peripheral mask\n",
  140. __func__);
  141. return 0;
  142. }
  143. /* Remove APSS bit mask information */
  144. dci_peripheral_list = dci_peripheral_list >> 1;
  145. if ((1 << peripheral_id) & (dci_peripheral_list))
  146. return 1;
  147. else
  148. return 0;
  149. }
  150. static void create_dci_log_mask_tbl(unsigned char *mask, uint8_t dirty)
  151. {
  152. unsigned char *temp = mask;
  153. uint8_t i;
  154. if (!mask)
  155. return;
  156. /* create hard coded table for log mask with 16 categories */
  157. for (i = 0; i < DCI_MAX_LOG_CODES; i++) {
  158. *temp = i;
  159. temp++;
  160. *temp = dirty ? 1 : 0;
  161. temp++;
  162. memset(temp, 0, DCI_MAX_ITEMS_PER_LOG_CODE);
  163. temp += DCI_MAX_ITEMS_PER_LOG_CODE;
  164. }
  165. }
  166. static void create_dci_event_mask_tbl(unsigned char *tbl_buf)
  167. {
  168. if (tbl_buf)
  169. memset(tbl_buf, 0, DCI_EVENT_MASK_SIZE);
  170. }
  171. void dci_drain_data(unsigned long data)
  172. {
  173. queue_work(driver->diag_dci_wq, &dci_data_drain_work);
  174. }
  175. static void dci_check_drain_timer(void)
  176. {
  177. if (!dci_timer_in_progress) {
  178. dci_timer_in_progress = 1;
  179. mod_timer(&dci_drain_timer, jiffies + msecs_to_jiffies(200));
  180. }
  181. }
  182. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  183. static void dci_handshake_work_fn(struct work_struct *work)
  184. {
  185. int err = 0;
  186. int max_retries = 5;
  187. struct dci_channel_status_t *status = container_of(work,
  188. struct dci_channel_status_t,
  189. handshake_work);
  190. if (status->open) {
  191. pr_debug("diag: In %s, remote dci channel is open, index: %d\n",
  192. __func__, status->id);
  193. return;
  194. }
  195. if (status->retry_count == max_retries) {
  196. status->retry_count = 0;
  197. pr_info("diag: dci channel connection handshake timed out, id: %d\n",
  198. status->id);
  199. err = diagfwd_bridge_close(TOKEN_TO_BRIDGE(status->id));
  200. if (err) {
  201. pr_err("diag: In %s, unable to close dci channel id: %d, err: %d\n",
  202. __func__, status->id, err);
  203. }
  204. return;
  205. }
  206. status->retry_count++;
  207. /*
  208. * Sleep for sometime to check for the connection status again. The
  209. * value should be optimum to include a roundabout time for a small
  210. * packet to the remote processor.
  211. */
  212. usleep_range(DCI_HANDSHAKE_RETRY_TIME, DCI_HANDSHAKE_RETRY_TIME + 100);
  213. mod_timer(&status->wait_time,
  214. jiffies + msecs_to_jiffies(DCI_HANDSHAKE_WAIT_TIME));
  215. }
  216. static void dci_chk_handshake(unsigned long data)
  217. {
  218. int index = (int)data;
  219. if (index < 0 || index >= NUM_DCI_PROC)
  220. return;
  221. queue_work(driver->diag_dci_wq,
  222. &dci_channel_status[index].handshake_work);
  223. }
  224. #endif
  225. static int diag_dci_init_buffer(struct diag_dci_buffer_t *buffer, int type)
  226. {
  227. if (!buffer || buffer->data)
  228. return -EINVAL;
  229. switch (type) {
  230. case DCI_BUF_PRIMARY:
  231. buffer->capacity = IN_BUF_SIZE;
  232. buffer->data = vzalloc(buffer->capacity);
  233. if (!buffer->data)
  234. return -ENOMEM;
  235. break;
  236. case DCI_BUF_SECONDARY:
  237. buffer->data = NULL;
  238. buffer->capacity = IN_BUF_SIZE;
  239. break;
  240. case DCI_BUF_CMD:
  241. buffer->capacity = DIAG_MAX_REQ_SIZE + DCI_BUF_SIZE;
  242. buffer->data = vzalloc(buffer->capacity);
  243. if (!buffer->data)
  244. return -ENOMEM;
  245. break;
  246. default:
  247. pr_err("diag: In %s, unknown type %d", __func__, type);
  248. return -EINVAL;
  249. }
  250. buffer->data_len = 0;
  251. buffer->in_busy = 0;
  252. buffer->buf_type = type;
  253. mutex_init(&buffer->data_mutex);
  254. return 0;
  255. }
  256. static inline int diag_dci_check_buffer(struct diag_dci_buffer_t *buf, int len)
  257. {
  258. if (!buf)
  259. return -EINVAL;
  260. /* Return 1 if the buffer is not busy and can hold new data */
  261. if ((buf->data_len + len < buf->capacity) && !buf->in_busy)
  262. return 1;
  263. return 0;
  264. }
  265. static void dci_add_buffer_to_list(struct diag_dci_client_tbl *client,
  266. struct diag_dci_buffer_t *buf)
  267. {
  268. if (!buf || !client || !buf->data)
  269. return;
  270. if (buf->in_list || buf->data_len == 0)
  271. return;
  272. mutex_lock(&client->write_buf_mutex);
  273. list_add_tail(&buf->buf_track, &client->list_write_buf);
  274. /*
  275. * In the case of DCI, there can be multiple packets in one read. To
  276. * calculate the wakeup source reference count, we must account for each
  277. * packet in a single read.
  278. */
  279. diag_ws_on_read(DIAG_WS_DCI, buf->data_len);
  280. mutex_lock(&buf->data_mutex);
  281. buf->in_busy = 1;
  282. buf->in_list = 1;
  283. mutex_unlock(&buf->data_mutex);
  284. mutex_unlock(&client->write_buf_mutex);
  285. }
  286. static int diag_dci_get_buffer(struct diag_dci_client_tbl *client,
  287. int data_source, int len)
  288. {
  289. struct diag_dci_buffer_t *buf_primary = NULL;
  290. struct diag_dci_buffer_t *buf_temp = NULL;
  291. struct diag_dci_buffer_t *curr = NULL;
  292. if (!client)
  293. return -EINVAL;
  294. if (len < 0 || len > IN_BUF_SIZE)
  295. return -EINVAL;
  296. curr = client->buffers[data_source].buf_curr;
  297. buf_primary = client->buffers[data_source].buf_primary;
  298. if (curr && diag_dci_check_buffer(curr, len) == 1)
  299. return 0;
  300. dci_add_buffer_to_list(client, curr);
  301. client->buffers[data_source].buf_curr = NULL;
  302. if (diag_dci_check_buffer(buf_primary, len) == 1) {
  303. client->buffers[data_source].buf_curr = buf_primary;
  304. return 0;
  305. }
  306. buf_temp = kzalloc(sizeof(struct diag_dci_buffer_t), GFP_KERNEL);
  307. if (!buf_temp)
  308. return -EIO;
  309. if (!diag_dci_init_buffer(buf_temp, DCI_BUF_SECONDARY)) {
  310. buf_temp->data = diagmem_alloc(driver, IN_BUF_SIZE,
  311. POOL_TYPE_DCI);
  312. if (!buf_temp->data) {
  313. kfree(buf_temp);
  314. buf_temp = NULL;
  315. return -ENOMEM;
  316. }
  317. client->buffers[data_source].buf_curr = buf_temp;
  318. return 0;
  319. }
  320. kfree(buf_temp);
  321. buf_temp = NULL;
  322. return -EIO;
  323. }
  324. void diag_dci_wakeup_clients(void)
  325. {
  326. struct list_head *start, *temp;
  327. struct diag_dci_client_tbl *entry = NULL;
  328. mutex_lock(&driver->dci_mutex);
  329. list_for_each_safe(start, temp, &driver->dci_client_list) {
  330. entry = list_entry(start, struct diag_dci_client_tbl, track);
  331. /*
  332. * Don't wake up the client when there is no pending buffer to
  333. * write or when it is writing to user space
  334. */
  335. if (!list_empty(&entry->list_write_buf) && !entry->in_service) {
  336. mutex_lock(&entry->write_buf_mutex);
  337. entry->in_service = 1;
  338. mutex_unlock(&entry->write_buf_mutex);
  339. diag_update_sleeping_process(entry->client->tgid,
  340. DCI_DATA_TYPE);
  341. }
  342. }
  343. mutex_unlock(&driver->dci_mutex);
  344. }
  345. void dci_data_drain_work_fn(struct work_struct *work)
  346. {
  347. int i;
  348. struct list_head *start, *temp;
  349. struct diag_dci_client_tbl *entry = NULL;
  350. struct diag_dci_buf_peripheral_t *proc_buf = NULL;
  351. struct diag_dci_buffer_t *buf_temp = NULL;
  352. mutex_lock(&driver->dci_mutex);
  353. list_for_each_safe(start, temp, &driver->dci_client_list) {
  354. entry = list_entry(start, struct diag_dci_client_tbl, track);
  355. for (i = 0; i < entry->num_buffers; i++) {
  356. proc_buf = &entry->buffers[i];
  357. mutex_lock(&proc_buf->buf_mutex);
  358. buf_temp = proc_buf->buf_primary;
  359. if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp))
  360. dci_add_buffer_to_list(entry, buf_temp);
  361. buf_temp = proc_buf->buf_cmd;
  362. if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp))
  363. dci_add_buffer_to_list(entry, buf_temp);
  364. buf_temp = proc_buf->buf_curr;
  365. if (DCI_CAN_ADD_BUF_TO_LIST(buf_temp)) {
  366. dci_add_buffer_to_list(entry, buf_temp);
  367. proc_buf->buf_curr = NULL;
  368. }
  369. mutex_unlock(&proc_buf->buf_mutex);
  370. }
  371. if (!list_empty(&entry->list_write_buf) && !entry->in_service) {
  372. mutex_lock(&entry->write_buf_mutex);
  373. entry->in_service = 1;
  374. mutex_unlock(&entry->write_buf_mutex);
  375. diag_update_sleeping_process(entry->client->tgid,
  376. DCI_DATA_TYPE);
  377. }
  378. }
  379. mutex_unlock(&driver->dci_mutex);
  380. dci_timer_in_progress = 0;
  381. }
  382. static int diag_process_single_dci_pkt(unsigned char *buf, int len,
  383. int data_source, int token)
  384. {
  385. uint8_t cmd_code = 0;
  386. if (!buf || len < 0) {
  387. pr_err("diag: Invalid input in %s, buf: %pK, len: %d\n",
  388. __func__, buf, len);
  389. return -EIO;
  390. }
  391. cmd_code = *(uint8_t *)buf;
  392. switch (cmd_code) {
  393. case LOG_CMD_CODE:
  394. extract_dci_log(buf, len, data_source, token, NULL);
  395. break;
  396. case EVENT_CMD_CODE:
  397. extract_dci_events(buf, len, data_source, token, NULL);
  398. break;
  399. case EXT_HDR_CMD_CODE:
  400. extract_dci_ext_pkt(buf, len, data_source, token);
  401. break;
  402. case DCI_PKT_RSP_CODE:
  403. case DCI_DELAYED_RSP_CODE:
  404. extract_dci_pkt_rsp(buf, len, data_source, token);
  405. break;
  406. case DCI_CONTROL_PKT_CODE:
  407. extract_dci_ctrl_pkt(buf, len, token);
  408. break;
  409. default:
  410. pr_err("diag: Unable to process single DCI packet, cmd_code: %d, data_source: %d",
  411. cmd_code, data_source);
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. /* Process the data read from apps userspace client */
  417. void diag_process_apps_dci_read_data(int data_type, void *buf, int recd_bytes)
  418. {
  419. int err = 0;
  420. if (!buf) {
  421. pr_err_ratelimited("diag: In %s, Null buf pointer\n", __func__);
  422. return;
  423. }
  424. if (data_type != DATA_TYPE_DCI_LOG && data_type != DATA_TYPE_DCI_EVENT
  425. && data_type != DCI_PKT_TYPE) {
  426. pr_err("diag: In %s, unsupported data_type: 0x%x\n",
  427. __func__, (unsigned int)data_type);
  428. return;
  429. }
  430. err = diag_process_single_dci_pkt(buf, recd_bytes, APPS_DATA,
  431. DCI_LOCAL_PROC);
  432. if (err)
  433. return;
  434. /* wake up all sleeping DCI clients which have some data */
  435. diag_dci_wakeup_clients();
  436. dci_check_drain_timer();
  437. }
  438. void diag_process_remote_dci_read_data(int index, void *buf, int recd_bytes)
  439. {
  440. int read_bytes = 0, err = 0;
  441. uint16_t dci_pkt_len;
  442. struct diag_dci_header_t *header = NULL;
  443. int header_len = sizeof(struct diag_dci_header_t);
  444. int token = BRIDGE_TO_TOKEN(index);
  445. if (!buf)
  446. return;
  447. diag_dci_record_traffic(recd_bytes, 0, 0, token);
  448. if (!partial_pkt.processing)
  449. goto start;
  450. if (partial_pkt.remaining > recd_bytes) {
  451. if ((partial_pkt.read_len + recd_bytes) >
  452. (MAX_DCI_PACKET_SZ)) {
  453. pr_err("diag: Invalid length %d, %d received in %s\n",
  454. partial_pkt.read_len, recd_bytes, __func__);
  455. goto end;
  456. }
  457. memcpy(partial_pkt.data + partial_pkt.read_len, buf,
  458. recd_bytes);
  459. read_bytes += recd_bytes;
  460. buf += read_bytes;
  461. partial_pkt.read_len += recd_bytes;
  462. partial_pkt.remaining -= recd_bytes;
  463. } else {
  464. if ((partial_pkt.read_len + partial_pkt.remaining) >
  465. (MAX_DCI_PACKET_SZ)) {
  466. pr_err("diag: Invalid length during partial read %d, %d received in %s\n",
  467. partial_pkt.read_len,
  468. partial_pkt.remaining, __func__);
  469. goto end;
  470. }
  471. memcpy(partial_pkt.data + partial_pkt.read_len, buf,
  472. partial_pkt.remaining);
  473. read_bytes += partial_pkt.remaining;
  474. buf += read_bytes;
  475. partial_pkt.read_len += partial_pkt.remaining;
  476. partial_pkt.remaining = 0;
  477. }
  478. if (partial_pkt.remaining == 0) {
  479. /*
  480. * Retrieve from the DCI control packet after the header = start
  481. * (1 byte) + version (1 byte) + length (2 bytes)
  482. */
  483. diag_process_single_dci_pkt(partial_pkt.data + 4,
  484. partial_pkt.read_len - header_len,
  485. DCI_REMOTE_DATA, token);
  486. partial_pkt.read_len = 0;
  487. partial_pkt.total_len = 0;
  488. partial_pkt.processing = 0;
  489. goto start;
  490. }
  491. goto end;
  492. start:
  493. while (read_bytes < recd_bytes) {
  494. header = (struct diag_dci_header_t *)buf;
  495. dci_pkt_len = header->length;
  496. if (header->cmd_code != DCI_CONTROL_PKT_CODE &&
  497. driver->num_dci_client == 0) {
  498. read_bytes += header_len + dci_pkt_len;
  499. buf += header_len + dci_pkt_len;
  500. continue;
  501. }
  502. if (dci_pkt_len + header_len > MAX_DCI_PACKET_SZ) {
  503. pr_err("diag: Invalid length in the dci packet field %d\n",
  504. dci_pkt_len);
  505. break;
  506. }
  507. if ((dci_pkt_len + header_len) > (recd_bytes - read_bytes)) {
  508. partial_pkt.read_len = recd_bytes - read_bytes;
  509. partial_pkt.total_len = dci_pkt_len + header_len;
  510. partial_pkt.remaining = partial_pkt.total_len -
  511. partial_pkt.read_len;
  512. partial_pkt.processing = 1;
  513. memcpy(partial_pkt.data, buf, partial_pkt.read_len);
  514. break;
  515. }
  516. /*
  517. * Retrieve from the DCI control packet after the header = start
  518. * (1 byte) + version (1 byte) + length (2 bytes)
  519. */
  520. err = diag_process_single_dci_pkt(buf + 4, dci_pkt_len,
  521. DCI_REMOTE_DATA, DCI_MDM_PROC);
  522. if (err)
  523. break;
  524. read_bytes += header_len + dci_pkt_len;
  525. buf += header_len + dci_pkt_len; /* advance to next DCI pkt */
  526. }
  527. end:
  528. if (err)
  529. return;
  530. /* wake up all sleeping DCI clients which have some data */
  531. diag_dci_wakeup_clients();
  532. dci_check_drain_timer();
  533. }
  534. /* Process the data read from the peripheral dci channels */
  535. void diag_dci_process_peripheral_data(struct diagfwd_info *p_info, void *buf,
  536. int recd_bytes)
  537. {
  538. int read_bytes = 0, err = 0;
  539. uint16_t dci_pkt_len;
  540. struct diag_dci_pkt_header_t *header = NULL;
  541. uint8_t recv_pkt_cmd_code;
  542. if (!buf || !p_info)
  543. return;
  544. /*
  545. * Release wakeup source when there are no more clients to
  546. * process DCI data
  547. */
  548. if (driver->num_dci_client == 0) {
  549. diag_ws_reset(DIAG_WS_DCI);
  550. return;
  551. }
  552. diag_dci_record_traffic(recd_bytes, p_info->type, p_info->peripheral,
  553. DCI_LOCAL_PROC);
  554. while (read_bytes < recd_bytes) {
  555. header = (struct diag_dci_pkt_header_t *)buf;
  556. recv_pkt_cmd_code = header->pkt_code;
  557. dci_pkt_len = header->len;
  558. /*
  559. * Check if the length of the current packet is lesser than the
  560. * remaining bytes in the received buffer. This includes space
  561. * for the Start byte (1), Version byte (1), length bytes (2)
  562. * and End byte (1)
  563. */
  564. if ((dci_pkt_len + 5) > (recd_bytes - read_bytes)) {
  565. pr_err("diag: Invalid length in %s, len: %d, dci_pkt_len: %d",
  566. __func__, recd_bytes, dci_pkt_len);
  567. diag_ws_release();
  568. return;
  569. }
  570. /*
  571. * Retrieve from the DCI control packet after the header = start
  572. * (1 byte) + version (1 byte) + length (2 bytes)
  573. */
  574. err = diag_process_single_dci_pkt(buf + 4, dci_pkt_len,
  575. (int)p_info->peripheral,
  576. DCI_LOCAL_PROC);
  577. if (err) {
  578. diag_ws_release();
  579. break;
  580. }
  581. read_bytes += 5 + dci_pkt_len;
  582. buf += 5 + dci_pkt_len; /* advance to next DCI pkt */
  583. }
  584. if (err)
  585. return;
  586. /* wake up all sleeping DCI clients which have some data */
  587. diag_dci_wakeup_clients();
  588. dci_check_drain_timer();
  589. }
  590. int diag_dci_query_log_mask(struct diag_dci_client_tbl *entry,
  591. uint16_t log_code)
  592. {
  593. uint16_t item_num;
  594. uint8_t equip_id, *log_mask_ptr, byte_mask;
  595. int byte_index, offset;
  596. if (!entry) {
  597. pr_err("diag: In %s, invalid client entry\n", __func__);
  598. return 0;
  599. }
  600. equip_id = LOG_GET_EQUIP_ID(log_code);
  601. item_num = LOG_GET_ITEM_NUM(log_code);
  602. byte_index = item_num/8 + 2;
  603. byte_mask = 0x01 << (item_num % 8);
  604. offset = equip_id * 514;
  605. if (offset + byte_index >= DCI_LOG_MASK_SIZE) {
  606. pr_err("diag: In %s, invalid offset: %d, log_code: %d, byte_index: %d\n",
  607. __func__, offset, log_code, byte_index);
  608. return 0;
  609. }
  610. log_mask_ptr = entry->dci_log_mask;
  611. log_mask_ptr = log_mask_ptr + offset + byte_index;
  612. return ((*log_mask_ptr & byte_mask) == byte_mask) ? 1 : 0;
  613. }
  614. int diag_dci_query_event_mask(struct diag_dci_client_tbl *entry,
  615. uint16_t event_id)
  616. {
  617. uint8_t *event_mask_ptr, byte_mask;
  618. int byte_index, bit_index;
  619. if (!entry) {
  620. pr_err("diag: In %s, invalid client entry\n", __func__);
  621. return 0;
  622. }
  623. byte_index = event_id/8;
  624. bit_index = event_id % 8;
  625. byte_mask = 0x1 << bit_index;
  626. if (byte_index >= DCI_EVENT_MASK_SIZE) {
  627. pr_err("diag: In %s, invalid, event_id: %d, byte_index: %d\n",
  628. __func__, event_id, byte_index);
  629. return 0;
  630. }
  631. event_mask_ptr = entry->dci_event_mask;
  632. event_mask_ptr = event_mask_ptr + byte_index;
  633. return ((*event_mask_ptr & byte_mask) == byte_mask) ? 1 : 0;
  634. }
  635. static int diag_dci_filter_commands(struct diag_pkt_header_t *header)
  636. {
  637. if (!header)
  638. return -ENOMEM;
  639. switch (header->cmd_code) {
  640. case 0x7d: /* Msg Mask Configuration */
  641. case 0x73: /* Log Mask Configuration */
  642. case 0x81: /* Event Mask Configuration */
  643. case 0x82: /* Event Mask Change */
  644. case 0x60: /* Event Mask Toggle */
  645. return 1;
  646. }
  647. if (header->cmd_code == 0x4b && header->subsys_id == 0x12) {
  648. switch (header->subsys_cmd_code) {
  649. case 0x60: /* Extended Event Mask Config */
  650. case 0x61: /* Extended Msg Mask Config */
  651. case 0x62: /* Extended Log Mask Config */
  652. case 0x20C: /* Set current Preset ID */
  653. case 0x20D: /* Get current Preset ID */
  654. case 0x218: /* HDLC Disabled Command */
  655. return 1;
  656. }
  657. }
  658. return 0;
  659. }
  660. static struct dci_pkt_req_entry_t *diag_register_dci_transaction(int uid,
  661. int client_id)
  662. {
  663. struct dci_pkt_req_entry_t *entry = NULL;
  664. entry = kzalloc(sizeof(struct dci_pkt_req_entry_t), GFP_KERNEL);
  665. if (!entry)
  666. return NULL;
  667. driver->dci_tag++;
  668. entry->client_id = client_id;
  669. entry->uid = uid;
  670. entry->tag = driver->dci_tag;
  671. pr_debug("diag: Registering DCI cmd req, client_id: %d, uid: %d, tag:%d\n",
  672. entry->client_id, entry->uid, entry->tag);
  673. list_add_tail(&entry->track, &driver->dci_req_list);
  674. return entry;
  675. }
  676. static struct dci_pkt_req_entry_t *diag_dci_get_request_entry(int tag)
  677. {
  678. struct list_head *start, *temp;
  679. struct dci_pkt_req_entry_t *entry = NULL;
  680. list_for_each_safe(start, temp, &driver->dci_req_list) {
  681. entry = list_entry(start, struct dci_pkt_req_entry_t, track);
  682. if (entry->tag == tag)
  683. return entry;
  684. }
  685. return NULL;
  686. }
  687. static int diag_dci_remove_req_entry(unsigned char *buf, int len,
  688. struct dci_pkt_req_entry_t *entry)
  689. {
  690. uint16_t rsp_count = 0, delayed_rsp_id = 0;
  691. if (!buf || len <= 0 || !entry) {
  692. pr_err("diag: In %s, invalid input buf: %pK, len: %d, entry: %pK\n",
  693. __func__, buf, len, entry);
  694. return -EIO;
  695. }
  696. /* It is an immediate response, delete it from the table */
  697. if (*buf != 0x80) {
  698. list_del(&entry->track);
  699. kfree(entry);
  700. entry = NULL;
  701. return 1;
  702. }
  703. /* It is a delayed response. Check if the length is valid */
  704. if (len < MIN_DELAYED_RSP_LEN) {
  705. pr_err("diag: Invalid delayed rsp packet length %d\n", len);
  706. return -EINVAL;
  707. }
  708. /*
  709. * If the delayed response id field (uint16_t at byte 8) is 0 then
  710. * there is only one response and we can remove the request entry.
  711. */
  712. delayed_rsp_id = *(uint16_t *)(buf + 8);
  713. if (delayed_rsp_id == 0) {
  714. list_del(&entry->track);
  715. kfree(entry);
  716. entry = NULL;
  717. return 1;
  718. }
  719. /*
  720. * Check the response count field (uint16 at byte 10). The request
  721. * entry can be deleted it it is the last response in the sequence.
  722. * It is the last response in the sequence if the response count
  723. * is 1 or if the signed bit gets dropped.
  724. */
  725. rsp_count = *(uint16_t *)(buf + 10);
  726. if (rsp_count > 0 && rsp_count < 0x1000) {
  727. list_del(&entry->track);
  728. kfree(entry);
  729. entry = NULL;
  730. return 1;
  731. }
  732. return 0;
  733. }
  734. static void dci_process_ctrl_status(unsigned char *buf, int len, int token)
  735. {
  736. struct diag_ctrl_dci_status *header = NULL;
  737. unsigned char *temp = buf;
  738. uint32_t read_len = 0;
  739. uint8_t i;
  740. int peripheral_mask, status;
  741. if (!buf || (len < sizeof(struct diag_ctrl_dci_status))) {
  742. pr_err("diag: In %s, invalid buf %pK or length: %d\n",
  743. __func__, buf, len);
  744. return;
  745. }
  746. if (!VALID_DCI_TOKEN(token)) {
  747. pr_err("diag: In %s, invalid DCI token %d\n", __func__, token);
  748. return;
  749. }
  750. header = (struct diag_ctrl_dci_status *)temp;
  751. temp += sizeof(struct diag_ctrl_dci_status);
  752. read_len += sizeof(struct diag_ctrl_dci_status);
  753. for (i = 0; i < header->count; i++) {
  754. if (read_len > (len - 2)) {
  755. pr_err("diag: In %s, Invalid length len: %d\n",
  756. __func__, len);
  757. return;
  758. }
  759. switch (*(uint8_t *)temp) {
  760. case PERIPHERAL_MODEM:
  761. peripheral_mask = DIAG_CON_MPSS;
  762. break;
  763. case PERIPHERAL_LPASS:
  764. peripheral_mask = DIAG_CON_LPASS;
  765. break;
  766. case PERIPHERAL_WCNSS:
  767. peripheral_mask = DIAG_CON_WCNSS;
  768. break;
  769. case PERIPHERAL_SENSORS:
  770. peripheral_mask = DIAG_CON_SENSORS;
  771. break;
  772. default:
  773. pr_err("diag: In %s, unknown peripheral, peripheral: %d\n",
  774. __func__, *(uint8_t *)temp);
  775. return;
  776. }
  777. temp += sizeof(uint8_t);
  778. read_len += sizeof(uint8_t);
  779. status = (*(uint8_t *)temp) ? DIAG_STATUS_OPEN :
  780. DIAG_STATUS_CLOSED;
  781. temp += sizeof(uint8_t);
  782. read_len += sizeof(uint8_t);
  783. diag_dci_notify_client(peripheral_mask, status, token);
  784. }
  785. }
  786. static void dci_process_ctrl_handshake_pkt(unsigned char *buf, int len,
  787. int token)
  788. {
  789. struct diag_ctrl_dci_handshake_pkt *header = NULL;
  790. unsigned char *temp = buf;
  791. int err = 0;
  792. if (!buf || (len < sizeof(struct diag_ctrl_dci_handshake_pkt)))
  793. return;
  794. if (!VALID_DCI_TOKEN(token))
  795. return;
  796. header = (struct diag_ctrl_dci_handshake_pkt *)temp;
  797. if (header->magic == DCI_MAGIC) {
  798. dci_channel_status[token].open = 1;
  799. err = dci_ops_tbl[token].send_log_mask(token);
  800. if (err) {
  801. pr_err("diag: In %s, unable to send log mask to token: %d, err: %d\n",
  802. __func__, token, err);
  803. }
  804. err = dci_ops_tbl[token].send_event_mask(token);
  805. if (err) {
  806. pr_err("diag: In %s, unable to send event mask to token: %d, err: %d\n",
  807. __func__, token, err);
  808. }
  809. }
  810. }
  811. void extract_dci_ctrl_pkt(unsigned char *buf, int len, int token)
  812. {
  813. unsigned char *temp = buf;
  814. uint32_t ctrl_pkt_id;
  815. diag_ws_on_read(DIAG_WS_DCI, len);
  816. if (!buf) {
  817. pr_err("diag: Invalid buffer in %s\n", __func__);
  818. goto err;
  819. }
  820. if (len < (sizeof(uint8_t) + sizeof(uint32_t))) {
  821. pr_err("diag: In %s, invalid length %d\n", __func__, len);
  822. goto err;
  823. }
  824. /* Skip the Control packet command code */
  825. temp += sizeof(uint8_t);
  826. len -= sizeof(uint8_t);
  827. ctrl_pkt_id = *(uint32_t *)temp;
  828. switch (ctrl_pkt_id) {
  829. case DIAG_CTRL_MSG_DCI_CONNECTION_STATUS:
  830. dci_process_ctrl_status(temp, len, token);
  831. break;
  832. case DIAG_CTRL_MSG_DCI_HANDSHAKE_PKT:
  833. dci_process_ctrl_handshake_pkt(temp, len, token);
  834. break;
  835. default:
  836. pr_debug("diag: In %s, unknown control pkt %d\n",
  837. __func__, ctrl_pkt_id);
  838. break;
  839. }
  840. err:
  841. /*
  842. * DCI control packets are not consumed by the clients. Mimic client
  843. * consumption by setting and clearing the wakeup source copy_count
  844. * explicitly.
  845. */
  846. diag_ws_on_copy_fail(DIAG_WS_DCI);
  847. }
  848. void extract_dci_pkt_rsp(unsigned char *buf, int len, int data_source,
  849. int token)
  850. {
  851. int tag;
  852. struct diag_dci_client_tbl *entry = NULL;
  853. void *temp_buf = NULL;
  854. uint8_t dci_cmd_code, cmd_code_len, delete_flag = 0;
  855. uint32_t rsp_len = 0;
  856. struct diag_dci_buffer_t *rsp_buf = NULL;
  857. struct dci_pkt_req_entry_t *req_entry = NULL;
  858. unsigned char *temp = buf;
  859. int save_req_uid = 0;
  860. struct diag_dci_pkt_rsp_header_t pkt_rsp_header;
  861. int header_len = sizeof(struct diag_dci_pkt_rsp_header_t);
  862. if (!buf) {
  863. pr_err("diag: Invalid pointer in %s\n", __func__);
  864. return;
  865. }
  866. dci_cmd_code = *(uint8_t *)(temp);
  867. if (dci_cmd_code == DCI_PKT_RSP_CODE) {
  868. cmd_code_len = sizeof(uint8_t);
  869. } else if (dci_cmd_code == DCI_DELAYED_RSP_CODE) {
  870. cmd_code_len = sizeof(uint32_t);
  871. } else {
  872. pr_err("diag: In %s, invalid command code %d\n", __func__,
  873. dci_cmd_code);
  874. return;
  875. }
  876. temp += cmd_code_len;
  877. tag = *(int *)temp;
  878. temp += sizeof(int);
  879. /*
  880. * The size of the response is (total length) - (length of the command
  881. * code, the tag (int)
  882. */
  883. rsp_len = len - (cmd_code_len + sizeof(int));
  884. if ((rsp_len == 0) || (rsp_len > (len - 5))) {
  885. pr_err("diag: Invalid length in %s, len: %d, rsp_len: %d",
  886. __func__, len, rsp_len);
  887. return;
  888. }
  889. mutex_lock(&driver->dci_mutex);
  890. req_entry = diag_dci_get_request_entry(tag);
  891. if (!req_entry) {
  892. pr_err_ratelimited("diag: No matching client for DCI data\n");
  893. mutex_unlock(&driver->dci_mutex);
  894. return;
  895. }
  896. entry = diag_dci_get_client_entry(req_entry->client_id);
  897. if (!entry) {
  898. pr_err("diag: In %s, couldn't find client entry, id:%d\n",
  899. __func__, req_entry->client_id);
  900. mutex_unlock(&driver->dci_mutex);
  901. return;
  902. }
  903. save_req_uid = req_entry->uid;
  904. /* Remove the headers and send only the response to this function */
  905. delete_flag = diag_dci_remove_req_entry(temp, rsp_len, req_entry);
  906. if (delete_flag < 0) {
  907. mutex_unlock(&driver->dci_mutex);
  908. return;
  909. }
  910. mutex_lock(&entry->buffers[data_source].buf_mutex);
  911. rsp_buf = entry->buffers[data_source].buf_cmd;
  912. mutex_lock(&rsp_buf->data_mutex);
  913. /*
  914. * Check if we can fit the data in the rsp buffer. The total length of
  915. * the rsp is the rsp length (write_len) + dci response packet header
  916. * length (sizeof(struct diag_dci_pkt_rsp_header_t))
  917. */
  918. if ((rsp_buf->data_len + header_len + rsp_len) > rsp_buf->capacity) {
  919. pr_alert("diag: create capacity for pkt rsp\n");
  920. temp_buf = vzalloc(rsp_buf->capacity + header_len + rsp_len);
  921. if (!temp_buf) {
  922. pr_err("diag: DCI realloc failed\n");
  923. mutex_unlock(&rsp_buf->data_mutex);
  924. mutex_unlock(&entry->buffers[data_source].buf_mutex);
  925. mutex_unlock(&driver->dci_mutex);
  926. return;
  927. }
  928. rsp_buf->capacity += header_len + rsp_len;
  929. if (rsp_buf->capacity > rsp_buf->data_len)
  930. memcpy(temp_buf, rsp_buf->data, rsp_buf->data_len);
  931. vfree(rsp_buf->data);
  932. rsp_buf->data = temp_buf;
  933. }
  934. /* Fill in packet response header information */
  935. pkt_rsp_header.type = DCI_PKT_RSP_TYPE;
  936. /* Packet Length = Response Length + Length of uid field (int) */
  937. pkt_rsp_header.length = rsp_len + sizeof(int);
  938. pkt_rsp_header.delete_flag = delete_flag;
  939. pkt_rsp_header.uid = save_req_uid;
  940. memcpy(rsp_buf->data + rsp_buf->data_len, &pkt_rsp_header, header_len);
  941. rsp_buf->data_len += header_len;
  942. memcpy(rsp_buf->data + rsp_buf->data_len, temp, rsp_len);
  943. rsp_buf->data_len += rsp_len;
  944. rsp_buf->data_source = data_source;
  945. mutex_unlock(&rsp_buf->data_mutex);
  946. /*
  947. * Add directly to the list for writing responses to the
  948. * userspace as these shouldn't be buffered and shouldn't wait
  949. * for log and event buffers to be full
  950. */
  951. dci_add_buffer_to_list(entry, rsp_buf);
  952. mutex_unlock(&entry->buffers[data_source].buf_mutex);
  953. mutex_unlock(&driver->dci_mutex);
  954. }
  955. static void copy_ext_hdr(struct diag_dci_buffer_t *data_buffer, void *ext_hdr)
  956. {
  957. if (!data_buffer) {
  958. pr_err("diag: In %s, data buffer is NULL", __func__);
  959. return;
  960. }
  961. *(int *)(data_buffer->data + data_buffer->data_len) =
  962. DCI_EXT_HDR_TYPE;
  963. data_buffer->data_len += sizeof(int);
  964. memcpy(data_buffer->data + data_buffer->data_len, ext_hdr,
  965. EXT_HDR_LEN);
  966. data_buffer->data_len += EXT_HDR_LEN;
  967. }
  968. static void copy_dci_event(unsigned char *buf, int len,
  969. struct diag_dci_client_tbl *client, int data_source,
  970. void *ext_hdr)
  971. {
  972. struct diag_dci_buffer_t *data_buffer = NULL;
  973. struct diag_dci_buf_peripheral_t *proc_buf = NULL;
  974. int err = 0, total_len = 0;
  975. if (!buf || !client) {
  976. pr_err("diag: Invalid pointers in %s", __func__);
  977. return;
  978. }
  979. total_len = sizeof(int) + len;
  980. if (ext_hdr)
  981. total_len += sizeof(int) + EXT_HDR_LEN;
  982. proc_buf = &client->buffers[data_source];
  983. mutex_lock(&proc_buf->buf_mutex);
  984. mutex_lock(&proc_buf->health_mutex);
  985. err = diag_dci_get_buffer(client, data_source, total_len);
  986. if (err) {
  987. if (err == -ENOMEM)
  988. proc_buf->health.dropped_events++;
  989. else
  990. pr_err("diag: In %s, invalid packet\n", __func__);
  991. mutex_unlock(&proc_buf->health_mutex);
  992. mutex_unlock(&proc_buf->buf_mutex);
  993. return;
  994. }
  995. data_buffer = proc_buf->buf_curr;
  996. proc_buf->health.received_events++;
  997. mutex_unlock(&proc_buf->health_mutex);
  998. mutex_unlock(&proc_buf->buf_mutex);
  999. mutex_lock(&data_buffer->data_mutex);
  1000. if (ext_hdr)
  1001. copy_ext_hdr(data_buffer, ext_hdr);
  1002. *(int *)(data_buffer->data + data_buffer->data_len) = DCI_EVENT_TYPE;
  1003. data_buffer->data_len += sizeof(int);
  1004. memcpy(data_buffer->data + data_buffer->data_len, buf, len);
  1005. data_buffer->data_len += len;
  1006. data_buffer->data_source = data_source;
  1007. mutex_unlock(&data_buffer->data_mutex);
  1008. }
  1009. void extract_dci_events(unsigned char *buf, int len, int data_source,
  1010. int token, void *ext_hdr)
  1011. {
  1012. uint16_t event_id, event_id_packet, length, temp_len;
  1013. uint8_t payload_len, payload_len_field;
  1014. uint8_t timestamp[8] = {0}, timestamp_len;
  1015. unsigned char event_data[MAX_EVENT_SIZE];
  1016. unsigned int total_event_len;
  1017. struct list_head *start, *temp;
  1018. struct diag_dci_client_tbl *entry = NULL;
  1019. if (!buf) {
  1020. pr_err("diag: In %s buffer is NULL\n", __func__);
  1021. return;
  1022. }
  1023. /*
  1024. * 1 byte for event code and 2 bytes for the length field.
  1025. * The length field indicates the total length removing the cmd_code
  1026. * and the length field. The event parsing in that case should happen
  1027. * till the end.
  1028. */
  1029. if (len < 3) {
  1030. pr_err("diag: In %s invalid len: %d\n", __func__, len);
  1031. return;
  1032. }
  1033. length = *(uint16_t *)(buf + 1); /* total length of event series */
  1034. if ((length == 0) || (len != (length + 3))) {
  1035. pr_err("diag: Incoming dci event length: %d is invalid\n",
  1036. length);
  1037. return;
  1038. }
  1039. /*
  1040. * Move directly to the start of the event series.
  1041. * The event parsing should happen from start of event
  1042. * series till the end.
  1043. */
  1044. temp_len = 3;
  1045. while (temp_len < length) {
  1046. event_id_packet = *(uint16_t *)(buf + temp_len);
  1047. event_id = event_id_packet & 0x0FFF; /* extract 12 bits */
  1048. if (event_id_packet & 0x8000) {
  1049. /* The packet has the two smallest byte of the
  1050. * timestamp
  1051. */
  1052. timestamp_len = 2;
  1053. } else {
  1054. /* The packet has the full timestamp. The first event
  1055. * will always have full timestamp. Save it in the
  1056. * timestamp buffer and use it for subsequent events if
  1057. * necessary.
  1058. */
  1059. timestamp_len = 8;
  1060. if ((temp_len + timestamp_len + 2) <= len)
  1061. memcpy(timestamp, buf + temp_len + 2,
  1062. timestamp_len);
  1063. else {
  1064. pr_err("diag: Invalid length in %s, len: %d, temp_len: %d",
  1065. __func__, len, temp_len);
  1066. return;
  1067. }
  1068. }
  1069. /* 13th and 14th bit represent the payload length */
  1070. if (((event_id_packet & 0x6000) >> 13) == 3) {
  1071. payload_len_field = 1;
  1072. if ((temp_len + timestamp_len + 3) <= len) {
  1073. payload_len = *(uint8_t *)
  1074. (buf + temp_len + 2 + timestamp_len);
  1075. } else {
  1076. pr_err("diag: Invalid length in %s, len: %d, temp_len: %d",
  1077. __func__, len, temp_len);
  1078. return;
  1079. }
  1080. if ((payload_len < (MAX_EVENT_SIZE - 13)) &&
  1081. ((temp_len + timestamp_len + payload_len + 3) <= len)) {
  1082. /*
  1083. * Copy the payload length and the payload
  1084. * after skipping temp_len bytes for already
  1085. * parsed packet, timestamp_len for timestamp
  1086. * buffer, 2 bytes for event_id_packet.
  1087. */
  1088. memcpy(event_data + 12, buf + temp_len + 2 +
  1089. timestamp_len, 1);
  1090. memcpy(event_data + 13, buf + temp_len + 2 +
  1091. timestamp_len + 1, payload_len);
  1092. } else {
  1093. pr_err("diag: event > %d, payload_len = %d, temp_len = %d\n",
  1094. (MAX_EVENT_SIZE - 13), payload_len, temp_len);
  1095. return;
  1096. }
  1097. } else {
  1098. payload_len_field = 0;
  1099. payload_len = (event_id_packet & 0x6000) >> 13;
  1100. /*
  1101. * Copy the payload after skipping temp_len bytes
  1102. * for already parsed packet, timestamp_len for
  1103. * timestamp buffer, 2 bytes for event_id_packet.
  1104. */
  1105. if ((payload_len < (MAX_EVENT_SIZE - 12)) &&
  1106. ((temp_len + timestamp_len + payload_len + 2) <= len))
  1107. memcpy(event_data + 12, buf + temp_len + 2 +
  1108. timestamp_len, payload_len);
  1109. else {
  1110. pr_err("diag: event > %d, payload_len = %d, temp_len = %d\n",
  1111. (MAX_EVENT_SIZE - 12), payload_len, temp_len);
  1112. return;
  1113. }
  1114. }
  1115. /* Before copying the data to userspace, check if we are still
  1116. * within the buffer limit. This is an error case, don't count
  1117. * it towards the health statistics.
  1118. *
  1119. * Here, the offset of 2 bytes(uint16_t) is for the
  1120. * event_id_packet length
  1121. */
  1122. temp_len += sizeof(uint16_t) + timestamp_len +
  1123. payload_len_field + payload_len;
  1124. if (temp_len > len) {
  1125. pr_err("diag: Invalid length in %s, len: %d, read: %d",
  1126. __func__, len, temp_len);
  1127. return;
  1128. }
  1129. /* 2 bytes for the event id & timestamp len is hard coded to 8,
  1130. * as individual events have full timestamp.
  1131. */
  1132. *(uint16_t *)(event_data) = 10 +
  1133. payload_len_field + payload_len;
  1134. *(uint16_t *)(event_data + 2) = event_id_packet & 0x7FFF;
  1135. memcpy(event_data + 4, timestamp, 8);
  1136. /* 2 bytes for the event length field which is added to
  1137. * the event data.
  1138. */
  1139. total_event_len = 2 + 10 + payload_len_field + payload_len;
  1140. /* parse through event mask tbl of each client and check mask */
  1141. mutex_lock(&driver->dci_mutex);
  1142. list_for_each_safe(start, temp, &driver->dci_client_list) {
  1143. entry = list_entry(start, struct diag_dci_client_tbl,
  1144. track);
  1145. if (entry->client_info.token != token)
  1146. continue;
  1147. if (diag_dci_query_event_mask(entry, event_id)) {
  1148. /* copy to client buffer */
  1149. copy_dci_event(event_data, total_event_len,
  1150. entry, data_source, ext_hdr);
  1151. }
  1152. }
  1153. mutex_unlock(&driver->dci_mutex);
  1154. }
  1155. }
  1156. static void copy_dci_log(unsigned char *buf, int len,
  1157. struct diag_dci_client_tbl *client, int data_source,
  1158. void *ext_hdr)
  1159. {
  1160. uint16_t log_length = 0;
  1161. struct diag_dci_buffer_t *data_buffer = NULL;
  1162. struct diag_dci_buf_peripheral_t *proc_buf = NULL;
  1163. int err = 0, total_len = 0;
  1164. if (!buf || !client) {
  1165. pr_err("diag: Invalid pointers in %s", __func__);
  1166. return;
  1167. }
  1168. log_length = *(uint16_t *)(buf + 2);
  1169. if (log_length > USHRT_MAX - 4) {
  1170. pr_err("diag: Integer overflow in %s, log_len: %d",
  1171. __func__, log_length);
  1172. return;
  1173. }
  1174. total_len = sizeof(int) + log_length;
  1175. if (ext_hdr)
  1176. total_len += sizeof(int) + EXT_HDR_LEN;
  1177. /* Check if we are within the len. The check should include the
  1178. * first 4 bytes for the Log code(2) and the length bytes (2)
  1179. */
  1180. if ((log_length + sizeof(uint16_t) + 2) > len) {
  1181. pr_err("diag: Invalid length in %s, log_len: %d, len: %d",
  1182. __func__, log_length, len);
  1183. return;
  1184. }
  1185. proc_buf = &client->buffers[data_source];
  1186. mutex_lock(&proc_buf->buf_mutex);
  1187. mutex_lock(&proc_buf->health_mutex);
  1188. err = diag_dci_get_buffer(client, data_source, total_len);
  1189. if (err) {
  1190. if (err == -ENOMEM)
  1191. proc_buf->health.dropped_logs++;
  1192. else
  1193. pr_err("diag: In %s, invalid packet\n", __func__);
  1194. mutex_unlock(&proc_buf->health_mutex);
  1195. mutex_unlock(&proc_buf->buf_mutex);
  1196. return;
  1197. }
  1198. data_buffer = proc_buf->buf_curr;
  1199. proc_buf->health.received_logs++;
  1200. mutex_unlock(&proc_buf->health_mutex);
  1201. mutex_unlock(&proc_buf->buf_mutex);
  1202. mutex_lock(&data_buffer->data_mutex);
  1203. if (!data_buffer->data) {
  1204. mutex_unlock(&data_buffer->data_mutex);
  1205. return;
  1206. }
  1207. if (ext_hdr)
  1208. copy_ext_hdr(data_buffer, ext_hdr);
  1209. *(int *)(data_buffer->data + data_buffer->data_len) = DCI_LOG_TYPE;
  1210. data_buffer->data_len += sizeof(int);
  1211. memcpy(data_buffer->data + data_buffer->data_len, buf + sizeof(int),
  1212. log_length);
  1213. data_buffer->data_len += log_length;
  1214. data_buffer->data_source = data_source;
  1215. mutex_unlock(&data_buffer->data_mutex);
  1216. }
  1217. void extract_dci_log(unsigned char *buf, int len, int data_source, int token,
  1218. void *ext_hdr)
  1219. {
  1220. uint16_t log_code, read_bytes = 0;
  1221. struct list_head *start, *temp;
  1222. struct diag_dci_client_tbl *entry = NULL;
  1223. if (!buf) {
  1224. pr_err("diag: In %s buffer is NULL\n", __func__);
  1225. return;
  1226. }
  1227. /*
  1228. * The first eight bytes for the incoming log packet contains
  1229. * Command code (2), the length of the packet (2), the length
  1230. * of the log (2) and log code (2)
  1231. */
  1232. if (len < 8) {
  1233. pr_err("diag: In %s invalid len: %d\n", __func__, len);
  1234. return;
  1235. }
  1236. log_code = *(uint16_t *)(buf + 6);
  1237. read_bytes += sizeof(uint16_t) + 6;
  1238. /* parse through log mask table of each client and check mask */
  1239. mutex_lock(&driver->dci_mutex);
  1240. list_for_each_safe(start, temp, &driver->dci_client_list) {
  1241. entry = list_entry(start, struct diag_dci_client_tbl, track);
  1242. if (entry->client_info.token != token)
  1243. continue;
  1244. if (diag_dci_query_log_mask(entry, log_code)) {
  1245. pr_debug("\t log code %x needed by client %d",
  1246. log_code, entry->client->tgid);
  1247. /* copy to client buffer */
  1248. copy_dci_log(buf, len, entry, data_source, ext_hdr);
  1249. }
  1250. }
  1251. mutex_unlock(&driver->dci_mutex);
  1252. }
  1253. void extract_dci_ext_pkt(unsigned char *buf, int len, int data_source,
  1254. int token)
  1255. {
  1256. uint8_t version, pkt_cmd_code = 0;
  1257. unsigned char *pkt = NULL;
  1258. if (!buf) {
  1259. pr_err("diag: In %s buffer is NULL\n", __func__);
  1260. return;
  1261. }
  1262. if (len < (EXT_HDR_LEN + sizeof(uint8_t))) {
  1263. pr_err("diag: In %s invalid len: %d\n", __func__, len);
  1264. return;
  1265. }
  1266. version = *(uint8_t *)buf + 1;
  1267. if (version < EXT_HDR_VERSION) {
  1268. pr_err("diag: %s, Extended header with invalid version: %d\n",
  1269. __func__, version);
  1270. return;
  1271. }
  1272. pkt = buf + EXT_HDR_LEN;
  1273. pkt_cmd_code = *(uint8_t *)pkt;
  1274. len -= EXT_HDR_LEN;
  1275. switch (pkt_cmd_code) {
  1276. case LOG_CMD_CODE:
  1277. extract_dci_log(pkt, len, data_source, token, buf);
  1278. break;
  1279. case EVENT_CMD_CODE:
  1280. extract_dci_events(pkt, len, data_source, token, buf);
  1281. break;
  1282. default:
  1283. pr_err("diag: %s unsupported cmd_code: %d, data_source: %d\n",
  1284. __func__, pkt_cmd_code, data_source);
  1285. return;
  1286. }
  1287. }
  1288. void diag_dci_channel_open_work(struct work_struct *work)
  1289. {
  1290. int i, j;
  1291. char dirty_bits[16];
  1292. uint8_t *client_log_mask_ptr;
  1293. uint8_t *log_mask_ptr;
  1294. int ret;
  1295. struct list_head *start, *temp;
  1296. struct diag_dci_client_tbl *entry = NULL;
  1297. /* Update apps and peripheral(s) with the dci log and event masks */
  1298. memset(dirty_bits, 0, 16 * sizeof(uint8_t));
  1299. /*
  1300. * From each log entry used by each client, determine
  1301. * which log entries in the cumulative logs that need
  1302. * to be updated on the peripheral.
  1303. */
  1304. mutex_lock(&driver->dci_mutex);
  1305. list_for_each_safe(start, temp, &driver->dci_client_list) {
  1306. entry = list_entry(start, struct diag_dci_client_tbl, track);
  1307. if (entry->client_info.token != DCI_LOCAL_PROC)
  1308. continue;
  1309. client_log_mask_ptr = entry->dci_log_mask;
  1310. for (j = 0; j < 16; j++) {
  1311. if (*(client_log_mask_ptr+1))
  1312. dirty_bits[j] = 1;
  1313. client_log_mask_ptr += 514;
  1314. }
  1315. }
  1316. mutex_unlock(&driver->dci_mutex);
  1317. mutex_lock(&dci_log_mask_mutex);
  1318. /* Update the appropriate dirty bits in the cumulative mask */
  1319. log_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].log_mask_composite;
  1320. for (i = 0; i < 16; i++) {
  1321. if (dirty_bits[i])
  1322. *(log_mask_ptr+1) = dirty_bits[i];
  1323. log_mask_ptr += 514;
  1324. }
  1325. mutex_unlock(&dci_log_mask_mutex);
  1326. /* Send updated mask to userspace clients */
  1327. diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
  1328. /* Send updated log mask to peripherals */
  1329. ret = dci_ops_tbl[DCI_LOCAL_PROC].send_log_mask(DCI_LOCAL_PROC);
  1330. /* Send updated event mask to userspace clients */
  1331. diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
  1332. /* Send updated event mask to peripheral */
  1333. ret = dci_ops_tbl[DCI_LOCAL_PROC].send_event_mask(DCI_LOCAL_PROC);
  1334. }
  1335. void diag_dci_notify_client(int peripheral_mask, int data, int proc)
  1336. {
  1337. int stat = 0;
  1338. struct siginfo info;
  1339. struct list_head *start, *temp;
  1340. struct diag_dci_client_tbl *entry = NULL;
  1341. struct pid *pid_struct = NULL;
  1342. struct task_struct *dci_task = NULL;
  1343. memset(&info, 0, sizeof(struct siginfo));
  1344. info.si_code = SI_QUEUE;
  1345. info.si_int = (peripheral_mask | data);
  1346. if (data == DIAG_STATUS_OPEN)
  1347. dci_ops_tbl[proc].peripheral_status |= peripheral_mask;
  1348. else
  1349. dci_ops_tbl[proc].peripheral_status &= ~peripheral_mask;
  1350. /* Notify the DCI process that the peripheral DCI Channel is up */
  1351. mutex_lock(&driver->dci_mutex);
  1352. list_for_each_safe(start, temp, &driver->dci_client_list) {
  1353. entry = list_entry(start, struct diag_dci_client_tbl, track);
  1354. if (entry->client_info.token != proc)
  1355. continue;
  1356. if (entry->client_info.notification_list & peripheral_mask) {
  1357. info.si_signo = entry->client_info.signal_type;
  1358. pid_struct = find_get_pid(entry->tgid);
  1359. if (pid_struct) {
  1360. dci_task = get_pid_task(pid_struct,
  1361. PIDTYPE_PID);
  1362. if (!dci_task) {
  1363. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  1364. "diag: dci client with pid = %d Exited..\n",
  1365. entry->tgid);
  1366. put_pid(pid_struct);
  1367. mutex_unlock(&driver->dci_mutex);
  1368. return;
  1369. }
  1370. if (entry->client &&
  1371. entry->tgid == dci_task->tgid) {
  1372. DIAG_LOG(DIAG_DEBUG_DCI,
  1373. "entry tgid = %d, dci client tgid = %d\n",
  1374. entry->tgid, dci_task->tgid);
  1375. stat = send_sig_info(
  1376. entry->client_info.signal_type,
  1377. &info, dci_task);
  1378. if (stat)
  1379. pr_err("diag: Err sending dci signal to client, signal data: 0x%x, stat: %d\n",
  1380. info.si_int, stat);
  1381. } else {
  1382. pr_err("diag: client data is corrupted, signal data: 0x%x, stat: %d\n",
  1383. info.si_int, stat);
  1384. }
  1385. put_task_struct(dci_task);
  1386. put_pid(pid_struct);
  1387. }
  1388. }
  1389. }
  1390. mutex_unlock(&driver->dci_mutex);
  1391. }
  1392. static int diag_send_dci_pkt(struct diag_cmd_reg_t *entry,
  1393. unsigned char *buf, int len, int tag)
  1394. {
  1395. int i, status = DIAG_DCI_NO_ERROR;
  1396. uint32_t write_len = 0;
  1397. struct diag_dci_pkt_header_t header;
  1398. if (!entry)
  1399. return -EIO;
  1400. if (len < 1 || len > DIAG_MAX_REQ_SIZE) {
  1401. pr_err("diag: dci: In %s, invalid length %d, max_length: %d\n",
  1402. __func__, len, (int)(DCI_REQ_BUF_SIZE - sizeof(header)));
  1403. return -EIO;
  1404. }
  1405. if ((len + sizeof(header) + sizeof(uint8_t)) > DCI_BUF_SIZE) {
  1406. pr_err("diag: dci: In %s, invalid length %d for apps_dci_buf, max_length: %d\n",
  1407. __func__, len, DIAG_MAX_REQ_SIZE);
  1408. return -EIO;
  1409. }
  1410. mutex_lock(&driver->dci_mutex);
  1411. /* prepare DCI packet */
  1412. header.start = CONTROL_CHAR;
  1413. header.version = 1;
  1414. header.len = len + sizeof(int) + sizeof(uint8_t);
  1415. header.pkt_code = DCI_PKT_RSP_CODE;
  1416. header.tag = tag;
  1417. memcpy(driver->apps_dci_buf, &header, sizeof(header));
  1418. write_len += sizeof(header);
  1419. memcpy(driver->apps_dci_buf + write_len, buf, len);
  1420. write_len += len;
  1421. *(uint8_t *)(driver->apps_dci_buf + write_len) = CONTROL_CHAR;
  1422. write_len += sizeof(uint8_t);
  1423. /* This command is registered locally on the Apps */
  1424. if (entry->proc == APPS_DATA) {
  1425. diag_update_pkt_buffer(driver->apps_dci_buf, write_len,
  1426. DCI_PKT_TYPE);
  1427. diag_update_sleeping_process(entry->pid, DCI_PKT_TYPE);
  1428. mutex_unlock(&driver->dci_mutex);
  1429. return DIAG_DCI_NO_ERROR;
  1430. }
  1431. for (i = 0; i < NUM_PERIPHERALS; i++)
  1432. if (entry->proc == i) {
  1433. status = 1;
  1434. break;
  1435. }
  1436. if (status) {
  1437. status = diag_dci_write_proc(entry->proc,
  1438. DIAG_DATA_TYPE,
  1439. driver->apps_dci_buf,
  1440. write_len);
  1441. } else {
  1442. pr_err("diag: Cannot send packet to peripheral %d",
  1443. entry->proc);
  1444. status = DIAG_DCI_SEND_DATA_FAIL;
  1445. }
  1446. mutex_unlock(&driver->dci_mutex);
  1447. return status;
  1448. }
  1449. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  1450. unsigned char *dci_get_buffer_from_bridge(int token)
  1451. {
  1452. uint8_t retries = 0, max_retries = 3;
  1453. unsigned char *buf = NULL;
  1454. do {
  1455. buf = diagmem_alloc(driver, DIAG_MDM_BUF_SIZE,
  1456. dci_ops_tbl[token].mempool);
  1457. if (!buf) {
  1458. usleep_range(5000, 5100);
  1459. retries++;
  1460. } else
  1461. break;
  1462. } while (retries < max_retries);
  1463. return buf;
  1464. }
  1465. int diag_dci_write_bridge(int token, unsigned char *buf, int len)
  1466. {
  1467. return diagfwd_bridge_write(TOKEN_TO_BRIDGE(token), buf, len);
  1468. }
  1469. int diag_dci_write_done_bridge(int index, unsigned char *buf, int len)
  1470. {
  1471. int token = BRIDGE_TO_TOKEN(index);
  1472. if (!VALID_DCI_TOKEN(token)) {
  1473. pr_err("diag: Invalid DCI token %d in %s\n", token, __func__);
  1474. return -EINVAL;
  1475. }
  1476. diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
  1477. return 0;
  1478. }
  1479. #endif
  1480. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  1481. static int diag_send_dci_pkt_remote(unsigned char *data, int len, int tag,
  1482. int token)
  1483. {
  1484. unsigned char *buf = NULL;
  1485. struct diag_dci_header_t dci_header;
  1486. int dci_header_size = sizeof(struct diag_dci_header_t);
  1487. int ret = DIAG_DCI_NO_ERROR;
  1488. uint32_t write_len = 0;
  1489. if (!data)
  1490. return -EIO;
  1491. buf = dci_get_buffer_from_bridge(token);
  1492. if (!buf) {
  1493. pr_err("diag: In %s, unable to get dci buffers to write data\n",
  1494. __func__);
  1495. return -EAGAIN;
  1496. }
  1497. dci_header.start = CONTROL_CHAR;
  1498. dci_header.version = 1;
  1499. /*
  1500. * The Length of the DCI packet = length of the command + tag (int) +
  1501. * the command code size (uint8_t)
  1502. */
  1503. dci_header.length = len + sizeof(int) + sizeof(uint8_t);
  1504. dci_header.cmd_code = DCI_PKT_RSP_CODE;
  1505. memcpy(buf + write_len, &dci_header, dci_header_size);
  1506. write_len += dci_header_size;
  1507. *(int *)(buf + write_len) = tag;
  1508. write_len += sizeof(int);
  1509. memcpy(buf + write_len, data, len);
  1510. write_len += len;
  1511. *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
  1512. write_len += sizeof(uint8_t);
  1513. ret = diag_dci_write_bridge(token, buf, write_len);
  1514. if (ret) {
  1515. pr_err("diag: error writing dci pkt to remote proc, token: %d, err: %d\n",
  1516. token, ret);
  1517. diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
  1518. } else {
  1519. ret = DIAG_DCI_NO_ERROR;
  1520. }
  1521. return ret;
  1522. }
  1523. #else
  1524. static int diag_send_dci_pkt_remote(unsigned char *data, int len, int tag,
  1525. int token)
  1526. {
  1527. return DIAG_DCI_NO_ERROR;
  1528. }
  1529. #endif
  1530. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  1531. int diag_dci_send_handshake_pkt(int index)
  1532. {
  1533. int err = 0;
  1534. int token = BRIDGE_TO_TOKEN(index);
  1535. int write_len = 0;
  1536. struct diag_ctrl_dci_handshake_pkt ctrl_pkt;
  1537. unsigned char *buf = NULL;
  1538. struct diag_dci_header_t dci_header;
  1539. if (!VALID_DCI_TOKEN(token)) {
  1540. pr_err("diag: In %s, invalid DCI token %d\n", __func__, token);
  1541. return -EINVAL;
  1542. }
  1543. buf = dci_get_buffer_from_bridge(token);
  1544. if (!buf) {
  1545. pr_err("diag: In %s, unable to get dci buffers to write data\n",
  1546. __func__);
  1547. return -EAGAIN;
  1548. }
  1549. dci_header.start = CONTROL_CHAR;
  1550. dci_header.version = 1;
  1551. /* Include the cmd code (uint8_t) in the length */
  1552. dci_header.length = sizeof(ctrl_pkt) + sizeof(uint8_t);
  1553. dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
  1554. memcpy(buf, &dci_header, sizeof(dci_header));
  1555. write_len += sizeof(dci_header);
  1556. ctrl_pkt.ctrl_pkt_id = DIAG_CTRL_MSG_DCI_HANDSHAKE_PKT;
  1557. /*
  1558. * The control packet data length accounts for the version (uint32_t)
  1559. * of the packet and the magic number (uint32_t).
  1560. */
  1561. ctrl_pkt.ctrl_pkt_data_len = 2 * sizeof(uint32_t);
  1562. ctrl_pkt.version = 1;
  1563. ctrl_pkt.magic = DCI_MAGIC;
  1564. memcpy(buf + write_len, &ctrl_pkt, sizeof(ctrl_pkt));
  1565. write_len += sizeof(ctrl_pkt);
  1566. *(uint8_t *)(buf + write_len) = CONTROL_CHAR;
  1567. write_len += sizeof(uint8_t);
  1568. err = diag_dci_write_bridge(token, buf, write_len);
  1569. if (err) {
  1570. pr_err("diag: error writing ack packet to remote proc, token: %d, err: %d\n",
  1571. token, err);
  1572. diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
  1573. return err;
  1574. }
  1575. mod_timer(&(dci_channel_status[token].wait_time),
  1576. jiffies + msecs_to_jiffies(DCI_HANDSHAKE_WAIT_TIME));
  1577. return 0;
  1578. }
  1579. #else
  1580. int diag_dci_send_handshake_pkt(int index)
  1581. {
  1582. return 0;
  1583. }
  1584. #endif
  1585. static int diag_dci_process_apps_pkt(struct diag_pkt_header_t *pkt_header,
  1586. unsigned char *req_buf, int req_len,
  1587. int tag)
  1588. {
  1589. uint8_t cmd_code, subsys_id, i, goto_download = 0;
  1590. uint8_t header_len = sizeof(struct diag_dci_pkt_header_t);
  1591. uint16_t ss_cmd_code;
  1592. uint32_t write_len = 0;
  1593. unsigned char *dest_buf = driver->apps_dci_buf;
  1594. unsigned char *payload_ptr = driver->apps_dci_buf + header_len;
  1595. struct diag_dci_pkt_header_t dci_header;
  1596. if (!pkt_header || !req_buf || req_len <= 0 || tag < 0)
  1597. return -EIO;
  1598. cmd_code = pkt_header->cmd_code;
  1599. subsys_id = pkt_header->subsys_id;
  1600. ss_cmd_code = pkt_header->subsys_cmd_code;
  1601. if (cmd_code == DIAG_CMD_DOWNLOAD) {
  1602. *payload_ptr = DIAG_CMD_DOWNLOAD;
  1603. write_len = sizeof(uint8_t);
  1604. goto_download = 1;
  1605. goto fill_buffer;
  1606. } else if (cmd_code == DIAG_CMD_VERSION) {
  1607. if (chk_polling_response()) {
  1608. for (i = 0; i < 55; i++, write_len++, payload_ptr++)
  1609. *(payload_ptr) = 0;
  1610. goto fill_buffer;
  1611. }
  1612. } else if (cmd_code == DIAG_CMD_EXT_BUILD) {
  1613. if (chk_polling_response()) {
  1614. *payload_ptr = DIAG_CMD_EXT_BUILD;
  1615. write_len = sizeof(uint8_t);
  1616. payload_ptr += sizeof(uint8_t);
  1617. for (i = 0; i < 8; i++, write_len++, payload_ptr++)
  1618. *(payload_ptr) = 0;
  1619. *(int *)(payload_ptr) = chk_config_get_id();
  1620. write_len += sizeof(int);
  1621. goto fill_buffer;
  1622. }
  1623. } else if (cmd_code == DIAG_CMD_LOG_ON_DMND) {
  1624. write_len = diag_cmd_log_on_demand(req_buf, req_len,
  1625. payload_ptr,
  1626. APPS_BUF_SIZE - header_len);
  1627. goto fill_buffer;
  1628. } else if (cmd_code != DIAG_CMD_DIAG_SUBSYS) {
  1629. return DIAG_DCI_TABLE_ERR;
  1630. }
  1631. if (subsys_id == DIAG_SS_DIAG) {
  1632. if (ss_cmd_code == DIAG_DIAG_MAX_PKT_SZ) {
  1633. memcpy(payload_ptr, pkt_header,
  1634. sizeof(struct diag_pkt_header_t));
  1635. write_len = sizeof(struct diag_pkt_header_t);
  1636. *(uint32_t *)(payload_ptr + write_len) =
  1637. DIAG_MAX_REQ_SIZE;
  1638. write_len += sizeof(uint32_t);
  1639. } else if (ss_cmd_code == DIAG_DIAG_STM) {
  1640. write_len = diag_process_stm_cmd(req_buf, payload_ptr);
  1641. }
  1642. } else if (subsys_id == DIAG_SS_PARAMS) {
  1643. if (ss_cmd_code == DIAG_DIAG_POLL) {
  1644. if (chk_polling_response()) {
  1645. memcpy(payload_ptr, pkt_header,
  1646. sizeof(struct diag_pkt_header_t));
  1647. write_len = sizeof(struct diag_pkt_header_t);
  1648. payload_ptr += write_len;
  1649. for (i = 0; i < 12; i++, write_len++) {
  1650. *(payload_ptr) = 0;
  1651. payload_ptr++;
  1652. }
  1653. }
  1654. } else if (ss_cmd_code == DIAG_DEL_RSP_WRAP) {
  1655. memcpy(payload_ptr, pkt_header,
  1656. sizeof(struct diag_pkt_header_t));
  1657. write_len = sizeof(struct diag_pkt_header_t);
  1658. *(int *)(payload_ptr + write_len) = wrap_enabled;
  1659. write_len += sizeof(int);
  1660. } else if (ss_cmd_code == DIAG_DEL_RSP_WRAP_CNT) {
  1661. wrap_enabled = true;
  1662. memcpy(payload_ptr, pkt_header,
  1663. sizeof(struct diag_pkt_header_t));
  1664. write_len = sizeof(struct diag_pkt_header_t);
  1665. *(uint16_t *)(payload_ptr + write_len) = wrap_count;
  1666. write_len += sizeof(uint16_t);
  1667. } else if (ss_cmd_code == DIAG_EXT_MOBILE_ID) {
  1668. write_len = diag_cmd_get_mobile_id(req_buf, req_len,
  1669. payload_ptr,
  1670. APPS_BUF_SIZE - header_len);
  1671. }
  1672. }
  1673. fill_buffer:
  1674. if (write_len > 0) {
  1675. /* Check if we are within the range of the buffer*/
  1676. if (write_len + header_len > DIAG_MAX_REQ_SIZE) {
  1677. pr_err("diag: In %s, invalid length %d\n", __func__,
  1678. write_len + header_len);
  1679. return -ENOMEM;
  1680. }
  1681. dci_header.start = CONTROL_CHAR;
  1682. dci_header.version = 1;
  1683. /*
  1684. * Length of the rsp pkt = actual data len + pkt rsp code
  1685. * (uint8_t) + tag (int)
  1686. */
  1687. dci_header.len = write_len + sizeof(uint8_t) + sizeof(int);
  1688. dci_header.pkt_code = DCI_PKT_RSP_CODE;
  1689. dci_header.tag = tag;
  1690. driver->in_busy_dcipktdata = 1;
  1691. memcpy(dest_buf, &dci_header, header_len);
  1692. diag_process_apps_dci_read_data(DCI_PKT_TYPE, dest_buf + 4,
  1693. dci_header.len);
  1694. driver->in_busy_dcipktdata = 0;
  1695. if (goto_download) {
  1696. /*
  1697. * Sleep for sometime so that the response reaches the
  1698. * client. The value 5000 empirically as an optimum
  1699. * time for the response to reach the client.
  1700. */
  1701. usleep_range(5000, 5100);
  1702. /* call download API */
  1703. msm_set_restart_mode(RESTART_DLOAD);
  1704. pr_alert("diag: download mode set, Rebooting SoC..\n");
  1705. kernel_restart(NULL);
  1706. }
  1707. return DIAG_DCI_NO_ERROR;
  1708. }
  1709. return DIAG_DCI_TABLE_ERR;
  1710. }
  1711. static int diag_process_dci_pkt_rsp(unsigned char *buf, int len)
  1712. {
  1713. int ret = DIAG_DCI_TABLE_ERR;
  1714. int common_cmd = 0;
  1715. struct diag_pkt_header_t *header = NULL;
  1716. unsigned char *temp = buf;
  1717. unsigned char *req_buf = NULL;
  1718. uint8_t retry_count = 0, max_retries = 3;
  1719. uint32_t read_len = 0, req_len = len;
  1720. struct dci_pkt_req_entry_t *req_entry = NULL;
  1721. struct diag_dci_client_tbl *dci_entry = NULL;
  1722. struct dci_pkt_req_t req_hdr;
  1723. struct diag_cmd_reg_t *reg_item;
  1724. struct diag_cmd_reg_entry_t reg_entry;
  1725. struct diag_cmd_reg_entry_t *temp_entry;
  1726. if (!buf)
  1727. return -EIO;
  1728. if (len <= sizeof(struct dci_pkt_req_t) || len > DCI_REQ_BUF_SIZE) {
  1729. pr_err("diag: dci: Invalid length %d len in %s", len, __func__);
  1730. return -EIO;
  1731. }
  1732. req_hdr = *(struct dci_pkt_req_t *)temp;
  1733. temp += sizeof(struct dci_pkt_req_t);
  1734. read_len += sizeof(struct dci_pkt_req_t);
  1735. req_len -= sizeof(struct dci_pkt_req_t);
  1736. req_buf = temp; /* Start of the Request */
  1737. header = (struct diag_pkt_header_t *)temp;
  1738. temp += sizeof(struct diag_pkt_header_t);
  1739. read_len += sizeof(struct diag_pkt_header_t);
  1740. if (read_len >= DCI_REQ_BUF_SIZE) {
  1741. pr_err("diag: dci: In %s, invalid read_len: %d\n", __func__,
  1742. read_len);
  1743. return -EIO;
  1744. }
  1745. mutex_lock(&driver->dci_mutex);
  1746. dci_entry = diag_dci_get_client_entry(req_hdr.client_id);
  1747. if (!dci_entry) {
  1748. pr_err("diag: Invalid client %d in %s\n",
  1749. req_hdr.client_id, __func__);
  1750. mutex_unlock(&driver->dci_mutex);
  1751. return DIAG_DCI_NO_REG;
  1752. }
  1753. /* Check if the command is allowed on DCI */
  1754. if (diag_dci_filter_commands(header)) {
  1755. pr_debug("diag: command not supported %d %d %d",
  1756. header->cmd_code, header->subsys_id,
  1757. header->subsys_cmd_code);
  1758. mutex_unlock(&driver->dci_mutex);
  1759. return DIAG_DCI_SEND_DATA_FAIL;
  1760. }
  1761. common_cmd = diag_check_common_cmd(header);
  1762. if (common_cmd < 0) {
  1763. pr_debug("diag: error in checking common command, %d\n",
  1764. common_cmd);
  1765. mutex_unlock(&driver->dci_mutex);
  1766. return DIAG_DCI_SEND_DATA_FAIL;
  1767. }
  1768. /*
  1769. * Previous packet is yet to be consumed by the client. Wait
  1770. * till the buffer is free.
  1771. */
  1772. while (retry_count < max_retries) {
  1773. retry_count++;
  1774. if (driver->in_busy_dcipktdata)
  1775. usleep_range(10000, 10100);
  1776. else
  1777. break;
  1778. }
  1779. /* The buffer is still busy */
  1780. if (driver->in_busy_dcipktdata) {
  1781. pr_err("diag: In %s, apps dci buffer is still busy. Dropping packet\n",
  1782. __func__);
  1783. mutex_unlock(&driver->dci_mutex);
  1784. return -EAGAIN;
  1785. }
  1786. /* Register this new DCI packet */
  1787. req_entry = diag_register_dci_transaction(req_hdr.uid,
  1788. req_hdr.client_id);
  1789. if (!req_entry) {
  1790. pr_alert("diag: registering new DCI transaction failed\n");
  1791. mutex_unlock(&driver->dci_mutex);
  1792. return DIAG_DCI_NO_REG;
  1793. }
  1794. mutex_unlock(&driver->dci_mutex);
  1795. /*
  1796. * If the client has registered for remote data, route the packet to the
  1797. * remote processor
  1798. */
  1799. if (dci_entry->client_info.token > 0) {
  1800. ret = diag_send_dci_pkt_remote(req_buf, req_len, req_entry->tag,
  1801. dci_entry->client_info.token);
  1802. return ret;
  1803. }
  1804. /* Check if it is a dedicated Apps command */
  1805. ret = diag_dci_process_apps_pkt(header, req_buf, req_len,
  1806. req_entry->tag);
  1807. if ((ret == DIAG_DCI_NO_ERROR && !common_cmd) || ret < 0)
  1808. return ret;
  1809. reg_entry.cmd_code = header->cmd_code;
  1810. reg_entry.subsys_id = header->subsys_id;
  1811. reg_entry.cmd_code_hi = header->subsys_cmd_code;
  1812. reg_entry.cmd_code_lo = header->subsys_cmd_code;
  1813. mutex_lock(&driver->cmd_reg_mutex);
  1814. temp_entry = diag_cmd_search(&reg_entry, ALL_PROC);
  1815. if (temp_entry) {
  1816. reg_item = container_of(temp_entry, struct diag_cmd_reg_t,
  1817. entry);
  1818. ret = diag_send_dci_pkt(reg_item, req_buf, req_len,
  1819. req_entry->tag);
  1820. } else {
  1821. DIAG_LOG(DIAG_DEBUG_DCI, "Command not found: %02x %02x %02x\n",
  1822. reg_entry.cmd_code, reg_entry.subsys_id,
  1823. reg_entry.cmd_code_hi);
  1824. }
  1825. mutex_unlock(&driver->cmd_reg_mutex);
  1826. return ret;
  1827. }
  1828. int diag_process_dci_transaction(unsigned char *buf, int len)
  1829. {
  1830. unsigned char *temp = buf;
  1831. uint16_t log_code, item_num;
  1832. int ret = -1, found = 0, client_id = 0, client_token = 0;
  1833. int count, set_mask, num_codes, bit_index, event_id, offset = 0;
  1834. unsigned int byte_index, read_len = 0;
  1835. uint8_t equip_id, *log_mask_ptr, *head_log_mask_ptr, byte_mask;
  1836. uint8_t *event_mask_ptr;
  1837. struct diag_dci_client_tbl *dci_entry = NULL;
  1838. if (!temp) {
  1839. pr_err("diag: Invalid buffer in %s\n", __func__);
  1840. return -ENOMEM;
  1841. }
  1842. /* This is Pkt request/response transaction */
  1843. if (*(int *)temp > 0) {
  1844. return diag_process_dci_pkt_rsp(buf, len);
  1845. } else if (*(int *)temp == DCI_LOG_TYPE) {
  1846. /* Minimum length of a log mask config is 12 + 2 bytes for
  1847. * atleast one log code to be set or reset.
  1848. */
  1849. if (len < DCI_LOG_CON_MIN_LEN || len > USER_SPACE_DATA) {
  1850. pr_err("diag: dci: Invalid length in %s\n", __func__);
  1851. return -EIO;
  1852. }
  1853. /* Extract each log code and put in client table */
  1854. temp += sizeof(int);
  1855. read_len += sizeof(int);
  1856. client_id = *(int *)temp;
  1857. temp += sizeof(int);
  1858. read_len += sizeof(int);
  1859. set_mask = *(int *)temp;
  1860. temp += sizeof(int);
  1861. read_len += sizeof(int);
  1862. num_codes = *(int *)temp;
  1863. temp += sizeof(int);
  1864. read_len += sizeof(int);
  1865. /* Find client table entry */
  1866. mutex_lock(&driver->dci_mutex);
  1867. dci_entry = diag_dci_get_client_entry(client_id);
  1868. if (!dci_entry) {
  1869. pr_err("diag: In %s, invalid client\n", __func__);
  1870. mutex_unlock(&driver->dci_mutex);
  1871. return ret;
  1872. }
  1873. client_token = dci_entry->client_info.token;
  1874. if (num_codes == 0 || (num_codes >= (USER_SPACE_DATA - 8)/2)) {
  1875. pr_err("diag: dci: Invalid number of log codes %d\n",
  1876. num_codes);
  1877. mutex_unlock(&driver->dci_mutex);
  1878. return -EIO;
  1879. }
  1880. head_log_mask_ptr = dci_entry->dci_log_mask;
  1881. if (!head_log_mask_ptr) {
  1882. pr_err("diag: dci: Invalid Log mask pointer in %s\n",
  1883. __func__);
  1884. mutex_unlock(&driver->dci_mutex);
  1885. return -ENOMEM;
  1886. }
  1887. pr_debug("diag: head of dci log mask %pK\n", head_log_mask_ptr);
  1888. count = 0; /* iterator for extracting log codes */
  1889. while (count < num_codes) {
  1890. if (read_len >= USER_SPACE_DATA) {
  1891. pr_err("diag: dci: Invalid length for log type in %s",
  1892. __func__);
  1893. mutex_unlock(&driver->dci_mutex);
  1894. return -EIO;
  1895. }
  1896. log_code = *(uint16_t *)temp;
  1897. equip_id = LOG_GET_EQUIP_ID(log_code);
  1898. item_num = LOG_GET_ITEM_NUM(log_code);
  1899. byte_index = item_num/8 + 2;
  1900. if (byte_index >= (DCI_MAX_ITEMS_PER_LOG_CODE+2)) {
  1901. pr_err("diag: dci: Log type, invalid byte index\n");
  1902. mutex_unlock(&driver->dci_mutex);
  1903. return ret;
  1904. }
  1905. byte_mask = 0x01 << (item_num % 8);
  1906. /*
  1907. * Parse through log mask table and find
  1908. * relevant range
  1909. */
  1910. log_mask_ptr = head_log_mask_ptr;
  1911. found = 0;
  1912. offset = 0;
  1913. while (log_mask_ptr && (offset < DCI_LOG_MASK_SIZE)) {
  1914. if (*log_mask_ptr == equip_id) {
  1915. found = 1;
  1916. pr_debug("diag: find equip id = %x at %pK\n",
  1917. equip_id, log_mask_ptr);
  1918. break;
  1919. }
  1920. pr_debug("diag: did not find equip id = %x at %d\n",
  1921. equip_id, *log_mask_ptr);
  1922. log_mask_ptr += 514;
  1923. offset += 514;
  1924. }
  1925. if (!found) {
  1926. pr_err("diag: dci equip id not found\n");
  1927. mutex_unlock(&driver->dci_mutex);
  1928. return ret;
  1929. }
  1930. *(log_mask_ptr+1) = 1; /* set the dirty byte */
  1931. log_mask_ptr = log_mask_ptr + byte_index;
  1932. if (set_mask)
  1933. *log_mask_ptr |= byte_mask;
  1934. else
  1935. *log_mask_ptr &= ~byte_mask;
  1936. /* add to cumulative mask */
  1937. update_dci_cumulative_log_mask(
  1938. offset, byte_index,
  1939. byte_mask, client_token);
  1940. temp += 2;
  1941. read_len += 2;
  1942. count++;
  1943. ret = DIAG_DCI_NO_ERROR;
  1944. }
  1945. /* send updated mask to userspace clients */
  1946. if (client_token == DCI_LOCAL_PROC)
  1947. diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
  1948. /* send updated mask to peripherals */
  1949. ret = dci_ops_tbl[client_token].send_log_mask(client_token);
  1950. mutex_unlock(&driver->dci_mutex);
  1951. } else if (*(int *)temp == DCI_EVENT_TYPE) {
  1952. /* Minimum length of a event mask config is 12 + 4 bytes for
  1953. * atleast one event id to be set or reset.
  1954. */
  1955. if (len < DCI_EVENT_CON_MIN_LEN || len > USER_SPACE_DATA) {
  1956. pr_err("diag: dci: Invalid length in %s\n", __func__);
  1957. return -EIO;
  1958. }
  1959. /* Extract each event id and put in client table */
  1960. temp += sizeof(int);
  1961. read_len += sizeof(int);
  1962. client_id = *(int *)temp;
  1963. temp += sizeof(int);
  1964. read_len += sizeof(int);
  1965. set_mask = *(int *)temp;
  1966. temp += sizeof(int);
  1967. read_len += sizeof(int);
  1968. num_codes = *(int *)temp;
  1969. temp += sizeof(int);
  1970. read_len += sizeof(int);
  1971. /* find client table entry */
  1972. mutex_lock(&driver->dci_mutex);
  1973. dci_entry = diag_dci_get_client_entry(client_id);
  1974. if (!dci_entry) {
  1975. pr_err("diag: In %s, invalid client\n", __func__);
  1976. mutex_unlock(&driver->dci_mutex);
  1977. return ret;
  1978. }
  1979. client_token = dci_entry->client_info.token;
  1980. /* Check for positive number of event ids. Also, the number of
  1981. * event ids should fit in the buffer along with set_mask and
  1982. * num_codes which are 4 bytes each.
  1983. */
  1984. if (num_codes == 0 || (num_codes >= (USER_SPACE_DATA - 8)/2)) {
  1985. pr_err("diag: dci: Invalid number of event ids %d\n",
  1986. num_codes);
  1987. mutex_unlock(&driver->dci_mutex);
  1988. return -EIO;
  1989. }
  1990. event_mask_ptr = dci_entry->dci_event_mask;
  1991. if (!event_mask_ptr) {
  1992. pr_err("diag: dci: Invalid event mask pointer in %s\n",
  1993. __func__);
  1994. mutex_unlock(&driver->dci_mutex);
  1995. return -ENOMEM;
  1996. }
  1997. pr_debug("diag: head of dci event mask %pK\n", event_mask_ptr);
  1998. count = 0; /* iterator for extracting log codes */
  1999. while (count < num_codes) {
  2000. if (read_len >= USER_SPACE_DATA) {
  2001. pr_err("diag: dci: Invalid length for event type in %s",
  2002. __func__);
  2003. mutex_unlock(&driver->dci_mutex);
  2004. return -EIO;
  2005. }
  2006. event_id = *(int *)temp;
  2007. byte_index = event_id/8;
  2008. if (byte_index >= DCI_EVENT_MASK_SIZE) {
  2009. pr_err("diag: dci: Event type, invalid byte index\n");
  2010. mutex_unlock(&driver->dci_mutex);
  2011. return ret;
  2012. }
  2013. bit_index = event_id % 8;
  2014. byte_mask = 0x1 << bit_index;
  2015. /*
  2016. * Parse through event mask table and set
  2017. * relevant byte & bit combination
  2018. */
  2019. if (set_mask)
  2020. *(event_mask_ptr + byte_index) |= byte_mask;
  2021. else
  2022. *(event_mask_ptr + byte_index) &= ~byte_mask;
  2023. /* add to cumulative mask */
  2024. update_dci_cumulative_event_mask(byte_index, byte_mask,
  2025. client_token);
  2026. temp += sizeof(int);
  2027. read_len += sizeof(int);
  2028. count++;
  2029. ret = DIAG_DCI_NO_ERROR;
  2030. }
  2031. /* send updated mask to userspace clients */
  2032. if (dci_entry->client_info.token == DCI_LOCAL_PROC)
  2033. diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
  2034. /* send updated mask to peripherals */
  2035. ret = dci_ops_tbl[client_token].send_event_mask(client_token);
  2036. mutex_unlock(&driver->dci_mutex);
  2037. } else {
  2038. pr_alert("diag: Incorrect DCI transaction\n");
  2039. }
  2040. return ret;
  2041. }
  2042. struct diag_dci_client_tbl *diag_dci_get_client_entry(int client_id)
  2043. {
  2044. struct list_head *start, *temp;
  2045. struct diag_dci_client_tbl *entry = NULL;
  2046. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2047. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2048. if (entry->client_info.client_id == client_id)
  2049. return entry;
  2050. }
  2051. return NULL;
  2052. }
  2053. struct diag_dci_client_tbl *dci_lookup_client_entry_pid(int tgid)
  2054. {
  2055. struct list_head *start, *temp;
  2056. struct diag_dci_client_tbl *entry = NULL;
  2057. struct pid *pid_struct = NULL;
  2058. struct task_struct *task_s = NULL;
  2059. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2060. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2061. pid_struct = find_get_pid(entry->tgid);
  2062. if (!pid_struct) {
  2063. DIAG_LOG(DIAG_DEBUG_DCI,
  2064. "diag: Exited pid (%d) doesn't match dci client of pid (%d)\n",
  2065. tgid, entry->tgid);
  2066. continue;
  2067. }
  2068. task_s = get_pid_task(pid_struct, PIDTYPE_PID);
  2069. if (!task_s) {
  2070. DIAG_LOG(DIAG_DEBUG_DCI,
  2071. "diag: valid task doesn't exist for pid = %d\n",
  2072. entry->tgid);
  2073. put_pid(pid_struct);
  2074. continue;
  2075. }
  2076. if (task_s == entry->client) {
  2077. if (entry->client->tgid == tgid) {
  2078. put_task_struct(task_s);
  2079. put_pid(pid_struct);
  2080. return entry;
  2081. }
  2082. }
  2083. put_task_struct(task_s);
  2084. put_pid(pid_struct);
  2085. }
  2086. return NULL;
  2087. }
  2088. void update_dci_cumulative_event_mask(int offset, uint8_t byte_mask, int token)
  2089. {
  2090. uint8_t *event_mask_ptr, *update_ptr = NULL;
  2091. struct list_head *start, *temp;
  2092. struct diag_dci_client_tbl *entry = NULL;
  2093. bool is_set = false;
  2094. mutex_lock(&dci_event_mask_mutex);
  2095. update_ptr = dci_ops_tbl[token].event_mask_composite;
  2096. if (!update_ptr) {
  2097. mutex_unlock(&dci_event_mask_mutex);
  2098. return;
  2099. }
  2100. update_ptr += offset;
  2101. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2102. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2103. if (entry->client_info.token != token)
  2104. continue;
  2105. event_mask_ptr = entry->dci_event_mask;
  2106. event_mask_ptr += offset;
  2107. if ((*event_mask_ptr & byte_mask) == byte_mask) {
  2108. is_set = true;
  2109. /* break even if one client has the event mask set */
  2110. break;
  2111. }
  2112. }
  2113. if (is_set == false)
  2114. *update_ptr &= ~byte_mask;
  2115. else
  2116. *update_ptr |= byte_mask;
  2117. mutex_unlock(&dci_event_mask_mutex);
  2118. }
  2119. void diag_dci_invalidate_cumulative_event_mask(int token)
  2120. {
  2121. int i = 0;
  2122. struct list_head *start, *temp;
  2123. struct diag_dci_client_tbl *entry = NULL;
  2124. uint8_t *event_mask_ptr, *update_ptr = NULL;
  2125. mutex_lock(&dci_event_mask_mutex);
  2126. update_ptr = dci_ops_tbl[token].event_mask_composite;
  2127. if (!update_ptr) {
  2128. mutex_unlock(&dci_event_mask_mutex);
  2129. return;
  2130. }
  2131. create_dci_event_mask_tbl(update_ptr);
  2132. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2133. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2134. if (entry->client_info.token != token)
  2135. continue;
  2136. event_mask_ptr = entry->dci_event_mask;
  2137. for (i = 0; i < DCI_EVENT_MASK_SIZE; i++)
  2138. *(update_ptr+i) |= *(event_mask_ptr+i);
  2139. }
  2140. mutex_unlock(&dci_event_mask_mutex);
  2141. }
  2142. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  2143. int diag_send_dci_event_mask_remote(int token)
  2144. {
  2145. unsigned char *buf = NULL;
  2146. struct diag_dci_header_t dci_header;
  2147. struct diag_ctrl_event_mask event_mask;
  2148. int dci_header_size = sizeof(struct diag_dci_header_t);
  2149. int event_header_size = sizeof(struct diag_ctrl_event_mask);
  2150. int i, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
  2151. unsigned char *event_mask_ptr = NULL;
  2152. uint32_t write_len = 0;
  2153. mutex_lock(&dci_event_mask_mutex);
  2154. event_mask_ptr = dci_ops_tbl[token].event_mask_composite;
  2155. if (!event_mask_ptr) {
  2156. mutex_unlock(&dci_event_mask_mutex);
  2157. return -EINVAL;
  2158. }
  2159. buf = dci_get_buffer_from_bridge(token);
  2160. if (!buf) {
  2161. pr_err("diag: In %s, unable to get dci buffers to write data\n",
  2162. __func__);
  2163. mutex_unlock(&dci_event_mask_mutex);
  2164. return -EAGAIN;
  2165. }
  2166. /* Frame the DCI header */
  2167. dci_header.start = CONTROL_CHAR;
  2168. dci_header.version = 1;
  2169. dci_header.length = event_header_size + DCI_EVENT_MASK_SIZE + 1;
  2170. dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
  2171. event_mask.cmd_type = DIAG_CTRL_MSG_EVENT_MASK;
  2172. event_mask.data_len = EVENT_MASK_CTRL_HEADER_LEN + DCI_EVENT_MASK_SIZE;
  2173. event_mask.stream_id = DCI_MASK_STREAM;
  2174. event_mask.status = DIAG_CTRL_MASK_VALID;
  2175. event_mask.event_config = 0; /* event config */
  2176. event_mask.event_mask_size = DCI_EVENT_MASK_SIZE;
  2177. for (i = 0; i < DCI_EVENT_MASK_SIZE; i++) {
  2178. if (event_mask_ptr[i] != 0) {
  2179. event_mask.event_config = 1;
  2180. break;
  2181. }
  2182. }
  2183. memcpy(buf + write_len, &dci_header, dci_header_size);
  2184. write_len += dci_header_size;
  2185. memcpy(buf + write_len, &event_mask, event_header_size);
  2186. write_len += event_header_size;
  2187. memcpy(buf + write_len, event_mask_ptr, DCI_EVENT_MASK_SIZE);
  2188. write_len += DCI_EVENT_MASK_SIZE;
  2189. *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
  2190. write_len += sizeof(uint8_t);
  2191. err = diag_dci_write_bridge(token, buf, write_len);
  2192. if (err) {
  2193. pr_err("diag: error writing event mask to remote proc, token: %d, err: %d\n",
  2194. token, err);
  2195. diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
  2196. ret = err;
  2197. } else {
  2198. ret = DIAG_DCI_NO_ERROR;
  2199. }
  2200. mutex_unlock(&dci_event_mask_mutex);
  2201. return ret;
  2202. }
  2203. #endif
  2204. int diag_send_dci_event_mask(int token)
  2205. {
  2206. void *buf = event_mask.update_buf;
  2207. struct diag_ctrl_event_mask header;
  2208. int header_size = sizeof(struct diag_ctrl_event_mask);
  2209. int ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR, i;
  2210. unsigned char *event_mask_ptr = NULL;
  2211. mutex_lock(&dci_event_mask_mutex);
  2212. event_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].event_mask_composite;
  2213. if (!event_mask_ptr) {
  2214. mutex_unlock(&dci_event_mask_mutex);
  2215. return -EINVAL;
  2216. }
  2217. mutex_lock(&event_mask.lock);
  2218. /* send event mask update */
  2219. header.cmd_type = DIAG_CTRL_MSG_EVENT_MASK;
  2220. header.data_len = EVENT_MASK_CTRL_HEADER_LEN + DCI_EVENT_MASK_SIZE;
  2221. header.stream_id = DCI_MASK_STREAM;
  2222. header.status = DIAG_CTRL_MASK_VALID;
  2223. header.event_config = 0; /* event config */
  2224. header.event_mask_size = DCI_EVENT_MASK_SIZE;
  2225. for (i = 0; i < DCI_EVENT_MASK_SIZE; i++) {
  2226. if (event_mask_ptr[i] != 0) {
  2227. header.event_config = 1;
  2228. break;
  2229. }
  2230. }
  2231. memcpy(buf, &header, header_size);
  2232. memcpy(buf+header_size, event_mask_ptr, DCI_EVENT_MASK_SIZE);
  2233. for (i = 0; i < NUM_PERIPHERALS; i++) {
  2234. /*
  2235. * Don't send to peripheral if its regular channel
  2236. * is down. It may also mean that the peripheral doesn't
  2237. * support DCI.
  2238. */
  2239. if (check_peripheral_dci_support(i, DCI_LOCAL_PROC)) {
  2240. err = diag_dci_write_proc(i, DIAG_CNTL_TYPE, buf,
  2241. header_size + DCI_EVENT_MASK_SIZE);
  2242. if (err != DIAG_DCI_NO_ERROR)
  2243. ret = DIAG_DCI_SEND_DATA_FAIL;
  2244. }
  2245. }
  2246. mutex_unlock(&event_mask.lock);
  2247. mutex_unlock(&dci_event_mask_mutex);
  2248. return ret;
  2249. }
  2250. void update_dci_cumulative_log_mask(int offset, unsigned int byte_index,
  2251. uint8_t byte_mask, int token)
  2252. {
  2253. uint8_t *log_mask_ptr, *update_ptr = NULL;
  2254. bool is_set = false;
  2255. struct list_head *start, *temp;
  2256. struct diag_dci_client_tbl *entry = NULL;
  2257. mutex_lock(&dci_log_mask_mutex);
  2258. update_ptr = dci_ops_tbl[token].log_mask_composite;
  2259. if (!update_ptr) {
  2260. mutex_unlock(&dci_log_mask_mutex);
  2261. return;
  2262. }
  2263. update_ptr += offset;
  2264. /* update the dirty bit */
  2265. *(update_ptr+1) = 1;
  2266. update_ptr = update_ptr + byte_index;
  2267. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2268. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2269. if (entry->client_info.token != token)
  2270. continue;
  2271. log_mask_ptr = entry->dci_log_mask;
  2272. log_mask_ptr = log_mask_ptr + offset + byte_index;
  2273. if ((*log_mask_ptr & byte_mask) == byte_mask) {
  2274. is_set = true;
  2275. /* break even if one client has the log mask set */
  2276. break;
  2277. }
  2278. }
  2279. if (is_set == false)
  2280. *update_ptr &= ~byte_mask;
  2281. else
  2282. *update_ptr |= byte_mask;
  2283. mutex_unlock(&dci_log_mask_mutex);
  2284. }
  2285. void diag_dci_invalidate_cumulative_log_mask(int token)
  2286. {
  2287. int i = 0;
  2288. struct list_head *start, *temp;
  2289. struct diag_dci_client_tbl *entry = NULL;
  2290. uint8_t *log_mask_ptr, *update_ptr = NULL;
  2291. /* Clear the composite mask and redo all the masks */
  2292. mutex_lock(&dci_log_mask_mutex);
  2293. update_ptr = dci_ops_tbl[token].log_mask_composite;
  2294. if (!update_ptr) {
  2295. mutex_unlock(&dci_log_mask_mutex);
  2296. return;
  2297. }
  2298. create_dci_log_mask_tbl(update_ptr, DCI_LOG_MASK_DIRTY);
  2299. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2300. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2301. if (entry->client_info.token != token)
  2302. continue;
  2303. log_mask_ptr = entry->dci_log_mask;
  2304. for (i = 0; i < DCI_LOG_MASK_SIZE; i++)
  2305. *(update_ptr+i) |= *(log_mask_ptr+i);
  2306. }
  2307. mutex_unlock(&dci_log_mask_mutex);
  2308. }
  2309. static int dci_fill_log_mask(unsigned char *dest_ptr, unsigned char *src_ptr)
  2310. {
  2311. struct diag_ctrl_log_mask header;
  2312. int header_len = sizeof(struct diag_ctrl_log_mask);
  2313. header.cmd_type = DIAG_CTRL_MSG_LOG_MASK;
  2314. header.num_items = DCI_MAX_ITEMS_PER_LOG_CODE;
  2315. header.data_len = 11 + DCI_MAX_ITEMS_PER_LOG_CODE;
  2316. header.stream_id = DCI_MASK_STREAM;
  2317. header.status = 3;
  2318. header.equip_id = *src_ptr;
  2319. header.log_mask_size = DCI_MAX_ITEMS_PER_LOG_CODE;
  2320. memcpy(dest_ptr, &header, header_len);
  2321. memcpy(dest_ptr + header_len, src_ptr + 2, DCI_MAX_ITEMS_PER_LOG_CODE);
  2322. return header_len + DCI_MAX_ITEMS_PER_LOG_CODE;
  2323. }
  2324. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  2325. int diag_send_dci_log_mask_remote(int token)
  2326. {
  2327. unsigned char *buf = NULL;
  2328. struct diag_dci_header_t dci_header;
  2329. int dci_header_size = sizeof(struct diag_dci_header_t);
  2330. int log_header_size = sizeof(struct diag_ctrl_log_mask);
  2331. uint8_t *log_mask_ptr = NULL;
  2332. int i, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
  2333. int updated;
  2334. uint32_t write_len = 0;
  2335. mutex_lock(&dci_log_mask_mutex);
  2336. log_mask_ptr = dci_ops_tbl[token].log_mask_composite;
  2337. if (!log_mask_ptr) {
  2338. mutex_unlock(&dci_log_mask_mutex);
  2339. return -EINVAL;
  2340. }
  2341. /* DCI header is common to all equipment IDs */
  2342. dci_header.start = CONTROL_CHAR;
  2343. dci_header.version = 1;
  2344. dci_header.length = log_header_size + DCI_MAX_ITEMS_PER_LOG_CODE + 1;
  2345. dci_header.cmd_code = DCI_CONTROL_PKT_CODE;
  2346. for (i = 0; i < DCI_MAX_LOG_CODES; i++) {
  2347. updated = 1;
  2348. write_len = 0;
  2349. if (!*(log_mask_ptr + 1)) {
  2350. log_mask_ptr += 514;
  2351. continue;
  2352. }
  2353. buf = dci_get_buffer_from_bridge(token);
  2354. if (!buf) {
  2355. pr_err("diag: In %s, unable to get dci buffers to write data\n",
  2356. __func__);
  2357. mutex_unlock(&dci_log_mask_mutex);
  2358. return -EAGAIN;
  2359. }
  2360. memcpy(buf + write_len, &dci_header, dci_header_size);
  2361. write_len += dci_header_size;
  2362. write_len += dci_fill_log_mask(buf + write_len, log_mask_ptr);
  2363. *(buf + write_len) = CONTROL_CHAR; /* End Terminator */
  2364. write_len += sizeof(uint8_t);
  2365. err = diag_dci_write_bridge(token, buf, write_len);
  2366. if (err) {
  2367. pr_err("diag: error writing log mask to remote processor, equip_id: %d, token: %d, err: %d\n",
  2368. i, token, err);
  2369. diagmem_free(driver, buf, dci_ops_tbl[token].mempool);
  2370. updated = 0;
  2371. }
  2372. if (updated)
  2373. *(log_mask_ptr + 1) = 0; /* clear dirty byte */
  2374. log_mask_ptr += 514;
  2375. }
  2376. mutex_unlock(&dci_log_mask_mutex);
  2377. return ret;
  2378. }
  2379. #endif
  2380. int diag_send_dci_log_mask(int token)
  2381. {
  2382. void *buf = log_mask.update_buf;
  2383. int write_len = 0;
  2384. uint8_t *log_mask_ptr = NULL;
  2385. int i, j, ret = DIAG_DCI_NO_ERROR, err = DIAG_DCI_NO_ERROR;
  2386. int updated;
  2387. mutex_lock(&dci_log_mask_mutex);
  2388. log_mask_ptr = dci_ops_tbl[DCI_LOCAL_PROC].log_mask_composite;
  2389. if (!log_mask_ptr) {
  2390. mutex_unlock(&dci_log_mask_mutex);
  2391. return -EINVAL;
  2392. }
  2393. mutex_lock(&log_mask.lock);
  2394. for (i = 0; i < 16; i++) {
  2395. updated = 1;
  2396. /* Dirty bit is set don't update the mask for this equip id */
  2397. if (!(*(log_mask_ptr + 1))) {
  2398. log_mask_ptr += 514;
  2399. continue;
  2400. }
  2401. write_len = dci_fill_log_mask(buf, log_mask_ptr);
  2402. for (j = 0; j < NUM_PERIPHERALS && write_len; j++) {
  2403. if (check_peripheral_dci_support(j, DCI_LOCAL_PROC)) {
  2404. err = diag_dci_write_proc(j, DIAG_CNTL_TYPE,
  2405. buf, write_len);
  2406. if (err != DIAG_DCI_NO_ERROR) {
  2407. updated = 0;
  2408. ret = DIAG_DCI_SEND_DATA_FAIL;
  2409. }
  2410. }
  2411. }
  2412. if (updated)
  2413. *(log_mask_ptr+1) = 0; /* clear dirty byte */
  2414. log_mask_ptr += 514;
  2415. }
  2416. mutex_unlock(&log_mask.lock);
  2417. mutex_unlock(&dci_log_mask_mutex);
  2418. return ret;
  2419. }
  2420. static int diag_dci_init_local(void)
  2421. {
  2422. struct dci_ops_tbl_t *temp = &dci_ops_tbl[DCI_LOCAL_PROC];
  2423. create_dci_log_mask_tbl(temp->log_mask_composite, DCI_LOG_MASK_CLEAN);
  2424. create_dci_event_mask_tbl(temp->event_mask_composite);
  2425. temp->peripheral_status |= DIAG_CON_APSS;
  2426. return 0;
  2427. }
  2428. #ifdef CONFIG_DIAGFWD_BRIDGE_CODE
  2429. static void diag_dci_init_handshake_remote(void)
  2430. {
  2431. int i;
  2432. struct dci_channel_status_t *temp = NULL;
  2433. for (i = DCI_REMOTE_BASE; i < NUM_DCI_PROC; i++) {
  2434. temp = &dci_channel_status[i];
  2435. temp->id = i;
  2436. setup_timer(&temp->wait_time, dci_chk_handshake, i);
  2437. INIT_WORK(&temp->handshake_work, dci_handshake_work_fn);
  2438. }
  2439. }
  2440. int diag_dci_init_remote(void)
  2441. {
  2442. int i;
  2443. struct dci_ops_tbl_t *temp = NULL;
  2444. diagmem_init(driver, POOL_TYPE_MDM_DCI_WRITE);
  2445. for (i = DCI_REMOTE_BASE; i < DCI_REMOTE_LAST; i++) {
  2446. temp = &dci_ops_tbl[i];
  2447. create_dci_log_mask_tbl(temp->log_mask_composite,
  2448. DCI_LOG_MASK_CLEAN);
  2449. create_dci_event_mask_tbl(temp->event_mask_composite);
  2450. }
  2451. partial_pkt.data = vzalloc(MAX_DCI_PACKET_SZ);
  2452. if (!partial_pkt.data)
  2453. return -ENOMEM;
  2454. partial_pkt.total_len = 0;
  2455. partial_pkt.read_len = 0;
  2456. partial_pkt.remaining = 0;
  2457. partial_pkt.processing = 0;
  2458. diag_dci_init_handshake_remote();
  2459. return 0;
  2460. }
  2461. #endif
  2462. static int diag_dci_init_ops_tbl(void)
  2463. {
  2464. int err = 0;
  2465. err = diag_dci_init_local();
  2466. if (err)
  2467. goto err;
  2468. return 0;
  2469. err:
  2470. return -ENOMEM;
  2471. }
  2472. int diag_dci_init(void)
  2473. {
  2474. int ret = 0;
  2475. driver->dci_tag = 0;
  2476. driver->dci_client_id = 0;
  2477. driver->num_dci_client = 0;
  2478. mutex_init(&driver->dci_mutex);
  2479. mutex_init(&dci_log_mask_mutex);
  2480. mutex_init(&dci_event_mask_mutex);
  2481. spin_lock_init(&ws_lock);
  2482. ret = diag_dci_init_ops_tbl();
  2483. if (ret)
  2484. goto err;
  2485. if (driver->apps_dci_buf == NULL) {
  2486. driver->apps_dci_buf = vzalloc(DCI_BUF_SIZE);
  2487. if (driver->apps_dci_buf == NULL)
  2488. goto err;
  2489. }
  2490. INIT_LIST_HEAD(&driver->dci_client_list);
  2491. INIT_LIST_HEAD(&driver->dci_req_list);
  2492. driver->diag_dci_wq = create_singlethread_workqueue("diag_dci_wq");
  2493. if (!driver->diag_dci_wq)
  2494. goto err;
  2495. INIT_WORK(&dci_data_drain_work, dci_data_drain_work_fn);
  2496. setup_timer(&dci_drain_timer, dci_drain_data, 0);
  2497. return DIAG_DCI_NO_ERROR;
  2498. err:
  2499. pr_err("diag: Could not initialize diag DCI buffers");
  2500. vfree(driver->apps_dci_buf);
  2501. driver->apps_dci_buf = NULL;
  2502. if (driver->diag_dci_wq)
  2503. destroy_workqueue(driver->diag_dci_wq);
  2504. vfree(partial_pkt.data);
  2505. partial_pkt.data = NULL;
  2506. mutex_destroy(&driver->dci_mutex);
  2507. mutex_destroy(&dci_log_mask_mutex);
  2508. mutex_destroy(&dci_event_mask_mutex);
  2509. return DIAG_DCI_NO_REG;
  2510. }
  2511. void diag_dci_channel_init(void)
  2512. {
  2513. uint8_t peripheral;
  2514. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  2515. diagfwd_open(peripheral, TYPE_DCI);
  2516. diagfwd_open(peripheral, TYPE_DCI_CMD);
  2517. }
  2518. }
  2519. void diag_dci_exit(void)
  2520. {
  2521. vfree(partial_pkt.data);
  2522. partial_pkt.data = NULL;
  2523. vfree(driver->apps_dci_buf);
  2524. driver->apps_dci_buf = NULL;
  2525. mutex_destroy(&driver->dci_mutex);
  2526. mutex_destroy(&dci_log_mask_mutex);
  2527. mutex_destroy(&dci_event_mask_mutex);
  2528. destroy_workqueue(driver->diag_dci_wq);
  2529. }
  2530. int diag_dci_clear_log_mask(int client_id)
  2531. {
  2532. int err = DIAG_DCI_NO_ERROR, token = DCI_LOCAL_PROC;
  2533. uint8_t *update_ptr;
  2534. struct diag_dci_client_tbl *entry = NULL;
  2535. entry = diag_dci_get_client_entry(client_id);
  2536. if (!entry) {
  2537. pr_err("diag: In %s, invalid client entry\n", __func__);
  2538. return DIAG_DCI_TABLE_ERR;
  2539. }
  2540. token = entry->client_info.token;
  2541. update_ptr = dci_ops_tbl[token].log_mask_composite;
  2542. create_dci_log_mask_tbl(entry->dci_log_mask, DCI_LOG_MASK_CLEAN);
  2543. diag_dci_invalidate_cumulative_log_mask(token);
  2544. /*
  2545. * Send updated mask to userspace clients only if the client
  2546. * is registered on the local processor
  2547. */
  2548. if (token == DCI_LOCAL_PROC)
  2549. diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
  2550. /* Send updated mask to peripherals */
  2551. err = dci_ops_tbl[token].send_log_mask(token);
  2552. return err;
  2553. }
  2554. int diag_dci_clear_event_mask(int client_id)
  2555. {
  2556. int err = DIAG_DCI_NO_ERROR, token = DCI_LOCAL_PROC;
  2557. uint8_t *update_ptr;
  2558. struct diag_dci_client_tbl *entry = NULL;
  2559. entry = diag_dci_get_client_entry(client_id);
  2560. if (!entry) {
  2561. pr_err("diag: In %s, invalid client entry\n", __func__);
  2562. return DIAG_DCI_TABLE_ERR;
  2563. }
  2564. token = entry->client_info.token;
  2565. update_ptr = dci_ops_tbl[token].event_mask_composite;
  2566. create_dci_event_mask_tbl(entry->dci_event_mask);
  2567. diag_dci_invalidate_cumulative_event_mask(token);
  2568. /*
  2569. * Send updated mask to userspace clients only if the client is
  2570. * registerted on the local processor
  2571. */
  2572. if (token == DCI_LOCAL_PROC)
  2573. diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
  2574. /* Send updated mask to peripherals */
  2575. err = dci_ops_tbl[token].send_event_mask(token);
  2576. return err;
  2577. }
  2578. uint8_t diag_dci_get_cumulative_real_time(int token)
  2579. {
  2580. uint8_t real_time = MODE_NONREALTIME;
  2581. struct list_head *start, *temp;
  2582. struct diag_dci_client_tbl *entry = NULL;
  2583. list_for_each_safe(start, temp, &driver->dci_client_list) {
  2584. entry = list_entry(start, struct diag_dci_client_tbl, track);
  2585. if (entry->real_time == MODE_REALTIME &&
  2586. entry->client_info.token == token) {
  2587. real_time = 1;
  2588. break;
  2589. }
  2590. }
  2591. return real_time;
  2592. }
  2593. int diag_dci_set_real_time(struct diag_dci_client_tbl *entry, uint8_t real_time)
  2594. {
  2595. if (!entry) {
  2596. pr_err("diag: In %s, invalid client entry\n", __func__);
  2597. return 0;
  2598. }
  2599. entry->real_time = real_time;
  2600. return 1;
  2601. }
  2602. int diag_dci_register_client(struct diag_dci_reg_tbl_t *reg_entry)
  2603. {
  2604. int i, err = 0;
  2605. struct diag_dci_client_tbl *new_entry = NULL;
  2606. struct diag_dci_buf_peripheral_t *proc_buf = NULL;
  2607. if (!reg_entry)
  2608. return DIAG_DCI_NO_REG;
  2609. if (!VALID_DCI_TOKEN(reg_entry->token)) {
  2610. pr_alert("diag: Invalid DCI client token, %d\n",
  2611. reg_entry->token);
  2612. return DIAG_DCI_NO_REG;
  2613. }
  2614. if (driver->dci_state == DIAG_DCI_NO_REG)
  2615. return DIAG_DCI_NO_REG;
  2616. if (driver->num_dci_client >= MAX_DCI_CLIENTS)
  2617. return DIAG_DCI_NO_REG;
  2618. new_entry = kzalloc(sizeof(struct diag_dci_client_tbl), GFP_KERNEL);
  2619. if (!new_entry)
  2620. return DIAG_DCI_NO_REG;
  2621. mutex_lock(&driver->dci_mutex);
  2622. get_task_struct(current);
  2623. new_entry->client = current;
  2624. new_entry->tgid = current->tgid;
  2625. new_entry->client_info.notification_list =
  2626. reg_entry->notification_list;
  2627. new_entry->client_info.signal_type =
  2628. reg_entry->signal_type;
  2629. new_entry->client_info.token = reg_entry->token;
  2630. switch (reg_entry->token) {
  2631. case DCI_LOCAL_PROC:
  2632. new_entry->num_buffers = NUM_DCI_PERIPHERALS;
  2633. break;
  2634. case DCI_MDM_PROC:
  2635. new_entry->num_buffers = 1;
  2636. break;
  2637. }
  2638. new_entry->buffers = NULL;
  2639. new_entry->real_time = MODE_REALTIME;
  2640. new_entry->in_service = 0;
  2641. INIT_LIST_HEAD(&new_entry->list_write_buf);
  2642. mutex_init(&new_entry->write_buf_mutex);
  2643. new_entry->dci_log_mask = vzalloc(DCI_LOG_MASK_SIZE);
  2644. if (!new_entry->dci_log_mask) {
  2645. pr_err("diag: Unable to create log mask for client, %d",
  2646. driver->dci_client_id);
  2647. goto fail_alloc;
  2648. }
  2649. create_dci_log_mask_tbl(new_entry->dci_log_mask, DCI_LOG_MASK_CLEAN);
  2650. new_entry->dci_event_mask = vzalloc(DCI_EVENT_MASK_SIZE);
  2651. if (!new_entry->dci_event_mask)
  2652. goto fail_alloc;
  2653. create_dci_event_mask_tbl(new_entry->dci_event_mask);
  2654. new_entry->buffers = kzalloc(new_entry->num_buffers *
  2655. sizeof(struct diag_dci_buf_peripheral_t),
  2656. GFP_KERNEL);
  2657. if (!new_entry->buffers) {
  2658. pr_err("diag: Unable to allocate buffers for peripherals in %s\n",
  2659. __func__);
  2660. goto fail_alloc;
  2661. }
  2662. for (i = 0; i < new_entry->num_buffers; i++) {
  2663. proc_buf = &new_entry->buffers[i];
  2664. if (!proc_buf)
  2665. goto fail_alloc;
  2666. mutex_init(&proc_buf->health_mutex);
  2667. mutex_init(&proc_buf->buf_mutex);
  2668. proc_buf->health.dropped_events = 0;
  2669. proc_buf->health.dropped_logs = 0;
  2670. proc_buf->health.received_events = 0;
  2671. proc_buf->health.received_logs = 0;
  2672. proc_buf->buf_primary = kzalloc(
  2673. sizeof(struct diag_dci_buffer_t),
  2674. GFP_KERNEL);
  2675. if (!proc_buf->buf_primary)
  2676. goto fail_alloc;
  2677. proc_buf->buf_cmd = kzalloc(sizeof(struct diag_dci_buffer_t),
  2678. GFP_KERNEL);
  2679. if (!proc_buf->buf_cmd)
  2680. goto fail_alloc;
  2681. err = diag_dci_init_buffer(proc_buf->buf_primary,
  2682. DCI_BUF_PRIMARY);
  2683. if (err)
  2684. goto fail_alloc;
  2685. err = diag_dci_init_buffer(proc_buf->buf_cmd, DCI_BUF_CMD);
  2686. if (err)
  2687. goto fail_alloc;
  2688. proc_buf->buf_curr = proc_buf->buf_primary;
  2689. }
  2690. list_add_tail(&new_entry->track, &driver->dci_client_list);
  2691. driver->dci_client_id++;
  2692. new_entry->client_info.client_id = driver->dci_client_id;
  2693. reg_entry->client_id = driver->dci_client_id;
  2694. driver->num_dci_client++;
  2695. if (driver->num_dci_client == 1)
  2696. diag_update_proc_vote(DIAG_PROC_DCI, VOTE_UP, reg_entry->token);
  2697. queue_work(driver->diag_real_time_wq, &driver->diag_real_time_work);
  2698. mutex_unlock(&driver->dci_mutex);
  2699. return driver->dci_client_id;
  2700. fail_alloc:
  2701. if (new_entry) {
  2702. for (i = 0; ((i < new_entry->num_buffers) &&
  2703. new_entry->buffers); i++) {
  2704. proc_buf = &new_entry->buffers[i];
  2705. if (proc_buf) {
  2706. mutex_destroy(&proc_buf->health_mutex);
  2707. if (proc_buf->buf_primary) {
  2708. vfree(proc_buf->buf_primary->data);
  2709. proc_buf->buf_primary->data = NULL;
  2710. mutex_destroy(
  2711. &proc_buf->buf_primary->data_mutex);
  2712. }
  2713. kfree(proc_buf->buf_primary);
  2714. proc_buf->buf_primary = NULL;
  2715. if (proc_buf->buf_cmd) {
  2716. vfree(proc_buf->buf_cmd->data);
  2717. proc_buf->buf_cmd->data = NULL;
  2718. mutex_destroy(
  2719. &proc_buf->buf_cmd->data_mutex);
  2720. }
  2721. kfree(proc_buf->buf_cmd);
  2722. proc_buf->buf_cmd = NULL;
  2723. }
  2724. }
  2725. vfree(new_entry->dci_event_mask);
  2726. new_entry->dci_event_mask = NULL;
  2727. vfree(new_entry->dci_log_mask);
  2728. new_entry->dci_log_mask = NULL;
  2729. kfree(new_entry->buffers);
  2730. new_entry->buffers = NULL;
  2731. kfree(new_entry);
  2732. new_entry = NULL;
  2733. }
  2734. mutex_unlock(&driver->dci_mutex);
  2735. return DIAG_DCI_NO_REG;
  2736. }
  2737. int diag_dci_deinit_client(struct diag_dci_client_tbl *entry)
  2738. {
  2739. int ret = DIAG_DCI_NO_ERROR, real_time = MODE_REALTIME, i, peripheral;
  2740. struct diag_dci_buf_peripheral_t *proc_buf = NULL;
  2741. struct diag_dci_buffer_t *buf_entry, *temp;
  2742. struct list_head *start, *req_temp;
  2743. struct dci_pkt_req_entry_t *req_entry = NULL;
  2744. int token = DCI_LOCAL_PROC;
  2745. if (!entry)
  2746. return DIAG_DCI_NOT_SUPPORTED;
  2747. token = entry->client_info.token;
  2748. /*
  2749. * Remove the entry from the list before freeing the buffers
  2750. * to ensure that we don't have any invalid access.
  2751. */
  2752. if (!list_empty(&entry->track))
  2753. list_del(&entry->track);
  2754. driver->num_dci_client--;
  2755. put_task_struct(entry->client);
  2756. entry->client = NULL;
  2757. /*
  2758. * Clear the client's log and event masks, update the cumulative
  2759. * masks and send the masks to peripherals
  2760. */
  2761. vfree(entry->dci_log_mask);
  2762. entry->dci_log_mask = NULL;
  2763. diag_dci_invalidate_cumulative_log_mask(token);
  2764. if (token == DCI_LOCAL_PROC)
  2765. diag_update_userspace_clients(DCI_LOG_MASKS_TYPE);
  2766. ret = dci_ops_tbl[token].send_log_mask(token);
  2767. if (ret != DIAG_DCI_NO_ERROR)
  2768. return ret;
  2769. vfree(entry->dci_event_mask);
  2770. entry->dci_event_mask = NULL;
  2771. diag_dci_invalidate_cumulative_event_mask(token);
  2772. if (token == DCI_LOCAL_PROC)
  2773. diag_update_userspace_clients(DCI_EVENT_MASKS_TYPE);
  2774. ret = dci_ops_tbl[token].send_event_mask(token);
  2775. if (ret != DIAG_DCI_NO_ERROR)
  2776. return ret;
  2777. list_for_each_safe(start, req_temp, &driver->dci_req_list) {
  2778. req_entry = list_entry(start, struct dci_pkt_req_entry_t,
  2779. track);
  2780. if (req_entry->client_id == entry->client_info.client_id) {
  2781. if (!list_empty(&req_entry->track))
  2782. list_del(&req_entry->track);
  2783. kfree(req_entry);
  2784. req_entry = NULL;
  2785. }
  2786. }
  2787. /* Clean up any buffer that is pending write */
  2788. mutex_lock(&entry->write_buf_mutex);
  2789. list_for_each_entry_safe(buf_entry, temp, &entry->list_write_buf,
  2790. buf_track) {
  2791. if (!list_empty(&buf_entry->buf_track))
  2792. list_del(&buf_entry->buf_track);
  2793. if (buf_entry->buf_type == DCI_BUF_SECONDARY) {
  2794. mutex_lock(&buf_entry->data_mutex);
  2795. diagmem_free(driver, buf_entry->data, POOL_TYPE_DCI);
  2796. buf_entry->data = NULL;
  2797. mutex_unlock(&buf_entry->data_mutex);
  2798. kfree(buf_entry);
  2799. buf_entry = NULL;
  2800. } else if (buf_entry->buf_type == DCI_BUF_CMD) {
  2801. peripheral = buf_entry->data_source;
  2802. if (peripheral == APPS_DATA)
  2803. continue;
  2804. }
  2805. /*
  2806. * These are buffers that can't be written to the client which
  2807. * means that the copy cannot be completed. Make sure that we
  2808. * remove those references in DCI wakeup source.
  2809. */
  2810. diag_ws_on_copy_fail(DIAG_WS_DCI);
  2811. }
  2812. mutex_unlock(&entry->write_buf_mutex);
  2813. for (i = 0; i < entry->num_buffers; i++) {
  2814. proc_buf = &entry->buffers[i];
  2815. buf_entry = proc_buf->buf_curr;
  2816. mutex_lock(&proc_buf->buf_mutex);
  2817. /* Clean up secondary buffer from mempool that is active */
  2818. if (buf_entry && buf_entry->buf_type == DCI_BUF_SECONDARY) {
  2819. mutex_lock(&buf_entry->data_mutex);
  2820. diagmem_free(driver, buf_entry->data, POOL_TYPE_DCI);
  2821. buf_entry->data = NULL;
  2822. mutex_unlock(&buf_entry->data_mutex);
  2823. mutex_destroy(&buf_entry->data_mutex);
  2824. kfree(buf_entry);
  2825. buf_entry = NULL;
  2826. }
  2827. mutex_lock(&proc_buf->buf_primary->data_mutex);
  2828. vfree(proc_buf->buf_primary->data);
  2829. proc_buf->buf_primary->data = NULL;
  2830. mutex_unlock(&proc_buf->buf_primary->data_mutex);
  2831. mutex_lock(&proc_buf->buf_cmd->data_mutex);
  2832. vfree(proc_buf->buf_cmd->data);
  2833. proc_buf->buf_cmd->data = NULL;
  2834. mutex_unlock(&proc_buf->buf_cmd->data_mutex);
  2835. mutex_destroy(&proc_buf->health_mutex);
  2836. mutex_destroy(&proc_buf->buf_primary->data_mutex);
  2837. mutex_destroy(&proc_buf->buf_cmd->data_mutex);
  2838. kfree(proc_buf->buf_primary);
  2839. proc_buf->buf_primary = NULL;
  2840. kfree(proc_buf->buf_cmd);
  2841. proc_buf->buf_cmd = NULL;
  2842. mutex_unlock(&proc_buf->buf_mutex);
  2843. }
  2844. mutex_destroy(&entry->write_buf_mutex);
  2845. kfree(entry->buffers);
  2846. entry->buffers = NULL;
  2847. kfree(entry);
  2848. entry = NULL;
  2849. if (driver->num_dci_client == 0) {
  2850. diag_update_proc_vote(DIAG_PROC_DCI, VOTE_DOWN, token);
  2851. } else {
  2852. real_time = diag_dci_get_cumulative_real_time(token);
  2853. diag_update_real_time_vote(DIAG_PROC_DCI, real_time, token);
  2854. }
  2855. queue_work(driver->diag_real_time_wq, &driver->diag_real_time_work);
  2856. return DIAG_DCI_NO_ERROR;
  2857. }
  2858. int diag_dci_write_proc(uint8_t peripheral, int pkt_type, char *buf, int len)
  2859. {
  2860. uint8_t dest_channel = TYPE_DATA;
  2861. int err = 0;
  2862. if (!buf || peripheral >= NUM_PERIPHERALS || len < 0 ||
  2863. !(driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask)) {
  2864. DIAG_LOG(DIAG_DEBUG_DCI,
  2865. "buf: 0x%pK, p: %d, len: %d, f_mask: %d\n",
  2866. buf, peripheral, len,
  2867. driver->feature[PERIPHERAL_MODEM].rcvd_feature_mask);
  2868. return -EINVAL;
  2869. }
  2870. if (pkt_type == DIAG_DATA_TYPE) {
  2871. dest_channel = TYPE_DCI_CMD;
  2872. } else if (pkt_type == DIAG_CNTL_TYPE) {
  2873. dest_channel = TYPE_CNTL;
  2874. } else {
  2875. pr_err("diag: Invalid DCI pkt type in %s", __func__);
  2876. return -EINVAL;
  2877. }
  2878. err = diagfwd_write(peripheral, dest_channel, buf, len);
  2879. if (err && err != -ENODEV) {
  2880. pr_err("diag: In %s, unable to write to peripheral: %d, type: %d, len: %d, err: %d\n",
  2881. __func__, peripheral, dest_channel, len, err);
  2882. } else {
  2883. err = DIAG_DCI_NO_ERROR;
  2884. }
  2885. return err;
  2886. }
  2887. int diag_dci_copy_health_stats(struct diag_dci_health_stats_proc *stats_proc)
  2888. {
  2889. struct diag_dci_client_tbl *entry = NULL;
  2890. struct diag_dci_health_t *health = NULL;
  2891. struct diag_dci_health_stats *stats = NULL;
  2892. int i, proc;
  2893. if (!stats_proc)
  2894. return -EINVAL;
  2895. stats = &stats_proc->health;
  2896. proc = stats_proc->proc;
  2897. if (proc < ALL_PROC || proc > APPS_DATA)
  2898. return -EINVAL;
  2899. entry = diag_dci_get_client_entry(stats_proc->client_id);
  2900. if (!entry)
  2901. return DIAG_DCI_NOT_SUPPORTED;
  2902. /*
  2903. * If the client has registered for remote processor, the
  2904. * proc field doesn't have any effect as they have only one buffer.
  2905. */
  2906. if (entry->client_info.token)
  2907. proc = 0;
  2908. stats->stats.dropped_logs = 0;
  2909. stats->stats.dropped_events = 0;
  2910. stats->stats.received_logs = 0;
  2911. stats->stats.received_events = 0;
  2912. if (proc != ALL_PROC) {
  2913. health = &entry->buffers[proc].health;
  2914. stats->stats.dropped_logs = health->dropped_logs;
  2915. stats->stats.dropped_events = health->dropped_events;
  2916. stats->stats.received_logs = health->received_logs;
  2917. stats->stats.received_events = health->received_events;
  2918. if (stats->reset_status) {
  2919. mutex_lock(&entry->buffers[proc].health_mutex);
  2920. health->dropped_logs = 0;
  2921. health->dropped_events = 0;
  2922. health->received_logs = 0;
  2923. health->received_events = 0;
  2924. mutex_unlock(&entry->buffers[proc].health_mutex);
  2925. }
  2926. return DIAG_DCI_NO_ERROR;
  2927. }
  2928. for (i = 0; i < entry->num_buffers; i++) {
  2929. health = &entry->buffers[i].health;
  2930. stats->stats.dropped_logs += health->dropped_logs;
  2931. stats->stats.dropped_events += health->dropped_events;
  2932. stats->stats.received_logs += health->received_logs;
  2933. stats->stats.received_events += health->received_events;
  2934. if (stats->reset_status) {
  2935. mutex_lock(&entry->buffers[i].health_mutex);
  2936. health->dropped_logs = 0;
  2937. health->dropped_events = 0;
  2938. health->received_logs = 0;
  2939. health->received_events = 0;
  2940. mutex_unlock(&entry->buffers[i].health_mutex);
  2941. }
  2942. }
  2943. return DIAG_DCI_NO_ERROR;
  2944. }
  2945. int diag_dci_get_support_list(struct diag_dci_peripherals_t *support_list)
  2946. {
  2947. if (!support_list)
  2948. return -ENOMEM;
  2949. if (!VALID_DCI_TOKEN(support_list->proc))
  2950. return -EIO;
  2951. support_list->list = dci_ops_tbl[support_list->proc].peripheral_status;
  2952. return DIAG_DCI_NO_ERROR;
  2953. }