blob: ea88655e7e08de503dd123ec9f21efbabfa35648 [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;
Takashi Sakamoto516a3062015-10-20 23:46:56 +090065 int i, len, consume;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090066 u8 status;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090067
Takashi Sakamotob7ab6142015-10-20 23:46:57 +090068 consume = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
69 if (consume == 0)
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090070 return 0;
71
72 /* On exclusive message. */
73 if (tscm->on_sysex[port]) {
74 /* Seek the end of exclusives. */
Takashi Sakamotob7ab6142015-10-20 23:46:57 +090075 for (i = 1; i < 4 || i < consume; ++i) {
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090076 if (buf[i] == 0xf7) {
77 tscm->on_sysex[port] = false;
78 break;
79 }
80 }
81
82 /* At the end of exclusive message, use label 0x07. */
83 if (!tscm->on_sysex[port]) {
Takashi Sakamotob7ab6142015-10-20 23:46:57 +090084 consume = i;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090085 buf[0] = (port << 4) | 0x07;
86 /* During exclusive message, use label 0x04. */
Takashi Sakamotob7ab6142015-10-20 23:46:57 +090087 } else if (consume == 3) {
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090088 buf[0] = (port << 4) | 0x04;
89 /* We need to fill whole 3 bytes. Go to next change. */
90 } else {
Takashi Sakamotob7ab6142015-10-20 23:46:57 +090091 consume = 0;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +090092 }
93 } else {
94 /* The beginning of exclusives. */
95 if (buf[1] == 0xf0) {
96 /* Transfer it in next chance in another condition. */
97 tscm->on_sysex[port] = true;
98 return 0;
99 } else {
100 /* On running-status. */
101 if ((buf[1] & 0x80) != 0x80)
102 status = tscm->running_status[port];
103 else
104 status = buf[1];
105
106 /* Calculate consume bytes. */
Takashi Sakamotob7ab6142015-10-20 23:46:57 +0900107 len = calculate_message_bytes(status);
108 if (len <= 0)
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900109 return 0;
110
111 /* On running-status. */
112 if ((buf[1] & 0x80) != 0x80) {
113 buf[3] = buf[2];
114 buf[2] = buf[1];
115 buf[1] = tscm->running_status[port];
116 consume--;
117 } else {
118 tscm->running_status[port] = buf[1];
119 }
120
121 /* Confirm length. */
Takashi Sakamotob7ab6142015-10-20 23:46:57 +0900122 if (consume < len)
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900123 return 0;
Takashi Sakamotob7ab6142015-10-20 23:46:57 +0900124 if (consume > len)
125 consume = len;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900126 }
127
128 buf[0] = (port << 4) | (buf[1] >> 4);
129 }
130
Takashi Sakamotob7ab6142015-10-20 23:46:57 +0900131 return consume;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900132}
133
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900134static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
135 int tcode, int destination, int source,
136 int generation, unsigned long long offset,
137 void *data, size_t length, void *callback_data)
138{
139 struct snd_tscm *tscm = callback_data;
140 u32 *buf = (u32 *)data;
141 unsigned int messages;
142 unsigned int i;
143 unsigned int port;
144 struct snd_rawmidi_substream *substream;
145 u8 *b;
146 int bytes;
147
148 if (offset != tscm->async_handler.offset)
149 goto end;
150
151 messages = length / 8;
152 for (i = 0; i < messages; i++) {
153 b = (u8 *)(buf + i * 2);
154
155 port = b[0] >> 4;
156 /* TODO: support virtual MIDI ports. */
Dan Carpenter724097052015-10-15 21:17:11 +0300157 if (port >= tscm->spec->midi_capture_ports)
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900158 goto end;
159
160 /* Assume the message length. */
161 bytes = calculate_message_bytes(b[1]);
162 /* On MIDI data or exclusives. */
163 if (bytes <= 0) {
164 /* Seek the end of exclusives. */
165 for (bytes = 1; bytes < 4; bytes++) {
166 if (b[bytes] == 0xf7)
167 break;
168 }
169 if (bytes == 4)
170 bytes = 3;
171 }
172
173 substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
174 if (substream != NULL)
175 snd_rawmidi_receive(substream, b + 1, bytes);
176 }
177end:
178 fw_send_response(card, request, RCODE_COMPLETE);
179}
180
181int snd_tscm_transaction_register(struct snd_tscm *tscm)
182{
183 static const struct fw_address_region resp_register_region = {
184 .start = 0xffffe0000000ull,
185 .end = 0xffffe000ffffull,
186 };
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900187 unsigned int i;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900188 int err;
189
190 /*
191 * Usually, two quadlets are transferred by one transaction. The first
192 * quadlet has MIDI messages, the rest includes timestamp.
193 * Sometimes, 8 set of the data is transferred by a block transaction.
194 */
195 tscm->async_handler.length = 8 * 8;
196 tscm->async_handler.address_callback = handle_midi_tx;
197 tscm->async_handler.callback_data = tscm;
198
199 err = fw_core_add_address_handler(&tscm->async_handler,
200 &resp_register_region);
201 if (err < 0)
202 return err;
203
204 err = snd_tscm_transaction_reregister(tscm);
205 if (err < 0)
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900206 goto error;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900207
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900208 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
209 err = snd_fw_async_midi_port_init(
210 &tscm->out_ports[i], tscm->unit,
211 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,
212 4, fill_message);
213 if (err < 0)
214 goto error;
215 }
216
217 return err;
218error:
219 fw_core_remove_address_handler(&tscm->async_handler);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900220 return err;
221}
222
223/* At bus reset, these registers are cleared. */
224int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
225{
226 struct fw_device *device = fw_parent_device(tscm->unit);
227 __be32 reg;
228 int err;
229
230 /* Register messaging address. Block transaction is not allowed. */
231 reg = cpu_to_be32((device->card->node_id << 16) |
232 (tscm->async_handler.offset >> 32));
233 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
234 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
235 &reg, sizeof(reg), 0);
236 if (err < 0)
237 return err;
238
239 reg = cpu_to_be32(tscm->async_handler.offset);
240 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
241 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
242 &reg, sizeof(reg), 0);
243 if (err < 0)
244 return err;
245
246 /* Turn on messaging. */
247 reg = cpu_to_be32(0x00000001);
Dan Carpenter69ec98d2015-10-15 21:16:30 +0300248 err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900249 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
250 &reg, sizeof(reg), 0);
Takashi Sakamotoe65e2cb2015-10-12 19:10:24 +0900251 if (err < 0)
252 return err;
253
254 /* Turn on FireWire LED. */
255 reg = cpu_to_be32(0x0001008e);
256 return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
257 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
258 &reg, sizeof(reg), 0);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900259}
260
261void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
262{
263 __be32 reg;
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900264 unsigned int i;
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900265
Takashi Sakamotoe65e2cb2015-10-12 19:10:24 +0900266 /* Turn off FireWire LED. */
267 reg = cpu_to_be32(0x0000008e);
268 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
269 TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
270 &reg, sizeof(reg), 0);
271
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900272 /* Turn off messaging. */
273 reg = cpu_to_be32(0x00000000);
274 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
275 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
276 &reg, sizeof(reg), 0);
277
278 /* Unregister the address. */
279 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
280 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
281 &reg, sizeof(reg), 0);
282 snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
283 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
284 &reg, sizeof(reg), 0);
285
286 fw_core_remove_address_handler(&tscm->async_handler);
Takashi Sakamoto3beab0f2015-10-12 19:10:22 +0900287 for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
288 snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);
Takashi Sakamoto107cc012015-10-12 19:10:21 +0900289}