Blame sb16_csp/cspctl.c

Packit 427e91
/*
Packit 427e91
 *  SB16/AWE32 Creative Signal Processor (ASP/CSP) control program
Packit 427e91
 *
Packit 427e91
 *  Copyright (c) 2000 by Uros Bizjak <uros@kss-loka.si>
Packit 427e91
 *
Packit 427e91
 *   This program is free software; you can redistribute it and/or modify
Packit 427e91
 *   it under the terms of the GNU General Public License as published by
Packit 427e91
 *   the Free Software Foundation; either version 2 of the License, or
Packit 427e91
 *   (at your option) any later version.
Packit 427e91
 *
Packit 427e91
 *   This program is distributed in the hope that it will be useful,
Packit 427e91
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 427e91
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 427e91
 *   GNU General Public License for more details.
Packit 427e91
 *
Packit 427e91
 *   You should have received a copy of the GNU General Public License
Packit 427e91
 *   along with this program; if not, write to the Free Software
Packit 427e91
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 427e91
 *
Packit 427e91
 */
Packit 427e91
Packit 427e91
#include <alsa/asoundlib.h>
Packit 427e91
#include <getopt.h>
Packit 427e91
#include <stdarg.h>
Packit 427e91
#include <stdio.h>
Packit 427e91
#include <stdlib.h>
Packit 427e91
#include <fcntl.h>
Packit 427e91
#include <unistd.h>
Packit 427e91
#include <assert.h>
Packit 427e91
#include <string.h>
Packit 427e91
#include <errno.h>
Packit 427e91
#include <linux/ioctl.h>
Packit 427e91
#include <alsa/sound/sb16_csp.h>
Packit 427e91
Packit 427e91
/* --- commands --- */
Packit 427e91
enum {
Packit 427e91
     COMMAND_LOAD,
Packit 427e91
     COMMAND_UNLOAD
Packit 427e91
};
Packit 427e91
Packit 427e91
snd_sb_csp_microcode_t microcode;
Packit 427e91
Packit 427e91
int card = 0;
Packit 427e91
Packit 427e91
static void error(const char *fmt,...)
Packit 427e91
{
Packit 427e91
	va_list va;
Packit 427e91
Packit 427e91
	va_start(va, fmt);
Packit 427e91
	fprintf(stderr, "Error: ");
Packit 427e91
	vfprintf(stderr, fmt, va);
Packit 427e91
	fprintf(stderr, "\n");
Packit 427e91
	va_end(va);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void help(char *command)
Packit 427e91
{
Packit 427e91
	fprintf(stderr,
Packit 427e91
		"Usage: %s command [-options] <filename>\n"
Packit 427e91
		"\nAvailable commands:\n"
Packit 427e91
		"  load    load microcode from filename\n"
Packit 427e91
		"  unload  unload microcode from CSP\n"
Packit 427e91
		"\nAvailable options:\n"
Packit 427e91
		"  -h,--help        this help\n"
Packit 427e91
		"  -c card          select card number, default = 0\n"
Packit 427e91
		"  -f function      select CSP function #, defaults to 1\n"
Packit 427e91
		"  -d description   optional codec description string\n"
Packit 427e91
		, command);
Packit 427e91
}
Packit 427e91
Packit 427e91
static int csp_command (int idx, int dev, int command, char *filename)
Packit 427e91
{
Packit 427e91
	int fd, err;
Packit 427e91
	snd_hwdep_t *handle;
Packit 427e91
	char name[16];
Packit 427e91
Packit 427e91
	sprintf(name, "hw:%i,%i", idx, dev);
Packit 427e91
Packit 427e91
	/* open CSP hwdep device */
Packit 427e91
	if ((err = snd_hwdep_open(&handle, name, O_WRONLY)) < 0) {
Packit 427e91
		error("CSP open (%i-%i): %s", idx, dev, snd_strerror(err));
Packit 427e91
		exit(1);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	switch (command) {
Packit 427e91
	case COMMAND_LOAD:
Packit 427e91
		/* open microcode file */
Packit 427e91
		if ((fd = open(filename, O_RDONLY, 0)) == -1) {
Packit 427e91
			perror(filename);
Packit 427e91
			exit(1);
Packit 427e91
		}
Packit 427e91
		/* read microcode to buffer */
Packit 427e91
		if (read(fd, &microcode.data, sizeof(microcode.data)) < 0) {
Packit 427e91
			error("%s: read error", filename);
Packit 427e91
			exit(1);
Packit 427e91
		}
Packit 427e91
		close(fd);
Packit 427e91
Packit 427e91
		/* load microcode to CSP */
Packit 427e91
		if (snd_hwdep_ioctl(handle, SNDRV_SB_CSP_IOCTL_LOAD_CODE, &microcode) < 0) {
Packit 427e91
			error("unable to load microcode");
Packit 427e91
			exit(1);
Packit 427e91
		}
Packit 427e91
		break;
Packit 427e91
	case COMMAND_UNLOAD:
Packit 427e91
		/* unload microcode from CSP */
Packit 427e91
		if (snd_hwdep_ioctl(handle, SNDRV_SB_CSP_IOCTL_UNLOAD_CODE, NULL) < 0) {
Packit 427e91
			error("unable to unload microcode");
Packit 427e91
			exit(1);
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
Packit 427e91
	snd_hwdep_close(handle);
Packit 427e91
	return 0;
Packit 427e91
}
Packit 427e91
Packit 427e91
int main(int argc, char *argv[])
Packit 427e91
{
Packit 427e91
	int dev;
Packit 427e91
	int command, c;
Packit 427e91
	int err;
Packit 427e91
Packit 427e91
	char card_id[32];
Packit 427e91
Packit 427e91
	snd_ctl_t *ctl_handle;
Packit 427e91
	snd_ctl_card_info_t *card_info;
Packit 427e91
	snd_hwdep_info_t *hwdep_info;
Packit 427e91
Packit 427e91
	snd_ctl_card_info_alloca(&card_info);
Packit 427e91
	snd_hwdep_info_alloca(&hwdep_info);
Packit 427e91
Packit 427e91
	if (argc > 1 && !strcmp(argv[1], "--help")) {
Packit 427e91
		help(argv[0]);
Packit 427e91
		return 0;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	strcpy (microcode.info.codec_name, "UNKNOWN");
Packit 427e91
	microcode.info.func_req = 1;
Packit 427e91
	while ((c = getopt(argc, argv, "hc:f:d:")) != EOF) {
Packit 427e91
		switch (c) {
Packit 427e91
		case 'h':
Packit 427e91
			help(argv[0]);
Packit 427e91
			return 0;
Packit 427e91
		case 'c':
Packit 427e91
			{
Packit 427e91
				card = snd_card_get_index(optarg);
Packit 427e91
				if (card < 0 || card > 31) {
Packit 427e91
					error ("wrong -c argument '%s'\n", optarg);
Packit 427e91
					return 1;
Packit 427e91
				}
Packit 427e91
			}
Packit 427e91
			break;
Packit 427e91
		case 'f':
Packit 427e91
			microcode.info.func_req = atoi(optarg);
Packit 427e91
			if ((microcode.info.func_req < 1) || (microcode.info.func_req > 4)) {
Packit 427e91
				error("value %i for function number is invalid",
Packit 427e91
				      microcode.info.func_req);
Packit 427e91
				return 1;
Packit 427e91
			}
Packit 427e91
			break;
Packit 427e91
		case 'd':
Packit 427e91
			if (strlen(optarg) > 16) {
Packit 427e91
				error("codec description '%s' too long", optarg);
Packit 427e91
				return 1;
Packit 427e91
			}
Packit 427e91
			strcpy(microcode.info.codec_name, optarg);
Packit 427e91
			break;
Packit 427e91
		default:
Packit 427e91
			return 1;
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	if (optind >= argc) {
Packit 427e91
		error("please specify command");
Packit 427e91
		return 1;
Packit 427e91
	}
Packit 427e91
	if (!strcmp (argv[optind], "load")) {
Packit 427e91
	     command = COMMAND_LOAD;
Packit 427e91
	} else if (!strcmp (argv[optind], "unload")) {
Packit 427e91
	     command = COMMAND_UNLOAD;
Packit 427e91
	} else {
Packit 427e91
		error ("command should be either 'load' or 'unload'");
Packit 427e91
		return 1;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	if ((command == COMMAND_LOAD) && (++optind >= argc)) {
Packit 427e91
		error ("missing microcode filename");
Packit 427e91
		return 1;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	// Get control handle for selected card
Packit 427e91
	sprintf(card_id, "hw:%i", card);
Packit 427e91
	if ((err = snd_ctl_open(&ctl_handle, card_id, 0)) < 0) {
Packit 427e91
		error("control open (%s): %s", card_id, snd_strerror(err));
Packit 427e91
		return 1;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	// Read control hardware info from card
Packit 427e91
	if ((err = snd_ctl_card_info(ctl_handle, card_info)) < 0) {
Packit 427e91
		error("control hardware info (%s): %s", card_id, snd_strerror(err));
Packit 427e91
		exit(1);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	// CSP chip is present only on SB16 and SB AWE cards
Packit 427e91
	if (strcmp(snd_ctl_card_info_get_driver(card_info), "SB16") != 0 &&
Packit 427e91
	    strcmp(snd_ctl_card_info_get_driver(card_info), "SB AWE") != 0) {
Packit 427e91
		error("not a SB16 or SB AWE type card");
Packit 427e91
		exit(1);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	// find CSP hardware dependant device and execute command
Packit 427e91
	dev = -1;
Packit 427e91
	err = 1;
Packit 427e91
	while (1) {
Packit 427e91
		if (snd_ctl_hwdep_next_device(ctl_handle, &dev) < 0)
Packit 427e91
			error("hwdep next device (%s): %s", card_id, snd_strerror(err));
Packit 427e91
		if (dev < 0)
Packit 427e91
			break;
Packit 427e91
		snd_hwdep_info_set_device(hwdep_info, dev);
Packit 427e91
		if (snd_ctl_hwdep_info(ctl_handle, hwdep_info) < 0) {
Packit 427e91
			if (err != -ENOENT)
Packit 427e91
				error("control hwdep info (%s): %s", card_id, snd_strerror(err));
Packit 427e91
			continue;
Packit 427e91
		}
Packit 427e91
		if (snd_hwdep_info_get_iface(hwdep_info) == SND_HWDEP_IFACE_SB16CSP) {
Packit 427e91
			err = csp_command (card, dev, command, argv[optind]);
Packit 427e91
			break;
Packit 427e91
		}
Packit 427e91
	}
Packit 427e91
	if (err)
Packit 427e91
		error("no CSP device present");
Packit 427e91
Packit 427e91
	snd_ctl_close(ctl_handle);
Packit 427e91
	return 0;
Packit 427e91
}