summaryrefslogtreecommitdiff
path: root/sys/dev/bluetooth/btuart.c
blob: 91eaa7a38b7649bbc525b6684241f4049d7c0771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*	$NetBSD: btuart.c,v 1.31 2022/10/26 23:44:03 riastradh Exp $	*/

/*-
 * Copyright (c) 2006, 2007 KIYOHARA Takashi
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: btuart.c,v 1.31 2022/10/26 23:44:03 riastradh Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/syslimits.h>
#include <sys/systm.h>
#include <sys/tty.h>

#include <sys/bus.h>
#include <sys/intr.h>

#include <netbt/bluetooth.h>
#include <netbt/hci.h>

#include "ioconf.h"

struct btuart_softc {
	device_t	sc_dev;
	struct tty *	sc_tp;		/* tty pointer */

	bool		sc_enabled;	/* device is enabled */
	struct hci_unit *sc_unit;	/* Bluetooth HCI handle */
	struct bt_stats	sc_stats;

	int		sc_state;	/* receive state */
	int		sc_want;	/* how much we want */
	struct mbuf *	sc_rxp;		/* incoming packet */

	bool		sc_xmit;	/* transmit is active */
	struct mbuf *	sc_txp;		/* outgoing packet */

	/* transmit queues */
	MBUFQ_HEAD()	sc_cmdq;
	MBUFQ_HEAD()	sc_aclq;
	MBUFQ_HEAD()	sc_scoq;
};

/* sc_state */
#define BTUART_RECV_PKT_TYPE	0	/* packet type */
#define BTUART_RECV_ACL_HDR	1	/* acl header */
#define BTUART_RECV_SCO_HDR	2	/* sco header */
#define BTUART_RECV_EVENT_HDR	3	/* event header */
#define BTUART_RECV_ACL_DATA	4	/* acl packet data */
#define BTUART_RECV_SCO_DATA	5	/* sco packet data */
#define BTUART_RECV_EVENT_DATA	6	/* event packet data */

static int btuart_match(device_t, cfdata_t, void *);
static void btuart_attach(device_t, device_t, void *);
static int btuart_detach(device_t, int);

static int btuartopen(dev_t, struct tty *);
static int btuartclose(struct tty *, int);
static int btuartioctl(struct tty *, u_long, void *, int, struct lwp *);
static int btuartinput(int, struct tty *);
static int btuartstart(struct tty *);

static int btuart_enable(device_t);
static void btuart_disable(device_t);
static void btuart_output_cmd(device_t, struct mbuf *);
static void btuart_output_acl(device_t, struct mbuf *);
static void btuart_output_sco(device_t, struct mbuf *);
static void btuart_stats(device_t, struct bt_stats *, int);

/*
 * It doesn't need to be exported, as only btuartattach() uses it,
 * but there's no "official" way to make it static.
 */
CFATTACH_DECL_NEW(btuart, sizeof(struct btuart_softc),
    btuart_match, btuart_attach, btuart_detach, NULL);

static struct linesw btuart_disc = {
	.l_name =	"btuart",
	.l_open =	btuartopen,
	.l_close =	btuartclose,
	.l_read =	ttyerrio,
	.l_write =	ttyerrio,
	.l_ioctl =	btuartioctl,
	.l_rint =	btuartinput,
	.l_start =	btuartstart,
	.l_modem =	ttymodem,
	.l_poll =	ttyerrpoll,
};

static const struct hci_if btuart_hci = {
	.enable =	btuart_enable,
	.disable =	btuart_disable,
	.output_cmd =	btuart_output_cmd,
	.output_acl =	btuart_output_acl,
	.output_sco =	btuart_output_sco,
	.get_stats =	btuart_stats,
	.ipl =		IPL_TTY,
};

/*****************************************************************************
 *
 *	autoconf(9) functions
 */

/*
 * pseudo-device attach routine.
 */
void
btuartattach(int num __unused)
{
	int error;

	error = ttyldisc_attach(&btuart_disc);
	if (error) {
		aprint_error("%s: unable to register line discipline, "
		    "error = %d\n", btuart_cd.cd_name, error);

		return;
	}

	error = config_cfattach_attach(btuart_cd.cd_name, &btuart_ca);
	if (error) {
		aprint_error("%s: unable to register cfattach, error = %d\n",
		    btuart_cd.cd_name, error);

		config_cfdriver_detach(&btuart_cd);
		(void) ttyldisc_detach(&btuart_disc);
	}
}

