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