source: GPL/trunk/alsa-kernel/drivers/opl3/opl3_seq.c

Last change on this file was 703, checked in by David Azarewicz, 4 years ago

Merge changes from next branch.

File size: 7.2 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 *
5 * Midi Sequencer interface routines for OPL2/OPL3/OPL4 FM
6 *
7 * OPL2/3 FM instrument loader:
8 * alsa-tools/seq/sbiload/
9 */
10
11#include "opl3_voice.h"
12#include <linux/init.h>
13#include <linux/moduleparam.h>
14#include <linux/module.h>
15#include <sound/initval.h>
16
17MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
18MODULE_LICENSE("GPL");
19MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
20#ifdef TARGET_OS2
21#define KBUILD_MODNAME "opl3_seq"
22#endif
23
24bool use_internal_drums = 0;
25module_param(use_internal_drums, bool, 0444);
26MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
27
28int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
29{
30 if (!try_module_get(opl3->card->module))
31 return -EFAULT;
32 return 0;
33
34}
35
36void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
37{
38 module_put(opl3->card->module);
39}
40
41int snd_opl3_synth_setup(struct snd_opl3 * opl3)
42{
43 int idx;
44 struct snd_hwdep *hwdep = opl3->hwdep;
45
46 mutex_lock(&hwdep->open_mutex);
47 if (hwdep->used) {
48 mutex_unlock(&hwdep->open_mutex);
49 return -EBUSY;
50 }
51 hwdep->used++;
52 mutex_unlock(&hwdep->open_mutex);
53
54 snd_opl3_reset(opl3);
55
56 for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
57 opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
58 opl3->voices[idx].time = 0;
59 opl3->voices[idx].keyon_reg = 0x00;
60 }
61 opl3->use_time = 0;
62 opl3->connection_reg = 0x00;
63 if (opl3->hardware >= OPL3_HW_OPL3) {
64 /* Clear 4-op connections */
65 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
66 opl3->connection_reg);
67 opl3->max_voices = MAX_OPL3_VOICES;
68 }
69 return 0;
70}
71
72void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
73{
74 unsigned long flags;
75 struct snd_hwdep *hwdep;
76
77 /* Stop system timer */
78 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
79 if (opl3->sys_timer_status) {
80 del_timer(&opl3->tlist);
81 opl3->sys_timer_status = 0;
82 }
83 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
84
85 snd_opl3_reset(opl3);
86 hwdep = opl3->hwdep;
87 mutex_lock(&hwdep->open_mutex);
88 hwdep->used--;
89 mutex_unlock(&hwdep->open_mutex);
90 wake_up(&hwdep->open_wait);
91}
92
93static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
94{
95 struct snd_opl3 *opl3 = private_data;
96 int err;
97
98 err = snd_opl3_synth_setup(opl3);
99 if (err < 0)
100 return err;
101
102 if (use_internal_drums) {
103 /* Percussion mode */
104 opl3->voices[6].state = opl3->voices[7].state =
105 opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
106 snd_opl3_load_drums(opl3);
107 opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
108 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
109 } else {
110 opl3->drum_reg = 0x00;
111 }
112
113 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
114 err = snd_opl3_synth_use_inc(opl3);
115 if (err < 0)
116 return err;
117 }
118 opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
119 return 0;
120}
121
122static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
123{
124 struct snd_opl3 *opl3 = private_data;
125
126 snd_opl3_synth_cleanup(opl3);
127
128 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
129 snd_opl3_synth_use_dec(opl3);
130 return 0;
131}
132
133/*
134 * MIDI emulation operators
135 */
136const struct snd_midi_op opl3_ops = {
137 .note_on = snd_opl3_note_on,
138 .note_off = snd_opl3_note_off,
139 .key_press = snd_opl3_key_press,
140 .note_terminate = snd_opl3_terminate_note,
141 .control = snd_opl3_control,
142 .nrpn = snd_opl3_nrpn,
143 .sysex = snd_opl3_sysex,
144};
145
146static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
147 void *private_data, int atomic, int hop)
148{
149 struct snd_opl3 *opl3 = private_data;
150
151 snd_midi_process_event(&opl3_ops, ev, opl3->chset);
152 return 0;
153}
154
155/* ------------------------------ */
156
157static void snd_opl3_synth_free_port(void *private_data)
158{
159 struct snd_opl3 *opl3 = private_data;
160
161 snd_midi_channel_free_set(opl3->chset);
162}
163
164static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
165{
166 struct snd_seq_port_callback callbacks;
167 char name[32];
168 int voices, opl_ver;
169
170 voices = (opl3->hardware < OPL3_HW_OPL3) ?
171 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
172 opl3->chset = snd_midi_channel_alloc_set(16);
173 if (opl3->chset == NULL)
174 return -ENOMEM;
175 opl3->chset->private_data = opl3;
176
177 memset(&callbacks, 0, sizeof(callbacks));
178 callbacks.owner = THIS_MODULE;
179 callbacks.use = snd_opl3_synth_use;
180 callbacks.unuse = snd_opl3_synth_unuse;
181 callbacks.event_input = snd_opl3_synth_event_input;
182 callbacks.private_free = snd_opl3_synth_free_port;
183 callbacks.private_data = opl3;
184
185 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
186 sprintf(name, "OPL%i FM Port", opl_ver);
187
188 opl3->chset->client = opl3->seq_client;
189 opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
190 SNDRV_SEQ_PORT_CAP_WRITE |
191 SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
192 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
193 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
194 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
195 SNDRV_SEQ_PORT_TYPE_HARDWARE |
196 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
197 16, voices,
198 name);
199 if (opl3->chset->port < 0) {
200 int port;
201 port = opl3->chset->port;
202 snd_midi_channel_free_set(opl3->chset);
203 return port;
204 }
205 return 0;
206}
207
208/* ------------------------------ */
209
210static int snd_opl3_seq_probe(struct device *_dev)
211{
212 struct snd_seq_device *dev = to_seq_dev(_dev);
213 struct snd_opl3 *opl3;
214 int client, err;
215 char name[32];
216 int opl_ver;
217
218 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
219 if (opl3 == NULL)
220 return -EINVAL;
221
222 spin_lock_init(&opl3->voice_lock);
223
224 opl3->seq_client = -1;
225
226 /* allocate new client */
227 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
228 sprintf(name, "OPL%i FM synth", opl_ver);
229 client = opl3->seq_client =
230 snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
231 name);
232 if (client < 0)
233 return client;
234
235 err = snd_opl3_synth_create_port(opl3);
236 if (err < 0) {
237 snd_seq_delete_kernel_client(client);
238 opl3->seq_client = -1;
239 return err;
240 }
241
242 /* setup system timer */
243#ifndef TARGET_OS2
244 timer_setup(&opl3->tlist, snd_opl3_timer_func, 0);
245#else
246 init_timer(&opl3->tlist);
247 opl3->tlist.function = (void(*)(unsigned long))snd_opl3_timer_func;
248 opl3->tlist.data = (unsigned long) opl3;
249#endif
250 spin_lock_init(&opl3->sys_timer_lock);
251 opl3->sys_timer_status = 0;
252
253#if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
254 snd_opl3_init_seq_oss(opl3, name);
255#endif
256 return 0;
257}
258
259static int snd_opl3_seq_remove(struct device *_dev)
260{
261 struct snd_seq_device *dev = to_seq_dev(_dev);
262 struct snd_opl3 *opl3;
263
264 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
265 if (opl3 == NULL)
266 return -EINVAL;
267
268#if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
269 snd_opl3_free_seq_oss(opl3);
270#endif
271 if (opl3->seq_client >= 0) {
272 snd_seq_delete_kernel_client(opl3->seq_client);
273 opl3->seq_client = -1;
274 }
275 return 0;
276}
277
278static struct snd_seq_driver opl3_seq_driver = {
279 .driver = {
280 .name = KBUILD_MODNAME,
281 .probe = snd_opl3_seq_probe,
282 .remove = snd_opl3_seq_remove,
283 },
284 .id = SNDRV_SEQ_DEV_ID_OPL3,
285 .argsize = sizeof(struct snd_opl3 *),
286};
287
288module_snd_seq_driver(opl3_seq_driver);
Note: See TracBrowser for help on using the repository browser.