summaryrefslogtreecommitdiff
path: root/sys/dev/bluetooth/bthci.c
blob: 0cbed61aaf7f393b72eea4a557df1af659f325ff (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
/*	$NetBSD: bthci.c,v 1.15 2004/01/04 05:39:35 dsainty Exp $	*/

/*
 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) and
 * David Sainty (David.Sainty@dtsp.co.nz).
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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: bthci.c,v 1.15 2004/01/04 05:39:35 dsainty Exp $");

#include "bthcidrv.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/malloc.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/vnode.h>

#include <dev/bluetooth/bluetooth.h>
#include <dev/bluetooth/bthci_util.h>
#include <dev/bluetooth/bthcivar.h>

#ifdef BTHCI_DEBUG
#define DPRINTF(x)	if (bthcidebug) printf x
#define DPRINTFN(n,x)	if (bthcidebug>(n)) printf x
#define Static
int bthcidebug = 99;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#define Static static
#endif

struct bthci_softc {
	struct	device			sc_dev;
	struct btframe_methods const	*sc_methods;
	void				*sc_handle;

	u_int8_t			*sc_rd_buf;
	size_t				sc_rd_len;
	struct selinfo			sc_rd_sel;

	int				sc_refcnt;
	int				sc_outputready;
	char				sc_open;
	char				sc_dying;
};

int bthci_match(struct device *parent, struct cfdata *match, void *aux);
void bthci_attach(struct device *parent, struct device *self, void *aux);
int bthci_activate(struct device *self, enum devact act);
int bthci_detach(struct device *self, int flags);

#if NBTHCIDRV == 0
/* In case we just have tty attachment. */
struct cfdriver bthci_cd = {
	NULL, "bthci", DV_DULL
};
#endif

CFATTACH_DECL(bthci, sizeof(struct bthci_softc),
    bthci_match, bthci_attach, bthci_detach, bthci_activate);

extern struct cfdriver bthci_cd;

dev_type_open(bthciopen);
dev_type_close(bthciclose);
dev_type_poll(bthcipoll);
dev_type_kqfilter(bthcikqfilter);

static int bthciread(dev_t dev, struct uio *uio, int flag);
static int bthciwrite(dev_t dev, struct uio *uio, int flag);

static void bthci_recveventdata(void *h, u_int8_t *data, size_t len);
static void bthci_recvacldata(void *h, u_int8_t *data, size_t len);
static void bthci_recvscodata(void *h, u_int8_t *data, size_t len);

const struct cdevsw bthci_cdevsw = {
	bthciopen, bthciclose, bthciread, bthciwrite, noioctl,
	nostop, notty, bthcipoll, nommap, bthcikqfilter,
};

#define BTHCIUNIT(dev) (minor(dev))

struct btframe_callback_methods const bthci_callbacks = {
	bthci_recveventdata, bthci_recvacldata, bthci_recvscodata
};

int
bthci_match(struct device *parent, struct cfdata *match, void *aux)
{
	/*struct bt_attach_args *bt = aux;*/

	return (1);
}

void
bthci_attach(struct device *parent, struct device *self, void *aux)
{
	struct bthci_softc *sc = (struct bthci_softc *)self;
	struct bt_attach_args *bt = aux;

	sc->sc_methods = bt->bt_methods;
	sc->sc_handle = bt->bt_handle;

	*bt->bt_cb = &bthci_callbacks;

#ifdef DIAGNOSTIC
	if (sc->sc_methods->bt_open == NULL ||
	    sc->sc_methods->bt_close == NULL ||

	    sc->sc_methods->bt_control.bt_alloc == NULL ||
	    sc->sc_methods->bt_control.bt_send == NULL ||

	    sc->sc_methods->bt_acldata.bt_alloc == NULL ||
	    sc->sc_methods->bt_acldata.bt_send == NULL ||

	    sc->sc_methods->bt_scodata.bt_alloc == NULL ||
	    sc->sc_methods->bt_scodata.bt_send == NULL)
		panic("%s: missing methods", sc->sc_dev.dv_xname);
#endif

	printf("\n");
}

int
bthci_activate(struct device *self, enum devact act)
{
	/*struct bthci_softc *sc = (struct bthci_softc *)self;*/

	switch (act) {
	case DVACT_ACTIVATE:
		return (EOPNOTSUPP);
		break;

	case DVACT_DEACTIVATE:
		break;
	}
	return (0);
}

static void
bthci_abortdealloc(struct bthci_softc *sc)
{
	DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));

	if (sc->sc_rd_buf != NULL) {
		free(sc->sc_rd_buf, M_TEMP);
		sc->sc_rd_buf = NULL;
	}
}

