xref: /openbmc/linux/sound/firewire/motu/motu.h (revision b830f94f)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * motu.h - a part of driver for MOTU FireWire series
4  *
5  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  */
7 
8 #ifndef SOUND_FIREWIRE_MOTU_H_INCLUDED
9 #define SOUND_FIREWIRE_MOTU_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/control.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/rawmidi.h>
26 #include <sound/firewire.h>
27 #include <sound/hwdep.h>
28 
29 #include "../lib.h"
30 #include "../amdtp-stream.h"
31 #include "../iso-resources.h"
32 
33 struct snd_motu_packet_format {
34 	unsigned char midi_flag_offset;
35 	unsigned char midi_byte_offset;
36 	unsigned char pcm_byte_offset;
37 
38 	unsigned char msg_chunks;
39 	unsigned char fixed_part_pcm_chunks[3];
40 	unsigned char differed_part_pcm_chunks[3];
41 };
42 
43 struct snd_motu {
44 	struct snd_card *card;
45 	struct fw_unit *unit;
46 	struct mutex mutex;
47 	spinlock_t lock;
48 
49 	bool registered;
50 	struct delayed_work dwork;
51 
52 	/* Model dependent information. */
53 	const struct snd_motu_spec *spec;
54 
55 	/* For packet streaming */
56 	struct snd_motu_packet_format tx_packet_formats;
57 	struct snd_motu_packet_format rx_packet_formats;
58 	struct amdtp_stream tx_stream;
59 	struct amdtp_stream rx_stream;
60 	struct fw_iso_resources tx_resources;
61 	struct fw_iso_resources rx_resources;
62 	unsigned int substreams_counter;
63 
64 	/* For notification. */
65 	struct fw_address_handler async_handler;
66 	u32 msg;
67 
68 	/* For uapi */
69 	int dev_lock_count;
70 	bool dev_lock_changed;
71 	wait_queue_head_t hwdep_wait;
72 };
73 
74 enum snd_motu_spec_flags {
75 	SND_MOTU_SPEC_SUPPORT_CLOCK_X2	= 0x0001,
76 	SND_MOTU_SPEC_SUPPORT_CLOCK_X4	= 0x0002,
77 	SND_MOTU_SPEC_TX_MICINST_CHUNK	= 0x0004,
78 	SND_MOTU_SPEC_TX_RETURN_CHUNK	= 0x0008,
79 	SND_MOTU_SPEC_TX_REVERB_CHUNK	= 0x0010,
80 	SND_MOTU_SPEC_HAS_AESEBU_IFACE	= 0x0020,
81 	SND_MOTU_SPEC_HAS_OPT_IFACE_A	= 0x0040,
82 	SND_MOTU_SPEC_HAS_OPT_IFACE_B	= 0x0080,
83 	SND_MOTU_SPEC_RX_MIDI_2ND_Q	= 0x0100,
84 	SND_MOTU_SPEC_RX_MIDI_3RD_Q	= 0x0200,
85 	SND_MOTU_SPEC_TX_MIDI_2ND_Q	= 0x0400,
86 	SND_MOTU_SPEC_TX_MIDI_3RD_Q	= 0x0800,
87 	SND_MOTU_SPEC_RX_SEPARETED_MAIN	= 0x1000,
88 };
89 
90 #define SND_MOTU_CLOCK_RATE_COUNT	6
91 extern const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT];
92 
93 enum snd_motu_clock_source {
94 	SND_MOTU_CLOCK_SOURCE_INTERNAL,
95 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB,
96 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT,
97 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A,
98 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B,
99 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT,
100 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A,
101 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B,
102 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX,
103 	SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR,
104 	SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC,
105 	SND_MOTU_CLOCK_SOURCE_UNKNOWN,
106 };
107 
108 struct snd_motu_protocol {
109 	int (*get_clock_rate)(struct snd_motu *motu, unsigned int *rate);
110 	int (*set_clock_rate)(struct snd_motu *motu, unsigned int rate);
111 	int (*get_clock_source)(struct snd_motu *motu,
112 				enum snd_motu_clock_source *source);
113 	int (*switch_fetching_mode)(struct snd_motu *motu, bool enable);
114 	int (*cache_packet_formats)(struct snd_motu *motu);
115 };
116 
117 struct snd_motu_spec {
118 	const char *const name;
119 	enum snd_motu_spec_flags flags;
120 
121 	unsigned char analog_in_ports;
122 	unsigned char analog_out_ports;
123 
124 	const struct snd_motu_protocol *const protocol;
125 };
126 
127 extern const struct snd_motu_protocol snd_motu_protocol_v2;
128 extern const struct snd_motu_protocol snd_motu_protocol_v3;
129 
130 extern const struct snd_motu_spec snd_motu_spec_traveler;
131 extern const struct snd_motu_spec snd_motu_spec_8pre;
132 
133 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
134 		    enum amdtp_stream_direction dir,
135 		    const struct snd_motu_protocol *const protocol);
136 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
137 			      unsigned int midi_ports,
138 			      struct snd_motu_packet_format *formats);
139 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
140 				      struct snd_pcm_runtime *runtime);
141 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
142 			     struct snd_rawmidi_substream *midi);
143 
144 int snd_motu_transaction_read(struct snd_motu *motu, u32 offset, __be32 *reg,
145 			      size_t size);
146 int snd_motu_transaction_write(struct snd_motu *motu, u32 offset, __be32 *reg,
147 			       size_t size);
148 int snd_motu_transaction_register(struct snd_motu *motu);
149 int snd_motu_transaction_reregister(struct snd_motu *motu);
150 void snd_motu_transaction_unregister(struct snd_motu *motu);
151 
152 int snd_motu_stream_init_duplex(struct snd_motu *motu);
153 void snd_motu_stream_destroy_duplex(struct snd_motu *motu);
154 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu);
155 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate);
156 int snd_motu_stream_start_duplex(struct snd_motu *motu);
157 void snd_motu_stream_stop_duplex(struct snd_motu *motu);
158 int snd_motu_stream_lock_try(struct snd_motu *motu);
159 void snd_motu_stream_lock_release(struct snd_motu *motu);
160 
161 void snd_motu_proc_init(struct snd_motu *motu);
162 
163 int snd_motu_create_pcm_devices(struct snd_motu *motu);
164 
165 int snd_motu_create_midi_devices(struct snd_motu *motu);
166 
167 int snd_motu_create_hwdep_device(struct snd_motu *motu);
168 #endif
169