seq_midi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 device *_dev)
  241. {
  242. struct snd_seq_device *dev = to_seq_dev(_dev);
  243. struct seq_midisynth_client *client;
  244. struct seq_midisynth *msynth, *ms;
  245. struct snd_seq_port_info *port __free(kfree) = NULL;
  246. struct snd_rawmidi_info *info __free(kfree) = NULL;
  247. struct snd_rawmidi *rmidi = dev->private_data;
  248. int newclient = 0;
  249. unsigned int p, ports;
  250. struct snd_seq_port_callback pcallbacks;
  251. struct snd_card *card = dev->card;
  252. int device = dev->device;
  253. unsigned int input_count = 0, output_count = 0;
  254. if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
  255. return -EINVAL;
  256. info = kmalloc(sizeof(*info), GFP_KERNEL);
  257. if (! info)
  258. return -ENOMEM;
  259. info->device = device;
  260. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  261. info->subdevice = 0;
  262. if (snd_rawmidi_info_select(card, info) >= 0)
  263. output_count = info->subdevices_count;
  264. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  265. if (snd_rawmidi_info_select(card, info) >= 0) {
  266. input_count = info->subdevices_count;
  267. }
  268. ports = output_count;
  269. if (ports < input_count)
  270. ports = input_count;
  271. if (ports == 0)
  272. return -ENODEV;
  273. if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
  274. ports = 256 / SNDRV_RAWMIDI_DEVICES;
  275. guard(mutex)(&register_mutex);
  276. client = synths[card->number];
  277. if (client == NULL) {
  278. newclient = 1;
  279. client = kzalloc(sizeof(*client), GFP_KERNEL);
  280. if (client == NULL)
  281. return -ENOMEM;
  282. client->seq_client =
  283. snd_seq_create_kernel_client(
  284. card, 0, "%s", card->shortname[0] ?
  285. (const char *)card->shortname : "External MIDI");
  286. if (client->seq_client < 0) {
  287. kfree(client);
  288. return -ENOMEM;
  289. }
  290. }
  291. msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
  292. port = kmalloc(sizeof(*port), GFP_KERNEL);
  293. if (msynth == NULL || port == NULL)
  294. goto __nomem;
  295. for (p = 0; p < ports; p++) {
  296. ms = &msynth[p];
  297. ms->rmidi = rmidi;
  298. if (snd_seq_midisynth_new(ms, card, device, p) < 0)
  299. goto __nomem;
  300. /* declare port */
  301. memset(port, 0, sizeof(*port));
  302. port->addr.client = client->seq_client;
  303. port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
  304. port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
  305. memset(info, 0, sizeof(*info));
  306. info->device = device;
  307. if (p < output_count)
  308. info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  309. else
  310. info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  311. info->subdevice = p;
  312. if (snd_rawmidi_info_select(card, info) >= 0)
  313. strcpy(port->name, info->subname);
  314. if (! port->name[0]) {
  315. if (info->name[0]) {
  316. if (ports > 1)
  317. scnprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
  318. else
  319. scnprintf(port->name, sizeof(port->name), "%s", info->name);
  320. } else {
  321. /* last resort */
  322. if (ports > 1)
  323. sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
  324. else
  325. sprintf(port->name, "MIDI %d-%d", card->number, device);
  326. }
  327. }
  328. if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
  329. port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  330. if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
  331. port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  332. if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
  333. info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
  334. port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  335. if (port->capability & SNDRV_SEQ_PORT_CAP_READ)
  336. port->direction |= SNDRV_SEQ_PORT_DIR_INPUT;
  337. if (port->capability & SNDRV_SEQ_PORT_CAP_WRITE)
  338. port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT;
  339. port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  340. | SNDRV_SEQ_PORT_TYPE_HARDWARE
  341. | SNDRV_SEQ_PORT_TYPE_PORT;
  342. port->midi_channels = 16;
  343. memset(&pcallbacks, 0, sizeof(pcallbacks));
  344. pcallbacks.owner = THIS_MODULE;
  345. pcallbacks.private_data = ms;
  346. pcallbacks.subscribe = midisynth_subscribe;
  347. pcallbacks.unsubscribe = midisynth_unsubscribe;
  348. pcallbacks.use = midisynth_use;
  349. pcallbacks.unuse = midisynth_unuse;
  350. pcallbacks.event_input = event_process_midi;
  351. port->kernel = &pcallbacks;
  352. if (rmidi->ops && rmidi->ops->get_port_info)
  353. rmidi->ops->get_port_info(rmidi, p, port);
  354. if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
  355. goto __nomem;
  356. ms->seq_client = client->seq_client;
  357. ms->seq_port = port->addr.port;
  358. }
  359. client->ports_per_device[device] = ports;
  360. client->ports[device] = msynth;
  361. client->num_ports++;
  362. if (newclient)
  363. synths[card->number] = client;
  364. return 0; /* success */
  365. __nomem:
  366. if (msynth != NULL) {
  367. for (p = 0; p < ports; p++)
  368. snd_seq_midisynth_delete(&msynth[p]);
  369. kfree(msynth);
  370. }
  371. if (newclient) {
  372. snd_seq_delete_kernel_client(client->seq_client);
  373. kfree(client);
  374. }
  375. return -ENOMEM;
  376. }
  377. /* release midi synth port */
  378. static int
  379. snd_seq_midisynth_remove(struct device *_dev)
  380. {
  381. struct snd_seq_device *dev = to_seq_dev(_dev);
  382. struct seq_midisynth_client *client;
  383. struct seq_midisynth *msynth;
  384. struct snd_card *card = dev->card;
  385. int device = dev->device, p, ports;
  386. guard(mutex)(&register_mutex);
  387. client = synths[card->number];
  388. if (client == NULL || client->ports[device] == NULL)
  389. return -ENODEV;
  390. ports = client->ports_per_device[device];
  391. client->ports_per_device[device] = 0;
  392. msynth = client->ports[device];
  393. client->ports[device] = NULL;
  394. for (p = 0; p < ports; p++)
  395. snd_seq_midisynth_delete(&msynth[p]);
  396. kfree(msynth);
  397. client->num_ports--;
  398. if (client->num_ports <= 0) {
  399. snd_seq_delete_kernel_client(client->seq_client);
  400. synths[card->number] = NULL;
  401. kfree(client);
  402. }
  403. return 0;
  404. }
  405. static struct snd_seq_driver seq_midisynth_driver = {
  406. .driver = {
  407. .name = KBUILD_MODNAME,
  408. .probe = snd_seq_midisynth_probe,
  409. .remove = snd_seq_midisynth_remove,
  410. },
  411. .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
  412. .argsize = 0,
  413. };
  414. module_snd_seq_driver(seq_midisynth_driver);