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
16 #define OUI_FOCUSRITE 0x00130e
17 #define OUI_TCELECTRONIC 0x001486
19 #define DICE_CATEGORY_ID 0x04
20 #define WEISS_CATEGORY_ID 0x00
21 #define LOUD_CATEGORY_ID 0x10
23 #define PROBE_DELAY_MS (2 * MSEC_PER_SEC)
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.
29 static bool force_two_pcm_support(struct fw_unit
*unit
)
31 const char *const models
[] = {
32 /* TC Electronic models. */
34 /* Focusrite models. */
43 err
= fw_csr_string(unit
->directory
, CSR_MODEL
, model
, sizeof(model
));
47 for (i
= 0; i
< ARRAY_SIZE(models
); i
++) {
48 if (strcmp(models
[i
], model
) == 0)
52 return i
< ARRAY_SIZE(models
);
55 static int check_dice_category(struct fw_unit
*unit
)
57 struct fw_device
*device
= fw_parent_device(unit
);
58 struct fw_csr_iterator it
;
59 int key
, val
, vendor
= -1, model
= -1;
60 unsigned int category
;
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
65 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
66 * ID, and a 22-bit serial number.
68 fw_csr_iterator_init(&it
, unit
->directory
);
69 while (fw_csr_iterator_next(&it
, &key
, &val
)) {
71 case CSR_SPECIFIER_ID
:
80 if (vendor
== OUI_FOCUSRITE
|| vendor
== OUI_TCELECTRONIC
) {
81 if (force_two_pcm_support(unit
))
85 if (vendor
== OUI_WEISS
)
86 category
= WEISS_CATEGORY_ID
;
87 else if (vendor
== OUI_LOUD
)
88 category
= LOUD_CATEGORY_ID
;
90 category
= DICE_CATEGORY_ID
;
91 if (device
->config_rom
[3] != ((vendor
<< 8) | category
) ||
92 device
->config_rom
[4] >> 22 != model
)
98 static int check_clock_caps(struct snd_dice
*dice
)
103 /* some very old firmwares don't tell about their clock support */
104 if (dice
->clock_caps
> 0) {
105 err
= snd_dice_transaction_read_global(dice
,
106 GLOBAL_CLOCK_CAPABILITIES
,
110 dice
->clock_caps
= be32_to_cpu(value
);
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
;
122 static void dice_card_strings(struct snd_dice
*dice
)
124 struct snd_card
*card
= dice
->card
;
125 struct fw_device
*dev
= fw_parent_device(dice
->unit
);
126 char vendor
[32], model
[32];
130 strcpy(card
->driver
, "DICE");
132 strcpy(card
->shortname
, "DICE");
133 BUILD_BUG_ON(NICK_NAME_SIZE
< sizeof(card
->shortname
));
134 err
= snd_dice_transaction_read_global(dice
, GLOBAL_NICK_NAME
,
136 sizeof(card
->shortname
));
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';
146 fw_csr_string(dev
->config_rom
+ 5, CSR_VENDOR
, vendor
, sizeof(vendor
));
148 fw_csr_string(dice
->unit
->directory
, CSR_MODEL
, model
, sizeof(model
));
149 snprintf(card
->longname
, sizeof(card
->longname
),
150 "%s %s (serial %u) at %s, S%d",
151 vendor
, model
, dev
->config_rom
[4] & 0x3fffff,
152 dev_name(&dice
->unit
->device
), 100 << dev
->max_speed
);
154 strcpy(card
->mixername
, "DICE");
157 static void dice_free(struct snd_dice
*dice
)
159 snd_dice_stream_destroy_duplex(dice
);
160 snd_dice_transaction_destroy(dice
);
161 fw_unit_put(dice
->unit
);
163 mutex_destroy(&dice
->mutex
);
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.
173 static void dice_card_free(struct snd_card
*card
)
175 dice_free(card
->private_data
);
178 static void do_registration(struct work_struct
*work
)
180 struct snd_dice
*dice
= container_of(work
, struct snd_dice
, dwork
.work
);
183 if (dice
->registered
)
186 err
= snd_card_new(&dice
->unit
->device
, -1, NULL
, THIS_MODULE
, 0,
191 if (force_two_pcm_support(dice
->unit
))
192 dice
->force_two_pcms
= true;
194 err
= snd_dice_transaction_init(dice
);
198 err
= check_clock_caps(dice
);
202 dice_card_strings(dice
);
204 snd_dice_create_proc(dice
);
206 err
= snd_dice_create_pcm(dice
);
210 err
= snd_dice_create_midi(dice
);
214 err
= snd_dice_create_hwdep(dice
);
218 err
= snd_card_register(dice
->card
);
223 * After registered, dice instance can be released corresponding to
224 * releasing the sound card instance.
226 dice
->card
->private_free
= dice_card_free
;
227 dice
->card
->private_data
= dice
;
228 dice
->registered
= true;
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
);
238 static void schedule_registration(struct snd_dice
*dice
)
240 struct fw_card
*fw_card
= fw_parent_device(dice
->unit
)->card
;
243 now
= get_jiffies_64();
244 delay
= fw_card
->reset_jiffies
+ msecs_to_jiffies(PROBE_DELAY_MS
);
246 if (time_after64(delay
, now
))
251 mod_delayed_work(system_wq
, &dice
->dwork
, delay
);
254 static int dice_probe(struct fw_unit
*unit
, const struct ieee1394_device_id
*id
)
256 struct snd_dice
*dice
;
259 err
= check_dice_category(unit
);
263 /* Allocate this independent of sound card instance. */
264 dice
= kzalloc(sizeof(struct snd_dice
), GFP_KERNEL
);
268 dice
->unit
= fw_unit_get(unit
);
269 dev_set_drvdata(&unit
->device
, dice
);
271 spin_lock_init(&dice
->lock
);
272 mutex_init(&dice
->mutex
);
273 init_completion(&dice
->clock_accepted
);
274 init_waitqueue_head(&dice
->hwdep_wait
);
276 err
= snd_dice_stream_init_duplex(dice
);
282 /* Allocate and register this sound card later. */
283 INIT_DEFERRABLE_WORK(&dice
->dwork
, do_registration
);
284 schedule_registration(dice
);
289 static void dice_remove(struct fw_unit
*unit
)
291 struct snd_dice
*dice
= dev_get_drvdata(&unit
->device
);
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.
298 cancel_delayed_work_sync(&dice
->dwork
);
300 if (dice
->registered
) {
301 /* No need to wait for releasing card object in this context. */
302 snd_card_free_when_closed(dice
->card
);
304 /* Don't forget this case. */
309 static void dice_bus_reset(struct fw_unit
*unit
)
311 struct snd_dice
*dice
= dev_get_drvdata(&unit
->device
);
313 /* Postpone a workqueue for deferred registration. */
314 if (!dice
->registered
)
315 schedule_registration(dice
);
317 /* The handler address register becomes initialized. */
318 snd_dice_transaction_reinit(dice
);
321 * After registration, userspace can start packet streaming, then this
322 * code block works fine.
324 if (dice
->registered
) {
325 mutex_lock(&dice
->mutex
);
326 snd_dice_stream_update_duplex(dice
);
327 mutex_unlock(&dice
->mutex
);
331 #define DICE_INTERFACE 0x000001
333 static const struct ieee1394_device_id dice_id_table
[] = {
335 .match_flags
= IEEE1394_MATCH_VERSION
,
336 .version
= DICE_INTERFACE
,
340 MODULE_DEVICE_TABLE(ieee1394
, dice_id_table
);
342 static struct fw_driver dice_driver
= {
344 .owner
= THIS_MODULE
,
345 .name
= KBUILD_MODNAME
,
349 .update
= dice_bus_reset
,
350 .remove
= dice_remove
,
351 .id_table
= dice_id_table
,
354 static int __init
alsa_dice_init(void)
356 return driver_register(&dice_driver
.driver
);
359 static void __exit
alsa_dice_exit(void)
361 driver_unregister(&dice_driver
.driver
);
364 module_init(alsa_dice_init
);
365 module_exit(alsa_dice_exit
);