hci_intel.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. intel = kzalloc(sizeof(*intel), GFP_KERNEL);
  326. if (!intel)
  327. return -ENOMEM;
  328. skb_queue_head_init(&intel->txq);
  329. INIT_WORK(&intel->busy_work, intel_busy_work);
  330. intel->hu = hu;
  331. hu->priv = intel;
  332. if (!intel_set_power(hu, true))
  333. set_bit(STATE_BOOTING, &intel->flags);
  334. return 0;
  335. }
  336. static int intel_close(struct hci_uart *hu)
  337. {
  338. struct intel_data *intel = hu->priv;
  339. BT_DBG("hu %p", hu);
  340. cancel_work_sync(&intel->busy_work);
  341. intel_set_power(hu, false);
  342. skb_queue_purge(&intel->txq);
  343. kfree_skb(intel->rx_skb);
  344. kfree(intel);
  345. hu->priv = NULL;
  346. return 0;
  347. }
  348. static int intel_flush(struct hci_uart *hu)
  349. {
  350. struct intel_data *intel = hu->priv;
  351. BT_DBG("hu %p", hu);
  352. skb_queue_purge(&intel->txq);
  353. return 0;
  354. }
  355. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  356. {
  357. struct sk_buff *skb;
  358. struct hci_event_hdr *hdr;
  359. struct hci_ev_cmd_complete *evt;
  360. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
  361. if (!skb)
  362. return -ENOMEM;
  363. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  364. hdr->evt = HCI_EV_CMD_COMPLETE;
  365. hdr->plen = sizeof(*evt) + 1;
  366. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  367. evt->ncmd = 0x01;
  368. evt->opcode = cpu_to_le16(opcode);
  369. *skb_put(skb, 1) = 0x00;
  370. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  371. return hci_recv_frame(hdev, skb);
  372. }
  373. static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
  374. {
  375. struct intel_data *intel = hu->priv;
  376. struct hci_dev *hdev = hu->hdev;
  377. u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
  378. struct sk_buff *skb;
  379. int err;
  380. /* This can be the first command sent to the chip, check
  381. * that the controller is ready.
  382. */
  383. err = intel_wait_booting(hu);
  384. clear_bit(STATE_BOOTING, &intel->flags);
  385. /* In case of timeout, try to continue anyway */
  386. if (err && err != -ETIMEDOUT)
  387. return err;
  388. bt_dev_info(hdev, "Change controller speed to %d", speed);
  389. speed_cmd[3] = intel_convert_speed(speed);
  390. if (speed_cmd[3] == 0xff) {
  391. bt_dev_err(hdev, "Unsupported speed");
  392. return -EINVAL;
  393. }
  394. /* Device will not accept speed change if Intel version has not been
  395. * previously requested.
  396. */
  397. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
  398. if (IS_ERR(skb)) {
  399. bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
  400. PTR_ERR(skb));
  401. return PTR_ERR(skb);
  402. }
  403. kfree_skb(skb);
  404. skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
  405. if (!skb) {
  406. bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
  407. return -ENOMEM;
  408. }
  409. memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
  410. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  411. hci_uart_set_flow_control(hu, true);
  412. skb_queue_tail(&intel->txq, skb);
  413. hci_uart_tx_wakeup(hu);
  414. /* wait 100ms to change baudrate on controller side */
  415. msleep(100);
  416. hci_uart_set_baudrate(hu, speed);
  417. hci_uart_set_flow_control(hu, false);
  418. return 0;
  419. }
  420. static int intel_setup(struct hci_uart *hu)
  421. {
  422. static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
  423. 0x00, 0x08, 0x04, 0x00 };
  424. struct intel_data *intel = hu->priv;
  425. struct hci_dev *hdev = hu->hdev;
  426. struct sk_buff *skb;
  427. struct intel_version ver;
  428. struct intel_boot_params *params;
  429. struct list_head *p;
  430. const struct firmware *fw;
  431. const u8 *fw_ptr;
  432. char fwname[64];
  433. u32 frag_len;
  434. ktime_t calltime, delta, rettime;
  435. unsigned long long duration;
  436. unsigned int init_speed, oper_speed;
  437. int speed_change = 0;
  438. int err;
  439. bt_dev_dbg(hdev, "start intel_setup");
  440. hu->hdev->set_diag = btintel_set_diag;
  441. hu->hdev->set_bdaddr = btintel_set_bdaddr;
  442. calltime = ktime_get();
  443. if (hu->init_speed)
  444. init_speed = hu->init_speed;
  445. else
  446. init_speed = hu->proto->init_speed;
  447. if (hu->oper_speed)
  448. oper_speed = hu->oper_speed;
  449. else
  450. oper_speed = hu->proto->oper_speed;
  451. if (oper_speed && init_speed && oper_speed != init_speed)
  452. speed_change = 1;
  453. /* Check that the controller is ready */
  454. err = intel_wait_booting(hu);
  455. clear_bit(STATE_BOOTING, &intel->flags);
  456. /* In case of timeout, try to continue anyway */
  457. if (err && err != -ETIMEDOUT)
  458. return err;
  459. set_bit(STATE_BOOTLOADER, &intel->flags);
  460. /* Read the Intel version information to determine if the device
  461. * is in bootloader mode or if it already has operational firmware
  462. * loaded.
  463. */
  464. err = btintel_read_version(hdev, &ver);
  465. if (err)
  466. return err;
  467. /* The hardware platform number has a fixed value of 0x37 and
  468. * for now only accept this single value.
  469. */
  470. if (ver.hw_platform != 0x37) {
  471. bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
  472. ver.hw_platform);
  473. return -EINVAL;
  474. }
  475. /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
  476. * supported by this firmware loading method. This check has been
  477. * put in place to ensure correct forward compatibility options
  478. * when newer hardware variants come along.
  479. */
  480. if (ver.hw_variant != 0x0b) {
  481. bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
  482. ver.hw_variant);
  483. return -EINVAL;
  484. }
  485. btintel_version_info(hdev, &ver);
  486. /* The firmware variant determines if the device is in bootloader
  487. * mode or is running operational firmware. The value 0x06 identifies
  488. * the bootloader and the value 0x23 identifies the operational
  489. * firmware.
  490. *
  491. * When the operational firmware is already present, then only
  492. * the check for valid Bluetooth device address is needed. This
  493. * determines if the device will be added as configured or
  494. * unconfigured controller.
  495. *
  496. * It is not possible to use the Secure Boot Parameters in this
  497. * case since that command is only available in bootloader mode.
  498. */
  499. if (ver.fw_variant == 0x23) {
  500. clear_bit(STATE_BOOTLOADER, &intel->flags);
  501. btintel_check_bdaddr(hdev);
  502. return 0;
  503. }
  504. /* If the device is not in bootloader mode, then the only possible
  505. * choice is to return an error and abort the device initialization.
  506. */
  507. if (ver.fw_variant != 0x06) {
  508. bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
  509. ver.fw_variant);
  510. return -ENODEV;
  511. }
  512. /* Read the secure boot parameters to identify the operating
  513. * details of the bootloader.
  514. */
  515. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_CMD_TIMEOUT);
  516. if (IS_ERR(skb)) {
  517. bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
  518. PTR_ERR(skb));
  519. return PTR_ERR(skb);
  520. }
  521. if (skb->len != sizeof(*params)) {
  522. bt_dev_err(hdev, "Intel boot parameters size mismatch");
  523. kfree_skb(skb);
  524. return -EILSEQ;
  525. }
  526. params = (struct intel_boot_params *)skb->data;
  527. if (params->status) {
  528. bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
  529. params->status);
  530. err = -bt_to_errno(params->status);
  531. kfree_skb(skb);
  532. return err;
  533. }
  534. bt_dev_info(hdev, "Device revision is %u",
  535. le16_to_cpu(params->dev_revid));
  536. bt_dev_info(hdev, "Secure boot is %s",
  537. params->secure_boot ? "enabled" : "disabled");
  538. bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
  539. params->min_fw_build_nn, params->min_fw_build_cw,
  540. 2000 + params->min_fw_build_yy);
  541. /* It is required that every single firmware fragment is acknowledged
  542. * with a command complete event. If the boot parameters indicate
  543. * that this bootloader does not send them, then abort the setup.
  544. */
  545. if (params->limited_cce != 0x00) {
  546. bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
  547. params->limited_cce);
  548. kfree_skb(skb);
  549. return -EINVAL;
  550. }
  551. /* If the OTP has no valid Bluetooth device address, then there will
  552. * also be no valid address for the operational firmware.
  553. */
  554. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  555. bt_dev_info(hdev, "No device address configured");
  556. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  557. }
  558. /* With this Intel bootloader only the hardware variant and device
  559. * revision information are used to select the right firmware.
  560. *
  561. * Currently this bootloader support is limited to hardware variant
  562. * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
  563. */
  564. snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
  565. le16_to_cpu(params->dev_revid));
  566. err = reject_firmware(&fw, fwname, &hdev->dev);
  567. if (err < 0) {
  568. bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
  569. err);
  570. kfree_skb(skb);
  571. return err;
  572. }
  573. bt_dev_info(hdev, "Found device firmware: %s", fwname);
  574. /* Save the DDC file name for later */
  575. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
  576. le16_to_cpu(params->dev_revid));
  577. kfree_skb(skb);
  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 the firmware download transaction with the Init fragment
  586. * represented by the 128 bytes of CSS header.
  587. */
  588. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  589. if (err < 0) {
  590. bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
  591. goto done;
  592. }
  593. /* Send the 256 bytes of public key information from the firmware
  594. * as the PKey fragment.
  595. */
  596. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  597. if (err < 0) {
  598. bt_dev_err(hdev, "Failed to send firmware public key (%d)",
  599. err);
  600. goto done;
  601. }
  602. /* Send the 256 bytes of signature information from the firmware
  603. * as the Sign fragment.
  604. */
  605. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  606. if (err < 0) {
  607. bt_dev_err(hdev, "Failed to send firmware signature (%d)",
  608. err);
  609. goto done;
  610. }
  611. fw_ptr = fw->data + 644;
  612. frag_len = 0;
  613. while (fw_ptr - fw->data < fw->size) {
  614. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  615. frag_len += sizeof(*cmd) + cmd->plen;
  616. bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
  617. fw->size);
  618. /* The parameter length of the secure send command requires
  619. * a 4 byte alignment. It happens so that the firmware file
  620. * contains proper Intel_NOP commands to align the fragments
  621. * as needed.
  622. *
  623. * Send set of commands with 4 byte alignment from the
  624. * firmware data buffer as a single Data fragement.
  625. */
  626. if (frag_len % 4)
  627. continue;
  628. /* Send each command from the firmware data buffer as
  629. * a single Data fragment.
  630. */
  631. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  632. if (err < 0) {
  633. bt_dev_err(hdev, "Failed to send firmware data (%d)",
  634. err);
  635. goto done;
  636. }
  637. fw_ptr += frag_len;
  638. frag_len = 0;
  639. }
  640. set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
  641. bt_dev_info(hdev, "Waiting for firmware download to complete");
  642. /* Before switching the device into operational mode and with that
  643. * booting the loaded firmware, wait for the bootloader notification
  644. * that all fragments have been successfully received.
  645. *
  646. * When the event processing receives the notification, then the
  647. * STATE_DOWNLOADING flag will be cleared.
  648. *
  649. * The firmware loading should not take longer than 5 seconds
  650. * and thus just timeout if that happens and fail the setup
  651. * of this device.
  652. */
  653. err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
  654. TASK_INTERRUPTIBLE,
  655. msecs_to_jiffies(5000));
  656. if (err == -EINTR) {
  657. bt_dev_err(hdev, "Firmware loading interrupted");
  658. err = -EINTR;
  659. goto done;
  660. }
  661. if (err) {
  662. bt_dev_err(hdev, "Firmware loading timeout");
  663. err = -ETIMEDOUT;
  664. goto done;
  665. }
  666. if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
  667. bt_dev_err(hdev, "Firmware loading failed");
  668. err = -ENOEXEC;
  669. goto done;
  670. }
  671. rettime = ktime_get();
  672. delta = ktime_sub(rettime, calltime);
  673. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  674. bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
  675. done:
  676. release_firmware(fw);
  677. if (err < 0)
  678. return err;
  679. /* We need to restore the default speed before Intel reset */
  680. if (speed_change) {
  681. err = intel_set_baudrate(hu, init_speed);
  682. if (err)
  683. return err;
  684. }
  685. calltime = ktime_get();
  686. set_bit(STATE_BOOTING, &intel->flags);
  687. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
  688. HCI_CMD_TIMEOUT);
  689. if (IS_ERR(skb))
  690. return PTR_ERR(skb);
  691. kfree_skb(skb);
  692. /* The bootloader will not indicate when the device is ready. This
  693. * is done by the operational firmware sending bootup notification.
  694. *
  695. * Booting into operational firmware should not take longer than
  696. * 1 second. However if that happens, then just fail the setup
  697. * since something went wrong.
  698. */
  699. bt_dev_info(hdev, "Waiting for device to boot");
  700. err = intel_wait_booting(hu);
  701. if (err)
  702. return err;
  703. clear_bit(STATE_BOOTING, &intel->flags);
  704. rettime = ktime_get();
  705. delta = ktime_sub(rettime, calltime);
  706. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  707. bt_dev_info(hdev, "Device booted in %llu usecs", duration);
  708. /* Enable LPM if matching pdev with wakeup enabled, set TX active
  709. * until further LPM TX notification.
  710. */
  711. mutex_lock(&intel_device_list_lock);
  712. list_for_each(p, &intel_device_list) {
  713. struct intel_device *dev = list_entry(p, struct intel_device,
  714. list);
  715. if (!hu->tty->dev)
  716. break;
  717. if (hu->tty->dev->parent == dev->pdev->dev.parent) {
  718. if (device_may_wakeup(&dev->pdev->dev)) {
  719. set_bit(STATE_LPM_ENABLED, &intel->flags);
  720. set_bit(STATE_TX_ACTIVE, &intel->flags);
  721. }
  722. break;
  723. }
  724. }
  725. mutex_unlock(&intel_device_list_lock);
  726. /* Ignore errors, device can work without DDC parameters */
  727. btintel_load_ddc_config(hdev, fwname);
  728. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
  729. if (IS_ERR(skb))
  730. return PTR_ERR(skb);
  731. kfree_skb(skb);
  732. if (speed_change) {
  733. err = intel_set_baudrate(hu, oper_speed);
  734. if (err)
  735. return err;
  736. }
  737. bt_dev_info(hdev, "Setup complete");
  738. clear_bit(STATE_BOOTLOADER, &intel->flags);
  739. return 0;
  740. }
  741. static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  742. {
  743. struct hci_uart *hu = hci_get_drvdata(hdev);
  744. struct intel_data *intel = hu->priv;
  745. struct hci_event_hdr *hdr;
  746. if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
  747. !test_bit(STATE_BOOTING, &intel->flags))
  748. goto recv;
  749. hdr = (void *)skb->data;
  750. /* When the firmware loading completes the device sends
  751. * out a vendor specific event indicating the result of
  752. * the firmware loading.
  753. */
  754. if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
  755. skb->data[2] == 0x06) {
  756. if (skb->data[3] != 0x00)
  757. set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
  758. if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
  759. test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
  760. smp_mb__after_atomic();
  761. wake_up_bit(&intel->flags, STATE_DOWNLOADING);
  762. }
  763. /* When switching to the operational firmware the device
  764. * sends a vendor specific event indicating that the bootup
  765. * completed.
  766. */
  767. } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
  768. skb->data[2] == 0x02) {
  769. if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
  770. smp_mb__after_atomic();
  771. wake_up_bit(&intel->flags, STATE_BOOTING);
  772. }
  773. }
  774. recv:
  775. return hci_recv_frame(hdev, skb);
  776. }
  777. static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
  778. {
  779. struct hci_uart *hu = hci_get_drvdata(hdev);
  780. struct intel_data *intel = hu->priv;
  781. bt_dev_dbg(hdev, "TX idle notification (%d)", value);
  782. if (value) {
  783. set_bit(STATE_TX_ACTIVE, &intel->flags);
  784. schedule_work(&intel->busy_work);
  785. } else {
  786. clear_bit(STATE_TX_ACTIVE, &intel->flags);
  787. }
  788. }
  789. static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
  790. {
  791. struct hci_lpm_pkt *lpm = (void *)skb->data;
  792. struct hci_uart *hu = hci_get_drvdata(hdev);
  793. struct intel_data *intel = hu->priv;
  794. switch (lpm->opcode) {
  795. case LPM_OP_TX_NOTIFY:
  796. if (lpm->dlen < 1) {
  797. bt_dev_err(hu->hdev, "Invalid LPM notification packet");
  798. break;
  799. }
  800. intel_recv_lpm_notify(hdev, lpm->data[0]);
  801. break;
  802. case LPM_OP_SUSPEND_ACK:
  803. set_bit(STATE_SUSPENDED, &intel->flags);
  804. if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
  805. smp_mb__after_atomic();
  806. wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
  807. }
  808. break;
  809. case LPM_OP_RESUME_ACK:
  810. clear_bit(STATE_SUSPENDED, &intel->flags);
  811. if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
  812. smp_mb__after_atomic();
  813. wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
  814. }
  815. break;
  816. default:
  817. bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
  818. break;
  819. }
  820. kfree_skb(skb);
  821. return 0;
  822. }
  823. #define INTEL_RECV_LPM \
  824. .type = HCI_LPM_PKT, \
  825. .hlen = HCI_LPM_HDR_SIZE, \
  826. .loff = 1, \
  827. .lsize = 1, \
  828. .maxlen = HCI_LPM_MAX_SIZE
  829. static const struct h4_recv_pkt intel_recv_pkts[] = {
  830. { H4_RECV_ACL, .recv = hci_recv_frame },
  831. { H4_RECV_SCO, .recv = hci_recv_frame },
  832. { H4_RECV_EVENT, .recv = intel_recv_event },
  833. { INTEL_RECV_LPM, .recv = intel_recv_lpm },
  834. };
  835. static int intel_recv(struct hci_uart *hu, const void *data, int count)
  836. {
  837. struct intel_data *intel = hu->priv;
  838. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  839. return -EUNATCH;
  840. intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
  841. intel_recv_pkts,
  842. ARRAY_SIZE(intel_recv_pkts));
  843. if (IS_ERR(intel->rx_skb)) {
  844. int err = PTR_ERR(intel->rx_skb);
  845. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  846. intel->rx_skb = NULL;
  847. return err;
  848. }
  849. return count;
  850. }
  851. static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  852. {
  853. struct intel_data *intel = hu->priv;
  854. struct list_head *p;
  855. BT_DBG("hu %p skb %p", hu, skb);
  856. if (!hu->tty->dev)
  857. goto out_enqueue;
  858. /* Be sure our controller is resumed and potential LPM transaction
  859. * completed before enqueuing any packet.
  860. */
  861. mutex_lock(&intel_device_list_lock);
  862. list_for_each(p, &intel_device_list) {
  863. struct intel_device *idev = list_entry(p, struct intel_device,
  864. list);
  865. if (hu->tty->dev->parent == idev->pdev->dev.parent) {
  866. pm_runtime_get_sync(&idev->pdev->dev);
  867. pm_runtime_mark_last_busy(&idev->pdev->dev);
  868. pm_runtime_put_autosuspend(&idev->pdev->dev);
  869. break;
  870. }
  871. }
  872. mutex_unlock(&intel_device_list_lock);
  873. out_enqueue:
  874. skb_queue_tail(&intel->txq, skb);
  875. return 0;
  876. }
  877. static struct sk_buff *intel_dequeue(struct hci_uart *hu)
  878. {
  879. struct intel_data *intel = hu->priv;
  880. struct sk_buff *skb;
  881. skb = skb_dequeue(&intel->txq);
  882. if (!skb)
  883. return skb;
  884. if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
  885. (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
  886. struct hci_command_hdr *cmd = (void *)skb->data;
  887. __u16 opcode = le16_to_cpu(cmd->opcode);
  888. /* When the 0xfc01 command is issued to boot into
  889. * the operational firmware, it will actually not
  890. * send a command complete event. To keep the flow
  891. * control working inject that event here.
  892. */
  893. if (opcode == 0xfc01)
  894. inject_cmd_complete(hu->hdev, opcode);
  895. }
  896. /* Prepend skb with frame type */
  897. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  898. return skb;
  899. }
  900. static const struct hci_uart_proto intel_proto = {
  901. .id = HCI_UART_INTEL,
  902. .name = "Intel",
  903. .manufacturer = 2,
  904. .init_speed = 115200,
  905. .oper_speed = 3000000,
  906. .open = intel_open,
  907. .close = intel_close,
  908. .flush = intel_flush,
  909. .setup = intel_setup,
  910. .set_baudrate = intel_set_baudrate,
  911. .recv = intel_recv,
  912. .enqueue = intel_enqueue,
  913. .dequeue = intel_dequeue,
  914. };
  915. #ifdef CONFIG_ACPI
  916. static const struct acpi_device_id intel_acpi_match[] = {
  917. { "INT33E1", 0 },
  918. { },
  919. };
  920. MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
  921. #endif
  922. #ifdef CONFIG_PM
  923. static int intel_suspend_device(struct device *dev)
  924. {
  925. struct intel_device *idev = dev_get_drvdata(dev);
  926. mutex_lock(&idev->hu_lock);
  927. if (idev->hu)
  928. intel_lpm_suspend(idev->hu);
  929. mutex_unlock(&idev->hu_lock);
  930. return 0;
  931. }
  932. static int intel_resume_device(struct device *dev)
  933. {
  934. struct intel_device *idev = dev_get_drvdata(dev);
  935. mutex_lock(&idev->hu_lock);
  936. if (idev->hu)
  937. intel_lpm_resume(idev->hu);
  938. mutex_unlock(&idev->hu_lock);
  939. return 0;
  940. }
  941. #endif
  942. #ifdef CONFIG_PM_SLEEP
  943. static int intel_suspend(struct device *dev)
  944. {
  945. struct intel_device *idev = dev_get_drvdata(dev);
  946. if (device_may_wakeup(dev))
  947. enable_irq_wake(idev->irq);
  948. return intel_suspend_device(dev);
  949. }
  950. static int intel_resume(struct device *dev)
  951. {
  952. struct intel_device *idev = dev_get_drvdata(dev);
  953. if (device_may_wakeup(dev))
  954. disable_irq_wake(idev->irq);
  955. return intel_resume_device(dev);
  956. }
  957. #endif
  958. static const struct dev_pm_ops intel_pm_ops = {
  959. SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
  960. SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
  961. };
  962. static int intel_probe(struct platform_device *pdev)
  963. {
  964. struct intel_device *idev;
  965. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  966. if (!idev)
  967. return -ENOMEM;
  968. mutex_init(&idev->hu_lock);
  969. idev->pdev = pdev;
  970. idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
  971. if (IS_ERR(idev->reset)) {
  972. dev_err(&pdev->dev, "Unable to retrieve gpio\n");
  973. return PTR_ERR(idev->reset);
  974. }
  975. idev->irq = platform_get_irq(pdev, 0);
  976. if (idev->irq < 0) {
  977. struct gpio_desc *host_wake;
  978. dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
  979. host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
  980. if (IS_ERR(host_wake)) {
  981. dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
  982. goto no_irq;
  983. }
  984. idev->irq = gpiod_to_irq(host_wake);
  985. if (idev->irq < 0) {
  986. dev_err(&pdev->dev, "No corresponding irq for gpio\n");
  987. goto no_irq;
  988. }
  989. }
  990. /* Only enable wake-up/irq when controller is powered */
  991. device_set_wakeup_capable(&pdev->dev, true);
  992. device_wakeup_disable(&pdev->dev);
  993. no_irq:
  994. platform_set_drvdata(pdev, idev);
  995. /* Place this instance on the device list */
  996. mutex_lock(&intel_device_list_lock);
  997. list_add_tail(&idev->list, &intel_device_list);
  998. mutex_unlock(&intel_device_list_lock);
  999. dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
  1000. desc_to_gpio(idev->reset), idev->irq);
  1001. return 0;
  1002. }
  1003. static int intel_remove(struct platform_device *pdev)
  1004. {
  1005. struct intel_device *idev = platform_get_drvdata(pdev);
  1006. device_wakeup_disable(&pdev->dev);
  1007. mutex_lock(&intel_device_list_lock);
  1008. list_del(&idev->list);
  1009. mutex_unlock(&intel_device_list_lock);
  1010. dev_info(&pdev->dev, "unregistered.\n");
  1011. return 0;
  1012. }
  1013. static struct platform_driver intel_driver = {
  1014. .probe = intel_probe,
  1015. .remove = intel_remove,
  1016. .driver = {
  1017. .name = "hci_intel",
  1018. .acpi_match_table = ACPI_PTR(intel_acpi_match),
  1019. .pm = &intel_pm_ops,
  1020. },
  1021. };
  1022. int __init intel_init(void)
  1023. {
  1024. platform_driver_register(&intel_driver);
  1025. return hci_uart_register_proto(&intel_proto);
  1026. }
  1027. int __exit intel_deinit(void)
  1028. {
  1029. platform_driver_unregister(&intel_driver);
  1030. return hci_uart_unregister_proto(&intel_proto);
  1031. }