blob: 45c3ce3d6020c985c89cf94c995d736a2aff8375 [file] [log] [blame]
Takashi Sakamoto107cc012015-10-12 19:10:21 +09001/*
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
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090061static 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 unsigned int len;
66 unsigned int i;
67 u8 status;
68 int consume;
69
70 buf[0] = buf[1] = buf[2] = buf[3] = 0x00;
71
72 len = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
73 if (len == 0)
74 return 0;
75
76 /* On exclusive message. */
77 if (tscm->on_sysex[port]) {
78 /* Seek the end of exclusives. */
79 for (i = 1; i < 4 || i < len; ++i) {
80 if (buf[i] == 0xf7) {
81 tscm->on_sysex[port] = false;
82 break;
83 }
84 }
85
86 /* At the end of exclusive message, use label 0x07. */
87 if (!tscm->on_sysex[port]) {
88 len = i;
89 buf[0] = (port << 4) | 0x07;
90 /* During exclusive message, use label 0x04. */
91 } else if (len == 3) {
92 buf[0] = (port << 4) | 0x04;
93 /* We need to fill whole 3 bytes. Go to next change. */
94 } else {
95 len = 0;
96 }
97 } else {
98 /* The beginning of exclusives. */
99 if (buf[1] == 0xf0) {
100 /* Transfer it in next chance in another condition. */
101 tscm->on_sysex[port] = true;
102 return 0;
103 } else {
104 /* On running-status. */
105 if ((buf[1] & 0x80) != 0x80)
106 status = tscm->running_status[port];
107 else
108 status = buf[1];
109
110 /* Calculate consume bytes. */
111 consume = calculate_message_bytes(status);
112 if (consume <= 0)
113 return 0;
114
115 /* On running-status. */
116 if ((buf[1] & 0x80) != 0x80) {
117 buf[3] = buf[2];
118 buf[2] = buf[1];
119 buf[1] = tscm->running_status[port];
120 consume--;
121 } else {
122 tscm->running_status[port] = buf[1];
123 }
124
125 /* Confirm length. */
126 if (len < consume)
127 return 0;
128 if (len > consume)
129 len = consume;
130 }
131
132 buf[0] = (port << 4) | (buf[1] >> 4);
133 }
134
135 return len;
136}
137
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900138static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
139 int tcode, int destination, int source,
140 int generation, unsigned long long offset,
141 void *data, size_t length, void *callback_data)
142{
143 struct snd_tscm *tscm = callback_data;
144 u32 *buf = (u32 *)data;
145 unsigned int messages;
146 unsigned int i;
147 unsigned int port;
148 struct snd_rawmidi_substream *substream;
149 u8 *b;
150 int bytes;
151
152 if (offset != tscm->async_handler.offset)
153 goto end;
154
155 messages = length / 8;
156 for (i = 0; i < messages; i++) {
157 b = (u8 *)(buf + i * 2);
158
159 port = b[0] >> 4;
160 /* TODO: support virtual MIDI ports. */
161 if (port > tscm->spec->midi_capture_ports)
162 goto end;
163
164 /* Assume the message length. */
165 bytes = calculate_message_bytes(b[1]);
166 /* On MIDI data or exclusives. */
167 if (bytes <= 0) {
168 /* Seek the end of exclusives. */
169 for (bytes = 1; bytes < 4; bytes++) {
170 if (b[bytes] == 0xf7)
171 break;
172 }
173 if (bytes == 4)
174 bytes = 3;
175 }
176
177 substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
178 if (substream != NULL)
179 snd_rawmidi_receive(substream, b + 1, bytes);
180 }
181end:
182 fw_send_response(card, request, RCODE_COMPLETE);
183}
184
185int snd_tscm_transaction_register(struct snd_tscm *tscm)
186{
187 static const struct fw_address_region resp_register_region = {
188 .start = 0xffffe0000000ull,
189 .end = 0xffffe000ffffull,
190 };
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900191 unsigned int i;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900192 int err;
193
194 /*
195 * Usually, two quadlets are transferred by one transaction. The first
196 * quadlet has MIDI messages, the rest includes timestamp.
197 * Sometimes, 8 set of the data is transferred by a block transaction.
198 */
199 tscm->async_handler.length = 8 * 8;
200 tscm->async_handler.address_callback = handle_midi_tx;
201 tscm->async_handler.callback_data = tscm;
202
203 err = fw_core_add_address_handler(&tscm->async_handler,
204 &resp_register_region);
205 if (err < 0)
206 return err;
207
208 err = snd_tscm_transaction_reregister(tscm);
209 if (err < 0)
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900210 goto error;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900211
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900212 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
213 err = snd_fw_async_midi_port_init(
214 &tscm->out_ports[i], tscm->unit,
215 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,
216 4, fill_message);
217 if (err < 0)
218 goto error;
219 }
220
221 return err;
222error:
223 fw_core_remove_address_handler(&tscm->async_handler);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900224 return err;
225}
226
227/* At bus reset, these registers are cleared. */
228int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
229{
230 struct fw_device *device = fw_parent_device(tscm->unit);
231 __be32 reg;
232 int err;
233
234 /* Register messaging address. Block transaction is not allowed. */
235 reg = cpu_to_be32((device->card->node_id << 16) |
236 (tscm->async_handler.offset >> 32));
237 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
238 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
239 &reg, sizeof(reg), 0);
240 if (err < 0)
241 return err;
242
243 reg = cpu_to_be32(tscm->async_handler.offset);
244 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
245 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
246 &reg, sizeof(reg), 0);
247 if (err < 0)
248 return err;
249
250 /* Turn on messaging. */
251 reg = cpu_to_be32(0x00000001);
Dan Carpenter69ec98d2015-10-15 21:16:30 +0300252 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900253 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
254 &reg, sizeof(reg), 0);
Takashi Sakamotoe65e2cb2015-10-12 19:10:24 +0900255 if (err < 0)
256 return err;
257
258 /* Turn on FireWire LED. */
259 reg = cpu_to_be32(0x0001008e);
260 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
261 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
262 &reg, sizeof(reg), 0);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900263}
264
265void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
266{
267 __be32 reg;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900268 unsigned int i;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900269
Takashi Sakamotoe65e2cb2015-10-12 19:10:24 +0900270 /* Turn off FireWire LED. */
271 reg = cpu_to_be32(0x0000008e);
272 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
273 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
274 &reg, sizeof(reg), 0);
275
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900276 /* Turn off messaging. */
277 reg = cpu_to_be32(0x00000000);
278 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
279 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
280 &reg, sizeof(reg), 0);
281
282 /* Unregister the address. */
283 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
284 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
285 &reg, sizeof(reg), 0);
286 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
287 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
288 &reg, sizeof(reg), 0);
289
290 fw_core_remove_address_handler(&tscm->async_handler);
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900291 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
292 snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900293}