seq_virmidi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <sound/driver.h>
  37. #include <linux/init.h>
  38. #include <linux/wait.h>
  39. #include <linux/sched.h>
  40. #include <linux/slab.h>
  41. #include <sound/core.h>
  42. #include <sound/rawmidi.h>
  43. #include <sound/info.h>
  44. #include <sound/control.h>
  45. #include <sound/minors.h>
  46. #include <sound/seq_kernel.h>
  47. #include <sound/seq_midi_event.h>
  48. #include <sound/seq_virmidi.h>
  49. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  50. MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
  51. MODULE_LICENSE("GPL");
  52. /*
  53. * initialize an event record
  54. */
  55. static void snd_virmidi_init_event(snd_virmidi_t *vmidi, snd_seq_event_t *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(snd_virmidi_dev_t *rdev, snd_seq_event_t *ev)
  75. {
  76. snd_virmidi_t *vmidi;
  77. struct list_head *list;
  78. unsigned char msg[4];
  79. int len;
  80. read_lock(&rdev->filelist_lock);
  81. list_for_each(list, &rdev->filelist) {
  82. vmidi = list_entry(list, snd_virmidi_t, list);
  83. if (!vmidi->trigger)
  84. continue;
  85. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  86. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  87. continue;
  88. snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
  89. } else {
  90. len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
  91. if (len > 0)
  92. snd_rawmidi_receive(vmidi->substream, msg, len);
  93. }
  94. }
  95. read_unlock(&rdev->filelist_lock);
  96. return 0;
  97. }
  98. /*
  99. * receive an event from the remote virmidi port
  100. *
  101. * for rawmidi inputs, you can call this function from the event
  102. * handler of a remote port which is attached to the virmidi via
  103. * SNDRV_VIRMIDI_SEQ_ATTACH.
  104. */
  105. /* exported */
  106. int snd_virmidi_receive(snd_rawmidi_t *rmidi, snd_seq_event_t *ev)
  107. {
  108. snd_virmidi_dev_t *rdev;
  109. rdev = rmidi->private_data;
  110. return snd_virmidi_dev_receive_event(rdev, ev);
  111. }
  112. /*
  113. * event handler of virmidi port
  114. */
  115. static int snd_virmidi_event_input(snd_seq_event_t *ev, int direct,
  116. void *private_data, int atomic, int hop)
  117. {
  118. snd_virmidi_dev_t *rdev;
  119. rdev = private_data;
  120. if (!(rdev->flags & SNDRV_VIRMIDI_USE))
  121. return 0; /* ignored */
  122. return snd_virmidi_dev_receive_event(rdev, ev);
  123. }
  124. /*
  125. * trigger rawmidi stream for input
  126. */
  127. static void snd_virmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  128. {
  129. snd_virmidi_t *vmidi = substream->runtime->private_data;
  130. if (up) {
  131. vmidi->trigger = 1;
  132. } else {
  133. vmidi->trigger = 0;
  134. }
  135. }
  136. /*
  137. * trigger rawmidi stream for output
  138. */
  139. static void snd_virmidi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  140. {
  141. snd_virmidi_t *vmidi = substream->runtime->private_data;
  142. int count, res;
  143. unsigned char buf[32], *pbuf;
  144. if (up) {
  145. vmidi->trigger = 1;
  146. if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
  147. !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
  148. snd_rawmidi_transmit_ack(substream, substream->runtime->buffer_size - substream->runtime->avail);
  149. return; /* ignored */
  150. }
  151. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  152. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, 0, 0) < 0)
  153. return;
  154. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  155. }
  156. while (1) {
  157. count = snd_rawmidi_transmit_peek(substream, buf, sizeof(buf));
  158. if (count <= 0)
  159. break;
  160. pbuf = buf;
  161. while (count > 0) {
  162. res = snd_midi_event_encode(vmidi->parser, pbuf, count, &vmidi->event);
  163. if (res < 0) {
  164. snd_midi_event_reset_encode(vmidi->parser);
  165. continue;
  166. }
  167. snd_rawmidi_transmit_ack(substream, res);
  168. pbuf += res;
  169. count -= res;
  170. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  171. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, 0, 0) < 0)
  172. return;
  173. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  174. }
  175. }
  176. }
  177. } else {
  178. vmidi->trigger = 0;
  179. }
  180. }
  181. /*
  182. * open rawmidi handle for input
  183. */
  184. static int snd_virmidi_input_open(snd_rawmidi_substream_t * substream)
  185. {
  186. snd_virmidi_dev_t *rdev = substream->rmidi->private_data;
  187. snd_rawmidi_runtime_t *runtime = substream->runtime;
  188. snd_virmidi_t *vmidi;
  189. unsigned long flags;
  190. vmidi = kcalloc(1, sizeof(*vmidi), GFP_KERNEL);
  191. if (vmidi == NULL)
  192. return -ENOMEM;
  193. vmidi->substream = substream;
  194. if (snd_midi_event_new(0, &vmidi->parser) < 0) {
  195. kfree(vmidi);
  196. return -ENOMEM;
  197. }
  198. vmidi->seq_mode = rdev->seq_mode;
  199. vmidi->client = rdev->client;
  200. vmidi->port = rdev->port;
  201. runtime->private_data = vmidi;
  202. write_lock_irqsave(&rdev->filelist_lock, flags);
  203. list_add_tail(&vmidi->list, &rdev->filelist);
  204. write_unlock_irqrestore(&rdev->filelist_lock, flags);
  205. vmidi->rdev = rdev;
  206. return 0;
  207. }
  208. /*
  209. * open rawmidi handle for output
  210. */
  211. static int snd_virmidi_output_open(snd_rawmidi_substream_t * substream)
  212. {
  213. snd_virmidi_dev_t *rdev = substream->rmidi->private_data;
  214. snd_rawmidi_runtime_t *runtime = substream->runtime;
  215. snd_virmidi_t *vmidi;
  216. vmidi = kcalloc(1, sizeof(*vmidi), GFP_KERNEL);
  217. if (vmidi == NULL)
  218. return -ENOMEM;
  219. vmidi->substream = substream;
  220. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
  221. kfree(vmidi);
  222. return -ENOMEM;
  223. }
  224. vmidi->seq_mode = rdev->seq_mode;
  225. vmidi->client = rdev->client;
  226. vmidi->port = rdev->port;
  227. snd_virmidi_init_event(vmidi, &vmidi->event);
  228. vmidi->rdev = rdev;
  229. runtime->private_data = vmidi;
  230. return 0;
  231. }
  232. /*
  233. * close rawmidi handle for input
  234. */
  235. static int snd_virmidi_input_close(snd_rawmidi_substream_t * substream)
  236. {
  237. snd_virmidi_t *vmidi = substream->runtime->private_data;
  238. snd_midi_event_free(vmidi->parser);
  239. list_del(&vmidi->list);
  240. substream->runtime->private_data = NULL;
  241. kfree(vmidi);
  242. return 0;
  243. }
  244. /*
  245. * close rawmidi handle for output
  246. */
  247. static int snd_virmidi_output_close(snd_rawmidi_substream_t * substream)
  248. {
  249. snd_virmidi_t *vmidi = substream->runtime->private_data;
  250. snd_midi_event_free(vmidi->parser);
  251. substream->runtime->private_data = NULL;
  252. kfree(vmidi);
  253. return 0;
  254. }
  255. /*
  256. * subscribe callback - allow output to rawmidi device
  257. */
  258. static int snd_virmidi_subscribe(void *private_data, snd_seq_port_subscribe_t *info)
  259. {
  260. snd_virmidi_dev_t *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, snd_seq_port_subscribe_t *info)
  271. {
  272. snd_virmidi_dev_t *rdev;
  273. rdev = private_data;
  274. rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
  275. module_put(rdev->card->module);
  276. return 0;
  277. }
  278. /*
  279. * use callback - allow input to rawmidi device
  280. */
  281. static int snd_virmidi_use(void *private_data, snd_seq_port_subscribe_t *info)
  282. {
  283. snd_virmidi_dev_t *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, snd_seq_port_subscribe_t *info)
  294. {
  295. snd_virmidi_dev_t *rdev;
  296. rdev = private_data;
  297. rdev->flags &= ~SNDRV_VIRMIDI_USE;
  298. module_put(rdev->card->module);
  299. return 0;
  300. }
  301. /*
  302. * Register functions
  303. */
  304. static snd_rawmidi_ops_t snd_virmidi_input_ops = {
  305. .open = snd_virmidi_input_open,
  306. .close = snd_virmidi_input_close,
  307. .trigger = snd_virmidi_input_trigger,
  308. };
  309. static snd_rawmidi_ops_t snd_virmidi_output_ops = {
  310. .open = snd_virmidi_output_open,
  311. .close = snd_virmidi_output_close,
  312. .trigger = snd_virmidi_output_trigger,
  313. };
  314. /*
  315. * create a sequencer client and a port
  316. */
  317. static int snd_virmidi_dev_attach_seq(snd_virmidi_dev_t *rdev)
  318. {
  319. int client;
  320. snd_seq_client_callback_t callbacks;
  321. snd_seq_port_callback_t pcallbacks;
  322. snd_seq_client_info_t *info;
  323. snd_seq_port_info_t *pinfo;
  324. int err;
  325. if (rdev->client >= 0)
  326. return 0;
  327. info = kmalloc(sizeof(*info), GFP_KERNEL);
  328. pinfo = kmalloc(sizeof(*pinfo), GFP_KERNEL);
  329. if (! info || ! pinfo) {
  330. err = -ENOMEM;
  331. goto __error;
  332. }
  333. memset(&callbacks, 0, sizeof(callbacks));
  334. callbacks.private_data = rdev;
  335. callbacks.allow_input = 1;
  336. callbacks.allow_output = 1;
  337. client = snd_seq_create_kernel_client(rdev->card, rdev->device, &callbacks);
  338. if (client < 0) {
  339. err = client;
  340. goto __error;
  341. }
  342. rdev->client = client;
  343. /* set client name */
  344. memset(info, 0, sizeof(*info));
  345. info->client = client;
  346. info->type = KERNEL_CLIENT;
  347. sprintf(info->name, "%s %d-%d", rdev->rmidi->name, rdev->card->number, rdev->device);
  348. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &info);
  349. /* create a port */
  350. memset(pinfo, 0, sizeof(*pinfo));
  351. pinfo->addr.client = client;
  352. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  353. /* set all capabilities */
  354. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  355. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  356. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  357. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC;
  358. pinfo->midi_channels = 16;
  359. memset(&pcallbacks, 0, sizeof(pcallbacks));
  360. pcallbacks.owner = THIS_MODULE;
  361. pcallbacks.private_data = rdev;
  362. pcallbacks.subscribe = snd_virmidi_subscribe;
  363. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  364. pcallbacks.use = snd_virmidi_use;
  365. pcallbacks.unuse = snd_virmidi_unuse;
  366. pcallbacks.event_input = snd_virmidi_event_input;
  367. pinfo->kernel = &pcallbacks;
  368. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo);
  369. if (err < 0) {
  370. snd_seq_delete_kernel_client(client);
  371. rdev->client = -1;
  372. goto __error;
  373. }
  374. rdev->port = pinfo->addr.port;
  375. err = 0; /* success */
  376. __error:
  377. kfree(info);
  378. kfree(pinfo);
  379. return err;
  380. }
  381. /*
  382. * release the sequencer client
  383. */
  384. static void snd_virmidi_dev_detach_seq(snd_virmidi_dev_t *rdev)
  385. {
  386. if (rdev->client >= 0) {
  387. snd_seq_delete_kernel_client(rdev->client);
  388. rdev->client = -1;
  389. }
  390. }
  391. /*
  392. * register the device
  393. */
  394. static int snd_virmidi_dev_register(snd_rawmidi_t *rmidi)
  395. {
  396. snd_virmidi_dev_t *rdev = rmidi->private_data;
  397. int err;
  398. switch (rdev->seq_mode) {
  399. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  400. err = snd_virmidi_dev_attach_seq(rdev);
  401. if (err < 0)
  402. return err;
  403. break;
  404. case SNDRV_VIRMIDI_SEQ_ATTACH:
  405. if (rdev->client == 0)
  406. return -EINVAL;
  407. /* should check presence of port more strictly.. */
  408. break;
  409. default:
  410. snd_printk(KERN_ERR "seq_mode is not set: %d\n", rdev->seq_mode);
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. /*
  416. * unregister the device
  417. */
  418. static int snd_virmidi_dev_unregister(snd_rawmidi_t *rmidi)
  419. {
  420. snd_virmidi_dev_t *rdev = rmidi->private_data;
  421. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  422. snd_virmidi_dev_detach_seq(rdev);
  423. return 0;
  424. }
  425. /*
  426. *
  427. */
  428. static snd_rawmidi_global_ops_t snd_virmidi_global_ops = {
  429. .dev_register = snd_virmidi_dev_register,
  430. .dev_unregister = snd_virmidi_dev_unregister,
  431. };
  432. /*
  433. * free device
  434. */
  435. static void snd_virmidi_free(snd_rawmidi_t *rmidi)
  436. {
  437. snd_virmidi_dev_t *rdev = rmidi->private_data;
  438. kfree(rdev);
  439. }
  440. /*
  441. * create a new device
  442. *
  443. */
  444. /* exported */
  445. int snd_virmidi_new(snd_card_t *card, int device, snd_rawmidi_t **rrmidi)
  446. {
  447. snd_rawmidi_t *rmidi;
  448. snd_virmidi_dev_t *rdev;
  449. int err;
  450. *rrmidi = NULL;
  451. if ((err = snd_rawmidi_new(card, "VirMidi", device,
  452. 16, /* may be configurable */
  453. 16, /* may be configurable */
  454. &rmidi)) < 0)
  455. return err;
  456. strcpy(rmidi->name, rmidi->id);
  457. rdev = kcalloc(1, sizeof(*rdev), GFP_KERNEL);
  458. if (rdev == NULL) {
  459. snd_device_free(card, rmidi);
  460. return -ENOMEM;
  461. }
  462. rdev->card = card;
  463. rdev->rmidi = rmidi;
  464. rdev->device = device;
  465. rdev->client = -1;
  466. rwlock_init(&rdev->filelist_lock);
  467. INIT_LIST_HEAD(&rdev->filelist);
  468. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  469. rmidi->private_data = rdev;
  470. rmidi->private_free = snd_virmidi_free;
  471. rmidi->ops = &snd_virmidi_global_ops;
  472. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
  473. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
  474. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  475. SNDRV_RAWMIDI_INFO_OUTPUT |
  476. SNDRV_RAWMIDI_INFO_DUPLEX;
  477. *rrmidi = rmidi;
  478. return 0;
  479. }
  480. /*
  481. * ENTRY functions
  482. */
  483. static int __init alsa_virmidi_init(void)
  484. {
  485. return 0;
  486. }
  487. static void __exit alsa_virmidi_exit(void)
  488. {
  489. }
  490. module_init(alsa_virmidi_init)
  491. module_exit(alsa_virmidi_exit)
  492. EXPORT_SYMBOL(snd_virmidi_new);
  493. EXPORT_SYMBOL(snd_virmidi_receive);