Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / bluetooth / hci_serdev.c
blob89a22e9b3253a8605fe19e652a7a8f0498d4b5df
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Bluetooth HCI serdev driver lib
5 * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org>
7 * Based on hci_ldisc.c:
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>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/serdev.h>
17 #include <linux/skbuff.h>
19 #include <net/bluetooth/bluetooth.h>
20 #include <net/bluetooth/hci_core.h>
22 #include "hci_uart.h"
24 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
26 struct hci_dev *hdev = hu->hdev;
28 /* Update HCI stat counters */
29 switch (pkt_type) {
30 case HCI_COMMAND_PKT:
31 hdev->stat.cmd_tx++;
32 break;
34 case HCI_ACLDATA_PKT:
35 hdev->stat.acl_tx++;
36 break;
38 case HCI_SCODATA_PKT:
39 hdev->stat.sco_tx++;
40 break;
44 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
46 struct sk_buff *skb = hu->tx_skb;
48 if (!skb) {
49 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
50 skb = hu->proto->dequeue(hu);
51 } else
52 hu->tx_skb = NULL;
54 return skb;
57 static void hci_uart_write_work(struct work_struct *work)
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;
64 /* REVISIT:
65 * should we cope with bad skbs or ->write() returning an error value?
67 do {
68 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
70 while ((skb = hci_uart_dequeue(hu))) {
71 int len;
73 len = serdev_device_write_buf(serdev,
74 skb->data, skb->len);
75 hdev->stat.byte_tx += len;
77 skb_pull(skb, len);
78 if (skb->len) {
79 hu->tx_skb = skb;
80 break;
83 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
84 kfree_skb(skb);
87 clear_bit(HCI_UART_SENDING, &hu->tx_state);
88 } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
91 /* ------- Interface to HCI layer ------ */
93 /* Reset device */
94 static int hci_uart_flush(struct hci_dev *hdev)
96 struct hci_uart *hu = hci_get_drvdata(hdev);
98 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
100 if (hu->tx_skb) {
101 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
104 /* Flush any pending characters in the driver and discipline. */
105 serdev_device_write_flush(hu->serdev);
107 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
108 hu->proto->flush(hu);
110 return 0;
113 /* Initialize device */
114 static int hci_uart_open(struct hci_dev *hdev)
116 struct hci_uart *hu = hci_get_drvdata(hdev);
117 int err;
119 BT_DBG("%s %p", hdev->name, hdev);
121 /* 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.
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);
132 /* Undo clearing this from hci_uart_close() */
133 hdev->flush = hci_uart_flush;
135 return 0;
138 /* Close device */
139 static int hci_uart_close(struct hci_dev *hdev)
141 struct hci_uart *hu = hci_get_drvdata(hdev);
143 BT_DBG("hdev %p", hdev);
145 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
146 return 0;
148 hci_uart_flush(hdev);
149 hdev->flush = NULL;
151 /* 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.
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);
160 return 0;
163 /* Send frames from HCI layer */
164 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
166 struct hci_uart *hu = hci_get_drvdata(hdev);
168 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
169 skb->len);
171 hu->proto->enqueue(hu, skb);
173 hci_uart_tx_wakeup(hu);
175 return 0;
178 static int hci_uart_setup(struct hci_dev *hdev)
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;
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;
194 if (speed)
195 serdev_device_set_baudrate(hu->serdev, speed);
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;
205 if (hu->proto->set_baudrate && speed) {
206 err = hu->proto->set_baudrate(hu, speed);
207 if (err)
208 bt_dev_err(hdev, "Failed to set baudrate");
209 else
210 serdev_device_set_baudrate(hu->serdev, speed);
213 if (hu->proto->setup)
214 return hu->proto->setup(hu);
216 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
217 return 0;
219 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
220 HCI_INIT_TIMEOUT);
221 if (IS_ERR(skb)) {
222 bt_dev_err(hdev, "Reading local version info failed (%ld)",
223 PTR_ERR(skb));
224 return 0;
227 if (skb->len != sizeof(*ver))
228 bt_dev_err(hdev, "Event length mismatch for version info");
230 kfree_skb(skb);
231 return 0;
234 /* Check if the device is wakeable */
235 static bool hci_uart_wakeup(struct hci_dev *hdev)
237 /* HCI UART devices are assumed to be wakeable by default.
238 * Implement wakeup callback to override this behavior.
240 return true;
243 /** hci_uart_write_wakeup - transmit buffer wakeup
244 * @serdev: serial device
246 * This function is called by the serdev framework when it accepts
247 * more data being sent.
249 static void hci_uart_write_wakeup(struct serdev_device *serdev)
251 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
253 BT_DBG("");
255 if (!hu || serdev != hu->serdev) {
256 WARN_ON(1);
257 return;
260 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
261 hci_uart_tx_wakeup(hu);
264 /** hci_uart_receive_buf - receive buffer wakeup
265 * @serdev: serial device
266 * @data: pointer to received data
267 * @count: count of received data in bytes
269 * This function is called by the serdev framework when it received data
270 * in the RX buffer.
272 * Return: number of processed bytes
274 static size_t hci_uart_receive_buf(struct serdev_device *serdev,
275 const u8 *data, size_t count)
277 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
279 if (!hu || serdev != hu->serdev) {
280 WARN_ON(1);
281 return 0;
284 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
285 return 0;
287 /* It does not need a lock here as it is already protected by a mutex in
288 * tty caller
290 hu->proto->recv(hu, data, count);
292 if (hu->hdev)
293 hu->hdev->stat.byte_rx += count;
295 return count;
298 static const struct serdev_device_ops hci_serdev_client_ops = {
299 .receive_buf = hci_uart_receive_buf,
300 .write_wakeup = hci_uart_write_wakeup,
303 int hci_uart_register_device_priv(struct hci_uart *hu,
304 const struct hci_uart_proto *p,
305 int sizeof_priv)
307 int err;
308 struct hci_dev *hdev;
310 BT_DBG("");
312 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
314 if (percpu_init_rwsem(&hu->proto_lock))
315 return -ENOMEM;
317 err = serdev_device_open(hu->serdev);
318 if (err)
319 goto err_rwsem;
321 err = p->open(hu);
322 if (err)
323 goto err_open;
325 hu->proto = p;
326 set_bit(HCI_UART_PROTO_READY, &hu->flags);
328 /* Initialize and register HCI device */
329 hdev = hci_alloc_dev_priv(sizeof_priv);
330 if (!hdev) {
331 BT_ERR("Can't allocate HCI device");
332 err = -ENOMEM;
333 goto err_alloc;
336 hu->hdev = hdev;
338 hdev->bus = HCI_UART;
339 hci_set_drvdata(hdev, hu);
341 INIT_WORK(&hu->init_ready, hci_uart_init_work);
342 INIT_WORK(&hu->write_work, hci_uart_write_work);
344 /* Only when vendor specific setup callback is provided, consider
345 * the manufacturer information valid. This avoids filling in the
346 * value for Ericsson when nothing is specified.
348 if (hu->proto->setup)
349 hdev->manufacturer = hu->proto->manufacturer;
351 hdev->open = hci_uart_open;
352 hdev->close = hci_uart_close;
353 hdev->flush = hci_uart_flush;
354 hdev->send = hci_uart_send_frame;
355 hdev->setup = hci_uart_setup;
356 if (!hdev->wakeup)
357 hdev->wakeup = hci_uart_wakeup;
358 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
360 if (test_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &hu->flags))
361 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
363 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
364 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
366 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
367 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
369 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
370 return 0;
372 if (hci_register_dev(hdev) < 0) {
373 BT_ERR("Can't register HCI device");
374 err = -ENODEV;
375 goto err_register;
378 set_bit(HCI_UART_REGISTERED, &hu->flags);
380 return 0;
382 err_register:
383 hci_free_dev(hdev);
384 err_alloc:
385 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
386 p->close(hu);
387 err_open:
388 serdev_device_close(hu->serdev);
389 err_rwsem:
390 percpu_free_rwsem(&hu->proto_lock);
391 return err;
393 EXPORT_SYMBOL_GPL(hci_uart_register_device_priv);
395 void hci_uart_unregister_device(struct hci_uart *hu)
397 struct hci_dev *hdev = hu->hdev;
399 cancel_work_sync(&hu->init_ready);
400 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
401 hci_unregister_dev(hdev);
402 hci_free_dev(hdev);
404 cancel_work_sync(&hu->write_work);
406 hu->proto->close(hu);
408 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
409 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
410 serdev_device_close(hu->serdev);
412 percpu_free_rwsem(&hu->proto_lock);
414 EXPORT_SYMBOL_GPL(hci_uart_unregister_device);