Path: blob/master/sound/firewire/tascam/tascam-transaction.c
26424 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* tascam-transaction.c - a part of driver for TASCAM FireWire series3*4* Copyright (c) 2015 Takashi Sakamoto5*/67#include "tascam.h"89/*10* When return minus value, given argument is not MIDI status.11* When return 0, given argument is a beginning of system exclusive.12* When return the others, given argument is MIDI data.13*/14static inline int calculate_message_bytes(u8 status)15{16switch (status) {17case 0xf6: /* Tune request. */18case 0xf8: /* Timing clock. */19case 0xfa: /* Start. */20case 0xfb: /* Continue. */21case 0xfc: /* Stop. */22case 0xfe: /* Active sensing. */23case 0xff: /* System reset. */24return 1;25case 0xf1: /* MIDI time code quarter frame. */26case 0xf3: /* Song select. */27return 2;28case 0xf2: /* Song position pointer. */29return 3;30case 0xf0: /* Exclusive. */31return 0;32case 0xf7: /* End of exclusive. */33break;34case 0xf4: /* Undefined. */35case 0xf5: /* Undefined. */36case 0xf9: /* Undefined. */37case 0xfd: /* Undefined. */38break;39default:40switch (status & 0xf0) {41case 0x80: /* Note on. */42case 0x90: /* Note off. */43case 0xa0: /* Polyphonic key pressure. */44case 0xb0: /* Control change and Mode change. */45case 0xe0: /* Pitch bend change. */46return 3;47case 0xc0: /* Program change. */48case 0xd0: /* Channel pressure. */49return 2;50default:51break;52}53break;54}5556return -EINVAL;57}5859static int fill_message(struct snd_fw_async_midi_port *port,60struct snd_rawmidi_substream *substream)61{62int i, len, consume;63u8 *label, *msg;64u8 status;6566/* The first byte is used for label, the rest for MIDI bytes. */67label = port->buf;68msg = port->buf + 1;6970consume = snd_rawmidi_transmit_peek(substream, msg, 3);71if (consume == 0)72return 0;7374/* On exclusive message. */75if (port->on_sysex) {76/* Seek the end of exclusives. */77for (i = 0; i < consume; ++i) {78if (msg[i] == 0xf7) {79port->on_sysex = false;80break;81}82}8384/* At the end of exclusive message, use label 0x07. */85if (!port->on_sysex) {86consume = i + 1;87*label = (substream->number << 4) | 0x07;88/* During exclusive message, use label 0x04. */89} else if (consume == 3) {90*label = (substream->number << 4) | 0x04;91/* We need to fill whole 3 bytes. Go to next change. */92} else {93return 0;94}9596len = consume;97} else {98/* The beginning of exclusives. */99if (msg[0] == 0xf0) {100/* Transfer it in next chance in another condition. */101port->on_sysex = true;102return 0;103} else {104/* On running-status. */105if ((msg[0] & 0x80) != 0x80)106status = port->running_status;107else108status = msg[0];109110/* Calculate consume bytes. */111len = calculate_message_bytes(status);112if (len <= 0)113return 0;114115/* On running-status. */116if ((msg[0] & 0x80) != 0x80) {117/* Enough MIDI bytes were not retrieved. */118if (consume < len - 1)119return 0;120consume = len - 1;121122msg[2] = msg[1];123msg[1] = msg[0];124msg[0] = port->running_status;125} else {126/* Enough MIDI bytes were not retrieved. */127if (consume < len)128return 0;129consume = len;130131port->running_status = msg[0];132}133}134135*label = (substream->number << 4) | (msg[0] >> 4);136}137138if (len > 0 && len < 3)139memset(msg + len, 0, 3 - len);140141return consume;142}143144static void async_midi_port_callback(struct fw_card *card, int rcode,145void *data, size_t length,146void *callback_data)147{148struct snd_fw_async_midi_port *port = callback_data;149struct snd_rawmidi_substream *substream = READ_ONCE(port->substream);150151/* This port is closed. */152if (substream == NULL)153return;154155if (rcode == RCODE_COMPLETE)156snd_rawmidi_transmit_ack(substream, port->consume_bytes);157else if (!rcode_is_permanent_error(rcode))158/* To start next transaction immediately for recovery. */159port->next_ktime = 0;160else161/* Don't continue processing. */162port->error = true;163164port->idling = true;165166if (!snd_rawmidi_transmit_empty(substream))167schedule_work(&port->work);168}169170static void midi_port_work(struct work_struct *work)171{172struct snd_fw_async_midi_port *port =173container_of(work, struct snd_fw_async_midi_port, work);174struct snd_rawmidi_substream *substream = READ_ONCE(port->substream);175int generation;176177/* Under transacting or error state. */178if (!port->idling || port->error)179return;180181/* Nothing to do. */182if (substream == NULL || snd_rawmidi_transmit_empty(substream))183return;184185/* Do it in next chance. */186if (ktime_after(port->next_ktime, ktime_get())) {187schedule_work(&port->work);188return;189}190191/*192* Fill the buffer. The callee must use snd_rawmidi_transmit_peek().193* Later, snd_rawmidi_transmit_ack() is called.194*/195memset(port->buf, 0, 4);196port->consume_bytes = fill_message(port, substream);197if (port->consume_bytes <= 0) {198/* Do it in next chance, immediately. */199if (port->consume_bytes == 0) {200port->next_ktime = 0;201schedule_work(&port->work);202} else {203/* Fatal error. */204port->error = true;205}206return;207}208209/* Set interval to next transaction. */210port->next_ktime = ktime_add_ns(ktime_get(),211port->consume_bytes * 8 * (NSEC_PER_SEC / 31250));212213/* Start this transaction. */214port->idling = false;215216/*217* In Linux FireWire core, when generation is updated with memory218* barrier, node id has already been updated. In this module, After219* this smp_rmb(), load/store instructions to memory are completed.220* Thus, both of generation and node id are available with recent221* values. This is a light-serialization solution to handle bus reset222* events on IEEE 1394 bus.223*/224generation = port->parent->generation;225smp_rmb();226227fw_send_request(port->parent->card, &port->transaction,228TCODE_WRITE_QUADLET_REQUEST,229port->parent->node_id, generation,230port->parent->max_speed,231TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_RX_QUAD,232port->buf, 4, async_midi_port_callback,233port);234}235236void snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port)237{238port->idling = true;239port->error = false;240port->running_status = 0;241port->on_sysex = false;242}243244static void handle_midi_tx(struct fw_card *card, struct fw_request *request,245int tcode, int destination, int source,246int generation, unsigned long long offset,247void *data, size_t length, void *callback_data)248{249struct snd_tscm *tscm = callback_data;250u32 *buf = (u32 *)data;251unsigned int messages;252unsigned int i;253unsigned int port;254struct snd_rawmidi_substream *substream;255u8 *b;256int bytes;257258if (offset != tscm->async_handler.offset)259goto end;260261messages = length / 8;262for (i = 0; i < messages; i++) {263b = (u8 *)(buf + i * 2);264265port = b[0] >> 4;266/* TODO: support virtual MIDI ports. */267if (port >= tscm->spec->midi_capture_ports)268goto end;269270/* Assume the message length. */271bytes = calculate_message_bytes(b[1]);272/* On MIDI data or exclusives. */273if (bytes <= 0) {274/* Seek the end of exclusives. */275for (bytes = 1; bytes < 4; bytes++) {276if (b[bytes] == 0xf7)277break;278}279if (bytes == 4)280bytes = 3;281}282283substream = READ_ONCE(tscm->tx_midi_substreams[port]);284if (substream != NULL)285snd_rawmidi_receive(substream, b + 1, bytes);286}287end:288fw_send_response(card, request, RCODE_COMPLETE);289}290291int snd_tscm_transaction_register(struct snd_tscm *tscm)292{293static const struct fw_address_region resp_register_region = {294.start = 0xffffe0000000ull,295.end = 0xffffe000ffffull,296};297unsigned int i;298int err;299300/*301* Usually, two quadlets are transferred by one transaction. The first302* quadlet has MIDI messages, the rest includes timestamp.303* Sometimes, 8 set of the data is transferred by a block transaction.304*/305tscm->async_handler.length = 8 * 8;306tscm->async_handler.address_callback = handle_midi_tx;307tscm->async_handler.callback_data = tscm;308309err = fw_core_add_address_handler(&tscm->async_handler,310&resp_register_region);311if (err < 0)312return err;313314err = snd_tscm_transaction_reregister(tscm);315if (err < 0)316goto error;317318for (i = 0; i < TSCM_MIDI_OUT_PORT_MAX; i++) {319tscm->out_ports[i].parent = fw_parent_device(tscm->unit);320tscm->out_ports[i].next_ktime = 0;321INIT_WORK(&tscm->out_ports[i].work, midi_port_work);322}323324return err;325error:326fw_core_remove_address_handler(&tscm->async_handler);327tscm->async_handler.callback_data = NULL;328return err;329}330331/* At bus reset, these registers are cleared. */332int snd_tscm_transaction_reregister(struct snd_tscm *tscm)333{334struct fw_device *device = fw_parent_device(tscm->unit);335__be32 reg;336int err;337338/* Register messaging address. Block transaction is not allowed. */339reg = cpu_to_be32((device->card->node_id << 16) |340(tscm->async_handler.offset >> 32));341err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,342TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,343®, sizeof(reg), 0);344if (err < 0)345return err;346347reg = cpu_to_be32(tscm->async_handler.offset);348err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,349TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,350®, sizeof(reg), 0);351if (err < 0)352return err;353354/* Turn on messaging. */355reg = cpu_to_be32(0x00000001);356err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,357TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,358®, sizeof(reg), 0);359if (err < 0)360return err;361362/* Turn on FireWire LED. */363reg = cpu_to_be32(0x0001008e);364return snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,365TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,366®, sizeof(reg), 0);367}368369void snd_tscm_transaction_unregister(struct snd_tscm *tscm)370{371__be32 reg;372373if (tscm->async_handler.callback_data == NULL)374return;375376/* Turn off FireWire LED. */377reg = cpu_to_be32(0x0000008e);378snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,379TSCM_ADDR_BASE + TSCM_OFFSET_LED_POWER,380®, sizeof(reg), 0);381382/* Turn off messaging. */383reg = cpu_to_be32(0x00000000);384snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,385TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ON,386®, sizeof(reg), 0);387388/* Unregister the address. */389snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,390TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_HI,391®, sizeof(reg), 0);392snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,393TSCM_ADDR_BASE + TSCM_OFFSET_MIDI_TX_ADDR_LO,394®, sizeof(reg), 0);395396fw_core_remove_address_handler(&tscm->async_handler);397tscm->async_handler.callback_data = NULL;398}399400401