seq_virmidi.c 14 KB

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