seq_virmidi.c 14 KB

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