]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/firewire/fireface/ff-transaction.c
751662b62389d129a3b83bcf2bc2f1933262d5db
[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 int snd_ff_transaction_get_clock(struct snd_ff *ff, unsigned int *rate,
12                                  enum snd_ff_clock_src *src)
13 {
14         __le32 reg;
15         u32 data;
16         int err;
17
18         err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
19                                  SND_FF_REG_CLOCK_CONFIG, &reg, sizeof(reg), 0);
20         if (err < 0)
21                 return err;
22         data = le32_to_cpu(reg);
23
24         /* Calculate sampling rate. */
25         switch ((data >> 1) & 0x03) {
26         case 0x01:
27                 *rate = 32000;
28                 break;
29         case 0x00:
30                 *rate = 44100;
31                 break;
32         case 0x03:
33                 *rate = 48000;
34                 break;
35         case 0x02:
36         default:
37                 return -EIO;
38         }
39
40         if (data & 0x08)
41                 *rate *= 2;
42         else if (data & 0x10)
43                 *rate *= 4;
44
45         /* Calculate source of clock. */
46         if (data & 0x01) {
47                 *src = SND_FF_CLOCK_SRC_INTERNAL;
48         } else {
49                 /* TODO: 0x02, 0x06, 0x07? */
50                 switch ((data >> 10) & 0x07) {
51                 case 0x00:
52                         *src = SND_FF_CLOCK_SRC_ADAT1;
53                         break;
54                 case 0x01:
55                         *src = SND_FF_CLOCK_SRC_ADAT2;
56                         break;
57                 case 0x03:
58                         *src = SND_FF_CLOCK_SRC_SPDIF;
59                         break;
60                 case 0x04:
61                         *src = SND_FF_CLOCK_SRC_WORD;
62                         break;
63                 case 0x05:
64                         *src = SND_FF_CLOCK_SRC_LTC;
65                         break;
66                 default:
67                         return -EIO;
68                 }
69         }
70
71         return 0;
72 }
73
74 static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
75                                      int rcode)
76 {
77         struct snd_rawmidi_substream *substream =
78                                 READ_ONCE(ff->rx_midi_substreams[port]);
79
80         if (rcode_is_permanent_error(rcode)) {
81                 ff->rx_midi_error[port] = true;
82                 return;
83         }
84
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]);
89                 return;
90         }
91
92         snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
93         ff->rx_bytes[port] = 0;
94
95         if (!snd_rawmidi_transmit_empty(substream))
96                 schedule_work(&ff->rx_midi_work[port]);
97 }
98
99 static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
100                                       void *data, size_t length,
101                                       void *callback_data)
102 {
103         struct snd_ff *ff =
104                 container_of(callback_data, struct snd_ff, transactions[0]);
105         finish_transmit_midi_msg(ff, 0, rcode);
106 }
107
108 static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
109                                       void *data, size_t length,
110                                       void *callback_data)
111 {
112         struct snd_ff *ff =
113                 container_of(callback_data, struct snd_ff, transactions[1]);
114         finish_transmit_midi_msg(ff, 1, rcode);
115 }
116
117 static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port,
118                                  unsigned int index, u8 byte)
119 {
120         ff->msg_buf[port][index] = cpu_to_le32(byte);
121 }
122
123 static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
124 {
125         struct snd_rawmidi_substream *substream =
126                         READ_ONCE(ff->rx_midi_substreams[port]);
127         u8 *buf = (u8 *)ff->msg_buf[port];
128         int i, len;
129
130         struct fw_device *fw_dev = fw_parent_device(ff->unit);
131         unsigned long long addr;
132         int generation;
133         fw_transaction_callback_t callback;
134
135         if (substream == NULL || snd_rawmidi_transmit_empty(substream))
136                 return;
137
138         if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
139                 return;
140
141         /* Do it in next chance. */
142         if (ktime_after(ff->next_ktime[port], ktime_get())) {
143                 schedule_work(&ff->rx_midi_work[port]);
144                 return;
145         }
146
147         len = snd_rawmidi_transmit_peek(substream, buf,
148                                         SND_FF_MAXIMIM_MIDI_QUADS);
149         if (len <= 0)
150                 return;
151
152         for (i = len - 1; i >= 0; i--)
153                 fill_midi_buf(ff, port, i, buf[i]);
154
155         if (port == 0) {
156                 addr = ff->spec->protocol->midi_rx_port_0_reg;
157                 callback = finish_transmit_midi0_msg;
158         } else {
159                 addr = ff->spec->protocol->midi_rx_port_1_reg;
160                 callback = finish_transmit_midi1_msg;
161         }
162
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;
167
168         /*
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.
175          */
176         generation = fw_dev->generation;
177         smp_rmb();
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]);
183 }
184
185 static void transmit_midi0_msg(struct work_struct *work)
186 {
187         struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
188
189         transmit_midi_msg(ff, 0);
190 }
191
192 static void transmit_midi1_msg(struct work_struct *work)
193 {
194         struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
195
196         transmit_midi_msg(ff, 1);
197 }
198
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)
203 {
204         struct snd_ff *ff = callback_data;
205         __le32 *buf = data;
206         u32 quad;
207         u8 byte;
208         unsigned int index;
209         struct snd_rawmidi_substream *substream;
210         int i;
211
212         fw_send_response(card, request, RCODE_COMPLETE);
213
214         for (i = 0; i < length / 4; i++) {
215                 quad = le32_to_cpu(buf[i]);
216
217                 /* Message in first port. */
218                 /*
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
221                  * it.
222                  */
223                 index = (quad >> 8) & 0xff;
224                 if (index > 0) {
225                         substream = READ_ONCE(ff->tx_midi_substreams[0]);
226                         if (substream != NULL) {
227                                 byte = quad & 0xff;
228                                 snd_rawmidi_receive(substream, &byte, 1);
229                         }
230                 }
231
232                 /* Message in second port. */
233                 index = (quad >> 24) & 0xff;
234                 if (index > 0) {
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);
239                         }
240                 }
241         }
242 }
243
244 static int allocate_own_address(struct snd_ff *ff, int i)
245 {
246         struct fw_address_region midi_msg_region;
247         int err;
248
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;
252
253         midi_msg_region.start = 0x000100000000ull * i;
254         midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
255
256         err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
257         if (err >= 0) {
258                 /* Controllers are allowed to register this region. */
259                 if (ff->async_handler.offset & 0x0000ffffffff) {
260                         fw_core_remove_address_handler(&ff->async_handler);
261                         err = -EAGAIN;
262                 }
263         }
264
265         return err;
266 }
267
268 /*
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
273  * messages.
274  *
275  * Here, I just describe MIDI-related offsets of the register, in little-endian
276  * order.
277  *
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
282  * 0x'0000'8010'051c.
283  *
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
290  *
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.
295  *
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
299  */
300 int snd_ff_transaction_reregister(struct snd_ff *ff)
301 {
302         struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
303         u32 addr;
304         __le32 reg;
305
306         /*
307          * Controllers are allowed to register its node ID and upper 2 byte of
308          * local address to listen asynchronous transactions.
309          */
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                                   &reg, sizeof(reg), 0);
315 }
316
317 int snd_ff_transaction_register(struct snd_ff *ff)
318 {
319         int i, err;
320
321         /*
322          * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
323          * be zero due to device specification.
324          */
325         for (i = 0; i < 0xffff; i++) {
326                 err = allocate_own_address(ff, i);
327                 if (err != -EBUSY && err != -EAGAIN)
328                         break;
329         }
330         if (err < 0)
331                 return err;
332
333         err = snd_ff_transaction_reregister(ff);
334         if (err < 0)
335                 return err;
336
337         INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
338         INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
339
340         return 0;
341 }
342
343 void snd_ff_transaction_unregister(struct snd_ff *ff)
344 {
345         __le32 reg;
346
347         if (ff->async_handler.callback_data == NULL)
348                 return;
349         ff->async_handler.callback_data = NULL;
350
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                            &reg, sizeof(reg), 0);
356
357         fw_core_remove_address_handler(&ff->async_handler);
358 }