/*
 * Autoconf match routine.
 */
static int
btuart_match(device_t self __unused, cfdata_t cfdata __unused,
	     void *arg __unused)
{

	/* pseudo-device; always present */
	return 1;
}

/*
 * Autoconf attach routine.
 * Called by config_attach_pseudo(9) when we open the line discipline.
 */
static void
btuart_attach(device_t parent __unused, device_t self, void *aux __unused)
{
	struct btuart_softc *sc = device_private(self);

	sc->sc_dev = self;

	MBUFQ_INIT(&sc->sc_cmdq);
	MBUFQ_INIT(&sc->sc_aclq);
	MBUFQ_INIT(&sc->sc_scoq);

	/* Attach Bluetooth unit */
	sc->sc_unit = hci_attach_pcb(&btuart_hci, self, 0);
	if (sc->sc_unit == NULL)
		aprint_error_dev(self, "HCI attach failed\n");
}

/*
 * Autoconf detach routine.
 * Called when we close the line discipline.
 */
static int
btuart_detach(device_t self, int flags __unused)
{
	struct btuart_softc *sc = device_private(self);

	btuart_disable(self);

	if (sc->sc_unit) {
		hci_detach_pcb(sc->sc_unit);
		sc->sc_unit = NULL;
	}

	return 0;
}

/*****************************************************************************
 *
 *	Line discipline functions.
 */

static int
btuartopen(dev_t devno __unused, struct tty *tp)
{
	struct btuart_softc *sc;
	device_t dev;
	cfdata_t cfdata;
	struct lwp *l = curlwp;		/* XXX */
	int error, unit, s;

	error = kauth_authorize_device(l->l_cred, KAUTH_DEVICE_BLUETOOTH_BTUART,
	    KAUTH_ARG(KAUTH_REQ_DEVICE_BLUETOOTH_BTUART_ADD), NULL, NULL, NULL);
	if (error)
		return (error);

	s = spltty();

	if (tp->t_linesw == &btuart_disc) {
		sc = tp->t_sc;
		if (sc != NULL) {
			splx(s);
			return EBUSY;
		}
	}

	cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
	for (unit = 0; unit < btuart_cd.cd_ndevs; unit++)
		if (device_lookup(&btuart_cd, unit) == NULL)
			break;

	cfdata->cf_name = btuart_cd.cd_name;
	cfdata->cf_atname = btuart_cd.cd_name;
	cfdata->cf_unit = unit;
	cfdata->cf_fstate = FSTATE_STAR;

	dev = config_attach_pseudo(cfdata);
	if (dev == NULL) {
		free(cfdata, M_DEVBUF);
		splx(s);
		return EIO;
	}
	sc = device_private(dev);

	aprint_normal_dev(dev, "major %llu minor %llu\n",
	    (unsigned long long)major(tp->t_dev),
	    (unsigned long long)minor(tp->t_dev));

	sc->sc_tp = tp;
	tp->t_sc = sc;

	ttylock(tp);
	ttyflush(tp, FREAD | FWRITE);
	ttyunlock(tp);

	splx(s);

	return 0;
}

static int
btuartclose(struct tty *tp, int flag __unused)
{
	struct btuart_softc *sc = tp->t_sc;
	cfdata_t cfdata;
	int s;

	s = spltty();

	ttylock(tp);
	ttyflush(tp, FREAD | FWRITE);
	ttyunlock(tp);	/* XXX */

	ttyldisc_release(tp->t_linesw);
	tp->t_linesw = ttyldisc_default();

	if (sc != NULL) {
		tp->t_sc = NULL;
		if (sc->sc_tp == tp) {
			cfdata = device_cfdata(sc->sc_dev);
			config_detach(sc->sc_dev, 0);
			free(cfdata, M_DEVBUF);
		}
	}

	splx(s);

	return 0;
}

static int
btuartioctl(struct tty *tp, u_long cmd, void *data __unused,
    int flag __unused, struct lwp *l __unused)
{
	struct btuart_softc *sc = tp->t_sc;
	int error;

	/*
	 * XXX
	 * This function can be called without KERNEL_LOCK when caller's
	 * struct cdevsw is set D_MPSAFE. Is KERNEL_LOCK required?
	 */

	if (sc == NULL || tp != sc->sc_tp)
		return EPASSTHROUGH;

	switch(cmd) {
	default:
		error = EPASSTHROUGH;
		break;
	}

	return error;
}

