xref: /openbmc/linux/sound/firewire/dice/dice.c (revision b923cda9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TC Applied Technologies Digital Interface Communications Engine driver
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  */
7 
8 #include "dice.h"
9 
10 MODULE_DESCRIPTION("DICE driver");
11 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
12 MODULE_LICENSE("GPL v2");
13 
14 #define OUI_WEISS		0x001c6a
15 #define OUI_LOUD		0x000ff2
16 #define OUI_FOCUSRITE		0x00130e
17 #define OUI_TCELECTRONIC	0x000166
18 #define OUI_ALESIS		0x000595
19 #define OUI_MAUDIO		0x000d6c
20 #define OUI_MYTEK		0x001ee8
21 #define OUI_SSL			0x0050c2	// Actually ID reserved by IEEE.
22 #define OUI_PRESONUS		0x000a92
23 #define OUI_HARMAN		0x000fd7
24 #define OUI_AVID		0x00a07e
25 
26 #define DICE_CATEGORY_ID	0x04
27 #define WEISS_CATEGORY_ID	0x00
28 #define LOUD_CATEGORY_ID	0x10
29 #define HARMAN_CATEGORY_ID	0x20
30 
31 #define MODEL_ALESIS_IO_BOTH	0x000001
32 
33 static int check_dice_category(struct fw_unit *unit)
34 {
35 	struct fw_device *device = fw_parent_device(unit);
36 	struct fw_csr_iterator it;
37 	int key, val, vendor = -1, model = -1;
38 	unsigned int category;
39 
40 	/*
41 	 * Check that GUID and unit directory are constructed according to DICE
42 	 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
43 	 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
44 	 * ID, and a 22-bit serial number.
45 	 */
46 	fw_csr_iterator_init(&it, unit->directory);
47 	while (fw_csr_iterator_next(&it, &key, &val)) {
48 		switch (key) {
49 		case CSR_SPECIFIER_ID:
50 			vendor = val;
51 			break;
52 		case CSR_MODEL:
53 			model = val;
54 			break;
55 		}
56 	}
57 
58 	if (vendor == OUI_WEISS)
59 		category = WEISS_CATEGORY_ID;
60 	else if (vendor == OUI_LOUD)
61 		category = LOUD_CATEGORY_ID;
62 	else if (vendor == OUI_HARMAN)
63 		category = HARMAN_CATEGORY_ID;
64 	else
65 		category = DICE_CATEGORY_ID;
66 	if (device->config_rom[3] != ((vendor << 8) | category) ||
67 	    device->config_rom[4] >> 22 != model)
68 		return -ENODEV;
69 
70 	return 0;
71 }
72 
73 static int check_clock_caps(struct snd_dice *dice)
74 {
75 	__be32 value;
76 	int err;
77 
78 	/* some very old firmwares don't tell about their clock support */
79 	if (dice->clock_caps > 0) {
80 		err = snd_dice_transaction_read_global(dice,
81 						GLOBAL_CLOCK_CAPABILITIES,
82 						&value, 4);
83 		if (err < 0)
84 			return err;
85 		dice->clock_caps = be32_to_cpu(value);
86 	} else {
87 		/* this should be supported by any device */
88 		dice->clock_caps = CLOCK_CAP_RATE_44100 |
89 				   CLOCK_CAP_RATE_48000 |
90 				   CLOCK_CAP_SOURCE_ARX1 |
91 				   CLOCK_CAP_SOURCE_INTERNAL;
92 	}
93 
94 	return 0;
95 }
96 
97 static void dice_card_strings(struct snd_dice *dice)
98 {
99 	struct snd_card *card = dice->card;
100 	struct fw_device *dev = fw_parent_device(dice->unit);
101 	char vendor[32], model[32];
102 	unsigned int i;
103 	int err;
104 
105 	strcpy(card->driver, "DICE");
106 
107 	strcpy(card->shortname, "DICE");
108 	BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
109 	err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
110 					       card->shortname,
111 					       sizeof(card->shortname));
112 	if (err >= 0) {
113 		/* DICE strings are returned in "always-wrong" endianness */
114 		BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
115 		for (i = 0; i < sizeof(card->shortname); i += 4)
116 			swab32s((u32 *)&card->shortname[i]);
117 		card->shortname[sizeof(card->shortname) - 1] = '\0';
118 	}
119 
120 	strcpy(vendor, "?");
121 	fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
122 	strcpy(model, "?");
123 	fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
124 	snprintf(card->longname, sizeof(card->longname),
125 		 "%s %s (serial %u) at %s, S%d",
126 		 vendor, model, dev->config_rom[4] & 0x3fffff,
127 		 dev_name(&dice->unit->device), 100 << dev->max_speed);
128 
129 	strcpy(card->mixername, "DICE");
130 }
131 
132 static void dice_card_free(struct snd_card *card)
133 {
134 	struct snd_dice *dice = card->private_data;
135 
136 	snd_dice_stream_destroy_duplex(dice);
137 	snd_dice_transaction_destroy(dice);
138 }
139 
140 static void do_registration(struct work_struct *work)
141 {
142 	struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
143 	int err;
144 
145 	if (dice->registered)
146 		return;
147 
148 	err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
149 			   &dice->card);
150 	if (err < 0)
151 		return;
152 	dice->card->private_free = dice_card_free;
153 	dice->card->private_data = dice;
154 
155 	err = snd_dice_transaction_init(dice);
156 	if (err < 0)
157 		goto error;
158 
159 	err = check_clock_caps(dice);
160 	if (err < 0)
161 		goto error;
162 
163 	dice_card_strings(dice);
164 
165 	err = dice->detect_formats(dice);
166 	if (err < 0)
167 		goto error;
168 
169 	err = snd_dice_stream_init_duplex(dice);
170 	if (err < 0)
171 		goto error;
172 
173 	snd_dice_create_proc(dice);
174 
175 	err = snd_dice_create_pcm(dice);
176 	if (err < 0)
177 		goto error;
178 
179 	err = snd_dice_create_midi(dice);
180 	if (err < 0)
181 		goto error;
182 
183 	err = snd_dice_create_hwdep(dice);
184 	if (err < 0)
185 		goto error;
186 
187 	err = snd_card_register(dice->card);
188 	if (err < 0)
189 		goto error;
190 
191 	dice->registered = true;
192 
193 	return;
194 error:
195 	snd_card_free(dice->card);
196 	dev_info(&dice->unit->device,
197 		 "Sound card registration failed: %d\n", err);
198 }
199 
200 static int dice_probe(struct fw_unit *unit,
201 		      const struct ieee1394_device_id *entry)
202 {
203 	struct snd_dice *dice;
204 	int err;
205 
206 	if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
207 		err = check_dice_category(unit);
208 		if (err < 0)
209 			return -ENODEV;
210 	}
211 
212 	/* Allocate this independent of sound card instance. */
213 	dice = devm_kzalloc(&unit->device, sizeof(struct snd_dice), GFP_KERNEL);
214 	if (!dice)
215 		return -ENOMEM;
216 	dice->unit = fw_unit_get(unit);
217 	dev_set_drvdata(&unit->device, dice);
218 
219 	if (!entry->driver_data) {
220 		dice->detect_formats = snd_dice_stream_detect_current_formats;
221 	} else {
222 		dice->detect_formats =
223 				(snd_dice_detect_formats_t)entry->driver_data;
224 	}
225 
226 	// Below models are compliant to IEC 61883-1/6 and have no quirk at high sampling transfer
227 	// frequency.
228 	// * Avid M-Box 3 Pro
229 	// * M-Audio Profire 610
230 	// * M-Audio Profire 2626
231 	if (entry->vendor_id == OUI_MAUDIO || entry->vendor_id == OUI_AVID)
232 		dice->disable_double_pcm_frames = true;
233 
234 	spin_lock_init(&dice->lock);
235 	mutex_init(&dice->mutex);
236 	init_completion(&dice->clock_accepted);
237 	init_waitqueue_head(&dice->hwdep_wait);
238 
239 	/* Allocate and register this sound card later. */
240 	INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
241 	snd_fw_schedule_registration(unit, &dice->dwork);
242 
243 	return 0;
244 }
245 
246 static void dice_remove(struct fw_unit *unit)
247 {
248 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
249 
250 	/*
251 	 * Confirm to stop the work for registration before the sound card is
252 	 * going to be released. The work is not scheduled again because bus
253 	 * reset handler is not called anymore.
254 	 */
255 	cancel_delayed_work_sync(&dice->dwork);
256 
257 	if (dice->registered) {
258 		// Block till all of ALSA character devices are released.
259 		snd_card_free(dice->card);
260 	}
261 
262 	mutex_destroy(&dice->mutex);
263 	fw_unit_put(dice->unit);
264 }
265 
266 static void dice_bus_reset(struct fw_unit *unit)
267 {
268 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
269 
270 	/* Postpone a workqueue for deferred registration. */
271 	if (!dice->registered)
272 		snd_fw_schedule_registration(unit, &dice->dwork);
273 
274 	/* The handler address register becomes initialized. */
275 	snd_dice_transaction_reinit(dice);
276 
277 	/*
278 	 * After registration, userspace can start packet streaming, then this
279 	 * code block works fine.
280 	 */
281 	if (dice->registered) {
282 		mutex_lock(&dice->mutex);
283 		snd_dice_stream_update_duplex(dice);
284 		mutex_unlock(&dice->mutex);
285 	}
286 }
287 
288 #define DICE_INTERFACE	0x000001
289 
290 #define DICE_DEV_ENTRY_TYPICAL(vendor, model, data) \
291 	{ \
292 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | \
293 				  IEEE1394_MATCH_MODEL_ID | \
294 				  IEEE1394_MATCH_SPECIFIER_ID | \
295 				  IEEE1394_MATCH_VERSION, \
296 		.vendor_id	= (vendor), \
297 		.model_id	= (model), \
298 		.specifier_id	= (vendor), \
299 		.version	= DICE_INTERFACE, \
300 		.driver_data = (kernel_ulong_t)(data), \
301 	}
302 
303 static const struct ieee1394_device_id dice_id_table[] = {
304 	// Avid M-Box 3 Pro. To match in probe function.
305 	DICE_DEV_ENTRY_TYPICAL(OUI_AVID, 0x000004, snd_dice_detect_extension_formats),
306 	/* M-Audio Profire 2626 has a different value in version field. */
307 	{
308 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
309 				  IEEE1394_MATCH_MODEL_ID,
310 		.vendor_id	= OUI_MAUDIO,
311 		.model_id	= 0x000010,
312 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
313 	},
314 	/* M-Audio Profire 610 has a different value in version field. */
315 	{
316 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
317 				  IEEE1394_MATCH_MODEL_ID,
318 		.vendor_id	= OUI_MAUDIO,
319 		.model_id	= 0x000011,
320 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
321 	},
322 	/* TC Electronic Konnekt 24D. */
323 	{
324 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
325 				  IEEE1394_MATCH_MODEL_ID,
326 		.vendor_id	= OUI_TCELECTRONIC,
327 		.model_id	= 0x000020,
328 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
329 	},
330 	/* TC Electronic Konnekt 8. */
331 	{
332 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
333 				  IEEE1394_MATCH_MODEL_ID,
334 		.vendor_id	= OUI_TCELECTRONIC,
335 		.model_id	= 0x000021,
336 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
337 	},
338 	/* TC Electronic Studio Konnekt 48. */
339 	{
340 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
341 				  IEEE1394_MATCH_MODEL_ID,
342 		.vendor_id	= OUI_TCELECTRONIC,
343 		.model_id	= 0x000022,
344 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
345 	},
346 	/* TC Electronic Konnekt Live. */
347 	{
348 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
349 				  IEEE1394_MATCH_MODEL_ID,
350 		.vendor_id	= OUI_TCELECTRONIC,
351 		.model_id	= 0x000023,
352 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
353 	},
354 	/* TC Electronic Desktop Konnekt 6. */
355 	{
356 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
357 				  IEEE1394_MATCH_MODEL_ID,
358 		.vendor_id	= OUI_TCELECTRONIC,
359 		.model_id	= 0x000024,
360 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
361 	},
362 	/* TC Electronic Impact Twin. */
363 	{
364 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
365 				  IEEE1394_MATCH_MODEL_ID,
366 		.vendor_id	= OUI_TCELECTRONIC,
367 		.model_id	= 0x000027,
368 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
369 	},
370 	/* TC Electronic Digital Konnekt x32. */
371 	{
372 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
373 				  IEEE1394_MATCH_MODEL_ID,
374 		.vendor_id	= OUI_TCELECTRONIC,
375 		.model_id	= 0x000030,
376 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
377 	},
378 	/* Alesis iO14/iO26. */
379 	{
380 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
381 				  IEEE1394_MATCH_MODEL_ID,
382 		.vendor_id	= OUI_ALESIS,
383 		.model_id	= MODEL_ALESIS_IO_BOTH,
384 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
385 	},
386 	// Alesis MasterControl.
387 	{
388 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
389 				  IEEE1394_MATCH_MODEL_ID,
390 		.vendor_id	= OUI_ALESIS,
391 		.model_id	= 0x000002,
392 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_mastercontrol_formats,
393 	},
394 	/* Mytek Stereo 192 DSD-DAC. */
395 	{
396 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
397 				  IEEE1394_MATCH_MODEL_ID,
398 		.vendor_id	= OUI_MYTEK,
399 		.model_id	= 0x000002,
400 		.driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
401 	},
402 	// Solid State Logic, Duende Classic and Mini.
403 	// NOTE: each field of GUID in config ROM is not compliant to standard
404 	// DICE scheme.
405 	{
406 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
407 				  IEEE1394_MATCH_MODEL_ID,
408 		.vendor_id	= OUI_SSL,
409 		.model_id	= 0x000070,
410 	},
411 	// Presonus FireStudio.
412 	{
413 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
414 				  IEEE1394_MATCH_MODEL_ID,
415 		.vendor_id	= OUI_PRESONUS,
416 		.model_id	= 0x000008,
417 		.driver_data	= (kernel_ulong_t)snd_dice_detect_presonus_formats,
418 	},
419 	// Lexicon I-ONYX FW810S.
420 	{
421 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
422 				  IEEE1394_MATCH_MODEL_ID,
423 		.vendor_id	= OUI_HARMAN,
424 		.model_id	= 0x000001,
425 		.driver_data	= (kernel_ulong_t)snd_dice_detect_harman_formats,
426 	},
427 	{
428 		.match_flags = IEEE1394_MATCH_VERSION,
429 		.version     = DICE_INTERFACE,
430 	},
431 	{ }
432 };
433 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
434 
435 static struct fw_driver dice_driver = {
436 	.driver   = {
437 		.owner	= THIS_MODULE,
438 		.name	= KBUILD_MODNAME,
439 		.bus	= &fw_bus_type,
440 	},
441 	.probe    = dice_probe,
442 	.update   = dice_bus_reset,
443 	.remove   = dice_remove,
444 	.id_table = dice_id_table,
445 };
446 
447 static int __init alsa_dice_init(void)
448 {
449 	return driver_register(&dice_driver.driver);
450 }
451 
452 static void __exit alsa_dice_exit(void)
453 {
454 	driver_unregister(&dice_driver.driver);
455 }
456 
457 module_init(alsa_dice_init);
458 module_exit(alsa_dice_exit);
459