xref: /linux/sound/firewire/dice/dice.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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");
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 	strscpy(card->driver, "DICE");
106 
107 	strscpy(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 	strscpy(vendor, "?");
121 	fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
122 	strscpy(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 	strscpy(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 	mutex_destroy(&dice->mutex);
140 	fw_unit_put(dice->unit);
141 }
142 
143 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
144 {
145 	struct snd_card *card;
146 	struct snd_dice *dice;
147 	snd_dice_detect_formats_t detect_formats;
148 	int err;
149 
150 	if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
151 		err = check_dice_category(unit);
152 		if (err < 0)
153 			return -ENODEV;
154 	}
155 
156 	err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, sizeof(*dice), &card);
157 	if (err < 0)
158 		return err;
159 	card->private_free = dice_card_free;
160 
161 	dice = card->private_data;
162 	dice->unit = fw_unit_get(unit);
163 	dev_set_drvdata(&unit->device, dice);
164 	dice->card = card;
165 
166 	if (!entry->driver_data)
167 		detect_formats = snd_dice_stream_detect_current_formats;
168 	else
169 		detect_formats = (snd_dice_detect_formats_t)entry->driver_data;
170 
171 	// Below models are compliant to IEC 61883-1/6 and have no quirk at high sampling transfer
172 	// frequency.
173 	// * Avid M-Box 3 Pro
174 	// * M-Audio Profire 610
175 	// * M-Audio Profire 2626
176 	if (entry->vendor_id == OUI_MAUDIO || entry->vendor_id == OUI_AVID)
177 		dice->disable_double_pcm_frames = true;
178 
179 	spin_lock_init(&dice->lock);
180 	mutex_init(&dice->mutex);
181 	init_completion(&dice->clock_accepted);
182 	init_waitqueue_head(&dice->hwdep_wait);
183 
184 	err = snd_dice_transaction_init(dice);
185 	if (err < 0)
186 		goto error;
187 
188 	err = check_clock_caps(dice);
189 	if (err < 0)
190 		goto error;
191 
192 	dice_card_strings(dice);
193 
194 	err = detect_formats(dice);
195 	if (err < 0)
196 		goto error;
197 
198 	err = snd_dice_stream_init_duplex(dice);
199 	if (err < 0)
200 		goto error;
201 
202 	snd_dice_create_proc(dice);
203 
204 	err = snd_dice_create_pcm(dice);
205 	if (err < 0)
206 		goto error;
207 
208 	err = snd_dice_create_midi(dice);
209 	if (err < 0)
210 		goto error;
211 
212 	err = snd_dice_create_hwdep(dice);
213 	if (err < 0)
214 		goto error;
215 
216 	err = snd_card_register(card);
217 	if (err < 0)
218 		goto error;
219 
220 	return 0;
221 error:
222 	snd_card_free(card);
223 	return err;
224 }
225 
226 static void dice_remove(struct fw_unit *unit)
227 {
228 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
229 
230 	// Block till all of ALSA character devices are released.
231 	snd_card_free(dice->card);
232 }
233 
234 static void dice_bus_reset(struct fw_unit *unit)
235 {
236 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
237 
238 	/* The handler address register becomes initialized. */
239 	snd_dice_transaction_reinit(dice);
240 
241 	guard(mutex)(&dice->mutex);
242 	snd_dice_stream_update_duplex(dice);
243 }
244 
245 #define DICE_INTERFACE	0x000001
246 
247 #define DICE_DEV_ENTRY_TYPICAL(vendor, model, data) \
248 	{ \
249 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | \
250 				  IEEE1394_MATCH_MODEL_ID | \
251 				  IEEE1394_MATCH_SPECIFIER_ID | \
252 				  IEEE1394_MATCH_VERSION, \
253 		.vendor_id	= (vendor), \
254 		.model_id	= (model), \
255 		.specifier_id	= (vendor), \
256 		.version	= DICE_INTERFACE, \
257 		.driver_data = (kernel_ulong_t)(data), \
258 	}
259 
260 static const struct ieee1394_device_id dice_id_table[] = {
261 	// Avid M-Box 3 Pro. To match in probe function.
262 	DICE_DEV_ENTRY_TYPICAL(OUI_AVID, 0x000004, snd_dice_detect_extension_formats),
263 	/* M-Audio Profire 2626 has a different value in version field. */
264 	{
265 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
266 				  IEEE1394_MATCH_MODEL_ID,
267 		.vendor_id	= OUI_MAUDIO,
268 		.model_id	= 0x000010,
269 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
270 	},
271 	/* M-Audio Profire 610 has a different value in version field. */
272 	{
273 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
274 				  IEEE1394_MATCH_MODEL_ID,
275 		.vendor_id	= OUI_MAUDIO,
276 		.model_id	= 0x000011,
277 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
278 	},
279 	/* TC Electronic Konnekt 24D. */
280 	{
281 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
282 				  IEEE1394_MATCH_MODEL_ID,
283 		.vendor_id	= OUI_TCELECTRONIC,
284 		.model_id	= 0x000020,
285 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
286 	},
287 	/* TC Electronic Konnekt 8. */
288 	{
289 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
290 				  IEEE1394_MATCH_MODEL_ID,
291 		.vendor_id	= OUI_TCELECTRONIC,
292 		.model_id	= 0x000021,
293 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
294 	},
295 	/* TC Electronic Studio Konnekt 48. */
296 	{
297 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
298 				  IEEE1394_MATCH_MODEL_ID,
299 		.vendor_id	= OUI_TCELECTRONIC,
300 		.model_id	= 0x000022,
301 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
302 	},
303 	/* TC Electronic Konnekt Live. */
304 	{
305 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
306 				  IEEE1394_MATCH_MODEL_ID,
307 		.vendor_id	= OUI_TCELECTRONIC,
308 		.model_id	= 0x000023,
309 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
310 	},
311 	/* TC Electronic Desktop Konnekt 6. */
312 	{
313 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
314 				  IEEE1394_MATCH_MODEL_ID,
315 		.vendor_id	= OUI_TCELECTRONIC,
316 		.model_id	= 0x000024,
317 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
318 	},
319 	/* TC Electronic Impact Twin. */
320 	{
321 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
322 				  IEEE1394_MATCH_MODEL_ID,
323 		.vendor_id	= OUI_TCELECTRONIC,
324 		.model_id	= 0x000027,
325 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
326 	},
327 	/* TC Electronic Digital Konnekt x32. */
328 	{
329 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
330 				  IEEE1394_MATCH_MODEL_ID,
331 		.vendor_id	= OUI_TCELECTRONIC,
332 		.model_id	= 0x000030,
333 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
334 	},
335 	/* Alesis iO14/iO26. */
336 	{
337 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
338 				  IEEE1394_MATCH_MODEL_ID,
339 		.vendor_id	= OUI_ALESIS,
340 		.model_id	= MODEL_ALESIS_IO_BOTH,
341 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
342 	},
343 	// Alesis MasterControl.
344 	{
345 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
346 				  IEEE1394_MATCH_MODEL_ID,
347 		.vendor_id	= OUI_ALESIS,
348 		.model_id	= 0x000002,
349 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_mastercontrol_formats,
350 	},
351 	/* Mytek Stereo 192 DSD-DAC. */
352 	{
353 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
354 				  IEEE1394_MATCH_MODEL_ID,
355 		.vendor_id	= OUI_MYTEK,
356 		.model_id	= 0x000002,
357 		.driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
358 	},
359 	// Solid State Logic, Duende Classic and Mini.
360 	// NOTE: each field of GUID in config ROM is not compliant to standard
361 	// DICE scheme.
362 	{
363 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
364 				  IEEE1394_MATCH_MODEL_ID,
365 		.vendor_id	= OUI_SSL,
366 		.model_id	= 0x000070,
367 	},
368 	// Presonus FireStudio.
369 	{
370 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
371 				  IEEE1394_MATCH_MODEL_ID,
372 		.vendor_id	= OUI_PRESONUS,
373 		.model_id	= 0x000008,
374 		.driver_data	= (kernel_ulong_t)snd_dice_detect_presonus_formats,
375 	},
376 	// Lexicon I-ONYX FW810S.
377 	{
378 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
379 				  IEEE1394_MATCH_MODEL_ID,
380 		.vendor_id	= OUI_HARMAN,
381 		.model_id	= 0x000001,
382 		.driver_data	= (kernel_ulong_t)snd_dice_detect_harman_formats,
383 	},
384 	// Focusrite Saffire Pro 40 with TCD3070-CH.
385 	// The model has quirk in its GUID, in which model field is 0x000013 and different from
386 	// model ID (0x0000de) in its root/unit directory.
387 	{
388 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
389 				  IEEE1394_MATCH_MODEL_ID,
390 		.vendor_id	= OUI_FOCUSRITE,
391 		.model_id	= 0x0000de,
392 		.driver_data	= (kernel_ulong_t)snd_dice_detect_focusrite_pro40_tcd3070_formats,
393 	},
394 	// Weiss DAC202: 192kHz 2-channel DAC
395 	{
396 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
397 		.vendor_id	= OUI_WEISS,
398 		.model_id	= 0x000007,
399 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
400 	},
401 	// Weiss DAC202: 192kHz 2-channel DAC (Maya edition)
402 	{
403 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
404 		.vendor_id	= OUI_WEISS,
405 		.model_id	= 0x000008,
406 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
407 	},
408 	// Weiss MAN301: 192kHz 2-channel music archive network player
409 	{
410 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
411 		.vendor_id	= OUI_WEISS,
412 		.model_id	= 0x00000b,
413 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
414 	},
415 	// Weiss INT202: 192kHz unidirectional 2-channel digital Firewire face
416 	{
417 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
418 		.vendor_id	= OUI_WEISS,
419 		.model_id	= 0x000006,
420 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
421 	},
422 	// Weiss INT203: 192kHz bidirectional 2-channel digital Firewire face
423 	{
424 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
425 		.vendor_id	= OUI_WEISS,
426 		.model_id	= 0x00000a,
427 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
428 	},
429 	// Weiss ADC2: 192kHz A/D converter with microphone preamps and inputs
430 	{
431 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
432 		.vendor_id	= OUI_WEISS,
433 		.model_id	= 0x000001,
434 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
435 	},
436 	// Weiss DAC2/Minerva: 192kHz 2-channel DAC
437 	{
438 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
439 		.vendor_id	= OUI_WEISS,
440 		.model_id	= 0x000003,
441 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
442 	},
443 	// Weiss Vesta: 192kHz 2-channel Firewire to AES/EBU interface
444 	{
445 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
446 		.vendor_id	= OUI_WEISS,
447 		.model_id	= 0x000002,
448 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
449 	},
450 	// Weiss AFI1: 192kHz 24-channel Firewire to ADAT or AES/EBU face
451 	{
452 		.match_flags	= IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
453 		.vendor_id	= OUI_WEISS,
454 		.model_id	= 0x000004,
455 		.driver_data	= (kernel_ulong_t)snd_dice_detect_weiss_formats,
456 	},
457 	{
458 		.match_flags = IEEE1394_MATCH_VERSION,
459 		.version     = DICE_INTERFACE,
460 	},
461 	{ }
462 };
463 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
464 
465 static struct fw_driver dice_driver = {
466 	.driver   = {
467 		.owner	= THIS_MODULE,
468 		.name	= KBUILD_MODNAME,
469 		.bus	= &fw_bus_type,
470 	},
471 	.probe    = dice_probe,
472 	.update   = dice_bus_reset,
473 	.remove   = dice_remove,
474 	.id_table = dice_id_table,
475 };
476 
477 static int __init alsa_dice_init(void)
478 {
479 	return driver_register(&dice_driver.driver);
480 }
481 
482 static void __exit alsa_dice_exit(void)
483 {
484 	driver_unregister(&dice_driver.driver);
485 }
486 
487 module_init(alsa_dice_init);
488 module_exit(alsa_dice_exit);
489