PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/bluetooth/hci_serdev.c

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