2 * ff-transaction.c - a part of driver for RME Fireface series
4 * Copyright (c) 2015-2017 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
11 #define SND_FF_REG_MIDI_RX_PORT_0 0x000080180000ull
12 #define SND_FF_REG_MIDI_RX_PORT_1 0x000080190000ull
14 int snd_ff_transaction_get_clock(struct snd_ff *ff, unsigned int *rate,
15 enum snd_ff_clock_src *src)
21 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
22 SND_FF_REG_CLOCK_CONFIG, ®, sizeof(reg), 0);
25 data = le32_to_cpu(reg);
27 /* Calculate sampling rate. */
28 switch ((data >> 1) & 0x03) {
48 /* Calculate source of clock. */
50 *src = SND_FF_CLOCK_SRC_INTERNAL;
52 /* TODO: 0x02, 0x06, 0x07? */
53 switch ((data >> 10) & 0x07) {
55 *src = SND_FF_CLOCK_SRC_ADAT1;
58 *src = SND_FF_CLOCK_SRC_ADAT2;
61 *src = SND_FF_CLOCK_SRC_SPDIF;
64 *src = SND_FF_CLOCK_SRC_WORD;
67 *src = SND_FF_CLOCK_SRC_LTC;
77 static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
80 struct snd_rawmidi_substream *substream =
81 READ_ONCE(ff->rx_midi_substreams[port]);
83 if (rcode_is_permanent_error(rcode)) {
84 ff->rx_midi_error[port] = true;
88 if (rcode != RCODE_COMPLETE) {
89 /* Transfer the message again, immediately. */
90 ff->next_ktime[port] = 0;
91 schedule_work(&ff->rx_midi_work[port]);
95 snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
96 ff->rx_bytes[port] = 0;
98 if (!snd_rawmidi_transmit_empty(substream))
99 schedule_work(&ff->rx_midi_work[port]);
102 static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
103 void *data, size_t length,
107 container_of(callback_data, struct snd_ff, transactions[0]);
108 finish_transmit_midi_msg(ff, 0, rcode);
111 static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
112 void *data, size_t length,
116 container_of(callback_data, struct snd_ff, transactions[1]);
117 finish_transmit_midi_msg(ff, 1, rcode);
120 static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port,
121 unsigned int index, u8 byte)
123 ff->msg_buf[port][index] = cpu_to_le32(byte);
126 static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
128 struct snd_rawmidi_substream *substream =
129 READ_ONCE(ff->rx_midi_substreams[port]);
130 u8 *buf = (u8 *)ff->msg_buf[port];
133 struct fw_device *fw_dev = fw_parent_device(ff->unit);
134 unsigned long long addr;
136 fw_transaction_callback_t callback;
138 if (substream == NULL || snd_rawmidi_transmit_empty(substream))
141 if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
144 /* Do it in next chance. */
145 if (ktime_after(ff->next_ktime[port], ktime_get())) {
146 schedule_work(&ff->rx_midi_work[port]);
150 len = snd_rawmidi_transmit_peek(substream, buf,
151 SND_FF_MAXIMIM_MIDI_QUADS);
155 for (i = len - 1; i >= 0; i--)
156 fill_midi_buf(ff, port, i, buf[i]);
159 addr = SND_FF_REG_MIDI_RX_PORT_0;
160 callback = finish_transmit_midi0_msg;
162 addr = SND_FF_REG_MIDI_RX_PORT_1;
163 callback = finish_transmit_midi1_msg;
166 /* Set interval to next transaction. */
167 ff->next_ktime[port] = ktime_add_ns(ktime_get(),
168 len * 8 * NSEC_PER_SEC / 31250);
169 ff->rx_bytes[port] = len;
172 * In Linux FireWire core, when generation is updated with memory
173 * barrier, node id has already been updated. In this module, After
174 * this smp_rmb(), load/store instructions to memory are completed.
175 * Thus, both of generation and node id are available with recent
176 * values. This is a light-serialization solution to handle bus reset
177 * events on IEEE 1394 bus.
179 generation = fw_dev->generation;
181 fw_send_request(fw_dev->card, &ff->transactions[port],
182 TCODE_WRITE_BLOCK_REQUEST,
183 fw_dev->node_id, generation, fw_dev->max_speed,
184 addr, &ff->msg_buf[port], len * 4,
185 callback, &ff->transactions[port]);
188 static void transmit_midi0_msg(struct work_struct *work)
190 struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
192 transmit_midi_msg(ff, 0);
195 static void transmit_midi1_msg(struct work_struct *work)
197 struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
199 transmit_midi_msg(ff, 1);
202 static void handle_midi_msg(struct fw_card *card, struct fw_request *request,
203 int tcode, int destination, int source,
204 int generation, unsigned long long offset,
205 void *data, size_t length, void *callback_data)
207 struct snd_ff *ff = callback_data;
212 struct snd_rawmidi_substream *substream;
215 fw_send_response(card, request, RCODE_COMPLETE);
217 for (i = 0; i < length / 4; i++) {
218 quad = le32_to_cpu(buf[i]);
220 /* Message in first port. */
222 * This value may represent the index of this unit when the same
223 * units are on the same IEEE 1394 bus. This driver doesn't use
226 index = (quad >> 8) & 0xff;
228 substream = READ_ONCE(ff->tx_midi_substreams[0]);
229 if (substream != NULL) {
231 snd_rawmidi_receive(substream, &byte, 1);
235 /* Message in second port. */
236 index = (quad >> 24) & 0xff;
238 substream = READ_ONCE(ff->tx_midi_substreams[1]);
239 if (substream != NULL) {
240 byte = (quad >> 16) & 0xff;
241 snd_rawmidi_receive(substream, &byte, 1);
247 static int allocate_own_address(struct snd_ff *ff, int i)
249 struct fw_address_region midi_msg_region;
252 ff->async_handler.length = SND_FF_MAXIMIM_MIDI_QUADS * 4;
253 ff->async_handler.address_callback = handle_midi_msg;
254 ff->async_handler.callback_data = ff;
256 midi_msg_region.start = 0x000100000000ull * i;
257 midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
259 err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
261 /* Controllers are allowed to register this region. */
262 if (ff->async_handler.offset & 0x0000ffffffff) {
263 fw_core_remove_address_handler(&ff->async_handler);
272 * The configuration to start asynchronous transactions for MIDI messages is in
273 * 0x'0000'8010'051c. This register includes the other options, thus this driver
274 * doesn't touch it and leaves the decision to userspace. The userspace MUST add
275 * 0x04000000 to write transactions to the register to receive any MIDI
278 * Here, I just describe MIDI-related offsets of the register, in little-endian
281 * Controllers are allowed to register higher 4 bytes of address to receive
282 * the transactions. The register is 0x'0000'8010'03f4. On the other hand, the
283 * controllers are not allowed to register lower 4 bytes of the address. They
284 * are forced to select from 4 options by writing corresponding bits to
287 * The 3rd-6th bits in MSB of this register are used to indicate lower 4 bytes
288 * of address to which the device transferrs the transactions.
289 * - 6th: 0x'....'....'0000'0180
290 * - 5th: 0x'....'....'0000'0100
291 * - 4th: 0x'....'....'0000'0080
292 * - 3rd: 0x'....'....'0000'0000
294 * This driver configure 0x'....'....'0000'0000 for units to receive MIDI
295 * messages. 3rd bit of the register should be configured, however this driver
296 * deligates this task to user space applications due to a restriction that
297 * this register is write-only and the other bits have own effects.
299 * The 1st and 2nd bits in LSB of this register are used to cancel transferring
300 * asynchronous transactions. These two bits have the same effect.
301 * - 1st/2nd: cancel transferring
303 int snd_ff_transaction_reregister(struct snd_ff *ff)
305 struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
310 * Controllers are allowed to register its node ID and upper 2 byte of
311 * local address to listen asynchronous transactions.
313 addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32);
314 reg = cpu_to_le32(addr);
315 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
316 ff->spec->protocol->midi_high_addr_reg,
317 ®, sizeof(reg), 0);
320 int snd_ff_transaction_register(struct snd_ff *ff)
325 * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
326 * be zero due to device specification.
328 for (i = 0; i < 0xffff; i++) {
329 err = allocate_own_address(ff, i);
330 if (err != -EBUSY && err != -EAGAIN)
336 err = snd_ff_transaction_reregister(ff);
340 INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
341 INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
346 void snd_ff_transaction_unregister(struct snd_ff *ff)
350 if (ff->async_handler.callback_data == NULL)
352 ff->async_handler.callback_data = NULL;
354 /* Release higher 4 bytes of address. */
355 reg = cpu_to_le32(0x00000000);
356 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
357 ff->spec->protocol->midi_high_addr_reg,
358 ®, sizeof(reg), 0);
360 fw_core_remove_address_handler(&ff->async_handler);