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 int snd_ff_transaction_get_clock(struct snd_ff *ff, unsigned int *rate,
12 enum snd_ff_clock_src *src)
18 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
19 SND_FF_REG_CLOCK_CONFIG, ®, sizeof(reg), 0);
22 data = le32_to_cpu(reg);
24 /* Calculate sampling rate. */
25 switch ((data >> 1) & 0x03) {
45 /* Calculate source of clock. */
47 *src = SND_FF_CLOCK_SRC_INTERNAL;
49 /* TODO: 0x02, 0x06, 0x07? */
50 switch ((data >> 10) & 0x07) {
52 *src = SND_FF_CLOCK_SRC_ADAT1;
55 *src = SND_FF_CLOCK_SRC_ADAT2;
58 *src = SND_FF_CLOCK_SRC_SPDIF;
61 *src = SND_FF_CLOCK_SRC_WORD;
64 *src = SND_FF_CLOCK_SRC_LTC;
74 static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
77 struct snd_rawmidi_substream *substream =
78 READ_ONCE(ff->rx_midi_substreams[port]);
80 if (rcode_is_permanent_error(rcode)) {
81 ff->rx_midi_error[port] = true;
85 if (rcode != RCODE_COMPLETE) {
86 /* Transfer the message again, immediately. */
87 ff->next_ktime[port] = 0;
88 schedule_work(&ff->rx_midi_work[port]);
92 snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
93 ff->rx_bytes[port] = 0;
95 if (!snd_rawmidi_transmit_empty(substream))
96 schedule_work(&ff->rx_midi_work[port]);
99 static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
100 void *data, size_t length,
104 container_of(callback_data, struct snd_ff, transactions[0]);
105 finish_transmit_midi_msg(ff, 0, rcode);
108 static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
109 void *data, size_t length,
113 container_of(callback_data, struct snd_ff, transactions[1]);
114 finish_transmit_midi_msg(ff, 1, rcode);
117 static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port,
118 unsigned int index, u8 byte)
120 ff->msg_buf[port][index] = cpu_to_le32(byte);
123 static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
125 struct snd_rawmidi_substream *substream =
126 READ_ONCE(ff->rx_midi_substreams[port]);
127 u8 *buf = (u8 *)ff->msg_buf[port];
130 struct fw_device *fw_dev = fw_parent_device(ff->unit);
131 unsigned long long addr;
133 fw_transaction_callback_t callback;
135 if (substream == NULL || snd_rawmidi_transmit_empty(substream))
138 if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
141 /* Do it in next chance. */
142 if (ktime_after(ff->next_ktime[port], ktime_get())) {
143 schedule_work(&ff->rx_midi_work[port]);
147 len = snd_rawmidi_transmit_peek(substream, buf,
148 SND_FF_MAXIMIM_MIDI_QUADS);
152 for (i = len - 1; i >= 0; i--)
153 fill_midi_buf(ff, port, i, buf[i]);
156 addr = ff->spec->protocol->midi_rx_port_0_reg;
157 callback = finish_transmit_midi0_msg;
159 addr = ff->spec->protocol->midi_rx_port_1_reg;
160 callback = finish_transmit_midi1_msg;
163 /* Set interval to next transaction. */
164 ff->next_ktime[port] = ktime_add_ns(ktime_get(),
165 len * 8 * NSEC_PER_SEC / 31250);
166 ff->rx_bytes[port] = len;
169 * In Linux FireWire core, when generation is updated with memory
170 * barrier, node id has already been updated. In this module, After
171 * this smp_rmb(), load/store instructions to memory are completed.
172 * Thus, both of generation and node id are available with recent
173 * values. This is a light-serialization solution to handle bus reset
174 * events on IEEE 1394 bus.
176 generation = fw_dev->generation;
178 fw_send_request(fw_dev->card, &ff->transactions[port],
179 TCODE_WRITE_BLOCK_REQUEST,
180 fw_dev->node_id, generation, fw_dev->max_speed,
181 addr, &ff->msg_buf[port], len * 4,
182 callback, &ff->transactions[port]);
185 static void transmit_midi0_msg(struct work_struct *work)
187 struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
189 transmit_midi_msg(ff, 0);
192 static void transmit_midi1_msg(struct work_struct *work)
194 struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
196 transmit_midi_msg(ff, 1);
199 static void handle_midi_msg(struct fw_card *card, struct fw_request *request,
200 int tcode, int destination, int source,
201 int generation, unsigned long long offset,
202 void *data, size_t length, void *callback_data)
204 struct snd_ff *ff = callback_data;
209 struct snd_rawmidi_substream *substream;
212 fw_send_response(card, request, RCODE_COMPLETE);
214 for (i = 0; i < length / 4; i++) {
215 quad = le32_to_cpu(buf[i]);
217 /* Message in first port. */
219 * This value may represent the index of this unit when the same
220 * units are on the same IEEE 1394 bus. This driver doesn't use
223 index = (quad >> 8) & 0xff;
225 substream = READ_ONCE(ff->tx_midi_substreams[0]);
226 if (substream != NULL) {
228 snd_rawmidi_receive(substream, &byte, 1);
232 /* Message in second port. */
233 index = (quad >> 24) & 0xff;
235 substream = READ_ONCE(ff->tx_midi_substreams[1]);
236 if (substream != NULL) {
237 byte = (quad >> 16) & 0xff;
238 snd_rawmidi_receive(substream, &byte, 1);
244 static int allocate_own_address(struct snd_ff *ff, int i)
246 struct fw_address_region midi_msg_region;
249 ff->async_handler.length = SND_FF_MAXIMIM_MIDI_QUADS * 4;
250 ff->async_handler.address_callback = handle_midi_msg;
251 ff->async_handler.callback_data = ff;
253 midi_msg_region.start = 0x000100000000ull * i;
254 midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
256 err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
258 /* Controllers are allowed to register this region. */
259 if (ff->async_handler.offset & 0x0000ffffffff) {
260 fw_core_remove_address_handler(&ff->async_handler);
269 * The configuration to start asynchronous transactions for MIDI messages is in
270 * 0x'0000'8010'051c. This register includes the other options, thus this driver
271 * doesn't touch it and leaves the decision to userspace. The userspace MUST add
272 * 0x04000000 to write transactions to the register to receive any MIDI
275 * Here, I just describe MIDI-related offsets of the register, in little-endian
278 * Controllers are allowed to register higher 4 bytes of address to receive
279 * the transactions. The register is 0x'0000'8010'03f4. On the other hand, the
280 * controllers are not allowed to register lower 4 bytes of the address. They
281 * are forced to select from 4 options by writing corresponding bits to
284 * The 3rd-6th bits in MSB of this register are used to indicate lower 4 bytes
285 * of address to which the device transferrs the transactions.
286 * - 6th: 0x'....'....'0000'0180
287 * - 5th: 0x'....'....'0000'0100
288 * - 4th: 0x'....'....'0000'0080
289 * - 3rd: 0x'....'....'0000'0000
291 * This driver configure 0x'....'....'0000'0000 for units to receive MIDI
292 * messages. 3rd bit of the register should be configured, however this driver
293 * deligates this task to user space applications due to a restriction that
294 * this register is write-only and the other bits have own effects.
296 * The 1st and 2nd bits in LSB of this register are used to cancel transferring
297 * asynchronous transactions. These two bits have the same effect.
298 * - 1st/2nd: cancel transferring
300 int snd_ff_transaction_reregister(struct snd_ff *ff)
302 struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
307 * Controllers are allowed to register its node ID and upper 2 byte of
308 * local address to listen asynchronous transactions.
310 addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32);
311 reg = cpu_to_le32(addr);
312 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
313 ff->spec->protocol->midi_high_addr_reg,
314 ®, sizeof(reg), 0);
317 int snd_ff_transaction_register(struct snd_ff *ff)
322 * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
323 * be zero due to device specification.
325 for (i = 0; i < 0xffff; i++) {
326 err = allocate_own_address(ff, i);
327 if (err != -EBUSY && err != -EAGAIN)
333 err = snd_ff_transaction_reregister(ff);
337 INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
338 INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
343 void snd_ff_transaction_unregister(struct snd_ff *ff)
347 if (ff->async_handler.callback_data == NULL)
349 ff->async_handler.callback_data = NULL;
351 /* Release higher 4 bytes of address. */
352 reg = cpu_to_le32(0x00000000);
353 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
354 ff->spec->protocol->midi_high_addr_reg,
355 ®, sizeof(reg), 0);
357 fw_core_remove_address_handler(&ff->async_handler);