blob: 8b64aef31a864523d428e6fa26b8c6a5f45db154 [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
17#define OUI_TCELECTRONIC 0x001486
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 Sakamotob59fb192015-12-31 13:58:12 +090023#define PROBE_DELAY_MS (2 * MSEC_PER_SEC)
24
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +090025/*
26 * Some models support several isochronous channels, while these streams are not
27 * always available. In this case, add the model name to this list.
28 */
29static bool force_two_pcm_support(struct fw_unit *unit)
30{
31 const char *const models[] = {
32 /* TC Electronic models. */
33 "StudioKonnekt48",
34 /* Focusrite models. */
35 "SAFFIRE_PRO_40",
36 "LIQUID_SAFFIRE_56",
37 "SAFFIRE_PRO_40_1",
38 };
39 char model[32];
40 unsigned int i;
41 int err;
42
43 err = fw_csr_string(unit->directory, CSR_MODEL, model, sizeof(model));
44 if (err < 0)
45 return false;
46
47 for (i = 0; i < ARRAY_SIZE(models); i++) {
48 if (strcmp(models[i], model) == 0)
49 break;
50 }
51
52 return i < ARRAY_SIZE(models);
53}
54
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090055static int check_dice_category(struct fw_unit *unit)
Clemens Ladischcbab3282011-09-04 22:16:02 +020056{
Clemens Ladischcbab3282011-09-04 22:16:02 +020057 struct fw_device *device = fw_parent_device(unit);
58 struct fw_csr_iterator it;
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090059 int key, val, vendor = -1, model = -1;
60 unsigned int category;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090061
Clemens Ladischcbab3282011-09-04 22:16:02 +020062 /*
63 * Check that GUID and unit directory are constructed according to DICE
64 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
Clemens Ladischa471fcd2012-02-13 21:55:13 +010065 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
66 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020067 */
68 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090069 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020070 switch (key) {
71 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090072 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020073 break;
74 case CSR_MODEL:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090075 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020076 break;
77 }
78 }
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +090079
80 if (vendor == OUI_FOCUSRITE || vendor == OUI_TCELECTRONIC) {
81 if (force_two_pcm_support(unit))
82 return 0;
83 }
84
Clemens Ladischa471fcd2012-02-13 21:55:13 +010085 if (vendor == OUI_WEISS)
86 category = WEISS_CATEGORY_ID;
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090087 else if (vendor == OUI_LOUD)
88 category = LOUD_CATEGORY_ID;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010089 else
90 category = DICE_CATEGORY_ID;
91 if (device->config_rom[3] != ((vendor << 8) | category) ||
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090092 device->config_rom[4] >> 22 != model)
93 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +020094
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090095 return 0;
Clemens Ladischcbab3282011-09-04 22:16:02 +020096}
97
Takashi Sakamoto6f688262016-02-08 22:54:19 +090098static int check_clock_caps(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020099{
Clemens Ladischa0301992011-12-04 21:47:00 +0100100 __be32 value;
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900101 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200102
Clemens Ladischa0301992011-12-04 21:47:00 +0100103 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900104 if (dice->clock_caps > 0) {
105 err = snd_dice_transaction_read_global(dice,
106 GLOBAL_CLOCK_CAPABILITIES,
107 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +0100108 if (err < 0)
109 return err;
110 dice->clock_caps = be32_to_cpu(value);
111 } else {
112 /* this should be supported by any device */
113 dice->clock_caps = CLOCK_CAP_RATE_44100 |
114 CLOCK_CAP_RATE_48000 |
115 CLOCK_CAP_SOURCE_ARX1 |
116 CLOCK_CAP_SOURCE_INTERNAL;
117 }
118
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200119 return 0;
120}
121
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900122static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200123{
124 struct snd_card *card = dice->card;
125 struct fw_device *dev = fw_parent_device(dice->unit);
126 char vendor[32], model[32];
127 unsigned int i;
128 int err;
129
130 strcpy(card->driver, "DICE");
131
132 strcpy(card->shortname, "DICE");
133 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900134 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
135 card->shortname,
136 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200137 if (err >= 0) {
138 /* DICE strings are returned in "always-wrong" endianness */
139 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
140 for (i = 0; i < sizeof(card->shortname); i += 4)
141 swab32s((u32 *)&card->shortname[i]);
142 card->shortname[sizeof(card->shortname) - 1] = '\0';
143 }
144
145 strcpy(vendor, "?");
146 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
147 strcpy(model, "?");
148 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
149 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200150 "%s %s (serial %u) at %s, S%d",
151 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200152 dev_name(&dice->unit->device), 100 << dev->max_speed);
153
154 strcpy(card->mixername, "DICE");
155}
156
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900157static void dice_free(struct snd_dice *dice)
158{
159 snd_dice_stream_destroy_duplex(dice);
160 snd_dice_transaction_destroy(dice);
161 fw_unit_put(dice->unit);
162
163 mutex_destroy(&dice->mutex);
164 kfree(dice);
165}
166
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900167/*
168 * This module releases the FireWire unit data after all ALSA character devices
169 * are released by applications. This is for releasing stream data or finishing
170 * transactions safely. Thus at returning from .remove(), this module still keep
171 * references for the unit.
172 */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900173static void dice_card_free(struct snd_card *card)
174{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900175 dice_free(card->private_data);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900176}
177
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900178static void do_registration(struct work_struct *work)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200179{
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900180 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200181 int err;
182
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900183 if (dice->registered)
184 return;
185
186 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
187 &dice->card);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200188 if (err < 0)
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900189 return;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200190
Takashi Sakamoto7cafc65b2016-03-07 22:35:45 +0900191 if (force_two_pcm_support(dice->unit))
192 dice->force_two_pcms = true;
193
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900194 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200195 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900196 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100197
Takashi Sakamoto6f688262016-02-08 22:54:19 +0900198 err = check_clock_caps(dice);
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100199 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900200 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200201
202 dice_card_strings(dice);
203
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900204 snd_dice_create_proc(dice);
205
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900206 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200207 if (err < 0)
208 goto error;
209
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900210 err = snd_dice_create_midi(dice);
211 if (err < 0)
212 goto error;
213
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900214 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200215 if (err < 0)
216 goto error;
217
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900218 err = snd_card_register(dice->card);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900219 if (err < 0)
220 goto error;
221
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900222 /*
223 * After registered, dice instance can be released corresponding to
224 * releasing the sound card instance.
225 */
226 dice->card->private_free = dice_card_free;
227 dice->card->private_data = dice;
228 dice->registered = true;
229
230 return;
231error:
232 snd_dice_transaction_destroy(dice);
233 snd_card_free(dice->card);
234 dev_info(&dice->unit->device,
235 "Sound card registration failed: %d\n", err);
236}
237
238static void schedule_registration(struct snd_dice *dice)
239{
240 struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
241 u64 now, delay;
242
243 now = get_jiffies_64();
244 delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
245
246 if (time_after64(delay, now))
247 delay -= now;
248 else
249 delay = 0;
250
251 mod_delayed_work(system_wq, &dice->dwork, delay);
252}
253
254static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
255{
256 struct snd_dice *dice;
257 int err;
258
259 err = check_dice_category(unit);
260 if (err < 0)
261 return -ENODEV;
262
263 /* Allocate this independent of sound card instance. */
264 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
265 if (dice == NULL)
266 return -ENOMEM;
267
268 dice->unit = fw_unit_get(unit);
269 dev_set_drvdata(&unit->device, dice);
270
271 spin_lock_init(&dice->lock);
272 mutex_init(&dice->mutex);
273 init_completion(&dice->clock_accepted);
274 init_waitqueue_head(&dice->hwdep_wait);
275
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900276 err = snd_dice_stream_init_duplex(dice);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900277 if (err < 0) {
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900278 dice_free(dice);
279 return err;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900280 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200281
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900282 /* Allocate and register this sound card later. */
283 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
284 schedule_registration(dice);
285
286 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200287}
288
289static void dice_remove(struct fw_unit *unit)
290{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900291 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200292
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900293 /*
294 * Confirm to stop the work for registration before the sound card is
295 * going to be released. The work is not scheduled again because bus
296 * reset handler is not called anymore.
297 */
298 cancel_delayed_work_sync(&dice->dwork);
299
300 if (dice->registered) {
301 /* No need to wait for releasing card object in this context. */
302 snd_card_free_when_closed(dice->card);
303 } else {
304 /* Don't forget this case. */
305 dice_free(dice);
306 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200307}
308
309static void dice_bus_reset(struct fw_unit *unit)
310{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900311 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200312
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900313 /* Postpone a workqueue for deferred registration. */
314 if (!dice->registered)
315 schedule_registration(dice);
316
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900317 /* The handler address register becomes initialized. */
318 snd_dice_transaction_reinit(dice);
319
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900320 /*
321 * After registration, userspace can start packet streaming, then this
322 * code block works fine.
323 */
324 if (dice->registered) {
325 mutex_lock(&dice->mutex);
326 snd_dice_stream_update_duplex(dice);
327 mutex_unlock(&dice->mutex);
328 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200329}
330
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200331#define DICE_INTERFACE 0x000001
332
333static const struct ieee1394_device_id dice_id_table[] = {
334 {
Clemens Ladischcbab3282011-09-04 22:16:02 +0200335 .match_flags = IEEE1394_MATCH_VERSION,
336 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200337 },
338 { }
339};
340MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
341
342static struct fw_driver dice_driver = {
343 .driver = {
344 .owner = THIS_MODULE,
345 .name = KBUILD_MODNAME,
346 .bus = &fw_bus_type,
347 },
348 .probe = dice_probe,
349 .update = dice_bus_reset,
350 .remove = dice_remove,
351 .id_table = dice_id_table,
352};
353
354static int __init alsa_dice_init(void)
355{
356 return driver_register(&dice_driver.driver);
357}
358
359static void __exit alsa_dice_exit(void)
360{
361 driver_unregister(&dice_driver.driver);
362}
363
364module_init(alsa_dice_init);
365module_exit(alsa_dice_exit);