emux_seq.c 9.0 KB

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