blob: 3b00d82d36cf77da48ef055b01ae882a2f3d1666 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rob Herring82f51692017-03-28 17:59:35 +02002/*
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>
Rob Herring82f51692017-03-28 17:59:35 +020012 */
13
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/serdev.h>
17#include <linux/skbuff.h>
18
19#include <net/bluetooth/bluetooth.h>
20#include <net/bluetooth/hci_core.h>
21
22#include "hci_uart.h"
23
Rob Herring82f51692017-03-28 17:59:35 +020024static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
25{
26 struct hci_dev *hdev = hu->hdev;
27
28 /* Update HCI stat counters */
29 switch (pkt_type) {
30 case HCI_COMMAND_PKT:
31 hdev->stat.cmd_tx++;
32 break;
33
34 case HCI_ACLDATA_PKT:
35 hdev->stat.acl_tx++;
36 break;
37
38 case HCI_SCODATA_PKT:
39 hdev->stat.sco_tx++;
40 break;
41 }
42}
43
44static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
45{
46 struct sk_buff *skb = hu->tx_skb;
47
Balakrishna Godavarthi5a637752018-08-22 17:34:12 +053048 if (!skb) {
49 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
50 skb = hu->proto->dequeue(hu);
51 } else
Rob Herring82f51692017-03-28 17:59:35 +020052 hu->tx_skb = NULL;
53
54 return skb;
55}
56
57static 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
64 /* REVISIT:
65 * should we cope with bad skbs or ->write() returning an error value?
66 */
67 do {
68 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
69
70 while ((skb = hci_uart_dequeue(hu))) {
71 int len;
72
73 len = serdev_device_write_buf(serdev,
74 skb->data, skb->len);
75 hdev->stat.byte_tx += len;
76
77 skb_pull(skb, len);
78 if (skb->len) {
79 hu->tx_skb = skb;
80 break;
81 }
82
83 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
84 kfree_skb(skb);
85 }
Rob Herring82f51692017-03-28 17:59:35 +020086
Claire Changafe0b1c2020-12-14 15:29:21 +080087 clear_bit(HCI_UART_SENDING, &hu->tx_state);
88 } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
Rob Herring82f51692017-03-28 17:59:35 +020089}
90
91/* ------- Interface to HCI layer ------ */
92
Rob Herring82f51692017-03-28 17:59:35 +020093/* Reset device */
94static int hci_uart_flush(struct hci_dev *hdev)
95{
96 struct hci_uart *hu = hci_get_drvdata(hdev);
97
98 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
99
100 if (hu->tx_skb) {
101 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
102 }
103
104 /* Flush any pending characters in the driver and discipline. */
105 serdev_device_write_flush(hu->serdev);
106
107 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
108 hu->proto->flush(hu);
109
110 return 0;
111}
112
Hans de Goede412fe292018-05-27 21:04:51 +0200113/* Initialize device */
114static int hci_uart_open(struct hci_dev *hdev)
115{
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530116 struct hci_uart *hu = hci_get_drvdata(hdev);
117 int err;
118
Hans de Goede412fe292018-05-27 21:04:51 +0200119 BT_DBG("%s %p", hdev->name, hdev);
120
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530121 /* When Quirk HCI_QUIRK_NON_PERSISTENT_SETUP is set by
122 * driver, BT SoC is completely turned OFF during
123 * BT OFF. Upon next BT ON UART port should be opened.
124 */
125 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
126 err = serdev_device_open(hu->serdev);
127 if (err)
128 return err;
129 set_bit(HCI_UART_PROTO_READY, &hu->flags);
130 }
131
Hans de Goede412fe292018-05-27 21:04:51 +0200132 /* Undo clearing this from hci_uart_close() */
133 hdev->flush = hci_uart_flush;
134
135 return 0;
136}
137
Rob Herring82f51692017-03-28 17:59:35 +0200138/* Close device */
139static int hci_uart_close(struct hci_dev *hdev)
140{
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530141 struct hci_uart *hu = hci_get_drvdata(hdev);
142
Rob Herring82f51692017-03-28 17:59:35 +0200143 BT_DBG("hdev %p", hdev);
144
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530145 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
146 return 0;
147
Rob Herring82f51692017-03-28 17:59:35 +0200148 hci_uart_flush(hdev);
149 hdev->flush = NULL;
150
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530151 /* When QUIRK HCI_QUIRK_NON_PERSISTENT_SETUP is set by driver,
152 * BT SOC is completely powered OFF during BT OFF, holding port
153 * open may drain the battery.
154 */
155 if (test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
156 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
157 serdev_device_close(hu->serdev);
158 }
159
Rob Herring82f51692017-03-28 17:59:35 +0200160 return 0;
161}
162
163/* Send frames from HCI layer */
164static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
165{
166 struct hci_uart *hu = hci_get_drvdata(hdev);
167
168 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
169 skb->len);
170
171 hu->proto->enqueue(hu, skb);
172
173 hci_uart_tx_wakeup(hu);
174
175 return 0;
176}
177
178static int hci_uart_setup(struct hci_dev *hdev)
179{
180 struct hci_uart *hu = hci_get_drvdata(hdev);
181 struct hci_rp_read_local_version *ver;
182 struct sk_buff *skb;
183 unsigned int speed;
184 int err;
185
186 /* Init speed if any */
187 if (hu->init_speed)
188 speed = hu->init_speed;
189 else if (hu->proto->init_speed)
190 speed = hu->proto->init_speed;
191 else
192 speed = 0;
193
194 if (speed)
195 serdev_device_set_baudrate(hu->serdev, speed);
196
197 /* Operational speed if any */
198 if (hu->oper_speed)
199 speed = hu->oper_speed;
200 else if (hu->proto->oper_speed)
201 speed = hu->proto->oper_speed;
202 else
203 speed = 0;
204
205 if (hu->proto->set_baudrate && speed) {
206 err = hu->proto->set_baudrate(hu, speed);
207 if (err)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100208 bt_dev_err(hdev, "Failed to set baudrate");
Rob Herring82f51692017-03-28 17:59:35 +0200209 else
210 serdev_device_set_baudrate(hu->serdev, speed);
211 }
212
213 if (hu->proto->setup)
214 return hu->proto->setup(hu);
215
216 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
217 return 0;
218
219 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
220 HCI_INIT_TIMEOUT);
221 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100222 bt_dev_err(hdev, "Reading local version info failed (%ld)",
223 PTR_ERR(skb));
Rob Herring82f51692017-03-28 17:59:35 +0200224 return 0;
225 }
226
Vaibhav Murkute0c0c09f2018-05-14 14:38:47 -0700227 if (skb->len != sizeof(*ver))
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100228 bt_dev_err(hdev, "Event length mismatch for version info");
Rob Herring82f51692017-03-28 17:59:35 +0200229
230 kfree_skb(skb);
231 return 0;
232}
233
234/** hci_uart_write_wakeup - transmit buffer wakeup
235 * @serdev: serial device
236 *
237 * This function is called by the serdev framework when it accepts
238 * more data being sent.
239 */
240static void hci_uart_write_wakeup(struct serdev_device *serdev)
241{
242 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
243
244 BT_DBG("");
245
246 if (!hu || serdev != hu->serdev) {
247 WARN_ON(1);
248 return;
249 }
250
251 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
252 hci_uart_tx_wakeup(hu);
253}
254
255/** hci_uart_receive_buf - receive buffer wakeup
256 * @serdev: serial device
257 * @data: pointer to received data
258 * @count: count of received data in bytes
259 *
260 * This function is called by the serdev framework when it received data
261 * in the RX buffer.
262 *
263 * Return: number of processed bytes
264 */
265static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
266 size_t count)
267{
268 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
269
270 if (!hu || serdev != hu->serdev) {
271 WARN_ON(1);
272 return 0;
273 }
274
275 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
276 return 0;
277
278 /* It does not need a lock here as it is already protected by a mutex in
279 * tty caller
280 */
281 hu->proto->recv(hu, data, count);
282
283 if (hu->hdev)
284 hu->hdev->stat.byte_rx += count;
285
286 return count;
287}
288
Rikard Falkeborn608c39f2020-05-09 15:17:19 +0200289static const struct serdev_device_ops hci_serdev_client_ops = {
Rob Herring82f51692017-03-28 17:59:35 +0200290 .receive_buf = hci_uart_receive_buf,
291 .write_wakeup = hci_uart_write_wakeup,
292};
293
294int hci_uart_register_device(struct hci_uart *hu,
295 const struct hci_uart_proto *p)
296{
297 int err;
298 struct hci_dev *hdev;
299
300 BT_DBG("");
301
Sebastian Reichel52b318e2017-03-28 17:59:36 +0200302 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
303
Hans de Goedee9ca0802018-05-27 21:04:52 +0200304 err = serdev_device_open(hu->serdev);
Rob Herring82f51692017-03-28 17:59:35 +0200305 if (err)
306 return err;
307
Hans de Goedee9ca0802018-05-27 21:04:52 +0200308 err = p->open(hu);
309 if (err)
310 goto err_open;
311
Rob Herring82f51692017-03-28 17:59:35 +0200312 hu->proto = p;
313 set_bit(HCI_UART_PROTO_READY, &hu->flags);
314
315 /* Initialize and register HCI device */
316 hdev = hci_alloc_dev();
317 if (!hdev) {
318 BT_ERR("Can't allocate HCI device");
319 err = -ENOMEM;
320 goto err_alloc;
321 }
322
323 hu->hdev = hdev;
324
325 hdev->bus = HCI_UART;
326 hci_set_drvdata(hdev, hu);
327
Hans de Goedefdee6d82018-05-27 21:04:53 +0200328 INIT_WORK(&hu->init_ready, hci_uart_init_work);
Rob Herring82f51692017-03-28 17:59:35 +0200329 INIT_WORK(&hu->write_work, hci_uart_write_work);
Lukas Wunnerd73e17282017-11-17 00:54:53 +0100330 percpu_init_rwsem(&hu->proto_lock);
Rob Herring82f51692017-03-28 17:59:35 +0200331
332 /* Only when vendor specific setup callback is provided, consider
333 * the manufacturer information valid. This avoids filling in the
334 * value for Ericsson when nothing is specified.
335 */
336 if (hu->proto->setup)
337 hdev->manufacturer = hu->proto->manufacturer;
338
339 hdev->open = hci_uart_open;
340 hdev->close = hci_uart_close;
341 hdev->flush = hci_uart_flush;
342 hdev->send = hci_uart_send_frame;
343 hdev->setup = hci_uart_setup;
344 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
345
Hans de Goedeb4a46992021-06-29 21:59:07 +0200346 if (test_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &hu->flags))
347 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
348
Rob Herring82f51692017-03-28 17:59:35 +0200349 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
350 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
351
352 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
353 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
354
Rob Herring82f51692017-03-28 17:59:35 +0200355 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
356 hdev->dev_type = HCI_AMP;
357 else
358 hdev->dev_type = HCI_PRIMARY;
359
360 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
361 return 0;
362
363 if (hci_register_dev(hdev) < 0) {
364 BT_ERR("Can't register HCI device");
365 err = -ENODEV;
366 goto err_register;
367 }
368
369 set_bit(HCI_UART_REGISTERED, &hu->flags);
370
371 return 0;
372
373err_register:
374 hci_free_dev(hdev);
375err_alloc:
376 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
377 p->close(hu);
Hans de Goedee9ca0802018-05-27 21:04:52 +0200378err_open:
379 serdev_device_close(hu->serdev);
Rob Herring82f51692017-03-28 17:59:35 +0200380 return err;
381}
Sebastian Reichel081f36a2017-03-28 17:59:37 +0200382EXPORT_SYMBOL_GPL(hci_uart_register_device);
Ian Moltonc34dc3b2017-07-08 17:37:41 +0100383
384void hci_uart_unregister_device(struct hci_uart *hu)
385{
386 struct hci_dev *hdev = hu->hdev;
387
Samuel Holland3b799252020-08-01 11:29:56 -0500388 cancel_work_sync(&hu->init_ready);
Nicolas Boichat202798d2020-07-21 10:37:16 +0800389 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
390 hci_unregister_dev(hdev);
Ian Moltonc34dc3b2017-07-08 17:37:41 +0100391 hci_free_dev(hdev);
392
393 cancel_work_sync(&hu->write_work);
394
395 hu->proto->close(hu);
Venkata Lakshmi Narayana Gubbade8892d2020-09-10 16:00:42 +0530396
397 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
398 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
399 serdev_device_close(hu->serdev);
400 }
Ian Moltonc34dc3b2017-07-08 17:37:41 +0100401}
402EXPORT_SYMBOL_GPL(hci_uart_unregister_device);