xref: /linux/drivers/bluetooth/hci_ldisc.c (revision 5ca8c91d1ea6534842e7e0065d15104d802506cd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth HCI UART driver
5  *
6  *  Copyright (C) 2000-2001  Qualcomm Incorporated
7  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
8  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/interrupt.h>
18 #include <linux/ptrace.h>
19 #include <linux/poll.h>
20 
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/signal.h>
26 #include <linux/ioctl.h>
27 #include <linux/skbuff.h>
28 #include <linux/firmware.h>
29 #include <linux/serdev.h>
30 
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
33 
34 #include "btintel.h"
35 #include "btbcm.h"
36 #include "hci_uart.h"
37 
38 #define VERSION "2.3"
39 
40 static const struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
41 
42 int hci_uart_register_proto(const struct hci_uart_proto *p)
43 {
44 	if (p->id >= HCI_UART_MAX_PROTO)
45 		return -EINVAL;
46 
47 	if (hup[p->id])
48 		return -EEXIST;
49 
50 	hup[p->id] = p;
51 
52 	BT_INFO("HCI UART protocol %s registered", p->name);
53 
54 	return 0;
55 }
56 
57 int hci_uart_unregister_proto(const struct hci_uart_proto *p)
58 {
59 	if (p->id >= HCI_UART_MAX_PROTO)
60 		return -EINVAL;
61 
62 	if (!hup[p->id])
63 		return -EINVAL;
64 
65 	hup[p->id] = NULL;
66 
67 	return 0;
68 }
69 
70 static const struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
71 {
72 	if (id >= HCI_UART_MAX_PROTO)
73 		return NULL;
74 
75 	return hup[id];
76 }
77 
78 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
79 {
80 	struct hci_dev *hdev = hu->hdev;
81 
82 	/* Update HCI stat counters */
83 	switch (pkt_type) {
84 	case HCI_COMMAND_PKT:
85 		hdev->stat.cmd_tx++;
86 		break;
87 
88 	case HCI_ACLDATA_PKT:
89 		hdev->stat.acl_tx++;
90 		break;
91 
92 	case HCI_SCODATA_PKT:
93 		hdev->stat.sco_tx++;
94 		break;
95 	}
96 }
97 
98 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
99 {
100 	struct sk_buff *skb = hu->tx_skb;
101 
102 	if (!skb) {
103 		percpu_down_read(&hu->proto_lock);
104 
105 		if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
106 		    test_bit(HCI_UART_PROTO_INIT, &hu->flags))
107 			skb = hu->proto->dequeue(hu);
108 
109 		percpu_up_read(&hu->proto_lock);
110 	} else {
111 		hu->tx_skb = NULL;
112 	}
113 
114 	return skb;
115 }
116 
117 int hci_uart_tx_wakeup(struct hci_uart *hu)
118 {
119 	/* This may be called in an IRQ context, so we can't sleep. Therefore
120 	 * we try to acquire the lock only, and if that fails we assume the
121 	 * tty is being closed because that is the only time the write lock is
122 	 * acquired. If, however, at some point in the future the write lock
123 	 * is also acquired in other situations, then this must be revisited.
124 	 */
125 	if (!percpu_down_read_trylock(&hu->proto_lock))
126 		return 0;
127 
128 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
129 	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags))
130 		goto no_schedule;
131 
132 	set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
133 	if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
134 		goto no_schedule;
135 
136 	BT_DBG("");
137 
138 	schedule_work(&hu->write_work);
139 
140 no_schedule:
141 	percpu_up_read(&hu->proto_lock);
142 
143 	return 0;
144 }
145 EXPORT_SYMBOL_GPL(hci_uart_tx_wakeup);
146 
147 static void hci_uart_write_work(struct work_struct *work)
148 {
149 	struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
150 	struct tty_struct *tty = hu->tty;
151 	struct hci_dev *hdev = hu->hdev;
152 	struct sk_buff *skb;
153 
154 	/* REVISIT: should we cope with bad skbs or ->write() returning
155 	 * and error value ?
156 	 */
157 
158 restart:
159 	clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
160 
161 	while ((skb = hci_uart_dequeue(hu))) {
162 		int len;
163 
164 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
165 		len = tty->ops->write(tty, skb->data, skb->len);
166 		hdev->stat.byte_tx += len;
167 
168 		skb_pull(skb, len);
169 		if (skb->len) {
170 			hu->tx_skb = skb;
171 			break;
172 		}
173 
174 		hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
175 		kfree_skb(skb);
176 	}
177 
178 	clear_bit(HCI_UART_SENDING, &hu->tx_state);
179 	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
180 		goto restart;
181 
182 	wake_up_bit(&hu->tx_state, HCI_UART_SENDING);
183 }
184 
185 void hci_uart_init_work(struct work_struct *work)
186 {
187 	struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
188 	int err;
189 	struct hci_dev *hdev;
190 
191 	if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
192 		return;
193 
194 	err = hci_register_dev(hu->hdev);
195 	if (err < 0) {
196 		BT_ERR("Can't register HCI device");
197 
198 		percpu_down_write(&hu->proto_lock);
199 		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
200 		percpu_up_write(&hu->proto_lock);
201 
202 		/* Safely cancel work after clearing flags */
203 		cancel_work_sync(&hu->write_work);
204 
205 		/* Close protocol before freeing hdev */
206 		hu->proto->close(hu);
207 		hdev = hu->hdev;
208 		hu->hdev = NULL;
209 		hci_free_dev(hdev);
210 		return;
211 	}
212 
213 	set_bit(HCI_UART_REGISTERED, &hu->flags);
214 }
215 
216 int hci_uart_init_ready(struct hci_uart *hu)
217 {
218 	if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
219 		return -EALREADY;
220 
221 	schedule_work(&hu->init_ready);
222 
223 	return 0;
224 }
225 
226 int hci_uart_wait_until_sent(struct hci_uart *hu)
227 {
228 	return wait_on_bit_timeout(&hu->tx_state, HCI_UART_SENDING,
229 				   TASK_INTERRUPTIBLE,
230 				   msecs_to_jiffies(2000));
231 }
232 
233 /* ------- Interface to HCI layer ------ */
234 /* Reset device */
235 static int hci_uart_flush(struct hci_dev *hdev)
236 {
237 	struct hci_uart *hu  = hci_get_drvdata(hdev);
238 	struct tty_struct *tty = hu->tty;
239 
240 	BT_DBG("hdev %p tty %p", hdev, tty);
241 
242 	disable_work_sync(&hu->write_work);
243 
244 	if (hu->tx_skb) {
245 		kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
246 	}
247 
248 	/* Flush any pending characters in the driver and discipline. */
249 	tty_ldisc_flush(tty);
250 	tty_driver_flush_buffer(tty);
251 
252 	percpu_down_read(&hu->proto_lock);
253 
254 	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
255 		hu->proto->flush(hu);
256 
257 	percpu_up_read(&hu->proto_lock);
258 
259 	/* Resume TX. Also reschedule in case work was queued concurrently;
260 	 * this may schedule write_work although there's nothing to do.
261 	 */
262 	enable_work(&hu->write_work);
263 	clear_bit(HCI_UART_SENDING, &hu->tx_state);
264 	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
265 		hci_uart_tx_wakeup(hu);
266 
267 	return 0;
268 }
269 
270 /* Initialize device */
271 static int hci_uart_open(struct hci_dev *hdev)
272 {
273 	BT_DBG("%s %p", hdev->name, hdev);
274 
275 	/* Undo clearing this from hci_uart_close() */
276 	hdev->flush = hci_uart_flush;
277 
278 	return 0;
279 }
280 
281 /* Close device */
282 static int hci_uart_close(struct hci_dev *hdev)
283 {
284 	BT_DBG("hdev %p", hdev);
285 
286 	hci_uart_flush(hdev);
287 	hdev->flush = NULL;
288 	return 0;
289 }
290 
291 /* Send frames from HCI layer */
292 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
293 {
294 	struct hci_uart *hu = hci_get_drvdata(hdev);
295 
296 	BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
297 	       skb->len);
298 
299 	percpu_down_read(&hu->proto_lock);
300 
301 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
302 	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
303 		percpu_up_read(&hu->proto_lock);
304 		return -EUNATCH;
305 	}
306 
307 	hu->proto->enqueue(hu, skb);
308 	percpu_up_read(&hu->proto_lock);
309 
310 	hci_uart_tx_wakeup(hu);
311 
312 	return 0;
313 }
314 
315 /* Check the underlying device or tty has flow control support */
316 bool hci_uart_has_flow_control(struct hci_uart *hu)
317 {
318 	/* serdev nodes check if the needed operations are present */
319 	if (hu->serdev)
320 		return true;
321 
322 	if (hu->tty->driver->ops->tiocmget && hu->tty->driver->ops->tiocmset)
323 		return true;
324 
325 	return false;
326 }
327 
328 /* Flow control or un-flow control the device */
329 void hci_uart_set_flow_control(struct hci_uart *hu, bool enable)
330 {
331 	struct tty_struct *tty = hu->tty;
332 	struct ktermios ktermios;
333 	int status;
334 	unsigned int set = 0;
335 	unsigned int clear = 0;
336 
337 	if (hu->serdev) {
338 		serdev_device_set_flow_control(hu->serdev, !enable);
339 		serdev_device_set_rts(hu->serdev, !enable);
340 		return;
341 	}
342 
343 	if (enable) {
344 		/* Disable hardware flow control */
345 		ktermios = tty->termios;
346 		ktermios.c_cflag &= ~CRTSCTS;
347 		tty_set_termios(tty, &ktermios);
348 		BT_DBG("Disabling hardware flow control: %s",
349 		       (tty->termios.c_cflag & CRTSCTS) ? "failed" : "success");
350 
351 		/* Clear RTS to prevent the device from sending */
352 		/* Most UARTs need OUT2 to enable interrupts */
353 		status = tty->driver->ops->tiocmget(tty);
354 		BT_DBG("Current tiocm 0x%x", status);
355 
356 		set &= ~(TIOCM_OUT2 | TIOCM_RTS);
357 		clear = ~set;
358 		set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
359 		       TIOCM_OUT2 | TIOCM_LOOP;
360 		clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
361 			 TIOCM_OUT2 | TIOCM_LOOP;
362 		status = tty->driver->ops->tiocmset(tty, set, clear);
363 		BT_DBG("Clearing RTS: %s", status ? "failed" : "success");
364 	} else {
365 		/* Set RTS to allow the device to send again */
366 		status = tty->driver->ops->tiocmget(tty);
367 		BT_DBG("Current tiocm 0x%x", status);
368 
369 		set |= (TIOCM_OUT2 | TIOCM_RTS);
370 		clear = ~set;
371 		set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
372 		       TIOCM_OUT2 | TIOCM_LOOP;
373 		clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
374 			 TIOCM_OUT2 | TIOCM_LOOP;
375 		status = tty->driver->ops->tiocmset(tty, set, clear);
376 		BT_DBG("Setting RTS: %s", status ? "failed" : "success");
377 
378 		/* Re-enable hardware flow control */
379 		ktermios = tty->termios;
380 		ktermios.c_cflag |= CRTSCTS;
381 		tty_set_termios(tty, &ktermios);
382 		BT_DBG("Enabling hardware flow control: %s",
383 		       !(tty->termios.c_cflag & CRTSCTS) ? "failed" : "success");
384 	}
385 }
386 
387 void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
388 			 unsigned int oper_speed)
389 {
390 	hu->init_speed = init_speed;
391 	hu->oper_speed = oper_speed;
392 }
393 
394 void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed)
395 {
396 	struct tty_struct *tty = hu->tty;
397 	struct ktermios ktermios;
398 
399 	ktermios = tty->termios;
400 	ktermios.c_cflag &= ~CBAUD;
401 	tty_termios_encode_baud_rate(&ktermios, speed, speed);
402 
403 	/* tty_set_termios() return not checked as it is always 0 */
404 	tty_set_termios(tty, &ktermios);
405 
406 	BT_DBG("%s: New tty speeds: %d/%d", hu->hdev->name,
407 	       tty->termios.c_ispeed, tty->termios.c_ospeed);
408 }
409 
410 static int hci_uart_setup(struct hci_dev *hdev)
411 {
412 	struct hci_uart *hu = hci_get_drvdata(hdev);
413 	struct hci_rp_read_local_version *ver;
414 	struct sk_buff *skb;
415 	unsigned int speed;
416 	int err;
417 
418 	/* Init speed if any */
419 	if (hu->init_speed)
420 		speed = hu->init_speed;
421 	else if (hu->proto->init_speed)
422 		speed = hu->proto->init_speed;
423 	else
424 		speed = 0;
425 
426 	if (speed)
427 		hci_uart_set_baudrate(hu, speed);
428 
429 	/* Operational speed if any */
430 	if (hu->oper_speed)
431 		speed = hu->oper_speed;
432 	else if (hu->proto->oper_speed)
433 		speed = hu->proto->oper_speed;
434 	else
435 		speed = 0;
436 
437 	if (hu->proto->set_baudrate && speed) {
438 		err = hu->proto->set_baudrate(hu, speed);
439 		if (!err)
440 			hci_uart_set_baudrate(hu, speed);
441 	}
442 
443 	if (hu->proto->setup)
444 		return hu->proto->setup(hu);
445 
446 	if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
447 		return 0;
448 
449 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
450 			     HCI_INIT_TIMEOUT);
451 	if (IS_ERR(skb)) {
452 		BT_ERR("%s: Reading local version information failed (%ld)",
453 		       hdev->name, PTR_ERR(skb));
454 		return 0;
455 	}
456 
457 	if (skb->len != sizeof(*ver)) {
458 		BT_ERR("%s: Event length mismatch for version information",
459 		       hdev->name);
460 		goto done;
461 	}
462 
463 	ver = (struct hci_rp_read_local_version *)skb->data;
464 
465 	switch (le16_to_cpu(ver->manufacturer)) {
466 #ifdef CONFIG_BT_HCIUART_INTEL
467 	case 2:
468 		hdev->set_bdaddr = btintel_set_bdaddr;
469 		btintel_check_bdaddr(hdev);
470 		break;
471 #endif
472 #ifdef CONFIG_BT_HCIUART_BCM
473 	case 15:
474 		hdev->set_bdaddr = btbcm_set_bdaddr;
475 		btbcm_check_bdaddr(hdev);
476 		break;
477 #endif
478 	default:
479 		break;
480 	}
481 
482 done:
483 	kfree_skb(skb);
484 	return 0;
485 }
486 
487 /* ------ LDISC part ------ */
488 /* hci_uart_tty_open
489  *
490  *     Called when line discipline changed to HCI_UART.
491  *
492  * Arguments:
493  *     tty    pointer to tty info structure
494  * Return Value:
495  *     0 if success, otherwise error code
496  */
497 static int hci_uart_tty_open(struct tty_struct *tty)
498 {
499 	struct hci_uart *hu;
500 
501 	BT_DBG("tty %p", tty);
502 
503 	if (!capable(CAP_NET_ADMIN))
504 		return -EPERM;
505 
506 	/* Error if the tty has no write op instead of leaving an exploitable
507 	 * hole
508 	 */
509 	if (tty->ops->write == NULL)
510 		return -EOPNOTSUPP;
511 
512 	hu = kzalloc_obj(*hu);
513 	if (!hu) {
514 		BT_ERR("Can't allocate control structure");
515 		return -ENFILE;
516 	}
517 	if (percpu_init_rwsem(&hu->proto_lock)) {
518 		BT_ERR("Can't allocate semaphore structure");
519 		kfree(hu);
520 		return -ENOMEM;
521 	}
522 
523 	tty->disc_data = hu;
524 	hu->tty = tty;
525 	tty->receive_room = 65536;
526 
527 	/* disable alignment support by default */
528 	hu->alignment = 1;
529 	hu->padding = 0;
530 
531 	/* Use serial port speed as oper_speed */
532 	hu->oper_speed = tty->termios.c_ospeed;
533 
534 	INIT_WORK(&hu->init_ready, hci_uart_init_work);
535 	INIT_WORK(&hu->write_work, hci_uart_write_work);
536 
537 	/* Flush any pending characters in the driver */
538 	tty_driver_flush_buffer(tty);
539 
540 	return 0;
541 }
542 
543 /* hci_uart_tty_close()
544  *
545  *    Called when the line discipline is changed to something
546  *    else, the tty is closed, or the tty detects a hangup.
547  */
548 static void hci_uart_tty_close(struct tty_struct *tty)
549 {
550 	struct hci_uart *hu = tty->disc_data;
551 	struct hci_dev *hdev;
552 	bool proto_ready;
553 
554 	BT_DBG("tty %p", tty);
555 
556 	/* Detach from the tty */
557 	tty->disc_data = NULL;
558 
559 	if (!hu)
560 		return;
561 
562 	/* Wait for init_ready to finish to prevent registration races */
563 	cancel_work_sync(&hu->init_ready);
564 
565 	proto_ready = test_bit(HCI_UART_PROTO_READY, &hu->flags);
566 	if (proto_ready) {
567 		percpu_down_write(&hu->proto_lock);
568 		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
569 		percpu_up_write(&hu->proto_lock);
570 	}
571 
572 	/*
573 	 * Unconditionally cancel write_work AFTER clearing PROTO_READY.
574 	 * This ensures that concurrent protocol timers cannot requeue
575 	 * write_work via hci_uart_tx_wakeup(), permanently preventing
576 	 * double-free races and UAFs.
577 	 */
578 	cancel_work_sync(&hu->write_work);
579 
580 	hdev = hu->hdev;
581 	if (hdev)
582 		hci_uart_close(hdev); /* proto->flush is safely skipped */
583 
584 	if (proto_ready) {
585 		if (hdev) {
586 			if (test_bit(HCI_UART_REGISTERED, &hu->flags))
587 				hci_unregister_dev(hdev);
588 		}
589 		/* Close protocol before freeing hdev (intrinsically purges queues) */
590 		hu->proto->close(hu);
591 
592 		if (hdev)
593 			hci_free_dev(hdev);
594 	}
595 	clear_bit(HCI_UART_PROTO_SET, &hu->flags);
596 
597 	percpu_free_rwsem(&hu->proto_lock);
598 
599 	kfree(hu);
600 }
601 
602 /* hci_uart_tty_wakeup()
603  *
604  *    Callback for transmit wakeup. Called when low level
605  *    device driver can accept more send data.
606  *
607  * Arguments:        tty    pointer to associated tty instance data
608  * Return Value:    None
609  */
610 static void hci_uart_tty_wakeup(struct tty_struct *tty)
611 {
612 	struct hci_uart *hu = tty->disc_data;
613 
614 	BT_DBG("");
615 
616 	if (!hu)
617 		return;
618 
619 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
620 
621 	if (tty != hu->tty)
622 		return;
623 
624 	if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
625 	    test_bit(HCI_UART_PROTO_INIT, &hu->flags))
626 		hci_uart_tx_wakeup(hu);
627 }
628 
629 /* hci_uart_tty_receive()
630  *
631  *     Called by tty low level driver when receive data is
632  *     available.
633  *
634  * Arguments:  tty          pointer to tty instance data
635  *             data         pointer to received data
636  *             flags        pointer to flags for data
637  *             count        count of received data in bytes
638  *
639  * Return Value:    None
640  */
641 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
642 				 const u8 *flags, size_t count)
643 {
644 	struct hci_uart *hu = tty->disc_data;
645 
646 	if (!hu || tty != hu->tty)
647 		return;
648 
649 	percpu_down_read(&hu->proto_lock);
650 
651 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
652 	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
653 		percpu_up_read(&hu->proto_lock);
654 		return;
655 	}
656 
657 	/* It does not need a lock here as it is already protected by a mutex in
658 	 * tty caller
659 	 */
660 	hu->proto->recv(hu, data, count);
661 
662 	if (hu->hdev)
663 		hu->hdev->stat.byte_rx += count;
664 
665 	percpu_up_read(&hu->proto_lock);
666 
667 	tty_unthrottle(tty);
668 }
669 
670 static int hci_uart_register_dev(struct hci_uart *hu)
671 {
672 	struct hci_dev *hdev;
673 	int err;
674 
675 	BT_DBG("");
676 
677 	/* Initialize and register HCI device */
678 	hdev = hci_alloc_dev();
679 	if (!hdev) {
680 		BT_ERR("Can't allocate HCI device");
681 		return -ENOMEM;
682 	}
683 
684 	hu->hdev = hdev;
685 
686 	hdev->bus = HCI_UART;
687 	hci_set_drvdata(hdev, hu);
688 
689 	/* Only when vendor specific setup callback is provided, consider
690 	 * the manufacturer information valid. This avoids filling in the
691 	 * value for Ericsson when nothing is specified.
692 	 */
693 	if (hu->proto->setup)
694 		hdev->manufacturer = hu->proto->manufacturer;
695 
696 	hdev->open  = hci_uart_open;
697 	hdev->close = hci_uart_close;
698 	hdev->flush = hci_uart_flush;
699 	hdev->send  = hci_uart_send_frame;
700 	hdev->setup = hci_uart_setup;
701 	SET_HCIDEV_DEV(hdev, hu->tty->dev);
702 
703 	if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
704 		hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
705 
706 	if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
707 		hci_set_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG);
708 
709 	if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
710 		hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
711 
712 	/* Only call open() for the protocol after hdev is fully initialized as
713 	 * open() (or a timer/workqueue it starts) may attempt to reference it.
714 	 */
715 	err = hu->proto->open(hu);
716 	if (err) {
717 		hu->hdev = NULL;
718 		hci_free_dev(hdev);
719 		return err;
720 	}
721 
722 	set_bit(HCI_UART_PROTO_INIT, &hu->flags);
723 
724 	if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
725 		return 0;
726 
727 	if (hci_register_dev(hdev) < 0) {
728 		BT_ERR("Can't register HCI device");
729 		percpu_down_write(&hu->proto_lock);
730 		clear_bit(HCI_UART_PROTO_INIT, &hu->flags);
731 		percpu_up_write(&hu->proto_lock);
732 		/* Cancel work after clearing flags */
733 		cancel_work_sync(&hu->write_work);
734 
735 		/* Close protocol before freeing hdev */
736 		hu->proto->close(hu);
737 		hu->hdev = NULL;
738 		hci_free_dev(hdev);
739 		return -ENODEV;
740 	}
741 
742 	set_bit(HCI_UART_REGISTERED, &hu->flags);
743 
744 	return 0;
745 }
746 
747 static int hci_uart_set_proto(struct hci_uart *hu, int id)
748 {
749 	const struct hci_uart_proto *p;
750 	int err;
751 
752 	p = hci_uart_get_proto(id);
753 	if (!p)
754 		return -EPROTONOSUPPORT;
755 
756 	hu->proto = p;
757 
758 	err = hci_uart_register_dev(hu);
759 	if (err) {
760 		return err;
761 	}
762 
763 	set_bit(HCI_UART_PROTO_READY, &hu->flags);
764 	clear_bit(HCI_UART_PROTO_INIT, &hu->flags);
765 
766 	return 0;
767 }
768 
769 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
770 {
771 	unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
772 				    BIT(HCI_UART_RESET_ON_INIT) |
773 				    BIT(HCI_UART_INIT_PENDING) |
774 				    BIT(HCI_UART_EXT_CONFIG) |
775 				    BIT(HCI_UART_VND_DETECT);
776 
777 	if (flags & ~valid_flags)
778 		return -EINVAL;
779 
780 	hu->hdev_flags = flags;
781 
782 	return 0;
783 }
784 
785 /* hci_uart_tty_ioctl()
786  *
787  *    Process IOCTL system call for the tty device.
788  *
789  * Arguments:
790  *
791  *    tty        pointer to tty instance data
792  *    cmd        IOCTL command code
793  *    arg        argument for IOCTL call (cmd dependent)
794  *
795  * Return Value:    Command dependent
796  */
797 static int hci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
798 			      unsigned long arg)
799 {
800 	struct hci_uart *hu = tty->disc_data;
801 	int err = 0;
802 
803 	BT_DBG("");
804 
805 	/* Verify the status of the device */
806 	if (!hu)
807 		return -EBADF;
808 
809 	switch (cmd) {
810 	case HCIUARTSETPROTO:
811 		if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
812 			err = hci_uart_set_proto(hu, arg);
813 			if (err)
814 				clear_bit(HCI_UART_PROTO_SET, &hu->flags);
815 		} else
816 			err = -EBUSY;
817 		break;
818 
819 	case HCIUARTGETPROTO:
820 		if (test_bit(HCI_UART_PROTO_SET, &hu->flags) &&
821 		    test_bit(HCI_UART_PROTO_READY, &hu->flags))
822 			err = hu->proto->id;
823 		else
824 			err = -EUNATCH;
825 		break;
826 
827 	case HCIUARTGETDEVICE:
828 		if (test_bit(HCI_UART_REGISTERED, &hu->flags))
829 			err = hu->hdev->id;
830 		else
831 			err = -EUNATCH;
832 		break;
833 
834 	case HCIUARTSETFLAGS:
835 		if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
836 			err = -EBUSY;
837 		else
838 			err = hci_uart_set_flags(hu, arg);
839 		break;
840 
841 	case HCIUARTGETFLAGS:
842 		err = hu->hdev_flags;
843 		break;
844 
845 	default:
846 		err = n_tty_ioctl_helper(tty, cmd, arg);
847 		break;
848 	}
849 
850 	return err;
851 }
852 
853 /*
854  * We don't provide read/write/poll interface for user space.
855  */
856 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
857 				 u8 *buf, size_t nr, void **cookie,
858 				 unsigned long offset)
859 {
860 	return 0;
861 }
862 
863 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
864 				  const u8 *data, size_t count)
865 {
866 	return 0;
867 }
868 
869 static struct tty_ldisc_ops hci_uart_ldisc = {
870 	.owner		= THIS_MODULE,
871 	.num		= N_HCI,
872 	.name		= "n_hci",
873 	.open		= hci_uart_tty_open,
874 	.close		= hci_uart_tty_close,
875 	.read		= hci_uart_tty_read,
876 	.write		= hci_uart_tty_write,
877 	.ioctl		= hci_uart_tty_ioctl,
878 	.compat_ioctl	= hci_uart_tty_ioctl,
879 	.receive_buf	= hci_uart_tty_receive,
880 	.write_wakeup	= hci_uart_tty_wakeup,
881 };
882 
883 static int __init hci_uart_init(void)
884 {
885 	int err;
886 
887 	BT_INFO("HCI UART driver ver %s", VERSION);
888 
889 	/* Register the tty discipline */
890 	err = tty_register_ldisc(&hci_uart_ldisc);
891 	if (err) {
892 		BT_ERR("HCI line discipline registration failed. (%d)", err);
893 		return err;
894 	}
895 
896 #ifdef CONFIG_BT_HCIUART_H4
897 	h4_init();
898 #endif
899 #ifdef CONFIG_BT_HCIUART_BCSP
900 	bcsp_init();
901 #endif
902 #ifdef CONFIG_BT_HCIUART_LL
903 	ll_init();
904 #endif
905 #ifdef CONFIG_BT_HCIUART_ATH3K
906 	ath_init();
907 #endif
908 #ifdef CONFIG_BT_HCIUART_3WIRE
909 	h5_init();
910 #endif
911 #ifdef CONFIG_BT_HCIUART_INTEL
912 	intel_init();
913 #endif
914 #ifdef CONFIG_BT_HCIUART_BCM
915 	bcm_init();
916 #endif
917 #ifdef CONFIG_BT_HCIUART_QCA
918 	qca_init();
919 #endif
920 #ifdef CONFIG_BT_HCIUART_AG6XX
921 	ag6xx_init();
922 #endif
923 #ifdef CONFIG_BT_HCIUART_MRVL
924 	mrvl_init();
925 #endif
926 #ifdef CONFIG_BT_HCIUART_AML
927 	aml_init();
928 #endif
929 	return 0;
930 }
931 
932 static void __exit hci_uart_exit(void)
933 {
934 #ifdef CONFIG_BT_HCIUART_H4
935 	h4_deinit();
936 #endif
937 #ifdef CONFIG_BT_HCIUART_BCSP
938 	bcsp_deinit();
939 #endif
940 #ifdef CONFIG_BT_HCIUART_LL
941 	ll_deinit();
942 #endif
943 #ifdef CONFIG_BT_HCIUART_ATH3K
944 	ath_deinit();
945 #endif
946 #ifdef CONFIG_BT_HCIUART_3WIRE
947 	h5_deinit();
948 #endif
949 #ifdef CONFIG_BT_HCIUART_INTEL
950 	intel_deinit();
951 #endif
952 #ifdef CONFIG_BT_HCIUART_BCM
953 	bcm_deinit();
954 #endif
955 #ifdef CONFIG_BT_HCIUART_QCA
956 	qca_deinit();
957 #endif
958 #ifdef CONFIG_BT_HCIUART_AG6XX
959 	ag6xx_deinit();
960 #endif
961 #ifdef CONFIG_BT_HCIUART_MRVL
962 	mrvl_deinit();
963 #endif
964 #ifdef CONFIG_BT_HCIUART_AML
965 	aml_deinit();
966 #endif
967 	tty_unregister_ldisc(&hci_uart_ldisc);
968 }
969 
970 module_init(hci_uart_init);
971 module_exit(hci_uart_exit);
972 
973 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
974 MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
975 MODULE_VERSION(VERSION);
976 MODULE_LICENSE("GPL");
977 MODULE_ALIAS_LDISC(N_HCI);
978