static int
btuartinput(int c, struct tty *tp)
{
	struct btuart_softc *sc = tp->t_sc;
	struct mbuf *m = sc->sc_rxp;
	int space = 0;

	if (!sc->sc_enabled)
		return 0;

	c &= TTY_CHARMASK;

	/* If we already started a packet, find the trailing end of it. */
	if (m) {
		while (m->m_next)
			m = m->m_next;

		space = M_TRAILINGSPACE(m);
	}

	if (space == 0) {
		if (m == NULL) {
			/* new packet */
			MGETHDR(m, M_DONTWAIT, MT_DATA);
			if (m == NULL) {
				aprint_error_dev(sc->sc_dev, "out of memory\n");
				sc->sc_stats.err_rx++;
				return 0;	/* (lost sync) */
			}

			sc->sc_rxp = m;
			m->m_pkthdr.len = m->m_len = 0;
			space = MHLEN;

			sc->sc_state = BTUART_RECV_PKT_TYPE;
			sc->sc_want = 1;
		} else {
			/* extend mbuf */
			MGET(m->m_next, M_DONTWAIT, MT_DATA);
			if (m->m_next == NULL) {
				aprint_error_dev(sc->sc_dev, "out of memory\n");
				sc->sc_stats.err_rx++;
				return 0;	/* (lost sync) */
			}

			m = m->m_next;
			m->m_len = 0;
			space = MLEN;

			if (sc->sc_want > MINCLSIZE) {
				MCLGET(m, M_DONTWAIT);
				if (m->m_flags & M_EXT)
					space = MCLBYTES;
			}
		}
	}

	mtod(m, uint8_t *)[m->m_len++] = c;
	sc->sc_rxp->m_pkthdr.len++;
	sc->sc_stats.byte_rx++;

	sc->sc_want--;
	if (sc->sc_want > 0)
		return 0;	/* want more */

	switch (sc->sc_state) {
	case BTUART_RECV_PKT_TYPE:	/* Got packet type */

		switch (c) {
		case HCI_ACL_DATA_PKT:
			sc->sc_state = BTUART_RECV_ACL_HDR;
			sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
			break;

		case HCI_SCO_DATA_PKT:
			sc->sc_state = BTUART_RECV_SCO_HDR;
			sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
			break;

		case HCI_EVENT_PKT:
			sc->sc_state = BTUART_RECV_EVENT_HDR;
			sc->sc_want = sizeof(hci_event_hdr_t) - 1;
			break;

		default:
			aprint_error_dev(sc->sc_dev,
			    "Unknown packet type=%#x!\n", c);
			sc->sc_stats.err_rx++;
			m_freem(sc->sc_rxp);
			sc->sc_rxp = NULL;
			return 0;	/* (lost sync) */
		}

		break;

	/*
	 * we assume (correctly of course :) that the packet headers all fit
	 * into a single pkthdr mbuf
	 */
	case BTUART_RECV_ACL_HDR:	/* Got ACL Header */
		sc->sc_state = BTUART_RECV_ACL_DATA;
		sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
		sc->sc_want = le16toh(sc->sc_want);
		break;

	case BTUART_RECV_SCO_HDR:	/* Got SCO Header */
		sc->sc_state = BTUART_RECV_SCO_DATA;
		sc->sc_want =  mtod(m, hci_scodata_hdr_t *)->length;
		break;

	case BTUART_RECV_EVENT_HDR:	/* Got Event Header */
		sc->sc_state = BTUART_RECV_EVENT_DATA;
		sc->sc_want =  mtod(m, hci_event_hdr_t *)->length;
		break;

	case BTUART_RECV_ACL_DATA:	/* ACL Packet Complete */
		if (!hci_input_acl(sc->sc_unit, sc->sc_rxp))
			sc->sc_stats.err_rx++;

		sc->sc_stats.acl_rx++;
		sc->sc_rxp = m = NULL;
		break;

	case BTUART_RECV_SCO_DATA:	/* SCO Packet Complete */
		if (!hci_input_sco(sc->sc_unit, sc->sc_rxp))
			sc->sc_stats.err_rx++;

		sc->sc_stats.sco_rx++;
		sc->sc_rxp = m = NULL;
		break;

	case BTUART_RECV_EVENT_DATA:	/* Event Packet Complete */
		if (!hci_input_event(sc->sc_unit, sc->sc_rxp))
			sc->sc_stats.err_rx++;

		sc->sc_stats.evt_rx++;
		sc->sc_rxp = m = NULL;
		break;

	default:
		panic("%s: invalid state %d!\n",
		    device_xname(sc->sc_dev), sc->sc_state);
	}

	return 0;
}

static int
btuartstart(struct tty *tp)
{
	struct btuart_softc *sc = tp->t_sc;
	struct mbuf *m;
	int count, rlen;
	uint8_t *rptr;

	if (!sc->sc_enabled)
		return 0;

	m = sc->sc_txp;
	if (m == NULL) {
		if (MBUFQ_FIRST(&sc->sc_cmdq)) {
			MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
			sc->sc_stats.cmd_tx++;
		} else if (MBUFQ_FIRST(&sc->sc_scoq)) {
			MBUFQ_DEQUEUE(&sc->sc_scoq, m);
			sc->sc_stats.sco_tx++;
		} else if (MBUFQ_FIRST(&sc->sc_aclq)) {
			MBUFQ_DEQUEUE(&sc->sc_aclq, m);
			sc->sc_stats.acl_tx++;
		} else {
			sc->sc_xmit = false;
			return 0; /* no more to send */
		}

		sc->sc_txp = m;
		sc->sc_xmit = true;
	}

	count = 0;
	rlen = 0;
	rptr = mtod(m, uint8_t *);

	for(;;) {
		if (rlen >= m->m_len) {
			m = m->m_next;
			if (m == NULL) {
				m = sc->sc_txp;
				sc->sc_txp = NULL;

				if (M_GETCTX(m, void *) == NULL)
					m_freem(m);
				else if (!hci_complete_sco(sc->sc_unit, m))
					sc->sc_stats.err_tx++;

				break;
			}

			rlen = 0;
			rptr = mtod(m, uint8_t *);
			continue;
		}

		if (putc(*rptr++, &tp->t_outq) < 0) {
			m_adj(m, rlen);
			break;
		}
		rlen++;
		count++;
	}

	sc->sc_stats.byte_tx += count;

	if (tp->t_outq.c_cc != 0 && tp->t_oproc != NULL)
		(*tp->t_oproc)(tp);

	return 0;
}

/*****************************************************************************
 *
 *	bluetooth(9) functions
 */

static int
btuart_enable(device_t self)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	if (sc->sc_enabled)
		return 0;

	s = spltty();

	sc->sc_enabled = true;
	sc->sc_xmit = false;

	splx(s);

	return 0;
}

static void
btuart_disable(device_t self)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	if (!sc->sc_enabled)
		return;

	s = spltty();

	if (sc->sc_rxp) {
		m_freem(sc->sc_rxp);
		sc->sc_rxp = NULL;
	}

	if (sc->sc_txp) {
		m_freem(sc->sc_txp);
		sc->sc_txp = NULL;
	}

	MBUFQ_DRAIN(&sc->sc_cmdq);
	MBUFQ_DRAIN(&sc->sc_aclq);
	MBUFQ_DRAIN(&sc->sc_scoq);

	sc->sc_enabled = false;

	splx(s);
}

static void
btuart_output_cmd(device_t self, struct mbuf *m)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	KASSERT(sc->sc_enabled);

	M_SETCTX(m, NULL);

	s = spltty();
	MBUFQ_ENQUEUE(&sc->sc_cmdq, m);
	if (!sc->sc_xmit)
		btuartstart(sc->sc_tp);

	splx(s);
}

static void
btuart_output_acl(device_t self, struct mbuf *m)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	KASSERT(sc->sc_enabled);

	M_SETCTX(m, NULL);

	s = spltty();
	MBUFQ_ENQUEUE(&sc->sc_aclq, m);
	if (!sc->sc_xmit)
		btuartstart(sc->sc_tp);

	splx(s);
}

static void
btuart_output_sco(device_t self, struct mbuf *m)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	KASSERT(sc->sc_enabled);

	s = spltty();
	MBUFQ_ENQUEUE(&sc->sc_scoq, m);
	if (!sc->sc_xmit)
		btuartstart(sc->sc_tp);

	splx(s);
}

static void
btuart_stats(device_t self, struct bt_stats *dest, int flush)
{
	struct btuart_softc *sc = device_private(self);
	int s;

	s = spltty();

	memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));

	if (flush)
		memset(&sc->sc_stats, 0, sizeof(struct bt_stats));

	splx(s);
}