usb_notify.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014-2023 Samsung Electronics Co. Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. /* usb notify layer v4.0 */
  11. #define NOTIFY_VERSION "4.0"
  12. #define pr_fmt(fmt) "usb_notify: " fmt
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/irq.h>
  17. #include <linux/gpio.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/err.h>
  22. #include <linux/kthread.h>
  23. #include <linux/reboot.h>
  24. #include <linux/usb_notify.h>
  25. #include <sound/core.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/audio.h>
  28. #include <linux/ratelimit.h>
  29. #include <linux/version.h>
  30. #include "host_notify_class.h"
  31. #include "dock_notify.h"
  32. #include "usb_notify_sysfs.h"
  33. #define DEFAULT_OVC_POLL_SEC 3
  34. #define MAX_SECURE_CONNECTION 10
  35. #define MAX_VAL 0x7FFFFFFF
  36. struct ovc {
  37. struct otg_notify *o_notify;
  38. wait_queue_head_t delay_wait;
  39. struct completion scanning_done;
  40. struct task_struct *th;
  41. struct mutex ovc_lock;
  42. int thread_remove;
  43. int can_ovc;
  44. int poll_period;
  45. int prev_state;
  46. void *data;
  47. int (*check_state)(void *data);
  48. };
  49. struct vbus_gpio {
  50. spinlock_t lock;
  51. int gpio_status;
  52. };
  53. struct otg_state_work {
  54. struct otg_notify *o_notify;
  55. struct work_struct otg_work;
  56. unsigned long event;
  57. int enable;
  58. };
  59. struct otg_booting_delay {
  60. struct delayed_work booting_work;
  61. unsigned long reserve_state;
  62. };
  63. struct typec_info {
  64. int data_role;
  65. int power_role;
  66. int pd;
  67. int doing_drswap;
  68. int doing_prswap;
  69. };
  70. struct usb_gadget_info {
  71. int bus_state;
  72. int usb_cable_connect;
  73. };
  74. struct usb_notify {
  75. struct otg_notify *o_notify;
  76. struct atomic_notifier_head otg_notifier;
  77. struct blocking_notifier_head extra_notifier;
  78. struct notifier_block otg_nb;
  79. struct notifier_block extra_nb;
  80. struct vbus_gpio v_gpio;
  81. struct host_notify_dev ndev;
  82. struct usb_notify_dev udev;
  83. struct workqueue_struct *notifier_wq;
  84. struct wakeup_source ws;
  85. struct otg_booster *booster;
  86. struct ovc ovc_info;
  87. struct otg_booting_delay b_delay;
  88. struct delayed_work check_work;
  89. struct work_struct reverse_bypass_on_work;
  90. struct typec_info typec_status;
  91. struct usb_gadget_info gadget_status;
  92. struct mutex state_lock;
  93. spinlock_t event_spin_lock;
  94. wait_queue_head_t init_delay;
  95. int is_device;
  96. int cond_max_speed;
  97. int check_work_complete;
  98. int oc_noti;
  99. int disable_v_drive;
  100. unsigned long c_type;
  101. int c_status;
  102. int sec_whitelist_enable;
  103. int sec_whitelist_enable_for_id;
  104. int sec_whitelist_enable_for_serial;
  105. int reserve_vbus_booster;
  106. int disable_state;
  107. int reverse_bypass_status;
  108. int lock_state;
  109. int restricted;
  110. int allowlist_restricted;
  111. int cond_sshub;
  112. int cond_hshub;
  113. int skip_possible_usb;
  114. unsigned int secure_connect_group[USB_GROUP_MAX];
  115. #if defined(CONFIG_USB_HW_PARAM)
  116. unsigned long long hw_param[USB_CCIC_HW_PARAM_MAX];
  117. #endif
  118. };
  119. struct usb_notify_core {
  120. struct otg_notify *o_notify;
  121. unsigned int lpm_charging_type_done;
  122. };
  123. static struct usb_notify_core *u_notify_core;
  124. /*
  125. * Define event types.
  126. * NOTIFY_EVENT_STATE can be called in both interrupt context
  127. * and process context. But it executes queue_work.
  128. * NOTIFY_EVENT_EXTRA can be called directly without queue_work.
  129. * But it must be called in process context.
  130. * NOTIFY_EVENT_DELAY events can not run inner booting delay.
  131. * NOTIFY_EVENT_NEED_VBUSDRIVE events need to drive 5v out
  132. * from phone charger ic
  133. * NOTIFY_EVENT_NOBLOCKING events are not blocked by disable sysfs.
  134. * NOTIFY_EVENT_NOSAVE events are not saved in cable type.
  135. */
  136. static int check_event_type(enum otg_notify_events event)
  137. {
  138. int ret = 0;
  139. switch (event) {
  140. case NOTIFY_EVENT_OVERCURRENT:
  141. case NOTIFY_EVENT_VBUSPOWER:
  142. case NOTIFY_EVENT_SMSC_OVC:
  143. case NOTIFY_EVENT_SMTD_EXT_CURRENT:
  144. case NOTIFY_EVENT_MMD_EXT_CURRENT:
  145. case NOTIFY_EVENT_HMD_EXT_CURRENT:
  146. case NOTIFY_EVENT_DEVICE_CONNECT:
  147. case NOTIFY_EVENT_GAMEPAD_CONNECT:
  148. case NOTIFY_EVENT_LANHUB_CONNECT:
  149. case NOTIFY_EVENT_POWER_SOURCE:
  150. case NOTIFY_EVENT_PD_CONTRACT:
  151. case NOTIFY_EVENT_VBUS_RESET:
  152. case NOTIFY_EVENT_RESERVE_BOOSTER:
  153. case NOTIFY_EVENT_USB_CABLE:
  154. case NOTIFY_EVENT_USBD_SUSPENDED:
  155. case NOTIFY_EVENT_USBD_UNCONFIGURED:
  156. case NOTIFY_EVENT_USBD_CONFIGURED:
  157. case NOTIFY_EVENT_DR_SWAP:
  158. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_CONNECT:
  159. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_ATTACH:
  160. ret |= NOTIFY_EVENT_EXTRA;
  161. break;
  162. case NOTIFY_EVENT_VBUS:
  163. case NOTIFY_EVENT_SMARTDOCK_USB:
  164. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_DELAY
  165. | NOTIFY_EVENT_NEED_CLIENT);
  166. break;
  167. case NOTIFY_EVENT_HOST:
  168. case NOTIFY_EVENT_HMT:
  169. case NOTIFY_EVENT_GAMEPAD:
  170. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_NEED_VBUSDRIVE
  171. | NOTIFY_EVENT_DELAY | NOTIFY_EVENT_NEED_HOST);
  172. break;
  173. case NOTIFY_EVENT_POGO:
  174. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_DELAY
  175. | NOTIFY_EVENT_NEED_HOST);
  176. break;
  177. case NOTIFY_EVENT_HOST_RELOAD:
  178. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_NEED_HOST
  179. | NOTIFY_EVENT_NOSAVE);
  180. break;
  181. case NOTIFY_EVENT_ALL_DISABLE:
  182. case NOTIFY_EVENT_HOST_DISABLE:
  183. case NOTIFY_EVENT_CLIENT_DISABLE:
  184. case NOTIFY_EVENT_MDM_ON_OFF:
  185. case NOTIFY_EVENT_MDM_ON_OFF_FOR_ID:
  186. case NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL:
  187. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_NOBLOCKING
  188. | NOTIFY_EVENT_NOSAVE);
  189. break;
  190. case NOTIFY_EVENT_DRIVE_VBUS:
  191. case NOTIFY_EVENT_LANHUB_TA:
  192. ret |= (NOTIFY_EVENT_STATE | NOTIFY_EVENT_NOSAVE
  193. | NOTIFY_EVENT_NEED_HOST);
  194. break;
  195. case NOTIFY_EVENT_SMARTDOCK_TA:
  196. case NOTIFY_EVENT_AUDIODOCK:
  197. case NOTIFY_EVENT_LANHUB:
  198. case NOTIFY_EVENT_MMDOCK:
  199. ret |= (NOTIFY_EVENT_DELAY | NOTIFY_EVENT_NEED_HOST);
  200. fallthrough;
  201. case NOTIFY_EVENT_CHARGER:
  202. case NOTIFY_EVENT_NONE:
  203. fallthrough;
  204. default:
  205. ret |= NOTIFY_EVENT_STATE;
  206. break;
  207. }
  208. return ret;
  209. }
  210. static int check_same_event_type(enum otg_notify_events event1,
  211. enum otg_notify_events event2)
  212. {
  213. return (check_event_type(event1)
  214. == check_event_type(event2));
  215. }
  216. const char *event_string(enum otg_notify_events event)
  217. {
  218. int virt;
  219. virt = IS_VIRTUAL(event);
  220. event = PHY_EVENT(event);
  221. switch (event) {
  222. case NOTIFY_EVENT_NONE:
  223. return "none";
  224. case NOTIFY_EVENT_VBUS:
  225. return virt ? "vbus(virtual)" : "vbus";
  226. case NOTIFY_EVENT_HOST:
  227. return virt ? "host_id(virtual)" : "host_id";
  228. case NOTIFY_EVENT_CHARGER:
  229. return virt ? "charger(virtual)" : "charger";
  230. case NOTIFY_EVENT_SMARTDOCK_TA:
  231. return virt ? "smartdock_ta(virtual)" : "smartdock_ta";
  232. case NOTIFY_EVENT_SMARTDOCK_USB:
  233. return virt ? "smartdock_usb(virtual)" : "smartdock_usb";
  234. case NOTIFY_EVENT_AUDIODOCK:
  235. return virt ? "audiodock(virtual)" : "audiodock";
  236. case NOTIFY_EVENT_LANHUB:
  237. return virt ? "lanhub(virtual)" : "lanhub";
  238. case NOTIFY_EVENT_LANHUB_TA:
  239. return virt ? "lanhub_ta(virtual)" : "lanhub_ta";
  240. case NOTIFY_EVENT_MMDOCK:
  241. return virt ? "mmdock(virtual)" : "mmdock";
  242. case NOTIFY_EVENT_HMT:
  243. return virt ? "hmt(virtual)" : "hmt";
  244. case NOTIFY_EVENT_GAMEPAD:
  245. return virt ? "gamepad(virtual)" : "gamepad";
  246. case NOTIFY_EVENT_POGO:
  247. return virt ? "pogo(virtual)" : "pogo";
  248. case NOTIFY_EVENT_HOST_RELOAD:
  249. return virt ? "host_reload(virtual)" : "host_reload";
  250. case NOTIFY_EVENT_DRIVE_VBUS:
  251. return "drive_vbus";
  252. case NOTIFY_EVENT_ALL_DISABLE:
  253. return "disable_all_notify";
  254. case NOTIFY_EVENT_HOST_DISABLE:
  255. return "disable_host_notify";
  256. case NOTIFY_EVENT_CLIENT_DISABLE:
  257. return "disable_client_notify";
  258. case NOTIFY_EVENT_MDM_ON_OFF:
  259. return "mdm control_notify";
  260. case NOTIFY_EVENT_MDM_ON_OFF_FOR_ID:
  261. return "mdm control_notify_for_id";
  262. case NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL:
  263. return "mdm control_notify_for_serial";
  264. case NOTIFY_EVENT_OVERCURRENT:
  265. return "overcurrent";
  266. case NOTIFY_EVENT_VBUSPOWER:
  267. return "vbus_power";
  268. case NOTIFY_EVENT_SMSC_OVC:
  269. return "smsc_ovc";
  270. case NOTIFY_EVENT_SMTD_EXT_CURRENT:
  271. return "smtd_ext_current";
  272. case NOTIFY_EVENT_MMD_EXT_CURRENT:
  273. return "mmd_ext_current";
  274. case NOTIFY_EVENT_HMD_EXT_CURRENT:
  275. return "hmd_ext_current";
  276. case NOTIFY_EVENT_DEVICE_CONNECT:
  277. return "device_connect";
  278. case NOTIFY_EVENT_GAMEPAD_CONNECT:
  279. return "gamepad_connect";
  280. case NOTIFY_EVENT_LANHUB_CONNECT:
  281. return "lanhub_connect";
  282. case NOTIFY_EVENT_POWER_SOURCE:
  283. return "power_role_source";
  284. case NOTIFY_EVENT_PD_CONTRACT:
  285. return "pd_contract";
  286. case NOTIFY_EVENT_VBUS_RESET:
  287. return "host_accessory_restart";
  288. case NOTIFY_EVENT_RESERVE_BOOSTER:
  289. return "reserve_booster";
  290. case NOTIFY_EVENT_USB_CABLE:
  291. return "usb_cable";
  292. case NOTIFY_EVENT_USBD_SUSPENDED:
  293. return "usb_d_suspended";
  294. case NOTIFY_EVENT_USBD_UNCONFIGURED:
  295. return "usb_d_unconfigured";
  296. case NOTIFY_EVENT_USBD_CONFIGURED:
  297. return "usb_d_configured";
  298. case NOTIFY_EVENT_DR_SWAP:
  299. return "dr_swap";
  300. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_CONNECT:
  301. return "reverse_bypass_device_connect";
  302. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_ATTACH:
  303. return "reverse_bypass_device_attach";
  304. default:
  305. return "undefined";
  306. }
  307. }
  308. EXPORT_SYMBOL(event_string);
  309. const char *status_string(enum otg_notify_event_status status)
  310. {
  311. switch (status) {
  312. case NOTIFY_EVENT_DISABLED:
  313. return "disabled";
  314. case NOTIFY_EVENT_DISABLING:
  315. return "disabling";
  316. case NOTIFY_EVENT_ENABLED:
  317. return "enabled";
  318. case NOTIFY_EVENT_ENABLING:
  319. return "enabling";
  320. case NOTIFY_EVENT_BLOCKED:
  321. return "blocked";
  322. case NOTIFY_EVENT_BLOCKING:
  323. return "blocking";
  324. default:
  325. return "undefined";
  326. }
  327. }
  328. EXPORT_SYMBOL(status_string);
  329. static const char *block_string(enum otg_notify_block_type type)
  330. {
  331. switch (type) {
  332. case NOTIFY_BLOCK_TYPE_NONE:
  333. return "block_off";
  334. case NOTIFY_BLOCK_TYPE_HOST:
  335. return "block_host";
  336. case NOTIFY_BLOCK_TYPE_CLIENT:
  337. return "block_client";
  338. case NOTIFY_BLOCK_TYPE_ALL:
  339. return "block_all";
  340. default:
  341. return "undefined";
  342. }
  343. }
  344. static int create_usb_notify(void)
  345. {
  346. int ret = 0;
  347. if (u_notify_core)
  348. goto err;
  349. u_notify_core = kzalloc(sizeof(struct usb_notify_core), GFP_KERNEL);
  350. if (!u_notify_core) {
  351. ret = -ENOMEM;
  352. goto err;
  353. }
  354. register_usblog_proc();
  355. ret = notify_class_init();
  356. if (ret) {
  357. unl_err("unable to do host_notify_class_init\n");
  358. goto err1;
  359. }
  360. ret = usb_notify_class_init();
  361. if (ret) {
  362. unl_err("unable to do usb_notify_class_init\n");
  363. goto err2;
  364. }
  365. external_notifier_init();
  366. return 0;
  367. err2:
  368. notify_class_exit();
  369. err1:
  370. unregister_usblog_proc();
  371. kfree(u_notify_core);
  372. err:
  373. return ret;
  374. }
  375. static bool is_host_cable_block(struct otg_notify *n)
  376. {
  377. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  378. if ((check_event_type(u_notify->c_type)
  379. & NOTIFY_EVENT_NEED_HOST) &&
  380. (u_notify->c_status == NOTIFY_EVENT_BLOCKED
  381. || u_notify->c_status == NOTIFY_EVENT_BLOCKING))
  382. return true;
  383. else
  384. return false;
  385. }
  386. static bool is_host_cable_enable(struct otg_notify *n)
  387. {
  388. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  389. if ((check_event_type(u_notify->c_type)
  390. & NOTIFY_EVENT_NEED_HOST) &&
  391. (u_notify->c_status == NOTIFY_EVENT_ENABLED
  392. || u_notify->c_status == NOTIFY_EVENT_ENABLING))
  393. return true;
  394. else
  395. return false;
  396. }
  397. static bool is_client_cable_block(struct otg_notify *n)
  398. {
  399. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  400. if ((check_event_type(u_notify->c_type)
  401. & NOTIFY_EVENT_NEED_CLIENT) &&
  402. (u_notify->c_status == NOTIFY_EVENT_BLOCKED
  403. || u_notify->c_status == NOTIFY_EVENT_BLOCKING))
  404. return true;
  405. else
  406. return false;
  407. }
  408. static bool is_client_cable_enable(struct otg_notify *n)
  409. {
  410. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  411. if ((check_event_type(u_notify->c_type)
  412. & NOTIFY_EVENT_NEED_CLIENT) &&
  413. (u_notify->c_status == NOTIFY_EVENT_ENABLED
  414. || u_notify->c_status == NOTIFY_EVENT_ENABLING))
  415. return true;
  416. else
  417. return false;
  418. }
  419. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  420. static bool is_hub_connected(struct otg_notify *n)
  421. {
  422. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  423. return (u_notify->cond_hshub || u_notify->cond_sshub);
  424. }
  425. #endif
  426. static bool check_block_event(struct otg_notify *n, unsigned long event)
  427. {
  428. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  429. if ((test_bit(NOTIFY_BLOCK_TYPE_HOST, &u_notify->udev.disable_state)
  430. && (check_event_type(event) & NOTIFY_EVENT_NEED_HOST))
  431. || (test_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  432. &u_notify->udev.disable_state)
  433. && (check_event_type(event) & NOTIFY_EVENT_NEED_CLIENT)))
  434. return true;
  435. else
  436. return false;
  437. }
  438. static void notify_event_lock_init(struct usb_notify *u_noti)
  439. {
  440. spin_lock_init(&u_noti->event_spin_lock);
  441. }
  442. static void notify_event_lock(struct usb_notify *u_noti, int type)
  443. {
  444. if (type & NOTIFY_EVENT_STATE)
  445. spin_lock(&u_noti->event_spin_lock);
  446. }
  447. static void notify_event_unlock(struct usb_notify *u_noti, int type)
  448. {
  449. if (type & NOTIFY_EVENT_STATE)
  450. spin_unlock(&u_noti->event_spin_lock);
  451. }
  452. static void enable_ovc(struct usb_notify *u_noti, int enable)
  453. {
  454. u_noti->ovc_info.can_ovc = enable;
  455. }
  456. static int ovc_scan_thread(void *data)
  457. {
  458. struct ovc *ovcinfo = (struct ovc *)data;
  459. struct otg_notify *o_notify = ovcinfo->o_notify;
  460. struct usb_notify *u_notify = (struct usb_notify *)(o_notify->u_notify);
  461. int state = 0, event = 0;
  462. while (!kthread_should_stop()) {
  463. wait_event_interruptible_timeout(ovcinfo->delay_wait,
  464. ovcinfo->thread_remove, (ovcinfo->poll_period)*HZ);
  465. if (ovcinfo->thread_remove)
  466. break;
  467. mutex_lock(&ovcinfo->ovc_lock);
  468. if (ovcinfo->check_state
  469. && ovcinfo->data
  470. && ovcinfo->can_ovc) {
  471. state = ovcinfo->check_state(ovcinfo->data);
  472. if (ovcinfo->prev_state != state) {
  473. if (state == HNOTIFY_LOW) {
  474. unl_err("%s overcurrent detected\n",
  475. __func__);
  476. host_state_notify(&u_notify->ndev,
  477. NOTIFY_HOST_OVERCURRENT);
  478. event
  479. = NOTIFY_EXTRA_USBHOST_OVERCURRENT;
  480. store_usblog_notify(NOTIFY_EXTRA,
  481. (void *)&event, NULL);
  482. } else if (state == HNOTIFY_HIGH) {
  483. unl_info("%s vbus draw detected\n",
  484. __func__);
  485. host_state_notify(&u_notify->ndev,
  486. NOTIFY_HOST_NONE);
  487. }
  488. }
  489. ovcinfo->prev_state = state;
  490. }
  491. mutex_unlock(&ovcinfo->ovc_lock);
  492. if (!ovcinfo->can_ovc)
  493. ovcinfo->thread_remove = 1;
  494. }
  495. unl_info("%s exit\n", __func__);
  496. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 17, 0)
  497. complete_and_exit(&ovcinfo->scanning_done, 0);
  498. #else
  499. kthread_complete_and_exit(&ovcinfo->scanning_done, 0);
  500. #endif
  501. return 0;
  502. }
  503. void ovc_start(struct usb_notify *u_noti)
  504. {
  505. struct otg_notify *o_notify = u_noti->o_notify;
  506. if (!u_noti->ovc_info.can_ovc)
  507. goto skip;
  508. u_noti->ovc_info.prev_state = HNOTIFY_INITIAL;
  509. u_noti->ovc_info.poll_period = (o_notify->smsc_ovc_poll_sec) ?
  510. o_notify->smsc_ovc_poll_sec : DEFAULT_OVC_POLL_SEC;
  511. reinit_completion(&u_noti->ovc_info.scanning_done);
  512. u_noti->ovc_info.thread_remove = 0;
  513. u_noti->ovc_info.th = kthread_run(ovc_scan_thread,
  514. &u_noti->ovc_info, "ovc-scan-thread");
  515. if (IS_ERR(u_noti->ovc_info.th)) {
  516. unl_err("Unable to start the ovc-scanning thread\n");
  517. complete(&u_noti->ovc_info.scanning_done);
  518. }
  519. unl_info("%s on\n", __func__);
  520. return;
  521. skip:
  522. complete(&u_noti->ovc_info.scanning_done);
  523. unl_info("%s skip\n", __func__);
  524. }
  525. void ovc_stop(struct usb_notify *u_noti)
  526. {
  527. u_noti->ovc_info.thread_remove = 1;
  528. wake_up_interruptible(&u_noti->ovc_info.delay_wait);
  529. wait_for_completion(&u_noti->ovc_info.scanning_done);
  530. mutex_lock(&u_noti->ovc_info.ovc_lock);
  531. u_noti->ovc_info.check_state = NULL;
  532. u_noti->ovc_info.data = 0;
  533. mutex_unlock(&u_noti->ovc_info.ovc_lock);
  534. unl_info("%s\n", __func__);
  535. }
  536. static void ovc_init(struct usb_notify *u_noti)
  537. {
  538. init_waitqueue_head(&u_noti->ovc_info.delay_wait);
  539. init_completion(&u_noti->ovc_info.scanning_done);
  540. mutex_init(&u_noti->ovc_info.ovc_lock);
  541. u_noti->ovc_info.prev_state = HNOTIFY_INITIAL;
  542. u_noti->ovc_info.o_notify = u_noti->o_notify;
  543. unl_info("%s\n", __func__);
  544. }
  545. static irqreturn_t vbus_irq_isr(int irq, void *data)
  546. {
  547. struct otg_notify *notify = (struct otg_notify *)(data);
  548. struct usb_notify *u_notify = (struct usb_notify *)(notify->u_notify);
  549. unsigned long flags = 0;
  550. int gpio_value = 0;
  551. irqreturn_t ret = IRQ_NONE;
  552. spin_lock_irqsave(&u_notify->v_gpio.lock, flags);
  553. gpio_value = gpio_get_value(notify->vbus_detect_gpio);
  554. if (u_notify->v_gpio.gpio_status != gpio_value) {
  555. u_notify->v_gpio.gpio_status = gpio_value;
  556. ret = IRQ_WAKE_THREAD;
  557. } else
  558. ret = IRQ_HANDLED;
  559. spin_unlock_irqrestore(&u_notify->v_gpio.lock, flags);
  560. return ret;
  561. }
  562. static irqreturn_t vbus_irq_thread(int irq, void *data)
  563. {
  564. struct otg_notify *notify = (struct otg_notify *)(data);
  565. struct usb_notify *u_notify = (struct usb_notify *)(notify->u_notify);
  566. unsigned long flags = 0;
  567. int gpio_value = 0, event = 0;
  568. spin_lock_irqsave(&u_notify->v_gpio.lock, flags);
  569. gpio_value = u_notify->v_gpio.gpio_status;
  570. spin_unlock_irqrestore(&u_notify->v_gpio.lock, flags);
  571. if (gpio_value) {
  572. u_notify->ndev.booster = NOTIFY_POWER_ON;
  573. unl_info("vbus on detect\n");
  574. if (notify->post_vbus_detect)
  575. notify->post_vbus_detect(NOTIFY_POWER_ON);
  576. } else {
  577. if ((u_notify->ndev.mode == NOTIFY_HOST_MODE)
  578. && (u_notify->ndev.booster == NOTIFY_POWER_ON
  579. && u_notify->oc_noti)) {
  580. host_state_notify(&u_notify->ndev,
  581. NOTIFY_HOST_OVERCURRENT);
  582. event = NOTIFY_EXTRA_USBHOST_OVERCURRENT;
  583. store_usblog_notify(NOTIFY_EXTRA,
  584. (void *)&event, NULL);
  585. unl_err("OTG overcurrent!!!!!!\n");
  586. } else {
  587. unl_info("vbus off detect\n");
  588. if (notify->post_vbus_detect)
  589. notify->post_vbus_detect(NOTIFY_POWER_OFF);
  590. }
  591. u_notify->ndev.booster = NOTIFY_POWER_OFF;
  592. }
  593. return IRQ_HANDLED;
  594. }
  595. int register_gpios(struct otg_notify *n)
  596. {
  597. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  598. int ret = 0;
  599. int vbus_irq = 0;
  600. int vbus_gpio = -1;
  601. int redriver_gpio = -1;
  602. if (!gpio_is_valid(n->vbus_detect_gpio))
  603. goto redriver_en_gpio_phase;
  604. vbus_gpio = n->vbus_detect_gpio;
  605. spin_lock_init(&u_notify->v_gpio.lock);
  606. if (n->pre_gpio)
  607. n->pre_gpio(vbus_gpio, NOTIFY_VBUS);
  608. ret = gpio_request(vbus_gpio, "vbus_detect_notify");
  609. if (ret) {
  610. unl_err("failed to request %d\n", vbus_gpio);
  611. goto err;
  612. }
  613. gpio_direction_input(vbus_gpio);
  614. u_notify->v_gpio.gpio_status
  615. = gpio_get_value(vbus_gpio);
  616. vbus_irq = gpio_to_irq(vbus_gpio);
  617. ret = request_threaded_irq(vbus_irq,
  618. vbus_irq_isr,
  619. vbus_irq_thread,
  620. (IRQF_TRIGGER_FALLING |
  621. IRQF_TRIGGER_RISING |
  622. IRQF_ONESHOT),
  623. "vbus_irq_notify",
  624. n);
  625. if (ret) {
  626. unl_err("Failed to register IRQ\n");
  627. goto err;
  628. }
  629. if (n->post_gpio)
  630. n->post_gpio(vbus_gpio, NOTIFY_VBUS);
  631. unl_info("vbus detect gpio %d is registered.\n", vbus_gpio);
  632. redriver_en_gpio_phase:
  633. if (!gpio_is_valid(n->redriver_en_gpio))
  634. goto err;
  635. redriver_gpio = n->redriver_en_gpio;
  636. if (n->pre_gpio)
  637. n->pre_gpio(redriver_gpio, NOTIFY_REDRIVER);
  638. ret = gpio_request(redriver_gpio, "usb30_redriver_en");
  639. if (ret) {
  640. unl_err("failed to request %d\n", redriver_gpio);
  641. goto err;
  642. }
  643. gpio_direction_output(redriver_gpio, 0);
  644. if (n->post_gpio)
  645. n->post_gpio(redriver_gpio, NOTIFY_REDRIVER);
  646. unl_info("redriver en gpio %d is registered.\n", redriver_gpio);
  647. err:
  648. return ret;
  649. }
  650. int do_notify_blockstate(struct otg_notify *n, unsigned long event,
  651. int type, int enable)
  652. {
  653. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  654. int ret = 0;
  655. switch (event) {
  656. case NOTIFY_EVENT_NONE:
  657. case NOTIFY_EVENT_CHARGER:
  658. break;
  659. case NOTIFY_EVENT_SMARTDOCK_USB:
  660. case NOTIFY_EVENT_VBUS:
  661. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  662. if (enable && (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT))
  663. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  664. #endif
  665. if (enable)
  666. if (n->set_chg_current)
  667. n->set_chg_current(NOTIFY_USB_CONFIGURED);
  668. break;
  669. case NOTIFY_EVENT_LANHUB:
  670. case NOTIFY_EVENT_HMT:
  671. case NOTIFY_EVENT_HOST:
  672. case NOTIFY_EVENT_MMDOCK:
  673. case NOTIFY_EVENT_SMARTDOCK_TA:
  674. case NOTIFY_EVENT_AUDIODOCK:
  675. case NOTIFY_EVENT_GAMEPAD:
  676. case NOTIFY_EVENT_POGO:
  677. if (n->unsupport_host) {
  678. unl_err("This model doesn't support usb host\n");
  679. goto skip;
  680. }
  681. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  682. if (enable && (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT))
  683. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  684. #endif
  685. if (enable)
  686. host_state_notify(&u_notify->ndev, NOTIFY_HOST_BLOCK);
  687. else
  688. host_state_notify(&u_notify->ndev, NOTIFY_HOST_NONE);
  689. break;
  690. case NOTIFY_EVENT_DRIVE_VBUS:
  691. ret = -ESRCH;
  692. break;
  693. default:
  694. break;
  695. }
  696. skip:
  697. return ret;
  698. }
  699. static void update_cable_status(struct otg_notify *n, unsigned long event,
  700. int virtual, int enable, int start)
  701. {
  702. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  703. if (enable) {
  704. u_notify->c_type = event;
  705. if (check_block_event(n, event) ||
  706. (check_event_type(u_notify->c_type)
  707. & NOTIFY_EVENT_NEED_HOST &&
  708. (n->unsupport_host || u_notify->restricted)))
  709. u_notify->c_status = (start) ?
  710. NOTIFY_EVENT_BLOCKING : NOTIFY_EVENT_BLOCKED;
  711. else
  712. u_notify->c_status = (start) ?
  713. NOTIFY_EVENT_ENABLING : NOTIFY_EVENT_ENABLED;
  714. } else {
  715. if (virtual)
  716. u_notify->c_status = (start) ?
  717. NOTIFY_EVENT_BLOCKING : NOTIFY_EVENT_BLOCKED;
  718. else {
  719. u_notify->c_type = NOTIFY_EVENT_NONE;
  720. u_notify->c_status = (start) ?
  721. NOTIFY_EVENT_DISABLING : NOTIFY_EVENT_DISABLED;
  722. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  723. if (!start)
  724. send_usb_restrict_uevent(USB_SECURE_RELEASE);
  725. #endif
  726. }
  727. }
  728. }
  729. static void reserve_state_check(struct work_struct *work)
  730. {
  731. struct otg_booting_delay *o_b_d =
  732. container_of(to_delayed_work(work),
  733. struct otg_booting_delay, booting_work);
  734. struct usb_notify *u_noti = container_of(o_b_d,
  735. struct usb_notify, b_delay);
  736. int enable = 1, type = 0;
  737. unsigned long state = 0;
  738. unl_info("%s +\n", __func__);
  739. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  740. /* We can wait up to two minutes. */
  741. wait_event_interruptible(u_noti->init_delay,
  742. (u_noti->lock_state != USB_NOTIFY_INIT_STATE
  743. || u_noti->b_delay.reserve_state == NOTIFY_EVENT_VBUS));
  744. unl_info("%s after wait\n", __func__);
  745. #endif
  746. if (u_noti->o_notify->booting_delay_sync_usb) {
  747. unl_info("%s wait dwc3 probe done -\n", __func__);
  748. return;
  749. }
  750. notify_event_lock(u_noti, NOTIFY_EVENT_STATE);
  751. u_noti->o_notify->booting_delay_sec = 0;
  752. state = u_noti->b_delay.reserve_state;
  753. type = check_event_type(state);
  754. u_noti->b_delay.reserve_state = NOTIFY_EVENT_NONE;
  755. unl_info("%s booting delay finished\n", __func__);
  756. if (state != NOTIFY_EVENT_NONE) {
  757. unl_info("%s event=%s(%lu) enable=%d\n", __func__,
  758. event_string(state), state, enable);
  759. if (type & NOTIFY_EVENT_STATE)
  760. atomic_notifier_call_chain
  761. (&u_noti->otg_notifier,
  762. state, &enable);
  763. else
  764. ;
  765. }
  766. notify_event_unlock(u_noti, NOTIFY_EVENT_STATE);
  767. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  768. if (!u_noti->skip_possible_usb)
  769. send_external_notify(EXTERNAL_NOTIFY_POSSIBLE_USB, 1);
  770. #else
  771. send_external_notify(EXTERNAL_NOTIFY_POSSIBLE_USB, 1);
  772. #endif
  773. unl_info("%s -\n", __func__);
  774. }
  775. static void device_connect_check(struct work_struct *work)
  776. {
  777. struct usb_notify *u_notify = container_of(to_delayed_work(work),
  778. struct usb_notify, check_work);
  779. unl_info("%s start. is_device=%d\n", __func__, u_notify->is_device);
  780. if (!u_notify->is_device) {
  781. send_external_notify(EXTERNAL_NOTIFY_3S_NODEVICE, 1);
  782. if (u_notify->o_notify->vbus_drive)
  783. u_notify->o_notify->vbus_drive(0);
  784. u_notify->typec_status.power_role = HNOTIFY_SINK;
  785. }
  786. u_notify->check_work_complete = 1;
  787. unl_info("%s finished\n", __func__);
  788. }
  789. static int set_notify_disable(struct usb_notify_dev *udev, int disable)
  790. {
  791. struct otg_notify *n = udev->o_notify;
  792. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  793. unsigned long usb_notify;
  794. int usb_notify_state;
  795. if (!n->disable_control) {
  796. unl_err("%s disable_control is not supported\n", __func__);
  797. goto skip;
  798. }
  799. unl_info("%s prev=%s(%d) => disable=%s(%d)\n", __func__,
  800. block_string(u_notify->disable_state), u_notify->disable_state,
  801. block_string(disable), disable);
  802. if (u_notify->disable_state == disable) {
  803. unl_err("%s duplicated state\n", __func__);
  804. goto skip;
  805. }
  806. u_notify->disable_state = disable;
  807. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  808. switch (disable) {
  809. case NOTIFY_BLOCK_TYPE_ALL:
  810. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 1);
  811. if (is_host_cable_enable(n) ||
  812. is_client_cable_enable(n)) {
  813. unl_info("%s event=%s(%lu) disable\n", __func__,
  814. event_string(VIRT_EVENT(u_notify->c_type)),
  815. VIRT_EVENT(u_notify->c_type));
  816. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  817. if (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT)
  818. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  819. }
  820. send_otg_notify(n, NOTIFY_EVENT_ALL_DISABLE, 1);
  821. usb_notify = NOTIFY_EVENT_ALL_DISABLE;
  822. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  823. store_usblog_notify(NOTIFY_EVENT,
  824. (void *)&usb_notify, (void *)&usb_notify_state);
  825. if (n->booting_delay_sec)
  826. u_notify->skip_possible_usb = 1;
  827. wake_up_interruptible(&u_notify->init_delay);
  828. break;
  829. case NOTIFY_BLOCK_TYPE_HOST:
  830. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 1);
  831. if (is_host_cable_enable(n)) {
  832. unl_info("%s event=%s(%lu) disable\n", __func__,
  833. event_string(VIRT_EVENT(u_notify->c_type)),
  834. VIRT_EVENT(u_notify->c_type));
  835. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  836. }
  837. send_otg_notify(n, NOTIFY_EVENT_HOST_DISABLE, 1);
  838. usb_notify = NOTIFY_EVENT_HOST_DISABLE;
  839. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  840. store_usblog_notify(NOTIFY_EVENT,
  841. (void *)&usb_notify, (void *)&usb_notify_state);
  842. if (!is_client_cable_block(n))
  843. goto skip;
  844. unl_info("%s event=%s(%lu) enable\n", __func__,
  845. event_string(VIRT_EVENT(u_notify->c_type)),
  846. VIRT_EVENT(u_notify->c_type));
  847. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  848. break;
  849. case NOTIFY_BLOCK_TYPE_CLIENT:
  850. if (is_client_cable_enable(n)) {
  851. unl_info("%s event=%s(%lu) disable\n", __func__,
  852. event_string(VIRT_EVENT(u_notify->c_type)),
  853. VIRT_EVENT(u_notify->c_type));
  854. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  855. }
  856. send_otg_notify(n, NOTIFY_EVENT_CLIENT_DISABLE, 1);
  857. usb_notify = NOTIFY_EVENT_CLIENT_DISABLE;
  858. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  859. store_usblog_notify(NOTIFY_EVENT,
  860. (void *)&usb_notify, (void *)&usb_notify_state);
  861. if (!is_host_cable_block(n))
  862. goto skip;
  863. if (n->unsupport_host)
  864. goto skip;
  865. unl_info("%s event=%s(%lu) enable\n", __func__,
  866. event_string(VIRT_EVENT(u_notify->c_type)),
  867. VIRT_EVENT(u_notify->c_type));
  868. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  869. break;
  870. case NOTIFY_BLOCK_TYPE_NONE:
  871. if (u_notify->restricted)
  872. u_notify->restricted = 0;
  873. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 0);
  874. send_otg_notify(n, NOTIFY_EVENT_ALL_DISABLE, 0);
  875. usb_notify = NOTIFY_EVENT_ALL_DISABLE;
  876. usb_notify_state = NOTIFY_EVENT_DISABLED;
  877. store_usblog_notify(NOTIFY_EVENT,
  878. (void *)&usb_notify, (void *)&usb_notify_state);
  879. if (u_notify->skip_possible_usb) {
  880. send_external_notify(EXTERNAL_NOTIFY_POSSIBLE_USB, 1);
  881. u_notify->skip_possible_usb = 0;
  882. }
  883. if (!is_host_cable_block(n) && !is_client_cable_block(n)) {
  884. if (u_notify->typec_status.power_role
  885. == HNOTIFY_SOURCE)
  886. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  887. goto skip;
  888. }
  889. if (check_event_type(u_notify->c_type)
  890. & NOTIFY_EVENT_NEED_HOST && n->unsupport_host)
  891. goto skip;
  892. unl_info("%s event=%s(%lu) enable\n", __func__,
  893. event_string(VIRT_EVENT(u_notify->c_type)),
  894. VIRT_EVENT(u_notify->c_type));
  895. if (is_host_cable_block(n)) {
  896. if (!n->auto_drive_vbus &&
  897. (u_notify->typec_status.power_role
  898. == HNOTIFY_SOURCE) &&
  899. check_event_type(u_notify->c_type)
  900. & NOTIFY_EVENT_NEED_VBUSDRIVE) {
  901. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  902. }
  903. } else {
  904. if (u_notify->typec_status.power_role
  905. == HNOTIFY_SOURCE)
  906. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  907. }
  908. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  909. break;
  910. }
  911. #else /* CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION */
  912. switch (disable) {
  913. case NOTIFY_BLOCK_TYPE_ALL:
  914. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 1);
  915. if (is_host_cable_enable(n) ||
  916. is_client_cable_enable(n)) {
  917. unl_info("%s event=%s(%lu) disable\n", __func__,
  918. event_string(VIRT_EVENT(u_notify->c_type)),
  919. VIRT_EVENT(u_notify->c_type));
  920. if (is_host_cable_enable(n)) {
  921. if (!n->auto_drive_vbus &&
  922. (u_notify->typec_status.power_role
  923. == HNOTIFY_SOURCE) &&
  924. check_event_type(u_notify->c_type)
  925. & NOTIFY_EVENT_NEED_VBUSDRIVE)
  926. send_otg_notify(n,
  927. NOTIFY_EVENT_DRIVE_VBUS, 0);
  928. } else {
  929. if (u_notify->typec_status.power_role
  930. == HNOTIFY_SOURCE)
  931. send_otg_notify(n,
  932. NOTIFY_EVENT_DRIVE_VBUS, 0);
  933. }
  934. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  935. } else {
  936. if (u_notify->typec_status.power_role
  937. == HNOTIFY_SOURCE)
  938. send_otg_notify(n,
  939. NOTIFY_EVENT_DRIVE_VBUS, 0);
  940. }
  941. send_otg_notify(n, NOTIFY_EVENT_ALL_DISABLE, 1);
  942. usb_notify = NOTIFY_EVENT_ALL_DISABLE;
  943. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  944. store_usblog_notify(NOTIFY_EVENT,
  945. (void *)&usb_notify, (void *)&usb_notify_state);
  946. break;
  947. case NOTIFY_BLOCK_TYPE_HOST:
  948. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 1);
  949. if (is_host_cable_enable(n)) {
  950. unl_info("%s event=%s(%lu) disable\n", __func__,
  951. event_string(VIRT_EVENT(u_notify->c_type)),
  952. VIRT_EVENT(u_notify->c_type));
  953. if (!n->auto_drive_vbus &&
  954. (u_notify->typec_status.power_role
  955. == HNOTIFY_SOURCE) &&
  956. check_event_type(u_notify->c_type)
  957. & NOTIFY_EVENT_NEED_VBUSDRIVE)
  958. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 0);
  959. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  960. } else {
  961. if (u_notify->typec_status.power_role
  962. == HNOTIFY_SOURCE)
  963. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 0);
  964. }
  965. send_otg_notify(n, NOTIFY_EVENT_HOST_DISABLE, 1);
  966. usb_notify = NOTIFY_EVENT_HOST_DISABLE;
  967. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  968. store_usblog_notify(NOTIFY_EVENT,
  969. (void *)&usb_notify, (void *)&usb_notify_state);
  970. if (!is_client_cable_block(n))
  971. goto skip;
  972. unl_info("%s event=%s(%lu) enable\n", __func__,
  973. event_string(VIRT_EVENT(u_notify->c_type)),
  974. VIRT_EVENT(u_notify->c_type));
  975. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  976. break;
  977. case NOTIFY_BLOCK_TYPE_CLIENT:
  978. if (is_client_cable_enable(n)) {
  979. unl_info("%s event=%s(%lu) disable\n", __func__,
  980. event_string(VIRT_EVENT(u_notify->c_type)),
  981. VIRT_EVENT(u_notify->c_type));
  982. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 0);
  983. }
  984. send_otg_notify(n, NOTIFY_EVENT_CLIENT_DISABLE, 1);
  985. usb_notify = NOTIFY_EVENT_CLIENT_DISABLE;
  986. usb_notify_state = NOTIFY_EVENT_BLOCKED;
  987. store_usblog_notify(NOTIFY_EVENT,
  988. (void *)&usb_notify, (void *)&usb_notify_state);
  989. if (!is_host_cable_block(n))
  990. goto skip;
  991. if (n->unsupport_host)
  992. goto skip;
  993. unl_info("%s event=%s(%lu) enable\n", __func__,
  994. event_string(VIRT_EVENT(u_notify->c_type)),
  995. VIRT_EVENT(u_notify->c_type));
  996. if (!n->auto_drive_vbus &&
  997. (u_notify->typec_status.power_role
  998. == HNOTIFY_SOURCE) &&
  999. check_event_type(u_notify->c_type)
  1000. & NOTIFY_EVENT_NEED_VBUSDRIVE)
  1001. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  1002. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  1003. break;
  1004. case NOTIFY_BLOCK_TYPE_NONE:
  1005. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_EARLY, 0);
  1006. send_otg_notify(n, NOTIFY_EVENT_ALL_DISABLE, 0);
  1007. usb_notify = NOTIFY_EVENT_ALL_DISABLE;
  1008. usb_notify_state = NOTIFY_EVENT_DISABLED;
  1009. store_usblog_notify(NOTIFY_EVENT,
  1010. (void *)&usb_notify, (void *)&usb_notify_state);
  1011. if (!is_host_cable_block(n) && !is_client_cable_block(n)) {
  1012. if (u_notify->typec_status.power_role
  1013. == HNOTIFY_SOURCE)
  1014. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  1015. goto skip;
  1016. }
  1017. if (check_event_type(u_notify->c_type)
  1018. & NOTIFY_EVENT_NEED_HOST && n->unsupport_host)
  1019. goto skip;
  1020. unl_info("%s event=%s(%lu) enable\n", __func__,
  1021. event_string(VIRT_EVENT(u_notify->c_type)),
  1022. VIRT_EVENT(u_notify->c_type));
  1023. if (is_host_cable_block(n)) {
  1024. if (!n->auto_drive_vbus &&
  1025. (u_notify->typec_status.power_role
  1026. == HNOTIFY_SOURCE) &&
  1027. check_event_type(u_notify->c_type)
  1028. & NOTIFY_EVENT_NEED_VBUSDRIVE)
  1029. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  1030. } else {
  1031. if (u_notify->typec_status.power_role
  1032. == HNOTIFY_SOURCE)
  1033. send_otg_notify(n, NOTIFY_EVENT_DRIVE_VBUS, 1);
  1034. }
  1035. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  1036. break;
  1037. }
  1038. #endif
  1039. skip:
  1040. return 0;
  1041. }
  1042. static void set_notify_mdm(struct usb_notify_dev *udev, int disable)
  1043. {
  1044. struct otg_notify *n = udev->o_notify;
  1045. switch (disable) {
  1046. case NOTIFY_MDM_TYPE_ON:
  1047. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF, 1);
  1048. if (is_host_cable_enable(n)) {
  1049. unl_info("%s event=%s(%d)\n", __func__,
  1050. event_string(
  1051. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD)),
  1052. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD));
  1053. send_otg_notify(n,
  1054. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD), 1);
  1055. }
  1056. break;
  1057. case NOTIFY_MDM_TYPE_OFF:
  1058. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF, 0);
  1059. break;
  1060. }
  1061. }
  1062. void set_notify_mdm_for_id(struct usb_notify_dev *udev, int disable)
  1063. {
  1064. struct otg_notify *n = udev->o_notify;
  1065. switch (disable) {
  1066. case NOTIFY_MDM_TYPE_ON:
  1067. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF_FOR_ID, 1);
  1068. if (is_host_cable_enable(n)) {
  1069. unl_info("%s event=%s(%d)\n", __func__,
  1070. event_string(
  1071. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD)),
  1072. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD));
  1073. send_otg_notify(n,
  1074. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD), 1);
  1075. }
  1076. break;
  1077. case NOTIFY_MDM_TYPE_OFF:
  1078. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF_FOR_ID, 0);
  1079. break;
  1080. }
  1081. }
  1082. void set_notify_mdm_for_serial(struct usb_notify_dev *udev, int disable)
  1083. {
  1084. struct otg_notify *n = udev->o_notify;
  1085. switch (disable) {
  1086. case NOTIFY_MDM_TYPE_ON:
  1087. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL, 1);
  1088. if (is_host_cable_enable(n)) {
  1089. unl_info("%s event=%s(%d)\n", __func__,
  1090. event_string(
  1091. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD)),
  1092. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD));
  1093. send_otg_notify(n,
  1094. VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD), 1);
  1095. }
  1096. break;
  1097. case NOTIFY_MDM_TYPE_OFF:
  1098. send_otg_notify(n, NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL, 0);
  1099. break;
  1100. }
  1101. }
  1102. static int control_usb_maximum_speed(struct usb_notify_dev *udev, int speed)
  1103. {
  1104. struct otg_notify *n = udev->o_notify;
  1105. int ret = 0;
  1106. if (n->usb_maximum_speed)
  1107. ret = n->usb_maximum_speed(speed);
  1108. return ret;
  1109. }
  1110. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1111. static int set_notify_lock_state(struct usb_notify_dev *udev)
  1112. {
  1113. struct otg_notify *n = udev->o_notify;
  1114. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  1115. int reserve_state = u_notify->b_delay.reserve_state;
  1116. int i, noti = 0, recover = 0, reload = 0, vdm_start = 0;
  1117. unl_info("%s +\n", __func__);
  1118. notify_event_lock(u_notify, NOTIFY_EVENT_STATE);
  1119. u_notify->lock_state = udev->secure_lock;
  1120. unl_info("%s lock_state=%d\n", __func__, u_notify->lock_state);
  1121. switch (u_notify->lock_state) {
  1122. case USB_NOTIFY_LOCK_USB_RESTRICT:
  1123. unl_info("%s lock. reserve_state(%s)\n", __func__, event_string(reserve_state));
  1124. if (n->booting_delay_sec && reserve_state != NOTIFY_EVENT_NONE
  1125. && check_event_type(reserve_state) & NOTIFY_EVENT_STATE)
  1126. noti = 1;
  1127. break;
  1128. case USB_NOTIFY_LOCK_USB_WORK:
  1129. wake_up_interruptible(&u_notify->init_delay);
  1130. break;
  1131. case USB_NOTIFY_UNLOCK:
  1132. wake_up_interruptible(&u_notify->init_delay);
  1133. for (i = 0; i < USB_GROUP_MAX; i++)
  1134. u_notify->secure_connect_group[i] = 0;
  1135. unl_info("%s host block=%d,host enable=%d,restricted=%d,allowlist_restricted=%d\n",
  1136. __func__,
  1137. is_host_cable_block(n), is_host_cable_enable(n),
  1138. u_notify->restricted, u_notify->allowlist_restricted);
  1139. if (u_notify->restricted)
  1140. vdm_start = 1;
  1141. if (is_host_cable_block(n) && u_notify->restricted) {
  1142. u_notify->restricted = 0;
  1143. recover = 1;
  1144. } else
  1145. u_notify->restricted = 0;
  1146. unl_info("%s is_hub_connected=%d, pd contract=%d\n",
  1147. __func__,
  1148. is_hub_connected(n), get_typec_status(n, NOTIFY_EVENT_PD_CONTRACT));
  1149. if (is_host_cable_enable(n) && !is_hub_connected(n)
  1150. && !get_typec_status(n, NOTIFY_EVENT_PD_CONTRACT)
  1151. && u_notify->allowlist_restricted)
  1152. reload = 1;
  1153. break;
  1154. default:
  1155. break;
  1156. }
  1157. notify_event_unlock(u_notify, NOTIFY_EVENT_STATE);
  1158. if (noti)
  1159. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  1160. if (recover && is_host_cable_block(n))
  1161. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  1162. if (reload && is_host_cable_enable(n))
  1163. send_otg_notify(n, VIRT_EVENT(NOTIFY_EVENT_HOST_RELOAD), 1);
  1164. if (vdm_start) {
  1165. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_PRE, 0);
  1166. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_POST, 0);
  1167. }
  1168. unl_info("%s -\n", __func__);
  1169. return 0;
  1170. }
  1171. #else
  1172. static int set_notify_lock_state(struct usb_notify_dev *udev)
  1173. {
  1174. struct otg_notify *n = udev->o_notify;
  1175. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  1176. int i;
  1177. u_notify->lock_state = udev->secure_lock;
  1178. if (udev->secure_lock) {
  1179. unl_info("%s lock\n", __func__);
  1180. } else {
  1181. for (i = 0; i < USB_GROUP_MAX; i++)
  1182. u_notify->secure_connect_group[i] = 0;
  1183. unl_info("%s unlock host cable=%d, restricted=%d\n", __func__,
  1184. is_host_cable_block(n), u_notify->restricted);
  1185. if (is_host_cable_block(n) && u_notify->restricted) {
  1186. u_notify->restricted = 0;
  1187. send_otg_notify(n, VIRT_EVENT(u_notify->c_type), 1);
  1188. } else
  1189. u_notify->restricted = 0;
  1190. }
  1191. return 0;
  1192. }
  1193. #endif
  1194. void send_usb_mdm_uevent(void)
  1195. {
  1196. struct otg_notify *o_notify = get_otg_notify();
  1197. char *envp[4];
  1198. char *type = {"TYPE=usbmdm"};
  1199. char *state = {"STATE=ADD"};
  1200. char *words = {"WORDS=no_whitelist"};
  1201. int index = 0;
  1202. if (!o_notify) {
  1203. unl_err("%s o_notify is null\n", __func__);
  1204. goto err;
  1205. }
  1206. envp[index++] = type;
  1207. envp[index++] = state;
  1208. envp[index++] = words;
  1209. envp[index++] = NULL;
  1210. if (send_usb_notify_uevent(o_notify, envp)) {
  1211. unl_err("%s error\n", __func__);
  1212. goto err;
  1213. }
  1214. unl_info("%s\n", __func__);
  1215. err:
  1216. return;
  1217. }
  1218. EXPORT_SYMBOL(send_usb_mdm_uevent);
  1219. void send_usb_restrict_uevent(int usb_restrict)
  1220. {
  1221. struct otg_notify *o_notify = get_otg_notify();
  1222. char *envp[4];
  1223. char *type = {"TYPE=usbrestrict"};
  1224. char *state = {"STATE=ADD"};
  1225. char *words;
  1226. int index = 0;
  1227. if (!o_notify) {
  1228. unl_err("%s o_notify is null\n", __func__);
  1229. goto err;
  1230. }
  1231. envp[index++] = type;
  1232. envp[index++] = state;
  1233. switch (usb_restrict) {
  1234. case USB_SECURE_RESTRICTED:
  1235. words = "WORDS=securerestrict";
  1236. break;
  1237. case USB_TIME_SECURE_RESTRICTED:
  1238. words = "WORDS=timesecurerestrict";
  1239. break;
  1240. case USB_SECURE_RELEASE:
  1241. words = "WORDS=securerelease";
  1242. break;
  1243. default:
  1244. unl_err("%s invalid input\n", __func__);
  1245. goto err;
  1246. }
  1247. envp[index++] = words;
  1248. envp[index++] = NULL;
  1249. if (send_usb_notify_uevent(o_notify, envp)) {
  1250. unl_err("%s error\n", __func__);
  1251. goto err;
  1252. }
  1253. unl_info("%s: %s(%d)\n", __func__, words, usb_restrict);
  1254. err:
  1255. return;
  1256. }
  1257. EXPORT_SYMBOL(send_usb_restrict_uevent);
  1258. void send_usb_certi_uevent(int usb_certi)
  1259. {
  1260. struct otg_notify *o_notify = get_otg_notify();
  1261. char *envp[4];
  1262. char *type = {"TYPE=usbcerti"};
  1263. char *state = {"STATE=ADD"};
  1264. char *words;
  1265. int index = 0;
  1266. static DEFINE_RATELIMIT_STATE(rs_warm_reset, 5 * HZ, 1);
  1267. if (!o_notify) {
  1268. unl_err("%s o_notify is null\n", __func__);
  1269. goto err;
  1270. }
  1271. envp[index++] = type;
  1272. envp[index++] = state;
  1273. switch (usb_certi) {
  1274. case USB_CERTI_UNSUPPORT_ACCESSORY:
  1275. words = "WORDS=unsupport_accessory";
  1276. break;
  1277. case USB_CERTI_NO_RESPONSE:
  1278. words = "WORDS=no_response";
  1279. break;
  1280. case USB_CERTI_HUB_DEPTH_EXCEED:
  1281. words = "WORDS=hub_depth_exceed";
  1282. break;
  1283. case USB_CERTI_HUB_POWER_EXCEED:
  1284. words = "WORDS=hub_power_exceed";
  1285. break;
  1286. case USB_CERTI_HOST_RESOURCE_EXCEED:
  1287. words = "WORDS=host_resource_exceed";
  1288. break;
  1289. case USB_CERTI_WARM_RESET:
  1290. if (!__ratelimit(&rs_warm_reset))
  1291. goto err;
  1292. words = "WORDS=no_response";
  1293. break;
  1294. default:
  1295. unl_err("%s invalid input\n", __func__);
  1296. goto err;
  1297. }
  1298. envp[index++] = words;
  1299. envp[index++] = NULL;
  1300. if (send_usb_notify_uevent(o_notify, envp)) {
  1301. unl_err("%s error\n", __func__);
  1302. goto err;
  1303. }
  1304. unl_info("%s: %s(%d)\n", __func__, words, usb_certi);
  1305. err:
  1306. return;
  1307. }
  1308. EXPORT_SYMBOL(send_usb_certi_uevent);
  1309. void send_usb_err_uevent(int err_type, int mode)
  1310. {
  1311. struct otg_notify *o_notify = get_otg_notify();
  1312. char *envp[4];
  1313. char *type = {"TYPE=usberr"};
  1314. char *state;
  1315. char *words;
  1316. int index = 0;
  1317. if (!o_notify) {
  1318. unl_err("%s o_notify is null\n", __func__);
  1319. goto err;
  1320. }
  1321. if (mode)
  1322. state = "STATE=ADD";
  1323. else
  1324. state = "STATE=REMOVE";
  1325. envp[index++] = type;
  1326. envp[index++] = state;
  1327. switch (err_type) {
  1328. case USB_ERR_ABNORMAL_RESET:
  1329. words = "WORDS=abnormal_reset";
  1330. #if defined(CONFIG_USB_HW_PARAM)
  1331. if (mode)
  1332. inc_hw_param(o_notify,
  1333. USB_CLIENT_ANDROID_AUTO_RESET_POPUP_COUNT);
  1334. #endif
  1335. break;
  1336. default:
  1337. unl_err("%s invalid input\n", __func__);
  1338. goto err;
  1339. }
  1340. envp[index++] = words;
  1341. envp[index++] = NULL;
  1342. if (send_usb_notify_uevent(o_notify, envp)) {
  1343. unl_err("%s error\n", __func__);
  1344. goto err;
  1345. }
  1346. unl_info("%s: %s\n", __func__, words);
  1347. err:
  1348. return;
  1349. }
  1350. EXPORT_SYMBOL(send_usb_err_uevent);
  1351. void send_usb_itracker_uevent(int err_type)
  1352. {
  1353. struct otg_notify *o_notify = get_otg_notify();
  1354. char *envp[4];
  1355. char *type = {"TYPE=usbtracker"};
  1356. char *state = {"STATE=ADD"};
  1357. char *words;
  1358. int index = 0;
  1359. if (!o_notify) {
  1360. unl_err("%s o_notify is null\n", __func__);
  1361. goto err;
  1362. }
  1363. envp[index++] = type;
  1364. envp[index++] = state;
  1365. switch (err_type) {
  1366. case NOTIFY_USB_CC_REPEAT:
  1367. words = "WORDS=repeat_ccirq";
  1368. break;
  1369. default:
  1370. unl_err("%s invalid input\n", __func__);
  1371. goto err;
  1372. }
  1373. envp[index++] = words;
  1374. envp[index++] = NULL;
  1375. if (send_usb_notify_uevent(o_notify, envp)) {
  1376. unl_err("%s error\n", __func__);
  1377. goto err;
  1378. }
  1379. unl_info("%s: %s\n", __func__, words);
  1380. err:
  1381. return;
  1382. }
  1383. EXPORT_SYMBOL(send_usb_itracker_uevent);
  1384. int get_class_index(int ch9_class_num)
  1385. {
  1386. int internal_class_index;
  1387. switch (ch9_class_num) {
  1388. case USB_CLASS_PER_INTERFACE:
  1389. internal_class_index = U_CLASS_PER_INTERFACE;
  1390. break;
  1391. case USB_CLASS_AUDIO:
  1392. internal_class_index = U_CLASS_AUDIO;
  1393. break;
  1394. case USB_CLASS_COMM:
  1395. internal_class_index = U_CLASS_COMM;
  1396. break;
  1397. case USB_CLASS_HID:
  1398. internal_class_index = U_CLASS_HID;
  1399. break;
  1400. case USB_CLASS_PHYSICAL:
  1401. internal_class_index = U_CLASS_PHYSICAL;
  1402. break;
  1403. case USB_CLASS_STILL_IMAGE:
  1404. internal_class_index = U_CLASS_STILL_IMAGE;
  1405. break;
  1406. case USB_CLASS_PRINTER:
  1407. internal_class_index = U_CLASS_PRINTER;
  1408. break;
  1409. case USB_CLASS_MASS_STORAGE:
  1410. internal_class_index = U_CLASS_MASS_STORAGE;
  1411. break;
  1412. case USB_CLASS_HUB:
  1413. internal_class_index = U_CLASS_HUB;
  1414. break;
  1415. case USB_CLASS_CDC_DATA:
  1416. internal_class_index = U_CLASS_CDC_DATA;
  1417. break;
  1418. case USB_CLASS_CSCID:
  1419. internal_class_index = U_CLASS_CSCID;
  1420. break;
  1421. case USB_CLASS_CONTENT_SEC:
  1422. internal_class_index = U_CLASS_CONTENT_SEC;
  1423. break;
  1424. case USB_CLASS_VIDEO:
  1425. internal_class_index = U_CLASS_VIDEO;
  1426. break;
  1427. case USB_CLASS_WIRELESS_CONTROLLER:
  1428. internal_class_index = U_CLASS_WIRELESS_CONTROLLER;
  1429. break;
  1430. case USB_CLASS_MISC:
  1431. internal_class_index = U_CLASS_MISC;
  1432. break;
  1433. case USB_CLASS_APP_SPEC:
  1434. internal_class_index = U_CLASS_APP_SPEC;
  1435. break;
  1436. case USB_CLASS_VENDOR_SPEC:
  1437. internal_class_index = U_CLASS_VENDOR_SPEC;
  1438. break;
  1439. default:
  1440. internal_class_index = 0;
  1441. break;
  1442. }
  1443. return internal_class_index;
  1444. }
  1445. static bool usb_match_any_interface_for_mdm(struct usb_device *udev,
  1446. int *whitelist_array)
  1447. {
  1448. unsigned int i;
  1449. for (i = 0; i < udev->descriptor.bNumConfigurations; ++i) {
  1450. struct usb_host_config *cfg = &udev->config[i];
  1451. unsigned int j;
  1452. for (j = 0; j < cfg->desc.bNumInterfaces; ++j) {
  1453. struct usb_interface_cache *cache;
  1454. struct usb_host_interface *intf;
  1455. int intf_class;
  1456. cache = cfg->intf_cache[j];
  1457. if (cache->num_altsetting == 0)
  1458. continue;
  1459. intf = &cache->altsetting[0];
  1460. intf_class = intf->desc.bInterfaceClass;
  1461. if (!whitelist_array[get_class_index(intf_class)]) {
  1462. unl_info("%s : FAIL,%x interface, it's not in whitelist\n",
  1463. __func__, intf_class);
  1464. store_usblog_notify(NOTIFY_PORT_CLASS_BLOCK,
  1465. (void *)&udev->descriptor.bDeviceClass,
  1466. (void *)&intf_class);
  1467. return false;
  1468. }
  1469. unl_info("%s : SUCCESS,%x interface, it's in whitelist\n",
  1470. __func__, intf_class);
  1471. }
  1472. }
  1473. return true;
  1474. }
  1475. int usb_check_whitelist_for_mdm(struct usb_device *dev)
  1476. {
  1477. int *whitelist_array;
  1478. struct otg_notify *o_notify;
  1479. struct usb_notify *u_notify;
  1480. /* return 1 if the enumeration will be going . */
  1481. /* return 0 if the enumeration will be skept . */
  1482. o_notify = get_otg_notify();
  1483. if (o_notify == NULL) {
  1484. unl_err("o_notify is NULL\n");
  1485. return 1;
  1486. }
  1487. u_notify = (struct usb_notify *)(o_notify->u_notify);
  1488. if (u_notify == NULL) {
  1489. unl_err("u_notify is NULL\n");
  1490. return 1;
  1491. }
  1492. if (u_notify->sec_whitelist_enable) {
  1493. whitelist_array = u_notify->udev.whitelist_array_for_mdm;
  1494. if (usb_match_any_interface_for_mdm(dev, whitelist_array)) {
  1495. dev_info(&dev->dev, "the device is matched with allowlist!\n");
  1496. return 1;
  1497. }
  1498. return 0;
  1499. }
  1500. return 1;
  1501. }
  1502. EXPORT_SYMBOL(usb_check_whitelist_for_mdm);
  1503. static bool usb_match_any_interface_for_id(struct usb_device *udev,
  1504. int *whitelist_array)
  1505. {
  1506. int i = 0;
  1507. unl_info("%s : New USB device found, idVendor=%04x, idProduct=%04x\n", __func__,
  1508. le16_to_cpu(udev->descriptor.idVendor),
  1509. le16_to_cpu(udev->descriptor.idProduct));
  1510. while (whitelist_array[i] && (i < MAX_WHITELIST_STR_LEN)) {
  1511. unl_info("%s : whitelist_array[%d]=%04x, whitelist_array[%d]=%04x\n",
  1512. __func__, i, whitelist_array[i], i+1, whitelist_array[i+1]);
  1513. if (whitelist_array[i] == le16_to_cpu(udev->descriptor.idVendor)
  1514. && whitelist_array[i+1] == le16_to_cpu(udev->descriptor.idProduct)) {
  1515. unl_info("%s : SUCCESS, it's in whitelist\n", __func__);
  1516. return true;
  1517. }
  1518. i += 2;
  1519. }
  1520. unl_info("%s : FAIL, it's not in whitelist\n", __func__);
  1521. return false;
  1522. }
  1523. int usb_check_whitelist_for_id(struct usb_device *dev)
  1524. {
  1525. int *whitelist_array;
  1526. struct otg_notify *o_notify;
  1527. struct usb_notify *u_notify;
  1528. /* return 1 if the enumeration will be going . */
  1529. /* return 0 if the enumeration will be skept . */
  1530. o_notify = get_otg_notify();
  1531. if (o_notify == NULL) {
  1532. unl_err("o_notify is NULL\n");
  1533. return 1;
  1534. }
  1535. u_notify = (struct usb_notify *)(o_notify->u_notify);
  1536. if (u_notify == NULL) {
  1537. unl_err("u_notify is NULL\n");
  1538. return 1;
  1539. }
  1540. if (u_notify->sec_whitelist_enable_for_id) {
  1541. whitelist_array = u_notify->udev.whitelist_array_for_mdm_for_id;
  1542. if (usb_match_any_interface_for_id(dev, whitelist_array)) {
  1543. dev_info(&dev->dev, "the device is matched with whitelist!\n");
  1544. return 1;
  1545. }
  1546. return 0;
  1547. }
  1548. return 1;
  1549. }
  1550. EXPORT_SYMBOL(usb_check_whitelist_for_id);
  1551. static bool usb_match_any_interface_for_serial(struct usb_device *udev,
  1552. char *whitelist_array)
  1553. {
  1554. char *ptr_serial = NULL;
  1555. unl_info("%s : New USB device found, SerialNumber: %s, whitelist_array: %s\n",
  1556. __func__, udev->serial, whitelist_array);
  1557. if (!udev->serial) {
  1558. unl_info("%s : FAIL, serial is null\n", __func__);
  1559. return false;
  1560. }
  1561. while ((ptr_serial = strsep(&whitelist_array, ":")) != NULL) {
  1562. if (!strcmp(ptr_serial, udev->serial)) {
  1563. unl_info("%s : SUCCESS, it's in whitelist\n", __func__);
  1564. return true;
  1565. }
  1566. }
  1567. unl_info("%s : FAIL, it's not in whitelist\n", __func__);
  1568. return false;
  1569. }
  1570. int usb_check_whitelist_for_serial(struct usb_device *dev)
  1571. {
  1572. char whitelist_array[MAX_WHITELIST_STR_LEN];
  1573. struct otg_notify *o_notify;
  1574. struct usb_notify *u_notify;
  1575. /* return 1 if the enumeration will be going . */
  1576. /* return 0 if the enumeration will be skept . */
  1577. o_notify = get_otg_notify();
  1578. if (o_notify == NULL) {
  1579. unl_err("o_notify is NULL\n");
  1580. return 1;
  1581. }
  1582. u_notify = (struct usb_notify *)(o_notify->u_notify);
  1583. if (u_notify == NULL) {
  1584. unl_err("u_notify is NULL\n");
  1585. return 1;
  1586. }
  1587. if (u_notify->sec_whitelist_enable_for_serial) {
  1588. strncpy(whitelist_array,
  1589. u_notify->udev.whitelist_array_for_mdm_for_serial, sizeof(whitelist_array)-1);
  1590. if (usb_match_any_interface_for_serial(dev, whitelist_array)) {
  1591. dev_info(&dev->dev, "the device is matched with whitelist!\n");
  1592. return 1;
  1593. }
  1594. return 0;
  1595. }
  1596. return 1;
  1597. }
  1598. EXPORT_SYMBOL(usb_check_whitelist_for_serial);
  1599. int usb_check_whitelist_enable_state(void)
  1600. {
  1601. struct otg_notify *o_notify;
  1602. struct usb_notify *u_notify;
  1603. o_notify = get_otg_notify();
  1604. if (o_notify == NULL) {
  1605. unl_err("o_notify is NULL\n");
  1606. return 0;
  1607. }
  1608. u_notify = (struct usb_notify *)(o_notify->u_notify);
  1609. if (u_notify == NULL) {
  1610. unl_err("u_notify is NULL\n");
  1611. return 0;
  1612. }
  1613. if (u_notify->sec_whitelist_enable_for_id &&
  1614. u_notify->sec_whitelist_enable_for_serial)
  1615. return NOTIFY_MDM_ID_AND_SERIAL;
  1616. else if (u_notify->sec_whitelist_enable_for_id)
  1617. return NOTIFY_MDM_ID;
  1618. else if (u_notify->sec_whitelist_enable_for_serial)
  1619. return NOTIFY_MDM_SERIAL;
  1620. else
  1621. return NOTIFY_MDM_NONE;
  1622. }
  1623. EXPORT_SYMBOL(usb_check_whitelist_enable_state);
  1624. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1625. int usb_check_allowlist_for_lockscreen_enabled_id(struct usb_device *dev)
  1626. {
  1627. int *allowlist_array;
  1628. struct otg_notify *o_notify;
  1629. struct usb_notify *u_notify;
  1630. int ret = USB_NOTIFY_NORESTRICT, noti = 0;
  1631. o_notify = get_otg_notify();
  1632. if (o_notify == NULL) {
  1633. unl_err("o_notify is NULL\n");
  1634. return 0;
  1635. }
  1636. u_notify = (struct usb_notify *)(o_notify->u_notify);
  1637. if (u_notify == NULL) {
  1638. unl_err("u_notify is NULL\n");
  1639. return 0;
  1640. }
  1641. mutex_lock(&u_notify->udev.lockscreen_enabled_lock);
  1642. notify_event_lock(u_notify, NOTIFY_EVENT_STATE);
  1643. if (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT) {
  1644. allowlist_array = u_notify->udev.allowlist_array_lockscreen_enabled_id;
  1645. unl_info("%s allowlist : %s\n", __func__,
  1646. u_notify->udev.allowlist_str_lockscreen_enabled_id);
  1647. if (usb_match_any_interface_for_id(dev, allowlist_array)) {
  1648. unl_info("the device is matched with allowlist for lockscreen!\n");
  1649. ret = USB_NOTIFY_ALLOWLOST;
  1650. goto done;
  1651. } else {
  1652. unl_info("the device is unmatched with allowlist for lockscreen!\n");
  1653. noti = 1;
  1654. if (u_notify->allowlist_restricted < MAX_VAL)
  1655. u_notify->allowlist_restricted++;
  1656. ret = USB_NOTIFY_NOLIST;
  1657. }
  1658. }
  1659. done:
  1660. notify_event_unlock(u_notify, NOTIFY_EVENT_STATE);
  1661. mutex_unlock(&u_notify->udev.lockscreen_enabled_lock);
  1662. if (noti)
  1663. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL(usb_check_allowlist_for_lockscreen_enabled_id);
  1667. bool check_usb_restrict_lock_state(struct otg_notify *n)
  1668. {
  1669. struct usb_notify *u_notify = NULL;
  1670. if (!n) {
  1671. unl_err("%s otg_notify is null\n", __func__);
  1672. return true;
  1673. }
  1674. u_notify = (struct usb_notify *)(n->u_notify);
  1675. if (!u_notify) {
  1676. unl_err("%s u_notify structure is null\n", __func__);
  1677. return true;
  1678. }
  1679. return (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT);
  1680. }
  1681. EXPORT_SYMBOL(check_usb_restrict_lock_state);
  1682. #endif
  1683. int usb_otg_restart_accessory(struct usb_device *dev)
  1684. {
  1685. struct otg_notify *o_notify;
  1686. int res = 0;
  1687. unl_info("%s\n", __func__);
  1688. o_notify = get_otg_notify();
  1689. if (o_notify == NULL) {
  1690. unl_err("o_notify is NULL\n");
  1691. return -1;
  1692. }
  1693. send_otg_notify(o_notify, NOTIFY_EVENT_VBUS_RESET, 0);
  1694. return res;
  1695. }
  1696. EXPORT_SYMBOL(usb_otg_restart_accessory);
  1697. static void otg_notify_state(struct otg_notify *n,
  1698. unsigned long event, int enable)
  1699. {
  1700. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  1701. int type = 0;
  1702. int virtual = 0;
  1703. int status = 0;
  1704. unsigned long prev_c_type = 0;
  1705. unl_info("%s+ event=%s(%lu), enable=%s\n", __func__,
  1706. event_string(event), event, enable == 0 ? "off" : "on");
  1707. prev_c_type = u_notify->c_type;
  1708. virtual = IS_VIRTUAL(event);
  1709. event = PHY_EVENT(event);
  1710. type = check_event_type(event);
  1711. if (virtual && enable) {
  1712. if (check_event_type(event) & NOTIFY_EVENT_NEED_HOST) {
  1713. if (!(check_event_type(u_notify->c_type)
  1714. & NOTIFY_EVENT_NEED_HOST)) {
  1715. unl_err("event skip. mismatch cable type(%s)\n",
  1716. event_string(u_notify->c_type));
  1717. goto no_save_event;
  1718. }
  1719. }
  1720. }
  1721. if (!(type & NOTIFY_EVENT_NOSAVE)) {
  1722. update_cable_status(n, event, virtual, enable, 1);
  1723. store_usblog_notify(NOTIFY_EVENT,
  1724. (void *)&event, (void *)&u_notify->c_status);
  1725. } else {
  1726. if (enable)
  1727. status = NOTIFY_EVENT_ENABLING;
  1728. else
  1729. status = NOTIFY_EVENT_DISABLING;
  1730. store_usblog_notify(NOTIFY_EVENT,
  1731. (void *)&event, (void *)&status);
  1732. }
  1733. if (check_block_event(n, event) &&
  1734. !(type & NOTIFY_EVENT_NOBLOCKING)) {
  1735. unl_err("%s usb notify is blocked. cause %s\n", __func__,
  1736. u_notify->udev.disable_state_cmd);
  1737. if (do_notify_blockstate(n, event, type, enable))
  1738. goto no_save_event;
  1739. else
  1740. goto err;
  1741. }
  1742. switch (event) {
  1743. case NOTIFY_EVENT_NONE:
  1744. break;
  1745. case NOTIFY_EVENT_SMARTDOCK_USB:
  1746. case NOTIFY_EVENT_VBUS:
  1747. if (enable) {
  1748. mutex_lock(&u_notify->state_lock);
  1749. u_notify->ndev.mode = NOTIFY_PERIPHERAL_MODE;
  1750. u_notify->typec_status.doing_drswap = 0;
  1751. mutex_unlock(&u_notify->state_lock);
  1752. if (n->is_wakelock)
  1753. __pm_stay_awake(&u_notify->ws);
  1754. if (gpio_is_valid(n->redriver_en_gpio))
  1755. gpio_direction_output
  1756. (n->redriver_en_gpio, 1);
  1757. if (n->pre_peri_delay_us)
  1758. usleep_range(n->pre_peri_delay_us * 1000,
  1759. n->pre_peri_delay_us * 1000);
  1760. if (n->set_peripheral)
  1761. n->set_peripheral(true);
  1762. } else {
  1763. mutex_lock(&u_notify->state_lock);
  1764. u_notify->ndev.mode = NOTIFY_NONE_MODE;
  1765. u_notify->gadget_status.bus_state
  1766. = NOTIFY_USB_UNCONFIGURED;
  1767. mutex_unlock(&u_notify->state_lock);
  1768. if (n->set_peripheral)
  1769. n->set_peripheral(false);
  1770. if (gpio_is_valid(n->redriver_en_gpio))
  1771. gpio_direction_output
  1772. (n->redriver_en_gpio, 0);
  1773. if (n->is_wakelock)
  1774. __pm_relax(&u_notify->ws);
  1775. }
  1776. break;
  1777. case NOTIFY_EVENT_LANHUB_TA:
  1778. u_notify->disable_v_drive = enable;
  1779. if (enable)
  1780. u_notify->oc_noti = 0;
  1781. if (n->set_lanhubta)
  1782. n->set_lanhubta(enable);
  1783. break;
  1784. case NOTIFY_EVENT_LANHUB:
  1785. if (n->unsupport_host) {
  1786. unl_err("This model doesn't support usb host\n");
  1787. goto err;
  1788. }
  1789. u_notify->disable_v_drive = enable;
  1790. if (enable) {
  1791. u_notify->oc_noti = 0;
  1792. u_notify->ndev.mode = NOTIFY_HOST_MODE;
  1793. host_state_notify(&u_notify->ndev, NOTIFY_HOST_ADD);
  1794. if (gpio_is_valid(n->redriver_en_gpio))
  1795. gpio_direction_output
  1796. (n->redriver_en_gpio, 1);
  1797. if (n->set_host)
  1798. n->set_host(true);
  1799. } else {
  1800. u_notify->ndev.mode = NOTIFY_NONE_MODE;
  1801. if (n->set_host)
  1802. n->set_host(false);
  1803. if (gpio_is_valid(n->redriver_en_gpio))
  1804. gpio_direction_output
  1805. (n->redriver_en_gpio, 0);
  1806. host_state_notify(&u_notify->ndev, NOTIFY_HOST_REMOVE);
  1807. }
  1808. break;
  1809. case NOTIFY_EVENT_HMT:
  1810. case NOTIFY_EVENT_HOST:
  1811. case NOTIFY_EVENT_GAMEPAD:
  1812. if (n->unsupport_host) {
  1813. unl_err("This model doesn't support usb host\n");
  1814. goto err;
  1815. }
  1816. u_notify->disable_v_drive = 0;
  1817. if (enable) {
  1818. if (check_same_event_type(prev_c_type, event)
  1819. && !virtual) {
  1820. unl_err("now host mode, skip this command\n");
  1821. goto err;
  1822. }
  1823. if (u_notify->restricted) {
  1824. send_usb_restrict_uevent(USB_SECURE_RESTRICTED);
  1825. unl_err("now restricted, skip this command\n");
  1826. goto err;
  1827. }
  1828. mutex_lock(&u_notify->state_lock);
  1829. u_notify->ndev.mode = NOTIFY_HOST_MODE;
  1830. u_notify->typec_status.doing_drswap = 0;
  1831. mutex_unlock(&u_notify->state_lock);
  1832. host_state_notify(&u_notify->ndev, NOTIFY_HOST_ADD);
  1833. if (gpio_is_valid(n->redriver_en_gpio))
  1834. gpio_direction_output
  1835. (n->redriver_en_gpio, 1);
  1836. if (n->auto_drive_vbus == NOTIFY_OP_PRE) {
  1837. u_notify->oc_noti = 1;
  1838. if (n->vbus_drive)
  1839. n->vbus_drive(1);
  1840. u_notify->typec_status.power_role
  1841. = HNOTIFY_SOURCE;
  1842. }
  1843. if (n->set_host)
  1844. n->set_host(true);
  1845. if (n->auto_drive_vbus == NOTIFY_OP_POST) {
  1846. u_notify->oc_noti = 1;
  1847. if (n->vbus_drive)
  1848. n->vbus_drive(1);
  1849. u_notify->typec_status.power_role
  1850. = HNOTIFY_SOURCE;
  1851. }
  1852. if (n->auto_drive_vbus == NOTIFY_OP_OFF) {
  1853. mutex_lock(&u_notify->state_lock);
  1854. if ((u_notify->typec_status.power_role
  1855. == HNOTIFY_SOURCE)
  1856. && u_notify->reserve_vbus_booster
  1857. && !is_blocked(n,
  1858. NOTIFY_BLOCK_TYPE_HOST)) {
  1859. unl_info("reserved vbus turn on\n");
  1860. if (n->vbus_drive)
  1861. n->vbus_drive(1);
  1862. u_notify->reserve_vbus_booster = 0;
  1863. }
  1864. mutex_unlock(&u_notify->state_lock);
  1865. }
  1866. } else { /* disable */
  1867. u_notify->ndev.mode = NOTIFY_NONE_MODE;
  1868. if (n->auto_drive_vbus == NOTIFY_OP_POST) {
  1869. u_notify->oc_noti = 0;
  1870. if (n->vbus_drive)
  1871. n->vbus_drive(0);
  1872. u_notify->typec_status.power_role
  1873. = HNOTIFY_SINK;
  1874. }
  1875. if (n->set_host)
  1876. n->set_host(false);
  1877. if (n->auto_drive_vbus == NOTIFY_OP_PRE) {
  1878. u_notify->oc_noti = 0;
  1879. if (n->vbus_drive)
  1880. n->vbus_drive(0);
  1881. u_notify->typec_status.power_role
  1882. = HNOTIFY_SINK;
  1883. }
  1884. if (gpio_is_valid(n->redriver_en_gpio))
  1885. gpio_direction_output
  1886. (n->redriver_en_gpio, 0);
  1887. host_state_notify(&u_notify->ndev, NOTIFY_HOST_REMOVE);
  1888. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  1889. u_notify->allowlist_restricted = 0;
  1890. #endif
  1891. }
  1892. break;
  1893. case NOTIFY_EVENT_CHARGER:
  1894. if (n->set_charger)
  1895. n->set_charger(enable);
  1896. break;
  1897. case NOTIFY_EVENT_MMDOCK:
  1898. enable_ovc(u_notify, enable);
  1899. /* To detect overcurrent, ndev state is initialized */
  1900. if (enable)
  1901. host_state_notify(&u_notify->ndev,
  1902. NOTIFY_HOST_NONE);
  1903. fallthrough;
  1904. case NOTIFY_EVENT_POGO:
  1905. case NOTIFY_EVENT_SMARTDOCK_TA:
  1906. case NOTIFY_EVENT_AUDIODOCK:
  1907. if (n->unsupport_host) {
  1908. unl_err("This model doesn't support usb host\n");
  1909. goto err;
  1910. }
  1911. u_notify->disable_v_drive = enable;
  1912. if (enable) {
  1913. u_notify->ndev.mode = NOTIFY_HOST_MODE;
  1914. if (n->set_host)
  1915. n->set_host(true);
  1916. } else {
  1917. u_notify->ndev.mode = NOTIFY_NONE_MODE;
  1918. if (n->set_host)
  1919. n->set_host(false);
  1920. }
  1921. break;
  1922. case NOTIFY_EVENT_HOST_RELOAD:
  1923. if (u_notify->ndev.mode != NOTIFY_HOST_MODE) {
  1924. unl_err("mode is not host. skip host reload.\n");
  1925. goto no_save_event;
  1926. }
  1927. if (n->unsupport_host) {
  1928. unl_err("This model doesn't support usb host\n");
  1929. goto no_save_event;
  1930. }
  1931. if (n->set_host) {
  1932. n->set_host(false);
  1933. msleep(100);
  1934. n->set_host(true);
  1935. }
  1936. goto no_save_event;
  1937. case NOTIFY_EVENT_DRIVE_VBUS:
  1938. if (n->unsupport_host) {
  1939. unl_err("This model doesn't support usb host\n");
  1940. goto no_save_event;
  1941. }
  1942. if (u_notify->disable_v_drive) {
  1943. unl_info("cable type=%s disable vbus draw\n",
  1944. event_string(u_notify->c_type));
  1945. goto no_save_event;
  1946. }
  1947. u_notify->oc_noti = enable;
  1948. if (n->vbus_drive)
  1949. n->vbus_drive((bool)enable);
  1950. mutex_lock(&u_notify->state_lock);
  1951. if (n->reverse_bypass_drive && !enable) {
  1952. n->reverse_bypass_drive(0);
  1953. u_notify->reverse_bypass_status = NOTIFY_EVENT_REVERSE_BYPASS_PREPARE;
  1954. }
  1955. mutex_unlock(&u_notify->state_lock);
  1956. goto no_save_event;
  1957. case NOTIFY_EVENT_ALL_DISABLE:
  1958. if (!n->disable_control) {
  1959. unl_err("This model doesn't support disable_control\n");
  1960. goto no_save_event;
  1961. }
  1962. if (enable) {
  1963. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_PRE, 1);
  1964. set_bit(NOTIFY_BLOCK_TYPE_HOST,
  1965. &u_notify->udev.disable_state);
  1966. set_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  1967. &u_notify->udev.disable_state);
  1968. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_POST, 1);
  1969. } else {
  1970. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_PRE, 0);
  1971. clear_bit(NOTIFY_BLOCK_TYPE_HOST,
  1972. &u_notify->udev.disable_state);
  1973. clear_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  1974. &u_notify->udev.disable_state);
  1975. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_POST, 0);
  1976. }
  1977. goto no_save_event;
  1978. case NOTIFY_EVENT_HOST_DISABLE:
  1979. if (!n->disable_control) {
  1980. unl_err("This model doesn't support disable_control\n");
  1981. goto no_save_event;
  1982. }
  1983. if (enable) {
  1984. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_PRE, 1);
  1985. clear_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  1986. &u_notify->udev.disable_state);
  1987. set_bit(NOTIFY_BLOCK_TYPE_HOST,
  1988. &u_notify->udev.disable_state);
  1989. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_POST, 1);
  1990. }
  1991. goto no_save_event;
  1992. case NOTIFY_EVENT_CLIENT_DISABLE:
  1993. if (!n->disable_control) {
  1994. unl_err("This model doesn't support disable_control\n");
  1995. goto no_save_event;
  1996. }
  1997. if (enable) {
  1998. clear_bit(NOTIFY_BLOCK_TYPE_HOST,
  1999. &u_notify->udev.disable_state);
  2000. set_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  2001. &u_notify->udev.disable_state);
  2002. }
  2003. goto no_save_event;
  2004. case NOTIFY_EVENT_MDM_ON_OFF:
  2005. unl_info("%s : mdm block enable for usb whiltelist = %d\n",
  2006. __func__, enable);
  2007. if (enable) {
  2008. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_PRE, 1);
  2009. /*whilte list start*/
  2010. u_notify->sec_whitelist_enable = 1;
  2011. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_POST, 1);
  2012. } else {
  2013. /*whilte list end*/
  2014. u_notify->sec_whitelist_enable = 0;
  2015. }
  2016. goto no_save_event;
  2017. case NOTIFY_EVENT_MDM_ON_OFF_FOR_ID:
  2018. unl_info("%s : mdm block enable for usb whiltelist = %d\n",
  2019. __func__, enable);
  2020. if (enable) {
  2021. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_PRE, 1);
  2022. /*whilte list start*/
  2023. u_notify->sec_whitelist_enable_for_id = 1;
  2024. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_POST, 1);
  2025. } else {
  2026. /*whilte list end*/
  2027. u_notify->sec_whitelist_enable_for_id = 0;
  2028. }
  2029. goto no_save_event;
  2030. case NOTIFY_EVENT_MDM_ON_OFF_FOR_SERIAL:
  2031. unl_info("%s : mdm block enable for usb whiltelist = %d\n",
  2032. __func__, enable);
  2033. if (enable) {
  2034. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_PRE, 1);
  2035. /*whilte list start*/
  2036. u_notify->sec_whitelist_enable_for_serial = 1;
  2037. send_external_notify(EXTERNAL_NOTIFY_MDMBLOCK_POST, 1);
  2038. } else {
  2039. /*whilte list end*/
  2040. u_notify->sec_whitelist_enable_for_serial = 0;
  2041. }
  2042. goto no_save_event;
  2043. default:
  2044. break;
  2045. }
  2046. if (((type & NOTIFY_EVENT_NEED_VBUSDRIVE)
  2047. && event != NOTIFY_EVENT_HOST)
  2048. || event == NOTIFY_EVENT_POGO) {
  2049. if (enable) {
  2050. if (n->device_check_sec) {
  2051. if (prev_c_type != NOTIFY_EVENT_HOST)
  2052. u_notify->is_device = 0;
  2053. u_notify->check_work_complete = 0;
  2054. schedule_delayed_work(&u_notify->check_work,
  2055. n->device_check_sec*HZ);
  2056. unl_info("%s check work start\n", __func__);
  2057. }
  2058. } else {
  2059. if (n->device_check_sec &&
  2060. !u_notify->check_work_complete) {
  2061. unl_info("%s check work cancel\n", __func__);
  2062. cancel_delayed_work_sync(&u_notify->check_work);
  2063. }
  2064. u_notify->is_device = 0;
  2065. }
  2066. }
  2067. if (type & NOTIFY_EVENT_NEED_HOST) {
  2068. if (!enable) {
  2069. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  2070. u_notify->allowlist_restricted = 0;
  2071. u_notify->cond_hshub = 0;
  2072. u_notify->cond_sshub = 0;
  2073. #endif
  2074. u_notify->is_device = 0;
  2075. unl_info("%s end host\n", __func__);
  2076. send_external_notify(EXTERNAL_NOTIFY_DEVICEADD, 0);
  2077. }
  2078. }
  2079. err:
  2080. update_cable_status(n, event, virtual, enable, 0);
  2081. no_save_event:
  2082. unl_info("%s- event=%s, cable=%s\n", __func__,
  2083. event_string(event),
  2084. event_string(u_notify->c_type));
  2085. }
  2086. static void extra_notify_state(struct otg_notify *n,
  2087. unsigned long event, int enable)
  2088. {
  2089. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2090. int status = 0;
  2091. unl_info("%s+ event=%s(%lu), enable=%s\n", __func__,
  2092. event_string(event), event, enable == 0 ? "off" : "on");
  2093. switch (event) {
  2094. case NOTIFY_EVENT_NONE:
  2095. break;
  2096. case NOTIFY_EVENT_OVERCURRENT:
  2097. if (!u_notify->ndev.dev) {
  2098. unl_err("ndev is NULL. Maybe usb host is not supported.\n");
  2099. break;
  2100. }
  2101. host_state_notify(&u_notify->ndev,
  2102. NOTIFY_HOST_OVERCURRENT);
  2103. unl_err("OTG overcurrent!!!!!!\n");
  2104. status = NOTIFY_EXTRA_USBHOST_OVERCURRENT;
  2105. store_usblog_notify(NOTIFY_EXTRA,
  2106. (void *)&status, NULL);
  2107. break;
  2108. case NOTIFY_EVENT_VBUSPOWER:
  2109. if (enable) {
  2110. u_notify->ndev.booster = NOTIFY_POWER_ON;
  2111. status = NOTIFY_EVENT_ENABLED;
  2112. } else {
  2113. u_notify->ndev.booster = NOTIFY_POWER_OFF;
  2114. status = NOTIFY_EVENT_DISABLED;
  2115. }
  2116. store_usblog_notify(NOTIFY_EVENT,
  2117. (void *)&event, (void *)&status);
  2118. break;
  2119. case NOTIFY_EVENT_SMSC_OVC:
  2120. if (enable)
  2121. ovc_start(u_notify);
  2122. else
  2123. ovc_stop(u_notify);
  2124. break;
  2125. case NOTIFY_EVENT_SMTD_EXT_CURRENT:
  2126. if (u_notify->c_type != NOTIFY_EVENT_SMARTDOCK_TA) {
  2127. unl_err("No smart dock!!!!!!\n");
  2128. break;
  2129. }
  2130. if (n->set_battcall)
  2131. n->set_battcall
  2132. (NOTIFY_EVENT_SMTD_EXT_CURRENT, enable);
  2133. break;
  2134. case NOTIFY_EVENT_MMD_EXT_CURRENT:
  2135. if (u_notify->c_type != NOTIFY_EVENT_MMDOCK) {
  2136. unl_err("No mmdock!!!!!!\n");
  2137. break;
  2138. }
  2139. if (n->set_battcall)
  2140. n->set_battcall
  2141. (NOTIFY_EVENT_MMD_EXT_CURRENT, enable);
  2142. break;
  2143. case NOTIFY_EVENT_HMD_EXT_CURRENT:
  2144. if (n->set_battcall)
  2145. n->set_battcall
  2146. (NOTIFY_EVENT_HMD_EXT_CURRENT, enable);
  2147. break;
  2148. case NOTIFY_EVENT_DEVICE_CONNECT:
  2149. if (enable) {
  2150. if (!u_notify->is_device) {
  2151. u_notify->is_device = 1;
  2152. send_external_notify(EXTERNAL_NOTIFY_DEVICEADD, 1);
  2153. }
  2154. }
  2155. if ((u_notify->lock_state == USB_NOTIFY_LOCK_USB_WORK)
  2156. || u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT) {
  2157. if (!enable)
  2158. detect_illegal_condition(NOTIFY_EVENT_SECURE_DISCONNECTION);
  2159. }
  2160. break;
  2161. case NOTIFY_EVENT_GAMEPAD_CONNECT:
  2162. if (u_notify->c_type == NOTIFY_EVENT_HOST ||
  2163. u_notify->c_type == NOTIFY_EVENT_GAMEPAD)
  2164. send_external_notify(EXTERNAL_NOTIFY_DEVICE_CONNECT,
  2165. EXTERNAL_NOTIFY_GPAD);
  2166. break;
  2167. case NOTIFY_EVENT_LANHUB_CONNECT:
  2168. if (u_notify->c_type == NOTIFY_EVENT_HOST ||
  2169. u_notify->c_type == NOTIFY_EVENT_LANHUB)
  2170. send_external_notify(EXTERNAL_NOTIFY_DEVICE_CONNECT,
  2171. EXTERNAL_NOTIFY_LANHUB);
  2172. break;
  2173. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_CONNECT:
  2174. mutex_lock(&u_notify->state_lock);
  2175. if (n->reverse_bypass_drive &&
  2176. u_notify->reverse_bypass_status == NOTIFY_EVENT_REVERSE_BYPASS_PREPARE) {
  2177. u_notify->reverse_bypass_status = NOTIFY_EVENT_REVERSE_BYPASS_ON;
  2178. n->reverse_bypass_drive(1);
  2179. }
  2180. mutex_unlock(&u_notify->state_lock);
  2181. break;
  2182. case NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_ATTACH:
  2183. mutex_lock(&u_notify->state_lock);
  2184. if (enable) {
  2185. u_notify->reverse_bypass_status = NOTIFY_EVENT_REVERSE_BYPASS_PREPARE;
  2186. } else {
  2187. u_notify->reverse_bypass_status = NOTIFY_EVENT_REVERSE_BYPASS_OFF;
  2188. if (n->reverse_bypass_drive)
  2189. n->reverse_bypass_drive(0);
  2190. }
  2191. mutex_unlock(&u_notify->state_lock);
  2192. break;
  2193. case NOTIFY_EVENT_POWER_SOURCE:
  2194. if (enable) {
  2195. u_notify->typec_status.power_role = HNOTIFY_SOURCE;
  2196. host_state_notify(&u_notify->ndev,
  2197. NOTIFY_HOST_SOURCE);
  2198. } else {
  2199. u_notify->typec_status.power_role = HNOTIFY_SINK;
  2200. host_state_notify(&u_notify->ndev,
  2201. NOTIFY_HOST_SINK);
  2202. }
  2203. send_external_notify(EXTERNAL_NOTIFY_POWERROLE,
  2204. u_notify->typec_status.power_role);
  2205. break;
  2206. case NOTIFY_EVENT_PD_CONTRACT:
  2207. if (enable)
  2208. u_notify->typec_status.pd = enable;
  2209. else
  2210. u_notify->typec_status.pd = 0;
  2211. break;
  2212. case NOTIFY_EVENT_VBUS_RESET:
  2213. send_external_notify(EXTERNAL_NOTIFY_VBUS_RESET, 0);
  2214. break;
  2215. case NOTIFY_EVENT_RESERVE_BOOSTER:
  2216. mutex_lock(&u_notify->state_lock);
  2217. if (enable)
  2218. u_notify->reserve_vbus_booster = 1;
  2219. else
  2220. u_notify->reserve_vbus_booster = 0;
  2221. mutex_unlock(&u_notify->state_lock);
  2222. break;
  2223. case NOTIFY_EVENT_USB_CABLE:
  2224. mutex_lock(&u_notify->state_lock);
  2225. if (enable)
  2226. u_notify->gadget_status.usb_cable_connect = 1;
  2227. else
  2228. u_notify->gadget_status.usb_cable_connect = 0;
  2229. if (u_notify->ndev.mode == NOTIFY_PERIPHERAL_MODE
  2230. && !u_notify->typec_status.doing_drswap) {
  2231. if ((u_notify->gadget_status.bus_state
  2232. == NOTIFY_USB_SUSPENDED)
  2233. && u_notify->gadget_status.usb_cable_connect) {
  2234. if (n->set_chg_current)
  2235. n->set_chg_current
  2236. (NOTIFY_USB_SUSPENDED);
  2237. }
  2238. }
  2239. mutex_unlock(&u_notify->state_lock);
  2240. break;
  2241. case NOTIFY_EVENT_USBD_SUSPENDED:
  2242. mutex_lock(&u_notify->state_lock);
  2243. if (u_notify->ndev.mode == NOTIFY_PERIPHERAL_MODE
  2244. && !u_notify->typec_status.doing_drswap) {
  2245. u_notify->gadget_status.bus_state
  2246. = NOTIFY_USB_SUSPENDED;
  2247. if (u_notify->gadget_status.usb_cable_connect
  2248. && u_notify->typec_status.power_role != HNOTIFY_SOURCE) {
  2249. if (n->set_chg_current)
  2250. n->set_chg_current
  2251. (NOTIFY_USB_SUSPENDED);
  2252. }
  2253. }
  2254. mutex_unlock(&u_notify->state_lock);
  2255. break;
  2256. case NOTIFY_EVENT_USBD_UNCONFIGURED:
  2257. mutex_lock(&u_notify->state_lock);
  2258. if (u_notify->ndev.mode == NOTIFY_PERIPHERAL_MODE)
  2259. u_notify->gadget_status.bus_state
  2260. = NOTIFY_USB_UNCONFIGURED;
  2261. mutex_unlock(&u_notify->state_lock);
  2262. break;
  2263. case NOTIFY_EVENT_USBD_CONFIGURED:
  2264. mutex_lock(&u_notify->state_lock);
  2265. if (u_notify->ndev.mode == NOTIFY_PERIPHERAL_MODE)
  2266. u_notify->gadget_status.bus_state
  2267. = NOTIFY_USB_CONFIGURED;
  2268. mutex_unlock(&u_notify->state_lock);
  2269. break;
  2270. case NOTIFY_EVENT_DR_SWAP:
  2271. mutex_lock(&u_notify->state_lock);
  2272. if (enable)
  2273. u_notify->typec_status.doing_drswap = 1;
  2274. else
  2275. u_notify->typec_status.doing_drswap = 0;
  2276. mutex_unlock(&u_notify->state_lock);
  2277. break;
  2278. default:
  2279. break;
  2280. }
  2281. unl_info("%s- event=%s(%lu), cable=%s\n", __func__,
  2282. event_string(event), event,
  2283. event_string(u_notify->c_type));
  2284. }
  2285. static void otg_notify_work(struct work_struct *data)
  2286. {
  2287. struct otg_state_work *state_work =
  2288. container_of(data, struct otg_state_work, otg_work);
  2289. otg_notify_state(state_work->o_notify,
  2290. state_work->event, state_work->enable);
  2291. kfree(state_work);
  2292. }
  2293. static int otg_notifier_callback(struct notifier_block *nb,
  2294. unsigned long event, void *param)
  2295. {
  2296. struct usb_notify *u_noti = container_of(nb,
  2297. struct usb_notify, otg_nb);
  2298. struct otg_notify *n = NULL;
  2299. struct otg_state_work *state_work = NULL;
  2300. n = u_noti->o_notify;
  2301. unl_info("%s event=%s(%lu)\n", __func__,
  2302. event_string(event), event);
  2303. if (event > VIRT_EVENT(NOTIFY_EVENT_VBUSPOWER)) {
  2304. unl_err("%s event is invalid\n", __func__);
  2305. return NOTIFY_DONE;
  2306. }
  2307. state_work = kmalloc(sizeof(struct otg_state_work), GFP_ATOMIC);
  2308. if (!state_work)
  2309. return notifier_from_errno(-ENOMEM);
  2310. INIT_WORK(&state_work->otg_work, otg_notify_work);
  2311. state_work->o_notify = n;
  2312. state_work->event = event;
  2313. state_work->enable = *(int *)param;
  2314. queue_work(u_noti->notifier_wq, &state_work->otg_work);
  2315. return NOTIFY_OK;
  2316. }
  2317. static int extra_notifier_callback(struct notifier_block *nb,
  2318. unsigned long event, void *param)
  2319. {
  2320. struct usb_notify *u_noti = container_of(nb,
  2321. struct usb_notify, extra_nb);
  2322. struct otg_notify *n = NULL;
  2323. n = u_noti->o_notify;
  2324. unl_info("%s event=%s(%lu)\n", __func__,
  2325. event_string(event), event);
  2326. if (event > VIRT_EVENT(NOTIFY_EVENT_VBUSPOWER)) {
  2327. unl_err("%s event is invalid\n", __func__);
  2328. return NOTIFY_DONE;
  2329. }
  2330. extra_notify_state(n, event, *(int *)param);
  2331. return NOTIFY_OK;
  2332. }
  2333. void send_otg_notify(struct otg_notify *n,
  2334. unsigned long event, int enable)
  2335. {
  2336. struct usb_notify *u_notify = NULL;
  2337. int type = 0;
  2338. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  2339. int noti = 0;
  2340. #endif
  2341. if (!n) {
  2342. unl_err("%s otg_notify is null\n", __func__);
  2343. goto end;
  2344. }
  2345. u_notify = (struct usb_notify *)(n->u_notify);
  2346. if (!u_notify) {
  2347. unl_err("%s u_notify structure is null\n", __func__);
  2348. goto end;
  2349. }
  2350. unl_info("%s event=%s(%lu) enable=%d\n", __func__,
  2351. event_string(event), event, enable);
  2352. type = check_event_type(event);
  2353. notify_event_lock(u_notify, type);
  2354. if ((type & NOTIFY_EVENT_DELAY) && (type & NOTIFY_EVENT_STATE)) {
  2355. if (n->booting_delay_sec) {
  2356. if (u_notify) {
  2357. u_notify->b_delay.reserve_state =
  2358. (enable) ? event : NOTIFY_EVENT_NONE;
  2359. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  2360. if (enable && (check_event_type(event) & NOTIFY_EVENT_NEED_CLIENT))
  2361. wake_up_interruptible(&u_notify->init_delay);
  2362. if (u_notify->lock_state == USB_NOTIFY_LOCK_USB_RESTRICT)
  2363. noti = 1;
  2364. #endif
  2365. unl_info("%s reserve event\n", __func__);
  2366. } else
  2367. unl_err("%s u_notify is null\n", __func__);
  2368. goto before_unlock;
  2369. }
  2370. }
  2371. if (type & NOTIFY_EVENT_EXTRA)
  2372. blocking_notifier_call_chain
  2373. (&u_notify->extra_notifier, event, &enable);
  2374. else if (type & NOTIFY_EVENT_STATE)
  2375. atomic_notifier_call_chain
  2376. (&u_notify->otg_notifier, event, &enable);
  2377. else
  2378. goto before_unlock;
  2379. before_unlock:
  2380. notify_event_unlock(u_notify, type);
  2381. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  2382. if (noti) {
  2383. if (enable)
  2384. send_usb_restrict_uevent(USB_TIME_SECURE_RESTRICTED);
  2385. else
  2386. send_usb_restrict_uevent(USB_SECURE_RELEASE);
  2387. }
  2388. #endif
  2389. end:
  2390. return;
  2391. }
  2392. EXPORT_SYMBOL(send_otg_notify);
  2393. int get_typec_status(struct otg_notify *n, int event)
  2394. {
  2395. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2396. int ret = -ENODEV;
  2397. if (u_notify == NULL) {
  2398. unl_err("u_notify is NULL\n");
  2399. goto end;
  2400. }
  2401. if (event == NOTIFY_EVENT_POWER_SOURCE) {
  2402. /* SINK == 0, SOURCE == 1 */
  2403. ret = u_notify->typec_status.power_role;
  2404. } else
  2405. ret = u_notify->typec_status.pd;
  2406. end:
  2407. return ret;
  2408. }
  2409. EXPORT_SYMBOL(get_typec_status);
  2410. struct otg_booster *find_get_booster(struct otg_notify *n)
  2411. {
  2412. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2413. int ret = 0;
  2414. if (!u_notify) {
  2415. unl_err("%s u_notify structure is null\n", __func__);
  2416. goto err;
  2417. }
  2418. if (!u_notify_core) {
  2419. ret = create_usb_notify();
  2420. if (ret) {
  2421. unl_err("unable create_usb_notify\n");
  2422. goto err;
  2423. }
  2424. }
  2425. if (!u_notify->booster) {
  2426. unl_err("error. No matching booster\n");
  2427. goto err;
  2428. }
  2429. return u_notify->booster;
  2430. err:
  2431. return NULL;
  2432. }
  2433. EXPORT_SYMBOL(find_get_booster);
  2434. int register_booster(struct otg_notify *n, struct otg_booster *b)
  2435. {
  2436. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2437. int ret = 0;
  2438. if (!u_notify) {
  2439. unl_err("%s u_notify structure is null\n", __func__);
  2440. goto err;
  2441. }
  2442. u_notify->booster = b;
  2443. err:
  2444. return ret;
  2445. }
  2446. EXPORT_SYMBOL(register_booster);
  2447. int register_ovc_func(struct otg_notify *n,
  2448. int (*check_state)(void *), void *data)
  2449. {
  2450. struct usb_notify *u_notify;
  2451. int ret = 0;
  2452. if (!n) {
  2453. unl_err("%s otg_notify is null\n", __func__);
  2454. return -ENODEV;
  2455. }
  2456. u_notify = (struct usb_notify *)(n->u_notify);
  2457. if (!u_notify) {
  2458. unl_err("%s u_notify structure is null\n", __func__);
  2459. return -EFAULT;
  2460. }
  2461. mutex_lock(&u_notify->ovc_info.ovc_lock);
  2462. u_notify->ovc_info.check_state = check_state;
  2463. u_notify->ovc_info.data = data;
  2464. mutex_unlock(&u_notify->ovc_info.ovc_lock);
  2465. unl_info("%s\n", __func__);
  2466. return ret;
  2467. }
  2468. EXPORT_SYMBOL(register_ovc_func);
  2469. int get_booster(struct otg_notify *n)
  2470. {
  2471. struct usb_notify *u_notify;
  2472. int ret = 0;
  2473. if (!n) {
  2474. unl_err("%s otg_notify is null\n", __func__);
  2475. return -ENODEV;
  2476. }
  2477. u_notify = (struct usb_notify *)(n->u_notify);
  2478. if (!u_notify) {
  2479. unl_err("%s u_notify structure is null\n", __func__);
  2480. return NOTIFY_NONE_MODE;
  2481. }
  2482. if (!u_notify_core) {
  2483. ret = create_usb_notify();
  2484. if (ret) {
  2485. unl_err("unable create_usb_notify\n");
  2486. return -EFAULT;
  2487. }
  2488. }
  2489. unl_info("%s usb booster=%d\n", __func__, u_notify->ndev.booster);
  2490. ret = u_notify->ndev.booster;
  2491. return ret;
  2492. }
  2493. EXPORT_SYMBOL(get_booster);
  2494. int get_usb_mode(struct otg_notify *n)
  2495. {
  2496. struct usb_notify *u_notify;
  2497. int ret = 0;
  2498. if (!n) {
  2499. unl_err("%s otg_notify is null\n", __func__);
  2500. return -ENODEV;
  2501. }
  2502. u_notify = (struct usb_notify *)(n->u_notify);
  2503. if (!u_notify) {
  2504. unl_err("%s u_notify structure is null\n", __func__);
  2505. return NOTIFY_NONE_MODE;
  2506. }
  2507. if (!u_notify_core) {
  2508. ret = create_usb_notify();
  2509. if (ret) {
  2510. unl_err("unable create_usb_notify\n");
  2511. return -EFAULT;
  2512. }
  2513. }
  2514. unl_info("%s usb mode=%d\n", __func__, u_notify->ndev.mode);
  2515. ret = u_notify->ndev.mode;
  2516. return ret;
  2517. }
  2518. EXPORT_SYMBOL(get_usb_mode);
  2519. unsigned long get_cable_type(struct otg_notify *n)
  2520. {
  2521. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2522. unsigned long ret = 0;
  2523. int noti_ret = 0;
  2524. if (!u_notify) {
  2525. unl_err("%s u_notify structure is null\n", __func__);
  2526. return NOTIFY_EVENT_NONE;
  2527. }
  2528. if (!u_notify_core) {
  2529. noti_ret = create_usb_notify();
  2530. if (noti_ret) {
  2531. unl_err("unable create_usb_notify\n");
  2532. return NOTIFY_EVENT_NONE;
  2533. }
  2534. }
  2535. unl_info("%s cable type =%s\n", __func__,
  2536. event_string(u_notify->c_type));
  2537. ret = u_notify->c_type;
  2538. return ret;
  2539. }
  2540. EXPORT_SYMBOL(get_cable_type);
  2541. int is_usb_host(struct otg_notify *n)
  2542. {
  2543. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  2544. int ret = 0;
  2545. int noti_ret = 0;
  2546. if (!u_notify) {
  2547. unl_err("%s u_notify structure is null\n", __func__);
  2548. return NOTIFY_EVENT_NONE;
  2549. }
  2550. if (!u_notify_core) {
  2551. noti_ret = create_usb_notify();
  2552. if (noti_ret) {
  2553. unl_err("unable create_usb_notify\n");
  2554. return NOTIFY_EVENT_NONE;
  2555. }
  2556. }
  2557. if (n->unsupport_host || !IS_ENABLED(CONFIG_USB_HOST_NOTIFY))
  2558. ret = 0;
  2559. else
  2560. ret = 1;
  2561. unl_info("%s %d\n", __func__, ret);
  2562. return ret;
  2563. }
  2564. EXPORT_SYMBOL(is_usb_host);
  2565. bool is_blocked(struct otg_notify *n, int type)
  2566. {
  2567. struct usb_notify *u_notify = NULL;
  2568. int ret = 0;
  2569. if (!n) {
  2570. unl_err("%s otg_notify is null\n", __func__);
  2571. goto end;
  2572. }
  2573. u_notify = (struct usb_notify *)(n->u_notify);
  2574. if (!u_notify) {
  2575. unl_err("%s u_notify structure is null\n", __func__);
  2576. goto end;
  2577. }
  2578. if (!u_notify_core) {
  2579. ret = create_usb_notify();
  2580. if (ret) {
  2581. unl_err("unable create_usb_notify\n");
  2582. goto end;
  2583. }
  2584. }
  2585. unl_info("%s type=%d, disable_state=%lu\n",
  2586. __func__, type, u_notify->udev.disable_state);
  2587. if (type == NOTIFY_BLOCK_TYPE_HOST) {
  2588. if (test_bit(NOTIFY_BLOCK_TYPE_HOST,
  2589. &u_notify->udev.disable_state))
  2590. goto end2;
  2591. } else if (type == NOTIFY_BLOCK_TYPE_CLIENT) {
  2592. if (test_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  2593. &u_notify->udev.disable_state))
  2594. goto end2;
  2595. } else if (type == NOTIFY_BLOCK_TYPE_ALL) {
  2596. if (test_bit(NOTIFY_BLOCK_TYPE_HOST,
  2597. &u_notify->udev.disable_state) &&
  2598. test_bit(NOTIFY_BLOCK_TYPE_CLIENT,
  2599. &u_notify->udev.disable_state))
  2600. goto end2;
  2601. }
  2602. end:
  2603. return false;
  2604. end2:
  2605. return true;
  2606. }
  2607. EXPORT_SYMBOL(is_blocked);
  2608. bool is_snkdfp_usb_device_connected(struct otg_notify *n)
  2609. {
  2610. struct usb_notify *u_notify = NULL;
  2611. if (!n) {
  2612. unl_err("%s otg_notify is null\n", __func__);
  2613. return false;
  2614. }
  2615. u_notify = (struct usb_notify *)(n->u_notify);
  2616. if (!u_notify) {
  2617. unl_err("%s u_notify structure is null\n", __func__);
  2618. return false;
  2619. }
  2620. unl_info("%s is_device = %d, power_role = %d\n",
  2621. __func__, u_notify->is_device,
  2622. u_notify->typec_status.power_role);
  2623. if (u_notify->is_device &&
  2624. u_notify->typec_status.power_role == HNOTIFY_SINK) {
  2625. return true;
  2626. }
  2627. return false;
  2628. }
  2629. EXPORT_SYMBOL(is_snkdfp_usb_device_connected);
  2630. int get_con_dev_max_speed(struct otg_notify *n)
  2631. {
  2632. struct usb_notify *u_notify = NULL;
  2633. if (!n) {
  2634. unl_err("%s otg_notify is null\n", __func__);
  2635. return false;
  2636. }
  2637. u_notify = (struct usb_notify *)(n->u_notify);
  2638. if (!u_notify) {
  2639. unl_err("%s u_notify structure is null\n", __func__);
  2640. return false;
  2641. }
  2642. unl_info("%s device max speed=%s\n", __func__,
  2643. usb_speed_string(u_notify->cond_max_speed));
  2644. return u_notify->cond_max_speed;
  2645. }
  2646. EXPORT_SYMBOL(get_con_dev_max_speed);
  2647. void set_con_dev_max_speed(struct otg_notify *n, int speed)
  2648. {
  2649. struct usb_notify *u_notify = NULL;
  2650. if (!n) {
  2651. unl_err("%s otg_notify is null\n", __func__);
  2652. return;
  2653. }
  2654. u_notify = (struct usb_notify *)(n->u_notify);
  2655. if (!u_notify) {
  2656. unl_err("%s u_notify structure is null\n", __func__);
  2657. return;
  2658. }
  2659. u_notify->cond_max_speed = speed;
  2660. unl_info("%s device max speed=%s\n", __func__,
  2661. usb_speed_string(speed));
  2662. }
  2663. EXPORT_SYMBOL(set_con_dev_max_speed);
  2664. void set_con_dev_hub(struct otg_notify *n, int speed, int conn)
  2665. {
  2666. struct usb_notify *u_notify = NULL;
  2667. if (!n) {
  2668. unl_err("%s otg_notify is null\n", __func__);
  2669. return;
  2670. }
  2671. u_notify = (struct usb_notify *)(n->u_notify);
  2672. if (!u_notify) {
  2673. unl_err("%s u_notify structure is null\n", __func__);
  2674. return;
  2675. }
  2676. if (speed >= USB_SPEED_SUPER) {
  2677. u_notify->cond_sshub = !!conn;
  2678. } else if (speed > USB_SPEED_UNKNOWN
  2679. && speed != USB_SPEED_WIRELESS) {
  2680. u_notify->cond_hshub = !!conn;
  2681. } else ;
  2682. unl_info("%s speed(%s), conn(%d)\n", __func__,
  2683. usb_speed_string(speed), conn);
  2684. }
  2685. EXPORT_SYMBOL(set_con_dev_hub);
  2686. void set_request_action(struct otg_notify *n, unsigned int request_action)
  2687. {
  2688. struct usb_notify *u_notify = NULL;
  2689. if (!n) {
  2690. unl_err("%s o_notify is null\n", __func__);
  2691. goto err;
  2692. }
  2693. u_notify = (struct usb_notify *)(n->u_notify);
  2694. if (!u_notify) {
  2695. unl_err("%s u_notify structure is null\n",
  2696. __func__);
  2697. goto err;
  2698. }
  2699. unl_info("%s prev action = %u set action as=%u\n",
  2700. __func__, u_notify->udev.request_action, request_action);
  2701. u_notify->udev.request_action = request_action;
  2702. err:
  2703. return;
  2704. }
  2705. EXPORT_SYMBOL(set_request_action);
  2706. struct dev_table {
  2707. struct usb_device_id dev;
  2708. int index;
  2709. };
  2710. static struct dev_table known_usbaudio_device_table[] = {
  2711. { .dev = { USB_DEVICE(0x04e8, 0xa051), },
  2712. },
  2713. { .dev = { USB_DEVICE(0x04e8, 0xa054), },
  2714. },
  2715. { .dev = { USB_DEVICE(0x04e8, 0xa05b), },
  2716. },
  2717. { .dev = { USB_DEVICE(0x04e8, 0xa058), },
  2718. },
  2719. { .dev = { USB_DEVICE(0x04e8, 0xa057), },
  2720. },
  2721. { .dev = { USB_DEVICE(0x04e8, 0xa059), },
  2722. },
  2723. { .dev = { USB_DEVICE(0x04e8, 0xa05e), },
  2724. },
  2725. {}
  2726. };
  2727. static struct dev_table reverse_bypass_device_table[] = {
  2728. { .dev = { USB_DEVICE(0x04e8, 0xa051), },
  2729. }, /* The device for reverse bypass */
  2730. {}
  2731. };
  2732. static int check_audio_id(struct usb_device *dev)
  2733. {
  2734. struct dev_table *id;
  2735. int ret = 0;
  2736. /* check VID, PID */
  2737. for (id = known_usbaudio_device_table; id->dev.match_flags; id++) {
  2738. if ((id->dev.match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  2739. (id->dev.match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  2740. id->dev.idVendor == le16_to_cpu(dev->descriptor.idVendor) &&
  2741. id->dev.idProduct == le16_to_cpu(dev->descriptor.idProduct)) {
  2742. ret = 1;
  2743. break;
  2744. }
  2745. }
  2746. if (ret)
  2747. unl_info("%s find\n", __func__);
  2748. return ret;
  2749. }
  2750. static int check_audio_descriptor(struct usb_device *dev)
  2751. {
  2752. struct usb_interface *intf;
  2753. struct usb_host_interface *alts;
  2754. struct usb_endpoint_descriptor *endpt;
  2755. unsigned int i, j;
  2756. int ret = 0;
  2757. __u8 play_intf = 0, cap_intf = 0;
  2758. __u8 aud_con_cnt = 0, out_ep = 0, in_ep = 0;
  2759. /* 1. check samsung vid */
  2760. if (le16_to_cpu(dev->descriptor.idVendor) != 0x04e8)
  2761. goto done;
  2762. /* 2. If set config is not execute, return false */
  2763. if (!dev->actconfig) {
  2764. unl_info("%s no set config\n", __func__);
  2765. goto done;
  2766. }
  2767. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  2768. intf = dev->actconfig->interface[i];
  2769. alts = intf->cur_altsetting;
  2770. if (alts->desc.bInterfaceClass == USB_CLASS_AUDIO) {
  2771. if (alts->desc.bInterfaceSubClass
  2772. == USB_SUBCLASS_AUDIOCONTROL)
  2773. aud_con_cnt++;
  2774. if (alts->desc.bInterfaceSubClass
  2775. != USB_SUBCLASS_AUDIOSTREAMING &&
  2776. alts->desc.bInterfaceSubClass
  2777. != USB_CLASS_VENDOR_SPEC)
  2778. continue;
  2779. out_ep = 0;
  2780. in_ep = 0;
  2781. for (j = 0; j < intf->num_altsetting; j++) {
  2782. alts = &intf->altsetting[j];
  2783. if (alts->desc.bNumEndpoints < 1)
  2784. continue;
  2785. endpt = &alts->endpoint[0].desc;
  2786. /*
  2787. * If there is endpoint[1],
  2788. * it will be sync endpoint(feedback).
  2789. */
  2790. if (!endpt)
  2791. continue;
  2792. if (endpt->bEndpointAddress & USB_DIR_IN) {
  2793. if (!in_ep)
  2794. in_ep = endpt->bEndpointAddress;
  2795. else if (in_ep !=
  2796. endpt->bEndpointAddress) {
  2797. unl_info("%s in_ep 2 or more\n",
  2798. __func__);
  2799. goto done;
  2800. } else
  2801. continue;
  2802. } else {
  2803. if (!out_ep)
  2804. out_ep =
  2805. endpt->bEndpointAddress;
  2806. else if (out_ep !=
  2807. endpt->bEndpointAddress) {
  2808. unl_info("%s out_ep 2 or more\n",
  2809. __func__);
  2810. goto done;
  2811. } else
  2812. continue;
  2813. }
  2814. }
  2815. if (out_ep)
  2816. play_intf++;
  2817. else if (in_ep)
  2818. cap_intf++;
  2819. else {
  2820. unl_err("%s no ep\n", __func__);
  2821. goto done;
  2822. }
  2823. }
  2824. }
  2825. /* 3. final check. AUDIOCONTROL 1. playback 1. capture 1 */
  2826. if (aud_con_cnt == 1 && play_intf == 1 && cap_intf == 1)
  2827. ret = 1;
  2828. done:
  2829. if (aud_con_cnt)
  2830. unl_info("%s ret=%d,aud_con_cnt=%d,play_intf=%d,cap_intf=%d\n",
  2831. __func__, ret, aud_con_cnt, play_intf, cap_intf);
  2832. return ret;
  2833. }
  2834. int is_known_usbaudio(struct usb_device *dev)
  2835. {
  2836. int ret = 0;
  2837. ret = check_audio_id(dev);
  2838. if (ret)
  2839. goto done;
  2840. ret = check_audio_descriptor(dev);
  2841. if (ret)
  2842. goto done;
  2843. done:
  2844. return ret;
  2845. }
  2846. EXPORT_SYMBOL(is_known_usbaudio);
  2847. #define MAX_C_D_L (2048)
  2848. int check_usbaudio(struct usb_device *dev)
  2849. {
  2850. struct otg_notify *o_notify = get_otg_notify();
  2851. struct usb_notify *u_notify = NULL;
  2852. struct usb_interface *intf;
  2853. struct usb_host_interface *alts;
  2854. unsigned int i;
  2855. int ret = 0;
  2856. u16 total_length;
  2857. if (!o_notify) {
  2858. unl_err("%s o_notify is null\n", __func__);
  2859. goto done;
  2860. }
  2861. u_notify = (struct usb_notify *)(o_notify->u_notify);
  2862. if (!u_notify) {
  2863. unl_err("%s u_notify structure is null\n",
  2864. __func__);
  2865. goto done;
  2866. }
  2867. if (u_notify->lock_state == USB_NOTIFY_UNLOCK)
  2868. goto done;
  2869. if (!dev->actconfig) {
  2870. unl_info("%s no set config\n", __func__);
  2871. goto done;
  2872. }
  2873. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  2874. intf = dev->actconfig->interface[i];
  2875. alts = intf->cur_altsetting;
  2876. if (alts->desc.bInterfaceClass == USB_CLASS_AUDIO) {
  2877. total_length = le16_to_cpu(dev->actconfig->desc.wTotalLength);
  2878. if (total_length > MAX_C_D_L) {
  2879. unl_info("%s total_length %u\n", __func__, total_length);
  2880. detect_illegal_condition(NOTIFY_EVENT_AUDIO_DESCRIPTOR);
  2881. ret = -EACCES;
  2882. break;
  2883. }
  2884. }
  2885. }
  2886. done:
  2887. return ret;
  2888. }
  2889. EXPORT_SYMBOL(check_usbaudio);
  2890. int check_usbgroup(struct usb_device *dev)
  2891. {
  2892. struct otg_notify *o_notify = get_otg_notify();
  2893. struct usb_notify *u_notify = NULL;
  2894. struct usb_interface *intf;
  2895. struct usb_host_interface *alts;
  2896. struct usb_device *hdev;
  2897. unsigned int i;
  2898. int ret = 0;
  2899. bool is_audio_group = false;
  2900. if (!o_notify) {
  2901. unl_err("%s o_notify is null\n", __func__);
  2902. goto done;
  2903. }
  2904. u_notify = (struct usb_notify *)(o_notify->u_notify);
  2905. if (!u_notify) {
  2906. unl_err("%s u_notify structure is null\n",
  2907. __func__);
  2908. goto done;
  2909. }
  2910. hdev = dev->parent;
  2911. if (!hdev) {
  2912. unl_err("%s root hub is not counted\n",
  2913. __func__);
  2914. goto done;
  2915. }
  2916. if (u_notify->lock_state == USB_NOTIFY_UNLOCK)
  2917. goto done;
  2918. if (!dev->actconfig) {
  2919. unl_info("%s no set config\n", __func__);
  2920. goto done;
  2921. }
  2922. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  2923. intf = dev->actconfig->interface[i];
  2924. alts = intf->cur_altsetting;
  2925. if (alts->desc.bInterfaceClass == USB_CLASS_AUDIO) {
  2926. is_audio_group = true;
  2927. break;
  2928. }
  2929. }
  2930. if (is_audio_group) {
  2931. if (u_notify->secure_connect_group[USB_GROUP_AUDIO] < MAX_VAL)
  2932. u_notify->secure_connect_group[USB_GROUP_AUDIO]++;
  2933. } else {
  2934. if (u_notify->secure_connect_group[USB_GROUP_OTEHR] < MAX_VAL)
  2935. u_notify->secure_connect_group[USB_GROUP_OTEHR]++;
  2936. }
  2937. unl_info("%s current audio_cnt=%d, other_cnt=%d\n", __func__,
  2938. u_notify->secure_connect_group[USB_GROUP_AUDIO],
  2939. u_notify->secure_connect_group[USB_GROUP_OTEHR]);
  2940. done:
  2941. return ret;
  2942. }
  2943. EXPORT_SYMBOL(check_usbgroup);
  2944. int is_usbhub(struct usb_device *dev)
  2945. {
  2946. struct otg_notify *o_notify = get_otg_notify();
  2947. struct usb_notify *u_notify = NULL;
  2948. struct usb_interface *intf;
  2949. struct usb_host_interface *alts;
  2950. unsigned int i;
  2951. int ret = 0;
  2952. if (!o_notify) {
  2953. unl_err("%s o_notify is null\n", __func__);
  2954. goto done;
  2955. }
  2956. u_notify = (struct usb_notify *)(o_notify->u_notify);
  2957. if (!u_notify) {
  2958. unl_err("%s u_notify structure is null\n",
  2959. __func__);
  2960. goto done;
  2961. }
  2962. if (!dev->actconfig) {
  2963. unl_info("%s no set config\n", __func__);
  2964. goto done;
  2965. }
  2966. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  2967. intf = dev->actconfig->interface[i];
  2968. alts = intf->cur_altsetting;
  2969. if (alts->desc.bInterfaceClass == USB_CLASS_HUB) {
  2970. ret = 1;
  2971. break;
  2972. }
  2973. }
  2974. done:
  2975. return ret;
  2976. }
  2977. EXPORT_SYMBOL(is_usbhub);
  2978. int disconnect_unauthorized_device(struct usb_device *dev)
  2979. {
  2980. struct otg_notify *o_notify = get_otg_notify();
  2981. struct usb_notify *u_notify = NULL;
  2982. int ret = 0;
  2983. if (!o_notify) {
  2984. unl_err("%s o_notify is null\n", __func__);
  2985. goto done;
  2986. }
  2987. u_notify = (struct usb_notify *)(o_notify->u_notify);
  2988. if (!u_notify) {
  2989. unl_err("%s u_notify structure is null\n",
  2990. __func__);
  2991. goto done;
  2992. }
  2993. if (u_notify->allowlist_restricted) {
  2994. u_notify->allowlist_restricted--;
  2995. if (u_notify->allowlist_restricted == 0)
  2996. send_usb_restrict_uevent(USB_SECURE_RELEASE);
  2997. }
  2998. unl_info("%s allowlist_restricted(%d)\n", __func__, u_notify->allowlist_restricted);
  2999. done:
  3000. return ret;
  3001. }
  3002. EXPORT_SYMBOL(disconnect_unauthorized_device);
  3003. void set_usb_audio_cardnum(int card_num, int bundle, int attach)
  3004. {
  3005. struct otg_notify *o_notify = get_otg_notify();
  3006. struct usb_notify *u_notify = NULL;
  3007. if (!o_notify) {
  3008. unl_err("%s o_notify is null\n", __func__);
  3009. goto err;
  3010. }
  3011. u_notify = (struct usb_notify *)(o_notify->u_notify);
  3012. if (!u_notify) {
  3013. unl_err("%s u_notify structure is null\n",
  3014. __func__);
  3015. goto err;
  3016. }
  3017. unl_info("%s card=%d attach=%d\n", __func__, card_num, attach);
  3018. if (attach) {
  3019. u_notify->udev.usb_audio_cards[card_num].cards = 1;
  3020. if (bundle)
  3021. u_notify->udev.usb_audio_cards[card_num].bundle = 1;
  3022. } else {
  3023. u_notify->udev.usb_audio_cards[card_num].cards = 0;
  3024. u_notify->udev.usb_audio_cards[card_num].bundle = 0;
  3025. }
  3026. err:
  3027. return;
  3028. }
  3029. EXPORT_SYMBOL(set_usb_audio_cardnum);
  3030. #ifdef CONFIG_USB_AUDIO_ENHANCED_DETECT_TIME
  3031. int __weak get_next_snd_card_number(struct module *module)
  3032. {
  3033. int idx = 0;
  3034. unl_info("%s call weak function\n", __func__);
  3035. return idx;
  3036. }
  3037. #endif
  3038. void send_usb_audio_uevent(struct usb_device *dev,
  3039. int card_num, int attach)
  3040. {
  3041. struct otg_notify *o_notify = get_otg_notify();
  3042. char *envp[6];
  3043. char *type = {"TYPE=usbaudio"};
  3044. char *state_add = {"STATE=ADD"};
  3045. char *state_remove = {"STATE=REMOVE"};
  3046. char vidpid_vuf[15];
  3047. char path_buf[50];
  3048. int index = 0;
  3049. #ifdef CONFIG_USB_AUDIO_ENHANCED_DETECT_TIME
  3050. char cardnum_buf[10];
  3051. int cardnum = 0;
  3052. #endif
  3053. if (!o_notify) {
  3054. unl_err("%s o_notify is null\n", __func__);
  3055. goto err;
  3056. }
  3057. if (!is_known_usbaudio(dev))
  3058. goto err;
  3059. envp[index++] = type;
  3060. if (attach)
  3061. envp[index++] = state_add;
  3062. else
  3063. envp[index++] = state_remove;
  3064. snprintf(vidpid_vuf, sizeof(vidpid_vuf),
  3065. "ID=%04X/%04X", le16_to_cpu(dev->descriptor.idVendor),
  3066. le16_to_cpu(dev->descriptor.idProduct));
  3067. envp[index++] = vidpid_vuf;
  3068. snprintf(path_buf, sizeof(path_buf),
  3069. "PATH=/dev/bus/usb/%03d/%03d", dev->bus->busnum, dev->devnum);
  3070. envp[index++] = path_buf;
  3071. #ifdef CONFIG_USB_AUDIO_ENHANCED_DETECT_TIME
  3072. if (attach && !card_num) {
  3073. cardnum = get_next_snd_card_number(THIS_MODULE);
  3074. if (cardnum < 0) {
  3075. unl_err("%s cardnum error\n", __func__);
  3076. goto err;
  3077. }
  3078. } else
  3079. cardnum = card_num;
  3080. set_usb_audio_cardnum(cardnum, 1, attach);
  3081. snprintf(cardnum_buf, sizeof(cardnum_buf),
  3082. "CARDNUM=%d", cardnum);
  3083. envp[index++] = cardnum_buf;
  3084. #endif
  3085. envp[index++] = NULL;
  3086. if (send_usb_notify_uevent(o_notify, envp)) {
  3087. unl_err("%s error\n", __func__);
  3088. goto err;
  3089. }
  3090. unl_info("%s\n", __func__);
  3091. err:
  3092. return;
  3093. }
  3094. EXPORT_SYMBOL(send_usb_audio_uevent);
  3095. int send_usb_notify_uevent(struct otg_notify *n, char *envp_ext[])
  3096. {
  3097. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  3098. int ret = 0;
  3099. if (!u_notify) {
  3100. unl_err("%s u_notify is null\n", __func__);
  3101. ret = -EFAULT;
  3102. goto err;
  3103. }
  3104. ret = usb_notify_dev_uevent(&u_notify->udev, envp_ext);
  3105. err:
  3106. return ret;
  3107. }
  3108. EXPORT_SYMBOL(send_usb_notify_uevent);
  3109. static int check_reverse_bypass_device(struct usb_device *dev)
  3110. {
  3111. struct dev_table *id;
  3112. /* check VID, PID */
  3113. for (id = reverse_bypass_device_table; id->dev.match_flags; id++) {
  3114. if ((id->dev.match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  3115. (id->dev.match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  3116. id->dev.idVendor == le16_to_cpu(dev->descriptor.idVendor) &&
  3117. id->dev.idProduct == le16_to_cpu(dev->descriptor.idProduct)) {
  3118. unl_info("%s found\n", __func__);
  3119. return 1;
  3120. }
  3121. }
  3122. return 0;
  3123. }
  3124. static int check_reverse_bypass_status(struct otg_notify *n)
  3125. {
  3126. struct usb_notify *u_notify = NULL;
  3127. if (!n) {
  3128. unl_err("%s otg_notify is null\n", __func__);
  3129. return false;
  3130. }
  3131. u_notify = (struct usb_notify *)(n->u_notify);
  3132. if (!u_notify) {
  3133. unl_err("%s u_notify structure is null\n", __func__);
  3134. return false;
  3135. }
  3136. unl_info("%s reverse bypass flag=%d\n", __func__, u_notify->reverse_bypass_status);
  3137. return u_notify->reverse_bypass_status;
  3138. }
  3139. static void reverse_bypass_drive_on_work(struct work_struct *w)
  3140. {
  3141. struct otg_notify *o_notify = get_otg_notify();
  3142. send_otg_notify(o_notify, NOTIFY_EVENT_REVERSE_BYPASS_DEVICE_CONNECT, 1);
  3143. #if defined(CONFIG_USB_HW_PARAM)
  3144. inc_hw_param(o_notify, USB_HOST_REVERSE_BYPASS_COUNT);
  3145. #endif
  3146. }
  3147. int check_new_device_added(struct usb_device *udev)
  3148. {
  3149. struct otg_notify *o_notify = get_otg_notify();
  3150. struct usb_notify *u_notify;
  3151. void *pdata;
  3152. int support_reverse_bypass_en;
  3153. struct usb_device *hdev;
  3154. struct usb_device *dev;
  3155. int port = 0, ret = 0;
  3156. if (!o_notify) {
  3157. pr_err("%s otg_notify is null\n", __func__);
  3158. return ret;
  3159. }
  3160. u_notify = (struct usb_notify *)(o_notify->u_notify);
  3161. if (!u_notify) {
  3162. pr_err("%s usb_notify is null\n", __func__);
  3163. return ret;
  3164. }
  3165. pdata = get_notify_data(o_notify);
  3166. if (!pdata) {
  3167. pr_err("%s pdata is null\n", __func__);
  3168. return ret;
  3169. }
  3170. if (!o_notify->get_support_reverse_bypass_en) {
  3171. pr_err("%s get_support_reverse_bypass_en is null\n", __func__);
  3172. return ret;
  3173. }
  3174. support_reverse_bypass_en =
  3175. o_notify->get_support_reverse_bypass_en(pdata);
  3176. unl_info("%s support_reverse_bypass_en : %d\n", __func__,
  3177. support_reverse_bypass_en);
  3178. hdev = udev->parent;
  3179. if (!hdev)
  3180. return ret;
  3181. hdev = udev->bus->root_hub;
  3182. if (!hdev)
  3183. return ret;
  3184. usb_hub_for_each_child(hdev, port, dev) {
  3185. if (support_reverse_bypass_en && check_reverse_bypass_device(dev)) {
  3186. switch (check_reverse_bypass_status(o_notify)) {
  3187. case NOTIFY_EVENT_REVERSE_BYPASS_OFF:
  3188. ret = -1;
  3189. break;
  3190. case NOTIFY_EVENT_REVERSE_BYPASS_PREPARE:
  3191. schedule_work(&u_notify->reverse_bypass_on_work);
  3192. ret = -1;
  3193. break;
  3194. case NOTIFY_EVENT_REVERSE_BYPASS_ON:
  3195. break;
  3196. default:
  3197. break;
  3198. }
  3199. return ret;
  3200. }
  3201. }
  3202. return ret;
  3203. }
  3204. EXPORT_SYMBOL(check_new_device_added);
  3205. int set_lpm_charging_type_done(struct otg_notify *n, unsigned int state)
  3206. {
  3207. struct usb_notify *u_notify = NULL;
  3208. int ret = 0;
  3209. if (!u_notify_core) {
  3210. pr_err("%s u_notify_core is null\n", __func__);
  3211. ret = -EFAULT;
  3212. goto err;
  3213. }
  3214. unl_info("%s state %u\n", __func__, state);
  3215. u_notify_core->lpm_charging_type_done = state;
  3216. if (!n) {
  3217. pr_err("%s otg_notify is null\n", __func__);
  3218. ret = -EFAULT;
  3219. goto err;
  3220. }
  3221. u_notify = (struct usb_notify *)(n->u_notify);
  3222. if (!u_notify) {
  3223. pr_err("%s u_notify is null\n", __func__);
  3224. ret = -EFAULT;
  3225. goto err;
  3226. }
  3227. u_notify->udev.lpm_charging_type_done = state;
  3228. err:
  3229. return ret;
  3230. }
  3231. EXPORT_SYMBOL(set_lpm_charging_type_done);
  3232. static int check_secure_connection(struct usb_notify *u_notify)
  3233. {
  3234. int i;
  3235. for (i = 0; i < USB_GROUP_MAX; i++) {
  3236. if (u_notify->secure_connect_group[i] >= MAX_SECURE_CONNECTION)
  3237. return true;
  3238. }
  3239. return false;
  3240. }
  3241. int detect_illegal_condition(int type)
  3242. {
  3243. struct otg_notify *o_notify = get_otg_notify();
  3244. struct usb_notify *u_notify;
  3245. int ret = 0, restricted = 0;
  3246. if (!o_notify) {
  3247. pr_err("%s otg_notify is null\n", __func__);
  3248. return ret;
  3249. }
  3250. u_notify = (struct usb_notify *)(o_notify->u_notify);
  3251. if (!u_notify) {
  3252. pr_err("%s usb_notify is null\n", __func__);
  3253. return ret;
  3254. }
  3255. unl_info("%s type %d +\n", __func__, type);
  3256. switch (type) {
  3257. case NOTIFY_EVENT_AUDIO_DESCRIPTOR:
  3258. restricted = 1;
  3259. #if defined(CONFIG_USB_HW_PARAM)
  3260. if (o_notify)
  3261. inc_hw_param(o_notify, USB_HOST_OVER_AUDIO_DESCRIPTOR_COUNT);
  3262. #endif
  3263. break;
  3264. case NOTIFY_EVENT_SECURE_DISCONNECTION:
  3265. if (check_secure_connection(u_notify))
  3266. restricted = 1;
  3267. break;
  3268. default:
  3269. break;
  3270. }
  3271. if (restricted) {
  3272. u_notify->restricted = 1;
  3273. #if defined(CONFIG_USB_HW_PARAM)
  3274. if (o_notify)
  3275. inc_hw_param(o_notify, USB_HOST_SB_COUNT);
  3276. #endif
  3277. if (is_host_cable_enable(o_notify))
  3278. send_otg_notify(o_notify, VIRT_EVENT(u_notify->c_type), 0);
  3279. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_PRE, 1);
  3280. send_external_notify(EXTERNAL_NOTIFY_HOSTBLOCK_POST, 1);
  3281. }
  3282. unl_info("%s type %d restricted=%d -\n", __func__, type, restricted);
  3283. return ret;
  3284. }
  3285. EXPORT_SYMBOL(detect_illegal_condition);
  3286. #if defined(CONFIG_USB_HW_PARAM)
  3287. unsigned long long *get_hw_param(struct otg_notify *n,
  3288. enum usb_hw_param index)
  3289. {
  3290. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  3291. int ret = 0;
  3292. if (index < 0 || index >= USB_CCIC_HW_PARAM_MAX) {
  3293. unl_err("%s usb_hw_param is out of bound\n", __func__);
  3294. return NULL;
  3295. }
  3296. if (!u_notify) {
  3297. unl_err("%s u_notify structure is null\n", __func__);
  3298. return NULL;
  3299. }
  3300. if (!u_notify_core) {
  3301. ret = create_usb_notify();
  3302. if (ret) {
  3303. unl_err("unable create_usb_notify\n");
  3304. return NULL;
  3305. }
  3306. }
  3307. return &(u_notify->hw_param[index]);
  3308. }
  3309. EXPORT_SYMBOL(get_hw_param);
  3310. int inc_hw_param(struct otg_notify *n,
  3311. enum usb_hw_param index)
  3312. {
  3313. struct usb_notify *u_notify;
  3314. int ret = 0;
  3315. if (!n) {
  3316. unl_err("%s otg_notify is null\n", __func__);
  3317. return -ENODEV;
  3318. }
  3319. u_notify = (struct usb_notify *)(n->u_notify);
  3320. if (index < 0 || index >= USB_CCIC_HW_PARAM_MAX) {
  3321. unl_err("%s usb_hw_param is out of bound\n", __func__);
  3322. ret = -ENOMEM;
  3323. return ret;
  3324. }
  3325. if (!u_notify) {
  3326. unl_err("%s u_notify structure is null\n", __func__);
  3327. ret = -ENOENT;
  3328. return ret;
  3329. }
  3330. if (!u_notify_core) {
  3331. ret = create_usb_notify();
  3332. if (ret) {
  3333. unl_err("unable create_usb_notify\n");
  3334. return ret;
  3335. }
  3336. }
  3337. u_notify->hw_param[index]++;
  3338. return ret;
  3339. }
  3340. EXPORT_SYMBOL(inc_hw_param);
  3341. int inc_hw_param_host(struct host_notify_dev *dev,
  3342. enum usb_hw_param index)
  3343. {
  3344. struct usb_notify *u_notify = container_of(dev,
  3345. struct usb_notify, ndev);
  3346. int ret = 0;
  3347. if (index < 0 || index >= USB_CCIC_HW_PARAM_MAX) {
  3348. unl_err("%s usb_hw_param is out of bound\n", __func__);
  3349. ret = -ENOMEM;
  3350. return ret;
  3351. }
  3352. if (!u_notify) {
  3353. unl_err("%s u_notify structure is null\n", __func__);
  3354. ret = -ENOENT;
  3355. return ret;
  3356. }
  3357. if (!u_notify_core) {
  3358. ret = create_usb_notify();
  3359. if (ret) {
  3360. unl_err("unable create_usb_notify\n");
  3361. return ret;
  3362. }
  3363. }
  3364. u_notify->hw_param[index]++;
  3365. return ret;
  3366. }
  3367. EXPORT_SYMBOL(inc_hw_param_host);
  3368. int register_hw_param_manager(struct otg_notify *n, unsigned long (*fptr)(int))
  3369. {
  3370. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  3371. int ret = 0;
  3372. if (!u_notify) {
  3373. unl_err("%s u_notify structure is null\n", __func__);
  3374. ret = -ENOENT;
  3375. goto err;
  3376. }
  3377. if (!u_notify_core) {
  3378. ret = create_usb_notify();
  3379. if (ret) {
  3380. unl_err("unable create_usb_notify\n");
  3381. goto err;
  3382. }
  3383. }
  3384. u_notify->udev.fp_hw_param_manager = fptr;
  3385. unl_info("%s\n", __func__);
  3386. err:
  3387. return ret;
  3388. }
  3389. EXPORT_SYMBOL(register_hw_param_manager);
  3390. #endif
  3391. void *get_notify_data(struct otg_notify *n)
  3392. {
  3393. if (n)
  3394. return n->o_data;
  3395. else
  3396. return NULL;
  3397. }
  3398. EXPORT_SYMBOL(get_notify_data);
  3399. void set_notify_data(struct otg_notify *n, void *data)
  3400. {
  3401. n->o_data = data;
  3402. }
  3403. EXPORT_SYMBOL(set_notify_data);
  3404. struct otg_notify *get_otg_notify(void)
  3405. {
  3406. if (!u_notify_core)
  3407. return NULL;
  3408. if (!u_notify_core->o_notify)
  3409. return NULL;
  3410. return u_notify_core->o_notify;
  3411. }
  3412. EXPORT_SYMBOL(get_otg_notify);
  3413. void enable_usb_notify(void)
  3414. {
  3415. struct otg_notify *o_notify = get_otg_notify();
  3416. struct usb_notify *u_notify = NULL;
  3417. if (!o_notify) {
  3418. unl_err("%s o_notify is null\n", __func__);
  3419. return;
  3420. }
  3421. u_notify = (struct usb_notify *)(o_notify->u_notify);
  3422. if (!u_notify) {
  3423. unl_err("%s u_notify structure is null\n",
  3424. __func__);
  3425. return;
  3426. }
  3427. if (!o_notify->booting_delay_sync_usb) {
  3428. unl_err("%s booting_delay_sync_usb is not setting\n",
  3429. __func__);
  3430. return;
  3431. }
  3432. o_notify->booting_delay_sync_usb = 0;
  3433. if (!delayed_work_pending(&u_notify->b_delay.booting_work))
  3434. schedule_delayed_work(&u_notify->b_delay.booting_work, 0);
  3435. else
  3436. unl_err("%s wait booting_delay\n", __func__);
  3437. }
  3438. EXPORT_SYMBOL(enable_usb_notify);
  3439. static int otg_notify_reboot(struct notifier_block *nb,
  3440. unsigned long event, void *cmd)
  3441. {
  3442. struct otg_notify *o_notify = get_otg_notify();
  3443. struct usb_notify *u_notify = NULL;
  3444. if (!o_notify) {
  3445. unl_err("%s o_notify is null\n", __func__);
  3446. goto err;
  3447. }
  3448. u_notify = (struct usb_notify *)(o_notify->u_notify);
  3449. if (!u_notify) {
  3450. unl_err("%s u_notify structure is null\n",
  3451. __func__);
  3452. goto err;
  3453. }
  3454. if (is_host_cable_enable(o_notify))
  3455. send_otg_notify(o_notify,
  3456. VIRT_EVENT(u_notify->c_type), 0);
  3457. err:
  3458. return NOTIFY_DONE;
  3459. }
  3460. static struct notifier_block otg_notify_reboot_nb = {
  3461. .notifier_call = otg_notify_reboot,
  3462. };
  3463. int set_otg_notify(struct otg_notify *n)
  3464. {
  3465. struct usb_notify *u_notify;
  3466. int ret = 0;
  3467. if (!u_notify_core) {
  3468. ret = create_usb_notify();
  3469. if (ret) {
  3470. pr_err("unable create_usb_notify\n");
  3471. goto err;
  3472. }
  3473. }
  3474. if (u_notify_core->o_notify && n) {
  3475. pr_err("error : already set o_notify\n");
  3476. goto err;
  3477. }
  3478. unl_info("registered otg_notify +\n");
  3479. if (!n) {
  3480. pr_err("otg notify structure is null\n");
  3481. ret = -EFAULT;
  3482. goto err1;
  3483. }
  3484. u_notify_core->o_notify = n;
  3485. u_notify = kzalloc(sizeof(struct usb_notify), GFP_KERNEL);
  3486. if (!u_notify) {
  3487. ret = -ENOMEM;
  3488. goto err1;
  3489. }
  3490. u_notify->o_notify = n;
  3491. n->u_notify = (void *)u_notify;
  3492. u_notify->notifier_wq
  3493. = create_singlethread_workqueue("usb_notify");
  3494. if (!u_notify->notifier_wq) {
  3495. unl_err("%s failed to create work queue\n", __func__);
  3496. ret = -ENOMEM;
  3497. goto err2;
  3498. }
  3499. ovc_init(u_notify);
  3500. notify_event_lock_init(u_notify);
  3501. mutex_init(&u_notify->state_lock);
  3502. ATOMIC_INIT_NOTIFIER_HEAD(&u_notify->otg_notifier);
  3503. u_notify->otg_nb.notifier_call = otg_notifier_callback;
  3504. ret = atomic_notifier_chain_register(&u_notify->otg_notifier,
  3505. &u_notify->otg_nb);
  3506. if (ret < 0) {
  3507. unl_err("atomic_notifier_chain_register failed\n");
  3508. goto err3;
  3509. }
  3510. BLOCKING_INIT_NOTIFIER_HEAD(&u_notify->extra_notifier);
  3511. u_notify->extra_nb.notifier_call = extra_notifier_callback;
  3512. ret = blocking_notifier_chain_register
  3513. (&u_notify->extra_notifier, &u_notify->extra_nb);
  3514. if (ret < 0) {
  3515. unl_err("blocking_notifier_chain_register failed\n");
  3516. goto err4;
  3517. }
  3518. if (!n->unsupport_host) {
  3519. u_notify->ndev.name = "usb_otg";
  3520. u_notify->ndev.set_booster = n->vbus_drive;
  3521. u_notify->ndev.set_mode = n->set_host;
  3522. ret = host_notify_dev_register(&u_notify->ndev);
  3523. if (ret < 0) {
  3524. unl_err("host_notify_dev_register is failed\n");
  3525. goto err5;
  3526. }
  3527. if (!n->vbus_drive) {
  3528. unl_err("vbus_drive is null\n");
  3529. goto err6;
  3530. }
  3531. }
  3532. u_notify->udev.name = "usb_control";
  3533. u_notify->udev.set_disable = set_notify_disable;
  3534. u_notify->udev.set_mdm = set_notify_mdm;
  3535. u_notify->udev.set_mdm_for_id = set_notify_mdm_for_id;
  3536. u_notify->udev.set_mdm_for_serial = set_notify_mdm_for_serial;
  3537. u_notify->udev.control_usb_max_speed = control_usb_maximum_speed;
  3538. u_notify->udev.fp_hw_param_manager = NULL;
  3539. u_notify->udev.set_lock_state = set_notify_lock_state;
  3540. u_notify->udev.o_notify = n;
  3541. ret = usb_notify_dev_register(&u_notify->udev);
  3542. if (ret < 0) {
  3543. unl_err("usb_notify_dev_register is failed\n");
  3544. goto err6;
  3545. }
  3546. u_notify->udev.lpm_charging_type_done
  3547. = u_notify_core->lpm_charging_type_done;
  3548. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  3549. u_notify->udev.secure_lock = USB_NOTIFY_INIT_STATE;
  3550. #endif
  3551. if (gpio_is_valid(n->vbus_detect_gpio) ||
  3552. gpio_is_valid(n->redriver_en_gpio)) {
  3553. ret = register_gpios(n);
  3554. if (ret < 0) {
  3555. unl_err("register_gpios is failed\n");
  3556. goto err7;
  3557. }
  3558. }
  3559. if (n->is_wakelock) {
  3560. u_notify->ws.name = "usb_notify";
  3561. wakeup_source_add(&u_notify->ws);
  3562. }
  3563. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  3564. init_waitqueue_head(&u_notify->init_delay);
  3565. #endif
  3566. if (n->booting_delay_sec) {
  3567. #ifndef CONFIG_DISABLE_LOCKSCREEN_USB_RESTRICTION
  3568. u_notify->lock_state = USB_NOTIFY_INIT_STATE;
  3569. #endif
  3570. INIT_DELAYED_WORK(&u_notify->b_delay.booting_work,
  3571. reserve_state_check);
  3572. schedule_delayed_work(&u_notify->b_delay.booting_work,
  3573. n->booting_delay_sec*HZ);
  3574. }
  3575. if (n->device_check_sec)
  3576. INIT_DELAYED_WORK(&u_notify->check_work,
  3577. device_connect_check);
  3578. INIT_WORK(&u_notify->reverse_bypass_on_work,
  3579. reverse_bypass_drive_on_work);
  3580. register_usbdev_notify();
  3581. register_reboot_notifier(&otg_notify_reboot_nb);
  3582. unl_info("registered otg_notify -\n");
  3583. return 0;
  3584. err7:
  3585. usb_notify_dev_unregister(&u_notify->udev);
  3586. err6:
  3587. if (!n->unsupport_host)
  3588. host_notify_dev_unregister(&u_notify->ndev);
  3589. err5:
  3590. blocking_notifier_chain_unregister(&u_notify->extra_notifier,
  3591. &u_notify->extra_nb);
  3592. err4:
  3593. atomic_notifier_chain_unregister(&u_notify->otg_notifier,
  3594. &u_notify->otg_nb);
  3595. err3:
  3596. flush_workqueue(u_notify->notifier_wq);
  3597. destroy_workqueue(u_notify->notifier_wq);
  3598. err2:
  3599. u_notify->o_notify = NULL;
  3600. kfree(u_notify);
  3601. err1:
  3602. u_notify_core->o_notify = NULL;
  3603. err:
  3604. return ret;
  3605. }
  3606. EXPORT_SYMBOL(set_otg_notify);
  3607. void put_otg_notify(struct otg_notify *n)
  3608. {
  3609. struct usb_notify *u_notify = (struct usb_notify *)(n->u_notify);
  3610. if (!u_notify) {
  3611. unl_err("%s u_notify structure is null\n", __func__);
  3612. return;
  3613. }
  3614. unregister_reboot_notifier(&otg_notify_reboot_nb);
  3615. unregister_usbdev_notify();
  3616. if (n->booting_delay_sec)
  3617. cancel_delayed_work_sync(&u_notify->b_delay.booting_work);
  3618. if (n->is_wakelock)
  3619. wakeup_source_remove(&u_notify->ws);
  3620. if (gpio_is_valid(n->redriver_en_gpio))
  3621. gpio_free(n->redriver_en_gpio);
  3622. if (gpio_is_valid(n->vbus_detect_gpio)) {
  3623. free_irq(gpio_to_irq(n->vbus_detect_gpio), NULL);
  3624. gpio_free(n->vbus_detect_gpio);
  3625. }
  3626. usb_notify_dev_unregister(&u_notify->udev);
  3627. if (!n->unsupport_host)
  3628. host_notify_dev_unregister(&u_notify->ndev);
  3629. blocking_notifier_chain_unregister(&u_notify->extra_notifier,
  3630. &u_notify->extra_nb);
  3631. atomic_notifier_chain_unregister(&u_notify->otg_notifier,
  3632. &u_notify->otg_nb);
  3633. flush_workqueue(u_notify->notifier_wq);
  3634. destroy_workqueue(u_notify->notifier_wq);
  3635. u_notify->o_notify = NULL;
  3636. kfree(u_notify);
  3637. }
  3638. EXPORT_SYMBOL(put_otg_notify);
  3639. static int __init usb_notify_init(void)
  3640. {
  3641. return create_usb_notify();
  3642. }
  3643. static void __exit usb_notify_exit(void)
  3644. {
  3645. if (!u_notify_core)
  3646. return;
  3647. usb_notify_class_exit();
  3648. notify_class_exit();
  3649. unregister_usblog_proc();
  3650. kfree(u_notify_core);
  3651. }
  3652. module_init(usb_notify_init);
  3653. module_exit(usb_notify_exit);
  3654. MODULE_AUTHOR("Samsung USB Team");
  3655. MODULE_DESCRIPTION("USB Notify Layer");
  3656. MODULE_LICENSE("GPL");
  3657. MODULE_VERSION(NOTIFY_VERSION);