1 /* plugin.h 2 ** Copyright (c) 2019-2020, The Linux Foundation. 3 ** 4 ** Redistribution and use in source and binary forms, with or without 5 ** modification, are permitted provided that the following conditions are 6 ** met: 7 ** * Redistributions of source code must retain the above copyright 8 ** notice, this list of conditions and the following disclaimer. 9 ** * Redistributions in binary form must reproduce the above 10 ** copyright notice, this list of conditions and the following 11 ** disclaimer in the documentation and/or other materials provided 12 ** with the distribution. 13 ** * Neither the name of The Linux Foundation nor the names of its 14 ** contributors may be used to endorse or promote products derived 15 ** from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **/ 29 30 #ifndef TINYALSA_PLUGIN_H 31 #define TINYALSA_PLUGIN_H 32 33 #include <poll.h> 34 #include <stdint.h> 35 #include <stdlib.h> 36 #include <sys/types.h> 37 #include <time.h> 38 39 #include <sound/asound.h> 40 41 /* static initializers */ 42 43 #define SND_VALUE_ENUM(etexts, eitems) \ 44 {.texts = etexts, .items = eitems} 45 46 #define SND_VALUE_BYTES(csize) \ 47 {.size = csize } 48 49 #define SND_VALUE_INTEGER(icount, imin, imax, istep) \ 50 {.count = icount, .min = imin, .max = imax, .step = istep } 51 52 #define SND_VALUE_TLV_BYTES(csize, cget, cput) \ 53 {.size = csize, .get = cget, .put = cput } 54 55 /* pointer based initializers */ 56 #define INIT_SND_CONTROL_INTEGER(c, cname, cget, cput, cint, pval, pdata) \ 57 { \ 58 c->iface = SNDRV_CTL_ELEM_IFACE_MIXER; \ 59 c->access = SNDRV_CTL_ELEM_ACCESS_READWRITE; \ 60 c->type = SNDRV_CTL_ELEM_TYPE_INTEGER; \ 61 c->name = cname; c->value = &cint; c->get = cget; c->put = cput; \ 62 c->private_value = pval; c->private_data = pdata; \ 63 } 64 65 #define INIT_SND_CONTROL_BYTES(c, cname, cget, cput, cint, pval, pdata) \ 66 { \ 67 c->iface = SNDRV_CTL_ELEM_IFACE_MIXER; \ 68 c->access = SNDRV_CTL_ELEM_ACCESS_READWRITE; \ 69 c->type = SNDRV_CTL_ELEM_TYPE_BYTES; \ 70 c->name = cname; c->value = &cint; c->get = cget; c->put = cput; \ 71 c->private_value = pval; c->private_data = pdata; \ 72 } 73 74 #define INIT_SND_CONTROL_ENUM(c, cname, cget, cput, cenum, pval, pdata) \ 75 { \ 76 c->iface = SNDRV_CTL_ELEM_IFACE_MIXER; \ 77 c->access = SNDRV_CTL_ELEM_ACCESS_READWRITE; \ 78 c->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; \ 79 c->name = cname; c->value = cenum; c->get = cget; c->put = cput; \ 80 c->private_value = pval; c->private_data = pdata; \ 81 } 82 83 #define INIT_SND_CONTROL_TLV_BYTES(c, cname, cbytes, priv_val, priv_data) \ 84 { \ 85 c->iface = SNDRV_CTL_ELEM_IFACE_MIXER; \ 86 c->access = SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE; \ 87 c->type = SNDRV_CTL_ELEM_TYPE_BYTES; \ 88 c->name = cname; c->value = &cbytes; \ 89 c->private_value = priv_val; c->private_data = priv_data; \ 90 } 91 92 struct mixer_plugin; 93 struct pcm_plugin; 94 struct snd_node; 95 96 /** Operations that are required to be registered by the plugin. 97 * @ingroup libtinyalsa-pcm 98 */ 99 struct pcm_plugin_ops { 100 /** open the pcm plugin */ 101 int (*open) (struct pcm_plugin **plugin, unsigned int card, 102 unsigned int device, unsigned int flags); 103 /** close the pcm plugin */ 104 int (*close) (struct pcm_plugin *plugin); 105 /** Set the PCM hardware parameters to the plugin */ 106 int (*hw_params) (struct pcm_plugin *plugin, 107 struct snd_pcm_hw_params *params); 108 /** Set the PCM software parameters to the plugin */ 109 int (*sw_params) (struct pcm_plugin *plugin, 110 struct snd_pcm_sw_params *params); 111 /** Synchronize the pointer */ 112 int (*sync_ptr) (struct pcm_plugin *plugin, 113 struct snd_pcm_sync_ptr *sync_ptr); 114 /** Write frames to plugin to be rendered to output */ 115 int (*writei_frames) (struct pcm_plugin *plugin, 116 struct snd_xferi *x); 117 /** Read frames from plugin captured from input */ 118 int (*readi_frames) (struct pcm_plugin *plugin, 119 struct snd_xferi *x); 120 /** Obtain the timestamp for the PCM */ 121 int (*ttstamp) (struct pcm_plugin *plugin, 122 int *tstamp); 123 /** Prepare the plugin for data transfer */ 124 int (*prepare) (struct pcm_plugin *plugin); 125 /** Start data transfer from/to the plugin */ 126 int (*start) (struct pcm_plugin *plugin); 127 /** Drop pcm frames */ 128 int (*drop) (struct pcm_plugin *plugin); 129 /** Any custom or alsa specific ioctl implementation */ 130 int (*ioctl) (struct pcm_plugin *plugin, 131 int cmd, void *arg); 132 void *(*mmap) (struct pcm_plugin *plugin, void *addr, size_t length, 133 int prot, int flags, off_t offset); 134 int (*munmap) (struct pcm_plugin *plugin, void *addr, size_t length); 135 int (*poll) (struct pcm_plugin *plugin, struct pollfd *pfd, nfds_t nfds, 136 int timeout); 137 }; 138 139 /** Minimum and maximum values for hardware parameter constraints. 140 * @ingroup libtinyalsa-pcm 141 */ 142 struct pcm_plugin_min_max { 143 /** Minimum value for the hardware parameter */ 144 unsigned int min; 145 /** Maximum value for the hardware parameter */ 146 unsigned int max; 147 }; 148 149 /** Encapsulate the hardware parameter constraints 150 * @ingroup libtinyalsa-pcm 151 */ 152 struct pcm_plugin_hw_constraints { 153 /** Value for SNDRV_PCM_HW_PARAM_ACCESS param */ 154 uint64_t access; 155 /** Value for SNDRV_PCM_HW_PARAM_FORMAT param. 156 * As of this implementation ALSA supports 52 formats */ 157 uint64_t format; 158 /** Value for SNDRV_PCM_HW_PARAM_SAMPLE_BITS param */ 159 struct pcm_plugin_min_max bit_width; 160 /** Value for SNDRV_PCM_HW_PARAM_CHANNELS param */ 161 struct pcm_plugin_min_max channels; 162 /** Value for SNDRV_PCM_HW_PARAM_RATE param */ 163 struct pcm_plugin_min_max rate; 164 /** Value for SNDRV_PCM_HW_PARAM_PERIODS param */ 165 struct pcm_plugin_min_max periods; 166 /** Value for SNDRV_PCM_HW_PARAM_PERIOD_BYTES param */ 167 struct pcm_plugin_min_max period_bytes; 168 }; 169 170 struct pcm_plugin { 171 /** Card number for the pcm device */ 172 unsigned int card; 173 /** device number for the pcm device */ 174 unsigned int device; 175 /** pointer to the contraints registered by the plugin */ 176 struct pcm_plugin_hw_constraints *constraints; 177 /** Indicates read/write mode, etc.. */ 178 int mode; 179 /* Pointer to hold the plugin's private data */ 180 void *priv; 181 /* Tracks the plugin state */ 182 unsigned int state; 183 }; 184 185 typedef void (*mixer_event_callback)(struct mixer_plugin *); 186 187 struct mixer_plugin_ops { 188 int (*open) (struct mixer_plugin **plugin, unsigned int card); 189 void (*close) (struct mixer_plugin **plugin); 190 int (*subscribe_events) (struct mixer_plugin *plugin, 191 mixer_event_callback event_cb); 192 ssize_t (*read_event) (struct mixer_plugin *plugin, 193 struct snd_ctl_event *ev, size_t size); 194 }; 195 196 struct snd_control { 197 snd_ctl_elem_iface_t iface; 198 unsigned int access; 199 const char *name; 200 snd_ctl_elem_type_t type; 201 void *value; 202 int (*get) (struct mixer_plugin *plugin, 203 struct snd_control *control, 204 struct snd_ctl_elem_value *ev); 205 int (*put) (struct mixer_plugin *plugin, 206 struct snd_control *control, 207 struct snd_ctl_elem_value *ev); 208 uint32_t private_value; 209 void *private_data; 210 }; 211 212 struct mixer_plugin { 213 unsigned int card; 214 void *priv; 215 216 int eventfd; 217 int subscribed; 218 int event_cnt; 219 220 struct snd_control *controls; 221 unsigned int num_controls; 222 }; 223 224 struct snd_value_enum { 225 unsigned int items; 226 char **texts; 227 }; 228 229 struct snd_value_bytes { 230 unsigned int size; 231 }; 232 233 struct snd_value_tlv_bytes { 234 unsigned int size; 235 int (*get) (struct mixer_plugin *plugin, 236 struct snd_control *control, 237 struct snd_ctl_tlv *tlv); 238 int (*put) (struct mixer_plugin *plugin, 239 struct snd_control *control, 240 struct snd_ctl_tlv *tlv); 241 }; 242 243 struct snd_value_int { 244 unsigned int count; 245 int min; 246 int max; 247 int step; 248 }; 249 250 /** Operations defined by the plugin. 251 * */ 252 struct snd_node_ops { 253 /** Function pointer to get card definition */ 254 void* (*open_card)(unsigned int card); 255 /** Function pointer to release card definition */ 256 void (*close_card)(void *card); 257 /** Get interger type properties from device definition */ 258 int (*get_int)(void *node, const char *prop, int *val); 259 /** Get string type properties from device definition */ 260 int (*get_str)(void *node, const char *prop, char **val); 261 /** Function pointer to get mixer definition */ 262 void* (*get_mixer)(void *card); 263 /** Function pointer to get PCM definition */ 264 void* (*get_pcm)(void *card, unsigned int id); 265 /** Reserved for other nodes such as compress */ 266 void* reserved[4]; 267 }; 268 269 #endif /* end of TINYALSA_PLUGIN_H */ 270