btintel.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth support for Intel devices
  5. *
  6. * Copyright (C) 2015 Intel Corporation
  7. */
  8. #include <linux/module.h>
  9. #include <linux/firmware.h>
  10. #include <linux/regmap.h>
  11. #include <linux/acpi.h>
  12. #include <acpi/acpi_bus.h>
  13. #include <linux/unaligned.h>
  14. #include <linux/efi.h>
  15. #include <net/bluetooth/bluetooth.h>
  16. #include <net/bluetooth/hci_core.h>
  17. #include "btintel.h"
  18. #define VERSION "0.1"
  19. #define BDADDR_INTEL (&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
  20. #define RSA_HEADER_LEN 644
  21. #define CSS_HEADER_OFFSET 8
  22. #define ECDSA_OFFSET 644
  23. #define ECDSA_HEADER_LEN 320
  24. #define BTINTEL_EFI_DSBR L"UefiCnvCommonDSBR"
  25. enum {
  26. DSM_SET_WDISABLE2_DELAY = 1,
  27. DSM_SET_RESET_METHOD = 3,
  28. };
  29. #define CMD_WRITE_BOOT_PARAMS 0xfc0e
  30. struct cmd_write_boot_params {
  31. __le32 boot_addr;
  32. u8 fw_build_num;
  33. u8 fw_build_ww;
  34. u8 fw_build_yy;
  35. } __packed;
  36. static struct {
  37. const char *driver_name;
  38. u8 hw_variant;
  39. u32 fw_build_num;
  40. } coredump_info;
  41. static const guid_t btintel_guid_dsm =
  42. GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233,
  43. 0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9);
  44. int btintel_check_bdaddr(struct hci_dev *hdev)
  45. {
  46. struct hci_rp_read_bd_addr *bda;
  47. struct sk_buff *skb;
  48. skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
  49. HCI_INIT_TIMEOUT);
  50. if (IS_ERR(skb)) {
  51. int err = PTR_ERR(skb);
  52. bt_dev_err(hdev, "Reading Intel device address failed (%d)",
  53. err);
  54. return err;
  55. }
  56. if (skb->len != sizeof(*bda)) {
  57. bt_dev_err(hdev, "Intel device address length mismatch");
  58. kfree_skb(skb);
  59. return -EIO;
  60. }
  61. bda = (struct hci_rp_read_bd_addr *)skb->data;
  62. /* For some Intel based controllers, the default Bluetooth device
  63. * address 00:03:19:9E:8B:00 can be found. These controllers are
  64. * fully operational, but have the danger of duplicate addresses
  65. * and that in turn can cause problems with Bluetooth operation.
  66. */
  67. if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
  68. bt_dev_err(hdev, "Found Intel default device address (%pMR)",
  69. &bda->bdaddr);
  70. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  71. }
  72. kfree_skb(skb);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
  76. int btintel_enter_mfg(struct hci_dev *hdev)
  77. {
  78. static const u8 param[] = { 0x01, 0x00 };
  79. struct sk_buff *skb;
  80. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
  81. if (IS_ERR(skb)) {
  82. bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
  83. PTR_ERR(skb));
  84. return PTR_ERR(skb);
  85. }
  86. kfree_skb(skb);
  87. return 0;
  88. }
  89. EXPORT_SYMBOL_GPL(btintel_enter_mfg);
  90. int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
  91. {
  92. u8 param[] = { 0x00, 0x00 };
  93. struct sk_buff *skb;
  94. /* The 2nd command parameter specifies the manufacturing exit method:
  95. * 0x00: Just disable the manufacturing mode (0x00).
  96. * 0x01: Disable manufacturing mode and reset with patches deactivated.
  97. * 0x02: Disable manufacturing mode and reset with patches activated.
  98. */
  99. if (reset)
  100. param[1] |= patched ? 0x02 : 0x01;
  101. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
  102. if (IS_ERR(skb)) {
  103. bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
  104. PTR_ERR(skb));
  105. return PTR_ERR(skb);
  106. }
  107. kfree_skb(skb);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL_GPL(btintel_exit_mfg);
  111. int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  112. {
  113. struct sk_buff *skb;
  114. int err;
  115. skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
  116. if (IS_ERR(skb)) {
  117. err = PTR_ERR(skb);
  118. bt_dev_err(hdev, "Changing Intel device address failed (%d)",
  119. err);
  120. return err;
  121. }
  122. kfree_skb(skb);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
  126. static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
  127. {
  128. u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  129. struct sk_buff *skb;
  130. int err;
  131. if (debug)
  132. mask[1] |= 0x62;
  133. skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
  134. if (IS_ERR(skb)) {
  135. err = PTR_ERR(skb);
  136. bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
  137. return err;
  138. }
  139. kfree_skb(skb);
  140. return 0;
  141. }
  142. int btintel_set_diag(struct hci_dev *hdev, bool enable)
  143. {
  144. struct sk_buff *skb;
  145. u8 param[3];
  146. int err;
  147. if (enable) {
  148. param[0] = 0x03;
  149. param[1] = 0x03;
  150. param[2] = 0x03;
  151. } else {
  152. param[0] = 0x00;
  153. param[1] = 0x00;
  154. param[2] = 0x00;
  155. }
  156. skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
  157. if (IS_ERR(skb)) {
  158. err = PTR_ERR(skb);
  159. if (err == -ENODATA)
  160. goto done;
  161. bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
  162. err);
  163. return err;
  164. }
  165. kfree_skb(skb);
  166. done:
  167. btintel_set_event_mask(hdev, enable);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(btintel_set_diag);
  171. static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
  172. {
  173. int err, ret;
  174. err = btintel_enter_mfg(hdev);
  175. if (err)
  176. return err;
  177. ret = btintel_set_diag(hdev, enable);
  178. err = btintel_exit_mfg(hdev, false, false);
  179. if (err)
  180. return err;
  181. return ret;
  182. }
  183. static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
  184. {
  185. int ret;
  186. /* Legacy ROM device needs to be in the manufacturer mode to apply
  187. * diagnostic setting
  188. *
  189. * This flag is set after reading the Intel version.
  190. */
  191. if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
  192. ret = btintel_set_diag_mfg(hdev, enable);
  193. else
  194. ret = btintel_set_diag(hdev, enable);
  195. return ret;
  196. }
  197. void btintel_hw_error(struct hci_dev *hdev, u8 code)
  198. {
  199. struct sk_buff *skb;
  200. u8 type = 0x00;
  201. bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
  202. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  203. if (IS_ERR(skb)) {
  204. bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
  205. PTR_ERR(skb));
  206. return;
  207. }
  208. kfree_skb(skb);
  209. skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
  210. if (IS_ERR(skb)) {
  211. bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
  212. PTR_ERR(skb));
  213. return;
  214. }
  215. if (skb->len != 13) {
  216. bt_dev_err(hdev, "Exception info size mismatch");
  217. kfree_skb(skb);
  218. return;
  219. }
  220. bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
  221. kfree_skb(skb);
  222. }
  223. EXPORT_SYMBOL_GPL(btintel_hw_error);
  224. int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
  225. {
  226. const char *variant;
  227. /* The hardware platform number has a fixed value of 0x37 and
  228. * for now only accept this single value.
  229. */
  230. if (ver->hw_platform != 0x37) {
  231. bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
  232. ver->hw_platform);
  233. return -EINVAL;
  234. }
  235. /* Check for supported iBT hardware variants of this firmware
  236. * loading method.
  237. *
  238. * This check has been put in place to ensure correct forward
  239. * compatibility options when newer hardware variants come along.
  240. */
  241. switch (ver->hw_variant) {
  242. case 0x07: /* WP - Legacy ROM */
  243. case 0x08: /* StP - Legacy ROM */
  244. case 0x0b: /* SfP */
  245. case 0x0c: /* WsP */
  246. case 0x11: /* JfP */
  247. case 0x12: /* ThP */
  248. case 0x13: /* HrP */
  249. case 0x14: /* CcP */
  250. break;
  251. default:
  252. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  253. ver->hw_variant);
  254. return -EINVAL;
  255. }
  256. switch (ver->fw_variant) {
  257. case 0x01:
  258. variant = "Legacy ROM 2.5";
  259. break;
  260. case 0x06:
  261. variant = "Bootloader";
  262. break;
  263. case 0x22:
  264. variant = "Legacy ROM 2.x";
  265. break;
  266. case 0x23:
  267. variant = "Firmware";
  268. break;
  269. default:
  270. bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
  271. return -EINVAL;
  272. }
  273. coredump_info.hw_variant = ver->hw_variant;
  274. coredump_info.fw_build_num = ver->fw_build_num;
  275. bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
  276. variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
  277. ver->fw_build_num, ver->fw_build_ww,
  278. 2000 + ver->fw_build_yy);
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(btintel_version_info);
  282. static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
  283. const void *param)
  284. {
  285. while (plen > 0) {
  286. struct sk_buff *skb;
  287. u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
  288. cmd_param[0] = fragment_type;
  289. memcpy(cmd_param + 1, param, fragment_len);
  290. skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
  291. cmd_param, HCI_INIT_TIMEOUT);
  292. if (IS_ERR(skb))
  293. return PTR_ERR(skb);
  294. kfree_skb(skb);
  295. plen -= fragment_len;
  296. param += fragment_len;
  297. }
  298. return 0;
  299. }
  300. int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
  301. {
  302. const struct firmware *fw;
  303. struct sk_buff *skb;
  304. const u8 *fw_ptr;
  305. int err;
  306. err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
  307. if (err < 0) {
  308. bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
  309. ddc_name, err);
  310. return err;
  311. }
  312. bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
  313. fw_ptr = fw->data;
  314. /* DDC file contains one or more DDC structure which has
  315. * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
  316. */
  317. while (fw->size > fw_ptr - fw->data) {
  318. u8 cmd_plen = fw_ptr[0] + sizeof(u8);
  319. skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
  320. HCI_INIT_TIMEOUT);
  321. if (IS_ERR(skb)) {
  322. bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
  323. PTR_ERR(skb));
  324. release_firmware(fw);
  325. return PTR_ERR(skb);
  326. }
  327. fw_ptr += cmd_plen;
  328. kfree_skb(skb);
  329. }
  330. release_firmware(fw);
  331. bt_dev_info(hdev, "Applying Intel DDC parameters completed");
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
  335. int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
  336. {
  337. int err, ret;
  338. err = btintel_enter_mfg(hdev);
  339. if (err)
  340. return err;
  341. ret = btintel_set_event_mask(hdev, debug);
  342. err = btintel_exit_mfg(hdev, false, false);
  343. if (err)
  344. return err;
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
  348. int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
  349. {
  350. struct sk_buff *skb;
  351. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
  352. if (IS_ERR(skb)) {
  353. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  354. PTR_ERR(skb));
  355. return PTR_ERR(skb);
  356. }
  357. if (!skb || skb->len != sizeof(*ver)) {
  358. bt_dev_err(hdev, "Intel version event size mismatch");
  359. kfree_skb(skb);
  360. return -EILSEQ;
  361. }
  362. memcpy(ver, skb->data, sizeof(*ver));
  363. kfree_skb(skb);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(btintel_read_version);
  367. int btintel_version_info_tlv(struct hci_dev *hdev,
  368. struct intel_version_tlv *version)
  369. {
  370. const char *variant;
  371. /* The hardware platform number has a fixed value of 0x37 and
  372. * for now only accept this single value.
  373. */
  374. if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
  375. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  376. INTEL_HW_PLATFORM(version->cnvi_bt));
  377. return -EINVAL;
  378. }
  379. /* Check for supported iBT hardware variants of this firmware
  380. * loading method.
  381. *
  382. * This check has been put in place to ensure correct forward
  383. * compatibility options when newer hardware variants come along.
  384. */
  385. switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
  386. case 0x17: /* TyP */
  387. case 0x18: /* Slr */
  388. case 0x19: /* Slr-F */
  389. case 0x1b: /* Mgr */
  390. case 0x1c: /* Gale Peak (GaP) */
  391. case 0x1d: /* BlazarU (BzrU) */
  392. case 0x1e: /* BlazarI (Bzr) */
  393. break;
  394. default:
  395. bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
  396. INTEL_HW_VARIANT(version->cnvi_bt));
  397. return -EINVAL;
  398. }
  399. switch (version->img_type) {
  400. case BTINTEL_IMG_BOOTLOADER:
  401. variant = "Bootloader";
  402. /* It is required that every single firmware fragment is acknowledged
  403. * with a command complete event. If the boot parameters indicate
  404. * that this bootloader does not send them, then abort the setup.
  405. */
  406. if (version->limited_cce != 0x00) {
  407. bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
  408. version->limited_cce);
  409. return -EINVAL;
  410. }
  411. /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
  412. if (version->sbe_type > 0x01) {
  413. bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
  414. version->sbe_type);
  415. return -EINVAL;
  416. }
  417. bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
  418. bt_dev_info(hdev, "Secure boot is %s",
  419. version->secure_boot ? "enabled" : "disabled");
  420. bt_dev_info(hdev, "OTP lock is %s",
  421. version->otp_lock ? "enabled" : "disabled");
  422. bt_dev_info(hdev, "API lock is %s",
  423. version->api_lock ? "enabled" : "disabled");
  424. bt_dev_info(hdev, "Debug lock is %s",
  425. version->debug_lock ? "enabled" : "disabled");
  426. bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
  427. version->min_fw_build_nn, version->min_fw_build_cw,
  428. 2000 + version->min_fw_build_yy);
  429. break;
  430. case BTINTEL_IMG_IML:
  431. variant = "Intermediate loader";
  432. break;
  433. case BTINTEL_IMG_OP:
  434. variant = "Firmware";
  435. break;
  436. default:
  437. bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
  438. return -EINVAL;
  439. }
  440. coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
  441. coredump_info.fw_build_num = version->build_num;
  442. bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
  443. 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
  444. version->build_type, version->build_num);
  445. if (version->img_type == BTINTEL_IMG_OP)
  446. bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(btintel_version_info_tlv);
  450. int btintel_parse_version_tlv(struct hci_dev *hdev,
  451. struct intel_version_tlv *version,
  452. struct sk_buff *skb)
  453. {
  454. /* Consume Command Complete Status field */
  455. skb_pull(skb, 1);
  456. /* Event parameters contatin multiple TLVs. Read each of them
  457. * and only keep the required data. Also, it use existing legacy
  458. * version field like hw_platform, hw_variant, and fw_variant
  459. * to keep the existing setup flow
  460. */
  461. while (skb->len) {
  462. struct intel_tlv *tlv;
  463. /* Make sure skb has a minimum length of the header */
  464. if (skb->len < sizeof(*tlv))
  465. return -EINVAL;
  466. tlv = (struct intel_tlv *)skb->data;
  467. /* Make sure skb has a enough data */
  468. if (skb->len < tlv->len + sizeof(*tlv))
  469. return -EINVAL;
  470. switch (tlv->type) {
  471. case INTEL_TLV_CNVI_TOP:
  472. version->cnvi_top = get_unaligned_le32(tlv->val);
  473. break;
  474. case INTEL_TLV_CNVR_TOP:
  475. version->cnvr_top = get_unaligned_le32(tlv->val);
  476. break;
  477. case INTEL_TLV_CNVI_BT:
  478. version->cnvi_bt = get_unaligned_le32(tlv->val);
  479. break;
  480. case INTEL_TLV_CNVR_BT:
  481. version->cnvr_bt = get_unaligned_le32(tlv->val);
  482. break;
  483. case INTEL_TLV_DEV_REV_ID:
  484. version->dev_rev_id = get_unaligned_le16(tlv->val);
  485. break;
  486. case INTEL_TLV_IMAGE_TYPE:
  487. version->img_type = tlv->val[0];
  488. break;
  489. case INTEL_TLV_TIME_STAMP:
  490. /* If image type is Operational firmware (0x03), then
  491. * running FW Calendar Week and Year information can
  492. * be extracted from Timestamp information
  493. */
  494. version->min_fw_build_cw = tlv->val[0];
  495. version->min_fw_build_yy = tlv->val[1];
  496. version->timestamp = get_unaligned_le16(tlv->val);
  497. break;
  498. case INTEL_TLV_BUILD_TYPE:
  499. version->build_type = tlv->val[0];
  500. break;
  501. case INTEL_TLV_BUILD_NUM:
  502. /* If image type is Operational firmware (0x03), then
  503. * running FW build number can be extracted from the
  504. * Build information
  505. */
  506. version->min_fw_build_nn = tlv->val[0];
  507. version->build_num = get_unaligned_le32(tlv->val);
  508. break;
  509. case INTEL_TLV_SECURE_BOOT:
  510. version->secure_boot = tlv->val[0];
  511. break;
  512. case INTEL_TLV_OTP_LOCK:
  513. version->otp_lock = tlv->val[0];
  514. break;
  515. case INTEL_TLV_API_LOCK:
  516. version->api_lock = tlv->val[0];
  517. break;
  518. case INTEL_TLV_DEBUG_LOCK:
  519. version->debug_lock = tlv->val[0];
  520. break;
  521. case INTEL_TLV_MIN_FW:
  522. version->min_fw_build_nn = tlv->val[0];
  523. version->min_fw_build_cw = tlv->val[1];
  524. version->min_fw_build_yy = tlv->val[2];
  525. break;
  526. case INTEL_TLV_LIMITED_CCE:
  527. version->limited_cce = tlv->val[0];
  528. break;
  529. case INTEL_TLV_SBE_TYPE:
  530. version->sbe_type = tlv->val[0];
  531. break;
  532. case INTEL_TLV_OTP_BDADDR:
  533. memcpy(&version->otp_bd_addr, tlv->val,
  534. sizeof(bdaddr_t));
  535. break;
  536. case INTEL_TLV_GIT_SHA1:
  537. version->git_sha1 = get_unaligned_le32(tlv->val);
  538. break;
  539. case INTEL_TLV_FW_ID:
  540. snprintf(version->fw_id, sizeof(version->fw_id),
  541. "%s", tlv->val);
  542. break;
  543. default:
  544. /* Ignore rest of information */
  545. break;
  546. }
  547. /* consume the current tlv and move to next*/
  548. skb_pull(skb, tlv->len + sizeof(*tlv));
  549. }
  550. return 0;
  551. }
  552. EXPORT_SYMBOL_GPL(btintel_parse_version_tlv);
  553. static int btintel_read_version_tlv(struct hci_dev *hdev,
  554. struct intel_version_tlv *version)
  555. {
  556. struct sk_buff *skb;
  557. const u8 param[1] = { 0xFF };
  558. if (!version)
  559. return -EINVAL;
  560. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  561. if (IS_ERR(skb)) {
  562. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  563. PTR_ERR(skb));
  564. return PTR_ERR(skb);
  565. }
  566. if (skb->data[0]) {
  567. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  568. skb->data[0]);
  569. kfree_skb(skb);
  570. return -EIO;
  571. }
  572. btintel_parse_version_tlv(hdev, version, skb);
  573. kfree_skb(skb);
  574. return 0;
  575. }
  576. /* ------- REGMAP IBT SUPPORT ------- */
  577. #define IBT_REG_MODE_8BIT 0x00
  578. #define IBT_REG_MODE_16BIT 0x01
  579. #define IBT_REG_MODE_32BIT 0x02
  580. struct regmap_ibt_context {
  581. struct hci_dev *hdev;
  582. __u16 op_write;
  583. __u16 op_read;
  584. };
  585. struct ibt_cp_reg_access {
  586. __le32 addr;
  587. __u8 mode;
  588. __u8 len;
  589. __u8 data[];
  590. } __packed;
  591. struct ibt_rp_reg_access {
  592. __u8 status;
  593. __le32 addr;
  594. __u8 data[];
  595. } __packed;
  596. static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
  597. void *val, size_t val_size)
  598. {
  599. struct regmap_ibt_context *ctx = context;
  600. struct ibt_cp_reg_access cp;
  601. struct ibt_rp_reg_access *rp;
  602. struct sk_buff *skb;
  603. int err = 0;
  604. if (reg_size != sizeof(__le32))
  605. return -EINVAL;
  606. switch (val_size) {
  607. case 1:
  608. cp.mode = IBT_REG_MODE_8BIT;
  609. break;
  610. case 2:
  611. cp.mode = IBT_REG_MODE_16BIT;
  612. break;
  613. case 4:
  614. cp.mode = IBT_REG_MODE_32BIT;
  615. break;
  616. default:
  617. return -EINVAL;
  618. }
  619. /* regmap provides a little-endian formatted addr */
  620. cp.addr = *(__le32 *)addr;
  621. cp.len = val_size;
  622. bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
  623. skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
  624. HCI_CMD_TIMEOUT);
  625. if (IS_ERR(skb)) {
  626. err = PTR_ERR(skb);
  627. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
  628. le32_to_cpu(cp.addr), err);
  629. return err;
  630. }
  631. if (skb->len != sizeof(*rp) + val_size) {
  632. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
  633. le32_to_cpu(cp.addr));
  634. err = -EINVAL;
  635. goto done;
  636. }
  637. rp = (struct ibt_rp_reg_access *)skb->data;
  638. if (rp->addr != cp.addr) {
  639. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
  640. le32_to_cpu(rp->addr));
  641. err = -EINVAL;
  642. goto done;
  643. }
  644. memcpy(val, rp->data, val_size);
  645. done:
  646. kfree_skb(skb);
  647. return err;
  648. }
  649. static int regmap_ibt_gather_write(void *context,
  650. const void *addr, size_t reg_size,
  651. const void *val, size_t val_size)
  652. {
  653. struct regmap_ibt_context *ctx = context;
  654. struct ibt_cp_reg_access *cp;
  655. struct sk_buff *skb;
  656. int plen = sizeof(*cp) + val_size;
  657. u8 mode;
  658. int err = 0;
  659. if (reg_size != sizeof(__le32))
  660. return -EINVAL;
  661. switch (val_size) {
  662. case 1:
  663. mode = IBT_REG_MODE_8BIT;
  664. break;
  665. case 2:
  666. mode = IBT_REG_MODE_16BIT;
  667. break;
  668. case 4:
  669. mode = IBT_REG_MODE_32BIT;
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. cp = kmalloc(plen, GFP_KERNEL);
  675. if (!cp)
  676. return -ENOMEM;
  677. /* regmap provides a little-endian formatted addr/value */
  678. cp->addr = *(__le32 *)addr;
  679. cp->mode = mode;
  680. cp->len = val_size;
  681. memcpy(&cp->data, val, val_size);
  682. bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
  683. skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
  684. if (IS_ERR(skb)) {
  685. err = PTR_ERR(skb);
  686. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
  687. le32_to_cpu(cp->addr), err);
  688. goto done;
  689. }
  690. kfree_skb(skb);
  691. done:
  692. kfree(cp);
  693. return err;
  694. }
  695. static int regmap_ibt_write(void *context, const void *data, size_t count)
  696. {
  697. /* data contains register+value, since we only support 32bit addr,
  698. * minimum data size is 4 bytes.
  699. */
  700. if (WARN_ONCE(count < 4, "Invalid register access"))
  701. return -EINVAL;
  702. return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
  703. }
  704. static void regmap_ibt_free_context(void *context)
  705. {
  706. kfree(context);
  707. }
  708. static const struct regmap_bus regmap_ibt = {
  709. .read = regmap_ibt_read,
  710. .write = regmap_ibt_write,
  711. .gather_write = regmap_ibt_gather_write,
  712. .free_context = regmap_ibt_free_context,
  713. .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
  714. .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
  715. };
  716. /* Config is the same for all register regions */
  717. static const struct regmap_config regmap_ibt_cfg = {
  718. .name = "btintel_regmap",
  719. .reg_bits = 32,
  720. .val_bits = 32,
  721. };
  722. struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
  723. u16 opcode_write)
  724. {
  725. struct regmap_ibt_context *ctx;
  726. bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
  727. opcode_write);
  728. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  729. if (!ctx)
  730. return ERR_PTR(-ENOMEM);
  731. ctx->op_read = opcode_read;
  732. ctx->op_write = opcode_write;
  733. ctx->hdev = hdev;
  734. return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
  735. }
  736. EXPORT_SYMBOL_GPL(btintel_regmap_init);
  737. int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
  738. {
  739. struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
  740. struct sk_buff *skb;
  741. params.boot_param = cpu_to_le32(boot_param);
  742. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
  743. HCI_INIT_TIMEOUT);
  744. if (IS_ERR(skb)) {
  745. bt_dev_err(hdev, "Failed to send Intel Reset command");
  746. return PTR_ERR(skb);
  747. }
  748. kfree_skb(skb);
  749. return 0;
  750. }
  751. EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
  752. int btintel_read_boot_params(struct hci_dev *hdev,
  753. struct intel_boot_params *params)
  754. {
  755. struct sk_buff *skb;
  756. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  757. if (IS_ERR(skb)) {
  758. bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
  759. PTR_ERR(skb));
  760. return PTR_ERR(skb);
  761. }
  762. if (skb->len != sizeof(*params)) {
  763. bt_dev_err(hdev, "Intel boot parameters size mismatch");
  764. kfree_skb(skb);
  765. return -EILSEQ;
  766. }
  767. memcpy(params, skb->data, sizeof(*params));
  768. kfree_skb(skb);
  769. if (params->status) {
  770. bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
  771. params->status);
  772. return -bt_to_errno(params->status);
  773. }
  774. bt_dev_info(hdev, "Device revision is %u",
  775. le16_to_cpu(params->dev_revid));
  776. bt_dev_info(hdev, "Secure boot is %s",
  777. params->secure_boot ? "enabled" : "disabled");
  778. bt_dev_info(hdev, "OTP lock is %s",
  779. params->otp_lock ? "enabled" : "disabled");
  780. bt_dev_info(hdev, "API lock is %s",
  781. params->api_lock ? "enabled" : "disabled");
  782. bt_dev_info(hdev, "Debug lock is %s",
  783. params->debug_lock ? "enabled" : "disabled");
  784. bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
  785. params->min_fw_build_nn, params->min_fw_build_cw,
  786. 2000 + params->min_fw_build_yy);
  787. return 0;
  788. }
  789. EXPORT_SYMBOL_GPL(btintel_read_boot_params);
  790. static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
  791. const struct firmware *fw)
  792. {
  793. int err;
  794. /* Start the firmware download transaction with the Init fragment
  795. * represented by the 128 bytes of CSS header.
  796. */
  797. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  798. if (err < 0) {
  799. bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
  800. goto done;
  801. }
  802. /* Send the 256 bytes of public key information from the firmware
  803. * as the PKey fragment.
  804. */
  805. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  806. if (err < 0) {
  807. bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
  808. goto done;
  809. }
  810. /* Send the 256 bytes of signature information from the firmware
  811. * as the Sign fragment.
  812. */
  813. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  814. if (err < 0) {
  815. bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
  816. goto done;
  817. }
  818. done:
  819. return err;
  820. }
  821. static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
  822. const struct firmware *fw)
  823. {
  824. int err;
  825. /* Start the firmware download transaction with the Init fragment
  826. * represented by the 128 bytes of CSS header.
  827. */
  828. err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
  829. if (err < 0) {
  830. bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
  831. return err;
  832. }
  833. /* Send the 96 bytes of public key information from the firmware
  834. * as the PKey fragment.
  835. */
  836. err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
  837. if (err < 0) {
  838. bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
  839. return err;
  840. }
  841. /* Send the 96 bytes of signature information from the firmware
  842. * as the Sign fragment
  843. */
  844. err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
  845. if (err < 0) {
  846. bt_dev_err(hdev, "Failed to send firmware signature (%d)",
  847. err);
  848. return err;
  849. }
  850. return 0;
  851. }
  852. static int btintel_download_firmware_payload(struct hci_dev *hdev,
  853. const struct firmware *fw,
  854. size_t offset)
  855. {
  856. int err;
  857. const u8 *fw_ptr;
  858. u32 frag_len;
  859. fw_ptr = fw->data + offset;
  860. frag_len = 0;
  861. err = -EINVAL;
  862. while (fw_ptr - fw->data < fw->size) {
  863. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  864. frag_len += sizeof(*cmd) + cmd->plen;
  865. /* The parameter length of the secure send command requires
  866. * a 4 byte alignment. It happens so that the firmware file
  867. * contains proper Intel_NOP commands to align the fragments
  868. * as needed.
  869. *
  870. * Send set of commands with 4 byte alignment from the
  871. * firmware data buffer as a single Data fragement.
  872. */
  873. if (!(frag_len % 4)) {
  874. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  875. if (err < 0) {
  876. bt_dev_err(hdev,
  877. "Failed to send firmware data (%d)",
  878. err);
  879. goto done;
  880. }
  881. fw_ptr += frag_len;
  882. frag_len = 0;
  883. }
  884. }
  885. done:
  886. return err;
  887. }
  888. static bool btintel_firmware_version(struct hci_dev *hdev,
  889. u8 num, u8 ww, u8 yy,
  890. const struct firmware *fw,
  891. u32 *boot_addr)
  892. {
  893. const u8 *fw_ptr;
  894. fw_ptr = fw->data;
  895. while (fw_ptr - fw->data < fw->size) {
  896. struct hci_command_hdr *cmd = (void *)(fw_ptr);
  897. /* Each SKU has a different reset parameter to use in the
  898. * HCI_Intel_Reset command and it is embedded in the firmware
  899. * data. So, instead of using static value per SKU, check
  900. * the firmware data and save it for later use.
  901. */
  902. if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
  903. struct cmd_write_boot_params *params;
  904. params = (void *)(fw_ptr + sizeof(*cmd));
  905. *boot_addr = le32_to_cpu(params->boot_addr);
  906. bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
  907. bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
  908. params->fw_build_num, params->fw_build_ww,
  909. params->fw_build_yy);
  910. return (num == params->fw_build_num &&
  911. ww == params->fw_build_ww &&
  912. yy == params->fw_build_yy);
  913. }
  914. fw_ptr += sizeof(*cmd) + cmd->plen;
  915. }
  916. return false;
  917. }
  918. int btintel_download_firmware(struct hci_dev *hdev,
  919. struct intel_version *ver,
  920. const struct firmware *fw,
  921. u32 *boot_param)
  922. {
  923. int err;
  924. /* SfP and WsP don't seem to update the firmware version on file
  925. * so version checking is currently not possible.
  926. */
  927. switch (ver->hw_variant) {
  928. case 0x0b: /* SfP */
  929. case 0x0c: /* WsP */
  930. /* Skip version checking */
  931. break;
  932. default:
  933. /* Skip download if firmware has the same version */
  934. if (btintel_firmware_version(hdev, ver->fw_build_num,
  935. ver->fw_build_ww, ver->fw_build_yy,
  936. fw, boot_param)) {
  937. bt_dev_info(hdev, "Firmware already loaded");
  938. /* Return -EALREADY to indicate that the firmware has
  939. * already been loaded.
  940. */
  941. return -EALREADY;
  942. }
  943. }
  944. /* The firmware variant determines if the device is in bootloader
  945. * mode or is running operational firmware. The value 0x06 identifies
  946. * the bootloader and the value 0x23 identifies the operational
  947. * firmware.
  948. *
  949. * If the firmware version has changed that means it needs to be reset
  950. * to bootloader when operational so the new firmware can be loaded.
  951. */
  952. if (ver->fw_variant == 0x23)
  953. return -EINVAL;
  954. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  955. if (err)
  956. return err;
  957. return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
  958. }
  959. EXPORT_SYMBOL_GPL(btintel_download_firmware);
  960. static int btintel_download_fw_tlv(struct hci_dev *hdev,
  961. struct intel_version_tlv *ver,
  962. const struct firmware *fw, u32 *boot_param,
  963. u8 hw_variant, u8 sbe_type)
  964. {
  965. int err;
  966. u32 css_header_ver;
  967. /* Skip download if firmware has the same version */
  968. if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
  969. ver->min_fw_build_cw,
  970. ver->min_fw_build_yy,
  971. fw, boot_param)) {
  972. bt_dev_info(hdev, "Firmware already loaded");
  973. /* Return -EALREADY to indicate that firmware has
  974. * already been loaded.
  975. */
  976. return -EALREADY;
  977. }
  978. /* The firmware variant determines if the device is in bootloader
  979. * mode or is running operational firmware. The value 0x01 identifies
  980. * the bootloader and the value 0x03 identifies the operational
  981. * firmware.
  982. *
  983. * If the firmware version has changed that means it needs to be reset
  984. * to bootloader when operational so the new firmware can be loaded.
  985. */
  986. if (ver->img_type == BTINTEL_IMG_OP)
  987. return -EINVAL;
  988. /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
  989. * only RSA secure boot engine. Hence, the corresponding sfi file will
  990. * have RSA header of 644 bytes followed by Command Buffer.
  991. *
  992. * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
  993. * secure boot engine. As a result, the corresponding sfi file will
  994. * have RSA header of 644, ECDSA header of 320 bytes followed by
  995. * Command Buffer.
  996. *
  997. * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
  998. * version: RSA(0x00010000) , ECDSA (0x00020000)
  999. */
  1000. css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
  1001. if (css_header_ver != 0x00010000) {
  1002. bt_dev_err(hdev, "Invalid CSS Header version");
  1003. return -EINVAL;
  1004. }
  1005. if (hw_variant <= 0x14) {
  1006. if (sbe_type != 0x00) {
  1007. bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
  1008. hw_variant);
  1009. return -EINVAL;
  1010. }
  1011. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  1012. if (err)
  1013. return err;
  1014. err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
  1015. if (err)
  1016. return err;
  1017. } else if (hw_variant >= 0x17) {
  1018. /* Check if CSS header for ECDSA follows the RSA header */
  1019. if (fw->data[ECDSA_OFFSET] != 0x06)
  1020. return -EINVAL;
  1021. /* Check if the CSS Header version is ECDSA(0x00020000) */
  1022. css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
  1023. if (css_header_ver != 0x00020000) {
  1024. bt_dev_err(hdev, "Invalid CSS Header version");
  1025. return -EINVAL;
  1026. }
  1027. if (sbe_type == 0x00) {
  1028. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  1029. if (err)
  1030. return err;
  1031. err = btintel_download_firmware_payload(hdev, fw,
  1032. RSA_HEADER_LEN + ECDSA_HEADER_LEN);
  1033. if (err)
  1034. return err;
  1035. } else if (sbe_type == 0x01) {
  1036. err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
  1037. if (err)
  1038. return err;
  1039. err = btintel_download_firmware_payload(hdev, fw,
  1040. RSA_HEADER_LEN + ECDSA_HEADER_LEN);
  1041. if (err)
  1042. return err;
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. static void btintel_reset_to_bootloader(struct hci_dev *hdev)
  1048. {
  1049. struct intel_reset params;
  1050. struct sk_buff *skb;
  1051. /* Send Intel Reset command. This will result in
  1052. * re-enumeration of BT controller.
  1053. *
  1054. * Intel Reset parameter description:
  1055. * reset_type : 0x00 (Soft reset),
  1056. * 0x01 (Hard reset)
  1057. * patch_enable : 0x00 (Do not enable),
  1058. * 0x01 (Enable)
  1059. * ddc_reload : 0x00 (Do not reload),
  1060. * 0x01 (Reload)
  1061. * boot_option: 0x00 (Current image),
  1062. * 0x01 (Specified boot address)
  1063. * boot_param: Boot address
  1064. *
  1065. */
  1066. params.reset_type = 0x01;
  1067. params.patch_enable = 0x01;
  1068. params.ddc_reload = 0x01;
  1069. params.boot_option = 0x00;
  1070. params.boot_param = cpu_to_le32(0x00000000);
  1071. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
  1072. &params, HCI_INIT_TIMEOUT);
  1073. if (IS_ERR(skb)) {
  1074. bt_dev_err(hdev, "FW download error recovery failed (%ld)",
  1075. PTR_ERR(skb));
  1076. return;
  1077. }
  1078. bt_dev_info(hdev, "Intel reset sent to retry FW download");
  1079. kfree_skb(skb);
  1080. /* Current Intel BT controllers(ThP/JfP) hold the USB reset
  1081. * lines for 2ms when it receives Intel Reset in bootloader mode.
  1082. * Whereas, the upcoming Intel BT controllers will hold USB reset
  1083. * for 150ms. To keep the delay generic, 150ms is chosen here.
  1084. */
  1085. msleep(150);
  1086. }
  1087. static int btintel_read_debug_features(struct hci_dev *hdev,
  1088. struct intel_debug_features *features)
  1089. {
  1090. struct sk_buff *skb;
  1091. u8 page_no = 1;
  1092. /* Intel controller supports two pages, each page is of 128-bit
  1093. * feature bit mask. And each bit defines specific feature support
  1094. */
  1095. skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
  1096. HCI_INIT_TIMEOUT);
  1097. if (IS_ERR(skb)) {
  1098. bt_dev_err(hdev, "Reading supported features failed (%ld)",
  1099. PTR_ERR(skb));
  1100. return PTR_ERR(skb);
  1101. }
  1102. if (skb->len != (sizeof(features->page1) + 3)) {
  1103. bt_dev_err(hdev, "Supported features event size mismatch");
  1104. kfree_skb(skb);
  1105. return -EILSEQ;
  1106. }
  1107. memcpy(features->page1, skb->data + 3, sizeof(features->page1));
  1108. /* Read the supported features page2 if required in future.
  1109. */
  1110. kfree_skb(skb);
  1111. return 0;
  1112. }
  1113. static int btintel_set_debug_features(struct hci_dev *hdev,
  1114. const struct intel_debug_features *features)
  1115. {
  1116. u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
  1117. 0x00, 0x00, 0x00 };
  1118. u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
  1119. u8 trace_enable = 0x02;
  1120. struct sk_buff *skb;
  1121. if (!features) {
  1122. bt_dev_warn(hdev, "Debug features not read");
  1123. return -EINVAL;
  1124. }
  1125. if (!(features->page1[0] & 0x3f)) {
  1126. bt_dev_info(hdev, "Telemetry exception format not supported");
  1127. return 0;
  1128. }
  1129. skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
  1130. if (IS_ERR(skb)) {
  1131. bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
  1132. PTR_ERR(skb));
  1133. return PTR_ERR(skb);
  1134. }
  1135. kfree_skb(skb);
  1136. skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
  1137. if (IS_ERR(skb)) {
  1138. bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
  1139. PTR_ERR(skb));
  1140. return PTR_ERR(skb);
  1141. }
  1142. kfree_skb(skb);
  1143. skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
  1144. if (IS_ERR(skb)) {
  1145. bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
  1146. PTR_ERR(skb));
  1147. return PTR_ERR(skb);
  1148. }
  1149. kfree_skb(skb);
  1150. bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
  1151. trace_enable, mask[3]);
  1152. return 0;
  1153. }
  1154. static int btintel_reset_debug_features(struct hci_dev *hdev,
  1155. const struct intel_debug_features *features)
  1156. {
  1157. u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1158. 0x00, 0x00, 0x00 };
  1159. u8 trace_enable = 0x00;
  1160. struct sk_buff *skb;
  1161. if (!features) {
  1162. bt_dev_warn(hdev, "Debug features not read");
  1163. return -EINVAL;
  1164. }
  1165. if (!(features->page1[0] & 0x3f)) {
  1166. bt_dev_info(hdev, "Telemetry exception format not supported");
  1167. return 0;
  1168. }
  1169. /* Should stop the trace before writing ddc event mask. */
  1170. skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
  1171. if (IS_ERR(skb)) {
  1172. bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
  1173. PTR_ERR(skb));
  1174. return PTR_ERR(skb);
  1175. }
  1176. kfree_skb(skb);
  1177. skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
  1178. if (IS_ERR(skb)) {
  1179. bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
  1180. PTR_ERR(skb));
  1181. return PTR_ERR(skb);
  1182. }
  1183. kfree_skb(skb);
  1184. bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
  1185. trace_enable, mask[3]);
  1186. return 0;
  1187. }
  1188. int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
  1189. {
  1190. struct intel_debug_features features;
  1191. int err;
  1192. bt_dev_dbg(hdev, "enable %d", enable);
  1193. /* Read the Intel supported features and if new exception formats
  1194. * supported, need to load the additional DDC config to enable.
  1195. */
  1196. err = btintel_read_debug_features(hdev, &features);
  1197. if (err)
  1198. return err;
  1199. /* Set or reset the debug features. */
  1200. if (enable)
  1201. err = btintel_set_debug_features(hdev, &features);
  1202. else
  1203. err = btintel_reset_debug_features(hdev, &features);
  1204. return err;
  1205. }
  1206. EXPORT_SYMBOL_GPL(btintel_set_quality_report);
  1207. static void btintel_coredump(struct hci_dev *hdev)
  1208. {
  1209. struct sk_buff *skb;
  1210. skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
  1211. if (IS_ERR(skb)) {
  1212. bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
  1213. return;
  1214. }
  1215. kfree_skb(skb);
  1216. }
  1217. static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
  1218. {
  1219. char buf[80];
  1220. snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
  1221. coredump_info.hw_variant);
  1222. skb_put_data(skb, buf, strlen(buf));
  1223. snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
  1224. coredump_info.fw_build_num);
  1225. skb_put_data(skb, buf, strlen(buf));
  1226. snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
  1227. skb_put_data(skb, buf, strlen(buf));
  1228. snprintf(buf, sizeof(buf), "Vendor: Intel\n");
  1229. skb_put_data(skb, buf, strlen(buf));
  1230. }
  1231. static int btintel_register_devcoredump_support(struct hci_dev *hdev)
  1232. {
  1233. struct intel_debug_features features;
  1234. int err;
  1235. err = btintel_read_debug_features(hdev, &features);
  1236. if (err) {
  1237. bt_dev_info(hdev, "Error reading debug features");
  1238. return err;
  1239. }
  1240. if (!(features.page1[0] & 0x3f)) {
  1241. bt_dev_dbg(hdev, "Telemetry exception format not supported");
  1242. return -EOPNOTSUPP;
  1243. }
  1244. hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
  1245. return err;
  1246. }
  1247. static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
  1248. struct intel_version *ver)
  1249. {
  1250. const struct firmware *fw;
  1251. char fwname[64];
  1252. int ret;
  1253. snprintf(fwname, sizeof(fwname),
  1254. "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
  1255. ver->hw_platform, ver->hw_variant, ver->hw_revision,
  1256. ver->fw_variant, ver->fw_revision, ver->fw_build_num,
  1257. ver->fw_build_ww, ver->fw_build_yy);
  1258. ret = request_firmware(&fw, fwname, &hdev->dev);
  1259. if (ret < 0) {
  1260. if (ret == -EINVAL) {
  1261. bt_dev_err(hdev, "Intel firmware file request failed (%d)",
  1262. ret);
  1263. return NULL;
  1264. }
  1265. bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
  1266. fwname, ret);
  1267. /* If the correct firmware patch file is not found, use the
  1268. * default firmware patch file instead
  1269. */
  1270. snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
  1271. ver->hw_platform, ver->hw_variant);
  1272. if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
  1273. bt_dev_err(hdev, "failed to open default fw file: %s",
  1274. fwname);
  1275. return NULL;
  1276. }
  1277. }
  1278. bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
  1279. return fw;
  1280. }
  1281. static int btintel_legacy_rom_patching(struct hci_dev *hdev,
  1282. const struct firmware *fw,
  1283. const u8 **fw_ptr, int *disable_patch)
  1284. {
  1285. struct sk_buff *skb;
  1286. struct hci_command_hdr *cmd;
  1287. const u8 *cmd_param;
  1288. struct hci_event_hdr *evt = NULL;
  1289. const u8 *evt_param = NULL;
  1290. int remain = fw->size - (*fw_ptr - fw->data);
  1291. /* The first byte indicates the types of the patch command or event.
  1292. * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
  1293. * in the current firmware buffer doesn't start with 0x01 or
  1294. * the size of remain buffer is smaller than HCI command header,
  1295. * the firmware file is corrupted and it should stop the patching
  1296. * process.
  1297. */
  1298. if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
  1299. bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
  1300. return -EINVAL;
  1301. }
  1302. (*fw_ptr)++;
  1303. remain--;
  1304. cmd = (struct hci_command_hdr *)(*fw_ptr);
  1305. *fw_ptr += sizeof(*cmd);
  1306. remain -= sizeof(*cmd);
  1307. /* Ensure that the remain firmware data is long enough than the length
  1308. * of command parameter. If not, the firmware file is corrupted.
  1309. */
  1310. if (remain < cmd->plen) {
  1311. bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
  1312. return -EFAULT;
  1313. }
  1314. /* If there is a command that loads a patch in the firmware
  1315. * file, then enable the patch upon success, otherwise just
  1316. * disable the manufacturer mode, for example patch activation
  1317. * is not required when the default firmware patch file is used
  1318. * because there are no patch data to load.
  1319. */
  1320. if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
  1321. *disable_patch = 0;
  1322. cmd_param = *fw_ptr;
  1323. *fw_ptr += cmd->plen;
  1324. remain -= cmd->plen;
  1325. /* This reads the expected events when the above command is sent to the
  1326. * device. Some vendor commands expects more than one events, for
  1327. * example command status event followed by vendor specific event.
  1328. * For this case, it only keeps the last expected event. so the command
  1329. * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
  1330. * last expected event.
  1331. */
  1332. while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
  1333. (*fw_ptr)++;
  1334. remain--;
  1335. evt = (struct hci_event_hdr *)(*fw_ptr);
  1336. *fw_ptr += sizeof(*evt);
  1337. remain -= sizeof(*evt);
  1338. if (remain < evt->plen) {
  1339. bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
  1340. return -EFAULT;
  1341. }
  1342. evt_param = *fw_ptr;
  1343. *fw_ptr += evt->plen;
  1344. remain -= evt->plen;
  1345. }
  1346. /* Every HCI commands in the firmware file has its correspond event.
  1347. * If event is not found or remain is smaller than zero, the firmware
  1348. * file is corrupted.
  1349. */
  1350. if (!evt || !evt_param || remain < 0) {
  1351. bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
  1352. return -EFAULT;
  1353. }
  1354. skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
  1355. cmd_param, evt->evt, HCI_INIT_TIMEOUT);
  1356. if (IS_ERR(skb)) {
  1357. bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
  1358. cmd->opcode, PTR_ERR(skb));
  1359. return PTR_ERR(skb);
  1360. }
  1361. /* It ensures that the returned event matches the event data read from
  1362. * the firmware file. At fist, it checks the length and then
  1363. * the contents of the event.
  1364. */
  1365. if (skb->len != evt->plen) {
  1366. bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
  1367. le16_to_cpu(cmd->opcode));
  1368. kfree_skb(skb);
  1369. return -EFAULT;
  1370. }
  1371. if (memcmp(skb->data, evt_param, evt->plen)) {
  1372. bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
  1373. le16_to_cpu(cmd->opcode));
  1374. kfree_skb(skb);
  1375. return -EFAULT;
  1376. }
  1377. kfree_skb(skb);
  1378. return 0;
  1379. }
  1380. static int btintel_legacy_rom_setup(struct hci_dev *hdev,
  1381. struct intel_version *ver)
  1382. {
  1383. const struct firmware *fw;
  1384. const u8 *fw_ptr;
  1385. int disable_patch, err;
  1386. struct intel_version new_ver;
  1387. BT_DBG("%s", hdev->name);
  1388. /* fw_patch_num indicates the version of patch the device currently
  1389. * have. If there is no patch data in the device, it is always 0x00.
  1390. * So, if it is other than 0x00, no need to patch the device again.
  1391. */
  1392. if (ver->fw_patch_num) {
  1393. bt_dev_info(hdev,
  1394. "Intel device is already patched. patch num: %02x",
  1395. ver->fw_patch_num);
  1396. goto complete;
  1397. }
  1398. /* Opens the firmware patch file based on the firmware version read
  1399. * from the controller. If it fails to open the matching firmware
  1400. * patch file, it tries to open the default firmware patch file.
  1401. * If no patch file is found, allow the device to operate without
  1402. * a patch.
  1403. */
  1404. fw = btintel_legacy_rom_get_fw(hdev, ver);
  1405. if (!fw)
  1406. goto complete;
  1407. fw_ptr = fw->data;
  1408. /* Enable the manufacturer mode of the controller.
  1409. * Only while this mode is enabled, the driver can download the
  1410. * firmware patch data and configuration parameters.
  1411. */
  1412. err = btintel_enter_mfg(hdev);
  1413. if (err) {
  1414. release_firmware(fw);
  1415. return err;
  1416. }
  1417. disable_patch = 1;
  1418. /* The firmware data file consists of list of Intel specific HCI
  1419. * commands and its expected events. The first byte indicates the
  1420. * type of the message, either HCI command or HCI event.
  1421. *
  1422. * It reads the command and its expected event from the firmware file,
  1423. * and send to the controller. Once __hci_cmd_sync_ev() returns,
  1424. * the returned event is compared with the event read from the firmware
  1425. * file and it will continue until all the messages are downloaded to
  1426. * the controller.
  1427. *
  1428. * Once the firmware patching is completed successfully,
  1429. * the manufacturer mode is disabled with reset and activating the
  1430. * downloaded patch.
  1431. *
  1432. * If the firmware patching fails, the manufacturer mode is
  1433. * disabled with reset and deactivating the patch.
  1434. *
  1435. * If the default patch file is used, no reset is done when disabling
  1436. * the manufacturer.
  1437. */
  1438. while (fw->size > fw_ptr - fw->data) {
  1439. int ret;
  1440. ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
  1441. &disable_patch);
  1442. if (ret < 0)
  1443. goto exit_mfg_deactivate;
  1444. }
  1445. release_firmware(fw);
  1446. if (disable_patch)
  1447. goto exit_mfg_disable;
  1448. /* Patching completed successfully and disable the manufacturer mode
  1449. * with reset and activate the downloaded firmware patches.
  1450. */
  1451. err = btintel_exit_mfg(hdev, true, true);
  1452. if (err)
  1453. return err;
  1454. /* Need build number for downloaded fw patches in
  1455. * every power-on boot
  1456. */
  1457. err = btintel_read_version(hdev, &new_ver);
  1458. if (err)
  1459. return err;
  1460. bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
  1461. new_ver.fw_patch_num);
  1462. goto complete;
  1463. exit_mfg_disable:
  1464. /* Disable the manufacturer mode without reset */
  1465. err = btintel_exit_mfg(hdev, false, false);
  1466. if (err)
  1467. return err;
  1468. bt_dev_info(hdev, "Intel firmware patch completed");
  1469. goto complete;
  1470. exit_mfg_deactivate:
  1471. release_firmware(fw);
  1472. /* Patching failed. Disable the manufacturer mode with reset and
  1473. * deactivate the downloaded firmware patches.
  1474. */
  1475. err = btintel_exit_mfg(hdev, true, false);
  1476. if (err)
  1477. return err;
  1478. bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
  1479. complete:
  1480. /* Set the event mask for Intel specific vendor events. This enables
  1481. * a few extra events that are useful during general operation.
  1482. */
  1483. btintel_set_event_mask_mfg(hdev, false);
  1484. btintel_check_bdaddr(hdev);
  1485. return 0;
  1486. }
  1487. static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
  1488. {
  1489. ktime_t delta, rettime;
  1490. unsigned long long duration;
  1491. int err;
  1492. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1493. bt_dev_info(hdev, "Waiting for firmware download to complete");
  1494. err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
  1495. TASK_INTERRUPTIBLE,
  1496. msecs_to_jiffies(msec));
  1497. if (err == -EINTR) {
  1498. bt_dev_err(hdev, "Firmware loading interrupted");
  1499. return err;
  1500. }
  1501. if (err) {
  1502. bt_dev_err(hdev, "Firmware loading timeout");
  1503. return -ETIMEDOUT;
  1504. }
  1505. if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
  1506. bt_dev_err(hdev, "Firmware loading failed");
  1507. return -ENOEXEC;
  1508. }
  1509. rettime = ktime_get();
  1510. delta = ktime_sub(rettime, calltime);
  1511. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  1512. bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
  1513. return 0;
  1514. }
  1515. static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
  1516. {
  1517. ktime_t delta, rettime;
  1518. unsigned long long duration;
  1519. int err;
  1520. bt_dev_info(hdev, "Waiting for device to boot");
  1521. err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
  1522. TASK_INTERRUPTIBLE,
  1523. msecs_to_jiffies(msec));
  1524. if (err == -EINTR) {
  1525. bt_dev_err(hdev, "Device boot interrupted");
  1526. return -EINTR;
  1527. }
  1528. if (err) {
  1529. bt_dev_err(hdev, "Device boot timeout");
  1530. return -ETIMEDOUT;
  1531. }
  1532. rettime = ktime_get();
  1533. delta = ktime_sub(rettime, calltime);
  1534. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1535. bt_dev_info(hdev, "Device booted in %llu usecs", duration);
  1536. return 0;
  1537. }
  1538. static int btintel_boot_wait_d0(struct hci_dev *hdev, ktime_t calltime,
  1539. int msec)
  1540. {
  1541. ktime_t delta, rettime;
  1542. unsigned long long duration;
  1543. int err;
  1544. bt_dev_info(hdev, "Waiting for device transition to d0");
  1545. err = btintel_wait_on_flag_timeout(hdev, INTEL_WAIT_FOR_D0,
  1546. TASK_INTERRUPTIBLE,
  1547. msecs_to_jiffies(msec));
  1548. if (err == -EINTR) {
  1549. bt_dev_err(hdev, "Device d0 move interrupted");
  1550. return -EINTR;
  1551. }
  1552. if (err) {
  1553. bt_dev_err(hdev, "Device d0 move timeout");
  1554. return -ETIMEDOUT;
  1555. }
  1556. rettime = ktime_get();
  1557. delta = ktime_sub(rettime, calltime);
  1558. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  1559. bt_dev_info(hdev, "Device moved to D0 in %llu usecs", duration);
  1560. return 0;
  1561. }
  1562. static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
  1563. {
  1564. ktime_t calltime;
  1565. int err;
  1566. calltime = ktime_get();
  1567. btintel_set_flag(hdev, INTEL_BOOTING);
  1568. btintel_set_flag(hdev, INTEL_WAIT_FOR_D0);
  1569. err = btintel_send_intel_reset(hdev, boot_addr);
  1570. if (err) {
  1571. bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
  1572. btintel_reset_to_bootloader(hdev);
  1573. return err;
  1574. }
  1575. /* The bootloader will not indicate when the device is ready. This
  1576. * is done by the operational firmware sending bootup notification.
  1577. *
  1578. * Booting into operational firmware should not take longer than
  1579. * 5 second. However if that happens, then just fail the setup
  1580. * since something went wrong.
  1581. */
  1582. err = btintel_boot_wait(hdev, calltime, 5000);
  1583. if (err == -ETIMEDOUT) {
  1584. btintel_reset_to_bootloader(hdev);
  1585. goto exit_error;
  1586. }
  1587. if (hdev->bus == HCI_PCI) {
  1588. /* In case of PCIe, after receiving bootup event, driver performs
  1589. * D0 entry by writing 0 to sleep control register (check
  1590. * btintel_pcie_recv_event())
  1591. * Firmware acks with alive interrupt indicating host is full ready to
  1592. * perform BT operation. Lets wait here till INTEL_WAIT_FOR_D0
  1593. * bit is cleared.
  1594. */
  1595. calltime = ktime_get();
  1596. err = btintel_boot_wait_d0(hdev, calltime, 2000);
  1597. }
  1598. exit_error:
  1599. return err;
  1600. }
  1601. static int btintel_get_fw_name(struct intel_version *ver,
  1602. struct intel_boot_params *params,
  1603. char *fw_name, size_t len,
  1604. const char *suffix)
  1605. {
  1606. switch (ver->hw_variant) {
  1607. case 0x0b: /* SfP */
  1608. case 0x0c: /* WsP */
  1609. snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
  1610. ver->hw_variant,
  1611. le16_to_cpu(params->dev_revid),
  1612. suffix);
  1613. break;
  1614. case 0x11: /* JfP */
  1615. case 0x12: /* ThP */
  1616. case 0x13: /* HrP */
  1617. case 0x14: /* CcP */
  1618. snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
  1619. ver->hw_variant,
  1620. ver->hw_revision,
  1621. ver->fw_revision,
  1622. suffix);
  1623. break;
  1624. default:
  1625. return -EINVAL;
  1626. }
  1627. return 0;
  1628. }
  1629. static int btintel_download_fw(struct hci_dev *hdev,
  1630. struct intel_version *ver,
  1631. struct intel_boot_params *params,
  1632. u32 *boot_param)
  1633. {
  1634. const struct firmware *fw;
  1635. char fwname[64];
  1636. int err;
  1637. ktime_t calltime;
  1638. if (!ver || !params)
  1639. return -EINVAL;
  1640. /* The firmware variant determines if the device is in bootloader
  1641. * mode or is running operational firmware. The value 0x06 identifies
  1642. * the bootloader and the value 0x23 identifies the operational
  1643. * firmware.
  1644. *
  1645. * When the operational firmware is already present, then only
  1646. * the check for valid Bluetooth device address is needed. This
  1647. * determines if the device will be added as configured or
  1648. * unconfigured controller.
  1649. *
  1650. * It is not possible to use the Secure Boot Parameters in this
  1651. * case since that command is only available in bootloader mode.
  1652. */
  1653. if (ver->fw_variant == 0x23) {
  1654. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1655. btintel_check_bdaddr(hdev);
  1656. /* SfP and WsP don't seem to update the firmware version on file
  1657. * so version checking is currently possible.
  1658. */
  1659. switch (ver->hw_variant) {
  1660. case 0x0b: /* SfP */
  1661. case 0x0c: /* WsP */
  1662. return 0;
  1663. }
  1664. /* Proceed to download to check if the version matches */
  1665. goto download;
  1666. }
  1667. /* Read the secure boot parameters to identify the operating
  1668. * details of the bootloader.
  1669. */
  1670. err = btintel_read_boot_params(hdev, params);
  1671. if (err)
  1672. return err;
  1673. /* It is required that every single firmware fragment is acknowledged
  1674. * with a command complete event. If the boot parameters indicate
  1675. * that this bootloader does not send them, then abort the setup.
  1676. */
  1677. if (params->limited_cce != 0x00) {
  1678. bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
  1679. params->limited_cce);
  1680. return -EINVAL;
  1681. }
  1682. /* If the OTP has no valid Bluetooth device address, then there will
  1683. * also be no valid address for the operational firmware.
  1684. */
  1685. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  1686. bt_dev_info(hdev, "No device address configured");
  1687. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1688. }
  1689. download:
  1690. /* With this Intel bootloader only the hardware variant and device
  1691. * revision information are used to select the right firmware for SfP
  1692. * and WsP.
  1693. *
  1694. * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
  1695. *
  1696. * Currently the supported hardware variants are:
  1697. * 11 (0x0b) for iBT3.0 (LnP/SfP)
  1698. * 12 (0x0c) for iBT3.5 (WsP)
  1699. *
  1700. * For ThP/JfP and for future SKU's, the FW name varies based on HW
  1701. * variant, HW revision and FW revision, as these are dependent on CNVi
  1702. * and RF Combination.
  1703. *
  1704. * 17 (0x11) for iBT3.5 (JfP)
  1705. * 18 (0x12) for iBT3.5 (ThP)
  1706. *
  1707. * The firmware file name for these will be
  1708. * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
  1709. *
  1710. */
  1711. err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
  1712. if (err < 0) {
  1713. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1714. /* Firmware has already been loaded */
  1715. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1716. return 0;
  1717. }
  1718. bt_dev_err(hdev, "Unsupported Intel firmware naming");
  1719. return -EINVAL;
  1720. }
  1721. err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
  1722. if (err < 0) {
  1723. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1724. /* Firmware has already been loaded */
  1725. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1726. return 0;
  1727. }
  1728. bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
  1729. fwname, err);
  1730. return err;
  1731. }
  1732. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  1733. if (fw->size < 644) {
  1734. bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
  1735. fw->size);
  1736. err = -EBADF;
  1737. goto done;
  1738. }
  1739. calltime = ktime_get();
  1740. btintel_set_flag(hdev, INTEL_DOWNLOADING);
  1741. /* Start firmware downloading and get boot parameter */
  1742. err = btintel_download_firmware(hdev, ver, fw, boot_param);
  1743. if (err < 0) {
  1744. if (err == -EALREADY) {
  1745. /* Firmware has already been loaded */
  1746. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1747. err = 0;
  1748. goto done;
  1749. }
  1750. /* When FW download fails, send Intel Reset to retry
  1751. * FW download.
  1752. */
  1753. btintel_reset_to_bootloader(hdev);
  1754. goto done;
  1755. }
  1756. /* Before switching the device into operational mode and with that
  1757. * booting the loaded firmware, wait for the bootloader notification
  1758. * that all fragments have been successfully received.
  1759. *
  1760. * When the event processing receives the notification, then the
  1761. * INTEL_DOWNLOADING flag will be cleared.
  1762. *
  1763. * The firmware loading should not take longer than 5 seconds
  1764. * and thus just timeout if that happens and fail the setup
  1765. * of this device.
  1766. */
  1767. err = btintel_download_wait(hdev, calltime, 5000);
  1768. if (err == -ETIMEDOUT)
  1769. btintel_reset_to_bootloader(hdev);
  1770. done:
  1771. release_firmware(fw);
  1772. return err;
  1773. }
  1774. static int btintel_bootloader_setup(struct hci_dev *hdev,
  1775. struct intel_version *ver)
  1776. {
  1777. struct intel_version new_ver;
  1778. struct intel_boot_params params;
  1779. u32 boot_param;
  1780. char ddcname[64];
  1781. int err;
  1782. BT_DBG("%s", hdev->name);
  1783. /* Set the default boot parameter to 0x0 and it is updated to
  1784. * SKU specific boot parameter after reading Intel_Write_Boot_Params
  1785. * command while downloading the firmware.
  1786. */
  1787. boot_param = 0x00000000;
  1788. btintel_set_flag(hdev, INTEL_BOOTLOADER);
  1789. err = btintel_download_fw(hdev, ver, &params, &boot_param);
  1790. if (err)
  1791. return err;
  1792. /* controller is already having an operational firmware */
  1793. if (ver->fw_variant == 0x23)
  1794. goto finish;
  1795. err = btintel_boot(hdev, boot_param);
  1796. if (err)
  1797. return err;
  1798. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1799. err = btintel_get_fw_name(ver, &params, ddcname,
  1800. sizeof(ddcname), "ddc");
  1801. if (err < 0) {
  1802. bt_dev_err(hdev, "Unsupported Intel firmware naming");
  1803. } else {
  1804. /* Once the device is running in operational mode, it needs to
  1805. * apply the device configuration (DDC) parameters.
  1806. *
  1807. * The device can work without DDC parameters, so even if it
  1808. * fails to load the file, no need to fail the setup.
  1809. */
  1810. btintel_load_ddc_config(hdev, ddcname);
  1811. }
  1812. hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
  1813. /* Read the Intel version information after loading the FW */
  1814. err = btintel_read_version(hdev, &new_ver);
  1815. if (err)
  1816. return err;
  1817. btintel_version_info(hdev, &new_ver);
  1818. finish:
  1819. /* Set the event mask for Intel specific vendor events. This enables
  1820. * a few extra events that are useful during general operation. It
  1821. * does not enable any debugging related events.
  1822. *
  1823. * The device will function correctly without these events enabled
  1824. * and thus no need to fail the setup.
  1825. */
  1826. btintel_set_event_mask(hdev, false);
  1827. return 0;
  1828. }
  1829. static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
  1830. char *fw_name, size_t len,
  1831. const char *suffix)
  1832. {
  1833. const char *format;
  1834. u32 cnvi, cnvr;
  1835. cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
  1836. INTEL_CNVX_TOP_STEP(ver->cnvi_top));
  1837. cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
  1838. INTEL_CNVX_TOP_STEP(ver->cnvr_top));
  1839. /* Only Blazar product supports downloading of intermediate loader
  1840. * image
  1841. */
  1842. if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e) {
  1843. u8 zero[BTINTEL_FWID_MAXLEN];
  1844. if (ver->img_type == BTINTEL_IMG_BOOTLOADER) {
  1845. format = "intel/ibt-%04x-%04x-iml.%s";
  1846. snprintf(fw_name, len, format, cnvi, cnvr, suffix);
  1847. return;
  1848. }
  1849. memset(zero, 0, sizeof(zero));
  1850. /* ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step-fw_id> */
  1851. if (memcmp(ver->fw_id, zero, sizeof(zero))) {
  1852. format = "intel/ibt-%04x-%04x-%s.%s";
  1853. snprintf(fw_name, len, format, cnvi, cnvr,
  1854. ver->fw_id, suffix);
  1855. return;
  1856. }
  1857. /* If firmware id is not present, fallback to legacy naming
  1858. * convention
  1859. */
  1860. }
  1861. /* Fallback to legacy naming convention for other controllers
  1862. * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
  1863. */
  1864. format = "intel/ibt-%04x-%04x.%s";
  1865. snprintf(fw_name, len, format, cnvi, cnvr, suffix);
  1866. }
  1867. static void btintel_get_iml_tlv(const struct intel_version_tlv *ver,
  1868. char *fw_name, size_t len,
  1869. const char *suffix)
  1870. {
  1871. const char *format;
  1872. u32 cnvi, cnvr;
  1873. cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
  1874. INTEL_CNVX_TOP_STEP(ver->cnvi_top));
  1875. cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
  1876. INTEL_CNVX_TOP_STEP(ver->cnvr_top));
  1877. format = "intel/ibt-%04x-%04x-iml.%s";
  1878. snprintf(fw_name, len, format, cnvi, cnvr, suffix);
  1879. }
  1880. static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
  1881. struct intel_version_tlv *ver,
  1882. u32 *boot_param)
  1883. {
  1884. const struct firmware *fw;
  1885. char fwname[128];
  1886. int err;
  1887. ktime_t calltime;
  1888. if (!ver || !boot_param)
  1889. return -EINVAL;
  1890. /* The firmware variant determines if the device is in bootloader
  1891. * mode or is running operational firmware. The value 0x03 identifies
  1892. * the bootloader and the value 0x23 identifies the operational
  1893. * firmware.
  1894. *
  1895. * When the operational firmware is already present, then only
  1896. * the check for valid Bluetooth device address is needed. This
  1897. * determines if the device will be added as configured or
  1898. * unconfigured controller.
  1899. *
  1900. * It is not possible to use the Secure Boot Parameters in this
  1901. * case since that command is only available in bootloader mode.
  1902. */
  1903. if (ver->img_type == BTINTEL_IMG_OP) {
  1904. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1905. btintel_check_bdaddr(hdev);
  1906. } else {
  1907. /*
  1908. * Check for valid bd address in boot loader mode. Device
  1909. * will be marked as unconfigured if empty bd address is
  1910. * found.
  1911. */
  1912. if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
  1913. bt_dev_info(hdev, "No device address configured");
  1914. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1915. }
  1916. }
  1917. if (ver->img_type == BTINTEL_IMG_OP) {
  1918. /* Controller running OP image. In case of FW downgrade,
  1919. * FWID TLV may not be present and driver may attempt to load
  1920. * firmware image which doesn't exist. Lets compare the version
  1921. * of IML image
  1922. */
  1923. if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e)
  1924. btintel_get_iml_tlv(ver, fwname, sizeof(fwname), "sfi");
  1925. else
  1926. btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
  1927. } else {
  1928. btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
  1929. }
  1930. err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
  1931. if (err < 0) {
  1932. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1933. /* Firmware has already been loaded */
  1934. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1935. return 0;
  1936. }
  1937. bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
  1938. fwname, err);
  1939. return err;
  1940. }
  1941. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  1942. if (fw->size < 644) {
  1943. bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
  1944. fw->size);
  1945. err = -EBADF;
  1946. goto done;
  1947. }
  1948. calltime = ktime_get();
  1949. btintel_set_flag(hdev, INTEL_DOWNLOADING);
  1950. /* Start firmware downloading and get boot parameter */
  1951. err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
  1952. INTEL_HW_VARIANT(ver->cnvi_bt),
  1953. ver->sbe_type);
  1954. if (err < 0) {
  1955. if (err == -EALREADY) {
  1956. /* Firmware has already been loaded */
  1957. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1958. err = 0;
  1959. goto done;
  1960. }
  1961. /* When FW download fails, send Intel Reset to retry
  1962. * FW download.
  1963. */
  1964. btintel_reset_to_bootloader(hdev);
  1965. goto done;
  1966. }
  1967. /* Before switching the device into operational mode and with that
  1968. * booting the loaded firmware, wait for the bootloader notification
  1969. * that all fragments have been successfully received.
  1970. *
  1971. * When the event processing receives the notification, then the
  1972. * BTUSB_DOWNLOADING flag will be cleared.
  1973. *
  1974. * The firmware loading should not take longer than 5 seconds
  1975. * and thus just timeout if that happens and fail the setup
  1976. * of this device.
  1977. */
  1978. err = btintel_download_wait(hdev, calltime, 5000);
  1979. if (err == -ETIMEDOUT)
  1980. btintel_reset_to_bootloader(hdev);
  1981. done:
  1982. release_firmware(fw);
  1983. return err;
  1984. }
  1985. static int btintel_get_codec_config_data(struct hci_dev *hdev,
  1986. __u8 link, struct bt_codec *codec,
  1987. __u8 *ven_len, __u8 **ven_data)
  1988. {
  1989. int err = 0;
  1990. if (!ven_data || !ven_len)
  1991. return -EINVAL;
  1992. *ven_len = 0;
  1993. *ven_data = NULL;
  1994. if (link != ESCO_LINK) {
  1995. bt_dev_err(hdev, "Invalid link type(%u)", link);
  1996. return -EINVAL;
  1997. }
  1998. *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
  1999. if (!*ven_data) {
  2000. err = -ENOMEM;
  2001. goto error;
  2002. }
  2003. /* supports only CVSD and mSBC offload codecs */
  2004. switch (codec->id) {
  2005. case 0x02:
  2006. **ven_data = 0x00;
  2007. break;
  2008. case 0x05:
  2009. **ven_data = 0x01;
  2010. break;
  2011. default:
  2012. err = -EINVAL;
  2013. bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
  2014. goto error;
  2015. }
  2016. /* codec and its capabilities are pre-defined to ids
  2017. * preset id = 0x00 represents CVSD codec with sampling rate 8K
  2018. * preset id = 0x01 represents mSBC codec with sampling rate 16K
  2019. */
  2020. *ven_len = sizeof(__u8);
  2021. return err;
  2022. error:
  2023. kfree(*ven_data);
  2024. *ven_data = NULL;
  2025. return err;
  2026. }
  2027. static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
  2028. {
  2029. /* Intel uses 1 as data path id for all the usecases */
  2030. *data_path_id = 1;
  2031. return 0;
  2032. }
  2033. static int btintel_configure_offload(struct hci_dev *hdev)
  2034. {
  2035. struct sk_buff *skb;
  2036. int err = 0;
  2037. struct intel_offload_use_cases *use_cases;
  2038. skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
  2039. if (IS_ERR(skb)) {
  2040. bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
  2041. PTR_ERR(skb));
  2042. return PTR_ERR(skb);
  2043. }
  2044. if (skb->len < sizeof(*use_cases)) {
  2045. err = -EIO;
  2046. goto error;
  2047. }
  2048. use_cases = (void *)skb->data;
  2049. if (use_cases->status) {
  2050. err = -bt_to_errno(skb->data[0]);
  2051. goto error;
  2052. }
  2053. if (use_cases->preset[0] & 0x03) {
  2054. hdev->get_data_path_id = btintel_get_data_path_id;
  2055. hdev->get_codec_config_data = btintel_get_codec_config_data;
  2056. }
  2057. error:
  2058. kfree_skb(skb);
  2059. return err;
  2060. }
  2061. static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
  2062. {
  2063. struct sk_buff *skb;
  2064. struct hci_ppag_enable_cmd ppag_cmd;
  2065. acpi_handle handle;
  2066. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  2067. union acpi_object *p, *elements;
  2068. u32 domain, mode;
  2069. acpi_status status;
  2070. /* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
  2071. switch (ver->cnvr_top & 0xFFF) {
  2072. case 0x504: /* Hrp2 */
  2073. case 0x202: /* Jfp2 */
  2074. case 0x201: /* Jfp1 */
  2075. bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
  2076. ver->cnvr_top & 0xFFF);
  2077. return;
  2078. }
  2079. handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
  2080. if (!handle) {
  2081. bt_dev_info(hdev, "No support for BT device in ACPI firmware");
  2082. return;
  2083. }
  2084. status = acpi_evaluate_object(handle, "PPAG", NULL, &buffer);
  2085. if (ACPI_FAILURE(status)) {
  2086. if (status == AE_NOT_FOUND) {
  2087. bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
  2088. return;
  2089. }
  2090. bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
  2091. return;
  2092. }
  2093. p = buffer.pointer;
  2094. if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
  2095. bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
  2096. p->type, p->package.count);
  2097. kfree(buffer.pointer);
  2098. return;
  2099. }
  2100. elements = p->package.elements;
  2101. /* PPAG table is located at element[1] */
  2102. p = &elements[1];
  2103. domain = (u32)p->package.elements[0].integer.value;
  2104. mode = (u32)p->package.elements[1].integer.value;
  2105. kfree(buffer.pointer);
  2106. if (domain != 0x12) {
  2107. bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
  2108. return;
  2109. }
  2110. /* PPAG mode
  2111. * BIT 0 : 0 Disabled in EU
  2112. * 1 Enabled in EU
  2113. * BIT 1 : 0 Disabled in China
  2114. * 1 Enabled in China
  2115. */
  2116. mode &= 0x03;
  2117. if (!mode) {
  2118. bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in BIOS");
  2119. return;
  2120. }
  2121. ppag_cmd.ppag_enable_flags = cpu_to_le32(mode);
  2122. skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd),
  2123. &ppag_cmd, HCI_CMD_TIMEOUT);
  2124. if (IS_ERR(skb)) {
  2125. bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
  2126. return;
  2127. }
  2128. bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", mode);
  2129. kfree_skb(skb);
  2130. }
  2131. static int btintel_acpi_reset_method(struct hci_dev *hdev)
  2132. {
  2133. int ret = 0;
  2134. acpi_status status;
  2135. union acpi_object *p, *ref;
  2136. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  2137. status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
  2138. if (ACPI_FAILURE(status)) {
  2139. bt_dev_err(hdev, "Failed to run _PRR method");
  2140. ret = -ENODEV;
  2141. return ret;
  2142. }
  2143. p = buffer.pointer;
  2144. if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
  2145. bt_dev_err(hdev, "Invalid arguments");
  2146. ret = -EINVAL;
  2147. goto exit_on_error;
  2148. }
  2149. ref = &p->package.elements[0];
  2150. if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
  2151. bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
  2152. ret = -EINVAL;
  2153. goto exit_on_error;
  2154. }
  2155. status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
  2156. if (ACPI_FAILURE(status)) {
  2157. bt_dev_err(hdev, "Failed to run_RST method");
  2158. ret = -ENODEV;
  2159. goto exit_on_error;
  2160. }
  2161. exit_on_error:
  2162. kfree(buffer.pointer);
  2163. return ret;
  2164. }
  2165. static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
  2166. struct intel_version_tlv *ver_tlv)
  2167. {
  2168. struct btintel_data *data = hci_get_priv(hdev);
  2169. acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
  2170. u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
  2171. union acpi_object *obj, argv4;
  2172. enum {
  2173. RESET_TYPE_WDISABLE2,
  2174. RESET_TYPE_VSEC
  2175. };
  2176. handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
  2177. if (!handle) {
  2178. bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
  2179. return;
  2180. }
  2181. if (!acpi_has_method(handle, "_PRR")) {
  2182. bt_dev_err(hdev, "No support for _PRR ACPI method");
  2183. return;
  2184. }
  2185. switch (ver_tlv->cnvi_top & 0xfff) {
  2186. case 0x910: /* GalePeak2 */
  2187. reset_payload[2] = RESET_TYPE_VSEC;
  2188. break;
  2189. default:
  2190. /* WDISABLE2 is the default reset method */
  2191. reset_payload[2] = RESET_TYPE_WDISABLE2;
  2192. if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
  2193. BIT(DSM_SET_WDISABLE2_DELAY))) {
  2194. bt_dev_err(hdev, "No dsm support to set reset delay");
  2195. return;
  2196. }
  2197. argv4.integer.type = ACPI_TYPE_INTEGER;
  2198. /* delay required to toggle BT power */
  2199. argv4.integer.value = 160;
  2200. obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
  2201. DSM_SET_WDISABLE2_DELAY, &argv4);
  2202. if (!obj) {
  2203. bt_dev_err(hdev, "Failed to call dsm to set reset delay");
  2204. return;
  2205. }
  2206. ACPI_FREE(obj);
  2207. }
  2208. bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
  2209. if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
  2210. DSM_SET_RESET_METHOD)) {
  2211. bt_dev_warn(hdev, "No support for dsm to set reset method");
  2212. return;
  2213. }
  2214. argv4.buffer.type = ACPI_TYPE_BUFFER;
  2215. argv4.buffer.length = sizeof(reset_payload);
  2216. argv4.buffer.pointer = reset_payload;
  2217. obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
  2218. DSM_SET_RESET_METHOD, &argv4);
  2219. if (!obj) {
  2220. bt_dev_err(hdev, "Failed to call dsm to set reset method");
  2221. return;
  2222. }
  2223. ACPI_FREE(obj);
  2224. data->acpi_reset_method = btintel_acpi_reset_method;
  2225. }
  2226. #define BTINTEL_ISODATA_HANDLE_BASE 0x900
  2227. static u8 btintel_classify_pkt_type(struct hci_dev *hdev, struct sk_buff *skb)
  2228. {
  2229. /*
  2230. * Distinguish ISO data packets form ACL data packets
  2231. * based on their connection handle value range.
  2232. */
  2233. if (hci_skb_pkt_type(skb) == HCI_ACLDATA_PKT) {
  2234. __u16 handle = __le16_to_cpu(hci_acl_hdr(skb)->handle);
  2235. if (hci_handle(handle) >= BTINTEL_ISODATA_HANDLE_BASE)
  2236. return HCI_ISODATA_PKT;
  2237. }
  2238. return hci_skb_pkt_type(skb);
  2239. }
  2240. /*
  2241. * UefiCnvCommonDSBR UEFI variable provides information from the OEM platforms
  2242. * if they have replaced the BRI (Bluetooth Radio Interface) resistor to
  2243. * overcome the potential STEP errors on their designs. Based on the
  2244. * configauration, bluetooth firmware shall adjust the BRI response line drive
  2245. * strength. The below structure represents DSBR data.
  2246. * struct {
  2247. * u8 header;
  2248. * u32 dsbr;
  2249. * } __packed;
  2250. *
  2251. * header - defines revision number of the structure
  2252. * dsbr - defines drive strength BRI response
  2253. * bit0
  2254. * 0 - instructs bluetooth firmware to use default values
  2255. * 1 - instructs bluetooth firmware to override default values
  2256. * bit3:1
  2257. * Reserved
  2258. * bit7:4
  2259. * DSBR override values (only if bit0 is set. Default value is 0xF
  2260. * bit31:7
  2261. * Reserved
  2262. * Expected values for dsbr field:
  2263. * 1. 0xF1 - indicates that the resistor on board is 33 Ohm
  2264. * 2. 0x00 or 0xB1 - indicates that the resistor on board is 10 Ohm
  2265. * 3. Non existing UEFI variable or invalid (none of the above) - indicates
  2266. * that the resistor on board is 10 Ohm
  2267. * Even if uefi variable is not present, driver shall send 0xfc0a command to
  2268. * firmware to use default values.
  2269. *
  2270. */
  2271. static int btintel_uefi_get_dsbr(u32 *dsbr_var)
  2272. {
  2273. struct btintel_dsbr {
  2274. u8 header;
  2275. u32 dsbr;
  2276. } __packed data;
  2277. efi_status_t status;
  2278. unsigned long data_size = 0;
  2279. efi_guid_t guid = EFI_GUID(0xe65d8884, 0xd4af, 0x4b20, 0x8d, 0x03,
  2280. 0x77, 0x2e, 0xcc, 0x3d, 0xa5, 0x31);
  2281. if (!IS_ENABLED(CONFIG_EFI))
  2282. return -EOPNOTSUPP;
  2283. if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
  2284. return -EOPNOTSUPP;
  2285. status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
  2286. NULL);
  2287. if (status != EFI_BUFFER_TOO_SMALL || !data_size)
  2288. return -EIO;
  2289. status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
  2290. &data);
  2291. if (status != EFI_SUCCESS)
  2292. return -ENXIO;
  2293. *dsbr_var = data.dsbr;
  2294. return 0;
  2295. }
  2296. static int btintel_set_dsbr(struct hci_dev *hdev, struct intel_version_tlv *ver)
  2297. {
  2298. struct btintel_dsbr_cmd {
  2299. u8 enable;
  2300. u8 dsbr;
  2301. } __packed;
  2302. struct btintel_dsbr_cmd cmd;
  2303. struct sk_buff *skb;
  2304. u8 status;
  2305. u32 dsbr;
  2306. bool apply_dsbr;
  2307. int err;
  2308. /* DSBR command needs to be sent for BlazarI + B0 step product after
  2309. * downloading IML image.
  2310. */
  2311. apply_dsbr = (ver->img_type == BTINTEL_IMG_IML &&
  2312. ((ver->cnvi_top & 0xfff) == BTINTEL_CNVI_BLAZARI) &&
  2313. INTEL_CNVX_TOP_STEP(ver->cnvi_top) == 0x01);
  2314. if (!apply_dsbr)
  2315. return 0;
  2316. dsbr = 0;
  2317. err = btintel_uefi_get_dsbr(&dsbr);
  2318. if (err < 0)
  2319. bt_dev_dbg(hdev, "Error reading efi: %ls (%d)",
  2320. BTINTEL_EFI_DSBR, err);
  2321. cmd.enable = dsbr & BIT(0);
  2322. cmd.dsbr = dsbr >> 4 & 0xF;
  2323. bt_dev_info(hdev, "dsbr: enable: 0x%2.2x value: 0x%2.2x", cmd.enable,
  2324. cmd.dsbr);
  2325. skb = __hci_cmd_sync(hdev, 0xfc0a, sizeof(cmd), &cmd, HCI_CMD_TIMEOUT);
  2326. if (IS_ERR(skb))
  2327. return -bt_to_errno(PTR_ERR(skb));
  2328. status = skb->data[0];
  2329. kfree_skb(skb);
  2330. if (status)
  2331. return -bt_to_errno(status);
  2332. return 0;
  2333. }
  2334. int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
  2335. struct intel_version_tlv *ver)
  2336. {
  2337. u32 boot_param;
  2338. char ddcname[64];
  2339. int err;
  2340. struct intel_version_tlv new_ver;
  2341. bt_dev_dbg(hdev, "");
  2342. /* Set the default boot parameter to 0x0 and it is updated to
  2343. * SKU specific boot parameter after reading Intel_Write_Boot_Params
  2344. * command while downloading the firmware.
  2345. */
  2346. boot_param = 0x00000000;
  2347. btintel_set_flag(hdev, INTEL_BOOTLOADER);
  2348. err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
  2349. if (err)
  2350. return err;
  2351. /* check if controller is already having an operational firmware */
  2352. if (ver->img_type == BTINTEL_IMG_OP)
  2353. goto finish;
  2354. err = btintel_boot(hdev, boot_param);
  2355. if (err)
  2356. return err;
  2357. err = btintel_read_version_tlv(hdev, ver);
  2358. if (err)
  2359. return err;
  2360. /* set drive strength of BRI response */
  2361. err = btintel_set_dsbr(hdev, ver);
  2362. if (err) {
  2363. bt_dev_err(hdev, "Failed to send dsbr command (%d)", err);
  2364. return err;
  2365. }
  2366. /* If image type returned is BTINTEL_IMG_IML, then controller supports
  2367. * intermediate loader image
  2368. */
  2369. if (ver->img_type == BTINTEL_IMG_IML) {
  2370. err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
  2371. if (err)
  2372. return err;
  2373. err = btintel_boot(hdev, boot_param);
  2374. if (err)
  2375. return err;
  2376. }
  2377. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  2378. btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
  2379. /* Once the device is running in operational mode, it needs to
  2380. * apply the device configuration (DDC) parameters.
  2381. *
  2382. * The device can work without DDC parameters, so even if it
  2383. * fails to load the file, no need to fail the setup.
  2384. */
  2385. btintel_load_ddc_config(hdev, ddcname);
  2386. /* Read supported use cases and set callbacks to fetch datapath id */
  2387. btintel_configure_offload(hdev);
  2388. hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
  2389. /* Set PPAG feature */
  2390. btintel_set_ppag(hdev, ver);
  2391. /* Read the Intel version information after loading the FW */
  2392. err = btintel_read_version_tlv(hdev, &new_ver);
  2393. if (err)
  2394. return err;
  2395. btintel_version_info_tlv(hdev, &new_ver);
  2396. finish:
  2397. /* Set the event mask for Intel specific vendor events. This enables
  2398. * a few extra events that are useful during general operation. It
  2399. * does not enable any debugging related events.
  2400. *
  2401. * The device will function correctly without these events enabled
  2402. * and thus no need to fail the setup.
  2403. */
  2404. btintel_set_event_mask(hdev, false);
  2405. return 0;
  2406. }
  2407. EXPORT_SYMBOL_GPL(btintel_bootloader_setup_tlv);
  2408. void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
  2409. {
  2410. switch (hw_variant) {
  2411. /* Legacy bootloader devices that supports MSFT Extension */
  2412. case 0x11: /* JfP */
  2413. case 0x12: /* ThP */
  2414. case 0x13: /* HrP */
  2415. case 0x14: /* CcP */
  2416. /* All Intel new genration controllers support the Microsoft vendor
  2417. * extension are using 0xFC1E for VsMsftOpCode.
  2418. */
  2419. case 0x17:
  2420. case 0x18:
  2421. case 0x19:
  2422. case 0x1b:
  2423. case 0x1c:
  2424. case 0x1d:
  2425. case 0x1e:
  2426. hci_set_msft_opcode(hdev, 0xFC1E);
  2427. break;
  2428. default:
  2429. /* Not supported */
  2430. break;
  2431. }
  2432. }
  2433. EXPORT_SYMBOL_GPL(btintel_set_msft_opcode);
  2434. void btintel_print_fseq_info(struct hci_dev *hdev)
  2435. {
  2436. struct sk_buff *skb;
  2437. u8 *p;
  2438. u32 val;
  2439. const char *str;
  2440. skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT);
  2441. if (IS_ERR(skb)) {
  2442. bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)",
  2443. PTR_ERR(skb));
  2444. return;
  2445. }
  2446. if (skb->len < (sizeof(u32) * 16 + 2)) {
  2447. bt_dev_dbg(hdev, "Malformed packet of length %u received",
  2448. skb->len);
  2449. kfree_skb(skb);
  2450. return;
  2451. }
  2452. p = skb_pull_data(skb, 1);
  2453. if (*p) {
  2454. bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p);
  2455. kfree_skb(skb);
  2456. return;
  2457. }
  2458. p = skb_pull_data(skb, 1);
  2459. switch (*p) {
  2460. case 0:
  2461. str = "Success";
  2462. break;
  2463. case 1:
  2464. str = "Fatal error";
  2465. break;
  2466. case 2:
  2467. str = "Semaphore acquire error";
  2468. break;
  2469. default:
  2470. str = "Unknown error";
  2471. break;
  2472. }
  2473. if (*p) {
  2474. bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
  2475. kfree_skb(skb);
  2476. return;
  2477. }
  2478. bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
  2479. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2480. bt_dev_dbg(hdev, "Reason: 0x%8.8x", val);
  2481. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2482. bt_dev_dbg(hdev, "Global version: 0x%8.8x", val);
  2483. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2484. bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val);
  2485. p = skb->data;
  2486. skb_pull_data(skb, 4);
  2487. bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
  2488. p[2], p[3]);
  2489. p = skb->data;
  2490. skb_pull_data(skb, 4);
  2491. bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
  2492. p[2], p[3]);
  2493. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2494. bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val);
  2495. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2496. bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val);
  2497. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2498. bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val);
  2499. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2500. bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val);
  2501. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2502. bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val);
  2503. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2504. bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val);
  2505. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2506. bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val);
  2507. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2508. bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val);
  2509. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2510. bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val);
  2511. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2512. bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val);
  2513. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2514. bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val);
  2515. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2516. bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val);
  2517. val = get_unaligned_le32(skb_pull_data(skb, 4));
  2518. bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val);
  2519. kfree_skb(skb);
  2520. }
  2521. EXPORT_SYMBOL_GPL(btintel_print_fseq_info);
  2522. static int btintel_setup_combined(struct hci_dev *hdev)
  2523. {
  2524. const u8 param[1] = { 0xFF };
  2525. struct intel_version ver;
  2526. struct intel_version_tlv ver_tlv;
  2527. struct sk_buff *skb;
  2528. int err;
  2529. BT_DBG("%s", hdev->name);
  2530. /* The some controllers have a bug with the first HCI command sent to it
  2531. * returning number of completed commands as zero. This would stall the
  2532. * command processing in the Bluetooth core.
  2533. *
  2534. * As a workaround, send HCI Reset command first which will reset the
  2535. * number of completed commands and allow normal command processing
  2536. * from now on.
  2537. *
  2538. * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
  2539. * in the SW_RFKILL ON state as a workaround of fixing LED issue during
  2540. * the shutdown() procedure, and once the device is in SW_RFKILL ON
  2541. * state, the only way to exit out of it is sending the HCI_Reset
  2542. * command.
  2543. */
  2544. if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
  2545. btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
  2546. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
  2547. HCI_INIT_TIMEOUT);
  2548. if (IS_ERR(skb)) {
  2549. bt_dev_err(hdev,
  2550. "sending initial HCI reset failed (%ld)",
  2551. PTR_ERR(skb));
  2552. return PTR_ERR(skb);
  2553. }
  2554. kfree_skb(skb);
  2555. }
  2556. /* Starting from TyP device, the command parameter and response are
  2557. * changed even though the OCF for HCI_Intel_Read_Version command
  2558. * remains same. The legacy devices can handle even if the
  2559. * command has a parameter and returns a correct version information.
  2560. * So, it uses new format to support both legacy and new format.
  2561. */
  2562. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  2563. if (IS_ERR(skb)) {
  2564. bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
  2565. PTR_ERR(skb));
  2566. return PTR_ERR(skb);
  2567. }
  2568. /* Check the status */
  2569. if (skb->data[0]) {
  2570. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  2571. skb->data[0]);
  2572. err = -EIO;
  2573. goto exit_error;
  2574. }
  2575. /* Apply the common HCI quirks for Intel device */
  2576. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2577. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2578. set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
  2579. /* Set up the quality report callback for Intel devices */
  2580. hdev->set_quality_report = btintel_set_quality_report;
  2581. /* For Legacy device, check the HW platform value and size */
  2582. if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
  2583. bt_dev_dbg(hdev, "Read the legacy Intel version information");
  2584. memcpy(&ver, skb->data, sizeof(ver));
  2585. /* Display version information */
  2586. btintel_version_info(hdev, &ver);
  2587. /* Check for supported iBT hardware variants of this firmware
  2588. * loading method.
  2589. *
  2590. * This check has been put in place to ensure correct forward
  2591. * compatibility options when newer hardware variants come
  2592. * along.
  2593. */
  2594. switch (ver.hw_variant) {
  2595. case 0x07: /* WP */
  2596. case 0x08: /* StP */
  2597. /* Legacy ROM product */
  2598. btintel_set_flag(hdev, INTEL_ROM_LEGACY);
  2599. /* Apply the device specific HCI quirks
  2600. *
  2601. * WBS for SdP - For the Legacy ROM products, only SdP
  2602. * supports the WBS. But the version information is not
  2603. * enough to use here because the StP2 and SdP have same
  2604. * hw_variant and fw_variant. So, this flag is set by
  2605. * the transport driver (btusb) based on the HW info
  2606. * (idProduct)
  2607. */
  2608. if (!btintel_test_flag(hdev,
  2609. INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
  2610. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
  2611. &hdev->quirks);
  2612. err = btintel_legacy_rom_setup(hdev, &ver);
  2613. break;
  2614. case 0x0b: /* SfP */
  2615. case 0x11: /* JfP */
  2616. case 0x12: /* ThP */
  2617. case 0x13: /* HrP */
  2618. case 0x14: /* CcP */
  2619. fallthrough;
  2620. case 0x0c: /* WsP */
  2621. /* Apply the device specific HCI quirks
  2622. *
  2623. * All Legacy bootloader devices support WBS
  2624. */
  2625. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
  2626. &hdev->quirks);
  2627. /* These variants don't seem to support LE Coded PHY */
  2628. set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
  2629. /* Setup MSFT Extension support */
  2630. btintel_set_msft_opcode(hdev, ver.hw_variant);
  2631. err = btintel_bootloader_setup(hdev, &ver);
  2632. btintel_register_devcoredump_support(hdev);
  2633. break;
  2634. default:
  2635. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  2636. ver.hw_variant);
  2637. err = -EINVAL;
  2638. }
  2639. hci_set_hw_info(hdev,
  2640. "INTEL platform=%u variant=%u revision=%u",
  2641. ver.hw_platform, ver.hw_variant,
  2642. ver.hw_revision);
  2643. goto exit_error;
  2644. }
  2645. /* memset ver_tlv to start with clean state as few fields are exclusive
  2646. * to bootloader mode and are not populated in operational mode
  2647. */
  2648. memset(&ver_tlv, 0, sizeof(ver_tlv));
  2649. /* For TLV type device, parse the tlv data */
  2650. err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
  2651. if (err) {
  2652. bt_dev_err(hdev, "Failed to parse TLV version information");
  2653. goto exit_error;
  2654. }
  2655. if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
  2656. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  2657. INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
  2658. err = -EINVAL;
  2659. goto exit_error;
  2660. }
  2661. /* Check for supported iBT hardware variants of this firmware
  2662. * loading method.
  2663. *
  2664. * This check has been put in place to ensure correct forward
  2665. * compatibility options when newer hardware variants come
  2666. * along.
  2667. */
  2668. switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
  2669. case 0x11: /* JfP */
  2670. case 0x12: /* ThP */
  2671. case 0x13: /* HrP */
  2672. case 0x14: /* CcP */
  2673. /* Some legacy bootloader devices starting from JfP,
  2674. * the operational firmware supports both old and TLV based
  2675. * HCI_Intel_Read_Version command based on the command
  2676. * parameter.
  2677. *
  2678. * For upgrading firmware case, the TLV based version cannot
  2679. * be used because the firmware filename for legacy bootloader
  2680. * is based on the old format.
  2681. *
  2682. * Also, it is not easy to convert TLV based version from the
  2683. * legacy version format.
  2684. *
  2685. * So, as a workaround for those devices, use the legacy
  2686. * HCI_Intel_Read_Version to get the version information and
  2687. * run the legacy bootloader setup.
  2688. */
  2689. err = btintel_read_version(hdev, &ver);
  2690. if (err)
  2691. break;
  2692. /* Apply the device specific HCI quirks
  2693. *
  2694. * All Legacy bootloader devices support WBS
  2695. */
  2696. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  2697. /* These variants don't seem to support LE Coded PHY */
  2698. set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
  2699. /* Setup MSFT Extension support */
  2700. btintel_set_msft_opcode(hdev, ver.hw_variant);
  2701. err = btintel_bootloader_setup(hdev, &ver);
  2702. btintel_register_devcoredump_support(hdev);
  2703. break;
  2704. case 0x18: /* GfP2 */
  2705. case 0x1c: /* GaP */
  2706. /* Re-classify packet type for controllers with LE audio */
  2707. hdev->classify_pkt_type = btintel_classify_pkt_type;
  2708. fallthrough;
  2709. case 0x17:
  2710. case 0x19:
  2711. case 0x1b:
  2712. case 0x1d:
  2713. case 0x1e:
  2714. /* Display version information of TLV type */
  2715. btintel_version_info_tlv(hdev, &ver_tlv);
  2716. /* Apply the device specific HCI quirks for TLV based devices
  2717. *
  2718. * All TLV based devices support WBS
  2719. */
  2720. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  2721. /* Setup MSFT Extension support */
  2722. btintel_set_msft_opcode(hdev,
  2723. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  2724. btintel_set_dsm_reset_method(hdev, &ver_tlv);
  2725. err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
  2726. if (err)
  2727. goto exit_error;
  2728. btintel_register_devcoredump_support(hdev);
  2729. btintel_print_fseq_info(hdev);
  2730. break;
  2731. default:
  2732. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  2733. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  2734. err = -EINVAL;
  2735. break;
  2736. }
  2737. hci_set_hw_info(hdev, "INTEL platform=%u variant=%u",
  2738. INTEL_HW_PLATFORM(ver_tlv.cnvi_bt),
  2739. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  2740. exit_error:
  2741. kfree_skb(skb);
  2742. return err;
  2743. }
  2744. int btintel_shutdown_combined(struct hci_dev *hdev)
  2745. {
  2746. struct sk_buff *skb;
  2747. int ret;
  2748. /* Send HCI Reset to the controller to stop any BT activity which
  2749. * were triggered. This will help to save power and maintain the
  2750. * sync b/w Host and controller
  2751. */
  2752. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  2753. if (IS_ERR(skb)) {
  2754. bt_dev_err(hdev, "HCI reset during shutdown failed");
  2755. return PTR_ERR(skb);
  2756. }
  2757. kfree_skb(skb);
  2758. /* Some platforms have an issue with BT LED when the interface is
  2759. * down or BT radio is turned off, which takes 5 seconds to BT LED
  2760. * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
  2761. * device in the RFKILL ON state which turns off the BT LED immediately.
  2762. */
  2763. if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
  2764. skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
  2765. if (IS_ERR(skb)) {
  2766. ret = PTR_ERR(skb);
  2767. bt_dev_err(hdev, "turning off Intel device LED failed");
  2768. return ret;
  2769. }
  2770. kfree_skb(skb);
  2771. }
  2772. return 0;
  2773. }
  2774. EXPORT_SYMBOL_GPL(btintel_shutdown_combined);
  2775. int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
  2776. {
  2777. hdev->manufacturer = 2;
  2778. hdev->setup = btintel_setup_combined;
  2779. hdev->shutdown = btintel_shutdown_combined;
  2780. hdev->hw_error = btintel_hw_error;
  2781. hdev->set_diag = btintel_set_diag_combined;
  2782. hdev->set_bdaddr = btintel_set_bdaddr;
  2783. coredump_info.driver_name = driver_name;
  2784. return 0;
  2785. }
  2786. EXPORT_SYMBOL_GPL(btintel_configure_setup);
  2787. int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
  2788. {
  2789. struct intel_tlv *tlv = (void *)&skb->data[5];
  2790. /* The first event is always an event type TLV */
  2791. if (tlv->type != INTEL_TLV_TYPE_ID)
  2792. goto recv_frame;
  2793. switch (tlv->val[0]) {
  2794. case INTEL_TLV_SYSTEM_EXCEPTION:
  2795. case INTEL_TLV_FATAL_EXCEPTION:
  2796. case INTEL_TLV_DEBUG_EXCEPTION:
  2797. case INTEL_TLV_TEST_EXCEPTION:
  2798. /* Generate devcoredump from exception */
  2799. if (!hci_devcd_init(hdev, skb->len)) {
  2800. hci_devcd_append(hdev, skb_clone(skb, GFP_ATOMIC));
  2801. hci_devcd_complete(hdev);
  2802. } else {
  2803. bt_dev_err(hdev, "Failed to generate devcoredump");
  2804. }
  2805. break;
  2806. default:
  2807. bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
  2808. }
  2809. recv_frame:
  2810. return hci_recv_frame(hdev, skb);
  2811. }
  2812. EXPORT_SYMBOL_GPL(btintel_diagnostics);
  2813. int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  2814. {
  2815. struct hci_event_hdr *hdr = (void *)skb->data;
  2816. const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
  2817. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  2818. hdr->plen > 0) {
  2819. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  2820. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  2821. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  2822. switch (skb->data[2]) {
  2823. case 0x02:
  2824. /* When switching to the operational firmware
  2825. * the device sends a vendor specific event
  2826. * indicating that the bootup completed.
  2827. */
  2828. btintel_bootup(hdev, ptr, len);
  2829. kfree_skb(skb);
  2830. return 0;
  2831. case 0x06:
  2832. /* When the firmware loading completes the
  2833. * device sends out a vendor specific event
  2834. * indicating the result of the firmware
  2835. * loading.
  2836. */
  2837. btintel_secure_send_result(hdev, ptr, len);
  2838. kfree_skb(skb);
  2839. return 0;
  2840. }
  2841. }
  2842. /* Handle all diagnostics events separately. May still call
  2843. * hci_recv_frame.
  2844. */
  2845. if (len >= sizeof(diagnostics_hdr) &&
  2846. memcmp(&skb->data[2], diagnostics_hdr,
  2847. sizeof(diagnostics_hdr)) == 0) {
  2848. return btintel_diagnostics(hdev, skb);
  2849. }
  2850. }
  2851. return hci_recv_frame(hdev, skb);
  2852. }
  2853. EXPORT_SYMBOL_GPL(btintel_recv_event);
  2854. void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
  2855. {
  2856. const struct intel_bootup *evt = ptr;
  2857. if (len != sizeof(*evt))
  2858. return;
  2859. if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
  2860. btintel_wake_up_flag(hdev, INTEL_BOOTING);
  2861. }
  2862. EXPORT_SYMBOL_GPL(btintel_bootup);
  2863. void btintel_secure_send_result(struct hci_dev *hdev,
  2864. const void *ptr, unsigned int len)
  2865. {
  2866. const struct intel_secure_send_result *evt = ptr;
  2867. if (len != sizeof(*evt))
  2868. return;
  2869. if (evt->result)
  2870. btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
  2871. if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
  2872. btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
  2873. btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
  2874. }
  2875. EXPORT_SYMBOL_GPL(btintel_secure_send_result);
  2876. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  2877. MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
  2878. MODULE_VERSION(VERSION);
  2879. MODULE_LICENSE("GPL");
  2880. MODULE_FIRMWARE("intel/ibt-11-5.sfi");
  2881. MODULE_FIRMWARE("intel/ibt-11-5.ddc");
  2882. MODULE_FIRMWARE("intel/ibt-12-16.sfi");
  2883. MODULE_FIRMWARE("intel/ibt-12-16.ddc");