119174295STakashi Sakamoto /* 219174295STakashi Sakamoto * ff-transaction.c - a part of driver for RME Fireface series 319174295STakashi Sakamoto * 419174295STakashi Sakamoto * Copyright (c) 2015-2017 Takashi Sakamoto 519174295STakashi Sakamoto * 619174295STakashi Sakamoto * Licensed under the terms of the GNU General Public License, version 2. 719174295STakashi Sakamoto */ 819174295STakashi Sakamoto 919174295STakashi Sakamoto #include "ff.h" 1019174295STakashi Sakamoto 1119174295STakashi Sakamoto static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port, 1219174295STakashi Sakamoto int rcode) 1319174295STakashi Sakamoto { 1419174295STakashi Sakamoto struct snd_rawmidi_substream *substream = 156aa7de05SMark Rutland READ_ONCE(ff->rx_midi_substreams[port]); 1619174295STakashi Sakamoto 1719174295STakashi Sakamoto if (rcode_is_permanent_error(rcode)) { 1819174295STakashi Sakamoto ff->rx_midi_error[port] = true; 1919174295STakashi Sakamoto return; 2019174295STakashi Sakamoto } 2119174295STakashi Sakamoto 2219174295STakashi Sakamoto if (rcode != RCODE_COMPLETE) { 2319174295STakashi Sakamoto /* Transfer the message again, immediately. */ 24e3a973c6STakashi Sakamoto ff->next_ktime[port] = 0; 2519174295STakashi Sakamoto schedule_work(&ff->rx_midi_work[port]); 2619174295STakashi Sakamoto return; 2719174295STakashi Sakamoto } 2819174295STakashi Sakamoto 2919174295STakashi Sakamoto snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]); 3019174295STakashi Sakamoto ff->rx_bytes[port] = 0; 3119174295STakashi Sakamoto 3219174295STakashi Sakamoto if (!snd_rawmidi_transmit_empty(substream)) 3319174295STakashi Sakamoto schedule_work(&ff->rx_midi_work[port]); 3419174295STakashi Sakamoto } 3519174295STakashi Sakamoto 3619174295STakashi Sakamoto static void finish_transmit_midi0_msg(struct fw_card *card, int rcode, 3719174295STakashi Sakamoto void *data, size_t length, 3819174295STakashi Sakamoto void *callback_data) 3919174295STakashi Sakamoto { 4019174295STakashi Sakamoto struct snd_ff *ff = 4119174295STakashi Sakamoto container_of(callback_data, struct snd_ff, transactions[0]); 4219174295STakashi Sakamoto finish_transmit_midi_msg(ff, 0, rcode); 4319174295STakashi Sakamoto } 4419174295STakashi Sakamoto 4519174295STakashi Sakamoto static void finish_transmit_midi1_msg(struct fw_card *card, int rcode, 4619174295STakashi Sakamoto void *data, size_t length, 4719174295STakashi Sakamoto void *callback_data) 4819174295STakashi Sakamoto { 4919174295STakashi Sakamoto struct snd_ff *ff = 5019174295STakashi Sakamoto container_of(callback_data, struct snd_ff, transactions[1]); 5119174295STakashi Sakamoto finish_transmit_midi_msg(ff, 1, rcode); 5219174295STakashi Sakamoto } 5319174295STakashi Sakamoto 5419174295STakashi Sakamoto static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port, 5519174295STakashi Sakamoto unsigned int index, u8 byte) 5619174295STakashi Sakamoto { 5719174295STakashi Sakamoto ff->msg_buf[port][index] = cpu_to_le32(byte); 5819174295STakashi Sakamoto } 5919174295STakashi Sakamoto 6019174295STakashi Sakamoto static void transmit_midi_msg(struct snd_ff *ff, unsigned int port) 6119174295STakashi Sakamoto { 6219174295STakashi Sakamoto struct snd_rawmidi_substream *substream = 636aa7de05SMark Rutland READ_ONCE(ff->rx_midi_substreams[port]); 6419174295STakashi Sakamoto u8 *buf = (u8 *)ff->msg_buf[port]; 6519174295STakashi Sakamoto int i, len; 6619174295STakashi Sakamoto 6719174295STakashi Sakamoto struct fw_device *fw_dev = fw_parent_device(ff->unit); 6819174295STakashi Sakamoto unsigned long long addr; 6919174295STakashi Sakamoto int generation; 7019174295STakashi Sakamoto fw_transaction_callback_t callback; 7119174295STakashi Sakamoto 7219174295STakashi Sakamoto if (substream == NULL || snd_rawmidi_transmit_empty(substream)) 7319174295STakashi Sakamoto return; 7419174295STakashi Sakamoto 7519174295STakashi Sakamoto if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port]) 7619174295STakashi Sakamoto return; 7719174295STakashi Sakamoto 7819174295STakashi Sakamoto /* Do it in next chance. */ 7919174295STakashi Sakamoto if (ktime_after(ff->next_ktime[port], ktime_get())) { 8019174295STakashi Sakamoto schedule_work(&ff->rx_midi_work[port]); 8119174295STakashi Sakamoto return; 8219174295STakashi Sakamoto } 8319174295STakashi Sakamoto 8419174295STakashi Sakamoto len = snd_rawmidi_transmit_peek(substream, buf, 8519174295STakashi Sakamoto SND_FF_MAXIMIM_MIDI_QUADS); 8619174295STakashi Sakamoto if (len <= 0) 8719174295STakashi Sakamoto return; 8819174295STakashi Sakamoto 8919174295STakashi Sakamoto for (i = len - 1; i >= 0; i--) 9019174295STakashi Sakamoto fill_midi_buf(ff, port, i, buf[i]); 9119174295STakashi Sakamoto 9219174295STakashi Sakamoto if (port == 0) { 9319174295STakashi Sakamoto addr = ff->spec->protocol->midi_rx_port_0_reg; 9419174295STakashi Sakamoto callback = finish_transmit_midi0_msg; 9519174295STakashi Sakamoto } else { 9619174295STakashi Sakamoto addr = ff->spec->protocol->midi_rx_port_1_reg; 9719174295STakashi Sakamoto callback = finish_transmit_midi1_msg; 9819174295STakashi Sakamoto } 9919174295STakashi Sakamoto 10019174295STakashi Sakamoto /* Set interval to next transaction. */ 10119174295STakashi Sakamoto ff->next_ktime[port] = ktime_add_ns(ktime_get(), 10219174295STakashi Sakamoto len * 8 * NSEC_PER_SEC / 31250); 10319174295STakashi Sakamoto ff->rx_bytes[port] = len; 10419174295STakashi Sakamoto 10519174295STakashi Sakamoto /* 10619174295STakashi Sakamoto * In Linux FireWire core, when generation is updated with memory 10719174295STakashi Sakamoto * barrier, node id has already been updated. In this module, After 10819174295STakashi Sakamoto * this smp_rmb(), load/store instructions to memory are completed. 10919174295STakashi Sakamoto * Thus, both of generation and node id are available with recent 11019174295STakashi Sakamoto * values. This is a light-serialization solution to handle bus reset 11119174295STakashi Sakamoto * events on IEEE 1394 bus. 11219174295STakashi Sakamoto */ 11319174295STakashi Sakamoto generation = fw_dev->generation; 11419174295STakashi Sakamoto smp_rmb(); 11519174295STakashi Sakamoto fw_send_request(fw_dev->card, &ff->transactions[port], 11619174295STakashi Sakamoto TCODE_WRITE_BLOCK_REQUEST, 11719174295STakashi Sakamoto fw_dev->node_id, generation, fw_dev->max_speed, 11819174295STakashi Sakamoto addr, &ff->msg_buf[port], len * 4, 11919174295STakashi Sakamoto callback, &ff->transactions[port]); 12019174295STakashi Sakamoto } 12119174295STakashi Sakamoto 12219174295STakashi Sakamoto static void transmit_midi0_msg(struct work_struct *work) 12319174295STakashi Sakamoto { 12419174295STakashi Sakamoto struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]); 12519174295STakashi Sakamoto 12619174295STakashi Sakamoto transmit_midi_msg(ff, 0); 12719174295STakashi Sakamoto } 12819174295STakashi Sakamoto 12919174295STakashi Sakamoto static void transmit_midi1_msg(struct work_struct *work) 13019174295STakashi Sakamoto { 13119174295STakashi Sakamoto struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]); 13219174295STakashi Sakamoto 13319174295STakashi Sakamoto transmit_midi_msg(ff, 1); 13419174295STakashi Sakamoto } 13519174295STakashi Sakamoto 13619174295STakashi Sakamoto static void handle_midi_msg(struct fw_card *card, struct fw_request *request, 13719174295STakashi Sakamoto int tcode, int destination, int source, 13819174295STakashi Sakamoto int generation, unsigned long long offset, 13919174295STakashi Sakamoto void *data, size_t length, void *callback_data) 14019174295STakashi Sakamoto { 14119174295STakashi Sakamoto struct snd_ff *ff = callback_data; 14219174295STakashi Sakamoto __le32 *buf = data; 14319174295STakashi Sakamoto u32 quad; 14419174295STakashi Sakamoto u8 byte; 14519174295STakashi Sakamoto unsigned int index; 14619174295STakashi Sakamoto struct snd_rawmidi_substream *substream; 14719174295STakashi Sakamoto int i; 14819174295STakashi Sakamoto 14919174295STakashi Sakamoto fw_send_response(card, request, RCODE_COMPLETE); 15019174295STakashi Sakamoto 15119174295STakashi Sakamoto for (i = 0; i < length / 4; i++) { 15219174295STakashi Sakamoto quad = le32_to_cpu(buf[i]); 15319174295STakashi Sakamoto 15419174295STakashi Sakamoto /* Message in first port. */ 15519174295STakashi Sakamoto /* 15619174295STakashi Sakamoto * This value may represent the index of this unit when the same 15719174295STakashi Sakamoto * units are on the same IEEE 1394 bus. This driver doesn't use 15819174295STakashi Sakamoto * it. 15919174295STakashi Sakamoto */ 16019174295STakashi Sakamoto index = (quad >> 8) & 0xff; 16119174295STakashi Sakamoto if (index > 0) { 1626aa7de05SMark Rutland substream = READ_ONCE(ff->tx_midi_substreams[0]); 16319174295STakashi Sakamoto if (substream != NULL) { 16419174295STakashi Sakamoto byte = quad & 0xff; 16519174295STakashi Sakamoto snd_rawmidi_receive(substream, &byte, 1); 16619174295STakashi Sakamoto } 16719174295STakashi Sakamoto } 16819174295STakashi Sakamoto 16919174295STakashi Sakamoto /* Message in second port. */ 17019174295STakashi Sakamoto index = (quad >> 24) & 0xff; 17119174295STakashi Sakamoto if (index > 0) { 1726aa7de05SMark Rutland substream = READ_ONCE(ff->tx_midi_substreams[1]); 17319174295STakashi Sakamoto if (substream != NULL) { 17419174295STakashi Sakamoto byte = (quad >> 16) & 0xff; 17519174295STakashi Sakamoto snd_rawmidi_receive(substream, &byte, 1); 17619174295STakashi Sakamoto } 17719174295STakashi Sakamoto } 17819174295STakashi Sakamoto } 17919174295STakashi Sakamoto } 18019174295STakashi Sakamoto 18119174295STakashi Sakamoto static int allocate_own_address(struct snd_ff *ff, int i) 18219174295STakashi Sakamoto { 18319174295STakashi Sakamoto struct fw_address_region midi_msg_region; 18419174295STakashi Sakamoto int err; 18519174295STakashi Sakamoto 18619174295STakashi Sakamoto ff->async_handler.length = SND_FF_MAXIMIM_MIDI_QUADS * 4; 18719174295STakashi Sakamoto ff->async_handler.address_callback = handle_midi_msg; 18819174295STakashi Sakamoto ff->async_handler.callback_data = ff; 18919174295STakashi Sakamoto 19019174295STakashi Sakamoto midi_msg_region.start = 0x000100000000ull * i; 19119174295STakashi Sakamoto midi_msg_region.end = midi_msg_region.start + ff->async_handler.length; 19219174295STakashi Sakamoto 19319174295STakashi Sakamoto err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region); 19419174295STakashi Sakamoto if (err >= 0) { 19519174295STakashi Sakamoto /* Controllers are allowed to register this region. */ 19619174295STakashi Sakamoto if (ff->async_handler.offset & 0x0000ffffffff) { 19719174295STakashi Sakamoto fw_core_remove_address_handler(&ff->async_handler); 19819174295STakashi Sakamoto err = -EAGAIN; 19919174295STakashi Sakamoto } 20019174295STakashi Sakamoto } 20119174295STakashi Sakamoto 20219174295STakashi Sakamoto return err; 20319174295STakashi Sakamoto } 20419174295STakashi Sakamoto 20519174295STakashi Sakamoto /* 20619174295STakashi Sakamoto * The configuration to start asynchronous transactions for MIDI messages is in 20719174295STakashi Sakamoto * 0x'0000'8010'051c. This register includes the other options, thus this driver 20819174295STakashi Sakamoto * doesn't touch it and leaves the decision to userspace. The userspace MUST add 20919174295STakashi Sakamoto * 0x04000000 to write transactions to the register to receive any MIDI 21019174295STakashi Sakamoto * messages. 21119174295STakashi Sakamoto * 21219174295STakashi Sakamoto * Here, I just describe MIDI-related offsets of the register, in little-endian 21319174295STakashi Sakamoto * order. 21419174295STakashi Sakamoto * 21519174295STakashi Sakamoto * Controllers are allowed to register higher 4 bytes of address to receive 21619174295STakashi Sakamoto * the transactions. The register is 0x'0000'8010'03f4. On the other hand, the 21719174295STakashi Sakamoto * controllers are not allowed to register lower 4 bytes of the address. They 21819174295STakashi Sakamoto * are forced to select from 4 options by writing corresponding bits to 21919174295STakashi Sakamoto * 0x'0000'8010'051c. 22019174295STakashi Sakamoto * 22119174295STakashi Sakamoto * The 3rd-6th bits in MSB of this register are used to indicate lower 4 bytes 22219174295STakashi Sakamoto * of address to which the device transferrs the transactions. 22319174295STakashi Sakamoto * - 6th: 0x'....'....'0000'0180 22419174295STakashi Sakamoto * - 5th: 0x'....'....'0000'0100 22519174295STakashi Sakamoto * - 4th: 0x'....'....'0000'0080 22619174295STakashi Sakamoto * - 3rd: 0x'....'....'0000'0000 22719174295STakashi Sakamoto * 22819174295STakashi Sakamoto * This driver configure 0x'....'....'0000'0000 for units to receive MIDI 22919174295STakashi Sakamoto * messages. 3rd bit of the register should be configured, however this driver 23019174295STakashi Sakamoto * deligates this task to user space applications due to a restriction that 23119174295STakashi Sakamoto * this register is write-only and the other bits have own effects. 23219174295STakashi Sakamoto * 23319174295STakashi Sakamoto * The 1st and 2nd bits in LSB of this register are used to cancel transferring 23419174295STakashi Sakamoto * asynchronous transactions. These two bits have the same effect. 23519174295STakashi Sakamoto * - 1st/2nd: cancel transferring 23619174295STakashi Sakamoto */ 23719174295STakashi Sakamoto int snd_ff_transaction_reregister(struct snd_ff *ff) 23819174295STakashi Sakamoto { 23919174295STakashi Sakamoto struct fw_card *fw_card = fw_parent_device(ff->unit)->card; 24019174295STakashi Sakamoto u32 addr; 24119174295STakashi Sakamoto __le32 reg; 24219174295STakashi Sakamoto 24319174295STakashi Sakamoto /* 24419174295STakashi Sakamoto * Controllers are allowed to register its node ID and upper 2 byte of 24519174295STakashi Sakamoto * local address to listen asynchronous transactions. 24619174295STakashi Sakamoto */ 24719174295STakashi Sakamoto addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32); 24819174295STakashi Sakamoto reg = cpu_to_le32(addr); 24919174295STakashi Sakamoto return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 25019174295STakashi Sakamoto ff->spec->protocol->midi_high_addr_reg, 25119174295STakashi Sakamoto ®, sizeof(reg), 0); 25219174295STakashi Sakamoto } 25319174295STakashi Sakamoto 25419174295STakashi Sakamoto int snd_ff_transaction_register(struct snd_ff *ff) 25519174295STakashi Sakamoto { 25619174295STakashi Sakamoto int i, err; 25719174295STakashi Sakamoto 25819174295STakashi Sakamoto /* 25919174295STakashi Sakamoto * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should 26019174295STakashi Sakamoto * be zero due to device specification. 26119174295STakashi Sakamoto */ 26219174295STakashi Sakamoto for (i = 0; i < 0xffff; i++) { 26319174295STakashi Sakamoto err = allocate_own_address(ff, i); 26419174295STakashi Sakamoto if (err != -EBUSY && err != -EAGAIN) 26519174295STakashi Sakamoto break; 26619174295STakashi Sakamoto } 26719174295STakashi Sakamoto if (err < 0) 26819174295STakashi Sakamoto return err; 26919174295STakashi Sakamoto 27019174295STakashi Sakamoto err = snd_ff_transaction_reregister(ff); 27119174295STakashi Sakamoto if (err < 0) 27219174295STakashi Sakamoto return err; 27319174295STakashi Sakamoto 27419174295STakashi Sakamoto INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg); 27519174295STakashi Sakamoto INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg); 27619174295STakashi Sakamoto 27719174295STakashi Sakamoto return 0; 27819174295STakashi Sakamoto } 27919174295STakashi Sakamoto 28019174295STakashi Sakamoto void snd_ff_transaction_unregister(struct snd_ff *ff) 28119174295STakashi Sakamoto { 28219174295STakashi Sakamoto __le32 reg; 28319174295STakashi Sakamoto 28419174295STakashi Sakamoto if (ff->async_handler.callback_data == NULL) 28519174295STakashi Sakamoto return; 28619174295STakashi Sakamoto ff->async_handler.callback_data = NULL; 28719174295STakashi Sakamoto 28819174295STakashi Sakamoto /* Release higher 4 bytes of address. */ 28919174295STakashi Sakamoto reg = cpu_to_le32(0x00000000); 29019174295STakashi Sakamoto snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 29119174295STakashi Sakamoto ff->spec->protocol->midi_high_addr_reg, 29219174295STakashi Sakamoto ®, sizeof(reg), 0); 29319174295STakashi Sakamoto 29419174295STakashi Sakamoto fw_core_remove_address_handler(&ff->async_handler); 29519174295STakashi Sakamoto } 296