1 /*
2  *  Copyright (c) 2013, The Linux Foundation. All rights reserved.
3  *  Not a Contribution.
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #ifndef HCI_UART_H
20 #define HCI_UART_H
21 
22 #include <asm-generic/ioctls.h>
23 
24 /* Variables to identify the platform */
25 /*BT HS UART TTY DEVICE */
26 #define BT_HS_UART_DEVICE "/dev/ttyHS0"
27 
28 /**** baud rates ****/
29 #define USERIAL_BAUD_300        0
30 #define USERIAL_BAUD_600        1
31 #define USERIAL_BAUD_1200       2
32 #define USERIAL_BAUD_2400       3
33 #define USERIAL_BAUD_9600       4
34 #define USERIAL_BAUD_19200      5
35 #define USERIAL_BAUD_57600      6
36 #define USERIAL_BAUD_115200     7
37 #define USERIAL_BAUD_230400     8
38 #define USERIAL_BAUD_460800     9
39 #define USERIAL_BAUD_921600     10
40 #define USERIAL_BAUD_1M         11
41 #define USERIAL_BAUD_1_5M       12
42 #define USERIAL_BAUD_2M         13
43 #define USERIAL_BAUD_3M         14
44 #define USERIAL_BAUD_4M         15
45 #define USERIAL_BAUD_AUTO       16
46 
47 /**** Data Format ****/
48 /* Stop Bits */
49 #define USERIAL_STOPBITS_1      1
50 #define USERIAL_STOPBITS_1_5    (1<<1)
51 #define USERIAL_STOPBITS_2      (1<<2)
52 
53 /* Parity Bits */
54 #define USERIAL_PARITY_NONE     (1<<3)
55 #define USERIAL_PARITY_EVEN     (1<<4)
56 #define USERIAL_PARITY_ODD      (1<<5)
57 
58 /* Data Bits */
59 #define USERIAL_DATABITS_5      (1<<6)
60 #define USERIAL_DATABITS_6      (1<<7)
61 #define USERIAL_DATABITS_7      (1<<8)
62 #define USERIAL_DATABITS_8      (1<<9)
63 
64 /* HCI Packet types */
65 #define HCI_COMMAND_PKT     0x01
66 #define HCI_ACLDATA_PKT      0x02
67 #define HCI_SCODATA_PKT     0x03
68 #define HCI_EVENT_PKT           0x04
69 #define HCI_VENDOR_PKT        0xff
70 
71 /* HCI Command/Event Opcode */
72 #define HCI_RESET                       0x0C03
73 #define EVT_CMD_COMPLETE       0x0E
74 
75 /* Command opcode pack/unpack */
76 #define cmd_opcode_pack(ogf, ocf)   (uint16_t)((ocf & 0x03ff)|(ogf << 10))
77 
78 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
79 /* These are the ioctl values used for bt_wake ioctl via UART driver. you may
80  * need to redefine them on you platform!
81  * Logically they need to be unique and not colide with existing uart ioctl's.
82  */
83 #ifndef USERIAL_IOCTL_BT_WAKE_ASSERT
84 #define USERIAL_IOCTL_BT_WAKE_ASSERT   0x8003
85 #endif
86 #ifndef USERIAL_IOCTL_BT_WAKE_DEASSERT
87 #define USERIAL_IOCTL_BT_WAKE_DEASSERT 0x8004
88 #endif
89 #ifndef USERIAL_IOCTL_BT_WAKE_GET_ST
90 #define USERIAL_IOCTL_BT_WAKE_GET_ST   0x8005
91 #endif
92 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
93 
94 /* UART CLOCK IOCTLS*/
95 /* UART CLOCK IOCTLS*/
96 #define USERIAL_OP_CLK_ON    TIOCPMGET    /* PM get */
97 #define USERIAL_OP_CLK_OFF    TIOCPMPUT   /* PM put */
98 #define USERIAL_OP_CLK_STATE    TIOCPMACT    /* PM is active */
99 
100 /******************************************************************************
101 **  Type definitions
102 ******************************************************************************/
103 
104 /* Structure used to configure serial port during open */
105 typedef struct
106 {
107     uint16_t fmt;       /* Data format */
108     uint8_t  baud;      /* Baud rate */
109 } tUSERIAL_CFG;
110 
111 typedef enum {
112 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
113     USERIAL_OP_ASSERT_BT_WAKE,
114     USERIAL_OP_DEASSERT_BT_WAKE,
115     USERIAL_OP_GET_BT_WAKE_STATE,
116 #endif
117     USERIAL_OP_FLOW_ON,
118     USERIAL_OP_FLOW_OFF,
119     USERIAL_OP_NOP,
120 } userial_vendor_ioctl_op_t;
121 
122 /* UPIO signals */
123 enum {
124     UPIO_BT_WAKE = 0,
125     UPIO_HOST_WAKE,
126     UPIO_LPM_MODE,
127     UPIO_MAX_COUNT
128 };
129 
130 /* UPIO assertion/deassertion */
131 enum {
132     UPIO_UNKNOWN = 0,
133     UPIO_DEASSERT,
134     UPIO_ASSERT
135 };
136 
137 #define VND_PORT_NAME_MAXLEN    256
138 
139 /* vendor serial control block */
140 typedef struct
141 {
142     int fd;                     /* fd to Bluetooth device */
143     struct termios termios;     /* serial terminal of BT port */
144     char port_name[VND_PORT_NAME_MAXLEN];
145 } vnd_userial_cb_t;
146 
147 typedef struct {
148     uint8_t     ncmd;
149     uint16_t    opcode;
150 } __attribute__ ((packed)) evt_cmd_complete;
151 
152 typedef struct {
153     uint8_t     status;
154     uint8_t     ncmd;
155     uint16_t    opcode;
156 } __attribute__ ((packed)) evt_cmd_status;
157 
158 typedef struct {
159     uint16_t    opcode;
160     uint8_t     plen;
161 } __attribute__ ((packed))  hci_command_hdr;
162 
163 typedef struct {
164     uint8_t     evt;
165     uint8_t     plen;
166 } __attribute__ ((packed))  hci_event_hdr;
167 
168 /******************************************************************************
169 **  Extern
170 ******************************************************************************/
171 extern vnd_userial_cb_t vnd_userial;
172 
173 
174 /*******************************************************************************
175 **
176 ** Function        userial_vendor_init
177 **
178 ** Description     Initialize userial vendor-specific control block
179 **
180 ** Returns         None
181 **
182 *******************************************************************************/
183 void userial_vendor_init(void);
184 
185 /*******************************************************************************
186 **
187 ** Function        userial_vendor_open
188 **
189 ** Description     Open the serial port with the given configuration
190 **
191 ** Returns         device fd
192 **
193 *******************************************************************************/
194 int userial_vendor_open(tUSERIAL_CFG *p_cfg);
195 
196 /*******************************************************************************
197 **
198 ** Function        userial_vendor_close
199 **
200 ** Description     Conduct vendor-specific close work
201 **
202 ** Returns         None
203 **
204 *******************************************************************************/
205 void userial_vendor_close(void);
206 
207 /*******************************************************************************
208 **
209 ** Function        userial_vendor_set_baud
210 **
211 ** Description     Set new baud rate
212 **
213 ** Returns         None
214 **
215 *******************************************************************************/
216 void userial_vendor_set_baud(uint8_t userial_baud);
217 
218 /*******************************************************************************
219 **
220 ** Function        userial_vendor_ioctl
221 **
222 ** Description     ioctl inteface
223 **
224 ** Returns         int error
225 **
226 *******************************************************************************/
227 int userial_vendor_ioctl(userial_vendor_ioctl_op_t op, int *p_data);
228 
229 /*******************************************************************************
230 **
231 ** Function        userial_to_tcio_baud
232 **
233 ** Description     helper function converts USERIAL baud rates into TCIO
234 **                  conforming baud rates
235 **
236 ** Returns         TRUE/FALSE
237 **
238 *******************************************************************************/
239 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud);
240 
241 /*******************************************************************************
242 **
243 ** Function        userial_to_baud_tcio
244 **
245 ** Description     helper function converts TCIO baud rate into integer
246 **
247 ** Returns         uint32_t
248 **
249 *******************************************************************************/
250 int userial_tcio_baud_to_int(uint32_t baud);
251 
252 /*******************************************************************************
253 **
254 ** Function        read_hci_event
255 **
256 ** Description     Read HCI event during vendor initialization
257 **
258 ** Returns         int: size to read
259 **
260 *******************************************************************************/
261 int read_hci_event(int fd, unsigned char* buf, int size);
262 
263 #endif /* HCI_UART_H */
264