seq_virmidi.c 14 KB

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