dice-midi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * dice_midi.c - a part of driver for Dice 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 "dice.h"
  9. static int midi_open(struct snd_rawmidi_substream *substream)
  10. {
  11. struct snd_dice *dice = substream->rmidi->private_data;
  12. int err;
  13. err = snd_dice_stream_lock_try(dice);
  14. if (err < 0)
  15. return err;
  16. mutex_lock(&dice->mutex);
  17. dice->substreams_counter++;
  18. err = snd_dice_stream_start_duplex(dice, 0);
  19. mutex_unlock(&dice->mutex);
  20. if (err < 0)
  21. snd_dice_stream_lock_release(dice);
  22. return err;
  23. }
  24. static int midi_close(struct snd_rawmidi_substream *substream)
  25. {
  26. struct snd_dice *dice = substream->rmidi->private_data;
  27. mutex_lock(&dice->mutex);
  28. dice->substreams_counter--;
  29. snd_dice_stream_stop_duplex(dice);
  30. mutex_unlock(&dice->mutex);
  31. snd_dice_stream_lock_release(dice);
  32. return 0;
  33. }
  34. static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
  35. {
  36. struct snd_dice *dice = substrm->rmidi->private_data;
  37. unsigned long flags;
  38. spin_lock_irqsave(&dice->lock, flags);
  39. if (up)
  40. amdtp_stream_midi_trigger(&dice->tx_stream,
  41. substrm->number, substrm);
  42. else
  43. amdtp_stream_midi_trigger(&dice->tx_stream,
  44. substrm->number, NULL);
  45. spin_unlock_irqrestore(&dice->lock, flags);
  46. }
  47. static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
  48. {
  49. struct snd_dice *dice = substrm->rmidi->private_data;
  50. unsigned long flags;
  51. spin_lock_irqsave(&dice->lock, flags);
  52. if (up)
  53. amdtp_stream_midi_trigger(&dice->rx_stream,
  54. substrm->number, substrm);
  55. else
  56. amdtp_stream_midi_trigger(&dice->rx_stream,
  57. substrm->number, NULL);
  58. spin_unlock_irqrestore(&dice->lock, flags);
  59. }
  60. static struct snd_rawmidi_ops capture_ops = {
  61. .open = midi_open,
  62. .close = midi_close,
  63. .trigger = midi_capture_trigger,
  64. };
  65. static struct snd_rawmidi_ops playback_ops = {
  66. .open = midi_open,
  67. .close = midi_close,
  68. .trigger = midi_playback_trigger,
  69. };
  70. static void set_midi_substream_names(struct snd_dice *dice,
  71. struct snd_rawmidi_str *str)
  72. {
  73. struct snd_rawmidi_substream *subs;
  74. list_for_each_entry(subs, &str->substreams, list) {
  75. snprintf(subs->name, sizeof(subs->name),
  76. "%s MIDI %d", dice->card->shortname, subs->number + 1);
  77. }
  78. }
  79. int snd_dice_create_midi(struct snd_dice *dice)
  80. {
  81. struct snd_rawmidi *rmidi;
  82. struct snd_rawmidi_str *str;
  83. unsigned int i, midi_in_ports, midi_out_ports;
  84. int err;
  85. midi_in_ports = midi_out_ports = 0;
  86. for (i = 0; i < 3; i++) {
  87. midi_in_ports = max(dice->tx_midi_ports[i], midi_in_ports);
  88. midi_out_ports = max(dice->rx_midi_ports[i], midi_out_ports);
  89. }
  90. if (midi_in_ports + midi_out_ports == 0)
  91. return 0;
  92. /* create midi ports */
  93. err = snd_rawmidi_new(dice->card, dice->card->driver, 0,
  94. midi_out_ports, midi_in_ports,
  95. &rmidi);
  96. if (err < 0)
  97. return err;
  98. snprintf(rmidi->name, sizeof(rmidi->name),
  99. "%s MIDI", dice->card->shortname);
  100. rmidi->private_data = dice;
  101. if (midi_in_ports > 0) {
  102. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  103. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  104. &capture_ops);
  105. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
  106. set_midi_substream_names(dice, str);
  107. }
  108. if (midi_out_ports > 0) {
  109. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  110. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  111. &playback_ops);
  112. str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  113. set_midi_substream_names(dice, str);
  114. }
  115. if ((midi_out_ports > 0) && (midi_in_ports > 0))
  116. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  117. return 0;
  118. }