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 len
= snd_rawmidi_transmit_peek(substream
, buf
+ 1, 3);
74 /* On exclusive message. */
75 if (tscm
->on_sysex
[port
]) {
76 /* Seek the end of exclusives. */
77 for (i
= 1; i
< 4 || i
< len
; ++i
) {
79 tscm
->on_sysex
[port
] = false;
84 /* At the end of exclusive message, use label 0x07. */
85 if (!tscm
->on_sysex
[port
]) {
87 buf
[0] = (port
<< 4) | 0x07;
88 /* During exclusive message, use label 0x04. */
89 } else if (len
== 3) {
90 buf
[0] = (port
<< 4) | 0x04;
91 /* We need to fill whole 3 bytes. Go to next change. */
96 /* The beginning of exclusives. */
98 /* Transfer it in next chance in another condition. */
99 tscm
->on_sysex
[port
] = true;
102 /* On running-status. */
103 if ((buf
[1] & 0x80) != 0x80)
104 status
= tscm
->running_status
[port
];
108 /* Calculate consume bytes. */
109 consume
= calculate_message_bytes(status
);
113 /* On running-status. */
114 if ((buf
[1] & 0x80) != 0x80) {
117 buf
[1] = tscm
->running_status
[port
];
120 tscm
->running_status
[port
] = buf
[1];
123 /* Confirm length. */
130 buf
[0] = (port
<< 4) | (buf
[1] >> 4);
136 static void handle_midi_tx(struct fw_card
*card
, struct fw_request
*request
,
137 int tcode
, int destination
, int source
,
138 int generation
, unsigned long long offset
,
139 void *data
, size_t length
, void *callback_data
)
141 struct snd_tscm
*tscm
= callback_data
;
142 u32
*buf
= (u32
*)data
;
143 unsigned int messages
;
146 struct snd_rawmidi_substream
*substream
;
150 if (offset
!= tscm
->async_handler
.offset
)
153 messages
= length
/ 8;
154 for (i
= 0; i
< messages
; i
++) {
155 b
= (u8
*)(buf
+ i
* 2);
158 /* TODO: support virtual MIDI ports. */
159 if (port
>= tscm
->spec
->midi_capture_ports
)
162 /* Assume the message length. */
163 bytes
= calculate_message_bytes(b
[1]);
164 /* On MIDI data or exclusives. */
166 /* Seek the end of exclusives. */
167 for (bytes
= 1; bytes
< 4; bytes
++) {
168 if (b
[bytes
] == 0xf7)
175 substream
= ACCESS_ONCE(tscm
->tx_midi_substreams
[port
]);
176 if (substream
!= NULL
)
177 snd_rawmidi_receive(substream
, b
+ 1, bytes
);
180 fw_send_response(card
, request
, RCODE_COMPLETE
);
183 int snd_tscm_transaction_register(struct snd_tscm
*tscm
)
185 static const struct fw_address_region resp_register_region
= {
186 .start
= 0xffffe0000000ull
,
187 .end
= 0xffffe000ffffull
,
193 * Usually, two quadlets are transferred by one transaction. The first
194 * quadlet has MIDI messages, the rest includes timestamp.
195 * Sometimes, 8 set of the data is transferred by a block transaction.
197 tscm
->async_handler
.length
= 8 * 8;
198 tscm
->async_handler
.address_callback
= handle_midi_tx
;
199 tscm
->async_handler
.callback_data
= tscm
;
201 err
= fw_core_add_address_handler(&tscm
->async_handler
,
202 &resp_register_region
);
206 err
= snd_tscm_transaction_reregister(tscm
);
210 for (i
= 0; i
< TSCM_MIDI_OUT_PORT_MAX
; i
++) {
211 err
= snd_fw_async_midi_port_init(
212 &tscm
->out_ports
[i
], tscm
->unit
,
213 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_RX_QUAD
,
221 fw_core_remove_address_handler(&tscm
->async_handler
);
225 /* At bus reset, these registers are cleared. */
226 int snd_tscm_transaction_reregister(struct snd_tscm
*tscm
)
228 struct fw_device
*device
= fw_parent_device(tscm
->unit
);
232 /* Register messaging address. Block transaction is not allowed. */
233 reg
= cpu_to_be32((device
->card
->node_id
<< 16) |
234 (tscm
->async_handler
.offset
>> 32));
235 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
236 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ADDR_HI
,
237 ®
, sizeof(reg
), 0);
241 reg
= cpu_to_be32(tscm
->async_handler
.offset
);
242 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
243 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ADDR_LO
,
244 ®
, sizeof(reg
), 0);
248 /* Turn on messaging. */
249 reg
= cpu_to_be32(0x00000001);
250 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
251 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ON
,
252 ®
, sizeof(reg
), 0);
256 /* Turn on FireWire LED. */
257 reg
= cpu_to_be32(0x0001008e);
258 return snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
259 TSCM_ADDR_BASE
+ TSCM_OFFSET_LED_POWER
,
260 ®
, sizeof(reg
), 0);
263 void snd_tscm_transaction_unregister(struct snd_tscm
*tscm
)
268 /* Turn off FireWire LED. */
269 reg
= cpu_to_be32(0x0000008e);
270 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
271 TSCM_ADDR_BASE
+ TSCM_OFFSET_LED_POWER
,
272 ®
, sizeof(reg
), 0);
274 /* Turn off messaging. */
275 reg
= cpu_to_be32(0x00000000);
276 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
277 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ON
,
278 ®
, sizeof(reg
), 0);
280 /* Unregister the address. */
281 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
282 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ADDR_HI
,
283 ®
, sizeof(reg
), 0);
284 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
285 TSCM_ADDR_BASE
+ TSCM_OFFSET_MIDI_TX_ADDR_LO
,
286 ®
, sizeof(reg
), 0);
288 fw_core_remove_address_handler(&tscm
->async_handler
);
289 for (i
= 0; i
< TSCM_MIDI_OUT_PORT_MAX
; i
++)
290 snd_fw_async_midi_port_destroy(&tscm
->out_ports
[i
]);