blob: 53ca441ea9b9c20005c2367e58e4a34c7e5c02f9 [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 Sakamoto7cafc652016-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 Sakamoto7cafc652016-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 Sakamoto7cafc652016-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 Sakamoto7cafc652016-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 Sakamoto0eced452016-03-31 08:47:03 +0900204 err = snd_dice_stream_init_duplex(dice);
205 if (err < 0)
206 goto error;
207
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900208 snd_dice_create_proc(dice);
209
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900210 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200211 if (err < 0)
212 goto error;
213
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900214 err = snd_dice_create_midi(dice);
215 if (err < 0)
216 goto error;
217
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900218 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200219 if (err < 0)
220 goto error;
221
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900222 err = snd_card_register(dice->card);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900223 if (err < 0)
224 goto error;
225
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900226 /*
227 * After registered, dice instance can be released corresponding to
228 * releasing the sound card instance.
229 */
230 dice->card->private_free = dice_card_free;
231 dice->card->private_data = dice;
232 dice->registered = true;
233
234 return;
235error:
Takashi Sakamoto0eced452016-03-31 08:47:03 +0900236 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900237 snd_dice_transaction_destroy(dice);
238 snd_card_free(dice->card);
239 dev_info(&dice->unit->device,
240 "Sound card registration failed: %d\n", err);
241}
242
243static void schedule_registration(struct snd_dice *dice)
244{
245 struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
246 u64 now, delay;
247
248 now = get_jiffies_64();
249 delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
250
251 if (time_after64(delay, now))
252 delay -= now;
253 else
254 delay = 0;
255
256 mod_delayed_work(system_wq, &dice->dwork, delay);
257}
258
259static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
260{
261 struct snd_dice *dice;
262 int err;
263
264 err = check_dice_category(unit);
265 if (err < 0)
266 return -ENODEV;
267
268 /* Allocate this independent of sound card instance. */
269 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
270 if (dice == NULL)
271 return -ENOMEM;
272
273 dice->unit = fw_unit_get(unit);
274 dev_set_drvdata(&unit->device, dice);
275
276 spin_lock_init(&dice->lock);
277 mutex_init(&dice->mutex);
278 init_completion(&dice->clock_accepted);
279 init_waitqueue_head(&dice->hwdep_wait);
280
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900281 /* Allocate and register this sound card later. */
282 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
283 schedule_registration(dice);
284
285 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200286}
287
288static void dice_remove(struct fw_unit *unit)
289{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900290 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200291
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900292 /*
293 * Confirm to stop the work for registration before the sound card is
294 * going to be released. The work is not scheduled again because bus
295 * reset handler is not called anymore.
296 */
297 cancel_delayed_work_sync(&dice->dwork);
298
299 if (dice->registered) {
300 /* No need to wait for releasing card object in this context. */
301 snd_card_free_when_closed(dice->card);
302 } else {
303 /* Don't forget this case. */
304 dice_free(dice);
305 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200306}
307
308static void dice_bus_reset(struct fw_unit *unit)
309{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900310 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200311
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900312 /* Postpone a workqueue for deferred registration. */
313 if (!dice->registered)
314 schedule_registration(dice);
315
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900316 /* The handler address register becomes initialized. */
317 snd_dice_transaction_reinit(dice);
318
Takashi Sakamotob59fb192015-12-31 13:58:12 +0900319 /*
320 * After registration, userspace can start packet streaming, then this
321 * code block works fine.
322 */
323 if (dice->registered) {
324 mutex_lock(&dice->mutex);
325 snd_dice_stream_update_duplex(dice);
326 mutex_unlock(&dice->mutex);
327 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200328}
329
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200330#define DICE_INTERFACE 0x000001
331
332static const struct ieee1394_device_id dice_id_table[] = {
333 {
Clemens Ladischcbab3282011-09-04 22:16:02 +0200334 .match_flags = IEEE1394_MATCH_VERSION,
335 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200336 },
337 { }
338};
339MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
340
341static struct fw_driver dice_driver = {
342 .driver = {
343 .owner = THIS_MODULE,
344 .name = KBUILD_MODNAME,
345 .bus = &fw_bus_type,
346 },
347 .probe = dice_probe,
348 .update = dice_bus_reset,
349 .remove = dice_remove,
350 .id_table = dice_id_table,
351};
352
353static int __init alsa_dice_init(void)
354{
355 return driver_register(&dice_driver.driver);
356}
357
358static void __exit alsa_dice_exit(void)
359{
360 driver_unregister(&dice_driver.driver);
361}
362
363module_init(alsa_dice_init);
364module_exit(alsa_dice_exit);