1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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 */
44 static inline struct sk_buff
*hci_uart_dequeue(struct hci_uart
*hu
)
46 struct sk_buff
*skb
= hu
->tx_skb
;
49 if (test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
50 skb
= hu
->proto
->dequeue(hu
);
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
;
65 * should we cope with bad skbs or ->write() returning an error value?
68 clear_bit(HCI_UART_TX_WAKEUP
, &hu
->tx_state
);
70 while ((skb
= hci_uart_dequeue(hu
))) {
73 len
= serdev_device_write_buf(serdev
,
75 hdev
->stat
.byte_tx
+= len
;
83 hci_uart_tx_complete(hu
, hci_skb_pkt_type(skb
));
86 } while (test_bit(HCI_UART_TX_WAKEUP
, &hu
->tx_state
));
88 clear_bit(HCI_UART_SENDING
, &hu
->tx_state
);
91 /* ------- Interface to HCI layer ------ */
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
);
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
);
113 /* Initialize device */
114 static int hci_uart_open(struct hci_dev
*hdev
)
116 BT_DBG("%s %p", hdev
->name
, hdev
);
118 /* Undo clearing this from hci_uart_close() */
119 hdev
->flush
= hci_uart_flush
;
125 static int hci_uart_close(struct hci_dev
*hdev
)
127 BT_DBG("hdev %p", hdev
);
129 hci_uart_flush(hdev
);
135 /* Send frames from HCI layer */
136 static int hci_uart_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
138 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
140 BT_DBG("%s: type %d len %d", hdev
->name
, hci_skb_pkt_type(skb
),
143 hu
->proto
->enqueue(hu
, skb
);
145 hci_uart_tx_wakeup(hu
);
150 static int hci_uart_setup(struct hci_dev
*hdev
)
152 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
153 struct hci_rp_read_local_version
*ver
;
158 /* Init speed if any */
160 speed
= hu
->init_speed
;
161 else if (hu
->proto
->init_speed
)
162 speed
= hu
->proto
->init_speed
;
167 serdev_device_set_baudrate(hu
->serdev
, speed
);
169 /* Operational speed if any */
171 speed
= hu
->oper_speed
;
172 else if (hu
->proto
->oper_speed
)
173 speed
= hu
->proto
->oper_speed
;
177 if (hu
->proto
->set_baudrate
&& speed
) {
178 err
= hu
->proto
->set_baudrate(hu
, speed
);
180 bt_dev_err(hdev
, "Failed to set baudrate");
182 serdev_device_set_baudrate(hu
->serdev
, speed
);
185 if (hu
->proto
->setup
)
186 return hu
->proto
->setup(hu
);
188 if (!test_bit(HCI_UART_VND_DETECT
, &hu
->hdev_flags
))
191 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
194 bt_dev_err(hdev
, "Reading local version info failed (%ld)",
199 if (skb
->len
!= sizeof(*ver
))
200 bt_dev_err(hdev
, "Event length mismatch for version info");
206 /** hci_uart_write_wakeup - transmit buffer wakeup
207 * @serdev: serial device
209 * This function is called by the serdev framework when it accepts
210 * more data being sent.
212 static void hci_uart_write_wakeup(struct serdev_device
*serdev
)
214 struct hci_uart
*hu
= serdev_device_get_drvdata(serdev
);
218 if (!hu
|| serdev
!= hu
->serdev
) {
223 if (test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
224 hci_uart_tx_wakeup(hu
);
227 /** hci_uart_receive_buf - receive buffer wakeup
228 * @serdev: serial device
229 * @data: pointer to received data
230 * @count: count of received data in bytes
232 * This function is called by the serdev framework when it received data
235 * Return: number of processed bytes
237 static int hci_uart_receive_buf(struct serdev_device
*serdev
, const u8
*data
,
240 struct hci_uart
*hu
= serdev_device_get_drvdata(serdev
);
242 if (!hu
|| serdev
!= hu
->serdev
) {
247 if (!test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
250 /* It does not need a lock here as it is already protected by a mutex in
253 hu
->proto
->recv(hu
, data
, count
);
256 hu
->hdev
->stat
.byte_rx
+= count
;
261 static const struct serdev_device_ops hci_serdev_client_ops
= {
262 .receive_buf
= hci_uart_receive_buf
,
263 .write_wakeup
= hci_uart_write_wakeup
,
266 int hci_uart_register_device(struct hci_uart
*hu
,
267 const struct hci_uart_proto
*p
)
270 struct hci_dev
*hdev
;
274 serdev_device_set_client_ops(hu
->serdev
, &hci_serdev_client_ops
);
276 err
= serdev_device_open(hu
->serdev
);
285 set_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
287 /* Initialize and register HCI device */
288 hdev
= hci_alloc_dev();
290 BT_ERR("Can't allocate HCI device");
297 hdev
->bus
= HCI_UART
;
298 hci_set_drvdata(hdev
, hu
);
300 INIT_WORK(&hu
->init_ready
, hci_uart_init_work
);
301 INIT_WORK(&hu
->write_work
, hci_uart_write_work
);
302 percpu_init_rwsem(&hu
->proto_lock
);
304 /* Only when vendor specific setup callback is provided, consider
305 * the manufacturer information valid. This avoids filling in the
306 * value for Ericsson when nothing is specified.
308 if (hu
->proto
->setup
)
309 hdev
->manufacturer
= hu
->proto
->manufacturer
;
311 hdev
->open
= hci_uart_open
;
312 hdev
->close
= hci_uart_close
;
313 hdev
->flush
= hci_uart_flush
;
314 hdev
->send
= hci_uart_send_frame
;
315 hdev
->setup
= hci_uart_setup
;
316 SET_HCIDEV_DEV(hdev
, &hu
->serdev
->dev
);
318 if (test_bit(HCI_UART_RAW_DEVICE
, &hu
->hdev_flags
))
319 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
321 if (test_bit(HCI_UART_EXT_CONFIG
, &hu
->hdev_flags
))
322 set_bit(HCI_QUIRK_EXTERNAL_CONFIG
, &hdev
->quirks
);
324 if (test_bit(HCI_UART_CREATE_AMP
, &hu
->hdev_flags
))
325 hdev
->dev_type
= HCI_AMP
;
327 hdev
->dev_type
= HCI_PRIMARY
;
329 if (test_bit(HCI_UART_INIT_PENDING
, &hu
->hdev_flags
))
332 if (hci_register_dev(hdev
) < 0) {
333 BT_ERR("Can't register HCI device");
338 set_bit(HCI_UART_REGISTERED
, &hu
->flags
);
345 clear_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
348 serdev_device_close(hu
->serdev
);
351 EXPORT_SYMBOL_GPL(hci_uart_register_device
);
353 void hci_uart_unregister_device(struct hci_uart
*hu
)
355 struct hci_dev
*hdev
= hu
->hdev
;
357 clear_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
359 cancel_work_sync(&hu
->init_ready
);
360 if (test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
361 hci_unregister_dev(hdev
);
364 cancel_work_sync(&hu
->write_work
);
366 hu
->proto
->close(hu
);
367 serdev_device_close(hu
->serdev
);
369 EXPORT_SYMBOL_GPL(hci_uart_unregister_device
);