seq_midi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Generic MIDI synth driver for ALSA sequencer
  3. * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. * Jaroslav Kysela <perex@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /*
  22. Possible options for midisynth module:
  23. - automatic opening of midi ports on first received event or subscription
  24. (close will be performed when client leaves)
  25. */
  26. #include <sound/driver.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/moduleparam.h>
  32. #include <asm/semaphore.h>
  33. #include <sound/core.h>
  34. #include <sound/rawmidi.h>
  35. #include <sound/seq_kernel.h>
  36. #include <sound/seq_device.h>
  37. #include <sound/seq_midi_event.h>
  38. #include <sound/initval.h>
  39. MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@suse.cz>");
  40. MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
  41. MODULE_LICENSE("GPL");
  42. static int output_buffer_size = PAGE_SIZE;
  43. module_param(output_buffer_size, int, 0644);
  44. MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
  45. static int input_buffer_size = PAGE_SIZE;
  46. module_param(input_buffer_size, int, 0644);
  47. MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
  48. /* data for this midi synth driver */
  49. typedef struct {
  50. snd_card_t *card;
  51. int device;
  52. int subdevice;
  53. snd_rawmidi_file_t input_rfile;
  54. snd_rawmidi_file_t output_rfile;
  55. int seq_client;
  56. int seq_port;
  57. snd_midi_event_t *parser;
  58. } seq_midisynth_t;
  59. typedef struct {
  60. int seq_client;
  61. int num_ports;
  62. int ports_per_device[SNDRV_RAWMIDI_DEVICES];
  63. seq_midisynth_t *ports[SNDRV_RAWMIDI_DEVICES];
  64. } seq_midisynth_client_t;
  65. static seq_midisynth_client_t *synths[SNDRV_CARDS];
  66. static DECLARE_MUTEX(register_mutex);
  67. /* handle rawmidi input event (MIDI v1.0 stream) */
  68. static void snd_midi_input_event(snd_rawmidi_substream_t * substream)
  69. {
  70. snd_rawmidi_runtime_t *runtime;
  71. seq_midisynth_t *msynth;
  72. snd_seq_event_t ev;
  73. char buf[16], *pbuf;
  74. long res, count;
  75. if (substream == NULL)
  76. return;
  77. runtime = substream->runtime;
  78. msynth = (seq_midisynth_t *) runtime->private_data;
  79. if (msynth == NULL)
  80. return;
  81. memset(&ev, 0, sizeof(ev));
  82. while (runtime->avail > 0) {
  83. res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
  84. if (res <= 0)
  85. continue;
  86. if (msynth->parser == NULL)
  87. continue;
  88. pbuf = buf;
  89. while (res > 0) {
  90. count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev);
  91. if (count < 0)
  92. break;
  93. pbuf += count;
  94. res -= count;
  95. if (ev.type != SNDRV_SEQ_EVENT_NONE) {
  96. ev.source.port = msynth->seq_port;
  97. ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  98. snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
  99. /* clear event and reset header */
  100. memset(&ev, 0, sizeof(ev));
  101. }
  102. }
  103. }
  104. }
  105. static int dump_midi(snd_rawmidi_substream_t *substream, const char *buf, int count)
  106. {
  107. snd_rawmidi_runtime_t *runtime;
  108. int tmp;
  109. snd_assert(substream != NULL || buf != NULL, return -EINVAL);
  110. runtime = substream->runtime;
  111. if ((tmp = runtime->avail) < count) {
  112. snd_printd("warning, output event was lost (count = %i, available = %i)\n", count, tmp);
  113. return -ENOMEM;
  114. }
  115. if (snd_rawmidi_kernel_write(substream, buf, count) < count)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. static int event_process_midi(snd_seq_event_t * ev, int direct,
  120. void *private_data, int atomic, int hop)
  121. {
  122. seq_midisynth_t *msynth = (seq_midisynth_t *) private_data;
  123. unsigned char msg[10]; /* buffer for constructing midi messages */
  124. snd_rawmidi_substream_t *substream;
  125. int res;
  126. snd_assert(msynth != NULL, return -EINVAL);
  127. substream = msynth->output_rfile.output;
  128. if (substream == NULL)
  129. return -ENODEV;
  130. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
  131. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
  132. /* invalid event */
  133. snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
  134. return 0;
  135. }
  136. res = snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
  137. snd_midi_event_reset_decode(msynth->parser);
  138. if (res < 0)
  139. return res;
  140. } else {
  141. if (msynth->parser == NULL)
  142. return -EIO;
  143. res = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
  144. if (res < 0)
  145. return res;
  146. if ((res = dump_midi(substream, msg, res)) < 0) {
  147. snd_midi_event_reset_decode(msynth->parser);
  148. return res;
  149. }
  150. }
  151. return 0;
  152. }
  153. static int snd_seq_midisynth_new(seq_midisynth_t *msynth,
  154. snd_card_t *card,
  155. int device,
  156. int subdevice)
  157. {
  158. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
  159. return -ENOMEM;
  160. msynth->card = card;
  161. msynth->device = device;
  162. msynth->subdevice = subdevice;
  163. return 0;
  164. }
  165. /* open associated midi device for input */
  166. static int midisynth_subscribe(void *private_data, snd_seq_port_subscribe_t *info)
  167. {
  168. int err;
  169. seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
  170. snd_rawmidi_runtime_t *runtime;
  171. snd_rawmidi_params_t params;
  172. /* open midi port */
  173. if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_INPUT, &msynth->input_rfile)) < 0) {
  174. snd_printd("midi input open failed!!!\n");
  175. return err;
  176. }
  177. runtime = msynth->input_rfile.input->runtime;
  178. memset(&params, 0, sizeof(params));
  179. params.avail_min = 1;
  180. params.buffer_size = input_buffer_size;
  181. if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
  182. snd_rawmidi_kernel_release(&msynth->input_rfile);
  183. return err;
  184. }
  185. snd_midi_event_reset_encode(msynth->parser);
  186. runtime->event = snd_midi_input_event;
  187. runtime->private_data = msynth;
  188. snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
  189. return 0;
  190. }
  191. /* close associated midi device for input */
  192. static int midisynth_unsubscribe(void *private_data, snd_seq_port_subscribe_t *info)
  193. {
  194. int err;
  195. seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
  196. snd_assert(msynth->input_rfile.input != NULL, return -EINVAL);
  197. err = snd_rawmidi_kernel_release(&msynth->input_rfile);
  198. return err;
  199. }
  200. /* open associated midi device for output */
  201. static int midisynth_use(void *private_data, snd_seq_port_subscribe_t *info)
  202. {
  203. int err;
  204. seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
  205. snd_rawmidi_params_t params;
  206. /* open midi port */
  207. if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_OUTPUT, &msynth->output_rfile)) < 0) {
  208. snd_printd("midi output open failed!!!\n");
  209. return err;
  210. }
  211. memset(&params, 0, sizeof(params));
  212. params.avail_min = 1;
  213. params.buffer_size = output_buffer_size;
  214. if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, &params)) < 0) {
  215. snd_rawmidi_kernel_release(&msynth->output_rfile);
  216. return err;
  217. }
  218. snd_midi_event_reset_decode(msynth->parser);
  219. return 0;
  220. }
  221. /* close associated midi device for output */
  222. static int midisynth_unuse(void *private_data, snd_seq_port_subscribe_t *info)
  223. {
  224. seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
  225. unsigned char buf = 0xff; /* MIDI reset */
  226. snd_assert(msynth->output_rfile.output != NULL, return -EINVAL);
  227. /* sending single MIDI reset message to shut the device up */
  228. snd_rawmidi_kernel_write(msynth->output_rfile.output, &buf, 1);
  229. snd_rawmidi_drain_output(msynth->output_rfile.output);
  230. return snd_rawmidi_kernel_release(&msynth->output_rfile);
  231. }
  232. /* delete given midi synth port */
  233. static void snd_seq_midisynth_delete(seq_midisynth_t *msynth)
  234. {
  235. if (msynth == NULL)
  236. return;
  237. if (msynth->seq_client > 0) {
  238. /* delete port */
  239. snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
  240. }
  241. if (msynth->parser)
  242. snd_midi_event_free(msynth->parser);
  243. }
  244. /* set our client name */
  245. static int set_client_name(seq_midisynth_client_t *client, snd_card_t *card,
  246. snd_rawmidi_info_t *rmidi)
  247. {
  248. snd_seq_client_info_t cinfo;
  249. const char *name;
  250. memset(&cinfo, 0, sizeof(cinfo));
  251. cinfo.client = client->seq_client;
  252. cinfo.type = KERNEL_CLIENT;
  253. name = rmidi->name[0] ? (const char *)rmidi->name : "External MIDI";
  254. strlcpy(cinfo.name, name, sizeof(cinfo.name));
  255. return snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
  256. }
  257. /* register new midi synth port */
  258. static int
  259. snd_seq_midisynth_register_port(snd_seq_device_t *dev)
  260. {
  261. seq_midisynth_client_t *client;
  262. seq_midisynth_t *msynth, *ms;
  263. snd_seq_port_info_t *port;
  264. snd_rawmidi_info_t *info;
  265. int newclient = 0;
  266. unsigned int p, ports;
  267. snd_seq_client_callback_t callbacks;
  268. snd_seq_port_callback_t pcallbacks;
  269. snd_card_t *card = dev->card;
  270. int device = dev->device;
  271. unsigned int input_count = 0, output_count = 0;
  272. snd_assert(card != NULL && device >= 0 && device < SNDRV_RAWMIDI_DEVICES, return -EINVAL);
  273. info = kmalloc(sizeof(*info), GFP_KERNEL);
  274. if (! info)
  275. return -ENOMEM;
  276. info->device = device;
  277. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  278. info->subdevice = 0;
  279. if (snd_rawmidi_info_select(card, info) >= 0)
  280. output_count = info->subdevices_count;
  281. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  282. if (snd_rawmidi_info_select(card, info) >= 0) {
  283. input_count = info->subdevices_count;
  284. }
  285. ports = output_count;
  286. if (ports < input_count)
  287. ports = input_count;
  288. if (ports == 0) {
  289. kfree(info);
  290. return -ENODEV;
  291. }
  292. if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
  293. ports = 256 / SNDRV_RAWMIDI_DEVICES;
  294. down(&register_mutex);
  295. client = synths[card->number];
  296. if (client == NULL) {
  297. newclient = 1;
  298. client = kcalloc(1, sizeof(*client), GFP_KERNEL);
  299. if (client == NULL) {
  300. up(&register_mutex);
  301. kfree(info);
  302. return -ENOMEM;
  303. }
  304. memset(&callbacks, 0, sizeof(callbacks));
  305. callbacks.private_data = client;
  306. callbacks.allow_input = callbacks.allow_output = 1;
  307. client->seq_client = snd_seq_create_kernel_client(card, 0, &callbacks);
  308. if (client->seq_client < 0) {
  309. kfree(client);
  310. up(&register_mutex);
  311. kfree(info);
  312. return -ENOMEM;
  313. }
  314. set_client_name(client, card, info);
  315. } else if (device == 0)
  316. set_client_name(client, card, info); /* use the first device's name */
  317. msynth = kcalloc(ports, sizeof(seq_midisynth_t), GFP_KERNEL);
  318. port = kmalloc(sizeof(*port), GFP_KERNEL);
  319. if (msynth == NULL || port == NULL)
  320. goto __nomem;
  321. for (p = 0; p < ports; p++) {
  322. ms = &msynth[p];
  323. if (snd_seq_midisynth_new(ms, card, device, p) < 0)
  324. goto __nomem;
  325. /* declare port */
  326. memset(port, 0, sizeof(*port));
  327. port->addr.client = client->seq_client;
  328. port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
  329. port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
  330. memset(info, 0, sizeof(*info));
  331. info->device = device;
  332. if (p < output_count)
  333. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  334. else
  335. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  336. info->subdevice = p;
  337. if (snd_rawmidi_info_select(card, info) >= 0)
  338. strcpy(port->name, info->subname);
  339. if (! port->name[0]) {
  340. if (info->name[0]) {
  341. if (ports > 1)
  342. snprintf(port->name, sizeof(port->name), "%s-%d", info->name, p);
  343. else
  344. snprintf(port->name, sizeof(port->name), "%s", info->name);
  345. } else {
  346. /* last resort */
  347. if (ports > 1)
  348. sprintf(port->name, "MIDI %d-%d-%d", card->number, device, p);
  349. else
  350. sprintf(port->name, "MIDI %d-%d", card->number, device);
  351. }
  352. }
  353. if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
  354. port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  355. if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
  356. port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  357. if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
  358. info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
  359. port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  360. port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC;
  361. port->midi_channels = 16;
  362. memset(&pcallbacks, 0, sizeof(pcallbacks));
  363. pcallbacks.owner = THIS_MODULE;
  364. pcallbacks.private_data = ms;
  365. pcallbacks.subscribe = midisynth_subscribe;
  366. pcallbacks.unsubscribe = midisynth_unsubscribe;
  367. pcallbacks.use = midisynth_use;
  368. pcallbacks.unuse = midisynth_unuse;
  369. pcallbacks.event_input = event_process_midi;
  370. port->kernel = &pcallbacks;
  371. if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
  372. goto __nomem;
  373. ms->seq_client = client->seq_client;
  374. ms->seq_port = port->addr.port;
  375. }
  376. client->ports_per_device[device] = ports;
  377. client->ports[device] = msynth;
  378. client->num_ports++;
  379. if (newclient)
  380. synths[card->number] = client;
  381. up(&register_mutex);
  382. kfree(info);
  383. kfree(port);
  384. return 0; /* success */
  385. __nomem:
  386. if (msynth != NULL) {
  387. for (p = 0; p < ports; p++)
  388. snd_seq_midisynth_delete(&msynth[p]);
  389. kfree(msynth);
  390. }
  391. if (newclient) {
  392. snd_seq_delete_kernel_client(client->seq_client);
  393. kfree(client);
  394. }
  395. kfree(info);
  396. kfree(port);
  397. up(&register_mutex);
  398. return -ENOMEM;
  399. }
  400. /* release midi synth port */
  401. static int
  402. snd_seq_midisynth_unregister_port(snd_seq_device_t *dev)
  403. {
  404. seq_midisynth_client_t *client;
  405. seq_midisynth_t *msynth;
  406. snd_card_t *card = dev->card;
  407. int device = dev->device, p, ports;
  408. down(&register_mutex);
  409. client = synths[card->number];
  410. if (client == NULL || client->ports[device] == NULL) {
  411. up(&register_mutex);
  412. return -ENODEV;
  413. }
  414. ports = client->ports_per_device[device];
  415. client->ports_per_device[device] = 0;
  416. msynth = client->ports[device];
  417. client->ports[device] = NULL;
  418. snd_runtime_check(msynth != NULL || ports <= 0, goto __skip);
  419. for (p = 0; p < ports; p++)
  420. snd_seq_midisynth_delete(&msynth[p]);
  421. kfree(msynth);
  422. __skip:
  423. client->num_ports--;
  424. if (client->num_ports <= 0) {
  425. snd_seq_delete_kernel_client(client->seq_client);
  426. synths[card->number] = NULL;
  427. kfree(client);
  428. }
  429. up(&register_mutex);
  430. return 0;
  431. }
  432. static int __init alsa_seq_midi_init(void)
  433. {
  434. static snd_seq_dev_ops_t ops = {
  435. snd_seq_midisynth_register_port,
  436. snd_seq_midisynth_unregister_port,
  437. };
  438. memset(&synths, 0, sizeof(synths));
  439. snd_seq_autoload_lock();
  440. snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH, &ops, 0);
  441. snd_seq_autoload_unlock();
  442. return 0;
  443. }
  444. static void __exit alsa_seq_midi_exit(void)
  445. {
  446. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH);
  447. }
  448. module_init(alsa_seq_midi_init)
  449. module_exit(alsa_seq_midi_exit)