oxfw-midi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_am824_midi_trigger(&oxfw->tx_stream,
  66. substrm->number, substrm);
  67. else
  68. amdtp_am824_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_am824_midi_trigger(&oxfw->rx_stream,
  79. substrm->number, substrm);
  80. else
  81. amdtp_am824_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_rawmidi *rmidi;
  108. struct snd_rawmidi_str *str;
  109. int err;
  110. if (oxfw->midi_input_ports == 0 && oxfw->midi_output_ports == 0)
  111. return 0;
  112. /* create midi ports */
  113. err = snd_rawmidi_new(oxfw->card, oxfw->card->driver, 0,
  114. oxfw->midi_output_ports, oxfw->midi_input_ports,
  115. &rmidi);
  116. if (err < 0)
  117. return err;
  118. snprintf(rmidi->name, sizeof(rmidi->name),
  119. "%s MIDI", oxfw->card->shortname);
  120. rmidi->private_data = oxfw;
  121. if (oxfw->midi_input_ports > 0) {
  122. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  123. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  124. &midi_capture_ops);
  125. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
  126. set_midi_substream_names(oxfw, str);
  127. }
  128. if (oxfw->midi_output_ports > 0) {
  129. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  130. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  131. &midi_playback_ops);
  132. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  133. set_midi_substream_names(oxfw, str);
  134. }
  135. if ((oxfw->midi_output_ports > 0) && (oxfw->midi_input_ports > 0))
  136. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  137. return 0;
  138. }