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