Skip to content

Commit 82f5169

Browse files
robherringholtmann
authored andcommitted
Bluetooth: hci_uart: add serdev driver support library
This adds library functions for serdev based BT drivers. This is largely copied from hci_ldisc.c and modified to use serdev calls. There's a little bit of duplication, but I avoided intermixing this as the ldisc code should eventually go away. Signed-off-by: Rob Herring <[email protected]> Cc: Marcel Holtmann <[email protected]> Cc: Gustavo Padovan <[email protected]> Cc: Johan Hedberg <[email protected]> Cc: [email protected] Acked-by: Pavel Machek <[email protected]> [Fix style issues reported by Pavel] Signed-off-by: Sebastian Reichel <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent aeac301 commit 82f5169

File tree

3 files changed

+366
-0
lines changed

3 files changed

+366
-0
lines changed

drivers/bluetooth/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ btmrvl-y := btmrvl_main.o
2929
btmrvl-$(CONFIG_DEBUG_FS) += btmrvl_debugfs.o
3030

3131
hci_uart-y := hci_ldisc.o
32+
hci_uart-$(CONFIG_SERIAL_DEV_BUS) += hci_serdev.o
3233
hci_uart-$(CONFIG_BT_HCIUART_H4) += hci_h4.o
3334
hci_uart-$(CONFIG_BT_HCIUART_BCSP) += hci_bcsp.o
3435
hci_uart-$(CONFIG_BT_HCIUART_LL) += hci_ll.o

drivers/bluetooth/hci_serdev.c

Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
/*
2+
* Bluetooth HCI serdev driver lib
3+
*
4+
* Copyright (C) 2017 Linaro, Ltd., Rob Herring <[email protected]>
5+
*
6+
* Based on hci_ldisc.c:
7+
*
8+
* Copyright (C) 2000-2001 Qualcomm Incorporated
9+
* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>
10+
* Copyright (C) 2004-2005 Marcel Holtmann <[email protected]>
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+
34+
struct serdev_device_ops hci_serdev_client_ops;
35+
36+
static 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+
56+
static 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+
68+
static 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+
104+
/* Initialize device */
105+
static int hci_uart_open(struct hci_dev *hdev)
106+
{
107+
struct hci_uart *hu = hci_get_drvdata(hdev);
108+
109+
BT_DBG("%s %p", hdev->name, hdev);
110+
111+
serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
112+
113+
return serdev_device_open(hu->serdev);
114+
}
115+
116+
/* Reset device */
117+
static int hci_uart_flush(struct hci_dev *hdev)
118+
{
119+
struct hci_uart *hu = hci_get_drvdata(hdev);
120+
121+
BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
122+
123+
if (hu->tx_skb) {
124+
kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
125+
}
126+
127+
/* Flush any pending characters in the driver and discipline. */
128+
serdev_device_write_flush(hu->serdev);
129+
130+
if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
131+
hu->proto->flush(hu);
132+
133+
return 0;
134+
}
135+
136+
/* Close device */
137+
static int hci_uart_close(struct hci_dev *hdev)
138+
{
139+
struct hci_uart *hu = hci_get_drvdata(hdev);
140+
141+
BT_DBG("hdev %p", hdev);
142+
143+
hci_uart_flush(hdev);
144+
hdev->flush = NULL;
145+
146+
serdev_device_close(hu->serdev);
147+
148+
return 0;
149+
}
150+
151+
/* Send frames from HCI layer */
152+
static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
153+
{
154+
struct hci_uart *hu = hci_get_drvdata(hdev);
155+
156+
BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
157+
skb->len);
158+
159+
hu->proto->enqueue(hu, skb);
160+
161+
hci_uart_tx_wakeup(hu);
162+
163+
return 0;
164+
}
165+
166+
static int hci_uart_setup(struct hci_dev *hdev)
167+
{
168+
struct hci_uart *hu = hci_get_drvdata(hdev);
169+
struct hci_rp_read_local_version *ver;
170+
struct sk_buff *skb;
171+
unsigned int speed;
172+
int err;
173+
174+
/* Init speed if any */
175+
if (hu->init_speed)
176+
speed = hu->init_speed;
177+
else if (hu->proto->init_speed)
178+
speed = hu->proto->init_speed;
179+
else
180+
speed = 0;
181+
182+
if (speed)
183+
serdev_device_set_baudrate(hu->serdev, speed);
184+
185+
/* Operational speed if any */
186+
if (hu->oper_speed)
187+
speed = hu->oper_speed;
188+
else if (hu->proto->oper_speed)
189+
speed = hu->proto->oper_speed;
190+
else
191+
speed = 0;
192+
193+
if (hu->proto->set_baudrate && speed) {
194+
err = hu->proto->set_baudrate(hu, speed);
195+
if (err)
196+
BT_ERR("%s: failed to set baudrate", hdev->name);
197+
else
198+
serdev_device_set_baudrate(hu->serdev, speed);
199+
}
200+
201+
if (hu->proto->setup)
202+
return hu->proto->setup(hu);
203+
204+
if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
205+
return 0;
206+
207+
skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
208+
HCI_INIT_TIMEOUT);
209+
if (IS_ERR(skb)) {
210+
BT_ERR("%s: Reading local version information failed (%ld)",
211+
hdev->name, PTR_ERR(skb));
212+
return 0;
213+
}
214+
215+
if (skb->len != sizeof(*ver)) {
216+
BT_ERR("%s: Event length mismatch for version information",
217+
hdev->name);
218+
}
219+
220+
kfree_skb(skb);
221+
return 0;
222+
}
223+
224+
/** hci_uart_write_wakeup - transmit buffer wakeup
225+
* @serdev: serial device
226+
*
227+
* This function is called by the serdev framework when it accepts
228+
* more data being sent.
229+
*/
230+
static void hci_uart_write_wakeup(struct serdev_device *serdev)
231+
{
232+
struct hci_uart *hu = serdev_device_get_drvdata(serdev);
233+
234+
BT_DBG("");
235+
236+
if (!hu || serdev != hu->serdev) {
237+
WARN_ON(1);
238+
return;
239+
}
240+
241+
if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
242+
hci_uart_tx_wakeup(hu);
243+
}
244+
245+
/** hci_uart_receive_buf - receive buffer wakeup
246+
* @serdev: serial device
247+
* @data: pointer to received data
248+
* @count: count of received data in bytes
249+
*
250+
* This function is called by the serdev framework when it received data
251+
* in the RX buffer.
252+
*
253+
* Return: number of processed bytes
254+
*/
255+
static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
256+
size_t count)
257+
{
258+
struct hci_uart *hu = serdev_device_get_drvdata(serdev);
259+
260+
if (!hu || serdev != hu->serdev) {
261+
WARN_ON(1);
262+
return 0;
263+
}
264+
265+
if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
266+
return 0;
267+
268+
/* It does not need a lock here as it is already protected by a mutex in
269+
* tty caller
270+
*/
271+
hu->proto->recv(hu, data, count);
272+
273+
if (hu->hdev)
274+
hu->hdev->stat.byte_rx += count;
275+
276+
return count;
277+
}
278+
279+
struct serdev_device_ops hci_serdev_client_ops = {
280+
.receive_buf = hci_uart_receive_buf,
281+
.write_wakeup = hci_uart_write_wakeup,
282+
};
283+
284+
int hci_uart_register_device(struct hci_uart *hu,
285+
const struct hci_uart_proto *p)
286+
{
287+
int err;
288+
struct hci_dev *hdev;
289+
290+
BT_DBG("");
291+
292+
err = p->open(hu);
293+
if (err)
294+
return err;
295+
296+
hu->proto = p;
297+
set_bit(HCI_UART_PROTO_READY, &hu->flags);
298+
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;
305+
}
306+
307+
hu->hdev = hdev;
308+
309+
hdev->bus = HCI_UART;
310+
hci_set_drvdata(hdev, hu);
311+
312+
INIT_WORK(&hu->write_work, hci_uart_write_work);
313+
314+
/* Only when vendor specific setup callback is provided, consider
315+
* the manufacturer information valid. This avoids filling in the
316+
* value for Ericsson when nothing is specified.
317+
*/
318+
if (hu->proto->setup)
319+
hdev->manufacturer = hu->proto->manufacturer;
320+
321+
hdev->open = hci_uart_open;
322+
hdev->close = hci_uart_close;
323+
hdev->flush = hci_uart_flush;
324+
hdev->send = hci_uart_send_frame;
325+
hdev->setup = hci_uart_setup;
326+
SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
327+
328+
if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
329+
set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
330+
331+
if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
332+
set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
333+
334+
if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
335+
set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
336+
337+
if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
338+
hdev->dev_type = HCI_AMP;
339+
else
340+
hdev->dev_type = HCI_PRIMARY;
341+
342+
if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
343+
return 0;
344+
345+
if (hci_register_dev(hdev) < 0) {
346+
BT_ERR("Can't register HCI device");
347+
err = -ENODEV;
348+
goto err_register;
349+
}
350+
351+
set_bit(HCI_UART_REGISTERED, &hu->flags);
352+
353+
return 0;
354+
355+
err_register:
356+
hci_free_dev(hdev);
357+
err_alloc:
358+
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
359+
p->close(hu);
360+
return err;
361+
}

drivers/bluetooth/hci_uart.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define HCI_UART_VND_DETECT 5
5959

6060
struct hci_uart;
61+
struct serdev_device;
6162

6263
struct hci_uart_proto {
6364
unsigned int id;
@@ -77,6 +78,7 @@ struct hci_uart_proto {
7778

7879
struct hci_uart {
7980
struct tty_struct *tty;
81+
struct serdev_device *serdev;
8082
struct hci_dev *hdev;
8183
unsigned long flags;
8284
unsigned long hdev_flags;
@@ -108,6 +110,8 @@ struct hci_uart {
108110

109111
int hci_uart_register_proto(const struct hci_uart_proto *p);
110112
int hci_uart_unregister_proto(const struct hci_uart_proto *p);
113+
int hci_uart_register_device(struct hci_uart *hu, const struct hci_uart_proto *p);
114+
111115
int hci_uart_tx_wakeup(struct hci_uart *hu);
112116
int hci_uart_init_ready(struct hci_uart *hu);
113117
void hci_uart_init_tty(struct hci_uart *hu);

0 commit comments

Comments
 (0)