1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Bluetooth HCI UART driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2000-2001 Qualcomm Incorporated
6*4882a593Smuzhiyun * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7*4882a593Smuzhiyun * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun * it under the terms of the GNU General Public License as published by
12*4882a593Smuzhiyun * the Free Software Foundation; either version 2 of the License, or
13*4882a593Smuzhiyun * (at your option) any later version.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
16*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*4882a593Smuzhiyun * GNU General Public License for more details.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
21*4882a593Smuzhiyun * along with this program; if not, write to the Free Software
22*4882a593Smuzhiyun * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/kernel.h>
28*4882a593Smuzhiyun #include <linux/init.h>
29*4882a593Smuzhiyun #include <linux/types.h>
30*4882a593Smuzhiyun #include <linux/fcntl.h>
31*4882a593Smuzhiyun #include <linux/interrupt.h>
32*4882a593Smuzhiyun #include <linux/ptrace.h>
33*4882a593Smuzhiyun #include <linux/poll.h>
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <linux/tty.h>
36*4882a593Smuzhiyun #include <linux/errno.h>
37*4882a593Smuzhiyun #include <linux/string.h>
38*4882a593Smuzhiyun #include <linux/signal.h>
39*4882a593Smuzhiyun #include <linux/ioctl.h>
40*4882a593Smuzhiyun #include <linux/skbuff.h>
41*4882a593Smuzhiyun #include <linux/version.h>
42*4882a593Smuzhiyun #include <net/bluetooth/bluetooth.h>
43*4882a593Smuzhiyun #include <net/bluetooth/hci_core.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include "hci_uart.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define NEW_TX_SCHED_POLICY
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #if WOBT_NOTIFY
50*4882a593Smuzhiyun #include <linux/suspend.h>
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #ifdef BTCOEX
54*4882a593Smuzhiyun #include "rtk_coex.h"
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #define VERSION "2.2.3634cd9.20220519-142433"
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #if HCI_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
60*4882a593Smuzhiyun #define GET_DRV_DATA(x) hci_get_drvdata(x)
61*4882a593Smuzhiyun #else
62*4882a593Smuzhiyun #define GET_DRV_DATA(x) (struct hci_uart *)(x->driver_data)
63*4882a593Smuzhiyun #endif
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #define SEMWAIT_TIMEOUT 50
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #if WOBT_NOTIFY
68*4882a593Smuzhiyun struct hci_rsp_read_local {
69*4882a593Smuzhiyun __u8 status;
70*4882a593Smuzhiyun __u8 hci_ver;
71*4882a593Smuzhiyun __le16 hci_rev;
72*4882a593Smuzhiyun __u8 lmp_ver;
73*4882a593Smuzhiyun __le16 manufacturer;
74*4882a593Smuzhiyun __le16 lmp_subver;
75*4882a593Smuzhiyun } __packed;
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
79*4882a593Smuzhiyun static int reset = 0;
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
83*4882a593Smuzhiyun static int hci_uart_flush(struct hci_dev *hdev);
84*4882a593Smuzhiyun
hci_uart_register_proto(struct hci_uart_proto * p)85*4882a593Smuzhiyun int hci_uart_register_proto(struct hci_uart_proto *p)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun if (p->id >= HCI_UART_MAX_PROTO)
88*4882a593Smuzhiyun return -EINVAL;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (hup[p->id])
91*4882a593Smuzhiyun return -EEXIST;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun hup[p->id] = p;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
hci_uart_unregister_proto(struct hci_uart_proto * p)98*4882a593Smuzhiyun int hci_uart_unregister_proto(struct hci_uart_proto *p)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun if (p->id >= HCI_UART_MAX_PROTO)
101*4882a593Smuzhiyun return -EINVAL;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun if (!hup[p->id])
104*4882a593Smuzhiyun return -EINVAL;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun hup[p->id] = NULL;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
hci_uart_get_proto(unsigned int id)111*4882a593Smuzhiyun static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun if (id >= HCI_UART_MAX_PROTO)
114*4882a593Smuzhiyun return NULL;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun return hup[id];
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
hci_uart_tx_complete(struct hci_uart * hu,int pkt_type)119*4882a593Smuzhiyun static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun struct hci_dev *hdev = hu->hdev;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* Update HCI stat counters */
124*4882a593Smuzhiyun switch (pkt_type) {
125*4882a593Smuzhiyun case HCI_COMMAND_PKT:
126*4882a593Smuzhiyun hdev->stat.cmd_tx++;
127*4882a593Smuzhiyun break;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun case HCI_ACLDATA_PKT:
130*4882a593Smuzhiyun hdev->stat.acl_tx++;
131*4882a593Smuzhiyun break;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun case HCI_SCODATA_PKT:
134*4882a593Smuzhiyun hdev->stat.sco_tx++;
135*4882a593Smuzhiyun break;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
hci_proto_read_lock(struct hci_uart * hu)139*4882a593Smuzhiyun static inline void hci_proto_read_lock(struct hci_uart *hu)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
142*4882a593Smuzhiyun percpu_down_read(&hu->proto_lock);
143*4882a593Smuzhiyun #else
144*4882a593Smuzhiyun down_read(&hu->proto_lock);
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
hci_proto_read_trylock(struct hci_uart * hu)148*4882a593Smuzhiyun static inline int hci_proto_read_trylock(struct hci_uart *hu)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
151*4882a593Smuzhiyun return percpu_down_read_trylock(&hu->proto_lock);
152*4882a593Smuzhiyun #else
153*4882a593Smuzhiyun return down_read_trylock(&hu->proto_lock);
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
hci_proto_read_unlock(struct hci_uart * hu)157*4882a593Smuzhiyun static inline void hci_proto_read_unlock(struct hci_uart *hu)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
160*4882a593Smuzhiyun percpu_up_read(&hu->proto_lock);
161*4882a593Smuzhiyun #else
162*4882a593Smuzhiyun up_read(&hu->proto_lock);
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
hci_proto_write_lock(struct hci_uart * hu)166*4882a593Smuzhiyun static inline void hci_proto_write_lock(struct hci_uart *hu)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
169*4882a593Smuzhiyun percpu_down_write(&hu->proto_lock);
170*4882a593Smuzhiyun #else
171*4882a593Smuzhiyun down_write(&hu->proto_lock);
172*4882a593Smuzhiyun #endif
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
hci_proto_write_unlock(struct hci_uart * hu)175*4882a593Smuzhiyun static inline void hci_proto_write_unlock(struct hci_uart *hu)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
178*4882a593Smuzhiyun percpu_up_write(&hu->proto_lock);
179*4882a593Smuzhiyun #else
180*4882a593Smuzhiyun up_write(&hu->proto_lock);
181*4882a593Smuzhiyun #endif
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
hci_proto_init_rwlock(struct hci_uart * hu)184*4882a593Smuzhiyun static inline int hci_proto_init_rwlock(struct hci_uart *hu)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
187*4882a593Smuzhiyun return percpu_init_rwsem(&hu->proto_lock);
188*4882a593Smuzhiyun #else
189*4882a593Smuzhiyun init_rwsem(&hu->proto_lock);
190*4882a593Smuzhiyun return 0;
191*4882a593Smuzhiyun #endif
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
hci_proto_free_rwlock(struct hci_uart * hu)194*4882a593Smuzhiyun static inline void hci_proto_free_rwlock(struct hci_uart *hu)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
197*4882a593Smuzhiyun percpu_free_rwsem(&hu->proto_lock);
198*4882a593Smuzhiyun #endif
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
hci_uart_dequeue(struct hci_uart * hu)201*4882a593Smuzhiyun static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun struct sk_buff *skb = hu->tx_skb;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (!skb) {
206*4882a593Smuzhiyun hci_proto_read_lock(hu);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
209*4882a593Smuzhiyun skb = hu->proto->dequeue(hu);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun hci_proto_read_unlock(hu);
212*4882a593Smuzhiyun } else {
213*4882a593Smuzhiyun hu->tx_skb = NULL;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun return skb;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* This may be called in an IRQ context */
hci_uart_tx_wakeup(struct hci_uart * hu)220*4882a593Smuzhiyun int hci_uart_tx_wakeup(struct hci_uart *hu)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun /* If acquiring lock fails we assume the tty is being closed because
223*4882a593Smuzhiyun * that is the only time the write lock is acquired. If, however,
224*4882a593Smuzhiyun * at some point in the future the write lock is also acquired in
225*4882a593Smuzhiyun * other situations, then this must be revisited.
226*4882a593Smuzhiyun */
227*4882a593Smuzhiyun if (!hci_proto_read_trylock(hu))
228*4882a593Smuzhiyun return 0;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* proto_lock is locked */
231*4882a593Smuzhiyun if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
232*4882a593Smuzhiyun goto no_schedule;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun #ifdef NEW_TX_SCHED_POLICY
235*4882a593Smuzhiyun set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
236*4882a593Smuzhiyun if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
237*4882a593Smuzhiyun goto no_schedule;
238*4882a593Smuzhiyun #else
239*4882a593Smuzhiyun if (in_interrupt() || in_atomic()) {
240*4882a593Smuzhiyun if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
241*4882a593Smuzhiyun set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
242*4882a593Smuzhiyun goto no_schedule;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun } else {
245*4882a593Smuzhiyun /* NOTE: proto_lock can't be spin lock, because it may
246*4882a593Smuzhiyun * schedule here. Schedule is not allowed while atomic
247*4882a593Smuzhiyun */
248*4882a593Smuzhiyun if (down_timeout(&hu->tx_sem,
249*4882a593Smuzhiyun msecs_to_jiffies(SEMWAIT_TIMEOUT)) == -ETIME) {
250*4882a593Smuzhiyun pr_warn("%s: Something went wrong with wait\n",
251*4882a593Smuzhiyun __func__);
252*4882a593Smuzhiyun goto no_schedule;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun /* semaphore is locked */
255*4882a593Smuzhiyun if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
256*4882a593Smuzhiyun set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
257*4882a593Smuzhiyun up(&hu->tx_sem);
258*4882a593Smuzhiyun goto no_schedule;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun up(&hu->tx_sem);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun #endif
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun BT_DBG("");
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun schedule_work(&hu->write_work);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun no_schedule:
269*4882a593Smuzhiyun hci_proto_read_unlock(hu);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return 0;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
hci_uart_write_work(struct work_struct * work)274*4882a593Smuzhiyun static void hci_uart_write_work(struct work_struct *work)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
277*4882a593Smuzhiyun struct tty_struct *tty = hu->tty;
278*4882a593Smuzhiyun struct hci_dev *hdev = hu->hdev;
279*4882a593Smuzhiyun struct sk_buff *skb;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* REVISIT: should we cope with bad skbs or ->write() returning
282*4882a593Smuzhiyun * and error value ?
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun restart:
286*4882a593Smuzhiyun clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun while ((skb = hci_uart_dequeue(hu))) {
289*4882a593Smuzhiyun int len;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
292*4882a593Smuzhiyun len = tty->ops->write(tty, skb->data, skb->len);
293*4882a593Smuzhiyun hdev->stat.byte_tx += len;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun skb_pull(skb, len);
296*4882a593Smuzhiyun if (skb->len) {
297*4882a593Smuzhiyun hu->tx_skb = skb;
298*4882a593Smuzhiyun break;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
302*4882a593Smuzhiyun kfree_skb(skb);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun #ifdef NEW_TX_SCHED_POLICY
306*4882a593Smuzhiyun clear_bit(HCI_UART_SENDING, &hu->tx_state);
307*4882a593Smuzhiyun if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
308*4882a593Smuzhiyun goto restart;
309*4882a593Smuzhiyun #else
310*4882a593Smuzhiyun if (down_timeout(&hu->tx_sem, msecs_to_jiffies(SEMWAIT_TIMEOUT))) {
311*4882a593Smuzhiyun pr_warn("%s: Something went wrong with wait\n", __func__);
312*4882a593Smuzhiyun goto restart;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun /* semaphore is locked */
315*4882a593Smuzhiyun if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)) {
316*4882a593Smuzhiyun up(&hu->tx_sem);
317*4882a593Smuzhiyun goto restart;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun clear_bit(HCI_UART_SENDING, &hu->tx_state);
321*4882a593Smuzhiyun up(&hu->tx_sem);
322*4882a593Smuzhiyun #endif
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /* ------- Interface to HCI layer ------ */
328*4882a593Smuzhiyun /* Initialize device */
hci_uart_open(struct hci_dev * hdev)329*4882a593Smuzhiyun static int hci_uart_open(struct hci_dev *hdev)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun BT_DBG("%s %p", hdev->name, hdev);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* Undo clearing this from hci_uart_close() */
334*4882a593Smuzhiyun hdev->flush = hci_uart_flush;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
337*4882a593Smuzhiyun set_bit(HCI_RUNNING, &hdev->flags);
338*4882a593Smuzhiyun #endif
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun #ifdef BTCOEX
341*4882a593Smuzhiyun rtk_btcoex_open(hdev);
342*4882a593Smuzhiyun #endif
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return 0;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /* static void hci_flush_sync(struct hci_dev *hdev)
348*4882a593Smuzhiyun * {
349*4882a593Smuzhiyun * #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
350*4882a593Smuzhiyun * u8 buf[2] = { 0, 0 };
351*4882a593Smuzhiyun * struct sk_buff *skb;
352*4882a593Smuzhiyun *
353*4882a593Smuzhiyun * BT_INFO("hci flush sync");
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * set_bit(HCI_INIT, &hdev->flags);
356*4882a593Smuzhiyun * skb = __hci_cmd_sync(hdev, 0xfc19, 2, buf, msecs_to_jiffies(2000));
357*4882a593Smuzhiyun * clear_bit(HCI_INIT, &hdev->flags);
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * if (IS_ERR(skb)) {
360*4882a593Smuzhiyun * BT_ERR("command 0xfc19 tx failed (%ld)\n", PTR_ERR(skb));
361*4882a593Smuzhiyun * return;
362*4882a593Smuzhiyun * }
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * if (skb->len == 1)
365*4882a593Smuzhiyun * BT_INFO("hci flush sync status %u", skb->data[0]);
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * kfree_skb(skb);
368*4882a593Smuzhiyun * #endif
369*4882a593Smuzhiyun * }
370*4882a593Smuzhiyun */
371*4882a593Smuzhiyun
__hci_uart_flush(struct hci_dev * hdev,u8 sync)372*4882a593Smuzhiyun static int __hci_uart_flush(struct hci_dev *hdev, u8 sync)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun struct hci_uart *hu = GET_DRV_DATA(hdev); //(struct hci_uart *) hdev->driver_data;
375*4882a593Smuzhiyun struct tty_struct *tty = hu->tty;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun BT_INFO("%s: hdev %p tty %p", __func__, hdev, tty);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* Make sure all HCI packets has been transmitted */
380*4882a593Smuzhiyun /* if (sync && test_bit(HCI_RUNNING, &hdev->flags))
381*4882a593Smuzhiyun * hci_flush_sync(hdev);
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun if (hu->tx_skb) {
385*4882a593Smuzhiyun kfree_skb(hu->tx_skb);
386*4882a593Smuzhiyun hu->tx_skb = NULL;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /* Flush any pending characters in the driver and discipline. */
390*4882a593Smuzhiyun /* tty_ldisc_flush(tty);
391*4882a593Smuzhiyun * tty_driver_flush_buffer(tty);
392*4882a593Smuzhiyun */
393*4882a593Smuzhiyun /* Don't flush the tty. Sometime, the hdev is closed abnormally.
394*4882a593Smuzhiyun * There may be cmd complete event in rx buf or the sent ack in tx buf.
395*4882a593Smuzhiyun * tty flush will result in hciX: command 0xXXXX tx timeout
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun tty_wait_until_sent(tty, msecs_to_jiffies(500));
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun hci_proto_read_lock(hu);
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
402*4882a593Smuzhiyun hu->proto->flush(hu);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun hci_proto_read_unlock(hu);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun return 0;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /* Reset device */
hci_uart_flush(struct hci_dev * hdev)410*4882a593Smuzhiyun static int hci_uart_flush(struct hci_dev *hdev)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun return __hci_uart_flush(hdev, 1);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* Close device */
hci_uart_close(struct hci_dev * hdev)416*4882a593Smuzhiyun static int hci_uart_close(struct hci_dev *hdev)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun BT_INFO("%s: hdev %p", __func__, hdev);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /* When in kernel 4.4.0 and greater, the HCI_RUNNING bit is
421*4882a593Smuzhiyun * cleared in hci_dev_do_close(). */
422*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
423*4882a593Smuzhiyun if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
424*4882a593Smuzhiyun return 0;
425*4882a593Smuzhiyun #else
426*4882a593Smuzhiyun if (test_bit(HCI_RUNNING, &hdev->flags))
427*4882a593Smuzhiyun BT_ERR("HCI_RUNNING is not cleared before.");
428*4882a593Smuzhiyun #endif
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (test_bit(HCI_RUNNING, &hdev->flags))
431*4882a593Smuzhiyun __hci_uart_flush(hdev, 0);
432*4882a593Smuzhiyun else
433*4882a593Smuzhiyun __hci_uart_flush(hdev, 1);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun hdev->flush = NULL;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun #ifdef BTCOEX
438*4882a593Smuzhiyun rtk_btcoex_close();
439*4882a593Smuzhiyun #endif
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun return 0;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /* Send frames from HCI layer */
445*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
hci_uart_send_frame(struct sk_buff * skb)446*4882a593Smuzhiyun int hci_uart_send_frame(struct sk_buff *skb)
447*4882a593Smuzhiyun #else
448*4882a593Smuzhiyun int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
449*4882a593Smuzhiyun #endif
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
452*4882a593Smuzhiyun struct hci_dev *hdev = (struct hci_dev *)skb->dev;
453*4882a593Smuzhiyun #endif
454*4882a593Smuzhiyun struct hci_uart *hu;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun if (!hdev) {
457*4882a593Smuzhiyun BT_ERR("Frame for unknown device (hdev=NULL)");
458*4882a593Smuzhiyun return -ENODEV;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
462*4882a593Smuzhiyun if (!test_bit(HCI_RUNNING, &hdev->flags))
463*4882a593Smuzhiyun return -EBUSY;
464*4882a593Smuzhiyun #endif
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun hu = GET_DRV_DATA(hdev); //(struct hci_uart *) hdev->driver_data;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
469*4882a593Smuzhiyun skb->len);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun #ifdef BTCOEX
472*4882a593Smuzhiyun if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)
473*4882a593Smuzhiyun rtk_btcoex_parse_cmd(skb->data, skb->len);
474*4882a593Smuzhiyun if (bt_cb(skb)->pkt_type == HCI_ACLDATA_PKT)
475*4882a593Smuzhiyun rtk_btcoex_parse_l2cap_data_tx(skb->data, skb->len);
476*4882a593Smuzhiyun #endif
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun hci_proto_read_lock(hu);
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
481*4882a593Smuzhiyun hci_proto_read_unlock(hu);
482*4882a593Smuzhiyun return -EUNATCH;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun hu->proto->enqueue(hu, skb);
486*4882a593Smuzhiyun hci_proto_read_unlock(hu);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun hci_uart_tx_wakeup(hu);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun return 0;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
hci_uart_destruct(struct hci_dev * hdev)494*4882a593Smuzhiyun static void hci_uart_destruct(struct hci_dev *hdev)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun if (!hdev)
497*4882a593Smuzhiyun return;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun BT_DBG("%s", hdev->name);
500*4882a593Smuzhiyun kfree(hdev->driver_data);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun #endif
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun #if WOBT_NOTIFY
505*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
skb_put_data(struct sk_buff * skb,const void * data,unsigned int len)506*4882a593Smuzhiyun static inline void *skb_put_data(struct sk_buff *skb, const void *data,
507*4882a593Smuzhiyun unsigned int len)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun void *tmp = skb_put(skb, len);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun memcpy(tmp, data, len);
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun return tmp;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun #endif
516*4882a593Smuzhiyun
hci_uart_async_send(struct hci_uart * hu,u16 opcode,u32 plen,const void * param)517*4882a593Smuzhiyun static int hci_uart_async_send(struct hci_uart *hu, u16 opcode,
518*4882a593Smuzhiyun u32 plen, const void *param)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun int len = HCI_COMMAND_HDR_SIZE + plen;
521*4882a593Smuzhiyun struct hci_command_hdr *hdr;
522*4882a593Smuzhiyun struct sk_buff *skb;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun skb = bt_skb_alloc(len, GFP_ATOMIC);
525*4882a593Smuzhiyun if (!skb)
526*4882a593Smuzhiyun return -ENOMEM;
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun hdr = (struct hci_command_hdr *)skb_put(skb, HCI_COMMAND_HDR_SIZE);
529*4882a593Smuzhiyun hdr->opcode = cpu_to_le16(opcode);
530*4882a593Smuzhiyun hdr->plen = plen;
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun if (plen)
533*4882a593Smuzhiyun memcpy(skb_put(skb, plen), param, plen);
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun BT_INFO("rtl: skb len %d", skb->len);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
540*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
541*4882a593Smuzhiyun bt_cb(skb)->opcode = opcode;
542*4882a593Smuzhiyun #else
543*4882a593Smuzhiyun bt_cb(skb)->hci.opcode = opcode;
544*4882a593Smuzhiyun #endif
545*4882a593Smuzhiyun #endif
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /* Stand-alone HCI commands must be flagged as
548*4882a593Smuzhiyun * single-command requests.
549*4882a593Smuzhiyun */
550*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
551*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
552*4882a593Smuzhiyun bt_cb(skb)->req.start = true;
553*4882a593Smuzhiyun #else
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
556*4882a593Smuzhiyun bt_cb(skb)->hci.req_start = true;
557*4882a593Smuzhiyun #else
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
560*4882a593Smuzhiyun #endif
561*4882a593Smuzhiyun #endif /* 4.4.0 */
562*4882a593Smuzhiyun #endif /* 3.10.0 */
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
565*4882a593Smuzhiyun hci_uart_send_frame(skb);
566*4882a593Smuzhiyun #else
567*4882a593Smuzhiyun hci_uart_send_frame(hu->hdev, skb);
568*4882a593Smuzhiyun #endif
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun /* hci_proto_read_lock(hu);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun * if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
573*4882a593Smuzhiyun * hci_proto_read_unlock(hu);
574*4882a593Smuzhiyun * BT_ERR("rtl send: proto not ready");
575*4882a593Smuzhiyun * return -EUNATCH;
576*4882a593Smuzhiyun * }
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun * hu->proto->enqueue(hu, skb);
579*4882a593Smuzhiyun * hci_proto_read_unlock(hu);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun * hci_uart_tx_wakeup(hu);
582*4882a593Smuzhiyun */
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun return 0;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
rtl_read_local_version(struct hci_dev * hdev,u8 * hci_ver,u16 * hci_rev,u16 * lmp_subver)587*4882a593Smuzhiyun static int rtl_read_local_version(struct hci_dev *hdev, u8 *hci_ver,
588*4882a593Smuzhiyun u16 *hci_rev, u16 *lmp_subver)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun struct hci_rsp_read_local *ver;
591*4882a593Smuzhiyun struct sk_buff *skb;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun skb = __hci_cmd_sync(hdev, 0x1001, 0, NULL, HCI_INIT_TIMEOUT);
594*4882a593Smuzhiyun if (IS_ERR(skb)) {
595*4882a593Smuzhiyun BT_ERR("rtl: Could not read lmp subversion");
596*4882a593Smuzhiyun return PTR_ERR(skb);
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun if (skb->len != sizeof(struct hci_rsp_read_local)) {
600*4882a593Smuzhiyun BT_ERR("%s: rtl: Local version length mismatch", hdev->name);
601*4882a593Smuzhiyun kfree_skb(skb);
602*4882a593Smuzhiyun return -EIO;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun ver = (struct hci_rsp_read_local *)skb->data;
606*4882a593Smuzhiyun *hci_ver = ver->hci_ver;
607*4882a593Smuzhiyun *hci_rev = le16_to_cpu(ver->hci_rev);
608*4882a593Smuzhiyun *lmp_subver = le16_to_cpu(ver->lmp_subver);
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun kfree_skb(skb);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun return 0;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun #if RTKBT_TV_POWERON_WHITELIST
rtkbt_lookup_le_device_poweron_whitelist(struct hci_uart * hu)616*4882a593Smuzhiyun static int rtkbt_lookup_le_device_poweron_whitelist(struct hci_uart *hu)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun struct hci_conn_params *p;
619*4882a593Smuzhiyun u8 *params;
620*4882a593Smuzhiyun int result = 0;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun hci_dev_lock(hu->hdev);
623*4882a593Smuzhiyun list_for_each_entry(p, &hu->hdev->le_conn_params, list) {
624*4882a593Smuzhiyun #if 0 // for debug message
625*4882a593Smuzhiyun BT_INFO("%s(): auto_connect = %d", __FUNCTION__, p->auto_connect);
626*4882a593Smuzhiyun BT_INFO("%s(): addr_type = 0x%02x", __FUNCTION__, p->addr_type);
627*4882a593Smuzhiyun BT_INFO("%s(): addr=%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
628*4882a593Smuzhiyun p->addr.b[5], p->addr.b[4], p->addr.b[3],
629*4882a593Smuzhiyun p->addr.b[2], p->addr.b[1], p->addr.b[0]);
630*4882a593Smuzhiyun #endif
631*4882a593Smuzhiyun if ( p->auto_connect == HCI_AUTO_CONN_ALWAYS &&
632*4882a593Smuzhiyun p->addr_type == ADDR_LE_DEV_PUBLIC ) {
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun BT_INFO("%s(): Set RTKBT LE Power-on Whitelist for "
635*4882a593Smuzhiyun "%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
636*4882a593Smuzhiyun p->addr.b[5], p->addr.b[4], p->addr.b[3],
637*4882a593Smuzhiyun p->addr.b[2], p->addr.b[1], p->addr.b[0]);
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun params = kzalloc(8, GFP_ATOMIC);
640*4882a593Smuzhiyun if (!params) {
641*4882a593Smuzhiyun BT_ERR("Can't allocate memory for params");
642*4882a593Smuzhiyun return -ENOMEM;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun params[0] = 0x00;
646*4882a593Smuzhiyun params[1] = p->addr.b[0];
647*4882a593Smuzhiyun params[2] = p->addr.b[1];
648*4882a593Smuzhiyun params[3] = p->addr.b[2];
649*4882a593Smuzhiyun params[4] = p->addr.b[3];
650*4882a593Smuzhiyun params[5] = p->addr.b[4];
651*4882a593Smuzhiyun params[6] = p->addr.b[5];
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun result = hci_uart_async_send(hu, 0xfc7b, 7, params);
654*4882a593Smuzhiyun if (result)
655*4882a593Smuzhiyun BT_ERR("rtl: Command failed for power-on whitelist");
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun msleep(500);
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun kfree(params);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun hci_dev_unlock(hu->hdev);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun return result;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun #endif
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun #if RTKBT_TV_POWERON_DATA_FILTER
rtkbt_set_le_device_poweron_data_filter(struct hci_uart * hu)669*4882a593Smuzhiyun static int rtkbt_set_le_device_poweron_data_filter(struct hci_uart *hu)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun /* Set data filter on Manufacturer field of Advertising data */
672*4882a593Smuzhiyun /* Manufacturer | ID | Additional data*/
673*4882a593Smuzhiyun /* Technicolor | 0x02af | 0x57, 0x41, 0x4b, 0x45, 0x55, 0x50 */
674*4882a593Smuzhiyun u8 params[8] = { 0xaf, 0x02, // Manufacturer ID
675*4882a593Smuzhiyun 0x57, 0x41, 0x4b, 0x45, 0x55, 0x50 }; // Additional data
676*4882a593Smuzhiyun int result = 0;
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun result = hci_uart_async_send(hu, 0xfc7f, 8, params);
679*4882a593Smuzhiyun if (result)
680*4882a593Smuzhiyun BT_ERR("rtl: Command failed for set data filter");
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun return result;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun #endif
685*4882a593Smuzhiyun
rtkbt_simulate_disconnect_event(struct hci_uart * hu)686*4882a593Smuzhiyun static int rtkbt_simulate_disconnect_event(struct hci_uart *hu)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun struct hci_conn *conn;
689*4882a593Smuzhiyun struct sk_buff *rx_skb;
690*4882a593Smuzhiyun u8 event_params[6] = { 0x05, 0x04, 0x00, 0x10, 0x00, 0x13 };
691*4882a593Smuzhiyun int result = 0;
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun hci_dev_lock(hu->hdev);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun conn = hci_conn_hash_lookup_state(hu->hdev, LE_LINK, BT_CONNECTED);
696*4882a593Smuzhiyun if (conn && (conn->state == BT_CONNECTED)){
697*4882a593Smuzhiyun rx_skb = alloc_skb(6, GFP_ATOMIC);
698*4882a593Smuzhiyun if (!rx_skb)
699*4882a593Smuzhiyun return -1;
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun event_params[3] = (u8)(conn->handle);
702*4882a593Smuzhiyun event_params[4] = (u8)(conn->handle >> 8);
703*4882a593Smuzhiyun hci_skb_pkt_type(rx_skb) = HCI_EVENT_PKT;
704*4882a593Smuzhiyun skb_put_data(rx_skb, event_params, 6);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun BT_INFO("Send Disconnect Complete EVENT to upper stack");
707*4882a593Smuzhiyun hci_recv_frame(hu->hdev, rx_skb);
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun hci_dev_unlock(hu->hdev);
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun msleep(1000);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun return result;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
rtkbt_notify_suspend(struct hci_uart * hu)717*4882a593Smuzhiyun static int rtkbt_notify_suspend(struct hci_uart *hu)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun u8 params_suspend_notify[1] = { 0x01 };
720*4882a593Smuzhiyun int result = 0;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun result = hci_uart_async_send(hu, 0xfc28, 1, params_suspend_notify);
723*4882a593Smuzhiyun if (result)
724*4882a593Smuzhiyun BT_ERR("Realtek suspend h5-bt failed");
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun msleep(500);
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun return result;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
le_scan_disable(struct hci_uart * hu)731*4882a593Smuzhiyun static void le_scan_disable(struct hci_uart *hu)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
734*4882a593Smuzhiyun if (use_ext_scan(hu->hdev)) {
735*4882a593Smuzhiyun u8 ext_enable_cp[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun hci_uart_async_send(hu, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 6, ext_enable_cp);
738*4882a593Smuzhiyun } else {
739*4882a593Smuzhiyun u8 enable_cp[2] = {0x00, 0x00};
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun #else
744*4882a593Smuzhiyun u8 enable_cp[2] = {0x00, 0x00};
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
747*4882a593Smuzhiyun #endif
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun return;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
le_scan_restart(struct hci_uart * hu)752*4882a593Smuzhiyun static void le_scan_restart(struct hci_uart *hu)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun int result;
755*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
756*4882a593Smuzhiyun if (use_ext_scan(hu->hdev)) {
757*4882a593Smuzhiyun u8 ext_enable_cp[6] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun BT_INFO("LE Extended Scan Restart...");
760*4882a593Smuzhiyun le_scan_disable(hu);
761*4882a593Smuzhiyun result = hci_uart_async_send(hu, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 6, ext_enable_cp);
762*4882a593Smuzhiyun if (result)
763*4882a593Smuzhiyun BT_ERR("LE Extended Scan Restart: Failed");
764*4882a593Smuzhiyun } else {
765*4882a593Smuzhiyun u8 enable_cp[2] = {0x01, 0x01};
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun BT_INFO("LE Scan Restart...");
768*4882a593Smuzhiyun le_scan_disable(hu);
769*4882a593Smuzhiyun result = hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
770*4882a593Smuzhiyun if (result)
771*4882a593Smuzhiyun BT_ERR("LE Scan Restart: Failed");
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun #else
774*4882a593Smuzhiyun u8 enable_cp[2] = {0x01, 0x01};
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun BT_INFO("LE Scan Restart");
777*4882a593Smuzhiyun le_scan_disable(hu);
778*4882a593Smuzhiyun result = hci_uart_async_send(hu, HCI_OP_LE_SET_SCAN_ENABLE, 2, enable_cp);
779*4882a593Smuzhiyun if (result)
780*4882a593Smuzhiyun BT_ERR("LE Scan Restart: Failed");
781*4882a593Smuzhiyun #endif
782*4882a593Smuzhiyun return;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
le_aoto_conn_always_exist(struct hci_uart * hu)785*4882a593Smuzhiyun static bool le_aoto_conn_always_exist(struct hci_uart *hu)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun struct hci_conn_params *p;
788*4882a593Smuzhiyun bool ret = false;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun hci_dev_lock(hu->hdev);
791*4882a593Smuzhiyun list_for_each_entry(p, &hu->hdev->le_conn_params, list) {
792*4882a593Smuzhiyun if ( p->auto_connect == HCI_AUTO_CONN_ALWAYS &&
793*4882a593Smuzhiyun p->addr_type == ADDR_LE_DEV_PUBLIC ) {
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun ret = true;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun hci_dev_unlock(hu->hdev);
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun return ret;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
hci_uart_pm_notifier(struct notifier_block * b,unsigned long v,void * d)803*4882a593Smuzhiyun static int hci_uart_pm_notifier(struct notifier_block *b, unsigned long v, void *d)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun int result;
806*4882a593Smuzhiyun struct hci_uart *hu = container_of(b, struct hci_uart, pm_notify_block);
807*4882a593Smuzhiyun u8 hci_ver = 0;
808*4882a593Smuzhiyun u16 hci_rev = 0;
809*4882a593Smuzhiyun u16 lmp_subver = 0;
810*4882a593Smuzhiyun #if WOBT_NOTIFY_BG_SCAN_LE_WHITELIST_ONLY
811*4882a593Smuzhiyun u8 params_bg_scan[5] = { 0x60, 0x01, 0x10, 0x00, 0x01 };
812*4882a593Smuzhiyun #endif
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun BT_INFO("%s: %lu", __func__, v);
815*4882a593Smuzhiyun switch (v) {
816*4882a593Smuzhiyun case PM_SUSPEND_PREPARE:
817*4882a593Smuzhiyun BT_INFO("rtl: bt suspending");
818*4882a593Smuzhiyun #if WOBT_NOTIFY_BG_SCAN_LE_WHITELIST_ONLY
819*4882a593Smuzhiyun /* Send set back ground scan parameters to Controller for power-on mode */
820*4882a593Smuzhiyun result = hci_uart_async_send(hu, 0xfc7a, 5, params_bg_scan);
821*4882a593Smuzhiyun if (result)
822*4882a593Smuzhiyun BT_ERR("Realtek bg-scan h5-bt failed");
823*4882a593Smuzhiyun /* FIXME: Ensure the above vendor command is sent to Controller
824*4882a593Smuzhiyun * and we received the h5 ack from Controller
825*4882a593Smuzhiyun * */
826*4882a593Smuzhiyun msleep(500);
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun #endif
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun #if RTKBT_TV_POWERON_WHITELIST
831*4882a593Smuzhiyun result = rtkbt_lookup_le_device_poweron_whitelist(hu);
832*4882a593Smuzhiyun if (result < 0) {
833*4882a593Smuzhiyun BT_ERR("rtkbt_lookup_le_device_poweron_whitelist error: %d", result);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun #endif
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun #if RTKBT_TV_POWERON_DATA_FILTER
838*4882a593Smuzhiyun result = rtkbt_set_le_device_poweron_data_filter(hu);
839*4882a593Smuzhiyun if (result < 0) {
840*4882a593Smuzhiyun BT_ERR("rtkbt_set_le_device_poweron_data_filter error: %d", result);
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun #endif
843*4882a593Smuzhiyun result = rtkbt_notify_suspend(hu);
844*4882a593Smuzhiyun if (result < 0) {
845*4882a593Smuzhiyun BT_ERR("rtkbt_notify_suspend error: %d", result);
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun break;
849*4882a593Smuzhiyun case PM_POST_SUSPEND:
850*4882a593Smuzhiyun result = rtl_read_local_version(hu->hdev, &hci_ver, &hci_rev,
851*4882a593Smuzhiyun &lmp_subver);
852*4882a593Smuzhiyun if (result)
853*4882a593Smuzhiyun break;
854*4882a593Smuzhiyun BT_INFO("rtl resume: hci ver %u, hci rev %04x, lmp subver %04x",
855*4882a593Smuzhiyun hci_ver, hci_rev, lmp_subver);
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun result = rtkbt_simulate_disconnect_event(hu);
858*4882a593Smuzhiyun if (result < 0)
859*4882a593Smuzhiyun BT_ERR("rtkbt_simulate_disconnect_event error: %d", result);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun if (le_aoto_conn_always_exist(hu))
862*4882a593Smuzhiyun le_scan_restart(hu);
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun break;
865*4882a593Smuzhiyun default:
866*4882a593Smuzhiyun BT_INFO("Caught msg %lu other than SUSPEND_PREPARE", v);
867*4882a593Smuzhiyun break;
868*4882a593Smuzhiyun }
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun return 0;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun #endif
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun /* ------ LDISC part ------ */
875*4882a593Smuzhiyun /* hci_uart_tty_open
876*4882a593Smuzhiyun *
877*4882a593Smuzhiyun * Called when line discipline changed to HCI_UART.
878*4882a593Smuzhiyun *
879*4882a593Smuzhiyun * Arguments:
880*4882a593Smuzhiyun * tty pointer to tty info structure
881*4882a593Smuzhiyun * Return Value:
882*4882a593Smuzhiyun * 0 if success, otherwise error code
883*4882a593Smuzhiyun */
hci_uart_tty_open(struct tty_struct * tty)884*4882a593Smuzhiyun static int hci_uart_tty_open(struct tty_struct *tty)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun struct hci_uart *hu = (void *)tty->disc_data;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun BT_DBG("tty %p", tty);
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /* But nothing ensures disc_data to be NULL. And since ld->ops->open
891*4882a593Smuzhiyun * shall be called only once, we do not need the check at all.
892*4882a593Smuzhiyun * So remove it.
893*4882a593Smuzhiyun *
894*4882a593Smuzhiyun * Note that this is not an issue now, but n_tty will start using the
895*4882a593Smuzhiyun * disc_data pointer and this invalid 'if' would trigger then rendering
896*4882a593Smuzhiyun * TTYs over BT unusable.
897*4882a593Smuzhiyun */
898*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
899*4882a593Smuzhiyun /* FIXME: This btw is bogus, nothing requires the old ldisc to clear
900*4882a593Smuzhiyun * the pointer
901*4882a593Smuzhiyun */
902*4882a593Smuzhiyun if (hu)
903*4882a593Smuzhiyun return -EEXIST;
904*4882a593Smuzhiyun #endif
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /* Error if the tty has no write op instead of leaving an exploitable
907*4882a593Smuzhiyun * hole
908*4882a593Smuzhiyun */
909*4882a593Smuzhiyun if (tty->ops->write == NULL)
910*4882a593Smuzhiyun return -EOPNOTSUPP;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
913*4882a593Smuzhiyun BT_ERR("Can't allocate control structure");
914*4882a593Smuzhiyun return -ENFILE;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun tty->disc_data = hu;
918*4882a593Smuzhiyun hu->tty = tty;
919*4882a593Smuzhiyun tty->receive_room = 65536;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun INIT_WORK(&hu->write_work, hci_uart_write_work);
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun hci_proto_init_rwlock(hu);
924*4882a593Smuzhiyun sema_init(&hu->tx_sem, 1);
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun /* Flush any pending characters in the driver and line discipline. */
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /* FIXME: why is this needed. Note don't use ldisc_ref here as the
929*4882a593Smuzhiyun open path is before the ldisc is referencable */
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun if (tty->ldisc->ops->flush_buffer)
932*4882a593Smuzhiyun tty->ldisc->ops->flush_buffer(tty);
933*4882a593Smuzhiyun tty_driver_flush_buffer(tty);
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun #if WOBT_NOTIFY
936*4882a593Smuzhiyun hu->pm_notify_block.notifier_call = hci_uart_pm_notifier;
937*4882a593Smuzhiyun register_pm_notifier(&hu->pm_notify_block);
938*4882a593Smuzhiyun #endif
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun return 0;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /* hci_uart_tty_close()
944*4882a593Smuzhiyun *
945*4882a593Smuzhiyun * Called when the line discipline is changed to something
946*4882a593Smuzhiyun * else, the tty is closed, or the tty detects a hangup.
947*4882a593Smuzhiyun */
hci_uart_tty_close(struct tty_struct * tty)948*4882a593Smuzhiyun static void hci_uart_tty_close(struct tty_struct *tty)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun struct hci_uart *hu = (void *)tty->disc_data;
951*4882a593Smuzhiyun struct hci_dev *hdev;
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun BT_INFO("%s: tty %p", __func__, tty);
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /* Detach from the tty */
956*4882a593Smuzhiyun tty->disc_data = NULL;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun if (!hu)
959*4882a593Smuzhiyun return;
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun hdev = hu->hdev;
962*4882a593Smuzhiyun if (hdev)
963*4882a593Smuzhiyun hci_uart_close(hdev);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
966*4882a593Smuzhiyun hci_proto_write_lock(hu);
967*4882a593Smuzhiyun clear_bit(HCI_UART_PROTO_READY, &hu->flags);
968*4882a593Smuzhiyun hci_proto_write_unlock(hu);
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun cancel_work_sync(&hu->write_work);
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun if (hdev) {
973*4882a593Smuzhiyun if (test_bit(HCI_UART_REGISTERED, &hu->flags))
974*4882a593Smuzhiyun hci_unregister_dev(hdev);
975*4882a593Smuzhiyun hci_free_dev(hdev);
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun hu->proto->close(hu);
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun clear_bit(HCI_UART_PROTO_SET, &hu->flags);
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun hci_proto_free_rwlock(hu);
982*4882a593Smuzhiyun #if WOBT_NOTIFY
983*4882a593Smuzhiyun unregister_pm_notifier(&hu->pm_notify_block);
984*4882a593Smuzhiyun #endif
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun kfree(hu);
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /* hci_uart_tty_wakeup()
990*4882a593Smuzhiyun *
991*4882a593Smuzhiyun * Callback for transmit wakeup. Called when low level
992*4882a593Smuzhiyun * device driver can accept more send data.
993*4882a593Smuzhiyun *
994*4882a593Smuzhiyun * Arguments: tty pointer to associated tty instance data
995*4882a593Smuzhiyun * Return Value: None
996*4882a593Smuzhiyun */
hci_uart_tty_wakeup(struct tty_struct * tty)997*4882a593Smuzhiyun static void hci_uart_tty_wakeup(struct tty_struct *tty)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun struct hci_uart *hu = (void *)tty->disc_data;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun BT_DBG("");
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun if (!hu)
1004*4882a593Smuzhiyun return;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun if (tty != hu->tty)
1009*4882a593Smuzhiyun return;
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
1012*4882a593Smuzhiyun hci_uart_tx_wakeup(hu);
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /* hci_uart_tty_receive()
1016*4882a593Smuzhiyun *
1017*4882a593Smuzhiyun * Called by tty low level driver when receive data is
1018*4882a593Smuzhiyun * available.
1019*4882a593Smuzhiyun *
1020*4882a593Smuzhiyun * Arguments: tty pointer to tty isntance data
1021*4882a593Smuzhiyun * data pointer to received data
1022*4882a593Smuzhiyun * flags pointer to flags for data
1023*4882a593Smuzhiyun * count count of received data in bytes
1024*4882a593Smuzhiyun *
1025*4882a593Smuzhiyun * Return Value: None
1026*4882a593Smuzhiyun */
hci_uart_tty_receive(struct tty_struct * tty,const u8 * data,const char * flags,int count)1027*4882a593Smuzhiyun static void hci_uart_tty_receive(struct tty_struct *tty, const u8 * data,
1028*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
1029*4882a593Smuzhiyun const char *flags, int count)
1030*4882a593Smuzhiyun #else
1031*4882a593Smuzhiyun char *flags, int count)
1032*4882a593Smuzhiyun #endif
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun struct hci_uart *hu = (void *)tty->disc_data;
1035*4882a593Smuzhiyun int (*proto_receive)(struct hci_uart *hu, void *data, int len);
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun if (!hu || tty != hu->tty)
1038*4882a593Smuzhiyun return;
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun hci_proto_read_lock(hu);
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
1043*4882a593Smuzhiyun hci_proto_read_unlock(hu);
1044*4882a593Smuzhiyun return;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun proto_receive = hu->proto->recv;
1048*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
1049*4882a593Smuzhiyun proto_receive(hu, (void *)data, count);
1050*4882a593Smuzhiyun hci_proto_read_unlock(hu);
1051*4882a593Smuzhiyun #else
1052*4882a593Smuzhiyun hci_proto_read_unlock(hu);
1053*4882a593Smuzhiyun /* It does not need a lock here as it is already protected by a mutex in
1054*4882a593Smuzhiyun * tty caller
1055*4882a593Smuzhiyun */
1056*4882a593Smuzhiyun proto_receive(hu, (void *)data, count);
1057*4882a593Smuzhiyun #endif
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun if (hu->hdev)
1060*4882a593Smuzhiyun hu->hdev->stat.byte_rx += count;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun tty_unthrottle(tty);
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
hci_uart_register_dev(struct hci_uart * hu)1065*4882a593Smuzhiyun static int hci_uart_register_dev(struct hci_uart *hu)
1066*4882a593Smuzhiyun {
1067*4882a593Smuzhiyun struct hci_dev *hdev;
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun BT_INFO("hci_uart_register_dev");
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun /* Initialize and register HCI device */
1072*4882a593Smuzhiyun hdev = hci_alloc_dev();
1073*4882a593Smuzhiyun if (!hdev) {
1074*4882a593Smuzhiyun BT_ERR("Can't allocate HCI device");
1075*4882a593Smuzhiyun return -ENOMEM;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun hu->hdev = hdev;
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun #if HCI_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
1081*4882a593Smuzhiyun hdev->bus = HCI_UART;
1082*4882a593Smuzhiyun #else
1083*4882a593Smuzhiyun hdev->type = HCI_UART;
1084*4882a593Smuzhiyun #endif
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
1087*4882a593Smuzhiyun hci_set_drvdata(hdev, hu);
1088*4882a593Smuzhiyun #else
1089*4882a593Smuzhiyun hdev->driver_data = hu;
1090*4882a593Smuzhiyun #endif
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun hdev->open = hci_uart_open;
1093*4882a593Smuzhiyun hdev->close = hci_uart_close;
1094*4882a593Smuzhiyun hdev->flush = hci_uart_flush;
1095*4882a593Smuzhiyun hdev->send = hci_uart_send_frame;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /* NOTE: No hdev->setup setting for Realtek BTUART because
1098*4882a593Smuzhiyun * the download procedure is done with rtk_hciattach in userspace
1099*4882a593Smuzhiyun * before this function called in hci_uart_set_proto()
1100*4882a593Smuzhiyun */
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun SET_HCIDEV_DEV(hdev, hu->tty->dev);
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
1105*4882a593Smuzhiyun hdev->destruct = hci_uart_destruct;
1106*4882a593Smuzhiyun hdev->owner = THIS_MODULE;
1107*4882a593Smuzhiyun #endif
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
1110*4882a593Smuzhiyun if (!reset)
1111*4882a593Smuzhiyun set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1112*4882a593Smuzhiyun #endif
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
1115*4882a593Smuzhiyun if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
1116*4882a593Smuzhiyun set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1117*4882a593Smuzhiyun #endif
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
1120*4882a593Smuzhiyun if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
1121*4882a593Smuzhiyun set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
1122*4882a593Smuzhiyun #endif
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
1125*4882a593Smuzhiyun if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
1126*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
1127*4882a593Smuzhiyun set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1128*4882a593Smuzhiyun #else
1129*4882a593Smuzhiyun set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1130*4882a593Smuzhiyun #endif
1131*4882a593Smuzhiyun #endif
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
1134*4882a593Smuzhiyun if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
1135*4882a593Smuzhiyun hdev->dev_type = HCI_AMP;
1136*4882a593Smuzhiyun else
1137*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
1138*4882a593Smuzhiyun hdev->dev_type = HCI_BREDR;
1139*4882a593Smuzhiyun #else
1140*4882a593Smuzhiyun hdev->dev_type = HCI_PRIMARY;
1141*4882a593Smuzhiyun #endif
1142*4882a593Smuzhiyun #endif
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
1145*4882a593Smuzhiyun set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1146*4882a593Smuzhiyun #endif
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(5, 10, 21)
1149*4882a593Smuzhiyun #if WOBT_NOTIFY
1150*4882a593Smuzhiyun set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
1151*4882a593Smuzhiyun #endif
1152*4882a593Smuzhiyun #endif
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun if (hci_register_dev(hdev) < 0) {
1155*4882a593Smuzhiyun BT_ERR("Can't register HCI device");
1156*4882a593Smuzhiyun hci_free_dev(hdev);
1157*4882a593Smuzhiyun return -ENODEV;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun set_bit(HCI_UART_REGISTERED, &hu->flags);
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun #ifdef BTCOEX
1163*4882a593Smuzhiyun rtk_btcoex_probe(hdev);
1164*4882a593Smuzhiyun #endif
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
hci_uart_set_proto(struct hci_uart * hu,int id)1169*4882a593Smuzhiyun static int hci_uart_set_proto(struct hci_uart *hu, int id)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun struct hci_uart_proto *p;
1172*4882a593Smuzhiyun int err;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun p = hci_uart_get_proto(id);
1175*4882a593Smuzhiyun if (!p)
1176*4882a593Smuzhiyun return -EPROTONOSUPPORT;
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun err = p->open(hu);
1179*4882a593Smuzhiyun if (err)
1180*4882a593Smuzhiyun return err;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun hu->proto = p;
1183*4882a593Smuzhiyun set_bit(HCI_UART_PROTO_READY, &hu->flags);
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun /* Initialize and register HCI dev */
1186*4882a593Smuzhiyun err = hci_uart_register_dev(hu);
1187*4882a593Smuzhiyun if (err) {
1188*4882a593Smuzhiyun clear_bit(HCI_UART_PROTO_READY, &hu->flags);
1189*4882a593Smuzhiyun p->close(hu);
1190*4882a593Smuzhiyun return err;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun return 0;
1194*4882a593Smuzhiyun }
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
hci_uart_set_flags(struct hci_uart * hu,unsigned long flags)1197*4882a593Smuzhiyun static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun /* TODO: Add HCI_UART_INIT_PENDING, HCI_UART_VND_DETECT check */
1200*4882a593Smuzhiyun unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
1201*4882a593Smuzhiyun BIT(HCI_UART_RESET_ON_INIT) |
1202*4882a593Smuzhiyun BIT(HCI_UART_CREATE_AMP) |
1203*4882a593Smuzhiyun BIT(HCI_UART_EXT_CONFIG);
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun if (flags & ~valid_flags)
1206*4882a593Smuzhiyun return -EINVAL;
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun hu->hdev_flags = flags;
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun return 0;
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun #endif
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun /* hci_uart_tty_ioctl()
1215*4882a593Smuzhiyun *
1216*4882a593Smuzhiyun * Process IOCTL system call for the tty device.
1217*4882a593Smuzhiyun *
1218*4882a593Smuzhiyun * Arguments:
1219*4882a593Smuzhiyun *
1220*4882a593Smuzhiyun * tty pointer to tty instance data
1221*4882a593Smuzhiyun * file pointer to open file object for device
1222*4882a593Smuzhiyun * cmd IOCTL command code
1223*4882a593Smuzhiyun * arg argument for IOCTL call (cmd dependent)
1224*4882a593Smuzhiyun *
1225*4882a593Smuzhiyun * Return Value: Command dependent
1226*4882a593Smuzhiyun */
hci_uart_tty_ioctl(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)1227*4882a593Smuzhiyun static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
1228*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun struct hci_uart *hu = (void *)tty->disc_data;
1231*4882a593Smuzhiyun int err = 0;
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun BT_DBG("");
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun /* Verify the status of the device */
1236*4882a593Smuzhiyun if (!hu)
1237*4882a593Smuzhiyun return -EBADF;
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun switch (cmd) {
1240*4882a593Smuzhiyun case HCIUARTSETPROTO:
1241*4882a593Smuzhiyun if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1242*4882a593Smuzhiyun err = hci_uart_set_proto(hu, arg);
1243*4882a593Smuzhiyun if (err) {
1244*4882a593Smuzhiyun clear_bit(HCI_UART_PROTO_SET, &hu->flags);
1245*4882a593Smuzhiyun return err;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun } else
1248*4882a593Smuzhiyun return -EBUSY;
1249*4882a593Smuzhiyun break;
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun case HCIUARTGETPROTO:
1252*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1253*4882a593Smuzhiyun return hu->proto->id;
1254*4882a593Smuzhiyun return -EUNATCH;
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun case HCIUARTGETDEVICE:
1257*4882a593Smuzhiyun if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1258*4882a593Smuzhiyun return hu->hdev->id;
1259*4882a593Smuzhiyun return -EUNATCH;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun case HCIUARTSETFLAGS:
1262*4882a593Smuzhiyun if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1263*4882a593Smuzhiyun return -EBUSY;
1264*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
1265*4882a593Smuzhiyun err = hci_uart_set_flags(hu, arg);
1266*4882a593Smuzhiyun if (err)
1267*4882a593Smuzhiyun return err;
1268*4882a593Smuzhiyun #else
1269*4882a593Smuzhiyun hu->hdev_flags = arg;
1270*4882a593Smuzhiyun #endif
1271*4882a593Smuzhiyun break;
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun case HCIUARTGETFLAGS:
1274*4882a593Smuzhiyun return hu->hdev_flags;
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun default:
1277*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
1278*4882a593Smuzhiyun err = n_tty_ioctl_helper(tty, cmd, arg);
1279*4882a593Smuzhiyun #else
1280*4882a593Smuzhiyun err = n_tty_ioctl_helper(tty, file, cmd, arg);
1281*4882a593Smuzhiyun #endif
1282*4882a593Smuzhiyun break;
1283*4882a593Smuzhiyun };
1284*4882a593Smuzhiyun
1285*4882a593Smuzhiyun return err;
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun /*
1289*4882a593Smuzhiyun * We don't provide read/write/poll interface for user space.
1290*4882a593Smuzhiyun */
1291*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 20) && \
1292*4882a593Smuzhiyun ((LINUX_VERSION_CODE < KERNEL_VERSION(5, 11, 0)) || \
1293*4882a593Smuzhiyun (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 3)))
hci_uart_tty_read(struct tty_struct * tty,struct file * file,unsigned char * buf,size_t nr,void ** cookie,unsigned long offset)1294*4882a593Smuzhiyun static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1295*4882a593Smuzhiyun unsigned char *buf, size_t nr,
1296*4882a593Smuzhiyun void **cookie, unsigned long offset)
1297*4882a593Smuzhiyun #else
1298*4882a593Smuzhiyun static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1299*4882a593Smuzhiyun unsigned char __user * buf, size_t nr)
1300*4882a593Smuzhiyun #endif
1301*4882a593Smuzhiyun {
1302*4882a593Smuzhiyun return 0;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun
hci_uart_tty_write(struct tty_struct * tty,struct file * file,const unsigned char * data,size_t count)1305*4882a593Smuzhiyun static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
1306*4882a593Smuzhiyun const unsigned char *data, size_t count)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun return 0;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
hci_uart_tty_poll(struct tty_struct * tty,struct file * filp,poll_table * wait)1311*4882a593Smuzhiyun static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
1312*4882a593Smuzhiyun struct file *filp, poll_table * wait)
1313*4882a593Smuzhiyun {
1314*4882a593Smuzhiyun return 0;
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun static struct tty_ldisc_ops hci_uart_ldisc = {
1318*4882a593Smuzhiyun .owner = THIS_MODULE,
1319*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
1320*4882a593Smuzhiyun .num = N_HCI,
1321*4882a593Smuzhiyun #endif
1322*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
1323*4882a593Smuzhiyun .magic = TTY_LDISC_MAGIC,
1324*4882a593Smuzhiyun #endif
1325*4882a593Smuzhiyun .name = "n_hci",
1326*4882a593Smuzhiyun .open = hci_uart_tty_open,
1327*4882a593Smuzhiyun .close = hci_uart_tty_close,
1328*4882a593Smuzhiyun .read = hci_uart_tty_read,
1329*4882a593Smuzhiyun .write = hci_uart_tty_write,
1330*4882a593Smuzhiyun .ioctl = hci_uart_tty_ioctl,
1331*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
1332*4882a593Smuzhiyun .compat_ioctl = hci_uart_tty_ioctl,
1333*4882a593Smuzhiyun #endif
1334*4882a593Smuzhiyun .poll = hci_uart_tty_poll,
1335*4882a593Smuzhiyun .receive_buf = hci_uart_tty_receive,
1336*4882a593Smuzhiyun .write_wakeup = hci_uart_tty_wakeup,
1337*4882a593Smuzhiyun };
1338*4882a593Smuzhiyun
hci_uart_init(void)1339*4882a593Smuzhiyun static int __init hci_uart_init(void)
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun int err;
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun BT_INFO("HCI UART driver ver %s", VERSION);
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun /* Register the tty discipline */
1346*4882a593Smuzhiyun #if HCI_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
1347*4882a593Smuzhiyun if ((err = tty_register_ldisc(&hci_uart_ldisc))) {
1348*4882a593Smuzhiyun #else
1349*4882a593Smuzhiyun if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
1350*4882a593Smuzhiyun #endif
1351*4882a593Smuzhiyun BT_ERR("HCI line discipline registration failed. (%d)", err);
1352*4882a593Smuzhiyun return err;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun #ifdef CONFIG_BT_HCIUART_H4
1355*4882a593Smuzhiyun h4_init();
1356*4882a593Smuzhiyun #endif
1357*4882a593Smuzhiyun /* Add realtek h5 support */
1358*4882a593Smuzhiyun h5_init();
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun #ifdef BTCOEX
1361*4882a593Smuzhiyun rtk_btcoex_init();
1362*4882a593Smuzhiyun #endif
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun return 0;
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun static void __exit hci_uart_exit(void)
1368*4882a593Smuzhiyun {
1369*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0)
1370*4882a593Smuzhiyun int err;
1371*4882a593Smuzhiyun #endif
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun #ifdef CONFIG_BT_HCIUART_H4
1374*4882a593Smuzhiyun h4_deinit();
1375*4882a593Smuzhiyun #endif
1376*4882a593Smuzhiyun h5_deinit();
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
1379*4882a593Smuzhiyun tty_unregister_ldisc(&hci_uart_ldisc);
1380*4882a593Smuzhiyun #else
1381*4882a593Smuzhiyun /* Release tty registration of line discipline */
1382*4882a593Smuzhiyun if ((err = tty_unregister_ldisc(N_HCI)))
1383*4882a593Smuzhiyun BT_ERR("Can't unregister HCI line discipline (%d)", err);
1384*4882a593Smuzhiyun #endif
1385*4882a593Smuzhiyun
1386*4882a593Smuzhiyun #ifdef BTCOEX
1387*4882a593Smuzhiyun rtk_btcoex_exit();
1388*4882a593Smuzhiyun #endif
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun module_init(hci_uart_init);
1392*4882a593Smuzhiyun module_exit(hci_uart_exit);
1393*4882a593Smuzhiyun
1394*4882a593Smuzhiyun #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
1395*4882a593Smuzhiyun module_param(reset, bool, 0644);
1396*4882a593Smuzhiyun MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1397*4882a593Smuzhiyun #endif
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1400*4882a593Smuzhiyun MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
1401*4882a593Smuzhiyun MODULE_VERSION(VERSION);
1402*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1403*4882a593Smuzhiyun MODULE_ALIAS_LDISC(N_HCI);
1404