digi00x-midi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * digi00x-midi.h - a part of driver for Digidesign Digi 002/003 family
  3. *
  4. * Copyright (c) 2014-2015 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "digi00x.h"
  9. static int midi_open(struct snd_rawmidi_substream *substream)
  10. {
  11. struct snd_dg00x *dg00x = substream->rmidi->private_data;
  12. int err;
  13. err = snd_dg00x_stream_lock_try(dg00x);
  14. if (err < 0)
  15. return err;
  16. mutex_lock(&dg00x->mutex);
  17. dg00x->substreams_counter++;
  18. err = snd_dg00x_stream_start_duplex(dg00x, 0);
  19. mutex_unlock(&dg00x->mutex);
  20. if (err < 0)
  21. snd_dg00x_stream_lock_release(dg00x);
  22. return err;
  23. }
  24. static int midi_close(struct snd_rawmidi_substream *substream)
  25. {
  26. struct snd_dg00x *dg00x = substream->rmidi->private_data;
  27. mutex_lock(&dg00x->mutex);
  28. dg00x->substreams_counter--;
  29. snd_dg00x_stream_stop_duplex(dg00x);
  30. mutex_unlock(&dg00x->mutex);
  31. snd_dg00x_stream_lock_release(dg00x);
  32. return 0;
  33. }
  34. static void midi_capture_trigger(struct snd_rawmidi_substream *substream,
  35. int up)
  36. {
  37. struct snd_dg00x *dg00x = substream->rmidi->private_data;
  38. unsigned int port;
  39. unsigned long flags;
  40. if (substream->rmidi->device == 0)
  41. port = substream->number;
  42. else
  43. port = 2;
  44. spin_lock_irqsave(&dg00x->lock, flags);
  45. if (up)
  46. amdtp_dot_midi_trigger(&dg00x->tx_stream, port, substream);
  47. else
  48. amdtp_dot_midi_trigger(&dg00x->tx_stream, port, NULL);
  49. spin_unlock_irqrestore(&dg00x->lock, flags);
  50. }
  51. static void midi_playback_trigger(struct snd_rawmidi_substream *substream,
  52. int up)
  53. {
  54. struct snd_dg00x *dg00x = substream->rmidi->private_data;
  55. unsigned int port;
  56. unsigned long flags;
  57. if (substream->rmidi->device == 0)
  58. port = substream->number;
  59. else
  60. port = 2;
  61. spin_lock_irqsave(&dg00x->lock, flags);
  62. if (up)
  63. amdtp_dot_midi_trigger(&dg00x->rx_stream, port, substream);
  64. else
  65. amdtp_dot_midi_trigger(&dg00x->rx_stream, port, NULL);
  66. spin_unlock_irqrestore(&dg00x->lock, flags);
  67. }
  68. static void set_substream_names(struct snd_dg00x *dg00x,
  69. struct snd_rawmidi *rmidi, bool is_console)
  70. {
  71. struct snd_rawmidi_substream *subs;
  72. struct snd_rawmidi_str *str;
  73. int i;
  74. for (i = 0; i < 2; ++i) {
  75. str = &rmidi->streams[i];
  76. list_for_each_entry(subs, &str->substreams, list) {
  77. if (!is_console) {
  78. snprintf(subs->name, sizeof(subs->name),
  79. "%s MIDI %d",
  80. dg00x->card->shortname,
  81. subs->number + 1);
  82. } else {
  83. snprintf(subs->name, sizeof(subs->name),
  84. "%s control",
  85. dg00x->card->shortname);
  86. }
  87. }
  88. }
  89. }
  90. static int add_substream_pair(struct snd_dg00x *dg00x, unsigned int out_ports,
  91. unsigned int in_ports, bool is_console)
  92. {
  93. static const struct snd_rawmidi_ops capture_ops = {
  94. .open = midi_open,
  95. .close = midi_close,
  96. .trigger = midi_capture_trigger,
  97. };
  98. static const struct snd_rawmidi_ops playback_ops = {
  99. .open = midi_open,
  100. .close = midi_close,
  101. .trigger = midi_playback_trigger,
  102. };
  103. const char *label;
  104. struct snd_rawmidi *rmidi;
  105. int err;
  106. /* Add physical midi ports. */
  107. err = snd_rawmidi_new(dg00x->card, dg00x->card->driver, is_console,
  108. out_ports, in_ports, &rmidi);
  109. if (err < 0)
  110. return err;
  111. rmidi->private_data = dg00x;
  112. if (!is_console)
  113. label = "%s control";
  114. else
  115. label = "%s MIDI";
  116. snprintf(rmidi->name, sizeof(rmidi->name), label,
  117. dg00x->card->shortname);
  118. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &playback_ops);
  119. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &capture_ops);
  120. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT |
  121. SNDRV_RAWMIDI_INFO_OUTPUT |
  122. SNDRV_RAWMIDI_INFO_DUPLEX;
  123. set_substream_names(dg00x, rmidi, is_console);
  124. return 0;
  125. }
  126. int snd_dg00x_create_midi_devices(struct snd_dg00x *dg00x)
  127. {
  128. int err;
  129. /* Add physical midi ports. */
  130. err = add_substream_pair(dg00x, DOT_MIDI_OUT_PORTS, DOT_MIDI_IN_PORTS,
  131. false);
  132. if (err < 0)
  133. return err;
  134. if (dg00x->is_console)
  135. err = add_substream_pair(dg00x, 1, 1, true);
  136. return err;
  137. }