hci_serdev.c 8.1 KB

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