tascam-transaction.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * tascam-transaction.c - a part of driver for TASCAM FireWire series
 *
 * Copyright (c) 2015 Takashi Sakamoto
 *
 * Licensed under the terms of the GNU General Public License, version 2.
 */

#include "tascam.h"

/*
 * When return minus value, given argument is not MIDI status.
 * When return 0, given argument is a beginning of system exclusive.
 * When return the others, given argument is MIDI data.
 */
static inline int calculate_message_bytes(u8 status)
{
	switch (status) {
	case 0xf6:	/* Tune request. */
	case 0xf8:	/* Timing clock. */
	case 0xfa:	/* Start. */
	case 0xfb:	/* Continue. */
	case 0xfc:	/* Stop. */
	case 0xfe:	/* Active sensing. */
	case 0xff:	/* System reset. */
		return 1;
	case 0xf1:	/* MIDI time code quarter frame. */
	case 0xf3:	/* Song select. */
		return 2;
	case 0xf2:	/* Song position pointer. */
		return 3;
	case 0xf0:	/* Exclusive. */
		return 0;
	case 0xf7:	/* End of exclusive. */
		break;
	case 0xf4:	/* Undefined. */
	case 0xf5:	/* Undefined. */
	case 0xf9:	/* Undefined. */
	case 0xfd:	/* Undefined. */
		break;
	default:
		switch (status & 0xf0) {
		case 0x80:	/* Note on. */
		case 0x90:	/* Note off. */
		case 0xa0:	/* Polyphonic key pressure. */
		case 0xb0:	/* Control change and Mode change. */
		case 0xe0:	/* Pitch bend change. */
			return 3;
		case 0xc0:	/* Program change. */
		case 0xd0:	/* Channel pressure. */
			return 2;
		default:
		break;
		}
	break;
	}

	return -EINVAL;
}

61 62 63 64
static int fill_message(struct snd_rawmidi_substream *substream, u8 *buf)
{
	struct snd_tscm *tscm = substream->rmidi->private_data;
	unsigned int port = substream->number;
65
	int i, len, consume;
66
	u8 *label, *msg;
67 68
	u8 status;

69 70 71 72 73
	/* The first byte is used for label, the rest for MIDI bytes. */
	label = buf;
	msg = buf + 1;

	consume = snd_rawmidi_transmit_peek(substream, msg, 3);
74
	if (consume == 0)
75 76 77 78 79
		return 0;

	/* On exclusive message. */
	if (tscm->on_sysex[port]) {
		/* Seek the end of exclusives. */
80 81
		for (i = 0; i < consume; ++i) {
			if (msg[i] == 0xf7) {
82 83 84 85 86 87 88
				tscm->on_sysex[port] = false;
				break;
			}
		}

		/* At the end of exclusive message, use label 0x07. */
		if (!tscm->on_sysex[port]) {
89 90
			consume = i + 1;
			*label = (port << 4) | 0x07;
91
		/* During exclusive message, use label 0x04. */
92
		} else if (consume == 3) {
93
			*label = (port << 4) | 0x04;
94 95
		/* We need to fill whole 3 bytes. Go to next change. */
		} else {
96
			return 0;
97
		}
98 99

		len = consume;
100 101
	} else {
		/* The beginning of exclusives. */
102
		if (msg[0] == 0xf0) {
103 104 105 106 107
			/* Transfer it in next chance in another condition. */
			tscm->on_sysex[port] = true;
			return 0;
		} else {
			/* On running-status. */
108
			if ((msg[0] & 0x80) != 0x80)
109 110
				status = tscm->running_status[port];
			else
111
				status = msg[0];
112 113

			/* Calculate consume bytes. */
114 115
			len = calculate_message_bytes(status);
			if (len <= 0)
116 117 118
				return 0;

			/* On running-status. */
119
			if ((msg[0] & 0x80) != 0x80) {
120 121 122 123 124
				/* Enough MIDI bytes were not retrieved. */
				if (consume < len - 1)
					return 0;
				consume = len - 1;

125 126 127
				msg[2] = msg[1];
				msg[1] = msg[0];
				msg[0] = tscm->running_status[port];
128
			} else {
129 130 131 132 133
				/* Enough MIDI bytes were not retrieved. */
				if (consume < len)
					return 0;
				consume = len;

134
				tscm->running_status[port] = msg[0];
135 136 137
			}
		}

138
		*label = (port << 4) | (msg[0] >> 4);
139 140
	}

141 142 143
	if (len > 0 && len < 3)
		memset(msg + len, 0, 3 - len);

144
	return consume;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
static void async_midi_port_callback(struct fw_card *card, int rcode,
				     void *data, size_t length,
				     void *callback_data)
{
	struct snd_fw_async_midi_port *port = callback_data;
	struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);

	/* This port is closed. */
	if (substream == NULL)
		return;

	if (rcode == RCODE_COMPLETE)
		snd_rawmidi_transmit_ack(substream, port->consume_bytes);
	else if (!rcode_is_permanent_error(rcode))
		/* To start next transaction immediately for recovery. */
		port->next_ktime = 0;
	else
		/* Don't continue processing. */
		port->error = true;

	port->idling = true;

	if (!snd_rawmidi_transmit_empty(substream))
		schedule_work(&port->work);
}

