1 /* 2 * motu.c - a part of driver for MOTU FireWire series 3 * 4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 5 * 6 * Licensed under the terms of the GNU General Public License, version 2. 7 */ 8 9 #include "motu.h" 10 11 #define OUI_MOTU 0x0001f2 12 13 MODULE_DESCRIPTION("MOTU FireWire driver"); 14 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>"); 15 MODULE_LICENSE("GPL v2"); 16 17 const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT] = { 18 /* mode 0 */ 19 [0] = 44100, 20 [1] = 48000, 21 /* mode 1 */ 22 [2] = 88200, 23 [3] = 96000, 24 /* mode 2 */ 25 [4] = 176400, 26 [5] = 192000, 27 }; 28 29 static void name_card(struct snd_motu *motu) 30 { 31 struct fw_device *fw_dev = fw_parent_device(motu->unit); 32 struct fw_csr_iterator it; 33 int key, val; 34 u32 version = 0; 35 36 fw_csr_iterator_init(&it, motu->unit->directory); 37 while (fw_csr_iterator_next(&it, &key, &val)) { 38 switch (key) { 39 case CSR_VERSION: 40 version = val; 41 break; 42 } 43 } 44 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); 53 } 54 55 static void motu_free(struct snd_motu *motu) 56 { 57 snd_motu_transaction_unregister(motu); 58 59 snd_motu_stream_destroy_duplex(motu); 60 } 61 62 /* 63 * This module releases the FireWire unit data after all ALSA character devices 64 * are released by applications. This is for releasing stream data or finishing 65 * transactions safely. Thus at returning from .remove(), this module still keep 66 * references for the unit. 67 */ 68 static void motu_card_free(struct snd_card *card) 69 { 70 motu_free(card->private_data); 71 } 72 73 static void do_registration(struct work_struct *work) 74 { 75 struct snd_motu *motu = container_of(work, struct snd_motu, dwork.work); 76 int err; 77 78 if (motu->registered) 79 return; 80 81 err = snd_card_new(&motu->unit->device, -1, NULL, THIS_MODULE, 0, 82 &motu->card); 83 if (err < 0) 84 return; 85 86 name_card(motu); 87 88 err = snd_motu_transaction_register(motu); 89 if (err < 0) 90 goto error; 91 92 err = snd_motu_stream_init_duplex(motu); 93 if (err < 0) 94 goto error; 95 96 snd_motu_proc_init(motu); 97 98 err = snd_motu_create_pcm_devices(motu); 99 if (err < 0) 100 goto error; 101 102 if ((motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_2ND_Q) || 103 (motu->spec->flags & SND_MOTU_SPEC_RX_MIDI_3RD_Q) || 104 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_2ND_Q) || 105 (motu->spec->flags & SND_MOTU_SPEC_TX_MIDI_3RD_Q)) { 106 err = snd_motu_create_midi_devices(motu); 107 if (err < 0) 108 goto error; 109 } 110 111 err = snd_motu_create_hwdep_device(motu); 112 if (err < 0) 113 goto error; 114 115 err = snd_card_register(motu->card); 116 if (err < 0) 117 goto error; 118 119 /* 120 * After registered, motu instance can be released corresponding to 121 * releasing the sound card instance. 122 */ 123 motu->card->private_free = motu_card_free; 124 motu->card->private_data = motu; 125 motu->registered = true; 126 127 return; 128 error: 129 snd_motu_transaction_unregister(motu); 130 snd_motu_stream_destroy_duplex(motu); 131 snd_card_free(motu->card); 132 dev_info(&motu->unit->device, 133 "Sound card registration failed: %d\n", err); 134 } 135 136 static int motu_probe(struct fw_unit *unit, 137 const struct ieee1394_device_id *entry) 138 { 139 struct snd_motu *motu; 140 141 /* Allocate this independently of sound card instance. */ 142 motu = devm_kzalloc(&unit->device, sizeof(struct snd_motu), GFP_KERNEL); 143 if (!motu) 144 return -ENOMEM; 145 motu->unit = fw_unit_get(unit); 146 dev_set_drvdata(&unit->device, motu); 147 148 motu->spec = (const struct snd_motu_spec *)entry->driver_data; 149 mutex_init(&motu->mutex); 150 spin_lock_init(&motu->lock); 151 init_waitqueue_head(&motu->hwdep_wait); 152 153 /* Allocate and register this sound card later. */ 154 INIT_DEFERRABLE_WORK(&motu->dwork, do_registration); 155 snd_fw_schedule_registration(unit, &motu->dwork); 156 157 return 0; 158 } 159 160 static void motu_remove(struct fw_unit *unit) 161 { 162 struct snd_motu *motu = dev_get_drvdata(&unit->device); 163 164 /* 165 * Confirm to stop the work for registration before the sound card is 166 * going to be released. The work is not scheduled again because bus 167 * reset handler is not called anymore. 168 */ 169 cancel_delayed_work_sync(&motu->dwork); 170 171 if (motu->registered) { 172 // Block till all of ALSA character devices are released. 173 snd_card_free(motu->card); 174 } 175 176 mutex_destroy(&motu->mutex); 177 fw_unit_put(motu->unit); 178 } 179 180 static void motu_bus_update(struct fw_unit *unit) 181 { 182 struct snd_motu *motu = dev_get_drvdata(&unit->device); 183 184 /* Postpone a workqueue for deferred registration. */ 185 if (!motu->registered) 186 snd_fw_schedule_registration(unit, &motu->dwork); 187 188 /* The handler address register becomes initialized. */ 189 snd_motu_transaction_reregister(motu); 190 } 191 192 static const struct snd_motu_spec motu_828mk2 = { 193 .name = "828mk2", 194 .protocol = &snd_motu_protocol_v2, 195 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 196 SND_MOTU_SPEC_TX_MICINST_CHUNK | 197 SND_MOTU_SPEC_TX_RETURN_CHUNK | 198 SND_MOTU_SPEC_RX_SEPARETED_MAIN | 199 SND_MOTU_SPEC_HAS_OPT_IFACE_A | 200 SND_MOTU_SPEC_RX_MIDI_2ND_Q | 201 SND_MOTU_SPEC_TX_MIDI_2ND_Q, 202 203 .analog_in_ports = 8, 204 .analog_out_ports = 8, 205 }; 206 207 const struct snd_motu_spec snd_motu_spec_traveler = { 208 .name = "Traveler", 209 .protocol = &snd_motu_protocol_v2, 210 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 211 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 | 212 SND_MOTU_SPEC_TX_RETURN_CHUNK | 213 SND_MOTU_SPEC_HAS_AESEBU_IFACE | 214 SND_MOTU_SPEC_HAS_OPT_IFACE_A | 215 SND_MOTU_SPEC_RX_MIDI_2ND_Q | 216 SND_MOTU_SPEC_TX_MIDI_2ND_Q, 217 218 .analog_in_ports = 8, 219 .analog_out_ports = 8, 220 }; 221 222 static const struct snd_motu_spec motu_828mk3 = { 223 .name = "828mk3", 224 .protocol = &snd_motu_protocol_v3, 225 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 226 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 | 227 SND_MOTU_SPEC_TX_MICINST_CHUNK | 228 SND_MOTU_SPEC_TX_RETURN_CHUNK | 229 SND_MOTU_SPEC_TX_REVERB_CHUNK | 230 SND_MOTU_SPEC_RX_SEPARETED_MAIN | 231 SND_MOTU_SPEC_HAS_OPT_IFACE_A | 232 SND_MOTU_SPEC_HAS_OPT_IFACE_B | 233 SND_MOTU_SPEC_RX_MIDI_3RD_Q | 234 SND_MOTU_SPEC_TX_MIDI_3RD_Q, 235 236 .analog_in_ports = 8, 237 .analog_out_ports = 8, 238 }; 239 240 static const struct snd_motu_spec motu_audio_express = { 241 .name = "AudioExpress", 242 .protocol = &snd_motu_protocol_v3, 243 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 | 244 SND_MOTU_SPEC_TX_MICINST_CHUNK | 245 SND_MOTU_SPEC_TX_RETURN_CHUNK | 246 SND_MOTU_SPEC_RX_SEPARETED_MAIN | 247 SND_MOTU_SPEC_RX_MIDI_2ND_Q | 248 SND_MOTU_SPEC_TX_MIDI_3RD_Q, 249 .analog_in_ports = 2, 250 .analog_out_ports = 4, 251 }; 252 253 #define SND_MOTU_DEV_ENTRY(model, data) \ 254 { \ 255 .match_flags = IEEE1394_MATCH_VENDOR_ID | \ 256 IEEE1394_MATCH_MODEL_ID | \ 257 IEEE1394_MATCH_SPECIFIER_ID, \ 258 .vendor_id = OUI_MOTU, \ 259 .model_id = model, \ 260 .specifier_id = OUI_MOTU, \ 261 .driver_data = (kernel_ulong_t)data, \ 262 } 263 264 static const struct ieee1394_device_id motu_id_table[] = { 265 SND_MOTU_DEV_ENTRY(0x101800, &motu_828mk2), 266 SND_MOTU_DEV_ENTRY(0x107800, &snd_motu_spec_traveler), 267 SND_MOTU_DEV_ENTRY(0x106800, &motu_828mk3), /* FireWire only. */ 268 SND_MOTU_DEV_ENTRY(0x100800, &motu_828mk3), /* Hybrid. */ 269 SND_MOTU_DEV_ENTRY(0x104800, &motu_audio_express), 270 { } 271 }; 272 MODULE_DEVICE_TABLE(ieee1394, motu_id_table); 273 274 static struct fw_driver motu_driver = { 275 .driver = { 276 .owner = THIS_MODULE, 277 .name = KBUILD_MODNAME, 278 .bus = &fw_bus_type, 279 }, 280 .probe = motu_probe, 281 .update = motu_bus_update, 282 .remove = motu_remove, 283 .id_table = motu_id_table, 284 }; 285 286 static int __init alsa_motu_init(void) 287 { 288 return driver_register(&motu_driver.driver); 289 } 290 291 static void __exit alsa_motu_exit(void) 292 { 293 driver_unregister(&motu_driver.driver); 294 } 295 296 module_init(alsa_motu_init); 297 module_exit(alsa_motu_exit); 298