1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2009 by Krzysztof Helt
5 * Routines for control of MPU-401 in UART mode
7 * MPU-401 supports UART mode which is not capable generate transmit
8 * interrupts thus output is done via polling. Also, if irq < 0, then
9 * input is done also via polling. Do not expect good performance.
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/ioport.h>
16 #include <linux/errno.h>
17 #include <linux/export.h>
18 #include <sound/core.h>
19 #include <sound/rawmidi.h>
23 #define MSNDMIDI_MODE_BIT_INPUT 0
24 #define MSNDMIDI_MODE_BIT_OUTPUT 1
25 #define MSNDMIDI_MODE_BIT_INPUT_TRIGGER 2
26 #define MSNDMIDI_MODE_BIT_OUTPUT_TRIGGER 3
31 unsigned long mode; /* MSNDMIDI_MODE_XXXX */
33 struct snd_rawmidi_substream *substream_input;
35 spinlock_t input_lock;
39 * input/output open/close - protected by open_mutex in rawmidi.c
41 static int snd_msndmidi_input_open(struct snd_rawmidi_substream *substream)
43 struct snd_msndmidi *mpu;
45 snd_printdd("snd_msndmidi_input_open()\n");
47 mpu = substream->rmidi->private_data;
49 mpu->substream_input = substream;
51 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);
58 static int snd_msndmidi_input_close(struct snd_rawmidi_substream *substream)
60 struct snd_msndmidi *mpu;
62 mpu = substream->rmidi->private_data;
63 snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_STOP);
64 clear_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode);
65 mpu->substream_input = NULL;
66 snd_msnd_disable_irq(mpu->dev);
70 static void snd_msndmidi_input_drop(struct snd_msndmidi *mpu)
74 tail = readw(mpu->dev->MIDQ + JQS_wTail);
75 writew(tail, mpu->dev->MIDQ + JQS_wHead);
81 static void snd_msndmidi_input_trigger(struct snd_rawmidi_substream *substream,
85 struct snd_msndmidi *mpu;
87 snd_printdd("snd_msndmidi_input_trigger(, %i)\n", up);
89 mpu = substream->rmidi->private_data;
90 spin_lock_irqsave(&mpu->input_lock, flags);
92 if (!test_and_set_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER,
94 snd_msndmidi_input_drop(mpu);
96 clear_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
98 spin_unlock_irqrestore(&mpu->input_lock, flags);
100 snd_msndmidi_input_read(mpu);
103 void snd_msndmidi_input_read(void *mpuv)
106 struct snd_msndmidi *mpu = mpuv;
107 void __iomem *pwMIDQData = mpu->dev->mappedbase + MIDQ_DATA_BUFF;
108 u16 head, tail, size;
110 spin_lock_irqsave(&mpu->input_lock, flags);
111 head = readw(mpu->dev->MIDQ + JQS_wHead);
112 tail = readw(mpu->dev->MIDQ + JQS_wTail);
113 size = readw(mpu->dev->MIDQ + JQS_wSize);
114 if (head > size || tail > size)
116 while (head != tail) {
117 unsigned char val = readw(pwMIDQData + 2 * head);
119 if (test_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
120 snd_rawmidi_receive(mpu->substream_input, &val, 1);
123 writew(head, mpu->dev->MIDQ + JQS_wHead);
126 spin_unlock_irqrestore(&mpu->input_lock, flags);
128 EXPORT_SYMBOL(snd_msndmidi_input_read);
130 static const struct snd_rawmidi_ops snd_msndmidi_input = {
131 .open = snd_msndmidi_input_open,
132 .close = snd_msndmidi_input_close,
133 .trigger = snd_msndmidi_input_trigger,
136 static void snd_msndmidi_free(struct snd_rawmidi *rmidi)
138 struct snd_msndmidi *mpu = rmidi->private_data;
142 int snd_msndmidi_new(struct snd_card *card, int device)
144 struct snd_msnd *chip = card->private_data;
145 struct snd_msndmidi *mpu;
146 struct snd_rawmidi *rmidi;
149 err = snd_rawmidi_new(card, "MSND-MIDI", device, 1, 1, &rmidi);
152 mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
154 snd_device_free(card, rmidi);
158 chip->msndmidi_mpu = mpu;
159 rmidi->private_data = mpu;
160 rmidi->private_free = snd_msndmidi_free;
161 spin_lock_init(&mpu->input_lock);
162 strcpy(rmidi->name, "MSND MIDI");
163 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
164 &snd_msndmidi_input);
165 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;