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 <sound/driver.h>
  37. #include <linux/init.h>
  38. #include <linux/wait.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 = kmalloc(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. memset(pinfo, 0, sizeof(*pinfo));
  345. pinfo->addr.client = client;
  346. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  347. /* set all capabilities */
  348. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  349. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  350. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  351. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  352. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  353. | SNDRV_SEQ_PORT_TYPE_PORT;
  354. pinfo->midi_channels = 16;
  355. memset(&pcallbacks, 0, sizeof(pcallbacks));
  356. pcallbacks.owner = THIS_MODULE;
  357. pcallbacks.private_data = rdev;
  358. pcallbacks.subscribe = snd_virmidi_subscribe;
  359. pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
  360. pcallbacks.use = snd_virmidi_use;
  361. pcallbacks.unuse = snd_virmidi_unuse;
  362. pcallbacks.event_input = snd_virmidi_event_input;
  363. pinfo->kernel = &pcallbacks;
  364. err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
  365. if (err < 0) {
  366. snd_seq_delete_kernel_client(client);
  367. rdev->client = -1;
  368. goto __error;
  369. }
  370. rdev->port = pinfo->addr.port;
  371. err = 0; /* success */
  372. __error:
  373. kfree(pinfo);
  374. return err;
  375. }
  376. /*
  377. * release the sequencer client
  378. */
  379. static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
  380. {
  381. if (rdev->client >= 0) {
  382. snd_seq_delete_kernel_client(rdev->client);
  383. rdev->client = -1;
  384. }
  385. }
  386. /*
  387. * register the device
  388. */
  389. static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
  390. {
  391. struct snd_virmidi_dev *rdev = rmidi->private_data;
  392. int err;
  393. switch (rdev->seq_mode) {
  394. case SNDRV_VIRMIDI_SEQ_DISPATCH:
  395. err = snd_virmidi_dev_attach_seq(rdev);
  396. if (err < 0)
  397. return err;
  398. break;
  399. case SNDRV_VIRMIDI_SEQ_ATTACH:
  400. if (rdev->client == 0)
  401. return -EINVAL;
  402. /* should check presence of port more strictly.. */
  403. break;
  404. default:
  405. snd_printk(KERN_ERR "seq_mode is not set: %d\n", rdev->seq_mode);
  406. return -EINVAL;
  407. }
  408. return 0;
  409. }
  410. /*
  411. * unregister the device
  412. */
  413. static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
  414. {
  415. struct snd_virmidi_dev *rdev = rmidi->private_data;
  416. if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
  417. snd_virmidi_dev_detach_seq(rdev);
  418. return 0;
  419. }
  420. /*
  421. *
  422. */
  423. static struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
  424. .dev_register = snd_virmidi_dev_register,
  425. .dev_unregister = snd_virmidi_dev_unregister,
  426. };
  427. /*
  428. * free device
  429. */
  430. static void snd_virmidi_free(struct snd_rawmidi *rmidi)
  431. {
  432. struct snd_virmidi_dev *rdev = rmidi->private_data;
  433. kfree(rdev);
  434. }
  435. /*
  436. * create a new device
  437. *
  438. */
  439. /* exported */
  440. int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
  441. {
  442. struct snd_rawmidi *rmidi;
  443. struct snd_virmidi_dev *rdev;
  444. int err;
  445. *rrmidi = NULL;
  446. if ((err = snd_rawmidi_new(card, "VirMidi", device,
  447. 16, /* may be configurable */
  448. 16, /* may be configurable */
  449. &rmidi)) < 0)
  450. return err;
  451. strcpy(rmidi->name, rmidi->id);
  452. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  453. if (rdev == NULL) {
  454. snd_device_free(card, rmidi);
  455. return -ENOMEM;
  456. }
  457. rdev->card = card;
  458. rdev->rmidi = rmidi;
  459. rdev->device = device;
  460. rdev->client = -1;
  461. rwlock_init(&rdev->filelist_lock);
  462. INIT_LIST_HEAD(&rdev->filelist);
  463. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
  464. rmidi->private_data = rdev;
  465. rmidi->private_free = snd_virmidi_free;
  466. rmidi->ops = &snd_virmidi_global_ops;
  467. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
  468. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
  469. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  470. SNDRV_RAWMIDI_INFO_OUTPUT |
  471. SNDRV_RAWMIDI_INFO_DUPLEX;
  472. *rrmidi = rmidi;
  473. return 0;
  474. }
  475. /*
  476. * ENTRY functions
  477. */
  478. static int __init alsa_virmidi_init(void)
  479. {
  480. return 0;
  481. }
  482. static void __exit alsa_virmidi_exit(void)
  483. {
  484. }
  485. module_init(alsa_virmidi_init)
  486. module_exit(alsa_virmidi_exit)
  487. EXPORT_SYMBOL(snd_virmidi_new);