motu-midi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * motu-midi.h - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "motu.h"
  9. static int midi_capture_open(struct snd_rawmidi_substream *substream)
  10. {
  11. struct snd_motu *motu = substream->rmidi->private_data;
  12. int err;
  13. err = snd_motu_stream_lock_try(motu);
  14. if (err < 0)
  15. return err;
  16. mutex_lock(&motu->mutex);
  17. motu->capture_substreams++;
  18. err = snd_motu_stream_start_duplex(motu, 0);
  19. mutex_unlock(&motu->mutex);
  20. if (err < 0)
  21. snd_motu_stream_lock_release(motu);
  22. return err;
  23. }
  24. static int midi_playback_open(struct snd_rawmidi_substream *substream)
  25. {
  26. struct snd_motu *motu = substream->rmidi->private_data;
  27. int err;
  28. err = snd_motu_stream_lock_try(motu);
  29. if (err < 0)
  30. return err;
  31. mutex_lock(&motu->mutex);
  32. motu->playback_substreams++;
  33. err = snd_motu_stream_start_duplex(motu, 0);
  34. mutex_unlock(&motu->mutex);
  35. if (err < 0)
  36. snd_motu_stream_lock_release(motu);
  37. return err;
  38. }
  39. static int midi_capture_close(struct snd_rawmidi_substream *substream)
  40. {
  41. struct snd_motu *motu = substream->rmidi->private_data;
  42. mutex_lock(&motu->mutex);
  43. motu->capture_substreams--;
  44. snd_motu_stream_stop_duplex(motu);
  45. mutex_unlock(&motu->mutex);
  46. snd_motu_stream_lock_release(motu);
  47. return 0;
  48. }
  49. static int midi_playback_close(struct snd_rawmidi_substream *substream)
  50. {
  51. struct snd_motu *motu = substream->rmidi->private_data;
  52. mutex_lock(&motu->mutex);
  53. motu->playback_substreams--;
  54. snd_motu_stream_stop_duplex(motu);
  55. mutex_unlock(&motu->mutex);
  56. snd_motu_stream_lock_release(motu);
  57. return 0;
  58. }
  59. static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
  60. {
  61. struct snd_motu *motu = substrm->rmidi->private_data;
  62. unsigned long flags;
  63. spin_lock_irqsave(&motu->lock, flags);
  64. if (up)
  65. amdtp_motu_midi_trigger(&motu->tx_stream, substrm->number,
  66. substrm);
  67. else
  68. amdtp_motu_midi_trigger(&motu->tx_stream, substrm->number,
  69. NULL);
  70. spin_unlock_irqrestore(&motu->lock, flags);
  71. }
  72. static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
  73. {
  74. struct snd_motu *motu = substrm->rmidi->private_data;
  75. unsigned long flags;
  76. spin_lock_irqsave(&motu->lock, flags);
  77. if (up)
  78. amdtp_motu_midi_trigger(&motu->rx_stream, substrm->number,
  79. substrm);
  80. else
  81. amdtp_motu_midi_trigger(&motu->rx_stream, substrm->number,
  82. NULL);
  83. spin_unlock_irqrestore(&motu->lock, flags);
  84. }
  85. static void set_midi_substream_names(struct snd_motu *motu,
  86. struct snd_rawmidi_str *str)
  87. {
  88. struct snd_rawmidi_substream *subs;
  89. list_for_each_entry(subs, &str->substreams, list) {
  90. snprintf(subs->name, sizeof(subs->name),
  91. "%s MIDI %d", motu->card->shortname, subs->number + 1);
  92. }
  93. }
  94. int snd_motu_create_midi_devices(struct snd_motu *motu)
  95. {
  96. static const struct snd_rawmidi_ops capture_ops = {
  97. .open = midi_capture_open,
  98. .close = midi_capture_close,
  99. .trigger = midi_capture_trigger,
  100. };
  101. static const struct snd_rawmidi_ops playback_ops = {
  102. .open = midi_playback_open,
  103. .close = midi_playback_close,
  104. .trigger = midi_playback_trigger,
  105. };
  106. struct snd_rawmidi *rmidi;
  107. struct snd_rawmidi_str *str;
  108. int err;
  109. /* create midi ports */
  110. err = snd_rawmidi_new(motu->card, motu->card->driver, 0, 1, 1, &rmidi);
  111. if (err < 0)
  112. return err;
  113. snprintf(rmidi->name, sizeof(rmidi->name),
  114. "%s MIDI", motu->card->shortname);
  115. rmidi->private_data = motu;
  116. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT |
  117. SNDRV_RAWMIDI_INFO_OUTPUT |
  118. SNDRV_RAWMIDI_INFO_DUPLEX;
  119. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  120. &capture_ops);
  121. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
  122. set_midi_substream_names(motu, str);
  123. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  124. &playback_ops);
  125. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  126. set_midi_substream_names(motu, str);
  127. return 0;
  128. }