seq_virmidi.c 14 KB

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