[388] | 1 | /*
|
---|
| 2 | * TINET (TCP/IP Protocol Stack)
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2001-2009 by Dep. of Computer Science and Engineering
|
---|
| 5 | * Tomakomai National College of Technology, JAPAN
|
---|
| 6 | * Copyright (C) 2014-2017 by Cores Co., Ltd. Japan
|
---|
| 7 | *
|
---|
| 8 | * ä¸è¨èä½æ¨©è
|
---|
| 9 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
| 10 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
| 11 | * å¤ã»åé
|
---|
| 12 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
| 13 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
| 14 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
| 15 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
| 16 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 17 | * ç¨ã§ããå½¢ã§åé
|
---|
| 18 | å¸ããå ´åã«ã¯ï¼åé
|
---|
| 19 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
| 20 | * è
|
---|
| 21 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
| 22 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 23 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 24 | * ç¨ã§ããªãå½¢ã§åé
|
---|
| 25 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
| 26 | * ã¨ï¼
|
---|
| 27 | * (a) åé
|
---|
| 28 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
| 29 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
| 30 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 31 | * (b) åé
|
---|
| 32 | å¸ã®å½¢æ
|
---|
| 33 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
| 34 | * å ±åãããã¨ï¼
|
---|
| 35 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
| 36 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 37 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
| 38 | 責ãããã¨ï¼
|
---|
| 39 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
| 40 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 41 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
| 42 | * å
|
---|
| 43 | 責ãããã¨ï¼
|
---|
| 44 | *
|
---|
| 45 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
| 46 | ã
|
---|
| 47 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
| 48 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
| 49 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
| 50 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
| 51 | *
|
---|
| 52 | * @(#) $Id: if_btusb.c 388 2019-05-22 11:25:18Z coas-nagasima $
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * Copyright (c) 1995, David Greenman
|
---|
| 57 | * All rights reserved.
|
---|
| 58 | *
|
---|
| 59 | * Redistribution and use in source and binary forms, with or without
|
---|
| 60 | * modification, are permitted provided that the following conditions
|
---|
| 61 | * are met:
|
---|
| 62 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 63 | * notice unmodified, this list of conditions, and the following
|
---|
| 64 | * disclaimer.
|
---|
| 65 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 66 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 67 | * documentation and/or other materials provided with the distribution.
|
---|
| 68 | *
|
---|
| 69 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 70 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 71 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 72 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
---|
| 73 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 74 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 75 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 76 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 77 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 78 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 79 | * SUCH DAMAGE.
|
---|
| 80 | *
|
---|
| 81 | * $FreeBSD: src/sys/i386/isa/if_ed.c,v 1.148.2.4 1999/09/25 13:08:18 nyan Exp $
|
---|
| 82 | */
|
---|
| 83 |
|
---|
| 84 | #define __BTSTACK_FILE__ "if_btusb.c"
|
---|
| 85 |
|
---|
| 86 | #ifdef TARGET_KERNEL_ASP
|
---|
| 87 |
|
---|
| 88 | #define CAST(type, val) ((type)(val))
|
---|
| 89 |
|
---|
| 90 | #include <kernel.h>
|
---|
| 91 | #include <sil.h>
|
---|
| 92 | #include <t_syslog.h>
|
---|
| 93 | #include "kernel_cfg.h"
|
---|
| 94 | #include "kernel/kernel_impl.h"
|
---|
| 95 | #include "btstack/utils.h"
|
---|
| 96 | #include "bt_main.h"
|
---|
| 97 | #include "usb_hbth.h"
|
---|
| 98 | //#include "hal_cpu.h"
|
---|
| 99 | //#include "hal_tick.h"
|
---|
| 100 | #include "hci.h"
|
---|
| 101 | #include "debug.h"
|
---|
| 102 |
|
---|
| 103 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 104 |
|
---|
| 105 | #ifdef TARGET_KERNEL_JSP
|
---|
| 106 |
|
---|
| 107 | #include <s_services.h>
|
---|
| 108 | #include <t_services.h>
|
---|
| 109 | #include "kernel_id.h"
|
---|
| 110 |
|
---|
| 111 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 112 |
|
---|
| 113 | #include <tinet_defs.h>
|
---|
| 114 | #include <tinet_config.h>
|
---|
| 115 |
|
---|
| 116 | #include <net/if.h>
|
---|
| 117 | #include <net/ethernet.h>
|
---|
| 118 | #include <net/if_arp.h>
|
---|
| 119 | #include <net/net.h>
|
---|
| 120 | #include <net/net_timer.h>
|
---|
| 121 | #include <net/net_count.h>
|
---|
| 122 | #include <net/net_buf.h>
|
---|
| 123 | #include "if_btusb.h"
|
---|
| 124 |
|
---|
| 125 | #include <string.h>
|
---|
| 126 |
|
---|
| 127 | #ifdef _MSC_VER
|
---|
| 128 | #include <stdlib.h>
|
---|
| 129 | #endif
|
---|
| 130 |
|
---|
| 131 | #ifndef TMO_IF_BTUSB_GET_NET_BUF
|
---|
| 132 | #define TMO_IF_BTUSB_GET_NET_BUF ULONG_C(1000000) /* [us] */
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
| 135 | uint8_t mac_addr[ETHER_ADDR_LEN];
|
---|
| 136 | static void (*btstack_network_send_packet_callback)(uint16_t bnep_cid, const uint8_t * packet, uint16_t size);
|
---|
| 137 |
|
---|
| 138 | int btstack_network_input_spos = 0;
|
---|
| 139 | int btstack_network_input_rpos = 0;
|
---|
| 140 | T_NET_BUF *btstack_network_input[NUM_IF_BTUSB_RXBUF];
|
---|
| 141 |
|
---|
| 142 | int btstack_network_output_spos = 0;
|
---|
| 143 | int btstack_network_output_rpos = 0;
|
---|
| 144 | T_NET_BUF *btstack_network_output[NUM_IF_BTUSB_TXBUF];
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã«ä¾åããã½ããã¦ã§ã¢æ
|
---|
| 148 | å ±
|
---|
| 149 | */
|
---|
| 150 |
|
---|
| 151 | typedef struct t_btusb_softc {
|
---|
| 152 | uint16_t bnep_cid;
|
---|
| 153 | char bnep_name[10];
|
---|
| 154 | bool_t over_flow;
|
---|
| 155 | } T_BTUSB_SOFTC;
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®ã½ããã¦ã§ã¢æ
|
---|
| 159 | å ±
|
---|
| 160 | */
|
---|
| 161 |
|
---|
| 162 | /* ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã«ä¾åããã½ããã¦ã§ã¢æ
|
---|
| 163 | å ± */
|
---|
| 164 |
|
---|
| 165 | static T_BTUSB_SOFTC btusb_softc;
|
---|
| 166 |
|
---|
| 167 | /* ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã«ä¾åããªãã½ããã¦ã§ã¢æ
|
---|
| 168 | å ± */
|
---|
| 169 |
|
---|
| 170 | T_IF_SOFTC if_softc = {
|
---|
| 171 | {{0,}}, /* ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®ã¢ãã¬ã¹ */
|
---|
| 172 | 0, /* éä¿¡ã¿ã¤ã ã¢ã¦ã */
|
---|
| 173 | &btusb_softc, /* ãã£ãã¤ã¹ä¾åã®ã½ããã¦ã§ã¢æ
|
---|
| 174 | å ± */
|
---|
| 175 | SEM_IF_BTUSB_SBUF_READY, /* éä¿¡ã»ããã© */
|
---|
| 176 | SEM_IF_BTUSB_RBUF_READY, /* åä¿¡ã»ããã© */
|
---|
| 177 |
|
---|
| 178 | #ifdef SUPPORT_INET6
|
---|
| 179 |
|
---|
| 180 | IF_MADDR_INIT, /* ãã«ããã£ã¹ãã¢ãã¬ã¹ãªã¹ã */
|
---|
| 181 |
|
---|
| 182 | #endif /* of #ifdef SUPPORT_INET6 */
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | /*
|
---|
| 186 | * å±æå¤æ°
|
---|
| 187 | */
|
---|
| 188 |
|
---|
| 189 | static void btusb_stop (T_BTUSB_SOFTC *sc);
|
---|
| 190 | static void btusb_init_sub (T_IF_SOFTC *ic);
|
---|
| 191 |
|
---|
| 192 | #ifdef SUPPORT_INET6
|
---|
| 193 |
|
---|
| 194 | static uint32_t ds_crc (uint8_t *addr);
|
---|
| 195 | static void ds_getmcaf (T_IF_SOFTC *ic, uint32_t *mcaf);
|
---|
| 196 |
|
---|
| 197 | /*
|
---|
| 198 | * ds_crc -- ã¤ã¼ãµãããã¢ãã¬ã¹ã® CRC ãè¨ç®ããã
|
---|
| 199 | */
|
---|
| 200 |
|
---|
| 201 | #define POLYNOMIAL 0x04c11db6
|
---|
| 202 |
|
---|
| 203 | static uint32_t
|
---|
| 204 | ds_crc (uint8_t *addr)
|
---|
| 205 | {
|
---|
| 206 | uint32_t crc = ULONG_C(0xffffffff);
|
---|
| 207 | int_t carry, len, bit;
|
---|
| 208 | uint8_t byte;
|
---|
| 209 |
|
---|
| 210 | for (len = ETHER_ADDR_LEN; len -- > 0; ) {
|
---|
| 211 | byte = *addr ++;
|
---|
| 212 | for (bit = 8; bit -- > 0; ) {
|
---|
| 213 | carry = ((crc & ULONG_C(0x80000000)) ? 1 : 0) ^ (byte & UINT_C(0x01));
|
---|
| 214 | crc <<= 1;
|
---|
| 215 | byte >>= 1;
|
---|
| 216 | if (carry)
|
---|
| 217 | crc = (crc ^ POLYNOMIAL) | carry;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | return crc;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | #undef POLYNOMIAL
|
---|
| 224 |
|
---|
| 225 | /*
|
---|
| 226 | * ds_getmcaf -- ãã«ããã£ã¹ãã¢ãã¬ã¹ã®ãªã¹ããããã«ããã£ã¹ãã¢ãã¬ã¹
|
---|
| 227 | * ãã£ã«ã¿ãè¨ç®ããã
|
---|
| 228 | */
|
---|
| 229 |
|
---|
| 230 | static void
|
---|
| 231 | ds_getmcaf (T_IF_SOFTC *ic, uint32_t *mcaf)
|
---|
| 232 | {
|
---|
| 233 | uint32_t count, index;
|
---|
| 234 | uint8_t *af = (uint8_t*)mcaf;
|
---|
| 235 |
|
---|
| 236 | mcaf[0] = mcaf[1] = 0;
|
---|
| 237 |
|
---|
| 238 | for (count = MAX_IF_MADDR_CNT; count -- > 0; ) {
|
---|
| 239 | index = ds_crc(ic->maddrs[count].lladdr) >> 26;
|
---|
| 240 | af[index >> 3] |= 1 << (index & 7);
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /*
|
---|
| 245 | * btusb_setrcr -- åä¿¡æ§æã¬ã¸ã¹ã¿ (RCR) ãè¨å®ããã
|
---|
| 246 | */
|
---|
| 247 |
|
---|
| 248 | static void
|
---|
| 249 | btusb_setrcr (T_IF_SOFTC *ic)
|
---|
| 250 | {
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /*
|
---|
| 254 | * btusb_addmulti -- ãã«ããã£ã¹ãã¢ãã¬ã¹ã追å ããã
|
---|
| 255 | */
|
---|
| 256 |
|
---|
| 257 | ER
|
---|
| 258 | btusb_addmulti (T_IF_SOFTC *ic)
|
---|
| 259 | {
|
---|
| 260 | btusb_setrcr(ic);
|
---|
| 261 | return E_OK;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | #endif /* of #ifdef SUPPORT_INET6 */
|
---|
| 265 |
|
---|
| 266 | void btstack_network_init(void (*send_packet_callback)(uint16_t bnep_cid, const uint8_t * packet, uint16_t size))
|
---|
| 267 | {
|
---|
| 268 | btstack_network_send_packet_callback = send_packet_callback;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | void btstack_network_up(uint16_t bnep_cid, bd_addr_t local_addr, bd_addr_t remote_addr)
|
---|
| 272 | {
|
---|
| 273 | T_BTUSB_SOFTC *sc = if_softc.sc;
|
---|
| 274 |
|
---|
| 275 | sc->bnep_cid = bnep_cid;
|
---|
| 276 |
|
---|
| 277 | /* MACã¢ãã¬ã¹ãæ´æ° */
|
---|
| 278 | memcpy(if_softc.ifaddr.lladdr, local_addr, sizeof(if_softc.ifaddr.lladdr));
|
---|
| 279 |
|
---|
| 280 | snprintf(sc->bnep_name, sizeof(sc->bnep_name), "BNEP%04x", bnep_cid);
|
---|
| 281 |
|
---|
| 282 | ether_set_link_up();
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | const char * btstack_network_get_name(uint16_t bnep_cid)
|
---|
| 286 | {
|
---|
| 287 | T_BTUSB_SOFTC *sc = if_softc.sc;
|
---|
| 288 |
|
---|
| 289 | return sc->bnep_name;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | int btstack_network_down(uint16_t bnep_cid)
|
---|
| 293 | {
|
---|
| 294 | T_BTUSB_SOFTC *sc = if_softc.sc;
|
---|
| 295 |
|
---|
| 296 | if (sc->bnep_cid != bnep_cid)
|
---|
| 297 | log_info("btstack_network_down(bnep_cid=%d)", bnep_cid);
|
---|
| 298 |
|
---|
| 299 | ether_set_link_down();
|
---|
| 300 |
|
---|
| 301 | sc->bnep_cid = 0;
|
---|
| 302 |
|
---|
| 303 | return 0;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /*
|
---|
| 307 | * btusb_stop -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ãåæ¢ããã
|
---|
| 308 | *
|
---|
| 309 | * 注æ: NIC å²ãè¾¼ã¿ç¦æ¢ç¶æ
|
---|
| 310 | ã§å¼ã³åºããã¨ã
|
---|
| 311 | */
|
---|
| 312 |
|
---|
| 313 | static void
|
---|
| 314 | btusb_stop (T_BTUSB_SOFTC *sc)
|
---|
| 315 | {
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | void btusb_bus_init() {}
|
---|
| 319 | void btusb_inter_init() {}
|
---|
| 320 |
|
---|
| 321 | /*
|
---|
| 322 | * btusb_init_sub -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®åæå
|
---|
| 323 | *
|
---|
| 324 | * 注æ: NIC å²ãè¾¼ã¿ç¦æ¢ç¶æ
|
---|
| 325 | ã§å¼ã³åºããã¨ã
|
---|
| 326 | */
|
---|
| 327 |
|
---|
| 328 | static void
|
---|
| 329 | btusb_init_sub (T_IF_SOFTC *ic)
|
---|
| 330 | {
|
---|
| 331 | #if defined(TARGET_KERNEL_ASP)
|
---|
| 332 |
|
---|
| 333 | /* ã¿ã¼ã²ããä¾åé¨ã®å²è¾¼ã¿åæå */
|
---|
| 334 | btusb_inter_init();
|
---|
| 335 |
|
---|
| 336 | #endif /* of #if defined(TARGET_KERNEL_ASP) */
|
---|
| 337 |
|
---|
| 338 | #if defined(TARGET_KERNEL_JSP) && TKERNEL_PRVER >= 0x1042u /* JSP-1.4.2 以é */
|
---|
| 339 |
|
---|
| 340 | /* ã¿ã¼ã²ããä¾åé¨ã®å²è¾¼ã¿åæå */
|
---|
| 341 | btusb_inter_init();
|
---|
| 342 |
|
---|
| 343 | #endif /* of #if defined(TARGET_KERNEL_JSP) && TKERNEL_PRVER >= 0x1042u */
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /*
|
---|
| 347 | * btusb_reset -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ããªã»ããããã
|
---|
| 348 | */
|
---|
| 349 |
|
---|
| 350 | void
|
---|
| 351 | btusb_reset (T_IF_SOFTC *ic)
|
---|
| 352 | {
|
---|
| 353 | NET_COUNT_ETHER_NIC(net_count_ether_nic[NC_ETHER_NIC_RESETS], 1);
|
---|
| 354 | btusb_stop(ic->sc);
|
---|
| 355 | btusb_init_sub(ic);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /*
|
---|
| 359 | * get_btusb_softc -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®ã½ããã¦ã§ã¢æ
|
---|
| 360 | å ±ãè¿ãã
|
---|
| 361 | */
|
---|
| 362 |
|
---|
| 363 | T_IF_SOFTC *
|
---|
| 364 | btusb_get_softc (void)
|
---|
| 365 | {
|
---|
| 366 | return &if_softc;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | /*
|
---|
| 370 | * btusb_watchdog -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®ã¯ããããã°ã¿ã¤ã ã¢ã¦ã
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | void
|
---|
| 374 | btusb_watchdog (T_IF_SOFTC *ic)
|
---|
| 375 | {
|
---|
| 376 | btusb_reset(ic);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /*
|
---|
| 380 | * btusb_probe -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®æ¤åº
|
---|
| 381 | */
|
---|
| 382 |
|
---|
| 383 | void
|
---|
| 384 | btusb_probe (T_IF_SOFTC *ic)
|
---|
| 385 | {
|
---|
| 386 | int i;
|
---|
| 387 | #if defined(TARGET_KERNEL_ASP)
|
---|
| 388 |
|
---|
| 389 | /* ã¿ã¼ã²ããä¾åé¨ã®ãã¹ã®åæå */
|
---|
| 390 | btusb_bus_init();
|
---|
| 391 |
|
---|
| 392 | #endif /* of #if defined(TARGET_KERNEL_ASP) */
|
---|
| 393 | #if defined(TARGET_KERNEL_JSP) && TKERNEL_PRVER >= 0x1042u /* JSP-1.4.2 以é */
|
---|
| 394 |
|
---|
| 395 | /* ã¿ã¼ã²ããä¾åé¨ã®ãã¹ã®åæå */
|
---|
| 396 | btusb_bus_init();
|
---|
| 397 |
|
---|
| 398 | #endif /* of #if defined(TARGET_KERNEL_JSP) && TKERNEL_PRVER >= 0x1042u */
|
---|
| 399 |
|
---|
| 400 | for(i = 0; i < ETHER_ADDR_LEN; i++){
|
---|
| 401 | ic->ifaddr.lladdr[i] = mac_addr[i];
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /*
|
---|
| 406 | * btusb_init -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®åæå
|
---|
| 407 | */
|
---|
| 408 |
|
---|
| 409 | void
|
---|
| 410 | btusb_init (T_IF_SOFTC *ic)
|
---|
| 411 | {
|
---|
| 412 | /* btusb_init æ¬ä½ãå¼ã³åºãã*/
|
---|
| 413 | btusb_init_sub(ic);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /*
|
---|
| 417 | * btstack_network_process_packet -- btstackãããã¬ã¼ã ã®åä¿¡
|
---|
| 418 | */
|
---|
| 419 |
|
---|
| 420 | void
|
---|
| 421 | btstack_network_process_packet(uint16_t bnep_cid, const uint8_t * packet, uint16_t size)
|
---|
| 422 | {
|
---|
| 423 | T_BTUSB_SOFTC *sc = if_softc.sc;
|
---|
| 424 | T_NET_BUF *input = NULL;
|
---|
| 425 | uint16_t align;
|
---|
| 426 | uint8_t *dst;
|
---|
| 427 | ER error;
|
---|
| 428 |
|
---|
| 429 | if (sc->bnep_cid != bnep_cid)
|
---|
| 430 | log_info("btstack_network_process_packet(bnep_cid=%d)", bnep_cid);
|
---|
| 431 |
|
---|
| 432 | // åä¿¡ãããã¡ããã£ã±ã
|
---|
| 433 | if (btstack_network_input[btstack_network_input_spos] != NULL) {
|
---|
| 434 | sc->over_flow = true;
|
---|
| 435 | return;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
| 439 | * +-----------+--------+---------+---------+
|
---|
| 440 | * | Ehter HDR | IP HDR | TCP HDR | TCP SDU |
|
---|
| 441 | * +-----------+--------+---------+---------+
|
---|
| 442 | * 14 20 20 n
|
---|
| 443 | * <----------------- len ---------------->
|
---|
| 444 | * ^
|
---|
| 445 | * t_net_buf 㧠4 ãªã¯ãããå¢çã«ã¢ã©ã¤ã³ããã¦ããã
|
---|
| 446 | *
|
---|
| 447 | * tcp_input 㨠udp_input ã§ã¯ãæ¬ä¼¼ããã㨠SDU ã§ãã§ãã¯ãµã ã
|
---|
| 448 | * è¨ç®ããããn ã 4 ãªã¯ãããå¢çã«ãªãããã« SDU ã®å¾ãã« 0 ã
|
---|
| 449 | * ãããã£ã³ã°ããããã®åãèæ
|
---|
| 450 | ®ã㦠net_buf ãç²å¾ããªããã°ãªããªãã
|
---|
| 451 | */
|
---|
| 452 | align = ((((size - sizeof(T_IF_HDR)) + 3) >> 2) << 2) + sizeof(T_IF_HDR);
|
---|
| 453 |
|
---|
| 454 | if ((error = tget_net_buf(&input, align, TMO_IF_BTUSB_GET_NET_BUF)) == E_OK && input != NULL) {
|
---|
| 455 | dst = input->buf + IF_ETHER_NIC_HDR_ALIGN;
|
---|
| 456 | memcpy(dst, (void *)packet, size);
|
---|
| 457 |
|
---|
| 458 | loc_cpu();
|
---|
| 459 |
|
---|
| 460 | btstack_network_input[btstack_network_input_spos] = input;
|
---|
| 461 | btstack_network_input_spos++;
|
---|
| 462 | if (btstack_network_input_spos == NUM_IF_BTUSB_RXBUF)
|
---|
| 463 | btstack_network_input_spos = 0;
|
---|
| 464 |
|
---|
| 465 | unl_cpu();
|
---|
| 466 |
|
---|
| 467 | sig_sem(if_softc.semid_rxb_ready);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | /*
|
---|
| 472 | * btusb_read -- ãã¬ã¼ã ã®èªã¿è¾¼ã¿
|
---|
| 473 | */
|
---|
| 474 |
|
---|
| 475 | T_NET_BUF *
|
---|
| 476 | btusb_read (T_IF_SOFTC *ic)
|
---|
| 477 | {
|
---|
| 478 | T_BTUSB_SOFTC *sc = ic->sc;
|
---|
| 479 | T_NET_BUF *input;
|
---|
| 480 |
|
---|
| 481 | if (sc->over_flow) {
|
---|
| 482 | sc->over_flow = false;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | loc_cpu();
|
---|
| 486 |
|
---|
| 487 | input = btstack_network_input[btstack_network_input_rpos];
|
---|
| 488 | btstack_network_input[btstack_network_input_rpos] = NULL;
|
---|
| 489 | btstack_network_input_rpos++;
|
---|
| 490 | if (btstack_network_input_rpos == NUM_IF_BTUSB_RXBUF)
|
---|
| 491 | btstack_network_input_rpos = 0;
|
---|
| 492 |
|
---|
| 493 | unl_cpu();
|
---|
| 494 |
|
---|
| 495 | if (input == NULL) {
|
---|
| 496 | NET_COUNT_ETHER_NIC(net_count_ether_nic[NC_ETHER_NIC_IN_ERR_PACKETS], 1);
|
---|
| 497 | NET_COUNT_ETHER_NIC(net_count_ether_nic[NC_ETHER_NIC_NO_BUFS], 1);
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | return input;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /*
|
---|
| 504 | * btusb_start -- éä¿¡ãã¬ã¼ã ããããã¡ãªã³ã°ããã
|
---|
| 505 | */
|
---|
| 506 |
|
---|
| 507 | void
|
---|
| 508 | btusb_start (T_IF_SOFTC *ic, T_NET_BUF *output)
|
---|
| 509 | {
|
---|
| 510 | T_BTUSB_SOFTC *sc = ic->sc;
|
---|
| 511 | bool_t send = (btstack_network_output_spos == btstack_network_output_rpos)
|
---|
| 512 | && (btstack_network_output[btstack_network_output_rpos] == NULL);
|
---|
| 513 |
|
---|
| 514 | if ((btstack_network_send_packet_callback == NULL)
|
---|
| 515 | || !bt_can_send_packet()) {
|
---|
| 516 | sig_sem(if_softc.semid_txb_ready);
|
---|
| 517 | return;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | output->flags |= NB_FLG_NOREL_IFOUT;
|
---|
| 521 |
|
---|
| 522 | loc_cpu();
|
---|
| 523 |
|
---|
| 524 | btstack_network_output[btstack_network_output_spos] = output;
|
---|
| 525 | btstack_network_output_spos++;
|
---|
| 526 | if (btstack_network_output_spos == NUM_IF_BTUSB_TXBUF)
|
---|
| 527 | btstack_network_output_spos = 0;
|
---|
| 528 |
|
---|
| 529 | unl_cpu();
|
---|
| 530 |
|
---|
| 531 | if (send)
|
---|
| 532 | btstack_network_send_packet_callback(sc->bnep_cid, output->buf, output->len);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | /*
|
---|
| 536 | * btstack_network_packet_sent -- ãããã¡ããéä¿¡ãã¬ã¼ã ã®éä¿¡ãå®äºããã
|
---|
| 537 | */
|
---|
| 538 |
|
---|
| 539 | void
|
---|
| 540 | btstack_network_packet_sent(uint16_t bnep_cid)
|
---|
| 541 | {
|
---|
| 542 | T_BTUSB_SOFTC *sc = if_softc.sc;
|
---|
| 543 | T_NET_BUF *rel, *output;
|
---|
| 544 |
|
---|
| 545 | if (sc->bnep_cid != bnep_cid)
|
---|
| 546 | log_info("btstack_network_packet_sent(bnep_cid=%d)", bnep_cid);
|
---|
| 547 |
|
---|
| 548 | loc_cpu();
|
---|
| 549 |
|
---|
| 550 | rel = btstack_network_output[btstack_network_output_rpos];
|
---|
| 551 | btstack_network_output[btstack_network_output_rpos] = NULL;
|
---|
| 552 | btstack_network_output_rpos++;
|
---|
| 553 | if (btstack_network_output_rpos == NUM_IF_BTUSB_TXBUF)
|
---|
| 554 | btstack_network_output_rpos = 0;
|
---|
| 555 |
|
---|
| 556 | output = btstack_network_output[btstack_network_output_rpos];
|
---|
| 557 |
|
---|
| 558 | unl_cpu();
|
---|
| 559 |
|
---|
| 560 | if (rel != NULL)
|
---|
| 561 | rel_net_buf(rel);
|
---|
| 562 |
|
---|
| 563 | sig_sem(if_softc.semid_txb_ready);
|
---|
| 564 |
|
---|
| 565 | if (output != NULL)
|
---|
| 566 | btstack_network_send_packet_callback(sc->bnep_cid, output->buf, output->len);
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | static void dummy_handler(void) {};
|
---|
| 570 | static void(*tick_handler)(void) = &dummy_handler;
|
---|
| 571 | void hal_tick_handler();
|
---|
| 572 |
|
---|
| 573 | void hal_tick_init(void)
|
---|
| 574 | {
|
---|
| 575 | // timer_regist_userfunc(hal_tick_handler);
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | void hal_tick_set_handler(void(*handler)(void))
|
---|
| 579 | {
|
---|
| 580 | if (handler == NULL) {
|
---|
| 581 | tick_handler = &dummy_handler;
|
---|
| 582 | return;
|
---|
| 583 | }
|
---|
| 584 | tick_handler = handler;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | int hal_tick_get_tick_period_in_ms(void)
|
---|
| 588 | {
|
---|
| 589 | return 1;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | void hal_tick_handler()
|
---|
| 593 | {
|
---|
| 594 | tick_handler();
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | uint32_t hal_time_ms(void)
|
---|
| 598 | {
|
---|
| 599 | SYSTIM time;
|
---|
| 600 | get_tim(&time);
|
---|
| 601 | return time / 1000;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | void hal_cpu_disable_irqs(void)
|
---|
| 605 | {
|
---|
| 606 | loc_cpu();
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | void hal_cpu_enable_irqs(void)
|
---|
| 610 | {
|
---|
| 611 | unl_cpu();
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | int btstack_sleep = 0;
|
---|
| 615 |
|
---|
| 616 | void hal_cpu_enable_irqs_and_sleep(void)
|
---|
| 617 | {
|
---|
| 618 | unl_cpu();
|
---|
| 619 | btstack_sleep = 1;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | //#define PACKET_DUMP
|
---|
| 623 | static void usb_dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
|
---|
| 624 | static void(*usb_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = usb_dummy_handler;
|
---|
| 625 |
|
---|
| 626 | void usb_bt_callback(uint8_t event_type, uint8_t *data, int size)
|
---|
| 627 | {
|
---|
| 628 | #ifdef PACKET_DUMP
|
---|
| 629 | int len = size;
|
---|
| 630 |
|
---|
| 631 | switch (event_type) {
|
---|
| 632 | case HCI_EVENT_PACKET:
|
---|
| 633 | printf("EVT <= ");
|
---|
| 634 | break;
|
---|
| 635 | case HCI_ACL_DATA_PACKET:
|
---|
| 636 | printf("ACL <= ");
|
---|
| 637 | break;
|
---|
| 638 | default:
|
---|
| 639 | printf("??? <= ");
|
---|
| 640 | break;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | if (len > 256)
|
---|
| 644 | len = 256;
|
---|
| 645 | printf_hexdump(data, len);
|
---|
| 646 | #endif
|
---|
| 647 | usb_packet_handler(event_type, data, size);
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | static void usb_init(const void *transport_config)
|
---|
| 651 | {
|
---|
| 652 |
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | static int usb_open(void *transport_config)
|
---|
| 656 | {
|
---|
| 657 | return 0;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | static int usb_close(void *transport_config)
|
---|
| 661 | {
|
---|
| 662 | return 0;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | static int usb_send_packet(uint8_t packet_type, uint8_t *packet, int size)
|
---|
| 666 | {
|
---|
| 667 | int r;
|
---|
| 668 | #ifdef PACKET_DUMP
|
---|
| 669 | int len = size;
|
---|
| 670 | if (len > 256)
|
---|
| 671 | len = 256;
|
---|
| 672 | #endif
|
---|
| 673 | switch (packet_type) {
|
---|
| 674 | case HCI_COMMAND_DATA_PACKET:
|
---|
| 675 | #ifdef PACKET_DUMP
|
---|
| 676 | printf("CMD => ");
|
---|
| 677 | printf_hexdump(packet, len);
|
---|
| 678 | #endif
|
---|
| 679 | r = bt_usb_hbth_control_write(packet, size);
|
---|
| 680 | if (r < 0 || r != size) {
|
---|
| 681 | log_error("Error submitting control transfer %d", r);
|
---|
| 682 | return -1;
|
---|
| 683 | }
|
---|
| 684 | return 0;
|
---|
| 685 | case HCI_ACL_DATA_PACKET:
|
---|
| 686 | #ifdef PACKET_DUMP
|
---|
| 687 | printf("ACL => ");
|
---|
| 688 | printf_hexdump(packet, len);
|
---|
| 689 | #endif
|
---|
| 690 | if (size >= 1024)
|
---|
| 691 | printf("HCI_ACL_DATA_PACKET size=%d\n", size);
|
---|
| 692 | r = bt_usb_hbth_bulk_write(packet, size);
|
---|
| 693 | if (r < 0) {
|
---|
| 694 | log_error("Error submitting data transfer, %d", r);
|
---|
| 695 | return -1;
|
---|
| 696 | }
|
---|
| 697 | return 0;
|
---|
| 698 | default:
|
---|
| 699 | return -1;
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | static void usb_register_packet_handler(void(*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size))
|
---|
| 704 | {
|
---|
| 705 | log_info("registering packet handler.");
|
---|
| 706 | usb_packet_handler = handler;
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | static const char * usb_get_transport_name(void){
|
---|
| 710 | return "USB";
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | static void usb_dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size)
|
---|
| 714 | {
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 | // single instance
|
---|
| 718 | static const hci_transport_t hci_transport_usb =
|
---|
| 719 | {
|
---|
| 720 | /*.open =*/ usb_open,
|
---|
| 721 | /*.close =*/ usb_close,
|
---|
| 722 | /*.send_packet =*/ usb_send_packet,
|
---|
| 723 | /*.register_packet_handler =*/ usb_register_packet_handler,
|
---|
| 724 | /*.get_transport_name =*/ usb_get_transport_name,
|
---|
| 725 | /*.set_baudrate =*/ NULL,
|
---|
| 726 | /*.can_send_packet_now =*/ NULL
|
---|
| 727 | };
|
---|
| 728 |
|
---|
| 729 | // get usb singleton
|
---|
| 730 | const hci_transport_t *hci_transport_usb_instance()
|
---|
| 731 | {
|
---|
| 732 | return &hci_transport_usb;
|
---|
| 733 | }
|
---|