Thomas Gleixner | da607e1 | 2019-05-29 16:57:59 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 2 | /* |
| 3 | * tascam.h - a part of driver for TASCAM FireWire series |
| 4 | * |
| 5 | * Copyright (c) 2015 Takashi Sakamoto |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SOUND_TASCAM_H_INCLUDED |
| 9 | #define SOUND_TASCAM_H_INCLUDED |
| 10 | |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/firewire.h> |
| 13 | #include <linux/firewire-constants.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mod_devicetable.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/compat.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 20 | |
| 21 | #include <sound/core.h> |
| 22 | #include <sound/initval.h> |
Takashi Sakamoto | 96e5fbb | 2015-10-01 22:02:13 +0900 | [diff] [blame] | 23 | #include <sound/info.h> |
Takashi Sakamoto | 47faeea | 2015-10-01 22:02:14 +0900 | [diff] [blame] | 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 26 | #include <sound/firewire.h> |
| 27 | #include <sound/hwdep.h> |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 28 | #include <sound/rawmidi.h> |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 29 | |
| 30 | #include "../lib.h" |
Takashi Sakamoto | 47faeea | 2015-10-01 22:02:14 +0900 | [diff] [blame] | 31 | #include "../amdtp-stream.h" |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 32 | #include "../iso-resources.h" |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 33 | |
Takashi Sakamoto | 6f81ba1 | 2015-10-01 22:02:12 +0900 | [diff] [blame] | 34 | struct snd_tscm_spec { |
| 35 | const char *const name; |
| 36 | bool has_adat; |
| 37 | bool has_spdif; |
| 38 | unsigned int pcm_capture_analog_channels; |
| 39 | unsigned int pcm_playback_analog_channels; |
| 40 | unsigned int midi_capture_ports; |
| 41 | unsigned int midi_playback_ports; |
Takashi Sakamoto | 6f81ba1 | 2015-10-01 22:02:12 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 44 | #define TSCM_MIDI_IN_PORT_MAX 4 |
| 45 | #define TSCM_MIDI_OUT_PORT_MAX 4 |
| 46 | |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 47 | struct snd_fw_async_midi_port { |
| 48 | struct fw_device *parent; |
| 49 | struct work_struct work; |
| 50 | bool idling; |
| 51 | ktime_t next_ktime; |
| 52 | bool error; |
| 53 | |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 54 | struct fw_transaction transaction; |
| 55 | |
Takashi Sakamoto | 98a00d3 | 2017-04-14 16:46:29 +0900 | [diff] [blame] | 56 | u8 buf[4]; |
Takashi Sakamoto | 1f94205 | 2017-04-14 16:46:31 +0900 | [diff] [blame] | 57 | u8 running_status; |
| 58 | bool on_sysex; |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 59 | |
| 60 | struct snd_rawmidi_substream *substream; |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 61 | int consume_bytes; |
| 62 | }; |
| 63 | |
Takashi Sakamoto | d716742 | 2018-11-23 13:13:05 +0900 | [diff] [blame] | 64 | #define SND_TSCM_QUEUE_COUNT 16 |
| 65 | |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 66 | struct snd_tscm { |
| 67 | struct snd_card *card; |
| 68 | struct fw_unit *unit; |
| 69 | |
| 70 | struct mutex mutex; |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 71 | spinlock_t lock; |
Takashi Sakamoto | 6f81ba1 | 2015-10-01 22:02:12 +0900 | [diff] [blame] | 72 | |
| 73 | const struct snd_tscm_spec *spec; |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 74 | |
| 75 | struct fw_iso_resources tx_resources; |
| 76 | struct fw_iso_resources rx_resources; |
| 77 | struct amdtp_stream tx_stream; |
| 78 | struct amdtp_stream rx_stream; |
| 79 | unsigned int substreams_counter; |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 80 | |
| 81 | int dev_lock_count; |
| 82 | bool dev_lock_changed; |
| 83 | wait_queue_head_t hwdep_wait; |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 84 | |
| 85 | /* For MIDI message incoming transactions. */ |
| 86 | struct fw_address_handler async_handler; |
| 87 | struct snd_rawmidi_substream *tx_midi_substreams[TSCM_MIDI_IN_PORT_MAX]; |
Takashi Sakamoto | 3beab0f | 2015-10-12 19:10:22 +0900 | [diff] [blame] | 88 | |
| 89 | /* For MIDI message outgoing transactions. */ |
| 90 | struct snd_fw_async_midi_port out_ports[TSCM_MIDI_OUT_PORT_MAX]; |
Takashi Sakamoto | 33b2e14 | 2018-11-23 13:13:03 +0900 | [diff] [blame] | 91 | |
| 92 | // A cache of status information in tx isoc packets. |
| 93 | __be32 state[SNDRV_FIREWIRE_TASCAM_STATE_COUNT]; |
Takashi Sakamoto | d716742 | 2018-11-23 13:13:05 +0900 | [diff] [blame] | 94 | struct snd_hwdep *hwdep; |
| 95 | struct snd_firewire_tascam_change queue[SND_TSCM_QUEUE_COUNT]; |
| 96 | unsigned int pull_pos; |
| 97 | unsigned int push_pos; |
Takashi Sakamoto | c281d46 | 2019-08-04 15:21:35 +0900 | [diff] [blame] | 98 | |
| 99 | struct amdtp_domain domain; |
Takashi Sakamoto | a9dd8a6 | 2021-05-31 11:51:02 +0900 | [diff] [blame] | 100 | bool need_long_tx_init_skip; |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
Takashi Sakamoto | 96e5fbb | 2015-10-01 22:02:13 +0900 | [diff] [blame] | 103 | #define TSCM_ADDR_BASE 0xffff00000000ull |
| 104 | |
| 105 | #define TSCM_OFFSET_FIRMWARE_REGISTER 0x0000 |
| 106 | #define TSCM_OFFSET_FIRMWARE_FPGA 0x0004 |
| 107 | #define TSCM_OFFSET_FIRMWARE_ARM 0x0008 |
| 108 | #define TSCM_OFFSET_FIRMWARE_HW 0x000c |
| 109 | |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 110 | #define TSCM_OFFSET_ISOC_TX_CH 0x0200 |
| 111 | #define TSCM_OFFSET_UNKNOWN 0x0204 |
| 112 | #define TSCM_OFFSET_START_STREAMING 0x0208 |
| 113 | #define TSCM_OFFSET_ISOC_RX_CH 0x020c |
| 114 | #define TSCM_OFFSET_ISOC_RX_ON 0x0210 /* Little conviction. */ |
| 115 | #define TSCM_OFFSET_TX_PCM_CHANNELS 0x0214 |
| 116 | #define TSCM_OFFSET_RX_PCM_CHANNELS 0x0218 |
| 117 | #define TSCM_OFFSET_MULTIPLEX_MODE 0x021c |
| 118 | #define TSCM_OFFSET_ISOC_TX_ON 0x0220 |
| 119 | /* Unknown 0x0224 */ |
| 120 | #define TSCM_OFFSET_CLOCK_STATUS 0x0228 |
| 121 | #define TSCM_OFFSET_SET_OPTION 0x022c |
| 122 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 123 | #define TSCM_OFFSET_MIDI_TX_ON 0x0300 |
| 124 | #define TSCM_OFFSET_MIDI_TX_ADDR_HI 0x0304 |
| 125 | #define TSCM_OFFSET_MIDI_TX_ADDR_LO 0x0308 |
| 126 | |
Takashi Sakamoto | e65e2cb | 2015-10-12 19:10:24 +0900 | [diff] [blame] | 127 | #define TSCM_OFFSET_LED_POWER 0x0404 |
| 128 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 129 | #define TSCM_OFFSET_MIDI_RX_QUAD 0x4000 |
| 130 | |
Takashi Sakamoto | e6e2fe2 | 2019-09-06 21:55:44 +0900 | [diff] [blame] | 131 | // Although FE-8 supports the above registers, it has no I/O interfaces for |
| 132 | // audio samples and music messages. Otherwise it supports another notification |
| 133 | // for status and control message as well as LED brightening. The message |
| 134 | // consists of quadlet-aligned data up to 32 quadlets. The first byte of message |
| 135 | // is fixed to 0x40. The second byte is between 0x00 to 0x1f and represent each |
| 136 | // control: |
| 137 | // fader: 0x00-0x07 |
| 138 | // button: 0x0d, 0x0e |
| 139 | // knob: 0x14-0x1b |
| 140 | // sensing: 0x0b |
| 141 | // |
| 142 | // The rest two bytes represent state of the controls; e.g. current value for |
| 143 | // fader and knob, bitmasks for button and sensing. |
| 144 | // Just after turning on, 32 quadlets messages with 0x00-0x1f are immediately |
| 145 | // sent in one transaction. After, several quadlets are sent in one transaction. |
| 146 | // |
| 147 | // TSCM_OFFSET_FE8_CTL_TX_ON 0x0310 |
| 148 | // TSCM_OFFSET_FE8_CTL_TX_ADDR_HI 0x0314 |
| 149 | // TSCM_OFFSET_FE8_CTL_TX_ADDR_LO 0x0318 |
| 150 | |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 151 | enum snd_tscm_clock { |
| 152 | SND_TSCM_CLOCK_INTERNAL = 0, |
| 153 | SND_TSCM_CLOCK_WORD = 1, |
| 154 | SND_TSCM_CLOCK_SPDIF = 2, |
| 155 | SND_TSCM_CLOCK_ADAT = 3, |
| 156 | }; |
| 157 | |
Takashi Sakamoto | 47faeea | 2015-10-01 22:02:14 +0900 | [diff] [blame] | 158 | int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit, |
| 159 | enum amdtp_stream_direction dir, unsigned int pcm_channels); |
| 160 | int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate); |
| 161 | int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s, |
| 162 | struct snd_pcm_runtime *runtime); |
Takashi Sakamoto | 47faeea | 2015-10-01 22:02:14 +0900 | [diff] [blame] | 163 | |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 164 | int snd_tscm_stream_get_rate(struct snd_tscm *tscm, unsigned int *rate); |
| 165 | int snd_tscm_stream_get_clock(struct snd_tscm *tscm, |
| 166 | enum snd_tscm_clock *clock); |
| 167 | int snd_tscm_stream_init_duplex(struct snd_tscm *tscm); |
| 168 | void snd_tscm_stream_update_duplex(struct snd_tscm *tscm); |
| 169 | void snd_tscm_stream_destroy_duplex(struct snd_tscm *tscm); |
Takashi Sakamoto | 262542e | 2019-10-07 20:05:22 +0900 | [diff] [blame] | 170 | int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate, |
Takashi Sakamoto | 128307d | 2019-10-18 00:54:19 +0900 | [diff] [blame] | 171 | unsigned int frames_per_period, |
| 172 | unsigned int frames_per_buffer); |
Takashi Sakamoto | 35efa5c | 2015-10-01 22:02:15 +0900 | [diff] [blame] | 173 | int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate); |
| 174 | void snd_tscm_stream_stop_duplex(struct snd_tscm *tscm); |
| 175 | |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 176 | void snd_tscm_stream_lock_changed(struct snd_tscm *tscm); |
| 177 | int snd_tscm_stream_lock_try(struct snd_tscm *tscm); |
| 178 | void snd_tscm_stream_lock_release(struct snd_tscm *tscm); |
| 179 | |
Takashi Sakamoto | 3e7dc65 | 2017-04-14 16:46:30 +0900 | [diff] [blame] | 180 | void snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port); |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 181 | |
| 182 | static inline void |
| 183 | snd_fw_async_midi_port_run(struct snd_fw_async_midi_port *port, |
| 184 | struct snd_rawmidi_substream *substream) |
| 185 | { |
| 186 | if (!port->error) { |
| 187 | port->substream = substream; |
| 188 | schedule_work(&port->work); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static inline void |
| 193 | snd_fw_async_midi_port_finish(struct snd_fw_async_midi_port *port) |
| 194 | { |
| 195 | port->substream = NULL; |
Takashi Sakamoto | 98a00d3 | 2017-04-14 16:46:29 +0900 | [diff] [blame] | 196 | cancel_work_sync(&port->work); |
Takashi Sakamoto | 531f471 | 2017-04-14 16:46:25 +0900 | [diff] [blame] | 197 | port->error = false; |
| 198 | } |
| 199 | |
Takashi Sakamoto | 107cc01 | 2015-10-12 19:10:21 +0900 | [diff] [blame] | 200 | int snd_tscm_transaction_register(struct snd_tscm *tscm); |
| 201 | int snd_tscm_transaction_reregister(struct snd_tscm *tscm); |
| 202 | void snd_tscm_transaction_unregister(struct snd_tscm *tscm); |
| 203 | |
Takashi Sakamoto | 96e5fbb | 2015-10-01 22:02:13 +0900 | [diff] [blame] | 204 | void snd_tscm_proc_init(struct snd_tscm *tscm); |
| 205 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 206 | int snd_tscm_create_pcm_devices(struct snd_tscm *tscm); |
| 207 | |
Takashi Sakamoto | 0db18e7 | 2015-10-12 19:10:23 +0900 | [diff] [blame] | 208 | int snd_tscm_create_midi_devices(struct snd_tscm *tscm); |
| 209 | |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 210 | int snd_tscm_create_hwdep_device(struct snd_tscm *tscm); |
| 211 | |
Takashi Sakamoto | c0949b2 | 2015-10-01 22:02:11 +0900 | [diff] [blame] | 212 | #endif |