xref: /linux/sound/drivers/opl3/opl3_oss.c (revision 75b1a8f9d62e50f05d0e4e9f3c8bcde32527ffc1)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Interface for OSS sequencer emulation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8d81a6d71SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include "opl3_voice.h"
101da177e4SLinus Torvalds 
115b1646a8STakashi Iwai static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
125b1646a8STakashi Iwai static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
135b1646a8STakashi Iwai static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
145b1646a8STakashi Iwai static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
155b1646a8STakashi Iwai static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /* operators */
181da177e4SLinus Torvalds 
1987065d3dSTakashi Iwai static const struct snd_seq_oss_callback oss_callback = {
201da177e4SLinus Torvalds 	.owner = 	THIS_MODULE,
211da177e4SLinus Torvalds 	.open =		snd_opl3_open_seq_oss,
221da177e4SLinus Torvalds 	.close =	snd_opl3_close_seq_oss,
231da177e4SLinus Torvalds 	.ioctl =	snd_opl3_ioctl_seq_oss,
241da177e4SLinus Torvalds 	.load_patch =	snd_opl3_load_patch_seq_oss,
251da177e4SLinus Torvalds 	.reset =	snd_opl3_reset_seq_oss,
261da177e4SLinus Torvalds };
271da177e4SLinus Torvalds 
285b1646a8STakashi Iwai static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
291da177e4SLinus Torvalds 				    void *private_data, int atomic, int hop)
301da177e4SLinus Torvalds {
315b1646a8STakashi Iwai 	struct snd_opl3 *opl3 = private_data;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	if (ev->type != SNDRV_SEQ_EVENT_OSS)
341da177e4SLinus Torvalds 		snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
351da177e4SLinus Torvalds 	return 0;
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /* ------------------------------ */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static void snd_opl3_oss_free_port(void *private_data)
411da177e4SLinus Torvalds {
425b1646a8STakashi Iwai 	struct snd_opl3 *opl3 = private_data;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	snd_midi_channel_free_set(opl3->oss_chset);
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
475b1646a8STakashi Iwai static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
481da177e4SLinus Torvalds {
495b1646a8STakashi Iwai 	struct snd_seq_port_callback callbacks;
501da177e4SLinus Torvalds 	char name[32];
511da177e4SLinus Torvalds 	int voices, opl_ver;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	voices = (opl3->hardware < OPL3_HW_OPL3) ?
541da177e4SLinus Torvalds 		MAX_OPL2_VOICES : MAX_OPL3_VOICES;
551da177e4SLinus Torvalds 	opl3->oss_chset = snd_midi_channel_alloc_set(voices);
561da177e4SLinus Torvalds 	if (opl3->oss_chset == NULL)
571da177e4SLinus Torvalds 		return -ENOMEM;
581da177e4SLinus Torvalds 	opl3->oss_chset->private_data = opl3;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	memset(&callbacks, 0, sizeof(callbacks));
611da177e4SLinus Torvalds 	callbacks.owner = THIS_MODULE;
621da177e4SLinus Torvalds 	callbacks.event_input = snd_opl3_oss_event_input;
631da177e4SLinus Torvalds 	callbacks.private_free = snd_opl3_oss_free_port;
641da177e4SLinus Torvalds 	callbacks.private_data = opl3;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
671da177e4SLinus Torvalds 	sprintf(name, "OPL%i OSS Port", opl_ver);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	opl3->oss_chset->client = opl3->seq_client;
701da177e4SLinus Torvalds 	opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
711da177e4SLinus Torvalds 							  SNDRV_SEQ_PORT_CAP_WRITE,
721da177e4SLinus Torvalds 							  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
73450047a7SClemens Ladisch 							  SNDRV_SEQ_PORT_TYPE_MIDI_GM |
74450047a7SClemens Ladisch 							  SNDRV_SEQ_PORT_TYPE_HARDWARE |
75450047a7SClemens Ladisch 							  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
761da177e4SLinus Torvalds 							  voices, voices,
771da177e4SLinus Torvalds 							  name);
781da177e4SLinus Torvalds 	if (opl3->oss_chset->port < 0) {
795e315e92SDave Jones 		int port;
805e315e92SDave Jones 		port = opl3->oss_chset->port;
811da177e4SLinus Torvalds 		snd_midi_channel_free_set(opl3->oss_chset);
825e315e92SDave Jones 		return port;
831da177e4SLinus Torvalds 	}
841da177e4SLinus Torvalds 	return 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* ------------------------------ */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* register OSS synth */
905b1646a8STakashi Iwai void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
911da177e4SLinus Torvalds {
925b1646a8STakashi Iwai 	struct snd_seq_oss_reg *arg;
935b1646a8STakashi Iwai 	struct snd_seq_device *dev;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
965b1646a8STakashi Iwai 			       sizeof(struct snd_seq_oss_reg), &dev) < 0)
971da177e4SLinus Torvalds 		return;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	opl3->oss_seq_dev = dev;
100*75b1a8f9SJoe Perches 	strscpy(dev->name, name, sizeof(dev->name));
1011da177e4SLinus Torvalds 	arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
1021da177e4SLinus Torvalds 	arg->type = SYNTH_TYPE_FM;
1031da177e4SLinus Torvalds 	if (opl3->hardware < OPL3_HW_OPL3) {
1041da177e4SLinus Torvalds 		arg->subtype = FM_TYPE_ADLIB;
1051da177e4SLinus Torvalds 		arg->nvoices = MAX_OPL2_VOICES;
1061da177e4SLinus Torvalds 	} else {
1071da177e4SLinus Torvalds 		arg->subtype = FM_TYPE_OPL3;
1081da177e4SLinus Torvalds 		arg->nvoices = MAX_OPL3_VOICES;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 	arg->oper = oss_callback;
1111da177e4SLinus Torvalds 	arg->private_data = opl3;
1121da177e4SLinus Torvalds 
1135e315e92SDave Jones 	if (snd_opl3_oss_create_port(opl3)) {
1141da177e4SLinus Torvalds 		/* register to OSS synth table */
1151da177e4SLinus Torvalds 		snd_device_register(opl3->card, dev);
1161da177e4SLinus Torvalds 	}
1175e315e92SDave Jones }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* unregister */
1205b1646a8STakashi Iwai void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	if (opl3->oss_seq_dev) {
123b1a3aa20STakashi Iwai 		/* The instance should have been released in prior */
1241da177e4SLinus Torvalds 		opl3->oss_seq_dev = NULL;
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* ------------------------------ */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /* open OSS sequencer */
1315b1646a8STakashi Iwai static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
1321da177e4SLinus Torvalds {
1335b1646a8STakashi Iwai 	struct snd_opl3 *opl3 = closure;
1341da177e4SLinus Torvalds 	int err;
1351da177e4SLinus Torvalds 
1365e246b85STakashi Iwai 	if (snd_BUG_ON(!arg))
1375e246b85STakashi Iwai 		return -ENXIO;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if ((err = snd_opl3_synth_setup(opl3)) < 0)
1401da177e4SLinus Torvalds 		return err;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	/* fill the argument data */
1431da177e4SLinus Torvalds 	arg->private_data = opl3;
1441da177e4SLinus Torvalds 	arg->addr.client = opl3->oss_chset->client;
1451da177e4SLinus Torvalds 	arg->addr.port = opl3->oss_chset->port;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
1481da177e4SLinus Torvalds 		return err;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
1511da177e4SLinus Torvalds 	return 0;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds /* close OSS sequencer */
1555b1646a8STakashi Iwai static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
1561da177e4SLinus Torvalds {
1575b1646a8STakashi Iwai 	struct snd_opl3 *opl3;
1581da177e4SLinus Torvalds 
1595e246b85STakashi Iwai 	if (snd_BUG_ON(!arg))
1605e246b85STakashi Iwai 		return -ENXIO;
1611da177e4SLinus Torvalds 	opl3 = arg->private_data;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	snd_opl3_synth_cleanup(opl3);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	snd_opl3_synth_use_dec(opl3);
1661da177e4SLinus Torvalds 	return 0;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /* load patch */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* from sound_config.h */
1721da177e4SLinus Torvalds #define SBFM_MAXINSTR	256
1731da177e4SLinus Torvalds 
1745b1646a8STakashi Iwai static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
1751da177e4SLinus Torvalds 				       const char __user *buf, int offs, int count)
1761da177e4SLinus Torvalds {
1775b1646a8STakashi Iwai 	struct snd_opl3 *opl3;
178224a0332STakashi Iwai 	struct sbi_instrument sbi;
179224a0332STakashi Iwai 	char name[32];
180224a0332STakashi Iwai 	int err, type;
1811da177e4SLinus Torvalds 
1825e246b85STakashi Iwai 	if (snd_BUG_ON(!arg))
1835e246b85STakashi Iwai 		return -ENXIO;
1841da177e4SLinus Torvalds 	opl3 = arg->private_data;
1851da177e4SLinus Torvalds 
186224a0332STakashi Iwai 	if (format == FM_PATCH)
187224a0332STakashi Iwai 		type = FM_PATCH_OPL2;
188224a0332STakashi Iwai 	else if (format == OPL3_PATCH)
189224a0332STakashi Iwai 		type = FM_PATCH_OPL3;
190224a0332STakashi Iwai 	else
191224a0332STakashi Iwai 		return -EINVAL;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	if (count < (int)sizeof(sbi)) {
19445203832STakashi Iwai 		snd_printk(KERN_ERR "FM Error: Patch record too short\n");
1951da177e4SLinus Torvalds 		return -EINVAL;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	if (copy_from_user(&sbi, buf, sizeof(sbi)))
1981da177e4SLinus Torvalds 		return -EFAULT;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
20145203832STakashi Iwai 		snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n",
202224a0332STakashi Iwai 			   sbi.channel);
2031da177e4SLinus Torvalds 		return -EINVAL;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
206224a0332STakashi Iwai 	memset(name, 0, sizeof(name));
207224a0332STakashi Iwai 	sprintf(name, "Chan%d", sbi.channel);
2081da177e4SLinus Torvalds 
209224a0332STakashi Iwai 	err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
210224a0332STakashi Iwai 				  sbi.operators);
211224a0332STakashi Iwai 	if (err < 0)
2121da177e4SLinus Torvalds 		return err;
213224a0332STakashi Iwai 
214224a0332STakashi Iwai 	return sizeof(sbi);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /* ioctl */
2185b1646a8STakashi Iwai static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
2191da177e4SLinus Torvalds 				  unsigned long ioarg)
2201da177e4SLinus Torvalds {
2215e246b85STakashi Iwai 	if (snd_BUG_ON(!arg))
2225e246b85STakashi Iwai 		return -ENXIO;
2231da177e4SLinus Torvalds 	switch (cmd) {
2241da177e4SLinus Torvalds 		case SNDCTL_FM_LOAD_INSTR:
22545203832STakashi Iwai 			snd_printk(KERN_ERR "OPL3: "
22645203832STakashi Iwai 				   "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. "
22745203832STakashi Iwai 				   "Fix the program.\n");
2281da177e4SLinus Torvalds 			return -EINVAL;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 		case SNDCTL_SYNTH_MEMAVL:
2311da177e4SLinus Torvalds 			return 0x7fffffff;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 		case SNDCTL_FM_4OP_ENABLE:
2341da177e4SLinus Torvalds 			// handled automatically by OPL instrument type
2351da177e4SLinus Torvalds 			return 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 		default:
2381da177e4SLinus Torvalds 			return -EINVAL;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	return 0;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /* reset device */
2445b1646a8STakashi Iwai static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
2451da177e4SLinus Torvalds {
2465e246b85STakashi Iwai 	if (snd_BUG_ON(!arg))
2475e246b85STakashi Iwai 		return -ENXIO;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	return 0;
2501da177e4SLinus Torvalds }
251