^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2009 by Krzysztof Helt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Routines for control of MPU-401 in UART mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * MPU-401 supports UART mode which is not capable generate transmit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * interrupts thus output is done via polling. Also, if irq < 0, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * input is done also via polling. Do not expect good performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <sound/rawmidi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "msnd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MSNDMIDI_MODE_BIT_INPUT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define MSNDMIDI_MODE_BIT_OUTPUT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MSNDMIDI_MODE_BIT_INPUT_TRIGGER 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define MSNDMIDI_MODE_BIT_OUTPUT_TRIGGER 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct snd_msndmidi {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct snd_msnd *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned long mode; /* MSNDMIDI_MODE_XXXX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct snd_rawmidi_substream *substream_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spinlock_t input_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * input/output open/close - protected by open_mutex in rawmidi.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int snd_msndmidi_input_open(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct snd_msndmidi *mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) snd_printdd("snd_msndmidi_input_open()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) mpu = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mpu->substream_input = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) snd_msnd_enable_irq(mpu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) set_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int snd_msndmidi_input_close(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct snd_msndmidi *mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mpu = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_STOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) clear_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) mpu->substream_input = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) snd_msnd_disable_irq(mpu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void snd_msndmidi_input_drop(struct snd_msndmidi *mpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u16 tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) tail = readw(mpu->dev->MIDQ + JQS_wTail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) writew(tail, mpu->dev->MIDQ + JQS_wHead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * trigger input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void snd_msndmidi_input_trigger(struct snd_rawmidi_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct snd_msndmidi *mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) snd_printdd("snd_msndmidi_input_trigger(, %i)\n", up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mpu = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) spin_lock_irqsave(&mpu->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!test_and_set_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) &mpu->mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) snd_msndmidi_input_drop(mpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) clear_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) spin_unlock_irqrestore(&mpu->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) snd_msndmidi_input_read(mpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void snd_msndmidi_input_read(void *mpuv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct snd_msndmidi *mpu = mpuv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void __iomem *pwMIDQData = mpu->dev->mappedbase + MIDQ_DATA_BUFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u16 head, tail, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_lock_irqsave(&mpu->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) head = readw(mpu->dev->MIDQ + JQS_wHead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) tail = readw(mpu->dev->MIDQ + JQS_wTail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) size = readw(mpu->dev->MIDQ + JQS_wSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (head > size || tail > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) while (head != tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned char val = readw(pwMIDQData + 2 * head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (test_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) snd_rawmidi_receive(mpu->substream_input, &val, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (++head > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) writew(head, mpu->dev->MIDQ + JQS_wHead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) spin_unlock_irqrestore(&mpu->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL(snd_msndmidi_input_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static const struct snd_rawmidi_ops snd_msndmidi_input = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .open = snd_msndmidi_input_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .close = snd_msndmidi_input_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .trigger = snd_msndmidi_input_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void snd_msndmidi_free(struct snd_rawmidi *rmidi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct snd_msndmidi *mpu = rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) kfree(mpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int snd_msndmidi_new(struct snd_card *card, int device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct snd_msnd *chip = card->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct snd_msndmidi *mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct snd_rawmidi *rmidi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) err = snd_rawmidi_new(card, "MSND-MIDI", device, 1, 1, &rmidi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (mpu == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) snd_device_free(card, rmidi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) mpu->dev = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) chip->msndmidi_mpu = mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rmidi->private_data = mpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) rmidi->private_free = snd_msndmidi_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spin_lock_init(&mpu->input_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) strcpy(rmidi->name, "MSND MIDI");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &snd_msndmidi_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }