emux_seq.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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(sizeof(*p), GFP_KERNEL);
  115. if (!p)
  116. return NULL;
  117. p->chset.channels = kcalloc(max_channels, sizeof(*p->chset.channels),
  118. GFP_KERNEL);
  119. if (!p->chset.channels) {
  120. kfree(p);
  121. return NULL;
  122. }
  123. for (i = 0; i < max_channels; i++)
  124. p->chset.channels[i].number = i;
  125. p->chset.private_data = p;
  126. p->chset.max_channels = max_channels;
  127. p->emu = emu;
  128. p->chset.client = emu->client;
  129. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  130. snd_emux_create_effect(p);
  131. #endif
  132. callback->private_free = free_port;
  133. callback->private_data = p;
  134. cap = SNDRV_SEQ_PORT_CAP_WRITE;
  135. if (oss_port) {
  136. type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
  137. } else {
  138. type = DEFAULT_MIDI_TYPE;
  139. cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  140. }
  141. p->chset.port = snd_seq_event_port_attach(emu->client, callback,
  142. cap, type, max_channels,
  143. emu->max_voices, name);
  144. return p;
  145. }
  146. /*
  147. * release memory block for port
  148. */
  149. static void
  150. free_port(void *private_data)
  151. {
  152. struct snd_emux_port *p;
  153. p = private_data;
  154. if (p) {
  155. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  156. snd_emux_delete_effect(p);
  157. #endif
  158. kfree(p->chset.channels);
  159. kfree(p);
  160. }
  161. }
  162. #define DEFAULT_DRUM_FLAGS (1<<9)
  163. /*
  164. * initialize the port specific parameters
  165. */
  166. static void
  167. snd_emux_init_port(struct snd_emux_port *p)
  168. {
  169. p->drum_flags = DEFAULT_DRUM_FLAGS;
  170. p->volume_atten = 0;
  171. snd_emux_reset_port(p);
  172. }
  173. /*
  174. * reset port
  175. */
  176. void
  177. snd_emux_reset_port(struct snd_emux_port *port)
  178. {
  179. int i;
  180. /* stop all sounds */
  181. snd_emux_sounds_off_all(port);
  182. snd_midi_channel_set_clear(&port->chset);
  183. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  184. snd_emux_clear_effect(port);
  185. #endif
  186. /* set port specific control parameters */
  187. port->ctrls[EMUX_MD_DEF_BANK] = 0;
  188. port->ctrls[EMUX_MD_DEF_DRUM] = 0;
  189. port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
  190. for (i = 0; i < port->chset.max_channels; i++) {
  191. struct snd_midi_channel *chan = port->chset.channels + i;
  192. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  193. }
  194. }
  195. /*
  196. * input sequencer event
  197. */
  198. int
  199. snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
  200. int atomic, int hop)
  201. {
  202. struct snd_emux_port *port;
  203. port = private_data;
  204. if (snd_BUG_ON(!port || !ev))
  205. return -EINVAL;
  206. snd_midi_process_event(&emux_ops, ev, &port->chset);
  207. return 0;
  208. }
  209. /*
  210. * increment usage count
  211. */
  212. static int
  213. __snd_emux_inc_count(struct snd_emux *emu)
  214. {
  215. emu->used++;
  216. if (!try_module_get(emu->ops.owner))
  217. goto __error;
  218. if (!try_module_get(emu->card->module)) {
  219. module_put(emu->ops.owner);
  220. __error:
  221. emu->used--;
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. int snd_emux_inc_count(struct snd_emux *emu)
  227. {
  228. int ret;
  229. mutex_lock(&emu->register_mutex);
  230. ret = __snd_emux_inc_count(emu);
  231. mutex_unlock(&emu->register_mutex);
  232. return ret;
  233. }
  234. /*
  235. * decrease usage count
  236. */
  237. static void
  238. __snd_emux_dec_count(struct snd_emux *emu)
  239. {
  240. module_put(emu->card->module);
  241. emu->used--;
  242. if (emu->used <= 0)
  243. snd_emux_terminate_all(emu);
  244. module_put(emu->ops.owner);
  245. }
  246. void snd_emux_dec_count(struct snd_emux *emu)
  247. {
  248. mutex_lock(&emu->register_mutex);
  249. __snd_emux_dec_count(emu);
  250. mutex_unlock(&emu->register_mutex);
  251. }
  252. /*
  253. * Routine that is called upon a first use of a particular port
  254. */
  255. static int
  256. snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
  257. {
  258. struct snd_emux_port *p;
  259. struct snd_emux *emu;
  260. p = private_data;
  261. if (snd_BUG_ON(!p))
  262. return -EINVAL;
  263. emu = p->emu;
  264. if (snd_BUG_ON(!emu))
  265. return -EINVAL;
  266. mutex_lock(&emu->register_mutex);
  267. snd_emux_init_port(p);
  268. __snd_emux_inc_count(emu);
  269. mutex_unlock(&emu->register_mutex);
  270. return 0;
  271. }
  272. /*
  273. * Routine that is called upon the last unuse() of a particular port.
  274. */
  275. static int
  276. snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  277. {
  278. struct snd_emux_port *p;
  279. struct snd_emux *emu;
  280. p = private_data;
  281. if (snd_BUG_ON(!p))
  282. return -EINVAL;
  283. emu = p->emu;
  284. if (snd_BUG_ON(!emu))
  285. return -EINVAL;
  286. mutex_lock(&emu->register_mutex);
  287. snd_emux_sounds_off_all(p);
  288. __snd_emux_dec_count(emu);
  289. mutex_unlock(&emu->register_mutex);
  290. return 0;
  291. }
  292. /*
  293. * attach virtual rawmidi devices
  294. */
  295. int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
  296. {
  297. int i;
  298. emu->vmidi = NULL;
  299. if (emu->midi_ports <= 0)
  300. return 0;
  301. emu->vmidi = kcalloc(emu->midi_ports, sizeof(*emu->vmidi), GFP_KERNEL);
  302. if (!emu->vmidi)
  303. return -ENOMEM;
  304. for (i = 0; i < emu->midi_ports; i++) {
  305. struct snd_rawmidi *rmidi;
  306. struct snd_virmidi_dev *rdev;
  307. if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
  308. goto __error;
  309. rdev = rmidi->private_data;
  310. sprintf(rmidi->name, "%s Synth MIDI", emu->name);
  311. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
  312. rdev->client = emu->client;
  313. rdev->port = emu->ports[i];
  314. if (snd_device_register(card, rmidi) < 0) {
  315. snd_device_free(card, rmidi);
  316. goto __error;
  317. }
  318. emu->vmidi[i] = rmidi;
  319. }
  320. return 0;
  321. __error:
  322. snd_emux_delete_virmidi(emu);
  323. return -ENOMEM;
  324. }
  325. int snd_emux_delete_virmidi(struct snd_emux *emu)
  326. {
  327. int i;
  328. if (!emu->vmidi)
  329. return 0;
  330. for (i = 0; i < emu->midi_ports; i++) {
  331. if (emu->vmidi[i])
  332. snd_device_free(emu->card, emu->vmidi[i]);
  333. }
  334. kfree(emu->vmidi);
  335. emu->vmidi = NULL;
  336. return 0;
  337. }