seq_virmidi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtual Raw MIDI client on Sequencer
  4. *
  5. * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
  6. * Jaroslav Kysela <perex@perex.cz>
  7. */
  8. /*
  9. * Virtual Raw MIDI client
  10. *
  11. * The virtual rawmidi client is a sequencer client which associate
  12. * a rawmidi device file. The created rawmidi device file can be
  13. * accessed as a normal raw midi, but its MIDI source and destination
  14. * are arbitrary. For example, a user-client software synth connected
  15. * to this port can be used as a normal midi device as well.
  16. *
  17. * The virtual rawmidi device accepts also multiple opens. Each file
  18. * has its own input buffer, so that no conflict would occur. The drain
  19. * of input/output buffer acts only to the local buffer.
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/wait.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/rawmidi.h>
  28. #include <sound/info.h>
  29. #include <sound/control.h>
  30. #include <sound/minors.h>
  31. #include <sound/seq_kernel.h>
  32. #include <sound/seq_midi_event.h>
  33. #include <sound/seq_virmidi.h>
  34. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  35. MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
  36. MODULE_LICENSE("GPL");
  37. /*
  38. * initialize an event record
  39. */
  40. static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
  41. struct snd_seq_event *ev)
  42. {
  43. memset(ev, 0, sizeof(*ev));
  44. ev->source.port = vmidi->port;
  45. switch (vmidi->seq_mode) {
  46. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  47. ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  48. break;
  49. case SNDRV_VIRMIDI_SEQ_ATTACH:
  50. /* FIXME: source and destination are same - not good.. */
  51. ev->dest.client = vmidi->client;
  52. ev->dest.port = vmidi->port;
  53. break;
  54. }
  55. ev->type = SNDRV_SEQ_EVENT_NONE;
  56. }
  57. /*
  58. * decode input event and put to read buffer of each opened file
  59. */
  60. /* callback for snd_seq_dump_var_event(), bridging to snd_rawmidi_receive() */
  61. static int dump_to_rawmidi(void *ptr, void *buf, int count)
  62. {
  63. return snd_rawmidi_receive(ptr, buf, count);
  64. }
  65. static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
  66. struct snd_seq_event *ev,
  67. bool atomic)
  68. {
  69. struct snd_virmidi *vmidi;
  70. unsigned char msg[4];
  71. int len;
  72. if (atomic)
  73. read_lock(&rdev->filelist_lock);
  74. else
  75. down_read(&rdev->filelist_sem);
  76. list_for_each_entry(vmidi, &rdev->filelist, list) {
  77. if (!READ_ONCE(vmidi->trigger))
  78. continue;
  79. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  80. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  81. continue;
  82. snd_seq_dump_var_event(ev, dump_to_rawmidi, vmidi->substream);
  83. snd_midi_event_reset_decode(vmidi->parser);
  84. } else {
  85. len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
  86. if (len > 0)
  87. snd_rawmidi_receive(vmidi->substream, msg, len);
  88. }
  89. }
  90. if (atomic)
  91. read_unlock(&rdev->filelist_lock);
  92. else
  93. up_read(&rdev->filelist_sem);
  94. return 0;
  95. }
  96. /*
  97. * event handler of virmidi port
  98. */
  99. static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
  100. void *private_data, int atomic, int hop)
  101. {
  102. struct snd_virmidi_dev *rdev;
  103. rdev = private_data;
  104. if (!(rdev->flags & SNDRV_VIRMIDI_USE))
  105. return 0; /* ignored */
  106. return snd_virmidi_dev_receive_event(rdev, ev, atomic);
  107. }
  108. /*
  109. * trigger rawmidi stream for input
  110. */
  111. static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  112. {
  113. struct snd_virmidi *vmidi = substream->runtime->private_data;
  114. WRITE_ONCE(vmidi->trigger, !!up);
  115. }
  116. /* process rawmidi bytes and send events;
  117. * we need no lock here for vmidi->event since it's handled only in this work
  118. */
  119. static void snd_vmidi_output_work(struct work_struct *work)
  120. {
  121. struct snd_virmidi *vmidi;
  122. struct snd_rawmidi_substream *substream;
  123. unsigned char input;
  124. int ret;
  125. vmidi = container_of(work, struct snd_virmidi, output_work);
  126. substream = vmidi->substream;
  127. /* discard the outputs in dispatch mode unless subscribed */
  128. if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
  129. !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
  130. snd_rawmidi_proceed(substream);
  131. return;
  132. }
  133. while (READ_ONCE(vmidi->trigger)) {
  134. if (snd_rawmidi_transmit(substream, &input, 1) != 1)
  135. break;
  136. if (!snd_midi_event_encode_byte(vmidi->parser, input,
  137. &vmidi->event))
  138. continue;
  139. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  140. ret = snd_seq_kernel_client_dispatch(vmidi->client,
  141. &vmidi->event,
  142. false, 0);
  143. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  144. if (ret < 0)
  145. break;
  146. }
  147. /* rawmidi input might be huge, allow to have a break */
  148. cond_resched();
  149. }
  150. }
  151. /*
  152. * trigger rawmidi stream for output
  153. */
  154. static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  155. {
  156. struct snd_virmidi *vmidi = substream->runtime->private_data;
  157. WRITE_ONCE(vmidi->trigger, !!up);
  158. if (up)
  159. queue_work(system_highpri_wq, &vmidi->output_work);
  160. }
  161. /*
  162. * open rawmidi handle for input
  163. */
  164. static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
  165. {
  166. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  167. struct snd_rawmidi_runtime *runtime = substream->runtime;
  168. struct snd_virmidi *vmidi;
  169. vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
  170. if (vmidi == NULL)
  171. return -ENOMEM;
  172. vmidi->substream = substream;
  173. if (snd_midi_event_new(0, &vmidi->parser) < 0) {
  174. kfree(vmidi);
  175. return -ENOMEM;
  176. }
  177. vmidi->seq_mode = rdev->seq_mode;
  178. vmidi->client = rdev->client;
  179. vmidi->port = rdev->port;
  180. runtime->private_data = vmidi;
  181. scoped_guard(rwsem_write, &rdev->filelist_sem) {
  182. guard(write_lock_irq)(&rdev->filelist_lock);
  183. list_add_tail(&vmidi->list, &rdev->filelist);
  184. }
  185. vmidi->rdev = rdev;
  186. return 0;
  187. }
  188. /*
  189. * open rawmidi handle for output
  190. */
  191. static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
  192. {
  193. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  194. struct snd_rawmidi_runtime *runtime = substream->runtime;
  195. struct snd_virmidi *vmidi;
  196. vmidi = kzalloc_obj(*vmidi);
  197. if (vmidi == NULL)
  198. return -ENOMEM;
  199. vmidi->substream = substream;
  200. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
  201. kfree(vmidi);
  202. return -ENOMEM;
  203. }
  204. vmidi->seq_mode = rdev->seq_mode;
  205. vmidi->client = rdev->client;
  206. vmidi->port = rdev->port;
  207. snd_virmidi_init_event(vmidi, &vmidi->event);
  208. vmidi->rdev = rdev;
  209. INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
  210. runtime->private_data = vmidi;
  211. return 0;
  212. }
  213. /*
  214. * close rawmidi handle for input
  215. */
  216. static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
  217. {
  218. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  219. struct snd_virmidi *vmidi = substream->runtime->private_data;
  220. scoped_guard(rwsem_write, &rdev->filelist_sem) {
  221. guard(write_lock_irq)(&rdev->filelist_lock);
  222. list_del(&vmidi->list);
  223. }
  224. snd_midi_event_free(vmidi->parser);
  225. substream->runtime->private_data = NULL;
  226. kfree(vmidi);
  227. return 0;
  228. }
  229. /*
  230. * close rawmidi handle for output
  231. */
  232. static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
  233. {
  234. struct snd_virmidi *vmidi = substream->runtime->private_data;
  235. WRITE_ONCE(vmidi->trigger, false); /* to be sure */
  236. cancel_work_sync(&vmidi->output_work);
  237. snd_midi_event_free(vmidi->parser);
  238. substream->runtime->private_data = NULL;
  239. kfree(vmidi);
  240. return 0;
  241. }
  242. /*
  243. * drain output work queue
  244. */
  245. static void snd_virmidi_output_drain(struct snd_rawmidi_substream *substream)
  246. {
  247. struct snd_virmidi *vmidi = substream->runtime->private_data;
  248. flush_work(&vmidi->output_work);
  249. }
  250. /*
  251. * subscribe callback - allow output to rawmidi device
  252. */
  253. static int snd_virmidi_subscribe(void *private_data,
  254. struct snd_seq_port_subscribe *info)
  255. {
  256. struct snd_virmidi_dev *rdev;
  257. rdev = private_data;
  258. if (!try_module_get(rdev->card->module))
  259. return -EFAULT;
  260. rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
  261. return 0;
  262. }
  263. /*
  264. * unsubscribe callback - disallow output to rawmidi device
  265. */
  266. static int snd_virmidi_unsubscribe(void *private_data,
  267. struct snd_seq_port_subscribe *info)
  268. {
  269. struct snd_virmidi_dev *rdev;
  270. rdev = private_data;
  271. rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
  272. module_put(rdev->card->module);
  273. return 0;
  274. }
  275. /*
  276. * use callback - allow input to rawmidi device
  277. */
  278. static int snd_virmidi_use(void *private_data,
  279. struct snd_seq_port_subscribe *info)
  280. {
  281. struct snd_virmidi_dev *rdev;
  282. rdev = private_data;
  283. if (!try_module_get(rdev->card->module))
  284. return -EFAULT;
  285. rdev->flags |= SNDRV_VIRMIDI_USE;
  286. return 0;
  287. }
  288. /*
  289. * unuse callback - disallow input to rawmidi device
  290. */
  291. static int snd_virmidi_unuse(void *private_data,
  292. struct snd_seq_port_subscribe *info)
  293. {
  294. struct snd_virmidi_dev *rdev;
  295. rdev = private_data;
  296. rdev->flags &= ~SNDRV_VIRMIDI_USE;
  297. module_put(rdev->card->module);
  298. return 0;
  299. }
  300. /*
  301. * Register functions
  302. */
  303. static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
  304. .open = snd_virmidi_input_open,
  305. .close = snd_virmidi_input_close,
  306. .trigger = snd_virmidi_input_trigger,
  307. };
  308. static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
  309. .open = snd_virmidi_output_open,
  310. .close = snd_virmidi_output_close,
  311. .trigger = snd_virmidi_output_trigger,
  312. .drain = snd_virmidi_output_drain,
  313. };
  314. /*
  315. * create a sequencer client and a port
  316. */
  317. static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
  318. {
  319. int client;
  320. struct snd_seq_port_callback pcallbacks;
  321. int err;
  322. if (rdev->client >= 0)
  323. return 0;
  324. struct snd_seq_port_info *pinfo __free(kfree) =
  325. kzalloc_obj(*pinfo);
  326. if (!pinfo)
  327. return -ENOMEM;
  328. client = snd_seq_create_kernel_client(rdev->card, rdev->device,
  329. "%s %d-%d", rdev->rmidi->name,
  330. rdev->card->number,
  331. rdev->device);
  332. if (client < 0)
  333. return client;
  334. rdev->client = client;
  335. /* create a port */
  336. pinfo->addr.client = client;
  337. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  338. /* set all capabilities */
  339. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  340. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  341. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  342. pinfo->direction = SNDRV_SEQ_PORT_DIR_BIDIRECTION;
  343. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  344. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  345. | SNDRV_SEQ_PORT_TYPE_PORT;
  346. pinfo->midi_channels = 16;
  347. memset(&pcallbacks, 0, sizeof(pcallbacks));
  348. pcallbacks.owner = THIS_MODULE;
  349. pcallbacks.private_data = rdev;
  350. pcallbacks.subscribe = snd_virmidi_subscribe;
  351. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  352. pcallbacks.use = snd_virmidi_use;
  353. pcallbacks.unuse = snd_virmidi_unuse;
  354. pcallbacks.event_input = snd_virmidi_event_input;
  355. pinfo->kernel = &pcallbacks;
  356. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
  357. if (err < 0) {
  358. snd_seq_delete_kernel_client(client);
  359. rdev->client = -1;
  360. return err;
  361. }
  362. rdev->port = pinfo->addr.port;
  363. return 0; /* success */
  364. }
  365. /*
  366. * release the sequencer client
  367. */
  368. static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
  369. {
  370. if (rdev->client >= 0) {
  371. snd_seq_delete_kernel_client(rdev->client);
  372. rdev->client = -1;
  373. }
  374. }
  375. /*
  376. * register the device
  377. */
  378. static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
  379. {
  380. struct snd_virmidi_dev *rdev = rmidi->private_data;
  381. int err;
  382. switch (rdev->seq_mode) {
  383. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  384. err = snd_virmidi_dev_attach_seq(rdev);
  385. if (err < 0)
  386. return err;
  387. break;
  388. case SNDRV_VIRMIDI_SEQ_ATTACH:
  389. if (rdev->client == 0)
  390. return -EINVAL;
  391. /* should check presence of port more strictly.. */
  392. break;
  393. default:
  394. pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. /*
  400. * unregister the device
  401. */
  402. static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
  403. {
  404. struct snd_virmidi_dev *rdev = rmidi->private_data;
  405. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  406. snd_virmidi_dev_detach_seq(rdev);
  407. return 0;
  408. }
  409. /*
  410. *
  411. */
  412. static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
  413. .dev_register = snd_virmidi_dev_register,
  414. .dev_unregister = snd_virmidi_dev_unregister,
  415. };
  416. /*
  417. * free device
  418. */
  419. static void snd_virmidi_free(struct snd_rawmidi *rmidi)
  420. {
  421. struct snd_virmidi_dev *rdev = rmidi->private_data;
  422. kfree(rdev);
  423. }
  424. /*
  425. * create a new device
  426. *
  427. */
  428. /* exported */
  429. int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
  430. {
  431. struct snd_rawmidi *rmidi;
  432. struct snd_virmidi_dev *rdev;
  433. int err;
  434. *rrmidi = NULL;
  435. err = snd_rawmidi_new(card, "VirMidi", device,
  436. 16, /* may be configurable */
  437. 16, /* may be configurable */
  438. &rmidi);
  439. if (err < 0)
  440. return err;
  441. strscpy(rmidi->name, rmidi->id);
  442. rdev = kzalloc_obj(*rdev);
  443. if (rdev == NULL) {
  444. snd_device_free(card, rmidi);
  445. return -ENOMEM;
  446. }
  447. rdev->card = card;
  448. rdev->rmidi = rmidi;
  449. rdev->device = device;
  450. rdev->client = -1;
  451. init_rwsem(&rdev->filelist_sem);
  452. rwlock_init(&rdev->filelist_lock);
  453. INIT_LIST_HEAD(&rdev->filelist);
  454. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  455. rmidi->private_data = rdev;
  456. rmidi->private_free = snd_virmidi_free;
  457. rmidi->ops = &snd_virmidi_global_ops;
  458. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
  459. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
  460. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  461. SNDRV_RAWMIDI_INFO_OUTPUT |
  462. SNDRV_RAWMIDI_INFO_DUPLEX;
  463. *rrmidi = rmidi;
  464. return 0;
  465. }
  466. EXPORT_SYMBOL(snd_virmidi_new);