int
bthci_detach(struct device *self, int flags)
{
	struct bthci_softc *sc = (struct bthci_softc *)self;
	int maj, mn;

	sc->sc_dying = 1;
	wakeup(&sc->sc_outputready);

	DPRINTFN(1, ("%s: waiting for refcount (%d)\n",
		     __func__, sc->sc_refcnt));

	if (--sc->sc_refcnt >= 0)
		tsleep(&sc->sc_refcnt, PZERO, "bthcdt", 0);

	DPRINTFN(1, ("%s: refcount complete\n", __func__));

	bthci_abortdealloc(sc);

	/* locate the major number */
	maj = cdevsw_lookup_major(&bthci_cdevsw);

	/* Nuke the vnodes for any open instances (calls close). */
	mn = self->dv_unit;
	vdevgone(maj, mn, mn, VCHR);

	DPRINTFN(1, ("%s: driver detached\n", __func__));

	return (0);
}

int
bthciopen(dev_t dev, int flag, int mode, struct proc *p)
{
	struct bthci_softc *sc;
	int error;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);
	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
		return (EIO);
	if (sc->sc_open)
		return (EBUSY);

	sc->sc_rd_buf = malloc(BTHCI_ACL_DATA_MAX_LEN + 1, M_TEMP, M_NOWAIT);
	if (sc->sc_rd_buf == NULL)
		return (ENOMEM);

	sc->sc_refcnt++;

	if (sc->sc_methods->bt_open != NULL) {
		error = sc->sc_methods->bt_open(sc->sc_handle, flag, mode, p);
		if (error)
			goto bad;
	}

	if (--sc->sc_refcnt < 0)
		wakeup(&sc->sc_refcnt);

	sc->sc_open = 1;
	return (0);

 bad:
	free(sc->sc_rd_buf, M_TEMP);
	sc->sc_rd_buf = NULL;

	return error;
}

int
bthciclose(dev_t dev, int flag, int mode, struct proc *p)
{
	struct bthci_softc *sc;
	int error;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);

	if (!sc->sc_open)
		return (0);

	sc->sc_refcnt++;

	sc->sc_open = 0;

	if (sc->sc_methods->bt_close != NULL)
		error = sc->sc_methods->bt_close(sc->sc_handle, flag, mode, p);
	else
		error = 0;

	bthci_abortdealloc(sc);

	if (--sc->sc_refcnt < 0)
		wakeup(&sc->sc_refcnt);

	return (error);
}

static int
bthciread(dev_t dev, struct uio *uio, int flag)
{
	struct bthci_softc *sc;
	int error;
	int s;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);

	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
	    !sc->sc_open || sc->sc_dying)
		return (EIO);

	DPRINTFN(1, ("%s: length=%lu\n", __func__,
		     (unsigned long)uio->uio_resid));

	sc->sc_refcnt++;

	s = sc->sc_methods->bt_splraise();
	while (!sc->sc_outputready) {
		DPRINTFN(5,("%s: calling tsleep()\n", __func__));
		error = tsleep(&sc->sc_outputready, PZERO | PCATCH,
			       "bthcrd", 0);
		if (sc->sc_dying)
			error = EIO;
		if (error) {
			splx(s);
			DPRINTFN(0, ("%s: tsleep() = %d\n",
				     __func__, error));
			goto ret;
		}
	}
	splx(s);

	if (uio->uio_resid < sc->sc_rd_len) {
#ifdef DIAGNOSTIC
		printf("bthciread: short read %ld < %ld\n",
		    (long)uio->uio_resid, (long)sc->sc_rd_len);
#endif
		error = EINVAL;
		goto ret;
	}

	error = uiomove(sc->sc_rd_buf, sc->sc_rd_len, uio);

	if (!error) {
		sc->sc_outputready = 0;
		sc->sc_rd_len = 0;
	}

 ret:
	if (--sc->sc_refcnt < 0)
		wakeup(&sc->sc_refcnt);

	return error;
}

