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 struct serdev_device_ops hci_serdev_client_ops
;
26 static inline void hci_uart_tx_complete(struct hci_uart
*hu
, int pkt_type
)
28 struct hci_dev
*hdev
= hu
->hdev
;
30 /* Update HCI stat counters */
46 static inline struct sk_buff
*hci_uart_dequeue(struct hci_uart
*hu
)
48 struct sk_buff
*skb
= hu
->tx_skb
;
51 if (test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
52 skb
= hu
->proto
->dequeue(hu
);
59 static void hci_uart_write_work(struct work_struct
*work
)
61 struct hci_uart
*hu
= container_of(work
, struct hci_uart
, write_work
);
62 struct serdev_device
*serdev
= hu
->serdev
;
63 struct hci_dev
*hdev
= hu
->hdev
;
67 * should we cope with bad skbs or ->write() returning an error value?
70 clear_bit(HCI_UART_TX_WAKEUP
, &hu
->tx_state
);
72 while ((skb
= hci_uart_dequeue(hu
))) {
75 len
= serdev_device_write_buf(serdev
,
77 hdev
->stat
.byte_tx
+= len
;
85 hci_uart_tx_complete(hu
, hci_skb_pkt_type(skb
));
88 } while (test_bit(HCI_UART_TX_WAKEUP
, &hu
->tx_state
));
90 clear_bit(HCI_UART_SENDING
, &hu
->tx_state
);
93 /* ------- Interface to HCI layer ------ */
96 static int hci_uart_flush(struct hci_dev
*hdev
)
98 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
100 BT_DBG("hdev %p serdev %p", hdev
, hu
->serdev
);
103 kfree_skb(hu
->tx_skb
); hu
->tx_skb
= NULL
;
106 /* Flush any pending characters in the driver and discipline. */
107 serdev_device_write_flush(hu
->serdev
);
109 if (test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
110 hu
->proto
->flush(hu
);
115 /* Initialize device */
116 static int hci_uart_open(struct hci_dev
*hdev
)
118 BT_DBG("%s %p", hdev
->name
, hdev
);
120 /* Undo clearing this from hci_uart_close() */
121 hdev
->flush
= hci_uart_flush
;
127 static int hci_uart_close(struct hci_dev
*hdev
)
129 BT_DBG("hdev %p", hdev
);
131 hci_uart_flush(hdev
);
137 /* Send frames from HCI layer */
138 static int hci_uart_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
140 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
142 BT_DBG("%s: type %d len %d", hdev
->name
, hci_skb_pkt_type(skb
),
145 hu
->proto
->enqueue(hu
, skb
);
147 hci_uart_tx_wakeup(hu
);
152 static int hci_uart_setup(struct hci_dev
*hdev
)
154 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
155 struct hci_rp_read_local_version
*ver
;
160 /* Init speed if any */
162 speed
= hu
->init_speed
;
163 else if (hu
->proto
->init_speed
)
164 speed
= hu
->proto
->init_speed
;
169 serdev_device_set_baudrate(hu
->serdev
, speed
);
171 /* Operational speed if any */
173 speed
= hu
->oper_speed
;
174 else if (hu
->proto
->oper_speed
)
175 speed
= hu
->proto
->oper_speed
;
179 if (hu
->proto
->set_baudrate
&& speed
) {
180 err
= hu
->proto
->set_baudrate(hu
, speed
);
182 bt_dev_err(hdev
, "Failed to set baudrate");
184 serdev_device_set_baudrate(hu
->serdev
, speed
);
187 if (hu
->proto
->setup
)
188 return hu
->proto
->setup(hu
);
190 if (!test_bit(HCI_UART_VND_DETECT
, &hu
->hdev_flags
))
193 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
196 bt_dev_err(hdev
, "Reading local version info failed (%ld)",
201 if (skb
->len
!= sizeof(*ver
))
202 bt_dev_err(hdev
, "Event length mismatch for version info");
208 /** hci_uart_write_wakeup - transmit buffer wakeup
209 * @serdev: serial device
211 * This function is called by the serdev framework when it accepts
212 * more data being sent.
214 static void hci_uart_write_wakeup(struct serdev_device
*serdev
)
216 struct hci_uart
*hu
= serdev_device_get_drvdata(serdev
);
220 if (!hu
|| serdev
!= hu
->serdev
) {
225 if (test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
226 hci_uart_tx_wakeup(hu
);
229 /** hci_uart_receive_buf - receive buffer wakeup
230 * @serdev: serial device
231 * @data: pointer to received data
232 * @count: count of received data in bytes
234 * This function is called by the serdev framework when it received data
237 * Return: number of processed bytes
239 static int hci_uart_receive_buf(struct serdev_device
*serdev
, const u8
*data
,
242 struct hci_uart
*hu
= serdev_device_get_drvdata(serdev
);
244 if (!hu
|| serdev
!= hu
->serdev
) {
249 if (!test_bit(HCI_UART_PROTO_READY
, &hu
->flags
))
252 /* It does not need a lock here as it is already protected by a mutex in
255 hu
->proto
->recv(hu
, data
, count
);
258 hu
->hdev
->stat
.byte_rx
+= count
;
263 static struct serdev_device_ops hci_serdev_client_ops
= {
264 .receive_buf
= hci_uart_receive_buf
,
265 .write_wakeup
= hci_uart_write_wakeup
,
268 int hci_uart_register_device(struct hci_uart
*hu
,
269 const struct hci_uart_proto
*p
)
272 struct hci_dev
*hdev
;
276 serdev_device_set_client_ops(hu
->serdev
, &hci_serdev_client_ops
);
278 err
= serdev_device_open(hu
->serdev
);
287 set_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
289 /* Initialize and register HCI device */
290 hdev
= hci_alloc_dev();
292 BT_ERR("Can't allocate HCI device");
299 hdev
->bus
= HCI_UART
;
300 hci_set_drvdata(hdev
, hu
);
302 INIT_WORK(&hu
->init_ready
, hci_uart_init_work
);
303 INIT_WORK(&hu
->write_work
, hci_uart_write_work
);
304 percpu_init_rwsem(&hu
->proto_lock
);
306 /* Only when vendor specific setup callback is provided, consider
307 * the manufacturer information valid. This avoids filling in the
308 * value for Ericsson when nothing is specified.
310 if (hu
->proto
->setup
)
311 hdev
->manufacturer
= hu
->proto
->manufacturer
;
313 hdev
->open
= hci_uart_open
;
314 hdev
->close
= hci_uart_close
;
315 hdev
->flush
= hci_uart_flush
;
316 hdev
->send
= hci_uart_send_frame
;
317 hdev
->setup
= hci_uart_setup
;
318 SET_HCIDEV_DEV(hdev
, &hu
->serdev
->dev
);
320 if (test_bit(HCI_UART_RAW_DEVICE
, &hu
->hdev_flags
))
321 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
323 if (test_bit(HCI_UART_EXT_CONFIG
, &hu
->hdev_flags
))
324 set_bit(HCI_QUIRK_EXTERNAL_CONFIG
, &hdev
->quirks
);
326 if (test_bit(HCI_UART_CREATE_AMP
, &hu
->hdev_flags
))
327 hdev
->dev_type
= HCI_AMP
;
329 hdev
->dev_type
= HCI_PRIMARY
;
331 if (test_bit(HCI_UART_INIT_PENDING
, &hu
->hdev_flags
))
334 if (hci_register_dev(hdev
) < 0) {
335 BT_ERR("Can't register HCI device");
340 set_bit(HCI_UART_REGISTERED
, &hu
->flags
);
347 clear_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
350 serdev_device_close(hu
->serdev
);
353 EXPORT_SYMBOL_GPL(hci_uart_register_device
);
355 void hci_uart_unregister_device(struct hci_uart
*hu
)
357 struct hci_dev
*hdev
= hu
->hdev
;
359 clear_bit(HCI_UART_PROTO_READY
, &hu
->flags
);
360 hci_unregister_dev(hdev
);
363 cancel_work_sync(&hu
->write_work
);
365 hu
->proto
->close(hu
);
366 serdev_device_close(hu
->serdev
);
368 EXPORT_SYMBOL_GPL(hci_uart_unregister_device
);