xref: /OK3568_Linux_fs/kernel/sound/drivers/opl4/opl4_seq.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * OPL4 sequencer functions
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
5*4882a593Smuzhiyun  * All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
9*4882a593Smuzhiyun  * are met:
10*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
11*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
12*4882a593Smuzhiyun  *    without modification.
13*4882a593Smuzhiyun  * 2. The name of the author may not be used to endorse or promote products
14*4882a593Smuzhiyun  *    derived from this software without specific prior written permission.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Alternatively, this software may be distributed and/or modified under the
17*4882a593Smuzhiyun  * terms of the GNU General Public License as published by the Free Software
18*4882a593Smuzhiyun  * Foundation; either version 2 of the License, or (at your option) any later
19*4882a593Smuzhiyun  * version.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22*4882a593Smuzhiyun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25*4882a593Smuzhiyun  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*4882a593Smuzhiyun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*4882a593Smuzhiyun  * SUCH DAMAGE.
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include "opl4_local.h"
35*4882a593Smuzhiyun #include <linux/init.h>
36*4882a593Smuzhiyun #include <linux/moduleparam.h>
37*4882a593Smuzhiyun #include <linux/module.h>
38*4882a593Smuzhiyun #include <sound/initval.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
41*4882a593Smuzhiyun MODULE_DESCRIPTION("OPL4 wavetable synth driver");
42*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun int volume_boost = 8;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun module_param(volume_boost, int, 0644);
47*4882a593Smuzhiyun MODULE_PARM_DESC(volume_boost, "Additional volume for OPL4 wavetable sounds.");
48*4882a593Smuzhiyun 
snd_opl4_seq_use_inc(struct snd_opl4 * opl4)49*4882a593Smuzhiyun static int snd_opl4_seq_use_inc(struct snd_opl4 *opl4)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	if (!try_module_get(opl4->card->module))
52*4882a593Smuzhiyun 		return -EFAULT;
53*4882a593Smuzhiyun 	return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
snd_opl4_seq_use_dec(struct snd_opl4 * opl4)56*4882a593Smuzhiyun static void snd_opl4_seq_use_dec(struct snd_opl4 *opl4)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	module_put(opl4->card->module);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
snd_opl4_seq_use(void * private_data,struct snd_seq_port_subscribe * info)61*4882a593Smuzhiyun static int snd_opl4_seq_use(void *private_data, struct snd_seq_port_subscribe *info)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = private_data;
64*4882a593Smuzhiyun 	int err;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	mutex_lock(&opl4->access_mutex);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (opl4->used) {
69*4882a593Smuzhiyun 		mutex_unlock(&opl4->access_mutex);
70*4882a593Smuzhiyun 		return -EBUSY;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 	opl4->used++;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
75*4882a593Smuzhiyun 		err = snd_opl4_seq_use_inc(opl4);
76*4882a593Smuzhiyun 		if (err < 0) {
77*4882a593Smuzhiyun 			mutex_unlock(&opl4->access_mutex);
78*4882a593Smuzhiyun 			return err;
79*4882a593Smuzhiyun 		}
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	mutex_unlock(&opl4->access_mutex);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	snd_opl4_synth_reset(opl4);
85*4882a593Smuzhiyun 	return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
snd_opl4_seq_unuse(void * private_data,struct snd_seq_port_subscribe * info)88*4882a593Smuzhiyun static int snd_opl4_seq_unuse(void *private_data, struct snd_seq_port_subscribe *info)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = private_data;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	snd_opl4_synth_shutdown(opl4);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	mutex_lock(&opl4->access_mutex);
95*4882a593Smuzhiyun 	opl4->used--;
96*4882a593Smuzhiyun 	mutex_unlock(&opl4->access_mutex);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
99*4882a593Smuzhiyun 		snd_opl4_seq_use_dec(opl4);
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun static const struct snd_midi_op opl4_ops = {
104*4882a593Smuzhiyun 	.note_on =		snd_opl4_note_on,
105*4882a593Smuzhiyun 	.note_off =		snd_opl4_note_off,
106*4882a593Smuzhiyun 	.note_terminate =	snd_opl4_terminate_note,
107*4882a593Smuzhiyun 	.control =		snd_opl4_control,
108*4882a593Smuzhiyun 	.sysex =		snd_opl4_sysex,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
snd_opl4_seq_event_input(struct snd_seq_event * ev,int direct,void * private_data,int atomic,int hop)111*4882a593Smuzhiyun static int snd_opl4_seq_event_input(struct snd_seq_event *ev, int direct,
112*4882a593Smuzhiyun 				    void *private_data, int atomic, int hop)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = private_data;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	snd_midi_process_event(&opl4_ops, ev, opl4->chset);
117*4882a593Smuzhiyun 	return 0;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
snd_opl4_seq_free_port(void * private_data)120*4882a593Smuzhiyun static void snd_opl4_seq_free_port(void *private_data)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = private_data;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	snd_midi_channel_free_set(opl4->chset);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
snd_opl4_seq_probe(struct device * _dev)127*4882a593Smuzhiyun static int snd_opl4_seq_probe(struct device *_dev)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct snd_seq_device *dev = to_seq_dev(_dev);
130*4882a593Smuzhiyun 	struct snd_opl4 *opl4;
131*4882a593Smuzhiyun 	int client;
132*4882a593Smuzhiyun 	struct snd_seq_port_callback pcallbacks;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	opl4 = *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
135*4882a593Smuzhiyun 	if (!opl4)
136*4882a593Smuzhiyun 		return -EINVAL;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	if (snd_yrw801_detect(opl4) < 0)
139*4882a593Smuzhiyun 		return -ENODEV;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	opl4->chset = snd_midi_channel_alloc_set(16);
142*4882a593Smuzhiyun 	if (!opl4->chset)
143*4882a593Smuzhiyun 		return -ENOMEM;
144*4882a593Smuzhiyun 	opl4->chset->private_data = opl4;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* allocate new client */
147*4882a593Smuzhiyun 	client = snd_seq_create_kernel_client(opl4->card, opl4->seq_dev_num,
148*4882a593Smuzhiyun 					      "OPL4 Wavetable");
149*4882a593Smuzhiyun 	if (client < 0) {
150*4882a593Smuzhiyun 		snd_midi_channel_free_set(opl4->chset);
151*4882a593Smuzhiyun 		return client;
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 	opl4->seq_client = client;
154*4882a593Smuzhiyun 	opl4->chset->client = client;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	/* create new port */
157*4882a593Smuzhiyun 	memset(&pcallbacks, 0, sizeof(pcallbacks));
158*4882a593Smuzhiyun 	pcallbacks.owner = THIS_MODULE;
159*4882a593Smuzhiyun 	pcallbacks.use = snd_opl4_seq_use;
160*4882a593Smuzhiyun 	pcallbacks.unuse = snd_opl4_seq_unuse;
161*4882a593Smuzhiyun 	pcallbacks.event_input = snd_opl4_seq_event_input;
162*4882a593Smuzhiyun 	pcallbacks.private_free = snd_opl4_seq_free_port;
163*4882a593Smuzhiyun 	pcallbacks.private_data = opl4;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	opl4->chset->port = snd_seq_event_port_attach(client, &pcallbacks,
166*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_CAP_WRITE |
167*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
168*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
169*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_TYPE_MIDI_GM |
170*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_TYPE_HARDWARE |
171*4882a593Smuzhiyun 						      SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
172*4882a593Smuzhiyun 						      16, 24,
173*4882a593Smuzhiyun 						      "OPL4 Wavetable Port");
174*4882a593Smuzhiyun 	if (opl4->chset->port < 0) {
175*4882a593Smuzhiyun 		int err = opl4->chset->port;
176*4882a593Smuzhiyun 		snd_midi_channel_free_set(opl4->chset);
177*4882a593Smuzhiyun 		snd_seq_delete_kernel_client(client);
178*4882a593Smuzhiyun 		opl4->seq_client = -1;
179*4882a593Smuzhiyun 		return err;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
snd_opl4_seq_remove(struct device * _dev)184*4882a593Smuzhiyun static int snd_opl4_seq_remove(struct device *_dev)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct snd_seq_device *dev = to_seq_dev(_dev);
187*4882a593Smuzhiyun 	struct snd_opl4 *opl4;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	opl4 = *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
190*4882a593Smuzhiyun 	if (!opl4)
191*4882a593Smuzhiyun 		return -EINVAL;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (opl4->seq_client >= 0) {
194*4882a593Smuzhiyun 		snd_seq_delete_kernel_client(opl4->seq_client);
195*4882a593Smuzhiyun 		opl4->seq_client = -1;
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun static struct snd_seq_driver opl4_seq_driver = {
201*4882a593Smuzhiyun 	.driver = {
202*4882a593Smuzhiyun 		.name = KBUILD_MODNAME,
203*4882a593Smuzhiyun 		.probe = snd_opl4_seq_probe,
204*4882a593Smuzhiyun 		.remove = snd_opl4_seq_remove,
205*4882a593Smuzhiyun 	},
206*4882a593Smuzhiyun 	.id = SNDRV_SEQ_DEV_ID_OPL4,
207*4882a593Smuzhiyun 	.argsize = sizeof(struct snd_opl4 *),
208*4882a593Smuzhiyun };
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun module_snd_seq_driver(opl4_seq_driver);
211