blob: 3142b7b48cb7b4a3a3f7628da8df9b3c067d6e7f [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/**
2 * TSIF driver
3 *
4 * Kernel API
5 *
6 * Copyright (c) 2009-2010, Code Aurora Forum. All rights
7 * reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 and
11 * only version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19#ifndef _TSIF_API_H_
20#define _TSIF_API_H_
21/**
22 * Theory of operation
23 *
24 * TSIF driver maintains internal cyclic data buffer where
25 * received TSIF packets are stored. Size of buffer, in packets,
26 * and its address, may be obtained by tsif_get_info().
27 *
28 * TSIF stream delivered to the client that should register with
29 * TSIF driver using tsif_attach()
30 *
31 * Producer-consumer pattern used. TSIF driver act as producer,
32 * writing data to the buffer; clientis consumer.
33 * 2 indexes maintained by the TSIF driver:
34 * - wi (write index) points to the next item to be written by
35 * TSIF
36 * - ri (read index) points to the next item available for read
37 * by the client.
38 * Write index advanced by the TSIF driver when new data
39 * received;
40 * Read index advanced only when client tell so to the TSIF
41 * driver by tsif_reclaim_packets()
42 *
43 * Consumer may directly access data TSIF buffer between ri and
44 * wi. When ri==wi, buffer is empty.
45 *
46 * TSIF driver notifies client about any change by calling
47 * notify function. Client should use tsif_get_state() to query
48 * new state.
49 */
50
51/* bytes in TSIF packet. not customizable */
52#define TSIF_PKT_SIZE (192)
53
54/**
55 * tsif_pkt_status - get TSIF packet status
56 *
57 * @pkt: TSIF packet location
58 *
59 * Return last DWORD of packet, containing status.
60 * Status dword consists of:
61 * - 3 low bytes TTS
62 * - 1 byte (last byte of packet) with status bits
63 */
64static inline u32 tsif_pkt_status(void *pkt)
65{
66 u32 *x = pkt;
67 return x[TSIF_PKT_SIZE / sizeof(u32) - 1];
68}
69
70/**
71 * Status dword parts for status returned by @tsif_pkt_status
72 */
73#define TSIF_STATUS_TTS(x) ((x) & 0xffffff)
74#define TSIF_STATUS_VALID(x) ((x) & (1<<24))
75#define TSIF_STATUS_FIRST(x) ((x) & (1<<25))
76#define TSIF_STATUS_OVFLW(x) ((x) & (1<<26))
77#define TSIF_STATUS_ERROR(x) ((x) & (1<<27))
78#define TSIF_STATUS_NULL(x) ((x) & (1<<28))
79#define TSIF_STATUS_TIMEO(x) ((x) & (1<<30))
80
81/**
82 * enum tsif_state - TSIF device state
83 * @tsif_state_stopped: Idle state, data acquisition not running
84 * @tsif_state_running: Data acquisition in progress
85 * @tsif_state_flushing: Device is flushing
86 *
87 * State transition diagram:
88 *
89 * init -> tsif_state_stopped
90 *
91 * tsif_state_stopped:
92 * - open -> tsif_state_running
93 *
94 * tsif_state_running:
95 * - close -> tsif_state_flushing
96 *
97 * tsif_state_flushing:
98 * - flushed -> tsif_state_stopped
99 */
100enum tsif_state {
101 tsif_state_stopped = 0,
102 tsif_state_running = 1,
103 tsif_state_flushing = 2,
104 tsif_state_error = 3,
105};
106
107/**
108 * tsif_attach - Attach to the device.
109 * @id: TSIF device ID, used to identify TSIF instance.
110 * @notify: client callback, called when
111 * any client visible TSIF state changed.
112 * This includes new data available and device state change
113 * @data: client data, will be passed to @notify
114 *
115 * Return TSIF cookie or error code
116 *
117 * Should be called prior to any other tsif_XXX function.
118 */
119void *tsif_attach(int id, void (*notify)(void *client_data), void *client_data);
120/**
121 * tsif_detach - detach from device
122 * @cookie: TSIF cookie previously obtained with tsif_attach()
123 */
124void tsif_detach(void *cookie);
125/**
126 * tsif_get_info - get data buffer info
127 * @cookie: TSIF cookie previously obtained with tsif_attach()
128 * @pdata: if not NULL, TSIF data buffer will be stored there
129 * @psize: if not NULL, TSIF data buffer size, in packets,
130 * will be stored there
131 *
132 * Data buffer information should be queried after each tsif_start() before
133 * using data; since data buffer will be re-allocated on tsif_start()
134 */
135void tsif_get_info(void *cookie, void **pdata, int *psize);
136/**
137 * tsif_set_mode - set TSIF mode
138 * @cookie: TSIF cookie previously obtained with tsif_attach()
139 * @mode: desired mode of operation
140 *
141 * Return error code
142 *
143 * Mode may be changed only when TSIF device is stopped.
144 */
145int tsif_set_mode(void *cookie, int mode);
146/**
147 * tsif_set_time_limit - set TSIF time limit
148 * @cookie: TSIF cookie previously obtained with tsif_attach()
149 * @value: desired time limit, 0 to disable
150 *
151 * Return error code
152 *
153 * Time limit may be changed only when TSIF device is stopped.
154 */
155int tsif_set_time_limit(void *cookie, u32 value);
156/**
157 * tsif_set_buf_config - configure data buffer
158 *
159 * @cookie: TSIF cookie previously obtained with tsif_attach()
160 * @pkts_in_chunk: requested number of packets per chunk
161 * @chunks_in_buf: requested number of chunks in buffer
162 *
163 * Return error code
164 *
165 * Parameter selection criteria:
166 *
167 * - @pkts_in_chunk defines size of DMA transfer and, in turn, time between
168 * consecutive DMA transfers. Increase @pkts_in_chunk reduces chance for
169 * hardware overflow. If TSIF stats reports overflows, increase it.
170 *
171 * - @chunks_in_buf * @pkts_in_chunk defines total buffer size. Increase this
172 * parameter if client latency is large and TSIF reports "soft drop" in its
173 * stats
174 */
175int tsif_set_buf_config(void *cookie, u32 pkts_in_chunk, u32 chunks_in_buf);
176/**
177 * tsif_get_state - query current data buffer information
178 * @cookie: TSIF cookie previously obtained with tsif_attach()
179 * @ri: if not NULL, read index will be stored here
180 * @wi: if not NULL, write index will be stored here
181 * @state: if not NULL, state will be stored here
182 */
183void tsif_get_state(void *cookie, int *ri, int *wi, enum tsif_state *state);
184/**
185 * tsif_start - start data acquisition
186 * @cookie: TSIF cookie previously obtained with tsif_attach()
187 *
188 * Return error code
189 */
190int tsif_start(void *cookie);
191/**
192 * tsif_stop - stop data acquisition
193 * @cookie: TSIF cookie previously obtained with tsif_attach()
194 *
195 * Data buffer allocated during this function call; thus client should
196 * query data buffer info using tsif_get_info() and reset its data pointers.
197 */
198void tsif_stop(void *cookie);
199/**
200 * tsif_reclaim_packets - inform that buffer space may be reclaimed
201 * @cookie: TSIF cookie previously obtained with tsif_attach()
202 * @ri: new value for read index
203 */
204void tsif_reclaim_packets(void *cookie, int ri);
205
206#endif /* _TSIF_API_H_ */
207