hci_serdev.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <robh@kernel.org>
  6. *
  7. * Based on hci_ldisc.c:
  8. *
  9. * Copyright (C) 2000-2001 Qualcomm Incorporated
  10. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  11. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  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. /** hci_uart_write_wakeup - transmit buffer wakeup
  184. * @serdev: serial device
  185. *
  186. * This function is called by the serdev framework when it accepts
  187. * more data being sent.
  188. */
  189. static void hci_uart_write_wakeup(struct serdev_device *serdev)
  190. {
  191. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  192. BT_DBG("");
  193. if (!hu || serdev != hu->serdev) {
  194. WARN_ON(1);
  195. return;
  196. }
  197. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  198. hci_uart_tx_wakeup(hu);
  199. }
  200. /** hci_uart_receive_buf - receive buffer wakeup
  201. * @serdev: serial device
  202. * @data: pointer to received data
  203. * @count: count of received data in bytes
  204. *
  205. * This function is called by the serdev framework when it received data
  206. * in the RX buffer.
  207. *
  208. * Return: number of processed bytes
  209. */
  210. static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
  211. size_t count)
  212. {
  213. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  214. if (!hu || serdev != hu->serdev) {
  215. WARN_ON(1);
  216. return 0;
  217. }
  218. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  219. return 0;
  220. /* It does not need a lock here as it is already protected by a mutex in
  221. * tty caller
  222. */
  223. hu->proto->recv(hu, data, count);
  224. if (hu->hdev)
  225. hu->hdev->stat.byte_rx += count;
  226. return count;
  227. }
  228. static const struct serdev_device_ops hci_serdev_client_ops = {
  229. .receive_buf = hci_uart_receive_buf,
  230. .write_wakeup = hci_uart_write_wakeup,
  231. };
  232. int hci_uart_register_device(struct hci_uart *hu,
  233. const struct hci_uart_proto *p)
  234. {
  235. int err;
  236. struct hci_dev *hdev;
  237. BT_DBG("");
  238. serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
  239. err = serdev_device_open(hu->serdev);
  240. if (err)
  241. return err;
  242. percpu_init_rwsem(&hu->proto_lock);
  243. err = p->open(hu);
  244. if (err)
  245. goto err_open;
  246. hu->proto = p;
  247. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  248. /* Initialize and register HCI device */
  249. hdev = hci_alloc_dev();
  250. if (!hdev) {
  251. BT_ERR("Can't allocate HCI device");
  252. err = -ENOMEM;
  253. goto err_alloc;
  254. }
  255. hu->hdev = hdev;
  256. hdev->bus = HCI_UART;
  257. hci_set_drvdata(hdev, hu);
  258. INIT_WORK(&hu->init_ready, hci_uart_init_work);
  259. INIT_WORK(&hu->write_work, hci_uart_write_work);
  260. /* Only when vendor specific setup callback is provided, consider
  261. * the manufacturer information valid. This avoids filling in the
  262. * value for Ericsson when nothing is specified.
  263. */
  264. if (hu->proto->setup)
  265. hdev->manufacturer = hu->proto->manufacturer;
  266. hdev->open = hci_uart_open;
  267. hdev->close = hci_uart_close;
  268. hdev->flush = hci_uart_flush;
  269. hdev->send = hci_uart_send_frame;
  270. hdev->setup = hci_uart_setup;
  271. SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
  272. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  273. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  274. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  275. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  276. if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  277. hdev->dev_type = HCI_AMP;
  278. else
  279. hdev->dev_type = HCI_PRIMARY;
  280. if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  281. return 0;
  282. if (hci_register_dev(hdev) < 0) {
  283. BT_ERR("Can't register HCI device");
  284. err = -ENODEV;
  285. goto err_register;
  286. }
  287. set_bit(HCI_UART_REGISTERED, &hu->flags);
  288. return 0;
  289. err_register:
  290. hci_free_dev(hdev);
  291. err_alloc:
  292. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  293. p->close(hu);
  294. err_open:
  295. serdev_device_close(hu->serdev);
  296. return err;
  297. }
  298. EXPORT_SYMBOL_GPL(hci_uart_register_device);
  299. void hci_uart_unregister_device(struct hci_uart *hu)
  300. {
  301. struct hci_dev *hdev = hu->hdev;
  302. cancel_work_sync(&hu->init_ready);
  303. if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  304. hci_unregister_dev(hdev);
  305. hci_free_dev(hdev);
  306. cancel_work_sync(&hu->write_work);
  307. hu->proto->close(hu);
  308. if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  309. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  310. serdev_device_close(hu->serdev);
  311. }
  312. }
  313. EXPORT_SYMBOL_GPL(hci_uart_unregister_device);