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