1/*	$NetBSD: bth5.h,v 1.2 2017/09/03 23:11:19 nat Exp $	*/
  2/*
  3 * Copyright (c) 2017 Nathanial Sloss <nathanialsloss@yahoo.com.au>
  4 * All rights reserved.
  5 *
  6 * Copyright (c) 2007 KIYOHARA Takashi
  7 * All rights reserved.
  8 *
  9 * Redistribution and use in source and binary forms, with or without
 10 * modification, are permitted provided that the following conditions
 11 * are met:
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions and the following disclaimer.
 14 * 2. Redistributions in binary form must reproduce the above copyright
 15 *    notice, this list of conditions and the following disclaimer in the
 16 *    documentation and/or other materials provided with the distribution.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 28 * POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#ifndef _DEV_BLUETOOTH_BTH5_H_
 32#define _DEV_BLUETOOTH_BTH5_H_
 33
 34/*
 35 * BT UART H5 (3-wire) serial protocol definitions.
 36 */
 37
 38/* BTH5 packet header */
 39typedef struct {
 40	uint8_t flags;
 41#if BYTE_ORDER == BIG_ENDIAN
 42	uint8_t plen1 :4;		/* Payload Length (bits 0-3) */
 43	uint8_t ident :4;		/* Protocol Identifier */
 44#else
 45	uint8_t ident :4;		/* Protocol Identifier */
 46	uint8_t plen1 :4;		/* Payload Length (bits 0-3) */
 47#endif
 48	uint8_t plen2;			/* Payload Length (bits 4-11) */
 49	uint8_t csum;			/* Checksum */
 50	u_char payload[0];
 51} __packed bth5_hdr_t;
 52
 53#define BTH5_FLAGS_SEQ_SHIFT	0
 54#define BTH5_FLAGS_SEQ_MASK	0x07
 55#define BTH5_FLAGS_SEQ(n) \
 56	(((n) & BTH5_FLAGS_SEQ_MASK) >> BTH5_FLAGS_SEQ_SHIFT)
 57#define BTH5_FLAGS_ACK_SHIFT	3
 58#define BTH5_FLAGS_ACK_MASK	0x38
 59#define BTH5_FLAGS_ACK(n) \
 60	(((n) & BTH5_FLAGS_ACK_MASK) >> BTH5_FLAGS_ACK_SHIFT)
 61#define BTH5_FLAGS_CRC_PRESENT	0x40
 62#define BTH5_FLAGS_PROTOCOL_TYPE 0x80
 63#define BTH5_FLAGS_PROTOCOL_REL	0x80
 64
 65#define BTH5_CONFIG_ACK_MASK	0x07
 66#define BTH5_CONFIG_FLOW_MASK	(1 << 7)
 67
 68#define BTH5_SET_PLEN(hdrp, n)				\
 69	do {						\
 70		(hdrp)->plen1 = ((n) & 0x00f);		\
 71		(hdrp)->plen2 = ((n) >> 4);		\
 72	} while (0)
 73#define BTH5_GET_PLEN(hdrp)	((hdrp)->plen1 | ((hdrp)->plen2 << 4))
 74
 75#define BTH5_GET_CSUM(hdrp)						\
 76	(0xff - (uint8_t)((hdrp)->flags + ((hdrp)->plen1 << 4) +	\
 77	(hdrp)->ident + (hdrp)->plen2))
 78#define BTH5_SET_CSUM(hdrp)	((hdrp)->csum = BTH5_GET_CSUM(hdrp))
 79
 80
 81#define BTH5_IDENT_ACKPKT	0	/* Used by MUX Layer */
 82
 83/* definitions of SLIP Layer */
 84#define BTH5_SLIP_PKTSTART	0xc0
 85#define BTH5_SLIP_PKTEND	BTH5_SLIP_PKTSTART
 86#define BTH5_SLIP_XON		0x11
 87#define BTH5_SLIP_XOFF		0x13
 88#define BTH5_SLIP_ESCAPE	0xdb
 89#define BTH5_SLIP_ESCAPE_PKTEND	0xdc
 90#define BTH5_SLIP_ESCAPE_ESCAPE	0xdd
 91#define BTH5_SLIP_ESCAPE_XON	0xde
 92#define BTH5_SLIP_ESCAPE_XOFF	0xdf
 93
 94
 95/* definitions of Sequencing Layer */
 96#define BTH5_SEQ_TX_TIMEOUT	(hz / 4)	/* 250 msec */
 97#define BTH5_SEQ_TX_WINSIZE	7
 98#define BTH5_SEQ_TX_RETRY_LIMIT	20
 99
100
101/*
102 *   Channel Allocation
103 */
104#define BTH5_CHANNEL_HCI_CMD	1	/* HCI Command and Event */
105#define BTH5_CHANNEL_HCI_EVT	4	/* HCI Command and Event */
106#define BTH5_CHANNEL_HCI_ACL	2	/* HCI ACL data */
107#define BTH5_CHANNEL_HCI_SCO	3	/* HCI SCO data */
108#define BTH5_CHANNEL_LE		15	/* Link Establishment */
109
110
111/*
112 *   Link Establishment Protocol
113 */
114typedef enum {
115	le_state_shy,
116	le_state_curious,
117	le_state_garrulous
118} bth5_le_state_t;
119
120#define BTH5_LE_SYNC		{ 0x01, 0x7e };
121#define BTH5_LE_SYNCRESP	{ 0x02, 0x7d };
122#define BTH5_LE_CONF		{ 0x03, 0xfc, 0x0f };
123#define BTH5_LE_CONFRESP	{ 0x04, 0x7b, 0x0f };
124
125#define BTH5_LE_TSHY_TIMEOUT	hz	/* XXXX: 1sec ? */
126#define BTH5_LE_TCONF_TIMEOUT	hz	/* XXXX: 1sec ? */
127
128#endif	/* !_DEV_BLUETOOTH_BTH5_H_ */