oxfw-midi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * oxfw_midi.c - a part of driver for OXFW970/971 based devices
  3. *
  4. * Copyright (c) 2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "oxfw.h"
  9. static int midi_capture_open(struct snd_rawmidi_substream *substream)
  10. {
  11. struct snd_oxfw *oxfw = substream->rmidi->private_data;
  12. int err;
  13. err = snd_oxfw_stream_lock_try(oxfw);
  14. if (err < 0)
  15. return err;
  16. mutex_lock(&oxfw->mutex);
  17. oxfw->capture_substreams++;
  18. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream, 0, 0);
  19. mutex_unlock(&oxfw->mutex);
  20. if (err < 0)
  21. snd_oxfw_stream_lock_release(oxfw);
  22. return err;
  23. }
  24. static int midi_playback_open(struct snd_rawmidi_substream *substream)
  25. {
  26. struct snd_oxfw *oxfw = substream->rmidi->private_data;
  27. int err;
  28. err = snd_oxfw_stream_lock_try(oxfw);
  29. if (err < 0)
  30. return err;
  31. mutex_lock(&oxfw->mutex);
  32. oxfw->playback_substreams++;
  33. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream, 0, 0);
  34. mutex_unlock(&oxfw->mutex);
  35. if (err < 0)
  36. snd_oxfw_stream_lock_release(oxfw);
  37. return err;
  38. }
  39. static int midi_capture_close(struct snd_rawmidi_substream *substream)
  40. {
  41. struct snd_oxfw *oxfw = substream->rmidi->private_data;
  42. mutex_lock(&oxfw->mutex);
  43. oxfw->capture_substreams--;
  44. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream);
  45. mutex_unlock(&oxfw->mutex);
  46. snd_oxfw_stream_lock_release(oxfw);
  47. return 0;
  48. }
  49. static int midi_playback_close(struct snd_rawmidi_substream *substream)
  50. {
  51. struct snd_oxfw *oxfw = substream->rmidi->private_data;
  52. mutex_lock(&oxfw->mutex);
  53. oxfw->playback_substreams--;
  54. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream);
  55. mutex_unlock(&oxfw->mutex);
  56. snd_oxfw_stream_lock_release(oxfw);
  57. return 0;
  58. }
  59. static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
  60. {
  61. struct snd_oxfw *oxfw = substrm->rmidi->private_data;
  62. unsigned long flags;
  63. spin_lock_irqsave(&oxfw->lock, flags);
  64. if (up)
  65. amdtp_stream_midi_trigger(&oxfw->tx_stream,
  66. substrm->number, substrm);
  67. else
  68. amdtp_stream_midi_trigger(&oxfw->tx_stream,
  69. substrm->number, NULL);
  70. spin_unlock_irqrestore(&oxfw->lock, flags);
  71. }
  72. static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
  73. {
  74. struct snd_oxfw *oxfw = substrm->rmidi->private_data;
  75. unsigned long flags;
  76. spin_lock_irqsave(&oxfw->lock, flags);
  77. if (up)
  78. amdtp_stream_midi_trigger(&oxfw->rx_stream,
  79. substrm->number, substrm);
  80. else
  81. amdtp_stream_midi_trigger(&oxfw->rx_stream,
  82. substrm->number, NULL);
  83. spin_unlock_irqrestore(&oxfw->lock, flags);
  84. }
  85. static struct snd_rawmidi_ops midi_capture_ops = {
  86. .open = midi_capture_open,
  87. .close = midi_capture_close,
  88. .trigger = midi_capture_trigger,
  89. };
  90. static struct snd_rawmidi_ops midi_playback_ops = {
  91. .open = midi_playback_open,
  92. .close = midi_playback_close,
  93. .trigger = midi_playback_trigger,
  94. };
  95. static void set_midi_substream_names(struct snd_oxfw *oxfw,
  96. struct snd_rawmidi_str *str)
  97. {
  98. struct snd_rawmidi_substream *subs;
  99. list_for_each_entry(subs, &str->substreams, list) {
  100. snprintf(subs->name, sizeof(subs->name),
  101. "%s MIDI %d",
  102. oxfw->card->shortname, subs->number + 1);
  103. }
  104. }
  105. int snd_oxfw_create_midi(struct snd_oxfw *oxfw)
  106. {
  107. struct snd_oxfw_stream_formation formation;
  108. struct snd_rawmidi *rmidi;
  109. struct snd_rawmidi_str *str;
  110. u8 *format;
  111. int i, err;
  112. /* If its stream has MIDI conformant data channel, add one MIDI port */
  113. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  114. format = oxfw->tx_stream_formats[i];
  115. if (format != NULL) {
  116. err = snd_oxfw_stream_parse_format(format, &formation);
  117. if (err >= 0 && formation.midi > 0)
  118. oxfw->midi_input_ports = 1;
  119. }
  120. format = oxfw->rx_stream_formats[i];
  121. if (format != NULL) {
  122. err = snd_oxfw_stream_parse_format(format, &formation);
  123. if (err >= 0 && formation.midi > 0)
  124. oxfw->midi_output_ports = 1;
  125. }
  126. }
  127. if ((oxfw->midi_input_ports == 0) && (oxfw->midi_output_ports == 0))
  128. return 0;
  129. /* create midi ports */
  130. err = snd_rawmidi_new(oxfw->card, oxfw->card->driver, 0,
  131. oxfw->midi_output_ports, oxfw->midi_input_ports,
  132. &rmidi);
  133. if (err < 0)
  134. return err;
  135. snprintf(rmidi->name, sizeof(rmidi->name),
  136. "%s MIDI", oxfw->card->shortname);
  137. rmidi->private_data = oxfw;
  138. if (oxfw->midi_input_ports > 0) {
  139. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  140. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  141. &midi_capture_ops);
  142. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
  143. set_midi_substream_names(oxfw, str);
  144. }
  145. if (oxfw->midi_output_ports > 0) {
  146. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  147. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  148. &midi_playback_ops);
  149. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  150. set_midi_substream_names(oxfw, str);
  151. }
  152. if ((oxfw->midi_output_ports > 0) && (oxfw->midi_input_ports > 0))
  153. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  154. return 0;
  155. }