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. unsigned char msg[4];
  80. int len;
  81. read_lock(&rdev->filelist_lock);
  82. list_for_each_entry(vmidi, &rdev->filelist, list) {
  83. if (!vmidi->trigger)
  84. continue;
  85. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  86. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  87. continue;
  88. snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
  89. } else {
  90. len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
  91. if (len > 0)
  92. snd_rawmidi_receive(vmidi->substream, msg, len);
  93. }
  94. }
  95. read_unlock(&rdev->filelist_lock);
  96. return 0;
  97. }
  98. /*
  99. * receive an event from the remote virmidi port
  100. *
  101. * for rawmidi inputs, you can call this function from the event
  102. * handler of a remote port which is attached to the virmidi via
  103. * SNDRV_VIRMIDI_SEQ_ATTACH.
  104. */
  105. #if 0
  106. int snd_virmidi_receive(struct snd_rawmidi *rmidi, struct snd_seq_event *ev)
  107. {
  108. struct snd_virmidi_dev *rdev;
  109. rdev = rmidi->private_data;
  110. return snd_virmidi_dev_receive_event(rdev, ev);
  111. }
  112. #endif /* 0 */
  113. /*
  114. * event handler of virmidi port
  115. */
  116. static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
  117. void *private_data, int atomic, int hop)
  118. {
  119. struct snd_virmidi_dev *rdev;
  120. rdev = private_data;
  121. if (!(rdev->flags & SNDRV_VIRMIDI_USE))
  122. return 0; /* ignored */
  123. return snd_virmidi_dev_receive_event(rdev, ev);
  124. }
  125. /*
  126. * trigger rawmidi stream for input
  127. */
  128. static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  129. {
  130. struct snd_virmidi *vmidi = substream->runtime->private_data;
  131. if (up) {
  132. vmidi->trigger = 1;
  133. } else {
  134. vmidi->trigger = 0;
  135. }
  136. }
  137. /*
  138. * trigger rawmidi stream for output
  139. */
  140. static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  141. {
  142. struct snd_virmidi *vmidi = substream->runtime->private_data;
  143. int count, res;
  144. unsigned char buf[32], *pbuf;
  145. if (up) {
  146. vmidi->trigger = 1;
  147. if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
  148. !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
  149. snd_rawmidi_transmit_ack(substream, substream->runtime->buffer_size - substream->runtime->avail);
  150. return; /* ignored */
  151. }
  152. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  153. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, in_atomic(), 0) < 0)
  154. return;
  155. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  156. }
  157. while (1) {
  158. count = snd_rawmidi_transmit_peek(substream, buf, sizeof(buf));
  159. if (count <= 0)
  160. break;
  161. pbuf = buf;
  162. while (count > 0) {
  163. res = snd_midi_event_encode(vmidi->parser, pbuf, count, &vmidi->event);
  164. if (res < 0) {
  165. snd_midi_event_reset_encode(vmidi->parser);
  166. continue;
  167. }
  168. snd_rawmidi_transmit_ack(substream, res);
  169. pbuf += res;
  170. count -= res;
  171. if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
  172. if (snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, in_atomic(), 0) < 0)
  173. return;
  174. vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
  175. }
  176. }
  177. }
  178. } else {
  179. vmidi->trigger = 0;
  180. }
  181. }
  182. /*
  183. * open rawmidi handle for input
  184. */
  185. static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
  186. {
  187. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  188. struct snd_rawmidi_runtime *runtime = substream->runtime;
  189. struct snd_virmidi *vmidi;
  190. unsigned long flags;
  191. vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
  192. if (vmidi == NULL)
  193. return -ENOMEM;
  194. vmidi->substream = substream;
  195. if (snd_midi_event_new(0, &vmidi->parser) < 0) {
  196. kfree(vmidi);
  197. return -ENOMEM;
  198. }
  199. vmidi->seq_mode = rdev->seq_mode;
  200. vmidi->client = rdev->client;
  201. vmidi->port = rdev->port;
  202. runtime->private_data = vmidi;
  203. write_lock_irqsave(&rdev->filelist_lock, flags);
  204. list_add_tail(&vmidi->list, &rdev->filelist);
  205. write_unlock_irqrestore(&rdev->filelist_lock, flags);
  206. vmidi->rdev = rdev;
  207. return 0;
  208. }
  209. /*
  210. * open rawmidi handle for output
  211. */
  212. static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
  213. {
  214. struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
  215. struct snd_rawmidi_runtime *runtime = substream->runtime;
  216. struct snd_virmidi *vmidi;
  217. vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
  218. if (vmidi == NULL)
  219. return -ENOMEM;
  220. vmidi->substream = substream;
  221. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
  222. kfree(vmidi);
  223. return -ENOMEM;
  224. }
  225. vmidi->seq_mode = rdev->seq_mode;
  226. vmidi->client = rdev->client;
  227. vmidi->port = rdev->port;
  228. snd_virmidi_init_event(vmidi, &vmidi->event);
  229. vmidi->rdev = rdev;
  230. runtime->private_data = vmidi;
  231. return 0;
  232. }
  233. /*
  234. * close rawmidi handle for input
  235. */
  236. static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
  237. {
  238. struct snd_virmidi *vmidi = substream->runtime->private_data;
  239. snd_midi_event_free(vmidi->parser);
  240. list_del(&vmidi->list);
  241. substream->runtime->private_data = NULL;
  242. kfree(vmidi);
  243. return 0;
  244. }
  245. /*
  246. * close rawmidi handle for output
  247. */
  248. static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
  249. {
  250. struct snd_virmidi *vmidi = substream->runtime->private_data;
  251. snd_midi_event_free(vmidi->parser);
  252. substream->runtime->private_data = NULL;
  253. kfree(vmidi);
  254. return 0;
  255. }
  256. /*
  257. * subscribe callback - allow output to rawmidi device
  258. */
  259. static int snd_virmidi_subscribe(void *private_data,
  260. struct snd_seq_port_subscribe *info)
  261. {
  262. struct snd_virmidi_dev *rdev;
  263. rdev = private_data;
  264. if (!try_module_get(rdev->card->module))
  265. return -EFAULT;
  266. rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
  267. return 0;
  268. }
  269. /*
  270. * unsubscribe callback - disallow output to rawmidi device
  271. */
  272. static int snd_virmidi_unsubscribe(void *private_data,
  273. struct snd_seq_port_subscribe *info)
  274. {
  275. struct snd_virmidi_dev *rdev;
  276. rdev = private_data;
  277. rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
  278. module_put(rdev->card->module);
  279. return 0;
  280. }
  281. /*
  282. * use callback - allow input to rawmidi device
  283. */
  284. static int snd_virmidi_use(void *private_data,
  285. struct snd_seq_port_subscribe *info)
  286. {
  287. struct snd_virmidi_dev *rdev;
  288. rdev = private_data;
  289. if (!try_module_get(rdev->card->module))
  290. return -EFAULT;
  291. rdev->flags |= SNDRV_VIRMIDI_USE;
  292. return 0;
  293. }
  294. /*
  295. * unuse callback - disallow input to rawmidi device
  296. */
  297. static int snd_virmidi_unuse(void *private_data,
  298. struct snd_seq_port_subscribe *info)
  299. {
  300. struct snd_virmidi_dev *rdev;
  301. rdev = private_data;
  302. rdev->flags &= ~SNDRV_VIRMIDI_USE;
  303. module_put(rdev->card->module);
  304. return 0;
  305. }
  306. /*
  307. * Register functions
  308. */
  309. static struct snd_rawmidi_ops snd_virmidi_input_ops = {
  310. .open = snd_virmidi_input_open,
  311. .close = snd_virmidi_input_close,
  312. .trigger = snd_virmidi_input_trigger,
  313. };
  314. static struct snd_rawmidi_ops snd_virmidi_output_ops = {
  315. .open = snd_virmidi_output_open,
  316. .close = snd_virmidi_output_close,
  317. .trigger = snd_virmidi_output_trigger,
  318. };
  319. /*
  320. * create a sequencer client and a port
  321. */
  322. static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
  323. {
  324. int client;
  325. struct snd_seq_port_callback pcallbacks;
  326. struct snd_seq_port_info *pinfo;
  327. int err;
  328. if (rdev->client >= 0)
  329. return 0;
  330. pinfo = kmalloc(sizeof(*pinfo), GFP_KERNEL);
  331. if (!pinfo) {
  332. err = -ENOMEM;
  333. goto __error;
  334. }
  335. client = snd_seq_create_kernel_client(rdev->card, rdev->device,
  336. "%s %d-%d", rdev->rmidi->name,
  337. rdev->card->number,
  338. rdev->device);
  339. if (client < 0) {
  340. err = client;
  341. goto __error;
  342. }
  343. rdev->client = client;
  344. /* create a port */
  345. memset(pinfo, 0, sizeof(*pinfo));
  346. pinfo->addr.client = client;
  347. sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
  348. /* set all capabilities */
  349. pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  350. pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
  351. pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
  352. pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
  353. | SNDRV_SEQ_PORT_TYPE_SOFTWARE
  354. | SNDRV_SEQ_PORT_TYPE_PORT;
  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);