hci_nokia.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 <asm/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 hci_nokia_radio_hdr {
  99. u8 evt;
  100. u8 dlen;
  101. } __packed;
  102. struct nokia_bt_dev {
  103. struct hci_uart hu;
  104. struct serdev_device *serdev;
  105. struct gpio_desc *reset;
  106. struct gpio_desc *wakeup_host;
  107. struct gpio_desc *wakeup_bt;
  108. unsigned long sysclk_speed;
  109. int wake_irq;
  110. struct sk_buff *rx_skb;
  111. struct sk_buff_head txq;
  112. bdaddr_t bdaddr;
  113. int init_error;
  114. struct completion init_completion;
  115. u8 man_id;
  116. u8 ver_id;
  117. bool initialized;
  118. bool tx_enabled;
  119. bool rx_enabled;
  120. };
  121. static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb);
  122. static void nokia_flow_control(struct serdev_device *serdev, bool enable)
  123. {
  124. if (enable) {
  125. serdev_device_set_rts(serdev, true);
  126. serdev_device_set_flow_control(serdev, true);
  127. } else {
  128. serdev_device_set_flow_control(serdev, false);
  129. serdev_device_set_rts(serdev, false);
  130. }
  131. }
  132. static irqreturn_t wakeup_handler(int irq, void *data)
  133. {
  134. struct nokia_bt_dev *btdev = data;
  135. struct device *dev = &btdev->serdev->dev;
  136. int wake_state = gpiod_get_value(btdev->wakeup_host);
  137. if (btdev->rx_enabled == wake_state)
  138. return IRQ_HANDLED;
  139. if (wake_state)
  140. pm_runtime_get(dev);
  141. else
  142. pm_runtime_put(dev);
  143. btdev->rx_enabled = wake_state;
  144. return IRQ_HANDLED;
  145. }
  146. static int nokia_reset(struct hci_uart *hu)
  147. {
  148. struct nokia_bt_dev *btdev = hu->priv;
  149. struct device *dev = &btdev->serdev->dev;
  150. int err;
  151. /* reset routine */
  152. gpiod_set_value_cansleep(btdev->reset, 1);
  153. gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
  154. msleep(100);
  155. /* safety check */
  156. err = gpiod_get_value_cansleep(btdev->wakeup_host);
  157. if (err == 1) {
  158. dev_err(dev, "reset: host wakeup not low!");
  159. return -EPROTO;
  160. }
  161. /* flush queue */
  162. serdev_device_write_flush(btdev->serdev);
  163. /* init uart */
  164. nokia_flow_control(btdev->serdev, false);
  165. serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE);
  166. gpiod_set_value_cansleep(btdev->reset, 0);
  167. /* wait for cts */
  168. err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
  169. if (err < 0) {
  170. dev_err(dev, "CTS not received: %d", err);
  171. return err;
  172. }
  173. nokia_flow_control(btdev->serdev, true);
  174. return 0;
  175. }
  176. static int nokia_send_alive_packet(struct hci_uart *hu)
  177. {
  178. struct nokia_bt_dev *btdev = hu->priv;
  179. struct device *dev = &btdev->serdev->dev;
  180. struct hci_nokia_alive_hdr *hdr;
  181. struct hci_nokia_alive_pkt *pkt;
  182. struct sk_buff *skb;
  183. int len;
  184. init_completion(&btdev->init_completion);
  185. len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt);
  186. skb = bt_skb_alloc(len, GFP_KERNEL);
  187. if (!skb)
  188. return -ENOMEM;
  189. hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT;
  190. memset(skb->data, 0x00, len);
  191. hdr = skb_put(skb, sizeof(*hdr));
  192. hdr->dlen = sizeof(*pkt);
  193. pkt = skb_put(skb, sizeof(*pkt));
  194. pkt->mid = NOKIA_ALIVE_REQ;
  195. nokia_enqueue(hu, skb);
  196. hci_uart_tx_wakeup(hu);
  197. dev_dbg(dev, "Alive sent");
  198. if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
  199. msecs_to_jiffies(1000))) {
  200. return -ETIMEDOUT;
  201. }
  202. if (btdev->init_error < 0)
  203. return btdev->init_error;
  204. return 0;
  205. }
  206. static int nokia_send_negotiation(struct hci_uart *hu)
  207. {
  208. struct nokia_bt_dev *btdev = hu->priv;
  209. struct device *dev = &btdev->serdev->dev;
  210. struct hci_nokia_neg_cmd *neg_cmd;
  211. struct hci_nokia_neg_hdr *neg_hdr;
  212. struct sk_buff *skb;
  213. int len, err;
  214. u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE);
  215. int sysclk = btdev->sysclk_speed / 1000;
  216. len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd);
  217. skb = bt_skb_alloc(len, GFP_KERNEL);
  218. if (!skb)
  219. return -ENOMEM;
  220. hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT;
  221. neg_hdr = skb_put(skb, sizeof(*neg_hdr));
  222. neg_hdr->dlen = sizeof(*neg_cmd);
  223. neg_cmd = skb_put(skb, sizeof(*neg_cmd));
  224. neg_cmd->ack = NOKIA_NEG_REQ;
  225. neg_cmd->baud = cpu_to_le16(baud);
  226. neg_cmd->unused1 = 0x0000;
  227. neg_cmd->proto = NOKIA_PROTO_BYTE;
  228. neg_cmd->sys_clk = cpu_to_le16(sysclk);
  229. neg_cmd->unused2 = 0x0000;
  230. btdev->init_error = 0;
  231. init_completion(&btdev->init_completion);
  232. nokia_enqueue(hu, skb);
  233. hci_uart_tx_wakeup(hu);
  234. dev_dbg(dev, "Negotiation sent");
  235. if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
  236. msecs_to_jiffies(10000))) {
  237. return -ETIMEDOUT;
  238. }
  239. if (btdev->init_error < 0)
  240. return btdev->init_error;
  241. /* Change to previously negotiated speed. Flow Control
  242. * is disabled until bluetooth adapter is ready to avoid
  243. * broken bytes being received.
  244. */
  245. nokia_flow_control(btdev->serdev, false);
  246. serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE);
  247. err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
  248. if (err < 0) {
  249. dev_err(dev, "CTS not received: %d", err);
  250. return err;
  251. }
  252. nokia_flow_control(btdev->serdev, true);
  253. dev_dbg(dev, "Negotiation successful");
  254. return 0;
  255. }
  256. static int nokia_setup_fw(struct hci_uart *hu)
  257. {
  258. struct nokia_bt_dev *btdev = hu->priv;
  259. struct device *dev = &btdev->serdev->dev;
  260. const char *fwname;
  261. const struct firmware *fw;
  262. const u8 *fw_ptr;
  263. size_t fw_size;
  264. int err;
  265. dev_dbg(dev, "setup firmware");
  266. if (btdev->man_id == NOKIA_ID_BCM2048) {
  267. fwname = FIRMWARE_BCM2048;
  268. } else if (btdev->man_id == NOKIA_ID_TI1271) {
  269. fwname = FIRMWARE_TI1271;
  270. } else {
  271. dev_err(dev, "Unsupported bluetooth device!");
  272. return -ENODEV;
  273. }
  274. err = request_firmware(&fw, fwname, dev);
  275. if (err < 0) {
  276. dev_err(dev, "%s: Failed to load Nokia firmware file (%d)",
  277. hu->hdev->name, err);
  278. return err;
  279. }
  280. fw_ptr = fw->data;
  281. fw_size = fw->size;
  282. while (fw_size >= 4) {
  283. u16 pkt_size = get_unaligned_le16(fw_ptr);
  284. u8 pkt_type = fw_ptr[2];
  285. const struct hci_command_hdr *cmd;
  286. u16 opcode;
  287. struct sk_buff *skb;
  288. switch (pkt_type) {
  289. case HCI_COMMAND_PKT:
  290. cmd = (struct hci_command_hdr *)(fw_ptr + 3);
  291. opcode = le16_to_cpu(cmd->opcode);
  292. skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen,
  293. fw_ptr + 3 + HCI_COMMAND_HDR_SIZE,
  294. HCI_INIT_TIMEOUT);
  295. if (IS_ERR(skb)) {
  296. err = PTR_ERR(skb);
  297. dev_err(dev, "%s: FW command %04x failed (%d)",
  298. hu->hdev->name, opcode, err);
  299. goto done;
  300. }
  301. kfree_skb(skb);
  302. break;
  303. case HCI_NOKIA_RADIO_PKT:
  304. case HCI_NOKIA_NEG_PKT:
  305. case HCI_NOKIA_ALIVE_PKT:
  306. break;
  307. }
  308. fw_ptr += pkt_size + 2;
  309. fw_size -= pkt_size + 2;
  310. }
  311. done:
  312. release_firmware(fw);
  313. return err;
  314. }
  315. static int nokia_setup(struct hci_uart *hu)
  316. {
  317. struct nokia_bt_dev *btdev = hu->priv;
  318. struct device *dev = &btdev->serdev->dev;
  319. int err;
  320. btdev->initialized = false;
  321. nokia_flow_control(btdev->serdev, false);
  322. pm_runtime_get_sync(dev);
  323. if (btdev->tx_enabled) {
  324. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  325. pm_runtime_put(&btdev->serdev->dev);
  326. btdev->tx_enabled = false;
  327. }
  328. dev_dbg(dev, "protocol setup");
  329. /* 0. reset connection */
  330. err = nokia_reset(hu);
  331. if (err < 0) {
  332. dev_err(dev, "Reset failed: %d", err);
  333. goto out;
  334. }
  335. /* 1. negotiate speed etc */
  336. err = nokia_send_negotiation(hu);
  337. if (err < 0) {
  338. dev_err(dev, "Negotiation failed: %d", err);
  339. goto out;
  340. }
  341. /* 2. verify correct setup using alive packet */
  342. err = nokia_send_alive_packet(hu);
  343. if (err < 0) {
  344. dev_err(dev, "Alive check failed: %d", err);
  345. goto out;
  346. }
  347. /* 3. send firmware */
  348. err = nokia_setup_fw(hu);
  349. if (err < 0) {
  350. dev_err(dev, "Could not setup FW: %d", err);
  351. goto out;
  352. }
  353. nokia_flow_control(btdev->serdev, false);
  354. serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE);
  355. nokia_flow_control(btdev->serdev, true);
  356. if (btdev->man_id == NOKIA_ID_BCM2048) {
  357. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  358. set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
  359. dev_dbg(dev, "bcm2048 has invalid bluetooth address!");
  360. }
  361. dev_dbg(dev, "protocol setup done!");
  362. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  363. pm_runtime_put(dev);
  364. btdev->tx_enabled = false;
  365. btdev->initialized = true;
  366. return 0;
  367. out:
  368. pm_runtime_put(dev);
  369. return err;
  370. }
  371. static int nokia_open(struct hci_uart *hu)
  372. {
  373. struct device *dev = &hu->serdev->dev;
  374. dev_dbg(dev, "protocol open");
  375. pm_runtime_enable(dev);
  376. return 0;
  377. }
  378. static int nokia_flush(struct hci_uart *hu)
  379. {
  380. struct nokia_bt_dev *btdev = hu->priv;
  381. dev_dbg(&btdev->serdev->dev, "flush device");
  382. skb_queue_purge(&btdev->txq);
  383. return 0;
  384. }
  385. static int nokia_close(struct hci_uart *hu)
  386. {
  387. struct nokia_bt_dev *btdev = hu->priv;
  388. struct device *dev = &btdev->serdev->dev;
  389. dev_dbg(dev, "close device");
  390. btdev->initialized = false;
  391. skb_queue_purge(&btdev->txq);
  392. kfree_skb(btdev->rx_skb);
  393. /* disable module */
  394. gpiod_set_value(btdev->reset, 1);
  395. gpiod_set_value(btdev->wakeup_bt, 0);
  396. pm_runtime_disable(&btdev->serdev->dev);
  397. return 0;
  398. }
  399. /* Enqueue frame for transmittion (padding, crc, etc) */
  400. static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  401. {
  402. struct nokia_bt_dev *btdev = hu->priv;
  403. int err;
  404. /* Prepend skb with frame type */
  405. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  406. /* Packets must be word aligned */
  407. if (skb->len % 2) {
  408. err = skb_pad(skb, 1);
  409. if (err)
  410. return err;
  411. skb_put(skb, 1);
  412. }
  413. skb_queue_tail(&btdev->txq, skb);
  414. return 0;
  415. }
  416. static int nokia_recv_negotiation_packet(struct hci_dev *hdev,
  417. struct sk_buff *skb)
  418. {
  419. struct hci_uart *hu = hci_get_drvdata(hdev);
  420. struct nokia_bt_dev *btdev = hu->priv;
  421. struct device *dev = &btdev->serdev->dev;
  422. struct hci_nokia_neg_hdr *hdr;
  423. struct hci_nokia_neg_evt *evt;
  424. int ret = 0;
  425. hdr = (struct hci_nokia_neg_hdr *)skb->data;
  426. if (hdr->dlen != sizeof(*evt)) {
  427. btdev->init_error = -EIO;
  428. ret = -EIO;
  429. goto finish_neg;
  430. }
  431. evt = skb_pull(skb, sizeof(*hdr));
  432. if (evt->ack != NOKIA_NEG_ACK) {
  433. dev_err(dev, "Negotiation received: wrong reply");
  434. btdev->init_error = -EINVAL;
  435. ret = -EINVAL;
  436. goto finish_neg;
  437. }
  438. btdev->man_id = evt->man_id;
  439. btdev->ver_id = evt->ver_id;
  440. dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u",
  441. evt->baud, evt->sys_clk, evt->man_id, evt->ver_id);
  442. finish_neg:
  443. complete(&btdev->init_completion);
  444. kfree_skb(skb);
  445. return ret;
  446. }
  447. static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb)
  448. {
  449. struct hci_uart *hu = hci_get_drvdata(hdev);
  450. struct nokia_bt_dev *btdev = hu->priv;
  451. struct device *dev = &btdev->serdev->dev;
  452. struct hci_nokia_alive_hdr *hdr;
  453. struct hci_nokia_alive_pkt *pkt;
  454. int ret = 0;
  455. hdr = (struct hci_nokia_alive_hdr *)skb->data;
  456. if (hdr->dlen != sizeof(*pkt)) {
  457. dev_err(dev, "Corrupted alive message");
  458. btdev->init_error = -EIO;
  459. ret = -EIO;
  460. goto finish_alive;
  461. }
  462. pkt = skb_pull(skb, sizeof(*hdr));
  463. if (pkt->mid != NOKIA_ALIVE_RESP) {
  464. dev_err(dev, "Alive received: invalid response: 0x%02x!",
  465. pkt->mid);
  466. btdev->init_error = -EINVAL;
  467. ret = -EINVAL;
  468. goto finish_alive;
  469. }
  470. dev_dbg(dev, "Alive received");
  471. finish_alive:
  472. complete(&btdev->init_completion);
  473. kfree_skb(skb);
  474. return ret;
  475. }
  476. static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb)
  477. {
  478. /* Packets received on the dedicated radio channel are
  479. * HCI events and so feed them back into the core.
  480. */
  481. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  482. return hci_recv_frame(hdev, skb);
  483. }
  484. /* Recv data */
  485. static const struct h4_recv_pkt nokia_recv_pkts[] = {
  486. { H4_RECV_ACL, .recv = hci_recv_frame },
  487. { H4_RECV_SCO, .recv = hci_recv_frame },
  488. { H4_RECV_EVENT, .recv = hci_recv_frame },
  489. { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet },
  490. { NOKIA_RECV_NEG, .recv = nokia_recv_negotiation_packet },
  491. { NOKIA_RECV_RADIO, .recv = nokia_recv_radio },
  492. };
  493. static int nokia_recv(struct hci_uart *hu, const void *data, int count)
  494. {
  495. struct nokia_bt_dev *btdev = hu->priv;
  496. struct device *dev = &btdev->serdev->dev;
  497. int err;
  498. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  499. return -EUNATCH;
  500. btdev->rx_skb = h4_recv_buf(hu->hdev, btdev->rx_skb, data, count,
  501. nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts));
  502. if (IS_ERR(btdev->rx_skb)) {
  503. err = PTR_ERR(btdev->rx_skb);
  504. dev_err(dev, "Frame reassembly failed (%d)", err);
  505. btdev->rx_skb = NULL;
  506. return err;
  507. }
  508. return count;
  509. }
  510. static struct sk_buff *nokia_dequeue(struct hci_uart *hu)
  511. {
  512. struct nokia_bt_dev *btdev = hu->priv;
  513. struct device *dev = &btdev->serdev->dev;
  514. struct sk_buff *result = skb_dequeue(&btdev->txq);
  515. if (!btdev->initialized)
  516. return result;
  517. if (btdev->tx_enabled == !!result)
  518. return result;
  519. if (result) {
  520. pm_runtime_get_sync(dev);
  521. gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
  522. } else {
  523. serdev_device_wait_until_sent(btdev->serdev, 0);
  524. gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
  525. pm_runtime_put(dev);
  526. }
  527. btdev->tx_enabled = !!result;
  528. return result;
  529. }
  530. static const struct hci_uart_proto nokia_proto = {
  531. .id = HCI_UART_NOKIA,
  532. .name = "Nokia",
  533. .open = nokia_open,
  534. .close = nokia_close,
  535. .recv = nokia_recv,
  536. .enqueue = nokia_enqueue,
  537. .dequeue = nokia_dequeue,
  538. .flush = nokia_flush,
  539. .setup = nokia_setup,
  540. .manufacturer = 1,
  541. };
  542. static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev)
  543. {
  544. struct device *dev = &serdev->dev;
  545. struct nokia_bt_dev *btdev;
  546. struct clk *sysclk;
  547. int err = 0;
  548. btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL);
  549. if (!btdev)
  550. return -ENOMEM;
  551. btdev->hu.serdev = btdev->serdev = serdev;
  552. serdev_device_set_drvdata(serdev, btdev);
  553. btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  554. if (IS_ERR(btdev->reset)) {
  555. err = PTR_ERR(btdev->reset);
  556. dev_err(dev, "could not get reset gpio: %d", err);
  557. return err;
  558. }
  559. btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN);
  560. if (IS_ERR(btdev->wakeup_host)) {
  561. err = PTR_ERR(btdev->wakeup_host);
  562. dev_err(dev, "could not get host wakeup gpio: %d", err);
  563. return err;
  564. }
  565. btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host);
  566. err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL,
  567. wakeup_handler,
  568. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  569. "wakeup", btdev);
  570. if (err) {
  571. dev_err(dev, "could request wakeup irq: %d", err);
  572. return err;
  573. }
  574. btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup",
  575. GPIOD_OUT_LOW);
  576. if (IS_ERR(btdev->wakeup_bt)) {
  577. err = PTR_ERR(btdev->wakeup_bt);
  578. dev_err(dev, "could not get BT wakeup gpio: %d", err);
  579. return err;
  580. }
  581. sysclk = devm_clk_get(dev, "sysclk");
  582. if (IS_ERR(sysclk)) {
  583. err = PTR_ERR(sysclk);
  584. dev_err(dev, "could not get sysclk: %d", err);
  585. return err;
  586. }
  587. clk_prepare_enable(sysclk);
  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");