blob: eee184b05d937f093a641137bce9ad2b1f6eec69 [file] [log] [blame]
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001/*
2 * TC Applied Technologies Digital Interface Communications Engine driver
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +09008#include "dice.h"
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02009
10MODULE_DESCRIPTION("DICE driver");
11MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
12MODULE_LICENSE("GPL v2");
13
Clemens Ladischa471fcd2012-02-13 21:55:13 +010014#define OUI_WEISS 0x001c6a
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090015#define OUI_LOUD 0x000ff2
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +090016#define OUI_FOCUSRITE 0x00130e
Takashi Sakamoto10412c42018-04-22 21:19:24 +090017#define OUI_TCELECTRONIC 0x000166
Takashi Sakamoto28b208f2018-05-02 19:16:44 +090018#define OUI_ALESIS 0x000595
Takashi Sakamoto58579c02018-05-02 19:16:45 +090019#define OUI_MAUDIO 0x000d6c
Melvin Vermeeren7c1543f2018-05-17 21:00:00 +020020#define OUI_MYTEK 0x001ee8
Takashi Sakamotob2e9e1c2019-01-28 20:40:58 +090021#define OUI_SSL 0x0050c2 // Actually ID reserved by IEEE.
Clemens Ladischa471fcd2012-02-13 21:55:13 +010022
23#define DICE_CATEGORY_ID 0x04
24#define WEISS_CATEGORY_ID 0x00
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090025#define LOUD_CATEGORY_ID 0x10
Clemens Ladischcbab3282011-09-04 22:16:02 +020026
Takashi Sakamoto28b208f2018-05-02 19:16:44 +090027#define MODEL_ALESIS_IO_BOTH 0x000001
28
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090029static int check_dice_category(struct fw_unit *unit)
Clemens Ladischcbab3282011-09-04 22:16:02 +020030{
Clemens Ladischcbab3282011-09-04 22:16:02 +020031 struct fw_device *device = fw_parent_device(unit);
32 struct fw_csr_iterator it;
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090033 int key, val, vendor = -1, model = -1;
34 unsigned int category;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090035
Clemens Ladischcbab3282011-09-04 22:16:02 +020036 /*
37 * Check that GUID and unit directory are constructed according to DICE
38 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
Clemens Ladischa471fcd2012-02-13 21:55:13 +010039 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
40 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020041 */
42 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090043 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020044 switch (key) {
45 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090046 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020047 break;
48 case CSR_MODEL:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090049 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020050 break;
51 }
52 }
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +090053
Clemens Ladischa471fcd2012-02-13 21:55:13 +010054 if (vendor == OUI_WEISS)
55 category = WEISS_CATEGORY_ID;
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090056 else if (vendor == OUI_LOUD)
57 category = LOUD_CATEGORY_ID;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010058 else
59 category = DICE_CATEGORY_ID;
60 if (device->config_rom[3] != ((vendor << 8) | category) ||
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090061 device->config_rom[4] >> 22 != model)
62 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +020063
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090064 return 0;
Clemens Ladischcbab3282011-09-04 22:16:02 +020065}
66
Takashi Sakamoto6f688262016-02-08 22:54:19 +090067static int check_clock_caps(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020068{
Clemens Ladischa0301992011-12-04 21:47:00 +010069 __be32 value;
Takashi Sakamoto6f688262016-02-08 22:54:19 +090070 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020071
Clemens Ladischa0301992011-12-04 21:47:00 +010072 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090073 if (dice->clock_caps > 0) {
74 err = snd_dice_transaction_read_global(dice,
75 GLOBAL_CLOCK_CAPABILITIES,
76 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +010077 if (err < 0)
78 return err;
79 dice->clock_caps = be32_to_cpu(value);
80 } else {
81 /* this should be supported by any device */
82 dice->clock_caps = CLOCK_CAP_RATE_44100 |
83 CLOCK_CAP_RATE_48000 |
84 CLOCK_CAP_SOURCE_ARX1 |
85 CLOCK_CAP_SOURCE_INTERNAL;
86 }
87
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020088 return 0;
89}
90
Takashi Sakamoto732d1532014-11-29 00:59:11 +090091static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020092{
93 struct snd_card *card = dice->card;
94 struct fw_device *dev = fw_parent_device(dice->unit);
95 char vendor[32], model[32];
96 unsigned int i;
97 int err;
98
99 strcpy(card->driver, "DICE");
100
101 strcpy(card->shortname, "DICE");
102 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900103 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
104 card->shortname,
105 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200106 if (err >= 0) {
107 /* DICE strings are returned in "always-wrong" endianness */
108 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
109 for (i = 0; i < sizeof(card->shortname); i += 4)
110 swab32s((u32 *)&card->shortname[i]);
111 card->shortname[sizeof(card->shortname) - 1] = '\0';
112 }
113
114 strcpy(vendor, "?");
115 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
116 strcpy(model, "?");
117 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
118 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200119 "%s %s (serial %u) at %s, S%d",
120 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200121 dev_name(&dice->unit->device), 100 << dev->max_speed);
122
123 strcpy(card->mixername, "DICE");
124}
125
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900126static void dice_card_free(struct snd_card *card)
127{
Takashi Sakamoto3babca42018-10-10 15:35:02 +0900128 struct snd_dice *dice = card->private_data;
129
130 snd_dice_stream_destroy_duplex(dice);
131 snd_dice_transaction_destroy(dice);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900132}
133
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900134static void do_registration(struct work_struct *work)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200135{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900136 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200137 int err;
138
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900139 if (dice->registered)
140 return;
141
142 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
143 &dice->card);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200144 if (err < 0)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900145 return;
Takashi Sakamoto3babca42018-10-10 15:35:02 +0900146 dice->card->private_free = dice_card_free;
147 dice->card->private_data = dice;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200148
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900149 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200150 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900151 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100152
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900153 err = check_clock_caps(dice);
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100154 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900155 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200156
157 dice_card_strings(dice);
158
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900159 err = dice->detect_formats(dice);
Takashi Sakamotob60152f2018-05-02 19:16:42 +0900160 if (err < 0)
161 goto error;
162
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900163 err = snd_dice_stream_init_duplex(dice);
164 if (err < 0)
165 goto error;
166
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900167 snd_dice_create_proc(dice);
168
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900169 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200170 if (err < 0)
171 goto error;
172
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900173 err = snd_dice_create_midi(dice);
174 if (err < 0)
175 goto error;
176
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900177 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200178 if (err < 0)
179 goto error;
180
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900181 err = snd_card_register(dice->card);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900182 if (err < 0)
183 goto error;
184
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900185 dice->registered = true;
186
187 return;
188error:
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900189 snd_card_free(dice->card);
190 dev_info(&dice->unit->device,
191 "Sound card registration failed: %d\n", err);
192}
193
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900194static int dice_probe(struct fw_unit *unit,
195 const struct ieee1394_device_id *entry)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900196{
197 struct snd_dice *dice;
198 int err;
199
Takashi Sakamotob2e9e1c2019-01-28 20:40:58 +0900200 if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900201 err = check_dice_category(unit);
202 if (err < 0)
203 return -ENODEV;
204 }
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900205
206 /* Allocate this independent of sound card instance. */
Takashi Sakamoto366a20d2018-10-03 08:21:50 +0900207 dice = devm_kzalloc(&unit->device, sizeof(struct snd_dice), GFP_KERNEL);
208 if (!dice)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900209 return -ENOMEM;
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900210 dice->unit = fw_unit_get(unit);
211 dev_set_drvdata(&unit->device, dice);
212
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900213 if (!entry->driver_data) {
214 dice->detect_formats = snd_dice_stream_detect_current_formats;
215 } else {
216 dice->detect_formats =
217 (snd_dice_detect_formats_t)entry->driver_data;
218 }
219
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900220 spin_lock_init(&dice->lock);
221 mutex_init(&dice->mutex);
222 init_completion(&dice->clock_accepted);
223 init_waitqueue_head(&dice->hwdep_wait);
224
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900225 /* Allocate and register this sound card later. */
226 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900227 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900228
229 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200230}
231
232static void dice_remove(struct fw_unit *unit)
233{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900234 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200235
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900236 /*
237 * Confirm to stop the work for registration before the sound card is
238 * going to be released. The work is not scheduled again because bus
239 * reset handler is not called anymore.
240 */
241 cancel_delayed_work_sync(&dice->dwork);
242
243 if (dice->registered) {
Takashi Sakamotoaedef162018-10-27 17:13:31 +0900244 // Block till all of ALSA character devices are released.
245 snd_card_free(dice->card);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900246 }
Takashi Sakamoto5b14ec22018-10-10 15:35:00 +0900247
248 mutex_destroy(&dice->mutex);
249 fw_unit_put(dice->unit);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200250}
251
252static void dice_bus_reset(struct fw_unit *unit)
253{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900254 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200255
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900256 /* Postpone a workqueue for deferred registration. */
257 if (!dice->registered)
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900258 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900259
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900260 /* The handler address register becomes initialized. */
261 snd_dice_transaction_reinit(dice);
262
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900263 /*
264 * After registration, userspace can start packet streaming, then this
265 * code block works fine.
266 */
267 if (dice->registered) {
268 mutex_lock(&dice->mutex);
269 snd_dice_stream_update_duplex(dice);
270 mutex_unlock(&dice->mutex);
271 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200272}
273
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200274#define DICE_INTERFACE 0x000001
275
276static const struct ieee1394_device_id dice_id_table[] = {
Takashi Sakamoto58579c02018-05-02 19:16:45 +0900277 /* M-Audio Profire 2626 has a different value in version field. */
Takashi Sakamotof8ff65b2016-04-30 22:06:46 +0900278 {
279 .match_flags = IEEE1394_MATCH_VENDOR_ID |
Takashi Sakamoto58579c02018-05-02 19:16:45 +0900280 IEEE1394_MATCH_MODEL_ID,
281 .vendor_id = OUI_MAUDIO,
282 .model_id = 0x000010,
283 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
284 },
285 /* M-Audio Profire 610 has a different value in version field. */
286 {
287 .match_flags = IEEE1394_MATCH_VENDOR_ID |
288 IEEE1394_MATCH_MODEL_ID,
289 .vendor_id = OUI_MAUDIO,
290 .model_id = 0x000011,
291 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
Takashi Sakamotof8ff65b2016-04-30 22:06:46 +0900292 },
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900293 /* TC Electronic Konnekt 24D. */
294 {
295 .match_flags = IEEE1394_MATCH_VENDOR_ID |
296 IEEE1394_MATCH_MODEL_ID,
297 .vendor_id = OUI_TCELECTRONIC,
298 .model_id = 0x000020,
299 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
300 },
301 /* TC Electronic Konnekt 8. */
302 {
303 .match_flags = IEEE1394_MATCH_VENDOR_ID |
304 IEEE1394_MATCH_MODEL_ID,
305 .vendor_id = OUI_TCELECTRONIC,
306 .model_id = 0x000021,
307 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
308 },
309 /* TC Electronic Studio Konnekt 48. */
310 {
311 .match_flags = IEEE1394_MATCH_VENDOR_ID |
312 IEEE1394_MATCH_MODEL_ID,
313 .vendor_id = OUI_TCELECTRONIC,
314 .model_id = 0x000022,
315 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
316 },
317 /* TC Electronic Konnekt Live. */
318 {
319 .match_flags = IEEE1394_MATCH_VENDOR_ID |
320 IEEE1394_MATCH_MODEL_ID,
321 .vendor_id = OUI_TCELECTRONIC,
322 .model_id = 0x000023,
323 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
324 },
325 /* TC Electronic Desktop Konnekt 6. */
326 {
327 .match_flags = IEEE1394_MATCH_VENDOR_ID |
328 IEEE1394_MATCH_MODEL_ID,
329 .vendor_id = OUI_TCELECTRONIC,
330 .model_id = 0x000024,
331 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
332 },
333 /* TC Electronic Impact Twin. */
334 {
335 .match_flags = IEEE1394_MATCH_VENDOR_ID |
336 IEEE1394_MATCH_MODEL_ID,
337 .vendor_id = OUI_TCELECTRONIC,
338 .model_id = 0x000027,
339 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
340 },
Takashi Sakamotod0aa5902018-05-20 14:40:44 +0900341 /* TC Electronic Digital Konnekt x32. */
342 {
343 .match_flags = IEEE1394_MATCH_VENDOR_ID |
344 IEEE1394_MATCH_MODEL_ID,
345 .vendor_id = OUI_TCELECTRONIC,
346 .model_id = 0x000030,
347 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
348 },
Takashi Sakamoto28b208f2018-05-02 19:16:44 +0900349 /* Alesis iO14/iO26. */
350 {
351 .match_flags = IEEE1394_MATCH_VENDOR_ID |
352 IEEE1394_MATCH_MODEL_ID,
353 .vendor_id = OUI_ALESIS,
354 .model_id = MODEL_ALESIS_IO_BOTH,
355 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
356 },
Melvin Vermeeren7c1543f2018-05-17 21:00:00 +0200357 /* Mytek Stereo 192 DSD-DAC. */
358 {
359 .match_flags = IEEE1394_MATCH_VENDOR_ID |
360 IEEE1394_MATCH_MODEL_ID,
361 .vendor_id = OUI_MYTEK,
362 .model_id = 0x000002,
363 .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
364 },
Takashi Sakamotob2e9e1c2019-01-28 20:40:58 +0900365 // Solid State Logic, Duende Classic and Mini.
366 // NOTE: each field of GUID in config ROM is not compliant to standard
367 // DICE scheme.
368 {
369 .match_flags = IEEE1394_MATCH_VENDOR_ID |
370 IEEE1394_MATCH_MODEL_ID,
371 .vendor_id = OUI_SSL,
372 .model_id = 0x000070,
373 },
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900374 {
375 .match_flags = IEEE1394_MATCH_VERSION,
376 .version = DICE_INTERFACE,
377 },
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200378 { }
379};
380MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
381
382static struct fw_driver dice_driver = {
383 .driver = {
384 .owner = THIS_MODULE,
385 .name = KBUILD_MODNAME,
386 .bus = &fw_bus_type,
387 },
388 .probe = dice_probe,
389 .update = dice_bus_reset,
390 .remove = dice_remove,
391 .id_table = dice_id_table,
392};
393
394static int __init alsa_dice_init(void)
395{
396 return driver_register(&dice_driver.driver);
397}
398
399static void __exit alsa_dice_exit(void)
400{
401 driver_unregister(&dice_driver.driver);
402}
403
404module_init(alsa_dice_init);
405module_exit(alsa_dice_exit);