Home | History | Annotate | Line # | Download | only in netbt
sco_upper.c revision 1.9
      1  1.9   plunky /*	$NetBSD: sco_upper.c,v 1.9 2010/01/04 19:20:05 plunky 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.9   plunky __KERNEL_RCSID(0, "$NetBSD: sco_upper.c,v 1.9 2010/01/04 19:20:05 plunky Exp $");
     36  1.1  gdamore 
     37  1.1  gdamore #include <sys/param.h>
     38  1.1  gdamore #include <sys/kernel.h>
     39  1.1  gdamore #include <sys/mbuf.h>
     40  1.1  gdamore #include <sys/proc.h>
     41  1.8   plunky #include <sys/socketvar.h>
     42  1.1  gdamore #include <sys/systm.h>
     43  1.1  gdamore 
     44  1.1  gdamore #include <netbt/bluetooth.h>
     45  1.1  gdamore #include <netbt/hci.h>
     46  1.1  gdamore #include <netbt/sco.h>
     47  1.1  gdamore 
     48  1.1  gdamore /****************************************************************************
     49  1.1  gdamore  *
     50  1.1  gdamore  *	SCO - Upper Protocol API
     51  1.1  gdamore  */
     52  1.1  gdamore 
     53  1.1  gdamore struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb);
     54  1.1  gdamore 
     55  1.1  gdamore /*
     56  1.1  gdamore  * sco_attach(handle, proto, upper)
     57  1.1  gdamore  *
     58  1.1  gdamore  *	Attach a new instance of SCO pcb to handle
     59  1.1  gdamore  */
     60  1.1  gdamore int
     61  1.1  gdamore sco_attach(struct sco_pcb **handle,
     62  1.1  gdamore 		const struct btproto *proto, void *upper)
     63  1.1  gdamore {
     64  1.1  gdamore 	struct sco_pcb *pcb;
     65  1.1  gdamore 
     66  1.6   plunky 	KASSERT(handle != NULL);
     67  1.6   plunky 	KASSERT(proto != NULL);
     68  1.6   plunky 	KASSERT(upper != NULL);
     69  1.1  gdamore 
     70  1.1  gdamore 	pcb = malloc(sizeof(struct sco_pcb), M_BLUETOOTH,
     71  1.1  gdamore 			M_NOWAIT | M_ZERO);
     72  1.1  gdamore 	if (pcb == NULL)
     73  1.1  gdamore 		return ENOMEM;
     74  1.1  gdamore 
     75  1.1  gdamore 	pcb->sp_proto = proto;
     76  1.1  gdamore 	pcb->sp_upper = upper;
     77  1.1  gdamore 
     78  1.1  gdamore 	LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next);
     79  1.1  gdamore 
     80  1.1  gdamore 	*handle = pcb;
     81  1.1  gdamore 	return 0;
     82  1.1  gdamore }
     83  1.1  gdamore 
     84  1.1  gdamore /*
     85  1.1  gdamore  * sco_bind(pcb, sockaddr)
     86  1.1  gdamore  *
     87  1.1  gdamore  *	Bind SCO pcb to local address
     88  1.1  gdamore  */
     89  1.1  gdamore int
     90  1.1  gdamore sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr)
     91  1.1  gdamore {
     92  1.1  gdamore 
     93  1.9   plunky 	if (pcb->sp_link != NULL || pcb->sp_flags & SP_LISTENING)
     94  1.9   plunky 		return EINVAL;
     95  1.9   plunky 
     96  1.1  gdamore 	bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr);
     97  1.1  gdamore 	return 0;
     98  1.1  gdamore }
     99  1.1  gdamore 
    100  1.1  gdamore /*
    101  1.1  gdamore  * sco_sockaddr(pcb, sockaddr)
    102  1.1  gdamore  *
    103  1.1  gdamore  *	Copy local address of PCB to sockaddr
    104  1.1  gdamore  */
    105  1.1  gdamore int
    106  1.1  gdamore sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
    107  1.1  gdamore {
    108  1.1  gdamore 
    109  1.1  gdamore 	memset(addr, 0, sizeof(struct sockaddr_bt));
    110  1.1  gdamore 	addr->bt_len = sizeof(struct sockaddr_bt);
    111  1.1  gdamore 	addr->bt_family = AF_BLUETOOTH;
    112  1.1  gdamore 	bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr);
    113  1.1  gdamore 	return 0;
    114  1.1  gdamore }
    115  1.1  gdamore 
    116  1.1  gdamore /*
    117  1.1  gdamore  * sco_connect(pcb, sockaddr)
    118  1.1  gdamore  *
    119  1.1  gdamore  *	Initiate a SCO connection to the destination address.
    120  1.1  gdamore  */
    121  1.1  gdamore int
    122  1.1  gdamore sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest)
    123  1.1  gdamore {
    124  1.1  gdamore 	hci_add_sco_con_cp cp;
    125  1.1  gdamore 	struct hci_unit *unit;
    126  1.1  gdamore 	struct hci_link *acl, *sco;
    127  1.1  gdamore 	int err;
    128  1.1  gdamore 
    129  1.1  gdamore 	if (pcb->sp_flags & SP_LISTENING)
    130  1.1  gdamore 		return EINVAL;
    131  1.1  gdamore 
    132  1.1  gdamore 	bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr);
    133  1.1  gdamore 
    134  1.1  gdamore 	if (bdaddr_any(&pcb->sp_raddr))
    135  1.1  gdamore 		return EDESTADDRREQ;
    136  1.1  gdamore 
    137  1.1  gdamore 	if (bdaddr_any(&pcb->sp_laddr)) {
    138  1.1  gdamore 		err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr);
    139  1.1  gdamore 		if (err)
    140  1.1  gdamore 			return err;
    141  1.1  gdamore 	}
    142  1.1  gdamore 
    143  1.1  gdamore 	unit = hci_unit_lookup(&pcb->sp_laddr);
    144  1.1  gdamore 	if (unit == NULL)
    145  1.1  gdamore 		return ENETDOWN;
    146  1.1  gdamore 
    147  1.1  gdamore 	/*
    148  1.1  gdamore 	 * We must have an already open ACL connection before we open the SCO
    149  1.1  gdamore 	 * connection, and since SCO connections dont happen on their own we
    150  1.1  gdamore 	 * will not open one, the application wanting this should have opened
    151  1.1  gdamore 	 * it previously.
    152  1.1  gdamore 	 */
    153  1.1  gdamore 	acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL);
    154  1.1  gdamore 	if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
    155  1.1  gdamore 		return EHOSTUNREACH;
    156  1.1  gdamore 
    157  1.7   plunky 	sco = hci_link_alloc(unit, &pcb->sp_raddr, HCI_LINK_SCO);
    158  1.1  gdamore 	if (sco == NULL)
    159  1.1  gdamore 		return ENOMEM;
    160  1.1  gdamore 
    161  1.1  gdamore 	sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr);
    162  1.1  gdamore 	KASSERT(sco->hl_link == acl);
    163  1.1  gdamore 
    164  1.1  gdamore 	cp.con_handle = htole16(acl->hl_handle);
    165  1.1  gdamore 	cp.pkt_type = htole16(0x00e0);		/* HV1, HV2, HV3 */
    166  1.1  gdamore 	err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp));
    167  1.1  gdamore 	if (err) {
    168  1.1  gdamore 		hci_link_free(sco, err);
    169  1.1  gdamore 		return err;
    170  1.1  gdamore 	}
    171  1.1  gdamore 
    172  1.1  gdamore 	sco->hl_sco = pcb;
    173  1.1  gdamore 	pcb->sp_link = sco;
    174  1.1  gdamore 
    175  1.1  gdamore 	pcb->sp_mtu = unit->hci_max_sco_size;
    176  1.1  gdamore 	return 0;
    177  1.1  gdamore }
    178  1.1  gdamore 
    179  1.1  gdamore /*
    180  1.1  gdamore  * sco_peeraddr(pcb, sockaddr)
    181  1.1  gdamore  *
    182  1.1  gdamore  *	Copy remote address of SCO pcb to sockaddr
    183  1.1  gdamore  */
    184  1.1  gdamore int
    185  1.1  gdamore sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
    186  1.1  gdamore {
    187  1.1  gdamore 
    188  1.1  gdamore 	memset(addr, 0, sizeof(struct sockaddr_bt));
    189  1.1  gdamore 	addr->bt_len = sizeof(struct sockaddr_bt);
    190  1.1  gdamore 	addr->bt_family = AF_BLUETOOTH;
    191  1.1  gdamore 	bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr);
    192  1.1  gdamore 	return 0;
    193  1.1  gdamore }
    194  1.1  gdamore 
    195  1.1  gdamore /*
    196  1.1  gdamore  * sco_disconnect(pcb, linger)
    197  1.1  gdamore  *
    198  1.1  gdamore  *	Initiate disconnection of connected SCO pcb
    199  1.1  gdamore  */
    200  1.1  gdamore int
    201  1.1  gdamore sco_disconnect(struct sco_pcb *pcb, int linger)
    202  1.1  gdamore {
    203  1.1  gdamore 	hci_discon_cp cp;
    204  1.1  gdamore 	struct hci_link *sco;
    205  1.1  gdamore 	int err;
    206  1.1  gdamore 
    207  1.1  gdamore 	sco = pcb->sp_link;
    208  1.1  gdamore 	if (sco == NULL)
    209  1.1  gdamore 		return EINVAL;
    210  1.1  gdamore 
    211  1.1  gdamore 	cp.con_handle = htole16(sco->hl_handle);
    212  1.1  gdamore 	cp.reason = 0x13;	/* "Remote User Terminated Connection" */
    213  1.1  gdamore 
    214  1.1  gdamore 	err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
    215  1.1  gdamore 	if (err || linger == 0) {
    216  1.1  gdamore 		sco->hl_sco = NULL;
    217  1.1  gdamore 		pcb->sp_link = NULL;
    218  1.1  gdamore 		hci_link_free(sco, err);
    219  1.1  gdamore 	}
    220  1.1  gdamore 
    221  1.1  gdamore 	return err;
    222  1.1  gdamore }
    223  1.1  gdamore 
    224  1.1  gdamore /*
    225  1.1  gdamore  * sco_detach(handle)
    226  1.1  gdamore  *
    227  1.1  gdamore  *	Detach SCO pcb from handle and clear up
    228  1.1  gdamore  */
    229  1.1  gdamore int
    230  1.1  gdamore sco_detach(struct sco_pcb **handle)
    231  1.1  gdamore {
    232  1.1  gdamore 	struct sco_pcb *pcb;
    233  1.1  gdamore 
    234  1.6   plunky 	KASSERT(handle != NULL);
    235  1.1  gdamore 	pcb = *handle;
    236  1.1  gdamore 	*handle = NULL;
    237  1.1  gdamore 
    238  1.1  gdamore 	if (pcb == NULL)
    239  1.1  gdamore 		return EINVAL;
    240  1.1  gdamore 
    241  1.1  gdamore 	if (pcb->sp_link != NULL) {
    242  1.1  gdamore 		sco_disconnect(pcb, 0);
    243  1.1  gdamore 		pcb->sp_link = NULL;
    244  1.1  gdamore 	}
    245  1.1  gdamore 
    246  1.1  gdamore 	LIST_REMOVE(pcb, sp_next);
    247  1.1  gdamore 	free(pcb, M_BLUETOOTH);
    248  1.1  gdamore 	return 0;
    249  1.1  gdamore }
    250  1.1  gdamore 
    251  1.1  gdamore /*
    252  1.1  gdamore  * sco_listen(pcb)
    253  1.1  gdamore  *
    254  1.1  gdamore  *	Mark pcb as a listener.
    255  1.1  gdamore  */
    256  1.1  gdamore int
    257  1.1  gdamore sco_listen(struct sco_pcb *pcb)
    258  1.1  gdamore {
    259  1.1  gdamore 
    260  1.1  gdamore 	if (pcb->sp_link != NULL)
    261  1.1  gdamore 		return EINVAL;
    262  1.1  gdamore 
    263  1.1  gdamore 	pcb->sp_flags |= SP_LISTENING;
    264  1.1  gdamore 	return 0;
    265  1.1  gdamore }
    266  1.1  gdamore 
    267  1.1  gdamore /*
    268  1.1  gdamore  * sco_send(pcb, mbuf)
    269  1.1  gdamore  *
    270  1.1  gdamore  *	Send data on SCO pcb.
    271  1.1  gdamore  *
    272  1.1  gdamore  * Gross hackage, we just output the packet directly onto the unit queue.
    273  1.1  gdamore  * This will work fine for one channel per unit, but for more channels it
    274  1.1  gdamore  * really needs fixing. We set the context so that when the packet is sent,
    275  1.1  gdamore  * we can drop a record from the socket buffer.
    276  1.1  gdamore  */
    277  1.1  gdamore int
    278  1.1  gdamore sco_send(struct sco_pcb *pcb, struct mbuf *m)
    279  1.1  gdamore {
    280  1.1  gdamore 	hci_scodata_hdr_t *hdr;
    281  1.1  gdamore 	int plen;
    282  1.1  gdamore 
    283  1.1  gdamore 	if (pcb->sp_link == NULL) {
    284  1.1  gdamore 		m_freem(m);
    285  1.1  gdamore 		return EINVAL;
    286  1.1  gdamore 	}
    287  1.1  gdamore 
    288  1.1  gdamore 	plen = m->m_pkthdr.len;
    289  1.1  gdamore 	DPRINTFN(10, "%d bytes\n", plen);
    290  1.1  gdamore 
    291  1.2   plunky 	/*
    292  1.2   plunky 	 * This is a temporary limitation, as USB devices cannot
    293  1.2   plunky 	 * handle SCO packet sizes that are not an integer number
    294  1.2   plunky 	 * of Isochronous frames. See ubt(4)
    295  1.2   plunky 	 */
    296  1.2   plunky 	if (plen != pcb->sp_mtu) {
    297  1.2   plunky 		m_freem(m);
    298  1.2   plunky 		return EMSGSIZE;
    299  1.2   plunky 	}
    300  1.2   plunky 
    301  1.1  gdamore 	M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_DONTWAIT);
    302  1.1  gdamore 	if (m == NULL)
    303  1.1  gdamore 		return ENOMEM;
    304  1.1  gdamore 
    305  1.1  gdamore 	hdr = mtod(m, hci_scodata_hdr_t *);
    306  1.1  gdamore 	hdr->type = HCI_SCO_DATA_PKT;
    307  1.1  gdamore 	hdr->con_handle = htole16(pcb->sp_link->hl_handle);
    308  1.1  gdamore 	hdr->length = plen;
    309  1.1  gdamore 
    310  1.1  gdamore 	pcb->sp_pending++;
    311  1.1  gdamore 	M_SETCTX(m, pcb->sp_link);
    312  1.1  gdamore 	hci_output_sco(pcb->sp_link->hl_unit, m);
    313  1.1  gdamore 
    314  1.1  gdamore 	return 0;
    315  1.1  gdamore }
    316  1.1  gdamore 
    317  1.1  gdamore /*
    318  1.8   plunky  * sco_setopt(pcb, sopt)
    319  1.1  gdamore  *
    320  1.1  gdamore  *	Set SCO pcb options
    321  1.1  gdamore  */
    322  1.1  gdamore int
    323  1.8   plunky sco_setopt(struct sco_pcb *pcb, const struct sockopt *sopt)
    324  1.1  gdamore {
    325  1.1  gdamore 	int err = 0;
    326  1.1  gdamore 
    327  1.8   plunky 	switch (sopt->sopt_name) {
    328  1.1  gdamore 	default:
    329  1.5   plunky 		err = ENOPROTOOPT;
    330  1.1  gdamore 		break;
    331  1.1  gdamore 	}
    332  1.1  gdamore 
    333  1.1  gdamore 	return err;
    334  1.1  gdamore }
    335  1.1  gdamore 
    336  1.1  gdamore /*
    337  1.8   plunky  * sco_getopt(pcb, sopt)
    338  1.1  gdamore  *
    339  1.1  gdamore  *	Get SCO pcb options
    340  1.1  gdamore  */
    341  1.1  gdamore int
    342  1.8   plunky sco_getopt(struct sco_pcb *pcb, struct sockopt *sopt)
    343  1.1  gdamore {
    344  1.1  gdamore 
    345  1.8   plunky 	switch (sopt->sopt_name) {
    346  1.1  gdamore 	case SO_SCO_MTU:
    347  1.8   plunky 		return sockopt_set(sopt, &pcb->sp_mtu, sizeof(uint16_t));
    348  1.1  gdamore 
    349  1.1  gdamore 	case SO_SCO_HANDLE:
    350  1.8   plunky 		if (pcb->sp_link)
    351  1.8   plunky 			return sockopt_set(sopt,
    352  1.8   plunky 			    &pcb->sp_link->hl_handle, sizeof(uint16_t));
    353  1.8   plunky 
    354  1.8   plunky 		return ENOTCONN;
    355  1.1  gdamore 
    356  1.1  gdamore 	default:
    357  1.1  gdamore 		break;
    358  1.1  gdamore 	}
    359  1.8   plunky 
    360  1.8   plunky 	return ENOPROTOOPT;
    361  1.1  gdamore }
    362