Home | History | Annotate | Line # | Download | only in netbt
      1 /*	$NetBSD: bt_proto.c,v 1.17 2023/08/07 13:31:54 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 Iain Hibbert.
      5  * Copyright (c) 2006 Itronix Inc.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of Itronix Inc. may not be used to endorse
     17  *    or promote products derived from this software without specific
     18  *    prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: bt_proto.c,v 1.17 2023/08/07 13:31:54 riastradh Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/domain.h>
     38 #include <sys/kernel.h>
     39 #include <sys/module.h>
     40 #include <sys/protosw.h>
     41 #include <sys/socket.h>
     42 #include <sys/systm.h>
     43 
     44 #include <net/route.h>
     45 
     46 #include <netbt/bluetooth.h>
     47 #include <netbt/hci.h>
     48 #include <netbt/l2cap.h>
     49 #include <netbt/rfcomm.h>
     50 #include <netbt/sco.h>
     51 
     52 DOMAIN_DEFINE(btdomain);	/* forward declare and add to link set */
     53 
     54 static void	bt_init(void);
     55 
     56 PR_WRAP_CTLOUTPUT(hci_ctloutput)
     57 PR_WRAP_CTLOUTPUT(sco_ctloutput)
     58 PR_WRAP_CTLOUTPUT(l2cap_ctloutput)
     59 PR_WRAP_CTLOUTPUT(rfcomm_ctloutput)
     60 
     61 #define	hci_ctloutput		hci_ctloutput_wrapper
     62 #define	sco_ctloutput		sco_ctloutput_wrapper
     63 #define	l2cap_ctloutput		l2cap_ctloutput_wrapper
     64 #define	rfcomm_ctloutput	rfcomm_ctloutput_wrapper
     65 
     66 static const struct protosw btsw[] = {
     67 	{ /* raw HCI commands */
     68 		.pr_type = SOCK_RAW,
     69 		.pr_domain = &btdomain,
     70 		.pr_protocol = BTPROTO_HCI,
     71 		.pr_flags = (PR_ADDR | PR_ATOMIC),
     72 		.pr_init = hci_init,
     73 		.pr_ctloutput = hci_ctloutput,
     74 		.pr_usrreqs = &hci_usrreqs,
     75 	},
     76 	{ /* HCI SCO data (audio) */
     77 		.pr_type = SOCK_SEQPACKET,
     78 		.pr_domain = &btdomain,
     79 		.pr_protocol = BTPROTO_SCO,
     80 		.pr_flags = (PR_CONNREQUIRED | PR_ATOMIC | PR_LISTEN),
     81 		.pr_ctloutput = sco_ctloutput,
     82 		.pr_usrreqs = &sco_usrreqs,
     83 	},
     84 	{ /* L2CAP Connection Oriented */
     85 		.pr_type = SOCK_SEQPACKET,
     86 		.pr_domain = &btdomain,
     87 		.pr_protocol = BTPROTO_L2CAP,
     88 		.pr_flags = (PR_CONNREQUIRED | PR_ATOMIC | PR_LISTEN),
     89 		.pr_ctloutput = l2cap_ctloutput,
     90 		.pr_usrreqs = &l2cap_usrreqs,
     91 		.pr_init = l2cap_init,
     92 	},
     93 	{ /* RFCOMM */
     94 		.pr_type = SOCK_STREAM,
     95 		.pr_domain = &btdomain,
     96 		.pr_protocol = BTPROTO_RFCOMM,
     97 		.pr_flags = (PR_CONNREQUIRED | PR_LISTEN | PR_WANTRCVD),
     98 		.pr_ctloutput = rfcomm_ctloutput,
     99 		.pr_usrreqs = &rfcomm_usrreqs,
    100 		.pr_init = rfcomm_init,
    101 	},
    102 };
    103 
    104 struct domain btdomain = {
    105 	.dom_family = AF_BLUETOOTH,
    106 	.dom_name = "bluetooth",
    107 	.dom_init = bt_init,
    108 	.dom_protosw = btsw,
    109 	.dom_protoswNPROTOSW = &btsw[__arraycount(btsw)],
    110 };
    111 
    112 kmutex_t *bt_lock;
    113 
    114 static void
    115 bt_init(void)
    116 {
    117 }
    118 
    119 MODULE(MODULE_CLASS_DRIVER, netbt, NULL);
    120 
    121 static int
    122 netbt_modcmd(modcmd_t cmd, void *aux)
    123 {
    124 
    125 	switch (cmd) {
    126 	case MODULE_CMD_INIT:
    127 		bt_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    128 		return 0;
    129 	case MODULE_CMD_FINI:
    130 		return EBUSY;	/* XXX */
    131 	default:
    132 		return ENOTTY;
    133 	}
    134 }
    135