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 v2");
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 
199 static void
200 do_registration(struct work_struct *work)
201 {
202 	struct snd_efw *efw = container_of(work, struct snd_efw, dwork.work);
203 	unsigned int card_index;
204 	int err;
205 
206 	if (efw->registered)
207 		return;
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;
218 	}
219 
220 	err = snd_card_new(&efw->unit->device, index[card_index],
221 			   id[card_index], THIS_MODULE, 0, &efw->card);
222 	if (err < 0) {
223 		mutex_unlock(&devices_mutex);
224 		return;
225 	}
226 	set_bit(card_index, devices_used);
227 	mutex_unlock(&devices_mutex);
228 
229 	efw->card->private_free = efw_card_free;
230 	efw->card->private_data = efw;
231 
232 	/* prepare response buffer */
233 	snd_efw_resp_buf_size = clamp(snd_efw_resp_buf_size,
234 				      SND_EFW_RESPONSE_MAXIMUM_BYTES, 4096U);
235 	efw->resp_buf = devm_kzalloc(&efw->card->card_dev,
236 				     snd_efw_resp_buf_size, GFP_KERNEL);
237 	if (!efw->resp_buf) {
238 		err = -ENOMEM;
239 		goto error;
240 	}
241 	efw->pull_ptr = efw->push_ptr = efw->resp_buf;
242 	snd_efw_transaction_add_instance(efw);
243 
244 	err = get_hardware_info(efw);
245 	if (err < 0)
246 		goto error;
247 
248 	err = snd_efw_stream_init_duplex(efw);
249 	if (err < 0)
250 		goto error;
251 
252 	snd_efw_proc_init(efw);
253 
254 	if (efw->midi_out_ports || efw->midi_in_ports) {
255 		err = snd_efw_create_midi_devices(efw);
256 		if (err < 0)
257 			goto error;
258 	}
259 
260 	err = snd_efw_create_pcm_devices(efw);
261 	if (err < 0)
262 		goto error;
263 
264 	err = snd_efw_create_hwdep_device(efw);
265 	if (err < 0)
266 		goto error;
267 
268 	err = snd_card_register(efw->card);
269 	if (err < 0)
270 		goto error;
271 
272 	efw->registered = true;
273 
274 	return;
275 error:
276 	snd_card_free(efw->card);
277 	dev_info(&efw->unit->device,
278 		 "Sound card registration failed: %d\n", err);
279 }
280 
281 static int
282 efw_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
283 {
284 	struct snd_efw *efw;
285 
286 	efw = devm_kzalloc(&unit->device, sizeof(struct snd_efw), GFP_KERNEL);
287 	if (efw == NULL)
288 		return -ENOMEM;
289 	efw->unit = fw_unit_get(unit);
290 	dev_set_drvdata(&unit->device, efw);
291 
292 	mutex_init(&efw->mutex);
293 	spin_lock_init(&efw->lock);
294 	init_waitqueue_head(&efw->hwdep_wait);
295 
296 	/* Allocate and register this sound card later. */
297 	INIT_DEFERRABLE_WORK(&efw->dwork, do_registration);
298 	snd_fw_schedule_registration(unit, &efw->dwork);
299 
300 	return 0;
301 }
302 
303 static void efw_update(struct fw_unit *unit)
304 {
305 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
306 
307 	/* Postpone a workqueue for deferred registration. */
308 	if (!efw->registered)
309 		snd_fw_schedule_registration(unit, &efw->dwork);
310 
311 	snd_efw_transaction_bus_reset(efw->unit);
312 
313 	/*
314 	 * After registration, userspace can start packet streaming, then this
315 	 * code block works fine.
316 	 */
317 	if (efw->registered) {
318 		mutex_lock(&efw->mutex);
319 		snd_efw_stream_update_duplex(efw);
320 		mutex_unlock(&efw->mutex);
321 	}
322 }
323 
324 static void efw_remove(struct fw_unit *unit)
325 {
326 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
327 
328 	/*
329 	 * Confirm to stop the work for registration before the sound card is
330 	 * going to be released. The work is not scheduled again because bus
331 	 * reset handler is not called anymore.
332 	 */
333 	cancel_delayed_work_sync(&efw->dwork);
334 
335 	if (efw->registered) {
336 		// Block till all of ALSA character devices are released.
337 		snd_card_free(efw->card);
338 	}
339 
340 	mutex_destroy(&efw->mutex);
341 	fw_unit_put(efw->unit);
342 }
343 
344 static const struct ieee1394_device_id efw_id_table[] = {
345 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_400F),
346 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_1200F),
347 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_8),
348 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12),
349 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12HD),
350 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12_APPLE),
351 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_2),
352 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_4),
353 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_9),
354 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_8),
355 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_HDMI),
356 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_RIP),
357 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_GOLDTOP),
358 	{}
359 };
360 MODULE_DEVICE_TABLE(ieee1394, efw_id_table);
361 
362 static struct fw_driver efw_driver = {
363 	.driver = {
364 		.owner = THIS_MODULE,
365 		.name = KBUILD_MODNAME,
366 		.bus = &fw_bus_type,
367 	},
368 	.probe    = efw_probe,
369 	.update   = efw_update,
370 	.remove   = efw_remove,
371 	.id_table = efw_id_table,
372 };
373 
374 static int __init snd_efw_init(void)
375 {
376 	int err;
377 
378 	err = snd_efw_transaction_register();
379 	if (err < 0)
380 		goto end;
381 
382 	err = driver_register(&efw_driver.driver);
383 	if (err < 0)
384 		snd_efw_transaction_unregister();
385 
386 end:
387 	return err;
388 }
389 
390 static void __exit snd_efw_exit(void)
391 {
392 	snd_efw_transaction_unregister();
393 	driver_unregister(&efw_driver.driver);
394 }
395 
396 module_init(snd_efw_init);
397 module_exit(snd_efw_exit);
398