seq_virmidi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. {
  77. struct snd_virmidi *vmidi;
  78. unsigned char msg[4];
  79. int len;
  80. read_lock(&rdev->filelist_lock);
  81. list_for_each_entry(vmidi, &rdev->filelist, list) {
  82. if (!vmidi->trigger)
  83. continue;
  84. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  85. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  86. continue;
  87. snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
  88. } else {
  89. len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
  90. if (len > 0)
  91. snd_rawmidi_receive(vmidi->substream, msg, len);
  92. }
  93. }
  94. read_unlock(&rdev->filelist_lock);
  95. return 0;
  96. }
  97. /*
  98. * receive an event from the remote virmidi port
  99. *
  100. * for rawmidi inputs, you can call this function from the event
  101. * handler of a remote port which is attached to the virmidi via
  102. * SNDRV_VIRMIDI_SEQ_ATTACH.
  103. */
  104. #if 0
  105. int snd_virmidi_receive(struct snd_rawmidi *rmidi, struct snd_seq_event *ev)
  106. {
  107. struct snd_virmidi_dev *rdev;
  108. rdev = rmidi->private_data;
  109. return snd_virmidi_dev_receive_event(rdev, ev);
  110. }
  111. #endif /* 0 */
  112. /*
  113. * event handler of virmidi port
  114. */
  115. static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
  116. void *private_data, int atomic, int hop)
  117. {
  118. struct snd_virmidi_dev *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(struct snd_rawmidi_substream *substream, int up)
  128. {
  129. struct snd_virmidi *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(struct snd_rawmidi_substream *substream, int up)
  140. {
  141. struct snd_virmidi *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, in_atomic(), 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, in_atomic(), 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(struct snd_rawmidi_substream *substream)
  185. {
  186. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  187. struct snd_rawmidi_runtime *runtime = substream->runtime;
  188. struct snd_virmidi *vmidi;
  189. unsigned long flags;
  190. vmidi = kzalloc(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(struct snd_rawmidi_substream *substream)
  212. {
  213. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  214. struct snd_rawmidi_runtime *runtime = substream->runtime;
  215. struct snd_virmidi *vmidi;
  216. vmidi = kzalloc(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(struct snd_rawmidi_substream *substream)
  236. {
  237. struct snd_virmidi *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(struct snd_rawmidi_substream *substream)
  248. {
  249. struct snd_virmidi *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,
  259. struct snd_seq_port_subscribe *info)
  260. {
  261. struct snd_virmidi_dev *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,
  272. struct snd_seq_port_subscribe *info)
  273. {
  274. struct snd_virmidi_dev *rdev;
  275. rdev = private_data;
  276. rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
  277. module_put(rdev->card->module);
  278. return 0;
  279. }
  280. /*
  281. * use callback - allow input to rawmidi device
  282. */
  283. static int snd_virmidi_use(void *private_data,
  284. struct snd_seq_port_subscribe *info)
  285. {
  286. struct snd_virmidi_dev *rdev;
  287. rdev = private_data;
  288. if (!try_module_get(rdev->card->module))
  289. return -EFAULT;
  290. rdev->flags |= SNDRV_VIRMIDI_USE;
  291. return 0;
  292. }
  293. /*
  294. * unuse callback - disallow input to rawmidi device
  295. */
  296. static int snd_virmidi_unuse(void *private_data,
  297. struct snd_seq_port_subscribe *info)
  298. {
  299. struct snd_virmidi_dev *rdev;
  300. rdev = private_data;
  301. rdev->flags &= ~SNDRV_VIRMIDI_USE;
  302. module_put(rdev->card->module);
  303. return 0;
  304. }
  305. /*
  306. * Register functions
  307. */
  308. static struct snd_rawmidi_ops snd_virmidi_input_ops = {
  309. .open = snd_virmidi_input_open,
  310. .close = snd_virmidi_input_close,
  311. .trigger = snd_virmidi_input_trigger,
  312. };
  313. static struct snd_rawmidi_ops snd_virmidi_output_ops = {
  314. .open = snd_virmidi_output_open,
  315. .close = snd_virmidi_output_close,
  316. .trigger = snd_virmidi_output_trigger,
  317. };
  318. /*
  319. * create a sequencer client and a port
  320. */
  321. static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
  322. {
  323. int client;
  324. struct snd_seq_port_callback pcallbacks;
  325. struct snd_seq_port_info *pinfo;
  326. int err;
  327. if (rdev->client >= 0)
  328. return 0;
  329. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  330. if (!pinfo) {
  331. err = -ENOMEM;
  332. goto __error;
  333. }
  334. client = snd_seq_create_kernel_client(rdev->card, rdev->device,
  335. "%s %d-%d", rdev->rmidi->name,
  336. rdev->card->number,
  337. rdev->device);
  338. if (client < 0) {
  339. err = client;
  340. goto __error;
  341. }
  342. rdev->client = client;
  343. /* create a port */
  344. pinfo->addr.client = client;
  345. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  346. /* set all capabilities */
  347. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  348. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  349. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  350. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  351. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  352. | SNDRV_SEQ_PORT_TYPE_PORT;
  353. pinfo->midi_channels = 16;
  354. memset(&pcallbacks, 0, sizeof(pcallbacks));
  355. pcallbacks.owner = THIS_MODULE;
  356. pcallbacks.private_data = rdev;
  357. pcallbacks.subscribe = snd_virmidi_subscribe;
  358. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  359. pcallbacks.use = snd_virmidi_use;
  360. pcallbacks.unuse = snd_virmidi_unuse;
  361. pcallbacks.event_input = snd_virmidi_event_input;
  362. pinfo->kernel = &pcallbacks;
  363. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
  364. if (err < 0) {
  365. snd_seq_delete_kernel_client(client);
  366. rdev->client = -1;
  367. goto __error;
  368. }
  369. rdev->port = pinfo->addr.port;
  370. err = 0; /* success */
  371. __error:
  372. kfree(pinfo);
  373. return err;
  374. }
  375. /*
  376. * release the sequencer client
  377. */
  378. static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
  379. {
  380. if (rdev->client >= 0) {
  381. snd_seq_delete_kernel_client(rdev->client);
  382. rdev->client = -1;
  383. }
  384. }
  385. /*
  386. * register the device
  387. */
  388. static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
  389. {
  390. struct snd_virmidi_dev *rdev = rmidi->private_data;
  391. int err;
  392. switch (rdev->seq_mode) {
  393. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  394. err = snd_virmidi_dev_attach_seq(rdev);
  395. if (err < 0)
  396. return err;
  397. break;
  398. case SNDRV_VIRMIDI_SEQ_ATTACH:
  399. if (rdev->client == 0)
  400. return -EINVAL;
  401. /* should check presence of port more strictly.. */
  402. break;
  403. default:
  404. pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
  405. return -EINVAL;
  406. }
  407. return 0;
  408. }
  409. /*
  410. * unregister the device
  411. */
  412. static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
  413. {
  414. struct snd_virmidi_dev *rdev = rmidi->private_data;
  415. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  416. snd_virmidi_dev_detach_seq(rdev);
  417. return 0;
  418. }
  419. /*
  420. *
  421. */
  422. static struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
  423. .dev_register = snd_virmidi_dev_register,
  424. .dev_unregister = snd_virmidi_dev_unregister,
  425. };
  426. /*
  427. * free device
  428. */
  429. static void snd_virmidi_free(struct snd_rawmidi *rmidi)
  430. {
  431. struct snd_virmidi_dev *rdev = rmidi->private_data;
  432. kfree(rdev);
  433. }
  434. /*
  435. * create a new device
  436. *
  437. */
  438. /* exported */
  439. int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
  440. {
  441. struct snd_rawmidi *rmidi;
  442. struct snd_virmidi_dev *rdev;
  443. int err;
  444. *rrmidi = NULL;
  445. if ((err = snd_rawmidi_new(card, "VirMidi", device,
  446. 16, /* may be configurable */
  447. 16, /* may be configurable */
  448. &rmidi)) < 0)
  449. return err;
  450. strcpy(rmidi->name, rmidi->id);
  451. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  452. if (rdev == NULL) {
  453. snd_device_free(card, rmidi);
  454. return -ENOMEM;
  455. }
  456. rdev->card = card;
  457. rdev->rmidi = rmidi;
  458. rdev->device = device;
  459. rdev->client = -1;
  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. /*
  475. * ENTRY functions
  476. */
  477. static int __init alsa_virmidi_init(void)
  478. {
  479. return 0;
  480. }
  481. static void __exit alsa_virmidi_exit(void)
  482. {
  483. }
  484. module_init(alsa_virmidi_init)
  485. module_exit(alsa_virmidi_exit)
  486. EXPORT_SYMBOL(snd_virmidi_new);