ALSA: firewire-tascam: fix loop condition with some readable variables
[deliverable/linux.git] / sound / firewire / tascam / tascam-transaction.c
CommitLineData
107cc012
TS
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 */
16static 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
3beab0f8
TS
61static 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;
516a3061 65 int i, len, consume;
123990e9 66 u8 *label, *msg;
3beab0f8 67 u8 status;
3beab0f8 68
123990e9
TS
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);
b7ab614f 74 if (consume == 0)
3beab0f8
TS
75 return 0;
76
77 /* On exclusive message. */
78 if (tscm->on_sysex[port]) {
79 /* Seek the end of exclusives. */
123990e9
TS
80 for (i = 0; i < consume; ++i) {
81 if (msg[i] == 0xf7) {
3beab0f8
TS
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]) {
123990e9
TS
89 consume = i + 1;
90 *label = (port << 4) | 0x07;
3beab0f8 91 /* During exclusive message, use label 0x04. */
b7ab614f 92 } else if (consume == 3) {
123990e9 93 *label = (port << 4) | 0x04;
3beab0f8
TS
94 /* We need to fill whole 3 bytes. Go to next change. */
95 } else {
b7ab614f 96 consume = 0;
3beab0f8
TS
97 }
98 } else {
99 /* The beginning of exclusives. */
123990e9 100 if (msg[0] == 0xf0) {
3beab0f8
TS
101 /* Transfer it in next chance in another condition. */
102 tscm->on_sysex[port] = true;
103 return 0;
104 } else {
105 /* On running-status. */
123990e9 106 if ((msg[0] & 0x80) != 0x80)
3beab0f8
TS
107 status = tscm->running_status[port];
108 else
123990e9 109 status = msg[0];
3beab0f8
TS
110
111 /* Calculate consume bytes. */
b7ab614f
TS
112 len = calculate_message_bytes(status);
113 if (len <= 0)
3beab0f8
TS
114 return 0;
115
116 /* On running-status. */
123990e9
TS
117 if ((msg[0] & 0x80) != 0x80) {
118 msg[2] = msg[1];
119 msg[1] = msg[0];
120 msg[0] = tscm->running_status[port];
3beab0f8
TS
121 consume--;
122 } else {
123990e9 123 tscm->running_status[port] = msg[0];
3beab0f8
TS
124 }
125
126 /* Confirm length. */
b7ab614f 127 if (consume < len)
3beab0f8 128 return 0;
b7ab614f
TS
129 if (consume > len)
130 consume = len;
3beab0f8
TS
131 }
132
123990e9 133 *label = (port << 4) | (msg[0] >> 4);
3beab0f8
TS
134 }
135
b7ab614f 136 return consume;
3beab0f8
TS
137}
138
107cc012
TS
139static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
140 int tcode, int destination, int source,
141 int generation, unsigned long long offset,
142 void *data, size_t length, void *callback_data)
143{
144 struct snd_tscm *tscm = callback_data;
145 u32 *buf = (u32 *)data;
146 unsigned int messages;
147 unsigned int i;
148 unsigned int port;
149 struct snd_rawmidi_substream *substream;
150 u8 *b;
151 int bytes;
152
153 if (offset != tscm->async_handler.offset)
154 goto end;
155
156 messages = length / 8;
157 for (i = 0; i < messages; i++) {
158 b = (u8 *)(buf + i * 2);
159
160 port = b[0] >> 4;
161 /* TODO: support virtual MIDI ports. */
72409705 162 if (port >= tscm->spec->midi_capture_ports)
107cc012
TS
163 goto end;
164
165 /* Assume the message length. */
166 bytes = calculate_message_bytes(b[1]);
167 /* On MIDI data or exclusives. */
168 if (bytes <= 0) {
169 /* Seek the end of exclusives. */
170 for (bytes = 1; bytes < 4; bytes++) {
171 if (b[bytes] == 0xf7)
172 break;
173 }
174 if (bytes == 4)
175 bytes = 3;
176 }
177
178 substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
179 if (substream != NULL)
180 snd_rawmidi_receive(substream, b + 1, bytes);
181 }
182end:
183 fw_send_response(card, request, RCODE_COMPLETE);
184}
185
186int snd_tscm_transaction_register(struct snd_tscm *tscm)
187{
188 static const struct fw_address_region resp_register_region = {
189 .start = 0xffffe0000000ull,
190 .end = 0xffffe000ffffull,
191 };
3beab0f8 192 unsigned int i;
107cc012
TS
193 int err;
194
195 /*
196 * Usually, two quadlets are transferred by one transaction. The first
197 * quadlet has MIDI messages, the rest includes timestamp.
198 * Sometimes, 8 set of the data is transferred by a block transaction.
199 */
200 tscm->async_handler.length = 8 * 8;
201 tscm->async_handler.address_callback = handle_midi_tx;
202 tscm->async_handler.callback_data = tscm;
203
204 err = fw_core_add_address_handler(&tscm->async_handler,
205 &resp_register_region);
206 if (err < 0)
207 return err;
208
209 err = snd_tscm_transaction_reregister(tscm);
210 if (err < 0)
3beab0f8
TS
211 goto error;
212
213 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
214 err = snd_fw_async_midi_port_init(
215 &tscm->out_ports[i], tscm->unit,
216 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,
217 4, fill_message);
218 if (err < 0)
219 goto error;
220 }
107cc012 221
3beab0f8
TS
222 return err;
223error:
224 fw_core_remove_address_handler(&tscm->async_handler);
107cc012
TS
225 return err;
226}
227
228/* At bus reset, these registers are cleared. */
229int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
230{
231 struct fw_device *device = fw_parent_device(tscm->unit);
232 __be32 reg;
233 int err;
234
235 /* Register messaging address. Block transaction is not allowed. */
236 reg = cpu_to_be32((device->card->node_id << 16) |
237 (tscm->async_handler.offset >> 32));
238 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
239 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
240 &reg, sizeof(reg), 0);
241 if (err < 0)
242 return err;
243
244 reg = cpu_to_be32(tscm->async_handler.offset);
245 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
246 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
247 &reg, sizeof(reg), 0);
248 if (err < 0)
249 return err;
250
251 /* Turn on messaging. */
252 reg = cpu_to_be32(0x00000001);
69ec98d7 253 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
107cc012
TS
254 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
255 &reg, sizeof(reg), 0);
e65e2cb9
TS
256 if (err < 0)
257 return err;
258
259 /* Turn on FireWire LED. */
260 reg = cpu_to_be32(0x0001008e);
261 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
262 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
263 &reg, sizeof(reg), 0);
107cc012
TS
264}
265
266void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
267{
268 __be32 reg;
3beab0f8 269 unsigned int i;
107cc012 270
e65e2cb9
TS
271 /* Turn off FireWire LED. */
272 reg = cpu_to_be32(0x0000008e);
273 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
274 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
275 &reg, sizeof(reg), 0);
276
107cc012
TS
277 /* Turn off messaging. */
278 reg = cpu_to_be32(0x00000000);
279 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
280 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
281 &reg, sizeof(reg), 0);
282
283 /* Unregister the address. */
284 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
285 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
286 &reg, sizeof(reg), 0);
287 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
288 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
289 &reg, sizeof(reg), 0);
290
291 fw_core_remove_address_handler(&tscm->async_handler);
3beab0f8
TS
292 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
293 snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);
107cc012 294}
This page took 0.040654 seconds and 5 git commands to generate.