hci_serdev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Bluetooth HCI serdev driver lib
  4. *
  5. * Copyright (C) 2017 Linaro, Ltd., Rob Herring <[email protected]>
  6. *
  7. * Based on hci_ldisc.c:
  8. *
  9. * Copyright (C) 2000-2001 Qualcomm Incorporated
  10. * Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>
  11. * Copyright (C) 2004-2005 Marcel Holtmann <[email protected]>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/serdev.h>
  16. #include <linux/skbuff.h>
  17. #include <net/bluetooth/bluetooth.h>
  18. #include <net/bluetooth/hci_core.h>
  19. #include "hci_uart.h"
  20. static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  21. {
  22. struct hci_dev *hdev = hu->hdev;
  23. /* Update HCI stat counters */
  24. switch (pkt_type) {
  25. case HCI_COMMAND_PKT:
  26. hdev->stat.cmd_tx++;
  27. break;
  28. case HCI_ACLDATA_PKT:
  29. hdev->stat.acl_tx++;
  30. break;
  31. case HCI_SCODATA_PKT:
  32. hdev->stat.sco_tx++;
  33. break;
  34. }
  35. }
  36. static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  37. {
  38. struct sk_buff *skb = hu->tx_skb;
  39. if (!skb) {
  40. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  41. skb = hu->proto->dequeue(hu);
  42. } else
  43. hu->tx_skb = NULL;
  44. return skb;
  45. }
  46. static void hci_uart_write_work(struct work_struct *work)
  47. {
  48. struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
  49. struct serdev_device *serdev = hu->serdev;
  50. struct hci_dev *hdev = hu->hdev;
  51. struct sk_buff *skb;
  52. /* REVISIT:
  53. * should we cope with bad skbs or ->write() returning an error value?
  54. */
  55. do {
  56. clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  57. while ((skb = hci_uart_dequeue(hu))) {
  58. int len;
  59. len = serdev_device_write_buf(serdev,
  60. skb->data, skb->len);
  61. hdev->stat.byte_tx += len;
  62. skb_pull(skb, len);
  63. if (skb->len) {
  64. hu->tx_skb = skb;
  65. break;
  66. }
  67. hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
  68. kfree_skb(skb);
  69. }
  70. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  71. } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
  72. }
  73. /* ------- Interface to HCI layer ------ */
  74. /* Reset device */
  75. static int hci_uart_flush(struct hci_dev *hdev)
  76. {
  77. struct hci_uart *hu = hci_get_drvdata(hdev);
  78. BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
  79. if (hu->tx_skb) {
  80. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  81. }
  82. /* Flush any pending characters in the driver and discipline. */
  83. serdev_device_write_flush(hu->serdev);
  84. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  85. hu->proto->flush(hu);
  86. return 0;
  87. }
  88. /* Initialize device */
  89. static int hci_uart_open(struct hci_dev *hdev)
  90. {
  91. struct hci_uart *hu = hci_get_drvdata(hdev);
  92. int err;
  93. BT_DBG("%s %p", hdev->name, hdev);
  94. /* When Quirk HCI_QUIRK_NON_PERSISTENT_SETUP is set by
  95. * driver, BT SoC is completely turned OFF during
  96. * BT OFF. Upon next BT ON UART port should be opened.
  97. */
  98. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  99. err = serdev_device_open(hu->serdev);
  100. if (err)
  101. return err;
  102. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  103. }
  104. /* Undo clearing this from hci_uart_close() */
  105. hdev->flush = hci_uart_flush;
  106. return 0;
  107. }
  108. /* Close device */
  109. static int hci_uart_close(struct hci_dev *hdev)
  110. {
  111. struct hci_uart *hu = hci_get_drvdata(hdev);
  112. BT_DBG("hdev %p", hdev);
  113. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  114. return 0;
  115. hci_uart_flush(hdev);
  116. hdev->flush = NULL;
  117. /* When QUIRK HCI_QUIRK_NON_PERSISTENT_SETUP is set by driver,
  118. * BT SOC is completely powered OFF during BT OFF, holding port
  119. * open may drain the battery.
  120. */
  121. if (test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
  122. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  123. serdev_device_close(hu->serdev);
  124. }
  125. return 0;
  126. }
  127. /* Send frames from HCI layer */
  128. static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  129. {
  130. struct hci_uart *hu = hci_get_drvdata(hdev);
  131. BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
  132. skb->len);
  133. hu->proto->enqueue(hu, skb);
  134. hci_uart_tx_wakeup(hu);
  135. return 0;
  136. }
  137. static int hci_uart_setup(struct hci_dev *hdev)
  138. {
  139. struct hci_uart *hu = hci_get_drvdata(hdev);
  140. struct hci_rp_read_local_version *ver;
  141. struct sk_buff *skb;
  142. unsigned int speed;
  143. int err;
  144. /* Init speed if any */
  145. if (hu->init_speed)
  146. speed = hu->init_speed;
  147. else if (hu->proto->init_speed)
  148. speed = hu->proto->init_speed;
  149. else
  150. speed = 0;
  151. if (speed)
  152. serdev_device_set_baudrate(hu->serdev, speed);
  153. /* Operational speed if any */
  154. if (hu->oper_speed)
  155. speed = hu->oper_speed;
  156. else if (hu->proto->oper_speed)
  157. speed = hu->proto->oper_speed;
  158. else
  159. speed = 0;
  160. if (hu->proto->set_baudrate && speed) {
  161. err = hu->proto->set_baudrate(hu, speed);
  162. if (err)
  163. bt_dev_err(hdev, "Failed to set baudrate");
  164. else
  165. serdev_device_set_baudrate(hu->serdev, speed);
  166. }
  167. if (hu->proto->setup)
  168. return hu->proto->setup(hu);
  169. if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
  170. return 0;
  171. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  172. HCI_INIT_TIMEOUT);
  173. if (IS_ERR(skb)) {
  174. bt_dev_err(hdev, "Reading local version info failed (%ld)",
  175. PTR_ERR(skb));
  176. return 0;
  177. }
  178. if (skb->len != sizeof(*ver))
  179. bt_dev_err(hdev, "Event length mismatch for version info");
  180. kfree_skb(skb);
  181. return 0;
  182. }
  183. /* Check if the device is wakeable */
  184. static bool hci_uart_wakeup(struct hci_dev *hdev)
  185. {
  186. /* HCI UART devices are assumed to be wakeable by default.
  187. * Implement wakeup callback to override this behavior.
  188. */
  189. return true;
  190. }
  191. /** hci_uart_write_wakeup - transmit buffer wakeup
  192. * @serdev: serial device
  193. *
  194. * This function is called by the serdev framework when it accepts
  195. * more data being sent.
  196. */
  197. static void hci_uart_write_wakeup(struct serdev_device *serdev)
  198. {
  199. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  200. BT_DBG("");
  201. if (!hu || serdev != hu->serdev) {
  202. WARN_ON(1);
  203. return;
  204. }
  205. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  206. hci_uart_tx_wakeup(hu);
  207. }
  208. /** hci_uart_receive_buf - receive buffer wakeup
  209. * @serdev: serial device
  210. * @data: pointer to received data
  211. * @count: count of received data in bytes
  212. *
  213. * This function is called by the serdev framework when it received data
  214. * in the RX buffer.
  215. *
  216. * Return: number of processed bytes
  217. */
  218. static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
  219. size_t count)
  220. {
  221. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  222. if (!hu || serdev != hu->serdev) {
  223. WARN_ON(1);
  224. return 0;
  225. }
  226. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  227. return 0;
  228. /* It does not need a lock here as it is already protected by a mutex in
  229. * tty caller
  230. */
  231. hu->proto->recv(hu, data, count);
  232. if (hu->hdev)
  233. hu->hdev->stat.byte_rx += count;
  234. return count;
  235. }
  236. static const struct serdev_device_ops hci_serdev_client_ops = {
  237. .receive_buf = hci_uart_receive_buf,
  238. .write_wakeup = hci_uart_write_wakeup,
  239. };
  240. int hci_uart_register_device(struct hci_uart *hu,
  241. const struct hci_uart_proto *p)
  242. {
  243. int err;
  244. struct hci_dev *hdev;
  245. BT_DBG("");
  246. serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
  247. if (percpu_init_rwsem(&hu->proto_lock))
  248. return -ENOMEM;
  249. err = serdev_device_open(hu->serdev);
  250. if (err)
  251. goto err_rwsem;
  252. err = p->open(hu);
  253. if (err)
  254. goto err_open;
  255. hu->proto = p;
  256. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  257. /* Initialize and register HCI device */
  258. hdev = hci_alloc_dev();
  259. if (!hdev) {
  260. BT_ERR("Can't allocate HCI device");
  261. err = -ENOMEM;
  262. goto err_alloc;
  263. }
  264. hu->hdev = hdev;
  265. hdev->bus = HCI_UART;
  266. hci_set_drvdata(hdev, hu);
  267. INIT_WORK(&hu->init_ready, hci_uart_init_work);
  268. INIT_WORK(&hu->write_work, hci_uart_write_work);
  269. /* Only when vendor specific setup callback is provided, consider
  270. * the manufacturer information valid. This avoids filling in the
  271. * value for Ericsson when nothing is specified.
  272. */
  273. if (hu->proto->setup)
  274. hdev->manufacturer = hu->proto->manufacturer;
  275. hdev->open = hci_uart_open;
  276. hdev->close = hci_uart_close;
  277. hdev->flush = hci_uart_flush;
  278. hdev->send = hci_uart_send_frame;
  279. hdev->setup = hci_uart_setup;
  280. if (!hdev->wakeup)
  281. hdev->wakeup = hci_uart_wakeup;
  282. SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
  283. if (test_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &hu->flags))
  284. set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
  285. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  286. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  287. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  288. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  289. if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  290. hdev->dev_type = HCI_AMP;
  291. else
  292. hdev->dev_type = HCI_PRIMARY;
  293. if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  294. return 0;
  295. if (hci_register_dev(hdev) < 0) {
  296. BT_ERR("Can't register HCI device");
  297. err = -ENODEV;
  298. goto err_register;
  299. }
  300. set_bit(HCI_UART_REGISTERED, &hu->flags);
  301. return 0;
  302. err_register:
  303. hci_free_dev(hdev);
  304. err_alloc:
  305. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  306. p->close(hu);
  307. err_open:
  308. serdev_device_close(hu->serdev);
  309. err_rwsem:
  310. percpu_free_rwsem(&hu->proto_lock);
  311. return err;
  312. }
  313. EXPORT_SYMBOL_GPL(hci_uart_register_device);
  314. void hci_uart_unregister_device(struct hci_uart *hu)
  315. {
  316. struct hci_dev *hdev = hu->hdev;
  317. cancel_work_sync(&hu->init_ready);
  318. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  319. hci_unregister_dev(hdev);
  320. hci_free_dev(hdev);
  321. cancel_work_sync(&hu->write_work);
  322. hu->proto->close(hu);
  323. if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  324. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  325. serdev_device_close(hu->serdev);
  326. }
  327. percpu_free_rwsem(&hu->proto_lock);
  328. }
  329. EXPORT_SYMBOL_GPL(hci_uart_unregister_device);