]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/firewire/fireface/ff-transaction.c
ALSA: fireface: share register for async transaction of MIDI messages
[linux.git] / sound / firewire / fireface / ff-transaction.c
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         u32 quad;
210         u8 byte;
211         unsigned int index;
212         struct snd_rawmidi_substream *substream;
213         int i;
214
215         fw_send_response(card, request, RCODE_COMPLETE);
216
217         for (i = 0; i < length / 4; i++) {
218                 quad = le32_to_cpu(buf[i]);
219
220                 /* Message in first port. */
221                 /*
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
224                  * it.
225                  */
226                 index = (quad >> 8) & 0xff;
227                 if (index > 0) {
228                         substream = READ_ONCE(ff->tx_midi_substreams[0]);
229                         if (substream != NULL) {
230                                 byte = quad & 0xff;
231                                 snd_rawmidi_receive(substream, &byte, 1);
232                         }
233                 }
234
235                 /* Message in second port. */
236                 index = (quad >> 24) & 0xff;
237                 if (index > 0) {
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);
242                         }
243                 }
244         }
245 }
246
247 static int allocate_own_address(struct snd_ff *ff, int i)
248 {
249         struct fw_address_region midi_msg_region;
250         int err;
251
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;
255
256         midi_msg_region.start = 0x000100000000ull * i;
257         midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
258
259         err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
260         if (err >= 0) {
261                 /* Controllers are allowed to register this region. */
262                 if (ff->async_handler.offset & 0x0000ffffffff) {
263                         fw_core_remove_address_handler(&ff->async_handler);
264                         err = -EAGAIN;
265                 }
266         }
267
268         return err;
269 }
270
271 /*
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
276  * messages.
277  *
278  * Here, I just describe MIDI-related offsets of the register, in little-endian
279  * order.
280  *
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
285  * 0x'0000'8010'051c.
286  *
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
293  *
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.
298  *
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
302  */
303 int snd_ff_transaction_reregister(struct snd_ff *ff)
304 {
305         struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
306         u32 addr;
307         __le32 reg;
308
309         /*
310          * Controllers are allowed to register its node ID and upper 2 byte of
311          * local address to listen asynchronous transactions.
312          */
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                                   &reg, sizeof(reg), 0);
318 }
319
320 int snd_ff_transaction_register(struct snd_ff *ff)
321 {
322         int i, err;
323
324         /*
325          * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
326          * be zero due to device specification.
327          */
328         for (i = 0; i < 0xffff; i++) {
329                 err = allocate_own_address(ff, i);
330                 if (err != -EBUSY && err != -EAGAIN)
331                         break;
332         }
333         if (err < 0)
334                 return err;
335
336         err = snd_ff_transaction_reregister(ff);
337         if (err < 0)
338                 return err;
339
340         INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
341         INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
342
343         return 0;
344 }
345
346 void snd_ff_transaction_unregister(struct snd_ff *ff)
347 {
348         __le32 reg;
349
350         if (ff->async_handler.callback_data == NULL)
351                 return;
352         ff->async_handler.callback_data = NULL;
353
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                            &reg, sizeof(reg), 0);
359
360         fw_core_remove_address_handler(&ff->async_handler);
361 }