Linux 5.1.15
[linux/fpc-iii.git] / drivers / bluetooth / hci_serdev.c
blob490abba94363a674e90dbf81bcb426ea901108f5
1 /*
2 * Bluetooth HCI serdev driver lib
4 * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org>
6 * Based on hci_ldisc.c:
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>
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.
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.
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/serdev.h>
27 #include <linux/skbuff.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "hci_uart.h"
34 static struct serdev_device_ops hci_serdev_client_ops;
36 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
38 struct hci_dev *hdev = hu->hdev;
40 /* Update HCI stat counters */
41 switch (pkt_type) {
42 case HCI_COMMAND_PKT:
43 hdev->stat.cmd_tx++;
44 break;
46 case HCI_ACLDATA_PKT:
47 hdev->stat.acl_tx++;
48 break;
50 case HCI_SCODATA_PKT:
51 hdev->stat.sco_tx++;
52 break;
56 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
58 struct sk_buff *skb = hu->tx_skb;
60 if (!skb) {
61 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
62 skb = hu->proto->dequeue(hu);
63 } else
64 hu->tx_skb = NULL;
66 return skb;
69 static void hci_uart_write_work(struct work_struct *work)
71 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
72 struct serdev_device *serdev = hu->serdev;
73 struct hci_dev *hdev = hu->hdev;
74 struct sk_buff *skb;
76 /* REVISIT:
77 * should we cope with bad skbs or ->write() returning an error value?
79 do {
80 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
82 while ((skb = hci_uart_dequeue(hu))) {
83 int len;
85 len = serdev_device_write_buf(serdev,
86 skb->data, skb->len);
87 hdev->stat.byte_tx += len;
89 skb_pull(skb, len);
90 if (skb->len) {
91 hu->tx_skb = skb;
92 break;
95 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
96 kfree_skb(skb);
98 } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
100 clear_bit(HCI_UART_SENDING, &hu->tx_state);
103 /* ------- Interface to HCI layer ------ */
105 /* Reset device */
106 static int hci_uart_flush(struct hci_dev *hdev)
108 struct hci_uart *hu = hci_get_drvdata(hdev);
110 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
112 if (hu->tx_skb) {
113 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
116 /* Flush any pending characters in the driver and discipline. */
117 serdev_device_write_flush(hu->serdev);
119 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
120 hu->proto->flush(hu);
122 return 0;
125 /* Initialize device */
126 static int hci_uart_open(struct hci_dev *hdev)
128 BT_DBG("%s %p", hdev->name, hdev);
130 /* Undo clearing this from hci_uart_close() */
131 hdev->flush = hci_uart_flush;
133 return 0;
136 /* Close device */
137 static int hci_uart_close(struct hci_dev *hdev)
139 BT_DBG("hdev %p", hdev);
141 hci_uart_flush(hdev);
142 hdev->flush = NULL;
144 return 0;
147 /* Send frames from HCI layer */
148 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
150 struct hci_uart *hu = hci_get_drvdata(hdev);
152 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
153 skb->len);
155 hu->proto->enqueue(hu, skb);
157 hci_uart_tx_wakeup(hu);
159 return 0;
162 static int hci_uart_setup(struct hci_dev *hdev)
164 struct hci_uart *hu = hci_get_drvdata(hdev);
165 struct hci_rp_read_local_version *ver;
166 struct sk_buff *skb;
167 unsigned int speed;
168 int err;
170 /* Init speed if any */
171 if (hu->init_speed)
172 speed = hu->init_speed;
173 else if (hu->proto->init_speed)
174 speed = hu->proto->init_speed;
175 else
176 speed = 0;
178 if (speed)
179 serdev_device_set_baudrate(hu->serdev, speed);
181 /* Operational speed if any */
182 if (hu->oper_speed)
183 speed = hu->oper_speed;
184 else if (hu->proto->oper_speed)
185 speed = hu->proto->oper_speed;
186 else
187 speed = 0;
189 if (hu->proto->set_baudrate && speed) {
190 err = hu->proto->set_baudrate(hu, speed);
191 if (err)
192 bt_dev_err(hdev, "Failed to set baudrate");
193 else
194 serdev_device_set_baudrate(hu->serdev, speed);
197 if (hu->proto->setup)
198 return hu->proto->setup(hu);
200 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
201 return 0;
203 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
204 HCI_INIT_TIMEOUT);
205 if (IS_ERR(skb)) {
206 bt_dev_err(hdev, "Reading local version info failed (%ld)",
207 PTR_ERR(skb));
208 return 0;
211 if (skb->len != sizeof(*ver))
212 bt_dev_err(hdev, "Event length mismatch for version info");
214 kfree_skb(skb);
215 return 0;
218 /** hci_uart_write_wakeup - transmit buffer wakeup
219 * @serdev: serial device
221 * This function is called by the serdev framework when it accepts
222 * more data being sent.
224 static void hci_uart_write_wakeup(struct serdev_device *serdev)
226 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
228 BT_DBG("");
230 if (!hu || serdev != hu->serdev) {
231 WARN_ON(1);
232 return;
235 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
236 hci_uart_tx_wakeup(hu);
239 /** hci_uart_receive_buf - receive buffer wakeup
240 * @serdev: serial device
241 * @data: pointer to received data
242 * @count: count of received data in bytes
244 * This function is called by the serdev framework when it received data
245 * in the RX buffer.
247 * Return: number of processed bytes
249 static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
250 size_t count)
252 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
254 if (!hu || serdev != hu->serdev) {
255 WARN_ON(1);
256 return 0;
259 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
260 return 0;
262 /* It does not need a lock here as it is already protected by a mutex in
263 * tty caller
265 hu->proto->recv(hu, data, count);
267 if (hu->hdev)
268 hu->hdev->stat.byte_rx += count;
270 return count;
273 static struct serdev_device_ops hci_serdev_client_ops = {
274 .receive_buf = hci_uart_receive_buf,
275 .write_wakeup = hci_uart_write_wakeup,
278 int hci_uart_register_device(struct hci_uart *hu,
279 const struct hci_uart_proto *p)
281 int err;
282 struct hci_dev *hdev;
284 BT_DBG("");
286 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
288 err = serdev_device_open(hu->serdev);
289 if (err)
290 return err;
292 err = p->open(hu);
293 if (err)
294 goto err_open;
296 hu->proto = p;
297 set_bit(HCI_UART_PROTO_READY, &hu->flags);
299 /* Initialize and register HCI device */
300 hdev = hci_alloc_dev();
301 if (!hdev) {
302 BT_ERR("Can't allocate HCI device");
303 err = -ENOMEM;
304 goto err_alloc;
307 hu->hdev = hdev;
309 hdev->bus = HCI_UART;
310 hci_set_drvdata(hdev, hu);
312 INIT_WORK(&hu->init_ready, hci_uart_init_work);
313 INIT_WORK(&hu->write_work, hci_uart_write_work);
314 percpu_init_rwsem(&hu->proto_lock);
316 /* Only when vendor specific setup callback is provided, consider
317 * the manufacturer information valid. This avoids filling in the
318 * value for Ericsson when nothing is specified.
320 if (hu->proto->setup)
321 hdev->manufacturer = hu->proto->manufacturer;
323 hdev->open = hci_uart_open;
324 hdev->close = hci_uart_close;
325 hdev->flush = hci_uart_flush;
326 hdev->send = hci_uart_send_frame;
327 hdev->setup = hci_uart_setup;
328 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
330 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
331 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
333 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
334 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
336 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
337 hdev->dev_type = HCI_AMP;
338 else
339 hdev->dev_type = HCI_PRIMARY;
341 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
342 return 0;
344 if (hci_register_dev(hdev) < 0) {
345 BT_ERR("Can't register HCI device");
346 err = -ENODEV;
347 goto err_register;
350 set_bit(HCI_UART_REGISTERED, &hu->flags);
352 return 0;
354 err_register:
355 hci_free_dev(hdev);
356 err_alloc:
357 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
358 p->close(hu);
359 err_open:
360 serdev_device_close(hu->serdev);
361 return err;
363 EXPORT_SYMBOL_GPL(hci_uart_register_device);
365 void hci_uart_unregister_device(struct hci_uart *hu)
367 struct hci_dev *hdev = hu->hdev;
369 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
370 hci_unregister_dev(hdev);
371 hci_free_dev(hdev);
373 cancel_work_sync(&hu->write_work);
375 hu->proto->close(hu);
376 serdev_device_close(hu->serdev);
378 EXPORT_SYMBOL_GPL(hci_uart_unregister_device);