hci_serdev.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. /* Reset device */
  85. static int hci_uart_flush(struct hci_dev *hdev)
  86. {
  87. struct hci_uart *hu = hci_get_drvdata(hdev);
  88. BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
  89. if (hu->tx_skb) {
  90. kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  91. }
  92. /* Flush any pending characters in the driver and discipline. */
  93. serdev_device_write_flush(hu->serdev);
  94. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  95. hu->proto->flush(hu);
  96. return 0;
  97. }
  98. /* Initialize device */
  99. static int hci_uart_open(struct hci_dev *hdev)
  100. {
  101. BT_DBG("%s %p", hdev->name, hdev);
  102. /* Undo clearing this from hci_uart_close() */
  103. hdev->flush = hci_uart_flush;
  104. return 0;
  105. }
  106. /* Close device */
  107. static int hci_uart_close(struct hci_dev *hdev)
  108. {
  109. BT_DBG("hdev %p", hdev);
  110. hci_uart_flush(hdev);
  111. hdev->flush = NULL;
  112. return 0;
  113. }
  114. /* Send frames from HCI layer */
  115. static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  116. {
  117. struct hci_uart *hu = hci_get_drvdata(hdev);
  118. BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
  119. skb->len);
  120. hu->proto->enqueue(hu, skb);
  121. hci_uart_tx_wakeup(hu);
  122. return 0;
  123. }
  124. static int hci_uart_setup(struct hci_dev *hdev)
  125. {
  126. struct hci_uart *hu = hci_get_drvdata(hdev);
  127. struct hci_rp_read_local_version *ver;
  128. struct sk_buff *skb;
  129. unsigned int speed;
  130. int err;
  131. /* Init speed if any */
  132. if (hu->init_speed)
  133. speed = hu->init_speed;
  134. else if (hu->proto->init_speed)
  135. speed = hu->proto->init_speed;
  136. else
  137. speed = 0;
  138. if (speed)
  139. serdev_device_set_baudrate(hu->serdev, speed);
  140. /* Operational speed if any */
  141. if (hu->oper_speed)
  142. speed = hu->oper_speed;
  143. else if (hu->proto->oper_speed)
  144. speed = hu->proto->oper_speed;
  145. else
  146. speed = 0;
  147. if (hu->proto->set_baudrate && speed) {
  148. err = hu->proto->set_baudrate(hu, speed);
  149. if (err)
  150. bt_dev_err(hdev, "Failed to set baudrate");
  151. else
  152. serdev_device_set_baudrate(hu->serdev, speed);
  153. }
  154. if (hu->proto->setup)
  155. return hu->proto->setup(hu);
  156. if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
  157. return 0;
  158. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  159. HCI_INIT_TIMEOUT);
  160. if (IS_ERR(skb)) {
  161. bt_dev_err(hdev, "Reading local version info failed (%ld)",
  162. PTR_ERR(skb));
  163. return 0;
  164. }
  165. if (skb->len != sizeof(*ver))
  166. bt_dev_err(hdev, "Event length mismatch for version info");
  167. kfree_skb(skb);
  168. return 0;
  169. }
  170. /** hci_uart_write_wakeup - transmit buffer wakeup
  171. * @serdev: serial device
  172. *
  173. * This function is called by the serdev framework when it accepts
  174. * more data being sent.
  175. */
  176. static void hci_uart_write_wakeup(struct serdev_device *serdev)
  177. {
  178. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  179. BT_DBG("");
  180. if (!hu || serdev != hu->serdev) {
  181. WARN_ON(1);
  182. return;
  183. }
  184. if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  185. hci_uart_tx_wakeup(hu);
  186. }
  187. /** hci_uart_receive_buf - receive buffer wakeup
  188. * @serdev: serial device
  189. * @data: pointer to received data
  190. * @count: count of received data in bytes
  191. *
  192. * This function is called by the serdev framework when it received data
  193. * in the RX buffer.
  194. *
  195. * Return: number of processed bytes
  196. */
  197. static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
  198. size_t count)
  199. {
  200. struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  201. if (!hu || serdev != hu->serdev) {
  202. WARN_ON(1);
  203. return 0;
  204. }
  205. if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  206. return 0;
  207. /* It does not need a lock here as it is already protected by a mutex in
  208. * tty caller
  209. */
  210. hu->proto->recv(hu, data, count);
  211. if (hu->hdev)
  212. hu->hdev->stat.byte_rx += count;
  213. return count;
  214. }
  215. static struct serdev_device_ops hci_serdev_client_ops = {
  216. .receive_buf = hci_uart_receive_buf,
  217. .write_wakeup = hci_uart_write_wakeup,
  218. };
  219. int hci_uart_register_device(struct hci_uart *hu,
  220. const struct hci_uart_proto *p)
  221. {
  222. int err;
  223. struct hci_dev *hdev;
  224. BT_DBG("");
  225. serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
  226. err = serdev_device_open(hu->serdev);
  227. if (err)
  228. return err;
  229. err = p->open(hu);
  230. if (err)
  231. goto err_open;
  232. hu->proto = p;
  233. set_bit(HCI_UART_PROTO_READY, &hu->flags);
  234. /* Initialize and register HCI device */
  235. hdev = hci_alloc_dev();
  236. if (!hdev) {
  237. BT_ERR("Can't allocate HCI device");
  238. err = -ENOMEM;
  239. goto err_alloc;
  240. }
  241. hu->hdev = hdev;
  242. hdev->bus = HCI_UART;
  243. hci_set_drvdata(hdev, hu);
  244. INIT_WORK(&hu->init_ready, hci_uart_init_work);
  245. INIT_WORK(&hu->write_work, hci_uart_write_work);
  246. percpu_init_rwsem(&hu->proto_lock);
  247. /* Only when vendor specific setup callback is provided, consider
  248. * the manufacturer information valid. This avoids filling in the
  249. * value for Ericsson when nothing is specified.
  250. */
  251. if (hu->proto->setup)
  252. hdev->manufacturer = hu->proto->manufacturer;
  253. hdev->open = hci_uart_open;
  254. hdev->close = hci_uart_close;
  255. hdev->flush = hci_uart_flush;
  256. hdev->send = hci_uart_send_frame;
  257. hdev->setup = hci_uart_setup;
  258. SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
  259. if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  260. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  261. if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  262. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  263. if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
  264. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  265. if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  266. hdev->dev_type = HCI_AMP;
  267. else
  268. hdev->dev_type = HCI_PRIMARY;
  269. if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  270. return 0;
  271. if (hci_register_dev(hdev) < 0) {
  272. BT_ERR("Can't register HCI device");
  273. err = -ENODEV;
  274. goto err_register;
  275. }
  276. set_bit(HCI_UART_REGISTERED, &hu->flags);
  277. return 0;
  278. err_register:
  279. hci_free_dev(hdev);
  280. err_alloc:
  281. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  282. p->close(hu);
  283. err_open:
  284. serdev_device_close(hu->serdev);
  285. return err;
  286. }
  287. EXPORT_SYMBOL_GPL(hci_uart_register_device);
  288. void hci_uart_unregister_device(struct hci_uart *hu)
  289. {
  290. struct hci_dev *hdev = hu->hdev;
  291. clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  292. hci_unregister_dev(hdev);
  293. hci_free_dev(hdev);
  294. cancel_work_sync(&hu->write_work);
  295. hu->proto->close(hu);
  296. serdev_device_close(hu->serdev);
  297. }
  298. EXPORT_SYMBOL_GPL(hci_uart_unregister_device);