msnd_midi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Copyright (c) 2009 by Krzysztof Helt
  4. * Routines for control of MPU-401 in UART mode
  5. *
  6. * MPU-401 supports UART mode which is not capable generate transmit
  7. * interrupts thus output is done via polling. Also, if irq < 0, then
  8. * input is done also via polling. Do not expect good performance.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <linux/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/ioport.h>
  29. #include <linux/errno.h>
  30. #include <sound/core.h>
  31. #include <sound/rawmidi.h>
  32. #include "msnd.h"
  33. #define MSNDMIDI_MODE_BIT_INPUT 0
  34. #define MSNDMIDI_MODE_BIT_OUTPUT 1
  35. #define MSNDMIDI_MODE_BIT_INPUT_TRIGGER 2
  36. #define MSNDMIDI_MODE_BIT_OUTPUT_TRIGGER 3
  37. struct snd_msndmidi {
  38. struct snd_msnd *dev;
  39. unsigned long mode; /* MSNDMIDI_MODE_XXXX */
  40. struct snd_rawmidi_substream *substream_input;
  41. spinlock_t input_lock;
  42. };
  43. /*
  44. * input/output open/close - protected by open_mutex in rawmidi.c
  45. */
  46. static int snd_msndmidi_input_open(struct snd_rawmidi_substream *substream)
  47. {
  48. struct snd_msndmidi *mpu;
  49. snd_printdd("snd_msndmidi_input_open()\n");
  50. mpu = substream->rmidi->private_data;
  51. mpu->substream_input = substream;
  52. snd_msnd_enable_irq(mpu->dev);
  53. snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_START);
  54. set_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
  55. return 0;
  56. }
  57. static int snd_msndmidi_input_close(struct snd_rawmidi_substream *substream)
  58. {
  59. struct snd_msndmidi *mpu;
  60. mpu = substream->rmidi->private_data;
  61. snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_STOP);
  62. clear_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
  63. mpu->substream_input = NULL;
  64. snd_msnd_disable_irq(mpu->dev);
  65. return 0;
  66. }
  67. static void snd_msndmidi_input_drop(struct snd_msndmidi *mpu)
  68. {
  69. u16 tail;
  70. tail = readw(mpu->dev->MIDQ + JQS_wTail);
  71. writew(tail, mpu->dev->MIDQ + JQS_wHead);
  72. }
  73. /*
  74. * trigger input
  75. */
  76. static void snd_msndmidi_input_trigger(struct snd_rawmidi_substream *substream,
  77. int up)
  78. {
  79. unsigned long flags;
  80. struct snd_msndmidi *mpu;
  81. snd_printdd("snd_msndmidi_input_trigger(, %i)\n", up);
  82. mpu = substream->rmidi->private_data;
  83. spin_lock_irqsave(&mpu->input_lock, flags);
  84. if (up) {
  85. if (!test_and_set_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
  86. &mpu->mode))
  87. snd_msndmidi_input_drop(mpu);
  88. } else {
  89. clear_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
  90. }
  91. spin_unlock_irqrestore(&mpu->input_lock, flags);
  92. if (up)
  93. snd_msndmidi_input_read(mpu);
  94. }
  95. void snd_msndmidi_input_read(void *mpuv)
  96. {
  97. unsigned long flags;
  98. struct snd_msndmidi *mpu = mpuv;
  99. void *pwMIDQData = mpu->dev->mappedbase + MIDQ_DATA_BUFF;
  100. spin_lock_irqsave(&mpu->input_lock, flags);
  101. while (readw(mpu->dev->MIDQ + JQS_wTail) !=
  102. readw(mpu->dev->MIDQ + JQS_wHead)) {
  103. u16 wTmp, val;
  104. val = readw(pwMIDQData + 2 * readw(mpu->dev->MIDQ + JQS_wHead));
  105. if (test_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
  106. &mpu->mode))
  107. snd_rawmidi_receive(mpu->substream_input,
  108. (unsigned char *)&val, 1);
  109. wTmp = readw(mpu->dev->MIDQ + JQS_wHead) + 1;
  110. if (wTmp > readw(mpu->dev->MIDQ + JQS_wSize))
  111. writew(0, mpu->dev->MIDQ + JQS_wHead);
  112. else
  113. writew(wTmp, mpu->dev->MIDQ + JQS_wHead);
  114. }
  115. spin_unlock_irqrestore(&mpu->input_lock, flags);
  116. }
  117. EXPORT_SYMBOL(snd_msndmidi_input_read);
  118. static struct snd_rawmidi_ops snd_msndmidi_input = {
  119. .open = snd_msndmidi_input_open,
  120. .close = snd_msndmidi_input_close,
  121. .trigger = snd_msndmidi_input_trigger,
  122. };
  123. static void snd_msndmidi_free(struct snd_rawmidi *rmidi)
  124. {
  125. struct snd_msndmidi *mpu = rmidi->private_data;
  126. kfree(mpu);
  127. }
  128. int snd_msndmidi_new(struct snd_card *card, int device)
  129. {
  130. struct snd_msnd *chip = card->private_data;
  131. struct snd_msndmidi *mpu;
  132. struct snd_rawmidi *rmidi;
  133. int err;
  134. err = snd_rawmidi_new(card, "MSND-MIDI", device, 1, 1, &rmidi);
  135. if (err < 0)
  136. return err;
  137. mpu = kcalloc(1, sizeof(*mpu), GFP_KERNEL);
  138. if (mpu == NULL) {
  139. snd_device_free(card, rmidi);
  140. return -ENOMEM;
  141. }
  142. mpu->dev = chip;
  143. chip->msndmidi_mpu = mpu;
  144. rmidi->private_data = mpu;
  145. rmidi->private_free = snd_msndmidi_free;
  146. spin_lock_init(&mpu->input_lock);
  147. strcpy(rmidi->name, "MSND MIDI");
  148. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  149. &snd_msndmidi_input);
  150. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  151. return 0;
  152. }