Home | History | Annotate | Line # | Download | only in bluetooth
bthub.c revision 1.2.2.1
      1  1.2.2.1       ad /*	$NetBSD: bthub.c,v 1.2.2.1 2006/11/18 21:34:04 ad 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.2.2.1       ad __KERNEL_RCSID(0, "$NetBSD: bthub.c,v 1.2.2.1 2006/11/18 21:34:04 ad 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.2.2.1       ad #include <prop/proplib.h>
     49  1.2.2.1       ad 
     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.2.2.1       ad #include "ioconf.h"
     55  1.2.2.1       ad 
     56      1.1  gdamore /*****************************************************************************
     57      1.1  gdamore  *
     58  1.2.2.1       ad  *	Bluetooth Device Hub
     59      1.1  gdamore  */
     60      1.1  gdamore 
     61      1.1  gdamore struct bthub_softc {
     62      1.1  gdamore 	struct device		sc_dev;
     63      1.1  gdamore 	LIST_HEAD(,btdev)	sc_list;
     64      1.1  gdamore };
     65      1.1  gdamore 
     66      1.1  gdamore /* autoconf(9) glue */
     67      1.1  gdamore static int	bthub_match(struct device *, struct cfdata *, void *);
     68      1.1  gdamore static void	bthub_attach(struct device *, struct device *, void *);
     69      1.1  gdamore static int	bthub_detach(struct device *, int);
     70      1.1  gdamore 
     71      1.1  gdamore CFATTACH_DECL(bthub, sizeof(struct bthub_softc),
     72      1.1  gdamore     bthub_match, bthub_attach, bthub_detach, NULL);
     73      1.1  gdamore 
     74  1.2.2.1       ad /* control file */
     75      1.1  gdamore dev_type_ioctl(bthubioctl);
     76      1.1  gdamore 
     77      1.1  gdamore const struct cdevsw bthub_cdevsw = {
     78      1.1  gdamore 	nullopen, nullclose, noread, nowrite, bthubioctl,
     79  1.2.2.1       ad 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     80      1.1  gdamore };
     81      1.1  gdamore 
     82  1.2.2.1       ad /* bthub functions */
     83  1.2.2.1       ad static int	bthub_print(void *, const char *);
     84  1.2.2.1       ad static int	bthub_pioctl(dev_t, unsigned long, prop_dictionary_t, int, struct lwp *);
     85      1.1  gdamore 
     86      1.1  gdamore /*****************************************************************************
     87      1.1  gdamore  *
     88      1.1  gdamore  *	bthub autoconf(9) routines
     89      1.1  gdamore  *
     90  1.2.2.1       ad  *	A Hub is attached to each Bluetooth Controller as it is enabled
     91      1.1  gdamore  */
     92      1.1  gdamore 
     93      1.1  gdamore static int
     94  1.2.2.1       ad bthub_match(struct device *self, struct cfdata *cfdata,
     95  1.2.2.1       ad     void *arg)
     96      1.1  gdamore {
     97      1.1  gdamore 
     98      1.1  gdamore 	return 1;
     99      1.1  gdamore }
    100      1.1  gdamore 
    101      1.1  gdamore static void
    102      1.1  gdamore bthub_attach(struct device *parent, struct device *self, void *aux)
    103      1.1  gdamore {
    104      1.1  gdamore 	struct bthub_softc *sc = (struct bthub_softc *)self;
    105  1.2.2.1       ad 	bdaddr_t *addr = aux;
    106  1.2.2.1       ad 	prop_dictionary_t dict;
    107  1.2.2.1       ad 	prop_object_t obj;
    108      1.1  gdamore 
    109      1.1  gdamore 	LIST_INIT(&sc->sc_list);
    110  1.2.2.1       ad 
    111  1.2.2.1       ad 	dict = device_properties(self);
    112  1.2.2.1       ad 	obj = prop_data_create_data(addr, sizeof(*addr));
    113  1.2.2.1       ad 	prop_dictionary_set(dict, BTDEVladdr, obj);
    114  1.2.2.1       ad 	prop_object_release(obj);
    115  1.2.2.1       ad 
    116  1.2.2.1       ad 	aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
    117  1.2.2.1       ad 			BTDEVladdr,
    118  1.2.2.1       ad 			addr->b[5], addr->b[4], addr->b[3],
    119  1.2.2.1       ad 			addr->b[2], addr->b[1], addr->b[0]);
    120  1.2.2.1       ad 
    121  1.2.2.1       ad 	aprint_normal("\n");
    122      1.1  gdamore }
    123      1.1  gdamore 
    124      1.1  gdamore static int
    125      1.1  gdamore bthub_detach(struct device *self, int flags)
    126      1.1  gdamore {
    127      1.1  gdamore 	struct bthub_softc *sc = (struct bthub_softc *)self;
    128  1.2.2.1       ad 	struct btdev *dev;
    129  1.2.2.1       ad 	int err;
    130      1.1  gdamore 
    131  1.2.2.1       ad 	while (!LIST_EMPTY(&sc->sc_list)) {
    132  1.2.2.1       ad 		dev = LIST_FIRST(&sc->sc_list);
    133  1.2.2.1       ad 		LIST_REMOVE(dev, sc_next);
    134  1.2.2.1       ad 
    135  1.2.2.1       ad 		err = config_detach((struct device *)dev, flags);
    136  1.2.2.1       ad 		if (err && (flags & DETACH_FORCE) == 0) {
    137  1.2.2.1       ad 			LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
    138  1.2.2.1       ad 			return err;
    139  1.2.2.1       ad 		}
    140      1.1  gdamore 	}
    141  1.2.2.1       ad 
    142      1.1  gdamore 	return 0;
    143      1.1  gdamore }
    144      1.1  gdamore 
    145  1.2.2.1       ad 
    146      1.1  gdamore /*****************************************************************************
    147      1.1  gdamore  *
    148  1.2.2.1       ad  *	bthub access functions to control device
    149      1.1  gdamore  */
    150      1.1  gdamore 
    151      1.1  gdamore int
    152      1.1  gdamore bthubioctl(dev_t devno, unsigned long cmd, caddr_t data, int flag, struct lwp *l)
    153      1.1  gdamore {
    154  1.2.2.1       ad 	prop_dictionary_t dict;
    155  1.2.2.1       ad 	int err;
    156  1.2.2.1       ad 
    157  1.2.2.1       ad 	switch(cmd) {
    158  1.2.2.1       ad 	case BTDEV_ATTACH:
    159  1.2.2.1       ad 	case BTDEV_DETACH:
    160  1.2.2.1       ad 		/* load dictionary */
    161  1.2.2.1       ad 		err = prop_dictionary_copyin_ioctl((const struct plistref *)data, cmd, &dict);
    162  1.2.2.1       ad 		if (err == 0) {
    163  1.2.2.1       ad 			err = bthub_pioctl(devno, cmd, dict, flag, l);
    164  1.2.2.1       ad 			prop_object_release(dict);
    165  1.2.2.1       ad 		}
    166  1.2.2.1       ad 		break;
    167  1.2.2.1       ad 
    168  1.2.2.1       ad 	default:
    169  1.2.2.1       ad 		err = EPASSTHROUGH;
    170  1.2.2.1       ad 		break;
    171  1.2.2.1       ad 	}
    172  1.2.2.1       ad 
    173  1.2.2.1       ad 	return err;
    174  1.2.2.1       ad }
    175  1.2.2.1       ad 
    176  1.2.2.1       ad static int
    177  1.2.2.1       ad bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
    178  1.2.2.1       ad     int flag, struct lwp *l)
    179  1.2.2.1       ad {
    180  1.2.2.1       ad 	struct bthub_softc *sc;
    181  1.2.2.1       ad 	struct btdev *dev;
    182  1.2.2.1       ad 	prop_data_t laddr, raddr;
    183  1.2.2.1       ad 	prop_string_t service;
    184  1.2.2.1       ad 	prop_dictionary_t prop;
    185  1.2.2.1       ad 	prop_object_t obj;
    186  1.2.2.1       ad 	int unit;
    187  1.2.2.1       ad 
    188  1.2.2.1       ad 	/* validate local address */
    189  1.2.2.1       ad 	laddr = prop_dictionary_get(dict, BTDEVladdr);
    190  1.2.2.1       ad 	if (prop_data_size(laddr) != sizeof(bdaddr_t))
    191  1.2.2.1       ad 		return EINVAL;
    192  1.2.2.1       ad 
    193  1.2.2.1       ad 	/* locate the relevant bthub */
    194  1.2.2.1       ad 	for (unit = 0 ; ; unit++) {
    195  1.2.2.1       ad 		if (unit == bthub_cd.cd_ndevs)
    196  1.2.2.1       ad 			return ENXIO;
    197  1.2.2.1       ad 
    198  1.2.2.1       ad 		sc = (struct bthub_softc *)bthub_cd.cd_devs[unit];
    199  1.2.2.1       ad 		if (sc == NULL)
    200  1.2.2.1       ad 			continue;
    201  1.2.2.1       ad 
    202  1.2.2.1       ad 		prop = device_properties(&sc->sc_dev);
    203  1.2.2.1       ad 		obj = prop_dictionary_get(prop, BTDEVladdr);
    204  1.2.2.1       ad 		if (prop_data_equals(laddr, obj))
    205  1.2.2.1       ad 			break;
    206  1.2.2.1       ad 	}
    207  1.2.2.1       ad 
    208  1.2.2.1       ad 	/* validate remote address */
    209  1.2.2.1       ad 	raddr = prop_dictionary_get(dict, BTDEVraddr);
    210  1.2.2.1       ad 	if (prop_data_size(raddr) != sizeof(bdaddr_t)
    211  1.2.2.1       ad 	    || bdaddr_any(prop_data_data_nocopy(raddr)))
    212  1.2.2.1       ad 		return EINVAL;
    213  1.2.2.1       ad 
    214  1.2.2.1       ad 	/* validate service name */
    215  1.2.2.1       ad 	service = prop_dictionary_get(dict, BTDEVservice);
    216  1.2.2.1       ad 	if (prop_object_type(service) != PROP_TYPE_STRING)
    217  1.2.2.1       ad 		return EINVAL;
    218  1.2.2.1       ad 
    219  1.2.2.1       ad 	/* locate matching child device, if any */
    220  1.2.2.1       ad 	LIST_FOREACH(dev, &sc->sc_list, sc_next) {
    221  1.2.2.1       ad 		prop = device_properties(&dev->sc_dev);
    222  1.2.2.1       ad 
    223  1.2.2.1       ad 		obj = prop_dictionary_get(prop, BTDEVraddr);
    224  1.2.2.1       ad 		if (!prop_object_equals(raddr, obj))
    225  1.2.2.1       ad 			continue;
    226  1.2.2.1       ad 
    227  1.2.2.1       ad 		obj = prop_dictionary_get(prop, BTDEVservice);
    228  1.2.2.1       ad 		if (!prop_object_equals(service, obj))
    229  1.2.2.1       ad 			continue;
    230      1.1  gdamore 
    231  1.2.2.1       ad 		break;
    232  1.2.2.1       ad 	}
    233      1.1  gdamore 
    234      1.1  gdamore 	switch (cmd) {
    235      1.1  gdamore 	case BTDEV_ATTACH:	/* attach BTDEV */
    236  1.2.2.1       ad 		if (dev != NULL)
    237  1.2.2.1       ad 			return EADDRINUSE;
    238      1.1  gdamore 
    239  1.2.2.1       ad 		dev = (struct btdev *)config_found((struct device *)sc,
    240  1.2.2.1       ad 						dict, bthub_print);
    241  1.2.2.1       ad 		if (dev == NULL)
    242      1.1  gdamore 			return ENXIO;
    243      1.1  gdamore 
    244  1.2.2.1       ad 		prop = device_properties(&dev->sc_dev);
    245  1.2.2.1       ad 		prop_dictionary_set(prop, BTDEVladdr, laddr);
    246  1.2.2.1       ad 		prop_dictionary_set(prop, BTDEVraddr, raddr);
    247  1.2.2.1       ad 		prop_dictionary_set(prop, BTDEVservice, service);
    248      1.1  gdamore 
    249  1.2.2.1       ad 		LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
    250  1.2.2.1       ad 		break;
    251      1.1  gdamore 
    252  1.2.2.1       ad 	case BTDEV_DETACH:	/* detach BTDEV */
    253  1.2.2.1       ad 		if (dev == NULL)
    254  1.2.2.1       ad 			return ENXIO;
    255      1.1  gdamore 
    256  1.2.2.1       ad 		LIST_REMOVE(dev, sc_next);
    257  1.2.2.1       ad 		config_detach((struct device *)dev, DETACH_FORCE);
    258      1.1  gdamore 		break;
    259      1.1  gdamore 	}
    260      1.1  gdamore 
    261  1.2.2.1       ad 	return 0;
    262      1.1  gdamore }
    263      1.1  gdamore 
    264      1.1  gdamore static int
    265      1.1  gdamore bthub_print(void *aux, const char *pnp)
    266      1.1  gdamore {
    267  1.2.2.1       ad 	prop_dictionary_t dict = aux;
    268  1.2.2.1       ad 	prop_object_t obj;
    269  1.2.2.1       ad 	const bdaddr_t *raddr;
    270  1.2.2.1       ad 
    271  1.2.2.1       ad 	if (pnp != NULL) {
    272  1.2.2.1       ad 		obj = prop_dictionary_get(dict, BTDEVtype);
    273  1.2.2.1       ad 		aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
    274  1.2.2.1       ad 					prop_string_cstring_nocopy(obj));
    275  1.2.2.1       ad 	}
    276      1.1  gdamore 
    277  1.2.2.1       ad 	obj = prop_dictionary_get(dict, BTDEVraddr);
    278  1.2.2.1       ad 	raddr = prop_data_data_nocopy(obj);
    279      1.1  gdamore 
    280  1.2.2.1       ad 	aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
    281  1.2.2.1       ad 			BTDEVraddr,
    282  1.2.2.1       ad 			raddr->b[5], raddr->b[4], raddr->b[3],
    283  1.2.2.1       ad 			raddr->b[2], raddr->b[1], raddr->b[0]);
    284      1.1  gdamore 
    285      1.1  gdamore 	return UNCONF;
    286      1.1  gdamore }
    287