1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  Shared Transport Header file
4  *	To be included by the protocol stack drivers for
5  *	Texas Instruments BT,FM and GPS combo chip drivers
6  *	and also serves the sub-modules of the shared transport driver.
7  *
8  *  Copyright (C) 2009-2010 Texas Instruments
9  *  Author: Pavan Savoy <pavan_savoy@ti.com>
10  */
11 
12 #ifndef TI_WILINK_ST_H
13 #define TI_WILINK_ST_H
14 
15 #include <linux/skbuff.h>
16 
17 /**
18  * enum proto-type - The protocol on WiLink chips which share a
19  *	common physical interface like UART.
20  */
21 enum proto_type {
22 	ST_BT,
23 	ST_FM,
24 	ST_GPS,
25 	ST_MAX_CHANNELS = 16,
26 };
27 
28 /**
29  * struct st_proto_s - Per Protocol structure from BT/FM/GPS to ST
30  * @type: type of the protocol being registered among the
31  *	available proto_type(BT, FM, GPS the protocol which share TTY).
32  * @recv: the receiver callback pointing to a function in the
33  *	protocol drivers called by the ST driver upon receiving
34  *	relevant data.
35  * @match_packet: reserved for future use, to make ST more generic
36  * @reg_complete_cb: callback handler pointing to a function in protocol
37  *	handler called by ST when the pending registrations are complete.
38  *	The registrations are marked pending, in situations when fw
39  *	download is in progress.
40  * @write: pointer to function in ST provided to protocol drivers from ST,
41  *	to be made use when protocol drivers have data to send to TTY.
42  * @priv_data: privdate data holder for the protocol drivers, sent
43  *	from the protocol drivers during registration, and sent back on
44  *	reg_complete_cb and recv.
45  * @chnl_id: channel id the protocol driver is interested in, the channel
46  *	id is nothing but the 1st byte of the packet in UART frame.
47  * @max_frame_size: size of the largest frame the protocol can receive.
48  * @hdr_len: length of the header structure of the protocol.
49  * @offset_len_in_hdr: this provides the offset of the length field in the
50  *	header structure of the protocol header, to assist ST to know
51  *	how much to receive, if the data is split across UART frames.
52  * @len_size: whether the length field inside the header is 2 bytes
53  *	or 1 byte.
54  * @reserve: the number of bytes ST needs to reserve in the skb being
55  *	prepared for the protocol driver.
56  */
57 struct st_proto_s {
58 	enum proto_type type;
59 	long (*recv) (void *, struct sk_buff *);
60 	unsigned char (*match_packet) (const unsigned char *data);
61 	void (*reg_complete_cb) (void *, int data);
62 	long (*write) (struct sk_buff *skb);
63 	void *priv_data;
64 
65 	unsigned char chnl_id;
66 	unsigned short max_frame_size;
67 	unsigned char hdr_len;
68 	unsigned char offset_len_in_hdr;
69 	unsigned char len_size;
70 	unsigned char reserve;
71 };
72 
73 extern long st_register(struct st_proto_s *);
74 extern long st_unregister(struct st_proto_s *);
75 
76 
77 /*
78  * header information used by st_core.c
79  */
80 
81 /* states of protocol list */
82 #define ST_NOTEMPTY	1
83 #define ST_EMPTY	0
84 
85 /*
86  * possible st_states
87  */
88 #define ST_INITIALIZING		1
89 #define ST_REG_IN_PROGRESS	2
90 #define ST_REG_PENDING		3
91 #define ST_WAITING_FOR_RESP	4
92 
93 /**
94  * struct st_data_s - ST core internal structure
95  * @st_state: different states of ST like initializing, registration
96  *	in progress, this is mainly used to return relevant err codes
97  *	when protocol drivers are registering. It is also used to track
98  *	the recv function, as in during fw download only HCI events
99  *	can occur , where as during other times other events CH8, CH9
100  *	can occur.
101  * @tty: tty provided by the TTY core for line disciplines.
102  * @tx_skb: If for some reason the tty's write returns lesser bytes written
103  *	then to maintain the rest of data to be written on next instance.
104  *	This needs to be protected, hence the lock inside wakeup func.
105  * @tx_state: if the data is being written onto the TTY and protocol driver
106  *	wants to send more, queue up data and mark that there is
107  *	more data to send.
108  * @list: the list of protocols registered, only MAX can exist, one protocol
109  *	can register only once.
110  * @rx_state: states to be maintained inside st's tty receive
111  * @rx_count: count to be maintained inside st's tty receieve
112  * @rx_skb: the skb where all data for a protocol gets accumulated,
113  *	since tty might not call receive when a complete event packet
114  *	is received, the states, count and the skb needs to be maintained.
115  * @rx_chnl: the channel ID for which the data is getting accumalated for.
116  * @txq: the list of skbs which needs to be sent onto the TTY.
117  * @tx_waitq: if the chip is not in AWAKE state, the skbs needs to be queued
118  *	up in here, PM(WAKEUP_IND) data needs to be sent and then the skbs
119  *	from waitq can be moved onto the txq.
120  *	Needs locking too.
121  * @lock: the lock to protect skbs, queues, and ST states.
122  * @protos_registered: count of the protocols registered, also when 0 the
123  *	chip enable gpio can be toggled, and when it changes to 1 the fw
124  *	needs to be downloaded to initialize chip side ST.
125  * @ll_state: the various PM states the chip can be, the states are notified
126  *	to us, when the chip sends relevant PM packets(SLEEP_IND, WAKE_IND).
127  * @kim_data: reference to the parent encapsulating structure.
128  *
129  */
130 struct st_data_s {
131 	unsigned long st_state;
132 	struct sk_buff *tx_skb;
133 #define ST_TX_SENDING	1
134 #define ST_TX_WAKEUP	2
135 	unsigned long tx_state;
136 	struct st_proto_s *list[ST_MAX_CHANNELS];
137 	bool is_registered[ST_MAX_CHANNELS];
138 	unsigned long rx_state;
139 	unsigned long rx_count;
140 	struct sk_buff *rx_skb;
141 	unsigned char rx_chnl;
142 	struct sk_buff_head txq, tx_waitq;
143 	spinlock_t lock;
144 	unsigned char	protos_registered;
145 	unsigned long ll_state;
146 	void *kim_data;
147 	struct tty_struct *tty;
148 	struct work_struct work_write_wakeup;
149 };
150 
151 /*
152  * wrapper around tty->ops->write_room to check
153  * availability during firmware download
154  */
155 int st_get_uart_wr_room(struct st_data_s *st_gdata);
156 /**
157  * st_int_write -
158  * point this to tty->driver->write or tty->ops->write
159  * depending upon the kernel version
160  */
161 int st_int_write(struct st_data_s*, const unsigned char*, int);
162 
163 /**
164  * st_write -
165  * internal write function, passed onto protocol drivers
166  * via the write function ptr of protocol struct
167  */
168 long st_write(struct sk_buff *);
169 
170 /* function to be called from ST-LL */
171 void st_ll_send_frame(enum proto_type, struct sk_buff *);
172 
173 /* internal wake up function */
174 void st_tx_wakeup(struct st_data_s *st_data);
175 
176 /* init, exit entry funcs called from KIM */
177 int st_core_init(struct st_data_s **);
178 void st_core_exit(struct st_data_s *);
179 
180 /* ask for reference from KIM */
181 void st_kim_ref(struct st_data_s **, int);
182 
183 #define GPS_STUB_TEST
184 #ifdef GPS_STUB_TEST
185 int gps_chrdrv_stub_write(const unsigned char*, int);
186 void gps_chrdrv_stub_init(void);
187 #endif
188 
189 /*
190  * header information used by st_kim.c
191  */
192 
193 /* time in msec to wait for
194  * line discipline to be installed
195  */
196 #define LDISC_TIME	1000
197 #define CMD_RESP_TIME	800
198 #define CMD_WR_TIME	5000
199 #define MAKEWORD(a, b)  ((unsigned short)(((unsigned char)(a)) \
200 	| ((unsigned short)((unsigned char)(b))) << 8))
201 
202 #define GPIO_HIGH 1
203 #define GPIO_LOW  0
204 
205 /* the Power-On-Reset logic, requires to attempt
206  * to download firmware onto chip more than once
207  * since the self-test for chip takes a while
208  */
209 #define POR_RETRY_COUNT 5
210 
211 /**
212  * struct chip_version - save the chip version
213  */
214 struct chip_version {
215 	unsigned short full;
216 	unsigned short chip;
217 	unsigned short min_ver;
218 	unsigned short maj_ver;
219 };
220 
221 #define UART_DEV_NAME_LEN 32
222 /**
223  * struct kim_data_s - the KIM internal data, embedded as the
224  *	platform's drv data. One for each ST device in the system.
225  * @uim_pid: KIM needs to communicate with UIM to request to install
226  *	the ldisc by opening UART when protocol drivers register.
227  * @kim_pdev: the platform device added in one of the board-XX.c file
228  *	in arch/XX/ directory, 1 for each ST device.
229  * @kim_rcvd: completion handler to notify when data was received,
230  *	mainly used during fw download, which involves multiple send/wait
231  *	for each of the HCI-VS commands.
232  * @ldisc_installed: completion handler to notify that the UIM accepted
233  *	the request to install ldisc, notify from tty_open which suggests
234  *	the ldisc was properly installed.
235  * @resp_buffer: data buffer for the .bts fw file name.
236  * @fw_entry: firmware class struct to request/release the fw.
237  * @rx_state: the rx state for kim's receive func during fw download.
238  * @rx_count: the rx count for the kim's receive func during fw download.
239  * @rx_skb: all of fw data might not come at once, and hence data storage for
240  *	whole of the fw response, only HCI_EVENTs and hence diff from ST's
241  *	response.
242  * @core_data: ST core's data, which mainly is the tty's disc_data
243  * @version: chip version available via a sysfs entry.
244  *
245  */
246 struct kim_data_s {
247 	long uim_pid;
248 	struct platform_device *kim_pdev;
249 	struct completion kim_rcvd, ldisc_installed;
250 	char resp_buffer[30];
251 	const struct firmware *fw_entry;
252 	unsigned nshutdown;
253 	unsigned long rx_state;
254 	unsigned long rx_count;
255 	struct sk_buff *rx_skb;
256 	struct st_data_s *core_data;
257 	struct chip_version version;
258 	unsigned char ldisc_install;
259 	unsigned char dev_name[UART_DEV_NAME_LEN + 1];
260 	unsigned flow_cntrl;
261 	unsigned baud_rate;
262 };
263 
264 /**
265  * functions called when 1 of the protocol drivers gets
266  * registered, these need to communicate with UIM to request
267  * ldisc installed, read chip_version, download relevant fw
268  */
269 long st_kim_start(void *);
270 long st_kim_stop(void *);
271 
272 void st_kim_complete(void *);
273 void kim_st_list_protocols(struct st_data_s *, void *);
274 void st_kim_recv(void *, const unsigned char *, long);
275 
276 
277 /*
278  * BTS headers
279  */
280 #define ACTION_SEND_COMMAND     1
281 #define ACTION_WAIT_EVENT       2
282 #define ACTION_SERIAL           3
283 #define ACTION_DELAY            4
284 #define ACTION_RUN_SCRIPT       5
285 #define ACTION_REMARKS          6
286 
287 /**
288  * struct bts_header - the fw file is NOT binary which can
289  *	be sent onto TTY as is. The .bts is more a script
290  *	file which has different types of actions.
291  *	Each such action needs to be parsed by the KIM and
292  *	relevant procedure to be called.
293  */
294 struct bts_header {
295 	u32 magic;
296 	u32 version;
297 	u8 future[24];
298 	u8 actions[];
299 } __attribute__ ((packed));
300 
301 /**
302  * struct bts_action - Each .bts action has its own type of
303  *	data.
304  */
305 struct bts_action {
306 	u16 type;
307 	u16 size;
308 	u8 data[];
309 } __attribute__ ((packed));
310 
311 struct bts_action_send {
312 	u8 data[0];
313 } __attribute__ ((packed));
314 
315 struct bts_action_wait {
316 	u32 msec;
317 	u32 size;
318 	u8 data[];
319 } __attribute__ ((packed));
320 
321 struct bts_action_delay {
322 	u32 msec;
323 } __attribute__ ((packed));
324 
325 struct bts_action_serial {
326 	u32 baud;
327 	u32 flow_control;
328 } __attribute__ ((packed));
329 
330 /**
331  * struct hci_command - the HCI-VS for intrepreting
332  *	the change baud rate of host-side UART, which
333  *	needs to be ignored, since UIM would do that
334  *	when it receives request from KIM for ldisc installation.
335  */
336 struct hci_command {
337 	u8 prefix;
338 	u16 opcode;
339 	u8 plen;
340 	u32 speed;
341 } __attribute__ ((packed));
342 
343 /*
344  * header information used by st_ll.c
345  */
346 
347 /* ST LL receiver states */
348 #define ST_W4_PACKET_TYPE       0
349 #define ST_W4_HEADER		1
350 #define ST_W4_DATA		2
351 
352 /* ST LL state machines */
353 #define ST_LL_ASLEEP               0
354 #define ST_LL_ASLEEP_TO_AWAKE      1
355 #define ST_LL_AWAKE                2
356 #define ST_LL_AWAKE_TO_ASLEEP      3
357 #define ST_LL_INVALID		   4
358 
359 /* different PM notifications coming from chip */
360 #define LL_SLEEP_IND	0x30
361 #define LL_SLEEP_ACK	0x31
362 #define LL_WAKE_UP_IND	0x32
363 #define LL_WAKE_UP_ACK	0x33
364 
365 /* initialize and de-init ST LL */
366 long st_ll_init(struct st_data_s *);
367 long st_ll_deinit(struct st_data_s *);
368 
369 /**
370  * enable/disable ST LL along with KIM start/stop
371  * called by ST Core
372  */
373 void st_ll_enable(struct st_data_s *);
374 void st_ll_disable(struct st_data_s *);
375 
376 /**
377  * various funcs used by ST core to set/get the various PM states
378  * of the chip.
379  */
380 unsigned long st_ll_getstate(struct st_data_s *);
381 unsigned long st_ll_sleep_state(struct st_data_s *, unsigned char);
382 void st_ll_wakeup(struct st_data_s *);
383 
384 /*
385  * header information used by st_core.c for FM and GPS
386  * packet parsing, the bluetooth headers are already available
387  * at net/bluetooth/
388  */
389 
390 struct fm_event_hdr {
391 	u8 plen;
392 } __attribute__ ((packed));
393 
394 #define FM_MAX_FRAME_SIZE 0xFF	/* TODO: */
395 #define FM_EVENT_HDR_SIZE 1	/* size of fm_event_hdr */
396 #define ST_FM_CH8_PKT 0x8
397 
398 /* gps stuff */
399 struct gps_event_hdr {
400 	u8 opcode;
401 	u16 plen;
402 } __attribute__ ((packed));
403 
404 /**
405  * struct ti_st_plat_data - platform data shared between ST driver and
406  *	platform specific board file which adds the ST device.
407  * @nshutdown_gpio: Host's GPIO line to which chip's BT_EN is connected.
408  * @dev_name: The UART/TTY name to which chip is interfaced. (eg: /dev/ttyS1)
409  * @flow_cntrl: Should always be 1, since UART's CTS/RTS is used for PM
410  *	purposes.
411  * @baud_rate: The baud rate supported by the Host UART controller, this will
412  *	be shared across with the chip via a HCI VS command from User-Space Init
413  *	Mgr application.
414  * @suspend:
415  * @resume: legacy PM routines hooked to platform specific board file, so as
416  *	to take chip-host interface specific action.
417  * @chip_enable:
418  * @chip_disable: Platform/Interface specific mux mode setting, GPIO
419  *	configuring, Host side PM disabling etc.. can be done here.
420  * @chip_asleep:
421  * @chip_awake: Chip specific deep sleep states is communicated to Host
422  *	specific board-xx.c to take actions such as cut UART clocks when chip
423  *	asleep or run host faster when chip awake etc..
424  *
425  */
426 struct ti_st_plat_data {
427 	u32 nshutdown_gpio;
428 	unsigned char dev_name[UART_DEV_NAME_LEN]; /* uart name */
429 	u32 flow_cntrl; /* flow control flag */
430 	u32 baud_rate;
431 	int (*suspend)(struct platform_device *, pm_message_t);
432 	int (*resume)(struct platform_device *);
433 	int (*chip_enable) (struct kim_data_s *);
434 	int (*chip_disable) (struct kim_data_s *);
435 	int (*chip_asleep) (struct kim_data_s *);
436 	int (*chip_awake) (struct kim_data_s *);
437 };
438 
439 #endif /* TI_WILINK_ST_H */
440