1 /*
2 * ALSA lib - local header file
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4 *
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #ifndef __LOCAL_H
23 #define __LOCAL_H
24
25 #include "config.h"
26
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <assert.h>
33 #ifdef HAVE_ENDIAN_H
34 #include <endian.h>
35 #elif defined(HAVE_SYS_ENDIAN_H)
36 #include <sys/endian.h>
37 #ifndef __BYTE_ORDER
38 #define __BYTE_ORDER BYTE_ORDER
39 #endif
40 #ifndef __LITTLE_ENDIAN
41 #define __LITTLE_ENDIAN LITTLE_ENDIAN
42 #endif
43 #ifndef __BIG_ENDIAN
44 #define __BIG_ENDIAN BIG_ENDIAN
45 #endif
46 #else
47 #error Header defining endianness not defined
48 #endif
49 #include <stdarg.h>
50 #include <poll.h>
51 #include <sys/types.h>
52 #include <errno.h>
53 #if defined(__linux__)
54 #include <linux/types.h>
55 #include <linux/ioctl.h>
56 #else
57 #include "type_compat.h"
58 #endif
59
60 #ifdef SUPPORT_RESMGR
61 #include <resmgr.h>
62 #endif
63 #ifdef HAVE_LIBDL
64 #include <dlfcn.h>
65 #else
66 #define RTLD_NOW 0
67 #endif
68
69 #if __BYTE_ORDER == __LITTLE_ENDIAN
70 #define SND_LITTLE_ENDIAN
71 #define SNDRV_LITTLE_ENDIAN
72 #elif __BYTE_ORDER == __BIG_ENDIAN
73 #define SND_BIG_ENDIAN
74 #define SNDRV_BIG_ENDIAN
75 #else
76 #error "Unsupported endian..."
77 #endif
78
79 #define _snd_config_iterator list_head
80 #define _snd_interval snd_interval
81 #define _snd_pcm_info snd_pcm_info
82 #define _snd_pcm_hw_params snd_pcm_hw_params
83 #define _snd_pcm_sw_params snd_pcm_sw_params
84 #define _snd_pcm_status snd_pcm_status
85
86 #define _snd_ctl_card_info snd_ctl_card_info
87 #define _snd_ctl_elem_id snd_ctl_elem_id
88 #define _snd_ctl_elem_list snd_ctl_elem_list
89 #define _snd_ctl_elem_info snd_ctl_elem_info
90 #define _snd_ctl_elem_value snd_ctl_elem_value
91 #define _snd_ctl_event snd_ctl_event
92
93 #define _snd_rawmidi_info snd_rawmidi_info
94 #define _snd_rawmidi_params snd_rawmidi_params
95 #define _snd_rawmidi_status snd_rawmidi_status
96
97 #define _snd_hwdep_info snd_hwdep_info
98 #define _snd_hwdep_dsp_status snd_hwdep_dsp_status
99 #define _snd_hwdep_dsp_image snd_hwdep_dsp_image
100
101 #define _snd_seq_queue_tempo snd_seq_queue_tempo
102 #define _snd_seq_client_info snd_seq_client_info
103 #define _snd_seq_port_info snd_seq_port_info
104 #define _snd_seq_system_info snd_seq_system_info
105 #define _snd_seq_queue_info snd_seq_queue_info
106 #define _snd_seq_queue_status snd_seq_queue_status
107 #define _snd_seq_queue_timer snd_seq_queue_timer
108 #define _snd_seq_port_subscribe snd_seq_port_subscribe
109 #define _snd_seq_query_subscribe snd_seq_query_subs
110 #define _snd_seq_client_pool snd_seq_client_pool
111 #define _snd_seq_remove_events snd_seq_remove_events
112
113 #define _snd_timer_id snd_timer_id
114 #define _snd_timer_ginfo snd_timer_ginfo
115 #define _snd_timer_gparams snd_timer_gparams
116 #define _snd_timer_gstatus snd_timer_gstatus
117 #define _snd_timer_select snd_timer_select
118 #define _snd_timer_info snd_timer_info
119 #define _snd_timer_params snd_timer_params
120 #define _snd_timer_status snd_timer_status
121
122 #define ALSA_LIBRARY_BUILD
123
124 /* rename some types for avoiding conflicts with alsalib's definitions */
125 #define snd_aes_iec958 sndrv_aes_iec958
126 #define snd_pcm_uframes_t sndrv_pcm_uframes_t
127 #define snd_pcm_sframes_t sndrv_pcm_sframes_t
128 #define snd_pcm_access_t sndrv_pcm_access_t
129 #define snd_pcm_format_t sndrv_pcm_format_t
130 #define snd_pcm_subformat_t sndrv_pcm_subformat_t
131 #define snd_pcm_state_t sndrv_pcm_state_t
132 #define snd_interval sndrv_interval
133 #define snd_mask sndrv_mask
134 #define snd_ctl_elem_type_t sndrv_ctl_elem_type_t
135 #define snd_ctl_elem_iface_t sndrv_ctl_elem_iface_t
136 #define snd_ctl_tlv sndrv_ctl_tlv
137
138 /* kill and replace kernel-specific types */
139 #ifndef __user
140 #define __user
141 #endif
142 #ifndef __force
143 #define __force
144 #endif
145
146 #include <sound/asound.h>
147
148 /* take back superfluous renames; some can be kept as is */
149 #undef snd_aes_iec958
150 #undef snd_pcm_uframes_t
151 #undef snd_pcm_sframes_t
152 #undef snd_pcm_access_t
153 #undef snd_pcm_format_t
154 #undef snd_pcm_subformat_t
155 #undef snd_pcm_state_t
156 #undef snd_ctl_elem_type_t
157 #undef snd_ctl_elem_iface_t
158
159 #include "asoundef.h"
160 #include "alsa-symbols.h"
161 #include "version.h"
162 #include "global.h"
163 #include "input.h"
164 #include "output.h"
165 #include "error.h"
166 #include "conf.h"
167 #include "pcm.h"
168 #include "pcm_plugin.h"
169 #include "rawmidi.h"
170 #include "timer.h"
171 #include "hwdep.h"
172 #include "control.h"
173 #include "mixer.h"
174 #include "seq_event.h"
175 #include "seq.h"
176
177 /* rename some types for avoiding conflicts with alsalib's definitions */
178 #define snd_seq_addr sndrv_seq_addr
179 #define snd_seq_tick_time_t sndrv_seq_tick_time_t
180 #define snd_seq_real_time sndrv_seq_real_time
181 #define snd_seq_timestamp sndrv_seq_timestamp
182 #define snd_seq_event_type_t sndrv_seq_event_type_t
183 #define snd_seq_event sndrv_seq_event
184 #define snd_seq_connect sndrv_seq_connect
185 #define snd_seq_ev_note sndrv_seq_ev_note
186 #define snd_seq_ev_ctrl sndrv_seq_ev_ctrl
187 #define snd_seq_ev_raw8 sndrv_seq_ev_raw8
188 #define snd_seq_ev_raw32 sndrv_seq_ev_raw32
189 #define snd_seq_ev_ext sndrv_seq_ev_ext
190 #define snd_seq_result sndrv_seq_result
191 #define snd_seq_queue_skew sndrv_seq_queue_skew
192 #define snd_seq_ev_queue_control sndrv_seq_ev_queue_control
193 #define snd_seq_client_t sndrv_seq_client_t
194 #define snd_seq_client_type_t sndrv_seq_client_type_t
195
196 #include <sound/asequencer.h>
197
198 /* take back some renames */
199 #undef snd_seq_client_t
200 #undef snd_seq_client_type_t
201
202 #include "seqmid.h"
203 #include "seq_midi_event.h"
204 #include "list.h"
205
206 struct _snd_async_handler {
207 enum {
208 SND_ASYNC_HANDLER_GENERIC,
209 SND_ASYNC_HANDLER_CTL,
210 SND_ASYNC_HANDLER_PCM,
211 SND_ASYNC_HANDLER_TIMER,
212 } type;
213 int fd;
214 union {
215 snd_ctl_t *ctl;
216 snd_pcm_t *pcm;
217 snd_timer_t *timer;
218 } u;
219 snd_async_callback_t callback;
220 void *private_data;
221 struct list_head glist;
222 struct list_head hlist;
223 };
224
225 typedef enum _snd_set_mode {
226 SND_CHANGE,
227 SND_TRY,
228 SND_TEST,
229 } snd_set_mode_t;
230
231 size_t page_align(size_t size);
232 size_t page_size(void);
233 size_t page_ptr(size_t object_offset, size_t object_size, size_t *offset, size_t *mmap_offset);
234
235 int safe_strtoll_base(const char *str, long long *val, int base);
safe_strtoll(const char * str,long long * val)236 static inline int safe_strtoll(const char *str, long long *val) { return safe_strtoll_base(str, val, 0); }
237 int safe_strtol_base(const char *str, long *val, int base);
safe_strtol(const char * str,long * val)238 static inline int safe_strtol(const char *str, long *val) { return safe_strtol_base(str, val, 0); }
239
240 int snd_send_fd(int sock, void *data, size_t len, int fd);
241 int snd_receive_fd(int sock, void *data, size_t len, int *fd);
242 size_t snd_strlcpy(char *dst, const char *src, size_t size);
243
244 /*
245 * error messages
246 */
247 #ifndef NDEBUG
248 #define CHECK_SANITY(x) x
249 extern snd_lib_error_handler_t snd_err_msg;
250 #define SNDMSG(args...) snd_err_msg(__FILE__, __LINE__, __func__, 0, ##args)
251 #define SYSMSG(args...) snd_err_msg(__FILE__, __LINE__, __func__, errno, ##args)
252 #else
253 #define CHECK_SANITY(x) 0 /* not evaluated */
254 #define SNDMSG(args...) /* nop */
255 #define SYSMSG(args...) /* nop */
256 #endif
257
258 /*
259 */
260 #define HAVE_GNU_LD
261 #define HAVE_ELF
262 #define HAVE_ASM_PREVIOUS_DIRECTIVE
263
264 /* Stolen from libc-symbols.h in GNU glibc */
265
266 /* When a reference to SYMBOL is encountered, the linker will emit a
267 warning message MSG. */
268
269 #define ASM_NAME(name) __SYMBOL_PREFIX name
270
271 #ifdef HAVE_GNU_LD
272 # ifdef HAVE_ELF
273
274 /* We want the .gnu.warning.SYMBOL section to be unallocated. */
275 # ifdef HAVE_ASM_PREVIOUS_DIRECTIVE
276 # define __make_section_unallocated(section_string) \
277 asm (".section " section_string "\n\t.previous");
278 # elif defined HAVE_ASM_POPSECTION_DIRECTIVE
279 # define __make_section_unallocated(section_string) \
280 asm (".pushsection " section_string "\n\t.popsection");
281 # else
282 # define __make_section_unallocated(section_string)
283 # endif
284
285 /* Tacking on "\n\t#" to the section name makes gcc put it's bogus
286 section attributes on what looks like a comment to the assembler. */
287 # ifdef HAVE_SECTION_QUOTES
288 # define link_warning(symbol, msg) \
289 __make_section_unallocated (".gnu.warning." ASM_NAME(#symbol)) \
290 static const char __evoke_link_warning_##symbol[] \
291 __attribute__ ((section (".gnu.warning." ASM_NAME(#symbol) "\"\n\t#\""))) = msg;
292 # else
293 # define link_warning(symbol, msg) \
294 __make_section_unallocated (".gnu.warning." ASM_NAME(#symbol)) \
295 static const char __evoke_link_warning_##symbol[] \
296 __attribute__ ((section (".gnu.warning." ASM_NAME(#symbol) "\n\t#"))) = msg;
297 # endif
298 # else
299 # define link_warning(symbol, msg) \
300 asm (".stabs \"" msg "\",30,0,0,0\n\t" \
301 ".stabs \"" ASM_NAME(#symbol) "\",1,0,0,0\n");
302 # endif
303 #else
304 /* We will never be heard; they will all die horribly. */
305 # define link_warning(symbol, msg)
306 #endif
307
snd_open_device(const char * filename,int fmode)308 static inline int snd_open_device(const char *filename, int fmode)
309 {
310 int fd;
311
312 #ifdef O_CLOEXEC
313 fmode |= O_CLOEXEC;
314 #endif
315 fd = open(filename, fmode);
316
317 /* open with resmgr */
318 #ifdef SUPPORT_RESMGR
319 if (fd < 0) {
320 if (errno == EAGAIN || errno == EBUSY)
321 return fd;
322 if (! access(filename, F_OK))
323 fd = rsm_open_device(filename, fmode);
324 }
325 #endif
326 #ifndef O_CLOEXEC
327 if (fd >= 0)
328 fcntl(fd, F_SETFD, FD_CLOEXEC);
329 #endif
330 return fd;
331 }
332
333 /* make local functions really local */
334 #define snd_dlobj_cache_get \
335 snd1_dlobj_cache_get
336 #define snd_dlobj_cache_get2 \
337 snd1_dlobj_cache_get2
338 #define snd_dlobj_cache_put \
339 snd1_dlobj_cache_put
340 #define snd_dlobj_cache_cleanup \
341 snd1_dlobj_cache_cleanup
342 #define snd_config_set_hop \
343 snd1_config_set_hop
344 #define snd_config_check_hop \
345 snd1_config_check_hop
346 #define snd_config_search_alias_hooks \
347 snd1_config_search_alias_hooks
348
349 /* dlobj cache */
350 void *snd_dlobj_cache_get(const char *lib, const char *name, const char *version, int verbose);
351 void *snd_dlobj_cache_get2(const char *lib, const char *name, const char *version, int verbose);
352 int snd_dlobj_cache_put(void *open_func);
353 void snd_dlobj_cache_cleanup(void);
354
355 /* for recursive checks */
356 void snd_config_set_hop(snd_config_t *conf, int hop);
357 int snd_config_check_hop(snd_config_t *conf);
358 #define SND_CONF_MAX_HOPS 64
359
360 int snd_config_search_alias_hooks(snd_config_t *config,
361 const char *base, const char *key,
362 snd_config_t **result);
363
364 int _snd_conf_generic_id(const char *id);
365
366 int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
367 int override, const char * const *default_include_path);
368
369 /* convenience macros */
370 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
371
372 #define container_of(ptr, type, member) ({ \
373 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
374 (type *)( (char *)__mptr - offsetof(type,member) );})
375
376 #ifdef INTERNAL
377 void *INTERNAL(snd_dlopen)(const char *name, int mode, char *errbuf, size_t errbuflen);
378 #endif
379
380 #ifdef BUILD_UCM
381
382 const char *uc_mgr_alibcfg_by_device(snd_config_t **config, const char *name);
383
_snd_is_ucm_device(const char * name)384 static inline int _snd_is_ucm_device(const char *name)
385 {
386 return name && name[0] == '_' && name[1] == 'u' && name[2] == 'c' && name[3] == 'm';
387 }
388
389 #else
390
uc_mgr_alibcfg_by_device(snd_config_t ** config,const char * name)391 static inline const char *uc_mgr_alibcfg_by_device(snd_config_t **config, const char *name) { return NULL; }
_snd_is_ucm_device(const char * name)392 static inline int _snd_is_ucm_device(const char *name) { return 0; }
393
394
395 #endif
396
397 #endif
398