1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * ff.h - a part of driver for RME Fireface series
4  *
5  * Copyright (c) 2015-2017 Takashi Sakamoto
6  */
7 
8 #ifndef SOUND_FIREFACE_H_INCLUDED
9 #define SOUND_FIREFACE_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>
19 #include <linux/sched/signal.h>
20 
21 #include <sound/core.h>
22 #include <sound/info.h>
23 #include <sound/rawmidi.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/hwdep.h>
27 #include <sound/firewire.h>
28 
29 #include "../lib.h"
30 #include "../amdtp-stream.h"
31 #include "../iso-resources.h"
32 
33 #define SND_FF_MAXIMIM_MIDI_QUADS	9
34 #define SND_FF_IN_MIDI_PORTS		2
35 #define SND_FF_OUT_MIDI_PORTS		2
36 
37 enum snd_ff_unit_version {
38 	SND_FF_UNIT_VERSION_FF800	= 0x000001,
39 	SND_FF_UNIT_VERSION_FF400	= 0x000002,
40 	SND_FF_UNIT_VERSION_UFX		= 0x000003,
41 	SND_FF_UNIT_VERSION_UCX		= 0x000004,
42 	SND_FF_UNIT_VERSION_802		= 0x000005,
43 };
44 
45 enum snd_ff_stream_mode {
46 	SND_FF_STREAM_MODE_LOW = 0,
47 	SND_FF_STREAM_MODE_MID,
48 	SND_FF_STREAM_MODE_HIGH,
49 	SND_FF_STREAM_MODE_COUNT,
50 };
51 
52 struct snd_ff_protocol;
53 struct snd_ff_spec {
54 	const unsigned int pcm_capture_channels[SND_FF_STREAM_MODE_COUNT];
55 	const unsigned int pcm_playback_channels[SND_FF_STREAM_MODE_COUNT];
56 
57 	unsigned int midi_in_ports;
58 	unsigned int midi_out_ports;
59 
60 	const struct snd_ff_protocol *protocol;
61 	u64 midi_high_addr;
62 	u8 midi_addr_range;
63 	u64 midi_rx_addrs[SND_FF_OUT_MIDI_PORTS];
64 };
65 
66 struct snd_ff {
67 	struct snd_card *card;
68 	struct fw_unit *unit;
69 	struct mutex mutex;
70 	spinlock_t lock;
71 
72 	enum snd_ff_unit_version unit_version;
73 	const struct snd_ff_spec *spec;
74 
75 	/* To handle MIDI tx. */
76 	struct snd_rawmidi_substream *tx_midi_substreams[SND_FF_IN_MIDI_PORTS];
77 	struct fw_address_handler async_handler;
78 
79 	/* TO handle MIDI rx. */
80 	struct snd_rawmidi_substream *rx_midi_substreams[SND_FF_OUT_MIDI_PORTS];
81 	bool on_sysex[SND_FF_OUT_MIDI_PORTS];
82 	__le32 msg_buf[SND_FF_OUT_MIDI_PORTS][SND_FF_MAXIMIM_MIDI_QUADS];
83 	struct work_struct rx_midi_work[SND_FF_OUT_MIDI_PORTS];
84 	struct fw_transaction transactions[SND_FF_OUT_MIDI_PORTS];
85 	ktime_t next_ktime[SND_FF_OUT_MIDI_PORTS];
86 	bool rx_midi_error[SND_FF_OUT_MIDI_PORTS];
87 	unsigned int rx_bytes[SND_FF_OUT_MIDI_PORTS];
88 
89 	unsigned int substreams_counter;
90 	struct amdtp_stream tx_stream;
91 	struct amdtp_stream rx_stream;
92 	struct fw_iso_resources tx_resources;
93 	struct fw_iso_resources rx_resources;
94 
95 	int dev_lock_count;
96 	bool dev_lock_changed;
97 	wait_queue_head_t hwdep_wait;
98 
99 	struct amdtp_domain domain;
100 
101 	void *msg_parser;
102 };
103 
104 enum snd_ff_clock_src {
105 	SND_FF_CLOCK_SRC_INTERNAL,
106 	SND_FF_CLOCK_SRC_SPDIF,
107 	SND_FF_CLOCK_SRC_ADAT1,
108 	SND_FF_CLOCK_SRC_ADAT2,
109 	SND_FF_CLOCK_SRC_WORD,
110 	SND_FF_CLOCK_SRC_LTC,
111 	/* TODO: perhaps TCO exists. */
112 };
113 
114 struct snd_ff_protocol {
115 	size_t msg_parser_size;
116 	bool (*has_msg)(struct snd_ff *ff);
117 	long (*copy_msg_to_user)(struct snd_ff *ff, char __user *buf, long count);
118 	void (*handle_msg)(struct snd_ff *ff, unsigned int offset, const __le32 *buf,
119 			   size_t length, u32 tstamp);
120 	int (*fill_midi_msg)(struct snd_ff *ff,
121 			     struct snd_rawmidi_substream *substream,
122 			     unsigned int port);
123 	int (*get_clock)(struct snd_ff *ff, unsigned int *rate,
124 			 enum snd_ff_clock_src *src);
125 	int (*switch_fetching_mode)(struct snd_ff *ff, bool enable);
126 	int (*allocate_resources)(struct snd_ff *ff, unsigned int rate);
127 	int (*begin_session)(struct snd_ff *ff, unsigned int rate);
128 	void (*finish_session)(struct snd_ff *ff);
129 	void (*dump_status)(struct snd_ff *ff, struct snd_info_buffer *buffer);
130 };
131 
132 extern const struct snd_ff_protocol snd_ff_protocol_ff800;
133 extern const struct snd_ff_protocol snd_ff_protocol_ff400;
134 extern const struct snd_ff_protocol snd_ff_protocol_latter;
135 
136 int snd_ff_transaction_register(struct snd_ff *ff);
137 int snd_ff_transaction_reregister(struct snd_ff *ff);
138 void snd_ff_transaction_unregister(struct snd_ff *ff);
139 
140 int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
141 			    unsigned int pcm_channels);
142 int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
143 				    struct snd_pcm_runtime *runtime);
144 int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
145 		  enum amdtp_stream_direction dir);
146 
147 int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
148 				      enum snd_ff_stream_mode *mode);
149 int snd_ff_stream_init_duplex(struct snd_ff *ff);
150 void snd_ff_stream_destroy_duplex(struct snd_ff *ff);
151 int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
152 				 unsigned int frames_per_period,
153 				 unsigned int frames_per_buffer);
154 int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate);
155 void snd_ff_stream_stop_duplex(struct snd_ff *ff);
156 void snd_ff_stream_update_duplex(struct snd_ff *ff);
157 
158 void snd_ff_stream_lock_changed(struct snd_ff *ff);
159 int snd_ff_stream_lock_try(struct snd_ff *ff);
160 void snd_ff_stream_lock_release(struct snd_ff *ff);
161 
162 void snd_ff_proc_init(struct snd_ff *ff);
163 const char *snd_ff_proc_get_clk_label(enum snd_ff_clock_src src);
164 
165 int snd_ff_create_midi_devices(struct snd_ff *ff);
166 
167 int snd_ff_create_pcm_devices(struct snd_ff *ff);
168 
169 int snd_ff_create_hwdep_devices(struct snd_ff *ff);
170 
171 #endif
172