btintel.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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 <asm/unaligned.h>
  12. #include <net/bluetooth/bluetooth.h>
  13. #include <net/bluetooth/hci_core.h>
  14. #include "btintel.h"
  15. #define VERSION "0.1"
  16. #define BDADDR_INTEL (&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
  17. #define RSA_HEADER_LEN 644
  18. #define CSS_HEADER_OFFSET 8
  19. #define ECDSA_OFFSET 644
  20. #define ECDSA_HEADER_LEN 320
  21. #define CMD_WRITE_BOOT_PARAMS 0xfc0e
  22. struct cmd_write_boot_params {
  23. u32 boot_addr;
  24. u8 fw_build_num;
  25. u8 fw_build_ww;
  26. u8 fw_build_yy;
  27. } __packed;
  28. int btintel_check_bdaddr(struct hci_dev *hdev)
  29. {
  30. struct hci_rp_read_bd_addr *bda;
  31. struct sk_buff *skb;
  32. skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
  33. HCI_INIT_TIMEOUT);
  34. if (IS_ERR(skb)) {
  35. int err = PTR_ERR(skb);
  36. bt_dev_err(hdev, "Reading Intel device address failed (%d)",
  37. err);
  38. return err;
  39. }
  40. if (skb->len != sizeof(*bda)) {
  41. bt_dev_err(hdev, "Intel device address length mismatch");
  42. kfree_skb(skb);
  43. return -EIO;
  44. }
  45. bda = (struct hci_rp_read_bd_addr *)skb->data;
  46. /* For some Intel based controllers, the default Bluetooth device
  47. * address 00:03:19:9E:8B:00 can be found. These controllers are
  48. * fully operational, but have the danger of duplicate addresses
  49. * and that in turn can cause problems with Bluetooth operation.
  50. */
  51. if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
  52. bt_dev_err(hdev, "Found Intel default device address (%pMR)",
  53. &bda->bdaddr);
  54. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  55. }
  56. kfree_skb(skb);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
  60. int btintel_enter_mfg(struct hci_dev *hdev)
  61. {
  62. static const u8 param[] = { 0x01, 0x00 };
  63. struct sk_buff *skb;
  64. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
  65. if (IS_ERR(skb)) {
  66. bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
  67. PTR_ERR(skb));
  68. return PTR_ERR(skb);
  69. }
  70. kfree_skb(skb);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(btintel_enter_mfg);
  74. int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
  75. {
  76. u8 param[] = { 0x00, 0x00 };
  77. struct sk_buff *skb;
  78. /* The 2nd command parameter specifies the manufacturing exit method:
  79. * 0x00: Just disable the manufacturing mode (0x00).
  80. * 0x01: Disable manufacturing mode and reset with patches deactivated.
  81. * 0x02: Disable manufacturing mode and reset with patches activated.
  82. */
  83. if (reset)
  84. param[1] |= patched ? 0x02 : 0x01;
  85. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
  86. if (IS_ERR(skb)) {
  87. bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
  88. PTR_ERR(skb));
  89. return PTR_ERR(skb);
  90. }
  91. kfree_skb(skb);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(btintel_exit_mfg);
  95. int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  96. {
  97. struct sk_buff *skb;
  98. int err;
  99. skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
  100. if (IS_ERR(skb)) {
  101. err = PTR_ERR(skb);
  102. bt_dev_err(hdev, "Changing Intel device address failed (%d)",
  103. err);
  104. return err;
  105. }
  106. kfree_skb(skb);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
  110. static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
  111. {
  112. u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  113. struct sk_buff *skb;
  114. int err;
  115. if (debug)
  116. mask[1] |= 0x62;
  117. skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
  118. if (IS_ERR(skb)) {
  119. err = PTR_ERR(skb);
  120. bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
  121. return err;
  122. }
  123. kfree_skb(skb);
  124. return 0;
  125. }
  126. int btintel_set_diag(struct hci_dev *hdev, bool enable)
  127. {
  128. struct sk_buff *skb;
  129. u8 param[3];
  130. int err;
  131. if (enable) {
  132. param[0] = 0x03;
  133. param[1] = 0x03;
  134. param[2] = 0x03;
  135. } else {
  136. param[0] = 0x00;
  137. param[1] = 0x00;
  138. param[2] = 0x00;
  139. }
  140. skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
  141. if (IS_ERR(skb)) {
  142. err = PTR_ERR(skb);
  143. if (err == -ENODATA)
  144. goto done;
  145. bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
  146. err);
  147. return err;
  148. }
  149. kfree_skb(skb);
  150. done:
  151. btintel_set_event_mask(hdev, enable);
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(btintel_set_diag);
  155. static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
  156. {
  157. int err, ret;
  158. err = btintel_enter_mfg(hdev);
  159. if (err)
  160. return err;
  161. ret = btintel_set_diag(hdev, enable);
  162. err = btintel_exit_mfg(hdev, false, false);
  163. if (err)
  164. return err;
  165. return ret;
  166. }
  167. static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
  168. {
  169. int ret;
  170. /* Legacy ROM device needs to be in the manufacturer mode to apply
  171. * diagnostic setting
  172. *
  173. * This flag is set after reading the Intel version.
  174. */
  175. if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
  176. ret = btintel_set_diag_mfg(hdev, enable);
  177. else
  178. ret = btintel_set_diag(hdev, enable);
  179. return ret;
  180. }
  181. static void btintel_hw_error(struct hci_dev *hdev, u8 code)
  182. {
  183. struct sk_buff *skb;
  184. u8 type = 0x00;
  185. bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
  186. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  187. if (IS_ERR(skb)) {
  188. bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
  189. PTR_ERR(skb));
  190. return;
  191. }
  192. kfree_skb(skb);
  193. skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
  194. if (IS_ERR(skb)) {
  195. bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
  196. PTR_ERR(skb));
  197. return;
  198. }
  199. if (skb->len != 13) {
  200. bt_dev_err(hdev, "Exception info size mismatch");
  201. kfree_skb(skb);
  202. return;
  203. }
  204. bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
  205. kfree_skb(skb);
  206. }
  207. int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
  208. {
  209. const char *variant;
  210. /* The hardware platform number has a fixed value of 0x37 and
  211. * for now only accept this single value.
  212. */
  213. if (ver->hw_platform != 0x37) {
  214. bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
  215. ver->hw_platform);
  216. return -EINVAL;
  217. }
  218. /* Check for supported iBT hardware variants of this firmware
  219. * loading method.
  220. *
  221. * This check has been put in place to ensure correct forward
  222. * compatibility options when newer hardware variants come along.
  223. */
  224. switch (ver->hw_variant) {
  225. case 0x07: /* WP - Legacy ROM */
  226. case 0x08: /* StP - Legacy ROM */
  227. case 0x0b: /* SfP */
  228. case 0x0c: /* WsP */
  229. case 0x11: /* JfP */
  230. case 0x12: /* ThP */
  231. case 0x13: /* HrP */
  232. case 0x14: /* CcP */
  233. break;
  234. default:
  235. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  236. ver->hw_variant);
  237. return -EINVAL;
  238. }
  239. switch (ver->fw_variant) {
  240. case 0x01:
  241. variant = "Legacy ROM 2.5";
  242. break;
  243. case 0x06:
  244. variant = "Bootloader";
  245. break;
  246. case 0x22:
  247. variant = "Legacy ROM 2.x";
  248. break;
  249. case 0x23:
  250. variant = "Firmware";
  251. break;
  252. default:
  253. bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
  254. return -EINVAL;
  255. }
  256. bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
  257. variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
  258. ver->fw_build_num, ver->fw_build_ww,
  259. 2000 + ver->fw_build_yy);
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(btintel_version_info);
  263. static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
  264. const void *param)
  265. {
  266. while (plen > 0) {
  267. struct sk_buff *skb;
  268. u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
  269. cmd_param[0] = fragment_type;
  270. memcpy(cmd_param + 1, param, fragment_len);
  271. skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
  272. cmd_param, HCI_INIT_TIMEOUT);
  273. if (IS_ERR(skb))
  274. return PTR_ERR(skb);
  275. kfree_skb(skb);
  276. plen -= fragment_len;
  277. param += fragment_len;
  278. }
  279. return 0;
  280. }
  281. int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
  282. {
  283. const struct firmware *fw;
  284. struct sk_buff *skb;
  285. const u8 *fw_ptr;
  286. int err;
  287. err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
  288. if (err < 0) {
  289. bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
  290. ddc_name, err);
  291. return err;
  292. }
  293. bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
  294. fw_ptr = fw->data;
  295. /* DDC file contains one or more DDC structure which has
  296. * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
  297. */
  298. while (fw->size > fw_ptr - fw->data) {
  299. u8 cmd_plen = fw_ptr[0] + sizeof(u8);
  300. skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
  301. HCI_INIT_TIMEOUT);
  302. if (IS_ERR(skb)) {
  303. bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
  304. PTR_ERR(skb));
  305. release_firmware(fw);
  306. return PTR_ERR(skb);
  307. }
  308. fw_ptr += cmd_plen;
  309. kfree_skb(skb);
  310. }
  311. release_firmware(fw);
  312. bt_dev_info(hdev, "Applying Intel DDC parameters completed");
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
  316. int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
  317. {
  318. int err, ret;
  319. err = btintel_enter_mfg(hdev);
  320. if (err)
  321. return err;
  322. ret = btintel_set_event_mask(hdev, debug);
  323. err = btintel_exit_mfg(hdev, false, false);
  324. if (err)
  325. return err;
  326. return ret;
  327. }
  328. EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
  329. int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
  330. {
  331. struct sk_buff *skb;
  332. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
  333. if (IS_ERR(skb)) {
  334. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  335. PTR_ERR(skb));
  336. return PTR_ERR(skb);
  337. }
  338. if (skb->len != sizeof(*ver)) {
  339. bt_dev_err(hdev, "Intel version event size mismatch");
  340. kfree_skb(skb);
  341. return -EILSEQ;
  342. }
  343. memcpy(ver, skb->data, sizeof(*ver));
  344. kfree_skb(skb);
  345. return 0;
  346. }
  347. EXPORT_SYMBOL_GPL(btintel_read_version);
  348. static int btintel_version_info_tlv(struct hci_dev *hdev,
  349. struct intel_version_tlv *version)
  350. {
  351. const char *variant;
  352. /* The hardware platform number has a fixed value of 0x37 and
  353. * for now only accept this single value.
  354. */
  355. if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
  356. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  357. INTEL_HW_PLATFORM(version->cnvi_bt));
  358. return -EINVAL;
  359. }
  360. /* Check for supported iBT hardware variants of this firmware
  361. * loading method.
  362. *
  363. * This check has been put in place to ensure correct forward
  364. * compatibility options when newer hardware variants come along.
  365. */
  366. switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
  367. case 0x17: /* TyP */
  368. case 0x18: /* Slr */
  369. case 0x19: /* Slr-F */
  370. case 0x1b: /* Mgr */
  371. break;
  372. default:
  373. bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
  374. INTEL_HW_VARIANT(version->cnvi_bt));
  375. return -EINVAL;
  376. }
  377. switch (version->img_type) {
  378. case 0x01:
  379. variant = "Bootloader";
  380. /* It is required that every single firmware fragment is acknowledged
  381. * with a command complete event. If the boot parameters indicate
  382. * that this bootloader does not send them, then abort the setup.
  383. */
  384. if (version->limited_cce != 0x00) {
  385. bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
  386. version->limited_cce);
  387. return -EINVAL;
  388. }
  389. /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
  390. if (version->sbe_type > 0x01) {
  391. bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
  392. version->sbe_type);
  393. return -EINVAL;
  394. }
  395. bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
  396. bt_dev_info(hdev, "Secure boot is %s",
  397. version->secure_boot ? "enabled" : "disabled");
  398. bt_dev_info(hdev, "OTP lock is %s",
  399. version->otp_lock ? "enabled" : "disabled");
  400. bt_dev_info(hdev, "API lock is %s",
  401. version->api_lock ? "enabled" : "disabled");
  402. bt_dev_info(hdev, "Debug lock is %s",
  403. version->debug_lock ? "enabled" : "disabled");
  404. bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
  405. version->min_fw_build_nn, version->min_fw_build_cw,
  406. 2000 + version->min_fw_build_yy);
  407. break;
  408. case 0x03:
  409. variant = "Firmware";
  410. break;
  411. default:
  412. bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
  413. return -EINVAL;
  414. }
  415. bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
  416. 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
  417. version->build_type, version->build_num);
  418. return 0;
  419. }
  420. static int btintel_parse_version_tlv(struct hci_dev *hdev,
  421. struct intel_version_tlv *version,
  422. struct sk_buff *skb)
  423. {
  424. /* Consume Command Complete Status field */
  425. skb_pull(skb, 1);
  426. /* Event parameters contatin multiple TLVs. Read each of them
  427. * and only keep the required data. Also, it use existing legacy
  428. * version field like hw_platform, hw_variant, and fw_variant
  429. * to keep the existing setup flow
  430. */
  431. while (skb->len) {
  432. struct intel_tlv *tlv;
  433. /* Make sure skb has a minimum length of the header */
  434. if (skb->len < sizeof(*tlv))
  435. return -EINVAL;
  436. tlv = (struct intel_tlv *)skb->data;
  437. /* Make sure skb has a enough data */
  438. if (skb->len < tlv->len + sizeof(*tlv))
  439. return -EINVAL;
  440. switch (tlv->type) {
  441. case INTEL_TLV_CNVI_TOP:
  442. version->cnvi_top = get_unaligned_le32(tlv->val);
  443. break;
  444. case INTEL_TLV_CNVR_TOP:
  445. version->cnvr_top = get_unaligned_le32(tlv->val);
  446. break;
  447. case INTEL_TLV_CNVI_BT:
  448. version->cnvi_bt = get_unaligned_le32(tlv->val);
  449. break;
  450. case INTEL_TLV_CNVR_BT:
  451. version->cnvr_bt = get_unaligned_le32(tlv->val);
  452. break;
  453. case INTEL_TLV_DEV_REV_ID:
  454. version->dev_rev_id = get_unaligned_le16(tlv->val);
  455. break;
  456. case INTEL_TLV_IMAGE_TYPE:
  457. version->img_type = tlv->val[0];
  458. break;
  459. case INTEL_TLV_TIME_STAMP:
  460. /* If image type is Operational firmware (0x03), then
  461. * running FW Calendar Week and Year information can
  462. * be extracted from Timestamp information
  463. */
  464. version->min_fw_build_cw = tlv->val[0];
  465. version->min_fw_build_yy = tlv->val[1];
  466. version->timestamp = get_unaligned_le16(tlv->val);
  467. break;
  468. case INTEL_TLV_BUILD_TYPE:
  469. version->build_type = tlv->val[0];
  470. break;
  471. case INTEL_TLV_BUILD_NUM:
  472. /* If image type is Operational firmware (0x03), then
  473. * running FW build number can be extracted from the
  474. * Build information
  475. */
  476. version->min_fw_build_nn = tlv->val[0];
  477. version->build_num = get_unaligned_le32(tlv->val);
  478. break;
  479. case INTEL_TLV_SECURE_BOOT:
  480. version->secure_boot = tlv->val[0];
  481. break;
  482. case INTEL_TLV_OTP_LOCK:
  483. version->otp_lock = tlv->val[0];
  484. break;
  485. case INTEL_TLV_API_LOCK:
  486. version->api_lock = tlv->val[0];
  487. break;
  488. case INTEL_TLV_DEBUG_LOCK:
  489. version->debug_lock = tlv->val[0];
  490. break;
  491. case INTEL_TLV_MIN_FW:
  492. version->min_fw_build_nn = tlv->val[0];
  493. version->min_fw_build_cw = tlv->val[1];
  494. version->min_fw_build_yy = tlv->val[2];
  495. break;
  496. case INTEL_TLV_LIMITED_CCE:
  497. version->limited_cce = tlv->val[0];
  498. break;
  499. case INTEL_TLV_SBE_TYPE:
  500. version->sbe_type = tlv->val[0];
  501. break;
  502. case INTEL_TLV_OTP_BDADDR:
  503. memcpy(&version->otp_bd_addr, tlv->val,
  504. sizeof(bdaddr_t));
  505. break;
  506. default:
  507. /* Ignore rest of information */
  508. break;
  509. }
  510. /* consume the current tlv and move to next*/
  511. skb_pull(skb, tlv->len + sizeof(*tlv));
  512. }
  513. return 0;
  514. }
  515. static int btintel_read_version_tlv(struct hci_dev *hdev,
  516. struct intel_version_tlv *version)
  517. {
  518. struct sk_buff *skb;
  519. const u8 param[1] = { 0xFF };
  520. if (!version)
  521. return -EINVAL;
  522. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  523. if (IS_ERR(skb)) {
  524. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  525. PTR_ERR(skb));
  526. return PTR_ERR(skb);
  527. }
  528. if (skb->data[0]) {
  529. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  530. skb->data[0]);
  531. kfree_skb(skb);
  532. return -EIO;
  533. }
  534. btintel_parse_version_tlv(hdev, version, skb);
  535. kfree_skb(skb);
  536. return 0;
  537. }
  538. /* ------- REGMAP IBT SUPPORT ------- */
  539. #define IBT_REG_MODE_8BIT 0x00
  540. #define IBT_REG_MODE_16BIT 0x01
  541. #define IBT_REG_MODE_32BIT 0x02
  542. struct regmap_ibt_context {
  543. struct hci_dev *hdev;
  544. __u16 op_write;
  545. __u16 op_read;
  546. };
  547. struct ibt_cp_reg_access {
  548. __le32 addr;
  549. __u8 mode;
  550. __u8 len;
  551. __u8 data[];
  552. } __packed;
  553. struct ibt_rp_reg_access {
  554. __u8 status;
  555. __le32 addr;
  556. __u8 data[];
  557. } __packed;
  558. static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
  559. void *val, size_t val_size)
  560. {
  561. struct regmap_ibt_context *ctx = context;
  562. struct ibt_cp_reg_access cp;
  563. struct ibt_rp_reg_access *rp;
  564. struct sk_buff *skb;
  565. int err = 0;
  566. if (reg_size != sizeof(__le32))
  567. return -EINVAL;
  568. switch (val_size) {
  569. case 1:
  570. cp.mode = IBT_REG_MODE_8BIT;
  571. break;
  572. case 2:
  573. cp.mode = IBT_REG_MODE_16BIT;
  574. break;
  575. case 4:
  576. cp.mode = IBT_REG_MODE_32BIT;
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. /* regmap provides a little-endian formatted addr */
  582. cp.addr = *(__le32 *)addr;
  583. cp.len = val_size;
  584. bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
  585. skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
  586. HCI_CMD_TIMEOUT);
  587. if (IS_ERR(skb)) {
  588. err = PTR_ERR(skb);
  589. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
  590. le32_to_cpu(cp.addr), err);
  591. return err;
  592. }
  593. if (skb->len != sizeof(*rp) + val_size) {
  594. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
  595. le32_to_cpu(cp.addr));
  596. err = -EINVAL;
  597. goto done;
  598. }
  599. rp = (struct ibt_rp_reg_access *)skb->data;
  600. if (rp->addr != cp.addr) {
  601. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
  602. le32_to_cpu(rp->addr));
  603. err = -EINVAL;
  604. goto done;
  605. }
  606. memcpy(val, rp->data, val_size);
  607. done:
  608. kfree_skb(skb);
  609. return err;
  610. }
  611. static int regmap_ibt_gather_write(void *context,
  612. const void *addr, size_t reg_size,
  613. const void *val, size_t val_size)
  614. {
  615. struct regmap_ibt_context *ctx = context;
  616. struct ibt_cp_reg_access *cp;
  617. struct sk_buff *skb;
  618. int plen = sizeof(*cp) + val_size;
  619. u8 mode;
  620. int err = 0;
  621. if (reg_size != sizeof(__le32))
  622. return -EINVAL;
  623. switch (val_size) {
  624. case 1:
  625. mode = IBT_REG_MODE_8BIT;
  626. break;
  627. case 2:
  628. mode = IBT_REG_MODE_16BIT;
  629. break;
  630. case 4:
  631. mode = IBT_REG_MODE_32BIT;
  632. break;
  633. default:
  634. return -EINVAL;
  635. }
  636. cp = kmalloc(plen, GFP_KERNEL);
  637. if (!cp)
  638. return -ENOMEM;
  639. /* regmap provides a little-endian formatted addr/value */
  640. cp->addr = *(__le32 *)addr;
  641. cp->mode = mode;
  642. cp->len = val_size;
  643. memcpy(&cp->data, val, val_size);
  644. bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
  645. skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
  646. if (IS_ERR(skb)) {
  647. err = PTR_ERR(skb);
  648. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
  649. le32_to_cpu(cp->addr), err);
  650. goto done;
  651. }
  652. kfree_skb(skb);
  653. done:
  654. kfree(cp);
  655. return err;
  656. }
  657. static int regmap_ibt_write(void *context, const void *data, size_t count)
  658. {
  659. /* data contains register+value, since we only support 32bit addr,
  660. * minimum data size is 4 bytes.
  661. */
  662. if (WARN_ONCE(count < 4, "Invalid register access"))
  663. return -EINVAL;
  664. return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
  665. }
  666. static void regmap_ibt_free_context(void *context)
  667. {
  668. kfree(context);
  669. }
  670. static const struct regmap_bus regmap_ibt = {
  671. .read = regmap_ibt_read,
  672. .write = regmap_ibt_write,
  673. .gather_write = regmap_ibt_gather_write,
  674. .free_context = regmap_ibt_free_context,
  675. .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
  676. .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
  677. };
  678. /* Config is the same for all register regions */
  679. static const struct regmap_config regmap_ibt_cfg = {
  680. .name = "btintel_regmap",
  681. .reg_bits = 32,
  682. .val_bits = 32,
  683. };
  684. struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
  685. u16 opcode_write)
  686. {
  687. struct regmap_ibt_context *ctx;
  688. bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
  689. opcode_write);
  690. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  691. if (!ctx)
  692. return ERR_PTR(-ENOMEM);
  693. ctx->op_read = opcode_read;
  694. ctx->op_write = opcode_write;
  695. ctx->hdev = hdev;
  696. return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
  697. }
  698. EXPORT_SYMBOL_GPL(btintel_regmap_init);
  699. int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
  700. {
  701. struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
  702. struct sk_buff *skb;
  703. params.boot_param = cpu_to_le32(boot_param);
  704. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
  705. HCI_INIT_TIMEOUT);
  706. if (IS_ERR(skb)) {
  707. bt_dev_err(hdev, "Failed to send Intel Reset command");
  708. return PTR_ERR(skb);
  709. }
  710. kfree_skb(skb);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
  714. int btintel_read_boot_params(struct hci_dev *hdev,
  715. struct intel_boot_params *params)
  716. {
  717. struct sk_buff *skb;
  718. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  719. if (IS_ERR(skb)) {
  720. bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
  721. PTR_ERR(skb));
  722. return PTR_ERR(skb);
  723. }
  724. if (skb->len != sizeof(*params)) {
  725. bt_dev_err(hdev, "Intel boot parameters size mismatch");
  726. kfree_skb(skb);
  727. return -EILSEQ;
  728. }
  729. memcpy(params, skb->data, sizeof(*params));
  730. kfree_skb(skb);
  731. if (params->status) {
  732. bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
  733. params->status);
  734. return -bt_to_errno(params->status);
  735. }
  736. bt_dev_info(hdev, "Device revision is %u",
  737. le16_to_cpu(params->dev_revid));
  738. bt_dev_info(hdev, "Secure boot is %s",
  739. params->secure_boot ? "enabled" : "disabled");
  740. bt_dev_info(hdev, "OTP lock is %s",
  741. params->otp_lock ? "enabled" : "disabled");
  742. bt_dev_info(hdev, "API lock is %s",
  743. params->api_lock ? "enabled" : "disabled");
  744. bt_dev_info(hdev, "Debug lock is %s",
  745. params->debug_lock ? "enabled" : "disabled");
  746. bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
  747. params->min_fw_build_nn, params->min_fw_build_cw,
  748. 2000 + params->min_fw_build_yy);
  749. return 0;
  750. }
  751. EXPORT_SYMBOL_GPL(btintel_read_boot_params);
  752. static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
  753. const struct firmware *fw)
  754. {
  755. int err;
  756. /* Start the firmware download transaction with the Init fragment
  757. * represented by the 128 bytes of CSS header.
  758. */
  759. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  760. if (err < 0) {
  761. bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
  762. goto done;
  763. }
  764. /* Send the 256 bytes of public key information from the firmware
  765. * as the PKey fragment.
  766. */
  767. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  768. if (err < 0) {
  769. bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
  770. goto done;
  771. }
  772. /* Send the 256 bytes of signature information from the firmware
  773. * as the Sign fragment.
  774. */
  775. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  776. if (err < 0) {
  777. bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
  778. goto done;
  779. }
  780. done:
  781. return err;
  782. }
  783. static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
  784. const struct firmware *fw)
  785. {
  786. int err;
  787. /* Start the firmware download transaction with the Init fragment
  788. * represented by the 128 bytes of CSS header.
  789. */
  790. err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
  791. if (err < 0) {
  792. bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
  793. return err;
  794. }
  795. /* Send the 96 bytes of public key information from the firmware
  796. * as the PKey fragment.
  797. */
  798. err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
  799. if (err < 0) {
  800. bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
  801. return err;
  802. }
  803. /* Send the 96 bytes of signature information from the firmware
  804. * as the Sign fragment
  805. */
  806. err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
  807. if (err < 0) {
  808. bt_dev_err(hdev, "Failed to send firmware signature (%d)",
  809. err);
  810. return err;
  811. }
  812. return 0;
  813. }
  814. static int btintel_download_firmware_payload(struct hci_dev *hdev,
  815. const struct firmware *fw,
  816. size_t offset)
  817. {
  818. int err;
  819. const u8 *fw_ptr;
  820. u32 frag_len;
  821. fw_ptr = fw->data + offset;
  822. frag_len = 0;
  823. err = -EINVAL;
  824. while (fw_ptr - fw->data < fw->size) {
  825. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  826. frag_len += sizeof(*cmd) + cmd->plen;
  827. /* The parameter length of the secure send command requires
  828. * a 4 byte alignment. It happens so that the firmware file
  829. * contains proper Intel_NOP commands to align the fragments
  830. * as needed.
  831. *
  832. * Send set of commands with 4 byte alignment from the
  833. * firmware data buffer as a single Data fragement.
  834. */
  835. if (!(frag_len % 4)) {
  836. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  837. if (err < 0) {
  838. bt_dev_err(hdev,
  839. "Failed to send firmware data (%d)",
  840. err);
  841. goto done;
  842. }
  843. fw_ptr += frag_len;
  844. frag_len = 0;
  845. }
  846. }
  847. done:
  848. return err;
  849. }
  850. static bool btintel_firmware_version(struct hci_dev *hdev,
  851. u8 num, u8 ww, u8 yy,
  852. const struct firmware *fw,
  853. u32 *boot_addr)
  854. {
  855. const u8 *fw_ptr;
  856. fw_ptr = fw->data;
  857. while (fw_ptr - fw->data < fw->size) {
  858. struct hci_command_hdr *cmd = (void *)(fw_ptr);
  859. /* Each SKU has a different reset parameter to use in the
  860. * HCI_Intel_Reset command and it is embedded in the firmware
  861. * data. So, instead of using static value per SKU, check
  862. * the firmware data and save it for later use.
  863. */
  864. if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
  865. struct cmd_write_boot_params *params;
  866. params = (void *)(fw_ptr + sizeof(*cmd));
  867. *boot_addr = le32_to_cpu(params->boot_addr);
  868. bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
  869. bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
  870. params->fw_build_num, params->fw_build_ww,
  871. params->fw_build_yy);
  872. return (num == params->fw_build_num &&
  873. ww == params->fw_build_ww &&
  874. yy == params->fw_build_yy);
  875. }
  876. fw_ptr += sizeof(*cmd) + cmd->plen;
  877. }
  878. return false;
  879. }
  880. int btintel_download_firmware(struct hci_dev *hdev,
  881. struct intel_version *ver,
  882. const struct firmware *fw,
  883. u32 *boot_param)
  884. {
  885. int err;
  886. /* SfP and WsP don't seem to update the firmware version on file
  887. * so version checking is currently not possible.
  888. */
  889. switch (ver->hw_variant) {
  890. case 0x0b: /* SfP */
  891. case 0x0c: /* WsP */
  892. /* Skip version checking */
  893. break;
  894. default:
  895. /* Skip download if firmware has the same version */
  896. if (btintel_firmware_version(hdev, ver->fw_build_num,
  897. ver->fw_build_ww, ver->fw_build_yy,
  898. fw, boot_param)) {
  899. bt_dev_info(hdev, "Firmware already loaded");
  900. /* Return -EALREADY to indicate that the firmware has
  901. * already been loaded.
  902. */
  903. return -EALREADY;
  904. }
  905. }
  906. /* The firmware variant determines if the device is in bootloader
  907. * mode or is running operational firmware. The value 0x06 identifies
  908. * the bootloader and the value 0x23 identifies the operational
  909. * firmware.
  910. *
  911. * If the firmware version has changed that means it needs to be reset
  912. * to bootloader when operational so the new firmware can be loaded.
  913. */
  914. if (ver->fw_variant == 0x23)
  915. return -EINVAL;
  916. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  917. if (err)
  918. return err;
  919. return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
  920. }
  921. EXPORT_SYMBOL_GPL(btintel_download_firmware);
  922. static int btintel_download_fw_tlv(struct hci_dev *hdev,
  923. struct intel_version_tlv *ver,
  924. const struct firmware *fw, u32 *boot_param,
  925. u8 hw_variant, u8 sbe_type)
  926. {
  927. int err;
  928. u32 css_header_ver;
  929. /* Skip download if firmware has the same version */
  930. if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
  931. ver->min_fw_build_cw,
  932. ver->min_fw_build_yy,
  933. fw, boot_param)) {
  934. bt_dev_info(hdev, "Firmware already loaded");
  935. /* Return -EALREADY to indicate that firmware has
  936. * already been loaded.
  937. */
  938. return -EALREADY;
  939. }
  940. /* The firmware variant determines if the device is in bootloader
  941. * mode or is running operational firmware. The value 0x01 identifies
  942. * the bootloader and the value 0x03 identifies the operational
  943. * firmware.
  944. *
  945. * If the firmware version has changed that means it needs to be reset
  946. * to bootloader when operational so the new firmware can be loaded.
  947. */
  948. if (ver->img_type == 0x03)
  949. return -EINVAL;
  950. /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
  951. * only RSA secure boot engine. Hence, the corresponding sfi file will
  952. * have RSA header of 644 bytes followed by Command Buffer.
  953. *
  954. * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
  955. * secure boot engine. As a result, the corresponding sfi file will
  956. * have RSA header of 644, ECDSA header of 320 bytes followed by
  957. * Command Buffer.
  958. *
  959. * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
  960. * version: RSA(0x00010000) , ECDSA (0x00020000)
  961. */
  962. css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
  963. if (css_header_ver != 0x00010000) {
  964. bt_dev_err(hdev, "Invalid CSS Header version");
  965. return -EINVAL;
  966. }
  967. if (hw_variant <= 0x14) {
  968. if (sbe_type != 0x00) {
  969. bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
  970. hw_variant);
  971. return -EINVAL;
  972. }
  973. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  974. if (err)
  975. return err;
  976. err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
  977. if (err)
  978. return err;
  979. } else if (hw_variant >= 0x17) {
  980. /* Check if CSS header for ECDSA follows the RSA header */
  981. if (fw->data[ECDSA_OFFSET] != 0x06)
  982. return -EINVAL;
  983. /* Check if the CSS Header version is ECDSA(0x00020000) */
  984. css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
  985. if (css_header_ver != 0x00020000) {
  986. bt_dev_err(hdev, "Invalid CSS Header version");
  987. return -EINVAL;
  988. }
  989. if (sbe_type == 0x00) {
  990. err = btintel_sfi_rsa_header_secure_send(hdev, fw);
  991. if (err)
  992. return err;
  993. err = btintel_download_firmware_payload(hdev, fw,
  994. RSA_HEADER_LEN + ECDSA_HEADER_LEN);
  995. if (err)
  996. return err;
  997. } else if (sbe_type == 0x01) {
  998. err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
  999. if (err)
  1000. return err;
  1001. err = btintel_download_firmware_payload(hdev, fw,
  1002. RSA_HEADER_LEN + ECDSA_HEADER_LEN);
  1003. if (err)
  1004. return err;
  1005. }
  1006. }
  1007. return 0;
  1008. }
  1009. static void btintel_reset_to_bootloader(struct hci_dev *hdev)
  1010. {
  1011. struct intel_reset params;
  1012. struct sk_buff *skb;
  1013. /* Send Intel Reset command. This will result in
  1014. * re-enumeration of BT controller.
  1015. *
  1016. * Intel Reset parameter description:
  1017. * reset_type : 0x00 (Soft reset),
  1018. * 0x01 (Hard reset)
  1019. * patch_enable : 0x00 (Do not enable),
  1020. * 0x01 (Enable)
  1021. * ddc_reload : 0x00 (Do not reload),
  1022. * 0x01 (Reload)
  1023. * boot_option: 0x00 (Current image),
  1024. * 0x01 (Specified boot address)
  1025. * boot_param: Boot address
  1026. *
  1027. */
  1028. params.reset_type = 0x01;
  1029. params.patch_enable = 0x01;
  1030. params.ddc_reload = 0x01;
  1031. params.boot_option = 0x00;
  1032. params.boot_param = cpu_to_le32(0x00000000);
  1033. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
  1034. &params, HCI_INIT_TIMEOUT);
  1035. if (IS_ERR(skb)) {
  1036. bt_dev_err(hdev, "FW download error recovery failed (%ld)",
  1037. PTR_ERR(skb));
  1038. return;
  1039. }
  1040. bt_dev_info(hdev, "Intel reset sent to retry FW download");
  1041. kfree_skb(skb);
  1042. /* Current Intel BT controllers(ThP/JfP) hold the USB reset
  1043. * lines for 2ms when it receives Intel Reset in bootloader mode.
  1044. * Whereas, the upcoming Intel BT controllers will hold USB reset
  1045. * for 150ms. To keep the delay generic, 150ms is chosen here.
  1046. */
  1047. msleep(150);
  1048. }
  1049. static int btintel_read_debug_features(struct hci_dev *hdev,
  1050. struct intel_debug_features *features)
  1051. {
  1052. struct sk_buff *skb;
  1053. u8 page_no = 1;
  1054. /* Intel controller supports two pages, each page is of 128-bit
  1055. * feature bit mask. And each bit defines specific feature support
  1056. */
  1057. skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
  1058. HCI_INIT_TIMEOUT);
  1059. if (IS_ERR(skb)) {
  1060. bt_dev_err(hdev, "Reading supported features failed (%ld)",
  1061. PTR_ERR(skb));
  1062. return PTR_ERR(skb);
  1063. }
  1064. if (skb->len != (sizeof(features->page1) + 3)) {
  1065. bt_dev_err(hdev, "Supported features event size mismatch");
  1066. kfree_skb(skb);
  1067. return -EILSEQ;
  1068. }
  1069. memcpy(features->page1, skb->data + 3, sizeof(features->page1));
  1070. /* Read the supported features page2 if required in future.
  1071. */
  1072. kfree_skb(skb);
  1073. return 0;
  1074. }
  1075. static int btintel_set_debug_features(struct hci_dev *hdev,
  1076. const struct intel_debug_features *features)
  1077. {
  1078. u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
  1079. 0x00, 0x00, 0x00 };
  1080. u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
  1081. u8 trace_enable = 0x02;
  1082. struct sk_buff *skb;
  1083. if (!features) {
  1084. bt_dev_warn(hdev, "Debug features not read");
  1085. return -EINVAL;
  1086. }
  1087. if (!(features->page1[0] & 0x3f)) {
  1088. bt_dev_info(hdev, "Telemetry exception format not supported");
  1089. return 0;
  1090. }
  1091. skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
  1092. if (IS_ERR(skb)) {
  1093. bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
  1094. PTR_ERR(skb));
  1095. return PTR_ERR(skb);
  1096. }
  1097. kfree_skb(skb);
  1098. skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
  1099. if (IS_ERR(skb)) {
  1100. bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
  1101. PTR_ERR(skb));
  1102. return PTR_ERR(skb);
  1103. }
  1104. kfree_skb(skb);
  1105. skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
  1106. if (IS_ERR(skb)) {
  1107. bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
  1108. PTR_ERR(skb));
  1109. return PTR_ERR(skb);
  1110. }
  1111. kfree_skb(skb);
  1112. bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
  1113. trace_enable, mask[3]);
  1114. return 0;
  1115. }
  1116. static int btintel_reset_debug_features(struct hci_dev *hdev,
  1117. const struct intel_debug_features *features)
  1118. {
  1119. u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1120. 0x00, 0x00, 0x00 };
  1121. u8 trace_enable = 0x00;
  1122. struct sk_buff *skb;
  1123. if (!features) {
  1124. bt_dev_warn(hdev, "Debug features not read");
  1125. return -EINVAL;
  1126. }
  1127. if (!(features->page1[0] & 0x3f)) {
  1128. bt_dev_info(hdev, "Telemetry exception format not supported");
  1129. return 0;
  1130. }
  1131. /* Should stop the trace before writing ddc event mask. */
  1132. skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
  1133. if (IS_ERR(skb)) {
  1134. bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
  1135. PTR_ERR(skb));
  1136. return PTR_ERR(skb);
  1137. }
  1138. kfree_skb(skb);
  1139. skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
  1140. if (IS_ERR(skb)) {
  1141. bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
  1142. PTR_ERR(skb));
  1143. return PTR_ERR(skb);
  1144. }
  1145. kfree_skb(skb);
  1146. bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
  1147. trace_enable, mask[3]);
  1148. return 0;
  1149. }
  1150. int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
  1151. {
  1152. struct intel_debug_features features;
  1153. int err;
  1154. bt_dev_dbg(hdev, "enable %d", enable);
  1155. /* Read the Intel supported features and if new exception formats
  1156. * supported, need to load the additional DDC config to enable.
  1157. */
  1158. err = btintel_read_debug_features(hdev, &features);
  1159. if (err)
  1160. return err;
  1161. /* Set or reset the debug features. */
  1162. if (enable)
  1163. err = btintel_set_debug_features(hdev, &features);
  1164. else
  1165. err = btintel_reset_debug_features(hdev, &features);
  1166. return err;
  1167. }
  1168. EXPORT_SYMBOL_GPL(btintel_set_quality_report);
  1169. static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
  1170. struct intel_version *ver)
  1171. {
  1172. const struct firmware *fw;
  1173. char fwname[64];
  1174. int ret;
  1175. snprintf(fwname, sizeof(fwname),
  1176. "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
  1177. ver->hw_platform, ver->hw_variant, ver->hw_revision,
  1178. ver->fw_variant, ver->fw_revision, ver->fw_build_num,
  1179. ver->fw_build_ww, ver->fw_build_yy);
  1180. ret = request_firmware(&fw, fwname, &hdev->dev);
  1181. if (ret < 0) {
  1182. if (ret == -EINVAL) {
  1183. bt_dev_err(hdev, "Intel firmware file request failed (%d)",
  1184. ret);
  1185. return NULL;
  1186. }
  1187. bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
  1188. fwname, ret);
  1189. /* If the correct firmware patch file is not found, use the
  1190. * default firmware patch file instead
  1191. */
  1192. snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
  1193. ver->hw_platform, ver->hw_variant);
  1194. if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
  1195. bt_dev_err(hdev, "failed to open default fw file: %s",
  1196. fwname);
  1197. return NULL;
  1198. }
  1199. }
  1200. bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
  1201. return fw;
  1202. }
  1203. static int btintel_legacy_rom_patching(struct hci_dev *hdev,
  1204. const struct firmware *fw,
  1205. const u8 **fw_ptr, int *disable_patch)
  1206. {
  1207. struct sk_buff *skb;
  1208. struct hci_command_hdr *cmd;
  1209. const u8 *cmd_param;
  1210. struct hci_event_hdr *evt = NULL;
  1211. const u8 *evt_param = NULL;
  1212. int remain = fw->size - (*fw_ptr - fw->data);
  1213. /* The first byte indicates the types of the patch command or event.
  1214. * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
  1215. * in the current firmware buffer doesn't start with 0x01 or
  1216. * the size of remain buffer is smaller than HCI command header,
  1217. * the firmware file is corrupted and it should stop the patching
  1218. * process.
  1219. */
  1220. if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
  1221. bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
  1222. return -EINVAL;
  1223. }
  1224. (*fw_ptr)++;
  1225. remain--;
  1226. cmd = (struct hci_command_hdr *)(*fw_ptr);
  1227. *fw_ptr += sizeof(*cmd);
  1228. remain -= sizeof(*cmd);
  1229. /* Ensure that the remain firmware data is long enough than the length
  1230. * of command parameter. If not, the firmware file is corrupted.
  1231. */
  1232. if (remain < cmd->plen) {
  1233. bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
  1234. return -EFAULT;
  1235. }
  1236. /* If there is a command that loads a patch in the firmware
  1237. * file, then enable the patch upon success, otherwise just
  1238. * disable the manufacturer mode, for example patch activation
  1239. * is not required when the default firmware patch file is used
  1240. * because there are no patch data to load.
  1241. */
  1242. if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
  1243. *disable_patch = 0;
  1244. cmd_param = *fw_ptr;
  1245. *fw_ptr += cmd->plen;
  1246. remain -= cmd->plen;
  1247. /* This reads the expected events when the above command is sent to the
  1248. * device. Some vendor commands expects more than one events, for
  1249. * example command status event followed by vendor specific event.
  1250. * For this case, it only keeps the last expected event. so the command
  1251. * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
  1252. * last expected event.
  1253. */
  1254. while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
  1255. (*fw_ptr)++;
  1256. remain--;
  1257. evt = (struct hci_event_hdr *)(*fw_ptr);
  1258. *fw_ptr += sizeof(*evt);
  1259. remain -= sizeof(*evt);
  1260. if (remain < evt->plen) {
  1261. bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
  1262. return -EFAULT;
  1263. }
  1264. evt_param = *fw_ptr;
  1265. *fw_ptr += evt->plen;
  1266. remain -= evt->plen;
  1267. }
  1268. /* Every HCI commands in the firmware file has its correspond event.
  1269. * If event is not found or remain is smaller than zero, the firmware
  1270. * file is corrupted.
  1271. */
  1272. if (!evt || !evt_param || remain < 0) {
  1273. bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
  1274. return -EFAULT;
  1275. }
  1276. skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
  1277. cmd_param, evt->evt, HCI_INIT_TIMEOUT);
  1278. if (IS_ERR(skb)) {
  1279. bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
  1280. cmd->opcode, PTR_ERR(skb));
  1281. return PTR_ERR(skb);
  1282. }
  1283. /* It ensures that the returned event matches the event data read from
  1284. * the firmware file. At fist, it checks the length and then
  1285. * the contents of the event.
  1286. */
  1287. if (skb->len != evt->plen) {
  1288. bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
  1289. le16_to_cpu(cmd->opcode));
  1290. kfree_skb(skb);
  1291. return -EFAULT;
  1292. }
  1293. if (memcmp(skb->data, evt_param, evt->plen)) {
  1294. bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
  1295. le16_to_cpu(cmd->opcode));
  1296. kfree_skb(skb);
  1297. return -EFAULT;
  1298. }
  1299. kfree_skb(skb);
  1300. return 0;
  1301. }
  1302. static int btintel_legacy_rom_setup(struct hci_dev *hdev,
  1303. struct intel_version *ver)
  1304. {
  1305. const struct firmware *fw;
  1306. const u8 *fw_ptr;
  1307. int disable_patch, err;
  1308. struct intel_version new_ver;
  1309. BT_DBG("%s", hdev->name);
  1310. /* fw_patch_num indicates the version of patch the device currently
  1311. * have. If there is no patch data in the device, it is always 0x00.
  1312. * So, if it is other than 0x00, no need to patch the device again.
  1313. */
  1314. if (ver->fw_patch_num) {
  1315. bt_dev_info(hdev,
  1316. "Intel device is already patched. patch num: %02x",
  1317. ver->fw_patch_num);
  1318. goto complete;
  1319. }
  1320. /* Opens the firmware patch file based on the firmware version read
  1321. * from the controller. If it fails to open the matching firmware
  1322. * patch file, it tries to open the default firmware patch file.
  1323. * If no patch file is found, allow the device to operate without
  1324. * a patch.
  1325. */
  1326. fw = btintel_legacy_rom_get_fw(hdev, ver);
  1327. if (!fw)
  1328. goto complete;
  1329. fw_ptr = fw->data;
  1330. /* Enable the manufacturer mode of the controller.
  1331. * Only while this mode is enabled, the driver can download the
  1332. * firmware patch data and configuration parameters.
  1333. */
  1334. err = btintel_enter_mfg(hdev);
  1335. if (err) {
  1336. release_firmware(fw);
  1337. return err;
  1338. }
  1339. disable_patch = 1;
  1340. /* The firmware data file consists of list of Intel specific HCI
  1341. * commands and its expected events. The first byte indicates the
  1342. * type of the message, either HCI command or HCI event.
  1343. *
  1344. * It reads the command and its expected event from the firmware file,
  1345. * and send to the controller. Once __hci_cmd_sync_ev() returns,
  1346. * the returned event is compared with the event read from the firmware
  1347. * file and it will continue until all the messages are downloaded to
  1348. * the controller.
  1349. *
  1350. * Once the firmware patching is completed successfully,
  1351. * the manufacturer mode is disabled with reset and activating the
  1352. * downloaded patch.
  1353. *
  1354. * If the firmware patching fails, the manufacturer mode is
  1355. * disabled with reset and deactivating the patch.
  1356. *
  1357. * If the default patch file is used, no reset is done when disabling
  1358. * the manufacturer.
  1359. */
  1360. while (fw->size > fw_ptr - fw->data) {
  1361. int ret;
  1362. ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
  1363. &disable_patch);
  1364. if (ret < 0)
  1365. goto exit_mfg_deactivate;
  1366. }
  1367. release_firmware(fw);
  1368. if (disable_patch)
  1369. goto exit_mfg_disable;
  1370. /* Patching completed successfully and disable the manufacturer mode
  1371. * with reset and activate the downloaded firmware patches.
  1372. */
  1373. err = btintel_exit_mfg(hdev, true, true);
  1374. if (err)
  1375. return err;
  1376. /* Need build number for downloaded fw patches in
  1377. * every power-on boot
  1378. */
  1379. err = btintel_read_version(hdev, &new_ver);
  1380. if (err)
  1381. return err;
  1382. bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
  1383. new_ver.fw_patch_num);
  1384. goto complete;
  1385. exit_mfg_disable:
  1386. /* Disable the manufacturer mode without reset */
  1387. err = btintel_exit_mfg(hdev, false, false);
  1388. if (err)
  1389. return err;
  1390. bt_dev_info(hdev, "Intel firmware patch completed");
  1391. goto complete;
  1392. exit_mfg_deactivate:
  1393. release_firmware(fw);
  1394. /* Patching failed. Disable the manufacturer mode with reset and
  1395. * deactivate the downloaded firmware patches.
  1396. */
  1397. err = btintel_exit_mfg(hdev, true, false);
  1398. if (err)
  1399. return err;
  1400. bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
  1401. complete:
  1402. /* Set the event mask for Intel specific vendor events. This enables
  1403. * a few extra events that are useful during general operation.
  1404. */
  1405. btintel_set_event_mask_mfg(hdev, false);
  1406. btintel_check_bdaddr(hdev);
  1407. return 0;
  1408. }
  1409. static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
  1410. {
  1411. ktime_t delta, rettime;
  1412. unsigned long long duration;
  1413. int err;
  1414. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1415. bt_dev_info(hdev, "Waiting for firmware download to complete");
  1416. err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
  1417. TASK_INTERRUPTIBLE,
  1418. msecs_to_jiffies(msec));
  1419. if (err == -EINTR) {
  1420. bt_dev_err(hdev, "Firmware loading interrupted");
  1421. return err;
  1422. }
  1423. if (err) {
  1424. bt_dev_err(hdev, "Firmware loading timeout");
  1425. return -ETIMEDOUT;
  1426. }
  1427. if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
  1428. bt_dev_err(hdev, "Firmware loading failed");
  1429. return -ENOEXEC;
  1430. }
  1431. rettime = ktime_get();
  1432. delta = ktime_sub(rettime, calltime);
  1433. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  1434. bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
  1435. return 0;
  1436. }
  1437. static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
  1438. {
  1439. ktime_t delta, rettime;
  1440. unsigned long long duration;
  1441. int err;
  1442. bt_dev_info(hdev, "Waiting for device to boot");
  1443. err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
  1444. TASK_INTERRUPTIBLE,
  1445. msecs_to_jiffies(msec));
  1446. if (err == -EINTR) {
  1447. bt_dev_err(hdev, "Device boot interrupted");
  1448. return -EINTR;
  1449. }
  1450. if (err) {
  1451. bt_dev_err(hdev, "Device boot timeout");
  1452. return -ETIMEDOUT;
  1453. }
  1454. rettime = ktime_get();
  1455. delta = ktime_sub(rettime, calltime);
  1456. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1457. bt_dev_info(hdev, "Device booted in %llu usecs", duration);
  1458. return 0;
  1459. }
  1460. static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
  1461. {
  1462. ktime_t calltime;
  1463. int err;
  1464. calltime = ktime_get();
  1465. btintel_set_flag(hdev, INTEL_BOOTING);
  1466. err = btintel_send_intel_reset(hdev, boot_addr);
  1467. if (err) {
  1468. bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
  1469. btintel_reset_to_bootloader(hdev);
  1470. return err;
  1471. }
  1472. /* The bootloader will not indicate when the device is ready. This
  1473. * is done by the operational firmware sending bootup notification.
  1474. *
  1475. * Booting into operational firmware should not take longer than
  1476. * 1 second. However if that happens, then just fail the setup
  1477. * since something went wrong.
  1478. */
  1479. err = btintel_boot_wait(hdev, calltime, 1000);
  1480. if (err == -ETIMEDOUT)
  1481. btintel_reset_to_bootloader(hdev);
  1482. return err;
  1483. }
  1484. static int btintel_get_fw_name(struct intel_version *ver,
  1485. struct intel_boot_params *params,
  1486. char *fw_name, size_t len,
  1487. const char *suffix)
  1488. {
  1489. switch (ver->hw_variant) {
  1490. case 0x0b: /* SfP */
  1491. case 0x0c: /* WsP */
  1492. snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
  1493. le16_to_cpu(ver->hw_variant),
  1494. le16_to_cpu(params->dev_revid),
  1495. suffix);
  1496. break;
  1497. case 0x11: /* JfP */
  1498. case 0x12: /* ThP */
  1499. case 0x13: /* HrP */
  1500. case 0x14: /* CcP */
  1501. snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
  1502. le16_to_cpu(ver->hw_variant),
  1503. le16_to_cpu(ver->hw_revision),
  1504. le16_to_cpu(ver->fw_revision),
  1505. suffix);
  1506. break;
  1507. default:
  1508. return -EINVAL;
  1509. }
  1510. return 0;
  1511. }
  1512. static int btintel_download_fw(struct hci_dev *hdev,
  1513. struct intel_version *ver,
  1514. struct intel_boot_params *params,
  1515. u32 *boot_param)
  1516. {
  1517. const struct firmware *fw;
  1518. char fwname[64];
  1519. int err;
  1520. ktime_t calltime;
  1521. if (!ver || !params)
  1522. return -EINVAL;
  1523. /* The firmware variant determines if the device is in bootloader
  1524. * mode or is running operational firmware. The value 0x06 identifies
  1525. * the bootloader and the value 0x23 identifies the operational
  1526. * firmware.
  1527. *
  1528. * When the operational firmware is already present, then only
  1529. * the check for valid Bluetooth device address is needed. This
  1530. * determines if the device will be added as configured or
  1531. * unconfigured controller.
  1532. *
  1533. * It is not possible to use the Secure Boot Parameters in this
  1534. * case since that command is only available in bootloader mode.
  1535. */
  1536. if (ver->fw_variant == 0x23) {
  1537. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1538. btintel_check_bdaddr(hdev);
  1539. /* SfP and WsP don't seem to update the firmware version on file
  1540. * so version checking is currently possible.
  1541. */
  1542. switch (ver->hw_variant) {
  1543. case 0x0b: /* SfP */
  1544. case 0x0c: /* WsP */
  1545. return 0;
  1546. }
  1547. /* Proceed to download to check if the version matches */
  1548. goto download;
  1549. }
  1550. /* Read the secure boot parameters to identify the operating
  1551. * details of the bootloader.
  1552. */
  1553. err = btintel_read_boot_params(hdev, params);
  1554. if (err)
  1555. return err;
  1556. /* It is required that every single firmware fragment is acknowledged
  1557. * with a command complete event. If the boot parameters indicate
  1558. * that this bootloader does not send them, then abort the setup.
  1559. */
  1560. if (params->limited_cce != 0x00) {
  1561. bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
  1562. params->limited_cce);
  1563. return -EINVAL;
  1564. }
  1565. /* If the OTP has no valid Bluetooth device address, then there will
  1566. * also be no valid address for the operational firmware.
  1567. */
  1568. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  1569. bt_dev_info(hdev, "No device address configured");
  1570. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1571. }
  1572. download:
  1573. /* With this Intel bootloader only the hardware variant and device
  1574. * revision information are used to select the right firmware for SfP
  1575. * and WsP.
  1576. *
  1577. * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
  1578. *
  1579. * Currently the supported hardware variants are:
  1580. * 11 (0x0b) for iBT3.0 (LnP/SfP)
  1581. * 12 (0x0c) for iBT3.5 (WsP)
  1582. *
  1583. * For ThP/JfP and for future SKU's, the FW name varies based on HW
  1584. * variant, HW revision and FW revision, as these are dependent on CNVi
  1585. * and RF Combination.
  1586. *
  1587. * 17 (0x11) for iBT3.5 (JfP)
  1588. * 18 (0x12) for iBT3.5 (ThP)
  1589. *
  1590. * The firmware file name for these will be
  1591. * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
  1592. *
  1593. */
  1594. err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
  1595. if (err < 0) {
  1596. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1597. /* Firmware has already been loaded */
  1598. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1599. return 0;
  1600. }
  1601. bt_dev_err(hdev, "Unsupported Intel firmware naming");
  1602. return -EINVAL;
  1603. }
  1604. err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
  1605. if (err < 0) {
  1606. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1607. /* Firmware has already been loaded */
  1608. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1609. return 0;
  1610. }
  1611. bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
  1612. fwname, err);
  1613. return err;
  1614. }
  1615. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  1616. if (fw->size < 644) {
  1617. bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
  1618. fw->size);
  1619. err = -EBADF;
  1620. goto done;
  1621. }
  1622. calltime = ktime_get();
  1623. btintel_set_flag(hdev, INTEL_DOWNLOADING);
  1624. /* Start firmware downloading and get boot parameter */
  1625. err = btintel_download_firmware(hdev, ver, fw, boot_param);
  1626. if (err < 0) {
  1627. if (err == -EALREADY) {
  1628. /* Firmware has already been loaded */
  1629. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1630. err = 0;
  1631. goto done;
  1632. }
  1633. /* When FW download fails, send Intel Reset to retry
  1634. * FW download.
  1635. */
  1636. btintel_reset_to_bootloader(hdev);
  1637. goto done;
  1638. }
  1639. /* Before switching the device into operational mode and with that
  1640. * booting the loaded firmware, wait for the bootloader notification
  1641. * that all fragments have been successfully received.
  1642. *
  1643. * When the event processing receives the notification, then the
  1644. * INTEL_DOWNLOADING flag will be cleared.
  1645. *
  1646. * The firmware loading should not take longer than 5 seconds
  1647. * and thus just timeout if that happens and fail the setup
  1648. * of this device.
  1649. */
  1650. err = btintel_download_wait(hdev, calltime, 5000);
  1651. if (err == -ETIMEDOUT)
  1652. btintel_reset_to_bootloader(hdev);
  1653. done:
  1654. release_firmware(fw);
  1655. return err;
  1656. }
  1657. static int btintel_bootloader_setup(struct hci_dev *hdev,
  1658. struct intel_version *ver)
  1659. {
  1660. struct intel_version new_ver;
  1661. struct intel_boot_params params;
  1662. u32 boot_param;
  1663. char ddcname[64];
  1664. int err;
  1665. BT_DBG("%s", hdev->name);
  1666. /* Set the default boot parameter to 0x0 and it is updated to
  1667. * SKU specific boot parameter after reading Intel_Write_Boot_Params
  1668. * command while downloading the firmware.
  1669. */
  1670. boot_param = 0x00000000;
  1671. btintel_set_flag(hdev, INTEL_BOOTLOADER);
  1672. err = btintel_download_fw(hdev, ver, &params, &boot_param);
  1673. if (err)
  1674. return err;
  1675. /* controller is already having an operational firmware */
  1676. if (ver->fw_variant == 0x23)
  1677. goto finish;
  1678. err = btintel_boot(hdev, boot_param);
  1679. if (err)
  1680. return err;
  1681. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1682. err = btintel_get_fw_name(ver, &params, ddcname,
  1683. sizeof(ddcname), "ddc");
  1684. if (err < 0) {
  1685. bt_dev_err(hdev, "Unsupported Intel firmware naming");
  1686. } else {
  1687. /* Once the device is running in operational mode, it needs to
  1688. * apply the device configuration (DDC) parameters.
  1689. *
  1690. * The device can work without DDC parameters, so even if it
  1691. * fails to load the file, no need to fail the setup.
  1692. */
  1693. btintel_load_ddc_config(hdev, ddcname);
  1694. }
  1695. hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
  1696. /* Read the Intel version information after loading the FW */
  1697. err = btintel_read_version(hdev, &new_ver);
  1698. if (err)
  1699. return err;
  1700. btintel_version_info(hdev, &new_ver);
  1701. finish:
  1702. /* Set the event mask for Intel specific vendor events. This enables
  1703. * a few extra events that are useful during general operation. It
  1704. * does not enable any debugging related events.
  1705. *
  1706. * The device will function correctly without these events enabled
  1707. * and thus no need to fail the setup.
  1708. */
  1709. btintel_set_event_mask(hdev, false);
  1710. return 0;
  1711. }
  1712. static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
  1713. char *fw_name, size_t len,
  1714. const char *suffix)
  1715. {
  1716. /* The firmware file name for new generation controllers will be
  1717. * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
  1718. */
  1719. snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
  1720. INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
  1721. INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
  1722. INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
  1723. INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
  1724. suffix);
  1725. }
  1726. static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
  1727. struct intel_version_tlv *ver,
  1728. u32 *boot_param)
  1729. {
  1730. const struct firmware *fw;
  1731. char fwname[64];
  1732. int err;
  1733. ktime_t calltime;
  1734. if (!ver || !boot_param)
  1735. return -EINVAL;
  1736. /* The firmware variant determines if the device is in bootloader
  1737. * mode or is running operational firmware. The value 0x03 identifies
  1738. * the bootloader and the value 0x23 identifies the operational
  1739. * firmware.
  1740. *
  1741. * When the operational firmware is already present, then only
  1742. * the check for valid Bluetooth device address is needed. This
  1743. * determines if the device will be added as configured or
  1744. * unconfigured controller.
  1745. *
  1746. * It is not possible to use the Secure Boot Parameters in this
  1747. * case since that command is only available in bootloader mode.
  1748. */
  1749. if (ver->img_type == 0x03) {
  1750. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1751. btintel_check_bdaddr(hdev);
  1752. } else {
  1753. /*
  1754. * Check for valid bd address in boot loader mode. Device
  1755. * will be marked as unconfigured if empty bd address is
  1756. * found.
  1757. */
  1758. if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
  1759. bt_dev_info(hdev, "No device address configured");
  1760. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1761. }
  1762. }
  1763. btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
  1764. err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
  1765. if (err < 0) {
  1766. if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1767. /* Firmware has already been loaded */
  1768. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1769. return 0;
  1770. }
  1771. bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
  1772. fwname, err);
  1773. return err;
  1774. }
  1775. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  1776. if (fw->size < 644) {
  1777. bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
  1778. fw->size);
  1779. err = -EBADF;
  1780. goto done;
  1781. }
  1782. calltime = ktime_get();
  1783. btintel_set_flag(hdev, INTEL_DOWNLOADING);
  1784. /* Start firmware downloading and get boot parameter */
  1785. err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
  1786. INTEL_HW_VARIANT(ver->cnvi_bt),
  1787. ver->sbe_type);
  1788. if (err < 0) {
  1789. if (err == -EALREADY) {
  1790. /* Firmware has already been loaded */
  1791. btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
  1792. err = 0;
  1793. goto done;
  1794. }
  1795. /* When FW download fails, send Intel Reset to retry
  1796. * FW download.
  1797. */
  1798. btintel_reset_to_bootloader(hdev);
  1799. goto done;
  1800. }
  1801. /* Before switching the device into operational mode and with that
  1802. * booting the loaded firmware, wait for the bootloader notification
  1803. * that all fragments have been successfully received.
  1804. *
  1805. * When the event processing receives the notification, then the
  1806. * BTUSB_DOWNLOADING flag will be cleared.
  1807. *
  1808. * The firmware loading should not take longer than 5 seconds
  1809. * and thus just timeout if that happens and fail the setup
  1810. * of this device.
  1811. */
  1812. err = btintel_download_wait(hdev, calltime, 5000);
  1813. if (err == -ETIMEDOUT)
  1814. btintel_reset_to_bootloader(hdev);
  1815. done:
  1816. release_firmware(fw);
  1817. return err;
  1818. }
  1819. static int btintel_get_codec_config_data(struct hci_dev *hdev,
  1820. __u8 link, struct bt_codec *codec,
  1821. __u8 *ven_len, __u8 **ven_data)
  1822. {
  1823. int err = 0;
  1824. if (!ven_data || !ven_len)
  1825. return -EINVAL;
  1826. *ven_len = 0;
  1827. *ven_data = NULL;
  1828. if (link != ESCO_LINK) {
  1829. bt_dev_err(hdev, "Invalid link type(%u)", link);
  1830. return -EINVAL;
  1831. }
  1832. *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
  1833. if (!*ven_data) {
  1834. err = -ENOMEM;
  1835. goto error;
  1836. }
  1837. /* supports only CVSD and mSBC offload codecs */
  1838. switch (codec->id) {
  1839. case 0x02:
  1840. **ven_data = 0x00;
  1841. break;
  1842. case 0x05:
  1843. **ven_data = 0x01;
  1844. break;
  1845. default:
  1846. err = -EINVAL;
  1847. bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
  1848. goto error;
  1849. }
  1850. /* codec and its capabilities are pre-defined to ids
  1851. * preset id = 0x00 represents CVSD codec with sampling rate 8K
  1852. * preset id = 0x01 represents mSBC codec with sampling rate 16K
  1853. */
  1854. *ven_len = sizeof(__u8);
  1855. return err;
  1856. error:
  1857. kfree(*ven_data);
  1858. *ven_data = NULL;
  1859. return err;
  1860. }
  1861. static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
  1862. {
  1863. /* Intel uses 1 as data path id for all the usecases */
  1864. *data_path_id = 1;
  1865. return 0;
  1866. }
  1867. static int btintel_configure_offload(struct hci_dev *hdev)
  1868. {
  1869. struct sk_buff *skb;
  1870. int err = 0;
  1871. struct intel_offload_use_cases *use_cases;
  1872. skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
  1873. if (IS_ERR(skb)) {
  1874. bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
  1875. PTR_ERR(skb));
  1876. return PTR_ERR(skb);
  1877. }
  1878. if (skb->len < sizeof(*use_cases)) {
  1879. err = -EIO;
  1880. goto error;
  1881. }
  1882. use_cases = (void *)skb->data;
  1883. if (use_cases->status) {
  1884. err = -bt_to_errno(skb->data[0]);
  1885. goto error;
  1886. }
  1887. if (use_cases->preset[0] & 0x03) {
  1888. hdev->get_data_path_id = btintel_get_data_path_id;
  1889. hdev->get_codec_config_data = btintel_get_codec_config_data;
  1890. }
  1891. error:
  1892. kfree_skb(skb);
  1893. return err;
  1894. }
  1895. static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
  1896. struct intel_version_tlv *ver)
  1897. {
  1898. u32 boot_param;
  1899. char ddcname[64];
  1900. int err;
  1901. struct intel_version_tlv new_ver;
  1902. bt_dev_dbg(hdev, "");
  1903. /* Set the default boot parameter to 0x0 and it is updated to
  1904. * SKU specific boot parameter after reading Intel_Write_Boot_Params
  1905. * command while downloading the firmware.
  1906. */
  1907. boot_param = 0x00000000;
  1908. btintel_set_flag(hdev, INTEL_BOOTLOADER);
  1909. err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
  1910. if (err)
  1911. return err;
  1912. /* check if controller is already having an operational firmware */
  1913. if (ver->img_type == 0x03)
  1914. goto finish;
  1915. err = btintel_boot(hdev, boot_param);
  1916. if (err)
  1917. return err;
  1918. btintel_clear_flag(hdev, INTEL_BOOTLOADER);
  1919. btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
  1920. /* Once the device is running in operational mode, it needs to
  1921. * apply the device configuration (DDC) parameters.
  1922. *
  1923. * The device can work without DDC parameters, so even if it
  1924. * fails to load the file, no need to fail the setup.
  1925. */
  1926. btintel_load_ddc_config(hdev, ddcname);
  1927. /* Read supported use cases and set callbacks to fetch datapath id */
  1928. btintel_configure_offload(hdev);
  1929. hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
  1930. /* Read the Intel version information after loading the FW */
  1931. err = btintel_read_version_tlv(hdev, &new_ver);
  1932. if (err)
  1933. return err;
  1934. btintel_version_info_tlv(hdev, &new_ver);
  1935. finish:
  1936. /* Set the event mask for Intel specific vendor events. This enables
  1937. * a few extra events that are useful during general operation. It
  1938. * does not enable any debugging related events.
  1939. *
  1940. * The device will function correctly without these events enabled
  1941. * and thus no need to fail the setup.
  1942. */
  1943. btintel_set_event_mask(hdev, false);
  1944. return 0;
  1945. }
  1946. static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
  1947. {
  1948. switch (hw_variant) {
  1949. /* Legacy bootloader devices that supports MSFT Extension */
  1950. case 0x11: /* JfP */
  1951. case 0x12: /* ThP */
  1952. case 0x13: /* HrP */
  1953. case 0x14: /* CcP */
  1954. /* All Intel new genration controllers support the Microsoft vendor
  1955. * extension are using 0xFC1E for VsMsftOpCode.
  1956. */
  1957. case 0x17:
  1958. case 0x18:
  1959. case 0x19:
  1960. case 0x1b:
  1961. hci_set_msft_opcode(hdev, 0xFC1E);
  1962. break;
  1963. default:
  1964. /* Not supported */
  1965. break;
  1966. }
  1967. }
  1968. static int btintel_setup_combined(struct hci_dev *hdev)
  1969. {
  1970. const u8 param[1] = { 0xFF };
  1971. struct intel_version ver;
  1972. struct intel_version_tlv ver_tlv;
  1973. struct sk_buff *skb;
  1974. int err;
  1975. BT_DBG("%s", hdev->name);
  1976. /* The some controllers have a bug with the first HCI command sent to it
  1977. * returning number of completed commands as zero. This would stall the
  1978. * command processing in the Bluetooth core.
  1979. *
  1980. * As a workaround, send HCI Reset command first which will reset the
  1981. * number of completed commands and allow normal command processing
  1982. * from now on.
  1983. *
  1984. * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
  1985. * in the SW_RFKILL ON state as a workaround of fixing LED issue during
  1986. * the shutdown() procedure, and once the device is in SW_RFKILL ON
  1987. * state, the only way to exit out of it is sending the HCI_Reset
  1988. * command.
  1989. */
  1990. if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
  1991. btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
  1992. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
  1993. HCI_INIT_TIMEOUT);
  1994. if (IS_ERR(skb)) {
  1995. bt_dev_err(hdev,
  1996. "sending initial HCI reset failed (%ld)",
  1997. PTR_ERR(skb));
  1998. return PTR_ERR(skb);
  1999. }
  2000. kfree_skb(skb);
  2001. }
  2002. /* Starting from TyP device, the command parameter and response are
  2003. * changed even though the OCF for HCI_Intel_Read_Version command
  2004. * remains same. The legacy devices can handle even if the
  2005. * command has a parameter and returns a correct version information.
  2006. * So, it uses new format to support both legacy and new format.
  2007. */
  2008. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  2009. if (IS_ERR(skb)) {
  2010. bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
  2011. PTR_ERR(skb));
  2012. return PTR_ERR(skb);
  2013. }
  2014. /* Check the status */
  2015. if (skb->data[0]) {
  2016. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  2017. skb->data[0]);
  2018. err = -EIO;
  2019. goto exit_error;
  2020. }
  2021. /* Apply the common HCI quirks for Intel device */
  2022. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2023. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2024. set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
  2025. /* Set up the quality report callback for Intel devices */
  2026. hdev->set_quality_report = btintel_set_quality_report;
  2027. /* For Legacy device, check the HW platform value and size */
  2028. if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
  2029. bt_dev_dbg(hdev, "Read the legacy Intel version information");
  2030. memcpy(&ver, skb->data, sizeof(ver));
  2031. /* Display version information */
  2032. btintel_version_info(hdev, &ver);
  2033. /* Check for supported iBT hardware variants of this firmware
  2034. * loading method.
  2035. *
  2036. * This check has been put in place to ensure correct forward
  2037. * compatibility options when newer hardware variants come
  2038. * along.
  2039. */
  2040. switch (ver.hw_variant) {
  2041. case 0x07: /* WP */
  2042. case 0x08: /* StP */
  2043. /* Legacy ROM product */
  2044. btintel_set_flag(hdev, INTEL_ROM_LEGACY);
  2045. /* Apply the device specific HCI quirks
  2046. *
  2047. * WBS for SdP - For the Legacy ROM products, only SdP
  2048. * supports the WBS. But the version information is not
  2049. * enough to use here because the StP2 and SdP have same
  2050. * hw_variant and fw_variant. So, this flag is set by
  2051. * the transport driver (btusb) based on the HW info
  2052. * (idProduct)
  2053. */
  2054. if (!btintel_test_flag(hdev,
  2055. INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
  2056. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
  2057. &hdev->quirks);
  2058. if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
  2059. set_bit(HCI_QUIRK_VALID_LE_STATES,
  2060. &hdev->quirks);
  2061. err = btintel_legacy_rom_setup(hdev, &ver);
  2062. break;
  2063. case 0x0b: /* SfP */
  2064. case 0x11: /* JfP */
  2065. case 0x12: /* ThP */
  2066. case 0x13: /* HrP */
  2067. case 0x14: /* CcP */
  2068. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  2069. fallthrough;
  2070. case 0x0c: /* WsP */
  2071. /* Apply the device specific HCI quirks
  2072. *
  2073. * All Legacy bootloader devices support WBS
  2074. */
  2075. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
  2076. &hdev->quirks);
  2077. /* Setup MSFT Extension support */
  2078. btintel_set_msft_opcode(hdev, ver.hw_variant);
  2079. err = btintel_bootloader_setup(hdev, &ver);
  2080. break;
  2081. default:
  2082. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  2083. ver.hw_variant);
  2084. err = -EINVAL;
  2085. }
  2086. goto exit_error;
  2087. }
  2088. /* memset ver_tlv to start with clean state as few fields are exclusive
  2089. * to bootloader mode and are not populated in operational mode
  2090. */
  2091. memset(&ver_tlv, 0, sizeof(ver_tlv));
  2092. /* For TLV type device, parse the tlv data */
  2093. err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
  2094. if (err) {
  2095. bt_dev_err(hdev, "Failed to parse TLV version information");
  2096. goto exit_error;
  2097. }
  2098. if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
  2099. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  2100. INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
  2101. err = -EINVAL;
  2102. goto exit_error;
  2103. }
  2104. /* Check for supported iBT hardware variants of this firmware
  2105. * loading method.
  2106. *
  2107. * This check has been put in place to ensure correct forward
  2108. * compatibility options when newer hardware variants come
  2109. * along.
  2110. */
  2111. switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
  2112. case 0x11: /* JfP */
  2113. case 0x12: /* ThP */
  2114. case 0x13: /* HrP */
  2115. case 0x14: /* CcP */
  2116. /* Some legacy bootloader devices starting from JfP,
  2117. * the operational firmware supports both old and TLV based
  2118. * HCI_Intel_Read_Version command based on the command
  2119. * parameter.
  2120. *
  2121. * For upgrading firmware case, the TLV based version cannot
  2122. * be used because the firmware filename for legacy bootloader
  2123. * is based on the old format.
  2124. *
  2125. * Also, it is not easy to convert TLV based version from the
  2126. * legacy version format.
  2127. *
  2128. * So, as a workaround for those devices, use the legacy
  2129. * HCI_Intel_Read_Version to get the version information and
  2130. * run the legacy bootloader setup.
  2131. */
  2132. err = btintel_read_version(hdev, &ver);
  2133. if (err)
  2134. break;
  2135. /* Apply the device specific HCI quirks
  2136. *
  2137. * All Legacy bootloader devices support WBS
  2138. */
  2139. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  2140. /* Set Valid LE States quirk */
  2141. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  2142. /* Setup MSFT Extension support */
  2143. btintel_set_msft_opcode(hdev, ver.hw_variant);
  2144. err = btintel_bootloader_setup(hdev, &ver);
  2145. break;
  2146. case 0x17:
  2147. case 0x18:
  2148. case 0x19:
  2149. case 0x1b:
  2150. /* Display version information of TLV type */
  2151. btintel_version_info_tlv(hdev, &ver_tlv);
  2152. /* Apply the device specific HCI quirks for TLV based devices
  2153. *
  2154. * All TLV based devices support WBS
  2155. */
  2156. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  2157. /* Apply LE States quirk from solar onwards */
  2158. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  2159. /* Setup MSFT Extension support */
  2160. btintel_set_msft_opcode(hdev,
  2161. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  2162. err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
  2163. break;
  2164. default:
  2165. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  2166. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  2167. err = -EINVAL;
  2168. break;
  2169. }
  2170. exit_error:
  2171. kfree_skb(skb);
  2172. return err;
  2173. }
  2174. static int btintel_shutdown_combined(struct hci_dev *hdev)
  2175. {
  2176. struct sk_buff *skb;
  2177. int ret;
  2178. /* Send HCI Reset to the controller to stop any BT activity which
  2179. * were triggered. This will help to save power and maintain the
  2180. * sync b/w Host and controller
  2181. */
  2182. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  2183. if (IS_ERR(skb)) {
  2184. bt_dev_err(hdev, "HCI reset during shutdown failed");
  2185. return PTR_ERR(skb);
  2186. }
  2187. kfree_skb(skb);
  2188. /* Some platforms have an issue with BT LED when the interface is
  2189. * down or BT radio is turned off, which takes 5 seconds to BT LED
  2190. * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
  2191. * device in the RFKILL ON state which turns off the BT LED immediately.
  2192. */
  2193. if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
  2194. skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
  2195. if (IS_ERR(skb)) {
  2196. ret = PTR_ERR(skb);
  2197. bt_dev_err(hdev, "turning off Intel device LED failed");
  2198. return ret;
  2199. }
  2200. kfree_skb(skb);
  2201. }
  2202. return 0;
  2203. }
  2204. int btintel_configure_setup(struct hci_dev *hdev)
  2205. {
  2206. hdev->manufacturer = 2;
  2207. hdev->setup = btintel_setup_combined;
  2208. hdev->shutdown = btintel_shutdown_combined;
  2209. hdev->hw_error = btintel_hw_error;
  2210. hdev->set_diag = btintel_set_diag_combined;
  2211. hdev->set_bdaddr = btintel_set_bdaddr;
  2212. return 0;
  2213. }
  2214. EXPORT_SYMBOL_GPL(btintel_configure_setup);
  2215. void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
  2216. {
  2217. const struct intel_bootup *evt = ptr;
  2218. if (len != sizeof(*evt))
  2219. return;
  2220. if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
  2221. btintel_wake_up_flag(hdev, INTEL_BOOTING);
  2222. }
  2223. EXPORT_SYMBOL_GPL(btintel_bootup);
  2224. void btintel_secure_send_result(struct hci_dev *hdev,
  2225. const void *ptr, unsigned int len)
  2226. {
  2227. const struct intel_secure_send_result *evt = ptr;
  2228. if (len != sizeof(*evt))
  2229. return;
  2230. if (evt->result)
  2231. btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
  2232. if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
  2233. btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
  2234. btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
  2235. }
  2236. EXPORT_SYMBOL_GPL(btintel_secure_send_result);
  2237. MODULE_AUTHOR("Marcel Holtmann <[email protected]>");
  2238. MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
  2239. MODULE_VERSION(VERSION);
  2240. MODULE_LICENSE("GPL");
  2241. MODULE_FIRMWARE("intel/ibt-11-5.sfi");
  2242. MODULE_FIRMWARE("intel/ibt-11-5.ddc");
  2243. MODULE_FIRMWARE("intel/ibt-12-16.sfi");
  2244. MODULE_FIRMWARE("intel/ibt-12-16.ddc");