hci_intel.c 31 KB

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