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)  * oxfw-scs1x.c - a part of driver for OXFW970/971 based devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "oxfw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define HSS1394_ADDRESS			0xc007dedadadaULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define HSS1394_MAX_PACKET_SIZE		64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define HSS1394_TAG_USER_DATA		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define HSS1394_TAG_CHANGE_ADDRESS	0xf1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct fw_scs1x {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct fw_address_handler hss_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u8 input_escape_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct snd_rawmidi_substream *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	/* For MIDI playback. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct snd_rawmidi_substream *output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	bool output_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	u8 output_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u8 output_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	bool output_escaped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	bool output_escape_high_nibble;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	wait_queue_head_t idle_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u8 buffer[HSS1394_MAX_PACKET_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	bool transaction_running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct fw_transaction transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned int transaction_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	bool error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct fw_device *fw_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static const u8 sysex_escape_prefix[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	0xf0,			/* SysEx begin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	0x00, 0x01, 0x60,	/* Stanton DJ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	0x48, 0x53, 0x53,	/* "HSS" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static void midi_input_escaped_byte(struct snd_rawmidi_substream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 				    u8 byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u8 nibbles[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	nibbles[0] = byte >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	nibbles[1] = byte & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	snd_rawmidi_receive(stream, nibbles, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void midi_input_byte(struct fw_scs1x *scs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			    struct snd_rawmidi_substream *stream, u8 byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	const u8 eox = 0xf7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (scs->input_escape_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		midi_input_escaped_byte(stream, byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		scs->input_escape_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (scs->input_escape_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			snd_rawmidi_receive(stream, &eox, sizeof(eox));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	} else if (byte == 0xf9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		snd_rawmidi_receive(stream, sysex_escape_prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				    ARRAY_SIZE(sysex_escape_prefix));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		midi_input_escaped_byte(stream, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		midi_input_escaped_byte(stream, 0xf9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		scs->input_escape_count = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		snd_rawmidi_receive(stream, &byte, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static void midi_input_packet(struct fw_scs1x *scs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			      struct snd_rawmidi_substream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			      const u8 *data, unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	const u8 eox = 0xf7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (data[0] == HSS1394_TAG_USER_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		for (i = 1; i < bytes; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			midi_input_byte(scs, stream, data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		snd_rawmidi_receive(stream, sysex_escape_prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				    ARRAY_SIZE(sysex_escape_prefix));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		for (i = 0; i < bytes; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			midi_input_escaped_byte(stream, data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		snd_rawmidi_receive(stream, &eox, sizeof(eox));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static void handle_hss(struct fw_card *card, struct fw_request *request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		       int tcode, int destination, int source, int generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		       unsigned long long offset, void *data, size_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		       void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct fw_scs1x *scs = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct snd_rawmidi_substream *stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int rcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (offset != scs->hss_handler.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		rcode = RCODE_ADDRESS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	    tcode != TCODE_WRITE_BLOCK_REQUEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		rcode = RCODE_TYPE_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (length >= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		stream = READ_ONCE(scs->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			midi_input_packet(scs, stream, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	rcode = RCODE_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	fw_send_response(card, request, rcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static void scs_write_callback(struct fw_card *card, int rcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			       void *data, size_t length, void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct fw_scs1x *scs = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (!rcode_is_permanent_error(rcode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		/* Don't retry for this data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (rcode == RCODE_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			scs->transaction_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		scs->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	scs->transaction_running = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	schedule_work(&scs->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static bool is_valid_running_status(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return status >= 0x80 && status <= 0xef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static bool is_one_byte_cmd(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return status == 0xf6 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	       status >= 0xf8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static bool is_two_bytes_cmd(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return (status >= 0xc0 && status <= 0xdf) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	       status == 0xf1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	       status == 0xf3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static bool is_three_bytes_cmd(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return (status >= 0x80 && status <= 0xbf) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	       (status >= 0xe0 && status <= 0xef) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	       status == 0xf2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static bool is_invalid_cmd(u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return status == 0xf4 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	       status == 0xf5 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	       status == 0xf9 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	       status == 0xfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static void scs_output_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct fw_scs1x *scs = container_of(work, struct fw_scs1x, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct snd_rawmidi_substream *stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	u8 byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	int generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (scs->transaction_running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	stream = READ_ONCE(scs->output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!stream || scs->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		scs->output_idle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		wake_up(&scs->idle_wait);
^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) 	if (scs->transaction_bytes > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	i = scs->output_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (snd_rawmidi_transmit(stream, &byte, 1) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			scs->output_bytes = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			scs->output_idle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			wake_up(&scs->idle_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		 * Convert from real MIDI to what I think the device expects (no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		 * running status, one command per packet, unescaped SysExs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (scs->output_escaped && byte < 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			if (scs->output_escape_high_nibble) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				if (i < HSS1394_MAX_PACKET_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 					scs->buffer[i] = byte << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 					scs->output_escape_high_nibble = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				scs->buffer[i++] |= byte & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				scs->output_escape_high_nibble = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		} else if (byte < 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			if (i == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				if (!is_valid_running_status(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 							scs->output_status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				scs->buffer[0] = HSS1394_TAG_USER_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 				scs->buffer[i++] = scs->output_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			scs->buffer[i++] = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			if ((i == 3 && is_two_bytes_cmd(scs->output_status)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			    (i == 4 && is_three_bytes_cmd(scs->output_status)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			if (i == 1 + ARRAY_SIZE(sysex_escape_prefix) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			    !memcmp(scs->buffer + 1, sysex_escape_prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				    ARRAY_SIZE(sysex_escape_prefix))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				scs->output_escaped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				scs->output_escape_high_nibble = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (i >= HSS1394_MAX_PACKET_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		} else if (byte == 0xf7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			if (scs->output_escaped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				if (i >= 1 && scs->output_escape_high_nibble &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				    scs->buffer[0] !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 						HSS1394_TAG_CHANGE_ADDRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				if (i > 1 && scs->output_status == 0xf0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					scs->buffer[i++] = 0xf7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			scs->output_escaped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		} else if (!is_invalid_cmd(byte) && byte < 0xf8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			scs->buffer[0] = HSS1394_TAG_USER_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			scs->buffer[i++] = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			scs->output_status = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			scs->output_escaped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			if (is_one_byte_cmd(byte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	scs->output_bytes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	scs->output_escaped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	scs->transaction_bytes = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	scs->transaction_running = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	generation = scs->fw_dev->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	smp_rmb(); /* node_id vs. generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	fw_send_request(scs->fw_dev->card, &scs->transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			TCODE_WRITE_BLOCK_REQUEST, scs->fw_dev->node_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			generation, scs->fw_dev->max_speed, HSS1394_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			scs->buffer, scs->transaction_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			scs_write_callback, scs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int midi_capture_open(struct snd_rawmidi_substream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int midi_capture_close(struct snd_rawmidi_substream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static void midi_capture_trigger(struct snd_rawmidi_substream *stream, int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct fw_scs1x *scs = stream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		scs->input_escape_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		WRITE_ONCE(scs->input, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		WRITE_ONCE(scs->input, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int midi_playback_open(struct snd_rawmidi_substream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int midi_playback_close(struct snd_rawmidi_substream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static void midi_playback_trigger(struct snd_rawmidi_substream *stream, int up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct fw_scs1x *scs = stream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		scs->output_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		scs->output_bytes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		scs->output_escaped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		scs->output_idle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		scs->transaction_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		scs->error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		WRITE_ONCE(scs->output, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		schedule_work(&scs->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		WRITE_ONCE(scs->output, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void midi_playback_drain(struct snd_rawmidi_substream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct fw_scs1x *scs = stream->rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	wait_event(scs->idle_wait, scs->output_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int register_address(struct snd_oxfw *oxfw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct fw_scs1x *scs = oxfw->spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	__be64 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	data = cpu_to_be64(((u64)HSS1394_TAG_CHANGE_ADDRESS << 56) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			    scs->hss_handler.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return snd_fw_transaction(oxfw->unit, TCODE_WRITE_BLOCK_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				  HSS1394_ADDRESS, &data, sizeof(data), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static void remove_scs1x(struct snd_rawmidi *rmidi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct fw_scs1x *scs = rmidi->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	fw_core_remove_address_handler(&scs->hss_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) void snd_oxfw_scs1x_update(struct snd_oxfw *oxfw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	register_address(oxfw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int snd_oxfw_scs1x_add(struct snd_oxfw *oxfw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	static const struct snd_rawmidi_ops midi_capture_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		.open    = midi_capture_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		.close   = midi_capture_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.trigger = midi_capture_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	static const struct snd_rawmidi_ops midi_playback_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		.open    = midi_playback_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		.close   = midi_playback_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		.trigger = midi_playback_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.drain   = midi_playback_drain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct snd_rawmidi *rmidi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct fw_scs1x *scs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	scs = devm_kzalloc(&oxfw->card->card_dev, sizeof(struct fw_scs1x),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (!scs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	scs->fw_dev = fw_parent_device(oxfw->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	oxfw->spec = scs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	/* Allocate own handler for imcoming asynchronous transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	scs->hss_handler.length = HSS1394_MAX_PACKET_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	scs->hss_handler.address_callback = handle_hss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	scs->hss_handler.callback_data = scs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	err = fw_core_add_address_handler(&scs->hss_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 					  &fw_high_memory_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	err = register_address(oxfw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		goto err_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	/* Use unique name for backward compatibility to scs1x module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	err = snd_rawmidi_new(oxfw->card, "SCS.1x", 0, 1, 1, &rmidi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto err_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	rmidi->private_data = scs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	rmidi->private_free = remove_scs1x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	snprintf(rmidi->name, sizeof(rmidi->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		 "%s MIDI", oxfw->card->shortname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			    SNDRV_RAWMIDI_INFO_OUTPUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			    SNDRV_RAWMIDI_INFO_DUPLEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			    &midi_capture_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			    &midi_playback_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	INIT_WORK(&scs->work, scs_output_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	init_waitqueue_head(&scs->idle_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	scs->output_idle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) err_allocated:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	fw_core_remove_address_handler(&scs->hss_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }