Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * tascam-transaction.c - a part of driver for TASCAM FireWire series |
| 3 | * |
| 4 | * Copyright (c) 2015 Takashi Sakamoto |
| 5 | * |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | |
| 9 | #include "tascam.h" |
| 10 | |
| 11 | /* |
| 12 | * When return minus value, given argument is not MIDI status. |
| 13 | * When return 0, given argument is a beginning of system exclusive. |
| 14 | * When return the others, given argument is MIDI data. |
| 15 | */ |
| 16 | static inline int calculate_message_bytes(u8 status) |
| 17 | { |
| 18 | switch (status) { |
| 19 | case 0xf6: /* Tune request. */ |
| 20 | case 0xf8: /* Timing clock. */ |
| 21 | case 0xfa: /* Start. */ |
| 22 | case 0xfb: /* Continue. */ |
| 23 | case 0xfc: /* Stop. */ |
| 24 | case 0xfe: /* Active sensing. */ |
| 25 | case 0xff: /* System reset. */ |
| 26 | return 1; |
| 27 | case 0xf1: /* MIDI time code quarter frame. */ |
| 28 | case 0xf3: /* Song select. */ |
| 29 | return 2; |
| 30 | case 0xf2: /* Song position pointer. */ |
| 31 | return 3; |
| 32 | case 0xf0: /* Exclusive. */ |
| 33 | return 0; |
| 34 | case 0xf7: /* End of exclusive. */ |
| 35 | break; |
| 36 | case 0xf4: /* Undefined. */ |
| 37 | case 0xf5: /* Undefined. */ |
| 38 | case 0xf9: /* Undefined. */ |
| 39 | case 0xfd: /* Undefined. */ |
| 40 | break; |
| 41 | default: |
| 42 | switch (status & 0xf0) { |
| 43 | case 0x80: /* Note on. */ |
| 44 | case 0x90: /* Note off. */ |
| 45 | case 0xa0: /* Polyphonic key pressure. */ |
| 46 | case 0xb0: /* Control change and Mode change. */ |
| 47 | case 0xe0: /* Pitch bend change. */ |
| 48 | return 3; |
| 49 | case 0xc0: /* Program change. */ |
| 50 | case 0xd0: /* Channel pressure. */ |
| 51 | return 2; |
| 52 | default: |
| 53 | break; |
| 54 | } |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 61 | static int fill_message(struct snd_rawmidi_substream *substream, u8 *buf) |
| 62 | { |
| 63 | struct snd_tscm *tscm = substream->rmidi->private_data; |
| 64 | unsigned int port = substream->number; |
Takashi Sakamoto | 516a306 | 2015-10-20 23:46:56 +0900 | [diff] [blame] | 65 | int i, len, consume; |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 66 | u8 *label, *msg; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 67 | u8 status; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 68 | |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 69 | /* The first byte is used for label, the rest for MIDI bytes. */ |
| 70 | label = buf; |
| 71 | msg = buf + 1; |
| 72 | |
| 73 | consume = snd_rawmidi_transmit_peek(substream, msg, 3); |
Takashi Sakamoto | b7ab614 | 2015-10-20 23:46:57 +0900 | [diff] [blame] | 74 | if (consume == 0) |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 75 | return 0; |
| 76 | |
| 77 | /* On exclusive message. */ |
| 78 | if (tscm->on_sysex[port]) { |
| 79 | /* Seek the end of exclusives. */ |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 80 | for (i = 0; i < consume; ++i) { |
| 81 | if (msg[i] == 0xf7) { |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 82 | tscm->on_sysex[port] = false; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* At the end of exclusive message, use label 0x07. */ |
| 88 | if (!tscm->on_sysex[port]) { |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 89 | consume = i + 1; |
| 90 | *label = (port << 4) | 0x07; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 91 | /* During exclusive message, use label 0x04. */ |
Takashi Sakamoto | b7ab614 | 2015-10-20 23:46:57 +0900 | [diff] [blame] | 92 | } else if (consume == 3) { |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 93 | *label = (port << 4) | 0x04; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 94 | /* We need to fill whole 3 bytes. Go to next change. */ |
| 95 | } else { |
Takashi Sakamoto | f937b43 | 2015-10-20 23:46:59 +0900 | [diff] [blame] | 96 | return 0; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 97 | } |
Takashi Sakamoto | f937b43 | 2015-10-20 23:46:59 +0900 | [diff] [blame] | 98 | |
| 99 | len = consume; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 100 | } else { |
| 101 | /* The beginning of exclusives. */ |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 102 | if (msg[0] == 0xf0) { |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 103 | /* Transfer it in next chance in another condition. */ |
| 104 | tscm->on_sysex[port] = true; |
| 105 | return 0; |
| 106 | } else { |
| 107 | /* On running-status. */ |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 108 | if ((msg[0] & 0x80) != 0x80) |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 109 | status = tscm->running_status[port]; |
| 110 | else |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 111 | status = msg[0]; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 112 | |
| 113 | /* Calculate consume bytes. */ |
Takashi Sakamoto | b7ab614 | 2015-10-20 23:46:57 +0900 | [diff] [blame] | 114 | len = calculate_message_bytes(status); |
| 115 | if (len <= 0) |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 116 | return 0; |
| 117 | |
| 118 | /* On running-status. */ |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 119 | if ((msg[0] & 0x80) != 0x80) { |
Takashi Sakamoto | f937b43 | 2015-10-20 23:46:59 +0900 | [diff] [blame] | 120 | /* Enough MIDI bytes were not retrieved. */ |
| 121 | if (consume < len - 1) |
| 122 | return 0; |
| 123 | consume = len - 1; |
| 124 | |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 125 | msg[2] = msg[1]; |
| 126 | msg[1] = msg[0]; |
| 127 | msg[0] = tscm->running_status[port]; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 128 | } else { |
Takashi Sakamoto | f937b43 | 2015-10-20 23:46:59 +0900 | [diff] [blame] | 129 | /* Enough MIDI bytes were not retrieved. */ |
| 130 | if (consume < len) |
| 131 | return 0; |
| 132 | consume = len; |
| 133 | |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 134 | tscm->running_status[port] = msg[0]; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 135 | } |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Takashi Sakamoto | 123990e | 2015-10-20 23:46:58 +0900 | [diff] [blame] | 138 | *label = (port << 4) | (msg[0] >> 4); |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Takashi Sakamoto | f937b43 | 2015-10-20 23:46:59 +0900 | [diff] [blame] | 141 | if (len > 0 && len < 3) |
| 142 | memset(msg + len, 0, 3 - len); |
| 143 | |
Takashi Sakamoto | b7ab614 | 2015-10-20 23:46:57 +0900 | [diff] [blame] | 144 | return consume; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 147 | static void handle_midi_tx(struct fw_card *card, struct fw_request *request, |
| 148 | int tcode, int destination, int source, |
| 149 | int generation, unsigned long long offset, |
| 150 | void *data, size_t length, void *callback_data) |
| 151 | { |
| 152 | struct snd_tscm *tscm = callback_data; |
| 153 | u32 *buf = (u32 *)data; |
| 154 | unsigned int messages; |
| 155 | unsigned int i; |
| 156 | unsigned int port; |
| 157 | struct snd_rawmidi_substream *substream; |
| 158 | u8 *b; |
| 159 | int bytes; |
| 160 | |
| 161 | if (offset != tscm->async_handler.offset) |
| 162 | goto end; |
| 163 | |
| 164 | messages = length / 8; |
| 165 | for (i = 0; i < messages; i++) { |
| 166 | b = (u8 *)(buf + i * 2); |
| 167 | |
| 168 | port = b[0] >> 4; |
| 169 | /* TODO: support virtual MIDI ports. */ |
Dan Carpenter | 72409705 | 2015-10-15 21:17:11 +0300 | [diff] [blame] | 170 | if (port >= tscm->spec->midi_capture_ports) |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 171 | goto end; |
| 172 | |
| 173 | /* Assume the message length. */ |
| 174 | bytes = calculate_message_bytes(b[1]); |
| 175 | /* On MIDI data or exclusives. */ |
| 176 | if (bytes <= 0) { |
| 177 | /* Seek the end of exclusives. */ |
| 178 | for (bytes = 1; bytes < 4; bytes++) { |
| 179 | if (b[bytes] == 0xf7) |
| 180 | break; |
| 181 | } |
| 182 | if (bytes == 4) |
| 183 | bytes = 3; |
| 184 | } |
| 185 | |
| 186 | substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]); |
| 187 | if (substream != NULL) |
| 188 | snd_rawmidi_receive(substream, b + 1, bytes); |
| 189 | } |
| 190 | end: |
| 191 | fw_send_response(card, request, RCODE_COMPLETE); |
| 192 | } |
| 193 | |
| 194 | int snd_tscm_transaction_register(struct snd_tscm *tscm) |
| 195 | { |
| 196 | static const struct fw_address_region resp_register_region = { |
| 197 | .start = 0xffffe0000000ull, |
| 198 | .end = 0xffffe000ffffull, |
| 199 | }; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 200 | unsigned int i; |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 201 | int err; |
| 202 | |
| 203 | /* |
| 204 | * Usually, two quadlets are transferred by one transaction. The first |
| 205 | * quadlet has MIDI messages, the rest includes timestamp. |
| 206 | * Sometimes, 8 set of the data is transferred by a block transaction. |
| 207 | */ |
| 208 | tscm->async_handler.length = 8 * 8; |
| 209 | tscm->async_handler.address_callback = handle_midi_tx; |
| 210 | tscm->async_handler.callback_data = tscm; |
| 211 | |
| 212 | err = fw_core_add_address_handler(&tscm->async_handler, |
| 213 | &resp_register_region); |
| 214 | if (err < 0) |
| 215 | return err; |
| 216 | |
| 217 | err = snd_tscm_transaction_reregister(tscm); |
| 218 | if (err < 0) |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 219 | goto error; |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 220 | |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 221 | for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) { |
| 222 | err = snd_fw_async_midi_port_init( |
| 223 | &tscm->out_ports[i], tscm->unit, |
| 224 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD, |
| 225 | 4, fill_message); |
| 226 | if (err < 0) |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | return err; |
| 231 | error: |
| 232 | fw_core_remove_address_handler(&tscm->async_handler); |
Takashi Sakamoto | 5d2560a | 2016-02-05 09:56:05 +0900 | [diff] [blame] | 233 | tscm->async_handler.callback_data = NULL; |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 234 | return err; |
| 235 | } |
| 236 | |
| 237 | /* At bus reset, these registers are cleared. */ |
| 238 | int snd_tscm_transaction_reregister(struct snd_tscm *tscm) |
| 239 | { |
| 240 | struct fw_device *device = fw_parent_device(tscm->unit); |
| 241 | __be32 reg; |
| 242 | int err; |
| 243 | |
| 244 | /* Register messaging address. Block transaction is not allowed. */ |
| 245 | reg = cpu_to_be32((device->card->node_id << 16) | |
| 246 | (tscm->async_handler.offset >> 32)); |
| 247 | err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 248 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI, |
| 249 | ®, sizeof(reg), 0); |
| 250 | if (err < 0) |
| 251 | return err; |
| 252 | |
| 253 | reg = cpu_to_be32(tscm->async_handler.offset); |
| 254 | err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 255 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO, |
| 256 | ®, sizeof(reg), 0); |
| 257 | if (err < 0) |
| 258 | return err; |
| 259 | |
| 260 | /* Turn on messaging. */ |
| 261 | reg = cpu_to_be32(0x00000001); |
Dan Carpenter | 69ec98d | 2015-10-15 21:16:30 +0300 | [diff] [blame] | 262 | err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 263 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON, |
| 264 | ®, sizeof(reg), 0); |
Takashi Sakamoto | e65e2cb | 2015-10-12 19:10:24 +0900 | [diff] [blame] | 265 | if (err < 0) |
| 266 | return err; |
| 267 | |
| 268 | /* Turn on FireWire LED. */ |
| 269 | reg = cpu_to_be32(0x0001008e); |
| 270 | return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 271 | TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER, |
| 272 | ®, sizeof(reg), 0); |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void snd_tscm_transaction_unregister(struct snd_tscm *tscm) |
| 276 | { |
| 277 | __be32 reg; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 278 | unsigned int i; |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 279 | |
Takashi Sakamoto | 5d2560a | 2016-02-05 09:56:05 +0900 | [diff] [blame] | 280 | if (tscm->async_handler.callback_data == NULL) |
| 281 | return; |
| 282 | |
Takashi Sakamoto | e65e2cb | 2015-10-12 19:10:24 +0900 | [diff] [blame] | 283 | /* Turn off FireWire LED. */ |
| 284 | reg = cpu_to_be32(0x0000008e); |
| 285 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 286 | TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER, |
| 287 | ®, sizeof(reg), 0); |
| 288 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 289 | /* Turn off messaging. */ |
| 290 | reg = cpu_to_be32(0x00000000); |
| 291 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 292 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON, |
| 293 | ®, sizeof(reg), 0); |
| 294 | |
| 295 | /* Unregister the address. */ |
| 296 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 297 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI, |
| 298 | ®, sizeof(reg), 0); |
| 299 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 300 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO, |
| 301 | ®, sizeof(reg), 0); |
| 302 | |
| 303 | fw_core_remove_address_handler(&tscm->async_handler); |
Takashi Sakamoto | 5d2560a | 2016-02-05 09:56:05 +0900 | [diff] [blame] | 304 | tscm->async_handler.callback_data = NULL; |
| 305 | |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 306 | for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) |
| 307 | snd_fw_async_midi_port_destroy(&tscm->out_ports[i]); |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 308 | } |