1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * fireworks.c - a part of driver for Fireworks based devices
4  *
5  * Copyright (c) 2009-2010 Clemens Ladisch
6  * Copyright (c) 2013-2014 Takashi Sakamoto
7  */
8 
9 /*
10  * Fireworks is a board module which Echo Audio produced. This module consists
11  * of three chipsets:
12  *  - Communication chipset for IEEE1394 PHY/Link and IEC 61883-1/6
13  *  - DSP or/and FPGA for signal processing
14  *  - Flash Memory to store firmwares
15  */
16 
17 #include "fireworks.h"
18 
19 MODULE_DESCRIPTION("Echo Fireworks driver");
20 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
21 MODULE_LICENSE("GPL");
22 
23 static int index[SNDRV_CARDS]	= SNDRV_DEFAULT_IDX;
24 static char *id[SNDRV_CARDS]	= SNDRV_DEFAULT_STR;
25 static bool enable[SNDRV_CARDS]	= SNDRV_DEFAULT_ENABLE_PNP;
26 unsigned int snd_efw_resp_buf_size	= 1024;
27 bool snd_efw_resp_buf_debug		= false;
28 
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "card index");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "enable Fireworks sound card");
35 module_param_named(resp_buf_size, snd_efw_resp_buf_size, uint, 0444);
36 MODULE_PARM_DESC(resp_buf_size,
37 		 "response buffer size (max 4096, default 1024)");
38 module_param_named(resp_buf_debug, snd_efw_resp_buf_debug, bool, 0444);
39 MODULE_PARM_DESC(resp_buf_debug, "store all responses to buffer");
40 
41 static DEFINE_MUTEX(devices_mutex);
42 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
43 
44 #define VENDOR_LOUD			0x000ff2
45 #define  MODEL_MACKIE_400F		0x00400f
46 #define  MODEL_MACKIE_1200F		0x01200f
47 
48 #define VENDOR_ECHO			0x001486
49 #define  MODEL_ECHO_AUDIOFIRE_12	0x00af12
50 #define  MODEL_ECHO_AUDIOFIRE_12HD	0x0af12d
51 #define  MODEL_ECHO_AUDIOFIRE_12_APPLE	0x0af12a
52 /* This is applied for AudioFire8 (until 2009 July) */
53 #define  MODEL_ECHO_AUDIOFIRE_8		0x000af8
54 #define  MODEL_ECHO_AUDIOFIRE_2		0x000af2
55 #define  MODEL_ECHO_AUDIOFIRE_4		0x000af4
56 /* AudioFire9 is applied for AudioFire8(since 2009 July) and AudioFirePre8 */
57 #define  MODEL_ECHO_AUDIOFIRE_9		0x000af9
58 /* unknown as product */
59 #define  MODEL_ECHO_FIREWORKS_8		0x0000f8
60 #define  MODEL_ECHO_FIREWORKS_HDMI	0x00afd1
61 
62 #define VENDOR_GIBSON			0x00075b
63 /* for Robot Interface Pack of Dark Fire, Dusk Tiger, Les Paul Standard 2010 */
64 #define  MODEL_GIBSON_RIP		0x00afb2
65 /* unknown as product */
66 #define  MODEL_GIBSON_GOLDTOP		0x00afb9
67 
68 /* part of hardware capability flags */
69 #define FLAG_RESP_ADDR_CHANGABLE	0
70 
71 static int
72 get_hardware_info(struct snd_efw *efw)
73 {
74 	struct fw_device *fw_dev = fw_parent_device(efw->unit);
75 	struct snd_efw_hwinfo *hwinfo;
76 	char version[12] = {0};
77 	int err;
78 
79 	hwinfo = kzalloc(sizeof(struct snd_efw_hwinfo), GFP_KERNEL);
80 	if (hwinfo == NULL)
81 		return -ENOMEM;
82 
83 	err = snd_efw_command_get_hwinfo(efw, hwinfo);
84 	if (err < 0)
85 		goto end;
86 
87 	/* firmware version for communication chipset */
88 	snprintf(version, sizeof(version), "%u.%u",
89 		 (hwinfo->arm_version >> 24) & 0xff,
90 		 (hwinfo->arm_version >> 16) & 0xff);
91 	efw->firmware_version = hwinfo->arm_version;
92 
93 	strcpy(efw->card->driver, "Fireworks");
94 	strcpy(efw->card->shortname, hwinfo->model_name);
95 	strcpy(efw->card->mixername, hwinfo->model_name);
96 	snprintf(efw->card->longname, sizeof(efw->card->longname),
97 		 "%s %s v%s, GUID %08x%08x at %s, S%d",
98 		 hwinfo->vendor_name, hwinfo->model_name, version,
99 		 hwinfo->guid_hi, hwinfo->guid_lo,
100 		 dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
101 
102 	if (hwinfo->flags & BIT(FLAG_RESP_ADDR_CHANGABLE))
103 		efw->resp_addr_changable = true;
104 
105 	efw->supported_sampling_rate = 0;
106 	if ((hwinfo->min_sample_rate <= 22050)
107 	 && (22050 <= hwinfo->max_sample_rate))
108 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_22050;
109 	if ((hwinfo->min_sample_rate <= 32000)
110 	 && (32000 <= hwinfo->max_sample_rate))
111 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_32000;
112 	if ((hwinfo->min_sample_rate <= 44100)
113 	 && (44100 <= hwinfo->max_sample_rate))
114 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_44100;
115 	if ((hwinfo->min_sample_rate <= 48000)
116 	 && (48000 <= hwinfo->max_sample_rate))
117 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_48000;
118 	if ((hwinfo->min_sample_rate <= 88200)
119 	 && (88200 <= hwinfo->max_sample_rate))
120 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_88200;
121 	if ((hwinfo->min_sample_rate <= 96000)
122 	 && (96000 <= hwinfo->max_sample_rate))
123 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_96000;
124 	if ((hwinfo->min_sample_rate <= 176400)
125 	 && (176400 <= hwinfo->max_sample_rate))
126 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_176400;
127 	if ((hwinfo->min_sample_rate <= 192000)
128 	 && (192000 <= hwinfo->max_sample_rate))
129 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_192000;
130 
131 	/* the number of MIDI ports, not of MIDI conformant data channels */
132 	if (hwinfo->midi_out_ports > SND_EFW_MAX_MIDI_OUT_PORTS ||
133 	    hwinfo->midi_in_ports > SND_EFW_MAX_MIDI_IN_PORTS) {
134 		err = -EIO;
135 		goto end;
136 	}
137 	efw->midi_out_ports = hwinfo->midi_out_ports;
138 	efw->midi_in_ports = hwinfo->midi_in_ports;
139 
140 	if (hwinfo->amdtp_tx_pcm_channels    > AM824_MAX_CHANNELS_FOR_PCM ||
141 	    hwinfo->amdtp_tx_pcm_channels_2x > AM824_MAX_CHANNELS_FOR_PCM ||
142 	    hwinfo->amdtp_tx_pcm_channels_4x > AM824_MAX_CHANNELS_FOR_PCM ||
143 	    hwinfo->amdtp_rx_pcm_channels    > AM824_MAX_CHANNELS_FOR_PCM ||
144 	    hwinfo->amdtp_rx_pcm_channels_2x > AM824_MAX_CHANNELS_FOR_PCM ||
145 	    hwinfo->amdtp_rx_pcm_channels_4x > AM824_MAX_CHANNELS_FOR_PCM) {
146 		err = -ENOSYS;
147 		goto end;
148 	}
149 	efw->pcm_capture_channels[0] = hwinfo->amdtp_tx_pcm_channels;
150 	efw->pcm_capture_channels[1] = hwinfo->amdtp_tx_pcm_channels_2x;
151 	efw->pcm_capture_channels[2] = hwinfo->amdtp_tx_pcm_channels_4x;
152 	efw->pcm_playback_channels[0] = hwinfo->amdtp_rx_pcm_channels;
153 	efw->pcm_playback_channels[1] = hwinfo->amdtp_rx_pcm_channels_2x;
154 	efw->pcm_playback_channels[2] = hwinfo->amdtp_rx_pcm_channels_4x;
155 
156 	/* Hardware metering. */
157 	if (hwinfo->phys_in_grp_count  > HWINFO_MAX_CAPS_GROUPS ||
158 	    hwinfo->phys_out_grp_count > HWINFO_MAX_CAPS_GROUPS) {
159 		err = -EIO;
160 		goto end;
161 	}
162 	efw->phys_in = hwinfo->phys_in;
163 	efw->phys_out = hwinfo->phys_out;
164 	efw->phys_in_grp_count = hwinfo->phys_in_grp_count;
165 	efw->phys_out_grp_count = hwinfo->phys_out_grp_count;
166 	memcpy(&efw->phys_in_grps, hwinfo->phys_in_grps,
167 	       sizeof(struct snd_efw_phys_grp) * hwinfo->phys_in_grp_count);
168 	memcpy(&efw->phys_out_grps, hwinfo->phys_out_grps,
169 	       sizeof(struct snd_efw_phys_grp) * hwinfo->phys_out_grp_count);
170 
171 	/* AudioFire8 (since 2009) and AudioFirePre8 */
172 	if (hwinfo->type == MODEL_ECHO_AUDIOFIRE_9)
173 		efw->is_af9 = true;
174 	/* These models uses the same firmware. */
175 	if (hwinfo->type == MODEL_ECHO_AUDIOFIRE_2 ||
176 	    hwinfo->type == MODEL_ECHO_AUDIOFIRE_4 ||
177 	    hwinfo->type == MODEL_ECHO_AUDIOFIRE_9 ||
178 	    hwinfo->type == MODEL_GIBSON_RIP ||
179 	    hwinfo->type == MODEL_GIBSON_GOLDTOP)
180 		efw->is_fireworks3 = true;
181 end:
182 	kfree(hwinfo);
183 	return err;
184 }
185 
186 static void
187 efw_card_free(struct snd_card *card)
188 {
189 	struct snd_efw *efw = card->private_data;
190 
191 	mutex_lock(&devices_mutex);
192 	clear_bit(efw->card_index, devices_used);
193 	mutex_unlock(&devices_mutex);
194 
195 	snd_efw_stream_destroy_duplex(efw);
196 	snd_efw_transaction_remove_instance(efw);
197 
198 	mutex_destroy(&efw->mutex);
199 	fw_unit_put(efw->unit);
200 }
201 
202 static int efw_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
203 {
204 	unsigned int card_index;
205 	struct snd_card *card;
206 	struct snd_efw *efw;
207 	int err;
208 
209 	// check registered cards.
210 	mutex_lock(&devices_mutex);
211 	for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) {
212 		if (!test_bit(card_index, devices_used) && enable[card_index])
213 			break;
214 	}
215 	if (card_index >= SNDRV_CARDS) {
216 		mutex_unlock(&devices_mutex);
217 		return -ENOENT;
218 	}
219 
220 	err = snd_card_new(&unit->device, index[card_index], id[card_index], THIS_MODULE,
221 			   sizeof(*efw), &card);
222 	if (err < 0) {
223 		mutex_unlock(&devices_mutex);
224 		return err;
225 	}
226 	card->private_free = efw_card_free;
227 	set_bit(card_index, devices_used);
228 	mutex_unlock(&devices_mutex);
229 
230 	efw = card->private_data;
231 	efw->unit = fw_unit_get(unit);
232 	dev_set_drvdata(&unit->device, efw);
233 	efw->card = card;
234 	efw->card_index = card_index;
235 
236 	mutex_init(&efw->mutex);
237 	spin_lock_init(&efw->lock);
238 	init_waitqueue_head(&efw->hwdep_wait);
239 
240 	// prepare response buffer.
241 	snd_efw_resp_buf_size = clamp(snd_efw_resp_buf_size, SND_EFW_RESPONSE_MAXIMUM_BYTES, 4096U);
242 	efw->resp_buf = devm_kzalloc(&card->card_dev, snd_efw_resp_buf_size, GFP_KERNEL);
243 	if (!efw->resp_buf) {
244 		err = -ENOMEM;
245 		goto error;
246 	}
247 	efw->pull_ptr = efw->push_ptr = efw->resp_buf;
248 	snd_efw_transaction_add_instance(efw);
249 
250 	err = get_hardware_info(efw);
251 	if (err < 0)
252 		goto error;
253 
254 	err = snd_efw_stream_init_duplex(efw);
255 	if (err < 0)
256 		goto error;
257 
258 	snd_efw_proc_init(efw);
259 
260 	if (efw->midi_out_ports || efw->midi_in_ports) {
261 		err = snd_efw_create_midi_devices(efw);
262 		if (err < 0)
263 			goto error;
264 	}
265 
266 	err = snd_efw_create_pcm_devices(efw);
267 	if (err < 0)
268 		goto error;
269 
270 	err = snd_efw_create_hwdep_device(efw);
271 	if (err < 0)
272 		goto error;
273 
274 	err = snd_card_register(card);
275 	if (err < 0)
276 		goto error;
277 
278 	return 0;
279 error:
280 	snd_card_free(card);
281 	return err;
282 }
283 
284 static void efw_update(struct fw_unit *unit)
285 {
286 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
287 
288 	snd_efw_transaction_bus_reset(efw->unit);
289 
290 	mutex_lock(&efw->mutex);
291 	snd_efw_stream_update_duplex(efw);
292 	mutex_unlock(&efw->mutex);
293 }
294 
295 static void efw_remove(struct fw_unit *unit)
296 {
297 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
298 
299 	// Block till all of ALSA character devices are released.
300 	snd_card_free(efw->card);
301 }
302 
303 #define SPECIFIER_1394TA	0x00a02d
304 #define VERSION_EFW		0x010000
305 
306 #define SND_EFW_DEV_ENTRY(vendor, model) \
307 { \
308 	.match_flags	= IEEE1394_MATCH_VENDOR_ID | \
309 			  IEEE1394_MATCH_MODEL_ID | \
310 			  IEEE1394_MATCH_SPECIFIER_ID | \
311 			  IEEE1394_MATCH_VERSION, \
312 	.vendor_id	= vendor,\
313 	.model_id	= model, \
314 	.specifier_id	= SPECIFIER_1394TA, \
315 	.version	= VERSION_EFW, \
316 }
317 
318 static const struct ieee1394_device_id efw_id_table[] = {
319 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_400F),
320 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_1200F),
321 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_8),
322 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12),
323 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12HD),
324 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12_APPLE),
325 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_2),
326 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_4),
327 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_9),
328 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_8),
329 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_HDMI),
330 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_RIP),
331 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_GOLDTOP),
332 	{}
333 };
334 MODULE_DEVICE_TABLE(ieee1394, efw_id_table);
335 
336 static struct fw_driver efw_driver = {
337 	.driver = {
338 		.owner = THIS_MODULE,
339 		.name = KBUILD_MODNAME,
340 		.bus = &fw_bus_type,
341 	},
342 	.probe    = efw_probe,
343 	.update   = efw_update,
344 	.remove   = efw_remove,
345 	.id_table = efw_id_table,
346 };
347 
348 static int __init snd_efw_init(void)
349 {
350 	int err;
351 
352 	err = snd_efw_transaction_register();
353 	if (err < 0)
354 		goto end;
355 
356 	err = driver_register(&efw_driver.driver);
357 	if (err < 0)
358 		snd_efw_transaction_unregister();
359 
360 end:
361 	return err;
362 }
363 
364 static void __exit snd_efw_exit(void)
365 {
366 	snd_efw_transaction_unregister();
367 	driver_unregister(&efw_driver.driver);
368 }
369 
370 module_init(snd_efw_init);
371 module_exit(snd_efw_exit);
372