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 check_clock_caps(struct snd_dice *dice)
65 /* some very old firmwares don't tell about their clock support */
66 if (dice->clock_caps > 0) {
67 err = snd_dice_transaction_read_global(dice,
68 GLOBAL_CLOCK_CAPABILITIES,
72 dice->clock_caps = be32_to_cpu(value);
74 /* this should be supported by any device */
75 dice->clock_caps = CLOCK_CAP_RATE_44100 |
76 CLOCK_CAP_RATE_48000 |
77 CLOCK_CAP_SOURCE_ARX1 |
78 CLOCK_CAP_SOURCE_INTERNAL;
84 static void dice_card_strings(struct snd_dice *dice)
86 struct snd_card *card = dice->card;
87 struct fw_device *dev = fw_parent_device(dice->unit);
88 char vendor[32], model[32];
92 strcpy(card->driver, "DICE");
94 strcpy(card->shortname, "DICE");
95 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
96 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
98 sizeof(card->shortname));
100 /* DICE strings are returned in "always-wrong" endianness */
101 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
102 for (i = 0; i < sizeof(card->shortname); i += 4)
103 swab32s((u32 *)&card->shortname[i]);
104 card->shortname[sizeof(card->shortname) - 1] = '\0';
108 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
110 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
111 snprintf(card->longname, sizeof(card->longname),
112 "%s %s (serial %u) at %s, S%d",
113 vendor, model, dev->config_rom[4] & 0x3fffff,
114 dev_name(&dice->unit->device), 100 << dev->max_speed);
116 strcpy(card->mixername, "DICE");
119 static void dice_free(struct snd_dice *dice)
121 snd_dice_stream_destroy_duplex(dice);
122 snd_dice_transaction_destroy(dice);
123 fw_unit_put(dice->unit);
125 mutex_destroy(&dice->mutex);
130 * This module releases the FireWire unit data after all ALSA character devices
131 * are released by applications. This is for releasing stream data or finishing
132 * transactions safely. Thus at returning from .remove(), this module still keep
133 * references for the unit.
135 static void dice_card_free(struct snd_card *card)
137 dice_free(card->private_data);
140 static void do_registration(struct work_struct *work)
142 struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
145 if (dice->registered)
148 err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
153 err = snd_dice_transaction_init(dice);
157 err = check_clock_caps(dice);
161 dice_card_strings(dice);
163 snd_dice_create_proc(dice);
165 err = snd_dice_create_pcm(dice);
169 err = snd_dice_create_midi(dice);
173 err = snd_dice_create_hwdep(dice);
177 err = snd_card_register(dice->card);
182 * After registered, dice instance can be released corresponding to
183 * releasing the sound card instance.
185 dice->card->private_free = dice_card_free;
186 dice->card->private_data = dice;
187 dice->registered = true;
191 snd_dice_transaction_destroy(dice);
192 snd_card_free(dice->card);
193 dev_info(&dice->unit->device,
194 "Sound card registration failed: %d\n", err);
197 static void schedule_registration(struct snd_dice *dice)
199 struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
202 now = get_jiffies_64();
203 delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
205 if (time_after64(delay, now))
210 mod_delayed_work(system_wq, &dice->dwork, delay);
213 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
215 struct snd_dice *dice;
218 err = check_dice_category(unit);
222 /* Allocate this independent of sound card instance. */
223 dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
227 dice->unit = fw_unit_get(unit);
228 dev_set_drvdata(&unit->device, dice);
230 spin_lock_init(&dice->lock);
231 mutex_init(&dice->mutex);
232 init_completion(&dice->clock_accepted);
233 init_waitqueue_head(&dice->hwdep_wait);
235 err = snd_dice_stream_init_duplex(dice);
241 /* Allocate and register this sound card later. */
242 INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
243 schedule_registration(dice);
248 static void dice_remove(struct fw_unit *unit)
250 struct snd_dice *dice = dev_get_drvdata(&unit->device);
253 * Confirm to stop the work for registration before the sound card is
254 * going to be released. The work is not scheduled again because bus
255 * reset handler is not called anymore.
257 cancel_delayed_work_sync(&dice->dwork);
259 if (dice->registered) {
260 /* No need to wait for releasing card object in this context. */
261 snd_card_free_when_closed(dice->card);
263 /* Don't forget this case. */
268 static void dice_bus_reset(struct fw_unit *unit)
270 struct snd_dice *dice = dev_get_drvdata(&unit->device);
272 /* Postpone a workqueue for deferred registration. */
273 if (!dice->registered)
274 schedule_registration(dice);
276 /* The handler address register becomes initialized. */
277 snd_dice_transaction_reinit(dice);
280 * After registration, userspace can start packet streaming, then this
281 * code block works fine.
283 if (dice->registered) {
284 mutex_lock(&dice->mutex);
285 snd_dice_stream_update_duplex(dice);
286 mutex_unlock(&dice->mutex);
290 #define DICE_INTERFACE 0x000001
292 static const struct ieee1394_device_id dice_id_table[] = {
294 .match_flags = IEEE1394_MATCH_VERSION,
295 .version = DICE_INTERFACE,
299 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
301 static struct fw_driver dice_driver = {
303 .owner = THIS_MODULE,
304 .name = KBUILD_MODNAME,
308 .update = dice_bus_reset,
309 .remove = dice_remove,
310 .id_table = dice_id_table,
313 static int __init alsa_dice_init(void)
315 return driver_register(&dice_driver.driver);
318 static void __exit alsa_dice_exit(void)
320 driver_unregister(&dice_driver.driver);
323 module_init(alsa_dice_init);
324 module_exit(alsa_dice_exit);