hci_intel.c 31 KB

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