bebob_midi.c 4.0 KB

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