xref: /openbmc/linux/sound/firewire/tascam/tascam.h (revision 1cac4f26)
1 /*
2  * tascam.h - a part of driver for TASCAM FireWire series
3  *
4  * Copyright (c) 2015 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #ifndef SOUND_TASCAM_H_INCLUDED
10 #define SOUND_TASCAM_H_INCLUDED
11 
12 #include <linux/device.h>
13 #include <linux/firewire.h>
14 #include <linux/firewire-constants.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/compat.h>
20 #include <linux/sched/signal.h>
21 
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/info.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/firewire.h>
28 #include <sound/hwdep.h>
29 #include <sound/rawmidi.h>
30 
31 #include "../lib.h"
32 #include "../amdtp-stream.h"
33 #include "../iso-resources.h"
34 
35 struct snd_tscm_spec {
36 	const char *const name;
37 	bool has_adat;
38 	bool has_spdif;
39 	unsigned int pcm_capture_analog_channels;
40 	unsigned int pcm_playback_analog_channels;
41 	unsigned int midi_capture_ports;
42 	unsigned int midi_playback_ports;
43 };
44 
45 #define TSCM_MIDI_IN_PORT_MAX	4
46 #define TSCM_MIDI_OUT_PORT_MAX	4
47 
48 struct snd_fw_async_midi_port {
49 	struct fw_device *parent;
50 	struct work_struct work;
51 	bool idling;
52 	ktime_t next_ktime;
53 	bool error;
54 
55 	struct fw_transaction transaction;
56 
57 	u8 buf[4];
58 	u8 running_status;
59 	bool on_sysex;
60 
61 	struct snd_rawmidi_substream *substream;
62 	int consume_bytes;
63 };
64 
65 #define SND_TSCM_QUEUE_COUNT	16
66 
67 struct snd_tscm {
68 	struct snd_card *card;
69 	struct fw_unit *unit;
70 
71 	struct mutex mutex;
72 	spinlock_t lock;
73 
74 	bool registered;
75 	struct delayed_work dwork;
76 	const struct snd_tscm_spec *spec;
77 
78 	struct fw_iso_resources tx_resources;
79 	struct fw_iso_resources rx_resources;
80 	struct amdtp_stream tx_stream;
81 	struct amdtp_stream rx_stream;
82 	unsigned int substreams_counter;
83 
84 	int dev_lock_count;
85 	bool dev_lock_changed;
86 	wait_queue_head_t hwdep_wait;
87 
88 	/* For MIDI message incoming transactions. */
89 	struct fw_address_handler async_handler;
90 	struct snd_rawmidi_substream *tx_midi_substreams[TSCM_MIDI_IN_PORT_MAX];
91 
92 	/* For MIDI message outgoing transactions. */
93 	struct snd_fw_async_midi_port out_ports[TSCM_MIDI_OUT_PORT_MAX];
94 
95 	// A cache of status information in tx isoc packets.
96 	__be32 state[SNDRV_FIREWIRE_TASCAM_STATE_COUNT];
97 	struct snd_hwdep *hwdep;
98 	struct snd_firewire_tascam_change queue[SND_TSCM_QUEUE_COUNT];
99 	unsigned int pull_pos;
100 	unsigned int push_pos;
101 };
102 
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 
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 
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 
127 #define TSCM_OFFSET_LED_POWER		0x0404
128 
129 #define TSCM_OFFSET_MIDI_RX_QUAD	0x4000
130 
131 enum snd_tscm_clock {
132 	SND_TSCM_CLOCK_INTERNAL = 0,
133 	SND_TSCM_CLOCK_WORD	= 1,
134 	SND_TSCM_CLOCK_SPDIF	= 2,
135 	SND_TSCM_CLOCK_ADAT	= 3,
136 };
137 
138 int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
139 		  enum amdtp_stream_direction dir, unsigned int pcm_channels);
140 int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate);
141 int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
142 				      struct snd_pcm_runtime *runtime);
143 
144 int snd_tscm_stream_get_rate(struct snd_tscm *tscm, unsigned int *rate);
145 int snd_tscm_stream_get_clock(struct snd_tscm *tscm,
146 			      enum snd_tscm_clock *clock);
147 int snd_tscm_stream_init_duplex(struct snd_tscm *tscm);
148 void snd_tscm_stream_update_duplex(struct snd_tscm *tscm);
149 void snd_tscm_stream_destroy_duplex(struct snd_tscm *tscm);
150 int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate);
151 void snd_tscm_stream_stop_duplex(struct snd_tscm *tscm);
152 
153 void snd_tscm_stream_lock_changed(struct snd_tscm *tscm);
154 int snd_tscm_stream_lock_try(struct snd_tscm *tscm);
155 void snd_tscm_stream_lock_release(struct snd_tscm *tscm);
156 
157 void snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port);
158 
159 static inline void
160 snd_fw_async_midi_port_run(struct snd_fw_async_midi_port *port,
161 			   struct snd_rawmidi_substream *substream)
162 {
163 	if (!port->error) {
164 		port->substream = substream;
165 		schedule_work(&port->work);
166 	}
167 }
168 
169 static inline void
170 snd_fw_async_midi_port_finish(struct snd_fw_async_midi_port *port)
171 {
172 	port->substream = NULL;
173 	cancel_work_sync(&port->work);
174 	port->error = false;
175 }
176 
177 int snd_tscm_transaction_register(struct snd_tscm *tscm);
178 int snd_tscm_transaction_reregister(struct snd_tscm *tscm);
179 void snd_tscm_transaction_unregister(struct snd_tscm *tscm);
180 
181 void snd_tscm_proc_init(struct snd_tscm *tscm);
182 
183 int snd_tscm_create_pcm_devices(struct snd_tscm *tscm);
184 
185 int snd_tscm_create_midi_devices(struct snd_tscm *tscm);
186 
187 int snd_tscm_create_hwdep_device(struct snd_tscm *tscm);
188 
189 #endif
190