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 static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
12 				     int rcode)
13 {
14 	struct snd_rawmidi_substream *substream =
15 				READ_ONCE(ff->rx_midi_substreams[port]);
16 
17 	if (rcode_is_permanent_error(rcode)) {
18 		ff->rx_midi_error[port] = true;
19 		return;
20 	}
21 
22 	if (rcode != RCODE_COMPLETE) {
23 		/* Transfer the message again, immediately. */
24 		ff->next_ktime[port] = 0;
25 		schedule_work(&ff->rx_midi_work[port]);
26 		return;
27 	}
28 
29 	snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
30 	ff->rx_bytes[port] = 0;
31 
32 	if (!snd_rawmidi_transmit_empty(substream))
33 		schedule_work(&ff->rx_midi_work[port]);
34 }
35 
36 static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
37 				      void *data, size_t length,
38 				      void *callback_data)
39 {
40 	struct snd_ff *ff =
41 		container_of(callback_data, struct snd_ff, transactions[0]);
42 	finish_transmit_midi_msg(ff, 0, rcode);
43 }
44 
45 static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
46 				      void *data, size_t length,
47 				      void *callback_data)
48 {
49 	struct snd_ff *ff =
50 		container_of(callback_data, struct snd_ff, transactions[1]);
51 	finish_transmit_midi_msg(ff, 1, rcode);
52 }
53 
54 static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
55 {
56 	struct snd_rawmidi_substream *substream =
57 			READ_ONCE(ff->rx_midi_substreams[port]);
58 	int quad_count;
59 
60 	struct fw_device *fw_dev = fw_parent_device(ff->unit);
61 	unsigned long long addr;
62 	int generation;
63 	fw_transaction_callback_t callback;
64 	int tcode;
65 
66 	if (substream == NULL || snd_rawmidi_transmit_empty(substream))
67 		return;
68 
69 	if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
70 		return;
71 
72 	/* Do it in next chance. */
73 	if (ktime_after(ff->next_ktime[port], ktime_get())) {
74 		schedule_work(&ff->rx_midi_work[port]);
75 		return;
76 	}
77 
78 	quad_count = ff->spec->protocol->fill_midi_msg(ff, substream, port);
79 	if (quad_count <= 0)
80 		return;
81 
82 	if (port == 0) {
83 		addr = ff->spec->midi_rx_addrs[0];
84 		callback = finish_transmit_midi0_msg;
85 	} else {
86 		addr = ff->spec->midi_rx_addrs[1];
87 		callback = finish_transmit_midi1_msg;
88 	}
89 
90 	/* Set interval to next transaction. */
91 	ff->next_ktime[port] = ktime_add_ns(ktime_get(),
92 				ff->rx_bytes[port] * 8 * NSEC_PER_SEC / 31250);
93 
94 	if (quad_count == 1)
95 		tcode = TCODE_WRITE_QUADLET_REQUEST;
96 	else
97 		tcode = TCODE_WRITE_BLOCK_REQUEST;
98 
99 	/*
100 	 * In Linux FireWire core, when generation is updated with memory
101 	 * barrier, node id has already been updated. In this module, After
102 	 * this smp_rmb(), load/store instructions to memory are completed.
103 	 * Thus, both of generation and node id are available with recent
104 	 * values. This is a light-serialization solution to handle bus reset
105 	 * events on IEEE 1394 bus.
106 	 */
107 	generation = fw_dev->generation;
108 	smp_rmb();
109 	fw_send_request(fw_dev->card, &ff->transactions[port], tcode,
110 			fw_dev->node_id, generation, fw_dev->max_speed,
111 			addr, &ff->msg_buf[port], quad_count * 4,
112 			callback, &ff->transactions[port]);
113 }
114 
115 static void transmit_midi0_msg(struct work_struct *work)
116 {
117 	struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
118 
119 	transmit_midi_msg(ff, 0);
120 }
121 
122 static void transmit_midi1_msg(struct work_struct *work)
123 {
124 	struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
125 
126 	transmit_midi_msg(ff, 1);
127 }
128 
129 static void handle_midi_msg(struct fw_card *card, struct fw_request *request,
130 			    int tcode, int destination, int source,
131 			    int generation, unsigned long long offset,
132 			    void *data, size_t length, void *callback_data)
133 {
134 	struct snd_ff *ff = callback_data;
135 	__le32 *buf = data;
136 
137 	fw_send_response(card, request, RCODE_COMPLETE);
138 
139 	offset -= ff->async_handler.offset;
140 	ff->spec->protocol->handle_midi_msg(ff, (unsigned int)offset, buf,
141 					    length);
142 }
143 
144 static int allocate_own_address(struct snd_ff *ff, int i)
145 {
146 	struct fw_address_region midi_msg_region;
147 	int err;
148 
149 	ff->async_handler.length = ff->spec->midi_addr_range;
150 	ff->async_handler.address_callback = handle_midi_msg;
151 	ff->async_handler.callback_data = ff;
152 
153 	midi_msg_region.start = 0x000100000000ull * i;
154 	midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
155 
156 	err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
157 	if (err >= 0) {
158 		/* Controllers are allowed to register this region. */
159 		if (ff->async_handler.offset & 0x0000ffffffff) {
160 			fw_core_remove_address_handler(&ff->async_handler);
161 			err = -EAGAIN;
162 		}
163 	}
164 
165 	return err;
166 }
167 
168 // Controllers are allowed to register higher 4 bytes of destination address to
169 // receive asynchronous transactions for MIDI messages, while the way to
170 // register lower 4 bytes of address is different depending on protocols. For
171 // details, please refer to comments in protocol implementations.
172 //
173 // This driver expects userspace applications to configure registers for the
174 // lower address because in most cases such registers has the other settings.
175 int snd_ff_transaction_reregister(struct snd_ff *ff)
176 {
177 	struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
178 	u32 addr;
179 	__le32 reg;
180 
181 	/*
182 	 * Controllers are allowed to register its node ID and upper 2 byte of
183 	 * local address to listen asynchronous transactions.
184 	 */
185 	addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32);
186 	reg = cpu_to_le32(addr);
187 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
188 				  ff->spec->midi_high_addr,
189 				  &reg, sizeof(reg), 0);
190 }
191 
192 int snd_ff_transaction_register(struct snd_ff *ff)
193 {
194 	int i, err;
195 
196 	/*
197 	 * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
198 	 * be zero due to device specification.
199 	 */
200 	for (i = 0; i < 0xffff; i++) {
201 		err = allocate_own_address(ff, i);
202 		if (err != -EBUSY && err != -EAGAIN)
203 			break;
204 	}
205 	if (err < 0)
206 		return err;
207 
208 	err = snd_ff_transaction_reregister(ff);
209 	if (err < 0)
210 		return err;
211 
212 	INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
213 	INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
214 
215 	return 0;
216 }
217 
218 void snd_ff_transaction_unregister(struct snd_ff *ff)
219 {
220 	__le32 reg;
221 
222 	if (ff->async_handler.callback_data == NULL)
223 		return;
224 	ff->async_handler.callback_data = NULL;
225 
226 	/* Release higher 4 bytes of address. */
227 	reg = cpu_to_le32(0x00000000);
228 	snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
229 			   ff->spec->midi_high_addr,
230 			   &reg, sizeof(reg), 0);
231 
232 	fw_core_remove_address_handler(&ff->async_handler);
233 }
234