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 | |
| 61 | static void handle_midi_tx(struct fw_card *card, struct fw_request *request, |
| 62 | int tcode, int destination, int source, |
| 63 | int generation, unsigned long long offset, |
| 64 | void *data, size_t length, void *callback_data) |
| 65 | { |
| 66 | struct snd_tscm *tscm = callback_data; |
| 67 | u32 *buf = (u32 *)data; |
| 68 | unsigned int messages; |
| 69 | unsigned int i; |
| 70 | unsigned int port; |
| 71 | struct snd_rawmidi_substream *substream; |
| 72 | u8 *b; |
| 73 | int bytes; |
| 74 | |
| 75 | if (offset != tscm->async_handler.offset) |
| 76 | goto end; |
| 77 | |
| 78 | messages = length / 8; |
| 79 | for (i = 0; i < messages; i++) { |
| 80 | b = (u8 *)(buf + i * 2); |
| 81 | |
| 82 | port = b[0] >> 4; |
| 83 | /* TODO: support virtual MIDI ports. */ |
| 84 | if (port > tscm->spec->midi_capture_ports) |
| 85 | goto end; |
| 86 | |
| 87 | /* Assume the message length. */ |
| 88 | bytes = calculate_message_bytes(b[1]); |
| 89 | /* On MIDI data or exclusives. */ |
| 90 | if (bytes <= 0) { |
| 91 | /* Seek the end of exclusives. */ |
| 92 | for (bytes = 1; bytes < 4; bytes++) { |
| 93 | if (b[bytes] == 0xf7) |
| 94 | break; |
| 95 | } |
| 96 | if (bytes == 4) |
| 97 | bytes = 3; |
| 98 | } |
| 99 | |
| 100 | substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]); |
| 101 | if (substream != NULL) |
| 102 | snd_rawmidi_receive(substream, b + 1, bytes); |
| 103 | } |
| 104 | end: |
| 105 | fw_send_response(card, request, RCODE_COMPLETE); |
| 106 | } |
| 107 | |
| 108 | int snd_tscm_transaction_register(struct snd_tscm *tscm) |
| 109 | { |
| 110 | static const struct fw_address_region resp_register_region = { |
| 111 | .start = 0xffffe0000000ull, |
| 112 | .end = 0xffffe000ffffull, |
| 113 | }; |
| 114 | int err; |
| 115 | |
| 116 | /* |
| 117 | * Usually, two quadlets are transferred by one transaction. The first |
| 118 | * quadlet has MIDI messages, the rest includes timestamp. |
| 119 | * Sometimes, 8 set of the data is transferred by a block transaction. |
| 120 | */ |
| 121 | tscm->async_handler.length = 8 * 8; |
| 122 | tscm->async_handler.address_callback = handle_midi_tx; |
| 123 | tscm->async_handler.callback_data = tscm; |
| 124 | |
| 125 | err = fw_core_add_address_handler(&tscm->async_handler, |
| 126 | &resp_register_region); |
| 127 | if (err < 0) |
| 128 | return err; |
| 129 | |
| 130 | err = snd_tscm_transaction_reregister(tscm); |
| 131 | if (err < 0) |
| 132 | fw_core_remove_address_handler(&tscm->async_handler); |
| 133 | |
| 134 | return err; |
| 135 | } |
| 136 | |
| 137 | /* At bus reset, these registers are cleared. */ |
| 138 | int snd_tscm_transaction_reregister(struct snd_tscm *tscm) |
| 139 | { |
| 140 | struct fw_device *device = fw_parent_device(tscm->unit); |
| 141 | __be32 reg; |
| 142 | int err; |
| 143 | |
| 144 | /* Register messaging address. Block transaction is not allowed. */ |
| 145 | reg = cpu_to_be32((device->card->node_id << 16) | |
| 146 | (tscm->async_handler.offset >> 32)); |
| 147 | err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 148 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI, |
| 149 | ®, sizeof(reg), 0); |
| 150 | if (err < 0) |
| 151 | return err; |
| 152 | |
| 153 | reg = cpu_to_be32(tscm->async_handler.offset); |
| 154 | err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 155 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO, |
| 156 | ®, sizeof(reg), 0); |
| 157 | if (err < 0) |
| 158 | return err; |
| 159 | |
| 160 | /* Turn on messaging. */ |
| 161 | reg = cpu_to_be32(0x00000001); |
| 162 | return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 163 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON, |
| 164 | ®, sizeof(reg), 0); |
| 165 | } |
| 166 | |
| 167 | void snd_tscm_transaction_unregister(struct snd_tscm *tscm) |
| 168 | { |
| 169 | __be32 reg; |
| 170 | |
| 171 | /* Turn off messaging. */ |
| 172 | reg = cpu_to_be32(0x00000000); |
| 173 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 174 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON, |
| 175 | ®, sizeof(reg), 0); |
| 176 | |
| 177 | /* Unregister the address. */ |
| 178 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 179 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI, |
| 180 | ®, sizeof(reg), 0); |
| 181 | snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 182 | TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO, |
| 183 | ®, sizeof(reg), 0); |
| 184 | |
| 185 | fw_core_remove_address_handler(&tscm->async_handler); |
| 186 | } |