seq_virmidi.c 14 KB

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