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