static void midi_port_work(struct work_struct *work)
{
	struct snd_fw_async_midi_port *port =
			container_of(work, struct snd_fw_async_midi_port, work);
	struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
	int generation;

	/* Under transacting or error state. */
	if (!port->idling || port->error)
		return;

	/* Nothing to do. */
	if (substream == NULL || snd_rawmidi_transmit_empty(substream))
		return;

	/* Do it in next chance. */
	if (ktime_after(port->next_ktime, ktime_get())) {
		schedule_work(&port->work);
		return;
	}

	/*
	 * Fill the buffer. The callee must use snd_rawmidi_transmit_peek().
	 * Later, snd_rawmidi_transmit_ack() is called.
	 */
198
	memset(port->buf, 0, 4);
199
	port->consume_bytes = fill_message(substream, port->buf);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	if (port->consume_bytes <= 0) {
		/* Do it in next chance, immediately. */
		if (port->consume_bytes == 0) {
			port->next_ktime = 0;
			schedule_work(&port->work);
		} else {
			/* Fatal error. */
			port->error = true;
		}
		return;
	}

	/* Set interval to next transaction. */
	port->next_ktime = ktime_add_ns(ktime_get(),
				port->consume_bytes * 8 * NSEC_PER_SEC / 31250);

	/* Start this transaction. */
	port->idling = false;

	/*
	 * In Linux FireWire core, when generation is updated with memory
	 * barrier, node id has already been updated. In this module, After
	 * this smp_rmb(), load/store instructions to memory are completed.
	 * Thus, both of generation and node id are available with recent
	 * values. This is a light-serialization solution to handle bus reset
	 * events on IEEE 1394 bus.
	 */
	generation = port->parent->generation;
	smp_rmb();

230 231
	fw_send_request(port->parent->card, &port->transaction,
			TCODE_WRITE_QUADLET_REQUEST,
232 233
			port->parent->node_id, generation,
			port->parent->max_speed, port->addr,
234
			port->buf, 4, async_midi_port_callback,
235 236 237 238
			port);
}

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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	if (port->buf == NULL)
		return -ENOMEM;

	port->parent = fw_parent_device(unit);
	port->addr = addr;
	port->idling = true;
	port->next_ktime = 0;
	port->error = false;

	INIT_WORK(&port->work, midi_port_work);

	return 0;
}

