seq_virmidi.c 14 KB

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