msnd_midi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/slab.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/errno.h>
  31. #include <sound/core.h>
  32. #include <sound/rawmidi.h>
  33. #include "msnd.h"
  34. #define MSNDMIDI_MODE_BIT_INPUT 0
  35. #define MSNDMIDI_MODE_BIT_OUTPUT 1
  36. #define MSNDMIDI_MODE_BIT_INPUT_TRIGGER 2
  37. #define MSNDMIDI_MODE_BIT_OUTPUT_TRIGGER 3
  38. struct snd_msndmidi {
  39. struct snd_msnd *dev;
  40. unsigned long mode; /* MSNDMIDI_MODE_XXXX */
  41. struct snd_rawmidi_substream *substream_input;
  42. spinlock_t input_lock;
  43. };
  44. /*
  45. * input/output open/close - protected by open_mutex in rawmidi.c
  46. */
  47. static int snd_msndmidi_input_open(struct snd_rawmidi_substream *substream)
  48. {
  49. struct snd_msndmidi *mpu;
  50. snd_printdd("snd_msndmidi_input_open()\n");
  51. mpu = substream->rmidi->private_data;
  52. mpu->substream_input = substream;
  53. snd_msnd_enable_irq(mpu->dev);
  54. snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_START);
  55. set_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
  56. return 0;
  57. }
  58. static int snd_msndmidi_input_close(struct snd_rawmidi_substream *substream)
  59. {
  60. struct snd_msndmidi *mpu;
  61. mpu = substream->rmidi->private_data;
  62. snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_STOP);
  63. clear_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
  64. mpu->substream_input = NULL;
  65. snd_msnd_disable_irq(mpu->dev);
  66. return 0;
  67. }
  68. static void snd_msndmidi_input_drop(struct snd_msndmidi *mpu)
  69. {
  70. u16 tail;
  71. tail = readw(mpu->dev->MIDQ + JQS_wTail);
  72. writew(tail, mpu->dev->MIDQ + JQS_wHead);
  73. }
  74. /*
  75. * trigger input
  76. */
  77. static void snd_msndmidi_input_trigger(struct snd_rawmidi_substream *substream,
  78. int up)
  79. {
  80. unsigned long flags;
  81. struct snd_msndmidi *mpu;
  82. snd_printdd("snd_msndmidi_input_trigger(, %i)\n", up);
  83. mpu = substream->rmidi->private_data;
  84. spin_lock_irqsave(&mpu->input_lock, flags);
  85. if (up) {
  86. if (!test_and_set_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
  87. &mpu->mode))
  88. snd_msndmidi_input_drop(mpu);
  89. } else {
  90. clear_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
  91. }
  92. spin_unlock_irqrestore(&mpu->input_lock, flags);
  93. if (up)
  94. snd_msndmidi_input_read(mpu);
  95. }
  96. void snd_msndmidi_input_read(void *mpuv)
  97. {
  98. unsigned long flags;
  99. struct snd_msndmidi *mpu = mpuv;
  100. void *pwMIDQData = mpu->dev->mappedbase + MIDQ_DATA_BUFF;
  101. spin_lock_irqsave(&mpu->input_lock, flags);
  102. while (readw(mpu->dev->MIDQ + JQS_wTail) !=
  103. readw(mpu->dev->MIDQ + JQS_wHead)) {
  104. u16 wTmp, val;
  105. val = readw(pwMIDQData + 2 * readw(mpu->dev->MIDQ + JQS_wHead));
  106. if (test_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
  107. &mpu->mode))
  108. snd_rawmidi_receive(mpu->substream_input,
  109. (unsigned char *)&val, 1);
  110. wTmp = readw(mpu->dev->MIDQ + JQS_wHead) + 1;
  111. if (wTmp > readw(mpu->dev->MIDQ + JQS_wSize))
  112. writew(0, mpu->dev->MIDQ + JQS_wHead);
  113. else
  114. writew(wTmp, mpu->dev->MIDQ + JQS_wHead);
  115. }
  116. spin_unlock_irqrestore(&mpu->input_lock, flags);
  117. }
  118. EXPORT_SYMBOL(snd_msndmidi_input_read);
  119. static struct snd_rawmidi_ops snd_msndmidi_input = {
  120. .open = snd_msndmidi_input_open,
  121. .close = snd_msndmidi_input_close,
  122. .trigger = snd_msndmidi_input_trigger,
  123. };
  124. static void snd_msndmidi_free(struct snd_rawmidi *rmidi)
  125. {
  126. struct snd_msndmidi *mpu = rmidi->private_data;
  127. kfree(mpu);
  128. }
  129. int snd_msndmidi_new(struct snd_card *card, int device)
  130. {
  131. struct snd_msnd *chip = card->private_data;
  132. struct snd_msndmidi *mpu;
  133. struct snd_rawmidi *rmidi;
  134. int err;
  135. err = snd_rawmidi_new(card, "MSND-MIDI", device, 1, 1, &rmidi);
  136. if (err < 0)
  137. return err;
  138. mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
  139. if (mpu == NULL) {
  140. snd_device_free(card, rmidi);
  141. return -ENOMEM;
  142. }
  143. mpu->dev = chip;
  144. chip->msndmidi_mpu = mpu;
  145. rmidi->private_data = mpu;
  146. rmidi->private_free = snd_msndmidi_free;
  147. spin_lock_init(&mpu->input_lock);
  148. strcpy(rmidi->name, "MSND MIDI");
  149. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  150. &snd_msndmidi_input);
  151. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  152. return 0;
  153. }