1 /*
2  * ff-transaction.c - a part of driver for RME Fireface series
3  *
4  * Copyright (c) 2015-2017 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "ff.h"
10 
11 #define SND_FF_REG_MIDI_RX_PORT_0	0x000080180000ull
12 #define SND_FF_REG_MIDI_RX_PORT_1	0x000080190000ull
13 
14 int snd_ff_transaction_get_clock(struct snd_ff *ff, unsigned int *rate,
15 				 enum snd_ff_clock_src *src)
16 {
17 	__le32 reg;
18 	u32 data;
19 	int err;
20 
21 	err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
22 				 SND_FF_REG_CLOCK_CONFIG, &reg, sizeof(reg), 0);
23 	if (err < 0)
24 		return err;
25 	data = le32_to_cpu(reg);
26 
27 	/* Calculate sampling rate. */
28 	switch ((data >> 1) & 0x03) {
29 	case 0x01:
30 		*rate = 32000;
31 		break;
32 	case 0x00:
33 		*rate = 44100;
34 		break;
35 	case 0x03:
36 		*rate = 48000;
37 		break;
38 	case 0x02:
39 	default:
40 		return -EIO;
41 	}
42 
43 	if (data & 0x08)
44 		*rate *= 2;
45 	else if (data & 0x10)
46 		*rate *= 4;
47 
48 	/* Calculate source of clock. */
49 	if (data & 0x01) {
50 		*src = SND_FF_CLOCK_SRC_INTERNAL;
51 	} else {
52 		/* TODO: 0x02, 0x06, 0x07? */
53 		switch ((data >> 10) & 0x07) {
54 		case 0x00:
55 			*src = SND_FF_CLOCK_SRC_ADAT1;
56 			break;
57 		case 0x01:
58 			*src = SND_FF_CLOCK_SRC_ADAT2;
59 			break;
60 		case 0x03:
61 			*src = SND_FF_CLOCK_SRC_SPDIF;
62 			break;
63 		case 0x04:
64 			*src = SND_FF_CLOCK_SRC_WORD;
65 			break;
66 		case 0x05:
67 			*src = SND_FF_CLOCK_SRC_LTC;
68 			break;
69 		default:
70 			return -EIO;
71 		}
72 	}
73 
74 	return 0;
75 }
76 
77 static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
78 				     int rcode)
79 {
80 	struct snd_rawmidi_substream *substream =
81 				READ_ONCE(ff->rx_midi_substreams[port]);
82 
83 	if (rcode_is_permanent_error(rcode)) {
84 		ff->rx_midi_error[port] = true;
85 		return;
86 	}
87 
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]);
92 		return;
93 	}
94 
95 	snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
96 	ff->rx_bytes[port] = 0;
97 
98 	if (!snd_rawmidi_transmit_empty(substream))
99 		schedule_work(&ff->rx_midi_work[port]);
100 }
101 
102 static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
103 				      void *data, size_t length,
104 				      void *callback_data)
105 {
106 	struct snd_ff *ff =
107 		container_of(callback_data, struct snd_ff, transactions[0]);
108 	finish_transmit_midi_msg(ff, 0, rcode);
109 }
110 
111 static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
112 				      void *data, size_t length,
113 				      void *callback_data)
114 {
115 	struct snd_ff *ff =
116 		container_of(callback_data, struct snd_ff, transactions[1]);
117 	finish_transmit_midi_msg(ff, 1, rcode);
118 }
119 
120 static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port,
121 				 unsigned int index, u8 byte)
122 {
123 	ff->msg_buf[port][index] = cpu_to_le32(byte);
124 }
125 
126 static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
127 {
128 	struct snd_rawmidi_substream *substream =
129 			READ_ONCE(ff->rx_midi_substreams[port]);
130 	u8 *buf = (u8 *)ff->msg_buf[port];
131 	int i, len;
132 
133 	struct fw_device *fw_dev = fw_parent_device(ff->unit);
134 	unsigned long long addr;
135 	int generation;
136 	fw_transaction_callback_t callback;
137 
138 	if (substream == NULL || snd_rawmidi_transmit_empty(substream))
139 		return;
140 
141 	if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
142 		return;
143 
144 	/* Do it in next chance. */
145 	if (ktime_after(ff->next_ktime[port], ktime_get())) {
146 		schedule_work(&ff->rx_midi_work[port]);
147 		return;
148 	}
149 
150 	len = snd_rawmidi_transmit_peek(substream, buf,
151 					SND_FF_MAXIMIM_MIDI_QUADS);
152 	if (len <= 0)
153 		return;
154 
155 	for (i = len - 1; i >= 0; i--)
156 		fill_midi_buf(ff, port, i, buf[i]);
157 
158 	if (port == 0) {
159 		addr = SND_FF_REG_MIDI_RX_PORT_0;
160 		callback = finish_transmit_midi0_msg;
161 	} else {
162 		addr = SND_FF_REG_MIDI_RX_PORT_1;
163 		callback = finish_transmit_midi1_msg;
164 	}
165 
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;
170 
171 	/*
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.
178 	 */
179 	generation = fw_dev->generation;
180 	smp_rmb();
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]);
186 }
187 
188 static void transmit_midi0_msg(struct work_struct *work)
189 {
190 	struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
191 
192 	transmit_midi_msg(ff, 0);
193 }
194 
195 static void transmit_midi1_msg(struct work_struct *work)
196 {
197 	struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
198 
199 	transmit_midi_msg(ff, 1);
200 }
201 
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)
206 {
207 	struct snd_ff *ff = callback_data;
208 	__le32 *buf = data;
209 
210 	fw_send_response(card, request, RCODE_COMPLETE);
211 
212 	ff->spec->protocol->handle_midi_msg(ff, buf, length);
213 }
214 
215 static int allocate_own_address(struct snd_ff *ff, int i)
216 {
217 	struct fw_address_region midi_msg_region;
218 	int err;
219 
220 	ff->async_handler.length = SND_FF_MAXIMIM_MIDI_QUADS * 4;
221 	ff->async_handler.address_callback = handle_midi_msg;
222 	ff->async_handler.callback_data = ff;
223 
224 	midi_msg_region.start = 0x000100000000ull * i;
225 	midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
226 
227 	err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
228 	if (err >= 0) {
229 		/* Controllers are allowed to register this region. */
230 		if (ff->async_handler.offset & 0x0000ffffffff) {
231 			fw_core_remove_address_handler(&ff->async_handler);
232 			err = -EAGAIN;
233 		}
234 	}
235 
236 	return err;
237 }
238 
239 /*
240  * Controllers are allowed to register higher 4 bytes of address to receive
241  * the transactions. Different models have different registers for this purpose;
242  * e.g. 0x'0000'8010'03f4 for Fireface 400.
243  * The controllers are not allowed to register lower 4 bytes of the address.
244  * They are forced to select one of 4 options for the part of address by writing
245  * corresponding bits to 0x'0000'8010'051f.
246  *
247  * The 3rd-6th bits of this register are flags to indicate lower 4 bytes of
248  * address to which the device transferrs the transactions. In short:
249  *  - 0x20: 0x'....'....'0000'0180
250  *  - 0x10: 0x'....'....'0000'0100
251  *  - 0x08: 0x'....'....'0000'0080
252  *  - 0x04: 0x'....'....'0000'0000
253  *
254  * This driver configure 0x'....'....'0000'0000 to receive MIDI messages from
255  * units. The 3rd bit of the register should be configured, however this driver
256  * deligates this task to userspace applications due to a restriction that this
257  * register is write-only and the other bits have own effects.
258  *
259  * Unlike Fireface 800, Fireface 400 cancels transferring asynchronous
260  * transactions when the 1st and 2nd of the register stand. These two bits have
261  * the same effect.
262  *  - 0x02, 0x01: cancel transferring
263  *
264  * On the other hand, the bits have no effect on Fireface 800. This model
265  * cancels asynchronous transactions when the higher 4 bytes of address is
266  * overwritten with zero.
267  */
268 int snd_ff_transaction_reregister(struct snd_ff *ff)
269 {
270 	struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
271 	u32 addr;
272 	__le32 reg;
273 
274 	/*
275 	 * Controllers are allowed to register its node ID and upper 2 byte of
276 	 * local address to listen asynchronous transactions.
277 	 */
278 	addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32);
279 	reg = cpu_to_le32(addr);
280 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
281 				  ff->spec->midi_high_addr,
282 				  &reg, sizeof(reg), 0);
283 }
284 
285 int snd_ff_transaction_register(struct snd_ff *ff)
286 {
287 	int i, err;
288 
289 	/*
290 	 * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
291 	 * be zero due to device specification.
292 	 */
293 	for (i = 0; i < 0xffff; i++) {
294 		err = allocate_own_address(ff, i);
295 		if (err != -EBUSY && err != -EAGAIN)
296 			break;
297 	}
298 	if (err < 0)
299 		return err;
300 
301 	err = snd_ff_transaction_reregister(ff);
302 	if (err < 0)
303 		return err;
304 
305 	INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
306 	INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
307 
308 	return 0;
309 }
310 
311 void snd_ff_transaction_unregister(struct snd_ff *ff)
312 {
313 	__le32 reg;
314 
315 	if (ff->async_handler.callback_data == NULL)
316 		return;
317 	ff->async_handler.callback_data = NULL;
318 
319 	/* Release higher 4 bytes of address. */
320 	reg = cpu_to_le32(0x00000000);
321 	snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
322 			   ff->spec->midi_high_addr,
323 			   &reg, sizeof(reg), 0);
324 
325 	fw_core_remove_address_handler(&ff->async_handler);
326 }
327