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