2 * TC Applied Technologies Digital Interface Communications Engine driver
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
10 MODULE_DESCRIPTION("DICE driver");
11 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
12 MODULE_LICENSE("GPL v2");
14 #define OUI_WEISS 0x001c6a
15 #define OUI_LOUD 0x000ff2
17 #define DICE_CATEGORY_ID 0x04
18 #define WEISS_CATEGORY_ID 0x00
19 #define LOUD_CATEGORY_ID 0x10
21 #define PROBE_DELAY_MS (2 * MSEC_PER_SEC)
23 static int check_dice_category(struct fw_unit *unit)
25 struct fw_device *device = fw_parent_device(unit);
26 struct fw_csr_iterator it;
27 int key, val, vendor = -1, model = -1;
28 unsigned int category;
31 * Check that GUID and unit directory are constructed according to DICE
32 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
33 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
34 * ID, and a 22-bit serial number.
36 fw_csr_iterator_init(&it, unit->directory);
37 while (fw_csr_iterator_next(&it, &key, &val)) {
39 case CSR_SPECIFIER_ID:
47 if (vendor == OUI_WEISS)
48 category = WEISS_CATEGORY_ID;
49 else if (vendor == OUI_LOUD)
50 category = LOUD_CATEGORY_ID;
52 category = DICE_CATEGORY_ID;
53 if (device->config_rom[3] != ((vendor << 8) | category) ||
54 device->config_rom[4] >> 22 != model)
60 static int highest_supported_mode_rate(struct snd_dice *dice,
61 unsigned int mode, unsigned int *rate)
65 for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) {
66 *rate = snd_dice_rates[i - 1];
67 if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0)
78 static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
84 if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
85 dice->tx_channels[mode] = 0;
86 dice->tx_midi_ports[mode] = 0;
87 dice->rx_channels[mode] = 0;
88 dice->rx_midi_ports[mode] = 0;
92 err = snd_dice_transaction_set_rate(dice, rate);
96 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
97 values, sizeof(values));
101 dice->tx_channels[mode] = be32_to_cpu(values[0]);
102 dice->tx_midi_ports[mode] = be32_to_cpu(values[1]);
104 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
105 values, sizeof(values));
109 dice->rx_channels[mode] = be32_to_cpu(values[0]);
110 dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
115 static int dice_read_params(struct snd_dice *dice)
120 /* some very old firmwares don't tell about their clock support */
121 if (dice->clock_caps > 0) {
122 err = snd_dice_transaction_read_global(dice,
123 GLOBAL_CLOCK_CAPABILITIES,
127 dice->clock_caps = be32_to_cpu(value);
129 /* this should be supported by any device */
130 dice->clock_caps = CLOCK_CAP_RATE_44100 |
131 CLOCK_CAP_RATE_48000 |
132 CLOCK_CAP_SOURCE_ARX1 |
133 CLOCK_CAP_SOURCE_INTERNAL;
136 for (mode = 2; mode >= 0; --mode) {
137 err = dice_read_mode_params(dice, mode);
145 static void dice_card_strings(struct snd_dice *dice)
147 struct snd_card *card = dice->card;
148 struct fw_device *dev = fw_parent_device(dice->unit);
149 char vendor[32], model[32];
153 strcpy(card->driver, "DICE");
155 strcpy(card->shortname, "DICE");
156 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
157 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
159 sizeof(card->shortname));
161 /* DICE strings are returned in "always-wrong" endianness */
162 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
163 for (i = 0; i < sizeof(card->shortname); i += 4)
164 swab32s((u32 *)&card->shortname[i]);
165 card->shortname[sizeof(card->shortname) - 1] = '\0';
169 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
171 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
172 snprintf(card->longname, sizeof(card->longname),
173 "%s %s (serial %u) at %s, S%d",
174 vendor, model, dev->config_rom[4] & 0x3fffff,
175 dev_name(&dice->unit->device), 100 << dev->max_speed);
177 strcpy(card->mixername, "DICE");
180 static void dice_free(struct snd_dice *dice)
182 snd_dice_stream_destroy_duplex(dice);
183 snd_dice_transaction_destroy(dice);
184 fw_unit_put(dice->unit);
186 mutex_destroy(&dice->mutex);
191 * This module releases the FireWire unit data after all ALSA character devices
192 * are released by applications. This is for releasing stream data or finishing
193 * transactions safely. Thus at returning from .remove(), this module still keep
194 * references for the unit.
196 static void dice_card_free(struct snd_card *card)
198 dice_free(card->private_data);
201 static void do_registration(struct work_struct *work)
203 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
206 if (dice->registered)
209 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
214 err = snd_dice_transaction_init(dice);
218 err = dice_read_params(dice);
222 dice_card_strings(dice);
224 snd_dice_create_proc(dice);
226 err = snd_dice_create_pcm(dice);
230 err = snd_dice_create_midi(dice);
234 err = snd_dice_create_hwdep(dice);
238 err = snd_card_register(dice->card);
243 * After registered, dice instance can be released corresponding to
244 * releasing the sound card instance.
246 dice->card->private_free = dice_card_free;
247 dice->card->private_data = dice;
248 dice->registered = true;
252 snd_dice_transaction_destroy(dice);
253 snd_card_free(dice->card);
254 dev_info(&dice->unit->device,
255 "Sound card registration failed: %d\n", err);
258 static void schedule_registration(struct snd_dice *dice)
260 struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
263 now = get_jiffies_64();
264 delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
266 if (time_after64(delay, now))
271 mod_delayed_work(system_wq, &dice->dwork, delay);
274 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
276 struct snd_dice *dice;
279 err = check_dice_category(unit);
283 /* Allocate this independent of sound card instance. */
284 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
288 dice->unit = fw_unit_get(unit);
289 dev_set_drvdata(&unit->device, dice);
291 spin_lock_init(&dice->lock);
292 mutex_init(&dice->mutex);
293 init_completion(&dice->clock_accepted);
294 init_waitqueue_head(&dice->hwdep_wait);
296 err = snd_dice_stream_init_duplex(dice);
302 /* Allocate and register this sound card later. */
303 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
304 schedule_registration(dice);
309 static void dice_remove(struct fw_unit *unit)
311 struct snd_dice *dice = dev_get_drvdata(&unit->device);
314 * Confirm to stop the work for registration before the sound card is
315 * going to be released. The work is not scheduled again because bus
316 * reset handler is not called anymore.
318 cancel_delayed_work_sync(&dice->dwork);
320 if (dice->registered) {
321 /* No need to wait for releasing card object in this context. */
322 snd_card_free_when_closed(dice->card);
324 /* Don't forget this case. */
329 static void dice_bus_reset(struct fw_unit *unit)
331 struct snd_dice *dice = dev_get_drvdata(&unit->device);
333 /* Postpone a workqueue for deferred registration. */
334 if (!dice->registered)
335 schedule_registration(dice);
337 /* The handler address register becomes initialized. */
338 snd_dice_transaction_reinit(dice);
341 * After registration, userspace can start packet streaming, then this
342 * code block works fine.
344 if (dice->registered) {
345 mutex_lock(&dice->mutex);
346 snd_dice_stream_update_duplex(dice);
347 mutex_unlock(&dice->mutex);
351 #define DICE_INTERFACE 0x000001
353 static const struct ieee1394_device_id dice_id_table[] = {
355 .match_flags = IEEE1394_MATCH_VERSION,
356 .version = DICE_INTERFACE,
360 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
362 static struct fw_driver dice_driver = {
364 .owner = THIS_MODULE,
365 .name = KBUILD_MODNAME,
369 .update = dice_bus_reset,
370 .remove = dice_remove,
371 .id_table = dice_id_table,
374 static int __init alsa_dice_init(void)
376 return driver_register(&dice_driver.driver);
379 static void __exit alsa_dice_exit(void)
381 driver_unregister(&dice_driver.driver);
384 module_init(alsa_dice_init);
385 module_exit(alsa_dice_exit);