xref: /linux/sound/firewire/bebob/bebob.h (revision 82fb3bf92c0fb6cd471295ba9c774a47c7d64c8c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * bebob.h - a part of driver for BeBoB based devices
4  *
5  * Copyright (c) 2013-2014 Takashi Sakamoto
6  */
7 
8 #ifndef SOUND_BEBOB_H_INCLUDED
9 #define SOUND_BEBOB_H_INCLUDED
10 
11 #include <linux/compat.h>
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/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched/signal.h>
20 
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/info.h>
24 #include <sound/rawmidi.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/firewire.h>
28 #include <sound/hwdep.h>
29 
30 #include "../lib.h"
31 #include "../fcp.h"
32 #include "../packets-buffer.h"
33 #include "../iso-resources.h"
34 #include "../amdtp-am824.h"
35 #include "../cmp.h"
36 
37 /* basic register addresses on DM1000/DM1100/DM1500 */
38 #define BEBOB_ADDR_REG_INFO	0xffffc8020000ULL
39 #define BEBOB_ADDR_REG_REQ	0xffffc8021000ULL
40 
41 struct snd_bebob;
42 
43 #define SND_BEBOB_STRM_FMT_ENTRIES	7
44 struct snd_bebob_stream_formation {
45 	unsigned int pcm;
46 	unsigned int midi;
47 };
48 /* this is a lookup table for index of stream formations */
49 extern const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES];
50 
51 /* device specific operations */
52 enum snd_bebob_clock_type {
53 	SND_BEBOB_CLOCK_TYPE_INTERNAL = 0,
54 	SND_BEBOB_CLOCK_TYPE_EXTERNAL,
55 	SND_BEBOB_CLOCK_TYPE_SYT,
56 };
57 struct snd_bebob_clock_spec {
58 	unsigned int num;
59 	const char *const *labels;
60 	const enum snd_bebob_clock_type *types;
61 	int (*get)(struct snd_bebob *bebob, unsigned int *id);
62 };
63 struct snd_bebob_rate_spec {
64 	int (*get)(struct snd_bebob *bebob, unsigned int *rate);
65 	int (*set)(struct snd_bebob *bebob, unsigned int rate);
66 };
67 struct snd_bebob_meter_spec {
68 	unsigned int num;
69 	const char *const *labels;
70 	int (*get)(struct snd_bebob *bebob, u32 *target, unsigned int size);
71 };
72 struct snd_bebob_spec {
73 	const struct snd_bebob_clock_spec *clock;
74 	const struct snd_bebob_rate_spec *rate;
75 	const struct snd_bebob_meter_spec *meter;
76 };
77 
78 struct snd_bebob {
79 	struct snd_card *card;
80 	struct fw_unit *unit;
81 	int card_index;
82 
83 	struct mutex mutex;
84 	spinlock_t lock;
85 
86 	const struct snd_bebob_spec *spec;
87 
88 	unsigned int midi_input_ports;
89 	unsigned int midi_output_ports;
90 
91 	struct amdtp_stream tx_stream;
92 	struct amdtp_stream rx_stream;
93 	struct cmp_connection out_conn;
94 	struct cmp_connection in_conn;
95 	unsigned int substreams_counter;
96 
97 	struct snd_bebob_stream_formation
98 		tx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
99 	struct snd_bebob_stream_formation
100 		rx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
101 
102 	int sync_input_plug;
103 
104 	/* for uapi */
105 	int dev_lock_count;
106 	bool dev_lock_changed;
107 	wait_queue_head_t hwdep_wait;
108 
109 	/* for M-Audio special devices */
110 	void *maudio_special_quirk;
111 
112 	bool discontinuity_quirk;
113 
114 	struct amdtp_domain domain;
115 };
116 
117 static inline int
118 snd_bebob_read_block(struct fw_unit *unit, u64 addr, void *buf, int size)
119 {
120 	return snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
121 				  BEBOB_ADDR_REG_INFO + addr,
122 				  buf, size, 0);
123 }
124 
125 static inline int
126 snd_bebob_read_quad(struct fw_unit *unit, u64 addr, u32 *buf)
127 {
128 	return snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
129 				  BEBOB_ADDR_REG_INFO + addr,
130 				  (void *)buf, sizeof(u32), 0);
131 }
132 
133 /* AV/C Audio Subunit Specification 1.0 (Oct 2000, 1394TA) */
134 int avc_audio_set_selector(struct fw_unit *unit, unsigned int subunit_id,
135 			   unsigned int fb_id, unsigned int num);
136 int avc_audio_get_selector(struct fw_unit *unit, unsigned  int subunit_id,
137 			   unsigned int fb_id, unsigned int *num);
138 
139 /*
140  * AVC command extensions, AV/C Unit and Subunit, Revision 17
141  * (Nov 2003, BridgeCo)
142  */
143 #define	AVC_BRIDGECO_ADDR_BYTES	6
144 enum avc_bridgeco_plug_dir {
145 	AVC_BRIDGECO_PLUG_DIR_IN	= 0x00,
146 	AVC_BRIDGECO_PLUG_DIR_OUT	= 0x01
147 };
148 enum avc_bridgeco_plug_mode {
149 	AVC_BRIDGECO_PLUG_MODE_UNIT		= 0x00,
150 	AVC_BRIDGECO_PLUG_MODE_SUBUNIT		= 0x01,
151 	AVC_BRIDGECO_PLUG_MODE_FUNCTION_BLOCK	= 0x02
152 };
153 enum avc_bridgeco_plug_unit {
154 	AVC_BRIDGECO_PLUG_UNIT_ISOC	= 0x00,
155 	AVC_BRIDGECO_PLUG_UNIT_EXT	= 0x01,
156 	AVC_BRIDGECO_PLUG_UNIT_ASYNC	= 0x02
157 };
158 enum avc_bridgeco_plug_type {
159 	AVC_BRIDGECO_PLUG_TYPE_ISOC	= 0x00,
160 	AVC_BRIDGECO_PLUG_TYPE_ASYNC	= 0x01,
161 	AVC_BRIDGECO_PLUG_TYPE_MIDI	= 0x02,
162 	AVC_BRIDGECO_PLUG_TYPE_SYNC	= 0x03,
163 	AVC_BRIDGECO_PLUG_TYPE_ANA	= 0x04,
164 	AVC_BRIDGECO_PLUG_TYPE_DIG	= 0x05,
165 	AVC_BRIDGECO_PLUG_TYPE_ADDITION	= 0x06
166 };
167 static inline void
168 avc_bridgeco_fill_unit_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
169 			    enum avc_bridgeco_plug_dir dir,
170 			    enum avc_bridgeco_plug_unit unit,
171 			    unsigned int pid)
172 {
173 	buf[0] = 0xff;	/* Unit */
174 	buf[1] = dir;
175 	buf[2] = AVC_BRIDGECO_PLUG_MODE_UNIT;
176 	buf[3] = unit;
177 	buf[4] = 0xff & pid;
178 	buf[5] = 0xff;	/* reserved */
179 }
180 static inline void
181 avc_bridgeco_fill_msu_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
182 			   enum avc_bridgeco_plug_dir dir,
183 			   unsigned int pid)
184 {
185 	buf[0] = 0x60;	/* Music subunit */
186 	buf[1] = dir;
187 	buf[2] = AVC_BRIDGECO_PLUG_MODE_SUBUNIT;
188 	buf[3] = 0xff & pid;
189 	buf[4] = 0xff;	/* reserved */
190 	buf[5] = 0xff;	/* reserved */
191 }
192 int avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
193 				 u8 addr[AVC_BRIDGECO_ADDR_BYTES],
194 				 u8 *buf, unsigned int len);
195 int avc_bridgeco_get_plug_type(struct fw_unit *unit,
196 			       u8 addr[AVC_BRIDGECO_ADDR_BYTES],
197 			       enum avc_bridgeco_plug_type *type);
198 int avc_bridgeco_get_plug_ch_count(struct fw_unit *unit, u8 addr[AVC_BRIDGECO_ADDR_BYTES],
199 				   unsigned int *ch_count);
200 int avc_bridgeco_get_plug_section_type(struct fw_unit *unit,
201 				       u8 addr[AVC_BRIDGECO_ADDR_BYTES],
202 				       unsigned int id, u8 *type);
203 int avc_bridgeco_get_plug_input(struct fw_unit *unit,
204 				u8 addr[AVC_BRIDGECO_ADDR_BYTES],
205 				u8 input[7]);
206 int avc_bridgeco_get_plug_strm_fmt(struct fw_unit *unit,
207 				   u8 addr[AVC_BRIDGECO_ADDR_BYTES], u8 *buf,
208 				   unsigned int *len, unsigned int eid);
209 
210 /* for AMDTP streaming */
211 int snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *rate);
212 int snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate);
213 int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
214 				   enum snd_bebob_clock_type *src);
215 int snd_bebob_stream_discover(struct snd_bebob *bebob);
216 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob);
217 int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate,
218 				    unsigned int frames_per_period,
219 				    unsigned int frames_per_buffer);
220 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob);
221 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob);
222 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob);
223 
224 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob);
225 int snd_bebob_stream_lock_try(struct snd_bebob *bebob);
226 void snd_bebob_stream_lock_release(struct snd_bebob *bebob);
227 
228 void snd_bebob_proc_init(struct snd_bebob *bebob);
229 
230 int snd_bebob_create_midi_devices(struct snd_bebob *bebob);
231 
232 int snd_bebob_create_pcm_devices(struct snd_bebob *bebob);
233 
234 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob);
235 
236 /* model specific operations */
237 extern const struct snd_bebob_spec phase88_rack_spec;
238 extern const struct snd_bebob_spec yamaha_terratec_spec;
239 extern const struct snd_bebob_spec saffirepro_26_spec;
240 extern const struct snd_bebob_spec saffirepro_10_spec;
241 extern const struct snd_bebob_spec saffire_le_spec;
242 extern const struct snd_bebob_spec saffire_spec;
243 extern const struct snd_bebob_spec maudio_fw410_spec;
244 extern const struct snd_bebob_spec maudio_audiophile_spec;
245 extern const struct snd_bebob_spec maudio_solo_spec;
246 extern const struct snd_bebob_spec maudio_ozonic_spec;
247 extern const struct snd_bebob_spec maudio_nrv10_spec;
248 extern const struct snd_bebob_spec maudio_special_spec;
249 int snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814);
250 int snd_bebob_maudio_load_firmware(struct fw_unit *unit);
251 
252 #define SND_BEBOB_DEV_ENTRY(vendor, model, data) \
253 { \
254 	.match_flags	= IEEE1394_MATCH_VENDOR_ID | \
255 			  IEEE1394_MATCH_MODEL_ID, \
256 	.vendor_id	= vendor, \
257 	.model_id	= model, \
258 	.driver_data	= (kernel_ulong_t)data \
259 }
260 
261 #endif
262