2019-05-27 14:55:05 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2005-10-29 01:20:45 +08:00
|
|
|
*
|
|
|
|
* Bluetooth HCI UART driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
* Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2005-10-29 01:20:45 +08:00
|
|
|
#ifndef N_HCI
|
2005-04-17 06:20:36 +08:00
|
|
|
#define N_HCI 15
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Ioctls */
|
2007-05-09 15:15:35 +08:00
|
|
|
#define HCIUARTSETPROTO _IOW('U', 200, int)
|
|
|
|
#define HCIUARTGETPROTO _IOR('U', 201, int)
|
|
|
|
#define HCIUARTGETDEVICE _IOR('U', 202, int)
|
2010-07-12 22:37:04 +08:00
|
|
|
#define HCIUARTSETFLAGS _IOW('U', 203, int)
|
|
|
|
#define HCIUARTGETFLAGS _IOR('U', 204, int)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* UART protocols */
|
2016-09-19 22:29:27 +08:00
|
|
|
#define HCI_UART_MAX_PROTO 12
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#define HCI_UART_H4 0
|
|
|
|
#define HCI_UART_BCSP 1
|
|
|
|
#define HCI_UART_3WIRE 2
|
|
|
|
#define HCI_UART_H4DS 3
|
2007-10-20 19:42:36 +08:00
|
|
|
#define HCI_UART_LL 4
|
2010-07-19 15:04:07 +08:00
|
|
|
#define HCI_UART_ATH3K 5
|
2015-04-05 07:13:02 +08:00
|
|
|
#define HCI_UART_INTEL 6
|
2015-04-05 07:13:03 +08:00
|
|
|
#define HCI_UART_BCM 7
|
2015-08-11 05:24:17 +08:00
|
|
|
#define HCI_UART_QCA 8
|
2016-02-22 17:48:03 +08:00
|
|
|
#define HCI_UART_AG6XX 9
|
2016-09-19 17:32:35 +08:00
|
|
|
#define HCI_UART_NOKIA 10
|
2016-09-19 22:29:27 +08:00
|
|
|
#define HCI_UART_MRVL 11
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-07-12 22:37:04 +08:00
|
|
|
#define HCI_UART_RAW_DEVICE 0
|
2012-02-25 00:12:24 +08:00
|
|
|
#define HCI_UART_RESET_ON_INIT 1
|
2012-02-25 00:09:38 +08:00
|
|
|
#define HCI_UART_CREATE_AMP 2
|
2012-07-16 21:12:11 +08:00
|
|
|
#define HCI_UART_INIT_PENDING 3
|
2014-07-11 13:12:58 +08:00
|
|
|
#define HCI_UART_EXT_CONFIG 4
|
2015-04-05 07:13:01 +08:00
|
|
|
#define HCI_UART_VND_DETECT 5
|
2010-07-12 22:37:04 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hci_uart;
|
2017-03-28 23:59:35 +08:00
|
|
|
struct serdev_device;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct hci_uart_proto {
|
|
|
|
unsigned int id;
|
2015-04-05 13:27:34 +08:00
|
|
|
const char *name;
|
2015-10-21 03:30:45 +08:00
|
|
|
unsigned int manufacturer;
|
2015-05-28 17:25:03 +08:00
|
|
|
unsigned int init_speed;
|
|
|
|
unsigned int oper_speed;
|
2005-04-17 06:20:36 +08:00
|
|
|
int (*open)(struct hci_uart *hu);
|
|
|
|
int (*close)(struct hci_uart *hu);
|
|
|
|
int (*flush)(struct hci_uart *hu);
|
2015-04-06 13:52:17 +08:00
|
|
|
int (*setup)(struct hci_uart *hu);
|
2015-05-28 17:25:03 +08:00
|
|
|
int (*set_baudrate)(struct hci_uart *hu, unsigned int speed);
|
2015-04-05 11:59:41 +08:00
|
|
|
int (*recv)(struct hci_uart *hu, const void *data, int len);
|
2005-04-17 06:20:36 +08:00
|
|
|
int (*enqueue)(struct hci_uart *hu, struct sk_buff *skb);
|
|
|
|
struct sk_buff *(*dequeue)(struct hci_uart *hu);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hci_uart {
|
2005-10-29 01:20:45 +08:00
|
|
|
struct tty_struct *tty;
|
2017-03-28 23:59:35 +08:00
|
|
|
struct serdev_device *serdev;
|
2005-10-29 01:20:45 +08:00
|
|
|
struct hci_dev *hdev;
|
|
|
|
unsigned long flags;
|
2010-07-12 22:37:04 +08:00
|
|
|
unsigned long hdev_flags;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-07-16 21:12:11 +08:00
|
|
|
struct work_struct init_ready;
|
2014-04-23 22:58:26 +08:00
|
|
|
struct work_struct write_work;
|
2012-07-16 21:12:11 +08:00
|
|
|
|
2015-04-05 13:11:43 +08:00
|
|
|
const struct hci_uart_proto *proto;
|
Bluetooth: hci_ldisc: Allow sleeping while proto locks are held.
Commit dec2c92880cc5435381d50e3045ef018a762a917 ("Bluetooth: hci_ldisc:
Use rwlocking to avoid closing proto races") introduced locks in
hci_ldisc that are held while calling the proto functions. These locks
are rwlock's, and hence do not allow sleeping while they are held.
However, the proto functions that hci_bcm registers use mutexes and
hence need to be able to sleep.
In more detail: hci_uart_tty_receive() and hci_uart_dequeue() both
acquire the rwlock, after which they call proto->recv() and
proto->dequeue(), respectively. In the case of hci_bcm these point to
bcm_recv() and bcm_dequeue(). The latter both acquire the
bcm_device_lock, which is a mutex, so doing so results in a call to
might_sleep(). But since we're holding a rwlock in hci_ldisc, that
results in the following BUG (this for the dequeue case - a similar
one for the receive case is omitted for brevity):
BUG: sleeping function called from invalid context at kernel/locking/mutex.c
in_atomic(): 1, irqs_disabled(): 0, pid: 7303, name: kworker/7:3
INFO: lockdep is turned off.
CPU: 7 PID: 7303 Comm: kworker/7:3 Tainted: G W OE 4.13.2+ #17
Hardware name: Apple Inc. MacBookPro13,3/Mac-A5C67F76ED83108C, BIOS MBP133.8
Workqueue: events hci_uart_write_work [hci_uart]
Call Trace:
dump_stack+0x8e/0xd6
___might_sleep+0x164/0x250
__might_sleep+0x4a/0x80
__mutex_lock+0x59/0xa00
? lock_acquire+0xa3/0x1f0
? lock_acquire+0xa3/0x1f0
? hci_uart_write_work+0xd3/0x160 [hci_uart]
mutex_lock_nested+0x1b/0x20
? mutex_lock_nested+0x1b/0x20
bcm_dequeue+0x21/0xc0 [hci_uart]
hci_uart_write_work+0xe6/0x160 [hci_uart]
process_one_work+0x253/0x6a0
worker_thread+0x4d/0x3b0
kthread+0x133/0x150
We can't replace the mutex in hci_bcm, because there are other calls
there that might sleep. Therefore this replaces the rwlock's in
hci_ldisc with rw_semaphore's (which allow sleeping). This is a safer
approach anyway as it reduces the restrictions on the proto callbacks.
Also, because acquiring write-lock is very rare compared to acquiring
the read-lock, the percpu variant of rw_semaphore is used.
Lastly, because hci_uart_tx_wakeup() may be called from an IRQ context,
we can't block (sleep) while trying acquire the read lock there, so we
use the trylock variant.
Signed-off-by: Ronald Tschalär <ronald@innovation.ch>
Reviewed-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
2017-10-26 13:14:53 +08:00
|
|
|
struct percpu_rw_semaphore proto_lock; /* Stop work for proto close */
|
2005-10-29 01:20:45 +08:00
|
|
|
void *priv;
|
|
|
|
|
|
|
|
struct sk_buff *tx_skb;
|
|
|
|
unsigned long tx_state;
|
2015-06-18 05:30:56 +08:00
|
|
|
|
|
|
|
unsigned int init_speed;
|
|
|
|
unsigned int oper_speed;
|
2017-03-28 23:59:34 +08:00
|
|
|
|
|
|
|
u8 alignment;
|
|
|
|
u8 padding;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2010-07-12 22:37:04 +08:00
|
|
|
/* HCI_UART proto flag bits */
|
2005-10-29 01:20:45 +08:00
|
|
|
#define HCI_UART_PROTO_SET 0
|
2012-07-16 21:12:11 +08:00
|
|
|
#define HCI_UART_REGISTERED 1
|
2016-04-04 16:48:13 +08:00
|
|
|
#define HCI_UART_PROTO_READY 2
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* TX states */
|
2005-10-29 01:20:45 +08:00
|
|
|
#define HCI_UART_SENDING 1
|
|
|
|
#define HCI_UART_TX_WAKEUP 2
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-04-05 13:11:43 +08:00
|
|
|
int hci_uart_register_proto(const struct hci_uart_proto *p);
|
|
|
|
int hci_uart_unregister_proto(const struct hci_uart_proto *p);
|
2017-03-28 23:59:35 +08:00
|
|
|
int hci_uart_register_device(struct hci_uart *hu, const struct hci_uart_proto *p);
|
2017-07-09 00:37:41 +08:00
|
|
|
void hci_uart_unregister_device(struct hci_uart *hu);
|
2017-03-28 23:59:35 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int hci_uart_tx_wakeup(struct hci_uart *hu);
|
2019-06-14 15:23:49 +08:00
|
|
|
int hci_uart_wait_until_sent(struct hci_uart *hu);
|
2012-07-16 21:12:11 +08:00
|
|
|
int hci_uart_init_ready(struct hci_uart *hu);
|
2018-05-28 03:04:53 +08:00
|
|
|
void hci_uart_init_work(struct work_struct *work);
|
2015-05-28 17:25:03 +08:00
|
|
|
void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed);
|
2015-06-18 05:30:56 +08:00
|
|
|
void hci_uart_set_flow_control(struct hci_uart *hu, bool enable);
|
|
|
|
void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
|
|
|
|
unsigned int oper_speed);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-10-29 01:20:45 +08:00
|
|
|
#ifdef CONFIG_BT_HCIUART_H4
|
|
|
|
int h4_init(void);
|
|
|
|
int h4_deinit(void);
|
2015-04-05 12:59:24 +08:00
|
|
|
|
2015-04-06 14:44:59 +08:00
|
|
|
struct h4_recv_pkt {
|
|
|
|
u8 type; /* Packet type */
|
|
|
|
u8 hlen; /* Header length */
|
|
|
|
u8 loff; /* Data length offset in header */
|
|
|
|
u8 lsize; /* Data length field size */
|
|
|
|
u16 maxlen; /* Max overall packet length */
|
|
|
|
int (*recv)(struct hci_dev *hdev, struct sk_buff *skb);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define H4_RECV_ACL \
|
|
|
|
.type = HCI_ACLDATA_PKT, \
|
|
|
|
.hlen = HCI_ACL_HDR_SIZE, \
|
|
|
|
.loff = 2, \
|
|
|
|
.lsize = 2, \
|
|
|
|
.maxlen = HCI_MAX_FRAME_SIZE \
|
|
|
|
|
|
|
|
#define H4_RECV_SCO \
|
|
|
|
.type = HCI_SCODATA_PKT, \
|
|
|
|
.hlen = HCI_SCO_HDR_SIZE, \
|
|
|
|
.loff = 2, \
|
|
|
|
.lsize = 1, \
|
|
|
|
.maxlen = HCI_MAX_SCO_SIZE
|
|
|
|
|
|
|
|
#define H4_RECV_EVENT \
|
|
|
|
.type = HCI_EVENT_PKT, \
|
|
|
|
.hlen = HCI_EVENT_HDR_SIZE, \
|
|
|
|
.loff = 1, \
|
|
|
|
.lsize = 1, \
|
|
|
|
.maxlen = HCI_MAX_EVENT_SIZE
|
|
|
|
|
2015-04-05 12:59:24 +08:00
|
|
|
struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
|
2015-04-06 14:44:59 +08:00
|
|
|
const unsigned char *buffer, int count,
|
|
|
|
const struct h4_recv_pkt *pkts, int pkts_count);
|
2005-10-29 01:20:45 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_BCSP
|
|
|
|
int bcsp_init(void);
|
|
|
|
int bcsp_deinit(void);
|
|
|
|
#endif
|
2007-10-20 19:42:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_LL
|
|
|
|
int ll_init(void);
|
|
|
|
int ll_deinit(void);
|
|
|
|
#endif
|
2010-07-19 15:04:07 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_ATH3K
|
|
|
|
int ath_init(void);
|
|
|
|
int ath_deinit(void);
|
|
|
|
#endif
|
2012-07-16 21:12:02 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_3WIRE
|
|
|
|
int h5_init(void);
|
|
|
|
int h5_deinit(void);
|
|
|
|
#endif
|
2015-04-05 07:13:02 +08:00
|
|
|
|
2015-07-01 18:20:26 +08:00
|
|
|
#ifdef CONFIG_BT_HCIUART_INTEL
|
|
|
|
int intel_init(void);
|
|
|
|
int intel_deinit(void);
|
|
|
|
#endif
|
|
|
|
|
2015-04-06 13:52:18 +08:00
|
|
|
#ifdef CONFIG_BT_HCIUART_BCM
|
|
|
|
int bcm_init(void);
|
|
|
|
int bcm_deinit(void);
|
|
|
|
#endif
|
2015-08-11 05:24:17 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_QCA
|
|
|
|
int qca_init(void);
|
|
|
|
int qca_deinit(void);
|
|
|
|
#endif
|
2016-02-22 17:48:03 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_AG6XX
|
|
|
|
int ag6xx_init(void);
|
|
|
|
int ag6xx_deinit(void);
|
|
|
|
#endif
|
2016-09-19 22:29:27 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_MRVL
|
|
|
|
int mrvl_init(void);
|
|
|
|
int mrvl_deinit(void);
|
|
|
|
#endif
|