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