blob: 0d3d36fb15408762ba9f58565b811e31d8a5b903 [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 Sakamoto7c2d4c02014-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 Sakamoto7cafc652016-03-07 22:35:45 +090016#define OUI_FOCUSRITE 0x00130e
Takashi Sakamotoc2424852018-04-22 21:19:24 +090017#define OUI_TCELECTRONIC 0x000166
Clemens Ladischa471fcd2012-02-13 21:55:13 +010018
19#define DICE_CATEGORY_ID 0x04
20#define WEISS_CATEGORY_ID 0x00
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090021#define LOUD_CATEGORY_ID 0x10
Clemens Ladischcbab3282011-09-04 22:16:02 +020022
Takashi Sakamoto7cafc652016-03-07 22:35:45 +090023/*
24 * Some models support several isochronous channels, while these streams are not
25 * always available. In this case, add the model name to this list.
26 */
27static bool force_two_pcm_support(struct fw_unit *unit)
28{
29 const char *const models[] = {
30 /* TC Electronic models. */
31 "StudioKonnekt48",
32 /* Focusrite models. */
33 "SAFFIRE_PRO_40",
34 "LIQUID_SAFFIRE_56",
35 "SAFFIRE_PRO_40_1",
36 };
37 char model[32];
38 unsigned int i;
39 int err;
40
41 err = fw_csr_string(unit->directory, CSR_MODEL, model, sizeof(model));
42 if (err < 0)
43 return false;
44
45 for (i = 0; i < ARRAY_SIZE(models); i++) {
46 if (strcmp(models[i], model) == 0)
47 break;
48 }
49
50 return i < ARRAY_SIZE(models);
51}
52
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090053static int check_dice_category(struct fw_unit *unit)
Clemens Ladischcbab3282011-09-04 22:16:02 +020054{
Clemens Ladischcbab3282011-09-04 22:16:02 +020055 struct fw_device *device = fw_parent_device(unit);
56 struct fw_csr_iterator it;
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090057 int key, val, vendor = -1, model = -1;
58 unsigned int category;
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090059
Clemens Ladischcbab3282011-09-04 22:16:02 +020060 /*
61 * Check that GUID and unit directory are constructed according to DICE
62 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
Clemens Ladischa471fcd2012-02-13 21:55:13 +010063 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
64 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020065 */
66 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090067 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020068 switch (key) {
69 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090070 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020071 break;
72 case CSR_MODEL:
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090073 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020074 break;
75 }
76 }
Takashi Sakamoto7cafc652016-03-07 22:35:45 +090077
78 if (vendor == OUI_FOCUSRITE || vendor == OUI_TCELECTRONIC) {
79 if (force_two_pcm_support(unit))
80 return 0;
81 }
82
Clemens Ladischa471fcd2012-02-13 21:55:13 +010083 if (vendor == OUI_WEISS)
84 category = WEISS_CATEGORY_ID;
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090085 else if (vendor == OUI_LOUD)
86 category = LOUD_CATEGORY_ID;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010087 else
88 category = DICE_CATEGORY_ID;
89 if (device->config_rom[3] != ((vendor << 8) | category) ||
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090090 device->config_rom[4] >> 22 != model)
91 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +020092
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090093 return 0;
Clemens Ladischcbab3282011-09-04 22:16:02 +020094}
95
Takashi Sakamoto6f688262016-02-08 22:54:19 +090096static int check_clock_caps(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020097{
Clemens Ladischa0301992011-12-04 21:47:00 +010098 __be32 value;
Takashi Sakamoto6f688262016-02-08 22:54:19 +090099 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200100
Clemens Ladischa0301992011-12-04 21:47:00 +0100101 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900102 if (dice->clock_caps > 0) {
103 err = snd_dice_transaction_read_global(dice,
104 GLOBAL_CLOCK_CAPABILITIES,
105 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +0100106 if (err < 0)
107 return err;
108 dice->clock_caps = be32_to_cpu(value);
109 } else {
110 /* this should be supported by any device */
111 dice->clock_caps = CLOCK_CAP_RATE_44100 |
112 CLOCK_CAP_RATE_48000 |
113 CLOCK_CAP_SOURCE_ARX1 |
114 CLOCK_CAP_SOURCE_INTERNAL;
115 }
116
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200117 return 0;
118}
119
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900120static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200121{
122 struct snd_card *card = dice->card;
123 struct fw_device *dev = fw_parent_device(dice->unit);
124 char vendor[32], model[32];
125 unsigned int i;
126 int err;
127
128 strcpy(card->driver, "DICE");
129
130 strcpy(card->shortname, "DICE");
131 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900132 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
133 card->shortname,
134 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200135 if (err >= 0) {
136 /* DICE strings are returned in "always-wrong" endianness */
137 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
138 for (i = 0; i < sizeof(card->shortname); i += 4)
139 swab32s((u32 *)&card->shortname[i]);
140 card->shortname[sizeof(card->shortname) - 1] = '\0';
141 }
142
143 strcpy(vendor, "?");
144 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
145 strcpy(model, "?");
146 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
147 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200148 "%s %s (serial %u) at %s, S%d",
149 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200150 dev_name(&dice->unit->device), 100 << dev->max_speed);
151
152 strcpy(card->mixername, "DICE");
153}
154
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900155static void dice_free(struct snd_dice *dice)
156{
157 snd_dice_stream_destroy_duplex(dice);
158 snd_dice_transaction_destroy(dice);
159 fw_unit_put(dice->unit);
160
161 mutex_destroy(&dice->mutex);
162 kfree(dice);
163}
164
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900165/*
166 * This module releases the FireWire unit data after all ALSA character devices
167 * are released by applications. This is for releasing stream data or finishing
168 * transactions safely. Thus at returning from .remove(), this module still keep
169 * references for the unit.
170 */
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900171static void dice_card_free(struct snd_card *card)
172{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900173 dice_free(card->private_data);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900174}
175
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900176static void do_registration(struct work_struct *work)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200177{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900178 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200179 int err;
180
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900181 if (dice->registered)
182 return;
183
184 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
185 &dice->card);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200186 if (err < 0)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900187 return;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200188
Takashi Sakamoto7cafc652016-03-07 22:35:45 +0900189 if (force_two_pcm_support(dice->unit))
190 dice->force_two_pcms = true;
191
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900192 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200193 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900194 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100195
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900196 err = check_clock_caps(dice);
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100197 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900198 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200199
200 dice_card_strings(dice);
201
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900202 err = snd_dice_stream_init_duplex(dice);
203 if (err < 0)
204 goto error;
205
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900206 snd_dice_create_proc(dice);
207
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900208 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200209 if (err < 0)
210 goto error;
211
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900212 err = snd_dice_create_midi(dice);
213 if (err < 0)
214 goto error;
215
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900216 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200217 if (err < 0)
218 goto error;
219
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900220 err = snd_card_register(dice->card);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900221 if (err < 0)
222 goto error;
223
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900224 /*
225 * After registered, dice instance can be released corresponding to
226 * releasing the sound card instance.
227 */
228 dice->card->private_free = dice_card_free;
229 dice->card->private_data = dice;
230 dice->registered = true;
231
232 return;
233error:
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900234 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900235 snd_dice_transaction_destroy(dice);
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900236 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900237 snd_card_free(dice->card);
238 dev_info(&dice->unit->device,
239 "Sound card registration failed: %d\n", err);
240}
241
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900242static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
243{
244 struct snd_dice *dice;
245 int err;
246
247 err = check_dice_category(unit);
248 if (err < 0)
249 return -ENODEV;
250
251 /* Allocate this independent of sound card instance. */
252 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
253 if (dice == NULL)
254 return -ENOMEM;
255
256 dice->unit = fw_unit_get(unit);
257 dev_set_drvdata(&unit->device, dice);
258
259 spin_lock_init(&dice->lock);
260 mutex_init(&dice->mutex);
261 init_completion(&dice->clock_accepted);
262 init_waitqueue_head(&dice->hwdep_wait);
263
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900264 /* Allocate and register this sound card later. */
265 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900266 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900267
268 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200269}
270
271static void dice_remove(struct fw_unit *unit)
272{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900273 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200274
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900275 /*
276 * Confirm to stop the work for registration before the sound card is
277 * going to be released. The work is not scheduled again because bus
278 * reset handler is not called anymore.
279 */
280 cancel_delayed_work_sync(&dice->dwork);
281
282 if (dice->registered) {
283 /* No need to wait for releasing card object in this context. */
284 snd_card_free_when_closed(dice->card);
285 } else {
286 /* Don't forget this case. */
287 dice_free(dice);
288 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200289}
290
291static void dice_bus_reset(struct fw_unit *unit)
292{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900293 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200294
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900295 /* Postpone a workqueue for deferred registration. */
296 if (!dice->registered)
Takashi Sakamoto923f92e2016-03-31 08:47:04 +0900297 snd_fw_schedule_registration(unit, &dice->dwork);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900298
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900299 /* The handler address register becomes initialized. */
300 snd_dice_transaction_reinit(dice);
301
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900302 /*
303 * After registration, userspace can start packet streaming, then this
304 * code block works fine.
305 */
306 if (dice->registered) {
307 mutex_lock(&dice->mutex);
308 snd_dice_stream_update_duplex(dice);
309 mutex_unlock(&dice->mutex);
310 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200311}
312
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200313#define DICE_INTERFACE 0x000001
314
315static const struct ieee1394_device_id dice_id_table[] = {
316 {
Clemens Ladischcbab3282011-09-04 22:16:02 +0200317 .match_flags = IEEE1394_MATCH_VERSION,
318 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200319 },
Takashi Sakamotof8ff65b2016-04-30 22:06:46 +0900320 /* M-Audio Profire 610/2626 has a different value in version field. */
321 {
322 .match_flags = IEEE1394_MATCH_VENDOR_ID |
323 IEEE1394_MATCH_SPECIFIER_ID,
324 .vendor_id = 0x000d6c,
325 .specifier_id = 0x000d6c,
326 },
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200327 { }
328};
329MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
330
331static struct fw_driver dice_driver = {
332 .driver = {
333 .owner = THIS_MODULE,
334 .name = KBUILD_MODNAME,
335 .bus = &fw_bus_type,
336 },
337 .probe = dice_probe,
338 .update = dice_bus_reset,
339 .remove = dice_remove,
340 .id_table = dice_id_table,
341};
342
343static int __init alsa_dice_init(void)
344{
345 return driver_register(&dice_driver.driver);
346}
347
348static void __exit alsa_dice_exit(void)
349{
350 driver_unregister(&dice_driver.driver);
351}
352
353module_init(alsa_dice_init);
354module_exit(alsa_dice_exit);