void snd_fw_async_midi_port_destroy(struct snd_fw_async_midi_port *port)
{
	snd_fw_async_midi_port_finish(port);
	cancel_work_sync(&port->work);
	kfree(port->buf);
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
			   int tcode, int destination, int source,
			   int generation, unsigned long long offset,
			   void *data, size_t length, void *callback_data)
{
	struct snd_tscm *tscm = callback_data;
	u32 *buf = (u32 *)data;
	unsigned int messages;
	unsigned int i;
	unsigned int port;
	struct snd_rawmidi_substream *substream;
	u8 *b;
	int bytes;

	if (offset != tscm->async_handler.offset)
		goto end;

	messages = length / 8;
	for (i = 0; i < messages; i++) {
		b = (u8 *)(buf + i * 2);

		port = b[0] >> 4;
		/* TODO: support virtual MIDI ports. */
286
		if (port >= tscm->spec->midi_capture_ports)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
			goto end;

		/* Assume the message length. */
		bytes = calculate_message_bytes(b[1]);
		/* On MIDI data or exclusives. */
		if (bytes <= 0) {
			/* Seek the end of exclusives. */
			for (bytes = 1; bytes < 4; bytes++) {
				if (b[bytes] == 0xf7)
					break;
			}
			if (bytes == 4)
				bytes = 3;
		}

		substream = ACCESS_ONCE(tscm->tx_midi_substreams[port]);
		if (substream != NULL)
			snd_rawmidi_receive(substream, b + 1, bytes);
	}
end:
	fw_send_response(card, request, RCODE_COMPLETE);
}

int snd_tscm_transaction_register(struct snd_tscm *tscm)
{
	static const struct fw_address_region resp_register_region = {
		.start	= 0xffffe0000000ull,
		.end	= 0xffffe000ffffull,
	};
316
	unsigned int i;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	int err;

	/*
	 * Usually, two quadlets are transferred by one transaction. The first
	 * quadlet has MIDI messages, the rest includes timestamp.
	 * Sometimes, 8 set of the data is transferred by a block transaction.
	 */
	tscm->async_handler.length = 8 * 8;
	tscm->async_handler.address_callback = handle_midi_tx;
	tscm->async_handler.callback_data = tscm;

	err = fw_core_add_address_handler(&tscm->async_handler,
					  &resp_register_region);
	if (err < 0)
		return err;

	err = snd_tscm_transaction_reregister(tscm);
	if (err < 0)
335 336 337 338 339
		goto error;

	for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
		err = snd_fw_async_midi_port_init(
				&tscm->out_ports[i], tscm->unit,
340
				TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD);
341 342 343
		if (err < 0)
			goto error;
	}
344

345 346 347
	return err;
error:
	fw_core_remove_address_handler(&tscm->async_handler);
348
	tscm->async_handler.callback_data = NULL;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	return err;
}

/* At bus reset, these registers are cleared. */
int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
{
	struct fw_device *device = fw_parent_device(tscm->unit);
	__be32 reg;
	int err;

	/* Register messaging address. Block transaction is not allowed. */
	reg = cpu_to_be32((device->card->node_id << 16) |
			  (tscm->async_handler.offset >> 32));
	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
				 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
				 &reg, sizeof(reg), 0);
	if (err < 0)
		return err;

	reg = cpu_to_be32(tscm->async_handler.offset);
	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
				 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
				 &reg, sizeof(reg), 0);
	if (err < 0)
		return err;

	/* Turn on messaging. */
	reg = cpu_to_be32(0x00000001);
377
	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
378 379
				  TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
				  &reg, sizeof(reg), 0);
380 381 382 383 384 385 386 387
	if (err < 0)
		return err;

	/* Turn on FireWire LED. */
	reg = cpu_to_be32(0x0001008e);
	return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
				  TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
				  &reg, sizeof(reg), 0);
388 389 390 391 392
}

void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
{
	__be32 reg;
393
	unsigned int i;
394

395 396 397
	if (tscm->async_handler.callback_data == NULL)
		return;

398 399 400 401 402 403
	/* Turn off FireWire LED. */
	reg = cpu_to_be32(0x0000008e);
	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
			   TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
			   &reg, sizeof(reg), 0);

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	/* Turn off messaging. */
	reg = cpu_to_be32(0x00000000);
	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
			   &reg, sizeof(reg), 0);

	/* Unregister the address. */
	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
			   &reg, sizeof(reg), 0);
	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
			   &reg, sizeof(reg), 0);

	fw_core_remove_address_handler(&tscm->async_handler);
419 420
	tscm->async_handler.callback_data = NULL;

421 422
	for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++)
		snd_fw_async_midi_port_destroy(&tscm->out_ports[i]);
423
}