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