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