seq_midi.c 13 KB

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