seq_virmidi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. char buf[32];
  139. while (snd_rawmidi_transmit(substream, buf, sizeof(buf)) > 0)
  140. ; /* ignored */
  141. return;
  142. }
  143. while (READ_ONCE(vmidi->trigger)) {
  144. if (snd_rawmidi_transmit(substream, &input, 1) != 1)
  145. break;
  146. if (!snd_midi_event_encode_byte(vmidi->parser, input,
  147. &vmidi->event))
  148. continue;
  149. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  150. ret = snd_seq_kernel_client_dispatch(vmidi->client,
  151. &vmidi->event,
  152. false, 0);
  153. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  154. if (ret < 0)
  155. break;
  156. }
  157. /* rawmidi input might be huge, allow to have a break */
  158. cond_resched();
  159. }
  160. }
  161. /*
  162. * trigger rawmidi stream for output
  163. */
  164. static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  165. {
  166. struct snd_virmidi *vmidi = substream->runtime->private_data;
  167. WRITE_ONCE(vmidi->trigger, !!up);
  168. if (up)
  169. queue_work(system_highpri_wq, &vmidi->output_work);
  170. }
  171. /*
  172. * open rawmidi handle for input
  173. */
  174. static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
  175. {
  176. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  177. struct snd_rawmidi_runtime *runtime = substream->runtime;
  178. struct snd_virmidi *vmidi;
  179. vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
  180. if (vmidi == NULL)
  181. return -ENOMEM;
  182. vmidi->substream = substream;
  183. if (snd_midi_event_new(0, &vmidi->parser) < 0) {
  184. kfree(vmidi);
  185. return -ENOMEM;
  186. }
  187. vmidi->seq_mode = rdev->seq_mode;
  188. vmidi->client = rdev->client;
  189. vmidi->port = rdev->port;
  190. runtime->private_data = vmidi;
  191. down_write(&rdev->filelist_sem);
  192. write_lock_irq(&rdev->filelist_lock);
  193. list_add_tail(&vmidi->list, &rdev->filelist);
  194. write_unlock_irq(&rdev->filelist_lock);
  195. up_write(&rdev->filelist_sem);
  196. vmidi->rdev = rdev;
  197. return 0;
  198. }
  199. /*
  200. * open rawmidi handle for output
  201. */
  202. static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
  203. {
  204. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  205. struct snd_rawmidi_runtime *runtime = substream->runtime;
  206. struct snd_virmidi *vmidi;
  207. vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
  208. if (vmidi == NULL)
  209. return -ENOMEM;
  210. vmidi->substream = substream;
  211. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
  212. kfree(vmidi);
  213. return -ENOMEM;
  214. }
  215. vmidi->seq_mode = rdev->seq_mode;
  216. vmidi->client = rdev->client;
  217. vmidi->port = rdev->port;
  218. snd_virmidi_init_event(vmidi, &vmidi->event);
  219. vmidi->rdev = rdev;
  220. INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
  221. runtime->private_data = vmidi;
  222. return 0;
  223. }
  224. /*
  225. * close rawmidi handle for input
  226. */
  227. static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
  228. {
  229. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  230. struct snd_virmidi *vmidi = substream->runtime->private_data;
  231. down_write(&rdev->filelist_sem);
  232. write_lock_irq(&rdev->filelist_lock);
  233. list_del(&vmidi->list);
  234. write_unlock_irq(&rdev->filelist_lock);
  235. up_write(&rdev->filelist_sem);
  236. snd_midi_event_free(vmidi->parser);
  237. substream->runtime->private_data = NULL;
  238. kfree(vmidi);
  239. return 0;
  240. }
  241. /*
  242. * close rawmidi handle for output
  243. */
  244. static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
  245. {
  246. struct snd_virmidi *vmidi = substream->runtime->private_data;
  247. WRITE_ONCE(vmidi->trigger, false); /* to be sure */
  248. cancel_work_sync(&vmidi->output_work);
  249. snd_midi_event_free(vmidi->parser);
  250. substream->runtime->private_data = NULL;
  251. kfree(vmidi);
  252. return 0;
  253. }
  254. /*
  255. * subscribe callback - allow output to rawmidi device
  256. */
  257. static int snd_virmidi_subscribe(void *private_data,
  258. struct snd_seq_port_subscribe *info)
  259. {
  260. struct snd_virmidi_dev *rdev;
  261. rdev = private_data;
  262. if (!try_module_get(rdev->card->module))
  263. return -EFAULT;
  264. rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
  265. return 0;
  266. }
  267. /*
  268. * unsubscribe callback - disallow output to rawmidi device
  269. */
  270. static int snd_virmidi_unsubscribe(void *private_data,
  271. struct snd_seq_port_subscribe *info)
  272. {
  273. struct snd_virmidi_dev *rdev;
  274. rdev = private_data;
  275. rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
  276. module_put(rdev->card->module);
  277. return 0;
  278. }
  279. /*
  280. * use callback - allow input to rawmidi device
  281. */
  282. static int snd_virmidi_use(void *private_data,
  283. struct snd_seq_port_subscribe *info)
  284. {
  285. struct snd_virmidi_dev *rdev;
  286. rdev = private_data;
  287. if (!try_module_get(rdev->card->module))
  288. return -EFAULT;
  289. rdev->flags |= SNDRV_VIRMIDI_USE;
  290. return 0;
  291. }
  292. /*
  293. * unuse callback - disallow input to rawmidi device
  294. */
  295. static int snd_virmidi_unuse(void *private_data,
  296. struct snd_seq_port_subscribe *info)
  297. {
  298. struct snd_virmidi_dev *rdev;
  299. rdev = private_data;
  300. rdev->flags &= ~SNDRV_VIRMIDI_USE;
  301. module_put(rdev->card->module);
  302. return 0;
  303. }
  304. /*
  305. * Register functions
  306. */
  307. static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
  308. .open = snd_virmidi_input_open,
  309. .close = snd_virmidi_input_close,
  310. .trigger = snd_virmidi_input_trigger,
  311. };
  312. static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
  313. .open = snd_virmidi_output_open,
  314. .close = snd_virmidi_output_close,
  315. .trigger = snd_virmidi_output_trigger,
  316. };
  317. /*
  318. * create a sequencer client and a port
  319. */
  320. static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
  321. {
  322. int client;
  323. struct snd_seq_port_callback pcallbacks;
  324. struct snd_seq_port_info *pinfo;
  325. int err;
  326. if (rdev->client >= 0)
  327. return 0;
  328. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  329. if (!pinfo) {
  330. err = -ENOMEM;
  331. goto __error;
  332. }
  333. client = snd_seq_create_kernel_client(rdev->card, rdev->device,
  334. "%s %d-%d", rdev->rmidi->name,
  335. rdev->card->number,
  336. rdev->device);
  337. if (client < 0) {
  338. err = client;
  339. goto __error;
  340. }
  341. rdev->client = client;
  342. /* create a port */
  343. pinfo->addr.client = client;
  344. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  345. /* set all capabilities */
  346. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  347. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  348. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  349. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  350. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  351. | SNDRV_SEQ_PORT_TYPE_PORT;
  352. pinfo->midi_channels = 16;
  353. memset(&pcallbacks, 0, sizeof(pcallbacks));
  354. pcallbacks.owner = THIS_MODULE;
  355. pcallbacks.private_data = rdev;
  356. pcallbacks.subscribe = snd_virmidi_subscribe;
  357. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  358. pcallbacks.use = snd_virmidi_use;
  359. pcallbacks.unuse = snd_virmidi_unuse;
  360. pcallbacks.event_input = snd_virmidi_event_input;
  361. pinfo->kernel = &pcallbacks;
  362. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
  363. if (err < 0) {
  364. snd_seq_delete_kernel_client(client);
  365. rdev->client = -1;
  366. goto __error;
  367. }
  368. rdev->port = pinfo->addr.port;
  369. err = 0; /* success */
  370. __error:
  371. kfree(pinfo);
  372. return err;
  373. }
  374. /*
  375. * release the sequencer client
  376. */
  377. static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
  378. {
  379. if (rdev->client >= 0) {
  380. snd_seq_delete_kernel_client(rdev->client);
  381. rdev->client = -1;
  382. }
  383. }
  384. /*
  385. * register the device
  386. */
  387. static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
  388. {
  389. struct snd_virmidi_dev *rdev = rmidi->private_data;
  390. int err;
  391. switch (rdev->seq_mode) {
  392. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  393. err = snd_virmidi_dev_attach_seq(rdev);
  394. if (err < 0)
  395. return err;
  396. break;
  397. case SNDRV_VIRMIDI_SEQ_ATTACH:
  398. if (rdev->client == 0)
  399. return -EINVAL;
  400. /* should check presence of port more strictly.. */
  401. break;
  402. default:
  403. pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. /*
  409. * unregister the device
  410. */
  411. static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
  412. {
  413. struct snd_virmidi_dev *rdev = rmidi->private_data;
  414. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  415. snd_virmidi_dev_detach_seq(rdev);
  416. return 0;
  417. }
  418. /*
  419. *
  420. */
  421. static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
  422. .dev_register = snd_virmidi_dev_register,
  423. .dev_unregister = snd_virmidi_dev_unregister,
  424. };
  425. /*
  426. * free device
  427. */
  428. static void snd_virmidi_free(struct snd_rawmidi *rmidi)
  429. {
  430. struct snd_virmidi_dev *rdev = rmidi->private_data;
  431. kfree(rdev);
  432. }
  433. /*
  434. * create a new device
  435. *
  436. */
  437. /* exported */
  438. int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
  439. {
  440. struct snd_rawmidi *rmidi;
  441. struct snd_virmidi_dev *rdev;
  442. int err;
  443. *rrmidi = NULL;
  444. if ((err = snd_rawmidi_new(card, "VirMidi", device,
  445. 16, /* may be configurable */
  446. 16, /* may be configurable */
  447. &rmidi)) < 0)
  448. return err;
  449. strcpy(rmidi->name, rmidi->id);
  450. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  451. if (rdev == NULL) {
  452. snd_device_free(card, rmidi);
  453. return -ENOMEM;
  454. }
  455. rdev->card = card;
  456. rdev->rmidi = rmidi;
  457. rdev->device = device;
  458. rdev->client = -1;
  459. init_rwsem(&rdev->filelist_sem);
  460. rwlock_init(&rdev->filelist_lock);
  461. INIT_LIST_HEAD(&rdev->filelist);
  462. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  463. rmidi->private_data = rdev;
  464. rmidi->private_free = snd_virmidi_free;
  465. rmidi->ops = &snd_virmidi_global_ops;
  466. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
  467. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
  468. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  469. SNDRV_RAWMIDI_INFO_OUTPUT |
  470. SNDRV_RAWMIDI_INFO_DUPLEX;
  471. *rrmidi = rmidi;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(snd_virmidi_new);