1 // SPDX-License-Identifier: GPL-2.0-only
3 * digi00x.c - a part of driver for Digidesign Digi 002/003 family
5 * Copyright (c) 2014-2015 Takashi Sakamoto
10 MODULE_DESCRIPTION("Digidesign Digi 002/003 family Driver");
11 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
12 MODULE_LICENSE("GPL v2");
14 #define VENDOR_DIGIDESIGN 0x00a07e
15 #define MODEL_CONSOLE 0x000001
16 #define MODEL_RACK 0x000002
17 #define SPEC_VERSION 0x000001
19 static int name_card(struct snd_dg00x *dg00x)
21 struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
26 err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
31 model = skip_spaces(name);
33 strcpy(dg00x->card->driver, "Digi00x");
34 strcpy(dg00x->card->shortname, model);
35 strcpy(dg00x->card->mixername, model);
36 snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
37 "Digidesign %s, GUID %08x%08x at %s, S%d", model,
38 fw_dev->config_rom[3], fw_dev->config_rom[4],
39 dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
44 static void dg00x_card_free(struct snd_card *card)
46 struct snd_dg00x *dg00x = card->private_data;
48 snd_dg00x_stream_destroy_duplex(dg00x);
49 snd_dg00x_transaction_unregister(dg00x);
52 static void do_registration(struct work_struct *work)
54 struct snd_dg00x *dg00x =
55 container_of(work, struct snd_dg00x, dwork.work);
58 if (dg00x->registered)
61 err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
65 dg00x->card->private_free = dg00x_card_free;
66 dg00x->card->private_data = dg00x;
68 err = name_card(dg00x);
72 err = snd_dg00x_stream_init_duplex(dg00x);
76 snd_dg00x_proc_init(dg00x);
78 err = snd_dg00x_create_pcm_devices(dg00x);
82 err = snd_dg00x_create_midi_devices(dg00x);
86 err = snd_dg00x_create_hwdep_device(dg00x);
90 err = snd_dg00x_transaction_register(dg00x);
94 err = snd_card_register(dg00x->card);
98 dg00x->registered = true;
102 snd_card_free(dg00x->card);
103 dev_info(&dg00x->unit->device,
104 "Sound card registration failed: %d\n", err);
107 static int snd_dg00x_probe(struct fw_unit *unit,
108 const struct ieee1394_device_id *entry)
110 struct snd_dg00x *dg00x;
112 /* Allocate this independent of sound card instance. */
113 dg00x = devm_kzalloc(&unit->device, sizeof(struct snd_dg00x),
118 dg00x->unit = fw_unit_get(unit);
119 dev_set_drvdata(&unit->device, dg00x);
121 mutex_init(&dg00x->mutex);
122 spin_lock_init(&dg00x->lock);
123 init_waitqueue_head(&dg00x->hwdep_wait);
125 dg00x->is_console = entry->model_id == MODEL_CONSOLE;
127 /* Allocate and register this sound card later. */
128 INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
129 snd_fw_schedule_registration(unit, &dg00x->dwork);
134 static void snd_dg00x_update(struct fw_unit *unit)
136 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
138 /* Postpone a workqueue for deferred registration. */
139 if (!dg00x->registered)
140 snd_fw_schedule_registration(unit, &dg00x->dwork);
142 snd_dg00x_transaction_reregister(dg00x);
145 * After registration, userspace can start packet streaming, then this
146 * code block works fine.
148 if (dg00x->registered) {
149 mutex_lock(&dg00x->mutex);
150 snd_dg00x_stream_update_duplex(dg00x);
151 mutex_unlock(&dg00x->mutex);
155 static void snd_dg00x_remove(struct fw_unit *unit)
157 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
160 * Confirm to stop the work for registration before the sound card is
161 * going to be released. The work is not scheduled again because bus
162 * reset handler is not called anymore.
164 cancel_delayed_work_sync(&dg00x->dwork);
166 if (dg00x->registered) {
167 // Block till all of ALSA character devices are released.
168 snd_card_free(dg00x->card);
171 mutex_destroy(&dg00x->mutex);
172 fw_unit_put(dg00x->unit);
175 static const struct ieee1394_device_id snd_dg00x_id_table[] = {
176 /* Both of 002/003 use the same ID. */
178 .match_flags = IEEE1394_MATCH_VENDOR_ID |
179 IEEE1394_MATCH_VERSION |
180 IEEE1394_MATCH_MODEL_ID,
181 .vendor_id = VENDOR_DIGIDESIGN,
182 .version = SPEC_VERSION,
183 .model_id = MODEL_CONSOLE,
186 .match_flags = IEEE1394_MATCH_VENDOR_ID |
187 IEEE1394_MATCH_VERSION |
188 IEEE1394_MATCH_MODEL_ID,
189 .vendor_id = VENDOR_DIGIDESIGN,
190 .version = SPEC_VERSION,
191 .model_id = MODEL_RACK,
195 MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
197 static struct fw_driver dg00x_driver = {
199 .owner = THIS_MODULE,
200 .name = KBUILD_MODNAME,
203 .probe = snd_dg00x_probe,
204 .update = snd_dg00x_update,
205 .remove = snd_dg00x_remove,
206 .id_table = snd_dg00x_id_table,
209 static int __init snd_dg00x_init(void)
211 return driver_register(&dg00x_driver.driver);
214 static void __exit snd_dg00x_exit(void)
216 driver_unregister(&dg00x_driver.driver);
219 module_init(snd_dg00x_init);
220 module_exit(snd_dg00x_exit);