hci_nokia.c 18 KB

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