hci_intel.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth HCI UART driver for Intel devices
  5. *
  6. * Copyright (C) 2015 Intel Corporation
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/firmware.h>
  12. #include <linux/module.h>
  13. #include <linux/wait.h>
  14. #include <linux/tty.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/acpi.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/pm_runtime.h>
  20. #include <net/bluetooth/bluetooth.h>
  21. #include <net/bluetooth/hci_core.h>
  22. #include "hci_uart.h"
  23. #include "btintel.h"
  24. #define STATE_BOOTLOADER 0
  25. #define STATE_DOWNLOADING 1
  26. #define STATE_FIRMWARE_LOADED 2
  27. #define STATE_FIRMWARE_FAILED 3
  28. #define STATE_BOOTING 4
  29. #define STATE_LPM_ENABLED 5
  30. #define STATE_TX_ACTIVE 6
  31. #define STATE_SUSPENDED 7
  32. #define STATE_LPM_TRANSACTION 8
  33. #define HCI_LPM_WAKE_PKT 0xf0
  34. #define HCI_LPM_PKT 0xf1
  35. #define HCI_LPM_MAX_SIZE 10
  36. #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
  37. #define LPM_OP_TX_NOTIFY 0x00
  38. #define LPM_OP_SUSPEND_ACK 0x02
  39. #define LPM_OP_RESUME_ACK 0x03
  40. #define LPM_SUSPEND_DELAY_MS 1000
  41. struct hci_lpm_pkt {
  42. __u8 opcode;
  43. __u8 dlen;
  44. __u8 data[];
  45. } __packed;
  46. struct intel_device {
  47. struct list_head list;
  48. struct platform_device *pdev;
  49. struct gpio_desc *reset;
  50. struct hci_uart *hu;
  51. struct mutex hu_lock;
  52. int irq;
  53. };
  54. static LIST_HEAD(intel_device_list);
  55. static DEFINE_MUTEX(intel_device_list_lock);
  56. struct intel_data {
  57. struct sk_buff *rx_skb;
  58. struct sk_buff_head txq;
  59. struct work_struct busy_work;
  60. struct hci_uart *hu;
  61. unsigned long flags;
  62. };
  63. static u8 intel_convert_speed(unsigned int speed)
  64. {
  65. switch (speed) {
  66. case 9600:
  67. return 0x00;
  68. case 19200:
  69. return 0x01;
  70. case 38400:
  71. return 0x02;
  72. case 57600:
  73. return 0x03;
  74. case 115200:
  75. return 0x04;
  76. case 230400:
  77. return 0x05;
  78. case 460800:
  79. return 0x06;
  80. case 921600:
  81. return 0x07;
  82. case 1843200:
  83. return 0x08;
  84. case 3250000:
  85. return 0x09;
  86. case 2000000:
  87. return 0x0a;
  88. case 3000000:
  89. return 0x0b;
  90. default:
  91. return 0xff;
  92. }
  93. }
  94. static int intel_wait_booting(struct hci_uart *hu)
  95. {
  96. struct intel_data *intel = hu->priv;
  97. int err;
  98. err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
  99. TASK_INTERRUPTIBLE,
  100. msecs_to_jiffies(1000));
  101. if (err == -EINTR) {
  102. bt_dev_err(hu->hdev, "Device boot interrupted");
  103. return -EINTR;
  104. }
  105. if (err) {
  106. bt_dev_err(hu->hdev, "Device boot timeout");
  107. return -ETIMEDOUT;
  108. }
  109. return err;
  110. }
  111. static int intel_wait_lpm_transaction(struct hci_uart *hu)
  112. {
  113. struct intel_data *intel = hu->priv;
  114. int err;
  115. err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
  116. TASK_INTERRUPTIBLE,
  117. msecs_to_jiffies(1000));
  118. if (err == -EINTR) {
  119. bt_dev_err(hu->hdev, "LPM transaction interrupted");
  120. return -EINTR;
  121. }
  122. if (err) {
  123. bt_dev_err(hu->hdev, "LPM transaction timeout");
  124. return -ETIMEDOUT;
  125. }
  126. return err;
  127. }
  128. static int intel_lpm_suspend(struct hci_uart *hu)
  129. {
  130. static const u8 suspend[] = { 0x01, 0x01, 0x01 };
  131. struct intel_data *intel = hu->priv;
  132. struct sk_buff *skb;
  133. if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
  134. test_bit(STATE_SUSPENDED, &intel->flags))
  135. return 0;
  136. if (test_bit(STATE_TX_ACTIVE, &intel->flags))
  137. return -EAGAIN;
  138. bt_dev_dbg(hu->hdev, "Suspending");
  139. skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
  140. if (!skb) {
  141. bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
  142. return -ENOMEM;
  143. }
  144. skb_put_data(skb, suspend, sizeof(suspend));
  145. hci_skb_pkt_type(skb) = HCI_LPM_PKT;
  146. set_bit(STATE_LPM_TRANSACTION, &intel->flags);
  147. /* LPM flow is a priority, enqueue packet at list head */
  148. skb_queue_head(&intel->txq, skb);
  149. hci_uart_tx_wakeup(hu);
  150. intel_wait_lpm_transaction(hu);
  151. /* Even in case of failure, continue and test the suspended flag */
  152. clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
  153. if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
  154. bt_dev_err(hu->hdev, "Device suspend error");
  155. return -EINVAL;
  156. }
  157. bt_dev_dbg(hu->hdev, "Suspended");
  158. hci_uart_set_flow_control(hu, true);
  159. return 0;
  160. }
  161. static int intel_lpm_resume(struct hci_uart *hu)
  162. {
  163. struct intel_data *intel = hu->priv;
  164. struct sk_buff *skb;
  165. if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
  166. !test_bit(STATE_SUSPENDED, &intel->flags))
  167. return 0;
  168. bt_dev_dbg(hu->hdev, "Resuming");
  169. hci_uart_set_flow_control(hu, false);
  170. skb = bt_skb_alloc(0, GFP_KERNEL);
  171. if (!skb) {
  172. bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
  173. return -ENOMEM;
  174. }
  175. hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
  176. set_bit(STATE_LPM_TRANSACTION, &intel->flags);
  177. /* LPM flow is a priority, enqueue packet at list head */
  178. skb_queue_head(&intel->txq, skb);
  179. hci_uart_tx_wakeup(hu);
  180. intel_wait_lpm_transaction(hu);
  181. /* Even in case of failure, continue and test the suspended flag */
  182. clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
  183. if (test_bit(STATE_SUSPENDED, &intel->flags)) {
  184. bt_dev_err(hu->hdev, "Device resume error");
  185. return -EINVAL;
  186. }
  187. bt_dev_dbg(hu->hdev, "Resumed");
  188. return 0;
  189. }
  190. static int intel_lpm_host_wake(struct hci_uart *hu)
  191. {
  192. static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
  193. struct intel_data *intel = hu->priv;
  194. struct sk_buff *skb;
  195. hci_uart_set_flow_control(hu, false);
  196. clear_bit(STATE_SUSPENDED, &intel->flags);
  197. skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
  198. if (!skb) {
  199. bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
  200. return -ENOMEM;
  201. }
  202. skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
  203. hci_skb_pkt_type(skb) = HCI_LPM_PKT;
  204. /* LPM flow is a priority, enqueue packet at list head */
  205. skb_queue_head(&intel->txq, skb);
  206. hci_uart_tx_wakeup(hu);
  207. bt_dev_dbg(hu->hdev, "Resumed by controller");
  208. return 0;
  209. }
  210. static irqreturn_t intel_irq(int irq, void *dev_id)
  211. {
  212. struct intel_device *idev = dev_id;
  213. dev_info(&idev->pdev->dev, "hci_intel irq\n");
  214. mutex_lock(&idev->hu_lock);
  215. if (idev->hu)
  216. intel_lpm_host_wake(idev->hu);
  217. mutex_unlock(&idev->hu_lock);
  218. /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
  219. pm_runtime_get(&idev->pdev->dev);
  220. pm_runtime_put_autosuspend(&idev->pdev->dev);
  221. return IRQ_HANDLED;
  222. }
  223. static int intel_set_power(struct hci_uart *hu, bool powered)
  224. {
  225. struct intel_device *idev;
  226. int err = -ENODEV;
  227. if (!hu->tty->dev)
  228. return err;
  229. mutex_lock(&intel_device_list_lock);
  230. list_for_each_entry(idev, &intel_device_list, list) {
  231. /* tty device and pdev device should share the same parent
  232. * which is the UART port.
  233. */
  234. if (hu->tty->dev->parent != idev->pdev->dev.parent)
  235. continue;
  236. if (!idev->reset) {
  237. err = -ENOTSUPP;
  238. break;
  239. }
  240. BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
  241. hu, dev_name(&idev->pdev->dev), powered);
  242. gpiod_set_value(idev->reset, powered);
  243. /* Provide to idev a hu reference which is used to run LPM
  244. * transactions (lpm suspend/resume) from PM callbacks.
  245. * hu needs to be protected against concurrent removing during
  246. * these PM ops.
  247. */
  248. mutex_lock(&idev->hu_lock);
  249. idev->hu = powered ? hu : NULL;
  250. mutex_unlock(&idev->hu_lock);
  251. if (idev->irq < 0)
  252. break;
  253. if (powered && device_can_wakeup(&idev->pdev->dev)) {
  254. err = devm_request_threaded_irq(&idev->pdev->dev,
  255. idev->irq, NULL,
  256. intel_irq,
  257. IRQF_ONESHOT,
  258. "bt-host-wake", idev);
  259. if (err) {
  260. BT_ERR("hu %p, unable to allocate irq-%d",
  261. hu, idev->irq);
  262. break;
  263. }
  264. device_wakeup_enable(&idev->pdev->dev);
  265. pm_runtime_set_active(&idev->pdev->dev);
  266. pm_runtime_use_autosuspend(&idev->pdev->dev);
  267. pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
  268. LPM_SUSPEND_DELAY_MS);
  269. pm_runtime_enable(&idev->pdev->dev);
  270. } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
  271. devm_free_irq(&idev->pdev->dev, idev->irq, idev);
  272. device_wakeup_disable(&idev->pdev->dev);
  273. pm_runtime_disable(&idev->pdev->dev);
  274. }
  275. }
  276. mutex_unlock(&intel_device_list_lock);
  277. return err;
  278. }
  279. static void intel_busy_work(struct work_struct *work)
  280. {
  281. struct intel_data *intel = container_of(work, struct intel_data,
  282. busy_work);
  283. struct intel_device *idev;
  284. if (!intel->hu->tty->dev)
  285. return;
  286. /* Link is busy, delay the suspend */
  287. mutex_lock(&intel_device_list_lock);
  288. list_for_each_entry(idev, &intel_device_list, list) {
  289. if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
  290. pm_runtime_get(&idev->pdev->dev);
  291. pm_runtime_put_autosuspend(&idev->pdev->dev);
  292. break;
  293. }
  294. }
  295. mutex_unlock(&intel_device_list_lock);
  296. }
  297. static int intel_open(struct hci_uart *hu)
  298. {
  299. struct intel_data *intel;
  300. BT_DBG("hu %p", hu);
  301. if (!hci_uart_has_flow_control(hu))
  302. return -EOPNOTSUPP;
  303. intel = kzalloc_obj(*intel);
  304. if (!intel)
  305. return -ENOMEM;
  306. skb_queue_head_init(&intel->txq);
  307. INIT_WORK(&intel->busy_work, intel_busy_work);
  308. intel->hu = hu;
  309. hu->priv = intel;
  310. if (!intel_set_power(hu, true))
  311. set_bit(STATE_BOOTING, &intel->flags);
  312. return 0;
  313. }
  314. static int intel_close(struct hci_uart *hu)
  315. {
  316. struct intel_data *intel = hu->priv;
  317. BT_DBG("hu %p", hu);
  318. cancel_work_sync(&intel->busy_work);
  319. intel_set_power(hu, false);
  320. skb_queue_purge(&intel->txq);
  321. kfree_skb(intel->rx_skb);
  322. kfree(intel);
  323. hu->priv = NULL;
  324. return 0;
  325. }
  326. static int intel_flush(struct hci_uart *hu)
  327. {
  328. struct intel_data *intel = hu->priv;
  329. BT_DBG("hu %p", hu);
  330. skb_queue_purge(&intel->txq);
  331. return 0;
  332. }
  333. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  334. {
  335. struct sk_buff *skb;
  336. struct hci_event_hdr *hdr;
  337. struct hci_ev_cmd_complete *evt;
  338. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
  339. if (!skb)
  340. return -ENOMEM;
  341. hdr = skb_put(skb, sizeof(*hdr));
  342. hdr->evt = HCI_EV_CMD_COMPLETE;
  343. hdr->plen = sizeof(*evt) + 1;
  344. evt = skb_put(skb, sizeof(*evt));
  345. evt->ncmd = 0x01;
  346. evt->opcode = cpu_to_le16(opcode);
  347. skb_put_u8(skb, 0x00);
  348. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  349. return hci_recv_frame(hdev, skb);
  350. }
  351. static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
  352. {
  353. struct intel_data *intel = hu->priv;
  354. struct hci_dev *hdev = hu->hdev;
  355. u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
  356. struct sk_buff *skb;
  357. int err;
  358. /* This can be the first command sent to the chip, check
  359. * that the controller is ready.
  360. */
  361. err = intel_wait_booting(hu);
  362. clear_bit(STATE_BOOTING, &intel->flags);
  363. /* In case of timeout, try to continue anyway */
  364. if (err && err != -ETIMEDOUT)
  365. return err;
  366. bt_dev_info(hdev, "Change controller speed to %d", speed);
  367. speed_cmd[3] = intel_convert_speed(speed);
  368. if (speed_cmd[3] == 0xff) {
  369. bt_dev_err(hdev, "Unsupported speed");
  370. return -EINVAL;
  371. }
  372. /* Device will not accept speed change if Intel version has not been
  373. * previously requested.
  374. */
  375. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
  376. if (IS_ERR(skb)) {
  377. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  378. PTR_ERR(skb));
  379. return PTR_ERR(skb);
  380. }
  381. kfree_skb(skb);
  382. skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
  383. if (!skb) {
  384. bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
  385. return -ENOMEM;
  386. }
  387. skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
  388. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  389. hci_uart_set_flow_control(hu, true);
  390. skb_queue_tail(&intel->txq, skb);
  391. hci_uart_tx_wakeup(hu);
  392. /* wait 100ms to change baudrate on controller side */
  393. msleep(100);
  394. hci_uart_set_baudrate(hu, speed);
  395. hci_uart_set_flow_control(hu, false);
  396. return 0;
  397. }
  398. static int intel_setup(struct hci_uart *hu)
  399. {
  400. struct intel_data *intel = hu->priv;
  401. struct hci_dev *hdev = hu->hdev;
  402. struct sk_buff *skb;
  403. struct intel_version ver;
  404. struct intel_boot_params params;
  405. struct intel_device *idev;
  406. const struct firmware *fw;
  407. char fwname[64];
  408. u32 boot_param;
  409. ktime_t calltime, delta, rettime;
  410. unsigned long long duration;
  411. unsigned int init_speed, oper_speed;
  412. int speed_change = 0;
  413. int err;
  414. bt_dev_dbg(hdev, "");
  415. hu->hdev->set_diag = btintel_set_diag;
  416. hu->hdev->set_bdaddr = btintel_set_bdaddr;
  417. /* Set the default boot parameter to 0x0 and it is updated to
  418. * SKU specific boot parameter after reading Intel_Write_Boot_Params
  419. * command while downloading the firmware.
  420. */
  421. boot_param = 0x00000000;
  422. calltime = ktime_get();
  423. if (hu->init_speed)
  424. init_speed = hu->init_speed;
  425. else
  426. init_speed = hu->proto->init_speed;
  427. if (hu->oper_speed)
  428. oper_speed = hu->oper_speed;
  429. else
  430. oper_speed = hu->proto->oper_speed;
  431. if (oper_speed && init_speed && oper_speed != init_speed)
  432. speed_change = 1;
  433. /* Check that the controller is ready */
  434. err = intel_wait_booting(hu);
  435. clear_bit(STATE_BOOTING, &intel->flags);
  436. /* In case of timeout, try to continue anyway */
  437. if (err && err != -ETIMEDOUT)
  438. return err;
  439. set_bit(STATE_BOOTLOADER, &intel->flags);
  440. /* Read the Intel version information to determine if the device
  441. * is in bootloader mode or if it already has operational firmware
  442. * loaded.
  443. */
  444. err = btintel_read_version(hdev, &ver);
  445. if (err)
  446. return err;
  447. /* The hardware platform number has a fixed value of 0x37 and
  448. * for now only accept this single value.
  449. */
  450. if (ver.hw_platform != 0x37) {
  451. bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
  452. ver.hw_platform);
  453. return -EINVAL;
  454. }
  455. /* Check for supported iBT hardware variants of this firmware
  456. * loading method.
  457. *
  458. * This check has been put in place to ensure correct forward
  459. * compatibility options when newer hardware variants come along.
  460. */
  461. switch (ver.hw_variant) {
  462. case 0x0b: /* LnP */
  463. case 0x0c: /* WsP */
  464. case 0x12: /* ThP */
  465. break;
  466. default:
  467. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  468. ver.hw_variant);
  469. return -EINVAL;
  470. }
  471. btintel_version_info(hdev, &ver);
  472. /* The firmware variant determines if the device is in bootloader
  473. * mode or is running operational firmware. The value 0x06 identifies
  474. * the bootloader and the value 0x23 identifies the operational
  475. * firmware.
  476. *
  477. * When the operational firmware is already present, then only
  478. * the check for valid Bluetooth device address is needed. This
  479. * determines if the device will be added as configured or
  480. * unconfigured controller.
  481. *
  482. * It is not possible to use the Secure Boot Parameters in this
  483. * case since that command is only available in bootloader mode.
  484. */
  485. if (ver.fw_variant == 0x23) {
  486. clear_bit(STATE_BOOTLOADER, &intel->flags);
  487. btintel_check_bdaddr(hdev);
  488. return 0;
  489. }
  490. /* If the device is not in bootloader mode, then the only possible
  491. * choice is to return an error and abort the device initialization.
  492. */
  493. if (ver.fw_variant != 0x06) {
  494. bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
  495. ver.fw_variant);
  496. return -ENODEV;
  497. }
  498. /* Read the secure boot parameters to identify the operating
  499. * details of the bootloader.
  500. */
  501. err = btintel_read_boot_params(hdev, &params);
  502. if (err)
  503. return err;
  504. /* It is required that every single firmware fragment is acknowledged
  505. * with a command complete event. If the boot parameters indicate
  506. * that this bootloader does not send them, then abort the setup.
  507. */
  508. if (params.limited_cce != 0x00) {
  509. bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
  510. params.limited_cce);
  511. return -EINVAL;
  512. }
  513. /* If the OTP has no valid Bluetooth device address, then there will
  514. * also be no valid address for the operational firmware.
  515. */
  516. if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
  517. bt_dev_info(hdev, "No device address configured");
  518. hci_set_quirk(hdev, HCI_QUIRK_INVALID_BDADDR);
  519. }
  520. /* With this Intel bootloader only the hardware variant and device
  521. * revision information are used to select the right firmware for SfP
  522. * and WsP.
  523. *
  524. * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
  525. *
  526. * Currently the supported hardware variants are:
  527. * 11 (0x0b) for iBT 3.0 (LnP/SfP)
  528. * 12 (0x0c) for iBT 3.5 (WsP)
  529. *
  530. * For ThP/JfP and for future SKU's, the FW name varies based on HW
  531. * variant, HW revision and FW revision, as these are dependent on CNVi
  532. * and RF Combination.
  533. *
  534. * 18 (0x12) for iBT3.5 (ThP/JfP)
  535. *
  536. * The firmware file name for these will be
  537. * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
  538. *
  539. */
  540. switch (ver.hw_variant) {
  541. case 0x0b: /* SfP */
  542. case 0x0c: /* WsP */
  543. snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
  544. ver.hw_variant, le16_to_cpu(params.dev_revid));
  545. break;
  546. case 0x12: /* ThP */
  547. snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
  548. ver.hw_variant, ver.hw_revision, ver.fw_revision);
  549. break;
  550. default:
  551. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  552. ver.hw_variant);
  553. return -EINVAL;
  554. }
  555. err = request_firmware(&fw, fwname, &hdev->dev);
  556. if (err < 0) {
  557. bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
  558. err);
  559. return err;
  560. }
  561. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  562. /* Save the DDC file name for later */
  563. switch (ver.hw_variant) {
  564. case 0x0b: /* SfP */
  565. case 0x0c: /* WsP */
  566. snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
  567. ver.hw_variant, le16_to_cpu(params.dev_revid));
  568. break;
  569. case 0x12: /* ThP */
  570. snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
  571. ver.hw_variant, ver.hw_revision, ver.fw_revision);
  572. break;
  573. default:
  574. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  575. ver.hw_variant);
  576. return -EINVAL;
  577. }
  578. if (fw->size < 644) {
  579. bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
  580. fw->size);
  581. err = -EBADF;
  582. goto done;
  583. }
  584. set_bit(STATE_DOWNLOADING, &intel->flags);
  585. /* Start firmware downloading and get boot parameter */
  586. err = btintel_download_firmware(hdev, &ver, fw, &boot_param);
  587. if (err < 0)
  588. goto done;
  589. set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
  590. bt_dev_info(hdev, "Waiting for firmware download to complete");
  591. /* Before switching the device into operational mode and with that
  592. * booting the loaded firmware, wait for the bootloader notification
  593. * that all fragments have been successfully received.
  594. *
  595. * When the event processing receives the notification, then the
  596. * STATE_DOWNLOADING flag will be cleared.
  597. *
  598. * The firmware loading should not take longer than 5 seconds
  599. * and thus just timeout if that happens and fail the setup
  600. * of this device.
  601. */
  602. err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
  603. TASK_INTERRUPTIBLE,
  604. msecs_to_jiffies(5000));
  605. if (err == -EINTR) {
  606. bt_dev_err(hdev, "Firmware loading interrupted");
  607. err = -EINTR;
  608. goto done;
  609. }
  610. if (err) {
  611. bt_dev_err(hdev, "Firmware loading timeout");
  612. err = -ETIMEDOUT;
  613. goto done;
  614. }
  615. if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
  616. bt_dev_err(hdev, "Firmware loading failed");
  617. err = -ENOEXEC;
  618. goto done;
  619. }
  620. rettime = ktime_get();
  621. delta = ktime_sub(rettime, calltime);
  622. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  623. bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
  624. done:
  625. release_firmware(fw);
  626. /* Check if there was an error and if is not -EALREADY which means the
  627. * firmware has already been loaded.
  628. */
  629. if (err < 0 && err != -EALREADY)
  630. return err;
  631. /* We need to restore the default speed before Intel reset */
  632. if (speed_change) {
  633. err = intel_set_baudrate(hu, init_speed);
  634. if (err)
  635. return err;
  636. }
  637. calltime = ktime_get();
  638. set_bit(STATE_BOOTING, &intel->flags);
  639. err = btintel_send_intel_reset(hdev, boot_param);
  640. if (err)
  641. return err;
  642. /* The bootloader will not indicate when the device is ready. This
  643. * is done by the operational firmware sending bootup notification.
  644. *
  645. * Booting into operational firmware should not take longer than
  646. * 1 second. However if that happens, then just fail the setup
  647. * since something went wrong.
  648. */
  649. bt_dev_info(hdev, "Waiting for device to boot");
  650. err = intel_wait_booting(hu);
  651. if (err)
  652. return err;
  653. clear_bit(STATE_BOOTING, &intel->flags);
  654. rettime = ktime_get();
  655. delta = ktime_sub(rettime, calltime);
  656. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  657. bt_dev_info(hdev, "Device booted in %llu usecs", duration);
  658. /* Enable LPM if matching pdev with wakeup enabled, set TX active
  659. * until further LPM TX notification.
  660. */
  661. mutex_lock(&intel_device_list_lock);
  662. list_for_each_entry(idev, &intel_device_list, list) {
  663. if (!hu->tty->dev)
  664. break;
  665. if (hu->tty->dev->parent == idev->pdev->dev.parent) {
  666. if (device_may_wakeup(&idev->pdev->dev)) {
  667. set_bit(STATE_LPM_ENABLED, &intel->flags);
  668. set_bit(STATE_TX_ACTIVE, &intel->flags);
  669. }
  670. break;
  671. }
  672. }
  673. mutex_unlock(&intel_device_list_lock);
  674. /* Ignore errors, device can work without DDC parameters */
  675. btintel_load_ddc_config(hdev, fwname);
  676. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
  677. if (IS_ERR(skb))
  678. return PTR_ERR(skb);
  679. kfree_skb(skb);
  680. if (speed_change) {
  681. err = intel_set_baudrate(hu, oper_speed);
  682. if (err)
  683. return err;
  684. }
  685. bt_dev_info(hdev, "Setup complete");
  686. clear_bit(STATE_BOOTLOADER, &intel->flags);
  687. return 0;
  688. }
  689. static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  690. {
  691. struct hci_uart *hu = hci_get_drvdata(hdev);
  692. struct intel_data *intel = hu->priv;
  693. struct hci_event_hdr *hdr;
  694. if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
  695. !test_bit(STATE_BOOTING, &intel->flags))
  696. goto recv;
  697. hdr = (void *)skb->data;
  698. /* When the firmware loading completes the device sends
  699. * out a vendor specific event indicating the result of
  700. * the firmware loading.
  701. */
  702. if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
  703. skb->data[2] == 0x06) {
  704. if (skb->data[3] != 0x00)
  705. set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
  706. if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
  707. test_bit(STATE_FIRMWARE_LOADED, &intel->flags))
  708. wake_up_bit(&intel->flags, STATE_DOWNLOADING);
  709. /* When switching to the operational firmware the device
  710. * sends a vendor specific event indicating that the bootup
  711. * completed.
  712. */
  713. } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
  714. skb->data[2] == 0x02) {
  715. if (test_and_clear_bit(STATE_BOOTING, &intel->flags))
  716. wake_up_bit(&intel->flags, STATE_BOOTING);
  717. }
  718. recv:
  719. return hci_recv_frame(hdev, skb);
  720. }
  721. static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
  722. {
  723. struct hci_uart *hu = hci_get_drvdata(hdev);
  724. struct intel_data *intel = hu->priv;
  725. bt_dev_dbg(hdev, "TX idle notification (%d)", value);
  726. if (value) {
  727. set_bit(STATE_TX_ACTIVE, &intel->flags);
  728. schedule_work(&intel->busy_work);
  729. } else {
  730. clear_bit(STATE_TX_ACTIVE, &intel->flags);
  731. }
  732. }
  733. static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
  734. {
  735. struct hci_lpm_pkt *lpm = (void *)skb->data;
  736. struct hci_uart *hu = hci_get_drvdata(hdev);
  737. struct intel_data *intel = hu->priv;
  738. switch (lpm->opcode) {
  739. case LPM_OP_TX_NOTIFY:
  740. if (lpm->dlen < 1) {
  741. bt_dev_err(hu->hdev, "Invalid LPM notification packet");
  742. break;
  743. }
  744. intel_recv_lpm_notify(hdev, lpm->data[0]);
  745. break;
  746. case LPM_OP_SUSPEND_ACK:
  747. set_bit(STATE_SUSPENDED, &intel->flags);
  748. if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
  749. wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
  750. break;
  751. case LPM_OP_RESUME_ACK:
  752. clear_bit(STATE_SUSPENDED, &intel->flags);
  753. if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
  754. wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
  755. break;
  756. default:
  757. bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
  758. break;
  759. }
  760. kfree_skb(skb);
  761. return 0;
  762. }
  763. #define INTEL_RECV_LPM \
  764. .type = HCI_LPM_PKT, \
  765. .hlen = HCI_LPM_HDR_SIZE, \
  766. .loff = 1, \
  767. .lsize = 1, \
  768. .maxlen = HCI_LPM_MAX_SIZE
  769. static const struct h4_recv_pkt intel_recv_pkts[] = {
  770. { H4_RECV_ACL, .recv = hci_recv_frame },
  771. { H4_RECV_SCO, .recv = hci_recv_frame },
  772. { H4_RECV_EVENT, .recv = intel_recv_event },
  773. { INTEL_RECV_LPM, .recv = intel_recv_lpm },
  774. };
  775. static int intel_recv(struct hci_uart *hu, const void *data, int count)
  776. {
  777. struct intel_data *intel = hu->priv;
  778. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  779. return -EUNATCH;
  780. intel->rx_skb = h4_recv_buf(hu, intel->rx_skb, data, count,
  781. intel_recv_pkts,
  782. ARRAY_SIZE(intel_recv_pkts));
  783. if (IS_ERR(intel->rx_skb)) {
  784. int err = PTR_ERR(intel->rx_skb);
  785. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  786. intel->rx_skb = NULL;
  787. return err;
  788. }
  789. return count;
  790. }
  791. static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  792. {
  793. struct intel_data *intel = hu->priv;
  794. struct intel_device *idev;
  795. BT_DBG("hu %p skb %p", hu, skb);
  796. if (!hu->tty->dev)
  797. goto out_enqueue;
  798. /* Be sure our controller is resumed and potential LPM transaction
  799. * completed before enqueuing any packet.
  800. */
  801. mutex_lock(&intel_device_list_lock);
  802. list_for_each_entry(idev, &intel_device_list, list) {
  803. if (hu->tty->dev->parent == idev->pdev->dev.parent) {
  804. pm_runtime_get_sync(&idev->pdev->dev);
  805. pm_runtime_put_autosuspend(&idev->pdev->dev);
  806. break;
  807. }
  808. }
  809. mutex_unlock(&intel_device_list_lock);
  810. out_enqueue:
  811. skb_queue_tail(&intel->txq, skb);
  812. return 0;
  813. }
  814. static struct sk_buff *intel_dequeue(struct hci_uart *hu)
  815. {
  816. struct intel_data *intel = hu->priv;
  817. struct sk_buff *skb;
  818. skb = skb_dequeue(&intel->txq);
  819. if (!skb)
  820. return skb;
  821. if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
  822. (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
  823. struct hci_command_hdr *cmd = (void *)skb->data;
  824. __u16 opcode = le16_to_cpu(cmd->opcode);
  825. /* When the BTINTEL_HCI_OP_RESET command is issued to boot into
  826. * the operational firmware, it will actually not send a command
  827. * complete event. To keep the flow control working inject that
  828. * event here.
  829. */
  830. if (opcode == BTINTEL_HCI_OP_RESET)
  831. inject_cmd_complete(hu->hdev, opcode);
  832. }
  833. /* Prepend skb with frame type */
  834. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  835. return skb;
  836. }
  837. static const struct hci_uart_proto intel_proto = {
  838. .id = HCI_UART_INTEL,
  839. .name = "Intel",
  840. .manufacturer = 2,
  841. .init_speed = 115200,
  842. .oper_speed = 3000000,
  843. .open = intel_open,
  844. .close = intel_close,
  845. .flush = intel_flush,
  846. .setup = intel_setup,
  847. .set_baudrate = intel_set_baudrate,
  848. .recv = intel_recv,
  849. .enqueue = intel_enqueue,
  850. .dequeue = intel_dequeue,
  851. };
  852. #ifdef CONFIG_ACPI
  853. static const struct acpi_device_id intel_acpi_match[] = {
  854. { "INT33E1", 0 },
  855. { "INT33E3", 0 },
  856. { }
  857. };
  858. MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
  859. #endif
  860. static int intel_suspend_device(struct device *dev)
  861. {
  862. struct intel_device *idev = dev_get_drvdata(dev);
  863. mutex_lock(&idev->hu_lock);
  864. if (idev->hu)
  865. intel_lpm_suspend(idev->hu);
  866. mutex_unlock(&idev->hu_lock);
  867. return 0;
  868. }
  869. static int intel_resume_device(struct device *dev)
  870. {
  871. struct intel_device *idev = dev_get_drvdata(dev);
  872. mutex_lock(&idev->hu_lock);
  873. if (idev->hu)
  874. intel_lpm_resume(idev->hu);
  875. mutex_unlock(&idev->hu_lock);
  876. return 0;
  877. }
  878. static int __maybe_unused intel_suspend(struct device *dev)
  879. {
  880. struct intel_device *idev = dev_get_drvdata(dev);
  881. if (device_may_wakeup(dev))
  882. enable_irq_wake(idev->irq);
  883. return intel_suspend_device(dev);
  884. }
  885. static int __maybe_unused intel_resume(struct device *dev)
  886. {
  887. struct intel_device *idev = dev_get_drvdata(dev);
  888. if (device_may_wakeup(dev))
  889. disable_irq_wake(idev->irq);
  890. return intel_resume_device(dev);
  891. }
  892. static const struct dev_pm_ops intel_pm_ops = {
  893. SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
  894. SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
  895. };
  896. static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
  897. static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
  898. static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
  899. { "reset-gpios", &reset_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  900. { "host-wake-gpios", &host_wake_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  901. { }
  902. };
  903. static int intel_probe(struct platform_device *pdev)
  904. {
  905. struct intel_device *idev;
  906. int ret;
  907. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  908. if (!idev)
  909. return -ENOMEM;
  910. mutex_init(&idev->hu_lock);
  911. idev->pdev = pdev;
  912. ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
  913. if (ret)
  914. dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
  915. idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
  916. if (IS_ERR(idev->reset)) {
  917. dev_err(&pdev->dev, "Unable to retrieve gpio\n");
  918. return PTR_ERR(idev->reset);
  919. }
  920. idev->irq = platform_get_irq(pdev, 0);
  921. if (idev->irq < 0) {
  922. struct gpio_desc *host_wake;
  923. dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
  924. host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
  925. if (IS_ERR(host_wake)) {
  926. dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
  927. goto no_irq;
  928. }
  929. idev->irq = gpiod_to_irq(host_wake);
  930. if (idev->irq < 0) {
  931. dev_err(&pdev->dev, "No corresponding irq for gpio\n");
  932. goto no_irq;
  933. }
  934. }
  935. /* Only enable wake-up/irq when controller is powered */
  936. device_set_wakeup_capable(&pdev->dev, true);
  937. device_wakeup_disable(&pdev->dev);
  938. no_irq:
  939. platform_set_drvdata(pdev, idev);
  940. /* Place this instance on the device list */
  941. mutex_lock(&intel_device_list_lock);
  942. list_add_tail(&idev->list, &intel_device_list);
  943. mutex_unlock(&intel_device_list_lock);
  944. dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
  945. desc_to_gpio(idev->reset), idev->irq);
  946. return 0;
  947. }
  948. static void intel_remove(struct platform_device *pdev)
  949. {
  950. struct intel_device *idev = platform_get_drvdata(pdev);
  951. device_wakeup_disable(&pdev->dev);
  952. mutex_lock(&intel_device_list_lock);
  953. list_del(&idev->list);
  954. mutex_unlock(&intel_device_list_lock);
  955. dev_info(&pdev->dev, "unregistered.\n");
  956. }
  957. static struct platform_driver intel_driver = {
  958. .probe = intel_probe,
  959. .remove = intel_remove,
  960. .driver = {
  961. .name = "hci_intel",
  962. .acpi_match_table = ACPI_PTR(intel_acpi_match),
  963. .pm = &intel_pm_ops,
  964. },
  965. };
  966. int __init intel_init(void)
  967. {
  968. int err;
  969. err = platform_driver_register(&intel_driver);
  970. if (err)
  971. return err;
  972. return hci_uart_register_proto(&intel_proto);
  973. }
  974. int __exit intel_deinit(void)
  975. {
  976. platform_driver_unregister(&intel_driver);
  977. return hci_uart_unregister_proto(&intel_proto);
  978. }