2 * motu.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
11 #define OUI_MOTU 0x0001f2
13 MODULE_DESCRIPTION("MOTU FireWire driver");
14 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
15 MODULE_LICENSE("GPL v2");
17 const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = {
29 static void name_card(struct snd_motu *motu)
31 struct fw_device *fw_dev = fw_parent_device(motu->unit);
32 struct fw_csr_iterator it;
36 fw_csr_iterator_init(&it, motu->unit->directory);
37 while (fw_csr_iterator_next(&it, &key, &val)) {
45 strcpy(motu->card->driver, "FW-MOTU");
46 strcpy(motu->card->shortname, motu->spec->name);
47 strcpy(motu->card->mixername, motu->spec->name);
48 snprintf(motu->card->longname, sizeof(motu->card->longname),
49 "MOTU %s (version:%d), GUID %08x%08x at %s, S%d",
50 motu->spec->name, version,
51 fw_dev->config_rom[3], fw_dev->config_rom[4],
52 dev_name(&motu->unit->device), 100 << fw_dev->max_speed);
55 static void motu_free(struct snd_motu *motu)
57 snd_motu_transaction_unregister(motu);
59 snd_motu_stream_destroy_duplex(motu);
60 fw_unit_put(motu->unit);
62 mutex_destroy(&motu->mutex);
67 * This module releases the FireWire unit data after all ALSA character devices
68 * are released by applications. This is for releasing stream data or finishing
69 * transactions safely. Thus at returning from .remove(), this module still keep
70 * references for the unit.
72 static void motu_card_free(struct snd_card *card)
74 motu_free(card->private_data);
77 static void do_registration(struct work_struct *work)
79 struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work);
85 err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0,
92 err = snd_motu_transaction_register(motu);
96 err = snd_motu_stream_init_duplex(motu);
100 snd_motu_proc_init(motu);
102 err = snd_motu_create_pcm_devices(motu);
106 if (motu->spec->flags & SND_MOTU_SPEC_HAS_MIDI) {
107 err = snd_motu_create_midi_devices(motu);
112 err = snd_motu_create_hwdep_device(motu);
116 err = snd_card_register(motu->card);
121 * After registered, motu instance can be released corresponding to
122 * releasing the sound card instance.
124 motu->card->private_free = motu_card_free;
125 motu->card->private_data = motu;
126 motu->registered = true;
130 snd_motu_transaction_unregister(motu);
131 snd_motu_stream_destroy_duplex(motu);
132 snd_card_free(motu->card);
133 dev_info(&motu->unit->device,
134 "Sound card registration failed: %d\n", err);
137 static int motu_probe(struct fw_unit *unit,
138 const struct ieee1394_device_id *entry)
140 struct snd_motu *motu;
142 /* Allocate this independently of sound card instance. */
143 motu = kzalloc(sizeof(struct snd_motu), GFP_KERNEL);
147 motu->spec = (const struct snd_motu_spec *)entry->driver_data;
148 motu->unit = fw_unit_get(unit);
149 dev_set_drvdata(&unit->device, motu);
151 mutex_init(&motu->mutex);
152 spin_lock_init(&motu->lock);
153 init_waitqueue_head(&motu->hwdep_wait);
155 /* Allocate and register this sound card later. */
156 INIT_DEFERRABLE_WORK(&motu->dwork, do_registration);
157 snd_fw_schedule_registration(unit, &motu->dwork);
162 static void motu_remove(struct fw_unit *unit)
164 struct snd_motu *motu = dev_get_drvdata(&unit->device);
167 * Confirm to stop the work for registration before the sound card is
168 * going to be released. The work is not scheduled again because bus
169 * reset handler is not called anymore.
171 cancel_delayed_work_sync(&motu->dwork);
173 if (motu->registered) {
174 /* No need to wait for releasing card object in this context. */
175 snd_card_free_when_closed(motu->card);
177 /* Don't forget this case. */
182 static void motu_bus_update(struct fw_unit *unit)
184 struct snd_motu *motu = dev_get_drvdata(&unit->device);
186 /* Postpone a workqueue for deferred registration. */
187 if (!motu->registered)
188 snd_fw_schedule_registration(unit, &motu->dwork);
190 /* The handler address register becomes initialized. */
191 snd_motu_transaction_reregister(motu);
194 static struct snd_motu_spec motu_828mk2 = {
196 .protocol = &snd_motu_protocol_v2,
197 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
198 SND_MOTU_SPEC_TX_MICINST_CHUNK |
199 SND_MOTU_SPEC_TX_RETURN_CHUNK |
200 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
201 SND_MOTU_SPEC_HAS_MIDI,
203 .analog_in_ports = 8,
204 .analog_out_ports = 8,
207 static struct snd_motu_spec motu_828mk3 = {
209 .protocol = &snd_motu_protocol_v3,
210 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
211 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
212 SND_MOTU_SPEC_TX_MICINST_CHUNK |
213 SND_MOTU_SPEC_TX_RETURN_CHUNK |
214 SND_MOTU_SPEC_TX_REVERB_CHUNK |
215 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
216 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
217 SND_MOTU_SPEC_HAS_MIDI,
219 .analog_in_ports = 8,
220 .analog_out_ports = 8,
223 #define SND_MOTU_DEV_ENTRY(model, data) \
225 .match_flags = IEEE1394_MATCH_VENDOR_ID | \
226 IEEE1394_MATCH_MODEL_ID | \
227 IEEE1394_MATCH_SPECIFIER_ID, \
228 .vendor_id = OUI_MOTU, \
230 .specifier_id = OUI_MOTU, \
231 .driver_data = (kernel_ulong_t)data, \
234 static const struct ieee1394_device_id motu_id_table[] = {
235 SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2),
236 SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3), /* FireWire only. */
237 SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3), /* Hybrid. */
240 MODULE_DEVICE_TABLE(ieee1394, motu_id_table);
242 static struct fw_driver motu_driver = {
244 .owner = THIS_MODULE,
245 .name = KBUILD_MODNAME,
249 .update = motu_bus_update,
250 .remove = motu_remove,
251 .id_table = motu_id_table,
254 static int __init alsa_motu_init(void)
256 return driver_register(&motu_driver.driver);
259 static void __exit alsa_motu_exit(void)
261 driver_unregister(&motu_driver.driver);
264 module_init(alsa_motu_init);
265 module_exit(alsa_motu_exit);