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. #if 0
  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. #endif /* 0 */
  113. /*
  114. * event handler of virmidi port
  115. */
  116. static int snd_virmidi_event_input(snd_seq_event_t *ev, int direct,
  117. void *private_data, int atomic, int hop)
  118. {
  119. snd_virmidi_dev_t *rdev;
  120. rdev = private_data;
  121. if (!(rdev->flags & SNDRV_VIRMIDI_USE))
  122. return 0; /* ignored */
  123. return snd_virmidi_dev_receive_event(rdev, ev);
  124. }
  125. /*
  126. * trigger rawmidi stream for input
  127. */
  128. static void snd_virmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  129. {
  130. snd_virmidi_t *vmidi = substream->runtime->private_data;
  131. if (up) {
  132. vmidi->trigger = 1;
  133. } else {
  134. vmidi->trigger = 0;
  135. }
  136. }
  137. /*
  138. * trigger rawmidi stream for output
  139. */
  140. static void snd_virmidi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  141. {
  142. snd_virmidi_t *vmidi = substream->runtime->private_data;
  143. int count, res;
  144. unsigned char buf[32], *pbuf;
  145. if (up) {
  146. vmidi->trigger = 1;
  147. if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
  148. !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
  149. snd_rawmidi_transmit_ack(substream, substream->runtime->buffer_size - substream->runtime->avail);
  150. return; /* ignored */
  151. }
  152. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  153. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, 0, 0) < 0)
  154. return;
  155. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  156. }
  157. while (1) {
  158. count = snd_rawmidi_transmit_peek(substream, buf, sizeof(buf));
  159. if (count <= 0)
  160. break;
  161. pbuf = buf;
  162. while (count > 0) {
  163. res = snd_midi_event_encode(vmidi->parser, pbuf, count, &vmidi->event);
  164. if (res < 0) {
  165. snd_midi_event_reset_encode(vmidi->parser);
  166. continue;
  167. }
  168. snd_rawmidi_transmit_ack(substream, res);
  169. pbuf += res;
  170. count -= res;
  171. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  172. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, 0, 0) < 0)
  173. return;
  174. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  175. }
  176. }
  177. }
  178. } else {
  179. vmidi->trigger = 0;
  180. }
  181. }
  182. /*
  183. * open rawmidi handle for input
  184. */
  185. static int snd_virmidi_input_open(snd_rawmidi_substream_t * substream)
  186. {
  187. snd_virmidi_dev_t *rdev = substream->rmidi->private_data;
  188. snd_rawmidi_runtime_t *runtime = substream->runtime;
  189. snd_virmidi_t *vmidi;
  190. unsigned long flags;
  191. vmidi = kcalloc(1, sizeof(*vmidi), GFP_KERNEL);
  192. if (vmidi == NULL)
  193. return -ENOMEM;
  194. vmidi->substream = substream;
  195. if (snd_midi_event_new(0, &vmidi->parser) < 0) {
  196. kfree(vmidi);
  197. return -ENOMEM;
  198. }
  199. vmidi->seq_mode = rdev->seq_mode;
  200. vmidi->client = rdev->client;
  201. vmidi->port = rdev->port;
  202. runtime->private_data = vmidi;
  203. write_lock_irqsave(&rdev->filelist_lock, flags);
  204. list_add_tail(&vmidi->list, &rdev->filelist);
  205. write_unlock_irqrestore(&rdev->filelist_lock, flags);
  206. vmidi->rdev = rdev;
  207. return 0;
  208. }
  209. /*
  210. * open rawmidi handle for output
  211. */
  212. static int snd_virmidi_output_open(snd_rawmidi_substream_t * substream)
  213. {
  214. snd_virmidi_dev_t *rdev = substream->rmidi->private_data;
  215. snd_rawmidi_runtime_t *runtime = substream->runtime;
  216. snd_virmidi_t *vmidi;
  217. vmidi = kcalloc(1, sizeof(*vmidi), GFP_KERNEL);
  218. if (vmidi == NULL)
  219. return -ENOMEM;
  220. vmidi->substream = substream;
  221. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
  222. kfree(vmidi);
  223. return -ENOMEM;
  224. }
  225. vmidi->seq_mode = rdev->seq_mode;
  226. vmidi->client = rdev->client;
  227. vmidi->port = rdev->port;
  228. snd_virmidi_init_event(vmidi, &vmidi->event);
  229. vmidi->rdev = rdev;
  230. runtime->private_data = vmidi;
  231. return 0;
  232. }
  233. /*
  234. * close rawmidi handle for input
  235. */
  236. static int snd_virmidi_input_close(snd_rawmidi_substream_t * substream)
  237. {
  238. snd_virmidi_t *vmidi = substream->runtime->private_data;
  239. snd_midi_event_free(vmidi->parser);
  240. list_del(&vmidi->list);
  241. substream->runtime->private_data = NULL;
  242. kfree(vmidi);
  243. return 0;
  244. }
  245. /*
  246. * close rawmidi handle for output
  247. */
  248. static int snd_virmidi_output_close(snd_rawmidi_substream_t * substream)
  249. {
  250. snd_virmidi_t *vmidi = substream->runtime->private_data;
  251. snd_midi_event_free(vmidi->parser);
  252. substream->runtime->private_data = NULL;
  253. kfree(vmidi);
  254. return 0;
  255. }
  256. /*
  257. * subscribe callback - allow output to rawmidi device
  258. */
  259. static int snd_virmidi_subscribe(void *private_data, snd_seq_port_subscribe_t *info)
  260. {
  261. snd_virmidi_dev_t *rdev;
  262. rdev = private_data;
  263. if (!try_module_get(rdev->card->module))
  264. return -EFAULT;
  265. rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
  266. return 0;
  267. }
  268. /*
  269. * unsubscribe callback - disallow output to rawmidi device
  270. */
  271. static int snd_virmidi_unsubscribe(void *private_data, snd_seq_port_subscribe_t *info)
  272. {
  273. snd_virmidi_dev_t *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, snd_seq_port_subscribe_t *info)
  283. {
  284. snd_virmidi_dev_t *rdev;
  285. rdev = private_data;
  286. if (!try_module_get(rdev->card->module))
  287. return -EFAULT;
  288. rdev->flags |= SNDRV_VIRMIDI_USE;
  289. return 0;
  290. }
  291. /*
  292. * unuse callback - disallow input to rawmidi device
  293. */
  294. static int snd_virmidi_unuse(void *private_data, snd_seq_port_subscribe_t *info)
  295. {
  296. snd_virmidi_dev_t *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 snd_rawmidi_ops_t 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 snd_rawmidi_ops_t 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(snd_virmidi_dev_t *rdev)
  319. {
  320. int client;
  321. snd_seq_client_callback_t callbacks;
  322. snd_seq_port_callback_t pcallbacks;
  323. snd_seq_client_info_t *info;
  324. snd_seq_port_info_t *pinfo;
  325. int err;
  326. if (rdev->client >= 0)
  327. return 0;
  328. info = kmalloc(sizeof(*info), GFP_KERNEL);
  329. pinfo = kmalloc(sizeof(*pinfo), GFP_KERNEL);
  330. if (! info || ! pinfo) {
  331. err = -ENOMEM;
  332. goto __error;
  333. }
  334. memset(&callbacks, 0, sizeof(callbacks));
  335. callbacks.private_data = rdev;
  336. callbacks.allow_input = 1;
  337. callbacks.allow_output = 1;
  338. client = snd_seq_create_kernel_client(rdev->card, rdev->device, &callbacks);
  339. if (client < 0) {
  340. err = client;
  341. goto __error;
  342. }
  343. rdev->client = client;
  344. /* set client name */
  345. memset(info, 0, sizeof(*info));
  346. info->client = client;
  347. info->type = KERNEL_CLIENT;
  348. sprintf(info->name, "%s %d-%d", rdev->rmidi->name, rdev->card->number, rdev->device);
  349. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, info);
  350. /* create a port */
  351. memset(pinfo, 0, sizeof(*pinfo));
  352. pinfo->addr.client = client;
  353. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  354. /* set all capabilities */
  355. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  356. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  357. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  358. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC;
  359. pinfo->midi_channels = 16;
  360. memset(&pcallbacks, 0, sizeof(pcallbacks));
  361. pcallbacks.owner = THIS_MODULE;
  362. pcallbacks.private_data = rdev;
  363. pcallbacks.subscribe = snd_virmidi_subscribe;
  364. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  365. pcallbacks.use = snd_virmidi_use;
  366. pcallbacks.unuse = snd_virmidi_unuse;
  367. pcallbacks.event_input = snd_virmidi_event_input;
  368. pinfo->kernel = &pcallbacks;
  369. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
  370. if (err < 0) {
  371. snd_seq_delete_kernel_client(client);
  372. rdev->client = -1;
  373. goto __error;
  374. }
  375. rdev->port = pinfo->addr.port;
  376. err = 0; /* success */
  377. __error:
  378. kfree(info);
  379. kfree(pinfo);
  380. return err;
  381. }
  382. /*
  383. * release the sequencer client
  384. */
  385. static void snd_virmidi_dev_detach_seq(snd_virmidi_dev_t *rdev)
  386. {
  387. if (rdev->client >= 0) {
  388. snd_seq_delete_kernel_client(rdev->client);
  389. rdev->client = -1;
  390. }
  391. }
  392. /*
  393. * register the device
  394. */
  395. static int snd_virmidi_dev_register(snd_rawmidi_t *rmidi)
  396. {
  397. snd_virmidi_dev_t *rdev = rmidi->private_data;
  398. int err;
  399. switch (rdev->seq_mode) {
  400. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  401. err = snd_virmidi_dev_attach_seq(rdev);
  402. if (err < 0)
  403. return err;
  404. break;
  405. case SNDRV_VIRMIDI_SEQ_ATTACH:
  406. if (rdev->client == 0)
  407. return -EINVAL;
  408. /* should check presence of port more strictly.. */
  409. break;
  410. default:
  411. snd_printk(KERN_ERR "seq_mode is not set: %d\n", rdev->seq_mode);
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * unregister the device
  418. */
  419. static int snd_virmidi_dev_unregister(snd_rawmidi_t *rmidi)
  420. {
  421. snd_virmidi_dev_t *rdev = rmidi->private_data;
  422. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  423. snd_virmidi_dev_detach_seq(rdev);
  424. return 0;
  425. }
  426. /*
  427. *
  428. */
  429. static snd_rawmidi_global_ops_t snd_virmidi_global_ops = {
  430. .dev_register = snd_virmidi_dev_register,
  431. .dev_unregister = snd_virmidi_dev_unregister,
  432. };
  433. /*
  434. * free device
  435. */
  436. static void snd_virmidi_free(snd_rawmidi_t *rmidi)
  437. {
  438. snd_virmidi_dev_t *rdev = rmidi->private_data;
  439. kfree(rdev);
  440. }
  441. /*
  442. * create a new device
  443. *
  444. */
  445. /* exported */
  446. int snd_virmidi_new(snd_card_t *card, int device, snd_rawmidi_t **rrmidi)
  447. {
  448. snd_rawmidi_t *rmidi;
  449. snd_virmidi_dev_t *rdev;
  450. int err;
  451. *rrmidi = NULL;
  452. if ((err = snd_rawmidi_new(card, "VirMidi", device,
  453. 16, /* may be configurable */
  454. 16, /* may be configurable */
  455. &rmidi)) < 0)
  456. return err;
  457. strcpy(rmidi->name, rmidi->id);
  458. rdev = kcalloc(1, sizeof(*rdev), GFP_KERNEL);
  459. if (rdev == NULL) {
  460. snd_device_free(card, rmidi);
  461. return -ENOMEM;
  462. }
  463. rdev->card = card;
  464. rdev->rmidi = rmidi;
  465. rdev->device = device;
  466. rdev->client = -1;
  467. rwlock_init(&rdev->filelist_lock);
  468. INIT_LIST_HEAD(&rdev->filelist);
  469. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  470. rmidi->private_data = rdev;
  471. rmidi->private_free = snd_virmidi_free;
  472. rmidi->ops = &snd_virmidi_global_ops;
  473. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
  474. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
  475. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  476. SNDRV_RAWMIDI_INFO_OUTPUT |
  477. SNDRV_RAWMIDI_INFO_DUPLEX;
  478. *rrmidi = rmidi;
  479. return 0;
  480. }
  481. /*
  482. * ENTRY functions
  483. */
  484. static int __init alsa_virmidi_init(void)
  485. {
  486. return 0;
  487. }
  488. static void __exit alsa_virmidi_exit(void)
  489. {
  490. }
  491. module_init(alsa_virmidi_init)
  492. module_exit(alsa_virmidi_exit)
  493. EXPORT_SYMBOL(snd_virmidi_new);