emux_seq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Midi Sequencer interface routines.
  4. *
  5. * Copyright (C) 1999 Steve Ratcliffe
  6. * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  7. */
  8. #include "emux_voice.h"
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. /* Prototypes for static functions */
  12. static void free_port(void *private);
  13. static void snd_emux_init_port(struct snd_emux_port *p);
  14. static int snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info);
  15. static int snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info);
  16. /*
  17. * MIDI emulation operators
  18. */
  19. static const struct snd_midi_op emux_ops = {
  20. .note_on = snd_emux_note_on,
  21. .note_off = snd_emux_note_off,
  22. .key_press = snd_emux_key_press,
  23. .note_terminate = snd_emux_terminate_note,
  24. .control = snd_emux_control,
  25. .nrpn = snd_emux_nrpn,
  26. .sysex = snd_emux_sysex,
  27. };
  28. /*
  29. * number of MIDI channels
  30. */
  31. #define MIDI_CHANNELS 16
  32. /*
  33. * type flags for MIDI sequencer port
  34. */
  35. #define DEFAULT_MIDI_TYPE (SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
  36. SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
  37. SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
  38. SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
  39. SNDRV_SEQ_PORT_TYPE_HARDWARE |\
  40. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  41. /*
  42. * Initialise the EMUX Synth by creating a client and registering
  43. * a series of ports.
  44. * Each of the ports will contain the 16 midi channels. Applications
  45. * can connect to these ports to play midi data.
  46. */
  47. int
  48. snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
  49. {
  50. int i;
  51. struct snd_seq_port_callback pinfo;
  52. char tmpname[64];
  53. emu->client = snd_seq_create_kernel_client(card, index,
  54. "%s WaveTable", emu->name);
  55. if (emu->client < 0) {
  56. dev_err(card->dev, "can't create client\n");
  57. return -ENODEV;
  58. }
  59. if (emu->num_ports <= 0) {
  60. dev_warn(card->dev, "seqports must be greater than zero\n");
  61. emu->num_ports = 1;
  62. } else if (emu->num_ports > SNDRV_EMUX_MAX_PORTS) {
  63. dev_warn(card->dev,
  64. "too many ports. limited max. ports %d\n",
  65. SNDRV_EMUX_MAX_PORTS);
  66. emu->num_ports = SNDRV_EMUX_MAX_PORTS;
  67. }
  68. memset(&pinfo, 0, sizeof(pinfo));
  69. pinfo.owner = THIS_MODULE;
  70. pinfo.use = snd_emux_use;
  71. pinfo.unuse = snd_emux_unuse;
  72. pinfo.event_input = snd_emux_event_input;
  73. for (i = 0; i < emu->num_ports; i++) {
  74. struct snd_emux_port *p;
  75. sprintf(tmpname, "%s Port %d", emu->name, i);
  76. p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
  77. 0, &pinfo);
  78. if (!p) {
  79. dev_err(card->dev, "can't create port\n");
  80. return -ENOMEM;
  81. }
  82. p->port_mode = SNDRV_EMUX_PORT_MODE_MIDI;
  83. snd_emux_init_port(p);
  84. emu->ports[i] = p->chset.port;
  85. emu->portptrs[i] = p;
  86. }
  87. return 0;
  88. }
  89. /*
  90. * Detach from the ports that were set up for this synthesizer and
  91. * destroy the kernel client.
  92. */
  93. void
  94. snd_emux_detach_seq(struct snd_emux *emu)
  95. {
  96. if (emu->voices)
  97. snd_emux_terminate_all(emu);
  98. if (emu->client >= 0) {
  99. snd_seq_delete_kernel_client(emu->client);
  100. emu->client = -1;
  101. }
  102. }
  103. /*
  104. * create a sequencer port and channel_set
  105. */
  106. struct snd_emux_port *
  107. snd_emux_create_port(struct snd_emux *emu, char *name,
  108. int max_channels, int oss_port,
  109. struct snd_seq_port_callback *callback)
  110. {
  111. struct snd_emux_port *p;
  112. int i, type, cap;
  113. /* Allocate structures for this channel */
  114. p = kzalloc_obj(*p);
  115. if (!p)
  116. return NULL;
  117. p->chset.channels = kzalloc_objs(*p->chset.channels, max_channels);
  118. if (!p->chset.channels) {
  119. kfree(p);
  120. return NULL;
  121. }
  122. for (i = 0; i < max_channels; i++)
  123. p->chset.channels[i].number = i;
  124. p->chset.private_data = p;
  125. p->chset.max_channels = max_channels;
  126. p->emu = emu;
  127. p->chset.client = emu->client;
  128. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  129. snd_emux_create_effect(p);
  130. #endif
  131. callback->private_free = free_port;
  132. callback->private_data = p;
  133. cap = SNDRV_SEQ_PORT_CAP_WRITE;
  134. if (oss_port) {
  135. type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
  136. } else {
  137. type = DEFAULT_MIDI_TYPE;
  138. cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  139. }
  140. p->chset.port = snd_seq_event_port_attach(emu->client, callback,
  141. cap, type, max_channels,
  142. emu->max_voices, name);
  143. return p;
  144. }
  145. /*
  146. * release memory block for port
  147. */
  148. static void
  149. free_port(void *private_data)
  150. {
  151. struct snd_emux_port *p;
  152. p = private_data;
  153. if (p) {
  154. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  155. snd_emux_delete_effect(p);
  156. #endif
  157. kfree(p->chset.channels);
  158. kfree(p);
  159. }
  160. }
  161. #define DEFAULT_DRUM_FLAGS (1<<9)
  162. /*
  163. * initialize the port specific parameters
  164. */
  165. static void
  166. snd_emux_init_port(struct snd_emux_port *p)
  167. {
  168. p->drum_flags = DEFAULT_DRUM_FLAGS;
  169. p->volume_atten = 0;
  170. snd_emux_reset_port(p);
  171. }
  172. /*
  173. * reset port
  174. */
  175. void
  176. snd_emux_reset_port(struct snd_emux_port *port)
  177. {
  178. int i;
  179. /* stop all sounds */
  180. snd_emux_sounds_off_all(port);
  181. snd_midi_channel_set_clear(&port->chset);
  182. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  183. snd_emux_clear_effect(port);
  184. #endif
  185. /* set port specific control parameters */
  186. port->ctrls[EMUX_MD_DEF_BANK] = 0;
  187. port->ctrls[EMUX_MD_DEF_DRUM] = 0;
  188. port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
  189. for (i = 0; i < port->chset.max_channels; i++) {
  190. struct snd_midi_channel *chan = port->chset.channels + i;
  191. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  192. }
  193. }
  194. /*
  195. * input sequencer event
  196. */
  197. int
  198. snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
  199. int atomic, int hop)
  200. {
  201. struct snd_emux_port *port;
  202. port = private_data;
  203. if (snd_BUG_ON(!port || !ev))
  204. return -EINVAL;
  205. snd_midi_process_event(&emux_ops, ev, &port->chset);
  206. return 0;
  207. }
  208. /*
  209. * increment usage count
  210. */
  211. static int
  212. __snd_emux_inc_count(struct snd_emux *emu)
  213. {
  214. emu->used++;
  215. if (!try_module_get(emu->ops.owner))
  216. goto __error;
  217. if (!try_module_get(emu->card->module)) {
  218. module_put(emu->ops.owner);
  219. __error:
  220. emu->used--;
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. int snd_emux_inc_count(struct snd_emux *emu)
  226. {
  227. guard(mutex)(&emu->register_mutex);
  228. return __snd_emux_inc_count(emu);
  229. }
  230. /*
  231. * decrease usage count
  232. */
  233. static void
  234. __snd_emux_dec_count(struct snd_emux *emu)
  235. {
  236. module_put(emu->card->module);
  237. emu->used--;
  238. if (emu->used <= 0)
  239. snd_emux_terminate_all(emu);
  240. module_put(emu->ops.owner);
  241. }
  242. void snd_emux_dec_count(struct snd_emux *emu)
  243. {
  244. guard(mutex)(&emu->register_mutex);
  245. __snd_emux_dec_count(emu);
  246. }
  247. /*
  248. * Routine that is called upon a first use of a particular port
  249. */
  250. static int
  251. snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
  252. {
  253. struct snd_emux_port *p;
  254. struct snd_emux *emu;
  255. p = private_data;
  256. if (snd_BUG_ON(!p))
  257. return -EINVAL;
  258. emu = p->emu;
  259. if (snd_BUG_ON(!emu))
  260. return -EINVAL;
  261. guard(mutex)(&emu->register_mutex);
  262. snd_emux_init_port(p);
  263. __snd_emux_inc_count(emu);
  264. return 0;
  265. }
  266. /*
  267. * Routine that is called upon the last unuse() of a particular port.
  268. */
  269. static int
  270. snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  271. {
  272. struct snd_emux_port *p;
  273. struct snd_emux *emu;
  274. p = private_data;
  275. if (snd_BUG_ON(!p))
  276. return -EINVAL;
  277. emu = p->emu;
  278. if (snd_BUG_ON(!emu))
  279. return -EINVAL;
  280. guard(mutex)(&emu->register_mutex);
  281. snd_emux_sounds_off_all(p);
  282. __snd_emux_dec_count(emu);
  283. return 0;
  284. }
  285. /*
  286. * attach virtual rawmidi devices
  287. */
  288. int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
  289. {
  290. int i;
  291. emu->vmidi = NULL;
  292. if (emu->midi_ports <= 0)
  293. return 0;
  294. emu->vmidi = kzalloc_objs(*emu->vmidi, emu->midi_ports);
  295. if (!emu->vmidi)
  296. return -ENOMEM;
  297. for (i = 0; i < emu->midi_ports; i++) {
  298. struct snd_rawmidi *rmidi;
  299. struct snd_virmidi_dev *rdev;
  300. if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
  301. goto __error;
  302. rdev = rmidi->private_data;
  303. sprintf(rmidi->name, "%s Synth MIDI", emu->name);
  304. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
  305. rdev->client = emu->client;
  306. rdev->port = emu->ports[i];
  307. if (snd_device_register(card, rmidi) < 0) {
  308. snd_device_free(card, rmidi);
  309. goto __error;
  310. }
  311. emu->vmidi[i] = rmidi;
  312. }
  313. return 0;
  314. __error:
  315. snd_emux_delete_virmidi(emu);
  316. return -ENOMEM;
  317. }
  318. int snd_emux_delete_virmidi(struct snd_emux *emu)
  319. {
  320. int i;
  321. if (!emu->vmidi)
  322. return 0;
  323. for (i = 0; i < emu->midi_ports; i++) {
  324. if (emu->vmidi[i])
  325. snd_device_free(emu->card, emu->vmidi[i]);
  326. }
  327. kfree(emu->vmidi);
  328. emu->vmidi = NULL;
  329. return 0;
  330. }