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