Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * tascam-transaction.c - a part of driver for TASCAM FireWire series
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2015 Takashi Sakamoto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "tascam.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * When return minus value, given argument is not MIDI status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * When return 0, given argument is a beginning of system exclusive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * When return the others, given argument is MIDI data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static inline int calculate_message_bytes(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	case 0xf6:	/* Tune request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	case 0xf8:	/* Timing clock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	case 0xfa:	/* Start. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	case 0xfb:	/* Continue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	case 0xfc:	/* Stop. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	case 0xfe:	/* Active sensing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	case 0xff:	/* System reset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	case 0xf1:	/* MIDI time code quarter frame. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	case 0xf3:	/* Song select. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	case 0xf2:	/* Song position pointer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	case 0xf0:	/* Exclusive. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	case 0xf7:	/* End of exclusive. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	case 0xf4:	/* Undefined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	case 0xf5:	/* Undefined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	case 0xf9:	/* Undefined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	case 0xfd:	/* Undefined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		switch (status & 0xf0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		case 0x80:	/* Note on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		case 0x90:	/* Note off. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		case 0xa0:	/* Polyphonic key pressure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		case 0xb0:	/* Control change and Mode change. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		case 0xe0:	/* Pitch bend change. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			return 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		case 0xc0:	/* Program change. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		case 0xd0:	/* Channel pressure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int fill_message(struct snd_fw_async_midi_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			struct snd_rawmidi_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int i, len, consume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u8 *label, *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* The first byte is used for label, the rest for MIDI bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	label = port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	msg = port->buf + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	consume = snd_rawmidi_transmit_peek(substream, msg, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (consume == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* On exclusive message. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (port->on_sysex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		/* Seek the end of exclusives. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		for (i = 0; i < consume; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			if (msg[i] == 0xf7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				port->on_sysex = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		/* At the end of exclusive message, use label 0x07. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		if (!port->on_sysex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			consume = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			*label = (substream->number << 4) | 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		/* During exclusive message, use label 0x04. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		} else if (consume == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			*label = (substream->number << 4) | 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		/* We need to fill whole 3 bytes. Go to next change. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		len = consume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* The beginning of exclusives. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		if (msg[0] == 0xf0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			/* Transfer it in next chance in another condition. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			port->on_sysex = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			/* On running-status. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			if ((msg[0] & 0x80) != 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				status = port->running_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				status = msg[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			/* Calculate consume bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			len = calculate_message_bytes(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			if (len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			/* On running-status. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			if ((msg[0] & 0x80) != 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				/* Enough MIDI bytes were not retrieved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				if (consume < len - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				consume = len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				msg[2] = msg[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				msg[1] = msg[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				msg[0] = port->running_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				/* Enough MIDI bytes were not retrieved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				if (consume < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				consume = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				port->running_status = msg[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		*label = (substream->number << 4) | (msg[0] >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (len > 0 && len < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		memset(msg + len, 0, 3 - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return consume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void async_midi_port_callback(struct fw_card *card, int rcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				     void *data, size_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				     void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct snd_fw_async_midi_port *port = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct snd_rawmidi_substream *substream = READ_ONCE(port->substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* This port is closed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (substream == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (rcode == RCODE_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		snd_rawmidi_transmit_ack(substream, port->consume_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	else if (!rcode_is_permanent_error(rcode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/* To start next transaction immediately for recovery. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		port->next_ktime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		/* Don't continue processing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		port->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	port->idling = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!snd_rawmidi_transmit_empty(substream))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		schedule_work(&port->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void midi_port_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct snd_fw_async_midi_port *port =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			container_of(work, struct snd_fw_async_midi_port, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct snd_rawmidi_substream *substream = READ_ONCE(port->substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* Under transacting or error state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!port->idling || port->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	/* Nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (substream == NULL || snd_rawmidi_transmit_empty(substream))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Do it in next chance. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (ktime_after(port->next_ktime, ktime_get())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		schedule_work(&port->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * Fill the buffer. The callee must use snd_rawmidi_transmit_peek().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * Later, snd_rawmidi_transmit_ack() is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	memset(port->buf, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	port->consume_bytes = fill_message(port, substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (port->consume_bytes <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		/* Do it in next chance, immediately. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (port->consume_bytes == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			port->next_ktime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			schedule_work(&port->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			/* Fatal error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			port->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/* Set interval to next transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	port->next_ktime = ktime_add_ns(ktime_get(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			port->consume_bytes * 8 * (NSEC_PER_SEC / 31250));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	/* Start this transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	port->idling = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	 * In Linux FireWire core, when generation is updated with memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	 * barrier, node id has already been updated. In this module, After
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	 * this smp_rmb(), load/store instructions to memory are completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	 * Thus, both of generation and node id are available with recent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 * values. This is a light-serialization solution to handle bus reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 * events on IEEE 1394 bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	generation = port->parent->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	fw_send_request(port->parent->card, &port->transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			port->parent->node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			port->parent->max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			port->buf, 4, async_midi_port_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) void snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	port->idling = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	port->error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	port->running_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	port->on_sysex = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void handle_midi_tx(struct fw_card *card, struct fw_request *request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			   int tcode, int destination, int source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			   int generation, unsigned long long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			   void *data, size_t length, void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct snd_tscm *tscm = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	u32 *buf = (u32 *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	unsigned int messages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	unsigned int port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct snd_rawmidi_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	u8 *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	int bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (offset != tscm->async_handler.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	messages = length / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	for (i = 0; i < messages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		b = (u8 *)(buf + i * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		port = b[0] >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		/* TODO: support virtual MIDI ports. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		if (port >= tscm->spec->midi_capture_ports)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		/* Assume the message length. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		bytes = calculate_message_bytes(b[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		/* On MIDI data or exclusives. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (bytes <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			/* Seek the end of exclusives. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			for (bytes = 1; bytes < 4; bytes++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				if (b[bytes] == 0xf7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			if (bytes == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				bytes = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		substream = READ_ONCE(tscm->tx_midi_substreams[port]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		if (substream != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			snd_rawmidi_receive(substream, b + 1, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	fw_send_response(card, request, RCODE_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int snd_tscm_transaction_register(struct snd_tscm *tscm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	static const struct fw_address_region resp_register_region = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		.start	= 0xffffe0000000ull,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		.end	= 0xffffe000ffffull,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * Usually, two quadlets are transferred by one transaction. The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 * quadlet has MIDI messages, the rest includes timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * Sometimes, 8 set of the data is transferred by a block transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	tscm->async_handler.length = 8 * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	tscm->async_handler.address_callback = handle_midi_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	tscm->async_handler.callback_data = tscm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	err = fw_core_add_address_handler(&tscm->async_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 					  &resp_register_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	err = snd_tscm_transaction_reregister(tscm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		tscm->out_ports[i].parent = fw_parent_device(tscm->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		tscm->out_ports[i].next_ktime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		INIT_WORK(&tscm->out_ports[i].work, midi_port_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	fw_core_remove_address_handler(&tscm->async_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	tscm->async_handler.callback_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* At bus reset, these registers are cleared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int snd_tscm_transaction_reregister(struct snd_tscm *tscm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct fw_device *device = fw_parent_device(tscm->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	__be32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/* Register messaging address. Block transaction is not allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	reg = cpu_to_be32((device->card->node_id << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			  (tscm->async_handler.offset >> 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				 &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	reg = cpu_to_be32(tscm->async_handler.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 				 TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				 &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/* Turn on messaging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	reg = cpu_to_be32(0x00000001);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				  TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				  &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	/* Turn on FireWire LED. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	reg = cpu_to_be32(0x0001008e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				  TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 				  &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) void snd_tscm_transaction_unregister(struct snd_tscm *tscm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	__be32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (tscm->async_handler.callback_data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	/* Turn off FireWire LED. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	reg = cpu_to_be32(0x0000008e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			   TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			   &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* Turn off messaging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	reg = cpu_to_be32(0x00000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			   &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	/* Unregister the address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			   &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			   TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			   &reg, sizeof(reg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	fw_core_remove_address_handler(&tscm->async_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	tscm->async_handler.callback_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }