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