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