blob: d4550f9962b5df8169cb0d593e57d9d66481fcac [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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
44#include <net/bluetooth/bluetooth.h>
45#include <net/bluetooth/hci_core.h>
46
47#include "hci_uart.h"
48
49#define VERSION "2.2"
50
51static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
52
53int hci_uart_register_proto(struct hci_uart_proto *p)
54{
55 if (p->id >= HCI_UART_MAX_PROTO)
56 return -EINVAL;
57
58 if (hup[p->id])
59 return -EEXIST;
60
61 hup[p->id] = p;
62
63 return 0;
64}
65
66int hci_uart_unregister_proto(struct hci_uart_proto *p)
67{
68 if (p->id >= HCI_UART_MAX_PROTO)
69 return -EINVAL;
70
71 if (!hup[p->id])
72 return -EINVAL;
73
74 hup[p->id] = NULL;
75
76 return 0;
77}
78
79static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
80{
81 if (id >= HCI_UART_MAX_PROTO)
82 return NULL;
83
84 return hup[id];
85}
86
87static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
88{
89 struct hci_dev *hdev = hu->hdev;
90
91 /* Update HCI stat counters */
92 switch (pkt_type) {
93 case HCI_COMMAND_PKT:
94 hdev->stat.cmd_tx++;
95 break;
96
97 case HCI_ACLDATA_PKT:
98 hdev->stat.acl_tx++;
99 break;
100
101 case HCI_SCODATA_PKT:
102 hdev->stat.sco_tx++;
103 break;
104 }
105}
106
107static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
108{
109 struct sk_buff *skb = hu->tx_skb;
110
111 if (!skb)
112 skb = hu->proto->dequeue(hu);
113 else
114 hu->tx_skb = NULL;
115
116 return skb;
117}
118
119int hci_uart_tx_wakeup(struct hci_uart *hu)
120{
121 if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
122 set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
123 return 0;
124 }
125
126 BT_DBG("");
127
128 schedule_work(&hu->write_work);
129
130 return 0;
131}
132
133static void hci_uart_write_work(struct work_struct *work)
134{
135 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
136 struct tty_struct *tty = hu->tty;
137 struct hci_dev *hdev = hu->hdev;
138 struct sk_buff *skb;
139
140 /* REVISIT: should we cope with bad skbs or ->write() returning
141 * and error value ?
142 */
143
144restart:
145 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
146
147 while ((skb = hci_uart_dequeue(hu))) {
148 int len;
149
150 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
151 len = tty->ops->write(tty, skb->data, skb->len);
152 hdev->stat.byte_tx += len;
153
154 skb_pull(skb, len);
155 if (skb->len) {
156 hu->tx_skb = skb;
157 break;
158 }
159
160 hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
161 kfree_skb(skb);
162 }
163
164 if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
165 goto restart;
166
167 clear_bit(HCI_UART_SENDING, &hu->tx_state);
168}
169
170/* ------- Interface to HCI layer ------ */
171/* Initialize device */
172static int hci_uart_open(struct hci_dev *hdev)
173{
174 BT_DBG("%s %p", hdev->name, hdev);
175
176 /* Nothing to do for UART driver */
177
178 set_bit(HCI_RUNNING, &hdev->flags);
179
180 return 0;
181}
182
183/* Reset device */
184static int hci_uart_flush(struct hci_dev *hdev)
185{
186 struct hci_uart *hu = hci_get_drvdata(hdev);
187 struct tty_struct *tty = hu->tty;
188
189 BT_DBG("hdev %p tty %p", hdev, tty);
190
191 if (hu->tx_skb) {
192 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
193 }
194
195 /* Flush any pending characters in the driver and discipline. */
196 tty_ldisc_flush(tty);
197 tty_driver_flush_buffer(tty);
198
199 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
200 hu->proto->flush(hu);
201
202 return 0;
203}
204
205/* Close device */
206static int hci_uart_close(struct hci_dev *hdev)
207{
208 BT_DBG("hdev %p", hdev);
209
210 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
211 return 0;
212
213 hci_uart_flush(hdev);
214 hdev->flush = NULL;
215 return 0;
216}
217
218/* Send frames from HCI layer */
219static int hci_uart_send_frame(struct sk_buff *skb)
220{
221 struct hci_dev* hdev = (struct hci_dev *) skb->dev;
222 struct hci_uart *hu;
223
224 if (!hdev) {
225 BT_ERR("Frame for unknown device (hdev=NULL)");
226 return -ENODEV;
227 }
228
229 if (!test_bit(HCI_RUNNING, &hdev->flags))
230 return -EBUSY;
231
232 hu = hci_get_drvdata(hdev);
233
234 BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
235
236 hu->proto->enqueue(hu, skb);
237
238 hci_uart_tx_wakeup(hu);
239
240 return 0;
241}
242
243/* ------ LDISC part ------ */
244/* hci_uart_tty_open
245 *
246 * Called when line discipline changed to HCI_UART.
247 *
248 * Arguments:
249 * tty pointer to tty info structure
250 * Return Value:
251 * 0 if success, otherwise error code
252 */
253static int hci_uart_tty_open(struct tty_struct *tty)
254{
255 struct hci_uart *hu = (void *) tty->disc_data;
256
257 BT_DBG("tty %p", tty);
258
259 /* FIXME: This btw is bogus, nothing requires the old ldisc to clear
260 the pointer */
261 if (hu)
262 return -EEXIST;
263
264 /* Error if the tty has no write op instead of leaving an exploitable
265 hole */
266 if (tty->ops->write == NULL)
267 return -EOPNOTSUPP;
268
269 if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
270 BT_ERR("Can't allocate control structure");
271 return -ENFILE;
272 }
273
274 tty->disc_data = hu;
275 hu->tty = tty;
276 tty->receive_room = 65536;
277
278 INIT_WORK(&hu->write_work, hci_uart_write_work);
279
280 spin_lock_init(&hu->rx_lock);
281
282 /* Flush any pending characters in the driver and line discipline. */
283
284 /* FIXME: why is this needed. Note don't use ldisc_ref here as the
285 open path is before the ldisc is referencable */
286
287 if (tty->ldisc->ops->flush_buffer)
288 tty->ldisc->ops->flush_buffer(tty);
289 tty_driver_flush_buffer(tty);
290
291 return 0;
292}
293
294/* hci_uart_tty_close()
295 *
296 * Called when the line discipline is changed to something
297 * else, the tty is closed, or the tty detects a hangup.
298 */
299static void hci_uart_tty_close(struct tty_struct *tty)
300{
301 struct hci_uart *hu = (void *)tty->disc_data;
302
303 BT_DBG("tty %p", tty);
304
305 /* Detach from the tty */
306 tty->disc_data = NULL;
307
308 if (hu) {
309 struct hci_dev *hdev = hu->hdev;
310
311 if (hdev)
312 hci_uart_close(hdev);
313
314 cancel_work_sync(&hu->write_work);
315
316 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
317 if (hdev) {
318 hci_unregister_dev(hdev);
319 hci_free_dev(hdev);
320 }
321 hu->proto->close(hu);
322 }
323
324 kfree(hu);
325 }
326}
327
328/* hci_uart_tty_wakeup()
329 *
330 * Callback for transmit wakeup. Called when low level
331 * device driver can accept more send data.
332 *
333 * Arguments: tty pointer to associated tty instance data
334 * Return Value: None
335 */
336static void hci_uart_tty_wakeup(struct tty_struct *tty)
337{
338 struct hci_uart *hu = (void *)tty->disc_data;
339
340 BT_DBG("");
341
342 if (!hu)
343 return;
344
345 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
346
347 if (tty != hu->tty)
348 return;
349
350 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
351 hci_uart_tx_wakeup(hu);
352}
353
354/* hci_uart_tty_receive()
355 *
356 * Called by tty low level driver when receive data is
357 * available.
358 *
359 * Arguments: tty pointer to tty isntance data
360 * data pointer to received data
361 * flags pointer to flags for data
362 * count count of received data in bytes
363 *
364 * Return Value: None
365 */
366static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
367{
368 struct hci_uart *hu = (void *)tty->disc_data;
369
370 if (!hu || tty != hu->tty)
371 return;
372
373 if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
374 return;
375
376 spin_lock(&hu->rx_lock);
377 hu->proto->recv(hu, (void *) data, count);
378 hu->hdev->stat.byte_rx += count;
379 spin_unlock(&hu->rx_lock);
380
381 tty_unthrottle(tty);
382}
383
384static int hci_uart_register_dev(struct hci_uart *hu)
385{
386 struct hci_dev *hdev;
387
388 BT_DBG("");
389
390 /* Initialize and register HCI device */
391 hdev = hci_alloc_dev();
392 if (!hdev) {
393 BT_ERR("Can't allocate HCI device");
394 return -ENOMEM;
395 }
396
397 hu->hdev = hdev;
398
399 hdev->bus = HCI_UART;
400 hci_set_drvdata(hdev, hu);
401
402 hdev->open = hci_uart_open;
403 hdev->close = hci_uart_close;
404 hdev->flush = hci_uart_flush;
405 hdev->send = hci_uart_send_frame;
406 hdev->parent = hu->tty->dev;
407
408 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
409 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
410
411 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
412 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
413
414 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
415 hdev->dev_type = HCI_AMP;
416 else
417 hdev->dev_type = HCI_BREDR;
418
419 if (hci_register_dev(hdev) < 0) {
420 BT_ERR("Can't register HCI device");
421 hci_free_dev(hdev);
422 return -ENODEV;
423 }
424
425 return 0;
426}
427
428static int hci_uart_set_proto(struct hci_uart *hu, int id)
429{
430 struct hci_uart_proto *p;
431 int err;
432
433 p = hci_uart_get_proto(id);
434 if (!p)
435 return -EPROTONOSUPPORT;
436
437 err = p->open(hu);
438 if (err)
439 return err;
440
441 hu->proto = p;
442
443 err = hci_uart_register_dev(hu);
444 if (err) {
445 p->close(hu);
446 return err;
447 }
448
449 return 0;
450}
451
452/* hci_uart_tty_ioctl()
453 *
454 * Process IOCTL system call for the tty device.
455 *
456 * Arguments:
457 *
458 * tty pointer to tty instance data
459 * file pointer to open file object for device
460 * cmd IOCTL command code
461 * arg argument for IOCTL call (cmd dependent)
462 *
463 * Return Value: Command dependent
464 */
465static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
466 unsigned int cmd, unsigned long arg)
467{
468 struct hci_uart *hu = (void *)tty->disc_data;
469 int err = 0;
470
471 BT_DBG("");
472
473 /* Verify the status of the device */
474 if (!hu)
475 return -EBADF;
476
477 switch (cmd) {
478 case HCIUARTSETPROTO:
479 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
480 err = hci_uart_set_proto(hu, arg);
481 if (err) {
482 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
483 return err;
484 }
485 } else
486 return -EBUSY;
487 break;
488
489 case HCIUARTGETPROTO:
490 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
491 return hu->proto->id;
492 return -EUNATCH;
493
494 case HCIUARTGETDEVICE:
495 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
496 return hu->hdev->id;
497 return -EUNATCH;
498
499 case HCIUARTSETFLAGS:
500 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
501 return -EBUSY;
502 hu->hdev_flags = arg;
503 break;
504
505 case HCIUARTGETFLAGS:
506 return hu->hdev_flags;
507
508 default:
509 err = n_tty_ioctl_helper(tty, file, cmd, arg);
510 break;
511 };
512
513 return err;
514}
515
516/*
517 * We don't provide read/write/poll interface for user space.
518 */
519static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
520 unsigned char __user *buf, size_t nr)
521{
522 return 0;
523}
524
525static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
526 const unsigned char *data, size_t count)
527{
528 return 0;
529}
530
531static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
532 struct file *filp, poll_table *wait)
533{
534 return 0;
535}
536
537static int __init hci_uart_init(void)
538{
539 static struct tty_ldisc_ops hci_uart_ldisc;
540 int err;
541
542 BT_INFO("HCI UART driver ver %s", VERSION);
543
544 /* Register the tty discipline */
545
546 memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
547 hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
548 hci_uart_ldisc.name = "n_hci";
549 hci_uart_ldisc.open = hci_uart_tty_open;
550 hci_uart_ldisc.close = hci_uart_tty_close;
551 hci_uart_ldisc.read = hci_uart_tty_read;
552 hci_uart_ldisc.write = hci_uart_tty_write;
553 hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
554 hci_uart_ldisc.poll = hci_uart_tty_poll;
555 hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
556 hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
557 hci_uart_ldisc.owner = THIS_MODULE;
558
559 if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
560 BT_ERR("HCI line discipline registration failed. (%d)", err);
561 return err;
562 }
563
564#ifdef CONFIG_BT_HCIUART_H4
565 h4_init();
566#endif
567#ifdef CONFIG_BT_HCIUART_BCSP
568 bcsp_init();
569#endif
570#ifdef CONFIG_BT_HCIUART_LL
571 ll_init();
572#endif
573#ifdef CONFIG_BT_HCIUART_ATH3K
574 ath_init();
575#endif
576
577 return 0;
578}
579
580static void __exit hci_uart_exit(void)
581{
582 int err;
583
584#ifdef CONFIG_BT_HCIUART_H4
585 h4_deinit();
586#endif
587#ifdef CONFIG_BT_HCIUART_BCSP
588 bcsp_deinit();
589#endif
590#ifdef CONFIG_BT_HCIUART_LL
591 ll_deinit();
592#endif
593#ifdef CONFIG_BT_HCIUART_ATH3K
594 ath_deinit();
595#endif
596
597 /* Release tty registration of line discipline */
598 if ((err = tty_unregister_ldisc(N_HCI)))
599 BT_ERR("Can't unregister HCI line discipline (%d)", err);
600}
601
602module_init(hci_uart_init);
603module_exit(hci_uart_exit);
604
605MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
606MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
607MODULE_VERSION(VERSION);
608MODULE_LICENSE("GPL");
609MODULE_ALIAS_LDISC(N_HCI);