File: vxloader.c

package info (click to toggle)
alsa-tools 1.2.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,184 kB
  • sloc: ansic: 23,759; cpp: 15,276; sh: 6,037; pascal: 1,140; asm: 1,053; makefile: 926; xml: 846; python: 251
file content (387 lines) | stat: -rw-r--r-- 8,023 bytes parent folder | download
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Firmware Loader for Digigram VX soundcards
 *
 * Copyright (c) 2003 by Takashi Iwai <tiwai@suse.de>
 *
 *   This program 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 program 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 program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <alsa/asoundlib.h>


#define PROGNAME	"vxloader"


/* max. number of cards (shouldn't be in the public header?) */
#define SND_CARDS	8


static int verbose = 0;

static void usage(void)
{
	printf("Boot loader for Digigram VX cards\n");
	printf("version %s\n", VERSION);
	printf("usage: vxloader [-c card] [-D device]\n");
}

static void error(const char *fmt, ...)
{
	if (verbose) {
		va_list ap;
		va_start(ap, fmt);
		fprintf(stderr, "%s: ", PROGNAME);
		vfprintf(stderr, fmt, ap);
		va_end(ap);
	}
}


/*
 * read a xilinx bitstream file
 */
static int read_xilinx_image(snd_hwdep_dsp_image_t *img, const char *fname)
{
	FILE *fp;
	char buf[256];
	int data, c, idx, length;
	unsigned char *imgbuf = NULL;
	char *p;

	if ((fp = fopen(fname, "r")) == NULL) {
		error("cannot open %s\n", fname);
		return -ENODEV;
	}

	snd_hwdep_dsp_image_set_name(img, fname);

	c = 0;
	data = 0;
	idx = 0;
	length = 0;
	while (fgets(buf, sizeof(buf), fp)) {
		if (strncmp(buf, "Bits:", 5) == 0) {
			for (p = buf + 5; *p && isspace(*p); p++)
				;
			if (! *p) {
				error("corrupted file %s in Bits line\n", fname);
				fclose(fp);
				return -EINVAL;
			}
			length = atoi(p);
			length /= 8;
			if (length <= 0) {
				error("corrupted file %s, detected length = %d\n", fname, length);
				fclose(fp);
				return -EINVAL;
			}
			snd_hwdep_dsp_image_set_length(img, length);
			imgbuf = malloc(length);
			if (! imgbuf) {
				error("cannot alloc %d bytes\n", length);
				fclose(fp);
				return -ENOMEM;
			}
			snd_hwdep_dsp_image_set_image(img, imgbuf);
			continue;
		}
		if (buf[0] != '0' && buf[1] != '1')
			continue;
		if (length <= 0) {
			error("corrupted file %s, starting without Bits line\n", fname);
			fclose(fp);
			return -EINVAL;
		}
		for (p = buf; *p == '0' || *p == '1'; p++) {
			data |= (*p - '0') << c;
			c++;
			if (c >= 8) {
				imgbuf[idx] = data;
				data = 0;
				c = 0;
				idx++;
				if (idx >= length)
					break;
			}
		}
	}
	if (c)
		imgbuf[idx++] = data;
	if (idx != length) {
		error("length doesn't match: %d != %d\n", idx, length);
		fclose(fp);
		return -EINVAL;
	}

	fclose(fp);
	return 0;
}

/*
 * read a binary image file
 */
static int read_boot_image(snd_hwdep_dsp_image_t *img, const char *fname)
{
	struct stat st;
	int fd, length;
	unsigned char *imgbuf;

	snd_hwdep_dsp_image_set_name(img, fname);
	if (stat(fname, &st) < 0) {
		error("cannot call stat %s\n", fname);
		return -ENODEV;
	}
	length = st.st_size;
	if (length == 0) {
		error("zero file size %s\n", fname);
		return -EINVAL;
	}

	imgbuf = malloc(st.st_size);
	if (! imgbuf) {
		error("cannot malloc %d bytes\n", length);
		return -ENOMEM;
	}
	snd_hwdep_dsp_image_set_length(img, length);
	snd_hwdep_dsp_image_set_image(img, imgbuf);

	fd = open(fname, O_RDONLY);
	if (fd < 0) {
		error("cannot open %s\n", fname);
		return -ENODEV;
	}
	if (read(fd, imgbuf, length) != length) {
		error("cannot read %d bytes from %s\n", length, fname);
		close(fd);
		return -EINVAL;
	}

	close(fd);
	return 0;
}


/*
 * parse the index file and get the file to read from the key
 */

#define MAX_PATH	128

