blob: 774eb2205668cac941e9c44319f9a32f186ac2c5 [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
Clemens Ladischa471fcd2012-02-13 21:55:13 +010021
22#define DICE_CATEGORY_ID 0x04
23#define WEISS_CATEGORY_ID 0x00
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090024#define LOUD_CATEGORY_ID 0x10
Clemens Ladischcbab3282011-09-04 22:16:02 +020025
Takashi Sakamoto28b208f2018-05-02 19:16:44 +090026#define MODEL_ALESIS_IO_BOTH 0x000001
27
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090028static int check_dice_category(struct fw_unit *unit)
Clemens Ladischcbab3282011-09-04 22:16:02 +020029{
Clemens Ladischcbab3282011-09-04 22:16:02 +020030 struct fw_device *device = fw_parent_device(unit);
31 struct fw_csr_iterator it;
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090032 int key, val, vendor = -1, model = -1;
33 unsigned int category;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090034
Clemens Ladischcbab3282011-09-04 22:16:02 +020035 /*
36 * Check that GUID and unit directory are constructed according to DICE
37 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
Clemens Ladischa471fcd2012-02-13 21:55:13 +010038 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
39 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020040 */
41 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090042 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020043 switch (key) {
44 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090045 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020046 break;
47 case CSR_MODEL:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090048 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020049 break;
50 }
51 }
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +090052
Clemens Ladischa471fcd2012-02-13 21:55:13 +010053 if (vendor == OUI_WEISS)
54 category = WEISS_CATEGORY_ID;
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090055 else if (vendor == OUI_LOUD)
56 category = LOUD_CATEGORY_ID;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010057 else
58 category = DICE_CATEGORY_ID;
59 if (device->config_rom[3] != ((vendor << 8) | category) ||
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090060 device->config_rom[4] >> 22 != model)
61 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +020062
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090063 return 0;
Clemens Ladischcbab3282011-09-04 22:16:02 +020064}
65
Takashi Sakamoto6f688262016-02-08 22:54:19 +090066static int check_clock_caps(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020067{
Clemens Ladischa0301992011-12-04 21:47:00 +010068 __be32 value;
Takashi Sakamoto6f688262016-02-08 22:54:19 +090069 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020070
Clemens Ladischa0301992011-12-04 21:47:00 +010071 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090072 if (dice->clock_caps > 0) {
73 err = snd_dice_transaction_read_global(dice,
74 GLOBAL_CLOCK_CAPABILITIES,
75 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +010076 if (err < 0)
77 return err;
78 dice->clock_caps = be32_to_cpu(value);
79 } else {
80 /* this should be supported by any device */
81 dice->clock_caps = CLOCK_CAP_RATE_44100 |
82 CLOCK_CAP_RATE_48000 |
83 CLOCK_CAP_SOURCE_ARX1 |
84 CLOCK_CAP_SOURCE_INTERNAL;
85 }
86
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020087 return 0;
88}
89
Takashi Sakamoto732d1532014-11-29 00:59:11 +090090static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020091{
92 struct snd_card *card = dice->card;
93 struct fw_device *dev = fw_parent_device(dice->unit);
94 char vendor[32], model[32];
95 unsigned int i;
96 int err;
97
98 strcpy(card->driver, "DICE");
99
100 strcpy(card->shortname, "DICE");
101 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900102 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
103 card->shortname,
104 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200105 if (err >= 0) {
106 /* DICE strings are returned in "always-wrong" endianness */
107 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
108 for (i = 0; i < sizeof(card->shortname); i += 4)
109 swab32s((u32 *)&card->shortname[i]);
110 card->shortname[sizeof(card->shortname) - 1] = '\0';
111 }
112
113 strcpy(vendor, "?");
114 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
115 strcpy(model, "?");
116 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
117 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200118 "%s %s (serial %u) at %s, S%d",
119 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200120 dev_name(&dice->unit->device), 100 << dev->max_speed);
121
122 strcpy(card->mixername, "DICE");
123}
124
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900125static void dice_free(struct snd_dice *dice)
126{
127 snd_dice_stream_destroy_duplex(dice);
128 snd_dice_transaction_destroy(dice);
129 fw_unit_put(dice->unit);
130
131 mutex_destroy(&dice->mutex);
132 kfree(dice);
133}
134
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900135/*
136 * This module releases the FireWire unit data after all ALSA character devices
137 * are released by applications. This is for releasing stream data or finishing
138 * transactions safely. Thus at returning from .remove(), this module still keep
139 * references for the unit.
140 */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900141static void dice_card_free(struct snd_card *card)
142{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900143 dice_free(card->private_data);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900144}
145
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900146static void do_registration(struct work_struct *work)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200147{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900148 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200149 int err;
150
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900151 if (dice->registered)
152 return;
153
154 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
155 &dice->card);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200156 if (err < 0)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900157 return;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200158
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900159 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200160 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900161 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100162
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900163 err = check_clock_caps(dice);
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100164 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900165 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200166
167 dice_card_strings(dice);
168
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900169 err = dice->detect_formats(dice);
Takashi Sakamotob60152f2018-05-02 19:16:42 +0900170 if (err < 0)
171 goto error;
172
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900173 err = snd_dice_stream_init_duplex(dice);
174 if (err < 0)
175 goto error;
176
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900177 snd_dice_create_proc(dice);
178
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900179 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200180 if (err < 0)
181 goto error;
182
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900183 err = snd_dice_create_midi(dice);
184 if (err < 0)
185 goto error;
186
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900187 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200188 if (err < 0)
189 goto error;
190
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900191 err = snd_card_register(dice->card);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900192 if (err < 0)
193 goto error;
194
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900195 /*
196 * After registered, dice instance can be released corresponding to
197 * releasing the sound card instance.
198 */
199 dice->card->private_free = dice_card_free;
200 dice->card->private_data = dice;
201 dice->registered = true;
202
203 return;
204error:
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900205 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900206 snd_dice_transaction_destroy(dice);
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900207 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900208 snd_card_free(dice->card);
209 dev_info(&dice->unit->device,
210 "Sound card registration failed: %d\n", err);
211}
212
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900213static int dice_probe(struct fw_unit *unit,
214 const struct ieee1394_device_id *entry)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900215{
216 struct snd_dice *dice;
217 int err;
218
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900219 if (!entry->driver_data) {
220 err = check_dice_category(unit);
221 if (err < 0)
222 return -ENODEV;
223 }
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900224
225 /* Allocate this independent of sound card instance. */
226 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
227 if (dice == NULL)
228 return -ENOMEM;
229
230 dice->unit = fw_unit_get(unit);
231 dev_set_drvdata(&unit->device, dice);
232
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900233 if (!entry->driver_data) {
234 dice->detect_formats = snd_dice_stream_detect_current_formats;
235 } else {
236 dice->detect_formats =
237 (snd_dice_detect_formats_t)entry->driver_data;
238 }
239
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900240 spin_lock_init(&dice->lock);
241 mutex_init(&dice->mutex);
242 init_completion(&dice->clock_accepted);
243 init_waitqueue_head(&dice->hwdep_wait);
244
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900245 /* Allocate and register this sound card later. */
246 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900247 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900248
249 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200250}
251
252static void dice_remove(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 /*
257 * Confirm to stop the work for registration before the sound card is
258 * going to be released. The work is not scheduled again because bus
259 * reset handler is not called anymore.
260 */
261 cancel_delayed_work_sync(&dice->dwork);
262
263 if (dice->registered) {
264 /* No need to wait for releasing card object in this context. */
265 snd_card_free_when_closed(dice->card);
266 } else {
267 /* Don't forget this case. */
268 dice_free(dice);
269 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200270}
271
272static void dice_bus_reset(struct fw_unit *unit)
273{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900274 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200275
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900276 /* Postpone a workqueue for deferred registration. */
277 if (!dice->registered)
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900278 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900279
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900280 /* The handler address register becomes initialized. */
281 snd_dice_transaction_reinit(dice);
282
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900283 /*
284 * After registration, userspace can start packet streaming, then this
285 * code block works fine.
286 */
287 if (dice->registered) {
288 mutex_lock(&dice->mutex);
289 snd_dice_stream_update_duplex(dice);
290 mutex_unlock(&dice->mutex);
291 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200292}
293
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200294#define DICE_INTERFACE 0x000001
295
296static const struct ieee1394_device_id dice_id_table[] = {
Takashi Sakamoto58579c02018-05-02 19:16:45 +0900297 /* M-Audio Profire 2626 has a different value in version field. */
Takashi Sakamotof8ff65b2016-04-30 22:06:46 +0900298 {
299 .match_flags = IEEE1394_MATCH_VENDOR_ID |
Takashi Sakamoto58579c02018-05-02 19:16:45 +0900300 IEEE1394_MATCH_MODEL_ID,
301 .vendor_id = OUI_MAUDIO,
302 .model_id = 0x000010,
303 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
304 },
305 /* M-Audio Profire 610 has a different value in version field. */
306 {
307 .match_flags = IEEE1394_MATCH_VENDOR_ID |
308 IEEE1394_MATCH_MODEL_ID,
309 .vendor_id = OUI_MAUDIO,
310 .model_id = 0x000011,
311 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
Takashi Sakamotof8ff65b2016-04-30 22:06:46 +0900312 },
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900313 /* TC Electronic Konnekt 24D. */
314 {
315 .match_flags = IEEE1394_MATCH_VENDOR_ID |
316 IEEE1394_MATCH_MODEL_ID,
317 .vendor_id = OUI_TCELECTRONIC,
318 .model_id = 0x000020,
319 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
320 },
321 /* TC Electronic Konnekt 8. */
322 {
323 .match_flags = IEEE1394_MATCH_VENDOR_ID |
324 IEEE1394_MATCH_MODEL_ID,
325 .vendor_id = OUI_TCELECTRONIC,
326 .model_id = 0x000021,
327 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
328 },
329 /* TC Electronic Studio Konnekt 48. */
330 {
331 .match_flags = IEEE1394_MATCH_VENDOR_ID |
332 IEEE1394_MATCH_MODEL_ID,
333 .vendor_id = OUI_TCELECTRONIC,
334 .model_id = 0x000022,
335 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
336 },
337 /* TC Electronic Konnekt Live. */
338 {
339 .match_flags = IEEE1394_MATCH_VENDOR_ID |
340 IEEE1394_MATCH_MODEL_ID,
341 .vendor_id = OUI_TCELECTRONIC,
342 .model_id = 0x000023,
343 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
344 },
345 /* TC Electronic Desktop Konnekt 6. */
346 {
347 .match_flags = IEEE1394_MATCH_VENDOR_ID |
348 IEEE1394_MATCH_MODEL_ID,
349 .vendor_id = OUI_TCELECTRONIC,
350 .model_id = 0x000024,
351 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
352 },
353 /* TC Electronic Impact Twin. */
354 {
355 .match_flags = IEEE1394_MATCH_VENDOR_ID |
356 IEEE1394_MATCH_MODEL_ID,
357 .vendor_id = OUI_TCELECTRONIC,
358 .model_id = 0x000027,
359 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
360 },
Takashi Sakamotod0aa5902018-05-20 14:40:44 +0900361 /* TC Electronic Digital Konnekt x32. */
362 {
363 .match_flags = IEEE1394_MATCH_VENDOR_ID |
364 IEEE1394_MATCH_MODEL_ID,
365 .vendor_id = OUI_TCELECTRONIC,
366 .model_id = 0x000030,
367 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
368 },
Takashi Sakamoto28b208f2018-05-02 19:16:44 +0900369 /* Alesis iO14/iO26. */
370 {
371 .match_flags = IEEE1394_MATCH_VENDOR_ID |
372 IEEE1394_MATCH_MODEL_ID,
373 .vendor_id = OUI_ALESIS,
374 .model_id = MODEL_ALESIS_IO_BOTH,
375 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
376 },
Melvin Vermeeren7c1543f2018-05-17 21:00:00 +0200377 /* Mytek Stereo 192 DSD-DAC. */
378 {
379 .match_flags = IEEE1394_MATCH_VENDOR_ID |
380 IEEE1394_MATCH_MODEL_ID,
381 .vendor_id = OUI_MYTEK,
382 .model_id = 0x000002,
383 .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
384 },
Takashi Sakamotof1f0f332018-05-02 19:16:43 +0900385 {
386 .match_flags = IEEE1394_MATCH_VERSION,
387 .version = DICE_INTERFACE,
388 },
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200389 { }
390};
391MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
392
393static struct fw_driver dice_driver = {
394 .driver = {
395 .owner = THIS_MODULE,
396 .name = KBUILD_MODNAME,
397 .bus = &fw_bus_type,
398 },
399 .probe = dice_probe,
400 .update = dice_bus_reset,
401 .remove = dice_remove,
402 .id_table = dice_id_table,
403};
404
405static int __init alsa_dice_init(void)
406{
407 return driver_register(&dice_driver.driver);
408}
409
410static void __exit alsa_dice_exit(void)
411{
412 driver_unregister(&dice_driver.driver);
413}
414
415module_init(alsa_dice_init);
416module_exit(alsa_dice_exit);