aboutsummaryrefslogtreecommitdiffstats
path: root/src/avirt-config.c
blob: a4a8957186f92cf836985bb688947c817573035d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Application interface library for the AVIRT driver
 * 
 * avirt-config.c - Main AVIRT configuration via configfs
 *
 * Copyright (C) 2018 Fiberdyne Systems Pty Ltd
 * Author: Mark Farrugia <mark.farrugia@fiberdyne.com.au>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <avirt/avirt.h>

#include <stdbool.h>
#include <stdio.h>
#include <alsa/asoundlib.h>
#include <sys/stat.h>
#include <sys/mount.h>

#define AVIRT_CONFIGFS_PATH_STREAMS "/config/snd-avirt/streams/"
#define AVIRT_CONFIGFS_PATH_MAXLEN 64
#define AVIRT_DEVICE_PATH "/dev/snd/by-path/platform-snd_avirt"

#define AVIRT_ERROR(errmsg) \
  fprintf(stderr, "AVIRT ERROR: %s\n", errmsg);
#define AVIRT_ERROR_V(fmt, args...) \
      fprintf(stderr, "AVIRT ERROR: " fmt "\n", ##args);

#define AVIRT_DEBUG_ON
#ifdef AVIRT_DEBUG_ON
# define AVIRT_DEBUG(debugmsg) \
  fprintf(stderr, "AVIRT DEBUG: %s\n", debugmsg);
# define AVIRT_DEBUG_V(fmt, args...) \
  fprintf(stderr, "[%s]: AVIRT DEBUG: " fmt "\n", __func__, ##args);
#endif

/**
 *  extracted IOCTLs from <alsa/asoundlib.h>
 */
#define _IOR_HACKED(type,nr,size)       _IOC(_IOC_READ,(type),(nr),size)
#define SNDRV_CTL_IOCTL_CARD_INFO(size) _IOR_HACKED('U', 0x01, size)

/**
 * Checks whether the configfs filesystem is mounted
 */
#define IS_CONFIGFS_MOUNTED()                             \
  do {                                                    \
    int err;                                              \
    if (!configfs_mounted) {                              \
      err = mount_configfs();                             \
      if (err < 0) return err;                            \
    }                                                     \
  } while (0)

/**
 * Write the given formatted string to a file given by path
 */
#define WRITE_TO_PATH(path, fmt, args...)                 \
  do {                                                    \
    FILE *fd = fopen(path, "w");                          \
    if (!fd) {                                            \
      AVIRT_ERROR_V("Failed to open file at '%s'", path); \
      return -EPERM;                                      \
    }                                                     \
    fprintf(fd, fmt, ##args);                             \
    fclose(fd);                                           \
  } while (0)

static bool configfs_mounted = false;
static bool card_sealed = false;
static int card_index = -1;

static int mount_configfs()
{
  int err;
  char fsline[100];
  bool configfs_supported = false;
  FILE *procfs;
  struct stat st = {0};

  // Check for /proc/filesystems for configfs support
  procfs = fopen("/proc/filesystems", "r");
  if (!procfs)
    return -1;

  while (fgets(fsline, 100, procfs))
  {
    if (!strstr(fsline, "configfs"))
      continue;
    configfs_supported = true;
  }

  if (!configfs_supported)
  {
    AVIRT_ERROR("configfs is not supported !");
    return -1;
  }

  // Check whether /config dir exists, if not, create it
  if (stat("/config", &st) == -1)
    mkdir("/config", S_IRWXU | S_IRWXG | S_IRWXO);
  
  err = mount("none", "/config", "configfs", 0, NULL);
  if (!err)
  {
    AVIRT_DEBUG("Successfully mounted configfs");
    configfs_mounted = true;
  }
  else
    AVIRT_ERROR("Failed to mount configfs filesystem!");

  return err;
}

int snd_avirt_info_get_card(int idx)
{
  int open_dev, err = 0;
  snd_ctl_card_info_t *card_info;
  char path[64];

  sprintf(path, AVIRT_DEVICE_PATH ".%d", idx);
  open_dev = open(path, O_RDONLY);
  if (open_dev < 0)
  {
    AVIRT_ERROR_V("Could not open device with path: %s", path);
    err = -ENODEV;
    goto exit_dev;
  }

  snd_ctl_card_info_alloca(&card_info);
  err = ioctl(open_dev,
              SNDRV_CTL_IOCTL_CARD_INFO(snd_ctl_card_info_sizeof()),
              card_info);
    if (err < 0)
    {
      AVIRT_ERROR("Could not ioctl card info for AVIRT");
      goto exit_dev;
    }

    return snd_ctl_card_info_get_card(card_info);

exit_dev:
  close(open_dev);

  return err;
}

int snd_avirt_pcm_info(const char *pcm_name, snd_pcm_info_t *pcm_info)
{
  int pcm_dev = -1, err = 0;
  snd_ctl_t *handle;
  char name[32];

  if (card_index < 0)
    card_index = snd_avirt_info_get_card(0); /* TD MF: Dynamically get the AVIRT card number */
  if (card_index < 0)
    return card_index;

  sprintf(name, "hw:%d", card_index);
  if ((err = snd_ctl_open(&handle, name, 0)) < 0)
  {
    AVIRT_ERROR_V("control open (%i): %s", card_index, snd_strerror(err));
    return err;
  }

  while (1)
  {
    if (snd_ctl_pcm_next_device(handle, &pcm_dev) < 0)
      AVIRT_ERROR("snd_ctl_pcm_next_device");
    if (pcm_dev < 0)
    {
      AVIRT_ERROR_V("Cannot find AVIRT device with name: %s", pcm_name)
      err = -ENODEV;
      goto exit_ctl;
    }
    snd_pcm_info_set_device(pcm_info, pcm_dev);
    snd_pcm_info_set_subdevice(pcm_info, 0);
    if ((err = snd_ctl_pcm_info(handle, pcm_info)) < 0)
    {
      if (err != -ENOENT)
      {
        AVIRT_ERROR_V("control digital audio info (%i): %s",
                    card_index, snd_strerror(err));
      }
      continue;
    }
    if (!strcmp(pcm_name, snd_pcm_info_get_name(pcm_info)))
      break;
  }

exit_ctl:
  snd_ctl_close(handle);

  return err;
}

int snd_avirt_stream_new(const char *name, unsigned int channels, int direction,
                         const char *map)
{
  int err;
  char path[AVIRT_CONFIGFS_PATH_MAXLEN];
  char path_attr[AVIRT_CONFIGFS_PATH_MAXLEN];

  IS_CONFIGFS_MOUNTED();

  // Check if card is already sealed
  if (card_sealed)
  {
    AVIRT_ERROR("Card is already sealed!");
    return -EPERM;
  }

  // This indicates to AVIRT the direction of the stream
  strcpy(path, AVIRT_CONFIGFS_PATH_STREAMS);
  switch (direction) {
    case SND_PCM_STREAM_PLAYBACK:
      strcat(path, "playback_");
      break;
    case SND_PCM_STREAM_CAPTURE:
      strcat(path, "capture_");
      break;
    default:
      return -EINVAL;
  }

  if ((AVIRT_CONFIGFS_PATH_MAXLEN - strlen(path)) < strlen(name))
  {
    AVIRT_ERROR_V("Cannot create stream '%s' since name is too long!", name);
    return -ENOMEM;
  }

  strcat(path, name);
  err = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
  if (err < 0)
  {
    AVIRT_ERROR_V("Cannot create stream '%s' at directory '%s'", name, path);
    return err;
  }

  // Write channels
  strcpy(path_attr, path);
  strcat(path_attr, "/channels");
  WRITE_TO_PATH(path_attr, "%d", channels);

  if (map)
  {
    // Write mapping
    strcpy(path_attr, path);
    strcat(path_attr, "/map");
    WRITE_TO_PATH(path_attr, "%s", map);
  }
  else
  {
    AVIRT_DEBUG("No map specified!");
  }

  AVIRT_DEBUG_V("Created stream: %s", name);

  return 0;
}

int snd_avirt_card_seal()
{
  int open_dev, err = 0;
  snd_ctl_card_info_t *card_info;
  char path_sealed[AVIRT_CONFIGFS_PATH_MAXLEN];

  // Check if card is already sealed
  if (card_sealed)
  {
    AVIRT_ERROR("Card is already sealed!");
    return -EPERM;
  }

  IS_CONFIGFS_MOUNTED();

  strcpy(path_sealed, AVIRT_CONFIGFS_PATH_STREAMS);
  strcat(path_sealed, "/sealed");
  WRITE_TO_PATH(path_sealed, "%d", 1);

  AVIRT_DEBUG("Card sealed!");
  card_sealed = true;

  usleep(20000); // Need to wait for the snd card to be registered
  
  // Get card index for AVIRT, now that it is registered
  open_dev = open(AVIRT_DEVICE_PATH, O_RDONLY);
  if (open_dev < 0)
  {
    AVIRT_ERROR_V("Could not open device with path: %s", AVIRT_DEVICE_PATH);
    err = -ENODEV;
    goto exit_dev;
  }

  snd_ctl_card_info_alloca(&card_info);
  err = ioctl(open_dev,
              SNDRV_CTL_IOCTL_CARD_INFO(snd_ctl_card_info_sizeof()),
              card_info);
    if (err < 0)
    {
      AVIRT_ERROR("Could not ioctl card info for AVIRT");
      goto exit_dev;
    }

    card_index = snd_ctl_card_info_get_card(card_info);

exit_dev:
  close(open_dev);

  return err;
}