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 #define OUI_TEAC 0x00022e
26
27 #define DICE_CATEGORY_ID 0x04
28 #define WEISS_CATEGORY_ID 0x00
29 #define LOUD_CATEGORY_ID 0x10
30 #define HARMAN_CATEGORY_ID 0x20
31
32 #define MODEL_ALESIS_IO_BOTH 0x000001
33
check_dice_category(struct fw_unit * unit)34 static int check_dice_category(struct fw_unit *unit)
35 {
36 struct fw_device *device = fw_parent_device(unit);
37 struct fw_csr_iterator it;
38 int key, val, vendor = -1, model = -1;
39 unsigned int category;
40
41 /*
42 * Check that GUID and unit directory are constructed according to DICE
43 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
44 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
45 * ID, and a 22-bit serial number.
46 */
47 fw_csr_iterator_init(&it, unit->directory);
48 while (fw_csr_iterator_next(&it, &key, &val)) {
49 switch (key) {
50 case CSR_SPECIFIER_ID:
51 vendor = val;
52 break;
53 case CSR_MODEL:
54 model = val;
55 break;
56 }
57 }
58
59 if (vendor == OUI_WEISS)
60 category = WEISS_CATEGORY_ID;
61 else if (vendor == OUI_LOUD)
62 category = LOUD_CATEGORY_ID;
63 else if (vendor == OUI_HARMAN)
64 category = HARMAN_CATEGORY_ID;
65 else
66 category = DICE_CATEGORY_ID;
67 if (device->config_rom[3] != ((vendor << 8) | category) ||
68 device->config_rom[4] >> 22 != model)
69 return -ENODEV;
70
71 return 0;
72 }
73
check_clock_caps(struct snd_dice * dice)74 static int check_clock_caps(struct snd_dice *dice)
75 {
76 __be32 value;
77 int err;
78
79 /* some very old firmwares don't tell about their clock support */
80 if (dice->clock_caps > 0) {
81 err = snd_dice_transaction_read_global(dice,
82 GLOBAL_CLOCK_CAPABILITIES,
83 &value, 4);
84 if (err < 0)
85 return err;
86 dice->clock_caps = be32_to_cpu(value);
87 } else {
88 /* this should be supported by any device */
89 dice->clock_caps = CLOCK_CAP_RATE_44100 |
90 CLOCK_CAP_RATE_48000 |
91 CLOCK_CAP_SOURCE_ARX1 |
92 CLOCK_CAP_SOURCE_INTERNAL;
93 }
94
95 return 0;
96 }
97
dice_card_strings(struct snd_dice * dice)98 static void dice_card_strings(struct snd_dice *dice)
99 {
100 struct snd_card *card = dice->card;
101 struct fw_device *dev = fw_parent_device(dice->unit);
102 char vendor[32], model[32];
103 unsigned int i;
104 int err;
105
106 strscpy(card->driver, "DICE");
107
108 strscpy(card->shortname, "DICE");
109 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
110 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
111 card->shortname,
112 sizeof(card->shortname));
113 if (err >= 0) {
114 /* DICE strings are returned in "always-wrong" endianness */
115 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
116 for (i = 0; i < sizeof(card->shortname); i += 4)
117 swab32s((u32 *)&card->shortname[i]);
118 card->shortname[sizeof(card->shortname) - 1] = '\0';
119 }
120
121 strscpy(vendor, "?");
122 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
123 strscpy(model, "?");
124 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
125 scnprintf(card->longname, sizeof(card->longname),
126 "%s %s (serial %u) at %s, S%d",
127 vendor, model, dev->config_rom[4] & 0x3fffff,
128 dev_name(&dice->unit->device), 100 << dev->max_speed);
129
130 strscpy(card->mixername, "DICE");
131 }
132
dice_card_free(struct snd_card * card)133 static void dice_card_free(struct snd_card *card)
134 {
135 struct snd_dice *dice = card->private_data;
136
137 snd_dice_stream_destroy_duplex(dice);
138 snd_dice_transaction_destroy(dice);
139
140 mutex_destroy(&dice->mutex);
141 fw_unit_put(dice->unit);
142 }
143
dice_probe(struct fw_unit * unit,const struct ieee1394_device_id * entry)144 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
145 {
146 struct snd_card *card;
147 struct snd_dice *dice;
148 snd_dice_detect_formats_t detect_formats;
149 int err;
150
151 if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
152 err = check_dice_category(unit);
153 if (err < 0)
154 return -ENODEV;
155 }
156
157 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, sizeof(*dice), &card);
158 if (err < 0)
159 return err;
160 card->private_free = dice_card_free;
161
162 dice = card->private_data;
163 dice->unit = fw_unit_get(unit);
164 dev_set_drvdata(&unit->device, dice);
165 dice->card = card;
166
167 if (!entry->driver_data)
168 detect_formats = snd_dice_stream_detect_current_formats;
169 else
170 detect_formats = (snd_dice_detect_formats_t)entry->driver_data;
171
172 // Below models are compliant to IEC 61883-1/6 and have no quirk at high sampling transfer
173 // frequency.
174 // * Avid M-Box 3 Pro
175 // * M-Audio Profire 610
176 // * M-Audio Profire 2626
177 if (entry->vendor_id == OUI_MAUDIO || entry->vendor_id == OUI_AVID)
178 dice->disable_double_pcm_frames = true;
179
180 spin_lock_init(&dice->lock);
181 mutex_init(&dice->mutex);
182 init_completion(&dice->clock_accepted);
183 init_waitqueue_head(&dice->hwdep_wait);
184
185 err = snd_dice_transaction_init(dice);
186 if (err < 0)
187 goto error;
188
189 err = check_clock_caps(dice);
190 if (err < 0)
191 goto error;
192
193 dice_card_strings(dice);
194
195 err = detect_formats(dice);
196 if (err < 0)
197 goto error;
198
199 err = snd_dice_stream_init_duplex(dice);
200 if (err < 0)
201 goto error;
202
203 snd_dice_create_proc(dice);
204
205 err = snd_dice_create_pcm(dice);
206 if (err < 0)
207 goto error;
208
209 err = snd_dice_create_midi(dice);
210 if (err < 0)
211 goto error;
212
213 err = snd_dice_create_hwdep(dice);
214 if (err < 0)
215 goto error;
216
217 err = snd_card_register(card);
218 if (err < 0)
219 goto error;
220
221 return 0;
222 error:
223 snd_card_free(card);
224 return err;
225 }
226
dice_remove(struct fw_unit * unit)227 static void dice_remove(struct fw_unit *unit)
228 {
229 struct snd_dice *dice = dev_get_drvdata(&unit->device);
230
231 // Block till all of ALSA character devices are released.
232 snd_card_free(dice->card);
233 }
234
dice_bus_reset(struct fw_unit * unit)235 static void dice_bus_reset(struct fw_unit *unit)
236 {
237 struct snd_dice *dice = dev_get_drvdata(&unit->device);
238
239 /* The handler address register becomes initialized. */
240 snd_dice_transaction_reinit(dice);
241
242 guard(mutex)(&dice->mutex);
243 snd_dice_stream_update_duplex(dice);
244 }
245
246 #define DICE_INTERFACE 0x000001
247
248 #define DICE_DEV_ENTRY_TYPICAL(vendor, model, data) \
249 { \
250 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
251 IEEE1394_MATCH_MODEL_ID | \
252 IEEE1394_MATCH_SPECIFIER_ID | \
253 IEEE1394_MATCH_VERSION, \
254 .vendor_id = (vendor), \
255 .model_id = (model), \
256 .specifier_id = (vendor), \
257 .version = DICE_INTERFACE, \
258 .driver_data = (kernel_ulong_t)(data), \
259 }
260
261 static const struct ieee1394_device_id dice_id_table[] = {
262 // Avid M-Box 3 Pro. To match in probe function.
263 DICE_DEV_ENTRY_TYPICAL(OUI_AVID, 0x000004, snd_dice_detect_extension_formats),
264 /* M-Audio Profire 2626 has a different value in version field. */
265 {
266 .match_flags = IEEE1394_MATCH_VENDOR_ID |
267 IEEE1394_MATCH_MODEL_ID,
268 .vendor_id = OUI_MAUDIO,
269 .model_id = 0x000010,
270 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
271 },
272 /* M-Audio Profire 610 has a different value in version field. */
273 {
274 .match_flags = IEEE1394_MATCH_VENDOR_ID |
275 IEEE1394_MATCH_MODEL_ID,
276 .vendor_id = OUI_MAUDIO,
277 .model_id = 0x000011,
278 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
279 },
280 /* TC Electronic Konnekt 24D. */
281 {
282 .match_flags = IEEE1394_MATCH_VENDOR_ID |
283 IEEE1394_MATCH_MODEL_ID,
284 .vendor_id = OUI_TCELECTRONIC,
285 .model_id = 0x000020,
286 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
287 },
288 /* TC Electronic Konnekt 8. */
289 {
290 .match_flags = IEEE1394_MATCH_VENDOR_ID |
291 IEEE1394_MATCH_MODEL_ID,
292 .vendor_id = OUI_TCELECTRONIC,
293 .model_id = 0x000021,
294 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
295 },
296 /* TC Electronic Studio Konnekt 48. */
297 {
298 .match_flags = IEEE1394_MATCH_VENDOR_ID |
299 IEEE1394_MATCH_MODEL_ID,
300 .vendor_id = OUI_TCELECTRONIC,
301 .model_id = 0x000022,
302 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
303 },
304 /* TC Electronic Konnekt Live. */
305 {
306 .match_flags = IEEE1394_MATCH_VENDOR_ID |
307 IEEE1394_MATCH_MODEL_ID,
308 .vendor_id = OUI_TCELECTRONIC,
309 .model_id = 0x000023,
310 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
311 },
312 /* TC Electronic Desktop Konnekt 6. */
313 {
314 .match_flags = IEEE1394_MATCH_VENDOR_ID |
315 IEEE1394_MATCH_MODEL_ID,
316 .vendor_id = OUI_TCELECTRONIC,
317 .model_id = 0x000024,
318 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
319 },
320 /* TC Electronic Impact Twin. */
321 {
322 .match_flags = IEEE1394_MATCH_VENDOR_ID |
323 IEEE1394_MATCH_MODEL_ID,
324 .vendor_id = OUI_TCELECTRONIC,
325 .model_id = 0x000027,
326 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
327 },
328 /* TC Electronic Digital Konnekt x32. */
329 {
330 .match_flags = IEEE1394_MATCH_VENDOR_ID |
331 IEEE1394_MATCH_MODEL_ID,
332 .vendor_id = OUI_TCELECTRONIC,
333 .model_id = 0x000030,
334 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
335 },
336 /* Alesis iO14/iO26. */
337 {
338 .match_flags = IEEE1394_MATCH_VENDOR_ID |
339 IEEE1394_MATCH_MODEL_ID,
340 .vendor_id = OUI_ALESIS,
341 .model_id = MODEL_ALESIS_IO_BOTH,
342 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
343 },
344 // Alesis MasterControl.
345 {
346 .match_flags = IEEE1394_MATCH_VENDOR_ID |
347 IEEE1394_MATCH_MODEL_ID,
348 .vendor_id = OUI_ALESIS,
349 .model_id = 0x000002,
350 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_mastercontrol_formats,
351 },
352 /* Mytek Stereo 192 DSD-DAC. */
353 {
354 .match_flags = IEEE1394_MATCH_VENDOR_ID |
355 IEEE1394_MATCH_MODEL_ID,
356 .vendor_id = OUI_MYTEK,
357 .model_id = 0x000002,
358 .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
359 },
360 // Solid State Logic, Duende Classic and Mini.
361 // NOTE: each field of GUID in config ROM is not compliant to standard
362 // DICE scheme.
363 {
364 .match_flags = IEEE1394_MATCH_VENDOR_ID |
365 IEEE1394_MATCH_MODEL_ID,
366 .vendor_id = OUI_SSL,
367 .model_id = 0x000070,
368 },
369 // Presonus FireStudio.
370 {
371 .match_flags = IEEE1394_MATCH_VENDOR_ID |
372 IEEE1394_MATCH_MODEL_ID,
373 .vendor_id = OUI_PRESONUS,
374 .model_id = 0x000008,
375 .driver_data = (kernel_ulong_t)snd_dice_detect_presonus_formats,
376 },
377 // Lexicon I-ONYX FW810S.
378 {
379 .match_flags = IEEE1394_MATCH_VENDOR_ID |
380 IEEE1394_MATCH_MODEL_ID,
381 .vendor_id = OUI_HARMAN,
382 .model_id = 0x000001,
383 .driver_data = (kernel_ulong_t)snd_dice_detect_harman_formats,
384 },
385 // Focusrite Saffire Pro 40 with TCD3070-CH.
386 // The model has quirk in its GUID, in which model field is 0x000013 and different from
387 // model ID (0x0000de) in its root/unit directory.
388 {
389 .match_flags = IEEE1394_MATCH_VENDOR_ID |
390 IEEE1394_MATCH_MODEL_ID,
391 .vendor_id = OUI_FOCUSRITE,
392 .model_id = 0x0000de,
393 .driver_data = (kernel_ulong_t)snd_dice_detect_focusrite_pro40_tcd3070_formats,
394 },
395 // Weiss DAC202: 192kHz 2-channel DAC
396 {
397 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
398 .vendor_id = OUI_WEISS,
399 .model_id = 0x000007,
400 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
401 },
402 // Weiss DAC202: 192kHz 2-channel DAC (Maya edition)
403 {
404 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
405 .vendor_id = OUI_WEISS,
406 .model_id = 0x000008,
407 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
408 },
409 // Weiss MAN301: 192kHz 2-channel music archive network player
410 {
411 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
412 .vendor_id = OUI_WEISS,
413 .model_id = 0x00000b,
414 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
415 },
416 // Weiss INT202: 192kHz unidirectional 2-channel digital Firewire face
417 {
418 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
419 .vendor_id = OUI_WEISS,
420 .model_id = 0x000006,
421 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
422 },
423 // Weiss INT203: 192kHz bidirectional 2-channel digital Firewire face
424 {
425 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
426 .vendor_id = OUI_WEISS,
427 .model_id = 0x00000a,
428 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
429 },
430 // Weiss ADC2: 192kHz A/D converter with microphone preamps and inputs
431 {
432 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
433 .vendor_id = OUI_WEISS,
434 .model_id = 0x000001,
435 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
436 },
437 // Weiss DAC2/Minerva: 192kHz 2-channel DAC
438 {
439 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
440 .vendor_id = OUI_WEISS,
441 .model_id = 0x000003,
442 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
443 },
444 // Weiss Vesta: 192kHz 2-channel Firewire to AES/EBU interface
445 {
446 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
447 .vendor_id = OUI_WEISS,
448 .model_id = 0x000002,
449 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
450 },
451 // Weiss AFI1: 192kHz 24-channel Firewire to ADAT or AES/EBU face
452 {
453 .match_flags = IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID,
454 .vendor_id = OUI_WEISS,
455 .model_id = 0x000004,
456 .driver_data = (kernel_ulong_t)snd_dice_detect_weiss_formats,
457 },
458 {
459 .match_flags = IEEE1394_MATCH_VERSION,
460 .version = DICE_INTERFACE,
461 },
462 // Tascam IF-FW/DM MkII for DM-3200 and DM-4800.
463 {
464 .match_flags = IEEE1394_MATCH_VENDOR_ID |
465 IEEE1394_MATCH_MODEL_ID |
466 IEEE1394_MATCH_SPECIFIER_ID |
467 IEEE1394_MATCH_VERSION,
468 .vendor_id = OUI_TEAC,
469 .model_id = OUI_TEAC,
470 .specifier_id = OUI_TEAC,
471 .version = 0x800006,
472 .driver_data = (kernel_ulong_t)snd_dice_detect_teac_formats,
473 },
474 { }
475 };
476 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
477
478 static struct fw_driver dice_driver = {
479 .driver = {
480 .owner = THIS_MODULE,
481 .name = KBUILD_MODNAME,
482 .bus = &fw_bus_type,
483 },
484 .probe = dice_probe,
485 .update = dice_bus_reset,
486 .remove = dice_remove,
487 .id_table = dice_id_table,
488 };
489
alsa_dice_init(void)490 static int __init alsa_dice_init(void)
491 {
492 return driver_register(&dice_driver.driver);
493 }
494
alsa_dice_exit(void)495 static void __exit alsa_dice_exit(void)
496 {
497 driver_unregister(&dice_driver.driver);
498 }
499
500 module_init(alsa_dice_init);
501 module_exit(alsa_dice_exit);
502