static int get_file_name(const char *key, unsigned int idx, char *fname)
{
	FILE *fp;
	char buf[128];
	char temp[32], *p;
	int len;

	snprintf(buf, sizeof(buf), "%s/%s.conf", DATAPATH, key);
	if ((fp = fopen(buf, "r")) == NULL) {
		error("cannot open the index file %s\n", buf);
		return -ENODEV;
	}

	sprintf(temp, "dsp%d", idx);
	len = strlen(temp);

	while (fgets(buf, sizeof(buf), fp)) {
		if (strncmp(buf, temp, len))
			continue;
		for (p = buf + len; *p && isspace(*p); p++)
			;
		if (*p == '/') {
			strncpy(fname, p, MAX_PATH - 1);
			fname[MAX_PATH - 1] = '\0';
		} else {
			snprintf(fname, MAX_PATH, "%s/%s", DATAPATH, p);
		}
		fname[MAX_PATH-1] = 0;
		/* chop the last linefeed */
		for (p = fname; *p && *p != '\n'; p++)
			;
		*p = 0;
		fclose(fp);
		return 0;
	}
	fclose(fp);
	error("cannot find the file entry for %s\n", temp);
	return -ENODEV;
}


/*
 * read and transfer the firmware binary
 */
static int load_firmware(snd_hwdep_t *hw, const char *id, unsigned int idx, int is_pcmcia)
{
	int err;
	char fname[MAX_PATH];
	int is_xilinx = 0;
	snd_hwdep_dsp_image_t *dsp;

	if (get_file_name(id, idx, fname) < 0)
		return -EINVAL;
	if (is_pcmcia) {
		if (idx == 1)
			is_xilinx = 1;
	} else {
		if (idx == 0)
			is_xilinx = 1;
	}

	snd_hwdep_dsp_image_alloca(&dsp);
	snd_hwdep_dsp_image_set_index(dsp, idx);
	if (is_xilinx)
		err = read_xilinx_image(dsp, fname);
	else
		err = read_boot_image(dsp, fname);
	if (err < 0)
		return err;

	err = snd_hwdep_dsp_load(hw, dsp);
	if (err < 0)
		error("error in loading %s\n", fname);
	return err;
}


/*
 * check the name id of the given hwdep handle
 */
static int check_hwinfo(snd_hwdep_t *hw, const char *id)
{
	snd_hwdep_info_t *info;
	int err;

	snd_hwdep_info_alloca(&info);
	if ((err = snd_hwdep_info(hw, info)) < 0)
		return err;
	if (strcmp(snd_hwdep_info_get_id(info), id))
		return -ENODEV;
	return 0; /* ok */
}

/*
 * load the firmware binaries
 */
static int vx_boot(const char *devname)
{
	snd_hwdep_t *hw;
	const char *id;
	int err, is_pcmcia;
	unsigned int idx, dsps, loaded;
	snd_hwdep_dsp_status_t *stat;

	if ((err = snd_hwdep_open(&hw, devname, O_RDWR)) < 0) {
		error("cannot open hwdep %s\n", devname);
		return err;
	}

	if (check_hwinfo(hw, "VX Loader") < 0) {
		error("invalid hwdep %s\n", devname);
		snd_hwdep_close(hw);
		return -ENODEV;
	}

	snd_hwdep_dsp_status_alloca(&stat);
	/* get the current status */
	if ((err = snd_hwdep_dsp_status(hw, stat)) < 0) {
		error("cannot get version for %s\n", devname);
		snd_hwdep_close(hw);
		return err;
	}

	if (snd_hwdep_dsp_status_get_chip_ready(stat))
		return 0; /* already loaded */

	id = snd_hwdep_dsp_status_get_id(stat);
	if (strcmp(id, "vxpocket") == 0 ||
	    strcmp(id, "vxp440") == 0)
		is_pcmcia = 1;
	else
		is_pcmcia = 0;

	dsps = snd_hwdep_dsp_status_get_num_dsps(stat);
	loaded = snd_hwdep_dsp_status_get_dsp_loaded(stat);

	for (idx = 0; idx < dsps; idx++) {
		if (loaded & (1 << idx))
			continue;
		if ((err = load_firmware(hw, id, idx, is_pcmcia)) < 0) {
			snd_hwdep_close(hw);
			return err;
		}
	}

	snd_hwdep_close(hw);
	return 0;
}


int main(int argc, char **argv)
{
	int c;
	int card = -1;
	char *device_name = NULL;
	char name[64];

	while ((c = getopt(argc, argv, "c:D:")) != -1) {
		switch (c) {
		case 'c':
			card = atoi(optarg);
			break;
		case 'D':
			device_name = optarg;
			break;
		default:
			usage();
			return 1;
		}
	}

	if (device_name) {
		verbose = 1;
		return vx_boot(device_name) != 0;
	}
	if (card >= 0) {
		sprintf(name, "hw:%d", card);
		verbose = 1;
		return vx_boot(name) != 0;
	}

	/* probe the all cards */
	for (c = 0; c < SND_CARDS; c++) {
		sprintf(name, "hw:%d", c);
		if (! vx_boot(name))
			card = c;
	}
	if (card < 0) {
		fprintf(stderr, PROGNAME ": no VX-compatible cards found\n");
		return 1;
	}
	return 0;
}