The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netbt/sco_upper.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $OpenBSD: sco_upper.c,v 1.3 2008/11/22 04:42:58 uwe Exp $       */
    2 /*      $NetBSD: sco_upper.c,v 1.8 2008/08/06 15:01:24 plunky Exp $     */
    3 
    4 /*-
    5  * Copyright (c) 2006 Itronix Inc.
    6  * All rights reserved.
    7  *
    8  * Written by Iain Hibbert for Itronix Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. The name of Itronix Inc. may not be used to endorse
   19  *    or promote products derived from this software without specific
   20  *    prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
   26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   29  * ON ANY THEORY OF LIABILITY, WHETHER IN
   30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32  * POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 
   37 #include <sys/param.h>
   38 #include <sys/kernel.h>
   39 #include <sys/mbuf.h>
   40 #include <sys/proc.h>
   41 #include <sys/systm.h>
   42 
   43 #include <netbt/bluetooth.h>
   44 #include <netbt/hci.h>
   45 #include <netbt/sco.h>
   46 
   47 /****************************************************************************
   48  *
   49  *      SCO - Upper Protocol API
   50  */
   51 
   52 struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb);
   53 
   54 /*
   55  * sco_attach(handle, proto, upper)
   56  *
   57  *      Attach a new instance of SCO pcb to handle
   58  */
   59 int
   60 sco_attach(struct sco_pcb **handle,
   61                 const struct btproto *proto, void *upper)
   62 {
   63         struct sco_pcb *pcb;
   64 
   65         KASSERT(handle != NULL);
   66         KASSERT(proto != NULL);
   67         KASSERT(upper != NULL);
   68 
   69         pcb = malloc(sizeof(*pcb), M_BLUETOOTH, M_NOWAIT | M_ZERO);
   70         if (pcb == NULL)
   71                 return ENOMEM;
   72 
   73         pcb->sp_proto = proto;
   74         pcb->sp_upper = upper;
   75 
   76         LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next);
   77 
   78         *handle = pcb;
   79         return 0;
   80 }
   81 
   82 /*
   83  * sco_bind(pcb, sockaddr)
   84  *
   85  *      Bind SCO pcb to local address
   86  */
   87 int
   88 sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr)
   89 {
   90 
   91         bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr);
   92         return 0;
   93 }
   94 
   95 /*
   96  * sco_sockaddr(pcb, sockaddr)
   97  *
   98  *      Copy local address of PCB to sockaddr
   99  */
  100 int
  101 sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
  102 {
  103 
  104         memset(addr, 0, sizeof(struct sockaddr_bt));
  105         addr->bt_len = sizeof(struct sockaddr_bt);
  106         addr->bt_family = AF_BLUETOOTH;
  107         bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr);
  108         return 0;
  109 }
  110 
  111 /*
  112  * sco_connect(pcb, sockaddr)
  113  *
  114  *      Initiate a SCO connection to the destination address.
  115  */
  116 int
  117 sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest)
  118 {
  119         hci_add_sco_con_cp cp;
  120         struct hci_unit *unit;
  121         struct hci_link *acl, *sco;
  122         int err;
  123 
  124         if (pcb->sp_flags & SP_LISTENING)
  125                 return EINVAL;
  126 
  127         bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr);
  128 
  129         if (bdaddr_any(&pcb->sp_raddr))
  130                 return EDESTADDRREQ;
  131 
  132         if (bdaddr_any(&pcb->sp_laddr)) {
  133                 err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr);
  134                 if (err)
  135                         return err;
  136         }
  137 
  138         unit = hci_unit_lookup(&pcb->sp_laddr);
  139         if (unit == NULL)
  140                 return ENETDOWN;
  141 
  142         /*
  143          * We must have an already open ACL connection before we open the SCO
  144          * connection, and since SCO connections dont happen on their own we
  145          * will not open one, the application wanting this should have opened
  146          * it previously.
  147          */
  148         acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL);
  149         if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
  150                 return EHOSTUNREACH;
  151 
  152         sco = hci_link_alloc(unit, &pcb->sp_raddr, HCI_LINK_SCO);
  153         if (sco == NULL)
  154                 return ENOMEM;
  155 
  156         sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr);
  157         KASSERT(sco->hl_link == acl);
  158 
  159         cp.con_handle = htole16(acl->hl_handle);
  160         cp.pkt_type = htole16(0x00e0);          /* HV1, HV2, HV3 */
  161         err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp));
  162         if (err) {
  163                 hci_link_free(sco, err);
  164                 return err;
  165         }
  166 
  167         sco->hl_sco = pcb;
  168         pcb->sp_link = sco;
  169 
  170         pcb->sp_mtu = unit->hci_max_sco_size;
  171         return 0;
  172 }
  173 
  174 /*
  175  * sco_peeraddr(pcb, sockaddr)
  176  *
  177  *      Copy remote address of SCO pcb to sockaddr
  178  */
  179 int
  180 sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
  181 {
  182 
  183         memset(addr, 0, sizeof(struct sockaddr_bt));
  184         addr->bt_len = sizeof(struct sockaddr_bt);
  185         addr->bt_family = AF_BLUETOOTH;
  186         bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr);
  187         return 0;
  188 }
  189 
  190 /*
  191  * sco_disconnect(pcb, linger)
  192  *
  193  *      Initiate disconnection of connected SCO pcb
  194  */
  195 int
  196 sco_disconnect(struct sco_pcb *pcb, int linger)
  197 {
  198         hci_discon_cp cp;
  199         struct hci_link *sco;
  200         int err;
  201 
  202         sco = pcb->sp_link;
  203         if (sco == NULL)
  204                 return EINVAL;
  205 
  206         cp.con_handle = htole16(sco->hl_handle);
  207         cp.reason = 0x13;       /* "Remote User Terminated Connection" */
  208 
  209         err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
  210         if (err || linger == 0) {
  211                 sco->hl_sco = NULL;
  212                 pcb->sp_link = NULL;
  213                 hci_link_free(sco, err);
  214         }
  215 
  216         return err;
  217 }
  218 
  219 /*
  220  * sco_detach(handle)
  221  *
  222  *      Detach SCO pcb from handle and clear up
  223  */
  224 int
  225 sco_detach(struct sco_pcb **handle)
  226 {
  227         struct sco_pcb *pcb;
  228 
  229         KASSERT(handle != NULL);
  230         pcb = *handle;
  231         *handle = NULL;
  232 
  233         if (pcb == NULL)
  234                 return EINVAL;
  235 
  236         if (pcb->sp_link != NULL) {
  237                 sco_disconnect(pcb, 0);
  238                 pcb->sp_link = NULL;
  239         }
  240 
  241         LIST_REMOVE(pcb, sp_next);
  242         free(pcb, M_BLUETOOTH);
  243         return 0;
  244 }
  245 
  246 /*
  247  * sco_listen(pcb)
  248  *
  249  *      Mark pcb as a listener.
  250  */
  251 int
  252 sco_listen(struct sco_pcb *pcb)
  253 {
  254 
  255         if (pcb->sp_link != NULL)
  256                 return EINVAL;
  257 
  258         pcb->sp_flags |= SP_LISTENING;
  259         return 0;
  260 }
  261 
  262 /*
  263  * sco_send(pcb, mbuf)
  264  *
  265  *      Send data on SCO pcb.
  266  *
  267  * Gross hackage, we just output the packet directly onto the unit queue.
  268  * This will work fine for one channel per unit, but for more channels it
  269  * really needs fixing. We set the context so that when the packet is sent,
  270  * we can drop a record from the socket buffer.
  271  */
  272 int
  273 sco_send(struct sco_pcb *pcb, struct mbuf *m)
  274 {
  275         hci_scodata_hdr_t *hdr;
  276         int plen;
  277 
  278         if (pcb->sp_link == NULL) {
  279                 m_freem(m);
  280                 return EINVAL;
  281         }
  282 
  283         plen = m->m_pkthdr.len;
  284         DPRINTFN(10, "%d bytes\n", plen);
  285 
  286         /*
  287          * This is a temporary limitation, as USB devices cannot
  288          * handle SCO packet sizes that are not an integer number
  289          * of Isochronous frames. See ubt(4)
  290          */
  291         if (plen != pcb->sp_mtu) {
  292                 m_freem(m);
  293                 return EMSGSIZE;
  294         }
  295 
  296         M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_DONTWAIT);
  297         if (m == NULL)
  298                 return ENOMEM;
  299 
  300         hdr = mtod(m, hci_scodata_hdr_t *);
  301         hdr->type = HCI_SCO_DATA_PKT;
  302         hdr->con_handle = htole16(pcb->sp_link->hl_handle);
  303         hdr->length = plen;
  304 
  305         pcb->sp_pending++;
  306         M_SETCTX(m, pcb->sp_link);
  307         hci_output_sco(pcb->sp_link->hl_unit, m);
  308 
  309         return 0;
  310 }
  311 
  312 /*
  313  * sco_setopt(pcb, option, addr)
  314  *
  315  *      Set SCO pcb options
  316  */
  317 int
  318 sco_setopt(struct sco_pcb *pcb, int opt, void *addr)
  319 {
  320         int err = 0;
  321 
  322         switch (opt) {
  323         default:
  324                 err = ENOPROTOOPT;
  325                 break;
  326         }
  327 
  328         return err;
  329 }
  330 
  331 /*
  332  * sco_getopt(pcb, option, addr)
  333  *
  334  *      Get SCO pcb options
  335  */
  336 int
  337 sco_getopt(struct sco_pcb *pcb, int opt, void *addr)
  338 {
  339 
  340         switch (opt) {
  341         case SO_SCO_MTU:
  342                 *(uint16_t *)addr = pcb->sp_mtu;
  343                 return sizeof(uint16_t);
  344 
  345         case SO_SCO_HANDLE:
  346                 if (pcb->sp_link) {
  347                         *(uint16_t *)addr = pcb->sp_link->hl_handle;
  348                         return sizeof(uint16_t);
  349                 }
  350                 break;
  351 
  352         default:
  353                 break;
  354         }
  355         return 0;
  356 }

Cache object: a3965927dc4de043b33a5c6a52b90561


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.