seq_midi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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@perex.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 <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/module.h>
  31. #include <linux/mutex.h>
  32. #include <sound/core.h>
  33. #include <sound/rawmidi.h>
  34. #include <sound/seq_kernel.h>
  35. #include <sound/seq_device.h>
  36. #include <sound/seq_midi_event.h>
  37. #include <sound/initval.h>
  38. MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@perex.cz>");
  39. MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
  40. MODULE_LICENSE("GPL");
  41. static int output_buffer_size = PAGE_SIZE;
  42. module_param(output_buffer_size, int, 0644);
  43. MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
  44. static int input_buffer_size = PAGE_SIZE;
  45. module_param(input_buffer_size, int, 0644);
  46. MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
  47. /* data for this midi synth driver */
  48. struct seq_midisynth {
  49. struct snd_card *card;
  50. int device;
  51. int subdevice;
  52. struct snd_rawmidi_file input_rfile;
  53. struct snd_rawmidi_file output_rfile;
  54. int seq_client;
  55. int seq_port;
  56. struct snd_midi_event *parser;
  57. };
  58. struct seq_midisynth_client {
  59. int seq_client;
  60. int num_ports;
  61. int ports_per_device[SNDRV_RAWMIDI_DEVICES];
  62. struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
  63. };
  64. static struct seq_midisynth_client *synths[SNDRV_CARDS];
  65. static DEFINE_MUTEX(register_mutex);
  66. /* handle rawmidi input event (MIDI v1.0 stream) */
  67. static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
  68. {
  69. struct snd_rawmidi_runtime *runtime;
  70. struct seq_midisynth *msynth;
  71. struct snd_seq_event ev;
  72. char buf[16], *pbuf;
  73. long res, count;
  74. if (substream == NULL)
  75. return;
  76. runtime = substream->runtime;
  77. msynth = runtime->private_data;
  78. if (msynth == NULL)
  79. return;
  80. memset(&ev, 0, sizeof(ev));
  81. while (runtime->avail > 0) {
  82. res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
  83. if (res <= 0)
  84. continue;
  85. if (msynth->parser == NULL)
  86. continue;
  87. pbuf = buf;
  88. while (res > 0) {
  89. count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev);
  90. if (count < 0)
  91. break;
  92. pbuf += count;
  93. res -= count;
  94. if (ev.type != SNDRV_SEQ_EVENT_NONE) {
  95. ev.source.port = msynth->seq_port;
  96. ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  97. snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
  98. /* clear event and reset header */
  99. memset(&ev, 0, sizeof(ev));
  100. }
  101. }
  102. }
  103. }
  104. static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
  105. {
  106. struct snd_rawmidi_runtime *runtime;
  107. int tmp;
  108. if (snd_BUG_ON(!substream || !buf))
  109. return -EINVAL;
  110. runtime = substream->runtime;
  111. if ((tmp = runtime->avail) < count) {
  112. if (printk_ratelimit())
  113. pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
  114. return -ENOMEM;
  115. }
  116. if (snd_rawmidi_kernel_write(substream, buf, count) < count)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. static int event_process_midi(struct snd_seq_event *ev, int direct,
  121. void *private_data, int atomic, int hop)
  122. {
  123. struct seq_midisynth *msynth = private_data;
  124. unsigned char msg[10]; /* buffer for constructing midi messages */
  125. struct snd_rawmidi_substream *substream;
  126. int len;
  127. if (snd_BUG_ON(!msynth))
  128. return -EINVAL;
  129. substream = msynth->output_rfile.output;
  130. if (substream == NULL)
  131. return -ENODEV;
  132. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
  133. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
  134. /* invalid event */
  135. pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
  136. return 0;
  137. }
  138. snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
  139. snd_midi_event_reset_decode(msynth->parser);
  140. } else {
  141. if (msynth->parser == NULL)
  142. return -EIO;
  143. len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
  144. if (len < 0)
  145. return 0;
  146. if (dump_midi(substream, msg, len) < 0)
  147. snd_midi_event_reset_decode(msynth->parser);
  148. }
  149. return 0;
  150. }
  151. static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
  152. struct snd_card *card,
  153. int device,
  154. int subdevice)
  155. {
  156. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
  157. return -ENOMEM;
  158. msynth->card = card;
  159. msynth->device = device;
  160. msynth->subdevice = subdevice;
  161. return 0;
  162. }
  163. /* open associated midi device for input */
  164. static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
  165. {
  166. int err;
  167. struct seq_midisynth *msynth = private_data;
  168. struct snd_rawmidi_runtime *runtime;
  169. struct snd_rawmidi_params params;
  170. /* open midi port */
  171. if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
  172. msynth->subdevice,
  173. SNDRV_RAWMIDI_LFLG_INPUT,
  174. &msynth->input_rfile)) < 0) {
  175. pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
  176. return err;
  177. }
  178. runtime = msynth->input_rfile.input->runtime;
  179. memset(&params, 0, sizeof(params));
  180. params.avail_min = 1;
  181. params.buffer_size = input_buffer_size;
  182. if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
  183. snd_rawmidi_kernel_release(&msynth->input_rfile);
  184. return err;
  185. }
  186. snd_midi_event_reset_encode(msynth->parser);
  187. runtime->event = snd_midi_input_event;
  188. runtime->private_data = msynth;
  189. snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
  190. return 0;
  191. }
  192. /* close associated midi device for input */
  193. static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
  194. {
  195. int err;
  196. struct seq_midisynth *msynth = private_data;
  197. if (snd_BUG_ON(!msynth->input_rfile.input))
  198. return -EINVAL;
  199. err = snd_rawmidi_kernel_release(&msynth->input_rfile);
  200. return err;
  201. }
  202. /* open associated midi device for output */
  203. static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
  204. {
  205. int err;
  206. struct seq_midisynth *msynth = private_data;
  207. struct snd_rawmidi_params params;
  208. /* open midi port */
  209. if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
  210. msynth->subdevice,
  211. SNDRV_RAWMIDI_LFLG_OUTPUT,
  212. &msynth->output_rfile)) < 0) {
  213. pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
  214. return err;
  215. }
  216. memset(&params, 0, sizeof(params));
  217. params.avail_min = 1;
  218. params.buffer_size = output_buffer_size;
  219. params.no_active_sensing = 1;
  220. if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, &params)) < 0) {
  221. snd_rawmidi_kernel_release(&msynth->output_rfile);
  222. return err;
  223. }
  224. snd_midi_event_reset_decode(msynth->parser);
  225. return 0;
  226. }
  227. /* close associated midi device for output */
  228. static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  229. {
  230. struct seq_midisynth *msynth = private_data;
  231. if (snd_BUG_ON(!msynth->output_rfile.output))
  232. return -EINVAL;
  233. snd_rawmidi_drain_output(msynth->output_rfile.output);
  234. return snd_rawmidi_kernel_release(&msynth->output_rfile);
  235. }
  236. /* delete given midi synth port */
  237. static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
  238. {
  239. if (msynth == NULL)
  240. return;
  241. if (msynth->seq_client > 0) {
  242. /* delete port */
  243. snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
  244. }
  245. snd_midi_event_free(msynth->parser);
  246. }
  247. /* register new midi synth port */
  248. static int
  249. snd_seq_midisynth_probe(struct device *_dev)
  250. {
  251. struct snd_seq_device *dev = to_seq_dev(_dev);
  252. struct seq_midisynth_client *client;
  253. struct seq_midisynth *msynth, *ms;
  254. struct snd_seq_port_info *port;
  255. struct snd_rawmidi_info *info;
  256. struct snd_rawmidi *rmidi = dev->private_data;
  257. int newclient = 0;
  258. unsigned int p, ports;
  259. struct snd_seq_port_callback pcallbacks;
  260. struct snd_card *card = dev->card;
  261. int device = dev->device;
  262. unsigned int input_count = 0, output_count = 0;
  263. if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
  264. return -EINVAL;
  265. info = kmalloc(sizeof(*info), GFP_KERNEL);
  266. if (! info)
  267. return -ENOMEM;
  268. info->device = device;
  269. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  270. info->subdevice = 0;
  271. if (snd_rawmidi_info_select(card, info) >= 0)
  272. output_count = info->subdevices_count;
  273. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  274. if (snd_rawmidi_info_select(card, info) >= 0) {
  275. input_count = info->subdevices_count;
  276. }
  277. ports = output_count;
  278. if (ports < input_count)
  279. ports = input_count;
  280. if (ports == 0) {
  281. kfree(info);
  282. return -ENODEV;
  283. }
  284. if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
  285. ports = 256 / SNDRV_RAWMIDI_DEVICES;
  286. mutex_lock(&register_mutex);
  287. client = synths[card->number];
  288. if (client == NULL) {
  289. newclient = 1;
  290. client = kzalloc(sizeof(*client), GFP_KERNEL);
  291. if (client == NULL) {
  292. mutex_unlock(&register_mutex);
  293. kfree(info);
  294. return -ENOMEM;
  295. }
  296. client->seq_client =
  297. snd_seq_create_kernel_client(
  298. card, 0, "%s", card->shortname[0] ?
  299. (const char *)card->shortname : "External MIDI");
  300. if (client->seq_client < 0) {
  301. kfree(client);
  302. mutex_unlock(&register_mutex);
  303. kfree(info);
  304. return -ENOMEM;
  305. }
  306. }
  307. msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
  308. port = kmalloc(sizeof(*port), GFP_KERNEL);
  309. if (msynth == NULL || port == NULL)
  310. goto __nomem;
  311. for (p = 0; p < ports; p++) {
  312. ms = &msynth[p];
  313. if (snd_seq_midisynth_new(ms, card, device, p) < 0)
  314. goto __nomem;
  315. /* declare port */
  316. memset(port, 0, sizeof(*port));
  317. port->addr.client = client->seq_client;
  318. port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
  319. port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
  320. memset(info, 0, sizeof(*info));
  321. info->device = device;
  322. if (p < output_count)
  323. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  324. else
  325. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  326. info->subdevice = p;
  327. if (snd_rawmidi_info_select(card, info) >= 0)
  328. strcpy(port->name, info->subname);
  329. if (! port->name[0]) {
  330. if (info->name[0]) {
  331. if (ports > 1)
  332. snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
  333. else
  334. snprintf(port->name, sizeof(port->name), "%s", info->name);
  335. } else {
  336. /* last resort */
  337. if (ports > 1)
  338. sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
  339. else
  340. sprintf(port->name, "MIDI %d-%d", card->number, device);
  341. }
  342. }
  343. if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
  344. port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  345. if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
  346. port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  347. if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
  348. info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
  349. port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  350. port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  351. | SNDRV_SEQ_PORT_TYPE_HARDWARE
  352. | SNDRV_SEQ_PORT_TYPE_PORT;
  353. port->midi_channels = 16;
  354. memset(&pcallbacks, 0, sizeof(pcallbacks));
  355. pcallbacks.owner = THIS_MODULE;
  356. pcallbacks.private_data = ms;
  357. pcallbacks.subscribe = midisynth_subscribe;
  358. pcallbacks.unsubscribe = midisynth_unsubscribe;
  359. pcallbacks.use = midisynth_use;
  360. pcallbacks.unuse = midisynth_unuse;
  361. pcallbacks.event_input = event_process_midi;
  362. port->kernel = &pcallbacks;
  363. if (rmidi->ops && rmidi->ops->get_port_info)
  364. rmidi->ops->get_port_info(rmidi, p, port);
  365. if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
  366. goto __nomem;
  367. ms->seq_client = client->seq_client;
  368. ms->seq_port = port->addr.port;
  369. }
  370. client->ports_per_device[device] = ports;
  371. client->ports[device] = msynth;
  372. client->num_ports++;
  373. if (newclient)
  374. synths[card->number] = client;
  375. mutex_unlock(&register_mutex);
  376. kfree(info);
  377. kfree(port);
  378. return 0; /* success */
  379. __nomem:
  380. if (msynth != NULL) {
  381. for (p = 0; p < ports; p++)
  382. snd_seq_midisynth_delete(&msynth[p]);
  383. kfree(msynth);
  384. }
  385. if (newclient) {
  386. snd_seq_delete_kernel_client(client->seq_client);
  387. kfree(client);
  388. }
  389. kfree(info);
  390. kfree(port);
  391. mutex_unlock(&register_mutex);
  392. return -ENOMEM;
  393. }
  394. /* release midi synth port */
  395. static int
  396. snd_seq_midisynth_remove(struct device *_dev)
  397. {
  398. struct snd_seq_device *dev = to_seq_dev(_dev);
  399. struct seq_midisynth_client *client;
  400. struct seq_midisynth *msynth;
  401. struct snd_card *card = dev->card;
  402. int device = dev->device, p, ports;
  403. mutex_lock(&register_mutex);
  404. client = synths[card->number];
  405. if (client == NULL || client->ports[device] == NULL) {
  406. mutex_unlock(&register_mutex);
  407. return -ENODEV;
  408. }
  409. ports = client->ports_per_device[device];
  410. client->ports_per_device[device] = 0;
  411. msynth = client->ports[device];
  412. client->ports[device] = NULL;
  413. for (p = 0; p < ports; p++)
  414. snd_seq_midisynth_delete(&msynth[p]);
  415. kfree(msynth);
  416. client->num_ports--;
  417. if (client->num_ports <= 0) {
  418. snd_seq_delete_kernel_client(client->seq_client);
  419. synths[card->number] = NULL;
  420. kfree(client);
  421. }
  422. mutex_unlock(&register_mutex);
  423. return 0;
  424. }
  425. static struct snd_seq_driver seq_midisynth_driver = {
  426. .driver = {
  427. .name = KBUILD_MODNAME,
  428. .probe = snd_seq_midisynth_probe,
  429. .remove = snd_seq_midisynth_remove,
  430. },
  431. .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
  432. .argsize = 0,
  433. };
  434. module_snd_seq_driver(seq_midisynth_driver);