hci_nokia.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+
  4. *
  5. * Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org>
  6. * Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/errno.h>
  10. #include <linux/firmware.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/serdev.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/unaligned.h>
  23. #include <net/bluetooth/bluetooth.h>
  24. #include <net/bluetooth/hci_core.h>
  25. #include "hci_uart.h"
  26. #include "btbcm.h"
  27. #define VERSION "0.1"
  28. #define NOKIA_ID_BCM2048 0x04
  29. #define NOKIA_ID_TI1271 0x31
  30. #define FIRMWARE_BCM2048 "nokia/bcmfw.bin"
  31. #define FIRMWARE_TI1271 "nokia/ti1273.bin"
  32. #define HCI_NOKIA_NEG_PKT 0x06
  33. #define HCI_NOKIA_ALIVE_PKT 0x07
  34. #define HCI_NOKIA_RADIO_PKT 0x08
  35. #define HCI_NOKIA_NEG_HDR_SIZE 1
  36. #define HCI_NOKIA_MAX_NEG_SIZE 255
  37. #define HCI_NOKIA_ALIVE_HDR_SIZE 1
  38. #define HCI_NOKIA_MAX_ALIVE_SIZE 255
  39. #define HCI_NOKIA_RADIO_HDR_SIZE 2
  40. #define HCI_NOKIA_MAX_RADIO_SIZE 255
  41. #define NOKIA_PROTO_PKT 0x44
  42. #define NOKIA_PROTO_BYTE 0x4c
  43. #define NOKIA_NEG_REQ 0x00
  44. #define NOKIA_NEG_ACK 0x20
  45. #define NOKIA_NEG_NAK 0x40
  46. #define H4_TYPE_SIZE 1
  47. #define NOKIA_RECV_ALIVE \
  48. .type = HCI_NOKIA_ALIVE_PKT, \
  49. .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \
  50. .loff = 0, \
  51. .lsize = 1, \
  52. .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \
  53. #define NOKIA_RECV_NEG \
  54. .type = HCI_NOKIA_NEG_PKT, \
  55. .hlen = HCI_NOKIA_NEG_HDR_SIZE, \
  56. .loff = 0, \
  57. .lsize = 1, \
  58. .maxlen = HCI_NOKIA_MAX_NEG_SIZE \
  59. #define NOKIA_RECV_RADIO \
  60. .type = HCI_NOKIA_RADIO_PKT, \
  61. .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \
  62. .loff = 1, \
  63. .lsize = 1, \
  64. .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \
  65. struct hci_nokia_neg_hdr {
  66. u8 dlen;
  67. } __packed;
  68. struct hci_nokia_neg_cmd {
  69. u8 ack;
  70. u16 baud;
  71. u16 unused1;
  72. u8 proto;
  73. u16 sys_clk;
  74. u16 unused2;
  75. } __packed;
  76. #define NOKIA_ALIVE_REQ 0x55
  77. #define NOKIA_ALIVE_RESP 0xcc
  78. struct hci_nokia_alive_hdr {
  79. u8 dlen;
  80. } __packed;
  81. struct hci_nokia_alive_pkt {
  82. u8 mid;
  83. u8 unused;
  84. } __packed;
  85. struct hci_nokia_neg_evt {
  86. u8 ack;
  87. u16 baud;
  88. u16 unused1;
  89. u8 proto;
  90. u16 sys_clk;
  91. u16 unused2;
  92. u8 man_id;
  93. u8 ver_id;
  94. } __packed;
  95. #define MAX_BAUD_RATE 3692300
  96. #define SETUP_BAUD_RATE 921600
  97. #define INIT_BAUD_RATE 120000
  98. struct nokia_bt_dev {
  99. struct hci_uart hu;
  100. struct serdev_device *serdev;
  101. struct gpio_desc *reset;
  102. struct gpio_desc *wakeup_host;
  103. struct gpio_desc *wakeup_bt;
  104. unsigned long sysclk_speed;
  105. int wake_irq;
  106. struct sk_buff *rx_skb;
  107. struct sk_buff_head txq;
  108. bdaddr_t bdaddr;
  109. int init_error;
  110. struct completion init_completion;
  111. u8 man_id;
  112. u8 ver_id;
  113. bool initialized;
  114. bool tx_enabled;
  115. bool rx_enabled;
  116. };
  117. static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb);
  118. static void nokia_flow_control(struct serdev_device *serdev, bool enable)
  119. {
  120. if (enable) {
  121. serdev_device_set_rts(serdev, true);
  122. serdev_device_set_flow_control(serdev, true);
  123. } else {
  124. serdev_device_set_flow_control(serdev, false);
  125. serdev_device_set_rts(serdev, false);
  126. }
  127. }
  128. static irqreturn_t wakeup_handler(int irq, void *data)
  129. {
  130. struct nokia_bt_dev *btdev = data;
  131. struct device *dev = &btdev->serdev->dev;
  132. int wake_state = gpiod_get_value(btdev->wakeup_host);
  133. if (btdev->rx_enabled == wake_state)
  134. return IRQ_HANDLED;
  135. if (wake_state)
  136. pm_runtime_get(dev);
  137. else
  138. pm_runtime_put(dev);
  139. btdev->rx_enabled = wake_state;
  140. return IRQ_HANDLED;
  141. }
  142. static int nokia_reset(struct hci_uart *hu)
  143. {
  144. struct nokia_bt_dev *btdev = hu->priv;
  145. struct device *dev = &btdev->serdev->dev;
  146. int err;
  147. /* reset routine */
  148. gpiod_set_value_cansleep(btdev->reset, 1);
  149. gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
  150. msleep(100);
  151. /* safety check */
  152. err = gpiod_get_value_cansleep(btdev->wakeup_host);
  153. if (err == 1) {
  154. dev_err(dev, "reset: host wakeup not low!");
  155. return -EPROTO;
  156. }
  157. /* flush queue */
  158. serdev_device_write_flush(btdev->serdev);
  159. /* init uart */
  160. nokia_flow_control(btdev->serdev, false);
  161. serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE);
  162. gpiod_set_value_cansleep(btdev->reset, 0);
  163. /* wait for cts */
  164. err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
  165. if (err < 0) {
  166. dev_err(dev, "CTS not received: %d", err);
  167. return err;
  168. }
  169. nokia_flow_control(btdev->serdev, true);
  170. return 0;
  171. }
  172. static int nokia_send_alive_packet(struct hci_uart *hu)
  173. {
  174. struct nokia_bt_dev *btdev = hu->priv;
  175. struct device *dev = &btdev->serdev->dev;
  176. struct hci_nokia_alive_hdr *hdr;
  177. struct hci_nokia_alive_pkt *pkt;
  178. struct sk_buff *skb;
  179. int len;
  180. init_completion(&btdev->init_completion);
  181. len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt);
  182. skb = bt_skb_alloc(len, GFP_KERNEL);
  183. if (!skb)
  184. return -ENOMEM;
  185. hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT;
  186. memset(skb->data, 0x00, len);
  187. hdr = skb_put(skb, sizeof(*hdr));
  188. hdr->dlen = sizeof(*pkt);
  189. pkt = skb_put(skb, sizeof(*pkt));
  190. pkt->mid = NOKIA_ALIVE_REQ;
  191. nokia_enqueue(hu, skb);
  192. hci_uart_tx_wakeup(hu);
  193. dev_dbg(dev, "Alive sent");
  194. if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
  195. msecs_to_jiffies(1000))) {
  196. return -ETIMEDOUT;
  197. }
  198. if (btdev->init_error < 0)
  199. return btdev->init_error;
  200. return 0;
  201. }
  202. static int nokia_send_negotiation(struct hci_uart *hu)
  203. {
  204. struct nokia_bt_dev *btdev = hu->priv;
  205. struct device *dev = &btdev->serdev->dev;
  206. struct hci_nokia_neg_cmd *neg_cmd;
  207. struct hci_nokia_neg_hdr *neg_hdr;
  208. struct sk_buff *skb;
  209. int len, err;
  210. u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE);
  211. int sysclk = btdev->sysclk_speed / 1000;
  212. len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd);
  213. skb = bt_skb_alloc(len, GFP_KERNEL);
  214. if (!skb)
  215. return -ENOMEM;
  216. hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT;
  217. neg_hdr = skb_put(skb, sizeof(*neg_hdr));
  218. neg_hdr->dlen = sizeof(*neg_cmd);
  219. neg_cmd = skb_put(skb, sizeof(*neg_cmd));
  220. neg_cmd->ack = NOKIA_NEG_REQ;
  221. neg_cmd->baud = cpu_to_le16(baud);
  222. neg_cmd->unused1 = 0x0000;
  223. neg_cmd->proto = NOKIA_PROTO_BYTE;
  224. neg_cmd->sys_clk = cpu_to_le16(sysclk);
  225. neg_cmd->unused2 = 0x0000;
  226. btdev->init_error = 0;
  227. init_completion(&btdev->init_completion);
  228. nokia_enqueue(hu, skb);
  229. hci_uart_tx_wakeup(hu);
  230. dev_dbg(dev, "Negotiation sent");
  231. if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
  232. msecs_to_jiffies(10000))) {
  233. return -ETIMEDOUT;
  234. }
  235. if (btdev->init_error < 0)
  236. return btdev->init_error;
  237. /* Change to previously negotiated speed. Flow Control
  238. * is disabled until bluetooth adapter is ready to avoid
  239. * broken bytes being received.
  240. */
  241. nokia_flow_control(btdev->serdev, false);
  242. serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE);
  243. err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
  244. if (err < 0) {
  245. dev_err(dev, "CTS not received: %d", err);
  246. return err;
  247. }
  248. nokia_flow_control(btdev->serdev, true);
  249. dev_dbg(dev, "Negotiation successful");
  250. return 0;
  251. }
  252. static int nokia_setup_fw(struct hci_uart *hu)
  253. {
  254. struct nokia_bt_dev *btdev = hu->priv;
  255. struct device *dev = &btdev->serdev->dev;
  256. const char *fwname;
  257. const struct firmware *fw;
  258. const u8 *fw_ptr;
  259. size_t fw_size;
  260. int err;
  261. dev_dbg(dev, "setup firmware");
  262. if (btdev->man_id == NOKIA_ID_BCM2048) {
  263. fwname = FIRMWARE_BCM2048;
  264. } else if (btdev->man_id == NOKIA_ID_TI1271) {
  265. fwname = FIRMWARE_TI1271;
  266. } else {
  267. dev_err(dev, "Unsupported bluetooth device!");
  268. return -ENODEV;
  269. }
  270. err = request_firmware(&fw, fwname, dev);
  271. if (err < 0) {
  272. dev_err(dev, "%s: Failed to load Nokia firmware file (%d)",
  273. hu->hdev->name, err);
  274. return err;
  275. }
  276. fw_ptr = fw->data;
  277. fw_size = fw->size;
  278. while (fw_size >= 4) {
  279. u16 pkt_size = get_unaligned_le16(fw_ptr);
  280. u8 pkt_type = fw_ptr[2];
  281. const struct hci_command_hdr *cmd;
  282. u16 opcode;
  283. struct sk_buff *skb;
  284. switch (pkt_type) {
  285. case HCI_COMMAND_PKT:
  286. cmd = (struct hci_command_hdr *)(fw_ptr + 3);
  287. opcode = le16_to_cpu(cmd->opcode);
  288. skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen,
  289. fw_ptr + 3 + HCI_COMMAND_HDR_SIZE,
  290. HCI_INIT_TIMEOUT);
  291. if (IS_ERR(skb)) {
  292. err = PTR_ERR(skb);
  293. dev_err(dev, "%s: FW command %04x failed (%d)",
  294. hu->hdev->name, opcode, err);
  295. goto done;
  296. }
  297. kfree_skb(skb);
  298. break;
  299. case HCI_NOKIA_RADIO_PKT:
  300. case HCI_NOKIA_NEG_PKT:
  301. case HCI_NOKIA_ALIVE_PKT:
  302. break;
  303. }
  304. fw_ptr += pkt_size + 2;
  305. fw_size -= pkt_size + 2;
  306. }
  307. done:
  308. release_firmware(fw);
  309. return err;
  310. }
  311. static int nokia_setup(struct hci_uart *hu)
  312. {
  313. struct nokia_bt_dev *btdev = hu->priv;
  314. struct device *dev = &btdev->serdev->dev;
  315. int err;
  316. btdev->initialized = false;
  317. nokia_flow_control(btdev->serdev, false);
  318. pm_runtime_get_sync(dev);
  319. if (btdev->tx_enabled) {
  320. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  321. pm_runtime_put(&btdev->serdev->dev);
  322. btdev->tx_enabled = false;
  323. }
  324. dev_dbg(dev, "protocol setup");
  325. /* 0. reset connection */
  326. err = nokia_reset(hu);
  327. if (err < 0) {
  328. dev_err(dev, "Reset failed: %d", err);
  329. goto out;
  330. }
  331. /* 1. negotiate speed etc */
  332. err = nokia_send_negotiation(hu);
  333. if (err < 0) {
  334. dev_err(dev, "Negotiation failed: %d", err);
  335. goto out;
  336. }
  337. /* 2. verify correct setup using alive packet */
  338. err = nokia_send_alive_packet(hu);
  339. if (err < 0) {
  340. dev_err(dev, "Alive check failed: %d", err);
  341. goto out;
  342. }
  343. /* 3. send firmware */
  344. err = nokia_setup_fw(hu);
  345. if (err < 0) {
  346. dev_err(dev, "Could not setup FW: %d", err);
  347. goto out;
  348. }
  349. nokia_flow_control(btdev->serdev, false);
  350. serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE);
  351. nokia_flow_control(btdev->serdev, true);
  352. if (btdev->man_id == NOKIA_ID_BCM2048) {
  353. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  354. hci_set_quirk(hu->hdev, HCI_QUIRK_INVALID_BDADDR);
  355. dev_dbg(dev, "bcm2048 has invalid bluetooth address!");
  356. }
  357. dev_dbg(dev, "protocol setup done!");
  358. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  359. pm_runtime_put(dev);
  360. btdev->tx_enabled = false;
  361. btdev->initialized = true;
  362. return 0;
  363. out:
  364. pm_runtime_put(dev);
  365. return err;
  366. }
  367. static int nokia_open(struct hci_uart *hu)
  368. {
  369. struct device *dev = &hu->serdev->dev;
  370. dev_dbg(dev, "protocol open");
  371. pm_runtime_enable(dev);
  372. return 0;
  373. }
  374. static int nokia_flush(struct hci_uart *hu)
  375. {
  376. struct nokia_bt_dev *btdev = hu->priv;
  377. dev_dbg(&btdev->serdev->dev, "flush device");
  378. skb_queue_purge(&btdev->txq);
  379. return 0;
  380. }
  381. static int nokia_close(struct hci_uart *hu)
  382. {
  383. struct nokia_bt_dev *btdev = hu->priv;
  384. struct device *dev = &btdev->serdev->dev;
  385. dev_dbg(dev, "close device");
  386. btdev->initialized = false;
  387. skb_queue_purge(&btdev->txq);
  388. kfree_skb(btdev->rx_skb);
  389. /* disable module */
  390. gpiod_set_value(btdev->reset, 1);
  391. gpiod_set_value(btdev->wakeup_bt, 0);
  392. pm_runtime_disable(&btdev->serdev->dev);
  393. return 0;
  394. }
  395. /* Enqueue frame for transmission (padding, crc, etc) */
  396. static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  397. {
  398. struct nokia_bt_dev *btdev = hu->priv;
  399. int err;
  400. /* Prepend skb with frame type */
  401. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  402. /* Packets must be word aligned */
  403. if (skb->len % 2) {
  404. err = skb_pad(skb, 1);
  405. if (err)
  406. return err;
  407. skb_put(skb, 1);
  408. }
  409. skb_queue_tail(&btdev->txq, skb);
  410. return 0;
  411. }
  412. static int nokia_recv_negotiation_packet(struct hci_dev *hdev,
  413. struct sk_buff *skb)
  414. {
  415. struct hci_uart *hu = hci_get_drvdata(hdev);
  416. struct nokia_bt_dev *btdev = hu->priv;
  417. struct device *dev = &btdev->serdev->dev;
  418. struct hci_nokia_neg_hdr *hdr;
  419. struct hci_nokia_neg_evt *evt;
  420. int ret = 0;
  421. hdr = (struct hci_nokia_neg_hdr *)skb->data;
  422. if (hdr->dlen != sizeof(*evt)) {
  423. btdev->init_error = -EIO;
  424. ret = -EIO;
  425. goto finish_neg;
  426. }
  427. evt = skb_pull(skb, sizeof(*hdr));
  428. if (evt->ack != NOKIA_NEG_ACK) {
  429. dev_err(dev, "Negotiation received: wrong reply");
  430. btdev->init_error = -EINVAL;
  431. ret = -EINVAL;
  432. goto finish_neg;
  433. }
  434. btdev->man_id = evt->man_id;
  435. btdev->ver_id = evt->ver_id;
  436. dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u",
  437. evt->baud, evt->sys_clk, evt->man_id, evt->ver_id);
  438. finish_neg:
  439. complete(&btdev->init_completion);
  440. kfree_skb(skb);
  441. return ret;
  442. }
  443. static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb)
  444. {
  445. struct hci_uart *hu = hci_get_drvdata(hdev);
  446. struct nokia_bt_dev *btdev = hu->priv;
  447. struct device *dev = &btdev->serdev->dev;
  448. struct hci_nokia_alive_hdr *hdr;
  449. struct hci_nokia_alive_pkt *pkt;
  450. int ret = 0;
  451. hdr = (struct hci_nokia_alive_hdr *)skb->data;
  452. if (hdr->dlen != sizeof(*pkt)) {
  453. dev_err(dev, "Corrupted alive message");
  454. btdev->init_error = -EIO;
  455. ret = -EIO;
  456. goto finish_alive;
  457. }
  458. pkt = skb_pull(skb, sizeof(*hdr));
  459. if (pkt->mid != NOKIA_ALIVE_RESP) {
  460. dev_err(dev, "Alive received: invalid response: 0x%02x!",
  461. pkt->mid);
  462. btdev->init_error = -EINVAL;
  463. ret = -EINVAL;
  464. goto finish_alive;
  465. }
  466. dev_dbg(dev, "Alive received");
  467. finish_alive:
  468. complete(&btdev->init_completion);
  469. kfree_skb(skb);
  470. return ret;
  471. }
  472. static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb)
  473. {
  474. /* Packets received on the dedicated radio channel are
  475. * HCI events and so feed them back into the core.
  476. */
  477. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  478. return hci_recv_frame(hdev, skb);
  479. }
  480. /* Recv data */
  481. static const struct h4_recv_pkt nokia_recv_pkts[] = {
  482. { H4_RECV_ACL, .recv = hci_recv_frame },
  483. { H4_RECV_SCO, .recv = hci_recv_frame },
  484. { H4_RECV_EVENT, .recv = hci_recv_frame },
  485. { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet },
  486. { NOKIA_RECV_NEG, .recv = nokia_recv_negotiation_packet },
  487. { NOKIA_RECV_RADIO, .recv = nokia_recv_radio },
  488. };
  489. static int nokia_recv(struct hci_uart *hu, const void *data, int count)
  490. {
  491. struct nokia_bt_dev *btdev = hu->priv;
  492. struct device *dev = &btdev->serdev->dev;
  493. int err;
  494. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  495. return -EUNATCH;
  496. btdev->rx_skb = h4_recv_buf(hu, btdev->rx_skb, data, count,
  497. nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts));
  498. if (IS_ERR(btdev->rx_skb)) {
  499. err = PTR_ERR(btdev->rx_skb);
  500. dev_err(dev, "Frame reassembly failed (%d)", err);
  501. btdev->rx_skb = NULL;
  502. return err;
  503. }
  504. return count;
  505. }
  506. static struct sk_buff *nokia_dequeue(struct hci_uart *hu)
  507. {
  508. struct nokia_bt_dev *btdev = hu->priv;
  509. struct device *dev = &btdev->serdev->dev;
  510. struct sk_buff *result = skb_dequeue(&btdev->txq);
  511. if (!btdev->initialized)
  512. return result;
  513. if (btdev->tx_enabled == !!result)
  514. return result;
  515. if (result) {
  516. pm_runtime_get_sync(dev);
  517. gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
  518. } else {
  519. serdev_device_wait_until_sent(btdev->serdev, 0);
  520. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  521. pm_runtime_put(dev);
  522. }
  523. btdev->tx_enabled = !!result;
  524. return result;
  525. }
  526. static const struct hci_uart_proto nokia_proto = {
  527. .id = HCI_UART_NOKIA,
  528. .name = "Nokia",
  529. .open = nokia_open,
  530. .close = nokia_close,
  531. .recv = nokia_recv,
  532. .enqueue = nokia_enqueue,
  533. .dequeue = nokia_dequeue,
  534. .flush = nokia_flush,
  535. .setup = nokia_setup,
  536. .manufacturer = 1,
  537. };
  538. static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev)
  539. {
  540. struct device *dev = &serdev->dev;
  541. struct nokia_bt_dev *btdev;
  542. struct clk *sysclk;
  543. int err = 0;
  544. btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL);
  545. if (!btdev)
  546. return -ENOMEM;
  547. btdev->hu.serdev = btdev->serdev = serdev;
  548. serdev_device_set_drvdata(serdev, btdev);
  549. btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  550. if (IS_ERR(btdev->reset)) {
  551. err = PTR_ERR(btdev->reset);
  552. dev_err(dev, "could not get reset gpio: %d", err);
  553. return err;
  554. }
  555. btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN);
  556. if (IS_ERR(btdev->wakeup_host)) {
  557. err = PTR_ERR(btdev->wakeup_host);
  558. dev_err(dev, "could not get host wakeup gpio: %d", err);
  559. return err;
  560. }
  561. btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host);
  562. err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL,
  563. wakeup_handler,
  564. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  565. "wakeup", btdev);
  566. if (err) {
  567. dev_err(dev, "could request wakeup irq: %d", err);
  568. return err;
  569. }
  570. btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup",
  571. GPIOD_OUT_LOW);
  572. if (IS_ERR(btdev->wakeup_bt)) {
  573. err = PTR_ERR(btdev->wakeup_bt);
  574. dev_err(dev, "could not get BT wakeup gpio: %d", err);
  575. return err;
  576. }
  577. sysclk = devm_clk_get(dev, "sysclk");
  578. if (IS_ERR(sysclk)) {
  579. err = PTR_ERR(sysclk);
  580. dev_err(dev, "could not get sysclk: %d", err);
  581. return err;
  582. }
  583. err = clk_prepare_enable(sysclk);
  584. if (err) {
  585. dev_err(dev, "could not enable sysclk: %d", err);
  586. return err;
  587. }
  588. btdev->sysclk_speed = clk_get_rate(sysclk);
  589. clk_disable_unprepare(sysclk);
  590. skb_queue_head_init(&btdev->txq);
  591. btdev->hu.priv = btdev;
  592. btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */
  593. err = hci_uart_register_device(&btdev->hu, &nokia_proto);
  594. if (err) {
  595. dev_err(dev, "could not register bluetooth uart: %d", err);
  596. return err;
  597. }
  598. return 0;
  599. }
  600. static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev)
  601. {
  602. struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev);
  603. hci_uart_unregister_device(&btdev->hu);
  604. }
  605. static int nokia_bluetooth_runtime_suspend(struct device *dev)
  606. {
  607. struct serdev_device *serdev = to_serdev_device(dev);
  608. nokia_flow_control(serdev, false);
  609. return 0;
  610. }
  611. static int nokia_bluetooth_runtime_resume(struct device *dev)
  612. {
  613. struct serdev_device *serdev = to_serdev_device(dev);
  614. nokia_flow_control(serdev, true);
  615. return 0;
  616. }
  617. static const struct dev_pm_ops nokia_bluetooth_pm_ops = {
  618. SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend,
  619. nokia_bluetooth_runtime_resume,
  620. NULL)
  621. };
  622. #ifdef CONFIG_OF
  623. static const struct of_device_id nokia_bluetooth_of_match[] = {
  624. { .compatible = "nokia,h4p-bluetooth", },
  625. {},
  626. };
  627. MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match);
  628. #endif
  629. static struct serdev_device_driver nokia_bluetooth_serdev_driver = {
  630. .probe = nokia_bluetooth_serdev_probe,
  631. .remove = nokia_bluetooth_serdev_remove,
  632. .driver = {
  633. .name = "nokia-bluetooth",
  634. .pm = &nokia_bluetooth_pm_ops,
  635. .of_match_table = of_match_ptr(nokia_bluetooth_of_match),
  636. },
  637. };
  638. module_serdev_device_driver(nokia_bluetooth_serdev_driver);
  639. MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
  640. MODULE_DESCRIPTION("Bluetooth HCI UART Nokia H4+ driver ver " VERSION);
  641. MODULE_VERSION(VERSION);
  642. MODULE_LICENSE("GPL");