static int
bthcichanwrite(struct bthci_softc *sc,
	       struct btframe_channel const *btchannel, struct uio *uio)
{
	struct btframe_buffer *btframebuf;
	u_int8_t *btbuf;
	int error;
	size_t uiolen;

	sc->sc_refcnt++;

	uiolen = uio->uio_resid;

	btbuf = btchannel->bt_alloc(sc->sc_handle, uiolen, &btframebuf);
	if (btbuf == NULL) {
		error = ENOMEM;
		goto ret;
	}

	error = uiomove(btbuf, uiolen, uio);
	if (error)
		goto ret;

	error = btchannel->bt_send(sc->sc_handle, btframebuf, uiolen);

 ret:
	if (--sc->sc_refcnt < 0)
		wakeup(&sc->sc_refcnt);

	return error;
}

static int
bthciwrite(dev_t dev, struct uio *uio, int flag)
{
	struct bthci_softc *sc;
	struct btframe_channel const *btchannel;
	int error;
	size_t uiolen;
	u_int8_t bthcitype;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);

	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
		return (EIO);

	uiolen = uio->uio_resid;

	DPRINTFN(1, ("%s: length=%lu\n", __func__, (unsigned long)uiolen));

	if (uiolen > BTHCI_ACL_DATA_MAX_LEN + 1) {
#ifdef DIAGNOSTIC
		printf("bthciread: long write %ld\n", (long)uio->uio_resid);
#endif
		return (EINVAL);
	}

	if (uiolen <= 1) {
#ifdef DIAGNOSTIC
		printf("bthciread: short write %ld\n", (long)uio->uio_resid);
#endif
		return (EINVAL);
	}

	error = uiomove(&bthcitype, 1, uio);
	if (error)
		return (error);

	if (bthcitype == BTHCI_PKTID_COMMAND) {
		/* Command */
		btchannel = &sc->sc_methods->bt_control;
	} else if (bthcitype == BTHCI_PKTID_ACL_DATA) {
		/* ACL data */
		btchannel = &sc->sc_methods->bt_acldata;
	} else if (bthcitype == BTHCI_PKTID_SCO_DATA) {
		/* SCO data */
		btchannel = &sc->sc_methods->bt_scodata;
	} else {
		/* Bad packet type */
		return (EINVAL);
	}

	return (bthcichanwrite(sc, btchannel, uio));
}

#if 0
int
bthciioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
{
	struct bthci_softc *sc;
	void *vaddr = addr;
	int error;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);
	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
		return (EIO);

	switch (cmd) {
	case FIONBIO:
		/* All handled in the upper FS layer. */
		error = 0;
		break;

	default:
		error = EINVAL;
		break;
	}
	return (error);
}
#endif

int
bthcipoll(dev_t dev, int events, struct proc *p)
{
	struct bthci_softc *sc;
	int revents;
	int s;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);
	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
		return (EIO);

	revents = events & (POLLOUT | POLLWRNORM);

	if (events & (POLLIN | POLLRDNORM)) {
		s = sc->sc_methods->bt_splraise();
		if (sc->sc_outputready) {
			DPRINTFN(2,("%s: have data\n", __func__));
			revents |= events & (POLLIN | POLLRDNORM);
		} else {
			DPRINTFN(2,("%s: recording read select\n",
				    __func__));
			selrecord(p, &sc->sc_rd_sel);
		}
		splx(s);
	}

	return revents;
}

static void
filt_bthcirdetach(struct knote *kn)
{
	struct bthci_softc *sc = kn->kn_hook;
	int s;

	s = sc->sc_methods->bt_splraise();
	SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
	splx(s);
}

