^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 10/16/2005 Tilman Kranz <tilde@tk-sls.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Creative Audio MIDI, for the CA0106 Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Version: 0.0.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Changelog:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Implementation is based on mpu401 and emu10k1x and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * tested with ca0106.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * mpu401: Copyright (c) by Jaroslav Kysela <perex@perex.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * emu10k1x: Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sound/rawmidi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ca_midi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define ca_midi_write_data(midi, data) midi->write(midi, data, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define ca_midi_write_cmd(midi, data) midi->write(midi, data, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define ca_midi_read_data(midi) midi->read(midi, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define ca_midi_read_stat(midi) midi->read(midi, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define ca_midi_input_avail(midi) (!(ca_midi_read_stat(midi) & midi->input_avail))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define ca_midi_output_ready(midi) (!(ca_midi_read_stat(midi) & midi->output_ready))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void ca_midi_clear_rx(struct snd_ca_midi *midi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int timeout = 100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) for (; timeout > 0 && ca_midi_input_avail(midi); timeout--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ca_midi_read_data(midi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifdef CONFIG_SND_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (timeout <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) pr_err("ca_midi_clear_rx: timeout (status = 0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ca_midi_read_stat(midi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #endif
^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) static void ca_midi_interrupt(struct snd_ca_midi *midi, unsigned int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned char byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (midi->rmidi == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) midi->interrupt_disable(midi,midi->tx_enable | midi->rx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) spin_lock(&midi->input_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if ((status & midi->ipr_rx) && ca_midi_input_avail(midi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ca_midi_clear_rx(midi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) byte = ca_midi_read_data(midi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if(midi->substream_input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) snd_rawmidi_receive(midi->substream_input, &byte, 1);
^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) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) spin_unlock(&midi->input_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_lock(&midi->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if ((status & midi->ipr_tx) && ca_midi_output_ready(midi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (midi->substream_output &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ca_midi_write_data(midi, byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) midi->interrupt_disable(midi,midi->tx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) spin_unlock(&midi->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void ca_midi_cmd(struct snd_ca_midi *midi, unsigned char cmd, int ack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int timeout, ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) spin_lock_irqsave(&midi->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ca_midi_write_data(midi, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* ca_midi_clear_rx(midi); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ca_midi_write_cmd(midi, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ok = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) timeout = 10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) while (!ok && timeout-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (ca_midi_input_avail(midi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (ca_midi_read_data(midi) == midi->ack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!ok && ca_midi_read_data(midi) == midi->ack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) spin_unlock_irqrestore(&midi->input_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!ok)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) pr_err("ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) midi->get_dev_id_port(midi->dev_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ca_midi_read_stat(midi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ca_midi_read_data(midi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int ca_midi_input_open(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) spin_lock_irqsave(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) midi->midi_mode |= CA_MIDI_MODE_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) midi->substream_input = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ca_midi_cmd(midi, midi->reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ca_midi_cmd(midi, midi->enter_uart, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int ca_midi_output_open(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_lock_irqsave(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) midi->substream_output = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ca_midi_cmd(midi, midi->reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ca_midi_cmd(midi, midi->enter_uart, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int ca_midi_input_close(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_lock_irqsave(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) midi->interrupt_disable(midi,midi->rx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) midi->substream_input = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ca_midi_cmd(midi, midi->reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int ca_midi_output_close(struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) spin_lock_irqsave(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) midi->interrupt_disable(midi,midi->tx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) midi->substream_output = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ca_midi_cmd(midi, midi->reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) spin_unlock_irqrestore(&midi->open_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static void ca_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) midi->interrupt_enable(midi,midi->rx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) midi->interrupt_disable(midi, midi->rx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void ca_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct snd_ca_midi *midi = substream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (snd_BUG_ON(!midi->dev_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int max = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned char byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) spin_lock_irqsave(&midi->output_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* try to send some amount of bytes here before interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) while (max > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (ca_midi_output_ready(midi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) snd_rawmidi_transmit(substream, &byte, 1) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* no more data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock_irqrestore(&midi->output_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ca_midi_write_data(midi, byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) max--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) spin_unlock_irqrestore(&midi->output_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) midi->interrupt_enable(midi,midi->tx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) midi->interrupt_disable(midi,midi->tx_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static const struct snd_rawmidi_ops ca_midi_output =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .open = ca_midi_output_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) .close = ca_midi_output_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .trigger = ca_midi_output_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static const struct snd_rawmidi_ops ca_midi_input =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .open = ca_midi_input_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .close = ca_midi_input_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .trigger = ca_midi_input_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void ca_midi_free(struct snd_ca_midi *midi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) midi->interrupt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) midi->interrupt_enable = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) midi->interrupt_disable = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) midi->read = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) midi->write = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) midi->get_dev_id_card = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) midi->get_dev_id_port = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) midi->rmidi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void ca_rmidi_free(struct snd_rawmidi *rmidi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ca_midi_free(rmidi->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int ca_midi_init(void *dev_id, struct snd_ca_midi *midi, int device, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct snd_rawmidi *rmidi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) midi->dev_id = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) midi->interrupt = ca_midi_interrupt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) spin_lock_init(&midi->open_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) spin_lock_init(&midi->input_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) spin_lock_init(&midi->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) strcpy(rmidi->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) SNDRV_RAWMIDI_INFO_INPUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) SNDRV_RAWMIDI_INFO_DUPLEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rmidi->private_data = midi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) rmidi->private_free = ca_rmidi_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) midi->rmidi = rmidi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)