Linux Audio

Check our new training course

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