blob: aa2543b3c286968fece4acee97953eb2c1fe0ba8 [file] [log] [blame]
Rob Herring82f51692017-03-28 17:59:35 +02001/*
2 * Bluetooth HCI serdev driver lib
3 *
4 * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org>
5 *
6 * Based on hci_ldisc.c:
7 *
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>
11 *
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.
16 *
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.
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/types.h>
26#include <linux/serdev.h>
27#include <linux/skbuff.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
32#include "hci_uart.h"
33
Colin Ian King640e32c2017-06-23 09:37:38 +010034static struct serdev_device_ops hci_serdev_client_ops;
Rob Herring82f51692017-03-28 17:59:35 +020035
36static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
37{
38 struct hci_dev *hdev = hu->hdev;
39
40 /* Update HCI stat counters */
41 switch (pkt_type) {
42 case HCI_COMMAND_PKT:
43 hdev->stat.cmd_tx++;
44 break;
45
46 case HCI_ACLDATA_PKT:
47 hdev->stat.acl_tx++;
48 break;
49
50 case HCI_SCODATA_PKT:
51 hdev->stat.sco_tx++;
52 break;
53 }
54}
55
56static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
57{
58 struct sk_buff *skb = hu->tx_skb;
59
60 if (!skb)
61 skb = hu->proto->dequeue(hu);
62 else
63 hu->tx_skb = NULL;
64
65 return skb;
66}
67
68static void hci_uart_write_work(struct work_struct *work)
69{
70 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
71 struct serdev_device *serdev = hu->serdev;
72 struct hci_dev *hdev = hu->hdev;
73 struct sk_buff *skb;
74
75 /* REVISIT:
76 * should we cope with bad skbs or ->write() returning an error value?
77 */
78 do {
79 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
80
81 while ((skb = hci_uart_dequeue(hu))) {
82 int len;
83
84 len = serdev_device_write_buf(serdev,
85 skb->data, skb->len);
86 hdev->stat.byte_tx += len;
87
88 skb_pull(skb, len);
89 if (skb->len) {
90 hu->tx_skb = skb;
91 break;
92 }
93
94 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
95 kfree_skb(skb);
96 }
97 } while(test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
98
99 clear_bit(HCI_UART_SENDING, &hu->tx_state);
100}
101
102/* ------- Interface to HCI layer ------ */
103
Rob Herring82f51692017-03-28 17:59:35 +0200104/* Reset device */
105static int hci_uart_flush(struct hci_dev *hdev)
106{
107 struct hci_uart *hu = hci_get_drvdata(hdev);
108
109 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
110
111 if (hu->tx_skb) {
112 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
113 }
114
115 /* Flush any pending characters in the driver and discipline. */
116 serdev_device_write_flush(hu->serdev);
117
118 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
119 hu->proto->flush(hu);
120
121 return 0;
122}
123
Hans de Goede412fe292018-05-27 21:04:51 +0200124/* Initialize device */
125static int hci_uart_open(struct hci_dev *hdev)
126{
127 BT_DBG("%s %p", hdev->name, hdev);
128
129 /* Undo clearing this from hci_uart_close() */
130 hdev->flush = hci_uart_flush;
131
132 return 0;
133}
134
Rob Herring82f51692017-03-28 17:59:35 +0200135/* Close device */
136static int hci_uart_close(struct hci_dev *hdev)
137{
Rob Herring82f51692017-03-28 17:59:35 +0200138 BT_DBG("hdev %p", hdev);
139
140 hci_uart_flush(hdev);
141 hdev->flush = NULL;
142
Rob Herring82f51692017-03-28 17:59:35 +0200143 return 0;
144}
145
146/* Send frames from HCI layer */
147static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
148{
149 struct hci_uart *hu = hci_get_drvdata(hdev);
150
151 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
152 skb->len);
153
154 hu->proto->enqueue(hu, skb);
155
156 hci_uart_tx_wakeup(hu);
157
158 return 0;
159}
160
161static int hci_uart_setup(struct hci_dev *hdev)
162{
163 struct hci_uart *hu = hci_get_drvdata(hdev);
164 struct hci_rp_read_local_version *ver;
165 struct sk_buff *skb;
166 unsigned int speed;
167 int err;
168
169 /* Init speed if any */
170 if (hu->init_speed)
171 speed = hu->init_speed;
172 else if (hu->proto->init_speed)
173 speed = hu->proto->init_speed;
174 else
175 speed = 0;
176
177 if (speed)
178 serdev_device_set_baudrate(hu->serdev, speed);
179
180 /* Operational speed if any */
181 if (hu->oper_speed)
182 speed = hu->oper_speed;
183 else if (hu->proto->oper_speed)
184 speed = hu->proto->oper_speed;
185 else
186 speed = 0;
187
188 if (hu->proto->set_baudrate && speed) {
189 err = hu->proto->set_baudrate(hu, speed);
190 if (err)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100191 bt_dev_err(hdev, "Failed to set baudrate");
Rob Herring82f51692017-03-28 17:59:35 +0200192 else
193 serdev_device_set_baudrate(hu->serdev, speed);
194 }
195
196 if (hu->proto->setup)
197 return hu->proto->setup(hu);
198
199 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
200 return 0;
201
202 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
203 HCI_INIT_TIMEOUT);
204 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100205 bt_dev_err(hdev, "Reading local version info failed (%ld)",
206 PTR_ERR(skb));
Rob Herring82f51692017-03-28 17:59:35 +0200207 return 0;
208 }
209
Vaibhav Murkute0c0c09f2018-05-14 14:38:47 -0700210 if (skb->len != sizeof(*ver))
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100211 bt_dev_err(hdev, "Event length mismatch for version info");
Rob Herring82f51692017-03-28 17:59:35 +0200212
213 kfree_skb(skb);
214 return 0;
215}
216
217/** hci_uart_write_wakeup - transmit buffer wakeup
218 * @serdev: serial device
219 *
220 * This function is called by the serdev framework when it accepts
221 * more data being sent.
222 */
223static void hci_uart_write_wakeup(struct serdev_device *serdev)
224{
225 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
226
227 BT_DBG("");
228
229 if (!hu || serdev != hu->serdev) {
230 WARN_ON(1);
231 return;
232 }
233
234 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
235 hci_uart_tx_wakeup(hu);
236}
237
238/** hci_uart_receive_buf - receive buffer wakeup
239 * @serdev: serial device
240 * @data: pointer to received data
241 * @count: count of received data in bytes
242 *
243 * This function is called by the serdev framework when it received data
244 * in the RX buffer.
245 *
246 * Return: number of processed bytes
247 */
248static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
249 size_t count)
250{
251 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
252
253 if (!hu || serdev != hu->serdev) {
254 WARN_ON(1);
255 return 0;
256 }
257
258 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
259 return 0;
260
261 /* It does not need a lock here as it is already protected by a mutex in
262 * tty caller
263 */
264 hu->proto->recv(hu, data, count);
265
266 if (hu->hdev)
267 hu->hdev->stat.byte_rx += count;
268
269 return count;
270}
271
Colin Ian King640e32c2017-06-23 09:37:38 +0100272static struct serdev_device_ops hci_serdev_client_ops = {
Rob Herring82f51692017-03-28 17:59:35 +0200273 .receive_buf = hci_uart_receive_buf,
274 .write_wakeup = hci_uart_write_wakeup,
275};
276
277int hci_uart_register_device(struct hci_uart *hu,
278 const struct hci_uart_proto *p)
279{
280 int err;
281 struct hci_dev *hdev;
282
283 BT_DBG("");
284
Sebastian Reichel52b318e2017-03-28 17:59:36 +0200285 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
286
Hans de Goedee9ca0802018-05-27 21:04:52 +0200287 err = serdev_device_open(hu->serdev);
Rob Herring82f51692017-03-28 17:59:35 +0200288 if (err)
289 return err;
290
Hans de Goedee9ca0802018-05-27 21:04:52 +0200291 err = p->open(hu);
292 if (err)
293 goto err_open;
294
Rob Herring82f51692017-03-28 17:59:35 +0200295 hu->proto = p;
296 set_bit(HCI_UART_PROTO_READY, &hu->flags);
297
298 /* Initialize and register HCI device */
299 hdev = hci_alloc_dev();
300 if (!hdev) {
301 BT_ERR("Can't allocate HCI device");
302 err = -ENOMEM;
303 goto err_alloc;
304 }
305
306 hu->hdev = hdev;
307
308 hdev->bus = HCI_UART;
309 hci_set_drvdata(hdev, hu);
310
Hans de Goedefdee6d82018-05-27 21:04:53 +0200311 INIT_WORK(&hu->init_ready, hci_uart_init_work);
Rob Herring82f51692017-03-28 17:59:35 +0200312 INIT_WORK(&hu->write_work, hci_uart_write_work);
Lukas Wunnerd73e17282017-11-17 00:54:53 +0100313 percpu_init_rwsem(&hu->proto_lock);
Rob Herring82f51692017-03-28 17:59:35 +0200314
315 /* Only when vendor specific setup callback is provided, consider
316 * the manufacturer information valid. This avoids filling in the
317 * value for Ericsson when nothing is specified.
318 */
319 if (hu->proto->setup)
320 hdev->manufacturer = hu->proto->manufacturer;
321
322 hdev->open = hci_uart_open;
323 hdev->close = hci_uart_close;
324 hdev->flush = hci_uart_flush;
325 hdev->send = hci_uart_send_frame;
326 hdev->setup = hci_uart_setup;
327 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
328
329 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
330 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
331
332 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
333 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
334
335 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
336 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
337
338 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
339 hdev->dev_type = HCI_AMP;
340 else
341 hdev->dev_type = HCI_PRIMARY;
342
343 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
344 return 0;
345
346 if (hci_register_dev(hdev) < 0) {
347 BT_ERR("Can't register HCI device");
348 err = -ENODEV;
349 goto err_register;
350 }
351
352 set_bit(HCI_UART_REGISTERED, &hu->flags);
353
354 return 0;
355
356err_register:
357 hci_free_dev(hdev);
358err_alloc:
359 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
360 p->close(hu);
Hans de Goedee9ca0802018-05-27 21:04:52 +0200361err_open:
362 serdev_device_close(hu->serdev);
Rob Herring82f51692017-03-28 17:59:35 +0200363 return err;
364}
Sebastian Reichel081f36a2017-03-28 17:59:37 +0200365EXPORT_SYMBOL_GPL(hci_uart_register_device);
Ian Moltonc34dc3b2017-07-08 17:37:41 +0100366
367void hci_uart_unregister_device(struct hci_uart *hu)
368{
369 struct hci_dev *hdev = hu->hdev;
370
371 hci_unregister_dev(hdev);
372 hci_free_dev(hdev);
373
374 cancel_work_sync(&hu->write_work);
375
376 hu->proto->close(hu);
Hans de Goedee9ca0802018-05-27 21:04:52 +0200377 serdev_device_close(hu->serdev);
Ian Moltonc34dc3b2017-07-08 17:37:41 +0100378}
379EXPORT_SYMBOL_GPL(hci_uart_unregister_device);