Home | History | Annotate | Line # | Download | only in bluetooth
      1  1.26   thorpej /*	$NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $	*/
      2   1.1   gdamore 
      3   1.1   gdamore /*-
      4   1.1   gdamore  * Copyright (c) 2006 Itronix Inc.
      5   1.1   gdamore  * All rights reserved.
      6   1.1   gdamore  *
      7   1.1   gdamore  * Written by Iain Hibbert for Itronix Inc.
      8   1.1   gdamore  *
      9   1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     10   1.1   gdamore  * modification, are permitted provided that the following conditions
     11   1.1   gdamore  * are met:
     12   1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     13   1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     14   1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     17   1.1   gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     18   1.1   gdamore  *    or promote products derived from this software without specific
     19   1.1   gdamore  *    prior written permission.
     20   1.1   gdamore  *
     21   1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22   1.1   gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.1   gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1   gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25   1.1   gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26   1.1   gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27   1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28   1.1   gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1   gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1   gdamore  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1   gdamore  */
     33   1.1   gdamore 
     34   1.1   gdamore #include <sys/cdefs.h>
     35  1.26   thorpej __KERNEL_RCSID(0, "$NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $");
     36   1.1   gdamore 
     37   1.1   gdamore #include <sys/param.h>
     38   1.1   gdamore #include <sys/conf.h>
     39   1.1   gdamore #include <sys/device.h>
     40   1.1   gdamore #include <sys/fcntl.h>
     41   1.1   gdamore #include <sys/kernel.h>
     42   1.1   gdamore #include <sys/queue.h>
     43   1.1   gdamore #include <sys/malloc.h>
     44   1.1   gdamore #include <sys/mbuf.h>
     45   1.1   gdamore #include <sys/proc.h>
     46   1.1   gdamore #include <sys/systm.h>
     47   1.1   gdamore 
     48   1.3    plunky #include <prop/proplib.h>
     49   1.3    plunky 
     50   1.1   gdamore #include <netbt/bluetooth.h>
     51   1.1   gdamore 
     52   1.1   gdamore #include <dev/bluetooth/btdev.h>
     53   1.1   gdamore 
     54  1.17  uebayasi #include "ioconf.h"
     55  1.17  uebayasi 
     56   1.1   gdamore /*****************************************************************************
     57   1.1   gdamore  *
     58   1.3    plunky  *	Bluetooth Device Hub
     59   1.1   gdamore  */
     60   1.1   gdamore 
     61   1.1   gdamore /* autoconf(9) glue */
     62  1.15    cegger static int	bthub_match(device_t, cfdata_t, void *);
     63  1.12    plunky static void	bthub_attach(device_t, device_t, void *);
     64  1.12    plunky static int	bthub_detach(device_t, int);
     65   1.1   gdamore 
     66  1.13    plunky CFATTACH_DECL_NEW(bthub, 0,
     67   1.1   gdamore     bthub_match, bthub_attach, bthub_detach, NULL);
     68   1.1   gdamore 
     69   1.3    plunky /* control file */
     70   1.1   gdamore dev_type_ioctl(bthubioctl);
     71   1.1   gdamore 
     72   1.1   gdamore const struct cdevsw bthub_cdevsw = {
     73  1.19  dholland 	.d_open = nullopen,
     74  1.19  dholland 	.d_close = nullclose,
     75  1.19  dholland 	.d_read = noread,
     76  1.19  dholland 	.d_write = nowrite,
     77  1.19  dholland 	.d_ioctl = bthubioctl,
     78  1.19  dholland 	.d_stop = nostop,
     79  1.19  dholland 	.d_tty = notty,
     80  1.19  dholland 	.d_poll = nopoll,
     81  1.19  dholland 	.d_mmap = nommap,
     82  1.19  dholland 	.d_kqfilter = nokqfilter,
     83  1.20  dholland 	.d_discard = nodiscard,
     84  1.19  dholland 	.d_flag = D_OTHER,
     85   1.1   gdamore };
     86   1.1   gdamore 
     87   1.3    plunky /* bthub functions */
     88   1.3    plunky static int	bthub_print(void *, const char *);
     89   1.3    plunky static int	bthub_pioctl(dev_t, unsigned long, prop_dictionary_t, int, struct lwp *);
     90   1.1   gdamore 
     91   1.1   gdamore /*****************************************************************************
     92   1.1   gdamore  *
     93   1.1   gdamore  *	bthub autoconf(9) routines
     94   1.1   gdamore  *
     95   1.3    plunky  *	A Hub is attached to each Bluetooth Controller as it is enabled
     96   1.1   gdamore  */
     97   1.1   gdamore 
     98   1.1   gdamore static int
     99  1.15    cegger bthub_match(device_t self, cfdata_t cfdata, void *arg)
    100   1.1   gdamore {
    101   1.1   gdamore 
    102   1.1   gdamore 	return 1;
    103   1.1   gdamore }
    104   1.1   gdamore 
    105   1.1   gdamore static void
    106  1.12    plunky bthub_attach(device_t parent, device_t self, void *aux)
    107   1.1   gdamore {
    108   1.3    plunky 	bdaddr_t *addr = aux;
    109   1.3    plunky 	prop_dictionary_t dict;
    110   1.3    plunky 	prop_object_t obj;
    111   1.1   gdamore 
    112   1.3    plunky 	dict = device_properties(self);
    113  1.23   thorpej 	obj = prop_data_create_copy(addr, sizeof(*addr));
    114   1.3    plunky 	prop_dictionary_set(dict, BTDEVladdr, obj);
    115   1.3    plunky 	prop_object_release(obj);
    116   1.3    plunky 
    117   1.3    plunky 	aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
    118   1.3    plunky 			BTDEVladdr,
    119   1.3    plunky 			addr->b[5], addr->b[4], addr->b[3],
    120   1.3    plunky 			addr->b[2], addr->b[1], addr->b[0]);
    121   1.3    plunky 
    122   1.3    plunky 	aprint_normal("\n");
    123  1.18    plunky 
    124  1.21  dholland 	if (!pmf_device_register(self, NULL, NULL)) {
    125  1.21  dholland 		/*
    126  1.21  dholland 		 * XXX this should not be allowed to happen, but
    127  1.21  dholland 		 * avoiding it needs a pretty big rearrangement of
    128  1.21  dholland 		 * device attachments.
    129  1.21  dholland 		 */
    130  1.22  dholland 		aprint_error_dev(self, "couldn't establish power handler\n");
    131  1.21  dholland 	}
    132   1.1   gdamore }
    133   1.1   gdamore 
    134   1.1   gdamore static int
    135  1.12    plunky bthub_detach(device_t self, int flags)
    136   1.1   gdamore {
    137   1.1   gdamore 
    138  1.18    plunky 	pmf_device_deregister(self);
    139  1.18    plunky 
    140  1.13    plunky 	return config_detach_children(self, flags);
    141   1.1   gdamore }
    142   1.1   gdamore 
    143   1.1   gdamore /*****************************************************************************
    144   1.1   gdamore  *
    145   1.3    plunky  *	bthub access functions to control device
    146   1.1   gdamore  */
    147   1.1   gdamore 
    148   1.1   gdamore int
    149   1.9  christos bthubioctl(dev_t devno, unsigned long cmd, void *data, int flag, struct lwp *l)
    150   1.1   gdamore {
    151   1.3    plunky 	prop_dictionary_t dict;
    152   1.3    plunky 	int err;
    153   1.3    plunky 
    154   1.3    plunky 	switch(cmd) {
    155   1.3    plunky 	case BTDEV_ATTACH:
    156   1.3    plunky 	case BTDEV_DETACH:
    157   1.3    plunky 		/* load dictionary */
    158  1.11    plunky 		err = prop_dictionary_copyin_ioctl(data, cmd, &dict);
    159   1.3    plunky 		if (err == 0) {
    160   1.3    plunky 			err = bthub_pioctl(devno, cmd, dict, flag, l);
    161   1.3    plunky 			prop_object_release(dict);
    162   1.3    plunky 		}
    163   1.3    plunky 		break;
    164   1.3    plunky 
    165   1.3    plunky 	default:
    166   1.3    plunky 		err = EPASSTHROUGH;
    167   1.3    plunky 		break;
    168   1.3    plunky 	}
    169   1.3    plunky 
    170   1.3    plunky 	return err;
    171   1.3    plunky }
    172   1.3    plunky 
    173   1.3    plunky static int
    174   1.8  christos bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
    175   1.8  christos     int flag, struct lwp *l)
    176   1.3    plunky {
    177   1.6    plunky 	prop_data_t laddr, raddr;
    178   1.6    plunky 	prop_string_t service;
    179   1.6    plunky 	prop_dictionary_t prop;
    180   1.3    plunky 	prop_object_t obj;
    181  1.13    plunky 	device_t dev, self;
    182  1.13    plunky 	deviter_t di;
    183   1.3    plunky 	int unit;
    184   1.3    plunky 
    185   1.3    plunky 	/* validate local address */
    186   1.6    plunky 	laddr = prop_dictionary_get(dict, BTDEVladdr);
    187   1.6    plunky 	if (prop_data_size(laddr) != sizeof(bdaddr_t))
    188   1.3    plunky 		return EINVAL;
    189   1.3    plunky 
    190   1.3    plunky 	/* locate the relevant bthub */
    191   1.3    plunky 	for (unit = 0 ; ; unit++) {
    192   1.3    plunky 		if (unit == bthub_cd.cd_ndevs)
    193   1.3    plunky 			return ENXIO;
    194   1.3    plunky 
    195  1.14    cegger 		self = device_lookup(&bthub_cd, unit);
    196  1.13    plunky 		if (self == NULL)
    197   1.3    plunky 			continue;
    198  1.10    plunky 
    199  1.13    plunky 		prop = device_properties(self);
    200   1.6    plunky 		obj = prop_dictionary_get(prop, BTDEVladdr);
    201   1.6    plunky 		if (prop_data_equals(laddr, obj))
    202   1.3    plunky 			break;
    203   1.3    plunky 	}
    204   1.1   gdamore 
    205   1.3    plunky 	/* validate remote address */
    206   1.6    plunky 	raddr = prop_dictionary_get(dict, BTDEVraddr);
    207   1.6    plunky 	if (prop_data_size(raddr) != sizeof(bdaddr_t)
    208  1.24   thorpej 	    || bdaddr_any(prop_data_value(raddr)))
    209   1.6    plunky 		return EINVAL;
    210   1.6    plunky 
    211   1.6    plunky 	/* validate service name */
    212   1.6    plunky 	service = prop_dictionary_get(dict, BTDEVservice);
    213   1.6    plunky 	if (prop_object_type(service) != PROP_TYPE_STRING)
    214   1.3    plunky 		return EINVAL;
    215   1.3    plunky 
    216   1.6    plunky 	/* locate matching child device, if any */
    217  1.13    plunky 	deviter_init(&di, 0);
    218  1.13    plunky 	while ((dev = deviter_next(&di)) != NULL) {
    219  1.13    plunky 		if (device_parent(dev) != self)
    220  1.13    plunky 			continue;
    221  1.13    plunky 
    222  1.13    plunky 		prop = device_properties(dev);
    223   1.6    plunky 
    224   1.6    plunky 		obj = prop_dictionary_get(prop, BTDEVraddr);
    225   1.6    plunky 		if (!prop_object_equals(raddr, obj))
    226   1.6    plunky 			continue;
    227   1.6    plunky 
    228   1.6    plunky 		obj = prop_dictionary_get(prop, BTDEVservice);
    229   1.6    plunky 		if (!prop_object_equals(service, obj))
    230   1.6    plunky 			continue;
    231   1.6    plunky 
    232   1.6    plunky 		break;
    233   1.3    plunky 	}
    234  1.13    plunky 	deviter_release(&di);
    235   1.1   gdamore 
    236   1.1   gdamore 	switch (cmd) {
    237   1.1   gdamore 	case BTDEV_ATTACH:	/* attach BTDEV */
    238  1.13    plunky 		if (dev != NULL)
    239   1.3    plunky 			return EADDRINUSE;
    240   1.1   gdamore 
    241  1.26   thorpej 		dev = config_found(self, dict, bthub_print, CFARGS_NONE);
    242  1.10    plunky 		if (dev == NULL)
    243   1.1   gdamore 			return ENXIO;
    244   1.1   gdamore 
    245  1.12    plunky 		prop = device_properties(dev);
    246   1.6    plunky 		prop_dictionary_set(prop, BTDEVladdr, laddr);
    247   1.6    plunky 		prop_dictionary_set(prop, BTDEVraddr, raddr);
    248   1.6    plunky 		prop_dictionary_set(prop, BTDEVservice, service);
    249   1.3    plunky 		break;
    250   1.1   gdamore 
    251   1.1   gdamore 	case BTDEV_DETACH:	/* detach BTDEV */
    252  1.13    plunky 		if (dev == NULL)
    253   1.3    plunky 			return ENXIO;
    254   1.1   gdamore 
    255  1.13    plunky 		config_detach(dev, DETACH_FORCE);
    256   1.1   gdamore 		break;
    257   1.1   gdamore 	}
    258   1.1   gdamore 
    259   1.3    plunky 	return 0;
    260   1.1   gdamore }
    261   1.1   gdamore 
    262   1.1   gdamore static int
    263   1.1   gdamore bthub_print(void *aux, const char *pnp)
    264   1.1   gdamore {
    265   1.3    plunky 	prop_dictionary_t dict = aux;
    266   1.3    plunky 	prop_object_t obj;
    267   1.3    plunky 	const bdaddr_t *raddr;
    268   1.3    plunky 
    269   1.3    plunky 	if (pnp != NULL) {
    270   1.3    plunky 		obj = prop_dictionary_get(dict, BTDEVtype);
    271   1.3    plunky 		aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
    272  1.24   thorpej 					prop_string_value(obj));
    273   1.3    plunky 	}
    274   1.1   gdamore 
    275   1.3    plunky 	obj = prop_dictionary_get(dict, BTDEVraddr);
    276  1.24   thorpej 	raddr = prop_data_value(obj);
    277   1.1   gdamore 
    278   1.3    plunky 	aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
    279   1.3    plunky 			BTDEVraddr,
    280   1.3    plunky 			raddr->b[5], raddr->b[4], raddr->b[3],
    281   1.3    plunky 			raddr->b[2], raddr->b[1], raddr->b[0]);
    282   1.1   gdamore 
    283   1.1   gdamore 	return UNCONF;
    284   1.1   gdamore }
    285