2 * tascam-transaction.c - a part of driver for TASCAM FireWire series
4 * Copyright (c) 2015 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
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.
16 static inline int calculate_message_bytes(u8 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. */
27 case 0xf1: /* MIDI time code quarter frame. */
28 case 0xf3: /* Song select. */
30 case 0xf2: /* Song position pointer. */
32 case 0xf0: /* Exclusive. */
34 case 0xf7: /* End of exclusive. */
36 case 0xf4: /* Undefined. */
37 case 0xf5: /* Undefined. */
38 case 0xf9: /* Undefined. */
39 case 0xfd: /* Undefined. */
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. */
49 case 0xc0: /* Program change. */
50 case 0xd0: /* Channel pressure. */
61 static int fill_message(struct snd_rawmidi_substream *substream, u8 *buf)
63 struct snd_tscm *tscm = substream->rmidi->private_data;
64 unsigned int port = substream->number;
70 buf[0] = buf[1] = buf[2] = buf[3] = 0x00;
72 len = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
76 /* On exclusive message. */
77 if (tscm->on_sysex[port]) {
78 /* Seek the end of exclusives. */
79 for (i = 1; i < 4 || i < len; ++i) {
81 tscm->on_sysex[port] = false;
86 /* At the end of exclusive message, use label 0x07. */
87 if (!tscm->on_sysex[port]) {
89 buf[0] = (port << 4) | 0x07;
90 /* During exclusive message, use label 0x04. */
91 } else if (len == 3) {
92 buf[0] = (port << 4) | 0x04;
93 /* We need to fill whole 3 bytes. Go to next change. */
98 /* The beginning of exclusives. */
100 /* Transfer it in next chance in another condition. */
101 tscm->on_sysex[port] = true;
104 /* On running-status. */
105 if ((buf[1] & 0x80) != 0x80)
106 status = tscm->running_status[port];
110 /* Calculate consume bytes. */
111 consume = calculate_message_bytes(status);
115 /* On running-status. */
116 if ((buf[1] & 0x80) != 0x80) {
119 buf[1] = tscm->running_status[port];
122 tscm->running_status[port] = buf[1];
125 /* Confirm length. */
132 buf[0] = (port << 4) | (buf[1] >> 4);
138 static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
139 int tcode, int destination, int source,
140 int generation, unsigned long long offset,
141 void *data, size_t length, void *callback_data)
143 struct snd_tscm *tscm = callback_data;
144 u32 *buf = (u32 *)data;
145 unsigned int messages;
148 struct snd_rawmidi_substream *substream;
152 if (offset != tscm->async_handler.offset)
155 messages = length / 8;
156 for (i = 0; i < messages; i++) {
157 b = (u8 *)(buf + i * 2);
160 /* TODO: support virtual MIDI ports. */
161 if (port > tscm->spec->midi_capture_ports)
164 /* Assume the message length. */
165 bytes = calculate_message_bytes(b[1]);
166 /* On MIDI data or exclusives. */
168 /* Seek the end of exclusives. */
169 for (bytes = 1; bytes < 4; bytes++) {
170 if (b[bytes] == 0xf7)
177 substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
178 if (substream != NULL)
179 snd_rawmidi_receive(substream, b + 1, bytes);
182 fw_send_response(card, request, RCODE_COMPLETE);
185 int snd_tscm_transaction_register(struct snd_tscm *tscm)
187 static const struct fw_address_region resp_register_region = {
188 .start = 0xffffe0000000ull,
189 .end = 0xffffe000ffffull,
195 * Usually, two quadlets are transferred by one transaction. The first
196 * quadlet has MIDI messages, the rest includes timestamp.
197 * Sometimes, 8 set of the data is transferred by a block transaction.
199 tscm->async_handler.length = 8 * 8;
200 tscm->async_handler.address_callback = handle_midi_tx;
201 tscm->async_handler.callback_data = tscm;
203 err = fw_core_add_address_handler(&tscm->async_handler,
204 &resp_register_region);
208 err = snd_tscm_transaction_reregister(tscm);
212 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
213 err = snd_fw_async_midi_port_init(
214 &tscm->out_ports[i], tscm->unit,
215 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,
223 fw_core_remove_address_handler(&tscm->async_handler);
227 /* At bus reset, these registers are cleared. */
228 int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
230 struct fw_device *device = fw_parent_device(tscm->unit);
234 /* Register messaging address. Block transaction is not allowed. */
235 reg = cpu_to_be32((device->card->node_id << 16) |
236 (tscm->async_handler.offset >> 32));
237 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
238 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
239 ®, sizeof(reg), 0);
243 reg = cpu_to_be32(tscm->async_handler.offset);
244 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
245 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
246 ®, sizeof(reg), 0);
250 /* Turn on messaging. */
251 reg = cpu_to_be32(0x00000001);
252 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
253 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
254 ®, sizeof(reg), 0);
258 /* Turn on FireWire LED. */
259 reg = cpu_to_be32(0x0001008e);
260 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
261 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
262 ®, sizeof(reg), 0);
265 void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
270 /* Turn off FireWire LED. */
271 reg = cpu_to_be32(0x0000008e);
272 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
273 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
274 ®, sizeof(reg), 0);
276 /* Turn off messaging. */
277 reg = cpu_to_be32(0x00000000);
278 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
279 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
280 ®, sizeof(reg), 0);
282 /* Unregister the address. */
283 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
284 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
285 ®, sizeof(reg), 0);
286 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
287 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
288 ®, sizeof(reg), 0);
290 fw_core_remove_address_handler(&tscm->async_handler);
291 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
292 snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);