]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - sound/firewire/tascam/tascam-transaction.c
2f5e20cf4ce5ee8c6d5245083dbb0e29e94718fd
[mirror_ubuntu-artful-kernel.git] / sound / firewire / tascam / tascam-transaction.c
1 /*
2 * tascam-transaction.c - a part of driver for TASCAM FireWire series
3 *
4 * Copyright (c) 2015 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9 #include "tascam.h"
10
11 /*
12 * When return minus value, given argument is not MIDI status.
13 * When return 0, given argument is a beginning of system exclusive.
14 * When return the others, given argument is MIDI data.
15 */
16 static inline int calculate_message_bytes(u8 status)
17 {
18 switch (status) {
19 case 0xf6: /* Tune request. */
20 case 0xf8: /* Timing clock. */
21 case 0xfa: /* Start. */
22 case 0xfb: /* Continue. */
23 case 0xfc: /* Stop. */
24 case 0xfe: /* Active sensing. */
25 case 0xff: /* System reset. */
26 return 1;
27 case 0xf1: /* MIDI time code quarter frame. */
28 case 0xf3: /* Song select. */
29 return 2;
30 case 0xf2: /* Song position pointer. */
31 return 3;
32 case 0xf0: /* Exclusive. */
33 return 0;
34 case 0xf7: /* End of exclusive. */
35 break;
36 case 0xf4: /* Undefined. */
37 case 0xf5: /* Undefined. */
38 case 0xf9: /* Undefined. */
39 case 0xfd: /* Undefined. */
40 break;
41 default:
42 switch (status & 0xf0) {
43 case 0x80: /* Note on. */
44 case 0x90: /* Note off. */
45 case 0xa0: /* Polyphonic key pressure. */
46 case 0xb0: /* Control change and Mode change. */
47 case 0xe0: /* Pitch bend change. */
48 return 3;
49 case 0xc0: /* Program change. */
50 case 0xd0: /* Channel pressure. */
51 return 2;
52 default:
53 break;
54 }
55 break;
56 }
57
58 return -EINVAL;
59 }
60
61 static int fill_message(struct snd_rawmidi_substream *substream, u8 *buf)
62 {
63 struct snd_tscm *tscm = substream->rmidi->private_data;
64 unsigned int port = substream->number;
65 int i, len, consume;
66 u8 *label, *msg;
67 u8 status;
68
69 /* The first byte is used for label, the rest for MIDI bytes. */
70 label = buf;
71 msg = buf + 1;
72
73 consume = snd_rawmidi_transmit_peek(substream, msg, 3);
74 if (consume == 0)
75 return 0;
76
77 /* On exclusive message. */
78 if (tscm->on_sysex[port]) {
79 /* Seek the end of exclusives. */
80 for (i = 0; i < consume; ++i) {
81 if (msg[i] == 0xf7) {
82 tscm->on_sysex[port] = false;
83 break;
84 }
85 }
86
87 /* At the end of exclusive message, use label 0x07. */
88 if (!tscm->on_sysex[port]) {
89 consume = i + 1;
90 *label = (port << 4) | 0x07;
91 /* During exclusive message, use label 0x04. */
92 } else if (consume == 3) {
93 *label = (port << 4) | 0x04;
94 /* We need to fill whole 3 bytes. Go to next change. */
95 } else {
96 return 0;
97 }
98
99 len = consume;
100 } else {
101 /* The beginning of exclusives. */
102 if (msg[0] == 0xf0) {
103 /* Transfer it in next chance in another condition. */
104 tscm->on_sysex[port] = true;
105 return 0;
106 } else {
107 /* On running-status. */
108 if ((msg[0] & 0x80) != 0x80)
109 status = tscm->running_status[port];
110 else
111 status = msg[0];
112
113 /* Calculate consume bytes. */
114 len = calculate_message_bytes(status);
115 if (len <= 0)
116 return 0;
117
118 /* On running-status. */
119 if ((msg[0] & 0x80) != 0x80) {
120 /* Enough MIDI bytes were not retrieved. */
121 if (consume < len - 1)
122 return 0;
123 consume = len - 1;
124
125 msg[2] = msg[1];
126 msg[1] = msg[0];
127 msg[0] = tscm->running_status[port];
128 } else {
129 /* Enough MIDI bytes were not retrieved. */
130 if (consume < len)
131 return 0;
132 consume = len;
133
134 tscm->running_status[port] = msg[0];
135 }
136 }
137
138 *label = (port << 4) | (msg[0] >> 4);
139 }
140
141 if (len > 0 && len < 3)
142 memset(msg + len, 0, 3 - len);
143
144 return consume;
145 }
146
147 static void async_midi_port_callback(struct fw_card *card, int rcode,
148 void *data, size_t length,
149 void *callback_data)
150 {
151 struct snd_fw_async_midi_port *port = callback_data;
152 struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
153
154 /* This port is closed. */
155 if (substream == NULL)
156 return;
157
158 if (rcode == RCODE_COMPLETE)
159 snd_rawmidi_transmit_ack(substream, port->consume_bytes);
160 else if (!rcode_is_permanent_error(rcode))
161 /* To start next transaction immediately for recovery. */
162 port->next_ktime = 0;
163 else
164 /* Don't continue processing. */
165 port->error = true;
166
167 port->idling = true;
168
169 if (!snd_rawmidi_transmit_empty(substream))
170 schedule_work(&port->work);
171 }
172
173 static void midi_port_work(struct work_struct *work)
174 {
175 struct snd_fw_async_midi_port *port =
176 container_of(work, struct snd_fw_async_midi_port, work);
177 struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
178 int generation;
179
180 /* Under transacting or error state. */
181 if (!port->idling || port->error)
182 return;
183
184 /* Nothing to do. */
185 if (substream == NULL || snd_rawmidi_transmit_empty(substream))
186 return;
187
188 /* Do it in next chance. */
189 if (ktime_after(port->next_ktime, ktime_get())) {
190 schedule_work(&port->work);
191 return;
192 }
193
194 /*
195 * Fill the buffer. The callee must use snd_rawmidi_transmit_peek().
196 * Later, snd_rawmidi_transmit_ack() is called.
197 */
198 memset(port->buf, 0, 4);
199 port->consume_bytes = fill_message(substream, port->buf);
200 if (port->consume_bytes <= 0) {
201 /* Do it in next chance, immediately. */
202 if (port->consume_bytes == 0) {
203 port->next_ktime = 0;
204 schedule_work(&port->work);
205 } else {
206 /* Fatal error. */
207 port->error = true;
208 }
209 return;
210 }
211
212 /* Set interval to next transaction. */
213 port->next_ktime = ktime_add_ns(ktime_get(),
214 port->consume_bytes * 8 * NSEC_PER_SEC / 31250);
215
216 /* Start this transaction. */
217 port->idling = false;
218
219 /*
220 * In Linux FireWire core, when generation is updated with memory
221 * barrier, node id has already been updated. In this module, After
222 * this smp_rmb(), load/store instructions to memory are completed.
223 * Thus, both of generation and node id are available with recent
224 * values. This is a light-serialization solution to handle bus reset
225 * events on IEEE 1394 bus.
226 */
227 generation = port->parent->generation;
228 smp_rmb();
229
230 fw_send_request(port->parent->card, &port->transaction,
231 TCODE_WRITE_QUADLET_REQUEST,
232 port->parent->node_id, generation,
233 port->parent->max_speed, port->addr,
234 port->buf, 4, async_midi_port_callback,
235 port);
236 }
237
238 int snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port,
239 struct fw_unit *unit, u64 addr)
240 {
241 port->buf = kzalloc(4, GFP_KERNEL);
242 if (port->buf == NULL)
243 return -ENOMEM;
244
245 port->parent = fw_parent_device(unit);
246 port->addr = addr;
247 port->idling = true;
248 port->next_ktime = 0;
249 port->error = false;
250
251 INIT_WORK(&port->work, midi_port_work);
252
253 return 0;
254 }
255
256 void snd_fw_async_midi_port_destroy(struct snd_fw_async_midi_port *port)
257 {
258 snd_fw_async_midi_port_finish(port);
259 cancel_work_sync(&port->work);
260 kfree(port->buf);
261 }
262
263 static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
264 int tcode, int destination, int source,
265 int generation, unsigned long long offset,
266 void *data, size_t length, void *callback_data)
267 {
268 struct snd_tscm *tscm = callback_data;
269 u32 *buf = (u32 *)data;
270 unsigned int messages;
271 unsigned int i;
272 unsigned int port;
273 struct snd_rawmidi_substream *substream;
274 u8 *b;
275 int bytes;
276
277 if (offset != tscm->async_handler.offset)
278 goto end;
279
280 messages = length / 8;
281 for (i = 0; i < messages; i++) {
282 b = (u8 *)(buf + i * 2);
283
284 port = b[0] >> 4;
285 /* TODO: support virtual MIDI ports. */
286 if (port >= tscm->spec->midi_capture_ports)
287 goto end;
288
289 /* Assume the message length. */
290 bytes = calculate_message_bytes(b[1]);
291 /* On MIDI data or exclusives. */
292 if (bytes <= 0) {
293 /* Seek the end of exclusives. */
294 for (bytes = 1; bytes < 4; bytes++) {
295 if (b[bytes] == 0xf7)
296 break;
297 }
298 if (bytes == 4)
299 bytes = 3;
300 }
301
302 substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
303 if (substream != NULL)
304 snd_rawmidi_receive(substream, b + 1, bytes);
305 }
306 end:
307 fw_send_response(card, request, RCODE_COMPLETE);
308 }
309
310 int snd_tscm_transaction_register(struct snd_tscm *tscm)
311 {
312 static const struct fw_address_region resp_register_region = {
313 .start = 0xffffe0000000ull,
314 .end = 0xffffe000ffffull,
315 };
316 unsigned int i;
317 int err;
318
319 /*
320 * Usually, two quadlets are transferred by one transaction. The first
321 * quadlet has MIDI messages, the rest includes timestamp.
322 * Sometimes, 8 set of the data is transferred by a block transaction.
323 */
324 tscm->async_handler.length = 8 * 8;
325 tscm->async_handler.address_callback = handle_midi_tx;
326 tscm->async_handler.callback_data = tscm;
327
328 err = fw_core_add_address_handler(&tscm->async_handler,
329 &resp_register_region);
330 if (err < 0)
331 return err;
332
333 err = snd_tscm_transaction_reregister(tscm);
334 if (err < 0)
335 goto error;
336
337 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
338 err = snd_fw_async_midi_port_init(
339 &tscm->out_ports[i], tscm->unit,
340 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD);
341 if (err < 0)
342 goto error;
343 }
344
345 return err;
346 error:
347 fw_core_remove_address_handler(&tscm->async_handler);
348 tscm->async_handler.callback_data = NULL;
349 return err;
350 }
351
352 /* At bus reset, these registers are cleared. */
353 int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
354 {
355 struct fw_device *device = fw_parent_device(tscm->unit);
356 __be32 reg;
357 int err;
358
359 /* Register messaging address. Block transaction is not allowed. */
360 reg = cpu_to_be32((device->card->node_id << 16) |
361 (tscm->async_handler.offset >> 32));
362 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
363 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
364 &reg, sizeof(reg), 0);
365 if (err < 0)
366 return err;
367
368 reg = cpu_to_be32(tscm->async_handler.offset);
369 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
370 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
371 &reg, sizeof(reg), 0);
372 if (err < 0)
373 return err;
374
375 /* Turn on messaging. */
376 reg = cpu_to_be32(0x00000001);
377 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
378 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
379 &reg, sizeof(reg), 0);
380 if (err < 0)
381 return err;
382
383 /* Turn on FireWire LED. */
384 reg = cpu_to_be32(0x0001008e);
385 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
386 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
387 &reg, sizeof(reg), 0);
388 }
389
390 void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
391 {
392 __be32 reg;
393 unsigned int i;
394
395 if (tscm->async_handler.callback_data == NULL)
396 return;
397
398 /* Turn off FireWire LED. */
399 reg = cpu_to_be32(0x0000008e);
400 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
401 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
402 &reg, sizeof(reg), 0);
403
404 /* Turn off messaging. */
405 reg = cpu_to_be32(0x00000000);
406 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
407 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
408 &reg, sizeof(reg), 0);
409
410 /* Unregister the address. */
411 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
412 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
413 &reg, sizeof(reg), 0);
414 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
415 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
416 &reg, sizeof(reg), 0);
417
418 fw_core_remove_address_handler(&tscm->async_handler);
419 tscm->async_handler.callback_data = NULL;
420
421 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
422 snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);
423 }