/* ARGSUSED */
static int
filt_bthciread(struct knote *kn, long hint)
{
	struct bthci_softc *sc = kn->kn_hook;

	kn->kn_data = sc->sc_rd_len;
	return (kn->kn_data > 0);
}

static const struct filterops bthciread_filtops =
	{ 1, NULL, filt_bthcirdetach, filt_bthciread };

int
bthcikqfilter(dev_t dev, struct knote *kn)
{
	struct bthci_softc *sc;
	struct klist *klist;
	int s;

	sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
	if (sc == NULL)
		return (ENXIO);
	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
		return (EIO);

	switch (kn->kn_filter) {
	case EVFILT_READ:
		klist = &sc->sc_rd_sel.sel_klist;
		kn->kn_fop = &bthciread_filtops;
		break;
	default:
		return (1);
	}

	kn->kn_hook = sc;

	s = sc->sc_methods->bt_splraise();
	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
	splx(s);

	return (0);
}

static void
bthci_recveventdata(void *h, u_int8_t *data, size_t len)
{
	struct bthci_softc *sc = h;
	size_t pktlength;
	unsigned int ecode;

	if (len < BTHCI_EVENT_MIN_LEN || len > BTHCI_EVENT_MAX_LEN) {
		DPRINTFN(1,("%s: invalid sized event, size=%u\n",
			    __func__, (unsigned int)len));
		return;
	}

	ecode = data[0];
	DPRINTFN(1,("%s: received event: %02x (%s)\n", __func__,
		    ecode, bthci_eventstr(ecode)));

	pktlength = data[BTHCI_EVENT_LEN_OFFT] + BTHCI_EVENT_LEN_OFFT +
		BTHCI_EVENT_LEN_LENGTH;
	if (pktlength != len) {
		DPRINTFN(1,("%s: event length didn't match, "
			    "pktlen=%u size=%u\n",
			    __func__, (unsigned int)pktlength,
			    (unsigned int)len));
		return;
	}

	if (!sc->sc_open)
		return;

	if (sc->sc_rd_len != 0) {
		DPRINTFN(1,("%s: dropping an event, size=%u\n",
			    __func__, (unsigned int)len));
		return;
	}

	sc->sc_rd_buf[0] = BTHCI_PKTID_EVENT;
	memcpy(&sc->sc_rd_buf[1], data, len);
	sc->sc_rd_len = len + 1;
	sc->sc_outputready = 1;

	wakeup(&sc->sc_outputready);
	selnotify(&sc->sc_rd_sel, 0);
}

static void
bthci_recvacldata(void *h, u_int8_t *data, size_t len)
{
	struct bthci_softc *sc = h;
	size_t pktlength;

	if (!sc->sc_open)
		return;

	if (len < BTHCI_ACL_DATA_MIN_LEN || len > BTHCI_ACL_DATA_MAX_LEN) {
		DPRINTFN(1,("%s: invalid acl packet, size=%u\n",
			    __func__, (unsigned int)len));
		return;
	}

	pktlength = BTGETW(&data[BTHCI_ACL_DATA_LEN_OFFT]) +
		BTHCI_ACL_DATA_LEN_OFFT +
		BTHCI_ACL_DATA_LEN_LENGTH;

	if (pktlength != len) {
		DPRINTFN(1,("%s: acl packet length didn't match, "
			    "pktlen=%u size=%u\n",
			    __func__, (unsigned int)pktlength,
			    (unsigned int)len));
		return;
	}

	if (sc->sc_rd_len != 0) {
		DPRINTFN(1,("%s: dropping an acl packet, size=%u\n",
			    __func__, (unsigned int)len));
		return;
	}

	sc->sc_rd_buf[0] = BTHCI_PKTID_ACL_DATA;
	memcpy(&sc->sc_rd_buf[1], data, len);
	sc->sc_rd_len = len + 1;
	sc->sc_outputready = 1;

	wakeup(&sc->sc_outputready);
	selnotify(&sc->sc_rd_sel, 0);
}

static void
bthci_recvscodata(void *h, u_int8_t *data, size_t len)
{
	struct bthci_softc *sc = h;

	if (!sc->sc_open)
		return;
}