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